xref: /illumos-gate/usr/src/lib/pam_modules/unix_auth/unix_auth.c (revision a38ddfee9c8c6b6c5a2947ff52fd2338362a4444)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #include <stdlib.h>
28 #include <pwd.h>
29 #include <shadow.h>
30 #include <syslog.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <crypt.h>
34 #include <unistd.h>
35 #include <user_attr.h>
36 #include <auth_attr.h>
37 #include <userdefs.h>
38 #include <deflt.h>
39 #include <sys/stat.h>
40 #include <sys/param.h>
41 #include <stdarg.h>
42 
43 #include <security/pam_appl.h>
44 #include <security/pam_modules.h>
45 #include <security/pam_impl.h>
46 
47 #include <libintl.h>
48 
49 #include <passwdutil.h>
50 
51 #define	LOGINADMIN	"/etc/default/login"
52 #define	MAXTRYS		5
53 
54 /*PRINTFLIKE2*/
55 void
56 error(pam_handle_t *pamh, char *fmt, ...)
57 {
58 	va_list ap;
59 	char messages[1][PAM_MAX_MSG_SIZE];
60 
61 	va_start(ap, fmt);
62 	(void) vsnprintf(messages[0], sizeof (messages[0]), fmt, ap);
63 	(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL);
64 	va_end(ap);
65 }
66 
67 static int
68 get_max_failed(char *user)
69 {
70 	char *val = NULL;
71 	userattr_t *uattr;
72 	int do_lock = 0;
73 	int retval = 0;
74 	char *p;
75 
76 	if ((uattr = getusernam(user)) != NULL)
77 		val = kva_match(uattr->attr, USERATTR_LOCK_AFTER_RETRIES_KW);
78 
79 	if (val != NULL)
80 		do_lock = (strcasecmp(val, "yes") == 0);
81 	else if (defopen(AUTH_POLICY) == 0) {
82 		int flags;
83 		flags = defcntl(DC_GETFLAGS, 0);
84 		TURNOFF(flags, DC_CASE);
85 		(void) defcntl(DC_SETFLAGS, flags);
86 		if ((p = defread("LOCK_AFTER_RETRIES=")) != NULL)
87 			do_lock = (strcasecmp(p, "yes") == 0);
88 		(void) defopen(NULL);
89 	}
90 
91 	if (uattr != NULL)
92 		free_userattr(uattr);
93 
94 	if (do_lock) {
95 		retval = MAXTRYS;
96 		if (defopen(LOGINADMIN) == 0) {
97 			if ((p = defread("RETRIES=")) != NULL)
98 				retval = atoi(p);
99 			(void) defopen(NULL);
100 		}
101 	}
102 
103 	return (retval);
104 }
105 
106 static void
107 display_warning(pam_handle_t *pamh, int failures, char *homedir)
108 {
109 	char hushpath[MAXPATHLEN];
110 	struct stat buf;
111 
112 	(void) snprintf(hushpath, sizeof (hushpath), "%s/.hushlogin", homedir);
113 	if (stat(hushpath, &buf) == 0)
114 		return;
115 
116 	if (failures == 1)
117 		error(pamh, "Warning: 1 failed login attempt since last "
118 		    "successful login.");
119 	else if (failures < FAILCOUNT_MASK)
120 		error(pamh, "Warning: %d failed login attempts since last "
121 		    "successful login.", failures);
122 	else
123 		error(pamh, "Warning: at least %d failed login attempts since "
124 		    "last successful login.", failures);
125 }
126 
127 /*
128  * int pam_sm_authenticate(pamh, flags, arc, argv)
129  *
130  * This routine verifies that the password as stored in the
131  * PAM_AUTHTOK item is indeed the password that belongs to the user
132  * as stored in PAM_USER.
133  *
134  * This routine will not establish Secure RPC Credentials. If these
135  * credentials are needed to obtain the password from the NIS+ service,
136  * the pam_dhkeys module should be stacked before us!
137  */
138 int
139 pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
140 {
141 	int	i;
142 	int	debug = 0;
143 	int	nowarn = (flags & PAM_SILENT) != 0;
144 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
145 	char	*user;
146 	char	*passwd;
147 	char	*rep_passwd;
148 	char	*crypt_passwd;
149 	char	*repository_name;
150 	struct pam_repository *auth_rep;
151 	pwu_repository_t *pwu_rep;
152 	attrlist attr_pw[4];
153 	int	result;
154 	int	server_policy = 0;
155 	int	old_failed_count;
156 	char	*homedir = NULL;
157 	int	dolock = 1;
158 
159 	for (i = 0; i < argc; i++) {
160 		if (strcmp(argv[i], "debug") == 0)
161 			debug = 1;
162 		else if (strcmp(argv[i], "nowarn") == 0)
163 			nowarn = 1;
164 		else if (strcmp(argv[i], "server_policy") == 0)
165 			server_policy = 1;
166 		else if (strcmp(argv[i], "nolock") == 0)
167 			dolock = 0;
168 	}
169 
170 	if (debug)
171 		__pam_log(LOG_AUTH | LOG_DEBUG,
172 		    "pam_unix_auth: entering pam_sm_authenticate()");
173 
174 	if (pam_get_item(pamh, PAM_USER, (void **)&user) != PAM_SUCCESS) {
175 		__pam_log(LOG_AUTH | LOG_DEBUG, "pam_unix_auth: USER not set");
176 		return (PAM_SYSTEM_ERR);
177 	}
178 
179 	if (user == NULL || *user == '\0') {
180 		__pam_log(LOG_AUTH | LOG_DEBUG,
181 		    "pam_unix_auth: USER NULL or empty!\n");
182 		return (PAM_USER_UNKNOWN);
183 	}
184 
185 	if (pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd) != PAM_SUCCESS) {
186 		__pam_log(LOG_AUTH | LOG_DEBUG,
187 		    "pam_unix_auth: AUTHTOK not set!\n");
188 		return (PAM_SYSTEM_ERR);
189 	}
190 
191 	result = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
192 	if (result == PAM_SUCCESS && auth_rep != NULL) {
193 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
194 			return (PAM_BUF_ERR);
195 		pwu_rep->type = auth_rep->type;
196 		pwu_rep->scope = auth_rep->scope;
197 		pwu_rep->scope_len = auth_rep->scope_len;
198 	} else {
199 		pwu_rep = PWU_DEFAULT_REP;
200 	}
201 
202 	/*
203 	 * Get password and the name of the repository where the
204 	 * password resides.
205 	 */
206 	attr_pw[0].type = ATTR_PASSWD;		attr_pw[0].next = &attr_pw[1];
207 	attr_pw[1].type = ATTR_REP_NAME;	attr_pw[1].next = &attr_pw[2];
208 	/*
209 	 * Also get the current number of failed logins; we use
210 	 * this later to determine whether we need to reset the count
211 	 * on a succesful authentication. We use the home-directory
212 	 * to look for .hushlogin in order to optionaly surpress the
213 	 * "failed attempts" message.
214 	 */
215 	attr_pw[2].type = ATTR_FAILED_LOGINS;	attr_pw[2].next = &attr_pw[3];
216 	attr_pw[3].type = ATTR_HOMEDIR;		attr_pw[3].next = NULL;
217 
218 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
219 
220 	if (pwu_rep != PWU_DEFAULT_REP)
221 		free(pwu_rep);
222 
223 	if (result == PWU_NOT_FOUND) {
224 		__pam_log(LOG_AUTH | LOG_DEBUG,
225 		    "pam_unix_auth: user %s not found\n", user);
226 		return (PAM_USER_UNKNOWN);
227 	}
228 
229 	if (result == PWU_DENIED) {
230 		__pam_log(LOG_AUTH | LOG_DEBUG,
231 		    "pam_unix_auth: failed to obtain attributes");
232 		return (PAM_PERM_DENIED);
233 	}
234 
235 	if (result != PWU_SUCCESS)
236 		return (PAM_SYSTEM_ERR);
237 
238 	rep_passwd = attr_pw[0].data.val_s;
239 	repository_name = attr_pw[1].data.val_s;
240 	old_failed_count = attr_pw[2].data.val_i;
241 	homedir = attr_pw[3].data.val_s;
242 
243 	/*
244 	 * Chop off old SunOS-style password aging information.
245 	 *
246 	 * Note: old style password aging is only defined for UNIX-style
247 	 *	 crypt strings, hence the comma will always be at position 14.
248 	 * Note: This code is here because some other vendors might still
249 	 *	 support this style of password aging. If we don't remove
250 	 *	 the age field, no one will be able to login.
251 	 * XXX   yank this code when we're certain this "compatibility"
252 	 *	 isn't needed anymore.
253 	 */
254 	if (rep_passwd != NULL && rep_passwd[0] != '$' &&
255 	    strlen(rep_passwd) > 13 && rep_passwd[13] == ',')
256 		rep_passwd[13] = '\0';
257 
258 	/* Is a password check required? */
259 	if (rep_passwd == NULL || *rep_passwd == '\0') {
260 		if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
261 			result = PAM_AUTH_ERR;
262 			__pam_log(LOG_AUTH | LOG_NOTICE,
263 			    "pam_unix_auth: empty password for %s not allowed.",
264 			    user);
265 			goto out;
266 		} else {
267 			result = PAM_SUCCESS;
268 			goto out;
269 		}
270 	}
271 
272 	/*
273 	 * Password check *is* required. Make sure we have a valid
274 	 * pointer in PAM_AUTHTOK
275 	 */
276 	if (passwd == NULL) {
277 		result = PAM_AUTH_ERR;
278 		goto out;
279 	}
280 
281 	/*
282 	 * "rep_passwd" holds the encrypted password.
283 	 * If, however, we detect that the password equals NOPWDRTR,
284 	 * while we've obtained it from NIS+, it
285 	 * means that the permissions on the NIS+ table are too tight
286 	 * for us to get the password without having Secure RPC
287 	 * Credentials. In that case, we log an error stating that
288 	 * the Secure RPC credential Module should be on the PAM stack
289 	 * before the unix_auth module. We also tell the user to go
290 	 * and inform the administrator of this error.
291 	 */
292 	if (strcmp(repository_name, "nisplus") == 0 &&
293 	    strcmp(rep_passwd, NOPWDRTR) == 0) {
294 		__pam_log(LOG_AUTH | LOG_ERR,
295 		    "pam_unix_auth: NIS+ permissions require that"
296 		    "the pam_dhkeys module is on the PAM stack before "
297 		    "pam_unix_auth");
298 		if (nowarn == 0) {
299 			(void) snprintf(messages[0], sizeof (messages[0]),
300 			    dgettext(TEXT_DOMAIN,
301 			    "NIS+ permissions are too tight. "
302 			    "Please inform your administrator."));
303 			(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
304 			    messages, NULL);
305 		}
306 		result = PAM_USER_UNKNOWN;
307 		goto out;
308 	}
309 
310 	if (server_policy &&
311 	    strcmp(repository_name, "files") != 0 &&
312 	    strcmp(repository_name, "nis") != 0 &&
313 	    strcmp(repository_name, "nisplus") != 0) {
314 		result = PAM_IGNORE;
315 		goto out;
316 	}
317 
318 	/* Now check the entered password */
319 	if ((crypt_passwd = crypt(passwd, rep_passwd)) == NULL) {
320 		switch (errno) {
321 		case ENOMEM:
322 			result = PAM_BUF_ERR;
323 			break;
324 		case ELIBACC:
325 			result = PAM_OPEN_ERR;
326 			break;
327 		default:
328 			result = PAM_SYSTEM_ERR;
329 		}
330 		goto out;
331 	}
332 
333 	if (strcmp(crypt_passwd, rep_passwd) == 0)
334 		result = PAM_SUCCESS;
335 	else
336 		result = PAM_AUTH_ERR;
337 
338 	/* Clear or increment failed failed count */
339 	if (dolock && (result == PAM_SUCCESS && old_failed_count > 0)) {
340 		old_failed_count = __rst_failed_count(user, repository_name);
341 		if (nowarn == 0 && old_failed_count > 0)
342 			display_warning(pamh, old_failed_count, homedir);
343 	} else if (dolock && result == PAM_AUTH_ERR) {
344 		int max_failed = get_max_failed(user);
345 		if (max_failed != 0) {
346 			if (__incr_failed_count(user, repository_name,
347 			    max_failed) == PWU_ACCOUNT_LOCKED)
348 				result = PAM_MAXTRIES;
349 		}
350 	}
351 out:
352 	if (rep_passwd)
353 		free(rep_passwd);
354 	if (repository_name)
355 		free(repository_name);
356 	if (homedir)
357 		free(homedir);
358 	return (result);
359 }
360 
361 /*ARGSUSED*/
362 int
363 pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
364 {
365 	return (PAM_IGNORE);
366 }
367