3 # adduser 1.0 - add a new user to the system Author: Kees J. Bot
8 3) user
="$1"; group
="$2"; home
="$3"
10 *) echo "Usage: adduser user group home-dir" >&2; exit 1
17 *) echo "adduser: you must be root to add users" >&2; exit 1
20 # User and group names must be alphanumeric and no longer than 8 characters.
21 len
=`expr "$user" : '[a-z][a-z0-9]*$'`
22 if [ "$len" -eq 0 -o "$len" -gt 8 ]
25 "adduser: the user name must be alphanumeric and no longer than 8 characters"
29 len
=`expr "$group" : '[a-z][a-z0-9]*$'`
30 if [ "$len" -eq 0 -o "$len" -gt 8 ]
33 "adduser: the group name must be alphanumeric and no longer than 8 characters"
37 # The new user name must not exist, but the group must exist.
38 if grep "^$user:" /etc
/passwd
>/dev
/null
40 echo "adduser: user $user already exists" >&2
44 gid
=`sed -e "/^$group:/!d" -e 's/^[^:]*:[^:]*:\\([^:]*\\):.*/\\1/' /etc/group`
45 if [ `expr "$gid" : '[0-9]*$'` -eq 0 ]
47 echo "adduser: group $group does not exist" >&2
51 # Find the first free user-id of 10 or higher.
53 while grep "^[^:]*:[^:]*:$uid:.*" /etc
/passwd
>/dev
/null
61 # Lock the password file.
62 ln /etc
/passwd
/etc
/ptmp ||
{
63 echo "adduser: password file busy, try again later"
67 # Make the new home directory, it should not exist already.
73 # Make the new home directory by copying the honorary home directory of our
75 echo cpdir
/usr
/ast
"$home"
76 cpdir
/usr
/ast
"$home" ||
{
77 rm -rf /etc
/ptmp
"$home"
81 # Change the ownership to the new user.
82 echo chown
-R $uid:$gid "$home"
83 chown
-R $uid:$group "$home" ||
{
84 rm -rf /etc
/ptmp
"$home"
88 # Is there a shadow password file? If so add an entry.
91 echo "echo $user::0:0::: >>/etc/shadow"
92 echo "$user::0:0:::" >>/etc
/shadow ||
{
93 rm -rf /etc
/ptmp
"$home"
101 # Finish up by adding a password file entry.
102 echo "echo $user:$pwd:$uid:$gid:$user:$home: >>/etc/passwd"
103 echo "$user:$pwd:$uid:$gid:$user:$home:" >>/etc
/passwd ||
{
104 rm -rf /etc
/ptmp
"$home"
112 The new user $user has been added to the system. Note that the password,
113 full name, and shell may be changed with the commands passwd(1), chfn(1),
114 and chsh(1). The password is now empty, so only console logins are possible."
118 Also note that a new operator needs an executable search path (\$PATH) that
119 does not contain the current directory (an empty field or ".
" in \$PATH)."