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.23 2000/12/19 23:17:55 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);
89 Authmethod *authmethod_lookup(const char *name);
90 struct passwd *pwcopy(struct passwd *pw);
91 int user_key_allowed(struct passwd *pw, Key *key);
92 char *authmethods_get(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 = xmalloc(sizeof(*authctxt));
124 memset(authctxt, 'a', sizeof(*authctxt));
126 authctxt->attempt = 0;
127 authctxt->success = 0;
128 x_authctxt = authctxt; /*XXX*/
131 /* turn off kerberos, not supported by SSH2 */
132 options.kerberos_authentication = 0;
134 dispatch_init(&protocol_error);
135 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
136 dispatch_run(DISPATCH_BLOCK, &authctxt->success, 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;
189 int authenticated = 0;
191 if (authctxt == NULL)
192 fatal("input_userauth_request: no authctxt");
193 if (authctxt->attempt++ >= AUTH_FAIL_MAX) {
194 #ifdef WITH_AIXAUTHENTICATE
195 loginfailed(authctxt->user?authctxt->user:"NOUSER",
196 get_canonical_hostname(), "ssh");
197 #endif /* WITH_AIXAUTHENTICATE */
198 packet_disconnect("too many failed userauth_requests");
201 user = packet_get_string(NULL);
202 service = packet_get_string(NULL);
203 method = packet_get_string(NULL);
204 debug("userauth-request for user %s service %s method %s", user, service, method);
205 debug("attempt #%d", authctxt->attempt);
207 if (authctxt->attempt == 1) {
208 /* setup auth context */
209 struct passwd *pw = NULL;
210 setproctitle("%s", user);
212 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
213 authctxt->pw = pwcopy(pw);
215 debug2("input_userauth_request: setting up authctxt for %s", user);
220 log("input_userauth_request: illegal user %s", user);
222 authctxt->user = xstrdup(user);
223 authctxt->service = xstrdup(service);
224 } else if (authctxt->valid) {
225 if (strcmp(user, authctxt->user) != 0 ||
226 strcmp(service, authctxt->service) != 0) {
227 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
228 user, service, authctxt->user, authctxt->service);
233 m = authmethod_lookup(method);
235 debug2("input_userauth_request: try method %s", method);
236 authenticated = m->userauth(authctxt);
238 debug2("input_userauth_request: unsupported method %s", method);
240 if (!authctxt->valid && authenticated == 1) {
241 log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method);
245 /* Special handling for root */
246 if (authenticated == 1 &&
247 authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) {
249 log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname());
253 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
257 /* Log before sending the reply */
258 userauth_log(authctxt, authenticated, method);
259 userauth_reply(authctxt, authenticated);
268 userauth_log(Authctxt *authctxt, int authenticated, char *method)
270 void (*authlog) (const char *fmt,...) = verbose;
271 char *user = NULL, *authmsg = NULL;
273 /* Raise logging level */
274 if (authenticated == 1 ||
276 authctxt->attempt >= AUTH_FAIL_LOG ||
277 strcmp(method, "password") == 0)
280 if (authenticated == 1) {
281 authmsg = "Accepted";
282 } else if (authenticated == 0) {
285 authmsg = "Postponed";
288 if (authctxt->valid) {
289 user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user;
294 authlog("%s %s for %.200s from %.200s port %d ssh2",
303 userauth_reply(Authctxt *authctxt, int authenticated)
305 /* XXX todo: check if multiple auth methods are needed */
306 if (authenticated == 1) {
307 #ifdef WITH_AIXAUTHENTICATE
308 /* We don't have a pty yet, so just label the line as "ssh" */
309 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
310 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
312 #endif /* WITH_AIXAUTHENTICATE */
313 /* turn off userauth */
314 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
315 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
318 /* now we can break out */
319 authctxt->success = 1;
320 } else if (authenticated == 0) {
321 char *methods = authmethods_get();
322 packet_start(SSH2_MSG_USERAUTH_FAILURE);
323 packet_put_cstring(methods);
324 packet_put_char(0); /* XXX partial success, unused */
329 /* do nothing, we did already send a reply */
334 userauth_none(Authctxt *authctxt)
336 /* disable method "none", only allowed one time */
337 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)
352 return (sia_validate_user(NULL, saved_argc, saved_argv,
353 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
354 NULL, 0, NULL, "") == SIASUCCESS);
355 #else /* !HAVE_OSF_SIA && !USE_PAM */
356 return auth_password(authctxt->pw, "");
361 userauth_passwd(Authctxt *authctxt)
364 int authenticated = 0;
367 change = packet_get_char();
369 log("password change not supported");
370 password = packet_get_string(&len);
372 if (authctxt->valid &&
374 check_nt_auth(1, authctxt->pw->pw_uid) &&
377 auth_pam_password(authctxt->pw, password) == 1)
378 #elif defined(HAVE_OSF_SIA)
379 sia_validate_user(NULL, saved_argc, saved_argv,
380 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
381 NULL, 0, NULL, password) == SIASUCCESS)
382 #else /* !USE_PAM && !HAVE_OSF_SIA */
383 auth_password(authctxt->pw, password) == 1)
386 memset(password, 0, len);
388 return authenticated;
392 userauth_kbdint(Authctxt *authctxt)
394 int authenticated = 0;
398 lang = packet_get_string(NULL);
399 devs = packet_get_string(NULL);
402 debug("keyboard-interactive language %s devs %s", lang, devs);
404 if (authenticated == 0)
405 authenticated = auth2_pam(authctxt);
408 /* XXX hardcoded, we should look at devs */
409 if (authenticated == 0)
410 if (options.skey_authentication != 0)
411 authenticated = auth2_skey(authctxt);
416 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
419 return authenticated;
423 userauth_pubkey(Authctxt *authctxt)
427 char *pkalg, *pkblob, *sig;
428 u_int alen, blen, slen;
429 int have_sig, pktype;
430 int authenticated = 0;
432 if (!authctxt->valid) {
433 debug2("userauth_pubkey: disabled because of invalid user");
436 have_sig = packet_get_char();
437 if (datafellows & SSH_BUG_PKAUTH) {
438 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
439 /* no explicit pkalg given */
440 pkblob = packet_get_string(&blen);
442 buffer_append(&b, pkblob, blen);
443 /* so we have to extract the pkalg from the pkblob */
444 pkalg = buffer_get_string(&b, &alen);
447 pkalg = packet_get_string(&alen);
448 pkblob = packet_get_string(&blen);
450 pktype = key_type_from_name(pkalg);
451 if (pktype == KEY_UNSPEC) {
452 /* this is perfectly legal */
453 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
458 key = key_from_blob(pkblob, blen);
461 sig = packet_get_string(&slen);
464 if (datafellows & SSH_OLD_SESSIONID) {
465 buffer_append(&b, session_id2, session_id2_len);
467 buffer_put_string(&b, session_id2, session_id2_len);
469 /* reconstruct packet */
470 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
471 buffer_put_cstring(&b, authctxt->user);
472 buffer_put_cstring(&b,
473 datafellows & SSH_BUG_PKSERVICE ?
476 if (datafellows & SSH_BUG_PKAUTH) {
477 buffer_put_char(&b, have_sig);
479 buffer_put_cstring(&b, "publickey");
480 buffer_put_char(&b, have_sig);
481 buffer_put_cstring(&b, key_ssh_name(key));
483 buffer_put_string(&b, pkblob, blen);
487 /* test for correct signature */
488 if (user_key_allowed(authctxt->pw, key) &&
489 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
494 debug("test whether pkalg/pkblob are acceptable");
497 /* XXX fake reply and always send PK_OK ? */
499 * XXX this allows testing whether a user is allowed
500 * to login: if you happen to have a valid pubkey this
501 * message is sent. the message is NEVER sent at all
502 * if a user is not allowed to login. is this an
505 if (user_key_allowed(authctxt->pw, key)) {
506 packet_start(SSH2_MSG_USERAUTH_PK_OK);
507 packet_put_string(pkalg, alen);
508 packet_put_string(pkblob, blen);
514 if (authenticated != 1)
515 auth_clear_options();
518 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
522 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
525 return authenticated;
528 /* get current user */
533 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
539 authmethods_get(void)
541 Authmethod *method = NULL;
545 for (method = authmethods; method->name != NULL; method++) {
546 if (strcmp(method->name, "none") == 0)
548 if (method->enabled != NULL && *(method->enabled) != 0) {
550 size += strlen(DELIM);
551 size += strlen(method->name);
554 size++; /* trailing '\0' */
555 list = xmalloc(size);
558 for (method = authmethods; method->name != NULL; method++) {
559 if (strcmp(method->name, "none") == 0)
561 if (method->enabled != NULL && *(method->enabled) != 0) {
563 strlcat(list, DELIM, size);
564 strlcat(list, method->name, size);
571 authmethod_lookup(const char *name)
573 Authmethod *method = NULL;
575 for (method = authmethods; method->name != NULL; method++)
576 if (method->enabled != NULL &&
577 *(method->enabled) != 0 &&
578 strcmp(name, method->name) == 0)
580 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
584 /* return 1 if user allows given key */
586 user_key_allowed(struct passwd *pw, Key *key)
588 char line[8192], file[1024];
598 /* Temporarily use the user's uid. */
599 temporarily_use_uid(pw->pw_uid);
601 /* The authorized keys. */
602 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
603 SSH_USER_PERMITTED_KEYS2);
605 /* Fail quietly if file does not exist */
606 if (stat(file, &st) < 0) {
607 /* Restore the privileged uid. */
611 /* Open the file containing the authorized keys. */
612 f = fopen(file, "r");
614 /* Restore the privileged uid. */
618 if (options.strict_modes) {
621 /* Check open file in order to avoid open/stat races */
622 if (fstat(fileno(f), &st) < 0 ||
623 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
624 (st.st_mode & 022) != 0) {
625 snprintf(buf, sizeof buf,
626 "%s authentication refused for %.100s: "
627 "bad ownership or modes for '%s'.",
628 key_type(key), pw->pw_name, file);
631 /* Check path to SSH_USER_PERMITTED_KEYS */
633 static const char *check[] = {
634 "", SSH_USER_DIR, NULL
636 for (i = 0; check[i]; i++) {
637 snprintf(line, sizeof line, "%.500s/%.100s",
638 pw->pw_dir, check[i]);
639 if (stat(line, &st) < 0 ||
640 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
641 (st.st_mode & 022) != 0) {
642 snprintf(buf, sizeof buf,
643 "%s authentication refused for %.100s: "
644 "bad ownership or modes for '%s'.",
645 key_type(key), pw->pw_name, line);
659 found = key_new(key->type);
661 while (fgets(line, sizeof(line), f)) {
662 char *cp, *options = NULL;
664 /* Skip leading whitespace, empty and comment lines. */
665 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
667 if (!*cp || *cp == '\n' || *cp == '#')
670 if (key_read(found, &cp) == -1) {
671 /* no key? check if there are options for this key */
673 debug2("user_key_allowed: check options: '%s'", cp);
675 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
676 if (*cp == '\\' && cp[1] == '"')
677 cp++; /* Skip both */
681 /* Skip remaining whitespace. */
682 for (; *cp == ' ' || *cp == '\t'; cp++)
684 if (key_read(found, &cp) == -1) {
685 debug2("user_key_allowed: advance: '%s'", cp);
686 /* still no key? advance to next line*/
690 if (key_equal(found, key) &&
691 auth_parse_options(pw, options, linenum) == 1) {
693 debug("matching key found: file %s, line %ld",
705 pwcopy(struct passwd *pw)
707 struct passwd *copy = xmalloc(sizeof(*copy));
708 memset(copy, 0, sizeof(*copy));
709 copy->pw_name = xstrdup(pw->pw_name);
710 copy->pw_passwd = xstrdup(pw->pw_passwd);
711 copy->pw_uid = pw->pw_uid;
712 copy->pw_gid = pw->pw_gid;
713 #ifdef HAVE_PW_CLASS_IN_PASSWD
714 copy->pw_class = xstrdup(pw->pw_class);
716 copy->pw_dir = xstrdup(pw->pw_dir);
717 copy->pw_shell = xstrdup(pw->pw_shell);