]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/scan.c
Big backport from Enterprise
[bacula/bacula] / bacula / src / stored / scan.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 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  *
21  *   scan.c scan a directory (on a removable file) for a valid
22  *      Volume name. If found, open the file for append.
23  *
24  *    Kern Sibbald, MMVI
25  *
26  */
27
28 #include "bacula.h"
29 #include "stored.h"
30
31 /* Forward referenced functions */
32 static bool is_volume_name_legal(char *name);
33
34
35 bool DEVICE::scan_dir_for_volume(DCR *dcr)
36 {
37    DIR* dp;
38    struct dirent *entry, *result;
39    int name_max;
40    char *mount_point;
41    VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
42    char VolumeName[MAX_NAME_LENGTH];
43    struct stat statp;
44    bool found = false;
45    POOL_MEM fname(PM_FNAME);
46    bool need_slash = false;
47    int len;
48
49    dcrVolCatInfo = dcr->VolCatInfo;     /* structure assignment */
50    devVolCatInfo = VolCatInfo;          /* structure assignment */
51    bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
52
53    name_max = pathconf(".", _PC_NAME_MAX);
54    if (name_max < 1024) {
55       name_max = 1024;
56    }
57
58    if (device->mount_point) {
59       mount_point = device->mount_point;
60    } else {
61       mount_point = device->device_name;
62    }
63
64    if (!(dp = opendir(mount_point))) {
65       berrno be;
66       dev_errno = errno;
67       Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
68             mount_point, print_name(), be.bstrerror());
69       goto get_out;
70    }
71
72    len = strlen(mount_point);
73    if (len > 0) {
74       need_slash = !IsPathSeparator(mount_point[len - 1]);
75    }
76    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
77    for ( ;; ) {
78       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
79          dev_errno = EIO;
80          Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
81                mount_point, print_name());
82          break;
83       }
84       if (strcmp(result->d_name, ".") == 0 ||
85           strcmp(result->d_name, "..") == 0) {
86          continue;
87       }
88
89       if (!is_volume_name_legal(result->d_name)) {
90          continue;
91       }
92       pm_strcpy(fname, mount_point);
93       if (need_slash) {
94          pm_strcat(fname, "/");
95       }
96       pm_strcat(fname, result->d_name);
97       if (lstat(fname.c_str(), &statp) != 0 ||
98           !S_ISREG(statp.st_mode)) {
99          continue;                 /* ignore directories & special files */
100       }
101
102       /*
103        * OK, we got a different volume mounted. First save the
104        *  requested Volume info (dcr) structure, then query if
105        *  this volume is really OK. If not, put back the desired
106        *  volume name, mark it not in changer and continue.
107        */
108       bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
109       /* Check if this is a valid Volume in the pool */
110       if (!dir_get_volume_info(dcr, dcr->VolumeName, GET_VOL_INFO_FOR_WRITE)) {
111          continue;
112       }
113       /* This was not the volume we expected, but it is OK with
114        * the Director, so use it.
115        */
116       VolCatInfo = dcr->VolCatInfo;       /* structure assignment */
117       found = true;
118       break;                /* got a Volume */
119    }
120    free(entry);
121    closedir(dp);
122
123 get_out:
124    if (!found) {
125       /* Restore VolumeName we really wanted */
126       bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
127       dcr->VolCatInfo = dcrVolCatInfo;     /* structure assignment */
128       VolCatInfo = devVolCatInfo;          /* structure assignment */
129    }
130    Dsm_check(100);
131    return found;
132 }
133
134 /*
135  * Check if the Volume name has legal characters
136  * If ua is non-NULL send the message
137  */
138 static bool is_volume_name_legal(char *name)
139 {
140    int len;
141    const char *p;
142    const char *accept = ":.-_";
143
144    /* Restrict the characters permitted in the Volume name */
145    for (p=name; *p; p++) {
146       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
147          continue;
148       }
149       return false;
150    }
151    len = strlen(name);
152    if (len >= MAX_NAME_LENGTH) {
153       return false;
154    }
155    if (len == 0) {
156       return false;
157    }
158    return true;
159 }