summaryrefslogtreecommitdiff
path: root/usr/src/lib/pam_modules/ldap/ldap_acct_mgmt.c
blob: ee28f5a0ba3612e6adfb96ab74b9f23394c8f4cd (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "ldap_headers.h"

/*ARGSUSED*/
static void
ldap_cleanup(
	pam_handle_t *pamh,
	void *data,
	int pam_status)
{
	free((ldap_authtok_data *)data);
}

/*
 * warn_user_passwd_will_expire	- warn the user when the password will
 *					  expire.
 */

static void
warn_user_passwd_will_expire(
	pam_handle_t *pamh,
	int sec_until_expired)
{
	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
	int	days = 0, hours = 0;
	int	seconds_d = 0, seconds_h = 0;

	days = sec_until_expired / 86400;
	seconds_d = sec_until_expired % 86400;
	hours = (days * 24) + seconds_d / 3600;
	seconds_h = seconds_d % 3600;

	if (sec_until_expired <= (86400 * 2)) {
		if (seconds_d <= 3600 && days == 0)
			(void) snprintf(messages[0], sizeof (messages[0]),
			    dgettext(TEXT_DOMAIN,
			    "Your password will expire within one hour."));
		else
			(void) snprintf(messages[0], sizeof (messages[0]),
			    dgettext(TEXT_DOMAIN,
				"Your password will expire in %d hours."),
				(seconds_h == 0) ? hours : hours + 1);
	} else {
			(void) snprintf(messages[0], sizeof (messages[0]),
			    dgettext(TEXT_DOMAIN,
				"Your password will expire in %d days."),
				(seconds_d == 0) ? days : days + 1);
	}

	(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}

/*
 * display_acct_unlock_time - Display the time left for the account to
 * get auto unlocked after the maximum login failures has reached.
 */
static void
display_acct_unlock_time(pam_handle_t *pamh, int sec_b4_unlock)
{
	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
	int	days = 0, hours = 0;
	int	seconds_d = 0, seconds_h = 0;

	/* Account is locked forever */
	if (sec_b4_unlock == -1) {
		(void) snprintf(messages[0], sizeof (messages[0]),
		dgettext(TEXT_DOMAIN,
		"Your account is locked, please contact administrator."));
		(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1,
			messages, NULL);
		return;
	}

	days = sec_b4_unlock / 86400;
	seconds_d = sec_b4_unlock % 86400;
	hours = (days * 24) + seconds_d / 3600;
	seconds_h = seconds_d % 3600;

	if (sec_b4_unlock <= (86400 * 2)) {
		if (seconds_d <= 3600 && days == 0)
			(void) snprintf(messages[0], sizeof (messages[0]),
				dgettext(TEXT_DOMAIN,
				"Your account is locked and will be unlocked"
				" within one hour."));
		else
			(void) snprintf(messages[0], sizeof (messages[0]),
				dgettext(TEXT_DOMAIN,
				"Your account is locked and will be unlocked"
				" in %d hours."),
				(seconds_h == 0) ? hours : hours + 1);
	} else {
		(void) snprintf(messages[0], sizeof (messages[0]),
			dgettext(TEXT_DOMAIN,
			"Your account is locked and will be unlocked"
			" in %d days."),
			(seconds_d == 0) ? days : days + 1);
	}

	(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}

/*
 * warn_user_passwd_expired - warn the user that the password has expired
 */
static void
warn_user_passwd_expired(pam_handle_t *pamh, int grace)
{
	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];

	if (grace)
		(void) snprintf(messages[0], sizeof (messages[0]),
			dgettext(TEXT_DOMAIN,
			"Your password has expired. "
			"Number of grace logins allowed are %d."),
			grace);
	else
		(void) snprintf(messages[0], sizeof (messages[0]),
			dgettext(TEXT_DOMAIN,
			"Your password has expired."));

	(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}

/*
 * display_passwd_reset_msg - tell user that password has been reset by
 * administrator
 */
static void
display_passwd_reset_msg(pam_handle_t *pamh)
{
	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];

	(void) snprintf(messages[0], sizeof (messages[0]),
			dgettext(TEXT_DOMAIN,
			"Your password has been reset by administrator."));

	(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}

/*
 * Retreives account management related attributes for the user using
 * default binding and does local account checks .
 *
 * Return Value: PAM_SUCCESS - If account is valid, seconds param will have
 *				seconds left for password to expire
 *		 PAM_ACCT_EXPIRED - If account is inactive
 *		 PAM_NEW_AUTHTOK_REQD - Password is reset by admin
 *		 PAM_AUTHTOK_EXPIRED - User password has expired, grace
 *				param will have no. of grace logins allowed
 *		 PAM_MAXTRIES - If maximum failure of wrong password has reached
 *				seconds param will have no. of seconds for the
 *				account to get unlocked
 *		 PAM_AUTH_ERR - Failure return code
 */
static int
get_account_mgmt(char *user, int *seconds, int *grace)
{
	int rc	= PAM_AUTH_ERR;
	AcctUsableResponse_t	acctResp;

	(void *)memset((void*)&acctResp, 0, sizeof (acctResp));
	/* get the values for local account checking */
	if ((rc = __ns_ldap_getAcctMgmt(user, &acctResp))
		!= NS_LDAP_SUCCESS) {
		syslog(LOG_DEBUG,
			"__ns_ldap_getAcctMgmt() failed for %s with error %d",
				user, rc);
		return (PAM_AUTH_ERR);
	}

	if (acctResp.choice == 0) {
		/* should be able to login */
		*seconds =
			acctResp.AcctUsableResp.seconds_before_expiry;
		return (PAM_SUCCESS);
	} else if (acctResp.choice == 1) {
		/* cannot login */
		if (acctResp.AcctUsableResp.more_info.inactive)
			/* entry inactive */
			return (PAM_ACCT_EXPIRED);
		if (acctResp.AcctUsableResp.more_info.reset)
			/* password reset by administrator */
			return (PAM_NEW_AUTHTOK_REQD);
		if (acctResp.AcctUsableResp.more_info.expired) {
			/*
			 * password expired, check for grace logins.
			 */
			*grace =
				acctResp.AcctUsableResp.more_info.rem_grace;
			return (PAM_AUTHTOK_EXPIRED);
		}
		if (acctResp.AcctUsableResp.more_info.sec_b4_unlock) {
			/* max failures reached, seconds before unlock */
			*seconds =
				acctResp.AcctUsableResp.more_info.sec_b4_unlock;
			return (PAM_MAXTRIES);
		}
	}
	return (PAM_AUTH_ERR);
}

/*
 * pam_sm_acct_mgmt	main account managment routine.
 *			This routine relies on the LDAP
 *			directory server to provide the
 * 			password aging and account lockout
 * 			information. This is done by first
 *			trying to authenticate the user and
 *			then checking the password status
 *			returned.
 *
 *			Returns: module error or specific
 *			error on failure.
 */

int
pam_sm_acct_mgmt(
	pam_handle_t *pamh,
	int	flags,
	int	argc,
	const char **argv)
{

	char			*user = NULL;
	int			result = PAM_AUTH_ERR;
	int			debug = 0;
	int			i;
	char			*password = NULL;
	ns_cred_t		*credp = NULL;
	int			nowarn = 0;
	int			seconds = 0, grace = 0;
	ldap_authtok_data	*status;

	for (i = 0; i < argc; i++) {
		if (strcmp(argv[i], "debug") == 0)
			debug = 1;
		else if (strcasecmp(argv[i], "nowarn") == 0) {
			nowarn = 1;
			flags = flags | PAM_SILENT;
		}
		else
			syslog(LOG_DEBUG,
				"pam_ldap pam_sm_acct_mgmt: "
				"illegal option %s",
				argv[i]);
	}

	if ((result = pam_get_item(pamh, PAM_USER, (void **)&user))
							!= PAM_SUCCESS)
		goto out;

	if (debug)
		syslog(LOG_DEBUG,
			"ldap pam_sm_acct_mgmt(%s), flags = %x %s",
			(user)?user:"no-user", flags,
			(nowarn)? ", nowarn": "");

	if (user == NULL) {
		result = PAM_USER_UNKNOWN;
		goto out;
	}

	/* retrieve the password from the PAM handle */
	result = pam_get_item(pamh, PAM_AUTHTOK, (void **) &password);
	if (password == NULL) {
		if (debug)
			syslog(LOG_DEBUG, "ldap pam_sm_acct_mgmt: "
			    "no password for user %s", user);
		/* Do local account checking */
		result = get_account_mgmt(user, &seconds, &grace);
	} else {
		/* Try to authenticate to get password management info */
		result = authenticate(&credp, user,
				password, &seconds);
	}

	/*
	 * process the password management info.
	 * If user needs to change the password immediately,
	 * just return the rc.
	 * Otherwise, reset rc to the appropriate PAM error or
	 * warn the user about password expiration.
	 */
	if (result == PAM_MAXTRIES) {
		/* exceed retry limit, denied access to account */
		if (!(flags & PAM_SILENT))
			display_acct_unlock_time(pamh, seconds);
		result = PAM_PERM_DENIED;
	} else if (result == PAM_ACCT_EXPIRED)
		/* account is inactivated */
		result = PAM_ACCT_EXPIRED;
	else if (result == PAM_AUTHTOK_EXPIRED) {
		if (!(flags & PAM_SILENT))
			warn_user_passwd_expired(pamh, grace);
		/* password expired, check for grace logins */
		if (grace > 0)
			result = PAM_SUCCESS;
		else
			result = PAM_AUTHTOK_EXPIRED;
	} else if (result == PAM_NEW_AUTHTOK_REQD) {
		/* password has been reset by administrator */
		if (!(flags & PAM_SILENT))
			display_passwd_reset_msg(pamh);
		result = PAM_NEW_AUTHTOK_REQD;
	} else if (result == PAM_SUCCESS) {
		/*
		 * warn the user if the password
		 * is about to expire.
		 */
		if (!(flags & PAM_SILENT) &&
			seconds > 0)
			warn_user_passwd_will_expire(pamh,
				seconds);

	}

out:
	if (credp != NULL)
		(void) __ns_ldap_freeCred(&credp);

	/* store the password aging status in the pam handle */
	if (result != PAM_SUCCESS) {
		int pam_res;
		ldap_authtok_data *authtok_data;

		pam_res = pam_get_data(
			pamh, LDAP_AUTHTOK_DATA, (const void **)&authtok_data);

		if ((status = (ldap_authtok_data *)calloc
			(1, sizeof (ldap_authtok_data))) == NULL) {
			return (PAM_BUF_ERR);
		}

		if (pam_res == PAM_SUCCESS)
			(void) memcpy(status, authtok_data,
				sizeof (ldap_authtok_data));

		status->age_status = result;
		if (pam_set_data(pamh, LDAP_AUTHTOK_DATA, status, ldap_cleanup)
							!= PAM_SUCCESS) {
			free(status);
			return (PAM_SERVICE_ERR);
		}
	}

	return (result);
}