Subject: cronolog patch From: Johan Verrept Date: Fri, 16 May 2003 13:42:03 +0200 To: A.Ford@ford-mason.co.uk hello andrew, I moved to using your cronolog to handle my logfiles. As my scripts log in the same directory as my apache, I had a problem. Because cronolog runs as root, I could not write logs into the directories cronolog created from my php scripts (who run as nobody:nobody). As a solution, I have implemented priviledge seperation. This allows the user to control the user and group of the created log files (and directories). The patch adds two options --user and --group to cronolog.c and one function to the cronoutils.c. I have not looked at the other utilities. regards, J. diff -ru cronolog-1.6.2/src/cronolog.c cronolog-1.6.2-new/src/cronolog.c --- cronolog-1.6.2/src/cronolog.c Thu May 3 09:42:48 2001 +++ cronolog-1.6.2-new/src/cronolog.c Fri May 16 02:43:57 2003 @@ -117,12 +117,15 @@ " -e, --european European date formats (default)\n" \ " -s, --start-time=TIME starting time\n" \ " -z TZ, --time-zone=TZ use TZ for timezone\n" \ - " -V, --version print version number, then exit\n" + " -V, --version print version number, then exit\n" \ + " -u, --user set the user name\n" \ + " -g, --group set the group name\n" + /* Definition of the short and long program options */ -char *short_options = "ad:eop:s:z:H:P:S:l:hVx:"; +char *short_options = "ad:eop:s:z:H:P:S:l:hVx:u:g:"; #ifndef _WIN32 struct option long_options[] = @@ -139,7 +142,9 @@ { "delay", required_argument, NULL, 'd' }, { "once-only", no_argument, NULL, 'o' }, { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' } + { "version", no_argument, NULL, 'V' }, + { "user", required_argument, NULL, 'u' }, + { "group", required_argument, NULL, 'g' } }; #endif @@ -160,6 +165,8 @@ char *template; char *linkname = NULL; char *prevlinkname = NULL; + char *groupname = NULL; + char *username = NULL; mode_t linktype = 0; int n_bytes_read; int ch; @@ -253,6 +260,14 @@ fprintf(stderr, VERSION_MSG); exit(0); + case 'u': + username = optarg; + break; + + case 'g': + groupname = optarg; + break; + case 'h': case '?': fprintf(stderr, USAGE_MSG, argv[0]); @@ -268,6 +283,9 @@ DEBUG((VERSION_MSG "\n")); + if (username || groupname) + setuser (username, groupname); + if (start_time) { time_now = parse_time(start_time, use_american_date_formats); diff -ru cronolog-1.6.2/src/cronoutils.c cronolog-1.6.2-new/src/cronoutils.c --- cronolog-1.6.2/src/cronoutils.c Thu May 3 09:43:21 2001 +++ cronolog-1.6.2-new/src/cronoutils.c Fri May 16 03:15:37 2003 @@ -711,3 +711,56 @@ } +/* Set the user/group id, based on a name. + */ + +int +setuser (char *uname, char *gname) +{ + struct passwd *pwd = NULL; + struct group *grp = NULL; + + /* retrieve info */ + if (gname) { + grp = getgrnam (gname); + if (!grp) + { + perror ("Unable to get group"); + exit(2); + } + } + if (uname) { + pwd = getpwnam (uname); + if (!pwd) + { + perror ("Unable to get user"); + exit(2); + } + } + /* set group */ + if (grp) + { + if (setgid (grp->gr_gid)) + { + perror ("Unable to set group"); + exit(2); + } + } else { + if (pwd) + { + if (setgid (pwd->pw_gid)) + { + perror ("Unable to set group"); + exit(2); + } + } + } + if (pwd) + { + if (setuid (pwd->pw_uid)) + { + perror ("Unable to set user"); + exit(2); + } + } +} diff -ru cronolog-1.6.2/src/cronoutils.h cronolog-1.6.2-new/src/cronoutils.h --- cronolog-1.6.2/src/cronoutils.h Thu May 3 09:40:12 2001 +++ cronolog-1.6.2-new/src/cronoutils.h Fri May 16 03:03:30 2003 @@ -92,6 +92,8 @@ #include #include +#include +#include #if TIME_WITH_SYS_TIME # include @@ -172,7 +174,7 @@ void print_debug_msg(char *msg, ...); time_t parse_time(char *time_str, int); char *timestamp(time_t thetime); - +int setuser (char *uname, char *gname); /* Global variables */