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 : struct utimbuf ut;
253 :
254 : #ifdef HAVE_WIN32
255 : // You cannot set times on Windows junction points.
256 : if(statp->st_rdev==WIN32_JUNCTION_POINT)
257 : return 0;
258 : #endif
259 :
260 28 : ut.actime=statp->st_atime;
261 28 : ut.modtime=statp->st_mtime;
262 :
263 : // The mingw64 utime() appears not to work on read-only files.
264 : // Use the utime() from bacula instead.
265 : #ifdef HAVE_WIN32
266 : //e=utime(path, &ut);
267 : e=win32_utime(path, &ut);
268 : #else
269 28 : e=utime(path, &ut);
270 : #endif
271 28 : if(e<0)
272 : {
273 : struct berrno be;
274 0 : berrno_init(&be);
275 0 : logw(asfd, cntr, "Unable to set file times %s: ERR=%s\n",
276 0 : path, berrno_bstrerror(&be, errno));
277 : return -1;
278 : }
279 : return 0;
280 : }
281 :
282 17034 : uint64_t decode_file_no(struct iobuf *iobuf)
283 : {
284 : int64_t val;
285 17034 : from_base64(&val, iobuf->buf);
286 17034 : return (uint64_t)val;
287 : }
288 :
289 0 : uint64_t decode_file_no_and_save_path(struct iobuf *iobuf, char **save_path)
290 : {
291 : int64_t val;
292 0 : char *p=iobuf->buf;
293 0 : p+=from_base64(&val, iobuf->buf);
294 0 : *save_path=p+1;
295 0 : return (uint64_t)val;
296 : }
297 :
298 : #ifdef HAVE_LUTIMES
299 : static int do_lutimes(const char *path, struct stat *statp)
300 : {
301 : struct timeval t[2];
302 0 : t[0].tv_sec = statp->st_atime;
303 0 : t[0].tv_usec = 0;
304 0 : t[1].tv_sec = statp->st_mtime;
305 0 : t[1].tv_usec = 0;
306 0 : return lutimes(path, t);
307 : }
308 : #endif
309 :
310 28 : int attribs_set(struct asfd *asfd, const char *path,
311 0 : struct stat *statp, uint64_t winattr, struct cntr *cntr)
312 : {
313 : #ifdef HAVE_WIN32
314 : win32_chmod(path, statp->st_mode, winattr);
315 : attribs_set_file_times(asfd, path, statp, cntr);
316 : return 0;
317 : #endif
318 :
319 28 : if(lchown(path, statp->st_uid, statp->st_gid)<0)
320 : {
321 : struct berrno be;
322 0 : berrno_init(&be);
323 0 : logw(asfd, cntr,
324 : "Unable to set file owner of %s to %d:%d: ERR=%s\n",
325 : path, statp->st_uid, statp->st_gid,
326 0 : berrno_bstrerror(&be, errno));
327 : return -1;
328 : }
329 :
330 : /* Watch out, a metadata restore will have cmd set to CMD_METADATA or
331 : CMD_ENC_META, but that is OK at the moment because we are not doing
332 : meta stuff on links. */
333 28 : if(S_ISLNK(statp->st_mode))
334 : {
335 : #ifdef HAVE_LUTIMES
336 0 : if(do_lutimes(path, statp)) {
337 : struct berrno be;
338 0 : berrno_init(&be);
339 0 : logw(asfd, cntr, "Unable to set lutimes %s: ERR=%s\n",
340 0 : path, berrno_bstrerror(&be, errno));
341 : return -1;
342 : }
343 : #endif
344 : }
345 : else
346 : {
347 28 : if(chmod(path, statp->st_mode) < 0)
348 : {
349 : struct berrno be;
350 0 : berrno_init(&be);
351 0 : logw(asfd, cntr,
352 : "Unable to set file modes %s: ERR=%s\n",
353 0 : path, berrno_bstrerror(&be, errno));
354 : return -1;
355 : }
356 :
357 28 : if(attribs_set_file_times(asfd, path, statp, cntr))
358 : return -1;
359 : #ifdef HAVE_CHFLAGS
360 : /*
361 : * FreeBSD user flags
362 : *
363 : * Note, this should really be done before the utime() above,
364 : * but if the immutable bit is set, it will make the utimes()
365 : * fail.
366 : */
367 : if(chflags(path, statp->st_flags)<0)
368 : {
369 : struct berrno be;
370 : berrno_init(&be);
371 : logw(asfd, cntr,
372 : "Unable to set file flags %s: ERR=%s\n",
373 : path, berrno_bstrerror(&be, errno));
374 : return -1;
375 : }
376 : #endif
377 : }
378 :
379 : return 0;
380 : }
|