1 |
|
|
/* $OpenBSD: sshd.c,v 1.498 2017/11/03 03:18:53 dtucker Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 |
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 |
|
|
* All rights reserved |
6 |
|
|
* This program is the ssh daemon. It listens for connections from clients, |
7 |
|
|
* and performs authentication, executes use commands or shell, and forwards |
8 |
|
|
* information to/from the application to the user client over an encrypted |
9 |
|
|
* connection. This can also handle forwarding of X11, TCP/IP, and |
10 |
|
|
* authentication agent connections. |
11 |
|
|
* |
12 |
|
|
* As far as I am concerned, the code I have written for this software |
13 |
|
|
* can be used freely for any purpose. Any derived versions of this |
14 |
|
|
* software must be clearly marked as such, and if the derived work is |
15 |
|
|
* incompatible with the protocol description in the RFC file, it must be |
16 |
|
|
* called by a name other than "ssh" or "Secure Shell". |
17 |
|
|
* |
18 |
|
|
* SSH2 implementation: |
19 |
|
|
* Privilege Separation: |
20 |
|
|
* |
21 |
|
|
* Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved. |
22 |
|
|
* Copyright (c) 2002 Niels Provos. All rights reserved. |
23 |
|
|
* |
24 |
|
|
* Redistribution and use in source and binary forms, with or without |
25 |
|
|
* modification, are permitted provided that the following conditions |
26 |
|
|
* are met: |
27 |
|
|
* 1. Redistributions of source code must retain the above copyright |
28 |
|
|
* notice, this list of conditions and the following disclaimer. |
29 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
30 |
|
|
* notice, this list of conditions and the following disclaimer in the |
31 |
|
|
* documentation and/or other materials provided with the distribution. |
32 |
|
|
* |
33 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
34 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
35 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
36 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
37 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
38 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
39 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
40 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
41 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
42 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
43 |
|
|
*/ |
44 |
|
|
|
45 |
|
|
#include <sys/types.h> |
46 |
|
|
#include <sys/ioctl.h> |
47 |
|
|
#include <sys/wait.h> |
48 |
|
|
#include <sys/tree.h> |
49 |
|
|
#include <sys/stat.h> |
50 |
|
|
#include <sys/socket.h> |
51 |
|
|
#include <sys/time.h> |
52 |
|
|
#include <sys/queue.h> |
53 |
|
|
|
54 |
|
|
#include <errno.h> |
55 |
|
|
#include <fcntl.h> |
56 |
|
|
#include <netdb.h> |
57 |
|
|
#include <paths.h> |
58 |
|
|
#include <pwd.h> |
59 |
|
|
#include <signal.h> |
60 |
|
|
#include <stdio.h> |
61 |
|
|
#include <stdlib.h> |
62 |
|
|
#include <string.h> |
63 |
|
|
#include <unistd.h> |
64 |
|
|
#include <limits.h> |
65 |
|
|
|
66 |
|
|
#ifdef WITH_OPENSSL |
67 |
|
|
#include <openssl/bn.h> |
68 |
|
|
#endif |
69 |
|
|
|
70 |
|
|
#include "xmalloc.h" |
71 |
|
|
#include "ssh.h" |
72 |
|
|
#include "ssh2.h" |
73 |
|
|
#include "sshpty.h" |
74 |
|
|
#include "packet.h" |
75 |
|
|
#include "log.h" |
76 |
|
|
#include "buffer.h" |
77 |
|
|
#include "misc.h" |
78 |
|
|
#include "match.h" |
79 |
|
|
#include "servconf.h" |
80 |
|
|
#include "uidswap.h" |
81 |
|
|
#include "compat.h" |
82 |
|
|
#include "cipher.h" |
83 |
|
|
#include "digest.h" |
84 |
|
|
#include "key.h" |
85 |
|
|
#include "kex.h" |
86 |
|
|
#include "myproposal.h" |
87 |
|
|
#include "authfile.h" |
88 |
|
|
#include "pathnames.h" |
89 |
|
|
#include "atomicio.h" |
90 |
|
|
#include "canohost.h" |
91 |
|
|
#include "hostfile.h" |
92 |
|
|
#include "auth.h" |
93 |
|
|
#include "authfd.h" |
94 |
|
|
#include "msg.h" |
95 |
|
|
#include "dispatch.h" |
96 |
|
|
#include "channels.h" |
97 |
|
|
#include "session.h" |
98 |
|
|
#include "monitor.h" |
99 |
|
|
#ifdef GSSAPI |
100 |
|
|
#include "ssh-gss.h" |
101 |
|
|
#endif |
102 |
|
|
#include "monitor_wrap.h" |
103 |
|
|
#include "ssh-sandbox.h" |
104 |
|
|
#include "version.h" |
105 |
|
|
#include "ssherr.h" |
106 |
|
|
|
107 |
|
|
/* Re-exec fds */ |
108 |
|
|
#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1) |
109 |
|
|
#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2) |
110 |
|
|
#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3) |
111 |
|
|
#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4) |
112 |
|
|
|
113 |
|
|
extern char *__progname; |
114 |
|
|
|
115 |
|
|
/* Server configuration options. */ |
116 |
|
|
ServerOptions options; |
117 |
|
|
|
118 |
|
|
/* Name of the server configuration file. */ |
119 |
|
|
char *config_file_name = _PATH_SERVER_CONFIG_FILE; |
120 |
|
|
|
121 |
|
|
/* |
122 |
|
|
* Debug mode flag. This can be set on the command line. If debug |
123 |
|
|
* mode is enabled, extra debugging output will be sent to the system |
124 |
|
|
* log, the daemon will not go to background, and will exit after processing |
125 |
|
|
* the first connection. |
126 |
|
|
*/ |
127 |
|
|
int debug_flag = 0; |
128 |
|
|
|
129 |
|
|
/* Flag indicating that the daemon should only test the configuration and keys. */ |
130 |
|
|
int test_flag = 0; |
131 |
|
|
|
132 |
|
|
/* Flag indicating that the daemon is being started from inetd. */ |
133 |
|
|
int inetd_flag = 0; |
134 |
|
|
|
135 |
|
|
/* Flag indicating that sshd should not detach and become a daemon. */ |
136 |
|
|
int no_daemon_flag = 0; |
137 |
|
|
|
138 |
|
|
/* debug goes to stderr unless inetd_flag is set */ |
139 |
|
|
int log_stderr = 0; |
140 |
|
|
|
141 |
|
|
/* Saved arguments to main(). */ |
142 |
|
|
char **saved_argv; |
143 |
|
|
|
144 |
|
|
/* re-exec */ |
145 |
|
|
int rexeced_flag = 0; |
146 |
|
|
int rexec_flag = 1; |
147 |
|
|
int rexec_argc = 0; |
148 |
|
|
char **rexec_argv; |
149 |
|
|
|
150 |
|
|
/* |
151 |
|
|
* The sockets that the server is listening; this is used in the SIGHUP |
152 |
|
|
* signal handler. |
153 |
|
|
*/ |
154 |
|
|
#define MAX_LISTEN_SOCKS 16 |
155 |
|
|
int listen_socks[MAX_LISTEN_SOCKS]; |
156 |
|
|
int num_listen_socks = 0; |
157 |
|
|
|
158 |
|
|
/* |
159 |
|
|
* the client's version string, passed by sshd2 in compat mode. if != NULL, |
160 |
|
|
* sshd will skip the version-number exchange |
161 |
|
|
*/ |
162 |
|
|
char *client_version_string = NULL; |
163 |
|
|
char *server_version_string = NULL; |
164 |
|
|
|
165 |
|
|
/* Daemon's agent connection */ |
166 |
|
|
int auth_sock = -1; |
167 |
|
|
int have_agent = 0; |
168 |
|
|
|
169 |
|
|
/* |
170 |
|
|
* Any really sensitive data in the application is contained in this |
171 |
|
|
* structure. The idea is that this structure could be locked into memory so |
172 |
|
|
* that the pages do not get written into swap. However, there are some |
173 |
|
|
* problems. The private key contains BIGNUMs, and we do not (in principle) |
174 |
|
|
* have access to the internals of them, and locking just the structure is |
175 |
|
|
* not very useful. Currently, memory locking is not implemented. |
176 |
|
|
*/ |
177 |
|
|
struct { |
178 |
|
|
struct sshkey **host_keys; /* all private host keys */ |
179 |
|
|
struct sshkey **host_pubkeys; /* all public host keys */ |
180 |
|
|
struct sshkey **host_certificates; /* all public host certificates */ |
181 |
|
|
int have_ssh2_key; |
182 |
|
|
} sensitive_data; |
183 |
|
|
|
184 |
|
|
/* This is set to true when a signal is received. */ |
185 |
|
|
static volatile sig_atomic_t received_sighup = 0; |
186 |
|
|
static volatile sig_atomic_t received_sigterm = 0; |
187 |
|
|
|
188 |
|
|
/* session identifier, used by RSA-auth */ |
189 |
|
|
u_char session_id[16]; |
190 |
|
|
|
191 |
|
|
/* same for ssh2 */ |
192 |
|
|
u_char *session_id2 = NULL; |
193 |
|
|
u_int session_id2_len = 0; |
194 |
|
|
|
195 |
|
|
/* record remote hostname or ip */ |
196 |
|
|
u_int utmp_len = HOST_NAME_MAX+1; |
197 |
|
|
|
198 |
|
|
/* options.max_startup sized array of fd ints */ |
199 |
|
|
int *startup_pipes = NULL; |
200 |
|
|
int startup_pipe; /* in child */ |
201 |
|
|
|
202 |
|
|
/* variables used for privilege separation */ |
203 |
|
|
int use_privsep = -1; |
204 |
|
|
struct monitor *pmonitor = NULL; |
205 |
|
|
int privsep_is_preauth = 1; |
206 |
|
|
|
207 |
|
|
/* global authentication context */ |
208 |
|
|
Authctxt *the_authctxt = NULL; |
209 |
|
|
|
210 |
|
|
/* sshd_config buffer */ |
211 |
|
|
Buffer cfg; |
212 |
|
|
|
213 |
|
|
/* message to be displayed after login */ |
214 |
|
|
Buffer loginmsg; |
215 |
|
|
|
216 |
|
|
/* Prototypes for various functions defined later in this file. */ |
217 |
|
|
void destroy_sensitive_data(void); |
218 |
|
|
void demote_sensitive_data(void); |
219 |
|
|
static void do_ssh2_kex(void); |
220 |
|
|
|
221 |
|
|
/* |
222 |
|
|
* Close all listening sockets |
223 |
|
|
*/ |
224 |
|
|
static void |
225 |
|
|
close_listen_socks(void) |
226 |
|
|
{ |
227 |
|
|
int i; |
228 |
|
|
|
229 |
|
|
for (i = 0; i < num_listen_socks; i++) |
230 |
|
|
close(listen_socks[i]); |
231 |
|
|
num_listen_socks = -1; |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
static void |
235 |
|
|
close_startup_pipes(void) |
236 |
|
|
{ |
237 |
|
|
int i; |
238 |
|
|
|
239 |
|
|
if (startup_pipes) |
240 |
|
|
for (i = 0; i < options.max_startups; i++) |
241 |
|
|
if (startup_pipes[i] != -1) |
242 |
|
|
close(startup_pipes[i]); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
/* |
246 |
|
|
* Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; |
247 |
|
|
* the effect is to reread the configuration file (and to regenerate |
248 |
|
|
* the server key). |
249 |
|
|
*/ |
250 |
|
|
|
251 |
|
|
/*ARGSUSED*/ |
252 |
|
|
static void |
253 |
|
|
sighup_handler(int sig) |
254 |
|
|
{ |
255 |
|
|
int save_errno = errno; |
256 |
|
|
|
257 |
|
|
received_sighup = 1; |
258 |
|
|
signal(SIGHUP, sighup_handler); |
259 |
|
|
errno = save_errno; |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
/* |
263 |
|
|
* Called from the main program after receiving SIGHUP. |
264 |
|
|
* Restarts the server. |
265 |
|
|
*/ |
266 |
|
|
static void |
267 |
|
|
sighup_restart(void) |
268 |
|
|
{ |
269 |
|
|
logit("Received SIGHUP; restarting."); |
270 |
|
|
if (options.pid_file != NULL) |
271 |
|
|
unlink(options.pid_file); |
272 |
|
|
close_listen_socks(); |
273 |
|
|
close_startup_pipes(); |
274 |
|
|
alarm(0); /* alarm timer persists across exec */ |
275 |
|
|
signal(SIGHUP, SIG_IGN); /* will be restored after exec */ |
276 |
|
|
execv(saved_argv[0], saved_argv); |
277 |
|
|
logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0], |
278 |
|
|
strerror(errno)); |
279 |
|
|
exit(1); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
/* |
283 |
|
|
* Generic signal handler for terminating signals in the master daemon. |
284 |
|
|
*/ |
285 |
|
|
/*ARGSUSED*/ |
286 |
|
|
static void |
287 |
|
|
sigterm_handler(int sig) |
288 |
|
|
{ |
289 |
|
|
received_sigterm = sig; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
/* |
293 |
|
|
* SIGCHLD handler. This is called whenever a child dies. This will then |
294 |
|
|
* reap any zombies left by exited children. |
295 |
|
|
*/ |
296 |
|
|
/*ARGSUSED*/ |
297 |
|
|
static void |
298 |
|
|
main_sigchld_handler(int sig) |
299 |
|
|
{ |
300 |
|
|
int save_errno = errno; |
301 |
|
|
pid_t pid; |
302 |
|
|
int status; |
303 |
|
|
|
304 |
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || |
305 |
|
|
(pid < 0 && errno == EINTR)) |
306 |
|
|
; |
307 |
|
|
|
308 |
|
|
signal(SIGCHLD, main_sigchld_handler); |
309 |
|
|
errno = save_errno; |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
/* |
313 |
|
|
* Signal handler for the alarm after the login grace period has expired. |
314 |
|
|
*/ |
315 |
|
|
/*ARGSUSED*/ |
316 |
|
|
static void |
317 |
|
|
grace_alarm_handler(int sig) |
318 |
|
|
{ |
319 |
|
|
if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0) |
320 |
|
|
kill(pmonitor->m_pid, SIGALRM); |
321 |
|
|
|
322 |
|
|
/* |
323 |
|
|
* Try to kill any processes that we have spawned, E.g. authorized |
324 |
|
|
* keys command helpers. |
325 |
|
|
*/ |
326 |
|
|
if (getpgid(0) == getpid()) { |
327 |
|
|
signal(SIGTERM, SIG_IGN); |
328 |
|
|
kill(0, SIGTERM); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
/* Log error and exit. */ |
332 |
|
|
sigdie("Timeout before authentication for %s port %d", |
333 |
|
|
ssh_remote_ipaddr(active_state), ssh_remote_port(active_state)); |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
static void |
337 |
|
|
sshd_exchange_identification(struct ssh *ssh, int sock_in, int sock_out) |
338 |
|
|
{ |
339 |
|
|
u_int i; |
340 |
|
|
int remote_major, remote_minor; |
341 |
|
|
char *s; |
342 |
|
|
char buf[256]; /* Must not be larger than remote_version. */ |
343 |
|
|
char remote_version[256]; /* Must be at least as big as buf. */ |
344 |
|
|
|
345 |
|
|
xasprintf(&server_version_string, "SSH-%d.%d-%.100s%s%s\r\n", |
346 |
|
|
PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION, |
347 |
|
|
*options.version_addendum == '\0' ? "" : " ", |
348 |
|
|
options.version_addendum); |
349 |
|
|
|
350 |
|
|
/* Send our protocol version identification. */ |
351 |
|
|
if (atomicio(vwrite, sock_out, server_version_string, |
352 |
|
|
strlen(server_version_string)) |
353 |
|
|
!= strlen(server_version_string)) { |
354 |
|
|
logit("Could not write ident string to %s port %d", |
355 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
356 |
|
|
cleanup_exit(255); |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
/* Read other sides version identification. */ |
360 |
|
|
memset(buf, 0, sizeof(buf)); |
361 |
|
|
for (i = 0; i < sizeof(buf) - 1; i++) { |
362 |
|
|
if (atomicio(read, sock_in, &buf[i], 1) != 1) { |
363 |
|
|
logit("Did not receive identification string " |
364 |
|
|
"from %s port %d", |
365 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
366 |
|
|
cleanup_exit(255); |
367 |
|
|
} |
368 |
|
|
if (buf[i] == '\r') { |
369 |
|
|
buf[i] = 0; |
370 |
|
|
/* Kludge for F-Secure Macintosh < 1.0.2 */ |
371 |
|
|
if (i == 12 && |
372 |
|
|
strncmp(buf, "SSH-1.5-W1.0", 12) == 0) |
373 |
|
|
break; |
374 |
|
|
continue; |
375 |
|
|
} |
376 |
|
|
if (buf[i] == '\n') { |
377 |
|
|
buf[i] = 0; |
378 |
|
|
break; |
379 |
|
|
} |
380 |
|
|
} |
381 |
|
|
buf[sizeof(buf) - 1] = 0; |
382 |
|
|
client_version_string = xstrdup(buf); |
383 |
|
|
|
384 |
|
|
/* |
385 |
|
|
* Check that the versions match. In future this might accept |
386 |
|
|
* several versions and set appropriate flags to handle them. |
387 |
|
|
*/ |
388 |
|
|
if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n", |
389 |
|
|
&remote_major, &remote_minor, remote_version) != 3) { |
390 |
|
|
s = "Protocol mismatch.\n"; |
391 |
|
|
(void) atomicio(vwrite, sock_out, s, strlen(s)); |
392 |
|
|
logit("Bad protocol version identification '%.100s' " |
393 |
|
|
"from %s port %d", client_version_string, |
394 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
395 |
|
|
close(sock_in); |
396 |
|
|
close(sock_out); |
397 |
|
|
cleanup_exit(255); |
398 |
|
|
} |
399 |
|
|
debug("Client protocol version %d.%d; client software version %.100s", |
400 |
|
|
remote_major, remote_minor, remote_version); |
401 |
|
|
|
402 |
|
|
ssh->compat = compat_datafellows(remote_version); |
403 |
|
|
|
404 |
|
|
if ((ssh->compat & SSH_BUG_PROBE) != 0) { |
405 |
|
|
logit("probed from %s port %d with %s. Don't panic.", |
406 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), |
407 |
|
|
client_version_string); |
408 |
|
|
cleanup_exit(255); |
409 |
|
|
} |
410 |
|
|
if ((ssh->compat & SSH_BUG_SCANNER) != 0) { |
411 |
|
|
logit("scanned from %s port %d with %s. Don't panic.", |
412 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), |
413 |
|
|
client_version_string); |
414 |
|
|
cleanup_exit(255); |
415 |
|
|
} |
416 |
|
|
if ((ssh->compat & SSH_BUG_RSASIGMD5) != 0) { |
417 |
|
|
logit("Client version \"%.100s\" uses unsafe RSA signature " |
418 |
|
|
"scheme; disabling use of RSA keys", remote_version); |
419 |
|
|
} |
420 |
|
|
if ((ssh->compat & SSH_BUG_DERIVEKEY) != 0) { |
421 |
|
|
fatal("Client version \"%.100s\" uses unsafe key agreement; " |
422 |
|
|
"refusing connection", remote_version); |
423 |
|
|
} |
424 |
|
|
|
425 |
|
|
chop(server_version_string); |
426 |
|
|
debug("Local version string %.200s", server_version_string); |
427 |
|
|
|
428 |
|
|
if (remote_major != 2 || |
429 |
|
|
(remote_major == 1 && remote_minor != 99)) { |
430 |
|
|
s = "Protocol major versions differ.\n"; |
431 |
|
|
(void) atomicio(vwrite, sock_out, s, strlen(s)); |
432 |
|
|
close(sock_in); |
433 |
|
|
close(sock_out); |
434 |
|
|
logit("Protocol major versions differ for %s port %d: " |
435 |
|
|
"%.200s vs. %.200s", |
436 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), |
437 |
|
|
server_version_string, client_version_string); |
438 |
|
|
cleanup_exit(255); |
439 |
|
|
} |
440 |
|
|
} |
441 |
|
|
|
442 |
|
|
/* Destroy the host and server keys. They will no longer be needed. */ |
443 |
|
|
void |
444 |
|
|
destroy_sensitive_data(void) |
445 |
|
|
{ |
446 |
|
|
u_int i; |
447 |
|
|
|
448 |
|
|
for (i = 0; i < options.num_host_key_files; i++) { |
449 |
|
|
if (sensitive_data.host_keys[i]) { |
450 |
|
|
key_free(sensitive_data.host_keys[i]); |
451 |
|
|
sensitive_data.host_keys[i] = NULL; |
452 |
|
|
} |
453 |
|
|
if (sensitive_data.host_certificates[i]) { |
454 |
|
|
key_free(sensitive_data.host_certificates[i]); |
455 |
|
|
sensitive_data.host_certificates[i] = NULL; |
456 |
|
|
} |
457 |
|
|
} |
458 |
|
|
} |
459 |
|
|
|
460 |
|
|
/* Demote private to public keys for network child */ |
461 |
|
|
void |
462 |
|
|
demote_sensitive_data(void) |
463 |
|
|
{ |
464 |
|
|
struct sshkey *tmp; |
465 |
|
|
u_int i; |
466 |
|
|
|
467 |
|
|
for (i = 0; i < options.num_host_key_files; i++) { |
468 |
|
|
if (sensitive_data.host_keys[i]) { |
469 |
|
|
tmp = key_demote(sensitive_data.host_keys[i]); |
470 |
|
|
key_free(sensitive_data.host_keys[i]); |
471 |
|
|
sensitive_data.host_keys[i] = tmp; |
472 |
|
|
} |
473 |
|
|
/* Certs do not need demotion */ |
474 |
|
|
} |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
static void |
478 |
|
|
privsep_preauth_child(void) |
479 |
|
|
{ |
480 |
|
|
gid_t gidset[1]; |
481 |
|
|
struct passwd *pw; |
482 |
|
|
|
483 |
|
|
/* Enable challenge-response authentication for privilege separation */ |
484 |
|
|
privsep_challenge_enable(); |
485 |
|
|
|
486 |
|
|
#ifdef GSSAPI |
487 |
|
|
/* Cache supported mechanism OIDs for later use */ |
488 |
|
|
if (options.gss_authentication) |
489 |
|
|
ssh_gssapi_prepare_supported_oids(); |
490 |
|
|
#endif |
491 |
|
|
|
492 |
|
|
/* Demote the private keys to public keys. */ |
493 |
|
|
demote_sensitive_data(); |
494 |
|
|
|
495 |
|
|
/* Demote the child */ |
496 |
|
|
if (getuid() == 0 || geteuid() == 0) { |
497 |
|
|
if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) |
498 |
|
|
fatal("Privilege separation user %s does not exist", |
499 |
|
|
SSH_PRIVSEP_USER); |
500 |
|
|
explicit_bzero(pw->pw_passwd, strlen(pw->pw_passwd)); |
501 |
|
|
endpwent(); |
502 |
|
|
|
503 |
|
|
/* Change our root directory */ |
504 |
|
|
if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1) |
505 |
|
|
fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR, |
506 |
|
|
strerror(errno)); |
507 |
|
|
if (chdir("/") == -1) |
508 |
|
|
fatal("chdir(\"/\"): %s", strerror(errno)); |
509 |
|
|
|
510 |
|
|
/* |
511 |
|
|
* Drop our privileges |
512 |
|
|
* NB. Can't use setusercontext() after chroot. |
513 |
|
|
*/ |
514 |
|
|
debug3("privsep user:group %u:%u", (u_int)pw->pw_uid, |
515 |
|
|
(u_int)pw->pw_gid); |
516 |
|
|
gidset[0] = pw->pw_gid; |
517 |
|
|
if (setgroups(1, gidset) < 0) |
518 |
|
|
fatal("setgroups: %.100s", strerror(errno)); |
519 |
|
|
permanently_set_uid(pw); |
520 |
|
|
} |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
static int |
524 |
|
|
privsep_preauth(Authctxt *authctxt) |
525 |
|
|
{ |
526 |
|
|
int status, r; |
527 |
|
|
pid_t pid; |
528 |
|
|
struct ssh_sandbox *box = NULL; |
529 |
|
|
|
530 |
|
|
/* Set up unprivileged child process to deal with network data */ |
531 |
|
|
pmonitor = monitor_init(); |
532 |
|
|
/* Store a pointer to the kex for later rekeying */ |
533 |
|
|
pmonitor->m_pkex = &active_state->kex; |
534 |
|
|
|
535 |
|
|
if (use_privsep == PRIVSEP_ON) |
536 |
|
|
box = ssh_sandbox_init(); |
537 |
|
|
pid = fork(); |
538 |
|
|
if (pid == -1) { |
539 |
|
|
fatal("fork of unprivileged child failed"); |
540 |
|
|
} else if (pid != 0) { |
541 |
|
|
debug2("Network child is on pid %ld", (long)pid); |
542 |
|
|
|
543 |
|
|
pmonitor->m_pid = pid; |
544 |
|
|
if (have_agent) { |
545 |
|
|
r = ssh_get_authentication_socket(&auth_sock); |
546 |
|
|
if (r != 0) { |
547 |
|
|
error("Could not get agent socket: %s", |
548 |
|
|
ssh_err(r)); |
549 |
|
|
have_agent = 0; |
550 |
|
|
} |
551 |
|
|
} |
552 |
|
|
if (box != NULL) |
553 |
|
|
ssh_sandbox_parent_preauth(box, pid); |
554 |
|
|
monitor_child_preauth(authctxt, pmonitor); |
555 |
|
|
|
556 |
|
|
/* Wait for the child's exit status */ |
557 |
|
|
while (waitpid(pid, &status, 0) < 0) { |
558 |
|
|
if (errno == EINTR) |
559 |
|
|
continue; |
560 |
|
|
pmonitor->m_pid = -1; |
561 |
|
|
fatal("%s: waitpid: %s", __func__, strerror(errno)); |
562 |
|
|
} |
563 |
|
|
privsep_is_preauth = 0; |
564 |
|
|
pmonitor->m_pid = -1; |
565 |
|
|
if (WIFEXITED(status)) { |
566 |
|
|
if (WEXITSTATUS(status) != 0) |
567 |
|
|
fatal("%s: preauth child exited with status %d", |
568 |
|
|
__func__, WEXITSTATUS(status)); |
569 |
|
|
} else if (WIFSIGNALED(status)) |
570 |
|
|
fatal("%s: preauth child terminated by signal %d", |
571 |
|
|
__func__, WTERMSIG(status)); |
572 |
|
|
if (box != NULL) |
573 |
|
|
ssh_sandbox_parent_finish(box); |
574 |
|
|
return 1; |
575 |
|
|
} else { |
576 |
|
|
/* child */ |
577 |
|
|
close(pmonitor->m_sendfd); |
578 |
|
|
close(pmonitor->m_log_recvfd); |
579 |
|
|
|
580 |
|
|
/* Arrange for logging to be sent to the monitor */ |
581 |
|
|
set_log_handler(mm_log_handler, pmonitor); |
582 |
|
|
|
583 |
|
|
privsep_preauth_child(); |
584 |
|
|
setproctitle("%s", "[net]"); |
585 |
|
|
if (box != NULL) |
586 |
|
|
ssh_sandbox_child(box); |
587 |
|
|
|
588 |
|
|
return 0; |
589 |
|
|
} |
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
static void |
593 |
|
|
privsep_postauth(Authctxt *authctxt) |
594 |
|
|
{ |
595 |
|
|
if (authctxt->pw->pw_uid == 0) { |
596 |
|
|
/* File descriptor passing is broken or root login */ |
597 |
|
|
use_privsep = 0; |
598 |
|
|
goto skip; |
599 |
|
|
} |
600 |
|
|
|
601 |
|
|
/* New socket pair */ |
602 |
|
|
monitor_reinit(pmonitor); |
603 |
|
|
|
604 |
|
|
pmonitor->m_pid = fork(); |
605 |
|
|
if (pmonitor->m_pid == -1) |
606 |
|
|
fatal("fork of unprivileged child failed"); |
607 |
|
|
else if (pmonitor->m_pid != 0) { |
608 |
|
|
verbose("User child is on pid %ld", (long)pmonitor->m_pid); |
609 |
|
|
buffer_clear(&loginmsg); |
610 |
|
|
monitor_clear_keystate(pmonitor); |
611 |
|
|
monitor_child_postauth(pmonitor); |
612 |
|
|
|
613 |
|
|
/* NEVERREACHED */ |
614 |
|
|
exit(0); |
615 |
|
|
} |
616 |
|
|
|
617 |
|
|
/* child */ |
618 |
|
|
|
619 |
|
|
close(pmonitor->m_sendfd); |
620 |
|
|
pmonitor->m_sendfd = -1; |
621 |
|
|
|
622 |
|
|
/* Demote the private keys to public keys. */ |
623 |
|
|
demote_sensitive_data(); |
624 |
|
|
|
625 |
|
|
/* Drop privileges */ |
626 |
|
|
do_setusercontext(authctxt->pw); |
627 |
|
|
|
628 |
|
|
skip: |
629 |
|
|
/* It is safe now to apply the key state */ |
630 |
|
|
monitor_apply_keystate(pmonitor); |
631 |
|
|
|
632 |
|
|
/* |
633 |
|
|
* Tell the packet layer that authentication was successful, since |
634 |
|
|
* this information is not part of the key state. |
635 |
|
|
*/ |
636 |
|
|
packet_set_authenticated(); |
637 |
|
|
} |
638 |
|
|
|
639 |
|
|
static char * |
640 |
|
|
list_hostkey_types(void) |
641 |
|
|
{ |
642 |
|
|
Buffer b; |
643 |
|
|
const char *p; |
644 |
|
|
char *ret; |
645 |
|
|
u_int i; |
646 |
|
|
struct sshkey *key; |
647 |
|
|
|
648 |
|
|
buffer_init(&b); |
649 |
|
|
for (i = 0; i < options.num_host_key_files; i++) { |
650 |
|
|
key = sensitive_data.host_keys[i]; |
651 |
|
|
if (key == NULL) |
652 |
|
|
key = sensitive_data.host_pubkeys[i]; |
653 |
|
|
if (key == NULL) |
654 |
|
|
continue; |
655 |
|
|
/* Check that the key is accepted in HostkeyAlgorithms */ |
656 |
|
|
if (match_pattern_list(sshkey_ssh_name(key), |
657 |
|
|
options.hostkeyalgorithms, 0) != 1) { |
658 |
|
|
debug3("%s: %s key not permitted by HostkeyAlgorithms", |
659 |
|
|
__func__, sshkey_ssh_name(key)); |
660 |
|
|
continue; |
661 |
|
|
} |
662 |
|
|
switch (key->type) { |
663 |
|
|
case KEY_RSA: |
664 |
|
|
case KEY_DSA: |
665 |
|
|
case KEY_ECDSA: |
666 |
|
|
case KEY_ED25519: |
667 |
|
|
if (buffer_len(&b) > 0) |
668 |
|
|
buffer_append(&b, ",", 1); |
669 |
|
|
p = key_ssh_name(key); |
670 |
|
|
buffer_append(&b, p, strlen(p)); |
671 |
|
|
|
672 |
|
|
/* for RSA we also support SHA2 signatures */ |
673 |
|
|
if (key->type == KEY_RSA) { |
674 |
|
|
p = ",rsa-sha2-512,rsa-sha2-256"; |
675 |
|
|
buffer_append(&b, p, strlen(p)); |
676 |
|
|
} |
677 |
|
|
break; |
678 |
|
|
} |
679 |
|
|
/* If the private key has a cert peer, then list that too */ |
680 |
|
|
key = sensitive_data.host_certificates[i]; |
681 |
|
|
if (key == NULL) |
682 |
|
|
continue; |
683 |
|
|
switch (key->type) { |
684 |
|
|
case KEY_RSA_CERT: |
685 |
|
|
case KEY_DSA_CERT: |
686 |
|
|
case KEY_ECDSA_CERT: |
687 |
|
|
case KEY_ED25519_CERT: |
688 |
|
|
if (buffer_len(&b) > 0) |
689 |
|
|
buffer_append(&b, ",", 1); |
690 |
|
|
p = key_ssh_name(key); |
691 |
|
|
buffer_append(&b, p, strlen(p)); |
692 |
|
|
break; |
693 |
|
|
} |
694 |
|
|
} |
695 |
|
|
if ((ret = sshbuf_dup_string(&b)) == NULL) |
696 |
|
|
fatal("%s: sshbuf_dup_string failed", __func__); |
697 |
|
|
buffer_free(&b); |
698 |
|
|
debug("list_hostkey_types: %s", ret); |
699 |
|
|
return ret; |
700 |
|
|
} |
701 |
|
|
|
702 |
|
|
static struct sshkey * |
703 |
|
|
get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh) |
704 |
|
|
{ |
705 |
|
|
u_int i; |
706 |
|
|
struct sshkey *key; |
707 |
|
|
|
708 |
|
|
for (i = 0; i < options.num_host_key_files; i++) { |
709 |
|
|
switch (type) { |
710 |
|
|
case KEY_RSA_CERT: |
711 |
|
|
case KEY_DSA_CERT: |
712 |
|
|
case KEY_ECDSA_CERT: |
713 |
|
|
case KEY_ED25519_CERT: |
714 |
|
|
key = sensitive_data.host_certificates[i]; |
715 |
|
|
break; |
716 |
|
|
default: |
717 |
|
|
key = sensitive_data.host_keys[i]; |
718 |
|
|
if (key == NULL && !need_private) |
719 |
|
|
key = sensitive_data.host_pubkeys[i]; |
720 |
|
|
break; |
721 |
|
|
} |
722 |
|
|
if (key != NULL && key->type == type && |
723 |
|
|
(key->type != KEY_ECDSA || key->ecdsa_nid == nid)) |
724 |
|
|
return need_private ? |
725 |
|
|
sensitive_data.host_keys[i] : key; |
726 |
|
|
} |
727 |
|
|
return NULL; |
728 |
|
|
} |
729 |
|
|
|
730 |
|
|
struct sshkey * |
731 |
|
|
get_hostkey_public_by_type(int type, int nid, struct ssh *ssh) |
732 |
|
|
{ |
733 |
|
|
return get_hostkey_by_type(type, nid, 0, ssh); |
734 |
|
|
} |
735 |
|
|
|
736 |
|
|
struct sshkey * |
737 |
|
|
get_hostkey_private_by_type(int type, int nid, struct ssh *ssh) |
738 |
|
|
{ |
739 |
|
|
return get_hostkey_by_type(type, nid, 1, ssh); |
740 |
|
|
} |
741 |
|
|
|
742 |
|
|
struct sshkey * |
743 |
|
|
get_hostkey_by_index(int ind) |
744 |
|
|
{ |
745 |
|
|
if (ind < 0 || (u_int)ind >= options.num_host_key_files) |
746 |
|
|
return (NULL); |
747 |
|
|
return (sensitive_data.host_keys[ind]); |
748 |
|
|
} |
749 |
|
|
|
750 |
|
|
struct sshkey * |
751 |
|
|
get_hostkey_public_by_index(int ind, struct ssh *ssh) |
752 |
|
|
{ |
753 |
|
|
if (ind < 0 || (u_int)ind >= options.num_host_key_files) |
754 |
|
|
return (NULL); |
755 |
|
|
return (sensitive_data.host_pubkeys[ind]); |
756 |
|
|
} |
757 |
|
|
|
758 |
|
|
int |
759 |
|
|
get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh) |
760 |
|
|
{ |
761 |
|
|
u_int i; |
762 |
|
|
|
763 |
|
|
for (i = 0; i < options.num_host_key_files; i++) { |
764 |
|
|
if (key_is_cert(key)) { |
765 |
|
|
if (key == sensitive_data.host_certificates[i] || |
766 |
|
|
(compare && sensitive_data.host_certificates[i] && |
767 |
|
|
sshkey_equal(key, |
768 |
|
|
sensitive_data.host_certificates[i]))) |
769 |
|
|
return (i); |
770 |
|
|
} else { |
771 |
|
|
if (key == sensitive_data.host_keys[i] || |
772 |
|
|
(compare && sensitive_data.host_keys[i] && |
773 |
|
|
sshkey_equal(key, sensitive_data.host_keys[i]))) |
774 |
|
|
return (i); |
775 |
|
|
if (key == sensitive_data.host_pubkeys[i] || |
776 |
|
|
(compare && sensitive_data.host_pubkeys[i] && |
777 |
|
|
sshkey_equal(key, sensitive_data.host_pubkeys[i]))) |
778 |
|
|
return (i); |
779 |
|
|
} |
780 |
|
|
} |
781 |
|
|
return (-1); |
782 |
|
|
} |
783 |
|
|
|
784 |
|
|
/* Inform the client of all hostkeys */ |
785 |
|
|
static void |
786 |
|
|
notify_hostkeys(struct ssh *ssh) |
787 |
|
|
{ |
788 |
|
|
struct sshbuf *buf; |
789 |
|
|
struct sshkey *key; |
790 |
|
|
u_int i, nkeys; |
791 |
|
|
int r; |
792 |
|
|
char *fp; |
793 |
|
|
|
794 |
|
|
/* Some clients cannot cope with the hostkeys message, skip those. */ |
795 |
|
|
if (datafellows & SSH_BUG_HOSTKEYS) |
796 |
|
|
return; |
797 |
|
|
|
798 |
|
|
if ((buf = sshbuf_new()) == NULL) |
799 |
|
|
fatal("%s: sshbuf_new", __func__); |
800 |
|
|
for (i = nkeys = 0; i < options.num_host_key_files; i++) { |
801 |
|
|
key = get_hostkey_public_by_index(i, ssh); |
802 |
|
|
if (key == NULL || key->type == KEY_UNSPEC || |
803 |
|
|
sshkey_is_cert(key)) |
804 |
|
|
continue; |
805 |
|
|
fp = sshkey_fingerprint(key, options.fingerprint_hash, |
806 |
|
|
SSH_FP_DEFAULT); |
807 |
|
|
debug3("%s: key %d: %s %s", __func__, i, |
808 |
|
|
sshkey_ssh_name(key), fp); |
809 |
|
|
free(fp); |
810 |
|
|
if (nkeys == 0) { |
811 |
|
|
packet_start(SSH2_MSG_GLOBAL_REQUEST); |
812 |
|
|
packet_put_cstring("hostkeys-00@openssh.com"); |
813 |
|
|
packet_put_char(0); /* want-reply */ |
814 |
|
|
} |
815 |
|
|
sshbuf_reset(buf); |
816 |
|
|
if ((r = sshkey_putb(key, buf)) != 0) |
817 |
|
|
fatal("%s: couldn't put hostkey %d: %s", |
818 |
|
|
__func__, i, ssh_err(r)); |
819 |
|
|
packet_put_string(sshbuf_ptr(buf), sshbuf_len(buf)); |
820 |
|
|
nkeys++; |
821 |
|
|
} |
822 |
|
|
debug3("%s: sent %u hostkeys", __func__, nkeys); |
823 |
|
|
if (nkeys == 0) |
824 |
|
|
fatal("%s: no hostkeys", __func__); |
825 |
|
|
packet_send(); |
826 |
|
|
sshbuf_free(buf); |
827 |
|
|
} |
828 |
|
|
|
829 |
|
|
/* |
830 |
|
|
* returns 1 if connection should be dropped, 0 otherwise. |
831 |
|
|
* dropping starts at connection #max_startups_begin with a probability |
832 |
|
|
* of (max_startups_rate/100). the probability increases linearly until |
833 |
|
|
* all connections are dropped for startups > max_startups |
834 |
|
|
*/ |
835 |
|
|
static int |
836 |
|
|
drop_connection(int startups) |
837 |
|
|
{ |
838 |
|
|
int p, r; |
839 |
|
|
|
840 |
|
|
if (startups < options.max_startups_begin) |
841 |
|
|
return 0; |
842 |
|
|
if (startups >= options.max_startups) |
843 |
|
|
return 1; |
844 |
|
|
if (options.max_startups_rate == 100) |
845 |
|
|
return 1; |
846 |
|
|
|
847 |
|
|
p = 100 - options.max_startups_rate; |
848 |
|
|
p *= startups - options.max_startups_begin; |
849 |
|
|
p /= options.max_startups - options.max_startups_begin; |
850 |
|
|
p += options.max_startups_rate; |
851 |
|
|
r = arc4random_uniform(100); |
852 |
|
|
|
853 |
|
|
debug("drop_connection: p %d, r %d", p, r); |
854 |
|
|
return (r < p) ? 1 : 0; |
855 |
|
|
} |
856 |
|
|
|
857 |
|
|
static void |
858 |
|
|
usage(void) |
859 |
|
|
{ |
860 |
|
|
fprintf(stderr, "%s, %s\n", |
861 |
|
|
SSH_VERSION, |
862 |
|
|
#ifdef WITH_OPENSSL |
863 |
|
|
SSLeay_version(SSLEAY_VERSION) |
864 |
|
|
#else |
865 |
|
|
"without OpenSSL" |
866 |
|
|
#endif |
867 |
|
|
); |
868 |
|
|
fprintf(stderr, |
869 |
|
|
"usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]\n" |
870 |
|
|
" [-E log_file] [-f config_file] [-g login_grace_time]\n" |
871 |
|
|
" [-h host_key_file] [-o option] [-p port] [-u len]\n" |
872 |
|
|
); |
873 |
|
|
exit(1); |
874 |
|
|
} |
875 |
|
|
|
876 |
|
|
static void |
877 |
|
|
send_rexec_state(int fd, struct sshbuf *conf) |
878 |
|
|
{ |
879 |
|
|
struct sshbuf *m; |
880 |
|
|
int r; |
881 |
|
|
|
882 |
|
|
debug3("%s: entering fd = %d config len %zu", __func__, fd, |
883 |
|
|
sshbuf_len(conf)); |
884 |
|
|
|
885 |
|
|
/* |
886 |
|
|
* Protocol from reexec master to child: |
887 |
|
|
* string configuration |
888 |
|
|
*/ |
889 |
|
|
if ((m = sshbuf_new()) == NULL) |
890 |
|
|
fatal("%s: sshbuf_new failed", __func__); |
891 |
|
|
if ((r = sshbuf_put_stringb(m, conf)) != 0) |
892 |
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
893 |
|
|
if (ssh_msg_send(fd, 0, m) == -1) |
894 |
|
|
fatal("%s: ssh_msg_send failed", __func__); |
895 |
|
|
|
896 |
|
|
sshbuf_free(m); |
897 |
|
|
|
898 |
|
|
debug3("%s: done", __func__); |
899 |
|
|
} |
900 |
|
|
|
901 |
|
|
static void |
902 |
|
|
recv_rexec_state(int fd, Buffer *conf) |
903 |
|
|
{ |
904 |
|
|
Buffer m; |
905 |
|
|
char *cp; |
906 |
|
|
u_int len; |
907 |
|
|
|
908 |
|
|
debug3("%s: entering fd = %d", __func__, fd); |
909 |
|
|
|
910 |
|
|
buffer_init(&m); |
911 |
|
|
|
912 |
|
|
if (ssh_msg_recv(fd, &m) == -1) |
913 |
|
|
fatal("%s: ssh_msg_recv failed", __func__); |
914 |
|
|
if (buffer_get_char(&m) != 0) |
915 |
|
|
fatal("%s: rexec version mismatch", __func__); |
916 |
|
|
|
917 |
|
|
cp = buffer_get_string(&m, &len); |
918 |
|
|
if (conf != NULL) |
919 |
|
|
buffer_append(conf, cp, len); |
920 |
|
|
free(cp); |
921 |
|
|
|
922 |
|
|
buffer_free(&m); |
923 |
|
|
|
924 |
|
|
debug3("%s: done", __func__); |
925 |
|
|
} |
926 |
|
|
|
927 |
|
|
/* Accept a connection from inetd */ |
928 |
|
|
static void |
929 |
|
|
server_accept_inetd(int *sock_in, int *sock_out) |
930 |
|
|
{ |
931 |
|
|
int fd; |
932 |
|
|
|
933 |
|
|
startup_pipe = -1; |
934 |
|
|
if (rexeced_flag) { |
935 |
|
|
close(REEXEC_CONFIG_PASS_FD); |
936 |
|
|
*sock_in = *sock_out = dup(STDIN_FILENO); |
937 |
|
|
if (!debug_flag) { |
938 |
|
|
startup_pipe = dup(REEXEC_STARTUP_PIPE_FD); |
939 |
|
|
close(REEXEC_STARTUP_PIPE_FD); |
940 |
|
|
} |
941 |
|
|
} else { |
942 |
|
|
*sock_in = dup(STDIN_FILENO); |
943 |
|
|
*sock_out = dup(STDOUT_FILENO); |
944 |
|
|
} |
945 |
|
|
/* |
946 |
|
|
* We intentionally do not close the descriptors 0, 1, and 2 |
947 |
|
|
* as our code for setting the descriptors won't work if |
948 |
|
|
* ttyfd happens to be one of those. |
949 |
|
|
*/ |
950 |
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { |
951 |
|
|
dup2(fd, STDIN_FILENO); |
952 |
|
|
dup2(fd, STDOUT_FILENO); |
953 |
|
|
if (!log_stderr) |
954 |
|
|
dup2(fd, STDERR_FILENO); |
955 |
|
|
if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO)) |
956 |
|
|
close(fd); |
957 |
|
|
} |
958 |
|
|
debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out); |
959 |
|
|
} |
960 |
|
|
|
961 |
|
|
/* |
962 |
|
|
* Listen for TCP connections |
963 |
|
|
*/ |
964 |
|
|
static void |
965 |
|
|
listen_on_addrs(struct listenaddr *la) |
966 |
|
|
{ |
967 |
|
|
int ret, listen_sock; |
968 |
|
|
struct addrinfo *ai; |
969 |
|
|
char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
970 |
|
|
|
971 |
|
|
for (ai = la->addrs; ai; ai = ai->ai_next) { |
972 |
|
|
if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
973 |
|
|
continue; |
974 |
|
|
if (num_listen_socks >= MAX_LISTEN_SOCKS) |
975 |
|
|
fatal("Too many listen sockets. " |
976 |
|
|
"Enlarge MAX_LISTEN_SOCKS"); |
977 |
|
|
if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, |
978 |
|
|
ntop, sizeof(ntop), strport, sizeof(strport), |
979 |
|
|
NI_NUMERICHOST|NI_NUMERICSERV)) != 0) { |
980 |
|
|
error("getnameinfo failed: %.100s", |
981 |
|
|
ssh_gai_strerror(ret)); |
982 |
|
|
continue; |
983 |
|
|
} |
984 |
|
|
/* Create socket for listening. */ |
985 |
|
|
listen_sock = socket(ai->ai_family, ai->ai_socktype, |
986 |
|
|
ai->ai_protocol); |
987 |
|
|
if (listen_sock < 0) { |
988 |
|
|
/* kernel may not support ipv6 */ |
989 |
|
|
verbose("socket: %.100s", strerror(errno)); |
990 |
|
|
continue; |
991 |
|
|
} |
992 |
|
|
if (set_nonblock(listen_sock) == -1) { |
993 |
|
|
close(listen_sock); |
994 |
|
|
continue; |
995 |
|
|
} |
996 |
|
|
if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) == -1) { |
997 |
|
|
verbose("socket: CLOEXEC: %s", strerror(errno)); |
998 |
|
|
close(listen_sock); |
999 |
|
|
continue; |
1000 |
|
|
} |
1001 |
|
|
/* Socket options */ |
1002 |
|
|
set_reuseaddr(listen_sock); |
1003 |
|
|
if (la->rdomain != NULL && |
1004 |
|
|
set_rdomain(listen_sock, la->rdomain) == -1) { |
1005 |
|
|
close(listen_sock); |
1006 |
|
|
continue; |
1007 |
|
|
} |
1008 |
|
|
|
1009 |
|
|
debug("Bind to port %s on %s.", strport, ntop); |
1010 |
|
|
|
1011 |
|
|
/* Bind the socket to the desired port. */ |
1012 |
|
|
if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
1013 |
|
|
error("Bind to port %s on %s failed: %.200s.", |
1014 |
|
|
strport, ntop, strerror(errno)); |
1015 |
|
|
close(listen_sock); |
1016 |
|
|
continue; |
1017 |
|
|
} |
1018 |
|
|
listen_socks[num_listen_socks] = listen_sock; |
1019 |
|
|
num_listen_socks++; |
1020 |
|
|
|
1021 |
|
|
/* Start listening on the port. */ |
1022 |
|
|
if (listen(listen_sock, SSH_LISTEN_BACKLOG) < 0) |
1023 |
|
|
fatal("listen on [%s]:%s: %.100s", |
1024 |
|
|
ntop, strport, strerror(errno)); |
1025 |
|
|
logit("Server listening on %s port %s%s%s.", |
1026 |
|
|
ntop, strport, |
1027 |
|
|
la->rdomain == NULL ? "" : " rdomain ", |
1028 |
|
|
la->rdomain == NULL ? "" : la->rdomain); |
1029 |
|
|
} |
1030 |
|
|
} |
1031 |
|
|
|
1032 |
|
|
static void |
1033 |
|
|
server_listen(void) |
1034 |
|
|
{ |
1035 |
|
|
u_int i; |
1036 |
|
|
|
1037 |
|
|
for (i = 0; i < options.num_listen_addrs; i++) { |
1038 |
|
|
listen_on_addrs(&options.listen_addrs[i]); |
1039 |
|
|
freeaddrinfo(options.listen_addrs[i].addrs); |
1040 |
|
|
free(options.listen_addrs[i].rdomain); |
1041 |
|
|
memset(&options.listen_addrs[i], 0, |
1042 |
|
|
sizeof(options.listen_addrs[i])); |
1043 |
|
|
} |
1044 |
|
|
free(options.listen_addrs); |
1045 |
|
|
options.listen_addrs = NULL; |
1046 |
|
|
options.num_listen_addrs = 0; |
1047 |
|
|
|
1048 |
|
|
if (!num_listen_socks) |
1049 |
|
|
fatal("Cannot bind any address."); |
1050 |
|
|
} |
1051 |
|
|
|
1052 |
|
|
/* |
1053 |
|
|
* The main TCP accept loop. Note that, for the non-debug case, returns |
1054 |
|
|
* from this function are in a forked subprocess. |
1055 |
|
|
*/ |
1056 |
|
|
static void |
1057 |
|
|
server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s) |
1058 |
|
|
{ |
1059 |
|
|
fd_set *fdset; |
1060 |
|
|
int i, j, ret, maxfd; |
1061 |
|
|
int startups = 0; |
1062 |
|
|
int startup_p[2] = { -1 , -1 }; |
1063 |
|
|
struct sockaddr_storage from; |
1064 |
|
|
socklen_t fromlen; |
1065 |
|
|
pid_t pid; |
1066 |
|
|
|
1067 |
|
|
/* setup fd set for accept */ |
1068 |
|
|
fdset = NULL; |
1069 |
|
|
maxfd = 0; |
1070 |
|
|
for (i = 0; i < num_listen_socks; i++) |
1071 |
|
|
if (listen_socks[i] > maxfd) |
1072 |
|
|
maxfd = listen_socks[i]; |
1073 |
|
|
/* pipes connected to unauthenticated childs */ |
1074 |
|
|
startup_pipes = xcalloc(options.max_startups, sizeof(int)); |
1075 |
|
|
for (i = 0; i < options.max_startups; i++) |
1076 |
|
|
startup_pipes[i] = -1; |
1077 |
|
|
|
1078 |
|
|
/* |
1079 |
|
|
* Stay listening for connections until the system crashes or |
1080 |
|
|
* the daemon is killed with a signal. |
1081 |
|
|
*/ |
1082 |
|
|
for (;;) { |
1083 |
|
|
if (received_sighup) |
1084 |
|
|
sighup_restart(); |
1085 |
|
|
free(fdset); |
1086 |
|
|
fdset = xcalloc(howmany(maxfd + 1, NFDBITS), |
1087 |
|
|
sizeof(fd_mask)); |
1088 |
|
|
|
1089 |
|
|
for (i = 0; i < num_listen_socks; i++) |
1090 |
|
|
FD_SET(listen_socks[i], fdset); |
1091 |
|
|
for (i = 0; i < options.max_startups; i++) |
1092 |
|
|
if (startup_pipes[i] != -1) |
1093 |
|
|
FD_SET(startup_pipes[i], fdset); |
1094 |
|
|
|
1095 |
|
|
/* Wait in select until there is a connection. */ |
1096 |
|
|
ret = select(maxfd+1, fdset, NULL, NULL, NULL); |
1097 |
|
|
if (ret < 0 && errno != EINTR) |
1098 |
|
|
error("select: %.100s", strerror(errno)); |
1099 |
|
|
if (received_sigterm) { |
1100 |
|
|
logit("Received signal %d; terminating.", |
1101 |
|
|
(int) received_sigterm); |
1102 |
|
|
close_listen_socks(); |
1103 |
|
|
if (options.pid_file != NULL) |
1104 |
|
|
unlink(options.pid_file); |
1105 |
|
|
exit(received_sigterm == SIGTERM ? 0 : 255); |
1106 |
|
|
} |
1107 |
|
|
if (ret < 0) |
1108 |
|
|
continue; |
1109 |
|
|
|
1110 |
|
|
for (i = 0; i < options.max_startups; i++) |
1111 |
|
|
if (startup_pipes[i] != -1 && |
1112 |
|
|
FD_ISSET(startup_pipes[i], fdset)) { |
1113 |
|
|
/* |
1114 |
|
|
* the read end of the pipe is ready |
1115 |
|
|
* if the child has closed the pipe |
1116 |
|
|
* after successful authentication |
1117 |
|
|
* or if the child has died |
1118 |
|
|
*/ |
1119 |
|
|
close(startup_pipes[i]); |
1120 |
|
|
startup_pipes[i] = -1; |
1121 |
|
|
startups--; |
1122 |
|
|
} |
1123 |
|
|
for (i = 0; i < num_listen_socks; i++) { |
1124 |
|
|
if (!FD_ISSET(listen_socks[i], fdset)) |
1125 |
|
|
continue; |
1126 |
|
|
fromlen = sizeof(from); |
1127 |
|
|
*newsock = accept(listen_socks[i], |
1128 |
|
|
(struct sockaddr *)&from, &fromlen); |
1129 |
|
|
if (*newsock < 0) { |
1130 |
|
|
if (errno != EINTR && errno != EWOULDBLOCK && |
1131 |
|
|
errno != ECONNABORTED) |
1132 |
|
|
error("accept: %.100s", |
1133 |
|
|
strerror(errno)); |
1134 |
|
|
if (errno == EMFILE || errno == ENFILE) |
1135 |
|
|
usleep(100 * 1000); |
1136 |
|
|
continue; |
1137 |
|
|
} |
1138 |
|
|
if (unset_nonblock(*newsock) == -1) { |
1139 |
|
|
close(*newsock); |
1140 |
|
|
continue; |
1141 |
|
|
} |
1142 |
|
|
if (drop_connection(startups) == 1) { |
1143 |
|
|
char *laddr = get_local_ipaddr(*newsock); |
1144 |
|
|
char *raddr = get_peer_ipaddr(*newsock); |
1145 |
|
|
|
1146 |
|
|
verbose("drop connection #%d from [%s]:%d " |
1147 |
|
|
"on [%s]:%d past MaxStartups", startups, |
1148 |
|
|
raddr, get_peer_port(*newsock), |
1149 |
|
|
laddr, get_local_port(*newsock)); |
1150 |
|
|
free(laddr); |
1151 |
|
|
free(raddr); |
1152 |
|
|
close(*newsock); |
1153 |
|
|
continue; |
1154 |
|
|
} |
1155 |
|
|
if (pipe(startup_p) == -1) { |
1156 |
|
|
close(*newsock); |
1157 |
|
|
continue; |
1158 |
|
|
} |
1159 |
|
|
|
1160 |
|
|
if (rexec_flag && socketpair(AF_UNIX, |
1161 |
|
|
SOCK_STREAM, 0, config_s) == -1) { |
1162 |
|
|
error("reexec socketpair: %s", |
1163 |
|
|
strerror(errno)); |
1164 |
|
|
close(*newsock); |
1165 |
|
|
close(startup_p[0]); |
1166 |
|
|
close(startup_p[1]); |
1167 |
|
|
continue; |
1168 |
|
|
} |
1169 |
|
|
|
1170 |
|
|
for (j = 0; j < options.max_startups; j++) |
1171 |
|
|
if (startup_pipes[j] == -1) { |
1172 |
|
|
startup_pipes[j] = startup_p[0]; |
1173 |
|
|
if (maxfd < startup_p[0]) |
1174 |
|
|
maxfd = startup_p[0]; |
1175 |
|
|
startups++; |
1176 |
|
|
break; |
1177 |
|
|
} |
1178 |
|
|
|
1179 |
|
|
/* |
1180 |
|
|
* Got connection. Fork a child to handle it, unless |
1181 |
|
|
* we are in debugging mode. |
1182 |
|
|
*/ |
1183 |
|
|
if (debug_flag) { |
1184 |
|
|
/* |
1185 |
|
|
* In debugging mode. Close the listening |
1186 |
|
|
* socket, and start processing the |
1187 |
|
|
* connection without forking. |
1188 |
|
|
*/ |
1189 |
|
|
debug("Server will not fork when running in debugging mode."); |
1190 |
|
|
close_listen_socks(); |
1191 |
|
|
*sock_in = *newsock; |
1192 |
|
|
*sock_out = *newsock; |
1193 |
|
|
close(startup_p[0]); |
1194 |
|
|
close(startup_p[1]); |
1195 |
|
|
startup_pipe = -1; |
1196 |
|
|
pid = getpid(); |
1197 |
|
|
if (rexec_flag) { |
1198 |
|
|
send_rexec_state(config_s[0], |
1199 |
|
|
&cfg); |
1200 |
|
|
close(config_s[0]); |
1201 |
|
|
} |
1202 |
|
|
break; |
1203 |
|
|
} |
1204 |
|
|
|
1205 |
|
|
/* |
1206 |
|
|
* Normal production daemon. Fork, and have |
1207 |
|
|
* the child process the connection. The |
1208 |
|
|
* parent continues listening. |
1209 |
|
|
*/ |
1210 |
|
|
if ((pid = fork()) == 0) { |
1211 |
|
|
/* |
1212 |
|
|
* Child. Close the listening and |
1213 |
|
|
* max_startup sockets. Start using |
1214 |
|
|
* the accepted socket. Reinitialize |
1215 |
|
|
* logging (since our pid has changed). |
1216 |
|
|
* We break out of the loop to handle |
1217 |
|
|
* the connection. |
1218 |
|
|
*/ |
1219 |
|
|
startup_pipe = startup_p[1]; |
1220 |
|
|
close_startup_pipes(); |
1221 |
|
|
close_listen_socks(); |
1222 |
|
|
*sock_in = *newsock; |
1223 |
|
|
*sock_out = *newsock; |
1224 |
|
|
log_init(__progname, |
1225 |
|
|
options.log_level, |
1226 |
|
|
options.log_facility, |
1227 |
|
|
log_stderr); |
1228 |
|
|
if (rexec_flag) |
1229 |
|
|
close(config_s[0]); |
1230 |
|
|
break; |
1231 |
|
|
} |
1232 |
|
|
|
1233 |
|
|
/* Parent. Stay in the loop. */ |
1234 |
|
|
if (pid < 0) |
1235 |
|
|
error("fork: %.100s", strerror(errno)); |
1236 |
|
|
else |
1237 |
|
|
debug("Forked child %ld.", (long)pid); |
1238 |
|
|
|
1239 |
|
|
close(startup_p[1]); |
1240 |
|
|
|
1241 |
|
|
if (rexec_flag) { |
1242 |
|
|
send_rexec_state(config_s[0], &cfg); |
1243 |
|
|
close(config_s[0]); |
1244 |
|
|
close(config_s[1]); |
1245 |
|
|
} |
1246 |
|
|
close(*newsock); |
1247 |
|
|
} |
1248 |
|
|
|
1249 |
|
|
/* child process check (or debug mode) */ |
1250 |
|
|
if (num_listen_socks < 0) |
1251 |
|
|
break; |
1252 |
|
|
} |
1253 |
|
|
} |
1254 |
|
|
|
1255 |
|
|
/* |
1256 |
|
|
* If IP options are supported, make sure there are none (log and |
1257 |
|
|
* return an error if any are found). Basically we are worried about |
1258 |
|
|
* source routing; it can be used to pretend you are somebody |
1259 |
|
|
* (ip-address) you are not. That itself may be "almost acceptable" |
1260 |
|
|
* under certain circumstances, but rhosts autentication is useless |
1261 |
|
|
* if source routing is accepted. Notice also that if we just dropped |
1262 |
|
|
* source routing here, the other side could use IP spoofing to do |
1263 |
|
|
* rest of the interaction and could still bypass security. So we |
1264 |
|
|
* exit here if we detect any IP options. |
1265 |
|
|
*/ |
1266 |
|
|
static void |
1267 |
|
|
check_ip_options(struct ssh *ssh) |
1268 |
|
|
{ |
1269 |
|
|
int sock_in = ssh_packet_get_connection_in(ssh); |
1270 |
|
|
struct sockaddr_storage from; |
1271 |
|
|
u_char opts[200]; |
1272 |
|
|
socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from); |
1273 |
|
|
char text[sizeof(opts) * 3 + 1]; |
1274 |
|
|
|
1275 |
|
|
memset(&from, 0, sizeof(from)); |
1276 |
|
|
if (getpeername(sock_in, (struct sockaddr *)&from, |
1277 |
|
|
&fromlen) < 0) |
1278 |
|
|
return; |
1279 |
|
|
if (from.ss_family != AF_INET) |
1280 |
|
|
return; |
1281 |
|
|
/* XXX IPv6 options? */ |
1282 |
|
|
|
1283 |
|
|
if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts, |
1284 |
|
|
&option_size) >= 0 && option_size != 0) { |
1285 |
|
|
text[0] = '\0'; |
1286 |
|
|
for (i = 0; i < option_size; i++) |
1287 |
|
|
snprintf(text + i*3, sizeof(text) - i*3, |
1288 |
|
|
" %2.2x", opts[i]); |
1289 |
|
|
fatal("Connection from %.100s port %d with IP opts: %.800s", |
1290 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text); |
1291 |
|
|
} |
1292 |
|
|
return; |
1293 |
|
|
} |
1294 |
|
|
|
1295 |
|
|
/* Set the routing domain for this process */ |
1296 |
|
|
static void |
1297 |
|
|
set_process_rdomain(struct ssh *ssh, const char *name) |
1298 |
|
|
{ |
1299 |
|
|
int rtable, ortable = getrtable(); |
1300 |
|
|
const char *errstr; |
1301 |
|
|
|
1302 |
|
|
if (name == NULL) |
1303 |
|
|
return; /* default */ |
1304 |
|
|
|
1305 |
|
|
if (strcmp(name, "%D") == 0) { |
1306 |
|
|
/* "expands" to routing domain of connection */ |
1307 |
|
|
if ((name = ssh_packet_rdomain_in(ssh)) == NULL) |
1308 |
|
|
return; |
1309 |
|
|
} |
1310 |
|
|
|
1311 |
|
|
rtable = (int)strtonum(name, 0, 255, &errstr); |
1312 |
|
|
if (errstr != NULL) /* Shouldn't happen */ |
1313 |
|
|
fatal("Invalid routing domain \"%s\": %s", name, errstr); |
1314 |
|
|
if (rtable != ortable && setrtable(rtable) != 0) |
1315 |
|
|
fatal("Unable to set routing domain %d: %s", |
1316 |
|
|
rtable, strerror(errno)); |
1317 |
|
|
debug("%s: set routing domain %d (was %d)", __func__, rtable, ortable); |
1318 |
|
|
} |
1319 |
|
|
|
1320 |
|
|
/* |
1321 |
|
|
* Main program for the daemon. |
1322 |
|
|
*/ |
1323 |
|
|
int |
1324 |
|
|
main(int ac, char **av) |
1325 |
|
|
{ |
1326 |
|
|
struct ssh *ssh = NULL; |
1327 |
|
|
extern char *optarg; |
1328 |
|
|
extern int optind; |
1329 |
|
|
int r, opt, on = 1, already_daemon, remote_port; |
1330 |
|
|
int sock_in = -1, sock_out = -1, newsock = -1; |
1331 |
|
|
const char *remote_ip, *rdomain; |
1332 |
|
|
char *fp, *line, *laddr, *logfile = NULL; |
1333 |
|
|
int config_s[2] = { -1 , -1 }; |
1334 |
|
|
u_int i, j; |
1335 |
|
|
u_int64_t ibytes, obytes; |
1336 |
|
|
mode_t new_umask; |
1337 |
|
|
struct sshkey *key; |
1338 |
|
|
struct sshkey *pubkey; |
1339 |
|
|
int keytype; |
1340 |
|
|
Authctxt *authctxt; |
1341 |
|
|
struct connection_info *connection_info = NULL; |
1342 |
|
|
|
1343 |
|
|
ssh_malloc_init(); /* must be called before any mallocs */ |
1344 |
|
|
/* Save argv. */ |
1345 |
|
|
saved_argv = av; |
1346 |
|
|
rexec_argc = ac; |
1347 |
|
|
|
1348 |
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
1349 |
|
|
sanitise_stdfd(); |
1350 |
|
|
|
1351 |
|
|
/* Initialize configuration options to their default values. */ |
1352 |
|
|
initialize_server_options(&options); |
1353 |
|
|
|
1354 |
|
|
/* Parse command-line arguments. */ |
1355 |
|
|
while ((opt = getopt(ac, av, |
1356 |
|
|
"C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) { |
1357 |
|
|
switch (opt) { |
1358 |
|
|
case '4': |
1359 |
|
|
options.address_family = AF_INET; |
1360 |
|
|
break; |
1361 |
|
|
case '6': |
1362 |
|
|
options.address_family = AF_INET6; |
1363 |
|
|
break; |
1364 |
|
|
case 'f': |
1365 |
|
|
config_file_name = optarg; |
1366 |
|
|
break; |
1367 |
|
|
case 'c': |
1368 |
|
|
servconf_add_hostcert("[command-line]", 0, |
1369 |
|
|
&options, optarg); |
1370 |
|
|
break; |
1371 |
|
|
case 'd': |
1372 |
|
|
if (debug_flag == 0) { |
1373 |
|
|
debug_flag = 1; |
1374 |
|
|
options.log_level = SYSLOG_LEVEL_DEBUG1; |
1375 |
|
|
} else if (options.log_level < SYSLOG_LEVEL_DEBUG3) |
1376 |
|
|
options.log_level++; |
1377 |
|
|
break; |
1378 |
|
|
case 'D': |
1379 |
|
|
no_daemon_flag = 1; |
1380 |
|
|
break; |
1381 |
|
|
case 'E': |
1382 |
|
|
logfile = optarg; |
1383 |
|
|
/* FALLTHROUGH */ |
1384 |
|
|
case 'e': |
1385 |
|
|
log_stderr = 1; |
1386 |
|
|
break; |
1387 |
|
|
case 'i': |
1388 |
|
|
inetd_flag = 1; |
1389 |
|
|
break; |
1390 |
|
|
case 'r': |
1391 |
|
|
rexec_flag = 0; |
1392 |
|
|
break; |
1393 |
|
|
case 'R': |
1394 |
|
|
rexeced_flag = 1; |
1395 |
|
|
inetd_flag = 1; |
1396 |
|
|
break; |
1397 |
|
|
case 'Q': |
1398 |
|
|
/* ignored */ |
1399 |
|
|
break; |
1400 |
|
|
case 'q': |
1401 |
|
|
options.log_level = SYSLOG_LEVEL_QUIET; |
1402 |
|
|
break; |
1403 |
|
|
case 'b': |
1404 |
|
|
/* protocol 1, ignored */ |
1405 |
|
|
break; |
1406 |
|
|
case 'p': |
1407 |
|
|
options.ports_from_cmdline = 1; |
1408 |
|
|
if (options.num_ports >= MAX_PORTS) { |
1409 |
|
|
fprintf(stderr, "too many ports.\n"); |
1410 |
|
|
exit(1); |
1411 |
|
|
} |
1412 |
|
|
options.ports[options.num_ports++] = a2port(optarg); |
1413 |
|
|
if (options.ports[options.num_ports-1] <= 0) { |
1414 |
|
|
fprintf(stderr, "Bad port number.\n"); |
1415 |
|
|
exit(1); |
1416 |
|
|
} |
1417 |
|
|
break; |
1418 |
|
|
case 'g': |
1419 |
|
|
if ((options.login_grace_time = convtime(optarg)) == -1) { |
1420 |
|
|
fprintf(stderr, "Invalid login grace time.\n"); |
1421 |
|
|
exit(1); |
1422 |
|
|
} |
1423 |
|
|
break; |
1424 |
|
|
case 'k': |
1425 |
|
|
/* protocol 1, ignored */ |
1426 |
|
|
break; |
1427 |
|
|
case 'h': |
1428 |
|
|
servconf_add_hostkey("[command-line]", 0, |
1429 |
|
|
&options, optarg); |
1430 |
|
|
break; |
1431 |
|
|
case 't': |
1432 |
|
|
test_flag = 1; |
1433 |
|
|
break; |
1434 |
|
|
case 'T': |
1435 |
|
|
test_flag = 2; |
1436 |
|
|
break; |
1437 |
|
|
case 'C': |
1438 |
|
|
connection_info = get_connection_info(0, 0); |
1439 |
|
|
if (parse_server_match_testspec(connection_info, |
1440 |
|
|
optarg) == -1) |
1441 |
|
|
exit(1); |
1442 |
|
|
break; |
1443 |
|
|
case 'u': |
1444 |
|
|
utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL); |
1445 |
|
|
if (utmp_len > HOST_NAME_MAX+1) { |
1446 |
|
|
fprintf(stderr, "Invalid utmp length.\n"); |
1447 |
|
|
exit(1); |
1448 |
|
|
} |
1449 |
|
|
break; |
1450 |
|
|
case 'o': |
1451 |
|
|
line = xstrdup(optarg); |
1452 |
|
|
if (process_server_config_line(&options, line, |
1453 |
|
|
"command-line", 0, NULL, NULL) != 0) |
1454 |
|
|
exit(1); |
1455 |
|
|
free(line); |
1456 |
|
|
break; |
1457 |
|
|
case '?': |
1458 |
|
|
default: |
1459 |
|
|
usage(); |
1460 |
|
|
break; |
1461 |
|
|
} |
1462 |
|
|
} |
1463 |
|
|
if (rexeced_flag || inetd_flag) |
1464 |
|
|
rexec_flag = 0; |
1465 |
|
|
if (!test_flag && (rexec_flag && (av[0] == NULL || *av[0] != '/'))) |
1466 |
|
|
fatal("sshd re-exec requires execution with an absolute path"); |
1467 |
|
|
if (rexeced_flag) |
1468 |
|
|
closefrom(REEXEC_MIN_FREE_FD); |
1469 |
|
|
else |
1470 |
|
|
closefrom(REEXEC_DEVCRYPTO_RESERVED_FD); |
1471 |
|
|
|
1472 |
|
|
#ifdef WITH_OPENSSL |
1473 |
|
|
OpenSSL_add_all_algorithms(); |
1474 |
|
|
#endif |
1475 |
|
|
|
1476 |
|
|
/* If requested, redirect the logs to the specified logfile. */ |
1477 |
|
|
if (logfile != NULL) |
1478 |
|
|
log_redirect_stderr_to(logfile); |
1479 |
|
|
/* |
1480 |
|
|
* Force logging to stderr until we have loaded the private host |
1481 |
|
|
* key (unless started from inetd) |
1482 |
|
|
*/ |
1483 |
|
|
log_init(__progname, |
1484 |
|
|
options.log_level == SYSLOG_LEVEL_NOT_SET ? |
1485 |
|
|
SYSLOG_LEVEL_INFO : options.log_level, |
1486 |
|
|
options.log_facility == SYSLOG_FACILITY_NOT_SET ? |
1487 |
|
|
SYSLOG_FACILITY_AUTH : options.log_facility, |
1488 |
|
|
log_stderr || !inetd_flag); |
1489 |
|
|
|
1490 |
|
|
sensitive_data.have_ssh2_key = 0; |
1491 |
|
|
|
1492 |
|
|
/* |
1493 |
|
|
* If we're not doing an extended test do not silently ignore connection |
1494 |
|
|
* test params. |
1495 |
|
|
*/ |
1496 |
|
|
if (test_flag < 2 && connection_info != NULL) |
1497 |
|
|
fatal("Config test connection parameter (-C) provided without " |
1498 |
|
|
"test mode (-T)"); |
1499 |
|
|
|
1500 |
|
|
/* Fetch our configuration */ |
1501 |
|
|
buffer_init(&cfg); |
1502 |
|
|
if (rexeced_flag) |
1503 |
|
|
recv_rexec_state(REEXEC_CONFIG_PASS_FD, &cfg); |
1504 |
|
|
else if (strcasecmp(config_file_name, "none") != 0) |
1505 |
|
|
load_server_config(config_file_name, &cfg); |
1506 |
|
|
|
1507 |
|
|
parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name, |
1508 |
|
|
&cfg, NULL); |
1509 |
|
|
|
1510 |
|
|
/* Fill in default values for those options not explicitly set. */ |
1511 |
|
|
fill_default_server_options(&options); |
1512 |
|
|
|
1513 |
|
|
/* challenge-response is implemented via keyboard interactive */ |
1514 |
|
|
if (options.challenge_response_authentication) |
1515 |
|
|
options.kbd_interactive_authentication = 1; |
1516 |
|
|
|
1517 |
|
|
/* Check that options are sensible */ |
1518 |
|
|
if (options.authorized_keys_command_user == NULL && |
1519 |
|
|
(options.authorized_keys_command != NULL && |
1520 |
|
|
strcasecmp(options.authorized_keys_command, "none") != 0)) |
1521 |
|
|
fatal("AuthorizedKeysCommand set without " |
1522 |
|
|
"AuthorizedKeysCommandUser"); |
1523 |
|
|
if (options.authorized_principals_command_user == NULL && |
1524 |
|
|
(options.authorized_principals_command != NULL && |
1525 |
|
|
strcasecmp(options.authorized_principals_command, "none") != 0)) |
1526 |
|
|
fatal("AuthorizedPrincipalsCommand set without " |
1527 |
|
|
"AuthorizedPrincipalsCommandUser"); |
1528 |
|
|
|
1529 |
|
|
/* |
1530 |
|
|
* Check whether there is any path through configured auth methods. |
1531 |
|
|
* Unfortunately it is not possible to verify this generally before |
1532 |
|
|
* daemonisation in the presence of Match block, but this catches |
1533 |
|
|
* and warns for trivial misconfigurations that could break login. |
1534 |
|
|
*/ |
1535 |
|
|
if (options.num_auth_methods != 0) { |
1536 |
|
|
for (i = 0; i < options.num_auth_methods; i++) { |
1537 |
|
|
if (auth2_methods_valid(options.auth_methods[i], |
1538 |
|
|
1) == 0) |
1539 |
|
|
break; |
1540 |
|
|
} |
1541 |
|
|
if (i >= options.num_auth_methods) |
1542 |
|
|
fatal("AuthenticationMethods cannot be satisfied by " |
1543 |
|
|
"enabled authentication methods"); |
1544 |
|
|
} |
1545 |
|
|
|
1546 |
|
|
/* Check that there are no remaining arguments. */ |
1547 |
|
|
if (optind < ac) { |
1548 |
|
|
fprintf(stderr, "Extra argument %s.\n", av[optind]); |
1549 |
|
|
exit(1); |
1550 |
|
|
} |
1551 |
|
|
|
1552 |
|
|
debug("sshd version %s, %s", SSH_VERSION, |
1553 |
|
|
#ifdef WITH_OPENSSL |
1554 |
|
|
SSLeay_version(SSLEAY_VERSION) |
1555 |
|
|
#else |
1556 |
|
|
"without OpenSSL" |
1557 |
|
|
#endif |
1558 |
|
|
); |
1559 |
|
|
|
1560 |
|
|
/* load host keys */ |
1561 |
|
|
sensitive_data.host_keys = xcalloc(options.num_host_key_files, |
1562 |
|
|
sizeof(struct sshkey *)); |
1563 |
|
|
sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files, |
1564 |
|
|
sizeof(struct sshkey *)); |
1565 |
|
|
|
1566 |
|
|
if (options.host_key_agent) { |
1567 |
|
|
if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME)) |
1568 |
|
|
setenv(SSH_AUTHSOCKET_ENV_NAME, |
1569 |
|
|
options.host_key_agent, 1); |
1570 |
|
|
if ((r = ssh_get_authentication_socket(NULL)) == 0) |
1571 |
|
|
have_agent = 1; |
1572 |
|
|
else |
1573 |
|
|
error("Could not connect to agent \"%s\": %s", |
1574 |
|
|
options.host_key_agent, ssh_err(r)); |
1575 |
|
|
} |
1576 |
|
|
|
1577 |
|
|
for (i = 0; i < options.num_host_key_files; i++) { |
1578 |
|
|
if (options.host_key_files[i] == NULL) |
1579 |
|
|
continue; |
1580 |
|
|
key = key_load_private(options.host_key_files[i], "", NULL); |
1581 |
|
|
pubkey = key_load_public(options.host_key_files[i], NULL); |
1582 |
|
|
|
1583 |
|
|
if (pubkey == NULL && key != NULL) |
1584 |
|
|
pubkey = key_demote(key); |
1585 |
|
|
sensitive_data.host_keys[i] = key; |
1586 |
|
|
sensitive_data.host_pubkeys[i] = pubkey; |
1587 |
|
|
|
1588 |
|
|
if (key == NULL && pubkey != NULL && have_agent) { |
1589 |
|
|
debug("will rely on agent for hostkey %s", |
1590 |
|
|
options.host_key_files[i]); |
1591 |
|
|
keytype = pubkey->type; |
1592 |
|
|
} else if (key != NULL) { |
1593 |
|
|
keytype = key->type; |
1594 |
|
|
} else { |
1595 |
|
|
error("Could not load host key: %s", |
1596 |
|
|
options.host_key_files[i]); |
1597 |
|
|
sensitive_data.host_keys[i] = NULL; |
1598 |
|
|
sensitive_data.host_pubkeys[i] = NULL; |
1599 |
|
|
continue; |
1600 |
|
|
} |
1601 |
|
|
|
1602 |
|
|
switch (keytype) { |
1603 |
|
|
case KEY_RSA: |
1604 |
|
|
case KEY_DSA: |
1605 |
|
|
case KEY_ECDSA: |
1606 |
|
|
case KEY_ED25519: |
1607 |
|
|
if (have_agent || key != NULL) |
1608 |
|
|
sensitive_data.have_ssh2_key = 1; |
1609 |
|
|
break; |
1610 |
|
|
} |
1611 |
|
|
if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash, |
1612 |
|
|
SSH_FP_DEFAULT)) == NULL) |
1613 |
|
|
fatal("sshkey_fingerprint failed"); |
1614 |
|
|
debug("%s host key #%d: %s %s", |
1615 |
|
|
key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp); |
1616 |
|
|
free(fp); |
1617 |
|
|
} |
1618 |
|
|
if (!sensitive_data.have_ssh2_key) { |
1619 |
|
|
logit("sshd: no hostkeys available -- exiting."); |
1620 |
|
|
exit(1); |
1621 |
|
|
} |
1622 |
|
|
|
1623 |
|
|
/* |
1624 |
|
|
* Load certificates. They are stored in an array at identical |
1625 |
|
|
* indices to the public keys that they relate to. |
1626 |
|
|
*/ |
1627 |
|
|
sensitive_data.host_certificates = xcalloc(options.num_host_key_files, |
1628 |
|
|
sizeof(struct sshkey *)); |
1629 |
|
|
for (i = 0; i < options.num_host_key_files; i++) |
1630 |
|
|
sensitive_data.host_certificates[i] = NULL; |
1631 |
|
|
|
1632 |
|
|
for (i = 0; i < options.num_host_cert_files; i++) { |
1633 |
|
|
if (options.host_cert_files[i] == NULL) |
1634 |
|
|
continue; |
1635 |
|
|
key = key_load_public(options.host_cert_files[i], NULL); |
1636 |
|
|
if (key == NULL) { |
1637 |
|
|
error("Could not load host certificate: %s", |
1638 |
|
|
options.host_cert_files[i]); |
1639 |
|
|
continue; |
1640 |
|
|
} |
1641 |
|
|
if (!key_is_cert(key)) { |
1642 |
|
|
error("Certificate file is not a certificate: %s", |
1643 |
|
|
options.host_cert_files[i]); |
1644 |
|
|
key_free(key); |
1645 |
|
|
continue; |
1646 |
|
|
} |
1647 |
|
|
/* Find matching private key */ |
1648 |
|
|
for (j = 0; j < options.num_host_key_files; j++) { |
1649 |
|
|
if (key_equal_public(key, |
1650 |
|
|
sensitive_data.host_keys[j])) { |
1651 |
|
|
sensitive_data.host_certificates[j] = key; |
1652 |
|
|
break; |
1653 |
|
|
} |
1654 |
|
|
} |
1655 |
|
|
if (j >= options.num_host_key_files) { |
1656 |
|
|
error("No matching private key for certificate: %s", |
1657 |
|
|
options.host_cert_files[i]); |
1658 |
|
|
key_free(key); |
1659 |
|
|
continue; |
1660 |
|
|
} |
1661 |
|
|
sensitive_data.host_certificates[j] = key; |
1662 |
|
|
debug("host certificate: #%u type %d %s", j, key->type, |
1663 |
|
|
key_type(key)); |
1664 |
|
|
} |
1665 |
|
|
|
1666 |
|
|
if (use_privsep) { |
1667 |
|
|
struct stat st; |
1668 |
|
|
|
1669 |
|
|
if (getpwnam(SSH_PRIVSEP_USER) == NULL) |
1670 |
|
|
fatal("Privilege separation user %s does not exist", |
1671 |
|
|
SSH_PRIVSEP_USER); |
1672 |
|
|
if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) || |
1673 |
|
|
(S_ISDIR(st.st_mode) == 0)) |
1674 |
|
|
fatal("Missing privilege separation directory: %s", |
1675 |
|
|
_PATH_PRIVSEP_CHROOT_DIR); |
1676 |
|
|
if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0) |
1677 |
|
|
fatal("%s must be owned by root and not group or " |
1678 |
|
|
"world-writable.", _PATH_PRIVSEP_CHROOT_DIR); |
1679 |
|
|
} |
1680 |
|
|
|
1681 |
|
|
if (test_flag > 1) { |
1682 |
|
|
parse_server_match_config(&options, connection_info); |
1683 |
|
|
dump_config(&options); |
1684 |
|
|
} |
1685 |
|
|
|
1686 |
|
|
/* Configuration looks good, so exit if in test mode. */ |
1687 |
|
|
if (test_flag) |
1688 |
|
|
exit(0); |
1689 |
|
|
|
1690 |
|
|
if (rexec_flag) { |
1691 |
|
|
if (rexec_argc < 0) |
1692 |
|
|
fatal("rexec_argc %d < 0", rexec_argc); |
1693 |
|
|
rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *)); |
1694 |
|
|
for (i = 0; i < (u_int)rexec_argc; i++) { |
1695 |
|
|
debug("rexec_argv[%d]='%s'", i, saved_argv[i]); |
1696 |
|
|
rexec_argv[i] = saved_argv[i]; |
1697 |
|
|
} |
1698 |
|
|
rexec_argv[rexec_argc] = "-R"; |
1699 |
|
|
rexec_argv[rexec_argc + 1] = NULL; |
1700 |
|
|
} |
1701 |
|
|
|
1702 |
|
|
/* Ensure that umask disallows at least group and world write */ |
1703 |
|
|
new_umask = umask(0077) | 0022; |
1704 |
|
|
(void) umask(new_umask); |
1705 |
|
|
|
1706 |
|
|
/* Initialize the log (it is reinitialized below in case we forked). */ |
1707 |
|
|
if (debug_flag && (!inetd_flag || rexeced_flag)) |
1708 |
|
|
log_stderr = 1; |
1709 |
|
|
log_init(__progname, options.log_level, options.log_facility, log_stderr); |
1710 |
|
|
|
1711 |
|
|
/* |
1712 |
|
|
* If not in debugging mode, not started from inetd and not already |
1713 |
|
|
* daemonized (eg re-exec via SIGHUP), disconnect from the controlling |
1714 |
|
|
* terminal, and fork. The original process exits. |
1715 |
|
|
*/ |
1716 |
|
|
already_daemon = daemonized(); |
1717 |
|
|
if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) { |
1718 |
|
|
|
1719 |
|
|
if (daemon(0, 0) < 0) |
1720 |
|
|
fatal("daemon() failed: %.200s", strerror(errno)); |
1721 |
|
|
|
1722 |
|
|
disconnect_controlling_tty(); |
1723 |
|
|
} |
1724 |
|
|
/* Reinitialize the log (because of the fork above). */ |
1725 |
|
|
log_init(__progname, options.log_level, options.log_facility, log_stderr); |
1726 |
|
|
|
1727 |
|
|
/* Chdir to the root directory so that the current disk can be |
1728 |
|
|
unmounted if desired. */ |
1729 |
|
|
if (chdir("/") == -1) |
1730 |
|
|
error("chdir(\"/\"): %s", strerror(errno)); |
1731 |
|
|
|
1732 |
|
|
/* ignore SIGPIPE */ |
1733 |
|
|
signal(SIGPIPE, SIG_IGN); |
1734 |
|
|
|
1735 |
|
|
/* Get a connection, either from inetd or a listening TCP socket */ |
1736 |
|
|
if (inetd_flag) { |
1737 |
|
|
server_accept_inetd(&sock_in, &sock_out); |
1738 |
|
|
} else { |
1739 |
|
|
server_listen(); |
1740 |
|
|
|
1741 |
|
|
signal(SIGHUP, sighup_handler); |
1742 |
|
|
signal(SIGCHLD, main_sigchld_handler); |
1743 |
|
|
signal(SIGTERM, sigterm_handler); |
1744 |
|
|
signal(SIGQUIT, sigterm_handler); |
1745 |
|
|
|
1746 |
|
|
/* |
1747 |
|
|
* Write out the pid file after the sigterm handler |
1748 |
|
|
* is setup and the listen sockets are bound |
1749 |
|
|
*/ |
1750 |
|
|
if (options.pid_file != NULL && !debug_flag) { |
1751 |
|
|
FILE *f = fopen(options.pid_file, "w"); |
1752 |
|
|
|
1753 |
|
|
if (f == NULL) { |
1754 |
|
|
error("Couldn't create pid file \"%s\": %s", |
1755 |
|
|
options.pid_file, strerror(errno)); |
1756 |
|
|
} else { |
1757 |
|
|
fprintf(f, "%ld\n", (long) getpid()); |
1758 |
|
|
fclose(f); |
1759 |
|
|
} |
1760 |
|
|
} |
1761 |
|
|
|
1762 |
|
|
/* Accept a connection and return in a forked child */ |
1763 |
|
|
server_accept_loop(&sock_in, &sock_out, |
1764 |
|
|
&newsock, config_s); |
1765 |
|
|
} |
1766 |
|
|
|
1767 |
|
|
/* This is the child processing a new connection. */ |
1768 |
|
|
setproctitle("%s", "[accepted]"); |
1769 |
|
|
|
1770 |
|
|
/* |
1771 |
|
|
* Create a new session and process group since the 4.4BSD |
1772 |
|
|
* setlogin() affects the entire process group. We don't |
1773 |
|
|
* want the child to be able to affect the parent. |
1774 |
|
|
*/ |
1775 |
|
|
if (!debug_flag && !inetd_flag && setsid() < 0) |
1776 |
|
|
error("setsid: %.100s", strerror(errno)); |
1777 |
|
|
|
1778 |
|
|
if (rexec_flag) { |
1779 |
|
|
int fd; |
1780 |
|
|
|
1781 |
|
|
debug("rexec start in %d out %d newsock %d pipe %d sock %d", |
1782 |
|
|
sock_in, sock_out, newsock, startup_pipe, config_s[0]); |
1783 |
|
|
dup2(newsock, STDIN_FILENO); |
1784 |
|
|
dup2(STDIN_FILENO, STDOUT_FILENO); |
1785 |
|
|
if (startup_pipe == -1) |
1786 |
|
|
close(REEXEC_STARTUP_PIPE_FD); |
1787 |
|
|
else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) { |
1788 |
|
|
dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD); |
1789 |
|
|
close(startup_pipe); |
1790 |
|
|
startup_pipe = REEXEC_STARTUP_PIPE_FD; |
1791 |
|
|
} |
1792 |
|
|
|
1793 |
|
|
dup2(config_s[1], REEXEC_CONFIG_PASS_FD); |
1794 |
|
|
close(config_s[1]); |
1795 |
|
|
|
1796 |
|
|
execv(rexec_argv[0], rexec_argv); |
1797 |
|
|
|
1798 |
|
|
/* Reexec has failed, fall back and continue */ |
1799 |
|
|
error("rexec of %s failed: %s", rexec_argv[0], strerror(errno)); |
1800 |
|
|
recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL); |
1801 |
|
|
log_init(__progname, options.log_level, |
1802 |
|
|
options.log_facility, log_stderr); |
1803 |
|
|
|
1804 |
|
|
/* Clean up fds */ |
1805 |
|
|
close(REEXEC_CONFIG_PASS_FD); |
1806 |
|
|
newsock = sock_out = sock_in = dup(STDIN_FILENO); |
1807 |
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { |
1808 |
|
|
dup2(fd, STDIN_FILENO); |
1809 |
|
|
dup2(fd, STDOUT_FILENO); |
1810 |
|
|
if (fd > STDERR_FILENO) |
1811 |
|
|
close(fd); |
1812 |
|
|
} |
1813 |
|
|
debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d", |
1814 |
|
|
sock_in, sock_out, newsock, startup_pipe, config_s[0]); |
1815 |
|
|
} |
1816 |
|
|
|
1817 |
|
|
/* Executed child processes don't need these. */ |
1818 |
|
|
fcntl(sock_out, F_SETFD, FD_CLOEXEC); |
1819 |
|
|
fcntl(sock_in, F_SETFD, FD_CLOEXEC); |
1820 |
|
|
|
1821 |
|
|
/* |
1822 |
|
|
* Disable the key regeneration alarm. We will not regenerate the |
1823 |
|
|
* key since we are no longer in a position to give it to anyone. We |
1824 |
|
|
* will not restart on SIGHUP since it no longer makes sense. |
1825 |
|
|
*/ |
1826 |
|
|
alarm(0); |
1827 |
|
|
signal(SIGALRM, SIG_DFL); |
1828 |
|
|
signal(SIGHUP, SIG_DFL); |
1829 |
|
|
signal(SIGTERM, SIG_DFL); |
1830 |
|
|
signal(SIGQUIT, SIG_DFL); |
1831 |
|
|
signal(SIGCHLD, SIG_DFL); |
1832 |
|
|
|
1833 |
|
|
/* |
1834 |
|
|
* Register our connection. This turns encryption off because we do |
1835 |
|
|
* not have a key. |
1836 |
|
|
*/ |
1837 |
|
|
packet_set_connection(sock_in, sock_out); |
1838 |
|
|
packet_set_server(); |
1839 |
|
|
ssh = active_state; /* XXX */ |
1840 |
|
|
|
1841 |
|
|
check_ip_options(ssh); |
1842 |
|
|
|
1843 |
|
|
/* Prepare the channels layer */ |
1844 |
|
|
channel_init_channels(ssh); |
1845 |
|
|
channel_set_af(ssh, options.address_family); |
1846 |
|
|
process_permitopen(ssh, &options); |
1847 |
|
|
|
1848 |
|
|
/* Set SO_KEEPALIVE if requested. */ |
1849 |
|
|
if (options.tcp_keep_alive && packet_connection_is_on_socket() && |
1850 |
|
|
setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0) |
1851 |
|
|
error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); |
1852 |
|
|
|
1853 |
|
|
if ((remote_port = ssh_remote_port(ssh)) < 0) { |
1854 |
|
|
debug("ssh_remote_port failed"); |
1855 |
|
|
cleanup_exit(255); |
1856 |
|
|
} |
1857 |
|
|
|
1858 |
|
|
/* |
1859 |
|
|
* The rest of the code depends on the fact that |
1860 |
|
|
* ssh_remote_ipaddr() caches the remote ip, even if |
1861 |
|
|
* the socket goes away. |
1862 |
|
|
*/ |
1863 |
|
|
remote_ip = ssh_remote_ipaddr(ssh); |
1864 |
|
|
|
1865 |
|
|
rdomain = ssh_packet_rdomain_in(ssh); |
1866 |
|
|
|
1867 |
|
|
/* Log the connection. */ |
1868 |
|
|
laddr = get_local_ipaddr(sock_in); |
1869 |
|
|
verbose("Connection from %s port %d on %s port %d%s%s%s", |
1870 |
|
|
remote_ip, remote_port, laddr, ssh_local_port(ssh), |
1871 |
|
|
rdomain == NULL ? "" : " rdomain \"", |
1872 |
|
|
rdomain == NULL ? "" : rdomain, |
1873 |
|
|
rdomain == NULL ? "" : "\""); |
1874 |
|
|
free(laddr); |
1875 |
|
|
|
1876 |
|
|
/* |
1877 |
|
|
* We don't want to listen forever unless the other side |
1878 |
|
|
* successfully authenticates itself. So we set up an alarm which is |
1879 |
|
|
* cleared after successful authentication. A limit of zero |
1880 |
|
|
* indicates no limit. Note that we don't set the alarm in debugging |
1881 |
|
|
* mode; it is just annoying to have the server exit just when you |
1882 |
|
|
* are about to discover the bug. |
1883 |
|
|
*/ |
1884 |
|
|
signal(SIGALRM, grace_alarm_handler); |
1885 |
|
|
if (!debug_flag) |
1886 |
|
|
alarm(options.login_grace_time); |
1887 |
|
|
|
1888 |
|
|
sshd_exchange_identification(ssh, sock_in, sock_out); |
1889 |
|
|
packet_set_nonblocking(); |
1890 |
|
|
|
1891 |
|
|
/* allocate authentication context */ |
1892 |
|
|
authctxt = xcalloc(1, sizeof(*authctxt)); |
1893 |
|
|
|
1894 |
|
|
/* XXX global for cleanup, access from other modules */ |
1895 |
|
|
the_authctxt = authctxt; |
1896 |
|
|
|
1897 |
|
|
/* prepare buffer to collect messages to display to user after login */ |
1898 |
|
|
buffer_init(&loginmsg); |
1899 |
|
|
auth_debug_reset(); |
1900 |
|
|
|
1901 |
|
|
if (use_privsep) { |
1902 |
|
|
if (privsep_preauth(authctxt) == 1) |
1903 |
|
|
goto authenticated; |
1904 |
|
|
} else if (have_agent) { |
1905 |
|
|
if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) { |
1906 |
|
|
error("Unable to get agent socket: %s", ssh_err(r)); |
1907 |
|
|
have_agent = 0; |
1908 |
|
|
} |
1909 |
|
|
} |
1910 |
|
|
|
1911 |
|
|
/* perform the key exchange */ |
1912 |
|
|
/* authenticate user and start session */ |
1913 |
|
|
do_ssh2_kex(); |
1914 |
|
|
do_authentication2(authctxt); |
1915 |
|
|
|
1916 |
|
|
/* |
1917 |
|
|
* If we use privilege separation, the unprivileged child transfers |
1918 |
|
|
* the current keystate and exits |
1919 |
|
|
*/ |
1920 |
|
|
if (use_privsep) { |
1921 |
|
|
mm_send_keystate(pmonitor); |
1922 |
|
|
packet_clear_keys(); |
1923 |
|
|
exit(0); |
1924 |
|
|
} |
1925 |
|
|
|
1926 |
|
|
authenticated: |
1927 |
|
|
/* |
1928 |
|
|
* Cancel the alarm we set to limit the time taken for |
1929 |
|
|
* authentication. |
1930 |
|
|
*/ |
1931 |
|
|
alarm(0); |
1932 |
|
|
signal(SIGALRM, SIG_DFL); |
1933 |
|
|
authctxt->authenticated = 1; |
1934 |
|
|
if (startup_pipe != -1) { |
1935 |
|
|
close(startup_pipe); |
1936 |
|
|
startup_pipe = -1; |
1937 |
|
|
} |
1938 |
|
|
|
1939 |
|
|
if (options.routing_domain != NULL) |
1940 |
|
|
set_process_rdomain(ssh, options.routing_domain); |
1941 |
|
|
|
1942 |
|
|
/* |
1943 |
|
|
* In privilege separation, we fork another child and prepare |
1944 |
|
|
* file descriptor passing. |
1945 |
|
|
*/ |
1946 |
|
|
if (use_privsep) { |
1947 |
|
|
privsep_postauth(authctxt); |
1948 |
|
|
/* the monitor process [priv] will not return */ |
1949 |
|
|
} |
1950 |
|
|
|
1951 |
|
|
packet_set_timeout(options.client_alive_interval, |
1952 |
|
|
options.client_alive_count_max); |
1953 |
|
|
|
1954 |
|
|
/* Try to send all our hostkeys to the client */ |
1955 |
|
|
notify_hostkeys(ssh); |
1956 |
|
|
|
1957 |
|
|
/* Start session. */ |
1958 |
|
|
do_authenticated(ssh, authctxt); |
1959 |
|
|
|
1960 |
|
|
/* The connection has been terminated. */ |
1961 |
|
|
packet_get_bytes(&ibytes, &obytes); |
1962 |
|
|
verbose("Transferred: sent %llu, received %llu bytes", |
1963 |
|
|
(unsigned long long)obytes, (unsigned long long)ibytes); |
1964 |
|
|
|
1965 |
|
|
verbose("Closing connection to %.500s port %d", remote_ip, remote_port); |
1966 |
|
|
packet_close(); |
1967 |
|
|
|
1968 |
|
|
if (use_privsep) |
1969 |
|
|
mm_terminate(); |
1970 |
|
|
|
1971 |
|
|
exit(0); |
1972 |
|
|
} |
1973 |
|
|
|
1974 |
|
|
int |
1975 |
|
|
sshd_hostkey_sign(struct sshkey *privkey, struct sshkey *pubkey, |
1976 |
|
|
u_char **signature, size_t *slen, const u_char *data, size_t dlen, |
1977 |
|
|
const char *alg, u_int flag) |
1978 |
|
|
{ |
1979 |
|
|
int r; |
1980 |
|
|
u_int xxx_slen, xxx_dlen = dlen; |
1981 |
|
|
|
1982 |
|
|
if (privkey) { |
1983 |
|
|
if (PRIVSEP(key_sign(privkey, signature, &xxx_slen, data, xxx_dlen, |
1984 |
|
|
alg) < 0)) |
1985 |
|
|
fatal("%s: key_sign failed", __func__); |
1986 |
|
|
if (slen) |
1987 |
|
|
*slen = xxx_slen; |
1988 |
|
|
} else if (use_privsep) { |
1989 |
|
|
if (mm_key_sign(pubkey, signature, &xxx_slen, data, xxx_dlen, |
1990 |
|
|
alg) < 0) |
1991 |
|
|
fatal("%s: pubkey_sign failed", __func__); |
1992 |
|
|
if (slen) |
1993 |
|
|
*slen = xxx_slen; |
1994 |
|
|
} else { |
1995 |
|
|
if ((r = ssh_agent_sign(auth_sock, pubkey, signature, slen, |
1996 |
|
|
data, dlen, alg, datafellows)) != 0) |
1997 |
|
|
fatal("%s: ssh_agent_sign failed: %s", |
1998 |
|
|
__func__, ssh_err(r)); |
1999 |
|
|
} |
2000 |
|
|
return 0; |
2001 |
|
|
} |
2002 |
|
|
|
2003 |
|
|
/* SSH2 key exchange */ |
2004 |
|
|
static void |
2005 |
|
|
do_ssh2_kex(void) |
2006 |
|
|
{ |
2007 |
|
|
char *myproposal[PROPOSAL_MAX] = { KEX_SERVER }; |
2008 |
|
|
struct kex *kex; |
2009 |
|
|
int r; |
2010 |
|
|
|
2011 |
|
|
myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal( |
2012 |
|
|
options.kex_algorithms); |
2013 |
|
|
myproposal[PROPOSAL_ENC_ALGS_CTOS] = compat_cipher_proposal( |
2014 |
|
|
options.ciphers); |
2015 |
|
|
myproposal[PROPOSAL_ENC_ALGS_STOC] = compat_cipher_proposal( |
2016 |
|
|
options.ciphers); |
2017 |
|
|
myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
2018 |
|
|
myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
2019 |
|
|
|
2020 |
|
|
if (options.compression == COMP_NONE) { |
2021 |
|
|
myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
2022 |
|
|
myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; |
2023 |
|
|
} |
2024 |
|
|
|
2025 |
|
|
if (options.rekey_limit || options.rekey_interval) |
2026 |
|
|
packet_set_rekey_limits(options.rekey_limit, |
2027 |
|
|
options.rekey_interval); |
2028 |
|
|
|
2029 |
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal( |
2030 |
|
|
list_hostkey_types()); |
2031 |
|
|
|
2032 |
|
|
/* start key exchange */ |
2033 |
|
|
if ((r = kex_setup(active_state, myproposal)) != 0) |
2034 |
|
|
fatal("kex_setup: %s", ssh_err(r)); |
2035 |
|
|
kex = active_state->kex; |
2036 |
|
|
#ifdef WITH_OPENSSL |
2037 |
|
|
kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; |
2038 |
|
|
kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; |
2039 |
|
|
kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server; |
2040 |
|
|
kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server; |
2041 |
|
|
kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server; |
2042 |
|
|
kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; |
2043 |
|
|
kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; |
2044 |
|
|
kex->kex[KEX_ECDH_SHA2] = kexecdh_server; |
2045 |
|
|
#endif |
2046 |
|
|
kex->kex[KEX_C25519_SHA256] = kexc25519_server; |
2047 |
|
|
kex->server = 1; |
2048 |
|
|
kex->client_version_string=client_version_string; |
2049 |
|
|
kex->server_version_string=server_version_string; |
2050 |
|
|
kex->load_host_public_key=&get_hostkey_public_by_type; |
2051 |
|
|
kex->load_host_private_key=&get_hostkey_private_by_type; |
2052 |
|
|
kex->host_key_index=&get_hostkey_index; |
2053 |
|
|
kex->sign = sshd_hostkey_sign; |
2054 |
|
|
|
2055 |
|
|
ssh_dispatch_run_fatal(active_state, DISPATCH_BLOCK, &kex->done); |
2056 |
|
|
|
2057 |
|
|
session_id2 = kex->session_id; |
2058 |
|
|
session_id2_len = kex->session_id_len; |
2059 |
|
|
|
2060 |
|
|
#ifdef DEBUG_KEXDH |
2061 |
|
|
/* send 1st encrypted/maced/compressed message */ |
2062 |
|
|
packet_start(SSH2_MSG_IGNORE); |
2063 |
|
|
packet_put_cstring("markus"); |
2064 |
|
|
packet_send(); |
2065 |
|
|
packet_write_wait(); |
2066 |
|
|
#endif |
2067 |
|
|
debug("KEX done"); |
2068 |
|
|
} |
2069 |
|
|
|
2070 |
|
|
/* server specific fatal cleanup */ |
2071 |
|
|
void |
2072 |
|
|
cleanup_exit(int i) |
2073 |
|
|
{ |
2074 |
|
|
struct ssh *ssh = active_state; /* XXX */ |
2075 |
|
|
|
2076 |
|
|
if (the_authctxt) { |
2077 |
|
|
do_cleanup(ssh, the_authctxt); |
2078 |
|
|
if (use_privsep && privsep_is_preauth && |
2079 |
|
|
pmonitor != NULL && pmonitor->m_pid > 1) { |
2080 |
|
|
debug("Killing privsep child %d", pmonitor->m_pid); |
2081 |
|
|
if (kill(pmonitor->m_pid, SIGKILL) != 0 && |
2082 |
|
|
errno != ESRCH) |
2083 |
|
|
error("%s: kill(%d): %s", __func__, |
2084 |
|
|
pmonitor->m_pid, strerror(errno)); |
2085 |
|
|
} |
2086 |
|
|
} |
2087 |
|
|
_exit(i); |
2088 |
|
|
} |