我写一些可以用来创建Linux用户帐户的Web UI页面。此Web UI将用于CentOS 6(源自RHEL 6)。我发现关于什么构成有效的Linux用户名的不一致和不完整的信息。我去源,检查一个Linux shadow-utils源包,但我不能确保我正在看的版本实际上是作为CentOS 6的一部分相同。
下面是我目前使用的代码片段,其中包括从shadow-utils包4.1.4.3中复制/粘贴注释,加上一些我自己的注释,以及一个Java正则表达式搜索, utils源。
在chkname.c中引用的“is_valid_name()”检查显然不是从Linux上的useradd命令使用的,因为注释(和C代码源)不允许以数字开头的名称。但是,useradd允许创建一个类似“1234”的帐户。
我希望帮助调整从我现在到什么是更正确的,以及关于如何实现useradd.c与一些稍微不同的is_valid_name函数的信息。
谢谢!
艾伦
/** * 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); }
基本的gnu / linux用户名是一个32个字符的字符串(useradd(8))。这是来自BSD 4.3标准的传统格式。 passwd(5)添加了一些额外的限制,比如,不要使用大写字母,不要使用点,不要用破折号结束,它不能包含冒号。
为了安全起见,遵循C标识符的相同规则:
([a-z_][a-z0-9_]{0,30})
这是一半的问题。现代GNU / Linux发行版使用PAM进行用户身份验证。有了它,你可以选择你想要的任何规则,也任何数据源。
因为你正在编写程序,最好定义自己的格式,然后使用像pam_ldap,pam_mysql等来访问它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。