1 /*-
2 * Copyright (c) 2005-2006 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
6 *
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
9 * the following conditions are met:
10 *
11 * 1. Redistributions of source code or documentation must retain the above
12 * copyright notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Host Resources MIB for SNMPd. Implementation for hrFSTable
32 */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/mount.h>
38
39 #include <assert.h>
40 #include <err.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <sysexits.h>
45
46 #include "hostres_snmp.h"
47 #include "hostres_oid.h"
48 #include "hostres_tree.h"
49
50 /*
51 * File system access enum
52 */
53 enum hrFSAccess {
54 FS_READ_WRITE = 1,
55 FS_READ_ONLY = 2
56 };
57
58 /* maximum length (according to MIB) for fs_entry::mountPoint */
59 #define FS_MP_MLEN (128 + 1)
60
61 /* maximum length (according to MIB) for fs_entry::remoteMountPoint */
62 #define FS_RMP_MLEN (128 + 1)
63
64 /*
65 * This structure is used to hold a SNMP table entry
66 * for HOST-RESOURCES-MIB's hrFSTable
67 */
68 struct fs_entry {
69 int32_t index;
70 u_char *mountPoint;
71 u_char *remoteMountPoint;
72 const struct asn_oid *type;
73 int32_t access; /* enum hrFSAccess, see above */
74 int32_t bootable; /* TruthValue */
75 int32_t storageIndex; /* hrStorageTblEntry::index */
76 u_char lastFullBackupDate[11];
77 u_char lastPartialBackupDate[11];
78 #define HR_FS_FOUND 0x001
79 uint32_t flags; /* not in mib table, for internal use */
80 TAILQ_ENTRY(fs_entry) link;
81 };
82 TAILQ_HEAD(fs_tbl, fs_entry);
83
84 /*
85 * Next structure is used to keep o list of mappings from a specific name
86 * (a_name) to an entry in the hrFSTblEntry. We are trying to keep the same
87 * index for a specific name at least for the duration of one SNMP agent run.
88 */
89 struct fs_map_entry {
90 int32_t hrIndex; /* used for fs_entry::index */
91 u_char *a_name; /* map key same as fs_entry::mountPoint */
92
93 /* may be NULL if the respective hrFSTblEntry is (temporally) gone */
94 struct fs_entry *entry;
95 STAILQ_ENTRY(fs_map_entry) link;
96 };
97 STAILQ_HEAD(fs_map, fs_map_entry);
98
99 /* head of the list with hrFSTable's entries */
100 static struct fs_tbl fs_tbl = TAILQ_HEAD_INITIALIZER(fs_tbl);
101
102 /* for consistent table indexing */
103 static struct fs_map fs_map = STAILQ_HEAD_INITIALIZER(fs_map);
104
105 /* next index available for hrFSTable */
106 static uint32_t next_fs_index = 1;
107
108 /* last tick when hrFSTable was updated */
109 static uint64_t fs_tick;
110
111 /* maximum number of ticks between refreshs */
112 uint32_t fs_tbl_refresh = HR_FS_TBL_REFRESH * 100;
113
114 /* some constants */
115 static const struct asn_oid OIDX_hrFSBerkeleyFFS_c = OIDX_hrFSBerkeleyFFS;
116 static const struct asn_oid OIDX_hrFSiso9660_c = OIDX_hrFSiso9660;
117 static const struct asn_oid OIDX_hrFSNFS_c = OIDX_hrFSNFS;
118 static const struct asn_oid OIDX_hrFSLinuxExt2_c = OIDX_hrFSLinuxExt2;
119 static const struct asn_oid OIDX_hrFSOther_c = OIDX_hrFSOther;
120 static const struct asn_oid OIDX_hrFSFAT32_c = OIDX_hrFSFAT32;
121 static const struct asn_oid OIDX_hrFSNTFS_c = OIDX_hrFSNTFS;
122 static const struct asn_oid OIDX_hrFSNetware_c = OIDX_hrFSNetware;
123 static const struct asn_oid OIDX_hrFSHPFS_c = OIDX_hrFSHPFS;
124 static const struct asn_oid OIDX_hrFSUnknown_c = OIDX_hrFSUnknown;
125
126 /* file system type map */
127 static const struct {
128 const char *str; /* the type string */
129 const struct asn_oid *oid; /* the OID to return */
130 } fs_type_map[] = {
131 { "ufs", &OIDX_hrFSBerkeleyFFS_c },
132 { "zfs", &OIDX_hrFSOther_c },
133 { "cd9660", &OIDX_hrFSiso9660_c },
134 { "nfs", &OIDX_hrFSNFS_c },
135 { "ext2fs", &OIDX_hrFSLinuxExt2_c },
136 { "procfs", &OIDX_hrFSOther_c },
137 { "devfs", &OIDX_hrFSOther_c },
138 { "msdosfs", &OIDX_hrFSFAT32_c },
139 { "ntfs", &OIDX_hrFSNTFS_c },
140 { "nwfs", &OIDX_hrFSNetware_c },
141 { "hpfs", &OIDX_hrFSHPFS_c },
142 { "smbfs", &OIDX_hrFSOther_c },
143 };
144 #define N_FS_TYPE_MAP nitems(fs_type_map)
145
146 /**
147 * Create an entry into the FS table and an entry in the map (if needed).
148 */
149 static struct fs_entry *
fs_entry_create(const char * name)150 fs_entry_create(const char *name)
151 {
152 struct fs_entry *entry;
153 struct fs_map_entry *map;
154
155 assert(name != NULL);
156 assert(strlen(name) > 0);
157
158 STAILQ_FOREACH(map, &fs_map, link)
159 if (strcmp(map->a_name, name) == 0)
160 break;
161
162 if (map == NULL) {
163 size_t mount_point_len;
164
165 /* new object - get a new index */
166 if (next_fs_index > INT_MAX) {
167 /* Unrecoverable error - die clean and quicly*/
168 syslog(LOG_ERR, "%s: hrFSTable index wrap", __func__);
169 errx(EX_SOFTWARE, "hrFSTable index wrap");
170 }
171
172 if ((map = malloc(sizeof(*map))) == NULL) {
173 syslog(LOG_ERR, "%s: %m", __func__);
174 return (NULL);
175 }
176
177 mount_point_len = strlen(name) + 1;
178 if (mount_point_len > FS_MP_MLEN)
179 mount_point_len = FS_MP_MLEN;
180
181 if ((map->a_name = malloc(mount_point_len)) == NULL) {
182 syslog(LOG_ERR, "%s: %m", __func__);
183 free(map);
184 return (NULL);
185 }
186
187 strlcpy(map->a_name, name, mount_point_len);
188
189 map->hrIndex = next_fs_index++;
190 map->entry = NULL;
191 STAILQ_INSERT_TAIL(&fs_map, map, link);
192
193 HRDBG("%s added into hrFSMap at index=%d", name, map->hrIndex);
194 } else {
195 HRDBG("%s exists in hrFSMap index=%d", name, map->hrIndex);
196 }
197
198 if ((entry = malloc(sizeof(*entry))) == NULL) {
199 syslog(LOG_WARNING, "%s: %m", __func__);
200 return (NULL);
201 }
202
203 if ((entry->mountPoint = strdup(name)) == NULL) {
204 syslog(LOG_ERR, "%s: %m", __func__);
205 free(entry);
206 return (NULL);
207 }
208 entry->remoteMountPoint = NULL;
209
210 entry->index = map->hrIndex;
211 map->entry = entry;
212
213 INSERT_OBJECT_INT(entry, &fs_tbl);
214 return (entry);
215 }
216
217 /**
218 * Delete an entry in the FS table.
219 */
220 static void
fs_entry_delete(struct fs_entry * entry)221 fs_entry_delete(struct fs_entry* entry)
222 {
223 struct fs_map_entry *map;
224
225 assert(entry != NULL);
226
227 TAILQ_REMOVE(&fs_tbl, entry, link);
228 STAILQ_FOREACH(map, &fs_map, link)
229 if (map->entry == entry) {
230 map->entry = NULL;
231 break;
232 }
233 free(entry->mountPoint);
234 free(entry->remoteMountPoint);
235 free(entry);
236 }
237
238 /**
239 * Find a table entry by its name
240 */
241 static struct fs_entry *
fs_find_by_name(const char * name)242 fs_find_by_name(const char *name)
243 {
244 struct fs_entry *entry;
245
246 TAILQ_FOREACH(entry, &fs_tbl, link)
247 if (strcmp(entry->mountPoint, name) == 0)
248 return (entry);
249
250 return (NULL);
251 }
252
253 /**
254 * Get rid of all data
255 */
256 void
fini_fs_tbl(void)257 fini_fs_tbl(void)
258 {
259 struct fs_map_entry *n1;
260
261 while ((n1 = STAILQ_FIRST(&fs_map)) != NULL) {
262 STAILQ_REMOVE_HEAD(&fs_map, link);
263 if (n1->entry != NULL) {
264 TAILQ_REMOVE(&fs_tbl, n1->entry, link);
265 free(n1->entry->mountPoint);
266 free(n1->entry->remoteMountPoint);
267 free(n1->entry);
268 }
269 free(n1->a_name);
270 free(n1);
271 }
272 assert(TAILQ_EMPTY(&fs_tbl));
273 }
274
275 /**
276 * Called before the refreshing is started from the storage table.
277 */
278 void
fs_tbl_pre_refresh(void)279 fs_tbl_pre_refresh(void)
280 {
281 struct fs_entry *entry;
282
283 /* mark each entry as missisng */
284 TAILQ_FOREACH(entry, &fs_tbl, link)
285 entry->flags &= ~HR_FS_FOUND;
286 }
287
288 /**
289 * Called after refreshing from the storage table.
290 */
291 void
fs_tbl_post_refresh(void)292 fs_tbl_post_refresh(void)
293 {
294 struct fs_entry *entry, *entry_tmp;
295
296 /*
297 * Purge items that disappeared
298 */
299 TAILQ_FOREACH_SAFE(entry, &fs_tbl, link, entry_tmp)
300 if (!(entry->flags & HR_FS_FOUND))
301 fs_entry_delete(entry);
302
303 fs_tick = this_tick;
304 }
305
306 /*
307 * Refresh the FS table. This is done by forcing a refresh of the storage table.
308 */
309 void
refresh_fs_tbl(void)310 refresh_fs_tbl(void)
311 {
312
313 if (fs_tick == 0 || this_tick - fs_tick >= fs_tbl_refresh) {
314 refresh_storage_tbl(1);
315 HRDBG("refresh DONE");
316 }
317 }
318
319 /**
320 * Get the type OID for a given file system
321 */
322 const struct asn_oid *
fs_get_type(const struct statfs * fs_p)323 fs_get_type(const struct statfs *fs_p)
324 {
325 u_int t;
326
327 assert(fs_p != NULL);
328
329 for (t = 0; t < N_FS_TYPE_MAP; t++)
330 if (strcmp(fs_type_map[t].str, fs_p->f_fstypename) == 0)
331 return (fs_type_map[t].oid);
332
333 return (&OIDX_hrFSUnknown_c);
334 }
335
336 /*
337 * Given information returned from statfs(2) either create a new entry into
338 * the fs_tbl or refresh the entry if it is already there.
339 */
340 void
fs_tbl_process_statfs_entry(const struct statfs * fs_p,int32_t storage_idx)341 fs_tbl_process_statfs_entry(const struct statfs *fs_p, int32_t storage_idx)
342 {
343 struct fs_entry *entry;
344
345 assert(fs_p != 0);
346
347 HRDBG("for hrStorageEntry::index %d", storage_idx);
348
349 if (fs_p == NULL)
350 return;
351
352 if ((entry = fs_find_by_name(fs_p->f_mntonname)) != NULL ||
353 (entry = fs_entry_create(fs_p->f_mntonname)) != NULL) {
354 entry->flags |= HR_FS_FOUND;
355
356 free(entry->remoteMountPoint);
357 entry->remoteMountPoint = NULL;
358
359 if (!(fs_p->f_flags & MNT_LOCAL)) {
360 /* this is a remote mount */
361 entry->remoteMountPoint = strdup(fs_p->f_mntfromname);
362 /* if strdup failed, let it be NULL */
363
364 } else {
365 entry->remoteMountPoint = strdup("");
366 /* if strdup failed, let it be NULL */
367 }
368
369 entry->type = fs_get_type(fs_p);
370
371 if ((fs_p->f_flags & MNT_RDONLY) == MNT_RDONLY)
372 entry->access = FS_READ_ONLY;
373 else
374 entry->access = FS_READ_WRITE;
375
376 /* FIXME - bootable fs ?! */
377 entry->bootable = TRUTH_MK((fs_p->f_flags & MNT_ROOTFS)
378 == MNT_ROOTFS);
379
380 entry->storageIndex = storage_idx;
381
382 /* Info not available */
383 memset(entry->lastFullBackupDate, 0,
384 sizeof(entry->lastFullBackupDate));
385
386 /* Info not available */
387 memset(entry->lastPartialBackupDate, 0,
388 sizeof(entry->lastPartialBackupDate));
389
390 handle_partition_fs_index(fs_p->f_mntfromname, entry->index);
391 }
392 }
393
394 /*
395 * This is the implementation for a generated (by our SNMP "compiler" tool)
396 * function prototype, see hostres_tree.h
397 * It handles the SNMP operations for hrFSTable
398 */
399 int
op_hrFSTable(struct snmp_context * ctx __unused,struct snmp_value * value,u_int sub,u_int iidx __unused,enum snmp_op curr_op)400 op_hrFSTable(struct snmp_context *ctx __unused, struct snmp_value *value,
401 u_int sub, u_int iidx __unused, enum snmp_op curr_op)
402 {
403 struct fs_entry *entry;
404
405 refresh_fs_tbl();
406
407 switch (curr_op) {
408
409 case SNMP_OP_GETNEXT:
410 if ((entry = NEXT_OBJECT_INT(&fs_tbl,
411 &value->var, sub)) == NULL)
412 return (SNMP_ERR_NOSUCHNAME);
413 value->var.len = sub + 1;
414 value->var.subs[sub] = entry->index;
415 goto get;
416
417 case SNMP_OP_GET:
418 if ((entry = FIND_OBJECT_INT(&fs_tbl,
419 &value->var, sub)) == NULL)
420 return (SNMP_ERR_NOSUCHNAME);
421 goto get;
422
423 case SNMP_OP_SET:
424 if ((entry = FIND_OBJECT_INT(&fs_tbl,
425 &value->var, sub)) == NULL)
426 return (SNMP_ERR_NO_CREATION);
427 return (SNMP_ERR_NOT_WRITEABLE);
428
429 case SNMP_OP_ROLLBACK:
430 case SNMP_OP_COMMIT:
431 abort();
432 }
433 abort();
434 get:
435 switch (value->var.subs[sub - 1]) {
436
437 case LEAF_hrFSIndex:
438 value->v.integer = entry->index;
439 return (SNMP_ERR_NOERROR);
440
441 case LEAF_hrFSMountPoint:
442 return (string_get(value, entry->mountPoint, -1));
443
444 case LEAF_hrFSRemoteMountPoint:
445 if (entry->remoteMountPoint == NULL)
446 return (string_get(value, "", -1));
447 else
448 return (string_get(value, entry->remoteMountPoint, -1));
449 break;
450
451 case LEAF_hrFSType:
452 assert(entry->type != NULL);
453 value->v.oid = *(entry->type);
454 return (SNMP_ERR_NOERROR);
455
456 case LEAF_hrFSAccess:
457 value->v.integer = entry->access;
458 return (SNMP_ERR_NOERROR);
459
460 case LEAF_hrFSBootable:
461 value->v.integer = entry->bootable;
462 return (SNMP_ERR_NOERROR);
463
464 case LEAF_hrFSStorageIndex:
465 value->v.integer = entry->storageIndex;
466 return (SNMP_ERR_NOERROR);
467
468 case LEAF_hrFSLastFullBackupDate:
469 return (string_get(value, entry->lastFullBackupDate, 8));
470
471 case LEAF_hrFSLastPartialBackupDate:
472 return (string_get(value, entry->lastPartialBackupDate, 8));
473 }
474 abort();
475 }
476