File: import.cgi

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 (465 lines) | stat: -rwxr-xr-x 12,865 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/local/bin/perl
# import.cgi
# Work out what will be imported, and either tell the user or actually do it

require './virtual-server-lib.pl';
$access{'create'} && $access{'import'} || &error($text{'import_ecannot'});
&ReadParse();

&ui_print_header(undef, $text{'import_title'}, "");

# Check for domain clash
@doms = &list_domains();
($clash) = grep { $_->{'dom'} eq $in{'dom'} } @doms;
$clash && &error_exit($text{'import_eexists'});

# Validate username
$in{'user_def'} || $in{'user'} =~ /^[^\t :]+$/ ||
	&error_exit($text{'setup_euser2'});
$in{'group_def'} || $in{'group'} =~ /^[^\t :]+$/ ||
	&error_exit($text{'import_egroup'});

# Validate quota
if ($config{'home_quotas'}) {
        if ($in{'quota'} == -1) { $in{'quota'} = $in{'otherquota'} };
	$in{'quota'} =~ /^\d+$/ ||  &error($text{'setup_equota'});
	}

# Make sure IP is valid
if ($in{'virt'}) {
	&foreign_require("net", "net-lib.pl");
	($iface) = grep { $_->{'address'} eq $in{'ip'} }
				( &net::boot_interfaces(),
				  &net::active_interfaces() );
	$iface || &error_exit($text{'import_enoip'});
	$iface->{'address'} eq &get_default_ip() &&
		&error_exit($text{'import_eipsame'});
	foreach $d (@doms) {
		$d->{'ip'} eq $in{'ip'} &&
			&error_exit(&text('import_eipclash', $d->{'dom'}));
		}
	$iface->{'virtual'} eq '' &&
		&error_exit(&text('import_ereal', $iface->{'fullname'}));
	}

