Line data Source code
1 : /*
2 : Bacula® - The Network Backup Solution
3 :
4 : Copyright (C) 2002-2009 Free Software Foundation Europe e.V.
5 :
6 : The main author of Bacula is Kern Sibbald, with contributions from
7 : many others, a complete list can be found in the file AUTHORS.
8 : This program is Free Software; you can redistribute it and/or
9 : modify it under the terms of version three of the GNU Affero General Public
10 : License as published by the Free Software Foundation and included
11 : in the file LICENSE.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU Affero General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : Bacula® is a registered trademark of Kern Sibbald.
24 : The licensor of Bacula is the Free Software Foundation Europe
25 : (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 : Switzerland, email:ftf@fsfeurope.org.
27 : */
28 : /*
29 : * Encode and decode standard Unix attributes and
30 : * Extended attributes for Win32 and
31 : * other non-Unix systems, ...
32 : */
33 : /*
34 : * Some of these functions come from src/findlib/attribs.c in bacula-5.0.3.
35 : * Hence, the copyright notice above is retained.
36 : * Graham Keeling, 2014
37 : */
38 :
39 : #include "burp.h"
40 : #include "attribs.h"
41 : #include "alloc.h"
42 : #include "base64.h"
43 : #include "berrno.h"
44 : #include "cmd.h"
45 : #include "cntr.h"
46 : #include "log.h"
47 : #include "sbuf.h"
48 :
49 : // Encode a stat structure into a base64 character string.
50 95378 : int attribs_encode(struct sbuf *sb)
51 : {
52 : static char *p;
53 : static struct stat *statp;
54 :
55 95378 : if(!sb->attr.buf)
56 : {
57 95253 : sb->attr.cmd=CMD_ATTRIBS; // should not be needed
58 95253 : if(!(sb->attr.buf=(char *)malloc_w(256, __func__)))
59 : return -1;
60 : }
61 95378 : p=sb->attr.buf;
62 95378 : statp=&sb->statp;
63 :
64 95378 : if(sb->protocol2)
65 : {
66 : // Protocol1 does not have this field.
67 73444 : p += to_base64(sb->protocol2->index, p);
68 73444 : *p++ = ' ';
69 : // Protocol2 puts compression near the beginning.
70 73444 : p += to_base64(sb->compression, p);
71 73444 : *p++ = ' ';
72 : // Protocol1 does not have this field.
73 73444 : p += to_base64(sb->protocol2->encryption, p);
74 73444 : *p++ = ' ';
75 : }
76 95378 : p += to_base64(statp->st_dev, p);
77 95378 : *p++ = ' ';
78 95378 : p += to_base64(statp->st_ino, p);
79 95378 : *p++ = ' ';
80 95378 : p += to_base64(statp->st_mode, p);
81 95378 : *p++ = ' ';
82 95378 : p += to_base64(statp->st_nlink, p);
83 95378 : *p++ = ' ';
84 95378 : p += to_base64(statp->st_uid, p);
85 95378 : *p++ = ' ';
86 95378 : p += to_base64(statp->st_gid, p);
87 95378 : *p++ = ' ';
88 95378 : p += to_base64(statp->st_rdev, p);
89 95378 : *p++ = ' ';
90 95378 : p += to_base64(statp->st_size, p);
91 95378 : *p++ = ' ';
92 : #ifdef HAVE_WIN32
93 : p += to_base64(0, p); // place holder
94 : *p++ = ' ';
95 : p += to_base64(0, p); // place holder
96 : #else
97 95378 : p += to_base64(statp->st_blksize, p);
98 95378 : *p++ = ' ';
99 95378 : p += to_base64(statp->st_blocks, p);
100 : #endif
101 95378 : *p++ = ' ';
102 95378 : p += to_base64(statp->st_atime, p);
103 95378 : *p++ = ' ';
104 95378 : p += to_base64(statp->st_mtime, p);
105 95378 : *p++ = ' ';
106 95378 : p += to_base64(statp->st_ctime, p);
107 95378 : *p++ = ' ';
108 :
109 : #ifdef HAVE_CHFLAGS
110 : // chflags is a FreeBSD function.
111 : p += to_base64(statp->st_flags, p);
112 : #else
113 95378 : p += to_base64(0, p); // place holder
114 : #endif
115 95378 : *p++ = ' ';
116 :
117 95378 : p += to_base64(sb->winattr, p);
118 :
119 95378 : if(sb->protocol1)
120 : {
121 : // Protocol1 puts compression at the end.
122 21934 : *p++ = ' ';
123 21934 : p += to_base64(sb->compression, p);
124 : }
125 :
126 95378 : *p = 0;
127 :
128 95378 : sb->attr.len=p-sb->attr.buf;
129 :
130 95378 : return 0;
131 : }
132 :
133 : // Do casting according to unknown type to keep compiler happy.
134 : #define plug(st, val) st = (__typeof__(st))(val)
135 :
136 : // Decode a stat packet from base64 characters.
137 80758 : void attribs_decode(struct sbuf *sb)
138 : {
139 : static const char *p;
140 : static int64_t val;
141 : static struct stat *statp;
142 :
143 80758 : if(!(p=sb->attr.buf)) return;
144 80758 : statp=&sb->statp;
145 :
146 80758 : if(sb->protocol2)
147 : {
148 : // Protocol1 does not have this field.
149 57093 : p += from_base64(&val, p);
150 57093 : sb->protocol2->index=val;
151 57093 : p++;
152 : // Compression for protocol2.
153 57093 : p += from_base64(&val, p);
154 57093 : sb->compression=val;
155 57093 : p++;
156 : // Protocol1 does not have this field.
157 57093 : p += from_base64(&val, p);
158 57093 : sb->protocol2->encryption=val;
159 57093 : p++;
160 : }
161 80758 : p += from_base64(&val, p);
162 80758 : plug(statp->st_dev, val);
163 80758 : p++;
164 80758 : p += from_base64(&val, p);
165 80758 : plug(statp->st_ino, val);
166 80758 : p++;
167 80758 : p += from_base64(&val, p);
168 80758 : plug(statp->st_mode, val);
169 80758 : p++;
170 80758 : p += from_base64(&val, p);
171 80758 : plug(statp->st_nlink, val);
172 80758 : p++;
173 80758 : p += from_base64(&val, p);
174 80758 : plug(statp->st_uid, val);
175 80758 : p++;
176 80758 : p += from_base64(&val, p);
177 80758 : plug(statp->st_gid, val);
178 80758 : p++;
179 80758 : p += from_base64(&val, p);
180 80758 : plug(statp->st_rdev, val);
181 80758 : p++;
182 80758 : p += from_base64(&val, p);
183 80758 : plug(statp->st_size, val);
184 80758 : p++;
185 80758 : p += from_base64(&val, p);
186 : #ifdef HAVE_WIN32
187 : // plug(statp->st_blksize, val);
188 : p++;
189 : p += from_base64(&val, p);
190 : // plug(statp->st_blocks, val);
191 : #else
192 80758 : plug(statp->st_blksize, val);
193 80758 : p++;
194 80758 : p += from_base64(&val, p);
195 80758 : plug(statp->st_blocks, val);
196 : #endif
197 80758 : p++;
198 80758 : p += from_base64(&val, p);
199 80758 : plug(statp->st_atime, val);
200 80758 : p++;
201 80758 : p += from_base64(&val, p);
202 80758 : plug(statp->st_mtime, val);
203 80758 : p++;
204 80758 : p += from_base64(&val, p);
205 80758 : plug(statp->st_ctime, val);
206 :
207 : // FreeBSD user flags.
208 80758 : if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
209 : {
210 80740 : p++;
211 80740 : if(!*p) return;
212 80739 : p += from_base64(&val, p);
213 : #ifdef HAVE_CHFLAGS
214 : plug(statp->st_flags, val);
215 : }
216 : else
217 : {
218 : statp->st_flags = 0;
219 : #endif
220 : }
221 :
222 : // Look for winattr.
223 80757 : if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
224 : {
225 80739 : p++;
226 80739 : p += from_base64(&val, p);
227 : }
228 : else
229 18 : val = 0;
230 80757 : sb->winattr=val;
231 :
232 : // Compression for protocol1.
233 80757 : if(sb->protocol1)
234 : {
235 23664 : if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
236 : {
237 23656 : p++;
238 23656 : if(!*p) return;
239 23656 : p += from_base64(&val, p);
240 23656 : sb->compression=val;
241 : }
242 : else
243 8 : sb->compression=-1;
244 : }
245 : }
246 :
247 28 : int attribs_set_file_times(struct asfd *asfd,
248 : const char *path, struct stat *statp,
249 : struct cntr *cntr)
250 : {
251 : int e;
252 : struct utimbuf ut;
253 28 : ut.actime=statp->st_atime;
254 28 : ut.modtime=statp->st_mtime;
255 :
256 : // The mingw64 utime() appears not to work on read-only files.
257 : // Use the utime() from bacula instead.
258 : #ifdef HAVE_WIN32
259 : //e=utime(path, &ut);
260 : e=win32_utime(path, &ut);
261 : #else
262 28 : e=utime(path, &ut);
263 : #endif
264 28 : if(e<0)
265 : {
266 : struct berrno be;
267 0 : berrno_init(&be);
268 0 : logw(asfd, cntr, "Unable to set file times %s: ERR=%s\n",
269 0 : path, berrno_bstrerror(&be, errno));
270 : return -1;
271 : }
272 : return 0;
273 : }
274 :
275 17034 : uint64_t decode_file_no(struct iobuf *iobuf)
276 : {
277 : int64_t val;
278 17034 : from_base64(&val, iobuf->buf);
279 17034 : return (uint64_t)val;
280 : }
281 :
282 0 : uint64_t decode_file_no_and_save_path(struct iobuf *iobuf, char **save_path)
283 : {
284 : int64_t val;
285 0 : char *p=iobuf->buf;
286 0 : p+=from_base64(&val, iobuf->buf);
287 0 : *save_path=p+1;
288 0 : return (uint64_t)val;
289 : }
290 :
291 : #ifdef HAVE_LUTIMES
292 : static int do_lutimes(const char *path, struct stat *statp)
293 : {
294 : struct timeval t[2];
295 0 : t[0].tv_sec = statp->st_atime;
296 0 : t[0].tv_usec = 0;
297 0 : t[1].tv_sec = statp->st_mtime;
298 0 : t[1].tv_usec = 0;
299 0 : return lutimes(path, t);
300 : }
301 : #endif
302 :
303 28 : int attribs_set(struct asfd *asfd, const char *path,
304 0 : struct stat *statp, uint64_t winattr, struct cntr *cntr)
305 : {
306 : #ifdef HAVE_WIN32
307 : win32_chmod(path, statp->st_mode, winattr);
308 : attribs_set_file_times(asfd, path, statp, cntr);
309 : return 0;
310 : #endif
311 :
312 28 : if(lchown(path, statp->st_uid, statp->st_gid)<0)
313 : {
314 : struct berrno be;
315 0 : berrno_init(&be);
316 0 : logw(asfd, cntr,
317 : "Unable to set file owner of %s to %d:%d: ERR=%s\n",
318 : path, statp->st_uid, statp->st_gid,
319 0 : berrno_bstrerror(&be, errno));
320 : return -1;
321 : }
322 :
323 : /* Watch out, a metadata restore will have cmd set to CMD_METADATA or
324 : CMD_ENC_META, but that is OK at the moment because we are not doing
325 : meta stuff on links. */
326 28 : if(S_ISLNK(statp->st_mode))
327 : {
328 : #ifdef HAVE_LUTIMES
329 0 : if(do_lutimes(path, statp)) {
330 : struct berrno be;
331 0 : berrno_init(&be);
332 0 : logw(asfd, cntr, "Unable to set lutimes %s: ERR=%s\n",
333 0 : path, berrno_bstrerror(&be, errno));
334 : return -1;
335 : }
336 : #endif
337 : }
338 : else
339 : {
340 28 : if(chmod(path, statp->st_mode) < 0)
341 : {
342 : struct berrno be;
343 0 : berrno_init(&be);
344 0 : logw(asfd, cntr,
345 : "Unable to set file modes %s: ERR=%s\n",
346 0 : path, berrno_bstrerror(&be, errno));
347 : return -1;
348 : }
349 :
350 28 : if(attribs_set_file_times(asfd, path, statp, cntr))
351 : return -1;
352 : #ifdef HAVE_CHFLAGS
353 : /*
354 : * FreeBSD user flags
355 : *
356 : * Note, this should really be done before the utime() above,
357 : * but if the immutable bit is set, it will make the utimes()
358 : * fail.
359 : */
360 : if(chflags(path, statp->st_flags)<0)
361 : {
362 : struct berrno be;
363 : berrno_init(&be);
364 : logw(asfd, cntr,
365 : "Unable to set file flags %s: ERR=%s\n",
366 : path, berrno_bstrerror(&be, errno));
367 : return -1;
368 : }
369 : #endif
370 : }
371 :
372 : return 0;
373 : }
|