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.50 2001/04/04 20:32:56 markus Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 extern ServerOptions options;
54 extern u_char *session_id2;
55 extern int session_id2_len;
57 #ifdef WITH_AIXAUTHENTICATE
58 extern char *aixloginmsg;
61 static Authctxt *x_authctxt = NULL;
64 typedef struct Authmethod Authmethod;
67 int (*userauth)(Authctxt *authctxt);
73 void input_service_request(int type, int plen, void *ctxt);
74 void input_userauth_request(int type, int plen, void *ctxt);
75 void protocol_error(int type, int plen, void *ctxt);
78 Authmethod *authmethod_lookup(const char *name);
79 int user_key_allowed(struct passwd *pw, Key *key);
80 char *authmethods_get(void);
83 void userauth_banner(void);
84 void userauth_reply(Authctxt *authctxt, int authenticated);
85 int userauth_none(Authctxt *authctxt);
86 int userauth_passwd(Authctxt *authctxt);
87 int userauth_pubkey(Authctxt *authctxt);
88 int userauth_kbdint(Authctxt *authctxt);
90 Authmethod authmethods[] = {
96 &options.pubkey_authentication},
99 &options.password_authentication},
100 {"keyboard-interactive",
102 &options.kbd_interactive_authentication},
107 * loop until authctxt->success == TRUE
113 Authctxt *authctxt = authctxt_new();
115 x_authctxt = authctxt; /*XXX*/
117 /* challenge-reponse is implemented via keyboard interactive */
118 if (options.challenge_reponse_authentication)
119 options.kbd_interactive_authentication = 1;
121 dispatch_init(&protocol_error);
122 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
123 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
124 do_authenticated(authctxt);
128 protocol_error(int type, int plen, void *ctxt)
130 log("auth: protocol error: type %d plen %d", type, plen);
131 packet_start(SSH2_MSG_UNIMPLEMENTED);
138 input_service_request(int type, int plen, void *ctxt)
140 Authctxt *authctxt = ctxt;
143 char *service = packet_get_string(&len);
146 if (authctxt == NULL)
147 fatal("input_service_request: no authctxt");
149 if (strcmp(service, "ssh-userauth") == 0) {
150 if (!authctxt->success) {
152 /* now we can handle user-auth requests */
153 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
156 /* XXX all other service requests are denied */
159 packet_start(SSH2_MSG_SERVICE_ACCEPT);
160 packet_put_cstring(service);
164 debug("bad service request %s", service);
165 packet_disconnect("bad service request %s", service);
171 input_userauth_request(int type, int plen, void *ctxt)
173 Authctxt *authctxt = ctxt;
174 Authmethod *m = NULL;
175 char *user, *service, *method, *style = NULL;
176 int authenticated = 0;
178 if (authctxt == NULL)
179 fatal("input_userauth_request: no authctxt");
181 user = packet_get_string(NULL);
182 service = packet_get_string(NULL);
183 method = packet_get_string(NULL);
184 debug("userauth-request for user %s service %s method %s", user, service, method);
185 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
187 if ((style = strchr(user, ':')) != NULL)
190 if (authctxt->attempt++ == 0) {
191 /* setup auth context */
192 struct passwd *pw = NULL;
194 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
195 authctxt->pw = pwcopy(pw);
197 debug2("input_userauth_request: setting up authctxt for %s", user);
199 start_pam(pw->pw_name);
202 log("input_userauth_request: illegal user %s", user);
207 setproctitle("%s", pw ? user : "unknown");
208 authctxt->user = xstrdup(user);
209 authctxt->service = xstrdup(service);
210 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
211 } else if (authctxt->valid) {
212 if (strcmp(user, authctxt->user) != 0 ||
213 strcmp(service, authctxt->service) != 0) {
214 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
215 user, service, authctxt->user, authctxt->service);
220 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
221 authctxt->postponed = 0;
224 auth_close(authctxt->as);
229 /* try to authenticate user */
230 m = authmethod_lookup(method);
232 debug2("input_userauth_request: try method %s", method);
233 authenticated = m->userauth(authctxt);
235 userauth_finish(authctxt, authenticated, method);
243 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
245 if (!authctxt->valid && authenticated)
246 fatal("INTERNAL ERROR: authenticated invalid user %s",
249 /* Special handling for root */
250 if (authenticated && authctxt->pw->pw_uid == 0 &&
251 !auth_root_allowed(method))
255 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
260 /* Log before sending the reply */
261 auth_log(authctxt, authenticated, method, " ssh2");
263 if (!authctxt->postponed)
264 userauth_reply(authctxt, authenticated);
268 userauth_banner(void)
275 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
277 if ((fd = open(options.banner, O_RDONLY)) < 0)
279 if (fstat(fd, &st) < 0)
282 banner = xmalloc(len + 1);
283 if ((n = read(fd, banner, len)) < 0)
286 packet_start(SSH2_MSG_USERAUTH_BANNER);
287 packet_put_cstring(banner);
288 packet_put_cstring(""); /* language, unused */
290 debug("userauth_banner: sent");
299 userauth_reply(Authctxt *authctxt, int authenticated)
303 /* XXX todo: check if multiple auth methods are needed */
304 if (authenticated == 1) {
305 #ifdef WITH_AIXAUTHENTICATE
306 /* We don't have a pty yet, so just label the line as "ssh" */
307 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
308 get_canonical_hostname(options.reverse_mapping_check),
309 "ssh", &aixloginmsg) < 0)
311 #endif /* WITH_AIXAUTHENTICATE */
312 /* turn off userauth */
313 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
314 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
317 /* now we can break out */
318 authctxt->success = 1;
320 if (authctxt->failures++ > AUTH_FAIL_MAX)
321 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
322 methods = authmethods_get();
323 packet_start(SSH2_MSG_USERAUTH_FAILURE);
324 packet_put_cstring(methods);
325 packet_put_char(0); /* XXX partial success, unused */
333 userauth_none(Authctxt *authctxt)
335 /* disable method "none", only allowed one time */
336 Authmethod *m = authmethod_lookup("none");
342 if (authctxt->valid == 0)
346 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
350 return auth_pam_password(authctxt->pw, "");
351 #elif defined(HAVE_OSF_SIA)
353 #else /* !HAVE_OSF_SIA && !USE_PAM */
354 return auth_password(authctxt, "");
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 auth_sia_password(authctxt->user, password) == 1)
378 #else /* !USE_PAM && !HAVE_OSF_SIA */
379 auth_password(authctxt, password) == 1)
382 memset(password, 0, len);
384 return authenticated;
388 userauth_kbdint(Authctxt *authctxt)
390 int authenticated = 0;
394 lang = packet_get_string(NULL);
395 devs = packet_get_string(NULL);
398 debug("keyboard-interactive language %s devs %s", lang, devs);
400 if (options.challenge_reponse_authentication)
401 authenticated = auth2_challenge(authctxt, devs);
404 if (authenticated == 0)
405 authenticated = auth2_pam(authctxt);
410 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
413 return authenticated;
417 userauth_pubkey(Authctxt *authctxt)
421 char *pkalg, *pkblob, *sig;
422 u_int alen, blen, slen;
423 int have_sig, pktype;
424 int authenticated = 0;
426 if (!authctxt->valid) {
427 debug2("userauth_pubkey: disabled because of invalid user");
430 have_sig = packet_get_char();
431 if (datafellows & SSH_BUG_PKAUTH) {
432 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
433 /* no explicit pkalg given */
434 pkblob = packet_get_string(&blen);
436 buffer_append(&b, pkblob, blen);
437 /* so we have to extract the pkalg from the pkblob */
438 pkalg = buffer_get_string(&b, &alen);
441 pkalg = packet_get_string(&alen);
442 pkblob = packet_get_string(&blen);
444 pktype = key_type_from_name(pkalg);
445 if (pktype == KEY_UNSPEC) {
446 /* this is perfectly legal */
447 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
452 key = key_from_blob(pkblob, blen);
455 sig = packet_get_string(&slen);
458 if (datafellows & SSH_OLD_SESSIONID) {
459 buffer_append(&b, session_id2, session_id2_len);
461 buffer_put_string(&b, session_id2, session_id2_len);
463 /* reconstruct packet */
464 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
465 buffer_put_cstring(&b, authctxt->user);
466 buffer_put_cstring(&b,
467 datafellows & SSH_BUG_PKSERVICE ?
470 if (datafellows & SSH_BUG_PKAUTH) {
471 buffer_put_char(&b, have_sig);
473 buffer_put_cstring(&b, "publickey");
474 buffer_put_char(&b, have_sig);
475 buffer_put_cstring(&b, key_ssh_name(key));
477 buffer_put_string(&b, pkblob, blen);
481 /* test for correct signature */
482 if (user_key_allowed(authctxt->pw, key) &&
483 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
488 debug("test whether pkalg/pkblob are acceptable");
491 /* XXX fake reply and always send PK_OK ? */
493 * XXX this allows testing whether a user is allowed
494 * to login: if you happen to have a valid pubkey this
495 * message is sent. the message is NEVER sent at all
496 * if a user is not allowed to login. is this an
499 if (user_key_allowed(authctxt->pw, key)) {
500 packet_start(SSH2_MSG_USERAUTH_PK_OK);
501 packet_put_string(pkalg, alen);
502 packet_put_string(pkblob, blen);
505 authctxt->postponed = 1;
508 if (authenticated != 1)
509 auth_clear_options();
512 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
516 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
519 return authenticated;
522 /* get current user */
527 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
533 authmethods_get(void)
535 Authmethod *method = NULL;
539 for (method = authmethods; method->name != NULL; method++) {
540 if (strcmp(method->name, "none") == 0)
542 if (method->enabled != NULL && *(method->enabled) != 0) {
544 size += strlen(DELIM);
545 size += strlen(method->name);
548 size++; /* trailing '\0' */
549 list = xmalloc(size);
552 for (method = authmethods; method->name != NULL; method++) {
553 if (strcmp(method->name, "none") == 0)
555 if (method->enabled != NULL && *(method->enabled) != 0) {
557 strlcat(list, DELIM, size);
558 strlcat(list, method->name, size);
565 authmethod_lookup(const char *name)
567 Authmethod *method = NULL;
569 for (method = authmethods; method->name != NULL; method++)
570 if (method->enabled != NULL &&
571 *(method->enabled) != 0 &&
572 strcmp(name, method->name) == 0)
574 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
578 /* return 1 if user allows given key */
580 user_key_allowed(struct passwd *pw, Key *key)
582 char line[8192], file[MAXPATHLEN];
592 /* Temporarily use the user's uid. */
593 temporarily_use_uid(pw->pw_uid);
595 /* The authorized keys. */
596 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
597 _PATH_SSH_USER_PERMITTED_KEYS2);
599 /* Fail quietly if file does not exist */
600 if (stat(file, &st) < 0) {
601 /* Restore the privileged uid. */
605 /* Open the file containing the authorized keys. */
606 f = fopen(file, "r");
608 /* Restore the privileged uid. */
612 if (options.strict_modes) {
615 /* Check open file in order to avoid open/stat races */
616 if (fstat(fileno(f), &st) < 0 ||
617 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
618 (st.st_mode & 022) != 0) {
619 snprintf(buf, sizeof buf,
620 "%s authentication refused for %.100s: "
621 "bad ownership or modes for '%s'.",
622 key_type(key), pw->pw_name, file);
625 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
627 static const char *check[] = {
628 "", _PATH_SSH_USER_DIR, NULL
630 for (i = 0; check[i]; i++) {
631 snprintf(line, sizeof line, "%.500s/%.100s",
632 pw->pw_dir, check[i]);
633 if (stat(line, &st) < 0 ||
634 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
635 (st.st_mode & 022) != 0) {
636 snprintf(buf, sizeof buf,
637 "%s authentication refused for %.100s: "
638 "bad ownership or modes for '%s'.",
639 key_type(key), pw->pw_name, line);
653 found = key_new(key->type);
655 while (fgets(line, sizeof(line), f)) {
656 char *cp, *options = NULL;
658 /* Skip leading whitespace, empty and comment lines. */
659 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
661 if (!*cp || *cp == '\n' || *cp == '#')
664 if (key_read(found, &cp) == -1) {
665 /* no key? check if there are options for this key */
667 debug2("user_key_allowed: check options: '%s'", cp);
669 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
670 if (*cp == '\\' && cp[1] == '"')
671 cp++; /* Skip both */
675 /* Skip remaining whitespace. */
676 for (; *cp == ' ' || *cp == '\t'; cp++)
678 if (key_read(found, &cp) == -1) {
679 debug2("user_key_allowed: advance: '%s'", cp);
680 /* still no key? advance to next line*/
684 if (key_equal(found, key) &&
685 auth_parse_options(pw, options, file, linenum) == 1) {
687 debug("matching key found: file %s, line %ld",
696 debug2("key not found");