aboutsummaryrefslogtreecommitdiff
path: root/release/adduser.sh
blob: 3ccd70064a0d83e32ba690baf31afe740ad6567a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/stand/sh
#
# Written:  November 6th, 1994
# Copyright (C) 1994 by Michael Reifenberger
#
# Permission to copy or use this software for any purpose is granted
# provided that this message stay intact, and at this location (e.g. no
# putting your name on top after doing something trivial like reindenting
# it, just to make it look like you wrote it!).

########################
# First set some globals
startuid=1000;
startgid=1000;
gname=guest
uname=guest
shell="/bin/csh"
needgentry="NO"

. /stand/miscfuncs.sh

#########################
# Some Functions we need.
#
###########################
# Show the User all options
usage() {
message "
adduser -h	Prints help
adduser -i	For interactively adding users

Command line options:
adduser [-u UserName][-g GroupName][-s Shell]"
  exit 1
}
##########################
# Get the next free UserID
getuid() {
local xx=$startuid;
uid=$startuid;
for i in `cut -f 3 -d : /etc/master.passwd | cut -c 2- | sort -n`; do
  if [ $i -lt $xx ]; then
  elif [ $i -eq $xx ]; then xx=`expr $xx + 1`
  else uid=$xx; return 0
  fi
done
}
#######################################################
# Get the next free GroupID or the GroupID of GroupName
getgid() {
local xx=$startgid;
gid=$startgid;
needgentry="YES"
if grep -q \^$gname: /etc/group; then
  gid=`grep \^$gname: /etc/group | cut -f 3 -d:`
  needgentry="NO"
else
  for i in `cut -f 3 -d : /etc/group | cut -c 2- | sort -n`; do
    if [ $i -lt $xx ]; then
    elif [ $i -eq $xx ]; then xx=`expr $xx + 1`
    else gid=$xx; return 0
    fi
  done
fi
}
##########################################
# Ask the User interactively what he wants
interact() {
dialog --title "Add New User Name" --clear \
--inputbox "Please specify a login name for the user:\n\
Hit [return] for a default of <$uname>" -1 -1 2> /tmp/i.$$
ret=$?
case $ret in
  0)
   if [ x`cat /tmp/i.$$` != x ]; then 
     uname=`cat /tmp/i.$$`; fi;;
  1|255)
    exit 1;;
esac
if grep -q \^$uname: /etc/master.passwd; then
  error "Username $uname already exists."
  exit 1
fi
dialog --title "Group Name" --clear \
--inputbox "Which group should $uname belong to?\n\
Hit [return] for default of <$gname>" -1 -1 2> /tmp/i.$$
ret=$?
case $ret in
  0)
   if [ x`cat /tmp/i.$$` != x ]; then
     gname=`cat /tmp/i.$$`; fi;;
  1|255)
    exit 1;;
esac
dialog --title "Login Shell" --clear \
--inputbox "Please specify which login shell\n<$uname> should use\n\
Hit [return] for default of <$shell>" -1 -1 2> /tmp/i.$$
ret=$?
case $ret in
  0)
    if [ x`cat /tmp/i.$$` != x ]; then
      shell=`cat /tmp/i.$$`; fi;;
  1|255)
    exit 1;;
esac
##############
# Remove junk
rm -f /tmp/i.$$
}

#########
# START #
#########

###################################
# Parse the commandline for options
set -- `getopt hiu:g:s: $*`
if [ $? != 0 ]; then
  usage
fi
for i; do
  case "$i"
  in
    -h)
      usage; shift;;
    -i)
      interact; shift; iflag=yes; break;;
    -u)
      uname=$2; shift; shift;;
    -g)
      gname=$2; shift; shift;;
    -s)
      shell=$2; shift; shift;;
    --)
      shift; break;;
#    *)
#     usage; shift;;
  esac
done
#####################
# This is no Edituser
if grep -q \^$uname: /etc/master.passwd; then
  error "This user already exists in the master password file.\n
Use 'chpass' to edit an existing user rather than adduser.."
  exit 1;
fi

###############
# Get Free ID's
getuid;
getgid;
###################
# Only if necessary
if [ $needgentry = "YES" ]; then
  echo "$gname:*:$gid:$uname" >> /etc/group
fi
################
# Make /home BTW
mkdir -p -m755 /home/$uname
if [ ! -d /home/$uname ]; then
  error "Could not create /home/$uname"
  exit 1
else
  for xx in /usr/share/skel/*; do
    cp $xx /home/$uname/.`basename $xx | cut -f 2 -d .`
  done
fi
#####################
# Make the User happy
if [ ! -x $shell ]; then
  message "There is no <$shell> shell, using /bin/sh instead.\n
  If you wish, you can change this choice later with 'chpass'"
  shell="/bin/csh"
elif ! grep -q $shell /etc/shells; then
  echo $shell >> /etc/shells
  echo "<$shell> added to /etc/shells"
fi
echo "$uname:*:$uid:$gid::0:0:User &:/home/$uname:$shell" >> /etc/master.passwd
pwd_mkdb /etc/master.passwd
chown -R $uname.$gname /home/$uname
chmod -R 644 /home/$uname
chmod 755 /home/$uname
passwd $uname