Skip to content

Commit

Permalink
lib/chkname.[ch]: login_name_max_size(): Add function
Browse files Browse the repository at this point in the history
It encapsulates some logic that we may want to reuse elsewhere.

Link: <shadow-maint#989>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed May 10, 2024
1 parent 53067b0 commit 5c0af11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/chkname.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 5c0af11

Please sign in to comment.