Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scan.cc « stored « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 857a877f287e8bd373ea4e1343da0ed01c2b6928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
   Copyright (C) 2016-2022 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
// Kern Sibbald, MMVI
/**
 * scan.c scan a directory (on a removable file) for a valid
 * Volume name. If found, open the file for append.
 */

#include "include/bareos.h"
#include "stored/stored.h"
#include "stored/device_control_record.h"
#include "lib/berrno.h"

namespace storagedaemon {

/* Forward referenced functions */
static bool IsVolumeNameLegal(char* name);

bool Device::ScanForVolume(DeviceControlRecord* dcr)
{
  return ScanForVolumeImpl(dcr);  // call virtual implementation
}

/* default implementation: don't scan at all */
bool Device::ScanForVolumeImpl(DeviceControlRecord*) { return false; }

bool Device::ScanDirectoryForVolume(DeviceControlRecord* dcr)
{
  DIR* dp;
  struct dirent* result;
#ifdef USE_READDIR_R
  struct dirent* entry;
#endif
  int name_max;
  char* mount_point;
  VolumeCatalogInfo dcrVolCatInfo, devVolCatInfo;
  char VolumeName[MAX_NAME_LENGTH];
  struct stat statp;
  bool found = false;
  PoolMem fname(PM_FNAME);
  bool need_slash = false;
  int len;

  dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
  devVolCatInfo = VolCatInfo;      /* structure assignment */
  bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));

  name_max = pathconf(".", _PC_NAME_MAX);
  if (name_max < 1024) { name_max = 1024; }

  if (device_resource->mount_point) {
    mount_point = device_resource->mount_point;
  } else {
    mount_point = device_resource->archive_device_string;
  }

  if (!(dp = opendir(mount_point))) {
    BErrNo be;
    dev_errno = errno;
    Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
          mount_point, print_name(), be.bstrerror());
    goto get_out;
  }

  len = strlen(mount_point);
  if (len > 0) { need_slash = !IsPathSeparator(mount_point[len - 1]); }

#ifdef USE_READDIR_R
  entry = (struct dirent*)malloc(sizeof(struct dirent) + name_max + 1000);
  while (1) {
    if ((Readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
#else
  while (1) {
    result = readdir(dp);
    if (result == NULL) {
#endif
      dev_errno = EIO;
      Dmsg2(
          129,
          "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
          mount_point, print_name());
      break;
    }
    if (bstrcmp(result->d_name, ".") || bstrcmp(result->d_name, "..")) {
      continue;
    }

    if (!IsVolumeNameLegal(result->d_name)) { continue; }
    PmStrcpy(fname, mount_point);
    if (need_slash) { PmStrcat(fname, "/"); }
    PmStrcat(fname, result->d_name);
    if (lstat(fname.c_str(), &statp) != 0 || !S_ISREG(statp.st_mode)) {
      continue; /* ignore directories & special files */
    }

    /*
     * OK, we got a different volume mounted. First save the
     *  requested Volume info (dcr) structure, then query if
     *  this volume is really OK. If not, put back the desired
     *  volume name, mark it not in changer and continue.
     */
    /* Check if this is a valid Volume in the pool */
    bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
    if (!dcr->DirGetVolumeInfo(GET_VOL_INFO_FOR_WRITE)) { continue; }
    /* This was not the volume we expected, but it is OK with
     * the Director, so use it.
     */
    VolCatInfo = dcr->VolCatInfo; /* structure assignment */
    found = true;
    break; /* got a Volume */
  }
#ifdef USE_READDIR_R
  free(entry);
#endif
  closedir(dp);

get_out:
  if (!found) {
    /* Restore VolumeName we really wanted */
    bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
    dcr->VolCatInfo = dcrVolCatInfo; /* structure assignment */
    VolCatInfo = devVolCatInfo;      /* structure assignment */
  }
  return found;
}

/**
 * Check if the Volume name has legal characters
 * If ua is non-NULL send the message
 */
static bool IsVolumeNameLegal(char* name)
{
  int len;
  const char* p;
  const char* accept = ":.-_/";

  if (name[0] == '/') { return false; }
  /* Restrict the characters permitted in the Volume name */
  for (p = name; *p; p++) {
    if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
      continue;
    }
    return false;
  }
  len = strlen(name);
  if (len >= MAX_NAME_LENGTH) { return false; }
  if (len == 0) { return false; }
  return true;
}

} /* namespace storagedaemon */