/[adduser]/trunk/adduser
ViewVC logotype

Contents of /trunk/adduser

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations) (download)
Fri Jun 6 21:41:05 2003 UTC (9 years, 11 months ago) by rb
File size: 23468 byte(s)
Load adduser-3.11.1 into trunk.
1 #!/usr/bin/perl -w
2 #
3 # adduser VERSION
4 #
5 # adduser: a utility to add users to the system
6 # addgroup: a utility to add groups to the system
7
8 # Copyright (C) 1997, 1998, 1999 Guy Maor <maor@debian.org>
9 # Copyright (C) 1995 Ted Hajek <tedhajek@boombox.micro.umn.edu>
10 # Ian A. Murdock <imurdock@gnu.ai.mit.edu>
11 # General scheme of the program adapted by the original debian 'adduser'
12 # program by Ian A. Murdock <imurdock@gnu.ai.mit.edu>.
13 #
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #
28 #
29 ####################
30 # the program can be called as:
31 #
32 # adduser [--home DIR] [--uid ID] [--ingroup GROUP | --gid ID]
33 # [--disabled-password] [--gecos GECOS] [--no-create-home] user
34 # add a normal user to the system
35 # example: adduser fred
36 # $action = "adduser"
37 #
38 # adduser --group [--gid ID] group
39 # addgroup [--gid ID] group
40 # add a system group
41 # example: addgroup --quiet www-data
42 # $action = "addgroup"
43 #
44 # adduser --system [--home DIR] [--uid ID] [--group | --ingroup GROUP
45 # | --gid ID] [--disabled-password] [--gecos GECOS] [--no-create-home] user
46 # add a system user. Create a like-named, like-id'd group with
47 # --group, add to an existing group with --ingroup or --gid. Add
48 # to "nogroup" with neither.
49 # example: adduser --system --home /home/gopher-data --group gopher
50 # $action = "addsysuser"
51 #
52 # adduser user group
53 # add the existing user to an existing group.
54 # $action = "addusertogroup"
55 #
56 # all commands take the following options:
57 # --quiet don't give progress information on STDOUT
58 # --force-badname disable checking of names for funny characters
59 # --help usage message
60 # --version version number and copyright
61 # --conf FILE use FILE instead of /etc/adduser.conf
62 ############
63
64 BEGIN {
65 eval 'use Locale::gettext';
66 if ($@) {
67 *gettext = sub { shift };
68 *textdomain = sub { "" };
69 *LC_MESSAGES = sub { 5 };
70 }
71 eval {
72 require POSIX;
73 import POSIX qw(setlocale);
74 };
75 if ($@) {
76 *setlocale = sub { 1 };
77 }
78 }
79
80 setlocale(LC_MESSAGES, "");
81 textdomain("adduser");
82
83 $verbose = 1; # should we be verbose?
84 $allow_badname = 0; # should we allow bad names?
85 $ask_passwd = 1; # ask for a passwd?
86
87 $defaults = "/etc/adduser.conf";
88 $nogroup_id = getgrnam("nogroup") || 65534;
89 $0 =~ s+.*/++;
90 $nscdinit = "/etc/init.d/nscd";
91
92 $config{"dshell"} = "/bin/bash";
93 $config{"first_system_uid"} = 100;
94 $config{"last_system_uid"} = 999;
95 $config{"first_uid"} = 1000;
96 $config{"last_uid"} = 29999;
97 $config{"dhome"} = "/home";
98 $config{"skel"} = "/etc/skel";
99 $config{"usergroups"} = "yes";
100 $config{"users_gid"} = "100";
101 $config{"grouphomes"} = "no";
102 $config{"letterhomes"} = "no";
103 $config{"quotauser"} = "";
104
105 $action = $0 eq "addgroup" ? "addgroup" : "adduser";
106
107 while ($arg = shift(@ARGV)) {
108 die "$0: ",_("No options allowed after names.\n")
109 if ($names[0] && $arg =~ /^--/);
110 if ($arg eq "--quiet") {
111 $verbose = 0;
112 } elsif ($arg eq "--force-badname") {
113 $allow_badname = 1;
114 } elsif ($arg eq "--help") {
115 &usage();
116 exit 0;
117 } elsif ($arg eq "--version") {
118 &version();
119 exit 0;
120 } elsif ($arg eq "--system") {
121 $action = "addsysuser" if ($action eq "adduser");
122 } elsif ($arg eq "--group") {
123 $found_group_opt = 1;
124 } elsif ($arg eq "--ingroup") {
125 die "$0: ",_("--ingroup requires an argument.\n")
126 if (!($ingroup_name = shift(@ARGV)));
127 } elsif ($arg eq "--home") {
128 die "$0: ",_("--home requires an argument.\n")
129 if (!($special_home = shift(@ARGV)));
130 print "$0: ",_("Warning: The home dir you specified already exists.\n")
131 if (-d $special_home && $verbose);
132 die "$0: ",_("The home dir must be an absolute path.\n")
133 if ($special_home !~ m+^/+ );
134 } elsif ($arg eq "--gecos") {
135 $new_gecos = shift(@ARGV);
136 } elsif ($arg eq "--disabled-password") {
137 $ask_passwd = 0;
138 } elsif ($arg eq "--uid") {
139 die "$0: ",_("--uid requires a numeric argument.\n")
140 if (!($new_uid = shift(@ARGV)) || $new_uid !~ /^\-?\d+$/);
141 } elsif ($arg eq "--gid") {
142 die "$0: ",_("--gid requires a numeric argument.\n")
143 if (!($new_gid = shift(@ARGV)) || $new_gid !~ /^\-?\d+$/);
144 } elsif ($arg eq "--conf") {
145 die "$0: ",_("--conf requires an argument.\n")
146 if (!($defaults = shift(@ARGV)));
147 dief (_("`%s' doesn't exist.\n",$defaults))
148 if (! -f $defaults);
149 } elsif ($arg eq "--no-create-home") {
150 $no_create_home = 1;
151 } elsif ($arg eq "--debug") {
152 $debugging = 1;
153 } elsif ($arg =~ /^--/) { # bad argument!
154 dief (_("Unknown argument `%s'.\n"),$arg);
155 } else { # it's a username
156 push (@names, $arg);
157 }
158 }
159
160 die "$0: ",_("Only root may add a user or group to the system.\n") if ($> != 0);
161
162 if (@names == 0) {
163 print _("Enter a username to add: ");
164 chop($answer=<STDIN>);
165 push(@names, $answer);
166 }
167 die "$0: ",_("I need a name to add.\n") if (length($names[0]) == 0);
168 die "$0: ",_("No more than two names.\n") if (@names > 2);
169 if (@names == 2) { # must be addusertogroup
170 die "$0: ",_("Specify only one name in this mode.\n")
171 if ($action eq "addsysuser" || $found_group_opt);
172 $action = "addusertogroup";
173 $existing_user = shift (@names);
174 $existing_group = shift (@names);
175 }
176 else {
177 $new_name = shift (@names);
178 }
179
180 if ($found_group_opt) {
181 if ($action eq "addsysuser") {
182 $make_group_also = 1;
183 }
184 else {
185 $action = "addgroup";
186 }
187 }
188 die "$0: ",_("--group, --ingroup, and --gid options are mutually exclusive.\n") if
189 ($action ne "addgroup" &&
190 defined($found_group_opt) +defined($ingroup_name) +defined($new_gid) > 1);
191
192
193 #####
194 # OK, we've processed the arguments. $action equals one of the following,
195 # and the appropriate variables have been set:
196 #
197 # $action = "adduser"
198 # $new_name - the name of the new user.
199 # $ingroup_name | $new_gid - the group to add the user to
200 # $special_home, $new_uid, $new_gecos - optional overrides
201 # $action = "addgroup"
202 # $new_name - the name of the new group
203 # $new_gid - optional override
204 # $action = "addsysuser"
205 # $new_name - the name of the new user
206 # $make_group_also | $ingroup_name | $new_gid | 0 - which group
207 # $special_home, $new_uid, $new_gecos - optional overrides
208 # $action = "addusertogroup"
209 # $existing_user - the user to be added
210 # $existing_group - the group to add her to
211 #####
212
213 &read_config($defaults);
214 &checkname($new_name) if $new_name;
215 $SIG{'INT'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'handler';
216
217 ##############
218 ## addgroup ##
219 ##############
220 if ($action eq "addgroup") {
221 dief (_("The group `%s' already exists.\n"),$new_name)
222 if (getgrnam($new_name));
223 dief (_("The GID `%s' is already in use.\n"),$new_gid)
224 if (defined($new_gid) && getgrgid($new_gid));
225 if (!defined($new_gid)) {
226 $new_gid = &first_avail_id($config{"first_system_uid"},
227 $config{"last_system_uid"},
228 &get_current_gids);
229
230 if ($new_gid == -1) {
231 print STDERR "$0: ",_("No GID is available in the range "),
232 "$config{\"first_system_uid\"} - $config{\"last_system_uid\"}\n",
233 "(FIRST_SYS_UID - LAST_SYS_UID). ";
234 dief (_("Group `%s' not created.\n"),$new_name);
235 }
236 }
237
238 printf (_("Adding group %s (%s)...\n"),$new_name,$new_gid) if $verbose;
239 &stopnscd();
240 &systemcall('groupadd', '-g', $new_gid, $new_name);
241 &startnscd();
242 print _("Done.\n") if $verbose;
243 exit 0;
244 }
245
246
247 ####################
248 ## addusertogroup ##
249 ####################
250 elsif ($action eq "addusertogroup") {
251 dief (_("The user `%s' doesn't exist.\n"),$existing_user)
252 if (!getpwnam($existing_user));
253 dief (_("The group `%s' doesn't exist.\n"),$existing_group)
254 if (!getgrnam($existing_group));
255 if (&user_is_member($existing_user, $existing_group)) {
256 printf _("The user `%s' is already a member of %s.\n"),
257 $existing_user,$existing_group if $verbose;
258 exit 0; # not really an error
259 }
260
261 printf _("Adding user %s to group %s...\n"),$existing_user,$existing_group
262 if $verbose;
263 &stopnscd();
264 # FIXME - the next line has a race condition.
265 &systemcall('usermod', '-G',
266 join(",", get_users_groups($existing_user), $existing_group),
267 $existing_user);
268 &startnscd();
269 print _("Done.\n") if $verbose;
270 exit 0;
271 }
272
273
274 ################
275 ## addsysuser ##
276 ################
277 elsif ($action eq "addsysuser") {
278 $new_gid = $nogroup_id
279 if (!$ingroup_name && !defined($new_gid) && !$make_group_also);
280 &check_user_group();
281 printf (_("Adding system user %s...\n"),$new_name) if $verbose;
282
283 if (!defined($new_uid) && $make_group_also) {
284 $new_uid = &first_avail_id($config{"first_system_uid"},
285 $config{"last_system_uid"},
286 &get_current_uids, &get_current_gids);
287 if ($new_uid == -1) {
288 print STDERR "$0: ",_("No UID/GID pair is available in the range "),
289 "$config{\"first_system_uid\"} - $config{\"last_system_uid\"}\n",
290 "(FIRST_SYS_UID - LAST_SYS_UID). ";
291 dief (_("User `%s' not created.\n"),$new_name);
292 }
293 $new_gid = $new_uid;
294 $ingroup_name = $new_name;
295 }
296 elsif (!defined($new_uid) && !$make_group_also) {
297 $new_uid = &first_avail_id($config{"first_system_uid"},
298 $config{"last_system_uid"},
299 &get_current_uids);
300 if ($new_uid == -1) {
301 print STDERR "$0: ",_("No UID is available in the range "),
302 "$config{\"first_system_uid\"} - $config{\"last_system_uid\"}\n",
303 "(FIRST_SYS_UID - LAST_SYS_UID). ";
304 &dief (_("User `%s' not created.\n"),$new_name);
305 }
306 if (defined($new_gid)) { $ingroup_name = getgrgid($new_gid); }
307 elsif ($ingroup_name) { $new_gid = getgrnam($ingroup_name); }
308 else { die _("Internal error"); }
309 }
310 else {
311 if (defined($new_gid)) { $ingroup_name = getgrgid($new_gid); }
312 elsif ($ingroup_name) { $new_gid = getgrnam($ingroup_name); }
313 elsif ($make_group_also){ $new_gid=$new_uid; $ingroup_name=$new_name; }
314 else { die _("Internal error"); }
315 }
316
317 &stopnscd();
318 if ($make_group_also) {
319 print _("Adding new group $new_name ($new_gid).\n") if $verbose;
320 $undogroup = $new_name;
321 &systemcall('groupadd', '-g', $new_gid, $new_name);
322 }
323
324 printf _("Adding new user %s (%s) with group %s.\n"),$new_name,$new_uid,$ingroup_name
325 if $verbose;
326 $home_dir = $special_home || &homedir($new_name, $ingroup_name);
327 $undouser = $new_name;
328 &systemcall('useradd', '-d', $home_dir, '-g', $ingroup_name, '-s',
329 '/bin/false', '-u', $new_uid, $new_name);
330 &startnscd();
331 &systemcall('chfn', '-f', $new_gecos, $new_name) if ($new_gecos);
332
333 if (-e $home_dir) {
334 printf _("Home directory %s already exists.\n"),$home_dir if $verbose;
335 } elsif ($no_create_home) {
336 print _("Not creating home directory.\n") if $verbose
337 } else {
338 printf _("Creating home directory %s.\n"),$home_dir if $verbose;
339 $undohome = $home_dir;
340 &mktree($home_dir) || &cleanup("Couldn't create $home_dir: $!.\n");
341 chown($new_uid, $new_gid, $home_dir)
342 || &cleanup("chown $new_uid:$new_gid $home_dir: $!\n");
343 $dir_mode = $make_group_also ? 02755 : 0755;
344 chmod ($dir_mode, $home_dir) ||
345 &cleanup("chmod $dir_mode $home_dir: $!\n");
346 }
347
348 exit 0;
349 }
350
351
352 #############
353 ## adduser ##
354 #############
355 elsif ($action eq "adduser") {
356 if (!$ingroup_name && !defined($new_gid)) {
357 if ($config{"usergroups"} eq "yes") { $make_group_also = 1; }
358 else { $new_gid = $config{"users_gid"}; }
359 }
360 &check_user_group();
361 printf _("Adding user %s...\n"),$new_name if $verbose;
362
363 if (!defined($new_uid) && $make_group_also) {
364 $new_uid = &first_avail_id($config{"first_uid"},
365 $config{"last_uid"},
366 &get_current_uids, &get_current_gids);
367 if ($new_uid == -1) {
368 print STDERR "$0: ",_("No UID/GID pair is available in the range "),
369 "$config{\"first_uid\"} - $config{\"last_uid\"}\n",
370 "(FIRST_UID - LAST_UID). ";
371 dief(_("User `%s' not created.\n"),$new_name);
372 }
373 $new_gid = $new_uid;
374 $ingroup_name = $new_name;
375 }
376 elsif (!defined($new_uid) && !$make_group_also) {
377 $new_uid = &first_avail_id($config{"first_uid"},
378 $config{"last_uid"},
379 &get_current_uids);
380 if ($new_uid == -1) {
381 print STDERR "$0: ",_("No UID is available in the range "),
382 "$config{\"first_uid\"} - $config{\"last_uid\"}\n",
383 "(FIRST_UID - LAST_UID). ";
384 dief(_("User `%s' not created.\n"),$new_name);
385 }
386 if (defined($new_gid)) { $ingroup_name = getgrgid($new_gid); }
387 elsif ($ingroup_name) { $new_gid = getgrnam($ingroup_name); }
388 else { die _("Internal error"); }
389 }
390 else {
391 if (defined($new_gid)) { $ingroup_name = getgrgid($new_gid); }
392 elsif ($ingroup_name) { $new_gid = getgrnam($ingroup_name); }
393 elsif ($make_group_also){ $new_gid=$new_uid; $ingroup_name=$new_name; }
394 else { die _("Internal error"); }
395 }
396
397 &stopnscd();
398 if ($make_group_also) {
399 printf _("Adding new group %s (%s).\n"),$new_name,$new_gid if $verbose;
400 $undogroup = $new_name;
401 &systemcall('groupadd', '-g', $new_gid, $new_name);
402 }
403
404 printf _("Adding new user %s (%s) with group %s.\n"),$new_name,$new_uid,$ingroup_name
405 if $verbose;
406 $home_dir = $special_home || &homedir($new_name, $ingroup_name);
407 $undouser = $new_name;
408 &systemcall('useradd', '-d', $home_dir, '-g', $ingroup_name, '-s',
409 $config{dshell}, '-u', $new_uid, $new_name);
410 &startnscd();
411
412 if (-e $home_dir) {
413 printf _("Home directory %s already exists. Not copying from %s\n"),
414 $home_dir,$config{skel} if $verbose;
415 } elsif ($no_create_home) {
416 print "Not creating $home_dir.\n" if $verbose;
417 }
418 else {
419 printf _("Creating home directory %s.\n"),$home_dir if $verbose;
420 $undohome = $home_dir;
421 &mktree($home_dir) || &cleanup("Couldn't create $home_dir: $!.\n");
422 chown($new_uid, $new_gid, $home_dir)
423 || &cleanup("chown $new_uid:$new_gid $home_dir: $!\n");
424 $dir_mode = $make_group_also ? 02755 : 0755;
425 chmod ($dir_mode, $home_dir) ||
426 &cleanup("chmod $dir_mode $home_dir: $!\n");
427
428 if ($config{"skel"}) {
429 printf _("Copying files from %s\n"),$config{skel} if $verbose;
430 open(FIND, "cd $config{skel}; find . -print |")
431 || &cleanup("fork for find: $!\n");
432 while (<FIND>) {
433 chop;
434 next if ($_ eq ".");
435 &copy_to_dir($config{"skel"}, $_, $home_dir, $new_uid,
436 $new_gid, $make_group_also);
437 }
438 }
439 }
440
441 if ($ask_passwd) {
442 &systemcall('passwd', $new_name);
443 }
444
445 if ($new_gecos) {
446 &systemcall('chfn', '-f', $new_gecos, $new_name);
447 }
448 else {
449 for (;;) {
450 &systemcall('chfn', $new_name);
451 print _("Is the information correct? [y/n] ");
452 chop ($answer=<STDIN>);
453 last if ($answer =~ /^y/i);
454 }
455 }
456
457 if ($config{"quotauser"}) {
458 printf _("Setting quota from %s.\n"),$config{quotauser};
459 &systemcall('edquota', '-p', $config{quotauser}, $new_name);
460 }
461
462 &systemcall('/usr/local/sbin/adduser.local', $new_name, $new_uid,
463 $new_gid, $home_dir) if (-x "/usr/local/sbin/adduser.local");
464
465 exit 0;
466 }
467
468
469 # calculate home directory
470 sub homedir {
471 my $dir = $config{"dhome"};
472 $dir .= '/' . $_[1] if ($config{"grouphomes"} =~ /yes/i);
473 $dir .= '/' . substr($_[0],0,1) if ($config{"letterhomes"} =~ /yes/i);
474 $dir .= '/' . $_[0];
475 }
476
477
478 # create a directory and all leading directories
479 sub mktree {
480 my($tree) = @_;
481 my($done, @path);
482 my $default_dir_mode = 0755;
483
484 $tree =~ s:^/*(.*)/*$:$1:; # chop off leading & trailing slashes
485 @path = split(/\//, $tree);
486
487 $done = "";
488 while (@path) {
489 $done .= '/' . shift(@path);
490 -d $done || mkdir($done, $default_dir_mode) || return 0;
491 }
492 1;
493 }
494
495
496 sub check_user_group() {
497 dief(_("The user `%s\' already exists.\n"),$new_name) if (getpwnam($new_name));
498 dief(_("The UID `%s' already exists.\n"),$new_uid)
499 if (defined($new_uid) && getpwuid($new_uid));
500 if ($make_group_also) {
501 dief(_("The group `%s' already exists.\n"),$new_name)
502 if (getgrnam($new_name));
503 dief(_("The GID `%s' already exists.\n"),$new_uid)
504 if (defined($new_uid) && getgrgid($new_uid));
505 }
506 else {
507 dief(_("The group `%s' doesn't exist.\n"),$ingroup_name)
508 if ($ingroup_name && !getgrnam($ingroup_name));
509 dief(_("The GID `%s' doesn't exist.\n"),$new_gid)
510 if (defined($new_gid) && !getgrgid($new_gid));
511 }
512 }
513
514
515 # copy files, directories, symlinks
516 sub copy_to_dir {
517 my($fromdir, $file, $todir, $newu, $newg, $sgiddir) = @_;
518
519 if (-l "$fromdir/$file") {
520 symlink(readlink("$fromdir/$file"), "$todir/$file")
521 || &cleanup("symlink: $!\n");
522 return;
523 }
524 elsif (-f "$fromdir/$file") {
525 open (FILE, "$fromdir/$file") || &cleanup("open $fromdir/$file: $!");
526 open (NEWFILE, ">$todir/$file") || &cleanup("open >$todir/$file: $!");
527
528 (print NEWFILE <FILE>) || &cleanup("print $todir/$file: $!");
529 close FILE;
530 close(NEWFILE) || &cleanup("close $todir/$file ");
531
532 }
533 elsif (-d "$fromdir/$file") {
534 mkdir("$todir/$file", 700) || &cleanup("mkdir: $!");
535 }
536 else {
537 &cleanup("Can't deal with $fromdir/$file. "
538 ."Not a dir, file, or symlink.\n");
539 }
540
541 chown($newu, $newg, "$todir/$file")
542 || &cleanup("chown $newu:$newg $todir/$file: $!\n");
543 $perm = (stat("$fromdir/$file"))[2] & 07777;
544 $perm |= 02000 if (-d "$fromdir/$file" && ($perm & 010) && $sgiddir);
545 chmod($perm, "$todir/$file") || &cleanup("chmod $todir/$file: $!\n");
546 }
547
548
549 # is name ok?
550 sub checkname {
551 my ($name) = @_;
552 if ($allow_badname && $name !~ /^[A-Za-z_][-_A-Za-z0-9]*$/) {
553 print STDERR
554 "$0: ",_("To avoid problems, the username should consist of a letter or
555 underscore followed by letters, digits, underscores, and dashes.\n");
556 exit 1;
557 }
558 elsif ($name !~ /^[a-z][a-z0-9]*$/) {
559 if (!$allow_badname) {
560 print STDERR
561 "$0: ",_("Please enter a username consisting of a lower case letter
562 followed by lower case letters and numbers. Use the `--force-badname'
563 option to allow underscores, dashes, and uppercase.\n");
564 exit 1;
565 }
566 print _("Allowing use of questionable username.\n") if ($verbose);
567 }
568 }
569
570
571 # return the smallest X such that
572 # $min <= X <= $max, and X is not an element of @ids
573 # or -1 if no such X
574 sub first_avail_id {
575 my ($min, $max, @ids) = @_;
576 @ids = sort {$a <=> $b} @ids;
577 printf _("Selecting from %s %s (%s).\n"),$min,$max,join(",",@ids) if $debugging;
578
579 while ($min <= $max) {
580 return $min if ($min < $ids[0] || @ids==0);
581 shift @ids if ($min > $ids[0]);
582 $min++ if ($min == $ids[0]);
583 }
584
585 -1; # nothing available
586 }
587
588
589 # return an array containing all the GIDs
590 sub get_current_gids {
591 my(@gids, $gid);
592 setgrent;
593 push @gids, $gid while defined($gid = (getgrent)[2]);
594 endgrent;
595 @gids;
596 }
597
598
599 # return an array containing all the UIDs
600 sub get_current_uids {
601 my(@uids, $uid);
602 setpwent;
603 push @uids, $uid while defined($uid = (getpwent)[2]);
604 endpwent;
605 @uids;
606 }
607
608
609 # return a user's groups
610 sub get_users_groups {
611 my($user) = @_;
612 my($name,$members,@groups);
613 setgrent;
614 while (($name,$members) = (getgrent)[0,3]) {
615 for (split(/ /, $members)) {
616 if ($user eq $_) {
617 push @groups, $name;
618 last;
619 }
620 }
621 }
622 endgrent;
623 @groups;
624 }
625
626
627 # user is member of group?
628 sub user_is_member {
629 my($user, $group) = @_;
630 for (split(/ /, (getgrnam($group))[3])) {
631 return 1 if ($user eq $_);
632 }
633 0;
634 }
635
636
637 # parse the configuration file
638 sub read_config {
639 my ($conf_file) = @_;
640 my ($var, $lcvar, $val);
641
642 if (! -f $conf_file) {
643 printf _("%s: %s doesn't exist. Using defaults.\n"),$0,$conf_file if $verbose;
644 return;
645 }
646
647 open (CONF, $conf_file) || dief("%s: %s\n",$conf_file,$!);
648 while (<CONF>) {
649 chomp;
650 next if /^#/ || /^\s*$/;
651
652 if ((($var, $val) = /^\s*(\S+)\s*=\s*(.*)/) != 2) {
653 warnf(_("Couldn't parse %s:%s.\n"),$conf_file,$.);
654 next;
655 }
656 $lcvar = lc $var;
657 if (!defined($config{$lcvar})) {
658 warnf(_("Unknown variable `%s' at %s:%s.\n"),$var,$conf_file,$.);
659 next;
660 }
661
662 $val =~ s/^"(.*)"$/$1/;
663 $val =~ s/^'(.*)'$/$1/;
664
665 $config{$lcvar} = $val;
666 }
667
668 close CONF || die "$!";
669 }
670
671
672 sub systemcall {
673 my $c = join(' ', @_);
674 print "$c\n" if $debugging;
675 if (system(@_)) {
676 &cleanup("$0: `$c' returned error code " . ($?>>8) . ". Aborting.\n")
677 if ($?>>8);
678 &cleanup("$0: `$c' exited from signal " . ($?&255) . ". Aborting.\n");
679 }
680 }
681
682
683 sub cleanup {
684 print "@{_}Cleaning up.\n";
685 if ($undohome) {
686 printf _("Removing directory `%s'\n"),$undohome;
687 system('rm', '-rf', $undohome);
688 }
689 if ($undouser) {
690 printf _("Removing user `%s'.\n"),$undouser;
691 system('userdel', $undouser);
692 }
693 if ($undogroup) {
694 printf _("Removing group `%s'.\n"),$undogroup;
695 system('groupdel', $undogroup);
696 }
697 &startnscd() if $undonscd;
698 exit 1;
699 }
700
701
702 sub stopnscd {
703 $undonscd = 1;
704 systemcall($nscdinit, "stop") if (-f $nscdinit);
705 }
706
707
708 sub startnscd {
709 systemcall($nscdinit, "start") if (-f $nscdinit);
710 $undonscd = 0;
711 }
712
713
714 sub handler {
715 my($sig) = @_;
716 &cleanup("Caught a SIG$sig.\n");
717 }
718
719
720 sub version {
721 print "$0: add a user or group to the system. Version VERSION
722 Copyright (C) 1997, 1998, 1999 Guy Maor <maor\@debian.org>
723 Copyright (C) 1995 Ian Murdock <imurdock\@gnu.ai.mit.edu>,
724 Ted Hajek <tedhajek\@boombox.micro.umn.edu>,
725
726 This program is free software; you can redistribute it and/or modify
727 it under the terms of the GNU General Public License as published by
728 the Free Software Foundation; either version 2 of the License, or (at
729 your option) any later version.
730
731 This program is distributed in the hope that it will be useful, but
732 WITHOUT ANY WARRANTY; without even the implied warranty of
733 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
734 General Public License, /usr/doc/copyright/GPL, for more details.
735 ";
736 }
737
738 sub usage {
739 printf _(
740 "adduser [--home DIR] [--no-create-home] [--uid ID] [--gecos GECOS]
741 [--ingroup GROUP | --gid ID] [--disabled-password] user
742 Add a normal user
743
744 adduser --system [--home DIR] [--no-create-home] [--uid ID] [--gecos GECOS]
745 [--group | --ingroup GROUP | --gid ID] [--disabled-password]
746 user
747 Add a system user
748
749 adduser --group [--gid ID] group
750 addgroup [--gid ID] group
751 Add a system group
752
753 adduser user group
754 Add an existing user to an existing group
755
756 Global configuration is in the file %s.
757 Other options are [--quiet] [--force-badname] [--help] [--version] [--conf
758 FILE].
759 "),$defaults;
760 }
761
762
763 sub _ {
764 return gettext("@_");
765 }
766
767
768 sub dief {
769 my ($form,@argu)=@_;
770 printf STDERR "$0: $form",@argu;
771 exit 1;
772 }
773
774 sub warnf {
775 my ($form,@argu)=@_;
776 printf STDERR "$0: $form",@argu;
777 }
778
779
780 # Local Variables:
781 # mode:cperl
782 # cperl-indent-level:4
783 # End:

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.5