Index: head/usr.sbin/pkg_install/add/extract.c =================================================================== --- head/usr.sbin/pkg_install/add/extract.c (revision 252362) +++ head/usr.sbin/pkg_install/add/extract.c (revision 252363) @@ -1,287 +1,288 @@ /* * FreeBSD install - a package for the installation and maintenance * of non-core utilities. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Jordan K. Hubbard * 18 July 1993 * * This is the package extraction code for the add module. * */ #include __FBSDID("$FreeBSD$"); #include #include #include "lib.h" #include "add.h" #define STARTSTRING "/usr/bin/tar cf -" #define TOOBIG(str) \ (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) ||\ ((int)strlen(str) + FILENAME_MAX + perm_count > maxargs)) #define PUSHOUT(todir) /* push out string */ \ if (where_count > (int)sizeof(STARTSTRING)-1) { \ strcat(where_args, "|/usr/bin/tar --unlink -xpPf - -C "); \ strcat(where_args, todir); \ if (system(where_args)) { \ cleanup(0); \ errx(2, "%s: can not invoke %ld byte tar pipeline: %s", \ __func__, (long)strlen(where_args), where_args); \ } \ strcpy(where_args, STARTSTRING); \ where_count = sizeof(STARTSTRING)-1; \ } \ if (perm_count) { \ apply_perms(todir, perm_args); \ perm_args[0] = 0;\ perm_count = 0; \ } static void rollback(const char *name, const char *home, PackingList start, PackingList stop) { PackingList q; char try[FILENAME_MAX], bup[FILENAME_MAX]; const char *dir; char *prefix = NULL; dir = home; for (q = start; q != stop; q = q->next) { if (q->type == PLIST_FILE) { snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name); if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) { (void)chflags(try, 0); (void)unlink(try); if (rename(bup, try)) warnx("rollback: unable to rename %s back to %s", bup, try); } } else if (q->type == PLIST_CWD) { if (!prefix) prefix = q->name; if (q->name == NULL) q->name = prefix; else if (strcmp(q->name, ".")) dir = q->name; else dir = home; } } } #define add_char(buf, len, pos, ch) do {\ if ((pos) < (len)) { \ buf[(pos)] = (ch); \ buf[(pos) + 1] = '\0'; \ } \ ++(pos); \ } while (0) static int add_arg(char *buf, int len, const char *str) { int i = 0; add_char(buf, len, i, ' '); for (; *str != '\0'; ++str) { if (!isalnum(*str) && *str != '/' && *str != '.' && *str != '-') add_char(buf, len, i, '\\'); add_char(buf, len, i, *str); } return (i); } void extract_plist(const char *home, Package *pkg) { PackingList p = pkg->head; char *last_file, *prefix = NULL; char *where_args, *perm_args, *last_chdir; - int maxargs, where_count = 0, perm_count = 0, add_count; + long maxargs; + int where_count = 0, perm_count = 0, add_count; Boolean preserve; maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */ where_args = alloca(maxargs); if (!where_args) { cleanup(0); errx(2, "%s: can't get argument list space", __func__); } perm_args = alloca(maxargs); if (!perm_args) { cleanup(0); errx(2, "%s: can't get argument list space", __func__); } strcpy(where_args, STARTSTRING); where_count = sizeof(STARTSTRING)-1; perm_args[0] = 0; last_chdir = 0; preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE; /* Reset the world */ Owner = NULL; Group = NULL; Mode = NULL; last_file = NULL; Directory = (char *)home; /* Do it */ while (p) { char cmd[FILENAME_MAX]; switch(p->type) { case PLIST_NAME: PkgName = p->name; if (Verbose) printf("extract: Package name is %s\n", p->name); break; case PLIST_FILE: last_file = p->name; if (Verbose) printf("extract: %s/%s\n", Directory, p->name); if (!Fake) { char try[FILENAME_MAX]; /* first try to rename it into place */ snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name); if (fexists(try)) { (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */ if (preserve && PkgName) { char pf[FILENAME_MAX]; if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) { if (rename(try, pf)) { warnx( "unable to back up %s to %s, aborting pkg_add", try, pf); rollback(PkgName, home, pkg->head, p); return; } } } } if (rename(p->name, try) == 0) { /* try to add to list of perms to be changed and run in bulk. */ if (p->name[0] == '/' || TOOBIG(p->name)) { PUSHOUT(Directory); } add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name); if (add_count < 0 || add_count >= maxargs - perm_count) { cleanup(0); errx(2, "%s: oops, miscounted strings!", __func__); } perm_count += add_count; } else { /* rename failed, try copying with a big tar command */ if (last_chdir != Directory) { if (last_chdir == NULL) { PUSHOUT(Directory); } else { PUSHOUT(last_chdir); } last_chdir = Directory; } else if (p->name[0] == '/' || TOOBIG(p->name)) { PUSHOUT(Directory); } add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name); if (add_count < 0 || add_count >= maxargs - where_count) { cleanup(0); errx(2, "%s: oops, miscounted strings!", __func__); } where_count += add_count; add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name); if (add_count < 0 || add_count >= maxargs - perm_count) { cleanup(0); errx(2, "%s: oops, miscounted strings!", __func__); } perm_count += add_count; } } break; case PLIST_CWD: if (!prefix) prefix = p->name; if (p->name == NULL) p->name = strdup(prefix); if (Verbose) printf("extract: CWD to %s\n", p->name); PUSHOUT(Directory); if (strcmp(p->name, ".")) { if (!Fake && make_hierarchy(p->name, TRUE) == FAIL) { cleanup(0); errx(2, "%s: unable to cwd to '%s'", __func__, p->name); } Directory = p->name; } else Directory = (char *)home; break; case PLIST_CMD: if ((strstr(p->name, "%B") || strstr(p->name, "%F") || strstr(p->name, "%f")) && last_file == NULL) { cleanup(0); errx(2, "%s: no last file specified for '%s' command", __func__, p->name); } if (strstr(p->name, "%D") && Directory == NULL) { cleanup(0); errx(2, "%s: no directory specified for '%s' command", __func__, p->name); } format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file); PUSHOUT(Directory); if (Verbose) printf("extract: execute '%s'\n", cmd); if (!Fake && system(cmd)) warnx("command '%s' failed", cmd); break; case PLIST_CHMOD: PUSHOUT(Directory); Mode = p->name; break; case PLIST_CHOWN: PUSHOUT(Directory); Owner = p->name; break; case PLIST_CHGRP: PUSHOUT(Directory); Group = p->name; break; case PLIST_COMMENT: /* FALLTHROUGH */ case PLIST_NOINST: break; case PLIST_IGNORE: p = p->next; break; default: break; } p = p->next; } PUSHOUT(Directory); } Index: head/usr.sbin/pkg_install/create/pl.c =================================================================== --- head/usr.sbin/pkg_install/create/pl.c (revision 252362) +++ head/usr.sbin/pkg_install/create/pl.c (revision 252363) @@ -1,280 +1,281 @@ /* * FreeBSD install - a package for the installation and maintenance * of non-core utilities. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Jordan K. Hubbard * 18 July 1993 * * Routines for dealing with the packing list. * */ #include __FBSDID("$FreeBSD$"); #include "lib.h" #include "create.h" #include #include #include /* Add an MD5 checksum entry for a file or link */ void add_cksum(Package *pkg, PackingList p, const char *fname) { char *cp = NULL, buf[33]; if (issymlink(fname)) { int len; char lnk[FILENAME_MAX]; if ((len = readlink(fname, lnk, FILENAME_MAX)) > 0) cp = MD5Data((unsigned char *)lnk, len, buf); } else if (isfile(fname)) { /* Don't record MD5 checksum for device nodes and such */ cp = MD5File(fname, buf); } if (cp != NULL) { PackingList tmp = new_plist_entry(); tmp->name = copy_string(strconcat("MD5:", cp)); tmp->type = PLIST_COMMENT; tmp->next = p->next; tmp->prev = p; p->next = tmp; if (pkg->tail == p) pkg->tail = tmp; } } /* Check a list for files that require preconversion */ void check_list(const char *home, Package *pkg) { const char *where = home; const char *there = NULL; char name[FILENAME_MAX]; char *prefix = NULL; PackingList p; for (p = pkg->head; p != NULL; p = p->next) switch (p->type) { case PLIST_CWD: if (!prefix) prefix = p->name; where = (p->name == NULL) ? prefix : p->name; break; case PLIST_IGNORE: p = p->next; break; case PLIST_SRC: there = p->name; break; case PLIST_FILE: if (there) snprintf(name, sizeof(name), "%s/%s", there, p->name); else snprintf(name, sizeof(name), "%s%s/%s", BaseDir && where && where[0] == '/' ? BaseDir : "", where, p->name); add_cksum(pkg, p, name); break; default: break; } } static int trylink(const char *from, const char *to) { if (link(from, to) == 0) return 0; if (errno == ENOENT) { /* try making the container directory */ char *cp = strrchr(to, '/'); if (cp) vsystem("/bin/mkdir -p %.*s", cp - to, to); return link(from, to); } return -1; } #define STARTSTRING "/usr/bin/tar cf -" #define TOOBIG(str) (int)strlen(str) + 6 + (int)strlen(home) + where_count > maxargs #define PUSHOUT() /* push out string */ \ if (where_count > (int)sizeof(STARTSTRING)-1) { \ strcat(where_args, "|/usr/bin/tar xpf -"); \ if (system(where_args)) { \ cleanup(0); \ errx(2, "%s: can't invoke tar pipeline", __func__); \ } \ memset(where_args, 0, maxargs); \ last_chdir = NULL; \ strcpy(where_args, STARTSTRING); \ where_count = sizeof(STARTSTRING)-1; \ } /* * Copy unmarked files in packing list to playpen - marked files * have already been copied in an earlier pass through the list. */ void copy_plist(const char *home, Package *plist) { PackingList p = plist->head; const char *where = home; const char *there = NULL, *mythere; char *where_args, *prefix = NULL; const char *last_chdir, *root = "/"; - int maxargs, where_count = 0, add_count; + long maxargs; + int where_count = 0, add_count; struct stat stb; dev_t curdir; maxargs = sysconf(_SC_ARG_MAX); maxargs -= 64; /* * Some slop for the tar cmd text, * and sh -c */ where_args = malloc(maxargs); if (!where_args) { cleanup(0); errx(2, "%s: can't get argument list space", __func__); } memset(where_args, 0, maxargs); strcpy(where_args, STARTSTRING); where_count = sizeof(STARTSTRING)-1; last_chdir = 0; if (stat(".", &stb) == 0) curdir = stb.st_dev; else curdir = (dev_t) -1; /* * It's ok if this is a valid dev_t; * this is just a hint for an * optimization. */ while (p) { if (p->type == PLIST_CWD) { if (!prefix) prefix = p->name; where = p->name == NULL ? prefix : p->name; } else if (p->type == PLIST_SRC) there = p->name; else if (p->type == PLIST_IGNORE) p = p->next; else if (p->type == PLIST_FILE && !p->marked) { char fn[FILENAME_MAX]; /* First, look for it in the "home" dir */ sprintf(fn, "%s/%s", home, p->name); if (fexists(fn)) { if (lstat(fn, &stb) == 0 && stb.st_dev == curdir && S_ISREG(stb.st_mode)) { /* * If we can link it to the playpen, that avoids a copy * and saves time. */ if (p->name[0] != '/') { /* * Don't link abspn stuff--it doesn't come from * local dir! */ if (trylink(fn, p->name) == 0) { p = p->next; continue; } } } if (TOOBIG(fn)) { PUSHOUT(); } if (p->name[0] == '/') { add_count = snprintf(&where_args[where_count], maxargs - where_count, " %s %s", last_chdir == root ? "" : "-C /", p->name); last_chdir = root; } else { add_count = snprintf(&where_args[where_count], maxargs - where_count, " %s%s %s", last_chdir == home ? "" : "-C ", last_chdir == home ? "" : home, p->name); last_chdir = home; } if (add_count < 0 || add_count >= maxargs - where_count) { cleanup(0); errx(2, "%s: oops, miscounted strings!", __func__); } where_count += add_count; } /* * Otherwise, try along the actual extraction path.. */ else { if (p->name[0] == '/') mythere = root; else mythere = there; if (mythere) snprintf(fn, sizeof(fn), "%s/%s", mythere, p->name); else snprintf(fn, sizeof(fn), "%s%s/%s", BaseDir && where && where[0] == '/' ? BaseDir : "", where, p->name); if (lstat(fn, &stb) == 0 && stb.st_dev == curdir && S_ISREG(stb.st_mode)) { /* * If we can link it to the playpen, that avoids a copy * and saves time. */ if (trylink(fn, p->name) == 0) { p = p->next; continue; } } if (TOOBIG(p->name)) { PUSHOUT(); } if (last_chdir == (mythere ? mythere : where)) add_count = snprintf(&where_args[where_count], maxargs - where_count, " %s", p->name); else add_count = snprintf(&where_args[where_count], maxargs - where_count, " -C %s %s", mythere ? mythere : where, p->name); if (add_count < 0 || add_count >= maxargs - where_count) { cleanup(0); errx(2, "%s: oops, miscounted strings!", __func__); } where_count += add_count; last_chdir = (mythere ? mythere : where); } } p = p->next; } PUSHOUT(); free(where_args); } Index: head/usr.sbin/pkg_install/lib/exec.c =================================================================== --- head/usr.sbin/pkg_install/lib/exec.c (revision 252362) +++ head/usr.sbin/pkg_install/lib/exec.c (revision 252363) @@ -1,110 +1,110 @@ /* * FreeBSD install - a package for the installation and maintenance * of non-core utilities. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Jordan K. Hubbard * 18 July 1993 * * Miscellaneous system routines. * */ #include __FBSDID("$FreeBSD$"); #include "lib.h" #include /* * Unusual system() substitute. Accepts format string and args, * builds and executes command. Returns exit code. */ int vsystem(const char *fmt, ...) { va_list args; char *cmd; long maxargs; int ret; maxargs = sysconf(_SC_ARG_MAX); maxargs -= 32; /* some slop for the sh -c */ cmd = malloc(maxargs); if (!cmd) { warnx("vsystem can't alloc arg space"); return 1; } va_start(args, fmt); if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) { warnx("vsystem args are too long"); va_end(args); return 1; } #ifdef DEBUG printf("Executing %s\n", cmd); #endif ret = system(cmd); va_end(args); free(cmd); return ret; } char * vpipe(const char *fmt, ...) { FILE *fp; char *cmd, *rp; - int maxargs; + long maxargs; va_list args; rp = malloc(MAXPATHLEN); if (!rp) { warnx("vpipe can't alloc buffer space"); return NULL; } maxargs = sysconf(_SC_ARG_MAX); maxargs -= 32; /* some slop for the sh -c */ cmd = alloca(maxargs); if (!cmd) { warnx("vpipe can't alloc arg space"); return NULL; } va_start(args, fmt); if (vsnprintf(cmd, maxargs, fmt, args) > maxargs) { warnx("vsystem args are too long"); va_end(args); return NULL; } #ifdef DEBUG fprintf(stderr, "Executing %s\n", cmd); #endif fflush(NULL); fp = popen(cmd, "r"); if (fp == NULL) { warnx("popen() failed"); va_end(args); return NULL; } get_string(rp, MAXPATHLEN, fp); #ifdef DEBUG fprintf(stderr, "Returned %s\n", rp); #endif va_end(args); if (pclose(fp) || (strlen(rp) == 0)) { free(rp); return NULL; } return rp; }