LCOV - code coverage report
Current view: top level - src - attribs.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 145 169 85.8 %
Date: 2018-01-01 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       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/encryption near the beginning.
      70       73445 :                 p += to_base64(sb->compression, p);
      71       73445 :                 *p++ = ' ';
      72       73445 :                 p += to_base64(sb->encryption, p);
      73       73445 :                 *p++ = ' ';
      74             :         }
      75       95379 :         p += to_base64(statp->st_dev, p);
      76       95379 :         *p++ = ' ';
      77       95379 :         p += to_base64(statp->st_ino, p);
      78       95379 :         *p++ = ' ';
      79       95379 :         p += to_base64(statp->st_mode, p);
      80       95379 :         *p++ = ' ';
      81       95379 :         p += to_base64(statp->st_nlink, p);
      82       95379 :         *p++ = ' ';
      83       95379 :         p += to_base64(statp->st_uid, p);
      84       95379 :         *p++ = ' ';
      85       95379 :         p += to_base64(statp->st_gid, p);
      86       95379 :         *p++ = ' ';
      87       95379 :         p += to_base64(statp->st_rdev, p);
      88       95379 :         *p++ = ' ';
      89       95379 :         p += to_base64(statp->st_size, p);
      90       95379 :         *p++ = ' ';
      91             : #ifdef HAVE_WIN32
      92             :         p += to_base64(0, p); // place holder
      93             :         *p++ = ' ';
      94             :         p += to_base64(0, p); // place holder
      95             : #else
      96       95379 :         p += to_base64(statp->st_blksize, p);
      97       95379 :         *p++ = ' ';
      98       95379 :         p += to_base64(statp->st_blocks, p);
      99             : #endif
     100       95379 :         *p++ = ' ';
     101       95379 :         p += to_base64(statp->st_atime, p);
     102       95379 :         *p++ = ' ';
     103       95379 :         p += to_base64(statp->st_mtime, p);
     104       95379 :         *p++ = ' ';
     105       95379 :         p += to_base64(statp->st_ctime, p);
     106       95379 :         *p++ = ' ';
     107             : 
     108             : #ifdef HAVE_CHFLAGS
     109             :         // chflags is a FreeBSD function.
     110             :         p += to_base64(statp->st_flags, p);
     111             : #else
     112       95379 :         p += to_base64(0, p); // place holder
     113             : #endif
     114       95379 :         *p++ = ' ';
     115             : 
     116       95379 :         p += to_base64(sb->winattr, p);
     117             : 
     118       95379 :         if(sb->protocol1)
     119             :         {
     120             :                 // Protocol1 puts compression/encryption at the end.
     121       21934 :                 *p++ = ' ';
     122       21934 :                 p += to_base64(sb->compression, p);
     123       21934 :                 *p++ = ' ';
     124       21934 :                 p += to_base64(sb->encryption, p);
     125       21934 :                 *p++ = ' ';
     126       21934 :                 p += to_base64(sb->protocol1->salt, p);
     127             :         }
     128             : 
     129       95379 :         *p = 0;
     130             : 
     131       95379 :         sb->attr.len=p-sb->attr.buf;
     132             : 
     133       95379 :         return 0;
     134             : }
     135             : 
     136             : // Do casting according to unknown type to keep compiler happy.
     137             : #define plug(st, val) st = (__typeof__(st))(val)
     138             : 
     139             : // Decode a stat packet from base64 characters.
     140       80762 : void attribs_decode(struct sbuf *sb)
     141             : {
     142             :         static const char *p;
     143             :         static int64_t val;
     144             :         static struct stat *statp;
     145             : 
     146       80762 :         if(!(p=sb->attr.buf)) return;
     147       80762 :         statp=&sb->statp;
     148             : 
     149       80762 :         if(sb->protocol2)
     150             :         {
     151             :                 // Protocol1 does not have this field.
     152       57097 :                 p += from_base64(&val, p);
     153       57097 :                 sb->protocol2->index=val;
     154       57097 :                 p++;
     155             :                 // Compression for protocol2.
     156       57097 :                 p += from_base64(&val, p);
     157       57097 :                 sb->compression=val;
     158       57097 :                 p++;
     159             :                 // Encryption for protocol2.
     160       57097 :                 p += from_base64(&val, p);
     161       57097 :                 sb->encryption=val;
     162       57097 :                 p++;
     163             :         }
     164       80762 :         p += from_base64(&val, p);
     165       80762 :         plug(statp->st_dev, val);
     166       80762 :         p++;
     167       80762 :         p += from_base64(&val, p);
     168       80762 :         plug(statp->st_ino, val);
     169       80762 :         p++;
     170       80762 :         p += from_base64(&val, p);
     171       80762 :         plug(statp->st_mode, val);
     172       80762 :         p++;
     173       80762 :         p += from_base64(&val, p);
     174       80762 :         plug(statp->st_nlink, val);
     175       80762 :         p++;
     176       80762 :         p += from_base64(&val, p);
     177       80762 :         plug(statp->st_uid, val);
     178       80762 :         p++;
     179       80762 :         p += from_base64(&val, p);
     180       80762 :         plug(statp->st_gid, val);
     181       80762 :         p++;
     182       80762 :         p += from_base64(&val, p);
     183       80762 :         plug(statp->st_rdev, val);
     184       80762 :         p++;
     185       80762 :         p += from_base64(&val, p);
     186       80762 :         plug(statp->st_size, val);
     187       80762 :         p++;
     188       80762 :         p += from_base64(&val, p);
     189             : #ifdef HAVE_WIN32
     190             :         //   plug(statp->st_blksize, val);
     191             :         p++;
     192             :         p += from_base64(&val, p);
     193             :         //   plug(statp->st_blocks, val);
     194             : #else
     195       80762 :         plug(statp->st_blksize, val);
     196       80762 :         p++;
     197       80762 :         p += from_base64(&val, p);
     198       80762 :         plug(statp->st_blocks, val);
     199             : #endif
     200       80762 :         p++;
     201       80762 :         p += from_base64(&val, p);
     202       80762 :         plug(statp->st_atime, val);
     203       80762 :         p++;
     204       80762 :         p += from_base64(&val, p);
     205       80762 :         plug(statp->st_mtime, val);
     206       80762 :         p++;
     207       80762 :         p += from_base64(&val, p);
     208       80762 :         plug(statp->st_ctime, val);
     209             : 
     210             :         // FreeBSD user flags.
     211       80762 :         if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
     212             :         {
     213       80743 :                 p++;
     214       80743 :                 if(!*p) return;
     215       80743 :                 p += from_base64(&val, p);
     216             : #ifdef HAVE_CHFLAGS
     217             :                 plug(statp->st_flags, val);
     218             :         }
     219             :         else
     220             :         {
     221             :                 statp->st_flags  = 0;
     222             : #endif
     223             :         }
     224             : 
     225             :         // Look for winattr.
     226       80762 :         sb->winattr=0;
     227       80762 :         if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
     228             :         {
     229       80743 :                 p++;
     230       80743 :                 p += from_base64(&val, p);
     231       80743 :                 sb->winattr=val;
     232             :         }
     233             : 
     234       80762 :         if(sb->protocol1)
     235             :         {
     236       23665 :                 sb->compression=-1;
     237       23665 :                 sb->encryption=ENCRYPTION_UNSET;
     238             : 
     239             :                 // Compression for protocol1.
     240       23665 :                 if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
     241             :                 {
     242       23656 :                         p++;
     243       23656 :                         if(!*p) return;
     244       23656 :                         p += from_base64(&val, p);
     245       23656 :                         sb->compression=val;
     246             :                 }
     247             : 
     248             :                 // Encryption for protocol1.
     249       23665 :                 if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
     250             :                 {
     251       23656 :                         p++;
     252       23656 :                         if(!*p) return;
     253       23656 :                         p += from_base64(&val, p);
     254       23656 :                         sb->encryption=val;
     255             :                 }
     256             : 
     257             :                 // Salt for protocol1.
     258       23665 :                 if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
     259             :                 {
     260       23656 :                         p++;
     261       23656 :                         if(!*p) return;
     262       23656 :                         p += from_base64(&val, p);
     263       23656 :                         sb->protocol1->salt=val;
     264             :                 }
     265             :         }
     266             : }
     267             : 
     268          28 : int attribs_set_file_times(struct asfd *asfd,
     269             :         const char *path, struct stat *statp,
     270             :         struct cntr *cntr)
     271             : {
     272             :         int e;
     273             : 
     274             : #ifdef HAVE_WIN32
     275             :         // You (probably) cannot set times on Windows junction points.
     276             :         if(statp->st_rdev==WIN32_JUNCTION_POINT)
     277             :                 return 0;
     278             : 
     279             :         // The mingw64 utime() appears not to work on read-only files.
     280             :         // Use the utime() from bacula instead.
     281             :         struct utimbuf ut;
     282             :         ut.actime=statp->st_atime;
     283             :         ut.modtime=statp->st_mtime;
     284             :         e=win32_utime(path, &ut);
     285             : #elif HAVE_LUTIMES
     286             :         struct timeval t[2];
     287          28 :         t[0].tv_sec = statp->st_atime;
     288          28 :         t[0].tv_usec = 0;
     289          28 :         t[1].tv_sec = statp->st_mtime;
     290          28 :         t[1].tv_usec = 0;
     291          28 :         e=lutimes(path, t);
     292             : #else
     293             :         struct timespec ts[2];
     294             :         ts[0].tv_sec=statp->st_atime;
     295             :         ts[0].tv_nsec=0;
     296             :         ts[1].tv_sec=statp->st_mtime;
     297             :         ts[1].tv_nsec=0;
     298             :         e=utimensat(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW);
     299             : #endif
     300          28 :         if(e<0)
     301             :         {
     302             :                 struct berrno be;
     303           0 :                 berrno_init(&be);
     304           0 :                 logw(asfd, cntr, "Unable to set file times %s: ERR=%s\n",
     305           0 :                         path, berrno_bstrerror(&be, errno));
     306             :                 return -1;
     307             :         }
     308             :         return 0;
     309             : }
     310             : 
     311       17034 : uint64_t decode_file_no(struct iobuf *iobuf)
     312             : {
     313             :         int64_t val;
     314       17034 :         from_base64(&val, iobuf->buf);
     315       17034 :         return (uint64_t)val;
     316             : }
     317             : 
     318           0 : uint64_t decode_file_no_and_save_path(struct iobuf *iobuf, char **save_path)
     319             : {
     320             :         int64_t val;
     321           0 :         char *p=iobuf->buf;
     322           0 :         p+=from_base64(&val, iobuf->buf);
     323           0 :         *save_path=p+1;
     324           0 :         return (uint64_t)val;
     325             : }
     326             : 
     327          28 : int attribs_set(struct asfd *asfd, const char *path,
     328             :         struct stat *statp, uint64_t winattr, struct cntr *cntr)
     329             : {
     330             : #ifdef HAVE_WIN32
     331             :         win32_chmod(path, statp->st_mode, winattr);
     332             :         attribs_set_file_times(asfd, path, statp, cntr);
     333             :         return 0;
     334             : #endif
     335             : 
     336          28 :         if(lchown(path, statp->st_uid, statp->st_gid)<0)
     337             :         {
     338             :                 struct berrno be;
     339           0 :                 berrno_init(&be);
     340           0 :                 char msg[256]="";
     341             : 
     342           0 :                 snprintf(msg, sizeof(msg),
     343             :                         "Unable to set file owner of %s to %d:%d: ERR=%s",
     344             :                         path, statp->st_uid, statp->st_gid,
     345           0 :                         berrno_bstrerror(&be, errno));
     346             : 
     347           0 :                 if(errno==EPERM)
     348             :                 {
     349             :                         static int do_owner_warning=1;
     350           0 :                         if(getuid()!=0)
     351             :                         {
     352           0 :                                 if(!do_owner_warning)
     353             :                                         return -1;
     354             : 
     355           0 :                                 logw(asfd, cntr, "%s - possibly because you are not root. Will suppress subsequent messages of this type.\n", msg);
     356           0 :                                 do_owner_warning=0;
     357           0 :                                 return -1;
     358             :                         }
     359             :                 }
     360           0 :                 logw(asfd, cntr, "%s\n", msg);
     361             : 
     362           0 :                 return -1;
     363             :         }
     364             : 
     365             :         /* Watch out, a metadata restore will have cmd set to CMD_METADATA or
     366             :            CMD_ENC_META, but that is OK at the moment because we are not doing
     367             :            meta stuff on links. */
     368          28 :         if(S_ISLNK(statp->st_mode))
     369             :         {
     370           0 :                 if(attribs_set_file_times(asfd, path, statp, cntr))
     371             :                         return -1;
     372             :         }
     373             :         else
     374             :         {
     375          28 :                 if(chmod(path, statp->st_mode) < 0)
     376             :                 {
     377             :                         struct berrno be;
     378           0 :                         berrno_init(&be);
     379           0 :                         logw(asfd, cntr,
     380             :                                 "Unable to set file modes %s: ERR=%s\n",
     381           0 :                                 path, berrno_bstrerror(&be, errno));
     382             :                         return -1;
     383             :                 }
     384             : 
     385          28 :                 if(attribs_set_file_times(asfd, path, statp, cntr))
     386             :                         return -1;
     387             : #ifdef HAVE_CHFLAGS
     388             :                 /*
     389             :                  * FreeBSD user flags
     390             :                  *
     391             :                  * Note, this should really be done before the utime() above,
     392             :                  *  but if the immutable bit is set, it will make the utimes()
     393             :                  *  fail.
     394             :                  */
     395             :                 if(chflags(path, statp->st_flags)<0)
     396             :                 {
     397             :                         struct berrno be;
     398             :                         berrno_init(&be);
     399             :                         logw(asfd, cntr,
     400             :                                 "Unable to set file flags %s: ERR=%s\n",
     401             :                                 path, berrno_bstrerror(&be, errno));
     402             :                         return -1;
     403             :                 }
     404             : #endif
     405             :         }
     406             : 
     407             :         return 0;
     408             : }

Generated by: LCOV version 1.10