File: many_create.cgi

package info (click to toggle)
webmin 1.180-3sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 16,900 kB
  • ctags: 2,152
  • sloc: perl: 55,792; java: 1,362; sh: 980; ansic: 221; makefile: 191
file content (176 lines) | stat: -rwxr-xr-x 3,926 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
#!/usr/local/bin/perl
# many_create.cgi
# Create multiple users from an uploaded text file

require './user-lib.pl';
&ReadParseMime();
if ($in{'file'}) {
	$data = $in{'file'};
	}
elsif ($in{'local'}) {
	open(LOCAL, $in{'local'}) || &error($text{'many_elocal'});
	while(<LOCAL>) {
		$data .= $_;
		}
	close(LOCAL);
	}
else {
	&error($text{'many_efile'});
	}

&ui_print_header(undef, $text{'many_title'}, "");
$| = 1;

# Work out a good base UID
&lock_user_files();
&my_setpwent();
while(@tmp = &my_getpwent()) {
	$used{$tmp[2]}++;
	$taken{$tmp[0]}++;
	}
&my_endpwent();
$newuid = int($config{'base_uid'});

# Work out a good base GID
&my_setgrent();
while(@tmp = &my_getgrent()) {
	$gused{$tmp[2]}++;
	$gtaken{$tmp[0]}++;
	}
&my_endgrent();
$newgid = int($config{'base_gid'});

print "<pre>\n";
$lnum = 0;
foreach $line (split(/[\r\n]+/, $data)) {
	$lnum++;
	local @line = split(/:/, $line, -1);
	local %user;
	if (&passfiles_type() == 2) {
		# SYSV-style passwd and shadow information
		if (@line != 12) {
			print &text('many_elen', $lnum, 12),"\n";
			next;
			}
		$user{'min'} = $line[7];
		$user{'max'} = $line[8];
		$user{'warn'} = $line[9];
		$user{'inactive'} = $line[10];
		$user{'expire'} = $line[11];
		$user{'change'} = int(time() / (60*60*24));
		}
	elsif (&passfiles_type() == 1) {
		# BSD master.passwd information
		if (@line != 10) {
			print &text('many_elen', $lnum, 10),"\n";
			next;
			}
		$user{'class'} = $line[7];
		$user{'change'} = $line[8];
		$user{'expire'} = $line[9];
		}
	else {
		# Classic passwd file information
		if (@line != 7) {
			print &text('many_elen', $lnum, 7),"\n";
			next;
			}
		}

	# Parse common fields
	if (!$line[0]) {
		print &text('many_eline', $lnum),"\n";
		next;
		}
	$user{'user'} = $line[0];
	if ($taken{$user{'user'}}) {
		print &text('many_euser', $lnum, $user{'user'}),"\n";
		next;
		}
	if ($line[2] !~ /^\d+$/) {
		# make up a UID
		while($used{$newuid}) {
			$newuid++;
			}
		$user{'uid'} = $newuid;
		}
	else {
		# use the given UID!!
		$user{'uid'} = $line[2];
		}
	$used{$user{'uid'}}++;
	if ($line[5] !~ /^\//) {
		print &text('many_ehome', $lnum, $line[5]),"\n";
		next;
		}
	$user{'home'} = $line[5];
	if (!-r $line[6]) {
		print &text('many_eshell', $lnum, $line[6]),"\n";
		next;
		}
	$user{'shell'} = $line[6];
	$user{'real'} = $line[4];
	if ($line[3] !~ /^\d+$/) {
		# Need to create a new group for the user
		if ($gtaken{$user{'user'}}) {
			print &text('many_egtaken', $lnum, $user{'user'}),"\n";
			next;
			}
		while($gused{$newgid}) {
			$newgid++;
			}
		local %group;
		$group{'group'} = $user{'user'};
		$user{'gid'} = $group{'gid'} = $newgid;
		&create_group(\%group);
		}
	else {
		$user{'gid'} = $line[3];
		}

	# Create the user!
	if ($in{'makehome'} && !-d $user{'home'}) {
		if (!mkdir($user{'home'}, oct($config{'homedir_perms'}))) {
			print &text('many_emkdir', $user{'home'}, $!),"\n";
			}
		chmod(oct($config{'homedir_perms'}), $user{'home'});
		chown($user{'uid'}, $user{'gid'}, $user{'home'});
		}
	if ($line[1] eq 'x') {
		# No login allowed
		$user{'pass'} = $config{'lock_string'};
		$user{'passmode'} = 1;
		}
	elsif ($line[1] eq '') {
		# No password needed
		$user{'pass'} = '';
		$user{'passmode'} = 0;
		}
	else {
		# Normal password
		$salt = chr(int(rand(26))+65) . chr(int(rand(26))+65);
		$user{'pass'} = crypt($line[1], $salt);
		$user{'passmode'} = 3;
		$user{'plainpass'} = $line[1];
		}
	&create_user(\%user);
	&other_modules("useradmin_create_user", \%user);

	if ($in{'copy'} && $in{'makehome'}) {
		# Copy files to user's home directory
		local $uf = $config{'user_files'};
		if ($group = &my_getgrgid($user{'gid'})) {
			$uf =~ s/\$group/$group/g;
			}
		$uf =~ s/\$gid/$user{'gid'}/g;
		&copy_skel_files($uf, $user{'home'},
				 $user{'uid'}, $user{'gid'});
		}

	print "<b>",&text('many_ok',$user{'user'}),"</b>\n";
	}
print "</pre>\n";
&unlock_user_files();

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