【问题标题】:What are the real rules for linux usernames on CentOS 6 and RHEL 6? [closed]CentOS 6 和 RHEL 6 上 linux 用户名的真正规则是什么? [关闭]
【发布时间】:2011-10-20 10:48:30
【问题描述】:

我正在编写一些可用于创建 Linux 用户帐户的 Web UI 页面。此 Web UI 将用于 CentOS 6(衍生自 RHEL 6)。我发现关于什么是有效 Linux 用户名的信息不一致且不完整。我查看了源代码,检查了一个 Linux shadow-utils 源代码包,但我无法确保我所查看的版本实际上与 CentOS 6 的版本相同。

下面是我目前使用的代码片段,其中包括从 shadow-utils 包版本 4.1.4.3 复制/粘贴 cmets,加上我自己的一些注释,以及一个 Java 正则表达式搜索,以遵循我从查找中的理解在 shadow-utils 源。

chkname.c 中引用的“is_valid_name()”检查显然不是 Linux 上的 useradd 命令使用的,因为 cmets(和 C 代码源)不允许名称以数字开头。但是,useradd 确实允许创建像“1234”这样的帐户。

我希望能帮助我从现在的内容调整为更正确的内容,以及有关如何使用一些略有不同的 is_valid_name 函数实现 useradd.c 的信息。

谢谢! 艾伦

/**
 * Define constants for use in isNameLinuxCompatible(...) method.
 *
 * The source for the Linux compatible user name rule is is_valid_name(...) a function in the "shadow" package
 * for Linux.  The source file for that function has a comment as follows:
 *      User/group names must match [a-z_][a-z0-9_-]*[$]
 * That expression is a little loose/sloppy since
 * (1) the trailing $ sign is optional, and
 * (2) uppercase A-Z is also ok (and case is significant, 'A' != 'a').
 *
 * We deal with (1) by using the [$]? form where the ? means zero or more characters (aka "greedy").
 * We deal with (2) by using the CASE_INSENSITIVE option.
 *
 * Another way to express this is:
 *  1st character:                      a-z_         required at least one char
 *  chars other than first and last:    a-z0-9_-     optional
 *  last character:                     $            optional
 * Max length is 31.  Min length is 1.
 *
 * NOTE: The initial ^ and final $ below are important since we need the entire string to satisfy the rule,
 * from beginning to end.
 *
 * See http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for reference info on pattern matching.
 */

private static final String  LINUX_USERNAME_REGEX     = "^[a-z_][a-z0-9_-]*[$]?$";
private static final Pattern LINUX_USERNAME_PATTERN   = Pattern.compile(LINUX_USERNAME_REGEX, Pattern.CASE_INSENSITIVE);
private static final int     LINUX_USERNAME_MINLENGTH = 1;
private static final int     LINUX_USERNAME_MAXLENGTH = 31;

/**
 * See if username is compatible with standard Linux rules for usernames, in terms of length and
 * in terms of content.
 *
 * @param username the name to be checked for validity
 * @return true if Linux compatible, else false
 */
public boolean isNameLinuxCompatible (final String username) {
    boolean nameOK = false;
    if (username != null) {
        int len = username.length();
        if ((len >= LINUX_USERNAME_MINLENGTH) && (len <= LINUX_USERNAME_MAXLENGTH)) {
            Matcher m = LINUX_USERNAME_PATTERN.matcher(username);
            nameOK = m.find();
        }
    }
    return (nameOK);
}

【问题讨论】:

  • 请记住,系统管理员也可以使用 pam 定义自己的规则
  • 我熟悉 pam,但不知道如何使用它来定义用户名规则,正如 Chris 所提到的。我想了解更多关于这方面的信息。特别是,我希望能够在 CentOS 6 上检查我的系统配置文件,以找出它允许的内容,然后在它允许的范围内进行测试。
  • 你需要编写一个 PAM 插件来确定“需求”,然后将其放入配置中。我不知道它有多普遍,我只是说除了这一个功能之外还有其他因素。

标签: linux centos shadow centos6


【解决方案1】:

基本的 gnu/linux 用户名是 32 个字符的字符串 (useradd(8))。这是 BSD 4.3 标准的遗留格式。 passwd(5) 增加了一些额外的限制,例如,不要使用大写字母,不要使用点,不要以破折号结尾,不能包含冒号。

为了安全起见,请遵循与 C 标识符相同的规则:

([a-z_][a-z0-9_]{0,30})

这是问题的一半。现代 GNU/Linux 发行版使用 PAM 进行用户身份验证。有了它,您可以选择任何您想要的规则以及任何数据源。

由于您正在编写程序,因此最好定义自己的格式,然后使用pam_ldappam_mysql 等来访问它。

【讨论】:

  • 感谢您提供的信息。我目前有一个安全的子集,但想扩展我允许的内容以更完全地匹配我的安装/配置允许的内容。我允许混合大小写,并且对此没有问题;例如,用户名“ACarwile”与“acarwile”不同。仅供参考,我的程序正在调用实用程序 useradd、usermod 和 userdel 来完成真正的工作。
  • user{add,mod,del} 是 PAM 感知工具,它们将允许底层 pam 模块允许的一切。
  • 感谢 Chris 和 Pablo 的反馈。最后,我们可以使用潜在用户名空间的一个子集。重要的是用户可以从一个相当大的空间中创建一个想要的名称,并且该名称将与 linux 兼容并且没有太多限制。前面提到的规则就足够了,我们不需要枚举所有可能的用户名。谢谢!艾伦
  • 一个陷阱:([a-z_][a-z0-9_]{0,30}) 读作“小写或下划线,后跟零到三十个字母数字字符”,这意味着用户名的总长度可以是 31 个字符
  • @berkes 加上 C 字符串的终止零等于 32,不是吗?
猜你喜欢
  • 2013-10-03
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 2021-08-11
  • 2011-11-12
  • 2015-06-29
  • 1970-01-01
相关资源
最近更新 更多