xref: /freebsd/usr.sbin/adduser/adduser.sh (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1#!/usr/bin/perl
2
3
4
5#  Copyright (c) 1994 GB Data Systems
6#  All rights reserved.
7#  Redistribution and use in source and binary forms, with or without
8#  modification, are permitted provided that the following conditions
9#  are met:
10#  1. Redistributions of source code must retain the above copyright
11#     notice, this list of conditions and the following disclaimer.
12#  2. Redistributions in binary form must reproduce the above copyright
13#     notice, this list of conditions and the following disclaimer in the
14#     documentation and/or other materials provided with the distribution.
15#  3. The name of the Author may not be used to endorse or promote products
16#     derived from this software without specific prior written permission.
17#  THIS SOFTWARE IS PROVIDED BY GB DATA AND CONTRIBUTORS ``AS IS'' AND
18#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED.  IN NO EVENT SHALL GB DATA OR CONTRIBUTORS BE LIABLE
21#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27#  SUCH DAMAGE.
28
29#
30# $Id: adduser,v 1.4 1994/12/28 17:27:21 gclarkii Exp $
31#
32
33$configfile = "\/etc\/adduser.conf";
34
35if (-f $configfile) {
36 open (CONFIG, "$configfile");
37  while (<CONFIG>) {
38    eval "$_";
39  }
40}
41
42open (WHOAMI, "whoami|");
43
44while (<WHOAMI>) {
45$whoami = $_;
46}
47chop $whoami;
48
49if ($whoami ne "root") {
50system "clear";
51print "\n\nYou must be root to add an user\n\n";
52close WHOAMI;
53exit;
54}
55close WHOAMI;
56
57# Start getting information and print a banner
58
59print "                                Adduser\n";
60print "             A system utility for adding users with defaults\n";
61print "\n\n";
62
63#
64# User ID
65#
66
67
68print "Please enter the login name of the user: ";
69chop ($userlogin = <STDIN>);
70
71
72sub subuid {
73$userid = "";
74print "Please enter the user id or hit enter for the next id: ";
75chop ($userid = <STDIN>);
76}
77
78while (!$userid) {
79&subuid;
80if (!$userid) {
81  if ($useautoids) {
82     open (USERID, "+<$userids");
83     chop ($xxuserid = <USERID>);
84     $userid = $xxuserid + 1;
85     close USERID;
86     open (USERID, "+>$userids");
87     print (USERID "$userid\n");
88     close USERID;
89   } else { &subuid; }
90}
91}
92
93#
94# Group ID
95#
96
97sub groupids {
98print "Please enter the group id or hit enter for the default id: ";
99chop ($groupid = <STDIN>);
100}
101
102&groupids;
103
104while (!$groupid) {
105  if ($defgroupid) {
106    if (!$groupid) {
107     $groupid = "$defgroupid";
108    } else { &groupids; }
109  } else { &groupids; }
110}
111
112#
113#  User name
114#
115
116print "Please enter the user's name: ";
117chop ($username = <STDIN>);
118
119#
120# Home directory
121#
122
123print "Please enter the users home directory or hit enter for default: ";
124chop ($userdir = <STDIN>);
125
126if (!$userdir) {
127	$userdir = "$defusrdir\/$userlogin";
128	print "$userdir\n";
129}
130
131#
132# Login Shell
133#
134
135print "Please enter the users login shell or hit enter for default: ";
136chop ($usershell = <STDIN>);
137
138if (!$usershell) {
139	$usershell = "$userdefshell";
140	print "$usershell\n";
141}
142
143#
144# Create password file entry
145#
146
147print "Opening and locking passwd file in blocking mode.\n";
148open (PASS, '>>/etc/master.passwd');
149flock (PASS, 2) || die "Can't lock passwd file, must be in use!!\n";
150print (PASS "$userlogin::$userid:$groupid::0:0:$username,,,:$userdir:$usershell\n");
151print "Unlocking and closing password file\n";
152flock (PASS,8);
153close PASS;
154print "Re-indexing password databases\n";
155system 'pwd_mkdb -p /etc/master.passwd';
156system "passwd $userlogin";
157
158#
159# Create user directory
160#
161print "Creating user directory\n";
162if (! -e $defusrdir) {
163   system "mkdir -p $defusrdir\/$userdir";
164} else {
165   system "mkdir $userdir";
166}
167
168
169print "Copying user shell files\n";
170system "cp $skel_location\/dot.login $userdir\/\.login";
171system "cp $skel_location\/dot.profile $userdir\/\.profile";
172
173if ($usershell eq "\/bin\/csh" || $usershell eq "\/usr\/local\/bin\/tcsh")
174   {
175   system "cp $skel_location\/dot.cshrc $userdir\/.cshrc";
176   }
177system  "chmod -R 664 $userdir";
178system  "chown -R $userid.$groupid $userdir";
179
180
181
182#
183# Print out information used in creation of this account
184#
185print "\n\n";
186print "Information used to create this account follows.\n";
187print "\n";
188print "Login Name:      $userlogin\n";
189print "UserId:          $userid\n";
190print "GroupId:         $groupid\n";
191print "UserName:        $username\n";
192print "HomeDir:         $userdir\n";
193print "Shell:           $usershell\n";
194print "\nDONE\n\n";
195
196