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