Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

    
87
fclose($fp);
88

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