]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/mkpath.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / findlib / mkpath.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20
21 /*
22  *  Kern Sibbald, September MMVII
23  *
24  *  This is tricky code, especially when writing from scratch. Fortunately,
25  *    a non-copyrighted version of mkdir was available to consult.
26  *
27  * ***FIXME*** the mkpath code could be significantly optimized by
28  *   walking up the path chain from the bottom until it either gets
29  *   to the top or finds an existing directory then walk back down
30  *   creating the path components.  Currently, it always starts at
31  *   the top, which can be rather inefficient for long path names.
32  *
33  */
34 #include "bacula.h"
35 #include "jcr.h"
36
37 #define dbglvl 50
38
39 /*
40  * For old systems that don't have lchown() or lchmod()
41  */
42 #ifndef HAVE_LCHOWN
43 #define lchown chown
44 #endif
45 #ifndef HAVE_LCHMOD
46 #define lchmod chmod
47 #endif
48
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  * Restore the owner and permissions (mode) of a Directory.
172  *  See attribs.c for the equivalent for files.
173  */
174 static void set_own_mod(ATTR *attr, char *path, uid_t owner, gid_t group, mode_t mode)
175 {
176    if (lchown(path, owner, group) != 0 && attr->uid == 0
177 #ifdef AFS
178         && errno != EPERM
179 #endif
180    ) {
181       berrno be;
182       Jmsg2(attr->jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
183            path, be.bstrerror());
184    }
185    if (lchmod(path, mode) != 0 && attr->uid == 0) {
186       berrno be;
187       Jmsg2(attr->jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
188            path, be.bstrerror());
189    }
190 }
191
192 /*
193  * mode is the mode bits to use in creating a new directory
194  *
195  * parent_mode are the parent's modes if we need to create parent
196  *    directories.
197  *
198  * owner and group are to set on any created dirs
199  *
200  * keep_dir_modes if set means don't change mode bits if dir exists
201  */
202 bool makepath(ATTR *attr, const char *apath, mode_t mode, mode_t parent_mode,
203             uid_t owner, gid_t group, int keep_dir_modes)
204 {
205    struct stat statp;
206    mode_t omask, tmode;
207    char *path = (char *)apath;
208    char *p;
209    int len;
210    bool ok = false;
211    int created;
212    char new_dir[5000];
213    int ndir = 0;
214    int i = 0;
215    int max_dirs = (int)sizeof(new_dir);
216    JCR *jcr = attr->jcr;
217
218    if (stat(path, &statp) == 0) {     /* Does dir exist? */
219       if (!S_ISDIR(statp.st_mode)) {
220          Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
221          return false;
222       }
223       /* Full path exists */
224       if (keep_dir_modes) {
225          return true;
226       }
227       set_own_mod(attr, path, owner, group, mode);
228       return true;
229    }
230    omask = umask(0);
231    umask(omask);
232    len = strlen(apath);
233    path = (char *)alloca(len+1);
234    bstrncpy(path, apath, len+1);
235    strip_trailing_slashes(path);
236    /*
237     * Now for one of the complexities. If we are not running as root,
238     *  then if the parent_mode does not have wx user perms, or we are
239     *  setting the userid or group, and the parent_mode has setuid, setgid,
240     *  or sticky bits, we must create the dir with open permissions, then
241     *  go back and patch all the dirs up with the correct perms.
242     * Solution, set everything to 0777, then go back and reset them at the
243     *  end.
244     */
245    tmode = 0777;
246
247    p = path;
248
249    /* Skip leading slash(es) */
250    while (IsPathSeparator(*p)) {
251       p++;
252    }
253    while ((p = first_path_separator(p))) {
254       char save_p;
255       save_p = *p;
256       *p = 0;
257       if (!makedir(jcr, path, tmode, &created)) {
258          goto bail_out;
259       }
260       if (ndir < max_dirs) {
261          new_dir[ndir++] = created;
262       }
263       *p = save_p;
264       while (IsPathSeparator(*p)) {
265          p++;
266       }
267    }
268    /* Create final component */
269    if (!makedir(jcr, path, tmode, &created)) {
270       goto bail_out;
271    }
272    if (ndir < max_dirs) {
273       new_dir[ndir++] = created;
274    }
275    if (ndir >= max_dirs) {
276       Jmsg0(jcr, M_WARNING, 0, _("Too many subdirectories. Some permissions not reset.\n"));
277    }
278
279    /* Now set the proper owner and modes */
280    p = path;
281    /* Skip leading slash(es) */
282    while (IsPathSeparator(*p)) {
283       p++;
284    }
285    while ((p = first_path_separator(p))) {
286       char save_p;
287       save_p = *p;
288       *p = 0;
289       if (i < ndir && new_dir[i++] && !keep_dir_modes) {
290          set_own_mod(attr, path, owner, group, parent_mode);
291       }
292       *p = save_p;
293       while (IsPathSeparator(*p)) {
294          p++;
295       }
296    }
297
298    /* Set for final component */
299    if (i < ndir && new_dir[i++]) {
300       set_own_mod(attr, path, owner, group, mode);
301    }
302
303    ok = true;
304 bail_out:
305    umask(omask);
306    return ok;
307 }