Debian Codegraph @

contrib/ftpasswd

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env perl
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2000-2021 TJ Saunders <[email protected]>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 #
19 # Based on MacGuyver's genuser.pl script, this script generates password
20 # files suitable for use with proftpd's AuthUserFile directive, in passwd(5)
21 # format, or AuthGroupFile, in group(5) format. The idea is somewhat similar
22 # to Apache's htpasswd program.
23 # ---------------------------------------------------------------------------
24
25 use strict;
26
27 use Fcntl qw(:flock);
28 use File::Basename qw(basename);
29 use Getopt::Long;
30
31 # turn off auto abbreviation
32 $Getopt::Long::auto_abbrev = 0;
33
34 my $program = basename($0);
35 my $default_passwd_file = "./ftpd.passwd";
36 my $default_group_file = "./ftpd.group";
37 my $shell_file = "/etc/shells";
38 my $default_cracklib_dict = "/usr/lib/cracklib_dict";
39 my $cracklib_dict;
40 my $output_file;
41 my $version = "1.3.0";
42
43 my @data;
44
45 my %opts = ();
46 GetOptions(\%opts,
47 'add-member=s',
48 'change-home',
49 'change-password',
50 'delete-group',
51 'delete-member=s',
52 'delete-user',
53 'des',
54 'enable-group-passwd',
55 'file=s',
56 'F|force',
57 'gecos=s',
58 'gid=n',
59 'group',
60 'hash',
61 'h|help',
62 'home=s',
63 'l|lock',
64 'md5',
65 'm|member=s@',
66 'name=s',
67 'not-previous-password',
68 'not-system-password',
69 'passwd',
70 'sha256',
71 'sha512',
72 'shell=s',
73 'stdin',
74 'uid=n',
75 'u|unlock',
76 'use-cracklib:s',
77 'version',
78 );
79
80 usage() if (defined($opts{'h'}));
81
82 version() if (defined($opts{'version'}));
83
84 # Per Bug#4171, check if we are on a Linux system, AND it has the
85 # /proc/sys/crypto/fips_enabled file, AND that entry says that FIPS mode is
86 # enabled. If these conditions are met, then neither DES nor MD5 will work.
87 #
88 # If either --des or --md5 are specified, OR if no hash is specified, we
89 # need to check.
90 if ((defined($opts{'des'}) || defined($opts{'md5'}))) {
91 if (open(my $fh, "< /proc/sys/crypto/fips_enabled")) {
92 my $fips_enabled = <$fh>;
93 close($fh);
94
95 chomp($fips_enabled);
96 if ($fips_enabled) {
97 die "$program: FIPS mode enabled on your system (see /proc/sys/crypto/fips_enabled), thus --des and --md5 will not be supported. Use --sha256 or --sha512.\n"
98 }
99 }
100 }
101
102 # check if "use-cracklib" was given as an option, and whether a path
103 # to other dictionary files was given.
104 if (defined($opts{'use-cracklib'})) {
105
106 # make sure that Crypt::Cracklib is installed before trying to use
107 # it later
108 eval { require Crypt::Cracklib };
109 die "$program: --use-cracklib requires Crypt::Cracklib to be installed\n" if $@;
110
111 if ($opts{'use-cracklib'} ne "") {
112 $cracklib_dict = $opts{'use-cracklib'};
113
114 } else {
115 $cracklib_dict = $default_cracklib_dict;
116 }
117 }
118
119 # Make sure that the given --home path exists, and is a directory.
120 if (exists($opts{'home'})) {
121 my $path = $opts{'home'};
122 unless (-e $path) {
123 die "$program: --home $path does not exist\n";
124 }
125
126 unless (-d $path) {
127 die "$program: --home $path is not a directory\n";
128 }
129 }
130
131 # make sure that both passwd and group modes haven't been simultaneously
132 # requested
133 if ((exists($opts{'passwd'}) && exists($opts{'group'})) ||
134 (exists($opts{'passwd'}) && exists($opts{'hash'})) ||
135 (exists($opts{'group'}) && exists($opts{'hash'}))) {
136 die "$program: please use *one*: --passwd, --group, or --hash\n";
137
138 } elsif (defined($opts{'passwd'})) {
139
140 # determine to which file to write the passwd entry
141 if (defined($opts{'file'})) {
142 $output_file = $opts{'file'};
143 print STDOUT "$program: using alternate file: $output_file\n"
144
145 } else {
146 $output_file = $default_passwd_file;
147 }
148
149 # make sure that the required arguments are present
150 die "$program: --passwd: missing required argument: --name\n"
151 unless (defined($opts{'name'}));
152
153 # check for and handle the --delete-user option.
154 if (defined($opts{'delete-user'})) {
155 open_output_file();
156
157 my ($pass, $uid, $gid, $gecos, $home, $shell) = find_passwd_entry(name =>
158 $opts{'name'});
159
160 handle_passwd_entry(name => $opts{'name'}, uid => $uid, gid => $gid,
161 gecos => $gecos, home => $home, shell => $shell,
162 delete_user => $opts{'delete-user'});
163
164 close_output_file();
165
166 # done
167 exit 0;
168 }
169
170 # check for and handle the --lock option.
171 if (defined($opts{'l'})) {
172 open_output_file();
173
174 my ($pass, $uid, $gid, $gecos, $home, $shell) = find_passwd_entry(name =>
175 $opts{'name'});
176
177 my $new_passwd = $pass;
178
179 # If this password is already "locked", leave it alone
180 if ($new_passwd !~ /^!/) {
181 $new_passwd = '!' . $new_passwd;
182 }
183
184 handle_passwd_entry(name => $opts{'name'}, uid => $uid, gid => $gid,
185 gecos => $gecos, home => $home, shell => $shell,
186 new_passwd => $new_passwd);
187
188 close_output_file();
189
190 # done
191 exit 0;
192 }
193
194 # check for and handle the --unlock option.
195 if (defined($opts{'u'})) {
196 open_output_file();
197
198 my ($pass, $uid, $gid, $gecos, $home, $shell) = find_passwd_entry(name =>
199 $opts{'name'});
200
201 my $new_passwd = $pass;
202 $new_passwd =~ s/^!+//;
203
204 handle_passwd_entry(name => $opts{'name'}, uid => $uid, gid => $gid,
205 gecos => $gecos, home => $home, shell => $shell,
206 new_passwd => $new_passwd);
207
208 close_output_file();
209
210 # done
211 exit 0;
212 }
213
214 # now check for the --change-password option. If present, lookup
215 # the given name in the password file, and reuse all the information
216 # except for the password
217 if (defined($opts{'change-password'})) {
218 open_output_file();
219
220 my ($pass, $uid, $gid, $gecos, $home, $shell) = find_passwd_entry(name =>
221 $opts{'name'});
222
223 handle_passwd_entry(name => $opts{'name'}, uid => $uid, gid => $gid,
224 gecos => $gecos, home => $home, shell => $shell);
225
226 close_output_file();
227
228 # done
229 exit 0;
230 }
231
232 # Now check for the --change-home option. If present, lookup the given name
233 # in the password file, and reuse all the information except for the home.
234 if (defined($opts{'change-home'})) {
235 if (!defined($opts{'home'})) {
236 die "$program: --change-home requires use of --home\n";
237 }
238
239 open_output_file();
240
241 my ($pass, $uid, $gid, $gecos, $home, $shell) = find_passwd_entry(name =>
242 $opts{'name'});
243
244 # We trick this function into not prompting for a password by acting
245 # as if we are handling a new password.
246 handle_passwd_entry(name => $opts{'name'}, uid => $uid, gid => $gid,
247 gecos => $gecos, home => $opts{'home'}, shell => $shell,
248 new_passwd => $pass);
249
250 close_output_file();
251
252 # done
253 exit 0;
254 }
255
256 # check for the --not-system-password option. If present, make sure that
257 # a) the script is running with root privs, and b) perl on the system is
258 # such that getpwnam() will return the system password
259 if (defined($opts{'not-system-password'})) {
260 die "$program: must be user root for system password check\n"
261 unless ($> == 0);
262 }
263
264 die "$program: --passwd: missing required argument: --home\n"
265 unless (defined($opts{'home'}));
266
267 die "$program: --passwd: missing required argument: --shell\n"
268 unless (defined($opts{'shell'}));
269
270 die "$program: --passwd: missing required argument: --uid\n"
271 unless (defined($opts{'uid'}));
272
273 # As per Flying Hamster's suggestion, have $opts{'gid'} default to --uid
274 # if none are specified on the command-line via --gid
275 unless (defined($opts{'gid'})) {
276 $opts{'gid'} = $opts{'uid'};
277 warn "$program: --passwd: missing --gid argument: default gid set to uid\n";
278 }
279
280 open_output_file();
281
282 handle_passwd_entry(name => $opts{'name'}, uid => $opts{'uid'},
283 gid => $opts{'gid'}, gecos => $opts{'gecos'}, home => $opts{'home'},
284 shell => $opts{'shell'}, delete_user => $opts{'delete-user'});
285
286 close_output_file();
287
288 # NOTE: if this process is not running as root, then the file generated
289 # is not owned by root. Issue a warning reminding the user to make the
290 # generated file mode 0400, owned by root, before using it.
291
292 } elsif (defined($opts{'group'})) {
293
294 # determine to which file to write the group entry
295 if (defined($opts{'file'})) {
296 $output_file = $opts{'file'};
297 print STDOUT "$program: using alternate file: $output_file\n";
298
299 } else {
300 $output_file = $default_group_file;
301 }
302
303 # check for and handle the --delete-group option.
304 if (defined($opts{'delete-group'})) {
305 open_output_file();
306
307 handle_group_entry(
308 name => $opts{'name'},
309 delete_group => $opts{'delete-group'}
310 );
311
312 close_output_file();
313
314 # done
315 exit 0;
316 }
317
318 # make sure the required options are present
319 if (!defined($opts{'add-member'}) &&
320 !defined($opts{'delete-member'})) {
321 die "$program: --group: missing required argument: --gid\n"
322 unless (defined($opts{'gid'}));
323 }
324
325 die "$program: --group: missing required argument: --name\n"
326 unless (defined($opts{'name'}));
327
328 open_output_file();
329
330 handle_group_entry(
331 gid => $opts{'gid'},
332 members => $opts{'m'},
333 name => $opts{'name'},
334 add_user => $opts{'add-member'},
335 delete_group => $opts{'delete-group'},
336 delete_user => $opts{'delete-member'}
337 );
338
339 close_output_file();
340
341 } elsif (defined($opts{'hash'})) {
342 print STDOUT "$program: ", get_passwd(), "\n";
343
344 } else {
345 die "$program: missing required --passwd or --group\n$program: use $program --help for details on usage\n\n";
346 }
347
348 # done
349 exit 0;
350
351 # ----------------------------------------------------------------------------
352 sub check_shell {
353 my %args = @_;
354
355 my $shell = $args{'shell'};
356 my $result = 0;
357
358 # check the given shell against the list in /etc/shells. If not present
359 # there, issue a message recognizing this, and suggesting that
360 # RequireValidShell be set to off, and that any necessary PAM modules be
361 # adjusted.
362
363 unless (open(SHELLS, "< $shell_file")) {
364 warn "$program: unable to open $shell_file: $!\n";
365 warn "$program: skipping check of $shell_file\n";
366 return;
367 }
368
369 while(my $line = <SHELLS>) {
370 chomp($line);
371
372 if ($line eq $shell) {
373 $result = 1;
374 last;
375 }
376 }
377
378 close(SHELLS);
379
380 unless ($result) {
381 print STDOUT "\n$program: $shell is not among the valid system shells. Use of\n";
382 print STDOUT "$program: \"RequireValidShell off\" may be required, and the PAM\n";
383 print STDOUT "$program: module configuration may need to be adjusted.\n\n";
384 }
385
386 return $result;
387 }
388
389 # ----------------------------------------------------------------------------
390 sub close_output_file {
391 my %args = @_;
392
393 if (open(my $fh, "> $output_file")) {
394 if (flock($fh, LOCK_EX|LOCK_NB)) {
395 # flush the data to the file
396 foreach my $line (@data) {
397 print $fh "$line\n";
398 }
399
400 # set the permissions appropriately, ie 0440, before closing the file
401 unless (chmod(0440, $output_file)) {
402 flock($fh, LOCK_UN);
403 die("$program: unable to set permissions on $output_file: $!");
404 }
405
406 flock($fh, LOCK_UN);
407
408 unless (close($fh)) {
409 die("$program: unable to close $output_file: $!\n");
410 }
411
412 } else {
413 close($fh);
414 die("$program: unable to write $output_file: Locked (in use) by another process\n");
415 }
416
417 } else {
418 die("$program: unable to open $output_file: $!\n");
419 }
420 }
421
422 # ----------------------------------------------------------------------------
423 sub find_passwd_entry {
424 my %args = @_;
425
426 my $name = $args{'name'};
427 my ($pass, $uid, $gid, $gecos, $home, $shell);
428 my $found = 0;
429
430 # given a name, find the corresponding entry in the passwd file
431 foreach my $line (@data) {
432 next unless $line =~ /^$name:/;
433
434 my @fields = split(':', $line);
435
436 $pass = $fields[1];
437 $uid = $fields[2];
438 $gid = $fields[3];
439 $gecos = $fields[4];
440 $home = $fields[5];
441 $shell = $fields[6];
442
443 $found = 1;
444
445 last;
446 }
447
448 unless ($found) {
449 print STDOUT "$program: error: no such user $name in $output_file\n";
450
451 # Restore the file permissions.
452 unless (chmod(0440, $output_file)) {
453 print STDERR "$program: unable to set permissions on $output_file: $!";
454 }
455
456 exit 1;
457 }
458
459 return ($pass, $uid, $gid, $gecos, $home, $shell);
460 }
461
462 # ----------------------------------------------------------------------------
463 sub get_salt {
464 my $salt;
465
466 # The determination of with encryption algorithm to use is done via
467 # the salt. The format and nature of the salt is how crypt(3) knows
468 # how to do its thing. By default, generate a salt that triggers MD5.
469
470 if (defined($opts{'des'})) {
471 # DES salt
472 $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
473
474 } elsif (defined($opts{'md5'})) {
475 # MD5 salt (16 characters)
476 $salt = join '', (0..9, 'A'..'Z', 'a'..'z')
477 [rand 62, rand 62, rand 62, rand 62,
478 rand 62, rand 62, rand 62, rand 62,
479 rand 62, rand 62, rand 62, rand 62,
480 rand 62, rand 62, rand 62, rand 62];
481 $salt = '$1$' . $salt;
482
483 } elsif (defined($opts{'sha512'})) {
484 # SHA-512 salt (16 characters)
485 $salt = join '', (0..9, 'A'..'Z', 'a'..'z')
486 [rand 62, rand 62, rand 62, rand 62,
487 rand 62, rand 62, rand 62, rand 62,
488 rand 62, rand 62, rand 62, rand 62,
489 rand 62, rand 62, rand 62, rand 62];
490 $salt = '$6$' . $salt;
491
492 } else {
493 # SHA-256 salt (16 characters)
494 $salt = join '', (0..9, 'A'..'Z', 'a'..'z')
495 [rand 62, rand 62, rand 62, rand 62,
496 rand 62, rand 62, rand 62, rand 62,
497 rand 62, rand 62, rand 62, rand 62,
498 rand 62, rand 62, rand 62, rand 62];
499 $salt = '$5$' . $salt;
500 }
501
502 return $salt;
503 }
504
505 # ----------------------------------------------------------------------------
506 sub get_passwd {
507 my %args = @_;
508 my $name = $args{'name'};
509 my ($passwd, $passwd2);
510
511 # If using a DES salt, print an informative message about the 8 character
512 # limit of relevant password characters.
513
514 if (defined($opts{'des'}) && !defined($opts{'stdin'})) {
515 print STDOUT "\nPlease be aware that only the first 8 characters of a DES password are\nrelevant. Use the --md5, --sha256, or --sha512 options as they do not have\nthis limitation.\n";
516 }
517
518 if (defined($opts{'stdin'})) {
519
520 # simply read in the password from stdin, as from a script
521 chomp($passwd = <STDIN>);
522
523 } else {
524
525 # Install a SIGINT handler, for cases where Ctrl-C may be used to abort
526 # the prompt.
527 $SIG{INT} = sub {
528 # Restore the file permissions.
529 unless (chmod(0440, $output_file)) {
530 print STDERR "$program: unable to set permissions on $output_file: $!";
531 }
532
533 # Restore the terminal echo behavior, too.
534 system "stty echo";
535
536 exit 1;
537 };
538
539 # Prompt for the password to be used
540 system "stty -echo";
541 print STDOUT "\nPassword: ";
542
543 # Open the tty for reading (is this portable?)
544 open(TTY, "/dev/tty") or die "$program: unable to open /dev/tty: $!\n";
545 chomp($passwd = <TTY>);
546 print STDOUT "\n";
547 system "stty echo";
548
549 # Prompt again, to make sure the user typed in the password correctly
550 system "stty -echo";
551 print STDOUT "Re-type password: ";
552 chomp($passwd2 = <TTY>);
553 print STDOUT "\n\n";
554 system "stty echo";
555 close(TTY);
556
557 # Restore default SIGINT handling
558 $SIG{INT} = 'DEFAULT';
559
560 if ($passwd2 ne $passwd) {
561 print STDOUT "Passwords do not match. Please try again.\n";
562 return get_passwd(name => $name);
563 }
564 }
565
566 if (defined($name) && defined($opts{'change-password'})) {
567
568 # retrieve the user's current password from the file and compare
569 my ($curpasswd, @junk) = find_passwd_entry(name => $name);
570
571 my $hash = crypt($passwd, $curpasswd);
572
573 if ($hash eq $curpasswd) {
574 if (defined($opts{'stdin'})) {
575 # cannot prompt again if automated. Simply print an error message
576 # and exit.
577 print STDOUT "$program: error: password matches current password\n";
578
579 # Restore the file permissions.
580 unless (chmod(0440, $output_file)) {
581 print STDERR "$program: unable to set permissions on $output_file: $!";
582 }
583
584 exit 2;
585
586 } else {
587 print STDOUT "Please use a password that is different from your current password.\n";
588 return get_passwd(name => $name);
589 }
590 }
591 }
592
593 if (defined($name) && defined($opts{'not-previous-password'})) {
594 # retrieve the user's current passwd and compare
595 my ($currpasswd) = find_passwd_entry(name => $name);
596
597 my $hash = crypt($passwd, $currpasswd);
598
599 if (($hash && $currpasswd) && $hash eq $currpasswd) {
600 if (defined($opts{'stdin'})) {
601
602 # cannot prompt again if automated. Simply print an error message
603 # and exit.
604 print STDOUT "$program: error: password matches previous password\n";
605
606 # Restore the file permissions.
607 unless (chmod(0440, $output_file)) {
608 print STDERR "$program: unable to set permissions on $output_file: $!";
609 }
610
611 exit 4;
612
613 } else {
614 print STDOUT "Please use a password that is different from your previous password.\n";
615 return get_passwd(name => $name);
616 }
617 }
618 }
619
620 if (defined($name) && defined($opts{'not-system-password'})) {
621 # retrieve the user's system passwd (from /etc/shadow) and compare
622 my $syspasswd = get_syspasswd(user => $name);
623
624 my $hash = crypt($passwd, $syspasswd);
625
626 if (($hash && $syspasswd) && $hash eq $syspasswd) {
627 if (defined($opts{'stdin'})) {
628 # Cannot prompt again if automated. Simply print an error message
629 # and exit.
630 print STDOUT "$program: error: password matches system password\n";
631
632 # Restore the file permissions.
633 unless (chmod(0440, $output_file)) {
634 print STDERR "$program: unable to set permissions on $output_file: $!";
635 }
636
637 exit 4;
638
639 } else {
640 print STDOUT "Please use a password that is different from your system password.\n";
641 return get_passwd(name => $name);
642 }
643 }
644 }
645
646 return "" if ($args{'allow_blank'} and $passwd eq "");
647
648 # check for BAD passwords, BLANK passwords, etc, if requested
649 if (defined($opts{'use-cracklib'})) {
650 require Crypt::Cracklib;
651 if (!Crypt::Cracklib::check($passwd, $cracklib_dict)) {
652 print STDOUT "Bad password: ", Crypt::Cracklib::fascist_check($passwd,
653 $cracklib_dict), "\n";
654 return get_passwd(name => $name);
655 }
656 }
657
658 my $salt = get_salt();
659
660 my $hash = crypt($passwd, $salt);
661
662 # Check that the crypt() implementation properly supports use of the MD5
663 # (or other non-DES algorithm), if specified.
664 if (!defined($opts{'des'})) {
665 if (defined($opts{'md5'})) {
666 # if the first three characters of the hash are not "$1$", the crypt()
667 # implementation doesn't support MD5. Some crypt()s will happily use
668 # "$1" as a salt even though this is not a valid DES salt. Humf.
669 #
670 # Perl doesn't treat strings as arrays of characters, so extracting the
671 # first three characters is a little more convoluted (I'm accustomed to
672 # C's strncmp(3) for this now).
673
674 my @string = split('', $hash);
675 my $prefix = $string[0] . $string[1] . $string[2];
676
677 if ($prefix ne '$1$') {
678 print STDOUT "You requested MD5 passwords but your system does not support it. Defaulting to DES passwords.\n\n";
679 }
680
681 } elsif (defined($opts{'sha256'})) {
682 # if the first three characters of the hash are not "$5$", the crypt()
683 # implementation doesn't support SHA-256. Some crypt()s will happily use
684 # "$5" as a salt even though this is not a valid DES salt. Humf.
685 #
686 # Perl doesn't treat strings as arrays of characters, so extracting the
687 # first three characters is a little more convoluted (I'm accustomed to
688 # C's strncmp(3) for this now).
689
690 my @string = split('', $hash);
691 my $prefix = $string[0] . $string[1] . $string[2];
692
693 if ($prefix ne '$5$') {
694 print STDOUT "You requested SHA-256 passwords but your system does not support it. Defaulting to DES passwords.\n\n";
695 }
696
697 } elsif (defined($opts{'sha512'})) {
698 # if the first three characters of the hash are not "$6$", the crypt()
699 # implementation doesn't support SHA-512. Some crypt()s will happily use
700 # "$6" as a salt even though this is not a valid DES salt. Humf.
701 #
702 # Perl doesn't treat strings as arrays of characters, so extracting the
703 # first three characters is a little more convoluted (I'm accustomed to
704 # C's strncmp(3) for this now).
705
706 my @string = split('', $hash);
707 my $prefix = $string[0] . $string[1] . $string[2];
708
709 if ($prefix ne '$6$') {
710 print STDOUT "You requested SHA-512 passwords but your system does not support it. Defaulting to DES passwords.\n\n";
711 }
712 }
713 }
714
715 return $hash;
716 }
717
718 # ----------------------------------------------------------------------------
719 sub get_syspasswd {
720 my %args = @_;
721
722 my $user = $args{'user'};
723
724 # test the shadow password support on this system. Some systems, such
725 # as the BSDs, use "transparent shadowing", where the real passwd will
726 # be returned via getpwnam() only if the process has root privs (effective
727 # UID of zero). That check has already been performed. However, other
728 # systems still may not return the password via getpwnam() (such as Linux).
729 # These other systems use a shadow password library of functions, and require
730 # other work to retrieve the password. On these systems, the retrieved
731 # password will be "x".
732
733 my $syspasswd = (getpwnam($user))[1];
734
735 if ($syspasswd eq "" || $syspasswd eq "x") {
736
737 # do the retrieval the hard way: open up /etc/shadow and iterate
738 # through each line. Yuck. *sigh*. Thanks to Micah Anderson
739 # for working out this issue.
740
741 open(SHADOW, "< /etc/shadow") or
742 die "$program: unable to access shadow file: $!\n";
743
744 while (chomp(my $line = <SHADOW>)) {
745 next unless $line =~ /^$user/;
746 $syspasswd = (split(':', $line))[1];
747 last;
748 }
749 close(SHADOW);
750
751 # if the password is still "x", you have problems
752 if ($syspasswd eq "x") {
753 die "$program: unable to retrieve shadow password.\nContact your system
754 administrator.\n";
755 }
756 }
757
758 return $syspasswd;
759 }
760
761 # ----------------------------------------------------------------------------
762 sub handle_group_entry {
763 my %args = @_;
764
765 my $gid = $args{'gid'};
766 my $name = $args{'name'};
767 my $delete_group = $args{'delete_group'};
768 my $delete_user = $args{'delete_user'};
769 my $add_user = $args{'add_user'};
770 my $passwd;
771
772 my $members = "";
773 $members = join(',', @{$args{'members'}}) if (defined($args{'members'}));
774
775 # check to see whether we should update the fields for this group (because
776 # it already exists), or to create a new entry
777
778 my $found = 0;
779 my $index = 0;
780 for ($index = 0; $index <= $#data; $index++) {
781 my @entry = split(':', $data[$index]);
782
783 if ($name eq $entry[0]) {
784 $found = 1;
785
786 # If we have not been given an explicit password, reuse the existing one.
787 $passwd = $entry[1] unless $passwd;
788
789 # If we have not been given an explicit GID, reuse the existing one.
790 $gid = $entry[2] unless $gid;
791
792 # If we have not been given explicit members, reuse the existing ones.
793 $members = $entry[3] if $members eq '';
794
795 last;
796 }
797 }
798
799 unless ($found) {
800 print STDOUT "$program: creating group entry for group $name\n";
801
802 } else {
803 print STDOUT "$program: updating group entry for group $name\n";
804 }
805
806 # if present, add the members given to the group. If none, just leave that
807 # field blank
808
809 # prompt for the group password, if requested
810 if (defined($opts{'enable-group-passwd'})) {
811 $passwd = get_passwd(name => $name, allow_blank => 1);
812
813 } else {
814 $passwd = "x";
815 }
816
817 # remove the entry to be updated
818 splice(@data, $index, 1);
819
820 if ($delete_group) {
821 print STDOUT "$program: entry deleted\n";
822 return;
823 }
824
825 if ($delete_user) {
826 $members =~ s/$delete_user//g;
827 $members =~ s/,,/,/g;
828 $members =~ s/^,//g;
829 $members =~ s/,$//g;
830 }
831
832 if ($add_user) {
833 if (length($members) > 0) {
834 $members .= ",$add_user";
835
836 } else {
837 $members = $add_user;
838 }
839 }
840
841 # Ensure that we have a sorted list of unique members
842 my $uniq_members = { map { $_ => 1 } split(',', $members) };
843 my $names = join(',', sort { $a <=> $b } keys(%$uniq_members));
844
845 # format: $name:$passwd:$gid:$members
846 push(@data, "$name:$passwd:$gid:$names");
847
848 # always sort by GIDs before printing out the file
849 @data = map { $_->[0] }
850 sort {
851 $a->[3] <=> $b->[3]
852 }
853 map { [ $_, (split /:/)[0, 1, 2, 3] ] }
854 @data;
855
856 if ($delete_group) {
857 print STDOUT "$program: entry deleted\n";
858
859 } elsif ($found) {
860 print STDOUT "$program: entry updated\n";
861
862 } else {
863 print STDOUT "$program: entry created\n";
864 }
865 }
866
867 # ----------------------------------------------------------------------------
868 sub handle_passwd_entry {
869 my %args = @_;
870
871 my $name = $args{'name'};
872 my $uid = $args{'uid'};
873 my $gid = $args{'gid'};
874 my $gecos = $args{'gecos'};
875 my $home = $args{'home'};
876 my $shell = $args{'shell'};
877 my $delete_user = $args{'delete_user'};
878 my $new_passwd = $args{'new_passwd'};
879
880 # Trim any trailing slashes in $home.
881 $home =~ s/(.*)\/$/$1/ if ($home =~ /\/$/);
882
883 # Make sure the given home directory is NOT a relative path (what a
884 # horrible idea).
885
886 unless ($home =~ /^\//) {
887 print STDOUT "$program: error: relative path given for home directory\n";
888 exit 8;
889 }
890
891 # check to see whether we should update the fields for this user (because
892 # they already exist), or create a new entry
893
894 my $found = 0;
895 my $index = 0;
896 for ($index = 0; $index <= $#data; $index++) {
897 my @entry = split(':', $data[$index]);
898
899 if ($name eq $entry[0]) {
900 $found = 1;
901 last;
902 }
903 }
904
905 unless ($found) {
906 print STDOUT "$program: creating passwd entry for user $name\n";
907
908 } else {
909 print STDOUT "$program: updating passwd entry for user $name\n";
910 }
911
912 my $passwd;
913
914 if (!$delete_user) {
915 if (!$new_passwd) {
916 # check the requested shell against the list in /etc/shells
917 check_shell(shell => $shell);
918
919 # prompt the user for the password
920 $passwd = get_passwd(name => $name);
921
922 } else {
923 $passwd = $new_passwd;
924 }
925 }
926
927 # remove the entry to be updated
928 splice(@data, $index, 1);
929
930 if ($delete_user) {
931 print STDOUT "$program: entry deleted\n";
932 return;
933 }
934
935 # format: $name:$passwd:$uid:$gid:$gecos:$home:$shell
936 push(@data, "$name:$passwd:$uid:$gid:$gecos:$home:$shell");
937
938 # always sort by UIDs before printing out the file
939 @data = map { $_->[0] }
940 sort {
941 $a->[3] <=> $b->[3]
942 }
943 map { [ $_, (split /:/)[0, 1, 2, 3, 4, 5, 6] ] }
944 @data;
945
946 if ($delete_user) {
947 print STDOUT "$program: entry deleted\n";
948
949 } elsif ($found) {
950 print STDOUT "$program: entry updated\n";
951
952 } else {
953 print STDOUT "$program: entry created\n";
954 }
955 }
956
957 # ----------------------------------------------------------------------------
958 sub open_output_file {
959 my %args = @_;
960
961 # open $output_file, paying attention to the --force command-line option
962 # If the file already exists, slurp up its contents for later updating.
963
964 if (-f $output_file) {
965 # make sure we can write/update the file first
966 unless (chmod(0644, $output_file)) {
967 die "$program: unable to set permissions on $output_file to 0644: $!\n";
968 }
969
970 if (open(my $fh, "< $output_file")) {
971 if (flock($fh, LOCK_SH|LOCK_NB)) {
972 chomp(@data = <$fh>);
973 flock($fh, LOCK_UN);
974 close($fh);
975
976 } else {
977 close($fh);
978 die("$program: unable to read $output_file: Locked (in use) by another process\n");
979 }
980
981
982 } else {
983 die("$program: unable to open $output_file: $!\n");
984 }
985 }
986
987 # if the --force option was given, just zero out any data that might have
988 # been read in, effectively erasing whatever contents there were. A new
989 # file is generated, anyway -- it's just a question of what data goes into
990 # it
991
992 @data = () if (defined($opts{'F'}));
993 }
994
995 # ----------------------------------------------------------------------------
996 sub usage {
997
998 print STDOUT <<END_OF_USAGE;
999
1000 usage: $program [--help] [--hash|--group|--passwd]
1001
1002 REQUIRED: --passwd, --group, or --hash. These specify whether $program is to
1003 operate on a passwd(5) format file, on a group(5) format file, or simply
1004 to generate a password hash, respectively.
1005
1006 If used with --passwd, $program creates a file in the passwd(5) format,
1007 suitable for use with proftpd's AuthUserFile configuration directive.
1008 You will be prompted for the password to use of the user, which will be
1009 encrypted, and written out as the encrypted string. New entries are
1010 appended to the file by default.
1011
1012 By default, using --passwd will write output to "$default_passwd_file".
1013
1014 Error exit values:
1015
1016 To make it easier for wrapper scripts to interact with $program, $program
1017 will exit with the following error values for the reasons described:
1018
1019 1 no such user
1020 2 password matches current password
1021 4 password matches system password
1022 8 relative path given for home directory
1023
1024 Options:
1025
1026 --file Write output to specified file, rather than "$default_passwd_file".
1027
1028 -F If the file to be used already exists, delete it and write a
1029 --force new one. By default, new entries will be appended to the file.
1030
1031 --gecos Descriptive string for the given user (usually the user's
1032 full name).
1033
1034 --gid Primary group ID for this user (optional, will default to
1035 given --uid value if absent).
1036
1037 -h Displays this message.
1038 --help
1039
1040 --home Home directory for the user (required).
1041
1042 --des Use the DES algorithm for encrypting passwords. The default
1043 is the MD5 algorithm.
1044
1045 --md5 Use the MD5 algorithm for encrypting passwords. This is the
1046 default.
1047
1048 --name Name of the user account (required). If the name does not
1049 exist in the specified output-file, an entry will be created
1050 for her. Otherwise, the given fields will be updated.
1051
1052 --shell Shell for the user (required). Recommended: /bin/false
1053
1054 --uid Numerical user ID (required)
1055
1056 --change-home
1057
1058 Update only the home directory field for a user. This option
1059 requires that the --name and --passwd options be used, but no
1060 others.
1061
1062 --change-password
1063
1064 Update only the password field for a user. This option
1065 requires that the --name and --passwd options be used, but
1066 no others. This also double-checks the given password against
1067 the user's current password in the existing passwd file, and
1068 requests that a new password be given if the entered password
1069 is the same as the current password.
1070
1071 --delete-user
1072
1073 Remove the entry for the given user name from the file.
1074
1075 -l Lock the password of the named account. This option disables a
1076 --lock password by changing it to a value which matches no possible
1077 encrypted value (it adds a '!' at the beginning of the
1078 password).
1079
1080 --not-previous-password
1081
1082 Double-checks the given password against the previous password
1083 for the user, and requests that a new password be given if
1084 the entered password is the same as the previous password.
1085
1086 --not-system-password
1087
1088 Double-checks the given password against the system password
1089 for the user, and requests that a new password be given if
1090 the entered password is the same as the system password. This
1091 helps to enforce different passwords for different types of
1092 access.
1093
1094 --sha256 Use the SHA-256 algorithm for encrypting passwords.
1095
1096 --sha512 Use the SHA-512 algorithm for encrypting passwords.
1097
1098 --stdin
1099 Read the password directly from standard in rather than
1100 prompting for it. This is useful for writing scripts that
1101 automate use of $program.
1102
1103 -u Unlock the password of the named account. This option
1104 --unlock re-enables a password by changing the password back to its
1105 previous value (to the value before using the -l option).
1106
1107 --use-cracklib
1108
1109 Causes $program to use Alec Muffet's cracklib routines in
1110 order to determine and prevent the use of bad or weak
1111 passwords. The optional path to this option specifies
1112 the path to the dictionary files to use -- default path
1113 is "$default_cracklib_dict". This requires the Perl
1114 Crypt::Cracklib module to be installed on your system.
1115
1116 --version
1117 Displays the version of $program.
1118
1119 If used with --group, $program creates a file in the group(5) format,
1120 suitable for use with proftpd's AuthGroupFile configuration directive.
1121
1122 By default, using --group will write output to "$default_group_file".
1123
1124 Options:
1125
1126 --add-member
1127
1128 Add the named member to the given group name from the file.
1129 Example:
1130
1131 \$ ftpasswd --group --file=... --name=ftpd --add-member=bob
1132
1133 --delete-group
1134
1135 Remove the entry for the given group name from the file.
1136
1137 --enable-group-passwd
1138
1139 Prompt for a group password. This is disabled by default,
1140 as group passwords are not usually a good idea at all.
1141
1142 --file Write output to specified file, rather than "$default_group_file".
1143
1144 -F If the file be used already exists, delete it and write a new
1145 --force one. By default, new entries will be appended to the file.
1146
1147 --gid Numerical group ID (required).
1148
1149 -h
1150 --help Displays this message.
1151
1152 -m
1153 --member User name to be a member of the group. This argument may be
1154 used multiple times to specify the full list of users to be
1155 members of this group.
1156
1157 --des Use the DES algorithm for encrypting passwords. The default
1158 is the SHA256 algorithm.
1159
1160 --md5 Use the MD5 algorithm for encrypting passwords.
1161
1162 --name Name of the group (required). If the name does not exist in
1163 the specified output-file, an entry will be created for them.
1164 Otherwise, the given fields will be updated.
1165
1166 --sha256 Use the SHA-256 algorithm for encrypting passwords. This is the
1167 default.
1168
1169 --sha512 Use the SHA-512 algorithm for encrypting passwords.
1170
1171 --stdin
1172 Read the password directly from standard in rather than
1173 prompting for it. This is useful for writing scripts that
1174 automate use of $program.
1175
1176 --use-cracklib
1177
1178 Causes $program to use Alec Muffet's cracklib routines in
1179 order to determine and prevent the use of bad or weak
1180 passwords. The optional path to this option specifies
1181 the path to the dictionary files to use -- default path
1182 is "$default_cracklib_dict". This requires the Perl
1183 Crypt::Cracklib module to be installed on your system.
1184
1185 --version
1186 Displays the version of $program.
1187
1188 If used with --hash, $program generates a hash of a password, as would
1189 appear in an AuthUserFile. The hash is written to standard out.
1190 This hash is suitable for use with proftpd's UserPassword directive.
1191
1192 Options:
1193
1194 --des Use the DES algorithm for encrypting passwords. The default
1195 is the MD5 algorithm.
1196
1197 --md5 Use the MD5 algorithm for encrypting passwords. This is the
1198 default.
1199
1200 --sha256 Use the SHA-256 algorithm for encrypting passwords.
1201
1202 --sha512 Use the SHA-512 algorithm for encrypting passwords.
1203
1204 --stdin
1205 Read the password directly from standard in rather than
1206 prompting for it. This is useful for writing scripts that
1207 automate use of $program.
1208
1209 --use-cracklib
1210
1211 Causes $program to use Alec Muffet's cracklib routines in
1212 order to determine and prevent the use of bad or weak
1213 passwords. The optional path to this option specifies
1214 the path to the dictionary files to use -- default path
1215 is "$default_cracklib_dict". This requires the Perl
1216 Crypt::Cracklib module to be installed on your system.
1217
1218 --version
1219 Displays the version of $program.
1220
1221 END_OF_USAGE
1222
1223 exit 0;
1224 }
1225
1226 # ---------------------------------------------------------------------------
1227 sub version {
1228 print STDOUT "$version\n";
1229
1230 exit 0;
1231 }
1232
1233 # ---------------------------------------------------------------------------
1234