summaryrefslogtreecommitdiff
path: root/usr/src/lib/pam_modules/unix_auth/unix_auth.c
blob: c1d2e7d04238c4212affa4fd79b680dad56778a4 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <crypt.h>
#include <unistd.h>
#include <user_attr.h>
#include <auth_attr.h>
#include <userdefs.h>
#include <deflt.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <stdarg.h>

#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_impl.h>

#include <libintl.h>

#include <passwdutil.h>

#define	LOGINADMIN	"/etc/default/login"
#define	MAXTRYS		5

/*PRINTFLIKE2*/
void
error(pam_handle_t *pamh, char *fmt, ...)
{
	va_list ap;
	char messages[1][PAM_MAX_MSG_SIZE];

	va_start(ap, fmt);
	(void) vsnprintf(messages[0], sizeof (messages[0]), fmt, ap);
	(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL);
	va_end(ap);
}

static int
get_max_failed(char *user)
{
	char *val = NULL;
	userattr_t *uattr;
	int do_lock = 0;
	int retval = 0;
	char *p;
	void	*defp;

	if ((uattr = getusernam(user)) != NULL)
		val = kva_match(uattr->attr, USERATTR_LOCK_AFTER_RETRIES_KW);

	if (val != NULL) {
		do_lock = (strcasecmp(val, "yes") == 0);
	} else if ((defp = defopen_r(AUTH_POLICY)) != NULL) {
		int flags;
		flags = defcntl_r(DC_GETFLAGS, 0, defp);
		TURNOFF(flags, DC_CASE);
		(void) defcntl_r(DC_SETFLAGS, flags, defp);
		if ((p = defread_r("LOCK_AFTER_RETRIES=", defp)) != NULL)
			do_lock = (strcasecmp(p, "yes") == 0);
		defclose_r(defp);
	}

	if (uattr != NULL)
		free_userattr(uattr);

	if (do_lock) {
		retval = MAXTRYS;
		if ((defp = defopen_r(LOGINADMIN)) != NULL) {
			if ((p = defread_r("RETRIES=", defp)) != NULL)
				retval = atoi(p);
			defclose_r(defp);
		}
	}

	return (retval);
}

static void
display_warning(pam_handle_t *pamh, int failures, char *homedir)
{
	char hushpath[MAXPATHLEN];
	struct stat buf;

	(void) snprintf(hushpath, sizeof (hushpath), "%s/.hushlogin", homedir);
	if (stat(hushpath, &buf) == 0)
		return;

	if (failures == 1)
		error(pamh, "Warning: 1 failed login attempt since last "
		    "successful login.");
	else if (failures < FAILCOUNT_MASK)
		error(pamh, "Warning: %d failed login attempts since last "
		    "successful login.", failures);
	else
		error(pamh, "Warning: at least %d failed login attempts since "
		    "last successful login.", failures);
}

/*
 * int pam_sm_authenticate(pamh, flags, arc, argv)
 *
 * This routine verifies that the password as stored in the
 * PAM_AUTHTOK item is indeed the password that belongs to the user
 * as stored in PAM_USER.
 *
 * This routine will not establish Secure RPC Credentials, the pam_dhkeys
 * module should be stacked before us if Secure RPC Credentials are needed
 * to obtain passwords.
 */
