summaryrefslogtreecommitdiff
path: root/usr/src/lib/pam_modules/passwd_auth/passwd_auth.c
blob: 051f5092245b333c1605d4b1de9f7b060831481e (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
/*
 * 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 <sys/types.h>
#include <sys/varargs.h>
#include <string.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include <crypt.h>
#include <pwd.h>
#include <libintl.h>

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

#include <passwdutil.h>

#include <sys/note.h>

/*PRINTFLIKE3*/
void
error(int nowarn, pam_handle_t *pamh, char *fmt, ...)
{
	va_list ap;
	char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];

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

/*
 * int pam_sm_authenticate(pamh, flags, argc, argv)
 *
 * Read authentication token from user.
 */
int
pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{

	char	*user;
	char	*password;
	char	*service;
	int	i;
	int	debug = 0;
	int	nowarn = 0;
	int	res;
	char	prompt[PAM_MAX_MSG_SIZE];
	char	*auth_user = NULL;
	int	retval;
	int	privileged;
	char	*rep_passwd = NULL;
	char	*repository_name = NULL;
	attrlist al[8];
	int	min;
	int	max;
	int	lstchg;
	int	server_policy = 0;

	pam_repository_t *auth_rep = NULL;
	pwu_repository_t *pwu_rep = NULL;

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

	if (flags & PAM_SILENT)
		nowarn = 1;

	if ((res = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS) {
		if (debug)
			syslog(LOG_DEBUG, "pam_passwd_auth: "
			    "get user failed: %s", pam_strerror(pamh, res));
		return (res);
	}

	if (user == NULL || *user == '\0') {
		syslog(LOG_ERR, "pam_passwd_auth: pam_sm_authenticate: "
		    "PAM_USER NULL or empty");
		return (PAM_SYSTEM_ERR);
	}

	res = pam_get_item(pamh, PAM_AUTHTOK, (void **)&password);
	if (res != PAM_SUCCESS)
		return (res);

	if (password != NULL)
		return (PAM_IGNORE);

	res = pam_get_item(pamh, PAM_SERVICE, (void **)&service);
	if (res != PAM_SUCCESS)
		return (res);

	res = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
	if (res != PAM_SUCCESS) {
		syslog(LOG_ERR, "pam_passwd_auth: pam_sm_authenticate: "
		    "error getting repository");
		return (PAM_SYSTEM_ERR);
	}

	if (auth_rep == NULL) {
		pwu_rep = PWU_DEFAULT_REP;
	} else {
		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;
	}

	res = __user_to_authenticate(user, pwu_rep, &auth_user, &privileged);
	if (res != PWU_SUCCESS) {
		if (res == PWU_NOT_FOUND)
			retval = PAM_USER_UNKNOWN;
		else if (res == PWU_DENIED)
			retval = PAM_PERM_DENIED;
		else if (res == PWU_REPOSITORY_ERROR) {
			syslog(LOG_NOTICE,
			    "pam_passwd_auth: detected unsupported "
			    "configuration in /etc/nsswitch.conf.");
			error(nowarn, pamh, dgettext(TEXT_DOMAIN, "%s: "
			    "Unsupported nsswitch entry for \"passwd:\"."
			    " Use \"-r repository \"."), service);
			retval = PAM_SYSTEM_ERR;
		} else
			retval = PAM_SYSTEM_ERR;
		if (debug)
			syslog(LOG_DEBUG, "passwd_auth: __user_to_authenticate "
			    "returned %d", retval);
		goto out;
	}

	if (auth_user == NULL) {		/* No authentication needed */
		if (debug)
			syslog(LOG_DEBUG,
			    "passwd_auth: no authentication needed.");
		retval = PAM_SUCCESS;
		goto out;
	}

	/*
	 * The password prompt differs between users updating their
	 * own password, and users updating other an user's password
	 */
	if (privileged) {
		/*
		 * TRANSLATION_NOTE
		 * The following string has a single space at the end
		 */
		(void) snprintf(prompt, sizeof (prompt),
		    dgettext(TEXT_DOMAIN, "Enter %s's password: "),
		    auth_user);
	} else {
		/*
		 * TRANSLATION_NOTE
		 * The following string has a single space at the end
		 */
		(void) snprintf(prompt, sizeof (prompt),
		    dgettext(TEXT_DOMAIN, "Enter existing login password: "));
	}

	retval = __pam_get_authtok(pamh, PAM_PROMPT, PAM_AUTHTOK, prompt,
	    &password);
	if (retval != PAM_SUCCESS)
		goto out;

	if (password == NULL) {
		syslog(LOG_ERR, "pam_passwd_auth: pam_sm_authenticate: "
		    "got NULL password from get_authtok()");
		retval = PAM_AUTH_ERR;
		goto out;
	}

	/* Privileged users can skip the tests that follow */
	if (privileged)
		goto setitem;

	/*
	 * Non privileged user: so we need to check the old password
	 * and possible restrictions on password changes.
	 */

	/* Get password and it's age from the repository specified */
	al[0].type = ATTR_PASSWD; al[0].next = &al[1];
	al[1].type = ATTR_MIN; al[1].next = &al[2];
	al[2].type = ATTR_MAX; al[2].next = &al[3];
	al[3].type = ATTR_LSTCHG; al[3].next = &al[4];
	al[4].type = ATTR_WARN; al[4].next = &al[5];
	al[5].type = ATTR_INACT; al[5].next = &al[6];
	al[6].type = ATTR_EXPIRE; al[6].next = &al[7];
	al[7].type = ATTR_REP_NAME; al[7].next = NULL;

	res = __get_authtoken_attr(auth_user, pwu_rep, al);

	if (res != PWU_SUCCESS) {
		retval = PAM_SYSTEM_ERR;
		goto out;
	}

	repository_name = al[7].data.val_s;

	/*
	 * if repository isn't files|nis, and user wants to follow server
	 * policy, return PAM_IGNORE
	 */
	if (server_policy &&
	    strcmp(repository_name, "files") != 0 &&
	    strcmp(repository_name, "nis") != 0) {
		retval = PAM_IGNORE;
		goto out;
	}

	rep_passwd = al[0].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, users won't be able to change their password.
	 * 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';

	if (strcmp(crypt(password, rep_passwd), rep_passwd) != 0) {
		retval = PAM_AUTH_ERR;
		goto out;
	}

	/*
	 * Now check to see if the user is allowed to change
	 * the password.
	 */
	min = al[1].data.val_i;
	max = al[2].data.val_i;
	lstchg = al[3].data.val_i;

	if (max != -1 && lstchg != 0) {
		/* aging is turned on, and a change is not forced */
		time_t daynow = DAY_NOW_32;
		if ((time_t)lstchg <= daynow) {
			/* Aged enough? */
			if (daynow < (time_t)(lstchg + min)) {
				error(nowarn, pamh, dgettext(TEXT_DOMAIN,
				    "%s: Sorry: less than %d days "
				    "since the last change."),
				    service, min);
				retval = PAM_PERM_DENIED;
				goto out;
			}

			/*
			 * users with min>max are not allowed to
			 * change their password.
			 */
			if (min > max) {
				error(nowarn, pamh, dgettext(TEXT_DOMAIN,
				    "%s: You may not change "
				    "this password."), service);
				retval = PAM_PERM_DENIED;
				goto out;
			}
		}
	}

setitem:

	retval = pam_set_item(pamh, PAM_AUTHTOK, (void *)password);

out:
	if (password) {
		(void) memset(password, 0, strlen(password));
		free(password);
	}
	if (rep_passwd) {
		(void) memset(rep_passwd, 0, strlen(rep_passwd));
		free(rep_passwd);
	}
	if (pwu_rep)
		free(pwu_rep);
	if (auth_user)
		free(auth_user);
	if (repository_name)
		free(repository_name);

	return (retval);
}

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