xref: /openbsd-src/usr.bin/ssh/auth2.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2.c,v 1.69 2001/07/23 18:14:58 stevesk Exp $");
27 
28 #include <openssl/evp.h>
29 
30 #include "ssh2.h"
31 #include "xmalloc.h"
32 #include "rsa.h"
33 #include "sshpty.h"
34 #include "packet.h"
35 #include "buffer.h"
36 #include "log.h"
37 #include "servconf.h"
38 #include "compat.h"
39 #include "channels.h"
40 #include "bufaux.h"
41 #include "auth.h"
42 #include "session.h"
43 #include "dispatch.h"
44 #include "key.h"
45 #include "cipher.h"
46 #include "kex.h"
47 #include "pathnames.h"
48 #include "uidswap.h"
49 #include "auth-options.h"
50 #include "misc.h"
51 #include "hostfile.h"
52 #include "canohost.h"
53 #include "tildexpand.h"
54 #include "match.h"
55 
56 /* import */
57 extern ServerOptions options;
58 extern u_char *session_id2;
59 extern int session_id2_len;
60 
61 static Authctxt	*x_authctxt = NULL;
62 static int one = 1;
63 
64 typedef struct Authmethod Authmethod;
65 struct Authmethod {
66 	char	*name;
67 	int	(*userauth)(Authctxt *authctxt);
68 	int	*enabled;
69 };
70 
71 /* protocol */
72 
73 static void input_service_request(int, int, void *);
74 static void input_userauth_request(int, int, void *);
75 static void protocol_error(int, int, void *);
76 
77 /* helper */
78 static Authmethod *authmethod_lookup(const char *);
79 static char *authmethods_get(void);
80 static int user_key_allowed(struct passwd *, Key *);
81 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
82 
83 /* auth */
84 static void userauth_banner(void);
85 static int userauth_none(Authctxt *);
86 static int userauth_passwd(Authctxt *);
87 static int userauth_pubkey(Authctxt *);
88 static int userauth_hostbased(Authctxt *);
89 static int userauth_kbdint(Authctxt *);
90 
91 Authmethod authmethods[] = {
92 	{"none",
93 		userauth_none,
94 		&one},
95 	{"publickey",
96 		userauth_pubkey,
97 		&options.pubkey_authentication},
98 	{"password",
99 		userauth_passwd,
100 		&options.password_authentication},
101 	{"keyboard-interactive",
102 		userauth_kbdint,
103 		&options.kbd_interactive_authentication},
104 	{"hostbased",
105 		userauth_hostbased,
106 		&options.hostbased_authentication},
107 	{NULL, NULL, NULL}
108 };
109 
110 /*
111  * loop until authctxt->success == TRUE
112  */
113 
114 void
115 do_authentication2()
116 {
117 	Authctxt *authctxt = authctxt_new();
118 
119 	x_authctxt = authctxt;		/*XXX*/
120 
121 	/* challenge-reponse is implemented via keyboard interactive */
122 	if (options.challenge_response_authentication)
123 		options.kbd_interactive_authentication = 1;
124 
125 	dispatch_init(&protocol_error);
126 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
127 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
128 	do_authenticated(authctxt);
129 }
130 
131 static void
132 protocol_error(int type, int plen, void *ctxt)
133 {
134 	log("auth: protocol error: type %d plen %d", type, plen);
135 	packet_start(SSH2_MSG_UNIMPLEMENTED);
136 	packet_put_int(0);
137 	packet_send();
138 	packet_write_wait();
139 }
140 
141 static void
142 input_service_request(int type, int plen, void *ctxt)
143 {
144 	Authctxt *authctxt = ctxt;
145 	u_int len;
146 	int accept = 0;
147 	char *service = packet_get_string(&len);
148 	packet_done();
149 
150 	if (authctxt == NULL)
151 		fatal("input_service_request: no authctxt");
152 
153 	if (strcmp(service, "ssh-userauth") == 0) {
154 		if (!authctxt->success) {
155 			accept = 1;
156 			/* now we can handle user-auth requests */
157 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
158 		}
159 	}
160 	/* XXX all other service requests are denied */
161 
162 	if (accept) {
163 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
164 		packet_put_cstring(service);
165 		packet_send();
166 		packet_write_wait();
167 	} else {
168 		debug("bad service request %s", service);
169 		packet_disconnect("bad service request %s", service);
170 	}
171 	xfree(service);
172 }
173 
174 static void
175 input_userauth_request(int type, int plen, void *ctxt)
176 {
177 	Authctxt *authctxt = ctxt;
178 	Authmethod *m = NULL;
179 	char *user, *service, *method, *style = NULL;
180 	int authenticated = 0;
181 
182 	if (authctxt == NULL)
183 		fatal("input_userauth_request: no authctxt");
184 
185 	user = packet_get_string(NULL);
186 	service = packet_get_string(NULL);
187 	method = packet_get_string(NULL);
188 	debug("userauth-request for user %s service %s method %s", user, service, method);
189 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
190 
191 	if ((style = strchr(user, ':')) != NULL)
192 		*style++ = 0;
193 
194 	if (authctxt->attempt++ == 0) {
195 		/* setup auth context */
196 		struct passwd *pw = NULL;
197 		pw = getpwnam(user);
198 		if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
199 			authctxt->pw = pwcopy(pw);
200 			authctxt->valid = 1;
201 			debug2("input_userauth_request: setting up authctxt for %s", user);
202 		} else {
203 			log("input_userauth_request: illegal user %s", user);
204 		}
205 		setproctitle("%s", pw ? user : "unknown");
206 		authctxt->user = xstrdup(user);
207 		authctxt->service = xstrdup(service);
208 		authctxt->style = style ? xstrdup(style) : NULL;
209 	} else if (strcmp(user, authctxt->user) != 0 ||
210 	    strcmp(service, authctxt->service) != 0) {
211 		packet_disconnect("Change of username or service not allowed: "
212 		    "(%s,%s) -> (%s,%s)",
213 		    authctxt->user, authctxt->service, user, service);
214 	}
215 	/* reset state */
216 	dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
217 	authctxt->postponed = 0;
218 #ifdef BSD_AUTH
219 	if (authctxt->as) {
220 		auth_close(authctxt->as);
221 		authctxt->as = NULL;
222 	}
223 #endif
224 
225 	/* try to authenticate user */
226 	m = authmethod_lookup(method);
227 	if (m != NULL) {
228 		debug2("input_userauth_request: try method %s", method);
229 		authenticated =	m->userauth(authctxt);
230 	}
231 	userauth_finish(authctxt, authenticated, method);
232 
233 	xfree(service);
234 	xfree(user);
235 	xfree(method);
236 }
237 
238 void
239 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
240 {
241 	char *methods;
242 
243 	if (!authctxt->valid && authenticated)
244 		fatal("INTERNAL ERROR: authenticated invalid user %s",
245 		    authctxt->user);
246 
247 	/* Special handling for root */
248 	if (authenticated && authctxt->pw->pw_uid == 0 &&
249 	    !auth_root_allowed(method))
250 		authenticated = 0;
251 
252 	/* Log before sending the reply */
253 	auth_log(authctxt, authenticated, method, " ssh2");
254 
255 	if (authctxt->postponed)
256 		return;
257 
258 	/* XXX todo: check if multiple auth methods are needed */
259 	if (authenticated == 1) {
260 		/* turn off userauth */
261 		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
262 		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
263 		packet_send();
264 		packet_write_wait();
265 		/* now we can break out */
266 		authctxt->success = 1;
267 	} else {
268 		if (authctxt->failures++ > AUTH_FAIL_MAX)
269 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
270 		methods = authmethods_get();
271 		packet_start(SSH2_MSG_USERAUTH_FAILURE);
272 		packet_put_cstring(methods);
273 		packet_put_char(0);	/* XXX partial success, unused */
274 		packet_send();
275 		packet_write_wait();
276 		xfree(methods);
277 	}
278 }
279 
280 static void
281 userauth_banner(void)
282 {
283 	struct stat st;
284 	char *banner = NULL;
285 	off_t len, n;
286 	int fd;
287 
288 	if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
289 		return;
290 	if ((fd = open(options.banner, O_RDONLY)) < 0)
291 		return;
292 	if (fstat(fd, &st) < 0)
293 		goto done;
294 	len = st.st_size;
295 	banner = xmalloc(len + 1);
296 	if ((n = read(fd, banner, len)) < 0)
297 		goto done;
298 	banner[n] = '\0';
299 	packet_start(SSH2_MSG_USERAUTH_BANNER);
300 	packet_put_cstring(banner);
301 	packet_put_cstring("");		/* language, unused */
302 	packet_send();
303 	debug("userauth_banner: sent");
304 done:
305 	if (banner)
306 		xfree(banner);
307 	close(fd);
308 	return;
309 }
310 
311 static int
312 userauth_none(Authctxt *authctxt)
313 {
314 	/* disable method "none", only allowed one time */
315 	Authmethod *m = authmethod_lookup("none");
316 	if (m != NULL)
317 		m->enabled = NULL;
318 	packet_done();
319 	userauth_banner();
320 	return authctxt->valid ? auth_password(authctxt, "") : 0;
321 }
322 
323 static int
324 userauth_passwd(Authctxt *authctxt)
325 {
326 	char *password;
327 	int authenticated = 0;
328 	int change;
329 	u_int len;
330 	change = packet_get_char();
331 	if (change)
332 		log("password change not supported");
333 	password = packet_get_string(&len);
334 	packet_done();
335 	if (authctxt->valid &&
336 	    auth_password(authctxt, password) == 1)
337 		authenticated = 1;
338 	memset(password, 0, len);
339 	xfree(password);
340 	return authenticated;
341 }
342 
343 static int
344 userauth_kbdint(Authctxt *authctxt)
345 {
346 	int authenticated = 0;
347 	char *lang, *devs;
348 
349 	lang = packet_get_string(NULL);
350 	devs = packet_get_string(NULL);
351 	packet_done();
352 
353 	debug("keyboard-interactive devs %s", devs);
354 
355 	if (options.challenge_response_authentication)
356 		authenticated = auth2_challenge(authctxt, devs);
357 
358 	xfree(devs);
359 	xfree(lang);
360 	return authenticated;
361 }
362 
363 static int
364 userauth_pubkey(Authctxt *authctxt)
365 {
366 	Buffer b;
367 	Key *key;
368 	char *pkalg, *pkblob, *sig;
369 	u_int alen, blen, slen;
370 	int have_sig, pktype;
371 	int authenticated = 0;
372 
373 	if (!authctxt->valid) {
374 		debug2("userauth_pubkey: disabled because of invalid user");
375 		return 0;
376 	}
377 	have_sig = packet_get_char();
378 	if (datafellows & SSH_BUG_PKAUTH) {
379 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
380 		/* no explicit pkalg given */
381 		pkblob = packet_get_string(&blen);
382 		buffer_init(&b);
383 		buffer_append(&b, pkblob, blen);
384 		/* so we have to extract the pkalg from the pkblob */
385 		pkalg = buffer_get_string(&b, &alen);
386 		buffer_free(&b);
387 	} else {
388 		pkalg = packet_get_string(&alen);
389 		pkblob = packet_get_string(&blen);
390 	}
391 	pktype = key_type_from_name(pkalg);
392 	if (pktype == KEY_UNSPEC) {
393 		/* this is perfectly legal */
394 		log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
395 		xfree(pkalg);
396 		xfree(pkblob);
397 		return 0;
398 	}
399 	key = key_from_blob(pkblob, blen);
400 	if (key != NULL) {
401 		if (have_sig) {
402 			sig = packet_get_string(&slen);
403 			packet_done();
404 			buffer_init(&b);
405 			if (datafellows & SSH_OLD_SESSIONID) {
406 				buffer_append(&b, session_id2, session_id2_len);
407 			} else {
408 				buffer_put_string(&b, session_id2, session_id2_len);
409 			}
410 			/* reconstruct packet */
411 			buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
412 			buffer_put_cstring(&b, authctxt->user);
413 			buffer_put_cstring(&b,
414 			    datafellows & SSH_BUG_PKSERVICE ?
415 			    "ssh-userauth" :
416 			    authctxt->service);
417 			if (datafellows & SSH_BUG_PKAUTH) {
418 				buffer_put_char(&b, have_sig);
419 			} else {
420 				buffer_put_cstring(&b, "publickey");
421 				buffer_put_char(&b, have_sig);
422 				buffer_put_cstring(&b, pkalg);
423 			}
424 			buffer_put_string(&b, pkblob, blen);
425 #ifdef DEBUG_PK
426 			buffer_dump(&b);
427 #endif
428 			/* test for correct signature */
429 			if (user_key_allowed(authctxt->pw, key) &&
430 			    key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
431 				authenticated = 1;
432 			buffer_clear(&b);
433 			xfree(sig);
434 		} else {
435 			debug("test whether pkalg/pkblob are acceptable");
436 			packet_done();
437 
438 			/* XXX fake reply and always send PK_OK ? */
439 			/*
440 			 * XXX this allows testing whether a user is allowed
441 			 * to login: if you happen to have a valid pubkey this
442 			 * message is sent. the message is NEVER sent at all
443 			 * if a user is not allowed to login. is this an
444 			 * issue? -markus
445 			 */
446 			if (user_key_allowed(authctxt->pw, key)) {
447 				packet_start(SSH2_MSG_USERAUTH_PK_OK);
448 				packet_put_string(pkalg, alen);
449 				packet_put_string(pkblob, blen);
450 				packet_send();
451 				packet_write_wait();
452 				authctxt->postponed = 1;
453 			}
454 		}
455 		if (authenticated != 1)
456 			auth_clear_options();
457 		key_free(key);
458 	}
459 	debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
460 	xfree(pkalg);
461 	xfree(pkblob);
462 	return authenticated;
463 }
464 
465 static int
466 userauth_hostbased(Authctxt *authctxt)
467 {
468 	Buffer b;
469 	Key *key;
470 	char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
471 	u_int alen, blen, slen;
472 	int pktype;
473 	int authenticated = 0;
474 
475 	if (!authctxt->valid) {
476 		debug2("userauth_hostbased: disabled because of invalid user");
477 		return 0;
478 	}
479 	pkalg = packet_get_string(&alen);
480 	pkblob = packet_get_string(&blen);
481 	chost = packet_get_string(NULL);
482 	cuser = packet_get_string(NULL);
483 	sig = packet_get_string(&slen);
484 
485 	debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
486 	    cuser, chost, pkalg, slen);
487 #ifdef DEBUG_PK
488 	debug("signature:");
489 	buffer_init(&b);
490 	buffer_append(&b, sig, slen);
491 	buffer_dump(&b);
492 	buffer_free(&b);
493 #endif
494 	pktype = key_type_from_name(pkalg);
495 	if (pktype == KEY_UNSPEC) {
496 		/* this is perfectly legal */
497 		log("userauth_hostbased: unsupported "
498 		    "public key algorithm: %s", pkalg);
499 		goto done;
500 	}
501 	key = key_from_blob(pkblob, blen);
502 	if (key == NULL) {
503 		debug("userauth_hostbased: cannot decode key: %s", pkalg);
504 		goto done;
505 	}
506 	service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
507 	    authctxt->service;
508 	buffer_init(&b);
509 	buffer_put_string(&b, session_id2, session_id2_len);
510 	/* reconstruct packet */
511 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
512 	buffer_put_cstring(&b, authctxt->user);
513 	buffer_put_cstring(&b, service);
514 	buffer_put_cstring(&b, "hostbased");
515 	buffer_put_string(&b, pkalg, alen);
516 	buffer_put_string(&b, pkblob, blen);
517 	buffer_put_cstring(&b, chost);
518 	buffer_put_cstring(&b, cuser);
519 #ifdef DEBUG_PK
520 	buffer_dump(&b);
521 #endif
522 	/* test for allowed key and correct signature */
523 	if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
524 	    key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
525 		authenticated = 1;
526 
527 	buffer_clear(&b);
528 	key_free(key);
529 
530 done:
531 	debug2("userauth_hostbased: authenticated %d", authenticated);
532 	xfree(pkalg);
533 	xfree(pkblob);
534 	xfree(cuser);
535 	xfree(chost);
536 	xfree(sig);
537 	return authenticated;
538 }
539 
540 /* get current user */
541 
542 struct passwd*
543 auth_get_user(void)
544 {
545 	return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
546 }
547 
548 #define	DELIM	","
549 
550 static char *
551 authmethods_get(void)
552 {
553 	Authmethod *method = NULL;
554 	u_int size = 0;
555 	char *list;
556 
557 	for (method = authmethods; method->name != NULL; method++) {
558 		if (strcmp(method->name, "none") == 0)
559 			continue;
560 		if (method->enabled != NULL && *(method->enabled) != 0) {
561 			if (size != 0)
562 				size += strlen(DELIM);
563 			size += strlen(method->name);
564 		}
565 	}
566 	size++;			/* trailing '\0' */
567 	list = xmalloc(size);
568 	list[0] = '\0';
569 
570 	for (method = authmethods; method->name != NULL; method++) {
571 		if (strcmp(method->name, "none") == 0)
572 			continue;
573 		if (method->enabled != NULL && *(method->enabled) != 0) {
574 			if (list[0] != '\0')
575 				strlcat(list, DELIM, size);
576 			strlcat(list, method->name, size);
577 		}
578 	}
579 	return list;
580 }
581 
582 static Authmethod *
583 authmethod_lookup(const char *name)
584 {
585 	Authmethod *method = NULL;
586 	if (name != NULL)
587 		for (method = authmethods; method->name != NULL; method++)
588 			if (method->enabled != NULL &&
589 			    *(method->enabled) != 0 &&
590 			    strcmp(name, method->name) == 0)
591 				return method;
592 	debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
593 	return NULL;
594 }
595 
596 /* return 1 if user allows given key */
597 static int
598 user_key_allowed2(struct passwd *pw, Key *key, char *file)
599 {
600 	char line[8192];
601 	int found_key = 0;
602 	FILE *f;
603 	u_long linenum = 0;
604 	struct stat st;
605 	Key *found;
606 
607 	if (pw == NULL)
608 		return 0;
609 
610 	/* Temporarily use the user's uid. */
611 	temporarily_use_uid(pw);
612 
613 	debug("trying public key file %s", file);
614 
615 	/* Fail quietly if file does not exist */
616 	if (stat(file, &st) < 0) {
617 		/* Restore the privileged uid. */
618 		restore_uid();
619 		return 0;
620 	}
621 	/* Open the file containing the authorized keys. */
622 	f = fopen(file, "r");
623 	if (!f) {
624 		/* Restore the privileged uid. */
625 		restore_uid();
626 		return 0;
627 	}
628 	if (options.strict_modes &&
629 	    secure_filename(f, file, pw, line, sizeof(line)) != 0) {
630 		fclose(f);
631 		log("Authentication refused: %s", line);
632 		restore_uid();
633 		return 0;
634 	}
635 
636 	found_key = 0;
637 	found = key_new(key->type);
638 
639 	while (fgets(line, sizeof(line), f)) {
640 		char *cp, *options = NULL;
641 		linenum++;
642 		/* Skip leading whitespace, empty and comment lines. */
643 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
644 			;
645 		if (!*cp || *cp == '\n' || *cp == '#')
646 			continue;
647 
648 		if (key_read(found, &cp) == -1) {
649 			/* no key?  check if there are options for this key */
650 			int quoted = 0;
651 			debug2("user_key_allowed: check options: '%s'", cp);
652 			options = cp;
653 			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
654 				if (*cp == '\\' && cp[1] == '"')
655 					cp++;	/* Skip both */
656 				else if (*cp == '"')
657 					quoted = !quoted;
658 			}
659 			/* Skip remaining whitespace. */
660 			for (; *cp == ' ' || *cp == '\t'; cp++)
661 				;
662 			if (key_read(found, &cp) == -1) {
663 				debug2("user_key_allowed: advance: '%s'", cp);
664 				/* still no key?  advance to next line*/
665 				continue;
666 			}
667 		}
668 		if (key_equal(found, key) &&
669 		    auth_parse_options(pw, options, file, linenum) == 1) {
670 			found_key = 1;
671 			debug("matching key found: file %s, line %lu",
672 			    file, linenum);
673 			break;
674 		}
675 	}
676 	restore_uid();
677 	fclose(f);
678 	key_free(found);
679 	if (!found_key)
680 		debug2("key not found");
681 	return found_key;
682 }
683 
684 /* check whether given key is in .ssh/authorized_keys* */
685 static int
686 user_key_allowed(struct passwd *pw, Key *key)
687 {
688 	int success;
689 	char *file;
690 
691 	file = authorized_keys_file(pw);
692 	success = user_key_allowed2(pw, key, file);
693 	xfree(file);
694 	if (success)
695 		return success;
696 
697 	/* try suffix "2" for backward compat, too */
698 	file = authorized_keys_file2(pw);
699 	success = user_key_allowed2(pw, key, file);
700 	xfree(file);
701 	return success;
702 }
703 
704 /* return 1 if given hostkey is allowed */
705 static int
706 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
707     Key *key)
708 {
709 	const char *resolvedname, *ipaddr, *lookup;
710 	int host_status, len;
711 
712 	resolvedname = get_canonical_hostname(options.reverse_mapping_check);
713 	ipaddr = get_remote_ipaddr();
714 
715 	debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
716 	    chost, resolvedname, ipaddr);
717 
718 	if (options.hostbased_uses_name_from_packet_only) {
719 		if (auth_rhosts2(pw, cuser, chost, chost) == 0)
720 			return 0;
721 		lookup = chost;
722 	} else {
723 		if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
724 			debug2("stripping trailing dot from chost %s", chost);
725 			chost[len - 1] = '\0';
726 		}
727 		if (strcasecmp(resolvedname, chost) != 0)
728 			log("userauth_hostbased mismatch: "
729 			    "client sends %s, but we resolve %s to %s",
730 			    chost, ipaddr, resolvedname);
731 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
732 			return 0;
733 		lookup = resolvedname;
734 	}
735 	debug2("userauth_hostbased: access allowed by auth_rhosts2");
736 
737 	host_status = check_key_in_hostfiles(pw, key, lookup,
738 	    _PATH_SSH_SYSTEM_HOSTFILE,
739 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
740 
741 	/* backward compat if no key has been found. */
742 	if (host_status == HOST_NEW)
743 		host_status = check_key_in_hostfiles(pw, key, lookup,
744 		    _PATH_SSH_SYSTEM_HOSTFILE2,
745 		    options.ignore_user_known_hosts ? NULL :
746 		    _PATH_SSH_USER_HOSTFILE2);
747 
748 	return (host_status == HOST_OK);
749 }
750 
751