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.34 2001/01/22 23:06:39 markus Exp $");
33 #include <openssl/evp.h>
53 #include "pathnames.h"
55 #include "auth-options.h"
58 extern ServerOptions options;
59 extern u_char *session_id2;
60 extern int session_id2_len;
62 #ifdef WITH_AIXAUTHENTICATE
63 extern char *aixloginmsg;
66 extern int saved_argc;
67 extern char **saved_argv;
70 static Authctxt *x_authctxt = NULL;
73 typedef struct Authmethod Authmethod;
76 int (*userauth)(Authctxt *authctxt);
82 void input_service_request(int type, int plen, void *ctxt);
83 void input_userauth_request(int type, int plen, void *ctxt);
84 void protocol_error(int type, int plen, void *ctxt);
87 Authmethod *authmethod_lookup(const char *name);
88 struct passwd *pwcopy(struct passwd *pw);
89 int user_key_allowed(struct passwd *pw, Key *key);
90 char *authmethods_get(void);
93 void userauth_banner(void);
94 int userauth_none(Authctxt *authctxt);
95 int userauth_passwd(Authctxt *authctxt);
96 int userauth_pubkey(Authctxt *authctxt);
97 int userauth_kbdint(Authctxt *authctxt);
99 Authmethod authmethods[] = {
105 &options.pubkey_authentication},
106 {"keyboard-interactive",
108 &options.kbd_interactive_authentication},
111 &options.password_authentication},
116 * loop until authctxt->success == TRUE
122 Authctxt *authctxt = authctxt_new();
124 x_authctxt = authctxt; /*XXX*/
126 /* challenge-reponse is implemented via keyboard interactive */
127 if (options.challenge_reponse_authentication)
128 options.kbd_interactive_authentication = 1;
131 /* If machine has AFS, set process authentication group. */
137 dispatch_init(&protocol_error);
138 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
139 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
140 do_authenticated2(authctxt);
144 protocol_error(int type, int plen, void *ctxt)
146 log("auth: protocol error: type %d plen %d", type, plen);
147 packet_start(SSH2_MSG_UNIMPLEMENTED);
154 input_service_request(int type, int plen, void *ctxt)
156 Authctxt *authctxt = ctxt;
159 char *service = packet_get_string(&len);
162 if (authctxt == NULL)
163 fatal("input_service_request: no authctxt");
165 if (strcmp(service, "ssh-userauth") == 0) {
166 if (!authctxt->success) {
168 /* now we can handle user-auth requests */
169 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
172 /* XXX all other service requests are denied */
175 packet_start(SSH2_MSG_SERVICE_ACCEPT);
176 packet_put_cstring(service);
180 debug("bad service request %s", service);
181 packet_disconnect("bad service request %s", service);
187 input_userauth_request(int type, int plen, void *ctxt)
189 Authctxt *authctxt = ctxt;
190 Authmethod *m = NULL;
191 char *user, *service, *method, *style = NULL;
192 int authenticated = 0;
194 if (authctxt == NULL)
195 fatal("input_userauth_request: no authctxt");
197 user = packet_get_string(NULL);
198 service = packet_get_string(NULL);
199 method = packet_get_string(NULL);
200 debug("userauth-request for user %s service %s method %s", user, service, method);
201 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
203 if ((style = strchr(user, ':')) != NULL)
206 if (authctxt->attempt++ == 0) {
207 /* setup auth context */
208 struct passwd *pw = NULL;
209 setproctitle("%s", user);
211 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
212 authctxt->pw = pwcopy(pw);
214 debug2("input_userauth_request: setting up authctxt for %s", user);
216 start_pam(pw->pw_name);
219 log("input_userauth_request: illegal user %s", user);
224 authctxt->user = xstrdup(user);
225 authctxt->service = xstrdup(service);
226 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
227 } else if (authctxt->valid) {
228 if (strcmp(user, authctxt->user) != 0 ||
229 strcmp(service, authctxt->service) != 0) {
230 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
231 user, service, authctxt->user, authctxt->service);
236 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
237 authctxt->postponed = 0;
239 /* try to authenticate user */
240 m = authmethod_lookup(method);
242 debug2("input_userauth_request: try method %s", method);
243 authenticated = m->userauth(authctxt);
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 && !auth_root_allowed())
254 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
258 /* Log before sending the reply */
259 auth_log(authctxt, authenticated, method, " ssh2");
261 if (!authctxt->postponed)
262 userauth_reply(authctxt, authenticated);
270 userauth_banner(void)
277 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
279 if ((fd = open(options.banner, O_RDONLY)) < 0) {
280 error("userauth_banner: open %s failed: %s",
281 options.banner, strerror(errno));
284 if (fstat(fd, &st) < 0)
287 banner = xmalloc(len + 1);
288 if ((n = read(fd, banner, len)) < 0)
291 packet_start(SSH2_MSG_USERAUTH_BANNER);
292 packet_put_cstring(banner);
293 packet_put_cstring(""); /* language, unused */
295 debug("userauth_banner: sent");
304 userauth_reply(Authctxt *authctxt, int authenticated)
308 /* XXX todo: check if multiple auth methods are needed */
310 #ifdef WITH_AIXAUTHENTICATE
311 /* We don't have a pty yet, so just label the line as "ssh" */
312 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
313 get_canonical_hostname(options.reverse_mapping_check),
314 "ssh", &aixloginmsg) < 0)
316 #endif /* WITH_AIXAUTHENTICATE */
317 /* turn off userauth */
318 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
319 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
322 /* now we can break out */
323 authctxt->success = 1;
325 if (authctxt->failures++ > AUTH_FAIL_MAX)
326 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
327 methods = authmethods_get();
328 packet_start(SSH2_MSG_USERAUTH_FAILURE);
329 packet_put_cstring(methods);
330 packet_put_char(0); /* XXX partial success, unused */
338 userauth_none(Authctxt *authctxt)
340 /* disable method "none", only allowed one time */
341 Authmethod *m = authmethod_lookup("none");
347 if (authctxt->valid == 0)
351 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
355 return auth_pam_password(authctxt->pw, "");
356 #elif defined(HAVE_OSF_SIA)
357 return (sia_validate_user(NULL, saved_argc, saved_argv,
358 get_canonical_hostname(options.reverse_mapping_check),
359 authctxt->user?authctxt->user:"NOUSER", NULL, 0,
360 NULL, "") == SIASUCCESS);
361 #else /* !HAVE_OSF_SIA && !USE_PAM */
362 return auth_password(authctxt->pw, "");
367 userauth_passwd(Authctxt *authctxt)
370 int authenticated = 0;
373 change = packet_get_char();
375 log("password change not supported");
376 password = packet_get_string(&len);
378 if (authctxt->valid &&
380 check_nt_auth(1, authctxt->pw->pw_uid) &&
383 auth_pam_password(authctxt->pw, password) == 1)
384 #elif defined(HAVE_OSF_SIA)
385 sia_validate_user(NULL, saved_argc, saved_argv,
386 get_canonical_hostname(options.reverse_mapping_check),
387 authctxt->user?authctxt->user:"NOUSER", NULL, 0, NULL,
388 password) == SIASUCCESS)
389 #else /* !USE_PAM && !HAVE_OSF_SIA */
390 auth_password(authctxt->pw, password) == 1)
393 memset(password, 0, len);
395 return authenticated;
399 userauth_kbdint(Authctxt *authctxt)
401 int authenticated = 0;
405 lang = packet_get_string(NULL);
406 devs = packet_get_string(NULL);
409 debug("keyboard-interactive language %s devs %s", lang, devs);
411 if (options.challenge_reponse_authentication)
412 authenticated = auth2_challenge(authctxt, devs);
415 if (authenticated == 0)
416 authenticated = auth2_pam(authctxt);
421 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
424 return authenticated;
428 userauth_pubkey(Authctxt *authctxt)
432 char *pkalg, *pkblob, *sig;
433 u_int alen, blen, slen;
434 int have_sig, pktype;
435 int authenticated = 0;
437 if (!authctxt->valid) {
438 debug2("userauth_pubkey: disabled because of invalid user");
441 have_sig = packet_get_char();
442 if (datafellows & SSH_BUG_PKAUTH) {
443 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
444 /* no explicit pkalg given */
445 pkblob = packet_get_string(&blen);
447 buffer_append(&b, pkblob, blen);
448 /* so we have to extract the pkalg from the pkblob */
449 pkalg = buffer_get_string(&b, &alen);
452 pkalg = packet_get_string(&alen);
453 pkblob = packet_get_string(&blen);
455 pktype = key_type_from_name(pkalg);
456 if (pktype == KEY_UNSPEC) {
457 /* this is perfectly legal */
458 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
463 key = key_from_blob(pkblob, blen);
466 sig = packet_get_string(&slen);
469 if (datafellows & SSH_OLD_SESSIONID) {
470 buffer_append(&b, session_id2, session_id2_len);
472 buffer_put_string(&b, session_id2, session_id2_len);
474 /* reconstruct packet */
475 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
476 buffer_put_cstring(&b, authctxt->user);
477 buffer_put_cstring(&b,
478 datafellows & SSH_BUG_PKSERVICE ?
481 if (datafellows & SSH_BUG_PKAUTH) {
482 buffer_put_char(&b, have_sig);
484 buffer_put_cstring(&b, "publickey");
485 buffer_put_char(&b, have_sig);
486 buffer_put_cstring(&b, key_ssh_name(key));
488 buffer_put_string(&b, pkblob, blen);
492 /* test for correct signature */
493 if (user_key_allowed(authctxt->pw, key) &&
494 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
499 debug("test whether pkalg/pkblob are acceptable");
502 /* XXX fake reply and always send PK_OK ? */
504 * XXX this allows testing whether a user is allowed
505 * to login: if you happen to have a valid pubkey this
506 * message is sent. the message is NEVER sent at all
507 * if a user is not allowed to login. is this an
510 if (user_key_allowed(authctxt->pw, key)) {
511 packet_start(SSH2_MSG_USERAUTH_PK_OK);
512 packet_put_string(pkalg, alen);
513 packet_put_string(pkblob, blen);
519 if (authenticated != 1)
520 auth_clear_options();
523 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
527 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
530 return authenticated;
533 /* get current user */
538 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
544 authmethods_get(void)
546 Authmethod *method = NULL;
550 for (method = authmethods; method->name != NULL; method++) {
551 if (strcmp(method->name, "none") == 0)
553 if (method->enabled != NULL && *(method->enabled) != 0) {
555 size += strlen(DELIM);
556 size += strlen(method->name);
559 size++; /* trailing '\0' */
560 list = xmalloc(size);
563 for (method = authmethods; method->name != NULL; method++) {
564 if (strcmp(method->name, "none") == 0)
566 if (method->enabled != NULL && *(method->enabled) != 0) {
568 strlcat(list, DELIM, size);
569 strlcat(list, method->name, size);
576 authmethod_lookup(const char *name)
578 Authmethod *method = NULL;
580 for (method = authmethods; method->name != NULL; method++)
581 if (method->enabled != NULL &&
582 *(method->enabled) != 0 &&
583 strcmp(name, method->name) == 0)
585 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
589 /* return 1 if user allows given key */
591 user_key_allowed(struct passwd *pw, Key *key)
593 char line[8192], file[MAXPATHLEN];
603 /* Temporarily use the user's uid. */
604 temporarily_use_uid(pw->pw_uid);
606 /* The authorized keys. */
607 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
608 _PATH_SSH_USER_PERMITTED_KEYS2);
610 /* Fail quietly if file does not exist */
611 if (stat(file, &st) < 0) {
612 /* Restore the privileged uid. */
616 /* Open the file containing the authorized keys. */
617 f = fopen(file, "r");
619 /* Restore the privileged uid. */
623 if (options.strict_modes) {
626 /* Check open file in order to avoid open/stat races */
627 if (fstat(fileno(f), &st) < 0 ||
628 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
629 (st.st_mode & 022) != 0) {
630 snprintf(buf, sizeof buf,
631 "%s authentication refused for %.100s: "
632 "bad ownership or modes for '%s'.",
633 key_type(key), pw->pw_name, file);
636 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
638 static const char *check[] = {
639 "", _PATH_SSH_USER_DIR, NULL
641 for (i = 0; check[i]; i++) {
642 snprintf(line, sizeof line, "%.500s/%.100s",
643 pw->pw_dir, check[i]);
644 if (stat(line, &st) < 0 ||
645 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
646 (st.st_mode & 022) != 0) {
647 snprintf(buf, sizeof buf,
648 "%s authentication refused for %.100s: "
649 "bad ownership or modes for '%s'.",
650 key_type(key), pw->pw_name, line);
664 found = key_new(key->type);
666 while (fgets(line, sizeof(line), f)) {
667 char *cp, *options = NULL;
669 /* Skip leading whitespace, empty and comment lines. */
670 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
672 if (!*cp || *cp == '\n' || *cp == '#')
675 if (key_read(found, &cp) == -1) {
676 /* no key? check if there are options for this key */
678 debug2("user_key_allowed: check options: '%s'", cp);
680 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
681 if (*cp == '\\' && cp[1] == '"')
682 cp++; /* Skip both */
686 /* Skip remaining whitespace. */
687 for (; *cp == ' ' || *cp == '\t'; cp++)
689 if (key_read(found, &cp) == -1) {
690 debug2("user_key_allowed: advance: '%s'", cp);
691 /* still no key? advance to next line*/
695 if (key_equal(found, key) &&
696 auth_parse_options(pw, options, file, linenum) == 1) {
698 debug("matching key found: file %s, line %ld",