diff --git a/add/futil.c b/add/futil.c index b4d4af1..dacbc25 100644 --- a/add/futil.c +++ b/add/futil.c @@ -50,7 +50,7 @@ make_hierarchy(char *dir) } } else { - if (vsystem("/bin/mkdir %s", dir)) { + if (mkdir(dir, 0777) == -1) { if (cp2) *cp2 = '/'; return FAIL; diff --git a/add/main.c b/add/main.c index bf77f4f..7afe99b 100644 --- a/add/main.c +++ b/add/main.c @@ -120,8 +120,8 @@ main(int argc, char **argv) int ch, error; char **start; char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr; - static char temppackageroot[MAXPATHLEN]; - static char pkgaddpath[MAXPATHLEN]; + static char temppackageroot[FILENAME_MAX]; + static char pkgaddpath[FILENAME_MAX]; if (*argv[0] != '/' && strchr(argv[0], '/') != NULL) PkgAddCmd = realpath(argv[0], pkgaddpath); @@ -209,7 +209,7 @@ main(int argc, char **argv) /* Get all the remaining package names, if any */ for (ch = 0; *argv; ch++, argv++) { - char temp[MAXPATHLEN]; + char temp[FILENAME_MAX]; if (Remote) { if ((packagesite = getpackagesite()) == NULL) errx(1, "package name too long"); @@ -293,7 +293,7 @@ static char * getpackagesite(void) { int reldate, i; - static char sitepath[MAXPATHLEN]; + static char sitepath[FILENAME_MAX]; struct utsname u; if (getenv("PACKAGESITE")) { diff --git a/add/perform.c b/add/perform.c index ece3e95..eb98d27 100644 --- a/add/perform.c +++ b/add/perform.c @@ -79,6 +79,7 @@ pkg_do(char *pkg) char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX]; char *conflict[2]; char **matched; + int fd; conflictsfound = 0; code = 0; @@ -298,7 +299,7 @@ pkg_do(char *pkg) char path[FILENAME_MAX], *cp = NULL; if (!Fake) { - char prefixArg[2 + MAXPATHLEN]; /* "-P" + Prefix */ + char prefixArg[2 + FILENAME_MAX]; /* "-P" + Prefix */ if (PrefixRecursive) { strlcpy(prefixArg, "-P", sizeof(prefixArg)); strlcat(prefixArg, Prefix, sizeof(prefixArg)); @@ -371,8 +372,10 @@ pkg_do(char *pkg) goto bomb; /* Look for the requirements file */ - if (fexists(REQUIRE_FNAME)) { - vsystem("/bin/chmod +x %s", REQUIRE_FNAME); /* be sure */ + if ((fd = open(REQUIRE_FNAME, O_RDWR)) != -1) { + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */ + close(fd); if (Verbose) printf("Running requirements file first for %s..\n", Plist.name); if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) { @@ -404,8 +407,10 @@ pkg_do(char *pkg) } /* If we're really installing, and have an installation file, run it */ - if (!NoInstall && fexists(pre_script)) { - vsystem("/bin/chmod +x %s", pre_script); /* make sure */ + if (!NoInstall && (fd = open(pre_script, O_RDWR)) != -1) { + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */ + close(fd); if (Verbose) printf("Running pre-install for %s..\n", Plist.name); if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) { @@ -433,8 +438,10 @@ pkg_do(char *pkg) } /* Run the installation script one last time? */ - if (!NoInstall && fexists(post_script)) { - vsystem("/bin/chmod +x %s", post_script); /* make sure */ + if (!NoInstall && (fd = open(post_script, O_RDWR)) != -1) { + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */ + close(fd); if (Verbose) printf("Running post-install for %s..\n", Plist.name); if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) { @@ -466,7 +473,10 @@ pkg_do(char *pkg) goto success; /* close enough for government work */ } /* Make sure pkg_info can read the entry */ - vsystem("/bin/chmod a+rx %s", LogDir); + fd = open(LogDir, O_RDWR); + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IRALL | S_IXALL); /* be sure, chmod a+rx */ + close(fd); move_file(".", DESC_FNAME, LogDir); move_file(".", COMMENT_FNAME, LogDir); if (fexists(INSTALL_FNAME)) diff --git a/create/perform.c b/create/perform.c index 8a7ff84..d4d137a 100644 --- a/create/perform.c +++ b/create/perform.c @@ -545,7 +545,7 @@ create_from_installed(const char *ipkg, const char *pkg, const char *suf) { FILE *fp; Package plist; - char homedir[MAXPATHLEN], log_dir[FILENAME_MAX]; + char homedir[FILENAME_MAX], log_dir[FILENAME_MAX]; snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg); if (!fexists(log_dir)) { diff --git a/delete/perform.c b/delete/perform.c index 13726ba..a245ff4 100644 --- a/delete/perform.c +++ b/delete/perform.c @@ -132,6 +132,8 @@ pkg_do(char *pkg) const char *post_script, *pre_arg, *post_arg; struct reqr_by_entry *rb_entry; struct reqr_by_head *rb_list; + int fd; + struct stat sb; if (!pkg || !(len = strlen(pkg))) return 1; @@ -221,10 +223,12 @@ pkg_do(char *pkg) setenv(PKG_PREFIX_VNAME, p->name, 1); - if (fexists(REQUIRE_FNAME)) { + if ((fd = open(REQUIRE_FNAME, O_RDWR)) != -1) { + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */ + close(fd); if (Verbose) printf("Executing 'require' script.\n"); - vsystem("/bin/chmod +x %s", REQUIRE_FNAME); /* be sure */ if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) { warnx("package %s fails requirements %s", pkg, Force ? "" : "- not deleted"); @@ -250,11 +254,13 @@ pkg_do(char *pkg) post_script = pre_arg = post_arg = NULL; } - if (!NoDeInstall && pre_script != NULL && fexists(pre_script)) { + if (!NoDeInstall && pre_script != NULL && (fd = open(pre_script, O_RDWR)) != -1) { if (Fake) printf("Would execute de-install script at this point.\n"); else { - vsystem("/bin/chmod +x %s", pre_script); /* make sure */ + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */ + close(fd); if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) { warnx("deinstall script returned error status"); if (!Force) @@ -326,11 +332,13 @@ pkg_do(char *pkg) return 1; } - if (!NoDeInstall && post_script != NULL && fexists(post_script)) { + if (!NoDeInstall && post_script != NULL && (fd = open(post_script, O_RDWR)) != -1) { if (Fake) printf("Would execute post-deinstall script at this point.\n"); else { - vsystem("/bin/chmod +x %s", post_script); /* make sure */ + fstat(fd, &sb); + fchmod(fd, sb.st_mode | S_IXALL); /* be sure, chmod a+x */ + close(fd); if (vsystem("./%s %s %s", post_script, pkg, post_arg)) { warnx("post-deinstall script returned error status"); if (!Force) diff --git a/info/perform.c b/info/perform.c index 658f645..77b9783 100644 --- a/info/perform.c +++ b/info/perform.c @@ -265,7 +265,7 @@ abspath(const char *pathname) int len; if (pathname[0] != '/') { - cwd = getcwd(NULL, MAXPATHLEN); + cwd = getcwd(NULL, FILENAME_MAX); asprintf(&resolved_path, "%s/%s/", cwd, pathname); } else asprintf(&resolved_path, "%s/", pathname); diff --git a/lib/exec.c b/lib/exec.c index 8da2001..39e88fe 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -66,7 +66,7 @@ vpipe(const char *fmt, ...) int maxargs; va_list args; - rp = malloc(MAXPATHLEN); + rp = malloc(FILENAME_MAX); if (!rp) { warnx("vpipe can't alloc buffer space"); return NULL; @@ -93,7 +93,7 @@ vpipe(const char *fmt, ...) warnx("popen() failed"); return NULL; } - get_string(rp, MAXPATHLEN, fp); + get_string(rp, FILENAME_MAX, fp); #ifdef DEBUG fprintf(stderr, "Returned %s\n", rp); #endif diff --git a/lib/file.c b/lib/file.c index 2442400..9032711 100644 --- a/lib/file.c +++ b/lib/file.c @@ -31,10 +31,13 @@ __FBSDID("$FreeBSD: src/usr.sbin/pkg_install/lib/file.c,v 1.68 2004/07/28 16:03: Boolean fexists(const char *fname) { - struct stat dummy; - if (!lstat(fname, &dummy)) - return TRUE; - return FALSE; + int fd; + + if ((fd = open(fname, O_RDONLY)) == -1) + return FALSE; + + close(fd); + return TRUE; } /* Quick check to see if something is a directory or symlink to a directory */ @@ -279,17 +282,23 @@ copy_file(const char *dir, const char *fname, const char *to) } void -move_file(const char *dir, const char *fname, const char *to) +move_file(const char *dir, const char *fname, const char *tdir) { - char cmd[FILENAME_MAX]; + char from[FILENAME_MAX]; + char to[FILENAME_MAX]; if (fname[0] == '/') - snprintf(cmd, FILENAME_MAX, "/bin/mv %s %s", fname, to); + strncpy(from, fname, FILENAME_MAX); else - snprintf(cmd, FILENAME_MAX, "/bin/mv %s/%s %s", dir, fname, to); - if (vsystem(cmd)) { - cleanup(0); - errx(2, "%s: could not perform '%s'", __func__, cmd); + snprintf(from, FILENAME_MAX, "%s/%s", dir, fname); + + snprintf(to, FILENAME_MAX, "%s/%s", tdir, fname); + + if (rename(from, to) == -1) { + if (vsystem("/bin/mv %s %s", from, to)) { + cleanup(0); + errx(2, "%s: could not move '%s' to '%s'", __func__, from, to); + } } } diff --git a/lib/lib.h b/lib/lib.h index ebd032c..c76d337 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -51,6 +51,11 @@ #define YES 2 #define NO 1 +/* Some more stat macros. */ +#define S_IRALL 0000444 +#define S_IWALL 0000222 +#define S_IXALL 0000111 + /* Usually "rm", but often "echo" during debugging! */ #define REMOVE_CMD "/bin/rm" diff --git a/updating/main.c b/updating/main.c index e5e649b..a91e1d1 100644 --- a/updating/main.c +++ b/updating/main.c @@ -60,7 +60,7 @@ main(int argc, char *argv[]) char originline[LINE_MAX]; /* Line of +CONTENTS */ /* Temporary variable to create path to +CONTENTS for installed ports. */ - char tmp_file[MAXPATHLEN]; + char tmp_file[FILENAME_MAX]; char updatingline[LINE_MAX]; /* Line of UPDATING */ int ch; /* Char used by getopt */