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 <[email protected]>
  • Loading branch information
alejandro-colomar committed May 10, 2024
1 parent 98aefe8 commit 9cecafe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
32 changes: 20 additions & 12 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,30 @@
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <unistd.h>

#include "defines.h"
#include "chkname.h"


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 @@ -76,18 +95,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
7 changes: 6 additions & 1 deletion lib/chkname.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
* false - bad name
*/

#include "defines.h"

#include <config.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 9cecafe

Please sign in to comment.