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