1 /*
2 Bacula(R) - The Network Backup Solution
3
4 Copyright (C) 2000-2020 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 int breaddir(DIR *dirp, POOLMEM *&d_name);
32
33 /* Forward referenced functions */
34 static bool is_volume_name_legal(char *name);
35
36
scan_dir_for_volume(DCR * dcr)37 bool DEVICE::scan_dir_for_volume(DCR *dcr)
38 {
39 DIR* dp;
40 int name_max;
41 char *mount_point;
42 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
43 char VolumeName[MAX_NAME_LENGTH];
44 struct stat statp;
45 bool found = false;
46 POOL_MEM fname(PM_FNAME);
47 POOL_MEM dname(PM_FNAME);
48 bool need_slash = false;
49 int len;
50
51 dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
52 devVolCatInfo = VolCatInfo; /* structure assignment */
53 bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
54
55 name_max = pathconf(".", _PC_NAME_MAX);
56 if (name_max < 1024) {
57 name_max = 1024;
58 }
59
60 if (device->mount_point) {
61 mount_point = device->mount_point;
62 } else {
63 mount_point = device->device_name;
64 }
65
66 if (!(dp = opendir(mount_point))) {
67 berrno be;
68 dev_errno = errno;
69 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
70 mount_point, print_name(), be.bstrerror());
71 goto get_out;
72 }
73
74 len = strlen(mount_point);
75 if (len > 0) {
76 need_slash = !IsPathSeparator(mount_point[len - 1]);
77 }
78 for ( ;; ) {
79 if (breaddir(dp, dname.addr()) != 0) {
80 dev_errno = EIO;
81 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
82 mount_point, print_name());
83 break;
84 }
85 if (strcmp(dname.c_str(), ".") == 0 ||
86 strcmp(dname.c_str(), "..") == 0) {
87 continue;
88 }
89
90 if (!is_volume_name_legal(dname.c_str())) {
91 continue;
92 }
93 pm_strcpy(fname, mount_point);
94 if (need_slash) {
95 pm_strcat(fname, "/");
96 }
97 pm_strcat(fname, dname);
98 if (lstat(fname.c_str(), &statp) != 0 ||
99 !S_ISREG(statp.st_mode)) {
100 continue; /* ignore directories & special files */
101 }
102
103 /*
104 * OK, we got a different volume mounted. First save the
105 * requested Volume info (dcr) structure, then query if
106 * this volume is really OK. If not, put back the desired
107 * volume name, mark it not in changer and continue.
108 */
109 bstrncpy(dcr->VolumeName, dname.c_str(), sizeof(dcr->VolumeName));
110 /* Check if this is a valid Volume in the pool */
111 if (!dir_get_volume_info(dcr, dcr->VolumeName, GET_VOL_INFO_FOR_WRITE)) {
112 continue;
113 }
114 /* This was not the volume we expected, but it is OK with
115 * the Director, so use it.
116 */
117 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
118 found = true;
119 break; /* got a Volume */
120 }
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 */
is_volume_name_legal(char * name)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 }
160