xref: /openbsd-src/lib/libc/gen/authenticate.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: authenticate.c,v 1.15 2005/12/19 17:07:43 millert Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Berkeley Software Design,
17  *	Inc.
18  * 4. The name of Berkeley Software Design, Inc.  may not be used to endorse
19  *    or promote products derived from this software without specific prior
20  *    written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	BSDI $From: authenticate.c,v 2.21 1999/09/08 22:33:26 prb Exp $
35  */
36 #include <sys/param.h>
37 #include <sys/stat.h>
38 
39 #include <ctype.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #include <login_cap.h>
43 #include <paths.h>
44 #include <pwd.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <syslog.h>
50 #include <unistd.h>
51 
52 #include <bsd_auth.h>
53 
54 static int _auth_checknologin(login_cap_t *, int);
55 
56 char *
57 auth_mkvalue(char *value)
58 {
59 	char *big, *p;
60 
61 	big = malloc(strlen(value) * 4 + 1);
62 	if (big == NULL)
63 		return (NULL);
64 	/*
65 	 * XXX - There should be a more standardized
66 	 * routine for doing this sort of thing.
67 	 */
68 	for (p = big; *value; ++value) {
69 		switch (*value) {
70 		case '\r':
71 			*p++ = '\\';
72 			*p++ = 'r';
73 			break;
74 		case '\n':
75 			*p++ = '\\';
76 			*p++ = 'n';
77 			break;
78 		case '\\':
79 			*p++ = '\\';
80 			*p++ = *value;
81 			break;
82 		case '\t':
83 		case ' ':
84 			if (p == big)
85 				*p++ = '\\';
86 			*p++ = *value;
87 			break;
88 		default:
89 			if (!isprint(*value)) {
90 				*p++ = '\\';
91 				*p++ = ((*value >> 6) & 0x3) + '0';
92 				*p++ = ((*value >> 3) & 0x7) + '0';
93 				*p++ = ((*value     ) & 0x7) + '0';
94 			} else
95 				*p++ = *value;
96 			break;
97 		}
98 	}
99 	*p = '\0';
100 	return (big);
101 }
102 
103 void
104 auth_checknologin(login_cap_t *lc)
105 {
106 	if (_auth_checknologin(lc, 1))
107 		exit(1);
108 }
109 
110 static int
111 _auth_checknologin(login_cap_t *lc, int print)
112 {
113 	struct stat sb;
114 	char *nologin;
115 	int mustfree;
116 
117 	if (login_getcapbool(lc, "ignorenologin", 0))
118 		return (0);
119 
120 	/*
121 	 * If we fail to get the nologin file due to a database error,
122 	 * assume there should have been one...
123 	 */
124 	nologin = login_getcapstr(lc, "nologin", "", NULL);
125 	mustfree = nologin && *nologin != '\0';
126 	if (nologin == NULL)
127 		goto print_nologin;
128 
129 	/* First try the nologin file specified in login.conf. */
130 	if (*nologin != '\0' && stat(nologin, &sb) == 0)
131 		goto print_nologin;
132 	if (mustfree) {
133 		free(nologin);
134 		mustfree = 0;
135 	}
136 
137 	/* If that doesn't exist try _PATH_NOLOGIN. */
138 	if (stat(_PATH_NOLOGIN, &sb) == 0) {
139 		nologin = _PATH_NOLOGIN;
140 		goto print_nologin;
141 	}
142 
143 	/* Couldn't stat any nologin files, must be OK to login. */
144 	return (0);
145 
146 print_nologin:
147 	if (print) {
148 		if (!nologin || *nologin == '\0' || auth_cat(nologin) == 0) {
149 			puts("Logins are not allowed at this time.");
150 			fflush(stdout);
151 		}
152 	}
153 	if (mustfree)
154 		free(nologin);
155 	return (-1);
156 }
157 
158 int
159 auth_cat(char *file)
160 {
161 	int fd, nchars;
162 	char tbuf[8192];
163 
164 	if ((fd = open(file, O_RDONLY, 0)) < 0)
165 		return (0);
166 	while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
167 		(void)write(fileno(stdout), tbuf, nchars);
168 	(void)close(fd);
169 	return (1);
170 }
171 
172 int
173 auth_approval(auth_session_t *as, login_cap_t *lc, char *name, char *type)
174 {
175 	int close_on_exit, close_lc_on_exit;
176 	struct passwd *pwd;
177 	char *approve, *s, path[MAXPATHLEN];
178 
179 	pwd = NULL;
180 	close_on_exit = as == NULL;
181 	close_lc_on_exit = lc == NULL;
182 
183 	if (as != NULL && name == NULL)
184 		name = auth_getitem(as, AUTHV_NAME);
185 
186 	if (as != NULL)
187 		pwd = auth_getpwd(as);
188 
189 	if (pwd == NULL) {
190 		if (name != NULL)
191 			pwd = getpwnam(name);
192 		else {
193 			if ((pwd = getpwuid(getuid())) == NULL) {
194 				syslog(LOG_ERR, "no such user id %u", getuid());
195 				_warnx("cannot approve who we don't recognize");
196 				return (0);
197 			}
198 			name = pwd->pw_name;
199 		}
200 	}
201 
202 	if (name == NULL)
203 		name = pwd->pw_name;
204 
205 	if (lc == NULL) {
206 		if (strlen(name) >= MAXPATHLEN) {
207 			syslog(LOG_ERR, "username to login %.*s...",
208 			    MAXPATHLEN, name);
209 			_warnx("username too long");
210 			return (0);
211 		}
212 		if (pwd == NULL && (approve = strchr(name, '.')) != NULL) {
213 			strlcpy(path, name, sizeof path);
214 			path[approve-name] = '\0';
215 			pwd = getpwnam(name);
216 		}
217 		lc = login_getclass(pwd ? pwd->pw_class : NULL);
218 		if (lc == NULL) {
219 			_warnx("unable to classify user");
220 			return (0);
221 		}
222 	}
223 
224 	if (!type)
225 		type = LOGIN_DEFSERVICE;
226 	else {
227 		if (strncmp(type, "approve-", 8) == 0)
228 			type += 8;
229 
230 		snprintf(path, sizeof(path), "approve-%s", type);
231 	}
232 
233 	if ((approve = login_getcapstr(lc, s = path, NULL, NULL)) == NULL)
234 		approve = login_getcapstr(lc, s = "approve", NULL, NULL);
235 
236 	if (approve && approve[0] != '/') {
237 		if (close_lc_on_exit)
238 			login_close(lc);
239 		syslog(LOG_ERR, "Invalid %s script: %s", s, approve);
240 		_warnx("invalid path to approval script");
241 		free(approve);
242 		return (0);
243 	}
244 
245 	if (as == NULL && (as = auth_open()) == NULL) {
246 		if (close_lc_on_exit)
247 			login_close(lc);
248 		syslog(LOG_ERR, "%m");
249 		_warn(NULL);
250 		if (approve)
251 			free(approve);
252 		return (0);
253 	}
254 
255 	auth_setstate(as, AUTH_OKAY);
256 	if (auth_setitem(as, AUTHV_NAME, name) < 0) {
257 		syslog(LOG_ERR, "%m");
258 		_warn(NULL);
259 		goto out;
260 	}
261 	if (auth_check_expire(as) < 0)	/* is this account expired */
262 		goto out;
263 	if (_auth_checknologin(lc,
264 	    auth_getitem(as, AUTHV_INTERACTIVE) != NULL)) {
265 		auth_setstate(as, (auth_getstate(as) & ~AUTH_ALLOW));
266 		goto out;
267 	}
268 	if (login_getcapbool(lc, "requirehome", 0) && pwd && pwd->pw_dir &&
269 	    pwd->pw_dir[0]) {
270 		struct stat sb;
271 
272 		if (stat(pwd->pw_dir, &sb) < 0 ||
273 		    (sb.st_mode & 0170000) != S_IFDIR ||
274 		    (pwd->pw_uid && sb.st_uid == pwd->pw_uid &&
275 		    (sb.st_mode & S_IXUSR) == 0)) {
276 			auth_setstate(as, (auth_getstate(as) & ~AUTH_ALLOW));
277 			goto out;
278 		}
279 	}
280 	if (approve)
281 		auth_call(as, approve, strrchr(approve, '/') + 1, name,
282 		    lc->lc_class, type, (char *)NULL);
283 
284 out:
285 	if (approve)
286 		free(approve);
287 	if (close_lc_on_exit)
288 		login_close(lc);
289 
290 	if (close_on_exit)
291 		return (auth_close(as));
292 	return (auth_getstate(as) & AUTH_ALLOW);
293 }
294 
295 auth_session_t *
296 auth_usercheck(char *name, char *style, char *type, char *password)
297 {
298 	char namebuf[MAXLOGNAME + 1 + NAME_MAX + 1];
299 	auth_session_t *as;
300 	login_cap_t *lc;
301 	struct passwd *pwd;
302 	char *sep, save;
303 
304 	if (strlcpy(namebuf, name, sizeof(namebuf)) >= sizeof(namebuf))
305 		return (NULL);
306 	name = namebuf;
307 
308 	/*
309 	 * Split up user:style names if we were not given a style
310 	 */
311 	if (style == NULL && (style = strchr(name, ':')) != NULL)
312 		*style++ = '\0';
313 
314 	/*
315 	 * Cope with user[./]instance.  We are only using this to get
316 	 * the class so it is okay if we strip a root instance
317 	 * The actual login script will pay attention to the instance.
318 	 */
319 	if ((pwd = getpwnam(name)) == NULL) {
320 		if ((sep = strpbrk(name, "./")) != NULL) {
321 			save = *sep;
322 			*sep = '\0';
323 			pwd = getpwnam(name);
324 			*sep = save;
325 		}
326 	}
327 	if ((lc = login_getclass(pwd ? pwd->pw_class : NULL)) == NULL)
328 		return (NULL);
329 
330 	if ((style = login_getstyle(lc, style, type)) == NULL) {
331 		login_close(lc);
332 		return (NULL);
333 	}
334 
335 	if (password) {
336 		if ((as = auth_open()) == NULL) {
337 			login_close(lc);
338 			return (NULL);
339 		}
340 		auth_setitem(as, AUTHV_SERVICE, "response");
341 		auth_setdata(as, "", 1);
342 		auth_setdata(as, password, strlen(password) + 1);
343 	} else
344 		as = NULL;
345 	as = auth_verify(as, style, name, lc->lc_class, (char *)NULL);
346 	login_close(lc);
347 	return (as);
348 }
349 
350 int
351 auth_userokay(char *name, char *style, char *type, char *password)
352 {
353 	auth_session_t *as;
354 
355 	as = auth_usercheck(name, style, type, password);
356 
357 	return (as != NULL ? auth_close(as) : 0);
358 }
359 
360 auth_session_t *
361 auth_userchallenge(char *name, char *style, char *type, char **challengep)
362 {
363 	char namebuf[MAXLOGNAME + 1 + NAME_MAX + 1];
364 	auth_session_t *as;
365 	login_cap_t *lc;
366 	struct passwd *pwd;
367 	char *sep, save;
368 
369 	if (strlen(name) >= sizeof(namebuf))
370 		return (NULL);
371 	strlcpy(namebuf, name, sizeof namebuf);
372 	name = namebuf;
373 
374 	/*
375 	 * Split up user:style names if we were not given a style
376 	 */
377 	if (style == NULL && (style = strchr(name, ':')) != NULL)
378 		*style++ = '\0';
379 
380 	/*
381 	 * Cope with user[./]instance.  We are only using this to get
382 	 * the class so it is okay if we strip a root instance
383 	 * The actual login script will pay attention to the instance.
384 	 */
385 	if ((pwd = getpwnam(name)) == NULL) {
386 		if ((sep = strpbrk(name, "./")) != NULL) {
387 			save = *sep;
388 			*sep = '\0';
389 			pwd = getpwnam(name);
390 			*sep = save;
391 		}
392 	}
393 	if ((lc = login_getclass(pwd ? pwd->pw_class : NULL)) == NULL)
394 		return (NULL);
395 
396 	if ((style = login_getstyle(lc, style, type)) == NULL ||
397 	    (as = auth_open()) == NULL) {
398 		login_close(lc);
399 		return (NULL);
400 	}
401 	if (auth_setitem(as, AUTHV_STYLE, style) < 0 ||
402 	    auth_setitem(as, AUTHV_NAME, name) < 0 ||
403 	    auth_setitem(as, AUTHV_CLASS, lc->lc_class) < 0) {
404 		auth_close(as);
405 		login_close(lc);
406 		return (NULL);
407 	}
408 	login_close(lc);
409 	*challengep = auth_challenge(as);
410 	return (as);
411 }
412 
413 int
414 auth_userresponse(auth_session_t *as, char *response, int more)
415 {
416 	char path[MAXPATHLEN];
417 	char *style, *name, *challenge, *class;
418 
419 	if (as == NULL)
420 		return (0);
421 
422 	auth_setstate(as, 0);
423 
424 	if ((style = auth_getitem(as, AUTHV_STYLE)) == NULL ||
425 	    (name = auth_getitem(as, AUTHV_NAME)) == NULL) {
426 		if (more == 0)
427 			return (auth_close(as));
428 		return(0);
429 	}
430 	challenge = auth_getitem(as, AUTHV_CHALLENGE);
431 	class = auth_getitem(as, AUTHV_CLASS);
432 
433 	if (challenge)
434 		auth_setdata(as, challenge, strlen(challenge) + 1);
435 	else
436 		auth_setdata(as, "", 1);
437 	if (response)
438 		auth_setdata(as, response, strlen(response) + 1);
439 	else
440 		auth_setdata(as, "", 1);
441 
442 	snprintf(path, sizeof(path), _PATH_AUTHPROG "%s", style);
443 
444 	auth_call(as, path, style, "-s", "response", name, class, (char *)NULL);
445 
446 	/*
447 	 * If they authenticated then make sure they did not expire
448 	 */
449 	if (auth_getstate(as) & AUTH_ALLOW)
450 		auth_check_expire(as);
451 	if (more == 0)
452 		return (auth_close(as));
453 	return (auth_getstate(as) & AUTH_ALLOW);
454 }
455 
456 /*
457  * Authenticate name with the specified style.
458  * If ``as'' is NULL a new session is formed with the default service.
459  * Returns NULL only if ``as'' is NULL and we were unable to allocate
460  * a new session.
461  *
462  * Use auth_close() or auth_getstate() to determine if the authentication
463  * worked.
464  */
465 auth_session_t *
466 auth_verify(auth_session_t *as, char *style, char *name, ...)
467 {
468 	va_list ap;
469 	char path[MAXPATHLEN];
470 
471 	if ((name == NULL || style == NULL) && as == NULL)
472 		return (as);
473 
474 	if (as == NULL && (as = auth_open()) == NULL)
475 		return (NULL);
476 	auth_setstate(as, 0);
477 
478 	if (style != NULL && auth_setitem(as, AUTHV_STYLE, style) < 0)
479 		return (as);
480 
481 	if (name != NULL && auth_setitem(as, AUTHV_NAME, name) < 0)
482 		return (as);
483 
484 	style = auth_getitem(as, AUTHV_STYLE);
485 	name = auth_getitem(as, AUTHV_NAME);
486 
487 	snprintf(path, sizeof(path), _PATH_AUTHPROG "%s", style);
488 	va_start(ap, name);
489 	auth_set_va_list(as, ap);
490 	auth_call(as, path, auth_getitem(as, AUTHV_STYLE), "-s",
491 	    auth_getitem(as, AUTHV_SERVICE), name, (char *)NULL);
492 	va_end(ap);
493 	return (as);
494 }
495