xref: /illumos-gate/usr/src/lib/pam_modules/unix_auth/unix_auth.c (revision 0bb073995ac5a95bd35f2dd790df1ea3d8c2d507)
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 		syslog(LOG_DEBUG,
172 		    "pam_unix_auth: entering pam_sm_authenticate()");
173 
174 	if (pam_get_item(pamh, PAM_USER, (void **)&user) != PAM_SUCCESS) {
175 		syslog(LOG_DEBUG, "pam_unix_auth: USER not set");
176 		return (PAM_SYSTEM_ERR);
177 	}
178 
179 	if (user == NULL || *user == '\0') {
180 		syslog(LOG_DEBUG, "pam_unix_auth: USER NULL or empty!\n");
181 		return (PAM_USER_UNKNOWN);
182 	}
183 
184 	if (pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd) != PAM_SUCCESS) {
185 		syslog(LOG_DEBUG, "pam_unix_auth: AUTHTOK not set!\n");
186 		return (PAM_SYSTEM_ERR);
187 	}
188 
189 	result = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
190 	if (result == PAM_SUCCESS && auth_rep != NULL) {
191 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
192 			return (PAM_BUF_ERR);
193 		pwu_rep->type = auth_rep->type;
194 		pwu_rep->scope = auth_rep->scope;
195 		pwu_rep->scope_len = auth_rep->scope_len;
196 	} else {
197 		pwu_rep = PWU_DEFAULT_REP;
198 	}
199 
200 	/*
201 	 * Get password and the name of the repository where the
202 	 * password resides.
203 	 */
204 	attr_pw[0].type = ATTR_PASSWD;		attr_pw[0].next = &attr_pw[1];
205 	attr_pw[1].type = ATTR_REP_NAME;	attr_pw[1].next = &attr_pw[2];
206 	/*
207 	 * Also get the current number of failed logins; we use
208 	 * this later to determine whether we need to reset the count
209 	 * on a succesful authentication. We use the home-directory
210 	 * to look for .hushlogin in order to optionaly surpress the
211 	 * "failed attempts" message.
212 	 */
213 	attr_pw[2].type = ATTR_FAILED_LOGINS;	attr_pw[2].next = &attr_pw[3];
214 	attr_pw[3].type = ATTR_HOMEDIR;		attr_pw[3].next = NULL;
215 
216 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
217 
218 	if (pwu_rep != PWU_DEFAULT_REP)
219 		free(pwu_rep);
220 
221 	if (result == PWU_NOT_FOUND) {
222 		syslog(LOG_DEBUG, "pam_unix_auth: user %s not found\n",
223 		    user);
224 		return (PAM_USER_UNKNOWN);
225 	}
226 
227 	if (result == PWU_DENIED) {
228 		syslog(LOG_DEBUG, "pam_unix_auth: failed to obtain attributes");
229 		return (PAM_PERM_DENIED);
230 	}
231 
232 	if (result != PWU_SUCCESS)
233 		return (PAM_SYSTEM_ERR);
234 
235 	rep_passwd = attr_pw[0].data.val_s;
236 	repository_name = attr_pw[1].data.val_s;
237 	old_failed_count = attr_pw[2].data.val_i;
238 	homedir = attr_pw[3].data.val_s;
239 
240 	/*
241 	 * Chop off old SunOS-style password aging information.
242 	 *
243 	 * Note: old style password aging is only defined for UNIX-style
244 	 *	 crypt strings, hence the comma will always be at position 14.
245 	 * Note: This code is here because some other vendors might still
246 	 *	 support this style of password aging. If we don't remove
247 	 *	 the age field, no one will be able to login.
248 	 * XXX   yank this code when we're certain this "compatibility"
249 	 *	 isn't needed anymore.
250 	 */
251 	if (rep_passwd != NULL && rep_passwd[0] != '$' &&
252 	    strlen(rep_passwd) > 13 && rep_passwd[13] == ',')
253 		rep_passwd[13] = '\0';
254 
255 	/* Is a password check required? */
256 	if (rep_passwd == NULL || *rep_passwd == '\0') {
257 		if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
258 			result = PAM_AUTH_ERR;
259 			goto out;
260 		} else {
261 			result = PAM_SUCCESS;
262 			goto out;
263 		}
264 	}
265 
266 	/*
267 	 * Password check *is* required. Make sure we have a valid
268 	 * pointer in PAM_AUTHTOK
269 	 */
270 	if (passwd == NULL) {
271 		result = PAM_AUTH_ERR;
272 		goto out;
273 	}
274 
275 	/*
276 	 * "rep_passwd" holds the encrypted password.
277 	 * If, however, we detect that the password equals NOPWDRTR,
278 	 * while we've obtained it from NIS+, it
279 	 * means that the permissions on the NIS+ table are too tight
280 	 * for us to get the password without having Secure RPC
281 	 * Credentials. In that case, we syslog an error stating that
282 	 * the Secure RPC credential Module should be on the PAM stack
283 	 * before the unix_auth module. We also tell the user to go
284 	 * and inform the administrator of this error.
285 	 */
286 	if (strcmp(repository_name, "nisplus") == 0 &&
287 	    strcmp(rep_passwd, NOPWDRTR) == 0) {
288 		syslog(LOG_ERR, "pam_unix_auth: NIS+ permissions require that"
289 		    "the pam_dhkeys module is on the PAM stack before "
290 		    "pam_unix_auth");
291 		if (nowarn == 0) {
292 			(void) snprintf(messages[0], sizeof (messages[0]),
293 			    dgettext(TEXT_DOMAIN,
294 			    "NIS+ permissions are too tight. "
295 			    "Please inform your administrator."));
296 			(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
297 			    messages, NULL);
298 		}
299 		result = PAM_USER_UNKNOWN;
300 		goto out;
301 	}
302 
303 	if (server_policy &&
304 	    strcmp(repository_name, "files") != 0 &&
305 	    strcmp(repository_name, "nis") != 0 &&
306 	    strcmp(repository_name, "nisplus") != 0) {
307 		result = PAM_IGNORE;
308 		goto out;
309 	}
310 
311 	/* Now check the entered password */
312 	if ((crypt_passwd = crypt(passwd, rep_passwd)) == NULL) {
313 		switch (errno) {
314 		case ENOMEM:
315 			result = PAM_BUF_ERR;
316 			break;
317 		case ELIBACC:
318 			result = PAM_OPEN_ERR;
319 			break;
320 		default:
321 			result = PAM_SYSTEM_ERR;
322 		}
323 		goto out;
324 	}
325 
326 	if (strcmp(crypt_passwd, rep_passwd) == 0)
327 		result = PAM_SUCCESS;
328 	else
329 		result = PAM_AUTH_ERR;
330 
331 	/* Clear or increment failed failed count */
332 	if (dolock && (result == PAM_SUCCESS && old_failed_count > 0)) {
333 		old_failed_count = __rst_failed_count(user, repository_name);
334 		if (nowarn == 0 && old_failed_count > 0)
335 			display_warning(pamh, old_failed_count, homedir);
336 	} else if (dolock && result == PAM_AUTH_ERR) {
337 		int max_failed = get_max_failed(user);
338 		if (max_failed != 0) {
339 			if (__incr_failed_count(user, repository_name,
340 			    max_failed) == PWU_ACCOUNT_LOCKED)
341 				result = PAM_MAXTRIES;
342 		}
343 	}
344 out:
345 	if (rep_passwd)
346 		free(rep_passwd);
347 	if (repository_name)
348 		free(repository_name);
349 	if (homedir)
350 		free(homedir);
351 	return (result);
352 }
353 
354 /*ARGSUSED*/
355 int
356 pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
357 {
358 	return (PAM_IGNORE);
359 }
360