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: 2019-02-24 03:08:19 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             :         e=win32_utime(path, statp);
     282             : #elif HAVE_LUTIMES
     283             :         struct timeval t[2];
     284          28 :         t[0].tv_sec = statp->st_atime;
     285          28 :         t[0].tv_usec = 0;
     286          28 :         t[1].tv_sec = statp->st_mtime;
     287          28 :         t[1].tv_usec = 0;
     288          28 :         e=lutimes(path, t);
     289             : #else
     290             :         struct timespec ts[2];
     291             :         ts[0].tv_sec=statp->st_atime;
     292             :         ts[0].tv_nsec=0;
     293             :         ts[1].tv_sec=statp->st_mtime;
     294             :         ts[1].tv_nsec=0;
     295             :         e=utimensat(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW);
     296             : #endif
     297          28 :         if(e<0)
     298             :         {
     299             :                 struct berrno be;
     300           0 :                 berrno_init(&be);
     301           0 :                 logw(asfd, cntr, "Unable to set file times %s: ERR=%s\n",
     302           0 :                         path, berrno_bstrerror(&be, errno));
     303             :                 return -1;
     304             :         }
     305             :         return 0;
     306             : }
     307             : 
     308       17034 : uint64_t decode_file_no(struct iobuf *iobuf)
     309             : {
     310             :         int64_t val;
     311       17034 :         from_base64(&val, iobuf->buf);
     312       17034 :         return (uint64_t)val;
     313             : }
     314             : 
     315           0 : uint64_t decode_file_no_and_save_path(struct iobuf *iobuf, char **save_path)
     316             : {
     317             :         int64_t val;
     318           0 :         char *p=iobuf->buf;
     319           0 :         p+=from_base64(&val, iobuf->buf);
     320           0 :         *save_path=p+1;
     321           0 :         return (uint64_t)val;
     322             : }
     323             : 
     324          28 : int attribs_set(struct asfd *asfd, const char *path,
     325             :         struct stat *statp, uint64_t winattr, struct cntr *cntr)
     326             : {
     327             : #ifdef HAVE_WIN32
     328             :         win32_chmod(path, statp->st_mode, winattr);
     329             :         attribs_set_file_times(asfd, path, statp, cntr);
     330             :         return 0;
     331             : #endif
     332             : 
     333          28 :         if(lchown(path, statp->st_uid, statp->st_gid)<0)
     334             :         {
     335             :                 struct berrno be;
     336           0 :                 berrno_init(&be);
     337           0 :                 char msg[256]="";
     338             : 
     339           0 :                 snprintf(msg, sizeof(msg),
     340             :                         "Unable to set file owner of %s to %d:%d: ERR=%s",
     341             :                         path, statp->st_uid, statp->st_gid,
     342           0 :                         berrno_bstrerror(&be, errno));
     343             : 
     344           0 :                 if(errno==EPERM)
     345             :                 {
     346             :                         static int do_owner_warning=1;
     347           0 :                         if(getuid()!=0)
     348             :                         {
     349           0 :                                 if(!do_owner_warning)
     350             :                                         return -1;
     351             : 
     352           0 :                                 logw(asfd, cntr, "%s - possibly because you are not root. Will suppress subsequent messages of this type.\n", msg);
     353           0 :                                 do_owner_warning=0;
     354           0 :                                 return -1;
     355             :                         }
     356             :                 }
     357           0 :                 logw(asfd, cntr, "%s\n", msg);
     358             : 
     359           0 :                 return -1;
     360             :         }
     361             : 
     362             :         /* Watch out, a metadata restore will have cmd set to CMD_METADATA or
     363             :            CMD_ENC_META, but that is OK at the moment because we are not doing
     364             :            meta stuff on links. */
     365          28 :         if(S_ISLNK(statp->st_mode))
     366             :         {
     367           0 :                 if(attribs_set_file_times(asfd, path, statp, cntr))
     368             :                         return -1;
     369             :         }
     370             :         else
     371             :         {
     372          28 :                 if(chmod(path, statp->st_mode) < 0)
     373             :                 {
     374             :                         struct berrno be;
     375           0 :                         berrno_init(&be);
     376           0 :                         logw(asfd, cntr,
     377             :                                 "Unable to set file modes %s: ERR=%s\n",
     378           0 :                                 path, berrno_bstrerror(&be, errno));
     379             :                         return -1;
     380             :                 }
     381             : 
     382          28 :                 if(attribs_set_file_times(asfd, path, statp, cntr))
     383             :                         return -1;
     384             : #ifdef HAVE_CHFLAGS
     385             :                 /*
     386             :                  * FreeBSD user flags
     387             :                  *
     388             :                  * Note, this should really be done before the utime() above,
     389             :                  *  but if the immutable bit is set, it will make the utimes()
     390             :                  *  fail.
     391             :                  */
     392             :                 if(chflags(path, statp->st_flags)<0)
     393             :                 {
     394             :                         struct berrno be;
     395             :                         berrno_init(&be);
     396             :                         logw(asfd, cntr,
     397             :                                 "Unable to set file flags %s: ERR=%s\n",
     398             :                                 path, berrno_bstrerror(&be, errno));
     399             :                         return -1;
     400             :                 }
     401             : #endif
     402             :         }
     403             : 
     404             :         return 0;
     405             : }

Generated by: LCOV version 1.13