source: trunk/minix/commands/scripts/adduser.sh@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.7 KB
Line 
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.
7case "$#" in
83) user="$1"; group="$2"; home="$3"
9 ;;
10*) echo "Usage: adduser user group home-dir" >&2; exit 1
11esac
12
13# We need to be root.
14case "`id`" in
15'uid=0('*)
16 ;;
17*) echo "adduser: you must be root to add users" >&2; exit 1
18esac
19
20# User and group names must be alphanumeric and no longer than 8 characters.
21len=`expr "$user" : '[a-z][a-z0-9]*$'`
22if [ "$len" -eq 0 -o "$len" -gt 8 ]
23then
24 echo >&2 \
25"adduser: the user name must be alphanumeric and no longer than 8 characters"
26 exit 1
27fi
28
29len=`expr "$group" : '[a-z][a-z0-9]*$'`
30if [ "$len" -eq 0 -o "$len" -gt 8 ]
31then
32 echo >&2 \
33"adduser: the group name must be alphanumeric and no longer than 8 characters"
34 exit 1
35fi
36
37# The new user name must not exist, but the group must exist.
38if grep "^$user:" /etc/passwd >/dev/null
39then
40 echo "adduser: user $user already exists" >&2
41 exit 1
42fi
43
44gid=`sed -e "/^$group:/!d" -e 's/^[^:]*:[^:]*:\\([^:]*\\):.*/\\1/' /etc/group`
45if [ `expr "$gid" : '[0-9]*$'` -eq 0 ]
46then
47 echo "adduser: group $group does not exist" >&2
48 exit 1
49fi
50
51# Find the first free user-id of 10 or higher.
52uid=10
53while grep "^[^:]*:[^:]*:$uid:.*" /etc/passwd >/dev/null
54do
55 uid=`expr $uid + 1`
56done
57
58# No interruptions.
59trap '' 1 2 3 15
60
61# Lock the password file.
62ln /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.
68mkdir "$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.
75echo cpdir /usr/ast "$home"
76cpdir /usr/ast "$home" || {
77 rm -rf /etc/ptmp "$home"
78 exit 1
79}
80
81# Change the ownership to the new user.
82echo chown -R $uid:$gid "$home"
83chown -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.
89if [ -f /etc/shadow ]
90then
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"
97else
98 pwd=
99fi
100
101# Finish up by adding a password file entry.
102echo "echo $user:$pwd:$uid:$gid:$user:$home: >>/etc/passwd"
103echo "$user:$pwd:$uid:$gid:$user:$home:" >>/etc/passwd || {
104 rm -rf /etc/ptmp "$home"
105 exit 1
106}
107
108# Remove the lock.
109rm /etc/ptmp || exit
110
111echo "
112The new user $user has been added to the system. Note that the password,
113full name, and shell may be changed with the commands passwd(1), chfn(1),
114and chsh(1). The password is now empty, so only console logins are possible."
115if [ $gid = 0 ]
116then
117 echo "\
118Also note that a new operator needs an executable search path (\$PATH) that
119does not contain the current directory (an empty field or "." in \$PATH)."
120fi
121exit 0
Note: See TracBrowser for help on using the repository browser.