if ($in{'confirm'}) {
	# Go ahead and do it
	print "$text{'import_doing'}<p>\n";
	&require_useradmin();

	# Get existing group and user details
	$crgroup = $in{'crgroup'} || $in{'group'};
	@groups = &foreign_call($usermodule, "list_groups");
	($ginfo) = grep { $_->{'group'} eq $crgroup } @groups;
	&build_group_taken(\%gtaken, \%ggtaken);
	$gid = $ginfo ? $ginfo->{'gid'} : &allocate_gid(\%gtaken);

	$cruser = $in{'cruser'} || $in{'user'};
	@users = &foreign_call($usermodule, "list_users");
	($uinfo) = grep { $_->{'user'} eq $cruser } @users;
	&build_taken(\%taken, \%utaken);
	$uid = $uinfo ? $uinfo->{'uid'} : &allocate_uid(\%taken);
	$ugid = $uinfo ? $uinfo->{'gid'} : $gid;
	$ugroup = $uinfo ? getgrgid($uinfo->{'gid'}) : $crgroup;
	$owner = $uinfo ? $uinfo->{'real'} : "Imported domain $in{'dom'}";

	# Create domain details
	%found = map { $_, 1 } split(/\0/, $in{'found'});
	@dbs = ( split(/\s+/, $in{'db_mysql'}),
		 split(/\s+/, $in{'db_postgres'}) );
	%dom = ( 'id', &domain_id(),
		 'dom', $in{'dom'},
		 'unix', 1,
		 'dir', 1,
		 'user', $cruser,
		 'group', $crgroup,
		 'ugroup', $ugroup,
		 'quota', &quota_parse('quota', $config{'home_quotas'}),
		 'uid', $uid,
		 'gid', $gid,
		 'ugid', $ugid,
		 'owner', $owner,
		 'mail', int($found{'mail'}),
		 'web', int($found{'web'}),
		 'ftp', int($found{'ftp'}),
		 'webalizer', int($found{'webalizer'}),
		 'mysql', int($found{'mysql'}),
		 'postgres', int($found{'postgres'}),
		 'logrotate', int($found{'logrotate'}),
		 'webmin', $in{'webmin'},
		 'name', !$in{'virt'},
		 'ip', $in{'ip'},
		 'iface', $in{'virt'} ? $iface->{'fullname'} : "",
		 'virt', $in{'virt'},
		 'dns', int($found{'dns'}),
		 'pass', $in{'pass'},
		 'db', $dbs[0],
		 'db_mysql', $in{'found_mysql'},
		 'db_postgres', $in{'found_postgres'},
		 'source', 'import.cgi'
		);

	# Work out home directory
	$dom{'home'} = $uinfo ? $uinfo->{'home'}
			      : &server_home_directory(\%dom);

	if (!$ginfo) {
		# Create the unix group
		print &text('setup_group', $crgroup),"<br>\n";
		&foreign_call($usermodule, "lock_user_files");
		%ginfo = ( 'group', $crgroup,
			   'gid', $gid );
		&foreign_call($usermodule, "set_group_envs", \%ginfo, 'CREATE_GROUP');
		&foreign_call($usermodule, "making_changes");
		&foreign_call($usermodule, "create_group", \%ginfo);
		&foreign_call($usermodule, "unlock_user_files");
		&foreign_call($usermodule, "made_changes");
		print $text{'setup_done'},"<p>\n";
		}
	else {
		%ginfo = %$ginfo;
		}

	if (!$uinfo) {
		# Create the Unix user
		# XXX use common function??
		print &text('setup_user', $cruser),"<br>\n";
		&foreign_call($usermodule, "lock_user_files");
		%uinfo = ( 'user', $cruser,
			   'uid', $uid,
			   'gid', $gid,
			   'pass', &foreign_call($usermodule,
					"encrypt_password", $in{'pass'}),
			   'real', $owner,
			   'home', $dom{'home'},
			   'shell', $config{'unix_shell'},
			   'mailbox', $cruser,
			   'dom', $in{'dom'},
			 );
		&set_pass_change(\%uinfo);
		&foreign_call($usermodule, "set_user_envs", \%uinfo, 'CREATE_USER', $in{'pass'}, [ ]);
		&foreign_call($usermodule, "making_changes");
		&foreign_call($usermodule, "create_user", \%uinfo);
		&foreign_call($usermodule, "unlock_user_files");
		&foreign_call($usermodule, "made_changes");
		&foreign_call($usermodule, "unlock_user_files");
		print $text{'setup_done'},"<p>\n";

		if (!-d $uinfo{'home'}) {
			# Create his home directory, and copy files into it
			print $text{'setup_home'},"<br>\n";
			&system_logged(
			  "mkdir '$uinfo{'home'}'");
			&system_logged(
			  "chmod '$uconfig{'homedir_perms'}' '$uinfo{'home'}'");
			&system_logged("chown $uid:$gid '$uinfo{'home'}'");
			&copy_skel_files($config{'virtual_skel'},
					 \%uinfo, $uinfo{'home'});
			print $text{'setup_done'},"<p>\n";
			}
		}
	else {
		%uinfo = %$uinfo;
		}

	# Setup web directories
	print $text{'import_dirs'},"<br>\n";
	foreach $d ( [ $config{'html_dir'} || 'public_html', '755' ],
		     [ 'cgi-bin', '755' ],
		     [ 'logs', '755' ],
		     [ $config{'homes_dir'}, '755' ] ) {
		if (!-d "$uinfo{'home'}/$d->[0]") {
			&system_logged(
				"mkdir '$uinfo{'home'}/$d->[0]' 2>/dev/null");
			&system_logged(
				"chmod $d->[1] '$uinfo{'home'}/$d->[0]'");
			&system_logged(
				"chown $uid:$ugid '$uinfo{'home'}/$d->[0]'");
			}
		}
	print $text{'setup_done'},"<p>\n";

	# Create the domain details
	print $text{'setup_save'},"<br>\n";
	&save_domain(\%dom);
	print $text{'setup_done'},"<p>\n";

	# Create or update webmin user
	if ($in{'webmin'}) {
		&require_acl();
		($wuser) = grep { $_->{'name'} eq $cruser } &acl::list_users();
		if ($wuser) {
			&modify_webmin(\%dom, \%dom);
			}
		else {
			&setup_webmin(\%dom);
			}
		}
	&run_post_actions();

	# Add to this user's list of domains if needed
	if (!&can_edit_domain(\%dom)) {
		$access{'domains'} = join(" ", split(/\s+/, $access{'domains'}),
					       $dom{'id'});
		&save_module_acl(\%access);
		}
	&webmin_log("import", "domain", $dom{'dom'});
	}
