2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Kern Sibbald, September MMVII
32 * This is tricky code, especially when writing from scratch. Fortunately,
33 * a non-copyrighted version of mkdir was available to consult.
40 static bool makedir(JCR *jcr, char *path, mode_t mode, int *created)
44 if (mkdir(path, mode) != 0) {
47 if (stat(path, &statp) != 0) {
48 Jmsg2(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"),
49 path, be.bstrerror());
51 } else if (!S_ISDIR(statp.st_mode)) {
52 Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
55 return true; /* directory exists */
61 static void set_own_mod(ATTR *attr, char *path, uid_t owner, gid_t group, mode_t mode)
63 if (chown(path, owner, group) != 0 && attr->uid == 0
69 Jmsg2(attr->jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
70 path, be.bstrerror());
72 if (chmod(path, mode) != 0 && attr->uid == 0) {
74 Jmsg2(attr->jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
75 path, be.bstrerror());
80 * mode is the mode bits to use in creating a new directory
82 * parent_mode are the parent's modes if we need to create parent
85 * owner and group are to set on any created dirs
87 * keep_dir_modes if set means don't change mode bits if dir exists
89 bool makepath(ATTR *attr, const char *apath, mode_t mode, mode_t parent_mode,
90 uid_t owner, gid_t group, int keep_dir_modes)
94 char *path = (char *)apath;
102 int max_dirs = (int)sizeof(new_dir);
103 JCR *jcr = attr->jcr;
105 if (stat(path, &statp) == 0) { /* Does dir exist? */
106 if (!S_ISDIR(statp.st_mode)) {
107 Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
110 /* Full path exists */
111 if (keep_dir_modes) {
114 set_own_mod(attr, path, owner, group, mode);
120 path = (char *)alloca(len+1);
121 bstrncpy(path, apath, len+1);
122 strip_trailing_slashes(path);
124 * Now for one of the complexities. If we are not running as root,
125 * then if the parent_mode does not have wx user perms, or we are
126 * setting the userid or group, and the parent_mode has setuid, setgid,
127 * or sticky bits, we must create the dir with open permissions, then
128 * go back and patch all the dirs up with the correct perms.
129 * Solution, set everything to 0777, then go back and reset them at the
134 #if defined(HAVE_WIN32)
135 /* Validate drive letter */
136 if (path[1] == ':') {
137 char drive[4] = "X:\\";
141 UINT drive_type = GetDriveType(drive);
143 if (drive_type == DRIVE_UNKNOWN || drive_type == DRIVE_NO_ROOT_DIR) {
144 Jmsg1(jcr, M_ERROR, 0, _("%c: is not a valid drive.\n"), path[0]);
148 if (path[2] == '\0') { /* attempt to create a drive */
150 goto bail_out; /* OK, it is already there */
161 /* Skip leading slash(es) */
162 while (IsPathSeparator(*p)) {
165 while ((p = first_path_separator(p))) {
169 if (!makedir(jcr, path, tmode, &created)) {
172 if (ndir < max_dirs) {
173 new_dir[ndir++] = created;
176 while (IsPathSeparator(*p)) {
180 /* Create final component */
181 if (!makedir(jcr, path, tmode, &created)) {
184 if (ndir < max_dirs) {
185 new_dir[ndir++] = created;
187 if (ndir >= max_dirs) {
188 Jmsg0(jcr, M_WARNING, 0, _("Too many subdirectories. Some permissions not reset.\n"));
191 /* Now set the proper owner and modes */
192 #if defined(HAVE_WIN32)
193 if (path[1] == ':') {
201 /* Skip leading slash(es) */
202 while (IsPathSeparator(*p)) {
205 while ((p = first_path_separator(p))) {
209 if (i < ndir && new_dir[i++] && !keep_dir_modes) {
210 set_own_mod(attr, path, owner, group, parent_mode);
213 while (IsPathSeparator(*p)) {
218 /* Set for final component */
219 if (i < ndir && new_dir[i++]) {
220 set_own_mod(attr, path, owner, group, mode);