File: feature-unix.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 (266 lines) | stat: -rw-r--r-- 7,840 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
# Functions for managing a domain's Unix user

$feature_depends{'unix'} = [ 'dir' ];

# setup_unix(&domain)
# Creates the Unix user and group for a domain
sub setup_unix
{
local $tmpl = &get_template($_[0]->{'template'});
&require_useradmin();
local (%uinfo, %ginfo);

# Create the group
&$first_print(&text('setup_group', $_[0]->{'group'}));
&foreign_call($usermodule, "lock_user_files");
%ginfo = ( 'group', $_[0]->{'group'},
	   'gid', $_[0]->{'gid'} );
&foreign_call($usermodule, "set_group_envs", \%ginfo, 'CREATE_GROUP');
&foreign_call($usermodule, "making_changes");
&foreign_call($usermodule, "create_group", \%ginfo);
&foreign_call($usermodule, "made_changes");
if (!defined(getgrnam($_[0]->{'group'}))) {
	&$second_print($text{'setup_ecrgroup'});
	exit;
	}
&$second_print($text{'setup_done'});

# Then the user
&$first_print(&text('setup_user', $_[0]->{'user'}));
%uinfo = ( 'user', $_[0]->{'user'},
	   'uid', $_[0]->{'uid'},
	   'gid', $_[0]->{'ugid'},
	   'pass', &foreign_call($usermodule, "encrypt_password", $_[0]->{'pass'}),
	   'real', $_[0]->{'owner'},
	   'home', $_[0]->{'home'},
	   'shell', $config{'unix_shell'},
	   'mailbox', $_[0]->{'user'},
	   'dom', $_[0]->{'dom'},
	   'dom_prefix', substr($_[0]->{'dom'}, 0, 1),
	 );
&set_pass_change(\%user);
&foreign_call($usermodule, "set_user_envs", \%uinfo, 'CREATE_USER', $_[0]->{'pass'}, [ ]);
&foreign_call($usermodule, "making_changes");
&foreign_call($usermodule, "create_user", \%uinfo);
&foreign_call($usermodule, "made_changes");
&foreign_call($usermodule, "unlock_user_files");
if ($config{'other_doms'}) {
	&foreign_call($usermodule, "other_modules", "useradmin_create_user",
		      \%uinfo);
	}
if (!defined(getpwnam($_[0]->{'user'}))) {
	&$second_print($text{'setup_ecruser'});
	exit;
	}
&$second_print($text{'setup_done'});

# Set the user's quota
if ($config{'home_quotas'}) {
	&set_server_quotas($_[0]);
	}

&create_mail_file(\%user);
}

# modify_unix(&domain, &olddomain)
# Change the password and real name for a domain unix user
sub modify_unix
{
if (!$_[0]->{'parent'}) {
	&require_useradmin();
	local @allusers = &list_domain_users($_[1]);
	local ($uinfo) = grep { $_->{'user'} eq $_[1]->{'user'} } @allusers;
	if ($uinfo) {
		&foreign_call($usermodule, "lock_user_files");
		local %old = %$uinfo;
		&$first_print($text{'save_user'});
		$uinfo->{'real'} = $_[0]->{'owner'};
		if ($_[0]->{'pass_set'}) {
			# Update the Unix user's password
			local $enc = &foreign_call($usermodule,
				"encrypt_password", $_[0]->{'pass'});
			if ($d->{'disabled'}) {
				# Just keep for later use when enabling
				$d->{'disabled_oldpass'} = $enc;
				}
			else {
				# Set password now
				$uinfo->{'pass'} = $enc;
				}
			$uinfo->{'plainpass'} = $_[0]->{'pass'};
			&set_pass_change($uinfo);
			}

		if ($_[0]->{'user'} ne $_[1]->{'user'}) {
			# Unix user was re-named
			$uinfo->{'olduser'} = $_[1]->{'user'};
			$uinfo->{'user'} = $_[0]->{'user'};
			&rename_mail_file($uinfo, \%old);
			}

		if ($_[0]->{'home'} ne $_[1]->{'home'}) {
			# Home directory was changed
			$uinfo->{'home'} = $_[0]->{'home'};
			}

		&modify_user($uinfo, \%old, undef);

		if ($config{'other_doms'}) {
			&foreign_call($usermodule, "other_modules",
				      "useradmin_modify_user", $uinfo, \%old);
			}

		&$second_print($text{'setup_done'});
		&foreign_call($usermodule, "unlock_user_files");
		}

	if ($config{'home_quotas'} && $access{'edit'} == 1) {
		# Update his quotas (if changed)
		if ($_[0]->{'quota'} != $_[1]->{'quota'} ||
		    $_[0]->{'uquota'} != $_[1]->{'uquota'}) {
			&$first_print($text{'save_quota'});
			&set_server_quotas($_[0]);
			&$second_print($text{'setup_done'});
			}
		}
	}
}

