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.24 2000/12/28 14:25:51 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->failures = 0;
128 authctxt->success = 0;
129 x_authctxt = authctxt; /*XXX*/
132 /* turn off kerberos, not supported by SSH2 */
133 options.kerberos_authentication = 0;
135 dispatch_init(&protocol_error);
136 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
137 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
142 protocol_error(int type, int plen, void *ctxt)
144 log("auth: protocol error: type %d plen %d", type, plen);
145 packet_start(SSH2_MSG_UNIMPLEMENTED);
152 input_service_request(int type, int plen, void *ctxt)
154 Authctxt *authctxt = ctxt;
157 char *service = packet_get_string(&len);
160 if (authctxt == NULL)
161 fatal("input_service_request: no authctxt");
163 if (strcmp(service, "ssh-userauth") == 0) {
164 if (!authctxt->success) {
166 /* now we can handle user-auth requests */
167 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
170 /* XXX all other service requests are denied */
173 packet_start(SSH2_MSG_SERVICE_ACCEPT);
174 packet_put_cstring(service);
178 debug("bad service request %s", service);
179 packet_disconnect("bad service request %s", service);
185 input_userauth_request(int type, int plen, void *ctxt)
187 Authctxt *authctxt = ctxt;
188 Authmethod *m = NULL;
189 char *user, *service, *method;
190 int authenticated = 0;
192 if (authctxt == NULL)
193 fatal("input_userauth_request: no authctxt");
195 user = packet_get_string(NULL);
196 service = packet_get_string(NULL);
197 method = packet_get_string(NULL);
198 debug("userauth-request for user %s service %s method %s", user, service, method);
199 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
201 if (authctxt->attempt++ == 0) {
202 /* setup auth context */
203 struct passwd *pw = NULL;
204 setproctitle("%s", user);
206 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
207 authctxt->pw = pwcopy(pw);
209 debug2("input_userauth_request: setting up authctxt for %s", user);
214 log("input_userauth_request: illegal user %s", user);
216 authctxt->user = xstrdup(user);
217 authctxt->service = xstrdup(service);
218 } else if (authctxt->valid) {
219 if (strcmp(user, authctxt->user) != 0 ||
220 strcmp(service, authctxt->service) != 0) {
221 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
222 user, service, authctxt->user, authctxt->service);
227 m = authmethod_lookup(method);
229 debug2("input_userauth_request: try method %s", method);
230 authenticated = m->userauth(authctxt);
232 debug2("input_userauth_request: unsupported method %s", method);
234 if (!authctxt->valid && authenticated == 1) {
235 log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method);
239 /* Special handling for root */
240 if (authenticated == 1 &&
241 authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) {
243 log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname());
247 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
251 /* Log before sending the reply */
252 userauth_log(authctxt, authenticated, method);
253 userauth_reply(authctxt, authenticated);
262 userauth_log(Authctxt *authctxt, int authenticated, char *method)
264 void (*authlog) (const char *fmt,...) = verbose;
265 char *user = NULL, *authmsg = NULL;
267 /* Raise logging level */
268 if (authenticated == 1 ||
270 authctxt->failures >= AUTH_FAIL_LOG ||
271 strcmp(method, "password") == 0)
274 if (authenticated == 1) {
275 authmsg = "Accepted";
276 } else if (authenticated == 0) {
279 authmsg = "Postponed";
282 if (authctxt->valid) {
283 user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user;
288 authlog("%s %s for %.200s from %.200s port %d ssh2",
297 userauth_reply(Authctxt *authctxt, int authenticated)
300 /* XXX todo: check if multiple auth methods are needed */
301 if (authenticated == 1) {
302 #ifdef WITH_AIXAUTHENTICATE
303 /* We don't have a pty yet, so just label the line as "ssh" */
304 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
305 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
307 #endif /* WITH_AIXAUTHENTICATE */
308 /* turn off userauth */
309 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
310 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
313 /* now we can break out */
314 authctxt->success = 1;
315 } else if (authenticated == 0) {
316 if (authctxt->failures++ >= AUTH_FAIL_MAX)
317 packet_disconnect("too many failed userauth_requests");
318 methods = authmethods_get();
319 packet_start(SSH2_MSG_USERAUTH_FAILURE);
320 packet_put_cstring(methods);
321 packet_put_char(0); /* XXX partial success, unused */
326 /* do nothing, we did already send a reply */
331 userauth_none(Authctxt *authctxt)
333 /* disable method "none", only allowed one time */
334 Authmethod *m = authmethod_lookup("none");
339 if (authctxt->valid == 0)
343 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
347 return auth_pam_password(authctxt->pw, "");
348 #elif defined(HAVE_OSF_SIA)
349 return (sia_validate_user(NULL, saved_argc, saved_argv,
350 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
351 NULL, 0, NULL, "") == SIASUCCESS);
352 #else /* !HAVE_OSF_SIA && !USE_PAM */
353 return auth_password(authctxt->pw, "");
358 userauth_passwd(Authctxt *authctxt)
361 int authenticated = 0;
364 change = packet_get_char();
366 log("password change not supported");
367 password = packet_get_string(&len);
369 if (authctxt->valid &&
371 check_nt_auth(1, authctxt->pw->pw_uid) &&
374 auth_pam_password(authctxt->pw, password) == 1)
375 #elif defined(HAVE_OSF_SIA)
376 sia_validate_user(NULL, saved_argc, saved_argv,
377 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
378 NULL, 0, NULL, password) == SIASUCCESS)
379 #else /* !USE_PAM && !HAVE_OSF_SIA */
380 auth_password(authctxt->pw, password) == 1)
383 memset(password, 0, len);
385 return authenticated;
389 userauth_kbdint(Authctxt *authctxt)
391 int authenticated = 0;
395 lang = packet_get_string(NULL);
396 devs = packet_get_string(NULL);
399 debug("keyboard-interactive language %s devs %s", lang, devs);
401 if (authenticated == 0)
402 authenticated = auth2_pam(authctxt);
405 /* XXX hardcoded, we should look at devs */
406 if (authenticated == 0)
407 if (options.skey_authentication != 0)
408 authenticated = auth2_skey(authctxt);
413 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
416 return authenticated;
420 userauth_pubkey(Authctxt *authctxt)
424 char *pkalg, *pkblob, *sig;
425 u_int alen, blen, slen;
426 int have_sig, pktype;
427 int authenticated = 0;
429 if (!authctxt->valid) {
430 debug2("userauth_pubkey: disabled because of invalid user");
433 have_sig = packet_get_char();
434 if (datafellows & SSH_BUG_PKAUTH) {
435 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
436 /* no explicit pkalg given */
437 pkblob = packet_get_string(&blen);
439 buffer_append(&b, pkblob, blen);
440 /* so we have to extract the pkalg from the pkblob */
441 pkalg = buffer_get_string(&b, &alen);
444 pkalg = packet_get_string(&alen);
445 pkblob = packet_get_string(&blen);
447 pktype = key_type_from_name(pkalg);
448 if (pktype == KEY_UNSPEC) {
449 /* this is perfectly legal */
450 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
455 key = key_from_blob(pkblob, blen);
458 sig = packet_get_string(&slen);
461 if (datafellows & SSH_OLD_SESSIONID) {
462 buffer_append(&b, session_id2, session_id2_len);
464 buffer_put_string(&b, session_id2, session_id2_len);
466 /* reconstruct packet */
467 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
468 buffer_put_cstring(&b, authctxt->user);
469 buffer_put_cstring(&b,
470 datafellows & SSH_BUG_PKSERVICE ?
473 if (datafellows & SSH_BUG_PKAUTH) {
474 buffer_put_char(&b, have_sig);
476 buffer_put_cstring(&b, "publickey");
477 buffer_put_char(&b, have_sig);
478 buffer_put_cstring(&b, key_ssh_name(key));
480 buffer_put_string(&b, pkblob, blen);
484 /* test for correct signature */
485 if (user_key_allowed(authctxt->pw, key) &&
486 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
491 debug("test whether pkalg/pkblob are acceptable");
494 /* XXX fake reply and always send PK_OK ? */
496 * XXX this allows testing whether a user is allowed
497 * to login: if you happen to have a valid pubkey this
498 * message is sent. the message is NEVER sent at all
499 * if a user is not allowed to login. is this an
502 if (user_key_allowed(authctxt->pw, key)) {
503 packet_start(SSH2_MSG_USERAUTH_PK_OK);
504 packet_put_string(pkalg, alen);
505 packet_put_string(pkblob, blen);
511 if (authenticated != 1)
512 auth_clear_options();
515 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
519 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
522 return authenticated;
525 /* get current user */
530 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
536 authmethods_get(void)
538 Authmethod *method = NULL;
542 for (method = authmethods; method->name != NULL; method++) {
543 if (strcmp(method->name, "none") == 0)
545 if (method->enabled != NULL && *(method->enabled) != 0) {
547 size += strlen(DELIM);
548 size += strlen(method->name);
551 size++; /* trailing '\0' */
552 list = xmalloc(size);
555 for (method = authmethods; method->name != NULL; method++) {
556 if (strcmp(method->name, "none") == 0)
558 if (method->enabled != NULL && *(method->enabled) != 0) {
560 strlcat(list, DELIM, size);
561 strlcat(list, method->name, size);
568 authmethod_lookup(const char *name)
570 Authmethod *method = NULL;
572 for (method = authmethods; method->name != NULL; method++)
573 if (method->enabled != NULL &&
574 *(method->enabled) != 0 &&
575 strcmp(name, method->name) == 0)
577 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
581 /* return 1 if user allows given key */
583 user_key_allowed(struct passwd *pw, Key *key)
585 char line[8192], file[1024];
595 /* Temporarily use the user's uid. */
596 temporarily_use_uid(pw->pw_uid);
598 /* The authorized keys. */
599 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
600 SSH_USER_PERMITTED_KEYS2);
602 /* Fail quietly if file does not exist */
603 if (stat(file, &st) < 0) {
604 /* Restore the privileged uid. */
608 /* Open the file containing the authorized keys. */
609 f = fopen(file, "r");
611 /* Restore the privileged uid. */
615 if (options.strict_modes) {
618 /* Check open file in order to avoid open/stat races */
619 if (fstat(fileno(f), &st) < 0 ||
620 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
621 (st.st_mode & 022) != 0) {
622 snprintf(buf, sizeof buf,
623 "%s authentication refused for %.100s: "
624 "bad ownership or modes for '%s'.",
625 key_type(key), pw->pw_name, file);
628 /* Check path to SSH_USER_PERMITTED_KEYS */
630 static const char *check[] = {
631 "", SSH_USER_DIR, NULL
633 for (i = 0; check[i]; i++) {
634 snprintf(line, sizeof line, "%.500s/%.100s",
635 pw->pw_dir, check[i]);
636 if (stat(line, &st) < 0 ||
637 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
638 (st.st_mode & 022) != 0) {
639 snprintf(buf, sizeof buf,
640 "%s authentication refused for %.100s: "
641 "bad ownership or modes for '%s'.",
642 key_type(key), pw->pw_name, line);
656 found = key_new(key->type);
658 while (fgets(line, sizeof(line), f)) {
659 char *cp, *options = NULL;
661 /* Skip leading whitespace, empty and comment lines. */
662 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
664 if (!*cp || *cp == '\n' || *cp == '#')
667 if (key_read(found, &cp) == -1) {
668 /* no key? check if there are options for this key */
670 debug2("user_key_allowed: check options: '%s'", cp);
672 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
673 if (*cp == '\\' && cp[1] == '"')
674 cp++; /* Skip both */
678 /* Skip remaining whitespace. */
679 for (; *cp == ' ' || *cp == '\t'; cp++)
681 if (key_read(found, &cp) == -1) {
682 debug2("user_key_allowed: advance: '%s'", cp);
683 /* still no key? advance to next line*/
687 if (key_equal(found, key) &&
688 auth_parse_options(pw, options, linenum) == 1) {
690 debug("matching key found: file %s, line %ld",
702 pwcopy(struct passwd *pw)
704 struct passwd *copy = xmalloc(sizeof(*copy));
705 memset(copy, 0, sizeof(*copy));
706 copy->pw_name = xstrdup(pw->pw_name);
707 copy->pw_passwd = xstrdup(pw->pw_passwd);
708 copy->pw_uid = pw->pw_uid;
709 copy->pw_gid = pw->pw_gid;
710 #ifdef HAVE_PW_CLASS_IN_PASSWD
711 copy->pw_class = xstrdup(pw->pw_class);
713 copy->pw_dir = xstrdup(pw->pw_dir);
714 copy->pw_shell = xstrdup(pw->pw_shell);