From 393119e5b9b32763f40a132021b8db1778bcd358 Mon Sep 17 00:00:00 2001 From: Eygene Ryabinkin Date: Mon, 6 Oct 2008 19:03:51 +0400 Subject: [PATCH 4/4] pkg_audit: add option to print origins Options '-o' or '--origin' adding package origins for the matched packages, appending one more field to the end of the fieldset. Signed-off-by: Eygene Ryabinkin --- usr.sbin/pkg_install/audit/main.c | 52 +++++++++++++++++++++++++++++-- usr.sbin/pkg_install/audit/pkg_audit.1 | 13 +++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/usr.sbin/pkg_install/audit/main.c b/usr.sbin/pkg_install/audit/main.c index 7e33690..1d27e09 100644 --- a/usr.sbin/pkg_install/audit/main.c +++ b/usr.sbin/pkg_install/audit/main.c @@ -52,6 +52,17 @@ __FBSDID("$FreeBSD$"); static inline void audit_package(const char *_pkgname, struct audit_contents *_head, struct match_session *_msess, FILE *_fp); +static void usage(void); + +/* Getopt stuff */ +static char opts[] = "o"; +static struct option longopts[] = { + { "origin", no_argument, NULL, 'o' }, + { NULL, 0, NULL, 0 } +}; + +/* Options */ +static int opt_printorigin = 0; int main(int argc, char *argv[]) @@ -59,7 +70,7 @@ main(int argc, char *argv[]) char freebsd[sizeof("FreeBSD-XXYYZZXXYYZZ")]; unsigned long reldate; size_t reldate_size = sizeof(reldate); - int mib[2]; + int mib[2], ch; FILE *in = stdin, *out = stdout; struct match_session *msess; @@ -72,8 +83,19 @@ main(int argc, char *argv[]) struct audit_contents head = SLIST_HEAD_INITIALIZER(head); - /* Make compiler happy */ - if (argv[argc] == NULL) {}; + /* Parse options */ + while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { + switch (ch) { + case 'o': + opt_printorigin++; + break; + default: + usage(); + break; + } + } + argc -= optind; + argv += optind; mib[0] = CTL_KERN; mib[1] = KERN_OSRELDATE; @@ -110,6 +132,10 @@ main(int argc, char *argv[]) fprintf(out, "%s|%s|%s|%s\n", freebsd, item->pkgglob, item->url, item->descr); + if (opt_printorigin) + fputs("|/usr/src\n", out); + else + fputc('\n', out); } } @@ -154,14 +180,32 @@ audit_package(const char *pkgname, struct audit_contents *head, struct match_session *msess, FILE *fp) { struct audit_entry *item; + const char *origin; SLIST_FOREACH (item, head, entries) { if (strncmp(pkgname, item->pkgglob, item->pfx_size) == 0 && match_matches(msess, item->pkgglob)) { - fprintf(fp, "%s|%s|%s|%s\n", + fprintf(fp, "%s|%s|%s|%s", pkgname, item->pkgglob, item->url, item->descr); + if (opt_printorigin) { + origin = match_get_pkgorigin(msess); + fprintf(fp, "|%s\n", + (origin == NULL ? "" : origin)); + if (origin) + free((void *)origin); + } else { + fputc('\n', fp); + } } } } + +static void +usage() +{ + fprintf(stderr, "%s\n", + "usage: pkg_audit [-o|--origin]"); + exit(1); +} diff --git a/usr.sbin/pkg_install/audit/pkg_audit.1 b/usr.sbin/pkg_install/audit/pkg_audit.1 index cd4abbc..71a0cc8 100644 --- a/usr.sbin/pkg_install/audit/pkg_audit.1 +++ b/usr.sbin/pkg_install/audit/pkg_audit.1 @@ -25,6 +25,7 @@ .Nd lists vulnerable ports installed in the system .Sh SYNOPSIS .Nm +.Op Fl o .Sh DESCRIPTION The .Nm @@ -33,11 +34,21 @@ file and list vulnerable packages that are present in the system. It is main purpose to help .Xr portaudit 1 utility to avoid time-consuming scripting. +.Pp .Nm reads vulnerability information from the standard input and writes the list of vulnerable ports to the standard output. Format of the output lines is the same as for the audit file, but -package matching globs are substituted with the actual package names. +the names of matched packages are prepended to the field set and +package origins are appended to the field set, if it was requested +via the +.Fl o +flag. +.Pp +Thus, the input lines should look like +.D1 pkg_glob|references|description +and output lines will look like +.D1 pkg_version|pkg_glob|references|description[|origin] .Sh TECHNICAL DETAILS First the audit file is parsed to the internal representation (currently it is linked list). -- 1.6.2.4