| 1 |
/* |
| 2 |
* swinst_rpm.c: |
| 3 |
* hrSWInstalledTable data access: |
| 4 |
*/ |
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#ifdef HAVE_STDLIB_H |
| 9 |
#include <stdlib.h> |
| 10 |
#endif |
| 11 |
#ifdef HAVE_UNISTD_H |
| 12 |
#include <unistd.h> |
| 13 |
#endif |
| 14 |
#ifdef HAVE_SYS_TYPES_H |
| 15 |
#include <sys/types.h> |
| 16 |
#endif |
| 17 |
#ifdef HAVE_STRING_H |
| 18 |
#include <string.h> |
| 19 |
#else |
| 20 |
#include <strings.h> |
| 21 |
#endif |
| 22 |
#ifdef HAVE_FCNTL_H |
| 23 |
#include <fcntl.h> |
| 24 |
#endif |
| 25 |
#include <sys/stat.h> |
| 26 |
|
| 27 |
#include <net-snmp/net-snmp-includes.h> |
| 28 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 29 |
#include <net-snmp/library/container.h> |
| 30 |
#include <net-snmp/library/snmp_debug.h> |
| 31 |
#include <net-snmp/data_access/swinst.h> |
| 32 |
#include "swinst_private.h" |
| 33 |
|
| 34 |
config_require(date_n_time); |
| 35 |
|
| 36 |
static char pkg_directory[SNMP_MAXBUF]; |
| 37 |
static char apt_fmt[SNMP_MAXBUF]; |
| 38 |
static char file[SNMP_MAXBUF]; |
| 39 |
|
| 40 |
/* --------------------------------------------------------------------- |
| 41 |
*/ |
| 42 |
void |
| 43 |
netsnmp_swinst_arch_init(void) |
| 44 |
{ |
| 45 |
strlcpy(pkg_directory, "/var/lib/dpkg/info", sizeof(pkg_directory)); |
| 46 |
snprintf(apt_fmt, SNMP_MAXBUF, "%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%ds", |
| 47 |
SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1, |
| 48 |
SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1); |
| 49 |
} |
| 50 |
|
| 51 |
void |
| 52 |
netsnmp_swinst_arch_shutdown(void) |
| 53 |
{ |
| 54 |
/* Nothing to do */ |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
/* --------------------------------------------------------------------- |
| 59 |
*/ |
| 60 |
int |
| 61 |
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags) |
| 62 |
{ |
| 63 |
FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Architecture}#${Status}\n'", "r"); |
| 64 |
char package[SNMP_MAXBUF]; |
| 65 |
char version[SNMP_MAXBUF]; |
| 66 |
char section[SNMP_MAXBUF]; |
| 67 |
char priority[SNMP_MAXBUF]; |
| 68 |
char essential[SNMP_MAXBUF]; |
| 69 |
char arch[SNMP_MAXBUF]; |
| 70 |
char status[SNMP_MAXBUF]; |
| 71 |
char buf[BUFSIZ]; |
| 72 |
struct stat stat_buf; |
| 73 |
netsnmp_swinst_entry *entry; |
| 74 |
u_char *date_buf; |
| 75 |
size_t date_len; |
| 76 |
int i = 1; |
| 77 |
|
| 78 |
if (p == NULL) { |
| 79 |
snmp_perror("dpkg-list"); |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
while (fgets(buf, BUFSIZ, p)) { |
| 84 |
DEBUGMSG(("swinst_apt", "entry: %s\n", buf)); |
| 85 |
entry = netsnmp_swinst_entry_create( i++ ); |
| 86 |
if (NULL == entry) |
| 87 |
continue; /* error already logged by function */ |
| 88 |
CONTAINER_INSERT(container, entry); |
| 89 |
|
| 90 |
sscanf(buf, apt_fmt, package, version, section, priority, essential, arch, status); |
| 91 |
if (strstr(status, "not-installed")) |
| 92 |
continue; |
| 93 |
|
| 94 |
entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), |
| 95 |
"%s_%s_%s", package, version, arch); |
| 96 |
if (entry->swName_len >= sizeof(entry->swName)) |
| 97 |
entry->swName_len = sizeof(entry->swName)-1; |
| 98 |
entry->swType = (strcmp(essential, "yes") == 0) |
| 99 |
? 2 /* operatingSystem */ |
| 100 |
: 4; /* application */ |
| 101 |
|
| 102 |
/* get the last mod date */ |
| 103 |
snprintf(file, sizeof(file), "%s/%s.list", pkg_directory, package); |
| 104 |
if(stat(file, &stat_buf) != -1) { |
| 105 |
date_buf = date_n_time(&stat_buf.st_mtime, &date_len); |
| 106 |
entry->swDate_len = date_len; |
| 107 |
memcpy(entry->swDate, date_buf, entry->swDate_len); |
| 108 |
} else { |
| 109 |
/* somewhy some files include :arch in .list name */ |
| 110 |
snprintf(file, sizeof(file), "%s/%s:%s.list", pkg_directory, package, arch); |
| 111 |
if(stat(file, &stat_buf) != -1) { |
| 112 |
date_buf = date_n_time(&stat_buf.st_mtime, &date_len); |
| 113 |
entry->swDate_len = date_len; |
| 114 |
memcpy(entry->swDate, date_buf, entry->swDate_len); |
| 115 |
} |
| 116 |
} |
| 117 |
/* FIXME, or fallback to whatever nonsesnse was here before, or leave it uninitialied? |
| 118 |
else { |
| 119 |
entry->swDate_len = 8; |
| 120 |
memcpy(entry->swDate, "\0\0\1\1\0\0\0\0", 8); |
| 121 |
} |
| 122 |
*/ |
| 123 |
} |
| 124 |
pclose(p); |
| 125 |
DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n", |
| 126 |
(int) CONTAINER_SIZE(container))); |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |