Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (17 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/libbb/copy_file.c

    r821 r1765  
    44 *
    55 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
     6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
    67 *
    78 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     
    1011
    1112#include "libbb.h"
    12 #include <utime.h>
    13 #include <errno.h>
    14 
     13
     14// POSIX: if exists and -i, ask (w/o -i assume yes).
     15// Then open w/o EXCL (yes, not unlink!).
     16// If open still fails and -f, try unlink, then try open again.
     17// Result: a mess:
     18// If dest is a softlink, we overwrite softlink's destination!
     19// (or fail, if it points to dir/nonexistent location/etc).
     20// This is strange, but POSIX-correct.
     21// coreutils cp has --remove-destination to override this...
     22//
     23// NB: we have special code which still allows for "cp file /dev/node"
     24// to work POSIX-ly (the only realistic case where it makes sense)
     25
     26#define DO_POSIX_CP 0  /* 1 - POSIX behavior, 0 - safe behavior */
     27
     28// errno must be set to relevant value ("why we cannot create dest?")
     29// for POSIX mode to give reasonable error message
     30static int ask_and_unlink(const char *dest, int flags)
     31{
     32#if DO_POSIX_CP
     33    if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
     34        // Either it exists, or the *path* doesnt exist
     35        bb_perror_msg("cannot create '%s'", dest);
     36        return -1;
     37    }
     38#endif
     39    // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
     40    // "cannot create" msg, we want unlink to be done (silently unless -i).
     41
     42    // TODO: maybe we should do it only if ctty is present?
     43    if (flags & FILEUTILS_INTERACTIVE) {
     44        // We would not do POSIX insanity. -i asks,
     45        // then _unlinks_ the offender. Presto.
     46        // (No "opening without O_EXCL", no "unlink only if -f")
     47        // Or else we will end up having 3 open()s!
     48        fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
     49        if (!bb_ask_confirmation())
     50            return 0; // not allowed to overwrite
     51    }
     52    if (unlink(dest) < 0) {
     53        bb_perror_msg("cannot remove '%s'", dest);
     54        return -1; // error
     55    }
     56    return 1; // ok (to try again)
     57}
     58
     59/* Return:
     60 * -1 error, copy not made
     61 *  0 copy is made or user answered "no" in interactive mode
     62 *    (failures to preserve mode/owner/times are not reported in exit code)
     63 */
    1564int copy_file(const char *source, const char *dest, int flags)
    1665{
     66    /* This is a recursive function, try to minimize stack usage */
     67    /* NB: each struct stat is ~100 bytes */
    1768    struct stat source_stat;
    1869    struct stat dest_stat;
    19     int dest_exists = 0;
    20     int status = 0;
    21 
    22     if ((!(flags & FILEUTILS_DEREFERENCE) &&
    23             lstat(source, &source_stat) < 0) ||
    24             ((flags & FILEUTILS_DEREFERENCE) &&
    25              stat(source, &source_stat) < 0)) {
    26         bb_perror_msg("%s", source);
     70    signed char retval = 0;
     71    signed char dest_exists = 0;
     72    signed char ovr;
     73
     74#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
     75
     76    if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
     77        // This may be a dangling symlink.
     78        // Making [sym]links to dangling symlinks works, so...
     79        if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
     80            goto make_links;
     81        bb_perror_msg("cannot stat '%s'", source);
    2782        return -1;
    2883    }
     
    3085    if (lstat(dest, &dest_stat) < 0) {
    3186        if (errno != ENOENT) {
    32             bb_perror_msg("unable to stat `%s'", dest);
     87            bb_perror_msg("cannot stat '%s'", dest);
    3388            return -1;
    3489        }
    3590    } else {
    36         if (source_stat.st_dev == dest_stat.st_dev &&
    37             source_stat.st_ino == dest_stat.st_ino)
    38         {
    39             bb_error_msg("`%s' and `%s' are the same file", source, dest);
     91        if (source_stat.st_dev == dest_stat.st_dev
     92         && source_stat.st_ino == dest_stat.st_ino
     93        ) {
     94            bb_error_msg("'%s' and '%s' are the same file", source, dest);
    4095            return -1;
    4196        }
    4297        dest_exists = 1;
    4398    }
     99
     100#if ENABLE_SELINUX
     101    if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
     102        security_context_t con;
     103        if (lgetfilecon(source, &con) >= 0) {
     104            if (setfscreatecon(con) < 0) {
     105                bb_perror_msg("cannot set setfscreatecon %s", con);
     106                freecon(con);
     107                return -1;
     108            }
     109        } else if (errno == ENOTSUP || errno == ENODATA) {
     110            setfscreatecon_or_die(NULL);
     111        } else {
     112            bb_perror_msg("cannot lgetfilecon %s", source);
     113            return -1;
     114        }
     115    }
     116#endif
    44117
    45118    if (S_ISDIR(source_stat.st_mode)) {
    46119        DIR *dp;
     120        const char *tp;
    47121        struct dirent *d;
    48122        mode_t saved_umask = 0;
    49123
    50124        if (!(flags & FILEUTILS_RECUR)) {
    51             bb_error_msg("%s: omitting directory", source);
    52             return -1;
    53         }
    54 
    55         /* Create DEST.  */
     125            bb_error_msg("omitting directory '%s'", source);
     126            return -1;
     127        }
     128
     129        /* Did we ever create source ourself before? */
     130        tp = is_in_ino_dev_hashtable(&source_stat);
     131        if (tp) {
     132            /* We did! it's a recursion! man the lifeboats... */
     133            bb_error_msg("recursion detected, omitting directory '%s'",
     134                    source);
     135            return -1;
     136        }
     137
     138        /* Create DEST */
    56139        if (dest_exists) {
    57140            if (!S_ISDIR(dest_stat.st_mode)) {
    58                 bb_error_msg("`%s' is not a directory", dest);
    59                 return -1;
    60             }
     141                bb_error_msg("target '%s' is not a directory", dest);
     142                return -1;
     143            }
     144            /* race here: user can substitute a symlink between
     145             * this check and actual creation of files inside dest */
    61146        } else {
    62147            mode_t mode;
     
    66151            if (!(flags & FILEUTILS_PRESERVE_STATUS))
    67152                mode = source_stat.st_mode & ~saved_umask;
     153            /* Allow owner to access new dir (at least for now) */
    68154            mode |= S_IRWXU;
    69 
    70155            if (mkdir(dest, mode) < 0) {
    71156                umask(saved_umask);
    72                 bb_perror_msg("cannot create directory `%s'", dest);
    73                 return -1;
    74             }
    75 
     157                bb_perror_msg("cannot create directory '%s'", dest);
     158                return -1;
     159            }
    76160            umask(saved_umask);
    77         }
    78 
    79         /* Recursively copy files in SOURCE.  */
    80         if ((dp = bb_opendir(source)) == NULL) {
    81             status = -1;
    82             goto preserve_status;
     161            /* need stat info for add_to_ino_dev_hashtable */
     162            if (lstat(dest, &dest_stat) < 0) {
     163                bb_perror_msg("cannot stat '%s'", dest);
     164                return -1;
     165            }
     166        }
     167        /* remember (dev,inode) of each created dir.
     168         * NULL: name is not remembered */
     169        add_to_ino_dev_hashtable(&dest_stat, NULL);
     170
     171        /* Recursively copy files in SOURCE */
     172        dp = opendir(source);
     173        if (dp == NULL) {
     174            retval = -1;
     175            goto preserve_mode_ugid_time;
    83176        }
    84177
     
    87180
    88181            new_source = concat_subpath_file(source, d->d_name);
    89             if(new_source == NULL)
     182            if (new_source == NULL)
    90183                continue;
    91184            new_dest = concat_path_file(dest, d->d_name);
    92185            if (copy_file(new_source, new_dest, flags) < 0)
    93                 status = -1;
     186                retval = -1;
    94187            free(new_source);
    95188            free(new_dest);
    96189        }
    97         /* closedir have only EBADF error, but "dp" not changes */
    98190        closedir(dp);
    99191
    100         if (!dest_exists &&
    101                 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
    102             bb_perror_msg("unable to change permissions of `%s'", dest);
    103             status = -1;
    104         }
    105     } else if (S_ISREG(source_stat.st_mode) ||
    106            (S_ISLNK(source_stat.st_mode) && (flags & FILEUTILS_DEREFERENCE)))
    107     {
     192        if (!dest_exists
     193         && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
     194        ) {
     195            bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
     196            /* retval = -1; - WRONG! copy *WAS* made */
     197        }
     198        goto preserve_mode_ugid_time;
     199    }
     200
     201    if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
     202        int (*lf)(const char *oldpath, const char *newpath);
     203 make_links:
     204        // Hmm... maybe
     205        // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
     206        // (but realpath returns NULL on dangling symlinks...)
     207        lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
     208        if (lf(source, dest) < 0) {
     209            ovr = ask_and_unlink(dest, flags);
     210            if (ovr <= 0)
     211                return ovr;
     212            if (lf(source, dest) < 0) {
     213                bb_perror_msg("cannot create link '%s'", dest);
     214                return -1;
     215            }
     216        }
     217        /* _Not_ jumping to preserve_mode_ugid_time:
     218         * hard/softlinks don't have those */
     219        return 0;
     220    }
     221
     222    if (S_ISREG(source_stat.st_mode)
     223     /* DEREF uses stat, which never returns S_ISLNK() == true. */
     224     /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
     225    ) {
    108226        int src_fd;
    109227        int dst_fd;
    110         if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
    111             char *link_name;
    112 
    113             if (!(flags & FILEUTILS_DEREFERENCE) &&
    114                     is_in_ino_dev_hashtable(&source_stat, &link_name)) {
    115                 if (link(link_name, dest) < 0) {
    116                     bb_perror_msg("unable to link `%s'", dest);
     228
     229        if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
     230            const char *link_target;
     231            link_target = is_in_ino_dev_hashtable(&source_stat);
     232            if (link_target) {
     233                if (link(link_target, dest) < 0) {
     234                    ovr = ask_and_unlink(dest, flags);
     235                    if (ovr <= 0)
     236                        return ovr;
     237                    if (link(link_target, dest) < 0) {
     238                        bb_perror_msg("cannot create link '%s'", dest);
     239                        return -1;
     240                    }
     241                }
     242                return 0;
     243            }
     244            add_to_ino_dev_hashtable(&source_stat, dest);
     245        }
     246
     247        src_fd = open_or_warn(source, O_RDONLY);
     248        if (src_fd < 0)
     249            return -1;
     250
     251        /* POSIX way is a security problem versus symlink attacks,
     252         * we do it only for non-symlinks, and only for non-recursive,
     253         * non-interactive cp. NB: it is still racy
     254         * for "cp file /home/bad_user/file" case
     255         * (user can rm file and create a link to /etc/passwd) */
     256        if (DO_POSIX_CP
     257         || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
     258             && !S_ISLNK(dest_stat.st_mode))
     259        ) {
     260            dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
     261        } else  /* safe way: */
     262            dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
     263        if (dst_fd == -1) {
     264            ovr = ask_and_unlink(dest, flags);
     265            if (ovr <= 0) {
     266                close(src_fd);
     267                return ovr;
     268            }
     269            /* It shouldn't exist. If it exists, do not open (symlink attack?) */
     270            dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
     271            if (dst_fd < 0) {
     272                close(src_fd);
     273                return -1;
     274            }
     275        }
     276
     277#if ENABLE_SELINUX
     278        if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
     279            || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
     280         && is_selinux_enabled() > 0
     281        ) {
     282            security_context_t con;
     283            if (getfscreatecon(&con) == -1) {
     284                bb_perror_msg("getfscreatecon");
     285                return -1;
     286            }
     287            if (con) {
     288                if (setfilecon(dest, con) == -1) {
     289                    bb_perror_msg("setfilecon:%s,%s", dest, con);
     290                    freecon(con);
    117291                    return -1;
    118292                }
    119 
    120                 return 0;
    121             }
    122             add_to_ino_dev_hashtable(&source_stat, dest);
    123         }
    124         src_fd = open(source, O_RDONLY);
    125         if (src_fd == -1) {
    126             bb_perror_msg("unable to open `%s'", source);
    127             return(-1);
    128         }
    129 
    130         if (dest_exists) {
    131             if (flags & FILEUTILS_INTERACTIVE) {
    132                 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
    133                 if (!bb_ask_confirmation()) {
    134                     close (src_fd);
    135                     return 0;
    136                 }
    137             }
    138 
    139             dst_fd = open(dest, O_WRONLY|O_TRUNC);
    140             if (dst_fd == -1) {
    141                 if (!(flags & FILEUTILS_FORCE)) {
    142                     bb_perror_msg("unable to open `%s'", dest);
    143                     close(src_fd);
    144                     return -1;
    145                 }
    146 
    147                 if (unlink(dest) < 0) {
    148                     bb_perror_msg("unable to remove `%s'", dest);
    149                     close(src_fd);
    150                     return -1;
    151                 }
    152 
    153                 goto dest_removed;
    154             }
    155         } else {
    156 dest_removed:
    157             dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
    158             if (dst_fd == -1) {
    159                 bb_perror_msg("unable to open `%s'", dest);
    160                 close(src_fd);
    161                 return(-1);
    162             }
    163         }
    164 
     293                freecon(con);
     294            }
     295        }
     296#endif
    165297        if (bb_copyfd_eof(src_fd, dst_fd) == -1)
    166             status = -1;
    167 
     298            retval = -1;
     299        /* Ok, writing side I can understand... */
    168300        if (close(dst_fd) < 0) {
    169             bb_perror_msg("unable to close `%s'", dest);
    170             status = -1;
    171         }
    172 
    173         if (close(src_fd) < 0) {
    174             bb_perror_msg("unable to close `%s'", source);
    175             status = -1;
    176         }
    177     } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
    178         S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
    179         S_ISLNK(source_stat.st_mode)) {
    180 
    181         if (dest_exists) {
    182             if((flags & FILEUTILS_FORCE) == 0) {
    183                 fprintf(stderr, "`%s' exists\n", dest);
    184                 return -1;
    185             }
    186             if(unlink(dest) < 0) {
    187                 bb_perror_msg("unable to remove `%s'", dest);
    188                 return -1;
    189             }
    190         }
    191         if (S_ISFIFO(source_stat.st_mode)) {
    192             if (mkfifo(dest, source_stat.st_mode) < 0) {
    193                 bb_perror_msg("cannot create fifo `%s'", dest);
    194                 return -1;
    195             }
    196         } else if (S_ISLNK(source_stat.st_mode)) {
    197             char *lpath;
    198 
    199             lpath = xreadlink(source);
    200             if (symlink(lpath, dest) < 0) {
    201                 bb_perror_msg("cannot create symlink `%s'", dest);
    202                 return -1;
    203             }
     301            bb_perror_msg("cannot close '%s'", dest);
     302            retval = -1;
     303        }
     304        /* ...but read size is already checked by bb_copyfd_eof */
     305        close(src_fd);
     306        goto preserve_mode_ugid_time;
     307    }
     308
     309    /* Source is a symlink or a special file */
     310    /* We are lazy here, a bit lax with races... */
     311    if (dest_exists) {
     312        errno = EEXIST;
     313        ovr = ask_and_unlink(dest, flags);
     314        if (ovr <= 0)
     315            return ovr;
     316    }
     317    if (S_ISLNK(source_stat.st_mode)) {
     318        char *lpath = xmalloc_readlink_or_warn(source);
     319        if (lpath) {
     320            int r = symlink(lpath, dest);
    204321            free(lpath);
    205 
     322            if (r < 0) {
     323                bb_perror_msg("cannot create symlink '%s'", dest);
     324                return -1;
     325            }
    206326            if (flags & FILEUTILS_PRESERVE_STATUS)
    207327                if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
    208                     bb_perror_msg("unable to preserve ownership of `%s'", dest);
    209 
    210             return 0;
    211 
    212         } else {
    213             if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
    214                 bb_perror_msg("unable to create `%s'", dest);
    215                 return -1;
    216             }
     328                    bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
     329        }
     330        /* _Not_ jumping to preserve_mode_ugid_time:
     331         * symlinks don't have those */
     332        return 0;
     333    }
     334    if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
     335     || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
     336    ) {
     337        if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
     338            bb_perror_msg("cannot create '%s'", dest);
     339            return -1;
    217340        }
    218341    } else {
    219         bb_error_msg("internal error: unrecognized file type");
     342        bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
    220343        return -1;
    221344    }
    222345
    223 preserve_status:
    224 
    225     if (flags & FILEUTILS_PRESERVE_STATUS) {
     346 preserve_mode_ugid_time:
     347
     348    if (flags & FILEUTILS_PRESERVE_STATUS
     349    /* Cannot happen: */
     350    /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
     351    ) {
    226352        struct utimbuf times;
    227         char *msg="unable to preserve %s of `%s'";
    228353
    229354        times.actime = source_stat.st_atime;
    230355        times.modtime = source_stat.st_mtime;
     356        /* BTW, utimes sets usec-precision time - just FYI */
    231357        if (utime(dest, &times) < 0)
    232             bb_perror_msg(msg, "times", dest);
     358            bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
    233359        if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
    234360            source_stat.st_mode &= ~(S_ISUID | S_ISGID);
    235             bb_perror_msg(msg, "ownership", dest);
     361            bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
    236362        }
    237363        if (chmod(dest, source_stat.st_mode) < 0)
    238             bb_perror_msg(msg, "permissions", dest);
    239     }
    240 
    241     return status;
     364            bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
     365    }
     366
     367    return retval;
    242368}
Note: See TracChangeset for help on using the changeset viewer.