--- rotatelogs.c.orig 2004-02-26 14:10:34.000000000 +0100 +++ rotatelogs.c 2004-02-27 10:30:45.000000000 +0100 @@ -69,6 +69,7 @@ #include "apr_lib.h" #include "apr_strings.h" #include "apr_errno.h" +#include "apr_getopt.h" #include "apr_file_io.h" #include "apr_file_info.h" #include "apr_general.h" @@ -83,6 +84,10 @@ #if APR_HAVE_STRINGS_H #include #endif +#ifdef HAVE_PWD_H +#include +#endif + #define BUFSIZE 65536 #define ERRMSGSZ 82 @@ -91,6 +96,26 @@ #define MAX_PATH 1024 #endif +static void usage(const char *progname) +{ +fprintf(stderr, +"Usage: %s -f -u [username] -t ",progname); +fprintf(stderr, +"-o [offset minutes from UTC] or -m \n\n"); +fprintf(stderr, +"E.g. add this:\n\nTransferLog \"|%s -f /some/where -t 86400\"\n\n",progname); +fprintf(stderr, +"or\n\nTransferLog \"|%s -f /some/where -m 5\"\n\n",progname); +fprintf(stderr, +"to httpd.conf. The generated name will be /some/where.nnnn " +"where nnnn is the\nsystem time at which the log nominally " +"starts (N.B. if using a rotation time,\nthe time will always " +"be a multiple of the rotation time, so you can synchronize\n" +"cron scripts with it). At the end of each rotation time or " +"when the file size\nis reached a new log is started.\n"); +exit(1); +} + int main (int argc, const char * const argv[]) { char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ]; @@ -98,65 +123,73 @@ unsigned int sRotation = 0; int nMessCount = 0; apr_size_t nRead, nWrite; + apr_status_t rv; int use_strftime = 0; int now = 0; + int c; const char *szLogRoot; + char *user = NULL; apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL; apr_pool_t *pool; - char *ptr = NULL; + apr_getopt_t *opt; + const char *optarg; + struct passwd *pwd; + apr_uid_t retreived_uid=0; + apr_gid_t retreived_gid=0; apr_app_initialize(&argc, &argv, NULL); atexit(apr_terminate); apr_pool_create(&pool, NULL); - if (argc < 3 || argc > 4) { - fprintf(stderr, - "Usage: %s " - "[offset minutes from UTC] or \n\n", - argv[0]); -#ifdef OS2 - fprintf(stderr, - "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n", - argv[0]); -#else - fprintf(stderr, - "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n", - argv[0]); - fprintf(stderr, - "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]); -#endif - fprintf(stderr, - "to httpd.conf. The generated name will be /some/where.nnnn " - "where nnnn is the\nsystem time at which the log nominally " - "starts (N.B. if using a rotation time,\nthe time will always " - "be a multiple of the rotation time, so you can synchronize\n" - "cron scripts with it). At the end of each rotation time or " - "when the file size\nis reached a new log is started.\n"); - exit(1); - } - szLogRoot = argv[1]; - - ptr = strchr (argv[2], 'M'); - if (ptr) { - if (*(ptr+1) == '\0') { - sRotation = atoi(argv[2]) * 1048576; - } - if (sRotation == 0) { - fprintf(stderr, "Invalid rotation size parameter\n"); - exit(1); - } - } - else { - if (argc >= 4) { - utc_offset = atoi(argv[3]) * 60; - } - tRotation = atoi(argv[2]); - if (tRotation <= 0) { - fprintf(stderr, "Rotation time must be > 0\n"); - exit(6); - } - } + apr_getopt_init(&opt, pool, argc, argv); + while ((rv=apr_getopt(opt,"u:f:t:o:m:",(char *)&c,&optarg)) == APR_SUCCESS) + { + switch (c) + { + case 'f': + szLogRoot = strdup(optarg); + break; + case 'o': + utc_offset = atoi(optarg) * 60; + break; + case 'm': + sRotation = atoi(optarg) * 1048576; + if (sRotation == 0) { + fprintf(stderr, "Invalid rotation size parameter\n"); + exit(1); + } + break; + case 't': + tRotation = atoi(optarg); + if (tRotation <= 0) { + fprintf(stderr, "Rotation time must be > 0\n"); + exit(6); + } + break; + case 'u': + user = strdup(optarg); + break; + default: + usage(argv[0]); + } + } + if (rv == APR_BADCH) { + usage(argv[0]); + } + + if (user != NULL) { +#if APR_HAS_USER + rv = apr_uid_get(&retreived_uid, &retreived_gid, user, pool); + if (setuid(retreived_uid) != 0) { + fprintf(stderr,"Failed to setuid %s\n",user); + exit(1); + } +#else + fprintf(stderr,"setuid not supported by your OS\n"); + exit(1); +#endif + } use_strftime = (strchr(szLogRoot, '%') != NULL); if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {