| 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 apt_fmt[SNMP_MAXBUF]; |
| 37 |
|
| 38 |
/* --------------------------------------------------------------------- |
| 39 |
*/ |
| 40 |
void |
| 41 |
netsnmp_swinst_arch_init(void) |
| 42 |
{ |
| •43 |
snprintf(apt_fmt, SNMP_MAXBUF, "%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%lld", |
| 44 |
SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1, |
| 45 |
SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1); |
| 46 |
} |
| 47 |
|
| 48 |
void |
| 49 |
netsnmp_swinst_arch_shutdown(void) |
| 50 |
{ |
| 51 |
/* Nothing to do */ |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
/* --------------------------------------------------------------------- |
| 56 |
*/ |
| 57 |
int |
| 58 |
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags) |
| 59 |
{ |
| •60 |
FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Architecture}#${Status}#${db-fsys:Last-Modified}\n'", "r"); |
| 61 |
char package[SNMP_MAXBUF]; |
| 62 |
char version[SNMP_MAXBUF]; |
| 63 |
char section[SNMP_MAXBUF]; |
| 64 |
char priority[SNMP_MAXBUF]; |
| 65 |
char essential[SNMP_MAXBUF]; |
| 66 |
char arch[SNMP_MAXBUF]; |
| 67 |
char status[SNMP_MAXBUF]; |
| •68 |
long long last_modified; |
| 69 |
char buf[BUFSIZ]; |
| 70 |
netsnmp_swinst_entry *entry; |
| 71 |
u_char *date_buf; |
| 72 |
size_t date_len; |
| 73 |
int i = 1; |
| 74 |
|
| 75 |
if (p == NULL) { |
| 76 |
snmp_perror("dpkg-list"); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
while (fgets(buf, BUFSIZ, p)) { |
| 81 |
DEBUGMSG(("swinst_apt", "entry: %s\n", buf)); |
| 82 |
entry = netsnmp_swinst_entry_create( i++ ); |
| 83 |
if (NULL == entry) |
| 84 |
continue; /* error already logged by function */ |
| 85 |
CONTAINER_INSERT(container, entry); |
| 86 |
|
| •87 |
sscanf(buf, apt_fmt, package, version, section, priority, essential, arch, status, &last_modified); |
| 88 |
if (strstr(status, "not-installed")) |
| 89 |
continue; |
| 90 |
|
| 91 |
entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), |
| 92 |
"%s_%s_%s", package, version, arch); |
| 93 |
if (entry->swName_len >= sizeof(entry->swName)) |
| 94 |
entry->swName_len = sizeof(entry->swName)-1; |
| 95 |
entry->swType = (strcmp(essential, "yes") == 0) |
| 96 |
? 2 /* operatingSystem */ |
| 97 |
: 4; /* application */ |
| 98 |
|
| •99 |
date_buf = date_n_time((time_t*)(&last_modified), &date_len); |
| 100 |
entry->swDate_len = date_len; |
| 101 |
memcpy(entry->swDate, date_buf, entry->swDate_len); |
| 102 |
|
| 103 |
/* FIXME, or fallback to whatever nonsesnse was here before, or leave it uninitialied? |
| 104 |
else { |
| 105 |
entry->swDate_len = 8; |
| 106 |
memcpy(entry->swDate, "\0\0\1\1\0\0\0\0", 8); |
| 107 |
} |
| 108 |
*/ |
| 109 |
} |
| 110 |
pclose(p); |
| 111 |
DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n", |
| 112 |
(int) CONTAINER_SIZE(container))); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |