summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swinstalled_tbl.c
blob: c7eecc5c1c4a96eaf537eee97cbfd42880c87b99 (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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
 * Copyright (c) 2005-2006 The FreeBSD Project
 * All rights reserved.
 *
 * Author: Victor Cruceru <soc-victor@freebsd.org>
 *
 * Redistribution of this software and documentation and use in source and
 * binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code or documentation must retain the above
 *    copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 *
 * Host Resources MIB implementation for SNMPd: instrumentation for
 * hrSWInstalledTable
 */

#include <sys/limits.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>

#include <assert.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sysexits.h>

#include "hostres_snmp.h"
#include "hostres_oid.h"
#include "hostres_tree.h"

#define	CONTENTS_FNAME	"+CONTENTS"

enum SWInstalledType {
	SWI_UNKNOWN		= 1,
	SWI_OPERATING_SYSTEM	= 2,
	SWI_DEVICE_DRIVER	= 3,
	SWI_APPLICATION		= 4
};

#define	SW_NAME_MLEN	(64 + 1)

/*
 * This structure is used to hold a SNMP table entry
 * for HOST-RESOURCES-MIB's hrSWInstalledTable
 */
struct swins_entry {
	int32_t		index;
	u_char		*name;	/* max len for this is SW_NAME_MLEN */
	const struct asn_oid *id;
	int32_t		type;	/* from enum SWInstalledType */
	u_char		date[11];
	u_int		date_len;

#define	HR_SWINSTALLED_FOUND		0x001
#define	HR_SWINSTALLED_IMMUTABLE	0x002
	uint32_t	flags;

	TAILQ_ENTRY(swins_entry) link;
};
TAILQ_HEAD(swins_tbl, swins_entry);

/*
 * Table to keep a conistent mapping between software and indexes.
 */
struct swins_map_entry {
	int32_t	index;	/* swins_entry::index */
	u_char	*name;	/* map key,a copy of swins_entry::name*/

	/*
	 * next may be NULL if the respective hrSWInstalledTblEntry
	 * is (temporally) gone
	 */
	struct swins_entry *entry;

	STAILQ_ENTRY(swins_map_entry) link;
};
STAILQ_HEAD(swins_map, swins_map_entry);

/* map for consistent indexing */
static struct swins_map swins_map = STAILQ_HEAD_INITIALIZER(swins_map);

/* the head of the list with hrSWInstalledTable's entries */
static struct swins_tbl swins_tbl = TAILQ_HEAD_INITIALIZER(swins_tbl);

/* next int available for indexing the hrSWInstalledTable */
static uint32_t next_swins_index = 1;

/* last (agent) tick when hrSWInstalledTable was updated */
static uint64_t swins_tick;

/* maximum number of ticks between updates of network table */
uint32_t swins_tbl_refresh = HR_SWINS_TBL_REFRESH * 100;

/* package directory */
u_char *pkg_dir;

/* last change of package list */
static time_t os_pkg_last_change;

/**
 * Create a new entry into the hrSWInstalledTable
 */
static struct swins_entry *
swins_entry_create(const char *name)
{
	struct swins_entry *entry;
	struct swins_map_entry *map;

	STAILQ_FOREACH(map, &swins_map, link)
		if (strcmp((const char *)map->name, name) == 0)
			break;

	if (map == NULL) {
		size_t name_len;
		/* new object - get a new index */
		if (next_swins_index > INT_MAX) {
			syslog(LOG_ERR, "%s: hrSWInstalledTable index wrap",
			    __func__ );
			/* There isn't much we can do here.
			 * If the next_swins_index is consumed
			 * then we can't add entries to this table
			 * So it is better to exit - if the table is sparsed
			 * at the next agent run we can fill it fully.
			 */
			errx(EX_SOFTWARE, "hrSWInstalledTable index wrap");
		}

		if ((map = malloc(sizeof(*map))) == NULL) {
			syslog(LOG_ERR, "%s: %m", __func__ );
			return (NULL);
		}

		name_len = strlen(name) + 1;
		if (name_len > SW_NAME_MLEN)
			 name_len = SW_NAME_MLEN;

		if ((map->name = malloc(name_len)) == NULL) {
			syslog(LOG_WARNING, "%s: %m", __func__);
			free(map);
			return (NULL);
		}

		map->index = next_swins_index++;
		strlcpy((char *)map->name, name, name_len);

		STAILQ_INSERT_TAIL(&swins_map, map, link);

		HRDBG("%s added into hrSWInstalled at %d", name, map->index);
	}

	if ((entry = malloc(sizeof(*entry))) == NULL) {
		syslog(LOG_WARNING, "%s: %m", __func__);
		return (NULL);
	}
	memset(entry, 0, sizeof(*entry));

	if ((entry->name = strdup(map->name)) == NULL) {
		syslog(LOG_WARNING, "%s: %m", __func__);
		free(entry);
		return (NULL);
	}

	entry->index = map->index;
	map->entry = entry;

	INSERT_OBJECT_INT(entry, &swins_tbl);

	return (entry);
}

/**
 * Delete an entry in the hrSWInstalledTable
 */
static void
swins_entry_delete(struct swins_entry *entry)
{
	struct swins_map_entry *map;

	assert(entry != NULL);

	TAILQ_REMOVE(&swins_tbl, entry, link);

	STAILQ_FOREACH(map, &swins_map, link)
		if (map->entry == entry) {
			map->entry = NULL;
			break;
		}

	free(entry->name);
	free(entry);
}

/**
 * Find an entry given it's name
 */
static struct swins_entry *
swins_find_by_name(const char *name)
{
	struct swins_entry *entry;

	TAILQ_FOREACH(entry, &swins_tbl, link)
		if (strcmp((const char*)entry->name, name) == 0)
			return (entry);
	return (NULL);
}

/**
 * Finalize this table
 */
void
fini_swins_tbl(void)
{
	struct swins_map_entry  *n1;

	while ((n1 = STAILQ_FIRST(&swins_map)) != NULL) {
		STAILQ_REMOVE_HEAD(&swins_map, link);
		if (n1->entry != NULL) {
			TAILQ_REMOVE(&swins_tbl, n1->entry, link);
			free(n1->entry->name);
			free(n1->entry);
		}
		free(n1->name);
		free(n1);
	}
	assert(TAILQ_EMPTY(&swins_tbl));
}

/**
 * Get the *running* O/S identification
 */
static void
swins_get_OS_ident(void)
{
	struct utsname os_id;
	char os_string[SW_NAME_MLEN] = "";
	struct swins_entry *entry;
	u_char *boot;
	struct stat sb;
	struct tm k_ts;

	if (uname(&os_id) == -1) {
		syslog(LOG_WARNING, "%s: %m", __func__);
		return;
	}

	snprintf(os_string, sizeof(os_string), "%s: %s",
	    os_id.sysname, os_id.version);

	if ((entry = swins_find_by_name(os_string)) != NULL ||
	    (entry = swins_entry_create(os_string)) == NULL)
		return;

	entry->flags |= (HR_SWINSTALLED_FOUND | HR_SWINSTALLED_IMMUTABLE);
	entry->id = &oid_zeroDotZero;
	entry->type = (int32_t)SWI_OPERATING_SYSTEM;
	memset(entry->date, 0, sizeof(entry->date));

	if (OS_getSystemInitialLoadParameters(&boot) == SNMP_ERR_NOERROR &&
	    strlen(boot) > 0 && stat(boot, &sb) == 0 &&
	    localtime_r(&sb.st_ctime, &k_ts) != NULL)
		entry->date_len = make_date_time(entry->date, &k_ts, 0);
}

/**
 * Read the installed packages
 */
static int
swins_get_packages(void)
{
	struct stat sb;
	DIR *p_dir;
	struct dirent *ent;
	struct tm k_ts;
	char *pkg_file;
	struct swins_entry *entry;
	int ret = 0;

	if (pkg_dir == NULL)
		/* initialisation may have failed */
		return (-1);

	if (stat(pkg_dir, &sb) != 0) {
		syslog(LOG_ERR, "hrSWInstalledTable: stat(\"%s\") failed: %m",
		    pkg_dir);
		return (-1);
	}
	if (!S_ISDIR(sb.st_mode)) {
		syslog(LOG_ERR, "hrSWInstalledTable: \"%s\" is not a directory",
		    pkg_dir);
		return (-1);
	}
	if (sb.st_ctime <= os_pkg_last_change) {
		HRDBG("no need to rescan installed packages -- "
		    "directory time-stamp unmodified");

		TAILQ_FOREACH(entry, &swins_tbl, link)
			entry->flags |= HR_SWINSTALLED_FOUND;

		return (0);
	}

	if ((p_dir = opendir(pkg_dir)) == NULL) {
		syslog(LOG_ERR, "hrSWInstalledTable: opendir(\"%s\") failed: "
		    "%m", pkg_dir);
		return (-1);
	}

	while (errno = 0, (ent = readdir(p_dir)) != NULL) {
		HRDBG("  pkg file: %s", ent->d_name);

		/* check that the contents file is a regular file */
		if (asprintf(&pkg_file, "%s/%s/%s", pkg_dir, ent->d_name,
		    CONTENTS_FNAME) == -1)
			continue;

		if (stat(pkg_file, &sb) != 0 ) {
			free(pkg_file);
			continue;
		}

		if (!S_ISREG(sb.st_mode)) {
			syslog(LOG_ERR, "hrSWInstalledTable: \"%s\" not a "
			    "regular file -- skipped", pkg_file);
			free(pkg_file);
			continue;
		}
		free(pkg_file);

		/* read directory timestamp on package */
		if (asprintf(&pkg_file, "%s/%s", pkg_dir, ent->d_name) == -1)
			continue;

		if (stat(pkg_file, &sb) == -1 ||
		    localtime_r(&sb.st_ctime, &k_ts) == NULL) {
			free(pkg_file);
			continue;
		}
		free(pkg_file);

		/* update or create entry */
		if ((entry = swins_find_by_name(ent->d_name)) == NULL &&
		    (entry = swins_entry_create(ent->d_name)) == NULL) {
			ret = -1;
			goto PKG_LOOP_END;
		}

		entry->flags |= HR_SWINSTALLED_FOUND;
		entry->id = &oid_zeroDotZero;
		entry->type = (int32_t)SWI_APPLICATION;

		entry->date_len = make_date_time(entry->date, &k_ts, 0);
	}

	if (errno != 0) {
		syslog(LOG_ERR, "hrSWInstalledTable: readdir_r(\"%s\") failed:"
		    " %m", pkg_dir);
		ret = -1;
	} else {
		/*
		 * save the timestamp of directory
		 * to avoid any further scanning
		 */
		os_pkg_last_change = sb.st_ctime;
	}
  PKG_LOOP_END:
	(void)closedir(p_dir);
	return (ret);
}

/**
 * Refresh the installed software table.
 */
void
refresh_swins_tbl(void)
{
	int ret;
	struct swins_entry *entry, *entry_tmp;

	if (this_tick - swins_tick < swins_tbl_refresh) {
		HRDBG("no refresh needed");
		return;
	}

	/* mark each entry as missing */
	TAILQ_FOREACH(entry, &swins_tbl, link)
		entry->flags &= ~HR_SWINSTALLED_FOUND;

	ret = swins_get_packages();

	TAILQ_FOREACH_SAFE(entry, &swins_tbl, link, entry_tmp)
		if (!(entry->flags & HR_SWINSTALLED_FOUND) &&
		    !(entry->flags & HR_SWINSTALLED_IMMUTABLE))
			swins_entry_delete(entry);

	if (ret == 0)
		swins_tick = this_tick;
}

/**
 * Create and populate the package table
 */
void
init_swins_tbl(void)
{

	if ((pkg_dir = malloc(sizeof(PATH_PKGDIR))) == NULL)
		syslog(LOG_ERR, "%s: %m", __func__);
	else
		strcpy(pkg_dir, PATH_PKGDIR);

	swins_get_OS_ident();
	refresh_swins_tbl();

	HRDBG("init done");
}

/**
 * SNMP handler
 */
int
op_hrSWInstalledTable(struct snmp_context *ctx __unused,
    struct snmp_value *value, u_int sub, u_int iidx __unused,
    enum snmp_op curr_op)
{
	struct swins_entry *entry;

	refresh_swins_tbl();

	switch (curr_op) {

	  case SNMP_OP_GETNEXT:
		if ((entry = NEXT_OBJECT_INT(&swins_tbl,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		value->var.len = sub + 1;
		value->var.subs[sub] = entry->index;
		goto get;

	  case SNMP_OP_GET:
		if ((entry = FIND_OBJECT_INT(&swins_tbl,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	  case SNMP_OP_SET:
		if ((entry = FIND_OBJECT_INT(&swins_tbl,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NO_CREATION);
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
		abort();
	}
	abort();

  get:
	switch (value->var.subs[sub - 1]) {

	  case LEAF_hrSWInstalledIndex:
		value->v.integer = entry->index;
		return (SNMP_ERR_NOERROR);

	  case LEAF_hrSWInstalledName:
		return (string_get(value, entry->name, -1));
		break;

	  case LEAF_hrSWInstalledID:
		assert(entry->id != NULL);
		value->v.oid = *entry->id;
		return (SNMP_ERR_NOERROR);

	  case LEAF_hrSWInstalledType:
		value->v.integer = entry->type;
		return (SNMP_ERR_NOERROR);

	  case LEAF_hrSWInstalledDate:
		return (string_get(value, entry->date, entry->date_len));
	}
	abort();
}

/**
 * Scalars
 */
int
op_hrSWInstalled(struct snmp_context *ctx __unused,
    struct snmp_value *value __unused, u_int sub,
    u_int iidx __unused, enum snmp_op curr_op)
{

	/* only SNMP GET is possible */
	switch (curr_op) {

	case SNMP_OP_GET:
		goto get;

	case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	case SNMP_OP_ROLLBACK:
	case SNMP_OP_COMMIT:
	case SNMP_OP_GETNEXT:
		abort();
	}
	abort();

  get:
	switch (value->var.subs[sub - 1]) {

	case LEAF_hrSWInstalledLastChange:
	case LEAF_hrSWInstalledLastUpdateTime:
		/*
		 * We always update the entire table so these two tick
		 * values should be equal.
		 */
		refresh_swins_tbl();
		if (swins_tick <= start_tick)
			value->v.uint32 = 0;
		else {
			uint64_t lastChange = swins_tick - start_tick;

			/* may overflow the SNMP type */
			value->v.uint32 =
			    (lastChange > UINT_MAX ? UINT_MAX : lastChange);
		}

		return (SNMP_ERR_NOERROR);

	default:
		abort();
	}
}
OpenPOWER on IntegriCloud