]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/attribs.c
Update doc
[bacula/bacula] / bacula / src / findlib / attribs.c
1 /*
2  *  Encode and decode standard Unix attributes and
3  *   Extended attributes for Win32 and
4  *   other non-Unix systems, or Unix systems with ACLs, ...
5  *
6  *    Kern Sibbald, October MMII
7  *
8  *   Version $Id$
9  *
10  */
11 /*
12    Copyright (C) 2000-2003 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "find.h"
33 #include "jcr.h"
34
35 #ifdef HAVE_CYGWIN
36
37 /* Forward referenced subroutines */
38 static
39 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, 
40                          int type, int stream, struct stat *statp,
41                          char *attribsEx, BFILE *ofd);
42 void unix_name_to_win32(POOLMEM **win32_name, char *name);
43 void win_error(void *jcr, char *prefix, POOLMEM *ofile);
44 HANDLE bget_handle(BFILE *bfd);
45 #endif
46
47 /* For old systems that don't have lchown() use chown() */
48 #ifndef HAVE_LCHOWN
49 #define lchown chown
50 #endif
51
52 /*=============================================================*/
53 /*                                                             */
54 /*             ***  A l l  S y s t e m s ***                   */
55 /*                                                             */
56 /*=============================================================*/
57
58
59 /* Encode a stat structure into a base64 character string */
60 void encode_stat(char *buf, struct stat *statp, uint32_t LinkFI)
61 {
62    char *p = buf;
63    /*
64     * NOTE: we should use rdev as major and minor device if
65     * it is a block or char device (S_ISCHR(statp->st_mode)
66     * or S_ISBLK(statp->st_mode)).  In all other cases,
67     * it is not used.   
68     *
69     */
70    p += to_base64((int64_t)statp->st_dev, p);
71    *p++ = ' ';                        /* separate fields with a space */
72    p += to_base64((int64_t)statp->st_ino, p);
73    *p++ = ' ';
74    p += to_base64((int64_t)statp->st_mode, p);
75    *p++ = ' ';
76    p += to_base64((int64_t)statp->st_nlink, p);
77    *p++ = ' ';
78    p += to_base64((int64_t)statp->st_uid, p);
79    *p++ = ' ';
80    p += to_base64((int64_t)statp->st_gid, p);
81    *p++ = ' ';
82    p += to_base64((int64_t)statp->st_rdev, p);
83    *p++ = ' ';
84    p += to_base64((int64_t)statp->st_size, p);
85    *p++ = ' ';
86    p += to_base64((int64_t)statp->st_blksize, p);
87    *p++ = ' ';
88    p += to_base64((int64_t)statp->st_blocks, p);
89    *p++ = ' ';
90    p += to_base64((int64_t)statp->st_atime, p);
91    *p++ = ' ';
92    p += to_base64((int64_t)statp->st_mtime, p);
93    *p++ = ' ';
94    p += to_base64((int64_t)statp->st_ctime, p);
95    *p++ = ' ';
96    p += to_base64((int64_t)LinkFI, p);
97
98 /* FreeBSD function */
99 #ifdef HAVE_CHFLAGS
100    *p++ = ' ';
101    p += to_base64((int64_t)statp->st_flags, p);
102 #endif
103    *p = 0;
104    return;
105 }
106
107
108
109 /* Decode a stat packet from base64 characters */
110 void
111 decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI) 
112 {
113    char *p = buf;
114    int64_t val;
115
116    p += from_base64(&val, p);
117    statp->st_dev = val;
118    p++;                               /* skip space */
119    p += from_base64(&val, p);
120    statp->st_ino = val;
121    p++;
122    p += from_base64(&val, p);
123    statp->st_mode = val;
124    p++;
125    p += from_base64(&val, p);
126    statp->st_nlink = val;
127    p++;
128    p += from_base64(&val, p);
129    statp->st_uid = val;
130    p++;
131    p += from_base64(&val, p);
132    statp->st_gid = val;
133    p++;
134    p += from_base64(&val, p);
135    statp->st_rdev = val;
136    p++;
137    p += from_base64(&val, p);
138    statp->st_size = val;
139    p++;
140    p += from_base64(&val, p);
141    statp->st_blksize = val;
142    p++;
143    p += from_base64(&val, p);
144    statp->st_blocks = val;
145    p++;
146    p += from_base64(&val, p);
147    statp->st_atime = val;
148    p++;
149    p += from_base64(&val, p);
150    statp->st_mtime = val;
151    p++;
152    p += from_base64(&val, p);
153    statp->st_ctime = val;
154    /* Optional FileIndex of hard linked file data */
155    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
156       p++;
157       p += from_base64(&val, p);
158       *LinkFI = (uint32_t)val;
159   } else {
160       *LinkFI = 0;
161   }
162
163 /* FreeBSD user flags */
164 #ifdef HAVE_CHFLAGS
165    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
166       p++;
167       p += from_base64(&val, p);
168       statp->st_flags = (uint32_t)val;
169   } else {
170       statp->st_flags  = 0;
171   }
172 #endif
173 }
174
175 /*
176  * Set file modes, permissions and times
177  *
178  *  fname is the original filename  
179  *  ofile is the output filename (may be in a different directory)
180  *
181  * Returns:  1 on success
182  *           0 on failure
183  */
184 int set_attributes(void *jcr, char *fname, char *ofile, char *lname, 
185                    int type, int stream, struct stat *statp,
186                    char *attribsEx, BFILE *ofd)
187 {
188    struct utimbuf ut;    
189    mode_t old_mask;
190    int stat = 1;
191
192 #ifdef HAVE_CYGWIN
193    if (set_win32_attributes(jcr, fname, ofile, lname, type, stream,
194                             statp, attribsEx, ofd)) {
195       return 1;
196    }
197    /*
198     * If Windows stuff failed, e.g. attempt to restore Unix file
199     *  to Windows, simply fall through and we will do it the     
200     *  universal way.
201     */
202 #endif
203
204    old_mask = umask(0);
205    if (is_bopen(ofd)) {
206       bclose(ofd);                    /* first close file */
207    }
208
209    ut.actime = statp->st_atime;
210    ut.modtime = statp->st_mtime;
211
212    /* ***FIXME**** optimize -- don't do if already correct */
213    /* 
214     * For link, change owner of link using lchown, but don't
215     *   try to do a chmod as that will update the file behind it.
216     */
217    if (type == FT_LNK) {
218       /* Change owner of link, not of real file */
219       if (lchown(ofile, statp->st_uid, statp->st_gid) < 0) {
220          Jmsg2(jcr, M_WARNING, 0, "Unable to set file owner %s: ERR=%s\n",
221             ofile, strerror(errno));
222          stat = 0;
223       }
224    } else {
225       if (chown(ofile, statp->st_uid, statp->st_gid) < 0) {
226          Jmsg2(jcr, M_WARNING, 0, "Unable to set file owner %s: ERR=%s\n",
227             ofile, strerror(errno));
228          stat = 0;
229       }
230       if (chmod(ofile, statp->st_mode) < 0) {
231          Jmsg2(jcr, M_WARNING, 0, "Unable to set file modes %s: ERR=%s\n",
232             ofile, strerror(errno));
233          stat = 0;
234       }
235
236       /* FreeBSD user flags */
237 #ifdef HAVE_CHFLAGS
238       if (chflags(ofile, statp->st_flags) < 0) {
239          Jmsg2(jcr, M_WARNING, 0, "Unable to set file flags %s: ERR=%s\n",
240             ofile, strerror(errno));
241          stat = 0;
242       }
243 #endif
244       /*
245        * Reset file times.
246        */
247       if (utime(ofile, &ut) < 0) {
248          Jmsg2(jcr, M_ERROR, 0, "Unable to set file times %s: ERR=%s\n",
249             ofile, strerror(errno));
250          stat = 0;
251       }
252    }
253    umask(old_mask);
254    return stat;
255 }
256
257
258 /*=============================================================*/
259 /*                                                             */
260 /*                 * * *  U n i x * * * *                      */
261 /*                                                             */
262 /*=============================================================*/
263
264 #ifndef HAVE_CYGWIN
265     
266 /*
267  * If you have a Unix system with extended attributes (e.g.
268  *  ACLs for Solaris, do it here.
269  */
270 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
271 {
272    *attribsEx = 0;                    /* no extended attributes */
273    return STREAM_UNIX_ATTRIBUTES;
274 }
275
276 void SetServicePrivileges(void *jcr)
277  { }
278
279
280 #endif
281
282
283
284 /*=============================================================*/
285 /*                                                             */
286 /*                 * * *  W i n 3 2 * * * *                    */
287 /*                                                             */
288 /*=============================================================*/
289
290 #ifdef HAVE_CYGWIN
291
292 int NoGetFileAttributesEx = 0;
293
294 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
295 {
296    char *p = attribsEx;
297    WIN32_FILE_ATTRIBUTE_DATA atts;
298    ULARGE_INTEGER li;
299
300    attribsEx[0] = 0;                  /* no extended attributes */
301
302    if (NoGetFileAttributesEx) {
303       return STREAM_UNIX_ATTRIBUTES;
304    }
305
306    unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
307    if (!GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
308                             (LPVOID)&atts)) {
309       win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
310       return STREAM_WIN32_ATTRIBUTES;
311    }
312
313    p += to_base64((uint64_t)atts.dwFileAttributes, p);
314    *p++ = ' ';                        /* separate fields with a space */
315    li.LowPart = atts.ftCreationTime.dwLowDateTime;
316    li.HighPart = atts.ftCreationTime.dwHighDateTime;
317    p += to_base64((uint64_t)li.QuadPart, p);
318    *p++ = ' ';
319    li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
320    li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
321    p += to_base64((uint64_t)li.QuadPart, p);
322    *p++ = ' ';
323    li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
324    li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
325    p += to_base64((uint64_t)li.QuadPart, p);
326    *p++ = ' ';
327    p += to_base64((uint64_t)atts.nFileSizeHigh, p);
328    *p++ = ' ';
329    p += to_base64((uint64_t)atts.nFileSizeLow, p);
330    *p = 0;
331    return STREAM_WIN32_ATTRIBUTES;
332 }
333
334 /* Define attributes that are legal to set with SetFileAttributes() */
335 #define SET_ATTRS ( \
336          FILE_ATTRIBUTE_ARCHIVE| \
337          FILE_ATTRIBUTE_HIDDEN| \
338          FILE_ATTRIBUTE_NORMAL| \
339          FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
340          FILE_ATTRIBUTE_OFFLINE| \
341          FILE_ATTRIBUTE_READONLY| \
342          FILE_ATTRIBUTE_SYSTEM| \
343          FILE_ATTRIBUTE_TEMPORARY)
344
345
346 /*
347  * Set Extended File Attributes for Win32
348  *
349  *  fname is the original filename  
350  *  ofile is the output filename (may be in a different directory)
351  *
352  * Returns:  1 on success
353  *           0 on failure
354  */
355 static
356 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, 
357                          int type, int stream, struct stat *statp,
358                          char *attribsEx, BFILE *ofd)
359 {
360    char *p = attribsEx;
361    int64_t val;
362    WIN32_FILE_ATTRIBUTE_DATA atts;
363    ULARGE_INTEGER li;
364    int stat;
365    POOLMEM *win32_ofile;
366
367    if (!p || !*p) {                   /* we should have attributes */
368       Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", ofile, ofd->fid);
369       if (is_bopen(ofd)) {
370          bclose(ofd);
371       }
372       return 0;
373    } else {
374       Dmsg2(100, "Attribs %s = %s\n", ofile, attribsEx);
375    }
376
377    p += from_base64(&val, p);
378    atts.dwFileAttributes = val;
379    p++;                               /* skip space */
380    p += from_base64(&val, p);
381    li.QuadPart = val;
382    atts.ftCreationTime.dwLowDateTime = li.LowPart;
383    atts.ftCreationTime.dwHighDateTime = li.HighPart;
384    p++;                               /* skip space */
385    p += from_base64(&val, p);
386    li.QuadPart = val;
387    atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
388    atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
389    p++;                               /* skip space */
390    p += from_base64(&val, p);
391    li.QuadPart = val;
392    atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
393    atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
394    p++;   
395    p += from_base64(&val, p);
396    atts.nFileSizeHigh = val;
397    p++;
398    p += from_base64(&val, p);
399    atts.nFileSizeLow = val;
400
401    /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
402
403    /* Convert to Windows path format */
404    win32_ofile = get_pool_memory(PM_FNAME);
405    unix_name_to_win32(&win32_ofile, ofile);
406
407    if (!is_bopen(ofd)) {
408       Dmsg1(100, "File not open: %s\n", ofile);
409       bopen(ofd, ofile, O_WRONLY|O_BINARY, 0);   /* attempt to open the file */
410    }
411
412    if (is_bopen(ofd)) {
413       Dmsg1(100, "SetFileTime %s\n", ofile);
414       stat = SetFileTime(bget_handle(ofd),
415                          &atts.ftCreationTime,
416                          &atts.ftLastAccessTime,
417                          &atts.ftLastWriteTime);
418       if (stat != 1) {
419          win_error(jcr, "SetFileTime:", win32_ofile);
420       }
421       bclose(ofd);
422    }
423
424    Dmsg1(100, "SetFileAtts %s\n", ofile);
425    stat = SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS);
426    if (stat != 1) {
427       win_error(jcr, "SetFileAttributes:", win32_ofile);
428    }
429    free_pool_memory(win32_ofile);
430    return 1;
431 }
432
433 void win_error(void *vjcr, char *prefix, POOLMEM *win32_ofile)
434 {
435    JCR *jcr = (JCR *)vjcr; 
436    DWORD lerror = GetLastError();
437    LPTSTR msg;
438    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
439                  FORMAT_MESSAGE_FROM_SYSTEM,
440                  NULL,
441                  lerror,
442                  0,
443                  (LPTSTR)&msg,
444                  0,
445                  NULL);
446    Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
447    strip_trailing_junk(msg);
448    Jmsg3(jcr, M_INFO, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
449    LocalFree(msg);
450 }
451
452 void win_error(void *vjcr, char *prefix, DWORD lerror)
453 {
454    JCR *jcr = (JCR *)vjcr; 
455    LPTSTR msg;
456    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
457                  FORMAT_MESSAGE_FROM_SYSTEM,
458                  NULL,
459                  lerror,
460                  0,
461                  (LPTSTR)&msg,
462                  0,
463                  NULL);
464    strip_trailing_junk(msg);
465    if (jcr) {
466       Jmsg2(jcr, M_INFO, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
467    } else {
468       MessageBox(NULL, msg, prefix, MB_OK);
469    }
470    LocalFree(msg);
471 }
472
473
474 /* Cygwin API definition */
475 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
476
477 void unix_name_to_win32(POOLMEM **win32_name, char *name)
478 {
479    /* One extra byte should suffice, but we double it */
480    *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
481    cygwin_conv_to_win32_path(name, *win32_name);
482 }
483
484 /*
485  * Setup privileges we think we will need.  We probably do not need
486  *  the SE_SECURITY_NAME, but since nothing seems to be working,
487  *  we get it hoping to fix the problems.
488  */
489 void SetServicePrivileges(void *jcr)
490 {
491     HANDLE hToken;
492     TOKEN_PRIVILEGES tkp, tkpPrevious;
493     DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
494     DWORD lerror;
495     // Get a token for this process. 
496     if (!OpenProcessToken(GetCurrentProcess(), 
497             TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
498        win_error(jcr, "OpenProcessToken", GetLastError());
499        /* Forge on anyway */
500     } 
501
502     // Get the LUID for the security privilege. 
503     if (!LookupPrivilegeValue(NULL, SE_SECURITY_NAME,  &tkp.Privileges[0].Luid)) {
504        win_error(jcr, "LookupPrivilegeValue", GetLastError());
505     }
506
507     tkp.PrivilegeCount = 1;
508     tkp.Privileges[0].Attributes = 0;
509     /* Get the privilege */
510     AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES),
511                             &tkpPrevious, &cbPrevious);
512     lerror = GetLastError();
513     if (lerror != ERROR_SUCCESS) {
514        win_error(jcr, "AdjustTokenPrivileges get SECURITY_NAME", lerror);
515     } 
516
517     tkpPrevious.PrivilegeCount = 1;
518     tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); 
519        
520     /* Set the security privilege for this process. */
521     AdjustTokenPrivileges(hToken, FALSE, &tkpPrevious, sizeof(TOKEN_PRIVILEGES),
522                             (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL);
523     lerror = GetLastError();
524     if (lerror != ERROR_SUCCESS) {
525        win_error(jcr, "AdjustTokenPrivileges set SECURITY_NAME", lerror);
526     } 
527
528     // Get the LUID for the backup privilege. 
529     if (!LookupPrivilegeValue(NULL, SE_BACKUP_NAME,  &tkp.Privileges[0].Luid)) {
530        win_error(jcr, "LookupPrivilegeValue", GetLastError());
531     }
532
533     tkp.PrivilegeCount = 1;
534     tkp.Privileges[0].Attributes = 0;
535     /* Get the current privilege */
536     AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES),
537                             &tkpPrevious, &cbPrevious);
538     lerror = GetLastError();
539     if (lerror != ERROR_SUCCESS) {
540        win_error(jcr, "AdjustTokenPrivileges get BACKUP_NAME", lerror);
541     } 
542
543     tkpPrevious.PrivilegeCount = 1;
544     tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); 
545        
546     /* Set the backup privilege for this process. */
547     AdjustTokenPrivileges(hToken, FALSE, &tkpPrevious, sizeof(TOKEN_PRIVILEGES),
548                             (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL);  
549     lerror = GetLastError();
550     if (lerror != ERROR_SUCCESS) {
551        win_error(jcr, "AdjustTokenPrivileges set BACKUP_NAME", lerror);
552     } 
553      
554     // Get the LUID for the restore privilege. 
555     if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tkp.Privileges[0].Luid)) {
556        win_error(jcr, "LookupPrivilegeValue", GetLastError());
557     }
558
559     tkp.PrivilegeCount = 1;
560     tkp.Privileges[0].Attributes = 0;
561     /* Get the privilege */
562     AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES),
563                             &tkpPrevious, &cbPrevious);
564     lerror = GetLastError();
565     if (lerror != ERROR_SUCCESS) {
566        win_error(jcr, "AdjustTokenPrivileges get RESTORE_NAME", lerror);
567     } 
568
569     tkpPrevious.PrivilegeCount = 1;
570     tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); 
571        
572     /* Set the security privilege for this process. */
573     AdjustTokenPrivileges(hToken, FALSE, &tkpPrevious, sizeof(TOKEN_PRIVILEGES),
574                             (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL);
575     lerror = GetLastError();
576     if (lerror != ERROR_SUCCESS) {
577        win_error(jcr, "AdjustTokenPrivileges set RESTORE_NAME", lerror);
578     } 
579     CloseHandle(hToken);
580 }
581
582 //     MessageBox(NULL, "Get restore priv failed: AdjustTokePrivileges", "restore", MB_OK);
583
584 #endif  /* HAVE_CYGWIN */