Add new 'group' option to rlm_linelog
authorMatthew Newton <mcn4@leicester.ac.uk>
Wed, 11 Jan 2012 12:29:02 +0000 (12:29 +0000)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 16 Jan 2012 20:41:25 +0000 (21:41 +0100)
Allows the group to be set when updating linelogs, rather
than being fixed as the group of the running daemon.

raddb/mods-available/linelog
src/modules/rlm_linelog/rlm_linelog.c

index 2be4d81..10f4697 100644 (file)
@@ -26,6 +26,14 @@ linelog {
        permissions = 0600
 
        #
+       # The Unix group of the log file.
+       #
+       # The user that freeradius runs as must be in the specified
+       # group, otherwise it will not be possible to set the group.
+       #
+       # group = freerad
+
+       #
        #  The default format string.
        format = "This is a log message for %{User-Name}"
 
index 24c809f..6773978 100644 (file)
@@ -31,6 +31,14 @@ RCSID("$Id$")
 #include <fcntl.h>
 #endif
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_GRP_H
+#include <grp.h>
+#endif
+
 #ifdef HAVE_SYSLOG_H
 #include <syslog.h>
 
@@ -46,6 +54,7 @@ typedef struct rlm_linelog_t {
        CONF_SECTION    *cs;
        char            *filename;
        int             permissions;
+       char            *group;
        char            *line;
        char            *reference;
 } rlm_linelog_t;
@@ -64,6 +73,8 @@ static const CONF_PARSER module_config[] = {
          offsetof(rlm_linelog_t,filename), NULL,  NULL},
        { "permissions",  PW_TYPE_INTEGER,
          offsetof(rlm_linelog_t,permissions), NULL,  "0600"},
+       { "group",  PW_TYPE_STRING_PTR,
+         offsetof(rlm_linelog_t,group), NULL,  NULL},
        { "format",  PW_TYPE_STRING_PTR,
          offsetof(rlm_linelog_t,line), NULL,  NULL},
        { "reference",  PW_TYPE_STRING_PTR,
@@ -199,6 +210,12 @@ static int do_linelog(void *instance, REQUEST *request)
        rlm_linelog_t *inst = (rlm_linelog_t*) instance;
        const char *value = inst->line;
 
+#ifdef HAVE_GRP_H
+       gid_t gid;
+       struct group *grp;
+       char *endptr;
+#endif
+
        if (inst->reference) {
                CONF_ITEM *ci;
                CONF_PAIR *cp;
@@ -263,6 +280,25 @@ static int do_linelog(void *instance, REQUEST *request)
                }
        }
 
+#ifdef HAVE_GRP_H
+       if (inst->group != NULL) {
+               gid = strtol(inst->group, &endptr, 10);
+               if (*endptr != '\0') {
+                       grp = getgrnam(inst->group);
+                       if (grp == NULL) {
+                               RDEBUG2("Unable to find system group \"%s\"", inst->group);
+                               goto skip_group;
+                       }
+                       gid = grp->gr_gid;
+               }
+
+               if (chown(buffer, -1, gid) == -1) {
+                       RDEBUG2("Unable to change system group of \"%s\"", buffer);
+               }
+       }
+#endif
+
+ skip_group:
        /*
         *      FIXME: Check length.
         */