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