# delete_unix(&domain)
# Delete the unix user and group for a domain
sub delete_unix
{
&require_useradmin();
local @allusers = &foreign_call($usermodule, "list_users");
local ($uinfo) = grep { $_->{'user'} eq $_[0]->{'user'} } @allusers;

if (!$_[0]->{'parent'}) {
	# Delete unix user
	&foreign_call($usermodule, "lock_user_files");
	if ($uinfo) {
		&$first_print($text{'delete_user'});
		&delete_user($uinfo, $_[0]);
		if ($config{'other_doms'}) {
			&foreign_call($usermodule, "other_modules",
				      "useradmin_delete_user", $uinfo);
			}
		&$second_print($text{'setup_done'});
		}

	# Delete unix group
	local @allgroups = &foreign_call($usermodule, "list_groups");
	local ($ginfo) = grep { $_->{'group'} eq $_[0]->{'group'} } @allgroups;
	if ($ginfo) {
		&$first_print($text{'delete_group'});
		&foreign_call($usermodule, "set_group_envs", $ginfo, 'DELETE_GROUP');
		&foreign_call($usermodule, "making_changes");
		&foreign_call($usermodule, "delete_group", $ginfo);
		&foreign_call($usermodule, "made_changes");
		&$second_print($text{'setup_done'});
		}
	&foreign_call($usermodule, "unlock_user_files");
	}
}

# check_unix_clash(&domain, [field])
sub check_unix_clash
{
return 0 if ($_[0]->{'parent'});	# user already exists!
if (!$_[1] || $_[1] eq 'user') {
	return 1 if (defined(getpwnam($_[0]->{'user'})));
	}
if (!$_[1] || $_[1] eq 'group') {
	return 1 if (defined(getgrnam($_[0]->{'group'})));
	}
return 0;
}

# disable_unix(&domain)
# Lock out the password of this domain's Unix user
sub disable_unix
{
if (!$_[0]->{'parent'}) {
	&require_useradmin();
	&foreign_call($usermodule, "lock_user_files");
	local @allusers = &foreign_call($usermodule, "list_users");
	local ($uinfo) = grep { $_->{'user'} eq $_[0]->{'user'} } @allusers;
	if ($uinfo) {
		&$first_print($text{'disable_unix'});
		&foreign_call($usermodule, "set_user_envs", $uinfo, 'MODIFY_USER', "");
		&foreign_call($usermodule, "making_changes");
		$_[0]->{'disabled_oldpass'} = $uinfo->{'pass'};
		$uinfo->{'pass'} = $uconfig{'lock_string'};
		&foreign_call($usermodule, "modify_user", $uinfo, $uinfo);
		&foreign_call($usermodule, "made_changes");
		&$second_print($text{'setup_done'});
		}
	&foreign_call($usermodule, "unlock_user_files");
	}
}

# enable_unix(&domain)
# Re-enable this domain's Unix user
sub enable_unix
{
if (!$_[0]->{'parent'}) {
	&require_useradmin();
	&foreign_call($usermodule, "lock_user_files");
	local @allusers = &foreign_call($usermodule, "list_users");
	local ($uinfo) = grep { $_->{'user'} eq $_[0]->{'user'} } @allusers;
	if ($uinfo) {
		&$first_print($text{'enable_unix'});
		&foreign_call($usermodule, "set_user_envs", $uinfo, 'MODIFY_USER', "");
		&foreign_call($usermodule, "making_changes");
		$uinfo->{'pass'} = $_[0]->{'disabled_oldpass'};
		delete($_[0]->{'disabled_oldpass'});
		&foreign_call($usermodule, "modify_user", $uinfo, $uinfo);
		&foreign_call($usermodule, "made_changes");
		&$second_print($text{'setup_done'});
		}
	&foreign_call($usermodule, "unlock_user_files");
	}
}

# backup_unix(&domain, file)
# Does nothing, since the domain's unix user details are already known
sub backup_unix
{
return 1;
}

# restore_unix(&domain, file, &options)
# Extracts the given tar file into a user's home directory
sub restore_unix
{
&$first_print($text{'restore_unixuser'});

# Also re-set quotas
if ($config{'home_quotas'}) {
	&set_server_quotas($_[0]);
	}

# And update password and description
&require_useradmin();
local @allusers = &foreign_call($usermodule, "list_users");
local ($uinfo) = grep { $_->{'user'} eq $_[0]->{'user'} } @allusers;
if ($uinfo && !$_[0]->{'parent'}) {
	&foreign_call($usermodule, "lock_user_files");
	$uinfo->{'real'} = $_[0]->{'owner'};
	local $enc = &foreign_call($usermodule, "encrypt_password",
			$_[0]->{'pass'});
	$uinfo->{'pass'} = $enc;
	&set_pass_change($uinfo);
	&foreign_call($usermodule, "set_user_envs",
		$uinfo, 'MODIFY_USER', $_[0]->{'pass'});
	&foreign_call($usermodule, "making_changes");
	&foreign_call($usermodule, "modify_user", $uinfo, $uinfo);
	&foreign_call($usermodule, "made_changes");
	&foreign_call($usermodule, "lock_user_files");
	}
&$second_print($text{'setup_done'});

return 1;
}

1;