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.43 2001/02/22 21:59:44 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 int userauth_none(Authctxt *authctxt);
85 int userauth_passwd(Authctxt *authctxt);
86 int userauth_pubkey(Authctxt *authctxt);
87 int userauth_kbdint(Authctxt *authctxt);
89 Authmethod authmethods[] = {
95 &options.pubkey_authentication},
98 &options.password_authentication},
99 {"keyboard-interactive",
101 &options.kbd_interactive_authentication},
106 * loop until authctxt->success == TRUE
112 Authctxt *authctxt = authctxt_new();
114 x_authctxt = authctxt; /*XXX*/
116 /* challenge-reponse is implemented via keyboard interactive */
117 if (options.challenge_reponse_authentication)
118 options.kbd_interactive_authentication = 1;
120 dispatch_init(&protocol_error);
121 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
122 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
123 do_authenticated2(authctxt);
127 protocol_error(int type, int plen, void *ctxt)
129 log("auth: protocol error: type %d plen %d", type, plen);
130 packet_start(SSH2_MSG_UNIMPLEMENTED);
137 input_service_request(int type, int plen, void *ctxt)
139 Authctxt *authctxt = ctxt;
142 char *service = packet_get_string(&len);
145 if (authctxt == NULL)
146 fatal("input_service_request: no authctxt");
148 if (strcmp(service, "ssh-userauth") == 0) {
149 if (!authctxt->success) {
151 /* now we can handle user-auth requests */
152 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
155 /* XXX all other service requests are denied */
158 packet_start(SSH2_MSG_SERVICE_ACCEPT);
159 packet_put_cstring(service);
163 debug("bad service request %s", service);
164 packet_disconnect("bad service request %s", service);
170 input_userauth_request(int type, int plen, void *ctxt)
172 Authctxt *authctxt = ctxt;
173 Authmethod *m = NULL;
174 char *user, *service, *method, *style = NULL;
175 int authenticated = 0;
177 if (authctxt == NULL)
178 fatal("input_userauth_request: no authctxt");
180 user = packet_get_string(NULL);
181 service = packet_get_string(NULL);
182 method = packet_get_string(NULL);
183 debug("userauth-request for user %s service %s method %s", user, service, method);
184 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
186 if ((style = strchr(user, ':')) != NULL)
189 if (authctxt->attempt++ == 0) {
190 /* setup auth context */
191 struct passwd *pw = NULL;
193 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
194 authctxt->pw = pwcopy(pw);
196 debug2("input_userauth_request: setting up authctxt for %s", user);
198 start_pam(pw->pw_name);
201 log("input_userauth_request: illegal user %s", user);
206 setproctitle("%s", pw ? user : "unknown");
207 authctxt->user = xstrdup(user);
208 authctxt->service = xstrdup(service);
209 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
210 } else if (authctxt->valid) {
211 if (strcmp(user, authctxt->user) != 0 ||
212 strcmp(service, authctxt->service) != 0) {
213 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
214 user, service, authctxt->user, authctxt->service);
219 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
220 authctxt->postponed = 0;
223 auth_close(authctxt->as);
228 /* try to authenticate user */
229 m = authmethod_lookup(method);
231 debug2("input_userauth_request: try method %s", method);
232 authenticated = m->userauth(authctxt);
234 if (!authctxt->valid && authenticated)
235 fatal("INTERNAL ERROR: authenticated invalid user %s",
238 /* Special handling for root */
239 if (authenticated && authctxt->pw->pw_uid == 0 &&
240 !auth_root_allowed(method))
244 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
249 /* Log before sending the reply */
250 auth_log(authctxt, authenticated, method, " ssh2");
252 if (!authctxt->postponed)
253 userauth_reply(authctxt, authenticated);
261 userauth_banner(void)
268 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
270 if ((fd = open(options.banner, O_RDONLY)) < 0) {
271 error("userauth_banner: open %s failed: %s",
272 options.banner, strerror(errno));
275 if (fstat(fd, &st) < 0)
278 banner = xmalloc(len + 1);
279 if ((n = read(fd, banner, len)) < 0)
282 packet_start(SSH2_MSG_USERAUTH_BANNER);
283 packet_put_cstring(banner);
284 packet_put_cstring(""); /* language, unused */
286 debug("userauth_banner: sent");
295 userauth_reply(Authctxt *authctxt, int authenticated)
299 /* XXX todo: check if multiple auth methods are needed */
300 if (authenticated == 1) {
301 #ifdef WITH_AIXAUTHENTICATE
302 /* We don't have a pty yet, so just label the line as "ssh" */
303 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
304 get_canonical_hostname(options.reverse_mapping_check),
305 "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;
316 if (authctxt->failures++ > AUTH_FAIL_MAX)
317 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
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 */
329 userauth_none(Authctxt *authctxt)
331 /* disable method "none", only allowed one time */
332 Authmethod *m = authmethod_lookup("none");
338 if (authctxt->valid == 0)
342 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
346 return auth_pam_password(authctxt->pw, "");
347 #elif defined(HAVE_OSF_SIA)
349 #else /* !HAVE_OSF_SIA && !USE_PAM */
350 return auth_password(authctxt, "");
355 userauth_passwd(Authctxt *authctxt)
358 int authenticated = 0;
361 change = packet_get_char();
363 log("password change not supported");
364 password = packet_get_string(&len);
366 if (authctxt->valid &&
368 check_nt_auth(1, authctxt->pw->pw_uid) &&
371 auth_pam_password(authctxt->pw, password) == 1)
372 #elif defined(HAVE_OSF_SIA)
373 auth_sia_password(authctxt->user, password) == 1)
374 #else /* !USE_PAM && !HAVE_OSF_SIA */
375 auth_password(authctxt, password) == 1)
378 memset(password, 0, len);
380 return authenticated;
384 userauth_kbdint(Authctxt *authctxt)
386 int authenticated = 0;
390 lang = packet_get_string(NULL);
391 devs = packet_get_string(NULL);
394 debug("keyboard-interactive language %s devs %s", lang, devs);
396 if (options.challenge_reponse_authentication)
397 authenticated = auth2_challenge(authctxt, devs);
400 if (authenticated == 0)
401 authenticated = auth2_pam(authctxt);
406 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
409 return authenticated;
413 userauth_pubkey(Authctxt *authctxt)
417 char *pkalg, *pkblob, *sig;
418 u_int alen, blen, slen;
419 int have_sig, pktype;
420 int authenticated = 0;
422 if (!authctxt->valid) {
423 debug2("userauth_pubkey: disabled because of invalid user");
426 have_sig = packet_get_char();
427 if (datafellows & SSH_BUG_PKAUTH) {
428 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
429 /* no explicit pkalg given */
430 pkblob = packet_get_string(&blen);
432 buffer_append(&b, pkblob, blen);
433 /* so we have to extract the pkalg from the pkblob */
434 pkalg = buffer_get_string(&b, &alen);
437 pkalg = packet_get_string(&alen);
438 pkblob = packet_get_string(&blen);
440 pktype = key_type_from_name(pkalg);
441 if (pktype == KEY_UNSPEC) {
442 /* this is perfectly legal */
443 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
448 key = key_from_blob(pkblob, blen);
451 sig = packet_get_string(&slen);
454 if (datafellows & SSH_OLD_SESSIONID) {
455 buffer_append(&b, session_id2, session_id2_len);
457 buffer_put_string(&b, session_id2, session_id2_len);
459 /* reconstruct packet */
460 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
461 buffer_put_cstring(&b, authctxt->user);
462 buffer_put_cstring(&b,
463 datafellows & SSH_BUG_PKSERVICE ?
466 if (datafellows & SSH_BUG_PKAUTH) {
467 buffer_put_char(&b, have_sig);
469 buffer_put_cstring(&b, "publickey");
470 buffer_put_char(&b, have_sig);
471 buffer_put_cstring(&b, key_ssh_name(key));
473 buffer_put_string(&b, pkblob, blen);
477 /* test for correct signature */
478 if (user_key_allowed(authctxt->pw, key) &&
479 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
484 debug("test whether pkalg/pkblob are acceptable");
487 /* XXX fake reply and always send PK_OK ? */
489 * XXX this allows testing whether a user is allowed
490 * to login: if you happen to have a valid pubkey this
491 * message is sent. the message is NEVER sent at all
492 * if a user is not allowed to login. is this an
495 if (user_key_allowed(authctxt->pw, key)) {
496 packet_start(SSH2_MSG_USERAUTH_PK_OK);
497 packet_put_string(pkalg, alen);
498 packet_put_string(pkblob, blen);
501 authctxt->postponed = 1;
504 if (authenticated != 1)
505 auth_clear_options();
508 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
512 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
515 return authenticated;
518 /* get current user */
523 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
529 authmethods_get(void)
531 Authmethod *method = NULL;
535 for (method = authmethods; method->name != NULL; method++) {
536 if (strcmp(method->name, "none") == 0)
538 if (method->enabled != NULL && *(method->enabled) != 0) {
540 size += strlen(DELIM);
541 size += strlen(method->name);
544 size++; /* trailing '\0' */
545 list = xmalloc(size);
548 for (method = authmethods; method->name != NULL; method++) {
549 if (strcmp(method->name, "none") == 0)
551 if (method->enabled != NULL && *(method->enabled) != 0) {
553 strlcat(list, DELIM, size);
554 strlcat(list, method->name, size);
561 authmethod_lookup(const char *name)
563 Authmethod *method = NULL;
565 for (method = authmethods; method->name != NULL; method++)
566 if (method->enabled != NULL &&
567 *(method->enabled) != 0 &&
568 strcmp(name, method->name) == 0)
570 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
574 /* return 1 if user allows given key */
576 user_key_allowed(struct passwd *pw, Key *key)
578 char line[8192], file[MAXPATHLEN];
588 /* Temporarily use the user's uid. */
589 temporarily_use_uid(pw->pw_uid);
591 /* The authorized keys. */
592 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
593 _PATH_SSH_USER_PERMITTED_KEYS2);
595 /* Fail quietly if file does not exist */
596 if (stat(file, &st) < 0) {
597 /* Restore the privileged uid. */
601 /* Open the file containing the authorized keys. */
602 f = fopen(file, "r");
604 /* Restore the privileged uid. */
608 if (options.strict_modes) {
611 /* Check open file in order to avoid open/stat races */
612 if (fstat(fileno(f), &st) < 0 ||
613 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
614 (st.st_mode & 022) != 0) {
615 snprintf(buf, sizeof buf,
616 "%s authentication refused for %.100s: "
617 "bad ownership or modes for '%s'.",
618 key_type(key), pw->pw_name, file);
621 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
623 static const char *check[] = {
624 "", _PATH_SSH_USER_DIR, NULL
626 for (i = 0; check[i]; i++) {
627 snprintf(line, sizeof line, "%.500s/%.100s",
628 pw->pw_dir, check[i]);
629 if (stat(line, &st) < 0 ||
630 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
631 (st.st_mode & 022) != 0) {
632 snprintf(buf, sizeof buf,
633 "%s authentication refused for %.100s: "
634 "bad ownership or modes for '%s'.",
635 key_type(key), pw->pw_name, line);
649 found = key_new(key->type);
651 while (fgets(line, sizeof(line), f)) {
652 char *cp, *options = NULL;
654 /* Skip leading whitespace, empty and comment lines. */
655 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
657 if (!*cp || *cp == '\n' || *cp == '#')
660 if (key_read(found, &cp) == -1) {
661 /* no key? check if there are options for this key */
663 debug2("user_key_allowed: check options: '%s'", cp);
665 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
666 if (*cp == '\\' && cp[1] == '"')
667 cp++; /* Skip both */
671 /* Skip remaining whitespace. */
672 for (; *cp == ' ' || *cp == '\t'; cp++)
674 if (key_read(found, &cp) == -1) {
675 debug2("user_key_allowed: advance: '%s'", cp);
676 /* still no key? advance to next line*/
680 if (key_equal(found, key) &&
681 auth_parse_options(pw, options, file, linenum) == 1) {
683 debug("matching key found: file %s, line %ld",