File: create-user.pl

package info (click to toggle)
webmin-virtual-server 2.50-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,608 kB
  • ctags: 392
  • sloc: perl: 15,687; makefile: 95; sh: 8
file content (215 lines) | stat: -rwxr-xr-x 5,430 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/local/bin/perl
# create-user.pl
# Adds a new mailbox, based on command-line parameters

$no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*\/)[^\/]+$/) {
	chdir($1);
	}
chop($pwd = `pwd`);
$0 = "$pwd/create-user.pl";
require './virtual-server-lib.pl';
$< == 0 || die "create-user.pl must be run as root";

$user = &create_initial_user();

# Parse command-line args
while(@ARGV > 0) {
	local $a = shift(@ARGV);
	if ($a eq "--domain") {
		$domain = shift(@ARGV);
		}
	elsif ($a eq "--user") {
		$username = lc(shift(@ARGV));
		}
	elsif ($a eq "--pass") {
		$pass = shift(@ARGV);
		}
	elsif ($a eq "--real") {
		$real = shift(@ARGV);
		}
	elsif ($a eq "--ftp") {
		$ftp = 1;
		}
	elsif ($a eq "--jailed-ftp") {
		$config{'jail_shell'} || usage();
		$ftp = 2;
		}
	elsif ($a eq "--noemail") {
		$noemail++;
		}
	elsif ($a eq "--extra") {
		local $extra = shift(@ARGV);
		push(@extra, $extra);
		}
	elsif ($a eq "--quota") {
		$quota = shift(@ARGV);
		}
	elsif ($a eq "--mail-quota") {
		$mquota = shift(@ARGV);
		}
	elsif ($a eq "--qmail-quota") {
		$qquota = shift(@ARGV);
		}
	}

# Make sure all needed args are set
$domain && $username && $pass || &usage();
if ($user->{'unix'}) {
	if ($config{'home_quotas'}) {
		$quota =~ /^\d+$/ || &usage();
		}
	if ($config{'mail_quotas'} && $config{'home_quotas'} ne $config{'mail_quotas'}) {
		$mquota =~ /^\d+$/ || &usage();
		}
	}
if ($user->{'mailquota'}) {
	!$qquota || $qquota =~ /^\d+$/ || usage();
	}
$d = &get_domain_by("dom", $domain);
$d || die("Virtual server $domain does not exist");
$username =~ /^[^ \t:]+$/ || die($text{'user_euser'});
if ($user->{'person'}) {
	$real =~ /^[^:]*$/ || die($text{'user_ereal'});
	}
foreach $e (@extra) {
	$e = lc($e);
	if ($e =~ /^([^\@ \t]+$)$/) {
		$e = "$e\@$d->{'dom'}";
		}
	if ($e !~ /^(\S+)\@(\S+)$/) {
		die(&text('user_eextra1', $e));
		}
	local ($eu, $ed) = ($1, $2);
	local $edom = &get_domain_by("dom", $ed);
	$edom && $edom->{'mail'} || die(&text('user_eextra2', $ed));
	}

# Build taken lists
&build_taken(\%taken, \%utaken);

# Construct user object
if ($user->{'unix'}) {
	$user->{'uid'} = &allocate_uid(\%taken);
	}
else {
	$user->{'uid'} = $d->{'uid'};
	}
$user->{'gid'} = $d->{'gid'};
if ($user->{'person'}) {
	$user->{'real'} = $real;
	}
if ($user->{'unix'}) {
	$user->{'shell'} = $ftp == 1 ? $config{'ftp_shell'} :
			   $ftp == 2 ? $config{'jail_shell'} :
				       $config{'shell'};
	}
if (!$user->{'fixedhome'}) {
	$user->{'home'} = "$d->{'home'}/$config{'homes_dir'}/$username";
	}
$user->{'passmode'} = 3;
$user->{'plainpass'} = $pass;
$user->{'pass'} = &foreign_call($usermodule, "encrypt_password", $pass);
$user->{'extraemail'} = \@extra;
if (($utaken{$username} || $config{'append'}) && !$user->{'noappend'}) {
	$user->{'user'} = &userdom_name($username, $d);
	}
else {
	$user->{'user'} = $username;
	}
$user->{'email'} = "$username\@$d->{'dom'}" if (!$noemail);
if ($user->{'mailquota'}) {
	$user->{'qquota'} = $qquota;
	}

if ($user->{'unix'}) {
	# Check for a Unix clash
	if ($utaken{$user->{'user'}} ||
	    &check_clash($username, $d->{'dom'})) {
		die($text{'user_eclash'});
		}
	}

# Check for clash within this domain
($clash) = grep { $_->{'user'} eq $username &&
		  $_->{'unix'} == $user->{'unix'} } @users;
$clash && &error($text{'user_eclash2'});

# Check if any extras clash
foreach $e (@extra) {
	$e =~ /^(\S+)\@(\S+)$/;
	if (&check_clash($1, $2)) {
		die(&text('user_eextra4', $e));
		}
	}

# Check if the name is too long
if ($lerr = &too_long($user->{'user'})) {
	die($lerr);
	}

# Create the user and virtusers and alias
&create_user($user, $d);

if ($user->{'home'}) {
	# Create his homedir
	system("mkdir -p '$user->{'home'}'");
	system("chown $user->{'uid'}:$user->{'gid'} '$user->{'home'}'");
	system("chmod 755 '$user->{'home'}'");

	# Copy files into homedir
	&copy_skel_files($config{'mail_skel'}, $user, $user->{'home'});
	}

# Create an empty mail file
&create_mail_file($user);

# Send an email upon creation
@erv = &send_user_email($d, $user);

if ($user->{'unix'}) {
	# Set quotas
	if ($config{'home_quotas'}) {
		&set_quota($user->{'user'}, $config{'home_quotas'}, $quota);
		}
	if ($config{'mail_quotas'} && $config{'home_quotas'} ne $config{'mail_quotas'}) {
		&set_quota($user->{'user'}, $config{'mail_quotas'}, $quota);
		}
	}

print "User $user->{'user'} created successfully\n";

sub usage
{
print "Adds a new mailbox user to an existing Virtualmin domain.\n";
print "\n";
print "usage: create-user.pl    --domain domain.name\n";
print "                         --user new-username\n";
print "                         --pass password-for-new-user\n";
if ($user->{'unix'}) {
	if ($config{'home_quotas'}) {
		print "                         --quota quota-in-blocks\n";
		}
	if ($config{'mail_quotas'} &&
	    $config{'mail_quotas'} ne $config{'home_quotas'}) {
		print "                         --mail-quota quota-in-blocks\n";
		}
	}
if ($user->{'mailquota'}) {
	print "                         --qmail-quota quota-in-bytes\n";
	}
if ($user->{'person'}) {
	print "                        [--real real-name-for-new-user]\n";
	}
if ($user->{'unix'}) {
	print "                        [--ftp]\n";
	if ($config{'jail_ftp'}) {
		print "                        [--jail-ftp]\n";
		}
	}
print "                        [--noemail]\n";
print "                        [--extra email.address\@some.domain]\n";
exit(1);
}