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 95379 : int attribs_encode(struct sbuf *sb)
51 : {
52 : static char *p;
53 : static struct stat *statp;
54 :
55 95379 : if(!sb->attr.buf)
56 : {
57 95254 : sb->attr.cmd=CMD_ATTRIBS; // should not be needed
58 95254 : if(!(sb->attr.buf=(char *)malloc_w(256, __func__)))
59 : return -1;
60 : }
61 95379 : p=sb->attr.buf;
62 95379 : statp=&sb->statp;
63 :
64 95379 : if(sb->protocol2)
65 : {
66 : // Protocol1 does not have this field.
67 73445 : p += to_base64(sb->protocol2->index, p);
68 73445 : *p++ = ' ';
69 : // Protocol2 puts compression near the beginning.
70 73445 : p += to_base64(sb->compression, p);
71 73445 : *p++ = ' ';
72 : // Protocol1 does not have this field.
73 73445 : p += to_base64(sb->protocol2->encryption, p);
74 73445 : *p++ = ' ';
75 : }
76 95379 : p += to_base64(statp->st_dev, p);
77 95379 : *p++ = ' ';
78 95379 : p += to_base64(statp->st_ino, p);
79 95379 : *p++ = ' ';
80 95379 : p += to_base64(statp->st_mode, p);
81 95379 : *p++ = ' ';
82 95379 : p += to_base64(statp->st_nlink, p);
83 95379 : *p++ = ' ';
84 95379 : p += to_base64(statp->st_uid, p);
85 95379 : *p++ = ' ';
86 95379 : p += to_base64(statp->st_gid, p);
87 95379 : *p++ = ' ';
88 95379 : p += to_base64(statp->st_rdev, p);
89 95379 : *p++ = ' ';
90 95379 : p += to_base64(statp->st_size, p);
91 95379 : *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 95379 : p += to_base64(statp->st_blksize, p);
98 95379 : *p++ = ' ';
99 95379 : p += to_base64(statp->st_blocks, p);
100 : #endif
101 95379 : *p++ = ' ';
102 95379 : p += to_base64(statp->st_atime, p);
103 95379 : *p++ = ' ';
104 95379 : p += to_base64(statp->st_mtime, p);
105 95379 : *p++ = ' ';
106 95379 : p += to_base64(statp->st_ctime, p);
107 95379 : *p++ = ' ';
108 :
109 : #ifdef HAVE_CHFLAGS
110 : // chflags is a FreeBSD function.
111 : p += to_base64(statp->st_flags, p);
112 : #else
113 95379 : p += to_base64(0, p); // place holder
114 : #endif
115 95379 : *p++ = ' ';
116 :
117 95379 : p += to_base64(sb->winattr, p);
118 :
119 95379 : 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 95379 : *p = 0;
127 :
128 95379 : sb->attr.len=p-sb->attr.buf;
129 :
130 95379 : 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 80760 : void attribs_decode(struct sbuf *sb)
138 : {
139 : static const char *p;
140 : static int64_t val;
141 : static struct stat *statp;
142 :
143 80760 : if(!(p=sb->attr.buf)) return;
144 80760 : statp=&sb->statp;
145 :
146 80760 : if(sb->protocol2)
147 : {
148 : // Protocol1 does not have this field.
149 57095 : p += from_base64(&val, p);
150 57095 : sb->protocol2->index=val;
151 57095 : p++;
152 : // Compression for protocol2.
153 57095 : p += from_base64(&val, p);
154 57095 : sb->compression=val;
155 57095 : p++;
156 : // Protocol1 does not have this field.
157 57095 : p += from_base64(&val, p);
158 57095 : sb->protocol2->encryption=val;
159 57095 : p++;
160 : }
161 80760 : p += from_base64(&val, p);
162 80760 : plug(statp->st_dev, val);
163 80760 : p++;
164 80760 : p += from_base64(&val, p);
165 80760 : plug(statp->st_ino, val);
166 80760 : p++;
167 80760 : p += from_base64(&val, p);
168 80760 : plug(statp->st_mode, val);
169 80760 : p++;
170 80760 : p += from_base64(&val, p);
171 80760 : plug(statp->st_nlink, val);
172 80760 : p++;
173 80760 : p += from_base64(&val, p);
174 80760 : plug(statp->st_uid, val);
175 80760 : p++;
176 80760 : p += from_base64(&val, p);
177 80760 : plug(statp->st_gid, val);
178 80760 : p++;
179 80760 : p += from_base64(&val, p);
180 80760 : plug(statp->st_rdev, val);
181 80760 : p++;
182 80760 : p += from_base64(&val, p);
183 80760 : plug(statp->st_size, val);
184 80760 : p++;
185 80760 : 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 80760 : plug(statp->st_blksize, val);
193 80760 : p++;
194 80760 : p += from_base64(&val, p);
195 80760 : plug(statp->st_blocks, val);
196 : #endif
197 80760 : p++;
198 80760 : p += from_base64(&val, p);
199 80760 : plug(statp->st_atime, val);
200 80760 : p++;
201 80760 : p += from_base64(&val, p);
202 80760 : plug(statp->st_mtime, val);
203 80760 : p++;
204 80760 : p += from_base64(&val, p);
205 80760 : plug(statp->st_ctime, val);
206 :
207 : // FreeBSD user flags.
208 80760 : if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
209 : {
210 80741 : p++;
211 80741 : if(!*p) return;
212 80741 : 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 80760 : if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
224 : {
225 80741 : p++;
226 80741 : p += from_base64(&val, p);
227 : }
228 : else
229 19 : val = 0;
230 80760 : sb->winattr=val;
231 :
232 : // Compression for protocol1.
233 80760 : if(sb->protocol1)
234 : {
235 23665 : 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 9 : 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 :
253 : #ifdef HAVE_WIN32
254 : // You (probably) cannot set times on Windows junction points.
255 : if(statp->st_rdev==WIN32_JUNCTION_POINT)
256 : return 0;
257 :
258 : // The mingw64 utime() appears not to work on read-only files.
259 : // Use the utime() from bacula instead.
260 : struct utimbuf ut;
261 : ut.actime=statp->st_atime;
262 : ut.modtime=statp->st_mtime;
263 : e=win32_utime(path, &ut);
264 : #elif HAVE_LUTIMES
265 : struct timeval t[2];
266 28 : t[0].tv_sec = statp->st_atime;
267 28 : t[0].tv_usec = 0;
268 28 : t[1].tv_sec = statp->st_mtime;
269 28 : t[1].tv_usec = 0;
270 28 : e=lutimes(path, t);
271 : #else
272 : struct timespec ts[2];
273 : ts[0].tv_sec=statp->st_atime;
274 : ts[0].tv_nsec=0;
275 : ts[1].tv_sec=statp->st_mtime;
276 : ts[1].tv_nsec=0;
277 : e=utimensat(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW);
278 : #endif
279 28 : if(e<0)
280 : {
281 : struct berrno be;
282 0 : berrno_init(&be);
283 0 : logw(asfd, cntr, "Unable to set file times %s: ERR=%s\n",
284 0 : path, berrno_bstrerror(&be, errno));
285 : return -1;
286 : }
287 : return 0;
288 : }
289 :
290 17034 : uint64_t decode_file_no(struct iobuf *iobuf)
291 : {
292 : int64_t val;
293 17034 : from_base64(&val, iobuf->buf);
294 17034 : return (uint64_t)val;
295 : }
296 :
297 0 : uint64_t decode_file_no_and_save_path(struct iobuf *iobuf, char **save_path)
298 : {
299 : int64_t val;
300 0 : char *p=iobuf->buf;
301 0 : p+=from_base64(&val, iobuf->buf);
302 0 : *save_path=p+1;
303 0 : return (uint64_t)val;
304 : }
305 :
306 28 : int attribs_set(struct asfd *asfd, const char *path,
307 : struct stat *statp, uint64_t winattr, struct cntr *cntr)
308 : {
309 : #ifdef HAVE_WIN32
310 : win32_chmod(path, statp->st_mode, winattr);
311 : attribs_set_file_times(asfd, path, statp, cntr);
312 : return 0;
313 : #endif
314 :
315 28 : if(lchown(path, statp->st_uid, statp->st_gid)<0)
316 : {
317 : struct berrno be;
318 0 : berrno_init(&be);
319 0 : logw(asfd, cntr,
320 : "Unable to set file owner of %s to %d:%d: ERR=%s\n",
321 : path, statp->st_uid, statp->st_gid,
322 0 : berrno_bstrerror(&be, errno));
323 : return -1;
324 : }
325 :
326 : /* Watch out, a metadata restore will have cmd set to CMD_METADATA or
327 : CMD_ENC_META, but that is OK at the moment because we are not doing
328 : meta stuff on links. */
329 28 : if(S_ISLNK(statp->st_mode))
330 : {
331 0 : if(attribs_set_file_times(asfd, path, statp, cntr))
332 : return -1;
333 : }
334 : else
335 : {
336 28 : if(chmod(path, statp->st_mode) < 0)
337 : {
338 : struct berrno be;
339 0 : berrno_init(&be);
340 0 : logw(asfd, cntr,
341 : "Unable to set file modes %s: ERR=%s\n",
342 0 : path, berrno_bstrerror(&be, errno));
343 : return -1;
344 : }
345 :
346 28 : if(attribs_set_file_times(asfd, path, statp, cntr))
347 : return -1;
348 : #ifdef HAVE_CHFLAGS
349 : /*
350 : * FreeBSD user flags
351 : *
352 : * Note, this should really be done before the utime() above,
353 : * but if the immutable bit is set, it will make the utimes()
354 : * fail.
355 : */
356 : if(chflags(path, statp->st_flags)<0)
357 : {
358 : struct berrno be;
359 : berrno_init(&be);
360 : logw(asfd, cntr,
361 : "Unable to set file flags %s: ERR=%s\n",
362 : path, berrno_bstrerror(&be, errno));
363 : return -1;
364 : }
365 : #endif
366 : }
367 :
368 : return 0;
369 : }
|