u_char * var_hrswinst(struct variable * vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method) { SWI_t *swi = &_myswi; /* XXX static for now */ int sw_idx = 0; static char string[SNMP_MAXPATH]; u_char *ret = NULL; struct stat stat_buf; if (vp->magic < HRSWINST_INDEX) { if (header_hrswinst(vp, name, length, exact, var_len, write_method) == MATCH_FAILED) return NULL; } else { sw_idx = header_hrswInstEntry(vp, name, length, exact, var_len, write_method); if (sw_idx == MATCH_FAILED) return NULL; } switch (vp->magic) { case HRSWINST_CHANGE: case HRSWINST_UPDATE: string[0] = '\0'; if (swi->swi_directory != NULL) { strncpy(string, swi->swi_directory, sizeof(string)); string[ sizeof(string)-1 ] = 0; } if (*string && (stat(string, &stat_buf) != -1)) { if (stat_buf.st_mtime > starttime.tv_sec) /* * changed 'recently' - i.e. since this agent started */ long_return = (stat_buf.st_mtime - starttime.tv_sec) * 100; else long_return = 0; /* predates this agent */ } else #if NETSNMP_NO_DUMMY_VALUES return NULL; #else long_return = 363136200; #endif ret = (u_char *) & long_return; break; case HRSWINST_INDEX: long_return = sw_idx; ret = (u_char *) & long_return; break; case HRSWINST_NAME: { strncpy(string, swi->swi_name, sizeof(string) - 1); /* * This will be unchanged from the initial "null" * value, if swi->swi_name is not defined */ string[sizeof(string) - 1] = '\0'; *var_len = strlen(string); ret = (u_char *) string; } break; case HRSWINST_ID: *var_len = nullOidLen; ret = (u_char *) nullOid; break; case HRSWINST_TYPE: { #ifdef HAVE_PKGINFO /* * at least on solaris2 this works */ char *catg = pkgparam(swi->swi_name, "CATEGORY"); if (catg == NULL) { long_return = 1; /* unknown */ } else { if (strstr(catg, "system") != NULL) { long_return = 2; /* operatingSystem */ } else if (strstr(catg, "application") != NULL) { long_return = 4; /* applcation */ } else { long_return = 1; /* unknown */ } free(catg); } #else # ifdef HAVE_LIBRPM char *rpm_groups; if ( headerGetEntry(swi->swi_h, RPMTAG_GROUP, NULL, (void **) &rpm_groups, NULL) ) { if ( strstr(rpm_groups, "System Environment") != NULL ) long_return = 2; /* operatingSystem */ else long_return = 4; /* applcation */ } else { long_return = 1; /* unknown */ } # else long_return = 1; /* unknown */ # endif #endif ret = (u_char *) & long_return; } break; case HRSWINST_DATE: { #ifdef HAVE_LIBRPM int_32 *rpm_data; if ( headerGetEntry(swi->swi_h, RPMTAG_INSTALLTIME, NULL, (void **) &rpm_data, NULL) ) { time_t installTime = *rpm_data; ret = date_n_time(&installTime, var_len); } else { ret = date_n_time(0, var_len); } #else if (swi->swi_directory != NULL) { snprintf(string, sizeof(string), "%s/%s", swi->swi_directory, swi->swi_name); string[ sizeof(string)-1 ] = 0; stat(string, &stat_buf); ret = date_n_time(&stat_buf.st_mtime, var_len); } else { #if NETSNMP_NO_DUMMY_VALUES return NULL; #endif sprintf(string, "back in the mists of time"); *var_len = strlen(string); ret = (u_char *) string; } #endif } break; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrswinst\n", vp->magic)); ret = NULL; break; } Release_HRSW_token(); return ret; }
/* --------------------------------------------------------------------- */ int netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags) { DIR *d; struct dirent *dp; struct stat stat_buf; #ifdef HAVE_PKGINFO char *v, *c; #endif char buf[ BUFSIZ ]; unsigned char *cp; time_t install_time; size_t date_len; int i = 1; netsnmp_swinst_entry *entry; #ifdef HAVE_LIBPKG struct pkgdb *db = NULL; struct pkgdb_it *it = NULL; struct pkg *pkg = NULL; char pkgname[ SNMP_MAXPATH ]; char pkgdate[ BUFSIZ ]; int pkgng = 0; if (pkg_init(NULL, NULL)) { snmp_log( LOG_ERR, "SWInst: error initializing pkgng db\n" ); return 1; } if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) { snmp_log( LOG_ERR, "SWInst: error opening pkgng db\n" ); return 1; } if (pkg_status(NULL) == PKG_STATUS_ACTIVE) { pkgng = 1; } else { snmp_log( LOG_INFO, "SWInst: not a pkgng system\n" ); } /* if we are using FreeBSD's pkgng */ if (pkgng) { if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) { snmp_log( LOG_ERR, "SWInst: error querying pkgng db\n" ); return 1; } while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) { pkg_snprintf(pkgname, sizeof(pkgname), "%n-%v", pkg, pkg); pkg_snprintf(pkgdate, sizeof(pkgdate), "%t", pkg); entry = netsnmp_swinst_entry_create( i++ ); if (NULL == entry) continue; /* error already logged by function */ CONTAINER_INSERT(container, entry); entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), "%s", pkgname ); if (entry->swName_len >= sizeof(entry->swName)) entry->swName_len = sizeof(entry->swName)-1; install_time = atoi(pkgdate); cp = date_n_time( &install_time, &date_len ); memcpy( entry->swDate, cp, date_len ); entry->swDate_len = date_len; } pkgdb_it_free(it); pkgdb_close(db); pkg_shutdown(); } else { #endif /* HAVE_LIBPKG */ if ( !pkg_directory[0] ) { return 1; /* Can't report installed packages if there isn't a list of them! */ } d = opendir( pkg_directory ); if (!d) return 1; while ((dp = readdir(d)) != NULL) { if ( '.' == dp->d_name[0] ) continue; snprintf( buf, BUFSIZ, "%s/%s", pkg_directory, dp->d_name ); if (stat( buf, &stat_buf ) < 0) continue; entry = netsnmp_swinst_entry_create( i++ ); if (NULL == entry) continue; /* error already logged by function */ CONTAINER_INSERT(container, entry); #ifdef HAVE_PKGINFO v = pkgparam( dp->d_name, "VERSION" ); c = pkgparam( dp->d_name, "CATEGORY" ); entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), "%s-%s", dp->d_name, v ); if (entry->swName_len >= sizeof(entry->swName)) entry->swName_len = sizeof(entry->swName)-1; entry->swType = (NULL != strstr( c, "system")) ? 2 /* operatingSystem */ : 4; /* application */ /* Do we need to free 'v' & 'c' ??? */ #else entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), "%s", dp->d_name ); if (entry->swName_len >= sizeof(entry->swName)) entry->swName_len = sizeof(entry->swName)-1; /* no information about O/S vs application packages ??? */ #endif install_time = stat_buf.st_mtime; cp = date_n_time( &install_time, &date_len ); memcpy( entry->swDate, cp, date_len ); entry->swDate_len = date_len; } closedir( d ); #ifdef HAVE_LIBPKG } #endif DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n", (int)CONTAINER_SIZE(container))); return 0; }
static void dumpinfo(struct cfstat *dp, int pkgLngth) { register int i; char *pt; char category[128]; if (qflag) { return; /* print nothing */ } if (rflag) { (void) puts((info.basedir) ? info.basedir : "none"); return; } if (Lflag) { (void) puts(info.pkginst); return; } else if (xflag) { (void) printf(XFMT, pkgLngth, pkgLngth, info.pkginst, info.name); if (info.arch || info.version) { (void) printf(CFMT, pkgLngth, pkgLngth, ""); if (info.arch) (void) printf("(%s) ", info.arch); if (info.version) (void) printf("%s", info.version); (void) printf("\n"); } return; } else if (!lflag) { if (info.catg) { (void) sscanf(info.catg, "%[^, \t\n]", category); } else { (void) strcpy(category, "(unknown)"); } (void) printf(SFMT, category, pkgLngth, pkgLngth, info.pkginst, info.name); return; } if (info.pkginst) (void) printf(FMT, "PKGINST", info.pkginst); if (info.name) (void) printf(FMT, "NAME", info.name); if (lflag && info.catg) (void) printf(FMT, "CATEGORY", info.catg); if (lflag && info.arch) (void) printf(FMT, "ARCH", info.arch); if (info.version) (void) printf(FMT, "VERSION", info.version); if (info.basedir) (void) printf(FMT, "BASEDIR", info.basedir); if (info.vendor) (void) printf(FMT, "VENDOR", info.vendor); for (i = 0; parmlst[i]; ++i) { if ((pt = pkgparam(info.pkginst, parmlst[i])) != NULL && *pt) (void) printf(FMT, parmlst[i], pt); } if (info.status == PI_SPOOLED) (void) printf(FMT, "STATUS", gettext("spooled")); else if (info.status == PI_PARTIAL) (void) printf(FMT, "STATUS", gettext("partially installed")); else if (info.status == PI_INSTALLED) (void) printf(FMT, "STATUS", gettext("completely installed")); else (void) printf(FMT, "STATUS", gettext("(unknown)")); (void) pkgparam(NULL, NULL); if (!lflag) { (void) putchar('\n'); return; } if (strcmp(pkgdir, get_PKGLOC())) getinfo(dp); if (dp->spooled) (void) printf( gettext("%10s: %7ld spooled pathnames\n"), "FILES", dp->spooled); if (dp->installed) (void) printf( gettext("%10s: %7ld installed pathnames\n"), "FILES", dp->installed); if (dp->partial) (void) printf( gettext("%20d partially installed pathnames\n"), dp->partial); if (dp->shared) (void) printf(gettext("%20d shared pathnames\n"), dp->shared); if (dp->link) (void) printf(gettext("%20d linked files\n"), dp->link); if (dp->dirs) (void) printf(gettext("%20d directories\n"), dp->dirs); if (dp->exec) (void) printf(gettext("%20d executables\n"), dp->exec); if (dp->setuid) (void) printf( gettext("%20d setuid/setgid executables\n"), dp->setuid); if (dp->info) (void) printf( gettext("%20d package information files\n"), dp->info+1); /* pkgmap counts! */ if (dp->tblks) (void) printf(gettext("%20ld blocks used (approx)\n"), dp->tblks); (void) putchar('\n'); }
/* --------------------------------------------------------------------- */ int netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags) { DIR *d; struct dirent *dp; struct stat stat_buf; #ifdef HAVE_PKGINFO char *v, *c; #endif char buf[ BUFSIZ ]; unsigned char *cp; time_t install_time; size_t date_len; int i = 1; netsnmp_swinst_entry *entry; if ( !pkg_directory[0] ) { return 1; /* Can't report installed packages if there isn't a list of them! */ } d = opendir( pkg_directory ); while (d != NULL && (dp = readdir(d)) != NULL) { if ( '.' == dp->d_name[0] ) continue; entry = netsnmp_swinst_entry_create( i++ ); if (NULL == entry) continue; /* error already logged by function */ CONTAINER_INSERT(container, entry); #ifdef HAVE_PKGINFO v = pkgparam( dp->d_name, "VERSION" ); c = pkgparam( dp->d_name, "CATEGORY" ); entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), "%s-%s", dp->d_name, v ); if (entry->swName_len >= sizeof(entry->swName)) entry->swName_len = sizeof(entry->swName)-1; entry->swType = (NULL != strstr( c, "system")) ? 2 /* operatingSystem */ : 4; /* application */ /* Do we need to free 'v' & 'c' ??? */ #else entry->swName_len = snprintf( entry->swName, sizeof(entry->swName), "%s", dp->d_name ); if (entry->swName_len >= sizeof(entry->swName)) entry->swName_len = sizeof(entry->swName)-1; /* no information about O/S vs application packages ??? */ #endif snprintf( buf, BUFSIZ, "%s/%s", pkg_directory, dp->d_name ); stat( buf, &stat_buf ); install_time = stat_buf.st_mtime; cp = date_n_time( &install_time, &date_len ); memcpy( entry->swDate, cp, date_len ); entry->swDate_len = date_len; } closedir( d ); DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n", (int)CONTAINER_SIZE(container))); return 0; }