sshd-session.c revision 1.13 1 /* $NetBSD: sshd-session.c,v 1.13 2026/05/16 15:08:30 christos Exp $ */
2 /* $OpenBSD: sshd-session.c,v 1.23 2026/03/11 09:10:59 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-session.c,v 1.13 2026/05/16 15:08:30 christos Exp $");
34
35 #include <sys/types.h>
36 #include <sys/param.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 #ifdef WITH_OPENSSL
46 #include <openssl/bn.h>
47 #include <openssl/evp.h>
48 #endif
49
50 #include <netinet/in.h>
51
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <netdb.h>
55 #include <paths.h>
56 #include <pwd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <stdarg.h>
62 #include <unistd.h>
63 #include <limits.h>
64
65 #include "xmalloc.h"
66 #include "ssh.h"
67 #include "ssh2.h"
68 #include "sshpty.h"
69 #include "packet.h"
70 #include "log.h"
71 #include "sshbuf.h"
72 #include "misc.h"
73 #include "match.h"
74 #include "servconf.h"
75 #include "uidswap.h"
76 #include "compat.h"
77 #include "cipher.h"
78 #include "digest.h"
79 #include "sshkey.h"
80 #include "kex.h"
81 #include "authfile.h"
82 #include "pathnames.h"
83 #include "atomicio.h"
84 #include "canohost.h"
85 #include "hostfile.h"
86 #include "auth.h"
87 #include "authfd.h"
88 #include "msg.h"
89 #include "dispatch.h"
90 #include "channels.h"
91 #include "session.h"
92 #include "monitor.h"
93 #ifdef GSSAPI
94 #include "ssh-gss.h"
95 #endif
96 #include "monitor_wrap.h"
97 #include "auth-options.h"
98 #include "version.h"
99 #include "ssherr.h"
100 #include "sk-api.h"
101 #include "srclimit.h"
102 #include "dh.h"
103
104 #include "pfilter.h"
105
106 #ifdef LIBWRAP
107 #include <tcpd.h>
108 #include <syslog.h>
109 int allow_severity = LOG_INFO;
110 int deny_severity = LOG_WARNING;
111
112 static void
113 check_connection(const char *argv0, int sock_in)
114 {
115 struct request_info req;
116
117 request_init(&req, RQ_DAEMON, argv0, RQ_FILE, sock_in, 0);
118 fromhost(&req);
119
120 if (hosts_access(&req))
121 return;
122 debug("Connection refused by tcp wrapper");
123 /* n.b. hosts_access(3) has logged and notified blocklistd */
124 refuse(&req);
125 /* NOTREACHED */
126 fatal("libwrap refuse returns");
127 }
128 #endif /* LIBWRAP */
129
130 #ifdef WITH_LDAP_PUBKEY
131 #include "ldapauth.h"
132 #endif
133
134 #ifndef HOST_NAME_MAX
135 #define HOST_NAME_MAX MAXHOSTNAMELEN
136 #endif
137
138 /* Re-exec fds */
139 #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
140 #define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 2)
141 #define REEXEC_MIN_FREE_FD (STDERR_FILENO + 3)
142
143 /* Privsep fds */
144 #define PRIVSEP_MONITOR_FD (STDERR_FILENO + 1)
145 #define PRIVSEP_LOG_FD (STDERR_FILENO + 2)
146 #define PRIVSEP_MIN_FREE_FD (STDERR_FILENO + 3)
147
148 extern char *__progname;
149
150 /* Server configuration options. */
151 ServerOptions options;
152
153 /* Name of the server configuration file. */
154 const char *config_file_name = _PATH_SERVER_CONFIG_FILE;
155
156 /*
157 * Debug mode flag. This can be set on the command line. If debug
158 * mode is enabled, extra debugging output will be sent to the system
159 * log, the daemon will not go to background, and will exit after processing
160 * the first connection.
161 */
162 int debug_flag = 0;
163
164 /* Flag indicating that the daemon is being started from inetd. */
165 static int inetd_flag = 0;
166
167 /* debug goes to stderr unless inetd_flag is set */
168 static int log_stderr = 0;
169
170 /* Saved arguments to main(). */
171 static char **saved_argv;
172
173 /* Daemon's agent connection */
174 int auth_sock = -1;
175 static int have_agent = 0;
176
177 /*
178 * Any really sensitive data in the application is contained in this
179 * structure. The idea is that this structure could be locked into memory so
180 * that the pages do not get written into swap. However, there are some
181 * problems. The private key contains BIGNUMs, and we do not (in principle)
182 * have access to the internals of them, and locking just the structure is
183 * not very useful. Currently, memory locking is not implemented.
184 */
185 struct {
186 u_int num_hostkeys;
187 struct sshkey **host_keys; /* all private host keys */
188 struct sshkey **host_pubkeys; /* all public host keys */
189 struct sshkey **host_certificates; /* all public host certificates */
190 } sensitive_data;
191
192 /* record remote hostname or ip */
193 u_int utmp_len = HOST_NAME_MAX+1;
194
195 static int startup_pipe = -1; /* in child */
196
197 /* variables used for privilege separation */
198 struct monitor *pmonitor = NULL;
199 int privsep_is_preauth = 1;
200
201 /* global connection state and authentication contexts */
202 Authctxt *the_authctxt = NULL;
203 struct ssh *the_active_state;
204
205 /* global key/cert auth options. XXX move to permanent ssh->authctxt? */
206 struct sshauthopt *auth_opts = NULL;
207
208 /* sshd_config buffer */
209 struct sshbuf *cfg;
210
211 /* Included files from the configuration file */
212 struct include_list includes = TAILQ_HEAD_INITIALIZER(includes);
213
214 /* message to be displayed after login */
215 struct sshbuf *loginmsg;
216
217 /* Prototypes for various functions defined later in this file. */
218 void destroy_sensitive_data(void);
219 void demote_sensitive_data(void);
220
221 /* XXX reduce to stub once postauth split */
222 int
223 mm_is_monitor(void)
224 {
225 /*
226 * m_pid is only set in the privileged part, and
227 * points to the unprivileged child.
228 */
229 return (pmonitor && pmonitor->m_pid > 0);
230 }
231
232 /*
233 * Signal handler for the alarm after the login grace period has expired.
234 * As usual, this may only take signal-safe actions, even though it is
235 * terminal.
236 */
237 __dead
238 static void
239 grace_alarm_handler(int sig)
240 {
241 pfilter_notify(1);
242 /*
243 * Try to kill any processes that we have spawned, E.g. authorized
244 * keys command helpers or privsep children.
245 */
246 if (getpgid(0) == getpid()) {
247 struct sigaction sa;
248
249 /* mask all other signals while in handler */
250 memset(&sa, 0, sizeof(sa));
251 sa.sa_handler = SIG_IGN;
252 sigfillset(&sa.sa_mask);
253 sa.sa_flags = SA_RESTART;
254 (void)sigaction(SIGTERM, &sa, NULL);
255 kill(0, SIGTERM);
256 }
257 _exit(EXIT_LOGIN_GRACE);
258 }
259
260 /* Destroy the host and server keys. They will no longer be needed. */
261 void
262 destroy_sensitive_data(void)
263 {
264 u_int i;
265
266 for (i = 0; i < options.num_host_key_files; i++) {
267 if (sensitive_data.host_keys[i]) {
268 sshkey_free(sensitive_data.host_keys[i]);
269 sensitive_data.host_keys[i] = NULL;
270 }
271 if (sensitive_data.host_certificates[i]) {
272 sshkey_free(sensitive_data.host_certificates[i]);
273 sensitive_data.host_certificates[i] = NULL;
274 }
275 }
276 }
277
278 /* Demote private to public keys for network child */
279 void
280 demote_sensitive_data(void)
281 {
282 struct sshkey *tmp;
283 u_int i;
284 int r;
285
286 for (i = 0; i < options.num_host_key_files; i++) {
287 if (sensitive_data.host_keys[i]) {
288 if ((r = sshkey_from_private(
289 sensitive_data.host_keys[i], &tmp)) != 0)
290 fatal_r(r, "could not demote host %s key",
291 sshkey_type(sensitive_data.host_keys[i]));
292 sshkey_free(sensitive_data.host_keys[i]);
293 sensitive_data.host_keys[i] = tmp;
294 }
295 /* Certs do not need demotion */
296 }
297 }
298
299 struct sshbuf *
300 pack_hostkeys(void)
301 {
302 struct sshbuf *keybuf = NULL, *hostkeys = NULL;
303 int r;
304 u_int i;
305
306 if ((hostkeys = sshbuf_new()) == NULL)
307 fatal_f("sshbuf_new failed");
308
309 /* pack hostkeys into a string. Empty key slots get empty strings */
310 for (i = 0; i < options.num_host_key_files; i++) {
311 /* public key */
312 if (sensitive_data.host_pubkeys[i] != NULL) {
313 if ((r = sshkey_puts(sensitive_data.host_pubkeys[i],
314 hostkeys)) != 0)
315 fatal_fr(r, "compose hostkey public");
316 } else {
317 if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0)
318 fatal_fr(r, "compose hostkey empty public");
319 }
320 /* cert */
321 if (sensitive_data.host_certificates[i] != NULL) {
322 if ((r = sshkey_puts(
323 sensitive_data.host_certificates[i],
324 hostkeys)) != 0)
325 fatal_fr(r, "compose host cert");
326 } else {
327 if ((r = sshbuf_put_string(hostkeys, NULL, 0)) != 0)
328 fatal_fr(r, "compose host cert empty");
329 }
330 }
331
332 sshbuf_free(keybuf);
333 return hostkeys;
334 }
335
336 static int
337 privsep_preauth(struct ssh *ssh)
338 {
339 int r;
340 pid_t pid;
341
342 /* Set up unprivileged child process to deal with network data */
343 pmonitor = monitor_init();
344 /* Store a pointer to the kex for later rekeying */
345 pmonitor->m_pkex = &ssh->kex;
346
347 if ((pid = fork()) == -1)
348 fatal("fork of unprivileged child failed");
349 else if (pid != 0) {
350 debug2("Network child is on pid %ld", (long)pid);
351 pmonitor->m_pid = pid;
352 if (have_agent) {
353 r = ssh_get_authentication_socket(&auth_sock);
354 if (r != 0) {
355 error_r(r, "Could not get agent socket");
356 have_agent = 0;
357 }
358 }
359 monitor_child_preauth(ssh, pmonitor);
360 privsep_is_preauth = 0;
361 return 1;
362 } else {
363 /* child */
364 close(pmonitor->m_sendfd);
365 close(pmonitor->m_log_recvfd);
366
367 /*
368 * Arrange unpriv-preauth child process fds:
369 * 0, 1 network socket
370 * 2 optional stderr
371 * 3 reserved
372 * 4 monitor message socket
373 * 5 monitor logging socket
374 *
375 * We know that the monitor sockets will have fds > 4 because
376 * of the reserved fds in main()
377 */
378
379 if (ssh_packet_get_connection_in(ssh) != STDIN_FILENO &&
380 dup2(ssh_packet_get_connection_in(ssh), STDIN_FILENO) == -1)
381 fatal("dup2 stdin failed: %s", strerror(errno));
382 if (ssh_packet_get_connection_out(ssh) != STDOUT_FILENO &&
383 dup2(ssh_packet_get_connection_out(ssh),
384 STDOUT_FILENO) == -1)
385 fatal("dup2 stdout failed: %s", strerror(errno));
386 /* leave stderr as-is */
387 log_redirect_stderr_to(NULL); /* dup can clobber log fd */
388 if (pmonitor->m_recvfd != PRIVSEP_MONITOR_FD &&
389 dup2(pmonitor->m_recvfd, PRIVSEP_MONITOR_FD) == -1)
390 fatal("dup2 monitor fd: %s", strerror(errno));
391 if (pmonitor->m_log_sendfd != PRIVSEP_LOG_FD &&
392 dup2(pmonitor->m_log_sendfd, PRIVSEP_LOG_FD) == -1)
393 fatal("dup2 log fd: %s", strerror(errno));
394 closefrom(PRIVSEP_MIN_FREE_FD);
395
396 saved_argv[0] = options.sshd_auth_path;
397 execv(options.sshd_auth_path, saved_argv);
398
399 fatal_f("exec of %s failed: %s",
400 options.sshd_auth_path, strerror(errno));
401 }
402 }
403
404 static void
405 privsep_postauth(struct ssh *ssh, Authctxt *authctxt)
406 {
407 /* New socket pair */
408 monitor_reinit(pmonitor);
409
410 pmonitor->m_pid = fork();
411 if (pmonitor->m_pid == -1)
412 fatal("fork of unprivileged child failed");
413 else if (pmonitor->m_pid != 0) {
414 verbose("User child is on pid %ld", (long)pmonitor->m_pid);
415 sshbuf_reset(loginmsg);
416 monitor_clear_keystate(ssh, pmonitor);
417 monitor_child_postauth(ssh, pmonitor);
418
419 /* NEVERREACHED */
420 exit(0);
421 }
422
423 /* child */
424
425 close(pmonitor->m_sendfd);
426 pmonitor->m_sendfd = -1;
427
428 /* Demote the private keys to public keys. */
429 demote_sensitive_data();
430
431 /* Drop privileges */
432 do_setusercontext(authctxt->pw);
433
434 /* It is safe now to apply the key state */
435 monitor_apply_keystate(ssh, pmonitor);
436
437 /*
438 * Tell the packet layer that authentication was successful, since
439 * this information is not part of the key state.
440 */
441 ssh_packet_set_authenticated(ssh);
442 }
443
444 static struct sshkey *
445 get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
446 {
447 u_int i;
448 struct sshkey *key;
449
450 for (i = 0; i < options.num_host_key_files; i++) {
451 switch (type) {
452 case KEY_RSA_CERT:
453 case KEY_ECDSA_CERT:
454 case KEY_ED25519_CERT:
455 case KEY_ECDSA_SK_CERT:
456 case KEY_ED25519_SK_CERT:
457 key = sensitive_data.host_certificates[i];
458 break;
459 default:
460 key = sensitive_data.host_keys[i];
461 if (key == NULL && !need_private)
462 key = sensitive_data.host_pubkeys[i];
463 break;
464 }
465 if (key == NULL || key->type != type)
466 continue;
467 switch (type) {
468 case KEY_ECDSA:
469 case KEY_ECDSA_SK:
470 case KEY_ECDSA_CERT:
471 case KEY_ECDSA_SK_CERT:
472 if (key->ecdsa_nid != nid)
473 continue;
474 /* FALLTHROUGH */
475 default:
476 return need_private ?
477 sensitive_data.host_keys[i] : key;
478 }
479 }
480 return NULL;
481 }
482
483 struct sshkey *
484 get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
485 {
486 return get_hostkey_by_type(type, nid, 0, ssh);
487 }
488
489 struct sshkey *
490 get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
491 {
492 return get_hostkey_by_type(type, nid, 1, ssh);
493 }
494
495 struct sshkey *
496 get_hostkey_by_index(int ind)
497 {
498 if (ind < 0 || (u_int)ind >= options.num_host_key_files)
499 return (NULL);
500 return (sensitive_data.host_keys[ind]);
501 }
502
503 struct sshkey *
504 get_hostkey_public_by_index(int ind, struct ssh *ssh)
505 {
506 if (ind < 0 || (u_int)ind >= options.num_host_key_files)
507 return (NULL);
508 return (sensitive_data.host_pubkeys[ind]);
509 }
510
511 int
512 get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh)
513 {
514 u_int i;
515
516 for (i = 0; i < options.num_host_key_files; i++) {
517 if (sshkey_is_cert(key)) {
518 if (key == sensitive_data.host_certificates[i] ||
519 (compare && sensitive_data.host_certificates[i] &&
520 sshkey_equal(key,
521 sensitive_data.host_certificates[i])))
522 return (i);
523 } else {
524 if (key == sensitive_data.host_keys[i] ||
525 (compare && sensitive_data.host_keys[i] &&
526 sshkey_equal(key, sensitive_data.host_keys[i])))
527 return (i);
528 if (key == sensitive_data.host_pubkeys[i] ||
529 (compare && sensitive_data.host_pubkeys[i] &&
530 sshkey_equal(key, sensitive_data.host_pubkeys[i])))
531 return (i);
532 }
533 }
534 return (-1);
535 }
536
537 /* Inform the client of all hostkeys */
538 static void
539 notify_hostkeys(struct ssh *ssh)
540 {
541 struct sshbuf *buf;
542 struct sshkey *key;
543 u_int i, nkeys;
544 int r;
545 char *fp;
546
547 /* Some clients cannot cope with the hostkeys message, skip those. */
548 if (ssh->compat & SSH_BUG_HOSTKEYS)
549 return;
550
551 if ((buf = sshbuf_new()) == NULL)
552 fatal_f("sshbuf_new");
553 for (i = nkeys = 0; i < options.num_host_key_files; i++) {
554 key = get_hostkey_public_by_index(i, ssh);
555 if (key == NULL || key->type == KEY_UNSPEC ||
556 sshkey_is_cert(key))
557 continue;
558 fp = sshkey_fingerprint(key, options.fingerprint_hash,
559 SSH_FP_DEFAULT);
560 debug3_f("key %d: %s %s", i, sshkey_ssh_name(key), fp);
561 free(fp);
562 if (nkeys == 0) {
563 /*
564 * Start building the request when we find the
565 * first usable key.
566 */
567 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
568 (r = sshpkt_put_cstring(ssh, "hostkeys-00 (at) openssh.com")) != 0 ||
569 (r = sshpkt_put_u8(ssh, 0)) != 0) /* want reply */
570 sshpkt_fatal(ssh, r, "%s: start request", __func__);
571 }
572 /* Append the key to the request */
573 sshbuf_reset(buf);
574 if ((r = sshkey_putb(key, buf)) != 0)
575 fatal_fr(r, "couldn't put hostkey %d", i);
576 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
577 sshpkt_fatal(ssh, r, "%s: append key", __func__);
578 nkeys++;
579 }
580 debug3_f("sent %u hostkeys", nkeys);
581 if (nkeys == 0)
582 fatal_f("no hostkeys");
583 if ((r = sshpkt_send(ssh)) != 0)
584 sshpkt_fatal(ssh, r, "%s: send", __func__);
585 sshbuf_free(buf);
586 }
587
588 __dead static void
589 usage(void)
590 {
591 fprintf(stderr, "%s, %s\n", SSH_VERSION, SSH_OPENSSL_VERSION);
592 fprintf(stderr,
593 "usage: sshd [-46DdeGiqTtV] [-C connection_spec] [-c host_cert_file]\n"
594 " [-E log_file] [-f config_file] [-g login_grace_time]\n"
595 " [-h host_key_file] [-o option] [-p port] [-u len]\n"
596 );
597 exit(1);
598 }
599
600 static void
601 parse_hostkeys(struct sshbuf *hostkeys)
602 {
603 int r;
604 u_int num_keys = 0;
605 struct sshkey *k;
606 struct sshbuf *kbuf;
607 const u_char *cp;
608 size_t len;
609
610 while (sshbuf_len(hostkeys) != 0) {
611 if (num_keys > 2048)
612 fatal_f("too many hostkeys");
613 sensitive_data.host_keys = xrecallocarray(
614 sensitive_data.host_keys, num_keys, num_keys + 1,
615 sizeof(*sensitive_data.host_pubkeys));
616 sensitive_data.host_pubkeys = xrecallocarray(
617 sensitive_data.host_pubkeys, num_keys, num_keys + 1,
618 sizeof(*sensitive_data.host_pubkeys));
619 sensitive_data.host_certificates = xrecallocarray(
620 sensitive_data.host_certificates, num_keys, num_keys + 1,
621 sizeof(*sensitive_data.host_certificates));
622 /* private key */
623 k = NULL;
624 if ((r = sshbuf_froms(hostkeys, &kbuf)) != 0)
625 fatal_fr(r, "extract privkey");
626 if (sshbuf_len(kbuf) != 0 &&
627 (r = sshkey_private_deserialize(kbuf, &k)) != 0)
628 fatal_fr(r, "parse pubkey");
629 sensitive_data.host_keys[num_keys] = k;
630 sshbuf_free(kbuf);
631 if (k)
632 debug2_f("privkey %u: %s", num_keys, sshkey_ssh_name(k));
633 /* public key */
634 k = NULL;
635 if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0)
636 fatal_fr(r, "extract pubkey");
637 if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0)
638 fatal_fr(r, "parse pubkey");
639 sensitive_data.host_pubkeys[num_keys] = k;
640 if (k)
641 debug2_f("pubkey %u: %s", num_keys, sshkey_ssh_name(k));
642 /* certificate */
643 k = NULL;
644 if ((r = sshbuf_get_string_direct(hostkeys, &cp, &len)) != 0)
645 fatal_fr(r, "extract pubkey");
646 if (len != 0 && (r = sshkey_from_blob(cp, len, &k)) != 0)
647 fatal_fr(r, "parse pubkey");
648 sensitive_data.host_certificates[num_keys] = k;
649 if (k)
650 debug2_f("cert %u: %s", num_keys, sshkey_ssh_name(k));
651 num_keys++;
652 }
653 sensitive_data.num_hostkeys = num_keys;
654 }
655
656 static void
657 recv_rexec_state(int fd, struct sshbuf *conf, uint64_t *timing_secretp)
658 {
659 struct sshbuf *m, *inc, *hostkeys;
660 u_char *cp, ver;
661 size_t len;
662 int r;
663 struct include_item *item;
664
665 debug3_f("entering fd = %d", fd);
666
667 if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
668 fatal_f("sshbuf_new failed");
669
670 /* receive config */
671 if (ssh_msg_recv(fd, m) == -1)
672 fatal_f("ssh_msg_recv failed");
673 if ((r = sshbuf_get_u8(m, &ver)) != 0)
674 fatal_fr(r, "parse version");
675 if (ver != 0)
676 fatal_f("rexec version mismatch");
677 if ((r = sshbuf_get_string(m, &cp, &len)) != 0 || /* XXX _direct */
678 (r = sshbuf_get_u64(m, timing_secretp)) != 0 ||
679 (r = sshbuf_get_stringb(m, inc)) != 0)
680 fatal_fr(r, "parse config");
681
682 if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
683 fatal_fr(r, "sshbuf_put");
684
685 while (sshbuf_len(inc) != 0) {
686 item = xcalloc(1, sizeof(*item));
687 if ((item->contents = sshbuf_new()) == NULL)
688 fatal_f("sshbuf_new failed");
689 if ((r = sshbuf_get_cstring(inc, &item->selector, NULL)) != 0 ||
690 (r = sshbuf_get_cstring(inc, &item->filename, NULL)) != 0 ||
691 (r = sshbuf_get_stringb(inc, item->contents)) != 0)
692 fatal_fr(r, "parse includes");
693 TAILQ_INSERT_TAIL(&includes, item, entry);
694 }
695
696 /* receive hostkeys */
697 sshbuf_reset(m);
698 if (ssh_msg_recv(fd, m) == -1)
699 fatal_f("ssh_msg_recv failed");
700 if ((r = sshbuf_get_u8(m, NULL)) != 0 ||
701 (r = sshbuf_froms(m, &hostkeys)) != 0)
702 fatal_fr(r, "parse config");
703 parse_hostkeys(hostkeys);
704
705 free(cp);
706 sshbuf_free(m);
707 sshbuf_free(hostkeys);
708 sshbuf_free(inc);
709
710 debug3_f("done");
711 }
712
713 /*
714 * If IP options are supported, make sure there are none (log and
715 * return an error if any are found). Basically we are worried about
716 * source routing; it can be used to pretend you are somebody
717 * (ip-address) you are not. That itself may be "almost acceptable"
718 * under certain circumstances, but rhosts authentication is useless
719 * if source routing is accepted. Notice also that if we just dropped
720 * source routing here, the other side could use IP spoofing to do
721 * rest of the interaction and could still bypass security. So we
722 * exit here if we detect any IP options.
723 */
724 static void
725 check_ip_options(struct ssh *ssh)
726 {
727 int sock_in = ssh_packet_get_connection_in(ssh);
728 struct sockaddr_storage from;
729 u_char opts[200];
730 socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from);
731 char text[sizeof(opts) * 3 + 1];
732
733 memset(&from, 0, sizeof(from));
734 if (getpeername(sock_in, (struct sockaddr *)&from,
735 &fromlen) == -1)
736 return;
737 if (from.ss_family != AF_INET)
738 return;
739 /* XXX IPv6 options? */
740
741 if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts,
742 &option_size) >= 0 && option_size != 0) {
743 text[0] = '\0';
744 for (i = 0; i < option_size; i++)
745 snprintf(text + i*3, sizeof(text) - i*3,
746 " %2.2x", opts[i]);
747 fatal("Connection from %.100s port %d with IP opts: %.800s",
748 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text);
749 }
750 }
751
752 #ifdef __OpenBSD__
753 /* Set the routing domain for this process */
754 static void
755 set_process_rdomain(struct ssh *ssh, const char *name)
756 {
757 int rtable, ortable = getrtable();
758 const char *errstr;
759
760 if (name == NULL)
761 return; /* default */
762
763 if (strcmp(name, "%D") == 0) {
764 /* "expands" to routing domain of connection */
765 if ((name = ssh_packet_rdomain_in(ssh)) == NULL)
766 return;
767 }
768
769 rtable = (int)strtonum(name, 0, 255, &errstr);
770 if (errstr != NULL) /* Shouldn't happen */
771 fatal("Invalid routing domain \"%s\": %s", name, errstr);
772 if (rtable != ortable && setrtable(rtable) != 0)
773 fatal("Unable to set routing domain %d: %s",
774 rtable, strerror(errno));
775 debug_f("set routing domain %d (was %d)", rtable, ortable);
776 }
777 #endif
778
779 /*
780 * Main program for the daemon.
781 */
782 int
783 main(int ac, char **av)
784 {
785 struct ssh *ssh = NULL;
786 extern char *optarg;
787 extern int optind;
788 int devnull, r, opt, on = 1, remote_port;
789 int sock_in = -1, sock_out = -1, rexeced_flag = 0, have_key = 0;
790 const char *remote_ip, *rdomain;
791 char *line, *laddr, *logfile = NULL;
792 u_int i;
793 u_int64_t ibytes, obytes;
794 mode_t new_umask;
795 Authctxt *authctxt;
796 struct connection_info *connection_info = NULL;
797 sigset_t sigmask;
798 uint64_t timing_secret = 0;
799 struct itimerval itv;
800
801 sigemptyset(&sigmask);
802 sigprocmask(SIG_SETMASK, &sigmask, NULL);
803
804 /* Save argv. */
805 saved_argv = av;
806
807 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
808 sanitise_stdfd();
809
810 /* Initialize configuration options to their default values. */
811 initialize_server_options(&options);
812
813 /* Parse command-line arguments. */
814 while ((opt = getopt(ac, av,
815 "C:E:b:c:f:g:h:k:o:p:u:46DGQRTdeiqrtV")) != -1) {
816 switch (opt) {
817 case '4':
818 options.address_family = AF_INET;
819 break;
820 case '6':
821 options.address_family = AF_INET6;
822 break;
823 case 'f':
824 config_file_name = optarg;
825 break;
826 case 'c':
827 servconf_add_hostcert("[command-line]", 0,
828 &options, optarg);
829 break;
830 case 'd':
831 if (debug_flag == 0) {
832 debug_flag = 1;
833 options.log_level = SYSLOG_LEVEL_DEBUG1;
834 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
835 options.log_level++;
836 break;
837 case 'D':
838 /* ignore */
839 break;
840 case 'E':
841 logfile = optarg;
842 /* FALLTHROUGH */
843 case 'e':
844 log_stderr = 1;
845 break;
846 case 'i':
847 inetd_flag = 1;
848 break;
849 case 'r':
850 /* ignore */
851 break;
852 case 'R':
853 rexeced_flag = 1;
854 break;
855 case 'Q':
856 /* ignored */
857 break;
858 case 'q':
859 options.log_level = SYSLOG_LEVEL_QUIET;
860 break;
861 case 'b':
862 /* protocol 1, ignored */
863 break;
864 case 'p':
865 options.ports_from_cmdline = 1;
866 if (options.num_ports >= MAX_PORTS) {
867 fprintf(stderr, "too many ports.\n");
868 exit(1);
869 }
870 options.ports[options.num_ports++] = a2port(optarg);
871 if (options.ports[options.num_ports-1] <= 0) {
872 fprintf(stderr, "Bad port number.\n");
873 exit(1);
874 }
875 break;
876 case 'g':
877 if ((options.login_grace_time = convtime(optarg)) == -1) {
878 fprintf(stderr, "Invalid login grace time.\n");
879 exit(1);
880 }
881 break;
882 case 'k':
883 /* protocol 1, ignored */
884 break;
885 case 'h':
886 servconf_add_hostkey("[command-line]", 0,
887 &options, optarg, 1);
888 break;
889 case 't':
890 case 'T':
891 case 'G':
892 fatal("test/dump modes not supported");
893 break;
894 case 'C':
895 connection_info = server_get_connection_info(ssh, 0, 0);
896 if (parse_server_match_testspec(connection_info,
897 optarg) == -1)
898 exit(1);
899 break;
900 case 'u':
901 utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
902 if (utmp_len > HOST_NAME_MAX+1) {
903 fprintf(stderr, "Invalid utmp length.\n");
904 exit(1);
905 }
906 break;
907 case 'o':
908 line = xstrdup(optarg);
909 if (process_server_config_line(&options, line,
910 "command-line", 0, NULL, NULL, &includes) != 0)
911 exit(1);
912 free(line);
913 break;
914 case 'V':
915 fprintf(stderr, "%s, %s\n",
916 SSH_VERSION, SSH_OPENSSL_VERSION);
917 exit(0);
918 default:
919 usage();
920 break;
921 }
922 }
923
924 /* Check that there are no remaining arguments. */
925 if (optind < ac) {
926 fprintf(stderr, "Extra argument %s.\n", av[optind]);
927 exit(1);
928 }
929
930 debug("sshd version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
931
932 if (!rexeced_flag)
933 fatal("sshd-session should not be executed directly");
934
935 closefrom(REEXEC_MIN_FREE_FD);
936
937 /* Reserve fds we'll need later for reexec things */
938 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
939 fatal("open %s: %s", _PATH_DEVNULL, strerror(errno));
940 while (devnull < PRIVSEP_MIN_FREE_FD) {
941 if ((devnull = dup(devnull)) == -1)
942 fatal("dup %s: %s", _PATH_DEVNULL, strerror(errno));
943 }
944
945 #ifdef WITH_OPENSSL
946 OpenSSL_add_all_algorithms();
947 #endif
948 /* If requested, redirect the logs to the specified logfile. */
949 if (logfile != NULL) {
950 char *cp, pid_s[32];
951
952 snprintf(pid_s, sizeof(pid_s), "%ld", (unsigned long)getpid());
953 cp = percent_expand(logfile,
954 "p", pid_s,
955 "P", "sshd-session",
956 (char *)NULL);
957 log_redirect_stderr_to(cp);
958 free(cp);
959 }
960
961 /*
962 * Force logging to stderr until we have loaded the private host
963 * key (unless started from inetd)
964 */
965 log_init(__progname,
966 options.log_level == SYSLOG_LEVEL_NOT_SET ?
967 SYSLOG_LEVEL_INFO : options.log_level,
968 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
969 SYSLOG_FACILITY_AUTH : options.log_facility,
970 log_stderr || !inetd_flag || debug_flag);
971
972 /* Fetch our configuration */
973 if ((cfg = sshbuf_new()) == NULL)
974 fatal("sshbuf_new config buf failed");
975 setproctitle("%s", "[rexeced]");
976 recv_rexec_state(REEXEC_CONFIG_PASS_FD, cfg, &timing_secret);
977 parse_server_config(&options, "rexec", cfg, &includes, NULL, 1);
978 /* Fill in default values for those options not explicitly set. */
979 fill_default_server_options(&options);
980 options.timing_secret = timing_secret;
981
982 /* Reinit logging in case config set Level, Facility or Verbose. */
983 log_init(__progname, options.log_level, options.log_facility,
984 log_stderr || !inetd_flag || debug_flag);
985
986 debug("sshd-session version %s, %s", SSH_VERSION, SSH_OPENSSL_VERSION);
987
988 #ifdef WITH_LDAP_PUBKEY
989 /* ldap_options_print(&options.lpk); */
990 /* XXX initialize/check ldap connection and set *LD */
991 if (options.lpk.on) {
992 if (options.lpk.l_conf && (ldap_parse_lconf(&options.lpk) < 0) )
993 error("[LDAP] could not parse %s", options.lpk.l_conf);
994 if (ldap_xconnect(&options.lpk) < 0)
995 error("[LDAP] could not initialize ldap connection");
996 }
997 #endif
998 if (!debug_flag && !inetd_flag) {
999 if ((startup_pipe = dup(REEXEC_CONFIG_PASS_FD)) == -1)
1000 fatal("internal error: no startup pipe");
1001
1002 /*
1003 * Signal parent that this child is at a point where
1004 * they can go away if they have a SIGHUP pending.
1005 */
1006 (void)atomicio(vwrite, startup_pipe, __UNCONST("\0"), 1);
1007 }
1008 /* close the fd, but keep the slot reserved */
1009 if (dup2(devnull, REEXEC_CONFIG_PASS_FD) == -1)
1010 fatal("dup2 devnull->config fd: %s", strerror(errno));
1011
1012 /* Check that options are sensible */
1013 if (options.authorized_keys_command_user == NULL &&
1014 (options.authorized_keys_command != NULL &&
1015 strcasecmp(options.authorized_keys_command, "none") != 0))
1016 fatal("AuthorizedKeysCommand set without "
1017 "AuthorizedKeysCommandUser");
1018 if (options.authorized_principals_command_user == NULL &&
1019 (options.authorized_principals_command != NULL &&
1020 strcasecmp(options.authorized_principals_command, "none") != 0))
1021 fatal("AuthorizedPrincipalsCommand set without "
1022 "AuthorizedPrincipalsCommandUser");
1023
1024 /*
1025 * Check whether there is any path through configured auth methods.
1026 * Unfortunately it is not possible to verify this generally before
1027 * daemonisation in the presence of Match block, but this catches
1028 * and warns for trivial misconfigurations that could break login.
1029 */
1030 if (options.num_auth_methods != 0) {
1031 for (i = 0; i < options.num_auth_methods; i++) {
1032 if (auth2_methods_valid(options.auth_methods[i],
1033 1) == 0)
1034 break;
1035 }
1036 if (i >= options.num_auth_methods)
1037 fatal("AuthenticationMethods cannot be satisfied by "
1038 "enabled authentication methods");
1039 }
1040
1041 #ifdef WITH_OPENSSL
1042 if (options.moduli_file != NULL)
1043 dh_set_moduli_file(options.moduli_file);
1044 #endif
1045
1046 if (options.host_key_agent) {
1047 if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1048 setenv(SSH_AUTHSOCKET_ENV_NAME,
1049 options.host_key_agent, 1);
1050 if ((r = ssh_get_authentication_socket(NULL)) == 0)
1051 have_agent = 1;
1052 else
1053 error_r(r, "Could not connect to agent \"%s\"",
1054 options.host_key_agent);
1055 }
1056
1057 if (options.num_host_key_files != sensitive_data.num_hostkeys) {
1058 fatal("internal error: hostkeys confused (config %u recvd %u)",
1059 options.num_host_key_files, sensitive_data.num_hostkeys);
1060 }
1061
1062 for (i = 0; i < options.num_host_key_files; i++) {
1063 if (sensitive_data.host_keys[i] != NULL ||
1064 (have_agent && sensitive_data.host_pubkeys[i] != NULL)) {
1065 have_key = 1;
1066 break;
1067 }
1068 }
1069 if (!have_key)
1070 fatal("internal error: monitor received no hostkeys");
1071
1072 /* Ensure that umask disallows at least group and world write */
1073 new_umask = umask(0077) | 0022;
1074 (void) umask(new_umask);
1075
1076 /* Initialize the log (it is reinitialized below in case we forked). */
1077 if (debug_flag)
1078 log_stderr = 1;
1079 log_init(__progname, options.log_level,
1080 options.log_facility, log_stderr);
1081 for (i = 0; i < options.num_log_verbose; i++)
1082 log_verbose_add(options.log_verbose[i]);
1083
1084 /* Reinitialize the log (because of the fork above). */
1085 log_init(__progname, options.log_level, options.log_facility, log_stderr);
1086
1087 /*
1088 * Chdir to the root directory so that the current disk can be
1089 * unmounted if desired.
1090 */
1091 if (chdir("/") == -1)
1092 error("chdir(\"/\"): %s", strerror(errno));
1093
1094 /* ignore SIGPIPE */
1095 ssh_signal(SIGPIPE, SIG_IGN);
1096
1097 /* Get a connection, either from inetd or rexec */
1098 if (inetd_flag) {
1099 /*
1100 * NB. must be different fd numbers for the !socket case,
1101 * as packet_connection_is_on_socket() depends on this.
1102 */
1103 sock_in = dup(STDIN_FILENO);
1104 sock_out = dup(STDOUT_FILENO);
1105 } else {
1106 /* rexec case; accept()ed socket in ancestor listener */
1107 sock_in = sock_out = dup(STDIN_FILENO);
1108 }
1109
1110 /*
1111 * We intentionally do not close the descriptors 0, 1, and 2
1112 * as our code for setting the descriptors won't work if
1113 * ttyfd happens to be one of those.
1114 */
1115 if (stdfd_devnull(1, 1, !log_stderr) == -1)
1116 error("stdfd_devnull failed");
1117 debug("network sockets: %d, %d", sock_in, sock_out);
1118
1119 /* This is the child processing a new connection. */
1120 setproctitle("%s", "[accepted]");
1121
1122 /* Executed child processes don't need these. */
1123 FD_CLOSEONEXEC(sock_out);
1124 FD_CLOSEONEXEC(sock_in);
1125
1126 /* We will not restart on SIGHUP since it no longer makes sense. */
1127 ssh_signal(SIGALRM, SIG_DFL);
1128 ssh_signal(SIGHUP, SIG_DFL);
1129 ssh_signal(SIGTERM, SIG_DFL);
1130 ssh_signal(SIGQUIT, SIG_DFL);
1131 ssh_signal(SIGCHLD, SIG_DFL);
1132
1133 pfilter_init();
1134
1135 /*
1136 * Register our connection. This turns encryption off because we do
1137 * not have a key.
1138 */
1139 if ((ssh = ssh_packet_set_connection(NULL, sock_in, sock_out)) == NULL)
1140 fatal("Unable to create connection");
1141 the_active_state = ssh;
1142 ssh_packet_set_server(ssh);
1143 ssh_packet_set_qos(ssh, options.ip_qos_interactive,
1144 options.ip_qos_bulk);
1145
1146 check_ip_options(ssh);
1147
1148 /* Prepare the channels layer */
1149 channel_init_channels(ssh);
1150 channel_set_af(ssh, options.address_family);
1151 server_process_channel_timeouts(ssh);
1152 server_process_permitopen(ssh);
1153
1154 /* Set SO_KEEPALIVE if requested. */
1155 if (options.tcp_keep_alive && ssh_packet_connection_is_on_socket(ssh) &&
1156 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1)
1157 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1158
1159 if ((remote_port = ssh_remote_port(ssh)) < 0) {
1160 debug("ssh_remote_port failed");
1161 cleanup_exit(255);
1162 }
1163
1164 /*
1165 * The rest of the code depends on the fact that
1166 * ssh_remote_ipaddr() caches the remote ip, even if
1167 * the socket goes away.
1168 */
1169 remote_ip = ssh_remote_ipaddr(ssh);
1170
1171 #ifdef LIBWRAP
1172 /* Check whether logins are denied from this host. */
1173 if (ssh_packet_connection_is_on_socket(ssh)) {
1174 /* First, try with the value stored in __progname */
1175 check_connection(__progname, sock_in);
1176 /*
1177 * Test with "sshd" as well, since that is what most people
1178 * will have in their hosts.allow and hosts.deny files.
1179 */
1180 check_connection("sshd", sock_in);
1181 }
1182 #endif /* LIBWRAP */
1183
1184 rdomain = ssh_packet_rdomain_in(ssh);
1185
1186 /* Log the connection. */
1187 laddr = get_local_ipaddr(sock_in);
1188 verbose("Connection from %s port %d on %s port %d%s%s%s",
1189 remote_ip, remote_port, laddr, ssh_local_port(ssh),
1190 rdomain == NULL ? "" : " rdomain \"",
1191 rdomain == NULL ? "" : rdomain,
1192 rdomain == NULL ? "" : "\"");
1193 free(laddr);
1194
1195 /*
1196 * We don't want to listen forever unless the other side
1197 * successfully authenticates itself. So we set up an alarm which is
1198 * cleared after successful authentication. A limit of zero
1199 * indicates no limit. Note that we don't set the alarm in debugging
1200 * mode; it is just annoying to have the server exit just when you
1201 * are about to discover the bug.
1202 */
1203 ssh_signal(SIGALRM, grace_alarm_handler);
1204 if (!debug_flag && options.login_grace_time > 0) {
1205 int ujitter = arc4random_uniform(4 * 1000000);
1206
1207 timerclear(&itv.it_interval);
1208 itv.it_value.tv_sec = options.login_grace_time;
1209 itv.it_value.tv_sec += ujitter / 1000000;
1210 itv.it_value.tv_usec = ujitter % 1000000;
1211
1212 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
1213 fatal("login grace time setitimer failed");
1214 }
1215
1216 ssh_packet_set_nonblocking(ssh);
1217
1218 /* allocate authentication context */
1219 authctxt = xcalloc(1, sizeof(*authctxt));
1220 ssh->authctxt = authctxt;
1221
1222 /* XXX global for cleanup, access from other modules */
1223 the_authctxt = authctxt;
1224
1225 /* Set default key authentication options */
1226 if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL)
1227 fatal("allocation failed");
1228
1229 /* prepare buffer to collect messages to display to user after login */
1230 if ((loginmsg = sshbuf_new()) == NULL)
1231 fatal("sshbuf_new loginmsg failed");
1232 auth_debug_reset();
1233
1234 if (privsep_preauth(ssh) != 1)
1235 fatal("privsep_preauth failed");
1236
1237 /* Now user is authenticated */
1238
1239 /*
1240 * Cancel the alarm we set to limit the time taken for
1241 * authentication.
1242 */
1243 timerclear(&itv.it_interval);
1244 timerclear(&itv.it_value);
1245 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
1246 fatal("login grace time clear failed");
1247 ssh_signal(SIGALRM, SIG_DFL);
1248 authctxt->authenticated = 1;
1249 if (startup_pipe != -1) {
1250 /* signal listener that authentication completed successfully */
1251 (void)atomicio(vwrite, startup_pipe, __UNCONST("\001"), 1);
1252 close(startup_pipe);
1253 startup_pipe = -1;
1254 }
1255
1256 #ifdef __OpenBSD__
1257 if (options.routing_domain != NULL)
1258 set_process_rdomain(ssh, options.routing_domain);
1259 #endif
1260
1261 #ifdef GSSAPI
1262 if (options.gss_authentication) {
1263 temporarily_use_uid(authctxt->pw);
1264 ssh_gssapi_storecreds();
1265 restore_uid();
1266 }
1267 #endif
1268 #ifdef USE_PAM
1269 if (options.use_pam) {
1270 do_pam_setcred();
1271 do_pam_session(ssh);
1272 }
1273 #endif
1274
1275 /*
1276 * In privilege separation, we fork another child and prepare
1277 * file descriptor passing.
1278 */
1279 privsep_postauth(ssh, authctxt);
1280 /* the monitor process [priv] will not return */
1281
1282 ssh_packet_set_timeout(ssh, options.client_alive_interval,
1283 options.client_alive_count_max);
1284
1285 /* Try to send all our hostkeys to the client */
1286 notify_hostkeys(ssh);
1287
1288 /* Start session. */
1289 do_authenticated(ssh, authctxt);
1290
1291 /* The connection has been terminated. */
1292 ssh_packet_get_bytes(ssh, &ibytes, &obytes);
1293 verbose("Transferred: sent %llu, received %llu bytes",
1294 (unsigned long long)obytes, (unsigned long long)ibytes);
1295
1296 #ifdef USE_PAM
1297 if (options.use_pam)
1298 finish_pam();
1299 #endif /* USE_PAM */
1300
1301 verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
1302 ssh_packet_close(ssh);
1303
1304 mm_terminate();
1305
1306 exit(0);
1307 }
1308
1309 int
1310 sshd_hostkey_sign(struct ssh *ssh, struct sshkey *privkey,
1311 struct sshkey *pubkey, u_char **signature, size_t *slenp,
1312 const u_char *data, size_t dlen, const char *alg)
1313 {
1314 if (privkey) {
1315 if (mm_sshkey_sign(ssh, privkey, signature, slenp,
1316 data, dlen, alg, options.sk_provider, NULL,
1317 ssh->compat) < 0)
1318 fatal_f("privkey sign failed");
1319 } else {
1320 if (mm_sshkey_sign(ssh, pubkey, signature, slenp,
1321 data, dlen, alg, options.sk_provider, NULL,
1322 ssh->compat) < 0)
1323 fatal_f("pubkey sign failed");
1324 }
1325 return 0;
1326 }
1327
1328 /* server specific fatal cleanup */
1329 void
1330 cleanup_exit(int i)
1331 {
1332 if (the_active_state != NULL && the_authctxt != NULL) {
1333 do_cleanup(the_active_state, the_authctxt);
1334 if (privsep_is_preauth &&
1335 pmonitor != NULL && pmonitor->m_pid > 1) {
1336 debug("Killing privsep child %d", pmonitor->m_pid);
1337 if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
1338 errno != ESRCH) {
1339 error_f("kill(%d): %s", pmonitor->m_pid,
1340 strerror(errno));
1341 }
1342 }
1343 }
1344 /* Override default fatal exit value when auth was attempted */
1345 if (i == 255 && monitor_auth_attempted()) {
1346 pfilter_notify(1);
1347 _exit(EXIT_AUTH_ATTEMPTED);
1348 }
1349 if (i == 255 && monitor_invalid_user())
1350 _exit(EXIT_INVALID_USER);
1351 _exit(i);
1352 }
1353