auth2.c revision 1.37 1 /* $NetBSD: auth2.c,v 1.37 2026/10/07 17:32:07 christos Exp $ */
2 /* $OpenBSD: auth2.c,v 1.176 2026/09/16 00:37:07 djm Exp $ */
3
4 /*
5 * Copyright (c) 2000 Markus Friedl. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "includes.h"
29 __RCSID("$NetBSD: auth2.c,v 1.37 2026/10/07 17:32:07 christos Exp $");
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/uio.h>
34
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <pwd.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <time.h>
42
43 #include "stdlib.h"
44 #include "atomicio.h"
45 #include "xmalloc.h"
46 #include "ssh2.h"
47 #include "packet.h"
48 #include "log.h"
49 #include "sshbuf.h"
50 #include "misc.h"
51 #include "servconf.h"
52 #include "sshkey.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "dispatch.h"
56 #include "pathnames.h"
57
58 #include "ssherr.h"
59 #ifdef GSSAPI
60 #include "ssh-gss.h"
61 #endif
62
63 #include "monitor_wrap.h"
64 #include "digest.h"
65 #include "kex.h"
66
67 /* import */
68 extern ServerOptions options;
69 extern struct sshbuf *loginmsg;
70
71 /* methods */
72
73 extern Authmethod method_none;
74 extern Authmethod method_pubkey;
75 extern Authmethod method_passwd;
76 extern Authmethod method_kbdint;
77 extern Authmethod method_hostbased;
78 #ifdef KRB5
79 extern Authmethod method_kerberos;
80 #endif
81 #ifdef GSSAPI
82 extern Authmethod method_gssapi;
83 #endif
84
85 static int log_flag = 0;
86
87 Authmethod *authmethods[] = {
88 &method_none,
89 &method_pubkey,
90 #ifdef GSSAPI
91 &method_gssapi,
92 #endif
93 &method_passwd,
94 &method_kbdint,
95 &method_hostbased,
96 #ifdef KRB5
97 &method_kerberos,
98 #endif
99 NULL
100 };
101
102 /* protocol */
103
104 static int input_service_request(int, uint32_t, struct ssh *);
105 static int input_userauth_request(int, uint32_t, struct ssh *);
106
107 /* helper */
108 static Authmethod *authmethod_byname(const char *);
109 static Authmethod *authmethod_lookup(Authctxt *, const char *);
110 static char *authmethods_get(Authctxt *authctxt);
111
112 #define MATCH_NONE 0 /* method or submethod mismatch */
113 #define MATCH_METHOD 1 /* method matches (no submethod specified) */
114 #define MATCH_BOTH 2 /* method and submethod match */
115 #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
116 static int list_starts_with(const char *, const char *, const char *);
117
118 char *
119 auth2_read_banner(void)
120 {
121 struct stat st;
122 char *banner = NULL;
123 size_t len, n;
124 int fd;
125
126 if ((fd = open(options.banner, O_RDONLY)) == -1)
127 return (NULL);
128 if (fstat(fd, &st) == -1) {
129 close(fd);
130 return (NULL);
131 }
132 if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
133 close(fd);
134 return (NULL);
135 }
136
137 len = (size_t)st.st_size; /* truncate */
138 banner = xmalloc(len + 1);
139 n = atomicio(read, fd, banner, len);
140 close(fd);
141
142 if (n != len) {
143 free(banner);
144 return (NULL);
145 }
146 banner[n] = '\0';
147
148 return (banner);
149 }
150
151 static void
152 userauth_send_banner(struct ssh *ssh, const char *msg)
153 {
154 int r;
155
156 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
157 (r = sshpkt_put_cstring(ssh, msg)) != 0 ||
158 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language, unused */
159 (r = sshpkt_send(ssh)) != 0)
160 fatal_fr(r, "send packet");
161 debug("%s: sent", __func__);
162 }
163
164 static void
165 userauth_banner(struct ssh *ssh)
166 {
167 char *banner = NULL;
168
169 if (options.banner == NULL)
170 return;
171
172 if ((banner = mm_auth2_read_banner()) == NULL)
173 goto done;
174 userauth_send_banner(ssh, banner);
175
176 done:
177 free(banner);
178 }
179
180 /*
181 * loop until authctxt->success == TRUE
182 */
183 void
184 do_authentication2(struct ssh *ssh)
185 {
186 Authctxt *authctxt = ssh->authctxt;
187
188 ssh_dispatch_init(ssh, &dispatch_protocol_error);
189 if (ssh->kex->ext_info_c)
190 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_input_ext_info);
191 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
192 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
193 ssh->authctxt = NULL;
194 }
195
196 static int
197 input_service_request(int type, uint32_t seq, struct ssh *ssh)
198 {
199 Authctxt *authctxt = ssh->authctxt;
200 char *service = NULL;
201 int r, acceptit = 0;
202
203 if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
204 (r = sshpkt_get_end(ssh)) != 0)
205 goto out;
206
207 if (authctxt == NULL)
208 fatal("input_service_request: no authctxt");
209
210 if (strcmp(service, "ssh-userauth") == 0) {
211 if (!authctxt->success) {
212 acceptit = 1;
213 /* now we can handle user-auth requests */
214 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
215 &input_userauth_request);
216 }
217 }
218 /* XXX all other service requests are denied */
219
220 if (acceptit) {
221 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
222 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
223 (r = sshpkt_send(ssh)) != 0 ||
224 (r = ssh_packet_write_wait(ssh)) < 0)
225 goto out;
226 } else {
227 debug("bad service request %s", service);
228 ssh_packet_disconnect(ssh, "bad service request %s", service);
229 }
230 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &dispatch_protocol_error);
231 r = 0;
232 out:
233 free(service);
234 return r;
235 }
236
237 #define MIN_FAIL_DELAY_SECONDS 0.005
238 #define MAX_FAIL_DELAY_SECONDS 5.0
239 static double
240 user_specific_delay(const char *user)
241 {
242 char b[512];
243 size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
244 u_char *hash = xmalloc(len);
245 double delay;
246
247 (void)snprintf(b, sizeof b, "%llu%s",
248 (unsigned long long)options.timing_secret, user);
249 if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
250 fatal_f("ssh_digest_memory");
251 /* 0-4.2 ms of delay */
252 delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
253 freezero(hash, len);
254 debug3_f("user specific delay %0.3lfms", delay*1000);
255 return MIN_FAIL_DELAY_SECONDS + delay;
256 }
257
258 static void
259 ensure_minimum_time_since(double start, double seconds)
260 {
261 struct timespec ts;
262 double elapsed = monotime_double() - start, req = seconds, remain;
263
264 if (elapsed > MAX_FAIL_DELAY_SECONDS) {
265 debug3_f("elapsed %0.3lfms exceeded the max delay "
266 "requested %0.3lfms)", elapsed*1000, req*1000);
267 return;
268 }
269
270 /* if we've already passed the requested time, scale up */
271 while ((remain = seconds - elapsed) < 0.0)
272 seconds *= 2;
273
274 ts.tv_sec = remain;
275 ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
276 debug3_f("elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
277 elapsed*1000, remain*1000, req*1000);
278 nanosleep(&ts, NULL);
279 }
280
281 void
282 auth_failure_delay(Authctxt *authctxt, double tstart)
283 {
284 ensure_minimum_time_since(tstart, user_specific_delay(authctxt->user));
285 }
286
287 static int
288 input_userauth_request(int type, uint32_t seq, struct ssh *ssh)
289 {
290 Authctxt *authctxt = ssh->authctxt;
291 Authmethod *m = NULL;
292 char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
293 int r, authenticated = 0;
294 double tstart = monotime_double();
295
296 if (authctxt == NULL)
297 fatal("input_userauth_request: no authctxt");
298
299 if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
300 (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
301 (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
302 goto out;
303 debug("userauth-request for user %s service %s method %s", user, service, method);
304 if (!log_flag) {
305 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
306 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), user);
307 log_flag = 1;
308 }
309 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
310
311 if ((style = strchr(user, ':')) != NULL)
312 *style++ = 0;
313
314 if (authctxt->attempt >= 1024)
315 auth_maxtries_exceeded(ssh);
316 if (authctxt->attempt++ == 0) {
317 /* setup auth context */
318 authctxt->pw = mm_getpwnamallow(ssh, user);
319 authctxt->user = xstrdup(user);
320 authctxt->service = xstrdup(service);
321 authctxt->style = style ? xstrdup(style) : NULL;
322 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
323 authctxt->valid = 1;
324 debug2_f("setting up authctxt for %s", user);
325 } else {
326 authctxt->valid = 0;
327 /* Invalid user, fake password information */
328 authctxt->pw = fakepw();
329 }
330 #ifdef USE_PAM
331 if (options.use_pam)
332 mm_start_pam(ssh);
333 #endif
334 ssh_packet_set_log_preamble(ssh, "%suser %s",
335 authctxt->valid ? "authenticating " : "invalid ", user);
336 setproctitle("%s [net]", authctxt->valid ? user : "unknown");
337 mm_inform_authserv(service, style);
338 userauth_banner(ssh);
339 if ((r = kex_server_update_ext_info(ssh)) != 0)
340 fatal_fr(r, "kex_server_update_ext_info failed");
341 if (auth2_setup_methods_lists(authctxt) != 0)
342 ssh_packet_disconnect(ssh,
343 "no authentication methods enabled");
344 } else if (strcmp(user, authctxt->user) != 0 ||
345 strcmp(service, authctxt->service) != 0) {
346 ssh_packet_disconnect(ssh, "Change of username or service "
347 "not allowed: (%s,%s) -> (%s,%s)",
348 authctxt->user, authctxt->service, user, service);
349 }
350 /* reset state */
351 auth2_challenge_stop(ssh);
352
353 #ifdef GSSAPI
354 ssh_gssapi_cleanup_global_client();
355 /* XXX move to auth2_gssapi_stop() */
356 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
357 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
358 #endif
359
360 auth2_authctxt_reset_info(authctxt);
361 authctxt->postponed = 0;
362 authctxt->server_caused_failure = 0;
363 authctxt->auth_failure_already_counted = 0;
364
365 /* try to authenticate user */
366 m = authmethod_lookup(authctxt, method);
367 if (m != NULL && authctxt->failures < options.max_authtries) {
368 debug2("input_userauth_request: try method %s", method);
369 authenticated = m->userauth(ssh, method);
370 }
371 if (!authctxt->authenticated && strcmp(method, "none") != 0)
372 auth_failure_delay(authctxt, tstart);
373
374 userauth_finish(ssh, authenticated, method, NULL);
375 r = 0;
376 out:
377 free(service);
378 free(user);
379 free(method);
380 return r;
381 }
382
383 void
384 userauth_finish(struct ssh *ssh, int authenticated, const char *packet_method,
385 const char *submethod)
386 {
387 Authctxt *authctxt = ssh->authctxt;
388 Authmethod *m = NULL;
389 const char *method = packet_method;
390 char *methods;
391 int r, partial = 0;
392
393 if (authenticated) {
394 if (!authctxt->valid) {
395 fatal("INTERNAL ERROR: authenticated invalid user %s",
396 authctxt->user);
397 }
398 if (authctxt->postponed)
399 fatal("INTERNAL ERROR: authenticated and postponed");
400 /* prefer primary authmethod name to possible synonym */
401 if ((m = authmethod_byname(method)) == NULL)
402 fatal("INTERNAL ERROR: bad method %s", method);
403 method = m->cfg->name;
404 }
405
406 /* Special handling for root */
407 if (authenticated && authctxt->pw->pw_uid == 0 &&
408 !auth_root_allowed(ssh, method)) {
409 authenticated = 0;
410 #ifdef SSH_AUDIT_EVENTS
411 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
412 #endif
413 }
414
415 #ifdef USE_PAM
416 if (options.use_pam && authenticated) {
417 int success = mm_do_pam_account();
418
419 /* if PAM returned a message, send it to the user */
420 if (sshbuf_len(loginmsg) > 0) {
421 if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0)
422 fatal("%s: buffer error: %s",
423 __func__, ssh_err(r));
424 userauth_send_banner(ssh,
425 (const char *)sshbuf_ptr(loginmsg));
426 if ((r = ssh_packet_write_wait(ssh)) < 0) {
427 sshpkt_fatal(ssh, r,
428 "%s: send PAM banner", __func__);
429 }
430 }
431 if (!success) {
432 fatal("Access denied for user %s by PAM account "
433 "configuration", authctxt->user);
434 }
435 }
436 #endif
437
438 if (authenticated && options.num_auth_methods != 0) {
439 if (!auth2_update_methods_lists(authctxt, method, submethod)) {
440 authenticated = 0;
441 partial = 1;
442 }
443 }
444
445 /* Log before sending the reply */
446 auth_log(ssh, authenticated, partial, method, submethod);
447
448 /* Update information exposed to session */
449 if (authenticated || partial)
450 auth2_update_session_info(authctxt, method, submethod);
451
452 if (authctxt->postponed)
453 return;
454
455 if (authenticated == 1) {
456 /* turn off userauth */
457 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
458 &dispatch_protocol_ignore);
459 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
460 (r = sshpkt_send(ssh)) != 0 ||
461 (r = ssh_packet_write_wait(ssh)) < 0)
462 fatal_fr(r, "send success packet");
463 /* now we can break out */
464 authctxt->success = 1;
465 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
466 } else {
467 /* Allow initial try of "none" auth without failure penalty */
468 if (!partial && !authctxt->server_caused_failure &&
469 !authctxt->auth_failure_already_counted &&
470 (authctxt->attempt > 1 || strcmp(method, "none") != 0))
471 authctxt->failures++;
472 if (authctxt->failures >= options.max_authtries)
473 auth_maxtries_exceeded(ssh);
474 methods = authmethods_get(authctxt);
475 debug3_f("failure partial=%d next methods=\"%s\"",
476 partial, methods);
477 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
478 (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
479 (r = sshpkt_put_u8(ssh, partial)) != 0 ||
480 (r = sshpkt_send(ssh)) != 0 ||
481 (r = ssh_packet_write_wait(ssh)) < 0)
482 fatal_fr(r, "send failure packet");
483 free(methods);
484 }
485 }
486
487 /*
488 * Checks whether method is allowed by at least one AuthenticationMethods
489 * methods list. Returns 1 if allowed, or no methods lists configured.
490 * 0 otherwise.
491 */
492 int
493 auth2_method_allowed(Authctxt *authctxt, const char *method,
494 const char *submethod)
495 {
496 u_int i;
497
498 /*
499 * NB. authctxt->num_auth_methods might be zero as a result of
500 * auth2_setup_methods_lists(), so check the configuration.
501 */
502 if (options.num_auth_methods == 0)
503 return 1;
504 for (i = 0; i < authctxt->num_auth_methods; i++) {
505 if (list_starts_with(authctxt->auth_methods[i], method,
506 submethod) != MATCH_NONE)
507 return 1;
508 }
509 return 0;
510 }
511
512 static char *
513 authmethods_get(Authctxt *authctxt)
514 {
515 struct sshbuf *b;
516 char *list;
517 int i, r;
518
519 if ((b = sshbuf_new()) == NULL)
520 fatal_f("sshbuf_new failed");
521 for (i = 0; authmethods[i] != NULL; i++) {
522 if (strcmp(authmethods[i]->cfg->name, "none") == 0)
523 continue;
524 if (authmethods[i]->cfg->enabled == NULL ||
525 *(authmethods[i]->cfg->enabled) == 0)
526 continue;
527 if (!auth2_method_allowed(authctxt, authmethods[i]->cfg->name,
528 NULL))
529 continue;
530 if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
531 authmethods[i]->cfg->name)) != 0)
532 fatal_fr(r, "buffer error");
533 }
534 if ((list = sshbuf_dup_string(b)) == NULL)
535 fatal_f("sshbuf_dup_string failed");
536 sshbuf_free(b);
537 return list;
538 }
539
540 static Authmethod *
541 authmethod_byname(const char *name)
542 {
543 int i;
544
545 if (name == NULL)
546 fatal_f("NULL authentication method name");
547 for (i = 0; authmethods[i] != NULL; i++) {
548 if (strcmp(name, authmethods[i]->cfg->name) == 0 ||
549 (authmethods[i]->cfg->synonym != NULL &&
550 strcmp(name, authmethods[i]->cfg->synonym) == 0))
551 return authmethods[i];
552 }
553 debug_f("unrecognized authentication method name: %s", name);
554 return NULL;
555 }
556
557 static Authmethod *
558 authmethod_lookup(Authctxt *authctxt, const char *name)
559 {
560 Authmethod *method;
561
562 if ((method = authmethod_byname(name)) == NULL)
563 return NULL;
564
565 if (method->cfg->enabled == NULL || *(method->cfg->enabled) == 0) {
566 debug3_f("method %s not enabled", name);
567 return NULL;
568 }
569 if (!auth2_method_allowed(authctxt, method->cfg->name, NULL)) {
570 debug3_f("method %s not allowed "
571 "by AuthenticationMethods", name);
572 return NULL;
573 }
574 return method;
575 }
576
577 /*
578 * Prune the AuthenticationMethods supplied in the configuration, removing
579 * any methods lists that include disabled methods. Note that this might
580 * leave authctxt->num_auth_methods == 0, even when multiple required auth
581 * has been requested. For this reason, all tests for whether multiple is
582 * enabled should consult options.num_auth_methods directly.
583 */
584 int
585 auth2_setup_methods_lists(Authctxt *authctxt)
586 {
587 u_int i;
588
589 /* First, normalise away the "any" pseudo-method */
590 if (options.num_auth_methods == 1 &&
591 strcmp(options.auth_methods[0], "any") == 0) {
592 free(options.auth_methods[0]);
593 options.auth_methods[0] = NULL;
594 options.num_auth_methods = 0;
595 }
596
597 if (options.num_auth_methods == 0)
598 return 0;
599 debug3_f("checking methods");
600 authctxt->auth_methods = xcalloc(options.num_auth_methods,
601 sizeof(*authctxt->auth_methods));
602 authctxt->num_auth_methods = 0;
603 for (i = 0; i < options.num_auth_methods; i++) {
604 if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
605 logit("Authentication methods list \"%s\" contains "
606 "disabled method, skipping",
607 options.auth_methods[i]);
608 continue;
609 }
610 debug("authentication methods list %d: %s",
611 authctxt->num_auth_methods, options.auth_methods[i]);
612 authctxt->auth_methods[authctxt->num_auth_methods++] =
613 xstrdup(options.auth_methods[i]);
614 }
615 if (authctxt->num_auth_methods == 0) {
616 error("No AuthenticationMethods left after eliminating "
617 "disabled methods");
618 return -1;
619 }
620 return 0;
621 }
622
623 static int
624 list_starts_with(const char *methods, const char *method,
625 const char *submethod)
626 {
627 size_t l = strlen(method);
628 int match;
629 const char *p;
630
631 if (strncmp(methods, method, l) != 0)
632 return MATCH_NONE;
633 p = methods + l;
634 match = MATCH_METHOD;
635 if (*p == ':') {
636 if (!submethod)
637 return MATCH_PARTIAL;
638 l = strlen(submethod);
639 p += 1;
640 if (strncmp(submethod, p, l))
641 return MATCH_NONE;
642 p += l;
643 match = MATCH_BOTH;
644 }
645 if (*p != ',' && *p != '\0')
646 return MATCH_NONE;
647 return match;
648 }
649
650 /*
651 * Remove method from the start of a comma-separated list of methods.
652 * Returns 0 if the list of methods did not start with that method or 1
653 * if it did.
654 */
655 static int
656 remove_method(char **methods, const char *method, const char *submethod)
657 {
658 char *omethods = *methods, *p;
659 size_t l = strlen(method);
660 int match;
661
662 match = list_starts_with(omethods, method, submethod);
663 if (match != MATCH_METHOD && match != MATCH_BOTH)
664 return 0;
665 p = omethods + l;
666 if (submethod && match == MATCH_BOTH)
667 p += 1 + strlen(submethod); /* include colon */
668 if (*p == ',')
669 p++;
670 *methods = xstrdup(p);
671 free(omethods);
672 return 1;
673 }
674
675 /*
676 * Called after successful authentication. Will remove the successful method
677 * from the start of each list in which it occurs. If it was the last method
678 * in any list, then authentication is deemed successful.
679 * Returns 1 if the method completed any authentication list or 0 otherwise.
680 */
681 int
682 auth2_update_methods_lists(Authctxt *authctxt, const char *method,
683 const char *submethod)
684 {
685 u_int i, found = 0;
686
687 debug3_f("updating methods list after \"%s\"", method);
688 for (i = 0; i < authctxt->num_auth_methods; i++) {
689 if (!remove_method(&(authctxt->auth_methods[i]), method,
690 submethod))
691 continue;
692 found = 1;
693 if (*authctxt->auth_methods[i] == '\0') {
694 debug2("authentication methods list %d complete", i);
695 return 1;
696 }
697 debug3("authentication methods list %d remaining: \"%s\"",
698 i, authctxt->auth_methods[i]);
699 }
700 /* This should not happen, but would be bad if it did */
701 if (!found)
702 fatal_f("method not in AuthenticationMethods");
703 return 0;
704 }
705
706 /* Reset method-specific information */
707 void auth2_authctxt_reset_info(Authctxt *authctxt)
708 {
709 sshkey_free(authctxt->auth_method_key);
710 free(authctxt->auth_method_info);
711 authctxt->auth_method_key = NULL;
712 authctxt->auth_method_info = NULL;
713 }
714
715 /* Record auth method-specific information for logs */
716 void
717 auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
718 {
719 va_list ap;
720 int i;
721
722 free(authctxt->auth_method_info);
723 authctxt->auth_method_info = NULL;
724
725 va_start(ap, fmt);
726 i = vasprintf(&authctxt->auth_method_info, fmt, ap);
727 va_end(ap);
728
729 if (i == -1)
730 fatal_f("vasprintf failed");
731 }
732
733 /*
734 * Records a public key used in authentication. This is used for logging
735 * and to ensure that the same key is not subsequently accepted again for
736 * multiple authentication.
737 */
738 void
739 auth2_record_key(Authctxt *authctxt, int authenticated,
740 const struct sshkey *key)
741 {
742 struct sshkey **tmp, *dup;
743 int r;
744
745 if ((r = sshkey_from_private(key, &dup)) != 0)
746 fatal_fr(r, "copy key");
747 sshkey_free(authctxt->auth_method_key);
748 authctxt->auth_method_key = dup;
749
750 if (!authenticated)
751 return;
752
753 /* If authenticated, make sure we don't accept this key again */
754 if ((r = sshkey_from_private(key, &dup)) != 0)
755 fatal_fr(r, "copy key");
756 if (authctxt->nprev_keys >= INT_MAX ||
757 (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
758 authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
759 fatal_f("reallocarray failed");
760 authctxt->prev_keys = tmp;
761 authctxt->prev_keys[authctxt->nprev_keys] = dup;
762 authctxt->nprev_keys++;
763
764 }
765
766 /* Checks whether a key has already been previously used for authentication */
767 int
768 auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
769 {
770 u_int i;
771 char *fp;
772
773 for (i = 0; i < authctxt->nprev_keys; i++) {
774 if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
775 fp = sshkey_fingerprint(authctxt->prev_keys[i],
776 options.fingerprint_hash, SSH_FP_DEFAULT);
777 debug3_f("key already used: %s %s",
778 sshkey_type(authctxt->prev_keys[i]),
779 fp == NULL ? "UNKNOWN" : fp);
780 free(fp);
781 return 1;
782 }
783 }
784 return 0;
785 }
786
787 /*
788 * Updates authctxt->session_info with details of authentication. Should be
789 * whenever an authentication method succeeds.
790 */
791 void
792 auth2_update_session_info(Authctxt *authctxt, const char *method,
793 const char *submethod)
794 {
795 int r;
796
797 if (authctxt->session_info == NULL) {
798 if ((authctxt->session_info = sshbuf_new()) == NULL)
799 fatal_f("sshbuf_new");
800 }
801
802 /* Append method[/submethod] */
803 if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
804 method, submethod == NULL ? "" : "/",
805 submethod == NULL ? "" : submethod)) != 0)
806 fatal_fr(r, "append method");
807
808 /* Append key if present */
809 if (authctxt->auth_method_key != NULL) {
810 if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
811 (r = sshkey_format_text(authctxt->auth_method_key,
812 authctxt->session_info)) != 0)
813 fatal_fr(r, "append key");
814 }
815
816 if (authctxt->auth_method_info != NULL) {
817 /* Ensure no ambiguity here */
818 if (strchr(authctxt->auth_method_info, '\n') != NULL)
819 fatal_f("auth_method_info contains \\n");
820 if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
821 (r = sshbuf_putf(authctxt->session_info, "%s",
822 authctxt->auth_method_info)) != 0) {
823 fatal_fr(r, "append method info");
824 }
825 }
826 if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
827 fatal_fr(r, "append");
828 }
829
830