]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/makepath.c
First cut 1.27 see kes23Oct02
[bacula/bacula] / bacula / src / findlib / makepath.c
1 /* makepath.c -- Ensure that a directory path exists.
2
3    Copyright (C) 1990, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering.  */
20
21 /* 
22  *   Modified by Kern Sibbald for use in Bacula, December 2000
23  *
24  *   Version $Id$
25  */
26
27 #include "bacula.h"
28 #include "jcr.h"
29 #include "save-cwd.h"
30
31
32 #if !defined(S_ISDIR) && defined(S_IFDIR)
33 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
34 #endif
35
36 #ifndef S_IRWXUGO
37 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
38 #endif
39
40
41 #ifndef S_ISUID
42 # define S_ISUID 04000
43 #endif
44 #ifndef S_ISGID
45 # define S_ISGID 02000
46 #endif
47 #ifndef S_ISVTX
48 # define S_ISVTX 01000
49 #endif
50 #ifndef S_IRUSR
51 # define S_IRUSR 0200
52 #endif
53 #ifndef S_IWUSR
54 # define S_IWUSR 0200
55 #endif
56 #ifndef S_IXUSR
57 # define S_IXUSR 0100
58 #endif
59
60 #ifndef S_IRWXU
61 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
62 #endif
63
64 #define WX_USR (S_IWUSR | S_IXUSR)
65
66 #define quote(path) path
67
68 extern void strip_trailing_slashes();
69
70 static int
71 cleanup(struct saved_cwd *cwd)
72 {
73    if (cwd->do_chdir) {
74       int _fail = restore_cwd(cwd, NULL, NULL);
75       free_cwd(cwd);
76       if (_fail)
77          return 1;
78    }
79    return 0;
80 }
81
82
83 /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
84    If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
85    function creates DIR and to zero otherwise.  Give a diagnostic and
86    return non-zero if DIR cannot be created or cannot be determined to
87    exist already.  Use DIRPATH in any diagnostic, not DIR.
88    Note that if DIR already exists, this function will return zero
89    (indicating success) and will set *CREATED_DIR_P to zero.  */
90
91 static int
92 make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
93 {
94   int fail = 0;
95   int created_dir;
96
97   Dmsg2(300, "make_dir mode=%o dir=%s\n", mode, dir);
98   created_dir = (mkdir(dir, mode) == 0);
99
100   if (!created_dir) {
101       struct stat stats;
102
103       /* The mkdir and stat calls below may appear to be reversed.
104          They are not.  It is important to call mkdir first and then to
105          call stat (to distinguish the three cases) only if mkdir fails.
106          The alternative to this approach is to `stat' each directory,
107          then to call mkdir if it doesn't exist.  But if some other process
108          were to create the directory between the stat & mkdir, the mkdir
109          would fail with EEXIST.  */
110
111       if (stat(dir, &stats)) {
112           Jmsg(jcr, M_ERROR, 0, "Cannot create directory %s: %s\n", 
113                   dirpath, strerror(errno));
114           fail = 1;
115       } else if (!S_ISDIR(stats.st_mode)) {
116           Jmsg(jcr, M_ERROR, 0, "%s exists but is not a directory\n", quote(dirpath));
117           fail = 1;
118       } else {
119           /* DIR (aka DIRPATH) already exists and is a directory. */
120       }
121   }
122
123   if (created_dir_p) {
124      *created_dir_p = created_dir;
125   }
126
127   return fail;
128 }
129
130 /* Ensure that the directory ARGPATH exists.
131    Remove any trailing slashes from ARGPATH before calling this function.
132
133    Create any leading directories that don't already exist, with
134    permissions PARENT_MODE.
135     
136    If the last element of ARGPATH does not exist, create it as
137    a new directory with permissions MODE.
138
139    If OWNER and GROUP are non-negative, use them to set the UID and GID of
140    any created directories.
141
142    If VERBOSE_FMT_STRING is nonzero, use it as a printf format
143    string for printing a message after successfully making a directory,
144    with the name of the directory that was just made as an argument.
145
146    If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
147    then do not attempt to set its permissions and ownership.
148
149    Return 0 if ARGPATH exists as a directory with the proper
150    ownership and permissions when done, otherwise 1.  */
151
152 int
153 make_path(
154            void *jcr,
155            const char *argpath,
156            int mode,
157            int parent_mode,
158            uid_t owner,
159            gid_t group,
160            int preserve_existing,
161            char *verbose_fmt_string)
162 {
163   struct stat stats;
164   int retval = 0;
165
166   if (stat(argpath, &stats)) {
167       char *slash;
168       int tmp_mode;             /* Initial perms for leading dirs.  */
169       int re_protect;           /* Should leading dirs be unwritable? */
170       struct ptr_list {
171         char *dirname_end;
172         struct ptr_list *next;
173       };
174       struct ptr_list *p, *leading_dirs = NULL;
175       struct saved_cwd cwd;
176       char *basename_dir;
177       char *dirpath;
178
179       /* Temporarily relax umask in case it's overly restrictive.  */
180       mode_t oldmask = umask (0);
181
182       /* Make a copy of ARGPATH that we can scribble NULs on.  */
183       dirpath = (char *)alloca(strlen(argpath) + 1);
184       strcpy (dirpath, argpath);
185       strip_trailing_slashes(dirpath);
186
187       /* If leading directories shouldn't be writable or executable,
188          or should have set[ug]id or sticky bits set and we are setting
189          their owners, we need to fix their permissions after making them.  */
190       if (((parent_mode & WX_USR) != WX_USR)
191           || ((owner != (uid_t) -1 || group != (gid_t) -1)
192               && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
193         {
194           tmp_mode = S_IRWXU;
195           re_protect = 1;
196         }
197       else {
198           tmp_mode = parent_mode;
199           re_protect = 0;
200       }
201
202       /* If we can record the current working directory, we may be able
203          to do the chdir optimization.  */
204       cwd.do_chdir = !save_cwd(&cwd);
205
206       /* If we've saved the cwd and DIRPATH is an absolute pathname,
207          we must chdir to `/' in order to enable the chdir optimization.
208          So if chdir ("/") fails, turn off the optimization.  */
209       if (cwd.do_chdir && *dirpath == '/' && chdir ("/") < 0) {
210          cwd.do_chdir = 0;
211       }
212
213       slash = dirpath;
214
215       /* Skip over leading slashes.  */
216       while (*slash == '/')
217          slash++;
218
219       while (1) {
220           int newly_created_dir;
221           int fail;
222
223           /* slash points to the leftmost unprocessed component of dirpath.  */
224           basename_dir = slash;
225
226           slash = strchr (slash, '/');
227           if (slash == NULL) {
228              break;
229           }
230
231           /* If we're *not* doing chdir before each mkdir, then we have to refer
232              to the target using the full (multi-component) directory name.  */
233           if (!cwd.do_chdir) {
234              basename_dir = dirpath;
235           }
236
237           *slash = '\0';
238           fail = make_dir(jcr, basename_dir, dirpath, tmp_mode, &newly_created_dir);
239           if (fail) {
240               umask(oldmask);
241               cleanup(&cwd);
242               return 1;
243           }
244
245           if (newly_created_dir) {
246               Dmsg0(300, "newly_created_dir\n");
247               if (verbose_fmt_string) {
248                  Jmsg(jcr, M_ERROR, 0, verbose_fmt_string, quote(dirpath));
249               }
250
251               if ((owner != (uid_t) -1 || group != (gid_t) -1)
252                   && chown (basename_dir, owner, group)
253 #if defined(AFS) && defined (EPERM)
254                   && errno != EPERM
255 #endif
256                   ) {
257                  Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n",
258                       quote (dirpath), strerror(errno));
259                  umask(oldmask);
260                  cleanup(&cwd);
261                  return 1;
262               }
263               Dmsg0(300, "Chown done.\n");
264
265               if (re_protect) {
266                  struct ptr_list *pnew = (struct ptr_list *)
267                     alloca (sizeof (struct ptr_list));
268                  pnew->dirname_end = slash;
269                  pnew->next = leading_dirs;
270                  leading_dirs = pnew;
271                  Dmsg0(300, "re_protect\n");
272               }
273           }
274
275           /* If we were able to save the initial working directory,
276              then we can use chdir to change into each directory before
277              creating an entry in that directory.  This avoids making
278              stat and mkdir process O(n^2) file name components.  */
279           if (cwd.do_chdir && chdir(basename_dir) < 0) {
280               Jmsg(jcr, M_ERROR, 0, "Cannot chdir to directory, %s: %s\n",
281                      quote (dirpath), strerror(errno));
282               umask(oldmask);
283               cleanup(&cwd);
284               return 1;
285           }
286
287           *slash++ = '/';
288
289           /* Avoid unnecessary calls to `stat' when given
290              pathnames containing multiple adjacent slashes.  */
291           while (*slash == '/')
292              slash++;
293       } /* end while (1) */
294
295       if (!cwd.do_chdir) {
296          basename_dir = dirpath;
297       }
298
299       /* We're done making leading directories.
300          Create the final component of the path.  */
301
302       Dmsg1(300, "Create final component. mode=%o\n", mode);
303       if (make_dir(jcr, basename_dir, dirpath, mode, NULL)) {
304           umask(oldmask);
305           cleanup(&cwd);
306           return 1;
307       }
308
309       /* Done creating directories.  Restore original umask.  */
310       umask (oldmask);
311
312       if (verbose_fmt_string != NULL) {
313          Jmsg(jcr, M_ERROR, 0, verbose_fmt_string, dirpath);
314       }
315
316       if (owner != (uid_t) -1 || group != (gid_t) -1) {
317           if (chown(basename_dir, owner, group)
318 #ifdef AFS
319               && errno != EPERM
320 #endif
321               )
322             {
323               Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n",
324                      quote (dirpath), strerror(errno));
325               retval = 1;
326             }
327       }
328
329       /* The above chown may have turned off some permission bits in MODE.
330          Another reason we may have to use chmod here is that mkdir(2) is
331          required to honor only the file permission bits.  In particular,
332          it need not honor the `special' bits, so if MODE includes any
333          special bits, set them here.  */
334       if (mode & ~S_IRWXUGO) {
335          Dmsg1(300, "Final chmod mode=%o\n", mode);
336       }
337       if ((mode & ~S_IRWXUGO) && chmod(basename_dir, mode)) {
338           Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n", 
339              quote(dirpath), strerror(errno));
340           retval = 1;
341       }
342
343      if (cleanup(&cwd))
344         return 1;
345
346       /* If the mode for leading directories didn't include owner "wx"
347          privileges, we have to reset their protections to the correct
348          value.  */
349       for (p = leading_dirs; p != NULL; p = p->next) {
350           *(p->dirname_end) = '\0';
351           Dmsg2(300, "Reset parent mode=%o dir=%s\n", parent_mode, dirpath);
352           if (chmod(dirpath, parent_mode)) {
353               Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n",
354                      quote (dirpath), strerror(errno));
355               retval = 1;
356           }
357       }
358   } else {
359       /* We get here if the entire path already exists.  */
360
361       const char *dirpath = argpath;
362
363       if (!S_ISDIR(stats.st_mode)) {
364           Jmsg(jcr, M_ERROR, 0, "%s exists but is not a directory\n", quote(dirpath));
365           return 1;
366       }
367
368       if (!preserve_existing) {
369           Dmsg0(300, "Do not preserve existing.\n");
370           /* chown must precede chmod because on some systems,
371              chown clears the set[ug]id bits for non-superusers,
372              resulting in incorrect permissions.
373              On System V, users can give away files with chown and then not
374              be able to chmod them.  So don't give files away.  */
375
376           if ((owner != (uid_t) -1 || group != (gid_t) -1)
377               && chown(dirpath, owner, group)
378 #ifdef AFS
379               && errno != EPERM
380 #endif
381               )
382             {
383               Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n",
384                      quote(dirpath), strerror(errno));
385               retval = 1;
386             }
387           if (chmod(dirpath, mode)) {
388               Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n",
389                                  quote(dirpath), strerror(errno));
390               retval = 1;
391           }
392           Dmsg2(300, "pathexists chmod mode=%o dir=%s\n", mode, dirpath);
393       }
394   }
395   return retval;
396 }