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.28 2001/01/18 17:00:00 markus Exp $");
33 #include <openssl/dsa.h>
34 #include <openssl/rsa.h>
35 #include <openssl/evp.h>
56 #include "auth-options.h"
59 extern ServerOptions options;
60 extern u_char *session_id2;
61 extern int session_id2_len;
63 #ifdef WITH_AIXAUTHENTICATE
64 extern char *aixloginmsg;
67 extern int saved_argc;
68 extern char **saved_argv;
71 static Authctxt *x_authctxt = NULL;
74 typedef struct Authmethod Authmethod;
77 int (*userauth)(Authctxt *authctxt);
83 void input_service_request(int type, int plen, void *ctxt);
84 void input_userauth_request(int type, int plen, void *ctxt);
85 void protocol_error(int type, int plen, void *ctxt);
88 Authmethod *authmethod_lookup(const char *name);
89 struct passwd *pwcopy(struct passwd *pw);
90 int user_key_allowed(struct passwd *pw, Key *key);
91 char *authmethods_get(void);
94 void userauth_banner(void);
95 int userauth_none(Authctxt *authctxt);
96 int userauth_passwd(Authctxt *authctxt);
97 int userauth_pubkey(Authctxt *authctxt);
98 int userauth_kbdint(Authctxt *authctxt);
100 Authmethod authmethods[] = {
106 &options.pubkey_authentication},
107 {"keyboard-interactive",
109 &options.kbd_interactive_authentication},
112 &options.password_authentication},
117 * loop until authctxt->success == TRUE
123 Authctxt *authctxt = authctxt_new();
125 x_authctxt = authctxt; /*XXX*/
128 /* If machine has AFS, set process authentication group. */
134 dispatch_init(&protocol_error);
135 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
136 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
137 do_authenticated2(authctxt);
141 protocol_error(int type, int plen, void *ctxt)
143 log("auth: protocol error: type %d plen %d", type, plen);
144 packet_start(SSH2_MSG_UNIMPLEMENTED);
151 input_service_request(int type, int plen, void *ctxt)
153 Authctxt *authctxt = ctxt;
156 char *service = packet_get_string(&len);
159 if (authctxt == NULL)
160 fatal("input_service_request: no authctxt");
162 if (strcmp(service, "ssh-userauth") == 0) {
163 if (!authctxt->success) {
165 /* now we can handle user-auth requests */
166 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
169 /* XXX all other service requests are denied */
172 packet_start(SSH2_MSG_SERVICE_ACCEPT);
173 packet_put_cstring(service);
177 debug("bad service request %s", service);
178 packet_disconnect("bad service request %s", service);
184 input_userauth_request(int type, int plen, void *ctxt)
186 Authctxt *authctxt = ctxt;
187 Authmethod *m = NULL;
188 char *user, *service, *method, *style = NULL;
189 int authenticated = 0;
191 if (authctxt == NULL)
192 fatal("input_userauth_request: no authctxt");
194 user = packet_get_string(NULL);
195 service = packet_get_string(NULL);
196 method = packet_get_string(NULL);
197 debug("userauth-request for user %s service %s method %s", user, service, method);
198 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
200 if ((style = strchr(user, ':')) != NULL)
203 if (authctxt->attempt++ == 0) {
204 /* setup auth context */
205 struct passwd *pw = NULL;
206 setproctitle("%s", user);
208 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
209 authctxt->pw = pwcopy(pw);
211 debug2("input_userauth_request: setting up authctxt for %s", user);
216 log("input_userauth_request: illegal user %s", user);
218 authctxt->user = xstrdup(user);
219 authctxt->service = xstrdup(service);
220 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
221 } else if (authctxt->valid) {
222 if (strcmp(user, authctxt->user) != 0 ||
223 strcmp(service, authctxt->service) != 0) {
224 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
225 user, service, authctxt->user, authctxt->service);
230 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
231 authctxt->postponed = 0;
233 /* try to authenticate user */
234 m = authmethod_lookup(method);
236 debug2("input_userauth_request: try method %s", method);
237 authenticated = m->userauth(authctxt);
239 if (!authctxt->valid && authenticated)
240 fatal("INTERNAL ERROR: authenticated invalid user %s",
243 /* Special handling for root */
244 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
248 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
252 /* Log before sending the reply */
253 auth_log(authctxt, authenticated, method, " ssh2");
255 if (!authctxt->postponed)
256 userauth_reply(authctxt, authenticated);
264 userauth_banner(void)
271 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
273 if ((fd = open(options.banner, O_RDONLY)) < 0) {
274 error("userauth_banner: open %s failed: %s",
275 options.banner, strerror(errno));
278 if (fstat(fd, &st) < 0)
281 banner = xmalloc(len + 1);
282 if ((n = read(fd, banner, len)) < 0)
285 packet_start(SSH2_MSG_USERAUTH_BANNER);
286 packet_put_cstring(banner);
287 packet_put_cstring(""); /* language, unused */
289 debug("userauth_banner: sent");
298 userauth_reply(Authctxt *authctxt, int authenticated)
302 /* XXX todo: check if multiple auth methods are needed */
304 #ifdef WITH_AIXAUTHENTICATE
305 /* We don't have a pty yet, so just label the line as "ssh" */
306 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
307 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
309 #endif /* WITH_AIXAUTHENTICATE */
310 /* turn off userauth */
311 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
312 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
315 /* now we can break out */
316 authctxt->success = 1;
318 if (authctxt->failures++ > AUTH_FAIL_MAX)
319 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
320 methods = authmethods_get();
321 packet_start(SSH2_MSG_USERAUTH_FAILURE);
322 packet_put_cstring(methods);
323 packet_put_char(0); /* XXX partial success, unused */
331 userauth_none(Authctxt *authctxt)
333 /* disable method "none", only allowed one time */
334 Authmethod *m = authmethod_lookup("none");
340 if (authctxt->valid == 0)
344 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
348 return auth_pam_password(authctxt->pw, "");
349 #elif defined(HAVE_OSF_SIA)
350 return (sia_validate_user(NULL, saved_argc, saved_argv,
351 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
352 NULL, 0, NULL, "") == SIASUCCESS);
353 #else /* !HAVE_OSF_SIA && !USE_PAM */
354 return auth_password(authctxt->pw, "");
359 userauth_passwd(Authctxt *authctxt)
362 int authenticated = 0;
365 change = packet_get_char();
367 log("password change not supported");
368 password = packet_get_string(&len);
370 if (authctxt->valid &&
372 check_nt_auth(1, authctxt->pw->pw_uid) &&
375 auth_pam_password(authctxt->pw, password) == 1)
376 #elif defined(HAVE_OSF_SIA)
377 sia_validate_user(NULL, saved_argc, saved_argv,
378 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
379 NULL, 0, NULL, password) == SIASUCCESS)
380 #else /* !USE_PAM && !HAVE_OSF_SIA */
381 auth_password(authctxt->pw, password) == 1)
384 memset(password, 0, len);
386 return authenticated;
390 userauth_kbdint(Authctxt *authctxt)
392 int authenticated = 0;
396 lang = packet_get_string(NULL);
397 devs = packet_get_string(NULL);
400 debug("keyboard-interactive language %s devs %s", lang, devs);
402 authenticated = auth2_challenge(authctxt, devs);
405 if (authenticated == 0)
406 authenticated = auth2_pam(authctxt);
411 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
414 return authenticated;
418 userauth_pubkey(Authctxt *authctxt)
422 char *pkalg, *pkblob, *sig;
423 u_int alen, blen, slen;
424 int have_sig, pktype;
425 int authenticated = 0;
427 if (!authctxt->valid) {
428 debug2("userauth_pubkey: disabled because of invalid user");
431 have_sig = packet_get_char();
432 if (datafellows & SSH_BUG_PKAUTH) {
433 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
434 /* no explicit pkalg given */
435 pkblob = packet_get_string(&blen);
437 buffer_append(&b, pkblob, blen);
438 /* so we have to extract the pkalg from the pkblob */
439 pkalg = buffer_get_string(&b, &alen);
442 pkalg = packet_get_string(&alen);
443 pkblob = packet_get_string(&blen);
445 pktype = key_type_from_name(pkalg);
446 if (pktype == KEY_UNSPEC) {
447 /* this is perfectly legal */
448 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
453 key = key_from_blob(pkblob, blen);
456 sig = packet_get_string(&slen);
459 if (datafellows & SSH_OLD_SESSIONID) {
460 buffer_append(&b, session_id2, session_id2_len);
462 buffer_put_string(&b, session_id2, session_id2_len);
464 /* reconstruct packet */
465 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
466 buffer_put_cstring(&b, authctxt->user);
467 buffer_put_cstring(&b,
468 datafellows & SSH_BUG_PKSERVICE ?
471 if (datafellows & SSH_BUG_PKAUTH) {
472 buffer_put_char(&b, have_sig);
474 buffer_put_cstring(&b, "publickey");
475 buffer_put_char(&b, have_sig);
476 buffer_put_cstring(&b, key_ssh_name(key));
478 buffer_put_string(&b, pkblob, blen);
482 /* test for correct signature */
483 if (user_key_allowed(authctxt->pw, key) &&
484 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
489 debug("test whether pkalg/pkblob are acceptable");
492 /* XXX fake reply and always send PK_OK ? */
494 * XXX this allows testing whether a user is allowed
495 * to login: if you happen to have a valid pubkey this
496 * message is sent. the message is NEVER sent at all
497 * if a user is not allowed to login. is this an
500 if (user_key_allowed(authctxt->pw, key)) {
501 packet_start(SSH2_MSG_USERAUTH_PK_OK);
502 packet_put_string(pkalg, alen);
503 packet_put_string(pkblob, blen);
509 if (authenticated != 1)
510 auth_clear_options();
513 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
517 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
520 return authenticated;
523 /* get current user */
528 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
534 authmethods_get(void)
536 Authmethod *method = NULL;
540 for (method = authmethods; method->name != NULL; method++) {
541 if (strcmp(method->name, "none") == 0)
543 if (method->enabled != NULL && *(method->enabled) != 0) {
545 size += strlen(DELIM);
546 size += strlen(method->name);
549 size++; /* trailing '\0' */
550 list = xmalloc(size);
553 for (method = authmethods; method->name != NULL; method++) {
554 if (strcmp(method->name, "none") == 0)
556 if (method->enabled != NULL && *(method->enabled) != 0) {
558 strlcat(list, DELIM, size);
559 strlcat(list, method->name, size);
566 authmethod_lookup(const char *name)
568 Authmethod *method = NULL;
570 for (method = authmethods; method->name != NULL; method++)
571 if (method->enabled != NULL &&
572 *(method->enabled) != 0 &&
573 strcmp(name, method->name) == 0)
575 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
579 /* return 1 if user allows given key */
581 user_key_allowed(struct passwd *pw, Key *key)
583 char line[8192], file[1024];
593 /* Temporarily use the user's uid. */
594 temporarily_use_uid(pw->pw_uid);
596 /* The authorized keys. */
597 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
598 SSH_USER_PERMITTED_KEYS2);
600 /* Fail quietly if file does not exist */
601 if (stat(file, &st) < 0) {
602 /* Restore the privileged uid. */
606 /* Open the file containing the authorized keys. */
607 f = fopen(file, "r");
609 /* Restore the privileged uid. */
613 if (options.strict_modes) {
616 /* Check open file in order to avoid open/stat races */
617 if (fstat(fileno(f), &st) < 0 ||
618 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
619 (st.st_mode & 022) != 0) {
620 snprintf(buf, sizeof buf,
621 "%s authentication refused for %.100s: "
622 "bad ownership or modes for '%s'.",
623 key_type(key), pw->pw_name, file);
626 /* Check path to SSH_USER_PERMITTED_KEYS */
628 static const char *check[] = {
629 "", SSH_USER_DIR, NULL
631 for (i = 0; check[i]; i++) {
632 snprintf(line, sizeof line, "%.500s/%.100s",
633 pw->pw_dir, check[i]);
634 if (stat(line, &st) < 0 ||
635 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
636 (st.st_mode & 022) != 0) {
637 snprintf(buf, sizeof buf,
638 "%s authentication refused for %.100s: "
639 "bad ownership or modes for '%s'.",
640 key_type(key), pw->pw_name, line);
654 found = key_new(key->type);
656 while (fgets(line, sizeof(line), f)) {
657 char *cp, *options = NULL;
659 /* Skip leading whitespace, empty and comment lines. */
660 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
662 if (!*cp || *cp == '\n' || *cp == '#')
665 if (key_read(found, &cp) == -1) {
666 /* no key? check if there are options for this key */
668 debug2("user_key_allowed: check options: '%s'", cp);
670 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
671 if (*cp == '\\' && cp[1] == '"')
672 cp++; /* Skip both */
676 /* Skip remaining whitespace. */
677 for (; *cp == ' ' || *cp == '\t'; cp++)
679 if (key_read(found, &cp) == -1) {
680 debug2("user_key_allowed: advance: '%s'", cp);
681 /* still no key? advance to next line*/
685 if (key_equal(found, key) &&
686 auth_parse_options(pw, options, linenum) == 1) {
688 debug("matching key found: file %s, line %ld",