LCOV - code coverage report
Current view: top level - src - attribs.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 128 152 84.2 %
Date: 2016-09-30 Functions: 5 6 83.3 %

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

Generated by: LCOV version 1.10