1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # adduser 1.0 - add a new user to the system Author: Kees J. Bot
|
---|
4 | # 16 Jan 1996
|
---|
5 |
|
---|
6 | # Check arguments.
|
---|
7 | case "$#" in
|
---|
8 | 3) user="$1"; group="$2"; home="$3"
|
---|
9 | ;;
|
---|
10 | *) echo "Usage: adduser user group home-dir" >&2; exit 1
|
---|
11 | esac
|
---|
12 |
|
---|
13 | # We need to be root.
|
---|
14 | case "`id`" in
|
---|
15 | 'uid=0('*)
|
---|
16 | ;;
|
---|
17 | *) echo "adduser: you must be root to add users" >&2; exit 1
|
---|
18 | esac
|
---|
19 |
|
---|
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 ]
|
---|
23 | then
|
---|
24 | echo >&2 \
|
---|
25 | "adduser: the user name must be alphanumeric and no longer than 8 characters"
|
---|
26 | exit 1
|
---|
27 | fi
|
---|
28 |
|
---|
29 | len=`expr "$group" : '[a-z][a-z0-9]*$'`
|
---|
30 | if [ "$len" -eq 0 -o "$len" -gt 8 ]
|
---|
31 | then
|
---|
32 | echo >&2 \
|
---|
33 | "adduser: the group name must be alphanumeric and no longer than 8 characters"
|
---|
34 | exit 1
|
---|
35 | fi
|
---|
36 |
|
---|
37 | # The new user name must not exist, but the group must exist.
|
---|
38 | if grep "^$user:" /etc/passwd >/dev/null
|
---|
39 | then
|
---|
40 | echo "adduser: user $user already exists" >&2
|
---|
41 | exit 1
|
---|
42 | fi
|
---|
43 |
|
---|
44 | gid=`sed -e "/^$group:/!d" -e 's/^[^:]*:[^:]*:\\([^:]*\\):.*/\\1/' /etc/group`
|
---|
45 | if [ `expr "$gid" : '[0-9]*$'` -eq 0 ]
|
---|
46 | then
|
---|
47 | echo "adduser: group $group does not exist" >&2
|
---|
48 | exit 1
|
---|
49 | fi
|
---|
50 |
|
---|
51 | # Find the first free user-id of 10 or higher.
|
---|
52 | uid=10
|
---|
53 | while grep "^[^:]*:[^:]*:$uid:.*" /etc/passwd >/dev/null
|
---|
54 | do
|
---|
55 | uid=`expr $uid + 1`
|
---|
56 | done
|
---|
57 |
|
---|
58 | # No interruptions.
|
---|
59 | trap '' 1 2 3 15
|
---|
60 |
|
---|
61 | # Lock the password file.
|
---|
62 | ln /etc/passwd /etc/ptmp || {
|
---|
63 | echo "adduser: password file busy, try again later"
|
---|
64 | exit 1
|
---|
65 | }
|
---|
66 |
|
---|
67 | # Make the new home directory, it should not exist already.
|
---|
68 | mkdir "$home" || {
|
---|
69 | rm -rf /etc/ptmp
|
---|
70 | exit 1
|
---|
71 | }
|
---|
72 |
|
---|
73 | # Make the new home directory by copying the honorary home directory of our
|
---|
74 | # fearless leader.
|
---|
75 | echo cpdir /usr/ast "$home"
|
---|
76 | cpdir /usr/ast "$home" || {
|
---|
77 | rm -rf /etc/ptmp "$home"
|
---|
78 | exit 1
|
---|
79 | }
|
---|
80 |
|
---|
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"
|
---|
85 | exit 1
|
---|
86 | }
|
---|
87 |
|
---|
88 | # Is there a shadow password file? If so add an entry.
|
---|
89 | if [ -f /etc/shadow ]
|
---|
90 | then
|
---|
91 | echo "echo $user::0:0::: >>/etc/shadow"
|
---|
92 | echo "$user::0:0:::" >>/etc/shadow || {
|
---|
93 | rm -rf /etc/ptmp "$home"
|
---|
94 | exit 1
|
---|
95 | }
|
---|
96 | pwd="##$user"
|
---|
97 | else
|
---|
98 | pwd=
|
---|
99 | fi
|
---|
100 |
|
---|
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"
|
---|
105 | exit 1
|
---|
106 | }
|
---|
107 |
|
---|
108 | # Remove the lock.
|
---|
109 | rm /etc/ptmp || exit
|
---|
110 |
|
---|
111 | echo "
|
---|
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."
|
---|
115 | if [ $gid = 0 ]
|
---|
116 | then
|
---|
117 | echo "\
|
---|
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)."
|
---|
120 | fi
|
---|
121 | exit 0
|
---|