forked from cfengine/core
/
verify_storage.c
485 lines (388 loc) · 12.6 KB
/
verify_storage.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
Copyright (C) Cfengine AS
This file is part of Cfengine 3 - written and maintained by Cfengine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of Cfengine, the applicable Commerical Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include "cf3.defs.h"
#include "cf3.extern.h"
#include "dir.h"
static void FindStoragePromiserObjects(Promise *pp);
static int VerifyFileSystem(char *name, Attributes a, Promise *pp);
static int VerifyFreeSpace(char *file, Attributes a, Promise *pp);
static void VolumeScanArrivals(char *file, Attributes a, Promise *pp);
static int FileSystemMountedCorrectly(Rlist *list, char *name, char *options, Attributes a, Promise *pp);
static int IsForeignFileSystem(struct stat *childstat, char *dir);
#ifndef MINGW
static int VerifyMountPromise(char *file, Attributes a, Promise *pp);
#endif /* NOT MINGW */
/*****************************************************************************/
void *FindAndVerifyStoragePromises(Promise *pp)
{
PromiseBanner(pp);
FindStoragePromiserObjects(pp);
return (void *) NULL;
}
/*****************************************************************************/
static void FindStoragePromiserObjects(Promise *pp)
{
/* Check if we are searching over a regular expression */
LocateFilePromiserGroup(pp->promiser, pp, VerifyStoragePromise);
}
/*****************************************************************************/
void VerifyStoragePromise(char *path, Promise *pp)
{
Attributes a = { {0} };
CfLock thislock;
a = GetStorageAttributes(pp);
CF_OCCUR++;
#ifdef MINGW
if (!a.havemount)
{
CfOut(cf_verbose, "", "storage.mount is not supported on Windows");
}
#endif
/* No parameter conflicts here */
if (a.mount.unmount)
{
if (a.mount.mount_source || a.mount.mount_server)
{
CfOut(cf_verbose, "", " !! An unmount promise indicates a mount-source information - probably in error\n");
}
}
else if (a.havemount)
{
if (a.mount.mount_source == NULL || a.mount.mount_server == NULL)
{
CfOut(cf_error, "", " !! Insufficient specification in mount promise - need source and server\n");
return;
}
}
thislock = AcquireLock(path, VUQNAME, CFSTARTTIME, a, pp, false);
if (thislock.lock == NULL)
{
return;
}
/* Do mounts first */
#ifndef MINGW
if (!MOUNTEDFSLIST && !LoadMountInfo(&MOUNTEDFSLIST))
{
CfOut(cf_error, "", "Couldn't obtain a list of mounted filesystems - aborting\n");
YieldCurrentLock(thislock);
return;
}
if (a.havemount)
{
VerifyMountPromise(path, a, pp);
}
#endif /* NOT MINGW */
/* Then check file system */
if (a.havevolume)
{
VerifyFileSystem(path, a, pp);
if (a.volume.freespace != CF_NOINT)
{
VerifyFreeSpace(path, a, pp);
}
if (a.volume.scan_arrivals)
{
VolumeScanArrivals(path, a, pp);
}
}
YieldCurrentLock(thislock);
}
/*******************************************************************/
/** Level */
/*******************************************************************/
static int VerifyFileSystem(char *name, Attributes a, Promise *pp)
{
struct stat statbuf, localstat;
Dir *dirh;
const struct dirent *dirp;
off_t sizeinbytes = 0;
long filecount = 0;
char buff[CF_BUFSIZE];
CfOut(cf_verbose, "", " -> Checking required filesystem %s\n", name);
if (cfstat(name, &statbuf) == -1)
{
return (false);
}
if (S_ISLNK(statbuf.st_mode))
{
KillGhostLink(name, a, pp);
return (true);
}
if (S_ISDIR(statbuf.st_mode))
{
if ((dirh = OpenDirLocal(name)) == NULL)
{
CfOut(cf_error, "opendir", "Can't open directory %s which checking required/disk\n", name);
return false;
}
for (dirp = ReadDir(dirh); dirp != NULL; dirp = ReadDir(dirh))
{
if (!ConsiderFile(dirp->d_name, name, a, pp))
{
continue;
}
filecount++;
strcpy(buff, name);
if (buff[strlen(buff)] != FILE_SEPARATOR)
{
strcat(buff, FILE_SEPARATOR_STR);
}
strcat(buff, dirp->d_name);
if (lstat(buff, &localstat) == -1)
{
if (S_ISLNK(localstat.st_mode))
{
KillGhostLink(buff, a, pp);
continue;
}
CfOut(cf_error, "lstat", "Can't stat volume %s\n", buff);
continue;
}
sizeinbytes += localstat.st_size;
}
CloseDir(dirh);
if (sizeinbytes < 0)
{
CfOut(cf_verbose, "", "Internal error: count of byte size was less than zero!\n");
return true;
}
if (sizeinbytes < a.volume.sensible_size)
{
cfPS(cf_error, CF_INTERPT, "", pp, a, " !! File system %s is suspiciously small! (%jd bytes)\n", name,
(intmax_t) sizeinbytes);
return (false);
}
if (filecount < a.volume.sensible_count)
{
cfPS(cf_error, CF_INTERPT, "", pp, a, " !! Filesystem %s has only %ld files/directories.\n", name,
filecount);
return (false);
}
}
cfPS(cf_inform, CF_NOP, "", pp, a, " -> Filesystem %s's content seems to be sensible as promised\n", name);
return (true);
}
/*******************************************************************/
static int VerifyFreeSpace(char *file, Attributes a, Promise *pp)
{
struct stat statbuf;
long kilobytes;
#ifdef MINGW
if (!a.volume.check_foreign)
{
CfOut(cf_verbose, "", "storage.volume.check_foreign is not supported on Windows (checking every mount)");
}
#endif /* MINGW */
if (cfstat(file, &statbuf) == -1)
{
CfOut(cf_error, "stat", "Couldn't stat %s checking diskspace\n", file);
return true;
}
#ifndef MINGW
if (!a.volume.check_foreign)
{
if (IsForeignFileSystem(&statbuf, file))
{
CfOut(cf_inform, "", "Filesystem %s is mounted from a foreign system, so skipping it", file);
return true;
}
}
#endif /* NOT MINGW */
kilobytes = a.volume.freespace;
if (kilobytes < 0)
{
int free = (int) GetDiskUsage(file, cfpercent);
kilobytes = -1 * kilobytes;
if (free < (int) kilobytes)
{
cfPS(cf_error, CF_FAIL, "", pp, a,
" !! Free disk space is under %ld%% for volume containing %s (%d%% free)\n", kilobytes, file, free);
return false;
}
}
else
{
off_t free = GetDiskUsage(file, cfabs);
kilobytes = kilobytes / 1024;
if (free < kilobytes)
{
cfPS(cf_error, CF_FAIL, "", pp, a, " !! Disk space under %ld kB for volume containing %s (%lld kB free)\n",
kilobytes, file, (long long) free);
return false;
}
}
return true;
}
/*******************************************************************/
static void VolumeScanArrivals(char *file, Attributes a, Promise *pp)
{
CfOut(cf_verbose, "", "Scan arrival sequence . not yet implemented\n");
}
/*******************************************************************/
static int FileSystemMountedCorrectly(Rlist *list, char *name, char *options, Attributes a, Promise *pp)
{
Rlist *rp;
Mount *mp;
int found = false;
for (rp = list; rp != NULL; rp = rp->next)
{
mp = (Mount *) rp->item;
if (mp == NULL)
{
continue;
}
/* Give primacy to the promised / affected object */
if (strcmp(name, mp->mounton) == 0)
{
/* We have found something mounted on the promiser dir */
found = true;
if (a.mount.mount_source && (strcmp(mp->source, a.mount.mount_source) != 0))
{
CfOut(cf_inform, "", "A different file system (%s:%s) is mounted on %s than what is promised\n",
mp->host, mp->source, name);
return false;
}
else
{
CfOut(cf_verbose, "", " -> File system %s seems to be mounted correctly\n", mp->source);
break;
}
}
}
if (!found)
{
if (!a.mount.unmount)
{
CfOut(cf_verbose, "", " !! File system %s seems not to be mounted correctly\n", name);
CF_MOUNTALL = true;
}
}
return found;
}
/*********************************************************************/
/* Mounting */
/*********************************************************************/
static int IsForeignFileSystem(struct stat *childstat, char *dir)
/* Is FS NFS mounted ? */
{
struct stat parentstat;
char vbuff[CF_BUFSIZE];
strncpy(vbuff, dir, CF_BUFSIZE - 1);
if (vbuff[strlen(vbuff) - 1] == FILE_SEPARATOR)
{
strcat(vbuff, "..");
}
else
{
strcat(vbuff, FILE_SEPARATOR_STR);
strcat(vbuff, "..");
}
if (cfstat(vbuff, &parentstat) == -1)
{
CfOut(cf_verbose, "stat", " !! Unable to stat %s", vbuff);
return (false);
}
if (childstat->st_dev != parentstat.st_dev)
{
Rlist *rp;
Mount *entry;
CfDebug("[%s is on a different file system, not descending]\n", dir);
for (rp = MOUNTEDFSLIST; rp != NULL; rp = rp->next)
{
entry = (Mount *) rp->item;
if (!strcmp(entry->mounton, dir))
{
if (entry->options && strstr(entry->options, "nfs"))
{
return (true);
}
}
}
}
CfDebug("NotMountedFileSystem\n");
return (false);
}
/*********************************************************************/
/* Unix-specific implementations */
/*********************************************************************/
#ifndef MINGW
static int VerifyMountPromise(char *name, Attributes a, Promise *pp)
{
char *options;
char dir[CF_BUFSIZE];
int changes = 0;
CfOut(cf_verbose, "", " -> Verifying mounted file systems on %s\n", name);
snprintf(dir, CF_BUFSIZE, "%s/.", name);
if (!IsPrivileged())
{
cfPS(cf_error, CF_INTERPT, "", pp, a, "Only root can mount filesystems.\n");
return false;
}
options = Rlist2String(a.mount.mount_options, ",");
if (!FileSystemMountedCorrectly(MOUNTEDFSLIST, name, options, a, pp))
{
if (!a.mount.unmount)
{
if (!MakeParentDirectory(dir, a.move_obstructions))
{
}
if (a.mount.editfstab)
{
changes += VerifyInFstab(name, a, pp);
}
else
{
cfPS(cf_inform, CF_FAIL, "", pp, a,
" -> Filesystem %s was not mounted as promised, and no edits were promised in %s\n", name,
VFSTAB[VSYSTEMHARDCLASS]);
// Mount explicitly
VerifyMount(name, a, pp);
}
}
else
{
if (a.mount.editfstab)
{
changes += VerifyNotInFstab(name, a, pp);
}
}
if (changes)
{
CF_MOUNTALL = true;
}
}
else
{
if (a.mount.unmount)
{
VerifyUnmount(name, a, pp);
if (a.mount.editfstab)
{
VerifyNotInFstab(name, a, pp);
}
}
else
{
cfPS(cf_inform, CF_NOP, "", pp, a, " -> Filesystem %s seems to be mounted as promised\n", name);
}
}
free(options);
return true;
}
#endif /* NOT MINGW */