From: Tobias Stoeckmann Date: Fri, 13 Mar 2026 16:23:45 +0000 (+0100) Subject: lib/sysconf: Move sysconf handling into own file X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=d6638d5d15eed5001a5238206c8e4e6ddac05fec;p=thirdparty%2Fshadow.git lib/sysconf: Move sysconf handling into own file Move sysconf() calls and error handling into its own file. While at it, adjust the data type for ngroup max value. Requested-by: Alejandro Colomar Reviewed-by: Alejandro Colomar Signed-off-by: Tobias Stoeckmann --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 72a57ad94..072754819 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -262,6 +262,8 @@ libshadow_la_SOURCES = \ subordinateio.h \ subordinateio.c \ sulog.c \ + sysconf.c \ + sysconf.h \ time/day_to_str.c \ time/day_to_str.h \ ttytype.c \ diff --git a/lib/chkname.c b/lib/chkname.c index 0abee4d29..0b6d9409c 100644 --- a/lib/chkname.c +++ b/lib/chkname.c @@ -37,29 +37,12 @@ #include "string/ctype/strisascii/strisdigit.h" #include "string/strcmp/streq.h" #include "string/strcmp/strcaseeq.h" - - -#ifndef LOGIN_NAME_MAX -# define LOGIN_NAME_MAX 256 -#endif +#include "sysconf.h" int allow_bad_names = false; -size_t -login_name_max_size(void) -{ - long conf; - - conf = sysconf(_SC_LOGIN_NAME_MAX); - if (conf == -1) - return LOGIN_NAME_MAX; - - return conf; -} - - static bool is_valid_name(const char *name) { diff --git a/lib/chkname.h b/lib/chkname.h index 40d004cc0..1f8e4e082 100644 --- a/lib/chkname.h +++ b/lib/chkname.h @@ -24,10 +24,8 @@ #include "config.h" #include -#include -extern size_t login_name_max_size(void); extern bool is_valid_user_name (const char *name); extern bool is_valid_group_name (const char *name); diff --git a/lib/sysconf.c b/lib/sysconf.c new file mode 100644 index 000000000..a9517dd2a --- /dev/null +++ b/lib/sysconf.c @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-FileCopyrightText: 2026, Tobias Stoeckmann +// SPDX-License-Identifier: BSD-3-Clause + + +#include "config.h" + +#include +#include + + +#ifndef LOGIN_NAME_MAX +# define LOGIN_NAME_MAX 256 +#endif + +#ifndef NGROUPS_MAX +# define NGROUPS_MAX 65536 +#endif + + +size_t +login_name_max_size(void) +{ + long conf; + + conf = sysconf(_SC_LOGIN_NAME_MAX); + if (conf == -1) + return LOGIN_NAME_MAX; + + return conf; +} + +size_t +ngroups_max_size(void) +{ + long conf; + + conf = sysconf(_SC_NGROUPS_MAX); + if (conf == -1) + conf = NGROUPS_MAX; + + return conf; +} diff --git a/lib/sysconf.h b/lib/sysconf.h new file mode 100644 index 000000000..467ef32a6 --- /dev/null +++ b/lib/sysconf.h @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-FileCopyrightText: 2026, Tobias Stoeckmann +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef _SYSCONF_H_ +#define _SYSCONF_H_ + + +#include "config.h" + +#include + + +extern size_t login_name_max_size(void); +extern size_t ngroups_max_size(void); + +#endif // include guard diff --git a/src/login.c b/src/login.c index b5b82f3a3..87bfff69c 100644 --- a/src/login.c +++ b/src/login.c @@ -48,6 +48,7 @@ #include "string/strdup/strdup.h" #include "string/strerrno.h" #include "string/strftime.h" +#include "sysconf.h" #ifdef USE_PAM diff --git a/src/useradd.c b/src/useradd.c index 5534e1d86..397d45f46 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -72,6 +72,7 @@ #include "string/strdup/strdup.h" #include "string/strerrno.h" #include "string/strtok/stpsep.h" +#include "sysconf.h" #ifndef SKEL_DIR @@ -153,7 +154,7 @@ static bool pw_locked = false; static bool gr_locked = false; static bool spw_locked = false; static char **user_groups; /* NULL-terminated list */ -static long sys_ngroups; +static size_t sys_ngroups; static bool do_grp_update = false; /* group files need to be updated */ extern int allow_bad_names; @@ -221,10 +222,6 @@ static bool home_added = false; #define DBTRFS_SUBVOLUME_HOME "BTRFS_SUBVOLUME_HOME" #define DLOG_INIT "LOG_INIT" -#ifndef NGROUPS_MAX -#define NGROUPS_MAX 65536 -#endif - /* local function prototypes */ NORETURN static void fail_exit (int, bool); static void get_defaults(const struct option_flags *); @@ -764,7 +761,7 @@ static int get_groups(char *list, const struct option_flags *flags) { struct group *grp; bool errors = false; - int ngroups = 0; + size_t ngroups = 0; bool process_selinux; process_selinux = !flags->chroot && !flags->prefix; @@ -825,7 +822,7 @@ static int get_groups(char *list, const struct option_flags *flags) if (ngroups == sys_ngroups) { fprintf (stderr, - _("%s: too many groups specified (max %d).\n"), + _("%s: too many groups specified (max %zu).\n"), Prog, ngroups); gr_free(grp); break; @@ -2532,9 +2529,7 @@ int main (int argc, char **argv) audit_help_open (); #endif - sys_ngroups = sysconf (_SC_NGROUPS_MAX); - if (sys_ngroups == -1) - sys_ngroups = NGROUPS_MAX; + sys_ngroups = ngroups_max_size(); user_groups = xmalloc_T(1 + sys_ngroups, char *); /* * Initialize the list to be empty diff --git a/src/usermod.c b/src/usermod.c index af66d38c0..6dfe461a2 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -66,6 +66,7 @@ #include "string/strdup/strdup.h" #include "string/strerrno.h" #include "string/strspn/stprspn.h" +#include "sysconf.h" #include "time/day_to_str.h" #include "typetraits.h" @@ -100,10 +101,6 @@ /* invalid shadow time indicating missing entry */ #define MISSING_TIME -2 -#ifndef NGROUPS_MAX -#define NGROUPS_MAX 65536 -#endif - /* * Structures */ @@ -138,7 +135,7 @@ static long user_expire = MISSING_TIME; static long user_newexpire = MISSING_TIME; static long user_inactive = MISSING_TIME; static long user_newinactive = MISSING_TIME; -static long sys_ngroups; +static size_t sys_ngroups; static char **user_groups; /* NULL-terminated list */ static const char* prefix = ""; @@ -238,7 +235,7 @@ static int get_groups (char *list) { struct group *grp; bool errors = false; - int ngroups = 0; + size_t ngroups = 0; /* * Initialize the list to be empty @@ -288,7 +285,7 @@ static int get_groups (char *list) if (ngroups == sys_ngroups) { fprintf (stderr, - _("%s: too many groups specified (max %d).\n"), + _("%s: too many groups specified (max %zu).\n"), Prog, ngroups); gr_free (grp); break; @@ -2206,9 +2203,7 @@ int main (int argc, char **argv) audit_help_open (); #endif - sys_ngroups = sysconf (_SC_NGROUPS_MAX); - if (sys_ngroups == -1) - sys_ngroups = NGROUPS_MAX; + sys_ngroups = ngroups_max_size(); user_groups = xmalloc_T(sys_ngroups + 1, char *); user_groups[0] = NULL; diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 9c548240f..74f672110 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -65,6 +65,7 @@ test_chkhash_LDADD = \ test_chkname_SOURCES = \ ../../lib/chkname.c \ + ../../lib/sysconf.c \ test_chkname.c \ $(NULL) test_chkname_CFLAGS = \