| 1 |
/* |
| 2 |
* swinst_pkginfo.c: |
| 3 |
* hrSWInstalledTable data access: |
| 4 |
*/ |
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
#include <net-snmp/net-snmp-features.h> |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
#ifdef HAVE_STDLIB_H |
| 11 |
#include <stdlib.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#ifdef HAVE_SYS_TYPES_H |
| 17 |
#include <sys/types.h> |
| 18 |
#endif |
| 19 |
#ifdef HAVE_STRING_H |
| 20 |
#include <string.h> |
| 21 |
#else |
| 22 |
#include <strings.h> |
| 23 |
#endif |
| 24 |
#ifdef HAVE_DIRENT_H |
| 25 |
#include <dirent.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifdef HAVE_PKGLOCS_H |
| 29 |
#include <pkglocs.h> |
| 30 |
#endif |
| 31 |
#ifdef HAVE_PKGINFO_H |
| 32 |
#include <pkginfo.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef HAVE_PKG_H |
| 36 |
#define restrict |
| 37 |
#include <pkg.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <net-snmp/net-snmp-includes.h> |
| 41 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 42 |
#include <net-snmp/library/container.h> |
| 43 |
#include <net-snmp/library/snmp_debug.h> |
| 44 |
#include <net-snmp/data_access/swinst.h> |
| 45 |
#include "swinst_private.h" |
| 46 |
|
| 47 |
netsnmp_feature_require(date_n_time); |
| 48 |
|
| 49 |
/* |
| 50 |
* Location of package directory. |
| 51 |
* Used for: |
| 52 |
* - reporting hrSWInstalledLast* objects |
| 53 |
* - detecting when the cached contents are out of date. |
| 54 |
*/ |
| 55 |
static char pkg_directory[SNMP_MAXPATH]; |
| 56 |
|
| 57 |
/* --------------------------------------------------------------------- |
| 58 |
*/ |
| 59 |
void |
| 60 |
netsnmp_swinst_arch_init(void) |
| 61 |
{ |
| 62 |
#if defined(PKGLOC) |
| 63 |
snprintf( pkg_directory, SNMP_MAXPATH, "%s", PKGLOC ); |
| 64 |
#elif defined(hpux9) |
| 65 |
snprintf( pkg_directory, SNMP_MAXPATH, "/system" ); |
| 66 |
#elif defined(hpux10) || defined(hpux11) |
| 67 |
snprintf( pkg_directory, SNMP_MAXPATH, "/var/adm/sw/products" ); |
| 68 |
#elif defined(freebsd2) || defined(openbsd) |
| 69 |
snprintf( pkg_directory, SNMP_MAXPATH, "/var/db/pkg" ); |
| 70 |
#elif defined(linux) |
| 71 |
snprintf( pkg_directory, SNMP_MAXPATH, "/var/cache/hrmib" ); |
| 72 |
#else |
| 73 |
pkg_directory[0] = '\0'; |
| 74 |
snmp_log( LOG_ERR, "SWInst: No package directory\n" ); |
| 75 |
#endif |
| 76 |
|
| 77 |
DEBUGMSGTL(("swinst:init:arch"," package directory = %s\n", |
| 78 |
pkg_directory)); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
netsnmp_swinst_arch_shutdown(void) |
| 84 |
{ |
| 85 |
pkg_directory[0] = '\0'; |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
/* --------------------------------------------------------------------- |
| 90 |
*/ |
| 91 |
int |
| 92 |
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags) |
| 93 |
{ |
| 94 |
DIR *d; |
| 95 |
struct dirent *dp; |
| 96 |
struct stat stat_buf; |
| 97 |
#ifdef HAVE_PKGINFO |
| 98 |
char *v, *c; |
| 99 |
#endif |
| 100 |
char buf[ BUFSIZ ]; |
| 101 |
unsigned char *cp; |
| 102 |
time_t install_time; |
| 103 |
size_t date_len; |
| 104 |
int i = 1; |
| 105 |
netsnmp_swinst_entry *entry; |
| 106 |
#ifdef HAVE_LIBPKG |
| 107 |
struct pkgdb *db = NULL; |
| 108 |
struct pkgdb_it *it = NULL; |
| 109 |
struct pkg *pkg = NULL; |
| 110 |
char pkgname[ SNMP_MAXPATH ]; |
| 111 |
char pkgdate[ BUFSIZ ]; |
| 112 |
int pkgng = 0; |
| 113 |
|
| 114 |
if (pkg_init(NULL, NULL)) { |
| 115 |
snmp_log( LOG_ERR, "SWInst: error initializing pkgng db\n" ); |
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) { |
| 120 |
snmp_log( LOG_ERR, "SWInst: error opening pkgng db\n" ); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
if (pkg_status(NULL) == PKG_STATUS_ACTIVE) { |
| 125 |
pkgng = 1; |
| 126 |
} else { |
| 127 |
snmp_log( LOG_INFO, "SWInst: not a pkgng system\n" ); |
| 128 |
} |
| 129 |
|
| 130 |
/* if we are using FreeBSD's pkgng */ |
| 131 |
if (pkgng) { |
| 132 |
if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) { |
| 133 |
snmp_log( LOG_ERR, "SWInst: error querying pkgng db\n" ); |
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) { |
| 138 |
pkg_snprintf(pkgname, sizeof(pkgname), "%n-%v", pkg, pkg); |
| 139 |
pkg_snprintf(pkgdate, sizeof(pkgdate), "%t", pkg); |
| 140 |
|
| 141 |
entry = netsnmp_swinst_entry_create( i++ ); |
| 142 |
|
| 143 |
if (NULL == entry) |
| 144 |
continue; /* error already logged by function */ |
| 145 |
|
| 146 |
CONTAINER_INSERT(container, entry); |
| 147 |
|
| 148 |
entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), |
| 149 |
"%s", pkgname ); |
| 150 |
if (entry->swName_len >= sizeof(entry->swName)) |
| 151 |
entry->swName_len = sizeof(entry->swName)-1; |
| 152 |
|
| 153 |
install_time = atoi(pkgdate); |
| 154 |
cp = date_n_time( &install_time, &date_len ); |
| 155 |
memcpy( entry->swDate, cp, date_len ); |
| 156 |
entry->swDate_len = date_len; |
| 157 |
} |
| 158 |
|
| 159 |
pkgdb_it_free(it); |
| 160 |
pkgdb_close(db); |
| 161 |
pkg_shutdown(); |
| 162 |
} else { |
| 163 |
#endif /* HAVE_LIBPKG */ |
| 164 |
if ( !pkg_directory[0] ) { |
| 165 |
return 1; /* Can't report installed packages |
| 166 |
if there isn't a list of them! */ |
| 167 |
} |
| 168 |
|
| 169 |
d = opendir( pkg_directory ); |
| 170 |
if (!d) |
| 171 |
return 1; |
| 172 |
|
| 173 |
while ((dp = readdir(d)) != NULL) { |
| 174 |
if ( '.' == dp->d_name[0] ) |
| 175 |
continue; |
| 176 |
snprintf( buf, BUFSIZ, "%s/%s", pkg_directory, dp->d_name ); |
| 177 |
if (stat( buf, &stat_buf ) < 0) |
| 178 |
continue; |
| 179 |
entry = netsnmp_swinst_entry_create( i++ ); |
| 180 |
if (NULL == entry) |
| 181 |
continue; /* error already logged by function */ |
| 182 |
CONTAINER_INSERT(container, entry); |
| 183 |
|
| 184 |
#ifdef HAVE_PKGINFO |
| 185 |
v = pkgparam( dp->d_name, NETSNMP_REMOVE_CONST(char *, "VERSION") ); |
| 186 |
c = pkgparam( dp->d_name, NETSNMP_REMOVE_CONST(char *, "CATEGORY") ); |
| 187 |
|
| 188 |
entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), |
| 189 |
"%s-%s", dp->d_name, v ); |
| 190 |
if (entry->swName_len >= sizeof(entry->swName)) |
| 191 |
entry->swName_len = sizeof(entry->swName)-1; |
| 192 |
entry->swType = (c && (NULL != strstr( c, "system"))) |
| 193 |
? 2 /* operatingSystem */ |
| 194 |
: 4; /* application */ |
| 195 |
|
| 196 |
/* pkgparam() return values must be freed. */ |
| 197 |
free(c); |
| 198 |
free(v); |
| 199 |
#else |
| 200 |
entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), |
| 201 |
"%s", dp->d_name ); |
| 202 |
if (entry->swName_len >= sizeof(entry->swName)) |
| 203 |
entry->swName_len = sizeof(entry->swName)-1; |
| 204 |
|
| 205 |
/* no information about O/S vs application packages ??? */ |
| 206 |
#endif |
| 207 |
|
| 208 |
install_time = stat_buf.st_mtime; |
| 209 |
cp = date_n_time( &install_time, &date_len ); |
| 210 |
memcpy( entry->swDate, cp, date_len ); |
| 211 |
entry->swDate_len = date_len; |
| 212 |
} |
| 213 |
closedir( d ); |
| 214 |
#ifdef HAVE_LIBPKG |
| 215 |
} |
| 216 |
#endif |
| 217 |
|
| 218 |
DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n", |
| 219 |
(int)CONTAINER_SIZE(container))); |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |