|
|
1.1 root 1: #!/bin/sh
2: # /etc/newusr 6/7/91
3: # Usage: /etc/newusr user UserName parentdir [ shell ]
4: # Requires root permissions.
5:
6: # File and directory names.
7: PFILE=/etc/passwd
8: GFILE=/etc/group
9: LOCK=/tmp/passwd.tmp
10: MAILDIR=/usr/spool/mail
11: SHELL=/bin/sh
12:
13: # Check usage.
14: if /bin/test $# -ne 3 -a $# -ne 4
15: then
16: echo "Usage: /etc/newusr user \"User Name\" parentdir [shell]" >&2 && exit 1
17: fi
18:
19: # Use SHELL if specified.
20: if /bin/test $# -eq 4
21: then
22: SHELL=$4
23: fi
24:
25: # Lock out other access to password and group files.
26: if /bin/test -f $LOCK
27: then
28: echo "newusr: lock file $LOCK exists" >&2 && exit 1
29: fi
30: >$LOCK
31:
32: # Make sure invoker is root.
33: chown root $LOCK 2>/dev/null || echo "newusr: permission denied" >&2 && rm $LOCK && exit 1
34:
35: # Sort $PFILE by uid and find max uid.
36: sort -n '-t:' +2 -3 -o $PFILE $PFILE
37: UID=`tail -1 $PFILE | sed -n "s/[^:]*:[^:]*:\\([^:]*\\).*/\\1/p"`
38:
39: # Find gid for group "user".
40: GID=`egrep user $GFILE | sed -n "s/[^:]*:[^:]*:\\([^:]*\\).*/\\1/p"`
41:
42: # Test if user exists already.
43: if egrep "^$1:" $PFILE >/dev/null
44: then
45: echo "newusr: user $1 already exists" >&2
46: else
47: UID=`expr $UID + 1`
48: HOME=$3/$1
49: PROF=$HOME/.profile
50: MAILBOX=$MAILDIR/$1
51: echo "Adding user: uname=$1 uid=$UID gname=user gid=$GID HOME=$HOME shell=$SHELL"
52:
53: # Update password file.
54: echo "$1::$UID:$GID:$2:$HOME:$SHELL" >>$PFILE
55:
56: # Update group file.
57: cat $GFILE >$LOCK
58: sed -e "/^user:/s/\$/,$1/" $LOCK >$GFILE
59:
60: # Make new directory, .profile, mailbox.
61: mkdir -r $HOME || echo /etc/newusr: $HOME already exists
62: echo "MAIL=$MAILBOX" >$PROF
63: >$MAILBOX
64: chown $1 $HOME $PROF $MAILBOX
65: chgrp user $HOME $PROF $MAILBOX
66: fi
67:
68: rm $LOCK
69: sync
70: exit 0
71:
72: # end of /etc/newusr
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.