forked from ChristianBeer/ProFTPd-Admin
-
Notifications
You must be signed in to change notification settings - Fork 11
/
add_user.php
342 lines (318 loc) · 15.9 KB
/
add_user.php
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
<?php
/**
* This file is part of ProFTPd Admin
*
* @package ProFTPd-Admin
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
*
* @copyright Lex Brugman <[email protected]>
* @copyright Christian Beer <[email protected]>
* @copyright Ricardo Padilha <[email protected]>
*
*/
global $cfg;
include_once ("configs/config.php");
include_once ("includes/Session.php");
include_once ("includes/AdminClass.php");
$ac = new AdminClass($cfg);
$field_userid = $cfg['field_userid'];
$field_uid = $cfg['field_uid'];
$field_ugid = $cfg['field_ugid'];
$field_ad_gid = 'ad_gid';
$field_passwd = $cfg['field_passwd'];
$field_homedir = $cfg['field_homedir'];
$field_shell = $cfg['field_shell'];
$field_sshpubkey = $cfg['field_sshpubkey'];
$field_title = $cfg['field_title'];
$field_name = $cfg['field_name'];
$field_company = $cfg['field_company'];
$field_email = $cfg['field_email'];
$field_comment = $cfg['field_comment'];
$field_disabled = $cfg['field_disabled'];
$field_expiration = $cfg['field_expiration'];
$groups = $ac->get_groups();
if (count($groups) == 0) {
$errormsg = 'There are no groups in the database; please create at least one group before creating users.';
}
/* Data validation */
if (empty($errormsg) && !empty($_REQUEST["action"]) && $_REQUEST["action"] == "create") {
$errors = array();
/* user id validation */
if (empty($_REQUEST[$field_userid])
|| !preg_match($cfg['userid_regex'], $_REQUEST[$field_userid])
|| strlen($_REQUEST[$field_userid]) > $cfg['max_userid_length']) {
array_push($errors, 'Invalid user name; user name must contain only letters, numbers, hyphens, and underscores with a maximum of '.$cfg['max_userid_length'].' characters.');
}
/* uid validation */
if (empty($_REQUEST[$field_uid]) || !$ac->is_valid_id($_REQUEST[$field_uid])) {
array_push($errors, 'Invalid UID; must be a positive integer.');
}
if ($cfg['max_uid'] != -1 && $cfg['min_uid'] != -1) {
if ($_REQUEST[$field_uid] > $cfg['max_uid'] || $_REQUEST[$field_uid] < $cfg['min_uid']) {
array_push($errors, 'Invalid UID; UID must be between ' . $cfg['min_uid'] . ' and ' . $cfg['max_uid'] . '.');
}
} else if ($cfg['max_uid'] != -1 && $_REQUEST[$field_uid] > $cfg['max_uid']) {
array_push($errors, 'Invalid UID; UID must be at most ' . $cfg['max_uid'] . '.');
} else if ($cfg['min_uid'] != -1 && $_REQUEST[$field_uid] < $cfg['min_uid']) {
array_push($errors, 'Invalid UID; UID must be at least ' . $cfg['min_uid'] . '.');
}
/* gid validation */
if (empty($_REQUEST[$field_ugid]) || !$ac->is_valid_id($_REQUEST[$field_ugid])) {
array_push($errors, 'Invalid main group; GID must be a positive integer.');
}
/* password length validation */
if (strlen($_REQUEST[$field_passwd]) < $cfg['min_passwd_length']) {
array_push($errors, 'Password is too short; minimum length is '.$cfg['min_passwd_length'].' characters.');
}
/* home directory validation */
if (strlen($_REQUEST[$field_homedir]) <= 1) {
array_push($errors, 'Invalid home directory; home directory cannot be empty.');
}
/* shell validation */
if (strlen($_REQUEST[$field_shell]) <= 1) {
array_push($errors, 'Invalid shell; shell cannot be empty.');
}
/* SSH public key validation */
// if (strpos($_REQUEST[$field_sshpubkey]) != 0) {
// array_push($errors, 'Invalid ssh public key; SSH public key must start with "ssh-".');
// }
/* user name uniqueness validation */
if ($ac->check_username($_REQUEST[$field_userid])) {
array_push($errors, 'User name already exists; name must be unique.');
}
/* gid existance validation */
if (!$ac->check_gid($_REQUEST[$field_ugid])) {
array_push($errors, 'Main group does not exist; GID cannot be found in the database.');
}
/* data validation passed */
if (count($errors) == 0) {
$disabled = isset($_REQUEST[$field_disabled]) ? '1':'0';
$userdata = array($field_userid => $_REQUEST[$field_userid],
$field_uid => $_REQUEST[$field_uid],
$field_ugid => $_REQUEST[$field_ugid],
$field_passwd => $_REQUEST[$field_passwd],
$field_homedir => $_REQUEST[$field_homedir],
$field_shell => $_REQUEST[$field_shell],
$field_sshpubkey => $_REQUEST[$field_sshpubkey],
$field_title => $_REQUEST[$field_title],
$field_name => $_REQUEST[$field_name],
$field_email => $_REQUEST[$field_email],
$field_company => $_REQUEST[$field_company],
$field_comment => $_REQUEST[$field_comment],
$field_expiration => $_REQUEST[$field_expiration],
$field_disabled => $disabled);
if ($ac->add_user($userdata)) {
if (isset($_REQUEST[$field_ad_gid])) {
foreach ($_REQUEST[$field_ad_gid] as $g_key => $g_gid) {
if (!$ac->is_valid_id($g_gid)) {
$warnmsg = 'Adding additional group failed; at least one of the additional groups had an invalid GID.';
continue;
}
// XXX: fix error handling here
$ac->add_user_to_group($_REQUEST[$field_userid], $g_gid);
}
}
$infomsg = 'User "'.$_REQUEST[$field_userid].'" created successfully.';
} else {
$errormsg = 'User "'.$_REQUEST[$field_userid].'" creation failed; check log files.';
}
} else {
$errormsg = implode($errors, "<br />\n");
}
}
/* Form values */
if (isset($errormsg)) {
/* This is a failed attempt */
$userid = $_REQUEST[$field_userid];
$uid = $_REQUEST[$field_uid];
$ugid = $_REQUEST[$field_ugid];
$ad_gid = $_REQUEST[$field_ad_gid];
$passwd = $_REQUEST[$field_passwd];
$expiration = $_REQUEST[$field_expiration];
$homedir = $_REQUEST[$field_homedir];
$shell = $_REQUEST[$field_shell];
$sshpubkey = $_REQUEST[$field_sshpubkey];
$title = $_REQUEST[$field_title];
$name = $_REQUEST[$field_name];
$email = $_REQUEST[$field_email];
$company = $_REQUEST[$field_company];
$comment = $_REQUEST[$field_comment];
$disabled = isset($_REQUEST[$field_disabled]) ? '1' : '0';
} else {
/* Default values */
$userid = "";
if (empty($cfg['default_uid'])) {
$uid = $ac->get_last_uid() + 1;
} else {
$uid = $cfg['default_uid'];
}
if (empty($infomsg)) {
$ugid = "";
$ad_gid = array();
$shell = "/bin/false";
} else {
$ugid = $_REQUEST[$field_ugid];
$ad_gid = $_REQUEST[$field_ad_gid];
$shell = $_REQUEST[$field_shell];
}
$expiration = date("Y-m-d H:i:s", strtotime("+1 month", $time));
$sshpubkey = "";
$passwd = $ac->generate_random_string((int) $cfg['min_passwd_length']);
$homedir = $cfg['default_homedir'];
$title = "m";
$name = "";
$email = "";
$company = "";
$comment = "";
$disabled = '0';
}
include ("includes/header.php");
?>
<?php include ("includes/messages.php"); ?>
<div class="col-xs-12 col-sm-8 col-md-6 center">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Add user</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
<form role="form" class="form-horizontal" method="post" data-toggle="validator">
<!-- User name -->
<div class="form-group">
<label for="<?php echo $field_userid; ?>" class="col-sm-4 control-label">User name</label>
<div class="controls col-sm-8">
<input type="text" class="form-control" id="<?php echo $field_userid; ?>" name="<?php echo $field_userid; ?>" value="<?php echo $userid; ?>" placeholder="Enter a user name" maxlength="<?php echo $cfg['max_userid_length']; ?>" pattern="<?php echo substr($cfg['userid_regex'], 2, -3); ?>" required />
<p class="help-block"><small>Only letters, numbers, hyphens, and underscores. Maximum <?php echo $cfg['max_userid_length']; ?> characters.</small></p>
</div>
</div>
<!-- UID -->
<div class="form-group">
<label for="<?php echo $field_uid; ?>" class="col-sm-4 control-label">UID</label>
<div class="controls col-sm-8">
<input type="number" class="form-control" id="<?php echo $field_uid; ?>" name="<?php echo $field_uid; ?>" value="<?php echo $uid; ?>" min="1" placeholder="Enter a UID" required />
<p class="help-block"><small>Positive integer.</small></p>
</div>
</div>
<!-- Main group -->
<div class="form-group">
<label for="<?php echo $field_ugid; ?>" class="col-sm-4 control-label">Main group</label>
<div class="controls col-sm-8">
<select class="form-control multiselect" id="<?php echo $field_ugid; ?>" name="<?php echo $field_ugid; ?>" required>
<?php foreach ($groups as $g_gid => $g_group) { ?>
<option value="<?php echo $g_gid; ?>" <?php if ($ugid == $g_gid) { echo 'selected="selected"'; } ?>><?php echo $g_group; ?></option>
<?php } ?>
</select>
</div>
</div>
<!-- Additional groups -->
<div class="form-group">
<label for="<?php echo $field_ad_gid; ?>" class="col-sm-4 control-label">Additional groups</label>
<div class="controls col-sm-8">
<select class="form-control multiselect" id="<?php echo $field_ad_gid; ?>" name="<?php echo $field_ad_gid; ?>[]" multiple="multiple">
<?php foreach ($groups as $g_gid => $g_group) { ?>
<option value="<?php echo $g_gid; ?>" <?php if ($ad_gid && array_key_exists($g_gid, $ad_gid)) { echo 'selected="selected"'; } ?>><?php echo $g_group; ?></option>
<?php } ?>
</select>
</div>
</div>
<!-- Password -->
<div class="form-group">
<label for="<?php echo $field_passwd; ?>" class="col-sm-4 control-label">Password</label>
<div class="controls col-sm-8">
<input type="text" class="form-control" id="<?php echo $field_passwd; ?>" name="<?php echo $field_passwd; ?>" value="<?php echo $passwd; ?>" placeholder="Enter a password" minlength="<?php echo $cfg['min_passwd_length']; ?>" required />
<p class="help-block"><small>Minimum length <?php echo $cfg['min_passwd_length']; ?> characters.</small></p>
</div>
</div>
<!-- expiration -->
<div class="form-group">
<label for="<?php echo $field_expiration; ?>" class="col-sm-4 control-label">Expiry Date</label>
<div class="controls col-sm-8" >
<input type="text" class="form-control" id='<?php echo $field_expiration; ?>' name="<?php echo $field_expiration; ?>" value="<?php echo $expiration; ?>" maxlength="19" />
</div>
</div>
<!-- Home directory -->
<div class="form-group">
<label for="<?php echo $field_homedir; ?>" class="col-sm-4 control-label">Home directory</label>
<div class="controls col-sm-8">
<input type="text" class="form-control" id="<?php echo $field_homedir; ?>" name="<?php echo $field_homedir; ?>" value="<?php echo $homedir; ?>" placeholder="Enter a home directory" />
</div>
</div>
<!-- Shell -->
<div class="form-group">
<label for="<?php echo $field_shell; ?>" class="col-sm-4 control-label">Shell</label>
<div class="controls col-sm-8">
<input type="text" class="form-control" id="<?php echo $field_shell; ?>" name="<?php echo $field_shell; ?>" value="<?php echo $shell; ?>" placeholder="Enter the user's shell" />
</div>
</div>
<!-- SSH Public Key -->
<div class="form-group">
<label for="<?php echo $field_sshpubkey; ?>" class="col-sm-4 control-label">SSH Public Key</label>
<div class="controls col-sm-8">
<textarea class="form-control" id="<?php echo $field_sshpubkey; ?>" name="<?php echo $field_sshpubkey; ?>" rows="9" placeholder="<?php echo $placeholder_sshpubkey; ?>"><?php echo $sshpubkey; ?></textarea>
<p class="help-block"><small> RFC4716 format </small></p>
</div>
</div>
<!-- Title -->
<div class="form-group">
<label for="<?php echo $field_title; ?>" class="col-sm-4 control-label">Title</label>
<div class="col-sm-8">
<select class="form-control" id="<?php echo $field_title; ?>" name="<?php echo $field_title; ?>" required>
<option value="m" <?php if ($title == 'm') { echo 'selected="selected"'; } ?>>Mr.</option>
<option value="f" <?php if ($title == 'f') { echo 'selected="selected"'; } ?>>Ms.</option>
</select>
</div>
</div>
<!-- Real name -->
<div class="form-group">
<label for="<?php echo $field_name; ?>" class="col-sm-4 control-label">Name</label>
<div class="controls col-sm-8">
<input type="text" class="form-control" id="<?php echo $field_name; ?>" name="<?php echo $field_name; ?>" value="<?php echo $name; ?>" placeholder="Enter the user's real name" />
</div>
</div>
<!-- Email -->
<div class="form-group">
<label for="<?php echo $field_email; ?>" class="col-sm-4 control-label">E-mail</label>
<div class="controls col-sm-8">
<input type="email" class="form-control" id="<?php echo $field_email; ?>" name="<?php echo $field_email; ?>" value="<?php echo $email; ?>" placeholder="Enter the user's email" />
</div>
</div>
<!-- Company -->
<div class="form-group">
<label for="<?php echo $field_company; ?>" class="col-sm-4 control-label">Company</label>
<div class="controls col-sm-8">
<input type="text" class="form-control" id="<?php echo $field_company; ?>" name="<?php echo $field_company; ?>" value="<?php echo $company; ?>" placeholder="Enter a company or department" />
</div>
</div>
<!-- Comment -->
<div class="form-group">
<label for="<?php echo $field_comment; ?>" class="col-sm-4 control-label">Comment</label>
<div class="controls col-sm-8">
<textarea class="form-control" id="<?php echo $field_comment; ?>" name="<?php echo $field_comment; ?>" rows="3" placeholder="Enter a comment or additional information about the user"><?php echo $comment; ?></textarea>
</div>
</div>
<!-- Suspended -->
<div class="form-group">
<label for="<?php echo $field_disabled; ?>" class="col-sm-4 control-label">Status</label>
<div class="controls col-sm-8">
<div class="checkbox">
<label>
<input type="checkbox" id="<?php echo $field_disabled; ?>" name="<?php echo $field_disabled; ?>" <?php if ($disabled) { echo 'checked="checked"'; } ?> />Suspended account
</label>
</div>
</div>
</div>
<!-- Actions -->
<div class="form-group">
<div class="col-sm-12">
<a class="btn btn-default" href="users.php">« View users</a>
<button type="submit" class="btn btn-primary pull-right" name="action" value="create">Create user</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include ("includes/footer.php"); ?>