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