2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2.c,v 1.92 2002/05/25 18:51:07 markus Exp $");
36 #include "pathnames.h"
37 #include "monitor_wrap.h"
40 extern ServerOptions options;
41 extern u_char *session_id2;
42 extern int session_id2_len;
44 Authctxt *x_authctxt = NULL;
47 typedef struct Authmethod Authmethod;
50 int (*userauth)(Authctxt *authctxt);
56 static void input_service_request(int, u_int32_t, void *);
57 static void input_userauth_request(int, u_int32_t, void *);
60 static Authmethod *authmethod_lookup(const char *);
61 static char *authmethods_get(void);
62 int user_key_allowed(struct passwd *, Key *);
63 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
67 Authmethod authmethods[] = {
73 &options.pubkey_authentication},
76 &options.password_authentication},
77 {"keyboard-interactive",
79 &options.kbd_interactive_authentication},
82 &options.hostbased_authentication},
87 * loop until authctxt->success == TRUE
91 do_authentication2(void)
93 Authctxt *authctxt = authctxt_new();
95 x_authctxt = authctxt; /*XXX*/
97 /* challenge-response is implemented via keyboard interactive */
98 if (options.challenge_response_authentication)
99 options.kbd_interactive_authentication = 1;
100 if (options.pam_authentication_via_kbd_int)
101 options.kbd_interactive_authentication = 1;
103 options.pam_authentication_via_kbd_int = 0;
105 dispatch_init(&dispatch_protocol_error);
106 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
107 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
113 input_service_request(int type, u_int32_t seq, void *ctxt)
115 Authctxt *authctxt = ctxt;
118 char *service = packet_get_string(&len);
121 if (authctxt == NULL)
122 fatal("input_service_request: no authctxt");
124 if (strcmp(service, "ssh-userauth") == 0) {
125 if (!authctxt->success) {
127 /* now we can handle user-auth requests */
128 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
131 /* XXX all other service requests are denied */
134 packet_start(SSH2_MSG_SERVICE_ACCEPT);
135 packet_put_cstring(service);
139 debug("bad service request %s", service);
140 packet_disconnect("bad service request %s", service);
146 input_userauth_request(int type, u_int32_t seq, void *ctxt)
148 Authctxt *authctxt = ctxt;
149 Authmethod *m = NULL;
150 char *user, *service, *method, *style = NULL;
151 int authenticated = 0;
153 if (authctxt == NULL)
154 fatal("input_userauth_request: no authctxt");
156 user = packet_get_string(NULL);
157 service = packet_get_string(NULL);
158 method = packet_get_string(NULL);
159 debug("userauth-request for user %s service %s method %s", user, service, method);
160 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
162 if ((style = strchr(user, ':')) != NULL)
165 if (authctxt->attempt++ == 0) {
166 /* setup auth context */
167 authctxt->pw = PRIVSEP(getpwnamallow(user));
168 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
170 debug2("input_userauth_request: setting up authctxt for %s", user);
172 PRIVSEP(start_pam(authctxt->pw->pw_name));
175 log("input_userauth_request: illegal user %s", user);
177 PRIVSEP(start_pam("NOUSER"));
180 setproctitle("%s%s", authctxt->pw ? user : "unknown",
181 use_privsep ? " [net]" : "");
182 authctxt->user = xstrdup(user);
183 authctxt->service = xstrdup(service);
184 authctxt->style = style ? xstrdup(style) : NULL;
186 mm_inform_authserv(service, style);
187 } else if (strcmp(user, authctxt->user) != 0 ||
188 strcmp(service, authctxt->service) != 0) {
189 packet_disconnect("Change of username or service not allowed: "
190 "(%s,%s) -> (%s,%s)",
191 authctxt->user, authctxt->service, user, service);
194 auth2_challenge_stop(authctxt);
195 authctxt->postponed = 0;
197 /* try to authenticate user */
198 m = authmethod_lookup(method);
200 debug2("input_userauth_request: try method %s", method);
201 authenticated = m->userauth(authctxt);
203 userauth_finish(authctxt, authenticated, method);
211 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
215 if (!authctxt->valid && authenticated)
216 fatal("INTERNAL ERROR: authenticated invalid user %s",
219 /* Special handling for root */
220 if (authenticated && authctxt->pw->pw_uid == 0 &&
221 !auth_root_allowed(method))
225 if (!use_privsep && authenticated && authctxt->user &&
226 !do_pam_account(authctxt->user, NULL))
230 /* Log before sending the reply */
231 auth_log(authctxt, authenticated, method, " ssh2");
233 if (authctxt->postponed)
236 /* XXX todo: check if multiple auth methods are needed */
237 if (authenticated == 1) {
238 /* turn off userauth */
239 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
240 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
243 /* now we can break out */
244 authctxt->success = 1;
246 if (authctxt->failures++ > AUTH_FAIL_MAX) {
247 #ifdef WITH_AIXAUTHENTICATE
248 loginfailed(authctxt->user,
249 get_canonical_hostname(options.verify_reverse_mapping),
251 #endif /* WITH_AIXAUTHENTICATE */
252 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
254 methods = authmethods_get();
255 packet_start(SSH2_MSG_USERAUTH_FAILURE);
256 packet_put_cstring(methods);
257 packet_put_char(0); /* XXX partial success, unused */
265 auth2_read_banner(void)
272 if ((fd = open(options.banner, O_RDONLY)) == -1)
274 if (fstat(fd, &st) == -1) {
279 banner = xmalloc(len + 1);
280 n = atomicio(read, fd, banner, len);
293 userauth_banner(void)
297 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
300 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
303 packet_start(SSH2_MSG_USERAUTH_BANNER);
304 packet_put_cstring(banner);
305 packet_put_cstring(""); /* language, unused */
307 debug("userauth_banner: sent");
315 userauth_none(Authctxt *authctxt)
317 /* disable method "none", only allowed one time */
318 Authmethod *m = authmethod_lookup("none");
324 if (authctxt->valid == 0)
328 if (check_nt_auth(1, authctxt->pw) == 0)
331 return PRIVSEP(auth_password(authctxt, ""));
335 userauth_passwd(Authctxt *authctxt)
338 int authenticated = 0;
341 change = packet_get_char();
343 log("password change not supported");
344 password = packet_get_string(&len);
346 if (authctxt->valid &&
348 check_nt_auth(1, authctxt->pw) &&
350 PRIVSEP(auth_password(authctxt, password)) == 1)
352 memset(password, 0, len);
354 return authenticated;
358 userauth_kbdint(Authctxt *authctxt)
360 int authenticated = 0;
363 lang = packet_get_string(NULL);
364 devs = packet_get_string(NULL);
367 debug("keyboard-interactive devs %s", devs);
369 if (options.challenge_response_authentication)
370 authenticated = auth2_challenge(authctxt, devs);
373 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
374 authenticated = auth2_pam(authctxt);
379 if (check_nt_auth(0, authctxt->pw) == 0)
382 return authenticated;
386 userauth_pubkey(Authctxt *authctxt)
391 u_char *pkblob, *sig;
392 u_int alen, blen, slen;
393 int have_sig, pktype;
394 int authenticated = 0;
396 if (!authctxt->valid) {
397 debug2("userauth_pubkey: disabled because of invalid user");
400 have_sig = packet_get_char();
401 if (datafellows & SSH_BUG_PKAUTH) {
402 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
403 /* no explicit pkalg given */
404 pkblob = packet_get_string(&blen);
406 buffer_append(&b, pkblob, blen);
407 /* so we have to extract the pkalg from the pkblob */
408 pkalg = buffer_get_string(&b, &alen);
411 pkalg = packet_get_string(&alen);
412 pkblob = packet_get_string(&blen);
414 pktype = key_type_from_name(pkalg);
415 if (pktype == KEY_UNSPEC) {
416 /* this is perfectly legal */
417 log("userauth_pubkey: unsupported public key algorithm: %s",
421 key = key_from_blob(pkblob, blen);
423 error("userauth_pubkey: cannot decode key: %s", pkalg);
426 if (key->type != pktype) {
427 error("userauth_pubkey: type mismatch for decoded key "
428 "(received %d, expected %d)", key->type, pktype);
432 sig = packet_get_string(&slen);
435 if (datafellows & SSH_OLD_SESSIONID) {
436 buffer_append(&b, session_id2, session_id2_len);
438 buffer_put_string(&b, session_id2, session_id2_len);
440 /* reconstruct packet */
441 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
442 buffer_put_cstring(&b, authctxt->user);
443 buffer_put_cstring(&b,
444 datafellows & SSH_BUG_PKSERVICE ?
447 if (datafellows & SSH_BUG_PKAUTH) {
448 buffer_put_char(&b, have_sig);
450 buffer_put_cstring(&b, "publickey");
451 buffer_put_char(&b, have_sig);
452 buffer_put_cstring(&b, pkalg);
454 buffer_put_string(&b, pkblob, blen);
458 /* test for correct signature */
460 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
461 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
462 buffer_len(&b))) == 1)
467 debug("test whether pkalg/pkblob are acceptable");
470 /* XXX fake reply and always send PK_OK ? */
472 * XXX this allows testing whether a user is allowed
473 * to login: if you happen to have a valid pubkey this
474 * message is sent. the message is NEVER sent at all
475 * if a user is not allowed to login. is this an
478 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
479 packet_start(SSH2_MSG_USERAUTH_PK_OK);
480 packet_put_string(pkalg, alen);
481 packet_put_string(pkblob, blen);
484 authctxt->postponed = 1;
487 if (authenticated != 1)
488 auth_clear_options();
490 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
496 if (check_nt_auth(0, authctxt->pw) == 0)
499 return authenticated;
503 userauth_hostbased(Authctxt *authctxt)
507 char *pkalg, *cuser, *chost, *service;
508 u_char *pkblob, *sig;
509 u_int alen, blen, slen;
511 int authenticated = 0;
513 if (!authctxt->valid) {
514 debug2("userauth_hostbased: disabled because of invalid user");
517 pkalg = packet_get_string(&alen);
518 pkblob = packet_get_string(&blen);
519 chost = packet_get_string(NULL);
520 cuser = packet_get_string(NULL);
521 sig = packet_get_string(&slen);
523 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
524 cuser, chost, pkalg, slen);
528 buffer_append(&b, sig, slen);
532 pktype = key_type_from_name(pkalg);
533 if (pktype == KEY_UNSPEC) {
534 /* this is perfectly legal */
535 log("userauth_hostbased: unsupported "
536 "public key algorithm: %s", pkalg);
539 key = key_from_blob(pkblob, blen);
541 error("userauth_hostbased: cannot decode key: %s", pkalg);
544 if (key->type != pktype) {
545 error("userauth_hostbased: type mismatch for decoded key "
546 "(received %d, expected %d)", key->type, pktype);
549 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
552 buffer_put_string(&b, session_id2, session_id2_len);
553 /* reconstruct packet */
554 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
555 buffer_put_cstring(&b, authctxt->user);
556 buffer_put_cstring(&b, service);
557 buffer_put_cstring(&b, "hostbased");
558 buffer_put_string(&b, pkalg, alen);
559 buffer_put_string(&b, pkblob, blen);
560 buffer_put_cstring(&b, chost);
561 buffer_put_cstring(&b, cuser);
565 /* test for allowed key and correct signature */
567 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
568 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
569 buffer_len(&b))) == 1)
574 debug2("userauth_hostbased: authenticated %d", authenticated);
582 return authenticated;
585 /* get current user */
590 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
596 authmethods_get(void)
598 Authmethod *method = NULL;
603 for (method = authmethods; method->name != NULL; method++) {
604 if (strcmp(method->name, "none") == 0)
606 if (method->enabled != NULL && *(method->enabled) != 0) {
607 if (buffer_len(&b) > 0)
608 buffer_append(&b, ",", 1);
609 buffer_append(&b, method->name, strlen(method->name));
612 buffer_append(&b, "\0", 1);
613 list = xstrdup(buffer_ptr(&b));
619 authmethod_lookup(const char *name)
621 Authmethod *method = NULL;
623 for (method = authmethods; method->name != NULL; method++)
624 if (method->enabled != NULL &&
625 *(method->enabled) != 0 &&
626 strcmp(name, method->name) == 0)
628 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");