#!/usr/bin/perl
#
-# (c) Copyright 1995 Wolfram Schneider. All rights reserved.
+# Copyright (c) 1995 Wolfram Schneider. All rights reserved.
+# Alle Rechte vorbehalten. Es gilt das kontinentaleuropische Urheberrecht.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
#
# /usr/sbin/adduser - add new user(s)
#
-# Bugs: my english, silly code
# Email: Wolfram Schneider <wosch@cs.tu-berlin.de>
#
-# $Id: adduser,v 1.57 1995/03/07 18:40:37 w Exp w $
+# $Id: adduser.perl,v 1.10 1996/02/10 17:15:47 wosch Exp $
#
# read variables
# List of directories where shells located
@path = ('/bin', '/usr/bin', '/usr/local/bin');
# common shells, first element has higher priority
- @shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh');
+ @shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh');
- $defaultshell = 'bash'; # defaultshell if not empty
+ $defaultshell = 'sh'; # defaultshell if not empty
$group_uniq = 'USER';
$defaultgroup = $group_uniq;# login groupname, $group_uniq means username
}
}
}
+
+# Allow /nonexistant and /bin/date as a valid shell for system utils
+ push(@list, "/nonexistant");
+ push(@shellpref, "no");
+ $shell{"no"} = "/nonexistant";
+
+ push(@list, "/bin/date");
+ push(@shellpref, "date");
+ $shell{"date"} = "/bin/date";
+
return $err;
}
while(1) {
$name = &confirm_list("Enter username", 1, "a-z0-9", "");
+ if (length($name) > 8) {
+ warn "Username is longer than 8 chars\a\n";
+ next;
+ }
last if (&new_users_name_valid($name) eq $name);
}
return $name;
Shell: $sh
EOF
- return &confirm_yn("Ok?", "yes");
+ return &confirm_yn("OK?", "yes");
}
# make password database
print "\n";
local(@message_buffer_append) = ();
- if (!&confirm_yn("Add somethings to message", "no")) {
- print "Finish with beginning ``.'' or ^D\n";
+ if (!&confirm_yn("Add anything to default message", "no")) {
+ print "Use ``.'' or ^D alone on a line to finish your message.\n";
push(@message_buffer_append, "\n");
while($read = <STDIN>) {
last if $read eq "\.\n";
while(1) {
$password = &confirm_list("Enter password", 1, "", "");
last if $password ne "";
- last if &confirm_yn("Use empty password?", "yes");
+ last if &confirm_yn("Use an empty password?", "yes");
}
return $password;
print "\n" if $verbose;
print "Ok, let's go.\n" .
- "Don't worry about mistakes. I ask you later for " .
- "correct input.\n" if $verbose;
+ "Don't worry about mistakes. I will give you the chance later to " .
+ "correct any input.\n" if $verbose;
# name: Username
# fullname: Full name
} else {
$new_users_ok = 0;
}
- if (!&confirm_yn("Continue with next user?", "yes")) {
- print "Good bye.\n" if $verbose;
+ if (!&confirm_yn("Add another user?", "yes")) {
+ print "Goodbye!\n" if $verbose;
last;
}
print "\n" if !$verbose;
return @array;
}
-# see /usr/src/usr.bin/passwd/local_passwd.c or librcypt
+# see /usr/src/usr.bin/passwd/local_passwd.c or librcypt, crypt(3)
sub salt {
- local($salt) = '_';
- local($i);
-
- srand;
+ local($salt); # initialization
+ local($i, $rand);
+ local(@itoa64) = ( 0 .. 9, a .. z, A .. Z ); # 0 .. 63
+ warn "calculate salt\n" if $verbose > 1;
# to64
- while(1) {
- $i = sprintf("%c", rand(256));
- $salt .= $i if $i =~ /[A-Za-z0-9]/;
- last if length($salt) == 8;
+ for ($i = 0; $i < 8; $i++) {
+ srand(time + $rand + $$);
+ $rand = rand(25*29*17 + $rand);
+ $salt .= $itoa64[$rand & $#itoa64];
}
+ warn "Salt is: $salt\n" if $verbose > 1;
return $salt;
}
+
# print banner
sub copyright {
print <<'EOF';
sub hints {
if ($verbose) {
print "Use option ``-silent'' if you don't want see " .
- "some warnings & questions.\n\n";
+ "all warnings & questions.\n\n";
} else {
print "Use option ``-verbose'' if you want see more warnings & " .
"questions \nor try to repair bugs.\n\n";
return 0;
}
+ if ($dotdir eq 'no') {
+ if (!mkdir("$homedir",0755)) {
+ warn "mkdir $homedir: $!\n"; return 0;
+ }
+ system 'chown', "$name:$group", $homedir;
+ return !$?;
+ }
+
# copy files from $dotdir to $homedir
# rename 'dot.foo' files to '.foo'
print "Copy files from $dotdir to $homedir\n" if $verbose;
local($file,@list) = @_;
local($e);
local($LOCK_EX) = 2;
+ local($LOCK_NB) = 4;
local($LOCK_UN) = 8;
open(F, ">> $file") || die "$file: $!\n";
print "Lock $file.\n" if $verbose > 1;
- flock(F, $LOCK_EX);
+ while(!flock(F, $LOCK_EX | $LOCK_NB)) {
+ warn "Cannot lock file: $file\a\n";
+ die "Sorry, give up\n"
+ unless &confirm_yn("Try again?", "yes");
+ }
print F join("\n", @list) . "\n";
close F;
print "Unlock $file.\n" if $verbose > 1;
$dotdir = &dotdir_default; # check $dotdir
$send_message = &message_default; # send message to new user
$defaultpasswd = &password_default; # maybe use password
-&config_write(0); # write variables in file
+&config_write(!$verbose); # write variables in file
# main loop for creating new users
&new_users; # add new users