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