522dc08b44ce68b3ba65b348217047f5af35238e
[openafs.git] / src / venus / up.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <afs/afs_args.h>
16 #define VIRTUE
17 #define VICE
18 #include <afs/vice.h>
19 #undef VIRTUE
20 #undef VICE
21 #include <afs/venus.h>
22 #include <afs/sys_prototypes.h>
23 #include <afs/afsutil.h>
24 #include <afs/afs_consts.h>
25
26 /* ************************************************************* */
27
28 #define MAXACL 400
29
30 short verbose = 0;
31 short renameTargets = 0;
32 short oneLevel = 0;
33 short preserveDate = 1;
34 short preserveMountPoints = 0;
35 short forceOverwrite = 0;
36
37 short setacl = 1;
38 short oldAcl = 0;
39 char file1[MAXPATHLEN], file2[MAXPATHLEN];
40
41 static char space[AFS_PIOCTL_MAXSIZE];
42
43 struct OldAcl {
44     int nplus;
45     int nminus;
46     int offset;
47     char data[1];
48 };
49
50 static void ScanArgs(int argc, char *argv[]);
51 static short MakeParent(char *file, afs_int32 owner);
52 static int Copy(char *file1, char *file2, short recursive, int level);
53 static int isMountPoint(char *name, struct ViceIoctl *blob);
54
55
56 /* ************************************************************ */
57 /*                                                               */
58 /* main program                                                  */
59 /*                                                               */
60 /* ************************************************************ */
61
62 #include "AFS_component_version_number.c"
63
64 int
65 main(int argc, char *argv[])
66 {
67 #ifdef  AFS_AIX32_ENV
68     /*
69      * The following signal action for AIX is necessary so that in case of a
70      * crash (i.e. core is generated) we can include the user's data section
71      * in the core dump. Unfortunately, by default, only a partial core is
72      * generated which, in many cases, isn't too useful.
73      */
74     struct sigaction nsa;
75
76     sigemptyset(&nsa.sa_mask);
77     nsa.sa_handler = SIG_DFL;
78     nsa.sa_flags = SA_FULLDUMP;
79     sigaction(SIGSEGV, &nsa, NULL);
80 #endif
81     ScanArgs(argc, argv);
82
83     /* now read each line of the CopyList */
84     if (Copy(file1, file2, !oneLevel, 0))
85         return(1);              /* some type of failure */
86
87     return(0);
88 }
89
90
91 #define USAGE "usage: up [-v1frxm] from to\n"
92 static void
93 ScanArgs(int argc, char *argv[])
94 {
95     /* skip program name */
96     argc--, argv++;
97
98     /* check for -flag options */
99     while (argc > 0 && *argv[0] == '-') {
100         char *cp = *argv;
101
102         if (strlen(cp) > 2) {
103             goto badoption;
104         }
105
106         switch (*++cp) {
107         case 'v':
108             verbose = 1;
109             break;
110
111         case '1':
112             oneLevel = 1;
113             break;
114
115         case 'r':
116             renameTargets = 1;
117             break;
118
119         case 'f':
120             forceOverwrite = 1;
121             break;
122
123         case 'x':
124             preserveDate = 0;
125             break;
126
127         case 'm':
128             preserveMountPoints = 1;
129             break;
130
131         default:
132             cp--;
133
134  badoption:
135             fprintf(stderr, "Unknown option: '%s'\n", cp);
136             fprintf(stderr, USAGE);
137             exit(1);
138         }
139         argc--, argv++;
140     }
141
142     if (argc != 2) {
143         fprintf(stderr, USAGE);
144         exit(1);
145     }
146
147     strncpy(file1, argv[0], MAXPATHLEN);
148     strncpy(file2, argv[1], MAXPATHLEN);
149
150 }                               /*ScanArgs */
151
152
153
154 /*
155  * MakeParent
156  *      Make sure the parent directory of this file exists.  Returns
157  *      1 if it exists, 0 otherwise.  Note: the owner argument
158  *      is a hack.  All directories made will have this owner.
159  */
160 static short
161 MakeParent(char *file, afs_int32 owner)
162 {
163     char parent[MAXPATHLEN];
164     char *p;
165     struct stat s;
166
167     strlcpy(parent, file, sizeof parent);
168
169     p = strrchr(parent, '/');
170     if (!p) {
171         strlcpy(parent, ".", sizeof parent);
172     } else if (p > parent) {
173         *p = '\0';
174     } else {
175         p[1] = '\0';
176     }
177
178     if (stat(parent, &s) < 0) {
179         if (!MakeParent(parent, owner))
180             return (0);
181
182         if (verbose) {
183             printf("Creating directory %s\n", parent);
184             fflush(stdout);
185         }
186
187         mkdir(parent, 0777);
188         chown(parent, owner, -1);
189     }
190     return (1);
191 }                               /*MakeParent */
192
193
194 /*
195  * Copy
196  *      This does the bulk of the work of the program.  Handle one file,
197  *      possibly copying subfiles if this is a directory
198  */
199 static int
200 Copy(char *file1, char *file2, short recursive, int level)
201 {
202     struct stat s1, s2;         /*Stat blocks */
203     struct ViceIoctl blob;
204     char aclspace[MAXACL];
205     afs_int32 rcode = 0, code;
206     int goods2 = 1;
207
208     code = lstat(file1, &s1);
209     if (code < 0) {
210         fprintf(stderr, "Can't find %s\n", file1);
211         return 1;
212     }
213
214     code = lstat(file2, &s2);
215     if (code < 0) {
216         if (!MakeParent(file2, s1.st_uid))
217             return 0;
218         goods2 = 0;
219     }
220
221     if ((s1.st_mode & S_IFMT) == S_IFREG) {
222         /*
223          * -------------------- Copy regular file --------------------
224          */
225         int f1, f2, n;
226         char buf[4096];         /* Must be bigger than sizeof (*head) */
227         struct timeval tv[2];
228         char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
229
230         if (verbose) {
231             printf("Level %d: File %s to %s\n", level, file1, file2);
232             fflush(stdout);
233         }
234
235         /* Wonder if there is a security hole */
236         if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
237             || ((s1.st_mode & 02002) == 02002)) {
238             fprintf(stderr,
239                     "WARNING: Mode-bits security hole in files %s and %s\n",
240                     file1, file2);
241         }
242
243         if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) {    /*c */
244             /* Don't ovewrite a write protected file (unless force: -f) */
245             if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
246                 fprintf(stderr,
247                         "File %s is write protected against its owner; not changed\n",
248                         file2);
249                 return 1;
250             }
251
252             if (verbose) {
253                 printf("  Copy file %s to %s (%u Bytes)\n", file1, file2,
254                        (unsigned int) s1.st_size);
255                 fflush(stdout);
256             }
257
258             strlcpy(tmpfile, file2, sizeof tmpfile);    /* Name of temporary file */
259             strlcat(tmpfile, ".UPD", sizeof tmpfile);
260
261             /* open file1 for input */
262             f1 = open(file1, O_RDONLY);
263             if (f1 < 0) {
264                 fprintf(stderr, "Unable to open input file %s: %s\n",
265                         file1, strerror(errno));
266                 return 1;
267             }
268
269             /* open temporary output file */
270             f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
271             if (f2 < 0) {
272                 fprintf(stderr, "Unable to open output file %s: %s\n",
273                         tmpfile, strerror(errno));
274                 fflush(stdout);
275                 close(f1);
276                 return 1;
277             }
278
279             /* Copy file1 to temporary file */
280             while ((n = read(f1, buf, sizeof(buf))) > 0) {
281                 if (write(f2, buf, n) != n) {
282                     fprintf(stderr,
283                             "Write failed, file %s must be copied again.\n",
284                             file2);
285                 }
286             }
287
288             /* preserve access and modification times: ("-x" disables) */
289             if (preserveDate) {
290                 tv[0].tv_sec = s1.st_atime;
291                 tv[0].tv_usec = 0;
292                 tv[1].tv_sec = s1.st_mtime;
293                 tv[1].tv_usec = 0;
294                 utimes(tmpfile, tv);
295             }
296
297             /* Close the files */
298             code = close(f1);
299             code = close(f2);
300             if (code < 0) {
301                 perror("close ");
302                 rcode = 1;
303             }
304
305             /* Rename file2 to file2.old. [-r] */
306             if (renameTargets && goods2) {
307                 strlcpy(newName, file2, sizeof newName);
308                 strlcat(newName, ".old", sizeof newName);
309                 if (verbose) {
310                     printf("  Renaming %s to %s\n", file2, newName);
311                     fflush(stdout);
312                 }
313                 if (rename(file2, newName) < 0) {
314                     fprintf(stderr, "Rename of %s to %s failed.\n", file2,
315                             newName);
316                 }
317             }
318
319             /* Rename temporary file to file2 */
320             code = rename(tmpfile, file2);
321             if (code < 0) {
322                 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
323                         file2);
324                 return 1;
325             }
326
327             /* Re-stat file2 and compare file sizes */
328             code = lstat(file2, &s2);
329             if (code < 0) {
330                 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
331                         file2);
332                 return 1;
333             }
334             if (s1.st_size != s2.st_size) {
335                 fprintf(stderr,
336                         "WARNING: New file %s is %u bytes long; should be %u\n",
337                         file2, (unsigned int) s2.st_size,
338                         (unsigned int) s1.st_size);
339             }
340         }
341
342         /*c */
343         /* Set the user-id */
344         if (s2.st_uid != s1.st_uid) {
345             if (verbose) {
346                 printf("  Set owner-id for %s to %d\n", file2, s1.st_uid);
347                 fflush(stdout);
348             }
349             code = chown(file2, s1.st_uid, -1);
350             if (code) {
351                 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
352                         file2, s1.st_uid);
353                 fflush(stdout);
354                 rcode = 1;
355                 s1.st_mode &= ~04000;   /* Don't set suid bit */
356             }
357         }
358
359         /* Set the group-id */
360         if (s2.st_gid != s1.st_gid) {
361             if (verbose) {
362                 printf("  Set group-id for %s to %d\n", file2, s1.st_gid);
363                 fflush(stdout);
364             }
365             code = chown(file2, -1, s1.st_gid);
366             if (code) {
367                 fprintf(stderr, "Unable to set group-id for %s to %d\n",
368                         file2, s1.st_gid);
369                 fflush(stdout);
370                 rcode = 1;
371                 s1.st_mode &= ~02000;   /* Don't set sgid bit */
372             }
373         }
374
375         /* Set the mode bits */
376         if (s1.st_mode != s2.st_mode) {
377             if (verbose) {
378                 printf("  Set mode-bit for %s to %o\n", file2,
379                        (s1.st_mode & 07777));
380                 fflush(stdout);
381             }
382             code = chmod(file2, s1.st_mode);
383             if (code) {
384                 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
385                         file2, s1.st_mode);
386                 rcode = 1;
387             }
388         }
389     }
390     /* regular file */
391     else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
392         /*
393          * --------------------- Copy symlink  --------------------
394          */
395         char linkvalue[MAXPATHLEN + 1];
396         int n;
397
398         if (verbose) {
399             printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
400             fflush(stdout);
401         }
402
403         /* Don't ovewrite a write protected directory (unless force: -f) */
404         if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
405             fprintf(stderr,
406                     "Link %s is write protected against its owner; not changed\n",
407                     file2);
408             return 1;
409         }
410
411         if (verbose) {
412             printf("  Copy symbolic link %s->%s to %s\n", file1, linkvalue,
413                    file2);
414             fflush(stdout);
415         }
416
417         n = readlink(file1, linkvalue, sizeof(linkvalue));
418         if (n == -1) {
419             fprintf(stderr, "Could not read symbolic link %s\n", file1);
420             perror("read link ");
421             return 1;
422         }
423         linkvalue[n] = 0;
424
425         unlink(file2);          /* Always make the new link (it was easier) */
426
427         code = symlink(linkvalue, file2);
428         if (code == -1) {
429             fprintf(stderr, "Could not create symbolic link %s\n", file2);
430             perror("create link ");
431             return 1;
432         }
433     }
434     /*Dealing with symlink */
435     else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
436         /*
437          * --------------------- Copy mount point  --------------------
438          */
439
440         if (code > 1) {
441             perror("checking for mount point ");
442             return 1;
443         }
444         if (verbose) {
445             printf("Level %d: Mount point %s to %s\n", level, file1, file2);
446             fflush(stdout);
447         }
448
449         /* Don't ovewrite a write protected directory (unless force: -f) */
450         if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
451             fprintf(stderr,
452                     "Target %s is write protected against its owner; not changed\n",
453                     file2);
454             return 1;
455         }
456
457         if (verbose) {
458             printf("  Copy mount point %s for vol %s to %s\n", file1,
459                    blob.out, file2);
460             fflush(stdout);
461         }
462
463         unlink(file2);          /* Always make the new link (it was easier) */
464
465         strcat(blob.out, ".");  /* stupid convention; these end with a period */
466         code = symlink(blob.out, file2);
467         if (code == -1) {
468             fprintf(stderr, "Could not create mount point %s for vol %s\n",
469                     file2, blob.out);
470             perror("create mount point ");
471             return 1;
472         }
473
474     }
475     /*Dealing with mount point */
476     else if (((s1.st_mode & S_IFMT) == S_IFDIR)
477              && (recursive || (level == 0))) {
478         /*
479          * ----------------------- Copy directory -----------------------
480          */
481         DIR *dir;
482         int tfd, code, i;
483         struct OldAcl *oacl;
484         char tacl[MAXACL];
485         char f1[MAXPATHLEN], f2[MAXPATHLEN];
486         char *p1, *p2;
487         struct dirent *d;
488         struct timeval tv[2];
489
490         if (verbose) {
491             printf("Level %d: Directory %s to %s\n", level, file1, file2);
492             fflush(stdout);
493         }
494
495         /* Don't ovewrite a write protected directory (unless force: -f) */
496         if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
497             fprintf(stderr,
498                     "Directory %s is write protected against its owner; not changed\n",
499                     file2);
500             return 1;
501         }
502
503         strlcpy(f1, file1, sizeof f1);
504         strlcpy(f2, file2, sizeof f2);
505         p1 = f1 + strlen(f1);
506         p2 = f2 + strlen(f2);
507         if (p1 == f1 || p1[-1] != '/')
508             *p1++ = '/';
509         if (p2 == f2 || p2[-1] != '/')
510             *p2++ = '/';
511
512         dir = opendir(file1);
513         if (dir == NULL) {
514             fprintf(stderr, "Couldn't open %s\n", file1);
515             return 1;
516         }
517
518         while ((d = readdir(dir)) != NULL) {
519             if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
520                 continue;
521             strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
522             strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
523             code = Copy(f1, f2, recursive, level + 1);
524             if (code && !rcode)
525                 rcode = 1;      /* remember errors */
526         }
527
528         closedir(dir);
529
530         if (verbose) {
531             printf("Level %d: Copied directory %s to %s\n", level, file1,
532                    file2);
533             fflush(stdout);
534         }
535
536         mkdir(file2, 0777);     /* Handle case where MakeParent not invoked. */
537
538         if (verbose) {
539             printf("  Set owner-id for %s to %d\n", file2, s1.st_uid);
540             fflush(stdout);
541         }
542         code = chown(file2, s1.st_uid, -1);
543         if (code) {
544             fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
545                     s1.st_uid);
546             fflush(stdout);
547             s1.st_mode &= ~04000;       /* Don't set suid bit */
548         }
549
550         if (verbose) {
551             printf("  Set group-id for %s to %d\n", file2, s1.st_gid);
552             fflush(stdout);
553         }
554         code = chown(file2, -1, s1.st_gid);
555         if (code) {
556             fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
557                     s1.st_gid);
558             fflush(stdout);
559             s1.st_mode &= ~02000;       /* Don't set sgid bit */
560         }
561
562         if (verbose) {
563             printf("  Set mode-bit for %s to %o\n", file2,
564                    (s1.st_mode & 07777));
565             fflush(stdout);
566         }
567         code = chmod(file2, s1.st_mode);
568         if (code) {
569             fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
570                     s1.st_mode);
571             fflush(stdout);
572             rcode = 1;
573         }
574
575         if (setacl == 1) {
576             if (verbose) {
577                 printf("  Set acls for %s\n", file2);
578                 fflush(stdout);
579             }
580
581             blob.in = aclspace;
582             blob.out = aclspace;
583             blob.in_size = 0;
584             blob.out_size = MAXACL;
585
586             if (oldAcl) {
587                 /* Get an old-style ACL and convert it */
588                 if (verbose) {
589                     printf("  Getting old style acl\n");
590                     fflush(stdout);
591                 }
592
593                 for (i = 1; i < strlen(file1); i++)
594                     if (file1[i] == '/')
595                         break;
596                 strlcpy(aclspace, &file1[i], sizeof aclspace);
597
598                 blob.in_size = 1 + strlen(aclspace);
599                 tfd = open(file1, O_RDONLY, 0);
600                 if (tfd < 0) {
601                     perror("old-acl open ");
602                     return 1;
603                 }
604                 code = ioctl(tfd, _VICEIOCTL(4), &blob);
605                 close(tfd);
606                 if (code < 0) {
607                     if (errno == EINVAL) {
608                         setacl = 0;
609                         if (verbose) {
610                             printf("  _VICEIOCTL(4) returns EINVAL\n");
611                             fflush(stdout);
612                         }
613                     } else {
614                         return 1;
615                     }
616                 }
617                 /* Now convert the thing. */
618                 oacl = (struct OldAcl *)(aclspace + 4);
619                 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
620                 strlcat(tacl, oacl->data, sizeof tacl);
621                 strlcpy(aclspace, tacl, sizeof aclspace);
622             } /*Grab and convert old-style ACL */
623             else {
624                 /* Get a new-style ACL */
625                 if (verbose) {
626                     printf("  Getting new style acl\n");
627                     fflush(stdout);
628                 }
629
630                 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
631                 if (code < 0) {
632                     if (errno == EINVAL) {
633                         setacl = 0;
634                         if (verbose) {
635                             printf("  _VICEIOCTL(2) returns EINVAL\n");
636                             fflush(stdout);
637                         }
638                     } else {
639                         perror("getacl ");
640                         return 1;
641                     }
642                 }
643             }                   /*Grab new-style ACL */
644
645             /*
646              * Now, set the new-style ACL.
647              */
648             if (setacl == 1) {
649                 if (verbose) {
650                     printf("  Setting new style acl\n");
651                     fflush(stdout);
652                 }
653                 blob.out = aclspace;
654                 blob.in = aclspace;
655                 blob.out_size = 0;
656                 blob.in_size = 1 + strlen(aclspace);
657                 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
658                 if (code) {
659                     if (errno == EINVAL) {
660                         setacl = 0;
661                         if (verbose) {
662                             printf("  _VICEIOCTL(1) returns EINVAL\n");
663                             fflush(stdout);
664                         }
665                     } else {
666                         fprintf(stderr, "Couldn't set acls for %s\n", file2);
667                         return 1;
668                     }
669                 }
670             }
671
672             if (setacl == 0) {
673                 printf("Not setting acls\n");
674             }
675         }
676
677         /* preserve access and modification times: ("-x" disables) */
678         if (preserveDate) {
679             tv[0].tv_sec = s1.st_atime;
680             tv[0].tv_usec = 0;
681             tv[1].tv_sec = s1.st_mtime;
682             tv[1].tv_usec = 0;
683             utimes(file2, tv);
684         }
685     }
686
687     return rcode;
688 }                               /*Copy */
689
690
691 static int
692 isMountPoint(char *name, struct ViceIoctl *blob)
693 {
694     afs_int32 code;
695     char true_name[1024];       /*dirname */
696     char parent_dir[1024];      /*Parent directory of true name */
697     char *last_component;       /*Last component of true name */
698
699     sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
700
701     /*
702      * Find rightmost slash, if any.
703      */
704     last_component = (char *)strrchr(true_name, '/');
705     if (last_component) {
706         /*
707          * Found it.  Designate everything before it as the parent directory,
708          * everything after it as the final component.
709          */
710         strncpy(parent_dir, true_name, last_component - true_name);
711         parent_dir[last_component - true_name] = 0;
712         last_component++;       /*Skip the slash */
713     } else {
714         /*
715          * No slash appears in the given file name.  Set parent_dir to the current
716          * directory, and the last component as the given name.
717          */
718         strlcpy(parent_dir, ".", sizeof parent_dir);
719         last_component = true_name;
720     }
721
722     if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
723         fprintf(stderr,
724                 "up: you may not use '.' or '..' as the last component\n");
725         fprintf(stderr, "up: of a name in the 'up' command.\n");
726         return 3;
727     }
728
729     blob->in = last_component;
730     blob->in_size = strlen(last_component) + 1;
731     blob->out_size = AFS_PIOCTL_MAXSIZE;
732     blob->out = space;
733     memset(space, 0, AFS_PIOCTL_MAXSIZE);
734
735     code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
736
737     if (code == 0) {
738         printf("'%s' is a mount point for volume '%s'\n", name, space);
739         fflush(stdout);
740         return 1;
741     } else {
742         if (errno == EINVAL) {
743             /* printf( "'%s' is not a mount point.\n", name);
744              * fflush(stdout);
745              */
746             return 0;
747         } else {
748             fprintf(stderr, "problem examining '%s' in '%s'.\n",
749                     last_component, parent_dir);
750             return 2;
751             /* Die(errno, (ti->data ? ti->data : parent_dir));
752              */
753         }
754     }
755 }