| 1 |
/* |
| 2 |
* Host Resources MIB - Installed Software group implementation - hr_swinst.c |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-features.h> |
| 8 |
|
| 9 |
#ifdef HAVE_SYS_PARAM_H |
| 10 |
#include <sys/param.h> |
| 11 |
#endif |
| 12 |
#include <sys/stat.h> |
| 13 |
#ifdef TIME_WITH_SYS_TIME |
| 14 |
# include <sys/time.h> |
| 15 |
# include <time.h> |
| 16 |
#else |
| 17 |
# ifdef HAVE_SYS_TIME_H |
| 18 |
# include <sys/time.h> |
| 19 |
# else |
| 20 |
# include <time.h> |
| 21 |
# endif |
| 22 |
#endif |
| 23 |
#ifdef HAVE_DIRENT_H |
| 24 |
#include <dirent.h> |
| 25 |
#else |
| 26 |
# define dirent direct |
| 27 |
# ifdef HAVE_SYS_NDIR_H |
| 28 |
# include <sys/ndir.h> |
| 29 |
# endif |
| 30 |
# ifdef HAVE_SYS_DIR_H |
| 31 |
# include <sys/dir.h> |
| 32 |
# endif |
| 33 |
# ifdef HAVE_NDIR_H |
| 34 |
# include <ndir.h> |
| 35 |
# endif |
| 36 |
#endif |
| 37 |
#ifdef HAVE_PKGLOCS_H |
| 38 |
#include <pkglocs.h> |
| 39 |
#endif |
| 40 |
#ifdef HAVE_PKGINFO_H |
| 41 |
#include <pkginfo.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifdef HAVE_LIBRPM |
| 45 |
#include <rpm/rpmlib.h> |
| 46 |
#include <rpm/header.h> |
| 47 |
#include <fcntl.h> |
| 48 |
|
| 49 |
#ifdef HAVE_RPM_RPMFILEUTIL_H |
| 50 |
#include <rpm/rpmfileutil.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef HAVE_RPMGETPATH |
| 54 |
#include <rpm/rpmmacro.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifdef HAVE_RPM_RPMTS_H |
| 58 |
#include <rpm/rpmts.h> |
| 59 |
#include <rpm/rpmdb.h> |
| 60 |
#endif |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifdef HAVE_STRING_H |
| 64 |
#include <string.h> |
| 65 |
#else |
| 66 |
#include <strings.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
#include "host_res.h" |
| 70 |
#include "hr_swinst.h" |
| 71 |
#include <net-snmp/utilities.h> |
| 72 |
|
| 73 |
#define HRSWINST_MONOTONICALLY_INCREASING |
| 74 |
|
| 75 |
netsnmp_feature_require(date_n_time); |
| 76 |
|
| 77 |
/********************* |
| 78 |
* |
| 79 |
* Kernel & interface information, |
| 80 |
* and internal forward declarations |
| 81 |
* |
| 82 |
*********************/ |
| 83 |
|
| 84 |
/* |
| 85 |
* Reorganize the global data into a single static structure. |
| 86 |
* |
| 87 |
* Old New |
| 88 |
*====================================================== |
| 89 |
* HRSW_directory swi->swi_directory |
| 90 |
* HRSW_name[100] swi->swi_name[SNMP_MAXPATH] |
| 91 |
* HRSW_index swi->swi_index |
| 92 |
* |
| 93 |
* swi->swi_dbpath (RPM only) |
| 94 |
* swi->swi_maxrec (RPM only) |
| 95 |
* swi->swi_nrec (RPM only) |
| 96 |
* swi->swi_recs (RPM only) |
| 97 |
* rpm_db swi->swi_rpmdb (RPM only) |
| 98 |
* swi->swi_h (RPM only) |
| 99 |
* swi->swi_prevx (RPM only) |
| 100 |
* |
| 101 |
* dp swi->swi_dp |
| 102 |
* de_p swi->swi_dep |
| 103 |
*/ |
| 104 |
typedef struct { |
| 105 |
#ifdef HAVE_LIBRPM |
| 106 |
char *swi_directory; |
| 107 |
#else |
| 108 |
const char *swi_directory; |
| 109 |
#endif |
| 110 |
char swi_name[SNMP_MAXPATH]; /* XXX longest file name */ |
| 111 |
int swi_index; |
| 112 |
|
| 113 |
#ifdef HAVE_LIBRPM |
| 114 |
const char *swi_dbpath; |
| 115 |
|
| 116 |
time_t swi_timestamp; /* modify time on database */ |
| 117 |
int swi_maxrec; /* no. of allocations */ |
| 118 |
int swi_nrec; /* no. of valid offsets */ |
| 119 |
int *swi_recs; /* db record offsets */ |
| 120 |
rpmts swi_rpmts; |
| 121 |
Header swi_h; |
| 122 |
int swi_prevx; |
| 123 |
#else |
| 124 |
DIR *swi_dp; |
| 125 |
struct dirent *swi_dep; |
| 126 |
#endif |
| 127 |
|
| 128 |
} SWI_t; |
| 129 |
|
| 130 |
static SWI_t _myswi = { NULL, "", 0 }; /* XXX static for now */ |
| 131 |
|
| 132 |
int header_hrswinst(struct variable *, oid *, size_t *, int, |
| 133 |
size_t *, WriteMethod **); |
| 134 |
int header_hrswInstEntry(struct variable *, oid *, size_t *, |
| 135 |
int, size_t *, WriteMethod **); |
| 136 |
|
| 137 |
#define starttime (*(const struct timeval*)netsnmp_get_agent_starttime()) |
| 138 |
|
| 139 |
/********************* |
| 140 |
* |
| 141 |
* Initialisation & common implementation functions |
| 142 |
* |
| 143 |
*********************/ |
| 144 |
static void Init_HR_SWInst(void); |
| 145 |
static int Get_Next_HR_SWInst(void); |
| 146 |
static void End_HR_SWInst(void); |
| 147 |
static int Save_HR_SW_info(int ix); |
| 148 |
|
| 149 |
static void Mark_HRSW_token(void); |
| 150 |
static void Release_HRSW_token(void); |
| 151 |
|
| 152 |
|
| 153 |
#define HRSWINST_CHANGE 1 |
| 154 |
#define HRSWINST_UPDATE 2 |
| 155 |
#define HRSWINST_INDEX 3 |
| 156 |
#define HRSWINST_NAME 4 |
| 157 |
#define HRSWINST_ID 5 |
| 158 |
#define HRSWINST_TYPE 6 |
| 159 |
#define HRSWINST_DATE 7 |
| 160 |
|
| 161 |
struct variable4 hrswinst_variables[] = { |
| 162 |
{HRSWINST_CHANGE, ASN_TIMETICKS, NETSNMP_OLDAPI_RONLY, |
| 163 |
var_hrswinst, 1, {1}}, |
| 164 |
{HRSWINST_UPDATE, ASN_TIMETICKS, NETSNMP_OLDAPI_RONLY, |
| 165 |
var_hrswinst, 1, {2}}, |
| 166 |
{HRSWINST_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 167 |
var_hrswinst, 3, {3, 1, 1}}, |
| 168 |
{HRSWINST_NAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 169 |
var_hrswinst, 3, {3, 1, 2}}, |
| 170 |
{HRSWINST_ID, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY, |
| 171 |
var_hrswinst, 3, {3, 1, 3}}, |
| 172 |
{HRSWINST_TYPE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 173 |
var_hrswinst, 3, {3, 1, 4}}, |
| 174 |
{HRSWINST_DATE, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY, |
| 175 |
var_hrswinst, 3, {3, 1, 5}} |
| 176 |
}; |
| 177 |
oid hrswinst_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 6 }; |
| 178 |
|
| 179 |
|
| 180 |
#ifdef PKGLOC /* Description from HRSW_dir/.../pkginfo: DESC= */ |
| 181 |
#define _PATH_HRSW_directory PKGLOC |
| 182 |
#endif |
| 183 |
#ifdef hpux9 /* Description from HRSW_dir/.../index: fd: */ |
| 184 |
#define _PATH_HRSW_directory "/system" |
| 185 |
#endif |
| 186 |
#ifdef hpux10 /* Description from HRSW_dir/.../pfiles/INDEX: title */ |
| 187 |
#define _PATH_HRSW_directory "/var/adm/sw/products" |
| 188 |
#endif |
| 189 |
#ifdef hpux11 /* Description from HRSW_dir/.../pfiles/INDEX: title */ |
| 190 |
#define _PATH_HRSW_directory "/var/adm/sw/products" |
| 191 |
#endif |
| 192 |
#ifdef freebsd2 |
| 193 |
#define _PATH_HRSW_directory "/var/db/pkg" |
| 194 |
#endif |
| 195 |
#if defined(linux) && !defined(HAVE_LIBRPM) |
| 196 |
#define _PATH_HRSW_directory "/var/cache/hrmib" |
| 197 |
#endif |
| 198 |
|
| 199 |
void |
| 200 |
init_hr_swinst(void) |
| 201 |
{ |
| 202 |
#if defined(HAVE_LIBRPM) || defined(_PATH_HRSW_directory) |
| 203 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 204 |
#endif |
| 205 |
#ifdef HAVE_LIBRPM |
| 206 |
struct stat stat_buf; |
| 207 |
#endif |
| 208 |
|
| 209 |
/* |
| 210 |
* Read settings from config file, |
| 211 |
* or take system-specific defaults |
| 212 |
*/ |
| 213 |
|
| 214 |
#ifdef HAVE_LIBRPM |
| 215 |
if (swi->swi_directory == NULL) { |
| 216 |
char path[SNMP_MAXPATH]; |
| 217 |
|
| 218 |
/* |
| 219 |
* XXX distinguish between rpm-2.5.x and rpm-2.9x |
| 220 |
*/ |
| 221 |
#ifdef HAVE_RPMGETPATH |
| 222 |
rpmReadConfigFiles(NULL, NULL); |
| 223 |
swi->swi_dbpath = rpmGetPath("%{_dbpath}", NULL); |
| 224 |
#else |
| 225 |
swi->swi_dbpath = "/var/lib/rpm"; /* Most likely */ |
| 226 |
#endif |
| 227 |
if (swi->swi_directory != NULL) |
| 228 |
free(swi->swi_directory); |
| 229 |
snprintf(path, sizeof(path), "%s/Packages", swi->swi_dbpath); |
| 230 |
if (stat(path, &stat_buf) == -1) |
| 231 |
snprintf(path, sizeof(path), "%s/packages.rpm", swi->swi_dbpath); |
| 232 |
/* check for SQLite DB backend */ |
| 233 |
if (stat(path, &stat_buf) == -1) |
| 234 |
snprintf(path, sizeof(path), "%s/rpmdb.sqlite", swi->swi_dbpath); |
| 235 |
path[ sizeof(path)-1 ] = 0; |
| 236 |
swi->swi_directory = strdup(path); |
| 237 |
#ifdef HAVE_RPMGETPATH |
| 238 |
rpmFreeRpmrc(); |
| 239 |
#endif |
| 240 |
} |
| 241 |
#else |
| 242 |
# ifdef _PATH_HRSW_directory |
| 243 |
if (swi->swi_directory == NULL) { |
| 244 |
swi->swi_directory = _PATH_HRSW_directory; |
| 245 |
} |
| 246 |
strcpy(swi->swi_name, "[installed name]"); /* default name */ |
| 247 |
# else |
| 248 |
/* |
| 249 |
* XXX SunOS4 package directory is ?? -MJS |
| 250 |
*/ |
| 251 |
return; /* packages not known - don't register */ |
| 252 |
# endif |
| 253 |
#endif |
| 254 |
|
| 255 |
REGISTER_MIB("host/hr_swinst", hrswinst_variables, variable4, |
| 256 |
hrswinst_variables_oid); |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* header_hrswinst(... |
| 261 |
* Arguments: |
| 262 |
* vp IN - pointer to variable entry that points here |
| 263 |
* name IN/OUT - IN/name requested, OUT/name found |
| 264 |
* length IN/OUT - length of IN/OUT oid's |
| 265 |
* exact IN - TRUE if an exact match was requested |
| 266 |
* var_len OUT - length of variable or 0 if function returned |
| 267 |
* write_method |
| 268 |
*/ |
| 269 |
|
| 270 |
int |
| 271 |
header_hrswinst(struct variable *vp, |
| 272 |
oid * name, |
| 273 |
size_t * length, |
| 274 |
int exact, size_t * var_len, WriteMethod ** write_method) |
| 275 |
{ |
| 276 |
#define HRSWINST_NAME_LENGTH 9 |
| 277 |
oid newname[MAX_OID_LEN]; |
| 278 |
int result; |
| 279 |
|
| 280 |
DEBUGMSGTL(("host/hr_swinst", "var_hrswinst: ")); |
| 281 |
DEBUGMSGOID(("host/hr_swinst", name, *length)); |
| 282 |
DEBUGMSG(("host/hr_swinst", " %d\n", exact)); |
| 283 |
|
| 284 |
memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid)); |
| 285 |
newname[HRSWINST_NAME_LENGTH] = 0; |
| 286 |
result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); |
| 287 |
if ((exact && (result != 0)) || (!exact && (result >= 0))) |
| 288 |
return (MATCH_FAILED); |
| 289 |
memcpy((char *) name, (char *) newname, |
| 290 |
(vp->namelen + 1) * sizeof(oid)); |
| 291 |
*length = vp->namelen + 1; |
| 292 |
|
| 293 |
*write_method = (WriteMethod*)0; |
| 294 |
*var_len = sizeof(long); /* default to 'long' results */ |
| 295 |
return (MATCH_SUCCEEDED); |
| 296 |
} |
| 297 |
|
| 298 |
int |
| 299 |
header_hrswInstEntry(struct variable *vp, |
| 300 |
oid * name, |
| 301 |
size_t * length, |
| 302 |
int exact, |
| 303 |
size_t * var_len, WriteMethod ** write_method) |
| 304 |
{ |
| 305 |
#define HRSWINST_ENTRY_NAME_LENGTH 11 |
| 306 |
oid newname[MAX_OID_LEN]; |
| 307 |
int swinst_idx, LowIndex = -1; |
| 308 |
int result; |
| 309 |
int err = 0, errcount = 0; |
| 310 |
|
| 311 |
DEBUGMSGTL(("host/hr_swinst", "var_hrswinstEntry: ")); |
| 312 |
DEBUGMSGOID(("host/hr_swinst", name, *length)); |
| 313 |
DEBUGMSG(("host/hr_swinst", " %d\n", exact)); |
| 314 |
|
| 315 |
memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid)); |
| 316 |
/* |
| 317 |
* Find "next" installed software entry |
| 318 |
*/ |
| 319 |
|
| 320 |
do { |
| 321 |
Init_HR_SWInst(); |
| 322 |
while ((swinst_idx = Get_Next_HR_SWInst()) != -1) { |
| 323 |
DEBUGMSG(("host/hr_swinst", "(index %d ....", swinst_idx)); |
| 324 |
|
| 325 |
newname[HRSWINST_ENTRY_NAME_LENGTH] = swinst_idx; |
| 326 |
DEBUGMSGOID(("host/hr_swinst", newname, *length)); |
| 327 |
DEBUGMSG(("host/hr_swinst", "\n")); |
| 328 |
result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); |
| 329 |
if (exact && (result == 0)) { |
| 330 |
LowIndex = swinst_idx; |
| 331 |
err = Save_HR_SW_info(LowIndex); |
| 332 |
break; |
| 333 |
} |
| 334 |
if ((!exact && (result < 0)) && |
| 335 |
(LowIndex == -1 || swinst_idx < LowIndex)) { |
| 336 |
LowIndex = swinst_idx; |
| 337 |
err = Save_HR_SW_info(LowIndex); |
| 338 |
#ifdef HRSWINST_MONOTONICALLY_INCREASING |
| 339 |
break; |
| 340 |
#endif |
| 341 |
} |
| 342 |
} |
| 343 |
if (err != 0 ) |
| 344 |
errcount++; |
| 345 |
/* restart until Save_HR_SW_info() succeeds,max. 3 times */ |
| 346 |
} while (err != 0 && errcount < 3); |
| 347 |
if (err != 0) { |
| 348 |
DEBUGMSGTL(("host/hr_swinst", "restart did not help, bailing out\n")); |
| 349 |
return (MATCH_FAILED); |
| 350 |
} |
| 351 |
|
| 352 |
Mark_HRSW_token(); |
| 353 |
End_HR_SWInst(); |
| 354 |
|
| 355 |
if (LowIndex == -1) { |
| 356 |
DEBUGMSGTL(("host/hr_swinst", "... index out of range\n")); |
| 357 |
return (MATCH_FAILED); |
| 358 |
} |
| 359 |
|
| 360 |
memcpy((char *) name, (char *) newname, |
| 361 |
(vp->namelen + 1) * sizeof(oid)); |
| 362 |
*length = vp->namelen + 1; |
| 363 |
*write_method = (WriteMethod*)0; |
| 364 |
*var_len = sizeof(long); /* default to 'long' results */ |
| 365 |
|
| 366 |
DEBUGMSGTL(("host/hr_inst", "... get installed S/W stats ")); |
| 367 |
DEBUGMSGOID(("host/hr_inst", name, *length)); |
| 368 |
DEBUGMSG(("host/hr_inst", "\n")); |
| 369 |
return LowIndex; |
| 370 |
} |
| 371 |
|
| 372 |
/********************* |
| 373 |
* |
| 374 |
* System specific implementation functions |
| 375 |
* |
| 376 |
*********************/ |
| 377 |
|
| 378 |
|
| 379 |
u_char * |
| 380 |
var_hrswinst(struct variable * vp, |
| 381 |
oid * name, |
| 382 |
size_t * length, |
| 383 |
int exact, size_t * var_len, WriteMethod ** write_method) |
| 384 |
{ |
| 385 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 386 |
int sw_idx = 0; |
| 387 |
static char string[SNMP_MAXPATH]; |
| 388 |
u_char *ret = NULL; |
| 389 |
struct stat stat_buf; |
| 390 |
|
| 391 |
if (vp->magic < HRSWINST_INDEX) { |
| 392 |
if (header_hrswinst(vp, name, length, exact, var_len, write_method) |
| 393 |
== MATCH_FAILED) |
| 394 |
return NULL; |
| 395 |
} else { |
| 396 |
|
| 397 |
sw_idx = |
| 398 |
header_hrswInstEntry(vp, name, length, exact, var_len, |
| 399 |
write_method); |
| 400 |
if (sw_idx == MATCH_FAILED) |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
switch (vp->magic) { |
| 405 |
case HRSWINST_CHANGE: |
| 406 |
case HRSWINST_UPDATE: |
| 407 |
string[0] = '\0'; |
| 408 |
|
| 409 |
if (swi->swi_directory != NULL) |
| 410 |
strlcpy(string, swi->swi_directory, sizeof(string)); |
| 411 |
|
| 412 |
if (*string && (stat(string, &stat_buf) != -1)) { |
| 413 |
if (stat_buf.st_mtime > starttime.tv_sec) |
| 414 |
/* |
| 415 |
* changed 'recently' - i.e. since this agent started |
| 416 |
*/ |
| 417 |
long_return = (stat_buf.st_mtime - starttime.tv_sec) * 100; |
| 418 |
else |
| 419 |
long_return = 0; /* predates this agent */ |
| 420 |
} else |
| 421 |
#ifdef NETSNMP_NO_DUMMY_VALUES |
| 422 |
return NULL; |
| 423 |
#else |
| 424 |
long_return = 363136200; |
| 425 |
#endif |
| 426 |
ret = (u_char *) & long_return; |
| 427 |
break; |
| 428 |
|
| 429 |
case HRSWINST_INDEX: |
| 430 |
long_return = sw_idx; |
| 431 |
ret = (u_char *) & long_return; |
| 432 |
break; |
| 433 |
case HRSWINST_NAME: |
| 434 |
{ |
| 435 |
#ifdef HAVE_PKGINFO |
| 436 |
char *pver; |
| 437 |
# endif |
| 438 |
strlcpy(string, swi->swi_name, sizeof(string)); |
| 439 |
|
| 440 |
/* If we are on a solaris machine, the package names do not include versioning info, |
| 441 |
* so we must add it manually |
| 442 |
*/ |
| 443 |
#ifdef HAVE_PKGINFO |
| 444 |
pver = pkgparam(swi->swi_name, "VERSION"); |
| 445 |
/* 1 spot for the terminating null and one for the dash */ |
| 446 |
if (pver && |
| 447 |
(strlen(pver) + 2 + strlen(string) <= sizeof(string))) { |
| 448 |
strcat(string, "-"); |
| 449 |
strcat(string, pver); |
| 450 |
} |
| 451 |
# endif |
| 452 |
|
| 453 |
/* |
| 454 |
* This will be unchanged from the initial "null" |
| 455 |
* value, if swi->swi_name is not defined |
| 456 |
*/ |
| 457 |
string[sizeof(string) - 1] = '\0'; |
| 458 |
*var_len = strlen(string); |
| 459 |
ret = (u_char *) string; |
| 460 |
} |
| 461 |
break; |
| 462 |
case HRSWINST_ID: |
| 463 |
*var_len = nullOidLen; |
| 464 |
ret = (u_char *) nullOid; |
| 465 |
break; |
| 466 |
case HRSWINST_TYPE: |
| 467 |
{ |
| 468 |
#ifdef HAVE_PKGINFO |
| 469 |
/* |
| 470 |
* at least on solaris2 this works |
| 471 |
*/ |
| 472 |
char *catg = pkgparam(swi->swi_name, "CATEGORY"); |
| 473 |
|
| 474 |
if (catg == NULL) { |
| 475 |
long_return = 1; /* unknown */ |
| 476 |
} else { |
| 477 |
if (strstr(catg, "system") != NULL) { |
| 478 |
long_return = 2; /* operatingSystem */ |
| 479 |
} else if (strstr(catg, "application") != NULL) { |
| 480 |
long_return = 4; /* applcation */ |
| 481 |
} else { |
| 482 |
long_return = 1; /* unknown */ |
| 483 |
} |
| 484 |
free(catg); |
| 485 |
} |
| 486 |
#else |
| 487 |
# ifdef HAVE_LIBRPM |
| 488 |
char *rpm_groups; |
| 489 |
if ( headerGetEntry(swi->swi_h, RPMTAG_GROUP, NULL, (void **) &rpm_groups, NULL) ) { |
| 490 |
if ( strstr(rpm_groups, "System Environment") != NULL ) |
| 491 |
long_return = 2; /* operatingSystem */ |
| 492 |
else |
| 493 |
long_return = 4; /* applcation */ |
| 494 |
} else { |
| 495 |
long_return = 1; /* unknown */ |
| 496 |
} |
| 497 |
# else |
| 498 |
long_return = 1; /* unknown */ |
| 499 |
# endif |
| 500 |
#endif |
| 501 |
ret = (u_char *) & long_return; |
| 502 |
} |
| 503 |
break; |
| 504 |
case HRSWINST_DATE: |
| 505 |
{ |
| 506 |
#ifdef HAVE_LIBRPM |
| 507 |
int32_t *rpm_data; |
| 508 |
if ( headerGetEntry(swi->swi_h, RPMTAG_INSTALLTIME, NULL, (void **) &rpm_data, NULL) ) { |
| 509 |
time_t installTime = *rpm_data; |
| 510 |
ret = date_n_time(&installTime, var_len); |
| 511 |
} else { |
| 512 |
ret = date_n_time(NULL, var_len); |
| 513 |
} |
| 514 |
#else |
| 515 |
if (swi->swi_directory != NULL) { |
| 516 |
snprintf(string, sizeof(string), "%s/%s", |
| 517 |
swi->swi_directory, swi->swi_name); |
| 518 |
string[ sizeof(string)-1 ] = 0; |
| 519 |
if (stat(string, &stat_buf) >= 0) |
| 520 |
ret = date_n_time(&stat_buf.st_mtime, var_len); |
| 521 |
else |
| 522 |
goto err; |
| 523 |
} else { |
| 524 |
err: |
| 525 |
#ifdef NETSNMP_NO_DUMMY_VALUES |
| 526 |
ret = NULL; |
| 527 |
#else |
| 528 |
sprintf(string, "back in the mists of time"); |
| 529 |
*var_len = strlen(string); |
| 530 |
ret = (u_char *) string; |
| 531 |
#endif |
| 532 |
} |
| 533 |
#endif |
| 534 |
} |
| 535 |
break; |
| 536 |
default: |
| 537 |
DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrswinst\n", |
| 538 |
vp->magic)); |
| 539 |
ret = NULL; |
| 540 |
break; |
| 541 |
} |
| 542 |
Release_HRSW_token(); |
| 543 |
return ret; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
/********************* |
| 548 |
* |
| 549 |
* Internal implementation functions |
| 550 |
* |
| 551 |
*********************/ |
| 552 |
|
| 553 |
#ifdef HAVE_LIBRPM |
| 554 |
static void |
| 555 |
Check_HRSW_cache(void *xxx) |
| 556 |
{ |
| 557 |
SWI_t *swi = (SWI_t *) xxx; |
| 558 |
|
| 559 |
/* |
| 560 |
* Make sure cache is up-to-date |
| 561 |
*/ |
| 562 |
if (swi->swi_recs != NULL) { |
| 563 |
struct stat sb; |
| 564 |
lstat(swi->swi_directory, &sb); |
| 565 |
if (swi->swi_timestamp == sb.st_mtime) |
| 566 |
return; |
| 567 |
swi->swi_timestamp = sb.st_mtime; |
| 568 |
swi->swi_maxrec = 0; |
| 569 |
} |
| 570 |
|
| 571 |
/* |
| 572 |
* Get header offsets |
| 573 |
*/ |
| 574 |
{ |
| 575 |
int ix = 0; |
| 576 |
int offset; |
| 577 |
|
| 578 |
rpmdbMatchIterator mi = NULL; |
| 579 |
Header h; |
| 580 |
mi = rpmtsInitIterator(swi->swi_rpmts, RPMDBI_PACKAGES, NULL, 0); |
| 581 |
while ((h = rpmdbNextIterator(mi)) != NULL) { |
| 582 |
offset = rpmdbGetIteratorOffset(mi); |
| 583 |
|
| 584 |
if (ix >= swi->swi_maxrec) { |
| 585 |
swi->swi_maxrec += 256; |
| 586 |
swi->swi_recs = (swi->swi_recs == NULL) |
| 587 |
? (int *) malloc(swi->swi_maxrec * sizeof(int)) |
| 588 |
: (int *) realloc(swi->swi_recs, |
| 589 |
swi->swi_maxrec * sizeof(int)); |
| 590 |
} |
| 591 |
swi->swi_recs[ix++] = offset; |
| 592 |
} |
| 593 |
rpmdbFreeIterator(mi); |
| 594 |
|
| 595 |
swi->swi_nrec = ix; |
| 596 |
} |
| 597 |
} |
| 598 |
#endif /* HAVE_LIBRPM */ |
| 599 |
|
| 600 |
void |
| 601 |
Init_HR_SWInst(void) |
| 602 |
{ |
| 603 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 604 |
swi->swi_index = 0; |
| 605 |
|
| 606 |
#ifdef HAVE_LIBRPM |
| 607 |
if (swi->swi_rpmts != NULL) |
| 608 |
return; |
| 609 |
swi->swi_rpmts = rpmtsCreate(); |
| 610 |
rpmtsSetVSFlags( swi->swi_rpmts, (_RPMVSF_NOSIGNATURES|_RPMVSF_NODIGESTS)); |
| 611 |
Check_HRSW_cache(swi); |
| 612 |
#else |
| 613 |
if (swi->swi_directory != NULL) { |
| 614 |
if (swi->swi_dp != NULL) { |
| 615 |
closedir(swi->swi_dp); |
| 616 |
swi->swi_dp = NULL; |
| 617 |
} |
| 618 |
if ((swi->swi_dp = opendir(swi->swi_directory)) == NULL) |
| 619 |
swi->swi_index = -1; |
| 620 |
} else |
| 621 |
swi->swi_index = -1; |
| 622 |
#endif |
| 623 |
} |
| 624 |
|
| 625 |
int |
| 626 |
Get_Next_HR_SWInst(void) |
| 627 |
{ |
| 628 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 629 |
|
| 630 |
if (swi->swi_index == -1) |
| 631 |
return -1; |
| 632 |
|
| 633 |
#ifdef HAVE_LIBRPM |
| 634 |
/* |
| 635 |
* XXX Watchout: index starts with 1 |
| 636 |
*/ |
| 637 |
if (0 <= swi->swi_index && swi->swi_index < swi->swi_nrec) |
| 638 |
return ++swi->swi_index; |
| 639 |
#else |
| 640 |
if (swi->swi_directory != NULL) { |
| 641 |
while ((swi->swi_dep = readdir(swi->swi_dp)) != NULL) { |
| 642 |
if (swi->swi_dep->d_name[0] == '.') |
| 643 |
continue; |
| 644 |
|
| 645 |
/* |
| 646 |
* Ought to check for "properly-formed" entry |
| 647 |
*/ |
| 648 |
|
| 649 |
return ++swi->swi_index; |
| 650 |
} |
| 651 |
} |
| 652 |
#endif |
| 653 |
|
| 654 |
return -1; |
| 655 |
} |
| 656 |
|
| 657 |
int |
| 658 |
Save_HR_SW_info(int ix) |
| 659 |
{ |
| 660 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 661 |
|
| 662 |
#ifdef HAVE_LIBRPM |
| 663 |
/* |
| 664 |
* XXX Watchout: ix starts with 1 |
| 665 |
*/ |
| 666 |
if (1 <= ix && ix <= swi->swi_nrec && ix != swi->swi_prevx) { |
| 667 |
int offset; |
| 668 |
Header h; |
| 669 |
char *n, *v, *r; |
| 670 |
|
| 671 |
offset = swi->swi_recs[ix - 1]; |
| 672 |
|
| 673 |
{ |
| 674 |
rpmdbMatchIterator mi; |
| 675 |
mi = rpmtsInitIterator(swi->swi_rpmts, RPMDBI_PACKAGES, |
| 676 |
&offset, sizeof(offset)); |
| 677 |
if ((h = rpmdbNextIterator(mi)) != NULL) |
| 678 |
h = headerLink(h); |
| 679 |
rpmdbFreeIterator(mi); |
| 680 |
} |
| 681 |
|
| 682 |
if (h == NULL) { |
| 683 |
DEBUGMSGTL(("host/hr_swinst", |
| 684 |
"RPM cache has probably expired when reading entry %d, " |
| 685 |
"reloading...\n", ix)); |
| 686 |
swi->swi_timestamp = 0; |
| 687 |
return -1; |
| 688 |
} |
| 689 |
if (swi->swi_h != NULL) |
| 690 |
headerFree(swi->swi_h); |
| 691 |
swi->swi_h = h; |
| 692 |
swi->swi_prevx = ix; |
| 693 |
|
| 694 |
headerGetEntry(swi->swi_h, RPMTAG_NAME, NULL, (void **) &n, NULL); |
| 695 |
headerGetEntry(swi->swi_h, RPMTAG_VERSION, NULL, (void **) &v, |
| 696 |
NULL); |
| 697 |
headerGetEntry(swi->swi_h, RPMTAG_RELEASE, NULL, (void **) &r, |
| 698 |
NULL); |
| 699 |
snprintf(swi->swi_name, sizeof(swi->swi_name), "%s-%s-%s", n, v, r); |
| 700 |
swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; |
| 701 |
} |
| 702 |
#else |
| 703 |
snprintf(swi->swi_name, sizeof(swi->swi_name), "%s", swi->swi_dep->d_name); |
| 704 |
swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; |
| 705 |
#endif |
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
void |
| 710 |
Mark_HRSW_token(void) |
| 711 |
{ |
| 712 |
} |
| 713 |
|
| 714 |
void |
| 715 |
Release_HRSW_token(void) |
| 716 |
{ |
| 717 |
#ifdef HAVE_LIBRPM |
| 718 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 719 |
if (swi != NULL && swi->swi_h) { |
| 720 |
headerFree(swi->swi_h); |
| 721 |
swi->swi_h = NULL; |
| 722 |
swi->swi_prevx = -1; |
| 723 |
} |
| 724 |
#endif /* HAVE_LIBRPM */ |
| 725 |
} |
| 726 |
|
| 727 |
void |
| 728 |
End_HR_SWInst(void) |
| 729 |
{ |
| 730 |
SWI_t *swi = &_myswi; /* XXX static for now */ |
| 731 |
|
| 732 |
#ifdef HAVE_LIBRPM |
| 733 |
rpmtsFree(swi->swi_rpmts); /* or only on finishing ? */ |
| 734 |
swi->swi_rpmts = NULL; |
| 735 |
#else |
| 736 |
if (swi->swi_dp != NULL) |
| 737 |
closedir(swi->swi_dp); |
| 738 |
swi->swi_dp = NULL; |
| 739 |
#endif |
| 740 |
} |