GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/passwd/local_passwd.c Lines: 0 94 0.0 %
Date: 2016-12-06 Branches: 0 64 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: local_passwd.c,v 1.47 2016/05/08 20:27:43 tim Exp $	*/
2
3
/*-
4
 * Copyright (c) 1990 The Regents of the University of California.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. Neither the name of the University nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
32
#include <sys/types.h>
33
#include <sys/stat.h>
34
#include <sys/uio.h>
35
36
#include <err.h>
37
#include <errno.h>
38
#include <fcntl.h>
39
#include <pwd.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <signal.h>
43
#include <string.h>
44
#include <unistd.h>
45
#include <util.h>
46
#include <login_cap.h>
47
48
#define UNCHANGED_MSG	"Password unchanged.\n"
49
50
static uid_t uid;
51
extern int pwd_check(login_cap_t *, char *);
52
extern int pwd_gettries(login_cap_t *);
53
54
int local_passwd(char *, int);
55
char *getnewpasswd(struct passwd *, login_cap_t *, int);
56
void kbintr(int);
57
58
int
59
local_passwd(char *uname, int authenticated)
60
{
61
	struct passwd *pw, *opw;
62
	login_cap_t *lc;
63
	sigset_t fullset;
64
	time_t period;
65
	int i, pfd, tfd = -1;
66
	int pwflags = _PASSWORD_OMITV7;
67
68
	if (!(pw = getpwnam_shadow(uname))) {
69
		warnx("unknown user %s.", uname);
70
		return(1);
71
	}
72
73
	if (pledge("stdio rpath wpath cpath getpw tty id proc exec", NULL) == -1)
74
		err(1, "pledge");
75
76
	if ((opw = pw_dup(pw)) == NULL) {
77
		warn(NULL);
78
		return(1);
79
	}
80
	if ((lc = login_getclass(pw->pw_class)) == NULL) {
81
		warnx("unable to get login class for user %s.", uname);
82
		free(opw);
83
		return(1);
84
	}
85
86
	uid = authenticated ? pw->pw_uid : getuid();
87
	if (uid && uid != pw->pw_uid) {
88
		warnx("login/uid mismatch, username argument required.");
89
		free(opw);
90
		return(1);
91
	}
92
93
	/* Get the new password. */
94
	pw->pw_passwd = getnewpasswd(pw, lc, authenticated);
95
96
	if (pledge("stdio rpath wpath cpath getpw id proc exec", NULL) == -1)
97
		err(1, "pledge");
98
99
	/* Reset password change time based on login.conf. */
100
	period = (time_t)login_getcaptime(lc, "passwordtime",
101
	    (quad_t)0, (quad_t)0);
102
	if (period > 0) {
103
		pw->pw_change = time(NULL) + period;
104
	} else {
105
		/*
106
		 * If the pw change time is the same we only need
107
		 * to update the spwd.db file.
108
		 */
109
		if (pw->pw_change != 0)
110
			pw->pw_change = 0;
111
		else
112
			pwflags = _PASSWORD_SECUREONLY;
113
	}
114
115
	/* Drop user's real uid and block all signals to avoid a DoS. */
116
	setuid(0);
117
	sigfillset(&fullset);
118
	sigdelset(&fullset, SIGINT);
119
	sigprocmask(SIG_BLOCK, &fullset, NULL);
120
121
	if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1)
122
		err(1, "pledge");
123
124
	/* Get a lock on the passwd file and open it. */
125
	pw_init();
126
	for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
127
		if (i == 4)
128
			(void)fputs("Attempting to lock password file, "
129
			    "please wait or press ^C to abort", stderr);
130
		(void)signal(SIGINT, kbintr);
131
		if (i % 16 == 0)
132
			fputc('.', stderr);
133
		usleep(250000);
134
		(void)signal(SIGINT, SIG_IGN);
135
	}
136
	if (i >= 4)
137
		fputc('\n', stderr);
138
	pfd = open(_PATH_MASTERPASSWD, O_RDONLY | O_CLOEXEC, 0);
139
	if (pfd < 0)
140
		pw_error(_PATH_MASTERPASSWD, 1, 1);
141
142
	/* Update master.passwd file and rebuild spwd.db. */
143
	pw_copy(pfd, tfd, pw, opw);
144
	free(opw);
145
	if (pw_mkdb(uname, pwflags) < 0)
146
		pw_error(NULL, 0, 1);
147
148
	return(0);
149
}
150
151
char *
152
getnewpasswd(struct passwd *pw, login_cap_t *lc, int authenticated)
153
{
154
	static char hash[_PASSWORD_LEN];
155
	char *p, *pref;
156
	int tries, pwd_tries;
157
	char buf[1024];
158
	sig_t saveint, savequit;
159
160
	saveint = signal(SIGINT, kbintr);
161
	savequit = signal(SIGQUIT, kbintr);
162
163
	if (!authenticated) {
164
		(void)printf("Changing password for %s.\n", pw->pw_name);
165
		if (uid != 0 && pw->pw_passwd[0] != '\0') {
166
			p = getpass("Old password:");
167
			if (p == NULL || *p == '\0') {
168
				(void)printf(UNCHANGED_MSG);
169
				pw_abort();
170
				exit(p == NULL ? 1 : 0);
171
			}
172
			if (crypt_checkpass(p, pw->pw_passwd) != 0) {
173
				errno = EACCES;
174
				pw_error(NULL, 1, 1);
175
			}
176
		}
177
	}
178
179
	pwd_tries = pwd_gettries(lc);
180
181
	for (buf[0] = '\0', tries = 0;;) {
182
		p = getpass("New password:");
183
		if (p == NULL || *p == '\0') {
184
			(void)printf(UNCHANGED_MSG);
185
			pw_abort();
186
			exit(p == NULL ? 1 : 0);
187
		}
188
		if (strcmp(p, "s/key") == 0) {
189
			printf("That password collides with a system feature. Choose another.\n");
190
			continue;
191
		}
192
193
		if ((tries++ < pwd_tries || pwd_tries == 0) &&
194
		    pwd_check(lc, p) == 0)
195
			continue;
196
		strlcpy(buf, p, sizeof(buf));
197
		p = getpass("Retype new password:");
198
		if (p != NULL && strcmp(buf, p) == 0)
199
			break;
200
		(void)printf("Mismatch; try again, EOF to quit.\n");
201
	}
202
203
	(void)signal(SIGINT, saveint);
204
	(void)signal(SIGQUIT, savequit);
205
206
	pref = login_getcapstr(lc, "localcipher", NULL, NULL);
207
	if (crypt_newhash(buf, pref, hash, sizeof(hash)) != 0) {
208
		(void)printf("Couldn't generate hash.\n");
209
		pw_error(NULL, 0, 0);
210
	}
211
	free(pref);
212
	return hash;
213
}
214
215
/* ARGSUSED */
216
void
217
kbintr(int signo)
218
{
219
	write(STDOUT_FILENO, "\n", 1);
220
	write(STDOUT_FILENO, UNCHANGED_MSG, sizeof(UNCHANGED_MSG) - 1);
221
	_exit(0);
222
}