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