2008-04-27 06:10:09 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2001 - 2005, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2005 - 2008, Nicolas François
|
2008-04-27 06:10:09 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2008-04-27 06:10:09 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-05-26 02:28:16 +05:30
|
|
|
* is_valid_user_name(), is_valid_group_name() - check the new user/group
|
|
|
|
* name for validity;
|
|
|
|
* return values:
|
|
|
|
* true - OK
|
|
|
|
* false - bad name
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <ctype.h>
|
|
|
|
#include "defines.h"
|
|
|
|
#include "chkname.h"
|
2008-05-26 02:28:16 +05:30
|
|
|
|
2019-08-24 02:12:37 +05:30
|
|
|
int allow_bad_names = false;
|
|
|
|
|
2008-05-26 02:28:16 +05:30
|
|
|
static bool is_valid_name (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2019-08-24 02:12:37 +05:30
|
|
|
if (allow_bad_names) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2022-08-16 17:16:22 +05:30
|
|
|
* User/group names must match gnu e-regex:
|
|
|
|
* [a-zA-Z0-9_.][a-zA-Z0-9_.-]{0,30}[a-zA-Z0-9_.$-]?
|
|
|
|
*
|
|
|
|
* 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;
|
2019-08-24 02:12:37 +05:30
|
|
|
|
2022-08-16 17:16:22 +05:30
|
|
|
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 == '.')) {
|
2008-05-26 02:28:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2022-08-16 17:16:22 +05:30
|
|
|
numeric = isdigit(*name);
|
|
|
|
|
2008-05-26 02:28:16 +05:30
|
|
|
while ('\0' != *++name) {
|
2022-08-16 17:16:22 +05:30
|
|
|
if (!((*name >= 'a' && *name <= 'z') ||
|
|
|
|
(*name >= 'A' && *name <= 'Z') ||
|
|
|
|
(*name >= '0' && *name <= '9') ||
|
|
|
|
*name == '_' ||
|
|
|
|
*name == '.' ||
|
|
|
|
*name == '-' ||
|
|
|
|
(*name == '$' && name[1] == '\0')
|
2008-05-26 02:28:16 +05:30
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-16 17:16:22 +05:30
|
|
|
numeric &= isdigit(*name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2022-08-16 17:16:22 +05:30
|
|
|
return !numeric;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2008-05-26 02:28:16 +05:30
|
|
|
bool is_valid_user_name (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
/*
|
|
|
|
* User names are limited by whatever utmp can
|
2008-12-23 03:38:13 +05:30
|
|
|
* handle.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2009-04-23 02:12:48 +05:30
|
|
|
if (strlen (name) > USER_NAME_MAX_LENGTH) {
|
2008-05-26 02:28:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-26 02:28:16 +05:30
|
|
|
return is_valid_name (name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2008-05-26 02:28:16 +05:30
|
|
|
bool is_valid_group_name (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
/*
|
2008-12-23 03:38:13 +05:30
|
|
|
* Arbitrary limit for group names.
|
|
|
|
* HP-UX 10 limits to 16 characters
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2008-12-24 04:12:22 +05:30
|
|
|
if ( (GROUP_NAME_MAX_LENGTH > 0)
|
|
|
|
&& (strlen (name) > GROUP_NAME_MAX_LENGTH)) {
|
2008-05-26 02:28:16 +05:30
|
|
|
return false;
|
2008-12-24 04:12:22 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-26 02:28:16 +05:30
|
|
|
return is_valid_name (name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-04-27 06:10:09 +05:30
|
|
|
|