2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
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.
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.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
22 * Kern Sibbald, September MMVII
24 * This is tricky code, especially when writing from scratch. Fortunately,
25 * a non-copyrighted version of mkdir was available to consult.
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.
40 * For old systems that don't have lchown() or lchmod()
50 typedef struct PrivateCurDir {
55 /* Initialize the path hash table */
56 static bool path_list_init(JCR *jcr)
59 jcr->path_list = (htable *)malloc(sizeof(htable));
61 /* Hard to know in advance how many directories will
62 * be stored in this hash
64 jcr->path_list->init(elt, &elt->link, 10000);
68 /* Add a path to the hash when we create a directory
69 * with the replace=NEVER option
71 bool path_list_add(JCR *jcr, uint32_t len, char *fname)
76 if (!jcr->path_list) {
80 /* we store CurDir, fname in the same chunk */
81 item = (CurDir *)jcr->path_list->hash_malloc(sizeof(CurDir)+len+1);
83 memset(item, 0, sizeof(CurDir));
84 memcpy(item->fname, fname, len+1);
86 jcr->path_list->insert(item->fname, item);
88 Dmsg1(dbglvl, "add fname=<%s>\n", fname);
92 void free_path_list(JCR *jcr)
95 jcr->path_list->destroy();
97 jcr->path_list = NULL;
101 bool path_list_lookup(JCR *jcr, char *fname)
106 if (!jcr->path_list) {
110 /* Strip trailing / */
111 int len = strlen(fname);
117 if (fname[len] == '/') { /* strip any trailing slash */
121 CurDir *temp = (CurDir *)jcr->path_list->lookup(fname);
126 Dmsg2(dbglvl, "lookup <%s> %s\n", fname, found?"ok":"not ok");
128 fname[len] = bkp; /* restore last / */
132 static bool makedir(JCR *jcr, char *path, mode_t mode, int *created)
136 if (mkdir(path, mode) != 0) {
139 if (lstat(path, &statp) != 0) {
140 Jmsg2(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"),
141 path, be.bstrerror());
143 } else if (!S_ISDIR(statp.st_mode)) {
144 Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
147 return true; /* directory exists */
150 /* TODO: This code rely on statp that is not initialized, we need to do a stat() */
151 if (S_ISLNK(statp.st_mode)) {
153 * Note, we created a directory, not a link, so if we find a
154 * link, there is a security problem here.
156 Jmsg1(jcr, M_FATAL, 0, _("Security problem!! We created directory %s, but it is a link.\n"),
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);
171 * Restore the owner and permissions (mode) of a Directory.
172 * See attribs.c for the equivalent for files.
174 static void set_own_mod(ATTR *attr, char *path, uid_t owner, gid_t group, mode_t mode)
176 if (lchown(path, owner, group) != 0 && attr->uid == 0
182 Jmsg2(attr->jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
183 path, be.bstrerror());
185 if (lchmod(path, mode) != 0 && attr->uid == 0) {
187 Jmsg2(attr->jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
188 path, be.bstrerror());
193 * mode is the mode bits to use in creating a new directory
195 * parent_mode are the parent's modes if we need to create parent
198 * owner and group are to set on any created dirs
200 * keep_dir_modes if set means don't change mode bits if dir exists
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)
207 char *path = (char *)apath;
215 int max_dirs = (int)sizeof(new_dir);
216 JCR *jcr = attr->jcr;
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);
223 /* Full path exists */
224 if (keep_dir_modes) {
227 set_own_mod(attr, path, owner, group, mode);
233 path = (char *)alloca(len+1);
234 bstrncpy(path, apath, len+1);
235 strip_trailing_slashes(path);
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
249 /* Skip leading slash(es) */
250 while (IsPathSeparator(*p)) {
253 while ((p = first_path_separator(p))) {
257 if (!makedir(jcr, path, tmode, &created)) {
260 if (ndir < max_dirs) {
261 new_dir[ndir++] = created;
264 while (IsPathSeparator(*p)) {
268 /* Create final component */
269 if (!makedir(jcr, path, tmode, &created)) {
272 if (ndir < max_dirs) {
273 new_dir[ndir++] = created;
275 if (ndir >= max_dirs) {
276 Jmsg0(jcr, M_WARNING, 0, _("Too many subdirectories. Some permissions not reset.\n"));
279 /* Now set the proper owner and modes */
281 /* Skip leading slash(es) */
282 while (IsPathSeparator(*p)) {
285 while ((p = first_path_separator(p))) {
289 if (i < ndir && new_dir[i++] && !keep_dir_modes) {
290 set_own_mod(attr, path, owner, group, parent_mode);
293 while (IsPathSeparator(*p)) {
298 /* Set for final component */
299 if (i < ndir && new_dir[i++]) {
300 set_own_mod(attr, path, owner, group, mode);