lib/chkname.[ch]: login_name_max_size(): Add function

It encapsulates some logic that we may want to reuse elsewhere.

Link: <https://github.com/shadow-maint/shadow/pull/989>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar 2024-05-11 01:34:05 +02:00 committed by Iker Pedrosa
parent 27e467a61a
commit 99df9d746e
2 changed files with 17 additions and 12 deletions

View File

@ -33,6 +33,20 @@
int allow_bad_names = false;
size_t
login_name_max_size(void)
{
long conf;
errno = 0;
conf = sysconf(_SC_LOGIN_NAME_MAX);
if (conf == -1 && errno != 0)
return LOGIN_NAME_MAX;
return conf;
}
static bool is_valid_name (const char *name)
{
if (allow_bad_names) {
@ -84,18 +98,7 @@ static bool is_valid_name (const char *name)
bool
is_valid_user_name(const char *name)
{
long conf;
size_t maxsize;
errno = 0;
conf = sysconf(_SC_LOGIN_NAME_MAX);
if (conf == -1 && errno != 0)
maxsize = LOGIN_NAME_MAX;
else
maxsize = conf;
if (strlen(name) >= maxsize)
if (strlen(name) >= login_name_max_size())
return false;
return is_valid_name(name);

View File

@ -24,8 +24,10 @@
#include <config.h>
#include <stdbool.h>
#include <stddef.h>
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);