summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/adduserdrake
blob: 1d2eeb9c5f30286f733834016be705d31a0e706b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);

use common qw(:common :functional :system :file);
use interactive;
use any;

local $_ = join '', @ARGV;

/-h/ and die "usage: adduserdrake [--beginner] [--expert] [<users...>]\n";

$::beginner = /-beginner/;
$::expert = /-expert/;
$::isStandalone = 1;

my @etc_pass_fields = qw(name pw uid gid realname home shell);
my @shells = grep { -x $_ } map { "/bin/$_" } qw(bash tcsh zsh ash ksh);
my $isMD5 = cat_("/etc/pam.d/passwd") =~ /md5/;
my $isShadow = cat_("/etc/pam.d/passwd") =~ /shadow/;
my $security = $ENV{SECURE_LEVEL};



if (my @l = grep { ! /^-/ } @ARGV) {
    addusers(map {{ name => $_, realname => $_ }} @l);
    exit 0;
}

my $in = vnew interactive('su');
my @users; 

new:
$u = { icon => translate('default') };
if ($in->ask_from_entries_refH(
        [ _("Add user"), _("Accept user"), _("Done") ],
        _("Enter a user\n%s", $users ? _("(already added %s)", join(", ", map { $_->{realname} || $_->{name} } @users)) : ''),
        [ 
	 _("Real name") => \$u->{realname},
	 _("User name") => \$u->{name},
	   $security < 2 ? () : (
         _("Password") => {val => \$u->{password}, hidden => 1},
         _("Password (again)") => {val => \$u->{password2}, hidden => 1},
	   ), $::beginner ? () : (
         _("Shell") => {val => \$u->{shell}, list => \@shells, not_edit => !$::expert} 
	   ), $security > 3 ? () : (
	 _("Icon") => {val => \$u->{icon}, list => [ map { translate($_) } @any::users ], not_edit => 1 },
	   ),
        ],
        focus_out => sub {
	    if ($_[0] eq 0) {
		$u->{name} ||= lc first($u->{realname} =~ /((\w|-)+)/);
	    }
	},
        complete => sub {
	    $u->{password} eq $u->{password2} or $o->ask_warn('', [ _("The passwords do not match"), _("Please try again") ]), return (1,3);
	    $security > 3 && length($u->{password}) < 6 and $o->ask_warn('', _("This password is too simple")), return (1,2);
	    $u->{name} or $o->ask_warn('', _("Please give a user name")), return (1,0);
	    $u->{name} =~ /^[a-z0-9_-]+$/ or $o->ask_warn('', _("The user name must contain only lower cased letters, numbers, `-' and `_'")), return (1,0);
	    member($u->{name}, map { $_->{name} } @users) and $o->ask_warn('', _("This user name is already added")), return (1,0);
	    $u->{icon} = untranslate($u->{icon}, @any::users);
	    return 0;
	},
)) {
    push @users, $u;
    goto new;
}

addusers(@users);

sub addusers {
    my @u = map { $_->{name} } my @users = @_;

    foreach (@users) {
	$_->{pw} = $isMD5 ? c::crypt_md5($_->{password}, salt(8)) : crypt($_->{password}, salt(2));
	$_->{shell} ||= "/bin/bash";
    }

    system("adduser $_") foreach @u;
    any::addUsers('', @u);

    substInFile {
	foreach my $u (@users) {
	    if (/^$u->{name}:/) {
		chomp;
		my %l; @l{@etc_pass_fields} = split ':';
		add2hash($u, \%l);
		$_ = join(':', @$u{@etc_pass_fields}) . "\n";
	    }
	}
    } "/etc/passwd";
    system("pwconv") if $isShadow;
}

$in->exit(0);