sshd-auth.c revision 1.2 1 /* $NetBSD: sshd-auth.c,v 1.2 2025/04/09 15:49:33 christos Exp $ */
2 /* $OpenBSD: sshd-auth.c,v 1.3 2025/01/16 06:37:10 dtucker Exp $ */
3
4 /*
5 * SSH2 implementation:
6 * Privilege Separation:
7 *
8 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
9 * Copyright (c) 2002 Niels Provos. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "includes.h"
33 __RCSID("$NetBSD: sshd-auth.c,v 1.2 2025/04/09 15:49:33 christos Exp $");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/wait.h>
39 #include <sys/tree.h>
40 #include <sys/stat.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/queue.h>
44
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <netdb.h>
48 #include <paths.h>
49 #include <pwd.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <stdarg.h>
55 #include <unistd.h>
56 #include <limits.h>
57
58 #ifdef WITH_OPENSSL
59 #include <openssl/bn.h>
60 #include <openssl/evp.h>
61 #endif
62
63 #include "xmalloc.h"
64 #include "ssh.h"
65 #include "ssh2.h"
66 #include "sshpty.h"
67 #include "packet.h"
68 #include "log.h"
69 #include "sshbuf.h"
70 #include "misc.h"
71 #include "match.h"
72 #include "servconf.h"
73 #include "uidswap.h"
74 #include "compat.h"
75 #include "cipher.h"
76 #include "digest.h"
77 #include "sshkey.h"
78 #include "kex.h"
79 #include "authfile.h"
80 #include "pathnames.h"
81 #include "atomicio.h"
82 #include "canohost.h"
83 #include "hostfile.h"
84 #include "auth.h"
85 #include "authfd.h"
86 #include "msg.h"
87 #include "dispatch.h"
88 #include "channels.h"
89 #include "session.h"
90 #include "monitor.h"
91 #ifdef GSSAPI
92 #include "ssh-gss.h"
93 #endif
94 #include "monitor_wrap.h"
95 #include "auth-options.h"
96 #include "version.h"
97 #include "ssherr.h"
98 #include "sk-api.h"
99 #include "srclimit.h"
100 #include "dh.h"
101
102 /* Privsep fds */
103 #define PRIVSEP_MONITOR_FD (STDERR_FILENO + 1)
104 #define PRIVSEP_LOG_FD (STDERR_FILENO + 2)
105 #define PRIVSEP_MIN_FREE_FD (STDERR_FILENO + 3)
106
107 #ifdef LIBWRAP
108 #include <syslog.h>
109 int allow_severity = LOG_INFO;
110 int deny_severity = LOG_WARNING;
111 #endif
112
113 extern char *__progname;
114
115 /* Server configuration options. */
116 ServerOptions options;
117
118 /* Name of the server configuration file. */
119 const char *config_file_name = _PATH_SERVER_CONFIG_FILE;
120
121 /*
122 * Debug mode flag. This can be set on the command line. If debug
123 * mode is enabled, extra debugging output will be sent to the system
124 * log, the daemon will not go to background, and will exit after processing
125 * the first connection.
126 */
127 int debug_flag = 0;
128
129 /* Flag indicating that the daemon is being started from inetd. */
130 static int inetd_flag = 0;
131
132 /* Saved arguments to main(). */
133 static char **saved_argv;
134
135 /* Daemon's agent connection */
136 int auth_sock = -1;
137 static int have_agent = 0;
138
139 u_int num_hostkeys;
140 struct sshkey **host_pubkeys; /* all public host keys */
141 struct sshkey **host_certificates; /* all public host certificates */
142
143 /* record remote hostname or ip */
144 #ifndef HOST_NAME_MAX
145 #define HOST_NAME_MAX MAXHOSTNAMELEN
146 #endif
147 u_int utmp_len = HOST_NAME_MAX+1;
148
149 /* variables used for privilege separation */
150 struct monitor *pmonitor = NULL;
151 int privsep_is_preauth = 1;
152
153 /* global connection state and authentication contexts */
154 Authctxt *the_authctxt = NULL;
155 struct ssh *the_active_state;
156
157 /* global key/cert auth options. XXX move to permanent ssh->authctxt? */
158 struct sshauthopt *auth_opts = NULL;
159
160 /* sshd_config buffer */
161 struct sshbuf *cfg;
162
163 /* Included files from the configuration file */
164 struct include_list includes = TAILQ_HEAD_INITIALIZER(includes);
165
166 /* message to be displayed after login */
167 struct sshbuf *loginmsg;
168
169 /* Prototypes for various functions defined later in this file. */
170 static void do_ssh2_kex(struct ssh *);
171
172 /* XXX stub */
173 int
174 mm_is_monitor(void)
175 {
176 return 0;
177 }
178
179 static void
180 privsep_child_demote(void)
181 {
182 gid_t gidset[1];
183 struct passwd *pw;
184
185 /* Demote the child */
186 if (getuid() == 0 || geteuid() == 0) {
187 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
188 fatal("Privilege separation user %s does not exist",
189 SSH_PRIVSEP_USER);
190 pw = pwcopy(pw); /* Ensure mutable */
191 endpwent();
192 freezero(pw->pw_passwd, strlen(pw->pw_passwd));
193
194 /* Change our root directory */
195 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
196 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
197 strerror(errno));
198 if (chdir("/") == -1)
199 fatal("chdir(\"/\"): %s", strerror(errno));
200
201 /*
202 * Drop our privileges
203 * NB. Can't use setusercontext() after chroot.
204 */
205 debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
206 (u_int)pw->pw_gid);
207 gidset[0] = pw->pw_gid;
208 if (setgroups(1, gidset) == -1)
209 fatal("setgroups: %.100s", strerror(errno));
210 permanently_set_uid(pw);
211 }
212
213 /* sandbox ourselves */
214 #ifdef __OpenBSD__
215 if (pledge("stdio", NULL) == -1)
216 fatal_f("pledge()");
217 #endif
218 }
219
220 static void
221 append_hostkey_type(struct sshbuf *b, const char *s)
222 {
223 int r;
224
225 if (match_pattern_list(s, options.hostkeyalgorithms, 0) != 1) {
226 debug3_f("%s key not permitted by HostkeyAlgorithms", s);
227 return;
228 }
229 if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) > 0 ? "," : "", s)) != 0)
230 fatal_fr(r, "sshbuf_putf");
231 }
232
233 static char *
234 list_hostkey_types(void)
235 {
236 struct sshbuf *b;
237 struct sshkey *key;
238 char *ret;
239 u_int i;
240
241 if ((b = sshbuf_new()) == NULL)
242 fatal_f("sshbuf_new failed");
243 for (i = 0; i < options.num_host_key_files; i++) {
244 key = host_pubkeys[i];
245 if (key == NULL)
246 continue;
247 switch (key->type) {
248 case KEY_RSA:
249 /* for RSA we also support SHA2 signatures */
250 append_hostkey_type(b, "rsa-sha2-512");
251 append_hostkey_type(b, "rsa-sha2-256");
252 /* FALLTHROUGH */
253 case KEY_DSA:
254 case KEY_ECDSA:
255 case KEY_ED25519:
256 case KEY_ECDSA_SK:
257 case KEY_ED25519_SK:
258 case KEY_XMSS:
259 append_hostkey_type(b, sshkey_ssh_name(key));
260 break;
261 }
262 /* If the private key has a cert peer, then list that too */
263 key = host_certificates[i];
264 if (key == NULL)
265 continue;
266 switch (key->type) {
267 case KEY_RSA_CERT:
268 /* for RSA we also support SHA2 signatures */
269 append_hostkey_type(b,
270 "rsa-sha2-512-cert-v01 (at) openssh.com");
271 append_hostkey_type(b,
272 "rsa-sha2-256-cert-v01 (at) openssh.com");
273 /* FALLTHROUGH */
274 case KEY_DSA_CERT:
275 case KEY_ECDSA_CERT:
276 case KEY_ED25519_CERT:
277 case KEY_ECDSA_SK_CERT:
278 case KEY_ED25519_SK_CERT:
279 case KEY_XMSS_CERT:
280 append_hostkey_type(b, sshkey_ssh_name(key));
281 break;
282 }
283 }
284 if ((ret = sshbuf_dup_string(b)) == NULL)
285 fatal_f("sshbuf_dup_string failed");
286 sshbuf_free(b);
287 debug_f("%s", ret);
288 return ret;
289 }
290
291 struct sshkey *
292 get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
293 {
294 u_int i;
295 struct sshkey *key;
296
297 for (i = 0; i < options.num_host_key_files; i++) {
298 switch (type) {
299 case KEY_RSA_CERT:
300 case KEY_DSA_CERT:
301 case KEY_ECDSA_CERT:
302 case KEY_ED25519_CERT:
303 case KEY_ECDSA_SK_CERT:
304 case KEY_ED25519_SK_CERT:
305 case KEY_XMSS_CERT:
306 key = host_certificates[i];
307 break;
308 default:
309 key = host_pubkeys[i];
310 break;
311 }
312 if (key == NULL || key->type != type)
313 continue;
314 switch (type) {
315 case KEY_ECDSA:
316 case KEY_ECDSA_SK:
317 case KEY_ECDSA_CERT:
318 case KEY_ECDSA_SK_CERT:
319 if (key->ecdsa_nid != nid)
320 continue;
321 /* FALLTHROUGH */
322 default:
323 return key;
324 }
325 }
326 return NULL;
327 }
328
329 /* XXX remove */
330 struct sshkey *
331 get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
332 {
333 return NULL;
334 }
335
336 /* XXX remove */
337 struct sshkey *
338 get_hostkey_by_index(int ind)
339 {
340 return NULL;
341 }
342
343 struct sshkey *
344 get_hostkey_public_by_index(int ind, struct ssh *ssh)
345 {
346 if (ind < 0 || (u_int)ind >= options.num_host_key_files)
347 return (NULL);
348 return host_pubkeys[ind];
349 }
350
351 int
352 get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh)
353 {
354 u_int i;
355
356 for (i = 0; i < options.num_host_key_files; i++) {
357 if (sshkey_is_cert(key)) {
358 if (key == host_certificates[i] ||
359 (compare && host_certificates[i] &&
360 sshkey_equal(key, host_certificates[i])))
361 return (i);
362 } else {
363 if (key == host_pubkeys[i] ||
364 (compare && host_pubkeys[i] &&
365 sshkey_equal(key, host_pubkeys[i])))
366 return (i);
367 }
368 }
369 return (-1);
370 }
371
372 static void
373 usage(void)
374 {
375 fprintf(stderr, "%s, %s\n", SSH_VERSION, SSH_OPENSSL_VERSION);
376 fprintf(stderr,
377 "usage: sshd [-46DdeGiqTtV] [-C connection_spec] [-c host_cert_file]\n"
378 " [-E log_file] [-f config_file] [-g login_grace_time]\n"
379 " [-h host_key_file] [-o option] [-p port] [-u len]\n"
380 );
381 exit(1);
382 }
383
384 static void
385 parse_hostkeys(struct sshbuf *hostkeys)
386 {
387 int r;
388 u_int num_keys = 0;
389 struct sshkey *k;
390 const u_char *cp;
391 size_t len;
392
393 while (sshbuf_len(hostkeys) != 0) {
394 if (num_keys > 2048)
395 fatal_f("too many hostkeys");
396 host_pubkeys = xrecallocarray(host_pubkeys,
397 num_keys, num_keys + 1, sizeof(*host_pubkeys));
398 host_certificates = xrecallocarray(host_certificates,
399 num_keys, num_keys + 1, sizeof(*host_certificates));
400 /* public key */
401 k = NULL;
402 if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0)
403 fatal_fr(r, "extract pubkey");
404 if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0)
405 fatal_fr(r, "parse pubkey");
406 host_pubkeys[num_keys] = k;
407 if (k)
408 debug2_f("key %u: %s", num_keys, sshkey_ssh_name(k));
409 /* certificate */
410 k = NULL;
411 if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0)
412 fatal_fr(r, "extract pubkey");
413 if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0)
414 fatal_fr(r, "parse pubkey");
415 host_certificates[num_keys] = k;
416 if (k)
417 debug2_f("cert %u: %s", num_keys, sshkey_ssh_name(k));
418 num_keys++;
419 }
420 num_hostkeys = num_keys;
421 }
422
423 static void
424 recv_privsep_state(struct ssh *ssh, struct sshbuf *conf,
425 uint64_t *timing_secretp)
426 {
427 struct sshbuf *hostkeys;
428
429 debug3_f("begin");
430
431 mm_get_state(ssh, &includes, conf, NULL, timing_secretp,
432 &hostkeys, NULL, NULL, NULL, NULL);
433 parse_hostkeys(hostkeys);
434
435 sshbuf_free(hostkeys);
436
437 debug3_f("done");
438 }
439
440 /*
441 * Main program for the daemon.
442 */
443 int
444 main(int ac, char **av)
445 {
446 struct ssh *ssh = NULL;
447 extern char *optarg;
448 extern int optind;
449 int r, opt, have_key = 0;
450 int sock_in = -1, sock_out = -1, rexeced_flag = 0;
451 char *line, *logfile = NULL;
452 u_int i;
453 mode_t new_umask;
454 Authctxt *authctxt;
455 struct connection_info *connection_info = NULL;
456 sigset_t sigmask;
457 uint64_t timing_secret = 0;
458
459 closefrom(PRIVSEP_MIN_FREE_FD);
460 sigemptyset(&sigmask);
461 sigprocmask(SIG_SETMASK, &sigmask, NULL);
462
463 /* Save argv. */
464 saved_argv = av;
465
466 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
467 sanitise_stdfd();
468
469 /* Initialize configuration options to their default values. */
470 initialize_server_options(&options);
471
472 /* Parse command-line arguments. */
473 while ((opt = getopt(ac, av,
474 "C:E:b:c:f:g:h:k:o:p:u:46DGQRTdeiqrtV")) != -1) {
475 switch (opt) {
476 case '4':
477 options.address_family = AF_INET;
478 break;
479 case '6':
480 options.address_family = AF_INET6;
481 break;
482 case 'f':
483 config_file_name = optarg;
484 break;
485 case 'c':
486 servconf_add_hostcert("[command-line]", 0,
487 &options, optarg);
488 break;
489 case 'd':
490 if (debug_flag == 0) {
491 debug_flag = 1;
492 options.log_level = SYSLOG_LEVEL_DEBUG1;
493 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
494 options.log_level++;
495 break;
496 case 'D':
497 /* ignore */
498 break;
499 case 'E':
500 logfile = optarg;
501 /* FALLTHROUGH */
502 case 'e':
503 /* ignore */
504 break;
505 case 'i':
506 inetd_flag = 1;
507 break;
508 case 'r':
509 /* ignore */
510 break;
511 case 'R':
512 rexeced_flag = 1;
513 break;
514 case 'Q':
515 /* ignored */
516 break;
517 case 'q':
518 options.log_level = SYSLOG_LEVEL_QUIET;
519 break;
520 case 'b':
521 /* protocol 1, ignored */
522 break;
523 case 'p':
524 options.ports_from_cmdline = 1;
525 if (options.num_ports >= MAX_PORTS) {
526 fprintf(stderr, "too many ports.\n");
527 exit(1);
528 }
529 options.ports[options.num_ports++] = a2port(optarg);
530 if (options.ports[options.num_ports-1] <= 0) {
531 fprintf(stderr, "Bad port number.\n");
532 exit(1);
533 }
534 break;
535 case 'g':
536 if ((options.login_grace_time = convtime(optarg)) == -1) {
537 fprintf(stderr, "Invalid login grace time.\n");
538 exit(1);
539 }
540 break;
541 case 'k':
542 /* protocol 1, ignored */
543 break;
544 case 'h':
545 servconf_add_hostkey("[command-line]", 0,
546 &options, optarg, 1);
547 break;
548 case 't':
549 case 'T':
550 case 'G':
551 fatal("test/dump modes not supported");
552 break;
553 case 'C':
554 connection_info = server_get_connection_info(ssh, 0, 0);
555 if (parse_server_match_testspec(connection_info,
556 optarg) == -1)
557 exit(1);
558 break;
559 case 'u':
560 utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
561 if (utmp_len > HOST_NAME_MAX+1) {
562 fprintf(stderr, "Invalid utmp length.\n");
563 exit(1);
564 }
565 break;
566 case 'o':
567 line = xstrdup(optarg);
568 if (process_server_config_line(&options, line,
569 "command-line", 0, NULL, NULL, &includes) != 0)
570 exit(1);
571 free(line);
572 break;
573 case 'V':
574 fprintf(stderr, "%s, %s\n",
575 SSH_VERSION, SSH_OPENSSL_VERSION);
576 exit(0);
577 default:
578 usage();
579 break;
580 }
581 }
582
583 if (!rexeced_flag)
584 fatal("sshd-auth should not be executed directly");
585
586 #ifdef WITH_OPENSSL
587 OpenSSL_add_all_algorithms();
588 #endif
589
590 /* If requested, redirect the logs to the specified logfile. */
591 if (logfile != NULL) {
592 char *cp, pid_s[32];
593
594 snprintf(pid_s, sizeof(pid_s), "%ld", (unsigned long)getpid());
595 cp = percent_expand(logfile,
596 "p", pid_s,
597 "P", "sshd-auth",
598 (char *)NULL);
599 log_redirect_stderr_to(cp);
600 free(cp);
601 }
602
603 log_init(__progname,
604 options.log_level == SYSLOG_LEVEL_NOT_SET ?
605 SYSLOG_LEVEL_INFO : options.log_level,
606 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
607 SYSLOG_FACILITY_AUTH : options.log_facility, 1);
608
609 /* XXX can't use monitor_init(); it makes fds */
610 pmonitor = xcalloc(1, sizeof(*pmonitor));
611 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
612 pmonitor->m_recvfd = PRIVSEP_MONITOR_FD;
613 pmonitor->m_log_sendfd = PRIVSEP_LOG_FD;
614 set_log_handler(mm_log_handler, pmonitor);
615
616 /* Check that there are no remaining arguments. */
617 if (optind < ac) {
618 fprintf(stderr, "Extra argument %s.\n", av[optind]);
619 exit(1);
620 }
621
622 /* Connection passed by stdin/out */
623 if (inetd_flag) {
624 /*
625 * NB. must be different fd numbers for the !socket case,
626 * as packet_connection_is_on_socket() depends on this.
627 */
628 sock_in = dup(STDIN_FILENO);
629 sock_out = dup(STDOUT_FILENO);
630 } else {
631 /* rexec case; accept()ed socket in ancestor listener */
632 sock_in = sock_out = dup(STDIN_FILENO);
633 }
634
635 if (stdfd_devnull(1, 1, 0) == -1)
636 error("stdfd_devnull failed");
637 debug("network sockets: %d, %d", sock_in, sock_out);
638
639 /*
640 * Register our connection. This turns encryption off because we do
641 * not have a key.
642 */
643 if ((ssh = ssh_packet_set_connection(NULL, sock_in, sock_out)) == NULL)
644 fatal("Unable to create connection");
645 the_active_state = ssh;
646 ssh_packet_set_server(ssh);
647 pmonitor->m_pkex = &ssh->kex;
648
649 /* Fetch our configuration */
650 if ((cfg = sshbuf_new()) == NULL)
651 fatal("sshbuf_new config buf failed");
652 setproctitle("%s", "[session-auth early]");
653 recv_privsep_state(ssh, cfg, &timing_secret);
654 parse_server_config(&options, "rexec", cfg, &includes, NULL, 1);
655 /* Fill in default values for those options not explicitly set. */
656 fill_default_server_options(&options);
657 options.timing_secret = timing_secret; /* XXX eliminate from unpriv */
658
659 /* Reinit logging in case config set Level, Facility or Verbose. */
660 log_init(__progname, options.log_level, options.log_facility, 1);
661
662 debug("sshd-auth version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
663
664 #ifdef WITH_OPENSSL
665 if (options.moduli_file != NULL)
666 dh_set_moduli_file(options.moduli_file);
667 #endif
668
669 if (options.host_key_agent) {
670 if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
671 setenv(SSH_AUTHSOCKET_ENV_NAME,
672 options.host_key_agent, 1);
673 if ((r = ssh_get_authentication_socket(NULL)) == 0)
674 have_agent = 1;
675 else
676 error_r(r, "Could not connect to agent \"%s\"",
677 options.host_key_agent);
678 }
679
680 if (options.num_host_key_files != num_hostkeys) {
681 fatal("internal error: hostkeys confused (config %u recvd %u)",
682 options.num_host_key_files, num_hostkeys);
683 }
684
685 for (i = 0; i < options.num_host_key_files; i++) {
686 if (host_pubkeys[i] != NULL) {
687 have_key = 1;
688 break;
689 }
690 }
691 if (!have_key)
692 fatal("internal error: received no hostkeys");
693
694 /* Ensure that umask disallows at least group and world write */
695 new_umask = umask(0077) | 0022;
696 (void) umask(new_umask);
697
698 /* Initialize the log (it is reinitialized below in case we forked). */
699 log_init(__progname, options.log_level, options.log_facility, 1);
700 set_log_handler(mm_log_handler, pmonitor);
701 for (i = 0; i < options.num_log_verbose; i++)
702 log_verbose_add(options.log_verbose[i]);
703
704 /*
705 * Chdir to the root directory so that the current disk can be
706 * unmounted if desired.
707 */
708 if (chdir("/") == -1)
709 error("chdir(\"/\"): %s", strerror(errno));
710
711 /* This is the child authenticating a new connection. */
712 setproctitle("%s", "[session-auth]");
713
714 /* Executed child processes don't need these. */
715 fcntl(sock_out, F_SETFD, FD_CLOEXEC);
716 fcntl(sock_in, F_SETFD, FD_CLOEXEC);
717
718 ssh_signal(SIGPIPE, SIG_IGN);
719 ssh_signal(SIGALRM, SIG_DFL);
720 ssh_signal(SIGHUP, SIG_DFL);
721 ssh_signal(SIGTERM, SIG_DFL);
722 ssh_signal(SIGQUIT, SIG_DFL);
723 ssh_signal(SIGCHLD, SIG_DFL);
724
725 /* Prepare the channels layer */
726 channel_init_channels(ssh);
727 channel_set_af(ssh, options.address_family);
728 server_process_channel_timeouts(ssh);
729 server_process_permitopen(ssh);
730
731 ssh_packet_set_nonblocking(ssh);
732
733 /* allocate authentication context */
734 authctxt = xcalloc(1, sizeof(*authctxt));
735 ssh->authctxt = authctxt;
736
737 /* XXX global for cleanup, access from other modules */
738 the_authctxt = authctxt;
739
740 /* Set default key authentication options */
741 if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL)
742 fatal("allocation failed");
743
744 /* prepare buffer to collect messages to display to user after login */
745 if ((loginmsg = sshbuf_new()) == NULL)
746 fatal("sshbuf_new loginmsg failed");
747 auth_debug_reset();
748
749 /* Enable challenge-response authentication for privilege separation */
750 privsep_challenge_enable();
751
752 #ifdef GSSAPI
753 /* Cache supported mechanism OIDs for later use */
754 ssh_gssapi_prepare_supported_oids();
755 #endif
756
757 privsep_child_demote();
758
759 /* perform the key exchange */
760 /* authenticate user and start session */
761 do_ssh2_kex(ssh);
762 do_authentication2(ssh);
763
764 /*
765 * The unprivileged child now transfers the current keystate and exits.
766 */
767 mm_send_keystate(ssh, pmonitor);
768 ssh_packet_clear_keys(ssh);
769 exit(0);
770 }
771
772 int
773 sshd_hostkey_sign(struct ssh *ssh, struct sshkey *privkey,
774 struct sshkey *pubkey, u_char **signature, size_t *slenp,
775 const u_char *data, size_t dlen, const char *alg)
776 {
777 if (privkey) {
778 if (mm_sshkey_sign(ssh, privkey, signature, slenp,
779 data, dlen, alg, options.sk_provider, NULL,
780 ssh->compat) < 0)
781 fatal_f("privkey sign failed");
782 } else {
783 if (mm_sshkey_sign(ssh, pubkey, signature, slenp,
784 data, dlen, alg, options.sk_provider, NULL,
785 ssh->compat) < 0)
786 fatal_f("pubkey sign failed");
787 }
788 return 0;
789 }
790
791 /* SSH2 key exchange */
792 static void
793 do_ssh2_kex(struct ssh *ssh)
794 {
795 char *hkalgs = NULL, *myproposal[PROPOSAL_MAX];
796 const char *compression = NULL;
797 struct kex *kex;
798 int r;
799
800 if (options.rekey_limit || options.rekey_interval)
801 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
802 options.rekey_interval);
803
804 if (options.compression == COMP_NONE)
805 compression = "none";
806 hkalgs = list_hostkey_types();
807
808 kex_proposal_populate_entries(ssh, myproposal, options.kex_algorithms,
809 options.ciphers, options.macs, compression, hkalgs);
810
811 free(hkalgs);
812
813 /* start key exchange */
814 if ((r = kex_setup(ssh, myproposal)) != 0)
815 fatal_r(r, "kex_setup");
816 kex_set_server_sig_algs(ssh, options.pubkey_accepted_algos);
817 kex = ssh->kex;
818
819 #ifdef WITH_OPENSSL
820 kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
821 kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
822 kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
823 kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
824 kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
825 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
826 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
827 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
828 #endif
829 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
830 kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
831 kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_server;
832 kex->load_host_public_key=&get_hostkey_public_by_type;
833 kex->load_host_private_key=&get_hostkey_private_by_type;
834 kex->host_key_index=&get_hostkey_index;
835 kex->sign = sshd_hostkey_sign;
836
837 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &kex->done);
838 kex_proposal_free_entries(myproposal);
839
840 #ifdef DEBUG_KEXDH
841 /* send 1st encrypted/maced/compressed message */
842 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
843 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
844 (r = sshpkt_send(ssh)) != 0 ||
845 (r = ssh_packet_write_wait(ssh)) != 0)
846 fatal_fr(r, "send test");
847 #endif
848 debug("KEX done");
849 }
850
851 /* server specific fatal cleanup */
852 void
853 cleanup_exit(int i)
854 {
855 _exit(i);
856 }
857