]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/mkpath.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / findlib / mkpath.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2018 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Kern Sibbald, September MMVII
21  *
22  *  This is tricky code, especially when writing from scratch. Fortunately,
23  *    a non-copyrighted version of mkdir was available to consult.
24  *
25  * ***FIXME*** the mkpath code could be significantly optimized by
26  *   walking up the path chain from the bottom until it either gets
27  *   to the top or finds an existing directory then walk back down
28  *   creating the path components.  Currently, it always starts at
29  *   the top, which can be rather inefficient for long path names.
30  *
31  */
32 #include "bacula.h"
33 #include "jcr.h"
34
35 #define dbglvl 50
36
37 /*
38  * For old systems that don't have lchown() or lchmod()
39  */
40 #ifndef HAVE_LCHOWN
41 #define lchown chown
42 #endif
43 #ifndef HAVE_LCHMOD
44 #define lchmod chmod
45 #endif
46
47 /* Defined in attribs.c */
48 void set_own_mod(ATTR *attr, char *path, uid_t owner, gid_t group, mode_t mode);
49
50 typedef struct PrivateCurDir {
51    hlink link;
52    char fname[1];
53 } CurDir;
54
55 /* Initialize the path hash table */
56 static bool path_list_init(JCR *jcr)
57 {
58    CurDir *elt = NULL;
59    jcr->path_list = (htable *)malloc(sizeof(htable));
60
61    /* Hard to know in advance how many directories will
62     * be stored in this hash
63     */
64    jcr->path_list->init(elt, &elt->link, 10000);
65    return true;
66 }
67
68 /* Add a path to the hash when we create a directory
69  * with the replace=NEVER option
70  */
71 bool path_list_add(JCR *jcr, uint32_t len, char *fname)
72 {
73    bool ret = true;
74    CurDir *item;
75
76    if (!jcr->path_list) {
77       path_list_init(jcr);
78    }
79
80    /* we store CurDir, fname in the same chunk */
81    item = (CurDir *)jcr->path_list->hash_malloc(sizeof(CurDir)+len+1);
82
83    memset(item, 0, sizeof(CurDir));
84    memcpy(item->fname, fname, len+1);
85
86    jcr->path_list->insert(item->fname, item);
87
88    Dmsg1(dbglvl, "add fname=<%s>\n", fname);
89    return ret;
90 }
91
92 void free_path_list(JCR *jcr)
93 {
94    if (jcr->path_list) {
95       jcr->path_list->destroy();
96       free(jcr->path_list);
97       jcr->path_list = NULL;
98    }
99 }
100
101 bool path_list_lookup(JCR *jcr, char *fname)
102 {
103    bool found=false;
104    char bkp;
105
106    if (!jcr->path_list) {
107       return false;
108    }
109
110    /* Strip trailing / */
111    int len = strlen(fname);
112    if (len == 0) {
113       return false;
114    }
115    len--;
116    bkp = fname[len];
117    if (fname[len] == '/') {       /* strip any trailing slash */
118       fname[len] = 0;
119    }
120
121    CurDir *temp = (CurDir *)jcr->path_list->lookup(fname);
122    if (temp) {
123       found=true;
124    }
125
126    Dmsg2(dbglvl, "lookup <%s> %s\n", fname, found?"ok":"not ok");
127
128    fname[len] = bkp;            /* restore last / */
129    return found;
130 }
131
132 static bool makedir(JCR *jcr, char *path, mode_t mode, int *created)
133 {
134    struct stat statp;
135
136    if (mkdir(path, mode) != 0) {
137       berrno be;
138       *created = false;
139       if (lstat(path, &statp) != 0) {
140          Jmsg2(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"),
141               path, be.bstrerror());
142          return false;
143       } else if (!S_ISDIR(statp.st_mode)) {
144          Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
145          return false;
146       }
147       return true;                 /* directory exists */
148    }
149 #if 0
150    /* TODO: This code rely on statp that is not initialized, we need to do a stat() */
151    if (S_ISLNK(statp.st_mode)) {
152       /*
153        * Note, we created a directory, not a link, so if we find a
154        *  link, there is a security problem here.
155        */
156       Jmsg1(jcr, M_FATAL, 0, _("Security problem!! We created directory %s, but it is a link.\n"),
157          path);
158       return false;
159    }
160 #endif
161    if (jcr->keep_path_list) {
162       /* When replace=NEVER, we keep track of all directories newly created */
163       path_list_add(jcr, strlen(path), path);
164    }
165
166    *created = true;
167    return true;
168 }
169
170 /*
171  * mode is the mode bits to use in creating a new directory
172  *
173  * parent_mode are the parent's modes if we need to create parent
174  *    directories.
175  *
176  * owner and group are to set on any created dirs
177  *
178  * keep_dir_modes if set means don't change mode bits if dir exists
179  */
180 bool makepath(ATTR *attr, const char *apath, mode_t mode, mode_t parent_mode,
181             uid_t owner, gid_t group, int keep_dir_modes)
182 {
183    struct stat statp;
184    mode_t omask, tmode;
185    char *path = (char *)apath;
186    char *p;
187    int len;
188    bool ok = false;
189    int created;
190    char new_dir[5000];
191    int ndir = 0;
192    int i = 0;
193    int max_dirs = (int)sizeof(new_dir);
194    JCR *jcr = attr->jcr;
195
196    if (stat(path, &statp) == 0) {     /* Does dir exist? */
197       if (!S_ISDIR(statp.st_mode)) {
198          Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
199          return false;
200       }
201       /* Full path exists */
202       if (keep_dir_modes) {
203          return true;
204       }
205       set_own_mod(attr, path, owner, group, mode);
206       return true;
207    }
208    omask = umask(0);
209    umask(omask);
210    len = strlen(apath);
211    path = (char *)alloca(len+1);
212    bstrncpy(path, apath, len+1);
213    strip_trailing_slashes(path);
214    /*
215     * Now for one of the complexities. If we are not running as root,
216     *  then if the parent_mode does not have wx user perms, or we are
217     *  setting the userid or group, and the parent_mode has setuid, setgid,
218     *  or sticky bits, we must create the dir with open permissions, then
219     *  go back and patch all the dirs up with the correct perms.
220     * Solution, set everything to 0777, then go back and reset them at the
221     *  end.
222     */
223    tmode = 0777;
224
225 #if defined(HAVE_WIN32)
226    /* Validate drive letter */
227    if (path[1] == ':') {
228       char drive[4] = "X:\\";
229
230       drive[0] = path[0];
231
232       UINT drive_type = GetDriveType(drive);
233
234       if (drive_type == DRIVE_UNKNOWN || drive_type == DRIVE_NO_ROOT_DIR) {
235          Jmsg1(jcr, M_ERROR, 0, _("%c: is not a valid drive.\n"), path[0]);
236          goto bail_out;
237       }
238
239       if (path[2] == '\0') {          /* attempt to create a drive */
240          ok = true;
241          goto bail_out;               /* OK, it is already there */
242       }
243
244       p = &path[3];
245    } else {
246       p = path;
247    }
248 #else
249    p = path;
250 #endif
251
252    /* Skip leading slash(es) */
253    while (IsPathSeparator(*p)) {
254       p++;
255    }
256    while ((p = first_path_separator(p))) {
257       char save_p;
258       save_p = *p;
259       *p = 0;
260       if (!makedir(jcr, path, tmode, &created)) {
261          goto bail_out;
262       }
263       if (ndir < max_dirs) {
264          new_dir[ndir++] = created;
265       }
266       *p = save_p;
267       while (IsPathSeparator(*p)) {
268          p++;
269       }
270    }
271    /* Create final component */
272    if (!makedir(jcr, path, tmode, &created)) {
273       goto bail_out;
274    }
275    if (ndir < max_dirs) {
276       new_dir[ndir++] = created;
277    }
278    if (ndir >= max_dirs) {
279       Jmsg0(jcr, M_WARNING, 0, _("Too many subdirectories. Some permissions not reset.\n"));
280    }
281
282    /* Now set the proper owner and modes */
283 #if defined(HAVE_WIN32)
284
285    /* Don't propagate the hidden or encrypted attributes to parent directories */
286    parent_mode &= ~S_ISVTX;
287    parent_mode &= ~S_ISGID;
288
289    if (path[1] == ':') {
290       p = &path[3];
291    } else {
292       p = path;
293    }
294 #else
295    p = path;
296 #endif
297    /* Skip leading slash(es) */
298    while (IsPathSeparator(*p)) {
299       p++;
300    }
301    while ((p = first_path_separator(p))) {
302       char save_p;
303       save_p = *p;
304       *p = 0;
305       if (i < ndir && new_dir[i++] && !keep_dir_modes) {
306          set_own_mod(attr, path, owner, group, parent_mode);
307       }
308       *p = save_p;
309       while (IsPathSeparator(*p)) {
310          p++;
311       }
312    }
313
314    /* Set for final component */
315    if (i < ndir && new_dir[i++]) {
316       set_own_mod(attr, path, owner, group, mode);
317    }
318
319    ok = true;
320 bail_out:
321    umask(omask);
322    return ok;
323 }