pam_unix.c revision 331722
1/*-
2 * Copyright 1998 Juniper Networks, Inc.
3 * All rights reserved.
4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software was developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote
21 *    products derived from this software without specific prior written
22 *    permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: stable/11/lib/libpam/modules/pam_unix/pam_unix.c 331722 2018-03-29 02:50:57Z eadler $");
39
40#include <sys/param.h>
41#include <sys/socket.h>
42#include <sys/time.h>
43#include <netinet/in.h>
44#include <arpa/inet.h>
45
46#include <login_cap.h>
47#include <netdb.h>
48#include <pwd.h>
49#include <stdlib.h>
50#include <string.h>
51#include <stdio.h>
52#include <syslog.h>
53#include <time.h>
54#include <unistd.h>
55
56#include <libutil.h>
57
58#ifdef YP
59#include <ypclnt.h>
60#endif
61
62#define PAM_SM_AUTH
63#define PAM_SM_ACCOUNT
64#define	PAM_SM_PASSWORD
65
66#include <security/pam_appl.h>
67#include <security/pam_modules.h>
68#include <security/pam_mod_misc.h>
69
70#define PASSWORD_HASH		"md5"
71#define DEFAULT_WARN		(2L * 7L * 86400L)  /* Two weeks */
72#define	SALTSIZE		32
73
74#define	LOCKED_PREFIX		"*LOCKED*"
75#define	LOCKED_PREFIX_LEN	(sizeof(LOCKED_PREFIX) - 1)
76
77static void makesalt(char []);
78
79static char password_hash[] =		PASSWORD_HASH;
80
81#define PAM_OPT_LOCAL_PASS	"local_pass"
82#define PAM_OPT_NIS_PASS	"nis_pass"
83
84/*
85 * authentication management
86 */
87PAM_EXTERN int
88pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
89    int argc __unused, const char *argv[] __unused)
90{
91	login_cap_t *lc;
92	struct passwd *pwd;
93	int retval;
94	const char *pass, *user, *realpw, *prompt;
95
96	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
97		user = getlogin();
98	} else {
99		retval = pam_get_user(pamh, &user, NULL);
100		if (retval != PAM_SUCCESS)
101			return (retval);
102	}
103	pwd = getpwnam(user);
104
105	PAM_LOG("Got user: %s", user);
106
107	if (pwd != NULL) {
108		PAM_LOG("Doing real authentication");
109		realpw = pwd->pw_passwd;
110		if (realpw[0] == '\0') {
111			if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) &&
112			    openpam_get_option(pamh, PAM_OPT_NULLOK))
113				return (PAM_SUCCESS);
114			PAM_LOG("Password is empty, using fake password");
115			realpw = "*";
116		}
117		lc = login_getpwclass(pwd);
118	} else {
119		PAM_LOG("Doing dummy authentication");
120		realpw = "*";
121		lc = login_getclass(NULL);
122	}
123	prompt = login_getcapstr(lc, "passwd_prompt", NULL, NULL);
124	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, prompt);
125	login_close(lc);
126	if (retval != PAM_SUCCESS)
127		return (retval);
128	PAM_LOG("Got password");
129	if (strnlen(pass, _PASSWORD_LEN + 1) > _PASSWORD_LEN) {
130		PAM_LOG("Password is too long, using fake password");
131		realpw = "*";
132	}
133	if (strcmp(crypt(pass, realpw), realpw) == 0)
134		return (PAM_SUCCESS);
135
136	PAM_VERBOSE_ERROR("UNIX authentication refused");
137	return (PAM_AUTH_ERR);
138}
139
140PAM_EXTERN int
141pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
142    int argc __unused, const char *argv[] __unused)
143{
144
145	return (PAM_SUCCESS);
146}
147
148/*
149 * account management
150 */
151PAM_EXTERN int
152pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
153    int argc __unused, const char *argv[] __unused)
154{
155	struct addrinfo hints, *res;
156	struct passwd *pwd;
157	struct timeval tp;
158	login_cap_t *lc;
159	time_t warntime;
160	int retval;
161	const char *user;
162	const void *rhost, *tty;
163	char rhostip[MAXHOSTNAMELEN] = "";
164
165	retval = pam_get_user(pamh, &user, NULL);
166	if (retval != PAM_SUCCESS)
167		return (retval);
168
169	if (user == NULL || (pwd = getpwnam(user)) == NULL)
170		return (PAM_SERVICE_ERR);
171
172	PAM_LOG("Got user: %s", user);
173
174	retval = pam_get_item(pamh, PAM_RHOST, &rhost);
175	if (retval != PAM_SUCCESS)
176		return (retval);
177
178	retval = pam_get_item(pamh, PAM_TTY, &tty);
179	if (retval != PAM_SUCCESS)
180		return (retval);
181
182	if (*pwd->pw_passwd == '\0' &&
183	    (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0)
184		return (PAM_NEW_AUTHTOK_REQD);
185
186	if (strncmp(pwd->pw_passwd, LOCKED_PREFIX, LOCKED_PREFIX_LEN) == 0)
187		return (PAM_AUTH_ERR);
188
189	lc = login_getpwclass(pwd);
190	if (lc == NULL) {
191		PAM_LOG("Unable to get login class for user %s", user);
192		return (PAM_SERVICE_ERR);
193	}
194
195	PAM_LOG("Got login_cap");
196
197	if (pwd->pw_change || pwd->pw_expire)
198		gettimeofday(&tp, NULL);
199
200	/*
201	 * Check pw_expire before pw_change - no point in letting the
202	 * user change the password on an expired account.
203	 */
204
205	if (pwd->pw_expire) {
206		warntime = login_getcaptime(lc, "warnexpire",
207		    DEFAULT_WARN, DEFAULT_WARN);
208		if (tp.tv_sec >= pwd->pw_expire) {
209			login_close(lc);
210			return (PAM_ACCT_EXPIRED);
211		} else if (pwd->pw_expire - tp.tv_sec < warntime &&
212		    (flags & PAM_SILENT) == 0) {
213			pam_error(pamh, "Warning: your account expires on %s",
214			    ctime(&pwd->pw_expire));
215		}
216	}
217
218	retval = PAM_SUCCESS;
219	if (pwd->pw_change) {
220		warntime = login_getcaptime(lc, "warnpassword",
221		    DEFAULT_WARN, DEFAULT_WARN);
222		if (tp.tv_sec >= pwd->pw_change) {
223			retval = PAM_NEW_AUTHTOK_REQD;
224		} else if (pwd->pw_change - tp.tv_sec < warntime &&
225		    (flags & PAM_SILENT) == 0) {
226			pam_error(pamh, "Warning: your password expires on %s",
227			    ctime(&pwd->pw_change));
228		}
229	}
230
231	/*
232	 * From here on, we must leave retval untouched (unless we
233	 * know we're going to fail), because we need to remember
234	 * whether we're supposed to return PAM_SUCCESS or
235	 * PAM_NEW_AUTHTOK_REQD.
236	 */
237
238	if (rhost && *(const char *)rhost != '\0') {
239		memset(&hints, 0, sizeof(hints));
240		hints.ai_family = AF_UNSPEC;
241		if (getaddrinfo(rhost, NULL, &hints, &res) == 0) {
242			getnameinfo(res->ai_addr, res->ai_addrlen,
243			    rhostip, sizeof(rhostip), NULL, 0,
244			    NI_NUMERICHOST);
245		}
246		if (res != NULL)
247			freeaddrinfo(res);
248	}
249
250	/*
251	 * Check host / tty / time-of-day restrictions
252	 */
253
254	if (!auth_hostok(lc, rhost, rhostip) ||
255	    !auth_ttyok(lc, tty) ||
256	    !auth_timeok(lc, time(NULL)))
257		retval = PAM_AUTH_ERR;
258
259	login_close(lc);
260
261	return (retval);
262}
263
264/*
265 * password management
266 *
267 * standard Unix and NIS password changing
268 */
269PAM_EXTERN int
270pam_sm_chauthtok(pam_handle_t *pamh, int flags,
271    int argc __unused, const char *argv[] __unused)
272{
273#ifdef YP
274	struct ypclnt *ypclnt;
275	const void *yp_domain, *yp_server;
276#endif
277	char salt[SALTSIZE + 1];
278	login_cap_t *lc;
279	struct passwd *pwd, *old_pwd;
280	const char *user, *old_pass, *new_pass;
281	char *encrypted;
282	time_t passwordtime;
283	int pfd, tfd, retval;
284
285	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
286		user = getlogin();
287	else {
288		retval = pam_get_user(pamh, &user, NULL);
289		if (retval != PAM_SUCCESS)
290			return (retval);
291	}
292	pwd = getpwnam(user);
293
294	if (pwd == NULL)
295		return (PAM_AUTHTOK_RECOVERY_ERR);
296
297	PAM_LOG("Got user: %s", user);
298
299	if (flags & PAM_PRELIM_CHECK) {
300
301		PAM_LOG("PRELIM round");
302
303		if (getuid() == 0 &&
304		    (pwd->pw_fields & _PWF_SOURCE) == _PWF_FILES)
305			/* root doesn't need the old password */
306			return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
307#ifdef YP
308		if (getuid() == 0 &&
309		    (pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
310
311			yp_domain = yp_server = NULL;
312			(void)pam_get_data(pamh, "yp_domain", &yp_domain);
313			(void)pam_get_data(pamh, "yp_server", &yp_server);
314
315			ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_server);
316			if (ypclnt == NULL)
317				return (PAM_BUF_ERR);
318
319			if (ypclnt_connect(ypclnt) == -1) {
320				ypclnt_free(ypclnt);
321				return (PAM_SERVICE_ERR);
322			}
323
324			retval = ypclnt_havepasswdd(ypclnt);
325			ypclnt_free(ypclnt);
326			if (retval == 1)
327				return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
328			else if (retval == -1)
329				return (PAM_SERVICE_ERR);
330		}
331#endif
332		if (pwd->pw_passwd[0] == '\0'
333		    && openpam_get_option(pamh, PAM_OPT_NULLOK)) {
334			/*
335			 * No password case. XXX Are we giving too much away
336			 * by not prompting for a password?
337			 * XXX check PAM_DISALLOW_NULL_AUTHTOK
338			 */
339			old_pass = "";
340			retval = PAM_SUCCESS;
341		} else {
342			retval = pam_get_authtok(pamh,
343			    PAM_OLDAUTHTOK, &old_pass, NULL);
344			if (retval != PAM_SUCCESS)
345				return (retval);
346		}
347		PAM_LOG("Got old password");
348		/* always encrypt first */
349		encrypted = crypt(old_pass, pwd->pw_passwd);
350		if (old_pass[0] == '\0' &&
351		    !openpam_get_option(pamh, PAM_OPT_NULLOK))
352			return (PAM_PERM_DENIED);
353		if (strcmp(encrypted, pwd->pw_passwd) != 0)
354			return (PAM_PERM_DENIED);
355	}
356	else if (flags & PAM_UPDATE_AUTHTOK) {
357		PAM_LOG("UPDATE round");
358
359		retval = pam_get_authtok(pamh,
360		    PAM_OLDAUTHTOK, &old_pass, NULL);
361		if (retval != PAM_SUCCESS)
362			return (retval);
363		PAM_LOG("Got old password");
364
365		/* get new password */
366		for (;;) {
367			retval = pam_get_authtok(pamh,
368			    PAM_AUTHTOK, &new_pass, NULL);
369			if (retval != PAM_TRY_AGAIN)
370				break;
371			pam_error(pamh, "Mismatch; try again, EOF to quit.");
372		}
373		PAM_LOG("Got new password");
374		if (retval != PAM_SUCCESS) {
375			PAM_VERBOSE_ERROR("Unable to get new password");
376			return (retval);
377		}
378
379		if (getuid() != 0 && new_pass[0] == '\0' &&
380		    !openpam_get_option(pamh, PAM_OPT_NULLOK))
381			return (PAM_PERM_DENIED);
382
383		if ((old_pwd = pw_dup(pwd)) == NULL)
384			return (PAM_BUF_ERR);
385
386		lc = login_getclass(pwd->pw_class);
387		if (login_setcryptfmt(lc, password_hash, NULL) == NULL)
388			openpam_log(PAM_LOG_ERROR,
389			    "can't set password cipher, relying on default");
390
391		/* set password expiry date */
392		pwd->pw_change = 0;
393		passwordtime = login_getcaptime(lc, "passwordtime", 0, 0);
394		if (passwordtime > 0)
395			pwd->pw_change = time(NULL) + passwordtime;
396
397		login_close(lc);
398		makesalt(salt);
399		pwd->pw_passwd = crypt(new_pass, salt);
400#ifdef YP
401		switch (old_pwd->pw_fields & _PWF_SOURCE) {
402		case _PWF_FILES:
403#endif
404			retval = PAM_SERVICE_ERR;
405			if (pw_init(NULL, NULL))
406				openpam_log(PAM_LOG_ERROR, "pw_init() failed");
407			else if ((pfd = pw_lock()) == -1)
408				openpam_log(PAM_LOG_ERROR, "pw_lock() failed");
409			else if ((tfd = pw_tmp(-1)) == -1)
410				openpam_log(PAM_LOG_ERROR, "pw_tmp() failed");
411			else if (pw_copy(pfd, tfd, pwd, old_pwd) == -1)
412				openpam_log(PAM_LOG_ERROR, "pw_copy() failed");
413			else if (pw_mkdb(pwd->pw_name) == -1)
414				openpam_log(PAM_LOG_ERROR, "pw_mkdb() failed");
415			else
416				retval = PAM_SUCCESS;
417			pw_fini();
418#ifdef YP
419			break;
420		case _PWF_NIS:
421			yp_domain = yp_server = NULL;
422			(void)pam_get_data(pamh, "yp_domain", &yp_domain);
423			(void)pam_get_data(pamh, "yp_server", &yp_server);
424			ypclnt = ypclnt_new(yp_domain,
425			    "passwd.byname", yp_server);
426			if (ypclnt == NULL) {
427				retval = PAM_BUF_ERR;
428			} else if (ypclnt_connect(ypclnt) == -1 ||
429			    ypclnt_passwd(ypclnt, pwd, old_pass) == -1) {
430				openpam_log(PAM_LOG_ERROR, "%s", ypclnt->error);
431				retval = PAM_SERVICE_ERR;
432			} else {
433				retval = PAM_SUCCESS;
434			}
435			ypclnt_free(ypclnt);
436			break;
437		default:
438			openpam_log(PAM_LOG_ERROR, "unsupported source 0x%x",
439			    pwd->pw_fields & _PWF_SOURCE);
440			retval = PAM_SERVICE_ERR;
441		}
442#endif
443		free(old_pwd);
444	}
445	else {
446		/* Very bad juju */
447		retval = PAM_ABORT;
448		PAM_LOG("Illegal 'flags'");
449	}
450
451	return (retval);
452}
453
454/* Mostly stolen from passwd(1)'s local_passwd.c - markm */
455
456static unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
457	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
458
459static void
460to64(char *s, long v, int n)
461{
462	while (--n >= 0) {
463		*s++ = itoa64[v&0x3f];
464		v >>= 6;
465	}
466}
467
468/* Salt suitable for traditional DES and MD5 */
469static void
470makesalt(char salt[SALTSIZE + 1])
471{
472	int i;
473
474	/* These are not really random numbers, they are just
475	 * numbers that change to thwart construction of a
476	 * dictionary.
477	 */
478	for (i = 0; i < SALTSIZE; i += 4)
479		to64(&salt[i], arc4random(), 4);
480	salt[SALTSIZE] = '\0';
481}
482
483PAM_MODULE_ENTRY("pam_unix");
484