int
pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	int	i;
	int	debug = 0;
	int	nowarn = (flags & PAM_SILENT) != 0;
	char	*user;
	char	*passwd;
	char	*rep_passwd;
	char	*crypt_passwd;
	char	*repository_name;
	struct pam_repository *auth_rep;
	pwu_repository_t *pwu_rep;
	attrlist attr_pw[4];
	int	result;
	int	server_policy = 0;
	int	old_failed_count;
	char	*homedir = NULL;
	int	dolock = 1;

	for (i = 0; i < argc; i++) {
		if (strcmp(argv[i], "debug") == 0)
			debug = 1;
		else if (strcmp(argv[i], "nowarn") == 0)
			nowarn = 1;
		else if (strcmp(argv[i], "server_policy") == 0)
			server_policy = 1;
		else if (strcmp(argv[i], "nolock") == 0)
			dolock = 0;
	}

	if (debug)
		__pam_log(LOG_AUTH | LOG_DEBUG,
		    "pam_unix_auth: entering pam_sm_authenticate()");

	if (pam_get_item(pamh, PAM_USER, (void **)&user) != PAM_SUCCESS) {
		__pam_log(LOG_AUTH | LOG_DEBUG, "pam_unix_auth: USER not set");
		return (PAM_SYSTEM_ERR);
	}

	if (user == NULL || *user == '\0') {
		__pam_log(LOG_AUTH | LOG_DEBUG,
		    "pam_unix_auth: USER NULL or empty!\n");
		return (PAM_USER_UNKNOWN);
	}

	if (pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd) != PAM_SUCCESS) {
		__pam_log(LOG_AUTH | LOG_DEBUG,
		    "pam_unix_auth: AUTHTOK not set!\n");
		return (PAM_SYSTEM_ERR);
	}

	result = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
	if (result == PAM_SUCCESS && auth_rep != NULL) {
		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
			return (PAM_BUF_ERR);
		pwu_rep->type = auth_rep->type;
		pwu_rep->scope = auth_rep->scope;
		pwu_rep->scope_len = auth_rep->scope_len;
	} else {
		pwu_rep = PWU_DEFAULT_REP;
	}

	/*
	 * Get password and the name of the repository where the
	 * password resides.
	 */
	attr_pw[0].type = ATTR_PASSWD;		attr_pw[0].next = &attr_pw[1];
	attr_pw[1].type = ATTR_REP_NAME;	attr_pw[1].next = &attr_pw[2];
	/*
	 * Also get the current number of failed logins; we use
	 * this later to determine whether we need to reset the count
	 * on a succesful authentication. We use the home-directory
	 * to look for .hushlogin in order to optionaly surpress the
	 * "failed attempts" message.
	 */
	attr_pw[2].type = ATTR_FAILED_LOGINS;	attr_pw[2].next = &attr_pw[3];
	attr_pw[3].type = ATTR_HOMEDIR;		attr_pw[3].next = NULL;

	result = __get_authtoken_attr(user, pwu_rep, attr_pw);

	if (pwu_rep != PWU_DEFAULT_REP)
		free(pwu_rep);

	if (result == PWU_NOT_FOUND) {
		__pam_log(LOG_AUTH | LOG_DEBUG,
		    "pam_unix_auth: user %s not found\n", user);
		return (PAM_USER_UNKNOWN);
	}

	if (result == PWU_DENIED) {
		__pam_log(LOG_AUTH | LOG_DEBUG,
		    "pam_unix_auth: failed to obtain attributes");
		return (PAM_PERM_DENIED);
	}

	if (result != PWU_SUCCESS)
		return (PAM_SYSTEM_ERR);

	rep_passwd = attr_pw[0].data.val_s;
	repository_name = attr_pw[1].data.val_s;
	old_failed_count = attr_pw[2].data.val_i;
	homedir = attr_pw[3].data.val_s;

	/*
	 * Chop off old SunOS-style password aging information.
	 *
	 * Note: old style password aging is only defined for UNIX-style
	 *	 crypt strings, hence the comma will always be at position 14.
	 * Note: This code is here because some other vendors might still
	 *	 support this style of password aging. If we don't remove
	 *	 the age field, no one will be able to login.
	 * XXX   yank this code when we're certain this "compatibility"
	 *	 isn't needed anymore.
	 */
	if (rep_passwd != NULL && rep_passwd[0] != '$' &&
	    strlen(rep_passwd) > 13 && rep_passwd[13] == ',')
		rep_passwd[13] = '\0';

	/* Is a password check required? */
	if (rep_passwd == NULL || *rep_passwd == '\0') {
		if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
			result = PAM_AUTH_ERR;
			__pam_log(LOG_AUTH | LOG_NOTICE,
			    "pam_unix_auth: empty password for %s not allowed.",
			    user);
			goto out;
		} else {
			result = PAM_SUCCESS;
			goto out;
		}
	}

	/*
	 * Password check *is* required. Make sure we have a valid
	 * pointer in PAM_AUTHTOK
	 */
	if (passwd == NULL) {
		result = PAM_AUTH_ERR;
		goto out;
	}

	if (server_policy &&
	    strcmp(repository_name, "files") != 0 &&
	    strcmp(repository_name, "nis") != 0) {
		result = PAM_IGNORE;
		goto out;
	}

	/* Now check the entered password */
	if ((crypt_passwd = crypt(passwd, rep_passwd)) == NULL) {
		switch (errno) {
		case ENOMEM:
			result = PAM_BUF_ERR;
			break;
		case ELIBACC:
			result = PAM_OPEN_ERR;
			break;
		default:
			result = PAM_SYSTEM_ERR;
		}
		goto out;
	}

	if (strcmp(crypt_passwd, rep_passwd) == 0)
		result = PAM_SUCCESS;
	else
		result = PAM_AUTH_ERR;

	/* Clear or increment failed failed count */
	if (dolock && (result == PAM_SUCCESS && old_failed_count > 0)) {
		old_failed_count = __rst_failed_count(user, repository_name);
		if (nowarn == 0 && old_failed_count > 0)
			display_warning(pamh, old_failed_count, homedir);
	} else if (dolock && result == PAM_AUTH_ERR) {
		int max_failed = get_max_failed(user);
		if (max_failed != 0) {
			if (__incr_failed_count(user, repository_name,
			    max_failed) == PWU_ACCOUNT_LOCKED)
				result = PAM_MAXTRIES;
		}
	}
out:
	if (rep_passwd)
		free(rep_passwd);
	if (repository_name)
		free(repository_name);
	if (homedir)
		free(homedir);
	return (result);
}

/*ARGSUSED*/
int
pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return (PAM_IGNORE);
}