summaryrefslogtreecommitdiffstats
path: root/plugins/check_apt.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_apt.c')
-rw-r--r--plugins/check_apt.c65
1 files changed, 28 insertions, 37 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index eb08a06..c2c693e 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -94,12 +94,6 @@ static int stderr_warning = 0; /* if a cmd issued output on stderr */
94static int exec_warning = 0; /* if a cmd exited non-zero */ 94static int exec_warning = 0; /* if a cmd exited non-zero */
95 95
96int main(int argc, char **argv) { 96int main(int argc, char **argv) {
97 int result = STATE_UNKNOWN;
98 int packages_available = 0;
99 int sec_count = 0;
100 char **packages_list = NULL;
101 char **secpackages_list = NULL;
102
103 /* Parse extra opts if any */ 97 /* Parse extra opts if any */
104 argv = np_extra_opts(&argc, argv, progname); 98 argv = np_extra_opts(&argc, argv, progname);
105 99
@@ -115,11 +109,16 @@ int main(int argc, char **argv) {
115 /* handle timeouts gracefully... */ 109 /* handle timeouts gracefully... */
116 alarm(timeout_interval); 110 alarm(timeout_interval);
117 111
112 int result = STATE_UNKNOWN;
118 /* if they want to run apt-get update first... */ 113 /* if they want to run apt-get update first... */
119 if (do_update) { 114 if (do_update) {
120 result = run_update(); 115 result = run_update();
121 } 116 }
122 117
118 int packages_available = 0;
119 int sec_count = 0;
120 char **packages_list = NULL;
121 char **secpackages_list = NULL;
123 /* apt-get upgrade */ 122 /* apt-get upgrade */
124 result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list)); 123 result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
125 124
@@ -156,8 +155,6 @@ int main(int argc, char **argv) {
156 155
157/* process command-line arguments */ 156/* process command-line arguments */
158int process_arguments(int argc, char **argv) { 157int process_arguments(int argc, char **argv) {
159 int c;
160
161 static struct option longopts[] = {{"version", no_argument, 0, 'V'}, 158 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
162 {"help", no_argument, 0, 'h'}, 159 {"help", no_argument, 0, 'h'},
163 {"verbose", no_argument, 0, 'v'}, 160 {"verbose", no_argument, 0, 'v'},
@@ -175,14 +172,14 @@ int process_arguments(int argc, char **argv) {
175 {"packages-warning", required_argument, 0, 'w'}, 172 {"packages-warning", required_argument, 0, 'w'},
176 {0, 0, 0, 0}}; 173 {0, 0, 0, 0}};
177 174
178 while (1) { 175 while (true) {
179 c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); 176 int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
180 177
181 if (c == -1 || c == EOF || c == 1) { 178 if (option_char == -1 || option_char == EOF || option_char == 1) {
182 break; 179 break;
183 } 180 }
184 181
185 switch (c) { 182 switch (option_char) {
186 case 'h': 183 case 'h':
187 print_help(); 184 print_help();
188 exit(STATE_UNKNOWN); 185 exit(STATE_UNKNOWN);
@@ -257,18 +254,7 @@ int process_arguments(int argc, char **argv) {
257 254
258/* run an apt-get upgrade */ 255/* run an apt-get upgrade */
259int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) { 256int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) {
260 int result = STATE_UNKNOWN;
261 int regres = 0;
262 int pc = 0;
263 int spc = 0;
264 struct output chld_out;
265 struct output chld_err;
266 regex_t ireg;
267 regex_t ereg; 257 regex_t ereg;
268 regex_t sreg;
269 char *cmdline = NULL;
270 char rerrbuf[64];
271
272 /* initialize ereg as it is possible it is printed while uninitialized */ 258 /* initialize ereg as it is possible it is printed while uninitialized */
273 memset(&ereg, '\0', sizeof(ereg.buffer)); 259 memset(&ereg, '\0', sizeof(ereg.buffer));
274 260
@@ -276,6 +262,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
276 return STATE_OK; 262 return STATE_OK;
277 } 263 }
278 264
265 int regres = 0;
266 regex_t ireg;
267 char rerrbuf[64];
279 /* compile the regexps */ 268 /* compile the regexps */
280 if (do_include != NULL) { 269 if (do_include != NULL) {
281 regres = regcomp(&ireg, do_include, REG_EXTENDED); 270 regres = regcomp(&ireg, do_include, REG_EXTENDED);
@@ -293,6 +282,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
293 } 282 }
294 } 283 }
295 284
285 regex_t sreg;
296 const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE; 286 const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE;
297 regres = regcomp(&sreg, crit_ptr, REG_EXTENDED); 287 regres = regcomp(&sreg, crit_ptr, REG_EXTENDED);
298 if (regres != 0) { 288 if (regres != 0) {
@@ -300,6 +290,10 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
300 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); 290 die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
301 } 291 }
302 292
293 int result = STATE_UNKNOWN;
294 struct output chld_out;
295 struct output chld_err;
296 char *cmdline = NULL;
303 cmdline = construct_cmdline(upgrade, upgrade_opts); 297 cmdline = construct_cmdline(upgrade, upgrade_opts);
304 if (input_filename != NULL) { 298 if (input_filename != NULL) {
305 /* read input from a file for testing */ 299 /* read input from a file for testing */
@@ -336,6 +330,8 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
336 * we may need to switch to the --print-uris output format, 330 * we may need to switch to the --print-uris output format,
337 * in which case the logic here will slightly change. 331 * in which case the logic here will slightly change.
338 */ 332 */
333 int pc = 0;
334 int spc = 0;
339 for (size_t i = 0; i < chld_out.lines; i++) { 335 for (size_t i = 0; i < chld_out.lines; i++) {
340 if (verbose) { 336 if (verbose) {
341 printf("%s\n", chld_out.line[i]); 337 printf("%s\n", chld_out.line[i]);
@@ -389,12 +385,12 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
389/* run an apt-get update (needs root) */ 385/* run an apt-get update (needs root) */
390int run_update(void) { 386int run_update(void) {
391 int result = STATE_UNKNOWN; 387 int result = STATE_UNKNOWN;
392 struct output chld_out;
393 struct output chld_err;
394 char *cmdline; 388 char *cmdline;
395
396 /* run the update */ 389 /* run the update */
397 cmdline = construct_cmdline(NO_UPGRADE, update_opts); 390 cmdline = construct_cmdline(NO_UPGRADE, update_opts);
391
392 struct output chld_out;
393 struct output chld_err;
398 result = np_runcmd(cmdline, &chld_out, &chld_err, 0); 394 result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
399 /* apt-get update changes exit status if it can't fetch packages. 395 /* apt-get update changes exit status if it can't fetch packages.
400 * since we were explicitly asked to do so, this is treated as 396 * since we were explicitly asked to do so, this is treated as
@@ -426,20 +422,16 @@ int run_update(void) {
426} 422}
427 423
428char *pkg_name(char *line) { 424char *pkg_name(char *line) {
429 char *start = NULL; 425 char *start = line + strlen(PKGINST_PREFIX);
430 char *space = NULL;
431 char *pkg = NULL;
432 int len = 0;
433 426
434 start = line + strlen(PKGINST_PREFIX); 427 int len = strlen(start);
435 len = strlen(start);
436 428
437 space = index(start, ' '); 429 char *space = index(start, ' ');
438 if (space != NULL) { 430 if (space != NULL) {
439 len = space - start; 431 len = space - start;
440 } 432 }
441 433
442 pkg = malloc(sizeof(char) * (len + 1)); 434 char *pkg = malloc(sizeof(char) * (len + 1));
443 if (!pkg) { 435 if (!pkg) {
444 die(STATE_UNKNOWN, "malloc failed!\n"); 436 die(STATE_UNKNOWN, "malloc failed!\n");
445 } 437 }
@@ -475,10 +467,8 @@ char *add_to_regexp(char *expr, const char *next) {
475} 467}
476 468
477char *construct_cmdline(upgrade_type u, const char *opts) { 469char *construct_cmdline(upgrade_type u, const char *opts) {
478 int len = 0;
479 const char *opts_ptr = NULL; 470 const char *opts_ptr = NULL;
480 const char *aptcmd = NULL; 471 const char *aptcmd = NULL;
481 char *cmd = NULL;
482 472
483 switch (u) { 473 switch (u) {
484 case UPGRADE: 474 case UPGRADE:
@@ -507,11 +497,12 @@ char *construct_cmdline(upgrade_type u, const char *opts) {
507 break; 497 break;
508 } 498 }
509 499
500 int len = 0;
510 len += strlen(PATH_TO_APTGET) + 1; /* "/usr/bin/apt-get " */ 501 len += strlen(PATH_TO_APTGET) + 1; /* "/usr/bin/apt-get " */
511 len += strlen(opts_ptr) + 1; /* "opts " */ 502 len += strlen(opts_ptr) + 1; /* "opts " */
512 len += strlen(aptcmd) + 1; /* "upgrade\0" */ 503 len += strlen(aptcmd) + 1; /* "upgrade\0" */
513 504
514 cmd = (char *)malloc(sizeof(char) * len); 505 char *cmd = (char *)malloc(sizeof(char) * len);
515 if (cmd == NULL) { 506 if (cmd == NULL) {
516 die(STATE_UNKNOWN, "malloc failed"); 507 die(STATE_UNKNOWN, "malloc failed");
517 } 508 }