Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little Bobby Tables #1151

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ libshadow_la_SOURCES = \
console.c \
copydir.c \
csrand.c \
ctype/ispfchar.c \
ctype/ispfchar.h \
defines.h \
encrypt.c \
env.c \
Expand Down
60 changes: 22 additions & 38 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* true - OK
* false - bad name
* errors:
* EINVAL Invalid name characters or sequences
* EINVAL Invalid name
* EILSEQ Invalid name character sequence
* EOVERFLOW Name longer than maximum size
*/

Expand All @@ -33,10 +34,11 @@

#include "defines.h"
#include "chkname.h"
#include "ctype/ispfchar.h"
#include "string/ctype/strchrisascii/strchriscntrl.h"
#include "string/ctype/strisascii/strisdigit.h"
#include "string/strcmp/streq.h"


int allow_bad_names = false;
#include "string/strcmp/strprefix.h"


size_t
Expand All @@ -56,8 +58,14 @@ login_name_max_size(void)
static bool
is_valid_name(const char *name)
{
if (allow_bad_names) {
return true;
if (streq(name, "")
|| streq(name, ".")
|| streq(name, "..")
|| strprefix(name, "-")
|| strisdigit(name))
{
errno = EINVAL;
return false;
}

/*
Expand All @@ -66,45 +74,21 @@ is_valid_name(const char *name)
*
* as a non-POSIX, extension, allow "$" as the last char for
* sake of Samba 3.x "add machine script"
*
* Also do not allow fully numeric names or just "." or "..".
*/
int numeric;

if ('\0' == *name ||
('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
'\0' == name[1])) ||
!((*name >= 'a' && *name <= 'z') ||
(*name >= 'A' && *name <= 'Z') ||
(*name >= '0' && *name <= '9') ||
*name == '_' ||
*name == '.'))
{
errno = EINVAL;

if (!ispfchar(*name)) {
errno = EILSEQ;
return false;
}

numeric = isdigit(*name);

while (!streq(++name, "")) {
if (!((*name >= 'a' && *name <= 'z') ||
(*name >= 'A' && *name <= 'Z') ||
(*name >= '0' && *name <= '9') ||
*name == '_' ||
*name == '.' ||
*name == '-' ||
(*name == '$' && name[1] == '\0')
))
{
errno = EINVAL;
if (streq(name, "$")) // Samba
return true;

if (!ispfchar(*name)) {
errno = EILSEQ;
return false;
}
numeric &= isdigit(*name);
}

if (numeric) {
errno = EINVAL;
return false;
}

return true;
Expand Down
12 changes: 12 additions & 0 deletions lib/ctype/ispfchar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "ctype/ispfchar.h"

#include <stdbool.h>


extern inline bool ispfchar(unsigned char c);
26 changes: 26 additions & 0 deletions lib/ctype/ispfchar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_CTYPE_ISPFCHAR_H_
#define SHADOW_INCLUDE_LIB_CTYPE_ISPFCHAR_H_


#include <config.h>

#include <ctype.h>
#include <stdbool.h>


inline bool ispfchar(unsigned char c);


// Return true if 'c' is a character from the Portable Filename Character Set.
inline bool
ispfchar(unsigned char c)
{
return isalnum(c) || c == '.' || c == '_' || c == '-';
}


#endif // include guard
12 changes: 0 additions & 12 deletions man/newusers.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,6 @@
<para>
The options which apply to the <command>newusers</command> command are:
</para>
<variablelist remap='IP'>
<varlistentry>
<term>
<option>--badname</option>&nbsp;
</term>
<listitem>
<para>
Allow names that do not conform to standards.
</para>
</listitem>
</varlistentry>
</variablelist>
<variablelist remap='IP' condition="no_pam">
<varlistentry>
<term><option>-c</option>, <option>--crypt-method</option></term>
Expand Down
10 changes: 0 additions & 10 deletions man/pwck.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,6 @@
The options which apply to the <command>pwck</command> command are:
</para>
<variablelist remap='IP'>
<varlistentry>
<term>
<option>--badname</option>&nbsp;
</term>
<listitem>
<para>
Allow names that do not conform to standards.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-h</option>, <option>--help</option></term>
<listitem>
Expand Down
10 changes: 0 additions & 10 deletions man/useradd.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,6 @@
<para>The options which apply to the <command>useradd</command> command are:
</para>
<variablelist remap='IP'>
<varlistentry>
<term>
<option>--badname</option>&nbsp;
</term>
<listitem>
<para>
Allow names that do not conform to standards.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-b</option>, <option>--base-dir</option>&nbsp;<replaceable>BASE_DIR</replaceable>
Expand Down
10 changes: 0 additions & 10 deletions man/usermod.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-b</option>, <option>--badname</option>
</term>
<listitem>
<para>
Allow names that do not conform to standards.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>-c</option>, <option>--comment</option>&nbsp;<replaceable>COMMENT</replaceable>
Expand Down
4 changes: 3 additions & 1 deletion src/chfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <getopt.h>

Expand Down Expand Up @@ -646,7 +648,7 @@ int main (int argc, char **argv)
*/
if (optind < argc) {
if (!is_valid_user_name (argv[optind])) {
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
fail_exit (E_NOPERM);
}
user = argv[optind];
Expand Down
4 changes: 3 additions & 1 deletion src/chsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#ident "$Id$"

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "chkname.h"
Expand Down Expand Up @@ -503,7 +505,7 @@ int main (int argc, char **argv)
*/
if (optind < argc) {
if (!is_valid_user_name (argv[optind])) {
fprintf (stderr, _("%s: Provided user name is not a valid name\n"), Prog);
fprintf(stderr, _("%s: user: %s\n"), Prog, strerror(errno));
fail_exit (1);
}
user = argv[optind];
Expand Down
5 changes: 2 additions & 3 deletions src/groupadd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ident "$Id$"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
Expand Down Expand Up @@ -247,9 +248,7 @@ static void
check_new_name(void)
{
if (!is_valid_group_name(group_name)) {
fprintf(stderr, _("%s: '%s' is not a valid group name\n"),
Prog, group_name);

fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
exit(E_BAD_ARG);
}

Expand Down
5 changes: 2 additions & 3 deletions src/groupmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ident "$Id$"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
Expand Down Expand Up @@ -381,9 +382,7 @@ check_new_name(void)
}

if (!is_valid_group_name(group_newname)) {
fprintf(stderr,
_("%s: invalid group name '%s'\n"),
Prog, group_newname);
fprintf(stderr, _("%s: group: %s\n"), Prog, strerror(errno));
exit(E_BAD_ARG);
}

Expand Down
6 changes: 4 additions & 2 deletions src/grpck.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

#include <config.h>

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>

#include "chkname.h"
#include "commonio.h"
Expand Down Expand Up @@ -562,7 +564,7 @@ static void check_grp_file (int *errors, bool *changed)
*/
if (!is_valid_group_name (grp->gr_name)) {
*errors += 1;
printf (_("invalid group name '%s'\n"), grp->gr_name);
printf(_("group: %s\n"), strerror(errno));
}

/*
Expand Down
11 changes: 5 additions & 6 deletions src/newgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "agetpass.h"
Expand Down Expand Up @@ -486,9 +487,8 @@ int main (int argc, char **argv)
*/
if ((argc > 0) && (argv[0][0] != '-')) {
if (!is_valid_group_name (argv[0])) {
fprintf (
stderr, _("%s: provided group is not a valid group name\n"),
Prog);
fprintf(stderr, _("%s: group: %s\n"),
Prog, strerror(errno));
goto failure;
}
group = argv[0];
Expand Down Expand Up @@ -523,9 +523,8 @@ int main (int argc, char **argv)
goto failure;
} else if (argv[0] != NULL) {
if (!is_valid_group_name (argv[0])) {
fprintf (
stderr, _("%s: provided group is not a valid group name\n"),
Prog);
fprintf(stderr, _("%s: group: %s\n"),
Prog, strerror(errno));
goto failure;
}
group = argv[0];
Expand Down
Loading
Loading