~ubuntu-branches/ubuntu/lucid/user-setup/lucid-201002122356

« back to all changes in this revision

Viewing changes to user-setup-apply

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2005-12-04 10:25:05 UTC
  • Revision ID: james.westby@ubuntu.com-20051204102505-7fan01zh4wf0q1jb
Tags: 0.03ubuntu1
* Port Ubuntu changes from passwd.config:
  - Allow an empty root password, indicating the use of sudo.
  - Default to the above and only offer the root password question in
    expert mode.
  - Add the initial user to the adm, dip, lpadmin, and scanner groups too,
    and to the admin group if no root password is set.
  - Adjust /etc/aliases to send root mail to the initial user.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
set -e
 
3
 
 
4
. /usr/share/debconf/confmodule
 
5
 
 
6
if [ "$1" ]; then
 
7
        ROOT="$1"
 
8
        chroot=chroot
 
9
        log='log-output -t user-setup'
 
10
else
 
11
        ROOT=
 
12
        chroot=
 
13
        log=
 
14
fi
 
15
 
 
16
. /usr/lib/user-setup/functions.sh
 
17
 
 
18
# Set a password, via chpasswd.
 
19
# Use perl rather than echo, to avoid the password
 
20
# showing in the process table. (However, this is normally
 
21
# only called when first installing the system, when root has no
 
22
# password at all, so that should be an unnecessary precaution).
 
23
#
 
24
# Pass in three arguments: the user, the password, and 'true' if the
 
25
# password has been pre-crypted (by preseeding).
 
