2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2018 Kern Sibbald
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.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Kern Sibbald, September MMVII
22 * This is tricky code, especially when writing from scratch. Fortunately,
23 * a non-copyrighted version of mkdir was available to consult.
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.
38 * For old systems that don't have lchown() or lchmod()
47 /* Defined in attribs.c */
48 void set_own_mod(ATTR *attr, char *path, uid_t owner, gid_t group, mode_t mode);
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 * mode is the mode bits to use in creating a new directory
173 * parent_mode are the parent's modes if we need to create parent
176 * owner and group are to set on any created dirs
178 * keep_dir_modes if set means don't change mode bits if dir exists
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)
185 char *path = (char *)apath;
193 int max_dirs = (int)sizeof(new_dir);
194 JCR *jcr = attr->jcr;
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);
201 /* Full path exists */
202 if (keep_dir_modes) {
205 set_own_mod(attr, path, owner, group, mode);
211 path = (char *)alloca(len+1);
212 bstrncpy(path, apath, len+1);
213 strip_trailing_slashes(path);
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
225 #if defined(HAVE_WIN32)
226 /* Validate drive letter */
227 if (path[1] == ':') {
228 char drive[4] = "X:\\";
232 UINT drive_type = GetDriveType(drive);
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]);
239 if (path[2] == '\0') { /* attempt to create a drive */
241 goto bail_out; /* OK, it is already there */
252 /* Skip leading slash(es) */
253 while (IsPathSeparator(*p)) {
256 while ((p = first_path_separator(p))) {
260 if (!makedir(jcr, path, tmode, &created)) {
263 if (ndir < max_dirs) {
264 new_dir[ndir++] = created;
267 while (IsPathSeparator(*p)) {
271 /* Create final component */
272 if (!makedir(jcr, path, tmode, &created)) {
275 if (ndir < max_dirs) {
276 new_dir[ndir++] = created;
278 if (ndir >= max_dirs) {
279 Jmsg0(jcr, M_WARNING, 0, _("Too many subdirectories. Some permissions not reset.\n"));
282 /* Now set the proper owner and modes */
283 #if defined(HAVE_WIN32)
285 /* Don't propagate the hidden or encrypted attributes to parent directories */
286 parent_mode &= ~S_ISVTX;
287 parent_mode &= ~S_ISGID;
289 if (path[1] == ':') {
297 /* Skip leading slash(es) */
298 while (IsPathSeparator(*p)) {
301 while ((p = first_path_separator(p))) {
305 if (i < ndir && new_dir[i++] && !keep_dir_modes) {
306 set_own_mod(attr, path, owner, group, parent_mode);
309 while (IsPathSeparator(*p)) {
314 /* Set for final component */
315 if (i < ndir && new_dir[i++]) {
316 set_own_mod(attr, path, owner, group, mode);