else {
	# Just work out what can be done
	print "$text{'import_idesc'}<p>\n";

	# Work out the Unix username
	if ($in{'user_def'}) {
		# Creating a new user with a name taken from the domain
		$in{'dom'} =~ /^([^\.]+)/;
		$try1 = $user = $1;
		if (defined(getpwnam($1)) || $config{'longname'}) {
			$user = $in{'dom'};
			$try2 = $user;
			if (defined(getpwnam($user))) {
				&error_exit(&text('setup_eauto', $try1,$try2));
				}
			}
		print "<b>",&text('import_user1',
				  "<tt>$user</tt>"),"</b><p>\n";
		}
	else {
		# Using a specified name, which may or may not exist
		if (defined(getpwnam($in{'user'}))) {
			print "<b>",&text('import_user2',
					  "<tt>$in{'user'}</tt>"),"</b><p>\n";
			}
		else {
			print "<b>",&text('import_user3',
					  "<tt>$in{'user'}</tt>"),"</b><p>\n";
			}
		}

	if ($in{'group_def'}) {
		# Need to create new group with the same name as the user
		$group = $user || $in{'user'};
		print "<b>",&text('import_group1',
				  "<tt>$group</tt>"),"</b><p>\n";
		}
	else {
		# Using a specified group, which may or may not exist
		if (scalar(@ginfo = getgrnam($in{'group'}))) {
			print "<b>",&text('import_group2',
					  "<tt>$in{'group'}</tt>"),"</b><p>\n";

			setpwent();
			while(@muinfo = getpwent()) {
				$mcount++ if ($muinfo[3] == $ginfo[2]);
				}
			endpwent();
			if ($mcount) {
				print "<b>",&text('import_mailboxes', $mcount,
					  "<tt>$in{'group'}</tt>"),"</b><p>\n";
				}
			}
		else {
			print "<b>",&text('import_group3',
					  "<tt>$in{'group'}</tt>"),"</b><p>\n";
			}
		}

	# Check for mail domain
	if ($config{'mail'}) {
		&require_mail();
		$found{'mail'}++ if (&is_local_domain($in{'dom'}));
		if ($found{'mail'}) {
			print "<b>",&text('import_mail',
				"<tt>$in{'dom'}</tt>"),"</b><p>\n";
			}
		else {
			print &text('import_nomail',
				"<tt>$in{'dom'}</tt>"),"<p>\n";
			}
		}

	# Check for an Apache virtualhost
	if ($config{'web'}) {
		&require_apache();
		($virt, $vconf) = &get_apache_virtual($in{'dom'}, $web_port);
		if ($virt) {
			$found{'web'}++;
			$sn = &apache::find_directive(
				"ServerName", $virt->{'members'});
			$dr = &apache::find_directive(
				"DocumentRoot", $virt->{'members'});
			print "<b>",&text('import_web',
				"<tt>$sn</tt>", "<tt>$dr</tt>"),"</b><p>\n";
			}
		else {
			print &text('import_noweb',
				"<tt>$in{'dom'}</tt>"),"<p>\n";
			}
		}

	# Check for Webalizer on the virtual host logs
	if ($config{'webalizer'}) {
		&require_webalizer();
		$log = &get_apache_log($in{'dom'});
		if ($log && -r &webalizer::config_file_name($log)) {
			$found{'webalizer'}++;
			print "<b>",&text('import_webalizer',
				    "<tt>$log</tt>"),"</b><p>\n";
			}
		else {
			print &text('import_nowebalizer',
				    "<tt>$log</tt>"),"<p>\n";
			}
		}

	# Check for a DNS domain
	if ($config{'dns'}) {
		&require_bind();
		$conf = &bind::get_config();
		@zones = &bind::find("zone", $conf);
		foreach $v (&bind::find("view", $conf)) {
			push(@zones, &bind::find("zone", $v->{'members'}));
			}
		foreach $z (@zones) {
			if ($z->{'value'} eq $in{'dom'}) {
				$found{'dns'}++;
				last;
				}
			}
		if ($found{'dns'}) {
			print "<b>",&text('import_dns',
				"<tt>$in{'dom'}</tt>"),"</b><p>\n";
			}
		else {
			print &text('import_nodns',
				"<tt>$in{'dom'}</tt>"),"<p>\n";
			}
		}

	# Check for databases
	@alldbs = &all_databases();
	foreach $t ('mysql', 'postgres') {
		foreach $db (split(/\s+/, $in{'db_'.$t})) {
			local ($got) = grep { $_->{'type'} eq $t &&
					      $_->{'name'} eq $db } @alldbs;
			if ($got) {
				$found{$t}++;
				print "<b>",&text('import_'.$t,
					"<tt>$db</tt>"),"</b><p>\n";
				}
			else {
				print &text('import_no'.$t,
					"<tt>$db</tt>"),"<p>\n";
				}
			push(@{$dbnames{$t}}, $db);
			}
		}

	# Check for a ProFTPd virtualhost
	if ($config{'ftp'} && $in{'virt'}) {
		&require_proftpd();
		($virt, $vconf, $anon, $aconf) = &get_proftpd_virtual($in{'ip'});
		if ($virt && $anon) {
			$found{'ftp'}++;
			print "<b>",&text('import_ftp',
				"<tt>$in{'ip'}</tt>",
				"<tt>$anon->{'value'}</tt>"),"</b><p>\n";
			}
		elsif ($virt) {
			$found{'ftp'}++;
			print "<b>",&text('import_ftpnoanon',
				"<tt>$in{'ip'}</tt>"),"</b><p>\n";
			}
		else {
			print &text('import_noftp',
				"<tt>$in{'ip'}</tt>"),"<p>\n";
			}
		}

	# Check for an existing logrotate configuration
	if ($config{'logrotate'}) {
		$log = &get_apache_log($in{'dom'});
		$lconf = &get_logrotate_section($log);
		if ($log && $lconf) {
			$found{'logrotate'}++;
			print "<b>",&text('import_logrotate',
				    "<tt>$log</tt>"),"</b><p>\n";
			}
		else {
			print &text('import_nologrotate',
				    "<tt>$log</tt>"),"<p>\n";
			}
		}

	# Check for plugin features
	foreach $f (@feature_plugins) {
		$pname = &plugin_call($f, "feature_name");
		if (&plugin_call($f, "feature_import", $in{'dom'},
				 $user || $in{'user'},
				 $in{'db'})) {
			$found{$p}++;
			print "<b>",&text('import_plugin', $pname),"</b><p>\n";
			}
		else {
			print &text('import_noplugin', $pname),"<p>\n";
			}
		}

	# Tell if a Webmin user will be created
	if ($in{'webmin'}) {
		&require_acl();
		($wuser) = grep { $_->{'name'} eq ($user || $in{'user'}) }
				&acl::list_users();
		if ($wuser) {
			print "<b>",&text('import_webmin1',
				  "<tt>$wuser->{'name'}</tt>"),"</b><p>\n";
			}
		else {
			print "<b>",&text('import_webmin2',
				  "<tt>$wuser->{'name'}</tt>"),"</b><p>\n";
			}
		}
	else {
		print "$text{'import_nowebmin'}<p>\n";
		}

	# Work out if IP would be assigned
	if ($in{'virt'}) {
		print "<b>",&text('import_virt',
				  "<tt>$iface->{'fullname'}</tt>"),"</b><p>\n";
		}
	else {
		print "$text{'import_novirt'}<p>\n";
		}

	# Output form with confirm button
	print &check_clicks_function();
	print "<center><form action=import.cgi method=post>\n";
	foreach $i (keys %in) {
		print "<input type=hidden name=$i value='",
			&html_escape($in{$i}),"'>\n";
		}
	foreach $f (keys %found) {
		print "<input type=hidden name=found value='$f'>\n";
		}
	foreach $t (keys %dbnames) {
		print "<input type=hidden name=found_$t value='",
		      join(" ", @{$dbnames{$t}}),"'>\n";
		}
	print "<input type=hidden name=cruser value='$user'>\n";
	print "<input type=hidden name=crgroup value='$group'>\n";
	print "<b>$text{'import_rusure'}</b><p>\n";
	print "<input type=submit name=confirm value='$text{'import_ok'}' ",
	      "onClick='check_clicks(form)'>\n";
	print "</form></center>\n";
	}

&ui_print_footer("", $text{'index_return'});

sub error_exit
{
print "<b>",$text{'import_err'}," : ",@_,"</b><p>\n";
&ui_print_footer("", $text{'index_return'});
exit;
}