c5332ca6a91daad67aab986de4132908035d1541
[mmh] / sbr / makedir.c
1
2 /*
3  * makedir.c -- make a directory
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 /*
13  * Modified to try recursive create.
14  */
15
16 #include <h/mh.h>
17 #include <errno.h>
18 #include <sys/param.h>
19 #include <sys/file.h>
20         
21 int
22 makedir (char *dir)
23 {
24     char            path[PATH_MAX];
25     char*           folder_perms_ASCII;
26     int             had_an_error = 0;
27     mode_t          folder_perms, saved_umask;
28     pid_t           pid;
29     register char*  c;
30
31     context_save();     /* save the context file */
32     fflush(stdout);
33
34     if (!(folder_perms_ASCII = context_find ("folder-protect")))
35         folder_perms_ASCII = foldprot;  /* defaults to "700" */
36     
37     /* Because mh-profile.man documents "Folder-Protect:" as an octal constant,
38        and we don't want to force the user to remember to include a leading
39        zero, we call atooi(folder_perms_ASCII) here rather than
40        strtoul(folder_perms_ASCII, NULL, 0).  Therefore, if anyone ever tries to
41        specify a mode in say, hex, they'll get garbage.  (I guess nmh uses its
42        atooi() function rather than calling strtoul() with a radix of 8 because
43        some ancient platforms are missing that functionality. */
44     folder_perms = atooi(folder_perms_ASCII);
45
46     /* Folders have definite desired permissions that are set -- we don't want
47        to interact with the umask.  Clear it temporarily. */
48     saved_umask = umask(0);
49
50     if (getuid () == geteuid ()) {
51         c = strncpy(path, dir, sizeof(path));     
52         
53         while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {   
54             *c = (char)0;
55             if (access(path, X_OK)) {
56                 if (errno != ENOENT){
57                     advise (dir, "unable to create directory");
58                     had_an_error = 1;
59                 }
60                 /* Create an outer directory. */
61                 if (mkdir(path, folder_perms)) {
62                     advise (dir, "unable to create directory");
63                     had_an_error = 1;
64                 }
65             }
66             *c = '/';
67         }
68
69         if (!had_an_error) {
70             /* Create the innermost nested subdirectory of the path we're being
71                asked to create. */
72             if (mkdir (dir, folder_perms) == -1) {
73                 advise (dir, "unable to create directory");
74                 had_an_error = 1;
75             }
76         }
77     }
78     else {
79         /* Ummm, why do we want to avoid creating directories with the effective
80            user ID?  None of the nmh tools are installed such that the effective
81            should be different from the real, and if some parent process made
82            the two be different, I don't see why it should be our job to enforce
83            the real UID.  Also, why the heck do we call the mkdir executable
84            rather than the library function in this case??  If we do want to
85            call the mkdir executable, we should at least be giving it -p (and
86            change the single chmod() call below) so it can successfully create
87            nested directories like the above code can.
88
89            -- Dan Harkless <dan-nmh@dilvish.speed.net> */
90         switch (pid = vfork()) {
91             case -1: 
92                 advise ("fork", "unable to");
93                 return 0;
94                 
95             case 0: 
96                 setgid (getgid ());
97                 setuid (getuid ());
98                 
99                 execl ("/bin/mkdir", "mkdir", dir, NULL);
100                 execl ("/usr/bin/mkdir", "mkdir", dir, NULL);
101                 fprintf (stderr, "unable to exec ");
102                 perror ("mkdir");
103                 _exit (-1);
104                 
105             default: 
106                 if (pidXwait(pid, "mkdir"))
107                     return 0;
108                 break;
109         }
110
111         chmod (dir, folder_perms);
112     }
113
114     umask(saved_umask);  /* put the user's umask back */
115
116     if (had_an_error)
117         return 0;  /* opposite of UNIX error return convention */
118     else
119         return 1;
120 }