Project

General

Profile

Download (2.63 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * changepassword
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2016 Electric Sheep Fencing
6
 * Copyright (c) 2016-2018 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
require_once("config.inc");
23
require("auth.inc");
24
require_once("functions.inc");
25

    
26
global $g, $config, $argv, $userindex;
27
$userindex = index_users();
28

    
29
$args = array_slice($argv, 3);
30

    
31
$password = "";
32
$confpassword = "";
33
$username = "";
34

    
35
$fp = fopen('php://stdin', 'r');
36

    
37
// If the first parameter is empty, ask for username
38
if (empty($args[0])) {
39
	echo gettext("Enter username: ");
40
	$username = fgets($fp);
41
} else {
42
	$username = $args[0];
43
}
44
$username = trim($username);
45

    
46
// If the user does not exist, bail
47
$user =& getUserEntry($username);
48
if ($user == NULL) {
49
	printf(gettext("User '%s' does not exist.\n"), $username);
50
	exit(-1);
51
} else {
52
	printf(gettext("Changing password for '%s'.\n"), $username);
53
}
54

    
55
// If the user does exist, prompt for password
56
while (empty($password)) {
57
	echo gettext("New Password") . ": ";
58
	exec('/bin/stty -echo');
59
	$password = trim(fgets($fp));
60
	exec('/bin/stty echo');
61
	echo "\n";
62
}
63

    
64
// Confirm password
65
while (empty($confpassword)) {
66
	echo gettext("Confirm New Password") . ": ";
67
	exec('/bin/stty -echo');
68
	$confpassword = trim(fgets($fp));
69
	exec('/bin/stty echo');
70
	echo "\n";
71
}
72

    
73
// Check if user is disabled
74
if (is_account_disabled($username)) {
75
	echo gettext("Account is disabled, would you like to re-enable? [y|n]") . ": ";
76
	if (strcasecmp(chop(fgets($fp)), "y") == 0) {
77
		unset($user['disabled']);
78
	}
79
}
80
// Check if user is expired
81
if (is_account_expired($username)) {
82
	echo gettext("Account is expired, would you like to clear the expiration date? [y|n]") . ": ";
83
	if (strcasecmp(chop(fgets($fp)), "y") == 0) {
84
		unset($user['expires']);
85
	}
86
}
87

    
88
fclose($fp);
89

    
90
// Compare password and confirm
91
if ($password == $confpassword) {
92
	//Reset password
93
	local_user_set_password($user, $password);
94
	local_user_set($user);
95
	write_config(sprintf(gettext("password changed for user '%s' from console."), $username));
96
	exit(0);
97
} else {
98
	echo gettext("New and Confirm passwords did not match.") . "\n";
99
	exit(-1);
100
}
(1-1/24)