--- logrotate-3.6.2.orig/logrotate.c Tue Jan 29 23:27:17 2002 +++ logrotate-3.6.2/logrotate.c Sat Mar 23 21:38:29 2002 @@ -63,46 +63,76 @@ return (states + i); } -static int runScript(char * logfn, char * script) { - int fd; - char filespec[32]; - char * cmd; - int rc; - +static int +runScript(const char *logfn, const char *script) +{ if (debug) { - message(MESS_DEBUG, "running script with arg %s: \"%s\"\n", + message(MESS_DEBUG, "running script with arg %s: \"%s\"\n", logfn, script); return 0; - } - - strcpy(filespec, "/tmp/logrotate.XXXXXX"); - if ((fd = mkstemp(filespec)) < 0 || fchmod(fd, 0700)) { - message(MESS_DEBUG, "error creating %s: %s\n", filespec, - strerror(errno)); - if (fd >= 0) close(fd); - return -1; - } + } else { + static const char interpeter_cmd[] = "#!/bin/sh\n\n"; + const char *tmpdir = NULL; + char *filespec = NULL, *cmd = NULL; + int len, fd; + + if (geteuid() == 0) { + tmpdir = "/var/run/logrotate"; + } else { + tmpdir = getenv("TMPDIR"); + if (!tmpdir || !*tmpdir) + tmpdir = "/tmp"; + } + + if (asprintf(&filespec, "%s/%s", tmpdir, "script-XXXXXX") < 0) { + message(MESS_ERROR, "asprintf: %m\n"); + return -1; + } + + if ((fd = mkstemp(filespec)) < 0) { + message(MESS_ERROR, "error creating %s: %m\n", filespec); + free(filespec); + return -1; + } + + len = sizeof(interpeter_cmd) - 1; + if (write(fd, interpeter_cmd, len) != len) { + message(MESS_ERROR, "error writing %s[%d]: %m\n", filespec, fd); + (void) unlink(filespec); + free(filespec); + close(fd); + return -1; + } + + len = strlen(script); + if (write(fd, script, len) != len) { + message(MESS_ERROR, "error writing %s[%d]: %m\n", filespec, fd); + (void) unlink(filespec); + free(filespec); + close(fd); + return -1; + } - if (write(fd, "#!/bin/sh\n\n", 11) != 11) { - close(fd); - unlink(filespec); - return -1; - } - if (write(fd, script, strlen(script)) != strlen(script)) { close(fd); - unlink(filespec); - return -1; - } - close(fd); + if (asprintf(&cmd, "/bin/sh %s '%s'", filespec, logfn) < 0) { + message(MESS_ERROR, "asprintf: %m\n"); + (void) unlink(filespec); + free(filespec); + return -1; + } - cmd = alloca(strlen(filespec) + strlen(logfn) + 20); - sprintf(cmd, "/bin/sh %s '%s'", filespec, logfn); - rc = system(cmd); + fd = system(cmd); + free(cmd); - unlink(filespec); + if (unlink(filespec) < 0) { + message(MESS_ERROR, "error unlinking %s: %m\n", filespec); + fd = -1; + } - return rc; + free(filespec); + return fd; + } } static int copyTruncate(char * currLog, char * saveLog, struct stat * sb, int flags) {