]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Simpler user/group parsing
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 30 Sep 2021 19:36:44 +0000 (14:36 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 30 Sep 2021 19:39:58 +0000 (14:39 -0500)
src/lib/server/cf_parse.c
src/lib/server/cf_parse.h
src/lib/server/main_config.c

index 9d604d6a9d571f9a838dfabe57e20556b6b2a71d..69dd578626d2f962e09c30cf4622b1b42cc8b8e4 100644 (file)
@@ -37,6 +37,7 @@ RCSID("$Id$")
 #include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/util/inet.h>
 #include <freeradius-devel/util/misc.h>
+#include <freeradius-devel/util/perm.h>
 #include <freeradius-devel/util/types.h>
 
 static CONF_PARSER conf_term = CONF_PARSER_TERMINATOR;
@@ -1719,3 +1720,33 @@ int cf_table_parse_uint32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent
        return 0;
 }
 
+/** Generic function for resolving UID strings to uid_t values
+ *
+ * Type should be FR_TYPE_VOID, struct field should be a uid_t.
+ */
+int cf_parse_uid(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
+                CONF_ITEM *ci, UNUSED CONF_PARSER const *rule)
+{
+       if (fr_perm_uid_from_str(ctx, (uid_t *)out, cf_pair_value(cf_item_to_pair(ci))) < 0) {
+               cf_log_perr(ci, "Failed resolving UID");
+               return -1;
+       }
+
+       return 0;
+}
+
+/** Generic function for resolving GID strings to uid_t values
+ *
+ * Type should be FR_TYPE_VOID, struct field should be a gid_t.
+ */
+int cf_parse_gid(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
+                CONF_ITEM *ci, UNUSED CONF_PARSER const *rule)
+{
+       if (fr_perm_gid_from_str(ctx, (gid_t *)out, cf_pair_value(cf_item_to_pair(ci))) < 0) {
+               cf_log_perr(ci, "Failed resolving GID");
+               return -1;
+       }
+
+       return 0;
+}
+
index 9809a282169674f5c422c214d6eb2474899bbd4c..ba2850e2f583309b6dffea7d2c46b7bbd0d7f0bd 100644 (file)
@@ -504,6 +504,11 @@ int                cf_table_parse_uint32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *paren
 int            cf_table_parse_int32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent,
                                     CONF_ITEM *ci, CONF_PARSER const *rule);
 
+int            cf_parse_uid(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
+                            CONF_ITEM *ci, CONF_PARSER const *rule);
+
+int            cf_parse_gid(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
+                            CONF_ITEM *ci, CONF_PARSER const *rule);
 
 #ifdef __cplusplus
 }
index 49dcc3ae9953ed51f573804958fa59f74eac57a0..f0f8d36f0a6ef81188cb0a456f27162d63ac90ee 100644 (file)
@@ -92,11 +92,6 @@ static int max_request_time_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF
 
 static int name_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
 
-#ifdef HAVE_SETUID
-static int uid_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
-static int gid_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
-#endif
-
 /*
  *     Log destinations
  */
@@ -230,8 +225,8 @@ static const CONF_PARSER server_config[] = {
  **********************************************************************/
 static const CONF_PARSER security_config[] = {
 #ifdef HAVE_SETUID
-       { FR_CONF_OFFSET_IS_SET("user", FR_TYPE_VOID, main_config_t, uid), .func = uid_parse },
-       { FR_CONF_OFFSET_IS_SET("group", FR_TYPE_VOID, main_config_t, gid), .func = gid_parse },
+       { FR_CONF_OFFSET_IS_SET("user", FR_TYPE_VOID, main_config_t, uid), .func = cf_parse_uid },
+       { FR_CONF_OFFSET_IS_SET("group", FR_TYPE_VOID, main_config_t, gid), .func = cf_parse_gid },
 #endif
        { FR_CONF_OFFSET("chroot", FR_TYPE_STRING, main_config_t, chroot_dir) },
        { FR_CONF_OFFSET("allow_core_dumps", FR_TYPE_BOOL, main_config_t, allow_core_dumps), .dflt = "no" },
@@ -373,48 +368,6 @@ static int name_parse(TALLOC_CTX *ctx, void *out, void *parent,
        return cf_pair_parse_value(ctx, out, parent, ci, rule);         /* Set new value */
 }
 
-#ifdef HAVE_SETUID
-static int uid_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
-                    CONF_ITEM *ci, UNUSED CONF_PARSER const *rule)
-{
-       struct passwd   *user;
-       char const      *uid_name;
-
-       uid_name = cf_pair_value(cf_item_to_pair(ci));
-
-       if (fr_perm_getpwnam(ctx, &user, uid_name) < 0) {
-               cf_log_perr(ci, "Cannot get passwd entry for user \"%s\"", uid_name);
-               return 0;
-       }
-
-       memcpy(out, &user->pw_uid, sizeof(user->pw_uid));
-
-       talloc_free(user);
-
-       return 0;
-}
-
-static int gid_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
-                    CONF_ITEM *ci, UNUSED CONF_PARSER const *rule)
-{
-       struct group    *group;
-       char const      *gid_name;
-
-       gid_name = cf_pair_value(cf_item_to_pair(ci));
-
-       if (fr_perm_getgrnam(ctx, &group, gid_name) < 0) {
-               cf_log_perr(ci, "Cannot resolve group name \"%s\"", gid_name);
-               return 0;
-       }
-
-       memcpy(out, &group->gr_gid, sizeof(group->gr_gid));
-
-       talloc_free(group);
-
-       return 0;
-}
-#endif
-
 static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent,
                              CONF_ITEM *ci, CONF_PARSER const *rule)
 {