]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - release/adduser.sh
Josh Littlefield's fixes for my extreme boneheadedness.
[FreeBSD/FreeBSD.git] / release / adduser.sh
1 #!/stand/sh
2 #
3 # Written:  November 6th, 1994
4 # Copyright (C) 1994 by Michael Reifenberger
5 #
6 # Permission to copy or use this software for any purpose is granted
7 # provided that this message stay intact, and at this location (e.g. no
8 # putting your name on top after doing something trivial like reindenting
9 # it, just to make it look like you wrote it!).
10
11 ########################
12 # First set some globals
13 startuid=1000;
14 startgid=1000;
15 gname=guest
16 uname=guest
17 shell="/bin/csh"
18 needgentry="NO"
19
20 . /stand/miscfuncs.sh
21
22 #########################
23 # Some Functions we need.
24 #
25 ###########################
26 # Show the User all options
27 usage() {
28 message "
29 adduser -h      Prints help
30 adduser -i      For interactively adding users
31
32 Command line options:
33 adduser [-u UserName][-g GroupName][-s Shell]"
34   exit 1
35 }
36 ##########################
37 # Get the next free UserID
38 getuid() {
39 local xx=$startuid;
40 uid=$startuid;
41 for i in `cut -f 3 -d : /etc/master.passwd | cut -c 2- | sort -n`; do
42   if [ $i -lt $xx ]; then
43   elif [ $i -eq $xx ]; then xx=`expr $xx + 1`
44   else uid=$xx; return 0
45   fi
46 done
47 }
48 #######################################################
49 # Get the next free GroupID or the GroupID of GroupName
50 getgid() {
51 local xx=$startgid;
52 gid=$startgid;
53 needgentry="YES"
54 if grep -q \^$gname: /etc/group; then
55   gid=`grep \^$gname: /etc/group | cut -f 3 -d:`
56   needgentry="NO"
57 else
58   for i in `cut -f 3 -d : /etc/group | cut -c 2- | sort -n`; do
59     if [ $i -lt $xx ]; then
60     elif [ $i -eq $xx ]; then xx=`expr $xx + 1`
61     else gid=$xx; return 0
62     fi
63   done
64 fi
65 }
66 ##########################################
67 # Ask the User interactively what he wants
68 interact() {
69 dialog --title "Add New User Name" --clear \
70 --inputbox "Please specify a login name for the user:\n\
71 Hit [return] for a default of <$uname>" -1 -1 2> /tmp/i.$$
72 ret=$?
73 case $ret in
74   0)
75    if [ x`cat /tmp/i.$$` != x ]; then 
76      uname=`cat /tmp/i.$$`; fi;;
77   1|255)
78     exit 1;;
79 esac
80 if grep -q \^$uname: /etc/master.passwd; then
81   error "Username $uname already exists."
82   exit 1
83 fi
84 dialog --title "Group Name" --clear \
85 --inputbox "Which group should $uname belong to?\n\
86 Hit [return] for default of <$gname>" -1 -1 2> /tmp/i.$$
87 ret=$?
88 case $ret in
89   0)
90    if [ x`cat /tmp/i.$$` != x ]; then
91      gname=`cat /tmp/i.$$`; fi;;
92   1|255)
93     exit 1;;
94 esac
95 dialog --title "Login Shell" --clear \
96 --inputbox "Please specify which login shell\n<$uname> should use\n\
97 Hit [return] for default of <$shell>" -1 -1 2> /tmp/i.$$
98 ret=$?
99 case $ret in
100   0)
101     if [ x`cat /tmp/i.$$` != x ]; then
102       shell=`cat /tmp/i.$$`; fi;;
103   1|255)
104     exit 1;;
105 esac
106 ##############
107 # Remove junk
108 rm -f /tmp/i.$$
109 }
110
111 #########
112 # START #
113 #########
114
115 ###################################
116 # Parse the commandline for options
117 set -- `getopt hiu:g:s: $*`
118 if [ $? != 0 ]; then
119   usage
120 fi
121 for i; do
122   case "$i"
123   in
124     -h)
125       usage; shift;;
126     -i)
127       interact; shift; iflag=yes; break;;
128     -u)
129       uname=$2; shift; shift;;
130     -g)
131       gname=$2; shift; shift;;
132     -s)
133       shell=$2; shift; shift;;
134     --)
135       shift; break;;
136 #    *)
137 #     usage; shift;;
138   esac
139 done
140 #####################
141 # This is no Edituser
142 if grep -q \^$uname: /etc/master.passwd; then
143   error "This user already exists in the master password file.\n
144 Use 'chpass' to edit an existing user rather than adduser.."
145   exit 1;
146 fi
147
148 ###############
149 # Get Free ID's
150 getuid;
151 getgid;
152 ###################
153 # Only if necessary
154 if [ $needgentry = "YES" ]; then
155   echo "$gname:*:$gid:$uname" >> /etc/group
156 fi
157 ################
158 # Make /home BTW
159 mkdir -p -m755 /home/$uname
160 if [ ! -d /home/$uname ]; then
161   error "Could not create /home/$uname"
162   exit 1
163 else
164   for xx in /usr/share/skel/*; do
165     cp $xx /home/$uname/.`basename $xx | cut -f 2 -d .`
166   done
167 fi
168 #####################
169 # Make the User happy
170 if [ ! -x $shell ]; then
171   message "There is no <$shell> shell, using /bin/sh instead.\n
172   If you wish, you can change this choice later with 'chpass'"
173   shell="/bin/csh"
174 elif ! grep -q $shell /etc/shells; then
175   echo $shell >> /etc/shells
176   echo "<$shell> added to /etc/shells"
177 fi
178 echo "$uname:*:$uid:$gid::0:0:User &:/home/$uname:$shell" >> /etc/master.passwd
179 pwd_mkdb /etc/master.passwd
180 chown -R $uname.$gname /home/$uname
181 chmod -R 644 /home/$uname
182 chmod 755 /home/$uname
183 passwd $uname