]> granicus.if.org Git - shadow/blob - libmisc/copydir.c
Stop at the first error.
[shadow] / libmisc / copydir.c
1 /*
2  * Copyright 1991 - 1994, Julianne Frances Haugh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #ident "$Id$"
33
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include "prototypes.h"
40 #include "defines.h"
41 #ifdef WITH_SELINUX
42 #include <selinux/selinux.h>
43 static int selinux_enabled = -1;
44 #endif
45 static const char *src_orig;
46 static const char *dst_orig;
47
48 struct link_name {
49         dev_t ln_dev;
50         ino_t ln_ino;
51         int ln_count;
52         char *ln_name;
53         struct link_name *ln_next;
54 };
55 static struct link_name *links;
56
57 static int copy_entry (const char *src, const char *dst,
58                        uid_t uid, gid_t gid);
59 static int copy_dir (const char *src, const char *dst,
60                      const struct stat *statp, const struct timeval mt[2],
61                      uid_t uid, gid_t gid);
62 #ifdef  S_IFLNK
63 static int copy_symlink (const char *src, const char *dst,
64                          const struct stat *statp, const struct timeval mt[2],
65                          uid_t uid, gid_t gid);
66 #endif
67 static int copy_hardlink (const char *src, const char *dst,
68                           struct link_name *lp);
69 static int copy_special (const char *src, const char *dst,
70                          const struct stat *statp, const struct timeval mt[2],
71                          uid_t uid, gid_t gid);
72 static int copy_file (const char *src, const char *dst,
73                       const struct stat *statp, const struct timeval mt[2],
74                       uid_t uid, gid_t gid);
75
76 #ifdef WITH_SELINUX
77 /*
78  * selinux_file_context - Set the security context before any file or
79  *                        directory creation.
80  *
81  *      selinux_file_context () should be called before any creation of file,
82  *      symlink, directory, ...
83  *
84  */
85 static int selinux_file_context (const char *dst_name)
86 {
87         security_context_t scontext = NULL;
88
89         if (selinux_enabled < 0) {
90                 selinux_enabled = is_selinux_enabled () > 0;
91         }
92         if (selinux_enabled) {
93                 /* Get the default security context for this file */
94                 if (matchpathcon (dst_name, 0, &scontext) < 0) {
95                         if (security_getenforce () != 0) {
96                                 return 1;
97                         }
98                 }
99                 /* Set the security context for the next created file */
100                 if (setfscreatecon (scontext) < 0) {
101                         if (security_getenforce () != 0) {
102                                 return 1;
103                         }
104                 }
105                 freecon (scontext);
106         }
107         return 0;
108 }
109 #endif
110
111 /*
112  * remove_link - delete a link from the link list
113  */
114 static void remove_link (struct link_name *ln)
115 {
116         struct link_name *lp;
117
118         if (links == ln) {
119                 links = ln->ln_next;
120                 free (ln->ln_name);
121                 free (ln);
122                 return;
123         }
124         for (lp = links; NULL !=lp; lp = lp->ln_next) {
125                 if (lp->ln_next == ln) {
126                         break;
127                 }
128         }
129
130         if (NULL == lp) {
131                 return;
132         }
133
134         lp->ln_next = lp->ln_next->ln_next;
135         free (ln->ln_name);
136         free (ln);
137 }
138
139 /*
140  * check_link - see if a file is really a link
141  */
142
143 static struct link_name *check_link (const char *name, const struct stat *sb)
144 {
145         struct link_name *lp;
146         int src_len;
147         int dst_len;
148         int name_len;
149         int len;
150
151         for (lp = links; lp; lp = lp->ln_next) {
152                 if ((lp->ln_dev == sb->st_dev) && (lp->ln_ino == sb->st_ino)) {
153                         return lp;
154                 }
155         }
156
157         if (sb->st_nlink == 1) {
158                 return NULL;
159         }
160
161         lp = (struct link_name *) xmalloc (sizeof *lp);
162         src_len = strlen (src_orig);
163         dst_len = strlen (dst_orig);
164         name_len = strlen (name);
165         lp->ln_dev = sb->st_dev;
166         lp->ln_ino = sb->st_ino;
167         lp->ln_count = sb->st_nlink;
168         len = name_len - src_len + dst_len + 1;
169         lp->ln_name = xmalloc (len);
170         snprintf (lp->ln_name, len, "%s%s", dst_orig, name + src_len);
171         lp->ln_next = links;
172         links = lp;
173
174         return NULL;
175 }
176
177 /*
178  * copy_tree - copy files in a directory tree
179  *
180  *      copy_tree() walks a directory tree and copies ordinary files
181  *      as it goes.
182  */
183 int copy_tree (const char *src_root, const char *dst_root, uid_t uid, gid_t gid)
184 {
185         char src_name[1024];
186         char dst_name[1024];
187         int err = 0;
188         int set_orig = 0;
189         struct DIRECT *ent;
190         DIR *dir;
191
192         /*
193          * Make certain both directories exist.  This routine is called
194          * after the home directory is created, or recursively after the
195          * target is created.  It assumes the target directory exists.
196          */
197
198         if (   (access (src_root, F_OK) != 0)
199             || (access (dst_root, F_OK) != 0)) {
200                 return -1;
201         }
202
203         /*
204          * Open the source directory and read each entry.  Every file
205          * entry in the directory is copied with the UID and GID set
206          * to the provided values.  As an added security feature only
207          * regular files (and directories ...) are copied, and no file
208          * is made set-ID.
209          */
210         dir = opendir (src_root);
211         if (NULL == dir) {
212                 return -1;
213         }
214
215         if (src_orig == 0) {
216                 src_orig = src_root;
217                 dst_orig = dst_root;
218                 set_orig++;
219         }
220         while ((0 == err) && (ent = readdir (dir)) != NULL) {
221
222                 /*
223                  * Skip the "." and ".." entries
224                  */
225
226                 if (strcmp (ent->d_name, ".") == 0 ||
227                     strcmp (ent->d_name, "..") == 0) {
228                         continue;
229                 }
230
231                 /*
232                  * Make the filename for both the source and the
233                  * destination files.
234                  */
235
236                 if (strlen (src_root) + strlen (ent->d_name) + 2 >
237                     sizeof src_name) {
238                         err = -1;
239                         break;
240                 }
241                 snprintf (src_name, sizeof src_name, "%s/%s", src_root,
242                           ent->d_name);
243
244                 if (strlen (dst_root) + strlen (ent->d_name) + 2 >
245                     sizeof dst_name) {
246                         err = -1;
247                         break;
248                 }
249                 snprintf (dst_name, sizeof dst_name, "%s/%s", dst_root,
250                           ent->d_name);
251
252                 err = copy_entry (src_name, dst_name, uid, gid);
253         }
254         closedir (dir);
255
256         if (set_orig) {
257                 src_orig = 0;
258                 dst_orig = 0;
259         }
260         return err;
261 }
262
263 static int copy_entry (const char *src, const char *dst,
264                        uid_t uid, gid_t gid)
265 {
266         int err = 0;
267         struct stat sb;
268         struct link_name *lp;
269         struct timeval mt[2];
270
271         if (LSTAT (src, &sb) == -1) {
272                 /* If we cannot stat the file, do not care. */
273         } else {
274 #if  defined(_BSD_SOURCE) || defined(_SVID_SOURCE)
275                 mt[0].tv_sec  = sb.st_atim.tv_sec;
276                 mt[0].tv_usec = sb.st_atim.tv_nsec / 1000;
277                 mt[1].tv_sec  = sb.st_mtim.tv_sec;
278                 mt[1].tv_usec = sb.st_mtim.tv_nsec / 1000;
279 #else
280                 mt[0].tv_sec  = sb.st_atime;
281                 mt[0].tv_usec = sb.st_atimensec / 1000;
282                 mt[1].tv_sec  = sb.st_mtime;
283                 mt[1].tv_usec = sb.st_mtimensec / 1000;
284 #endif
285
286                 if (S_ISDIR (sb.st_mode)) {
287                         err = copy_dir (src, dst, &sb, mt, uid, gid);
288                 }
289
290 #ifdef  S_IFLNK
291                 /*
292                  * Copy any symbolic links
293                  */
294
295                 else if (S_ISLNK (sb.st_mode)) {
296                         err = copy_symlink (src, dst, &sb, mt, uid, gid);
297                 }
298 #endif
299
300                 /*
301                  * See if this is a previously copied link
302                  */
303
304                 else if ((lp = check_link (src, &sb)) != NULL) {
305                         err = copy_hardlink (src, dst, lp);
306                 }
307
308                 /*
309                  * Deal with FIFOs and special files.  The user really
310                  * shouldn't have any of these, but it seems like it
311                  * would be nice to copy everything ...
312                  */
313
314                 else if (!S_ISREG (sb.st_mode)) {
315                         err = copy_special (src, dst, &sb, mt, uid, gid);
316                 }
317
318                 /*
319                  * Create the new file and copy the contents.  The new
320                  * file will be owned by the provided UID and GID values.
321                  */
322
323                 else {
324                         err = copy_file (src, dst, &sb, mt, uid, gid);
325                 }
326         }
327
328         return err;
329 }
330
331 static int copy_dir (const char *src, const char *dst,
332                      const struct stat *statp, const struct timeval mt[2],
333                      uid_t uid, gid_t gid)
334 {
335         int err = 0;
336
337         /*
338          * Create a new target directory, make it owned by
339          * the user and then recursively copy that directory.
340          */
341
342 #ifdef WITH_SELINUX
343         selinux_file_context (dst);
344 #endif
345         if (   (mkdir (dst, statp->st_mode) != 0)
346             || (chown (dst,
347                        (uid == (uid_t) - 1) ? statp->st_uid : uid,
348                        (gid == (gid_t) - 1) ? statp->st_gid : gid) != 0)
349             || (chmod (dst, statp->st_mode) != 0)
350             || (copy_tree (src, dst, uid, gid) != 0)
351             || (utimes (dst, mt) != 0)) {
352                 err = -1;
353         }
354
355         return err;
356 }
357
358 #ifdef  S_IFLNK
359 static int copy_symlink (const char *src, const char *dst,
360                          const struct stat *statp, const struct timeval mt[2],
361                          uid_t uid, gid_t gid)
362 {
363         char oldlink[1024];
364         char dummy[1024];
365         int len;
366         int err = 0;
367
368         /*
369          * Get the name of the file which the link points
370          * to.  If that name begins with the original
371          * source directory name, that part of the link
372          * name will be replaced with the original
373          * destination directory name.
374          */
375
376         len = readlink (src, oldlink, sizeof (oldlink) - 1);
377         if (len < 0) {
378                 return -1;
379         }
380         oldlink[len] = '\0';    /* readlink() does not NUL-terminate */
381         if (!strncmp (oldlink, src_orig, strlen (src_orig))) {
382                 snprintf (dummy, sizeof dummy, "%s%s",
383                           dst_orig,
384                           oldlink + strlen (src_orig));
385                 strcpy (oldlink, dummy);
386         }
387 #ifdef WITH_SELINUX
388         selinux_file_context (dst);
389 #endif
390         if (   (symlink (oldlink, dst) != 0)
391             || (lchown (dst,
392                         uid == (uid_t) - 1 ? statp->st_uid : uid,
393                         gid == (gid_t) - 1 ? statp->st_gid : gid) != 0)) {
394                 return -1;
395         }
396
397         /* 2007-10-18: We don't care about
398          *  exit status of lutimes because
399          *  it returns ENOSYS on many system
400          *  - not implemented
401          */
402         lutimes (dst, mt);
403
404         return err;
405 }
406 #endif
407
408 static int copy_hardlink (const char *src, const char *dst,
409                           struct link_name *lp)
410 {
411         /* TODO: selinux needed? */
412
413         if (link (lp->ln_name, dst) != 0) {
414                 return -1;
415         }
416         if (unlink (src) != 0) {
417                 return -1;
418         }
419
420         lp->ln_count--;
421         if (lp->ln_count <= 0) {
422                 remove_link (lp);
423         }
424
425         return 0;
426 }
427
428 static int copy_special (const char *src, const char *dst,
429                          const struct stat *statp, const struct timeval mt[2],
430                          uid_t uid, gid_t gid)
431 {
432         int err = 0;
433
434 #ifdef WITH_SELINUX
435         selinux_file_context (dst);
436 #endif
437
438         if (   (mknod (dst, statp->st_mode & ~07777, statp->st_rdev) != 0)
439             || (chown (dst,
440                        (uid == (uid_t) - 1) ? statp->st_uid : uid,
441                        (gid == (gid_t) - 1) ? statp->st_gid : gid) != 0)
442             || (chmod (dst, statp->st_mode & 07777) != 0)
443             || (utimes (dst, mt) != 0)) {
444                 err = -1;
445         }
446
447         return err;
448 }
449
450 static int copy_file (const char *src, const char *dst,
451                       const struct stat *statp, const struct timeval mt[2],
452                       uid_t uid, gid_t gid)
453 {
454         int err = 0;
455         int ifd;
456         int ofd;
457         char buf[1024];
458         int cnt;
459
460         ifd = open (src, O_RDONLY);
461         if (ifd < 0) {
462                 return -1;
463         }
464 #ifdef WITH_SELINUX
465         selinux_file_context (dst);
466 #endif
467         ofd = open (dst, O_WRONLY | O_CREAT | O_TRUNC, 0);
468         if (   (ofd < 0)
469             || (chown (dst,
470                        (uid == (uid_t) - 1) ? statp->st_uid : uid,
471                        (gid == (gid_t) - 1) ? statp->st_gid : gid) != 0)
472             || (chmod (dst, statp->st_mode & 07777) != 0)) {
473                 close (ifd);
474                 return -1;
475         }
476
477         while ((cnt = read (ifd, buf, sizeof buf)) > 0) {
478                 if (write (ofd, buf, cnt) != cnt) {
479                         return -1;
480                 }
481         }
482
483         close (ifd);
484
485         if (futimes (ofd, mt) != 0) {
486                 return -1;
487         }
488
489         if (close (ofd) != 0) {
490                 return -1;
491         }
492
493         return err;
494 }
495
496 /*
497  * remove_tree - remove files in a directory tree
498  *
499  *      remove_tree() walks a directory tree and deletes all the files
500  *      and directories.
501  */
502
503 int remove_tree (const char *root)
504 {
505         char new_name[1024];
506         int err = 0;
507         struct DIRECT *ent;
508         struct stat sb;
509         DIR *dir;
510
511         /*
512          * Make certain the directory exists.
513          */
514
515         if (access (root, F_OK) != 0) {
516                 return -1;
517         }
518
519         /*
520          * Open the source directory and read each entry.  Every file
521          * entry in the directory is copied with the UID and GID set
522          * to the provided values.  As an added security feature only
523          * regular files (and directories ...) are copied, and no file
524          * is made set-ID.
525          */
526         dir = opendir (root);
527         if (NULL == dir) {
528                 return -1;
529         }
530
531         while ((ent = readdir (dir))) {
532
533                 /*
534                  * Skip the "." and ".." entries
535                  */
536
537                 if (strcmp (ent->d_name, ".") == 0 ||
538                     strcmp (ent->d_name, "..") == 0) {
539                         continue;
540                 }
541
542                 /*
543                  * Make the filename for the current entry.
544                  */
545
546                 if (strlen (root) + strlen (ent->d_name) + 2 > sizeof new_name) {
547                         err = -1;
548                         break;
549                 }
550                 snprintf (new_name, sizeof new_name, "%s/%s", root,
551                           ent->d_name);
552                 if (LSTAT (new_name, &sb) == -1) {
553                         continue;
554                 }
555
556                 if (S_ISDIR (sb.st_mode)) {
557
558                         /*
559                          * Recursively delete this directory.
560                          */
561
562                         if (remove_tree (new_name)) {
563                                 err = -1;
564                                 break;
565                         }
566                         if (rmdir (new_name)) {
567                                 err = -1;
568                                 break;
569                         }
570                         continue;
571                 }
572                 unlink (new_name);
573         }
574         closedir (dir);
575
576         return err;
577 }
578