# read variables
sub variables {
$verbose = 1; # verbose = [0-2]
- $force = 0; # relax username validity check if true
+ $usernameregexp = "^[a-z0-9_][a-z0-9_-]*\$"; # configurable
+ $defaultusernameregexp = $usernameregexp; # remains constant
$defaultusepassword = "yes"; # use password authentication for new users
$defaultenableaccount = "yes"; # enable the account by default
$defaultemptypassword = "no"; # don't create an empty password
local($name);
while(1) {
- $name = &confirm_list("Enter username", 1, "a-z0-9_-", "");
- if (length($name) > 16) {
- warn "Username is longer than 16 chars\a\n";
- next;
- }
+ $name = &confirm_list("Enter username", 1, $usernameregexp, "");
last if (&new_users_name_valid($name));
}
return $name;
sub new_users_name_valid {
local($name) = @_;
- if ($force) {
- if ($name eq "a-z0-9_-") {
- warn "Please enter a username.\a\n";
- return 0;
- }
- if ($name =~ /[:\n]/) {
- warn "Illegal username, which would break your passwd file.\a\n";
- return 0;
- }
- if ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/) {
- warn "Caution: Username contains illegal characters.\n" .
- "Adding this user may cause utilities " .
- "or applications to malfunction,\n" .
- "or even impose a security risk on your system.\a\n";
+ if ($name eq $usernameregexp) { # user/admin just pressed <Return>
+ warn "Please enter a username\a\n";
+ return 0;
+ } elsif (length($name) > 16) {
+ warn "Username is longer than 16 characters.\a\n";
+ return 0;
+ } elsif ($name =~ /[:\n]/) {
+ warn "Username cannot contain colon or newline characters.\a\n";
+ return 0;
+ } elsif ($name !~ /$usernameregexp/) {
+ if ($usernameregexp eq $defaultusernameregexp) {
+ warn "Illegal username.\n" .
+ "Please use only lowercase Roman, decimal, underscore, " .
+ "or hyphen characters.\n" .
+ "Additionally, a username should not start with a hyphen.\a\n";
+ } else {
+ warn "Username doesn't match the regexp /$usernameregexp/\a\n";
}
- } elsif ($name !~ /^[a-z0-9_][a-z0-9_\-]*$/ || $name eq "a-z0-9_-") {
- warn "Illegal username.\n" .
- "Please use only lowercase Roman, decimal, underscore, " .
- "or hyphen characters.\n" .
- "Additionally, a username should not start with a hyphen.\a\n";
return 0;
- }
- if ($username{$name}) {
+ } elsif (defined($username{$name})) {
warn "Username ``$name'' already exists!\a\n"; return 0;
}
return 1;
[-class login_class]
[-config_create]
[-dotdir dotdir]
- [-f|-force]
[-group login_group]
[-h|-help]
[-home home]
if (/^--?(v|verbose)$/) { $verbose = 1 }
elsif (/^--?(s|silent|q|quiet)$/) { $verbose = 0 }
elsif (/^--?(debug)$/) { $verbose = 2 }
- elsif (/^--?(f|force)$/) { $force = 1 }
elsif (/^--?(h|help|\?)$/) { &usage }
elsif (/^--?(home)$/) { $home = $argv[0]; shift @argv }
elsif (/^--?(shell)$/) { $defaultshell = $argv[0]; shift @argv }
return 0;
}
+# allow configuring usernameregexp
+sub usernameregexp_default {
+ local($r) = $usernameregexp;
+
+ while ($verbose) {
+ $r = &confirm_list("Usernames must match regular expression:", 1,
+ $r, "");
+ eval "'foo' =~ /$r/";
+ last unless $@;
+ warn "Invalid regular expression\a\n";
+ }
+ $changes++ if $r ne $usernameregexp;
+ return $r;
+}
+
# test if $dotdir exist
# return "no" if $dotdir not exist or dotfiles should not copied
sub dotdir_default {
# verbose = [0-2]
verbose = $verbose
+# regular expression usernames are checked against (see perlre(1))
+# usernameregexp = 'regexp'
+usernameregexp = '$usernameregexp'
+
# use password-based authentication for new users
# defaultusepassword = "yes" | "no"
defaultusepassword = "$defaultusepassword"
# interactive
# some questions
+$usernameregexp = &usernameregexp_default; # regexp to check usernames against
&shells_add; # maybe add some new shells
$defaultshell = &shell_default; # enter default shell
$home = &home_partition($home); # find HOME partition