26
setpassword () {
 
27
        SETPASSWD_PW="$2"
 
28
        export SETPASSWD_PW
 
29
 
 
30
        # This is very annoying. chpasswd cannot handle generating md5
 
31
        # passwords as it is not PAM-aware. Thus, I have to work around
 
32
        # that by crypting the password myself if md5 is used.
 
33
        USE_MD5=1
 
34
        export USE_MD5
 
35
 
 
36
        if [ "$3" = true ]; then
 
37
                PRECRYPTED=1
 
38
        else
 
39
                PRECRYPTED=''
 
40
        fi
 
41
        export PRECRYPTED
 
42
        $chroot $ROOT perl -e '
 
43
                sub CreateCryptSalt {
 
44
                        my $md5 = shift;
 
45
 
 
46
                        my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
 
47
                        my ($in, $out);
 
48
 
 
49
                        my $cryptsaltlen = ($md5 ? 8 : 2);
 
50
 
 
51
                        open (F, "</dev/urandom") || die "No /dev/urandom found!";
 
52
                        foreach (1..$cryptsaltlen) {
 
53
                                read(F, $in, 1);
 
54
                                $out .= $valid[ord($in) % ($#valid + 1)];
 
55
                        }
 
56
                        close F;
 
57
                        return ($md5 ? "\$1\$$out\$" : $out);
 
58
                }
 
59
        
 
60
                open(P,"| chpasswd -e");
 
61
                if ($ENV{PRECRYPTED}) {
 
62
                        print P shift().":$ENV{SETPASSWD_PW}\n";
 
63
                } else {
 
64
                        print P shift().":".
 
65
                                crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
 
66
                                "\n";
 
67
                }
 
68
                close P;
 
69
        ' "$1"
 
70
        SETPASSWD_PW=''
 
71
        USE_MD5=''
 
72
        PRECRYPTED=''
 
73
}
 
74
 
 
75
sudoers_admin_group () {
 
76
        if [ -f $ROOT/etc/sudoers ]; then
 
77
                cat <<EOF >>$ROOT/etc/sudoers
 
78
 
 
79
# Members of the admin group may gain root privileges
 
80
%admin  ALL=(ALL) ALL
 
81
EOF
 
82
        fi
 
83
}
 
84
 
 
85
add_to_aliases () {
 
86
        if ! grep -qi ^root: $ROOT/etc/aliases 2>/dev/null; then
 
87
                cat <<EOF >>$ROOT/etc/aliases
 
88
# Added by installer for initial user
 
89
root:   $1
 
90
EOF
 
91
                if [ -x $ROOT/usr/bin/newaliases ]; then
 
92
                        $log $chroot $ROOT /usr/bin/newaliases
 
93
                fi
 
94
        fi
 
95
}
 
96
 
 
97
# Enable/disable shadow passwords.
 
98
db_get passwd/shadow
 
99
if [ "$RET" = true ]; then
 
100
        $log $chroot $ROOT shadowconfig on
 
101
else
 
102
        $log $chroot $ROOT shadowconfig off
 
103
fi
 
104
 
 
105
if ! root_password; then
 
106
        # Was the root password preseeded encrypted?
 
107
        if db_get passwd/root-password-crypted && [ "$RET" ]; then
 
108
                ROOT_PW="$RET"
 
109
                # The root password was preseeded encrypted. Clear it from
 
110
                # the database, then set it.
 
111
                db_set passwd/root-password-crypted ''
 
112
                setpassword root "$ROOT_PW" true
 
113
                ROOT_PW=
 
114
        else
 
115
                db_get passwd/root-password
 
116
                ROOT_PW="$RET"
 
117
                if [ "$ROOT_PW" ]; then
 
118
                        # Clear the root password from the database, and set
 
119
                        # the password.
 
120
                        db_set passwd/root-password ''
 
121
                        db_set passwd/root-password-again ''
 
122
                        setpassword root "$ROOT_PW" false
 
123
                        ROOT_PW=
 
124
                else
 
125
                        # Disable the root password.
 
126
                        echo 'root:*' | $chroot $ROOT chpasswd -e
 
127
                fi
 
128
        fi
 
129
fi
 
130
 
 
131
db_get passwd/make-user
 
132
if [ "$RET" = true ] && ! is_system_user; then
 
133
        if db_get passwd/user-password-crypted && [ "$RET" ]; then
 
134
                USER_PW="$RET"
 
135
                USER_PW_CRYPTED=true
 
136
        else
 
137
                db_get passwd/user-password
 
138
                USER_PW="$RET"
 
139
                USER_PW_CRYPTED=false
 
140
        fi
 
141
 
 
142
        if db_get passwd/user-uid && [ "$RET" ]; then
 
143
                if [ -x $ROOT/usr/sbin/adduser ]; then
 
144
                        UIDOPT="--uid $RET"
 
145
                else
 
146
                        UIDOPT="-u $RET"
 
147
                fi
 
148
        else
 
149
                UIDOPT=
 
150
        fi
 
151
 
 
152
        # Add the user to the database, using adduser in noninteractive
 
153
        # mode.
 
154
        db_get passwd/username
 
155
        USER="$RET"
 
156
        db_get passwd/user-fullname
 
157
 
 
158
        if [ -x $ROOT/usr/sbin/adduser ]; then
 
159
                $log $chroot $ROOT adduser --disabled-password --gecos "$RET" $UIDOPT "$USER" >/dev/null || true
 
160
        else
 
161
                $log $chroot $ROOT useradd -c "$RET" -m "$USER" $UIDOPT >/dev/null || true
 
162
        fi
 
163
 
 
164
        # Clear the user password from the database.
 
165
        if [ "$USER_PW_CRYPTED" = true ]; then
 
166
                db_set passwd/user-password-crypted ''
 
167
        else
 
168
                db_set passwd/user-password ''
 
169
                db_set passwd/user-password-again ''
 
170
        fi
 
171
        setpassword "$USER" "$USER_PW" "$USER_PW_CRYPTED"
 
172
 
 
173
        if [ -n "$USER" ]; then
 
174
                for group in lpadmin scanner; do
 
175
                        $log $chroot $ROOT addgroup --system $group >/dev/null 2>&1 || true
 
176
                done
 
177
                for group in adm audio cdrom dialout floppy video plugdev dip lpadmin scanner; do
 
178
                        $log $chroot $ROOT adduser "$USER" $group >/dev/null 2>&1 || true
 
179
                done
 
180
 
 
181
                if ! root_password; then
 
182
                        sudoers_admin_group
 
183
                        $log $chroot $ROOT addgroup --system admin >/dev/null 2>&1 || true
 
184
                        $log $chroot $ROOT adduser "$USER" admin >/dev/null 2>&1 || true
 
185
                fi
 
186
 
 
187
                add_to_aliases "$USER"
 
188
        fi
 
189
fi
 
190
 
 
191
exit 0