aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/multiuser/passwd.cpp
blob: 3c47d25296ac31bd334097ba2d7933845ea4777f (plain) (blame)
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
/*
 * Copyright 2008-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
 * Distributed under the terms of the MIT License.
 */


#include <errno.h>
#include <getopt.h>
#include <pwd.h>
#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

#include <OS.h>

#include <RegistrarDefs.h>
#include <user_group.h>
#include <util/KMessage.h>

#include <AutoDeleter.h>

#include "multiuser_utils.h"


extern const char *__progname;


static const char* kUsage =
	"Usage: %s [ <options> ] [ <user name> ]\n"
	"Change the password of the specified user.\n"
	"\n"
	"Options:\n"
	"  -d\n"
	"    Delete the password for the specified user.\n"
	"  -h, --help\n"
	"    Print usage info.\n"
	;

static void
print_usage_and_exit(bool error)
{
	fprintf(error ? stderr : stdout, kUsage, __progname);
	exit(error ? 1 : 0);
}


int
main(int argc, const char* const* argv)
{
	bool deletePassword = false;

	while (true) {
		static struct option sLongOptions[] = {
			{ "help", no_argument, 0, 'h' },
			{ 0, 0, 0, 0 }
		};

		opterr = 0; // don't print errors
		int c = getopt_long(argc, (char**)argv, "dh", sLongOptions, NULL);
		if (c == -1)
			break;


		switch (c) {
			case 'd':
				deletePassword = true;
				break;

			case 'h':
				print_usage_and_exit(false);
				break;

			default:
				print_usage_and_exit(true);
				break;
		}
	}

	if (optind + 1 < argc)
		print_usage_and_exit(true);

	const char* user = optind < argc ? argv[optind] : NULL;

	if (geteuid() != 0) {
		fprintf(stderr, "Error: You need to be root.\n");
		exit(1);
	}

	// this is a set-uid tool -- get the real UID
	uid_t uid = getuid();

	if (deletePassword) {
		if (uid != 0) {
			fprintf(stderr, "Error: Only root can delete users' passwords.\n");
			exit(1);
		}

		if (user == NULL) {
			fprintf(stderr, "Error: A user must be specified.\n");
			exit(1);
		}
	}

	// get the passwd entry
	struct passwd* passwd;
	if (user != NULL) {
		passwd = getpwnam(user);
		if (passwd == NULL) {
			fprintf(stderr, "Error: No user with name \"%s\".\n", user);
			exit(1);
		}

		if (uid != 0 && passwd->pw_uid != uid) {
			fprintf(stderr, "Error: Only root can change the passwd for other "
				"users.\n");
			exit(1);
		}
	} else {
		passwd = getpwuid(uid);
		if (passwd == NULL) {
			fprintf(stderr, "Error: Ugh! Couldn't get passwd entry for uid "
				"%d.\n", uid);
			exit(1);
		}

		user = passwd->pw_name;
	}

	// if not root, the user needs to authenticate
	if (uid != 0) {
		if (authenticate_user("old password: ", passwd, getspnam(user), 1,
				false) != B_OK) {
			exit(1);
		}
	}

	char password[LINE_MAX];
	char* encryptedPassword;

	if (deletePassword) {
		password[0] = '\0';
		encryptedPassword = password;
	} else {
		// read new password
		if (read_password("new password: ", password, sizeof(password), false)
				!= B_OK) {
			exit(1);
		}

		if (strlen(password) >= MAX_SHADOW_PWD_PASSWORD_LEN) {
			fprintf(stderr, "Error: The password is too long.\n");
			exit(1);
		}

		// read password again
		char repeatedPassword[LINE_MAX];
		if (read_password("repeat new password: ", repeatedPassword,
				sizeof(repeatedPassword), false) != B_OK) {
			exit(1);
		}

		// passwords need to match
		if (strcmp(password, repeatedPassword) != 0) {
			fprintf(stderr, "Error: passwords don't match\n");
			exit(1);
		}

		explicit_bzero(repeatedPassword, sizeof(repeatedPassword));

		// crypt it
		encryptedPassword = crypt(password, NULL);
		explicit_bzero(password, sizeof(password));
	}

	// prepare request for the registrar
	KMessage message(BPrivate::B_REG_UPDATE_USER);
	if (message.AddInt32("uid", passwd->pw_uid) != B_OK
		|| message.AddInt32("last changed", time(NULL)) != B_OK
		|| message.AddString("password", "x") != B_OK
		|| message.AddString("shadow password", encryptedPassword) != B_OK) {
		fprintf(stderr, "Error: Failed to construct message!\n");
		exit(1);
	}

	// send the request
	KMessage reply;
	status_t error = send_authentication_request_to_registrar(message, reply);
	if (error != B_OK) {
		fprintf(stderr, "Error: Failed to set the password: %s\n",
			strerror(error));
		exit(1);
	}

	return 0;
}