#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include "zpm.h" /* * -f package file, otherwise localdb * -p phase, defaults to 'configure' * -s script name, defaults to /var/tmp/zpm-script * -r chroot to directory * -o script output, /var/tmp/zpm-script.out, '-' for stdout * * arg is package id */ void usage(void) { fprintf(stderr, "usage: db hash file\n"); } int run(char *program, char **args, char *output, int *status) { /* if stdout is a terminal, leave stdout where it goes, * if it's not a terminal, redirect stdout to output. * in either case, send stderr to output, unless null * if output is null or "-", just run the program */ int interactive = 0; pid_t pid; int rv; errno = 0; interactive = isatty(1); pid = fork(); if (pid == -1) { *status = 1; return -1; } if (pid == 0) { /* child */ if (output && strcmp(output, "-") != 0) { close(2); rv = open(output, O_NOFOLLOW|O_TRUNC|O_CREAT|O_WRONLY, 0600); if (rv == -1) { perror("cannot open output file"); } if (!interactive) { rv = dup2(2,1); if (rv == -1) { perror("unable to redirect stdout"); exit(EXIT_FAILURE); } } } rv = execvp(program, args); if (rv == -1) { perror("cannot exec"); exit(EXIT_FAILURE); } } pid = wait(status); if (pid == -1) { perror("error waiting for child"); return -2; } if (WIFEXITED(*status)) { return WEXITSTATUS(*status); } return -3; } int main(int ac, char **av){ struct zpm zpm; int rv; int status; int failures = 0; char *pkgstr; int opt; int required = 0; char hash[ZPM_HASH_STRLEN+1]; char *args[4]; char *pkgid; char *rootdir = 0; char *db = "/var/lib/zpm/zpm.db"; char *script = "/var/tmp/zpm-script"; char *output = "/var/tmp/zpm-script.out"; char *phase = "configure"; if (getenv("ZPMDB")) { db = getenv("ZPMDB"); } /* ZPM_PACKAGE_FILE ? */ rootdir = getenv("ZPM_ROOT_DIR"); while ((opt = getopt(ac, av, "f:p:o:s:r:R")) != -1) { switch (opt) { case 'f': db = optarg; break; case 'p': phase = optarg; break; case 's': script = optarg; break; case 'o': output = optarg; break; case 'r': rootdir = optarg; break; case 'R': required = 1; break; default: usage(); exit(EXIT_FAILURE); break; } } int argn = optind; if (argn >= ac) { usage(); exit(EXIT_FAILURE); } pkgstr = av[argn]; if (!zpm_open(&zpm, db)) { fprintf(stderr, "unable to open zpm database: %s\n", db); if (zpm.errmsg) { fprintf(stderr, "error detail: %s\n", zpm.errmsg); } exit(EXIT_FAILURE); } pkgid = zpm_findpkg(&zpm, pkgstr); if (pkgid) { if (zpm_script_hash(&zpm, pkgid, phase, hash)) { /* since the script file name doesn't really * mean anything, pass in the phase as arg 0 */ /* TODO sanitize environment ? */ args[0] = phase; args[1] = pkgid; args[2] = 0; args[3] = 0; if (argn + 1 <= ac) { args[2] = av[argn+1]; } if (rootdir) { if (chdir(rootdir) == -1) { perror("can not chdir to rootdir"); exit(EXIT_FAILURE); } if (geteuid() == 0) { /* chroot is deprecated, and not in * posix. need to use OS/kernel * specific code. */ fprintf(stderr, "support for chroot equivalent not supported on this OS\n"); } else { fprintf(stderr, "unable to chroot as non root user\n"); } } else { if (chdir("/") == -1) { perror("can not chdir to /"); exit(EXIT_FAILURE); } } if (!zpm_extract(&zpm, hash, script, 0700)) { fprintf(stderr, "unable to extract script"); exit(EXIT_FAILURE); } rv = run(script, args, output, &status); if (rv) { // cat(output); fprintf(stderr, "package %s script failed with code %d\n", pkgid, rv); } /* TODO log output */ if (script) { unlink(script); } if (output) { unlink(output); } if (rv) { failures++; } } else { if (required) { fprintf(stderr, "no script for %s %s\n", phase, pkgid); failures++; } } free(pkgid); } else { fprintf(stderr, "unable to find package for %s in %s\n", pkgstr, db); failures++; } zpm_close(&zpm); return failures ? EXIT_FAILURE : EXIT_SUCCESS; }