]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
5 | * Created: Fri Mar 17 17:09:28 1995 ylo | |
6 | * This program is the ssh daemon. It listens for connections from clients, and | |
7 | * 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 authentication | |
10 | * agent connections. | |
11 | */ | |
8efc0c15 | 12 | |
13 | #include "includes.h" | |
14 | RCSID("$Id$"); | |
15 | ||
16 | #include "xmalloc.h" | |
17 | #include "rsa.h" | |
18 | #include "ssh.h" | |
19 | #include "pty.h" | |
20 | #include "packet.h" | |
21 | #include "buffer.h" | |
22 | #include "cipher.h" | |
23 | #include "mpaux.h" | |
24 | #include "servconf.h" | |
25 | #include "uidswap.h" | |
26 | #include "compat.h" | |
27 | ||
28 | #ifdef LIBWRAP | |
29 | #include <tcpd.h> | |
30 | #include <syslog.h> | |
31 | int allow_severity = LOG_INFO; | |
32 | int deny_severity = LOG_WARNING; | |
33 | #endif /* LIBWRAP */ | |
34 | ||
35 | #ifndef O_NOCTTY | |
36 | #define O_NOCTTY 0 | |
37 | #endif | |
38 | ||
8efc0c15 | 39 | /* Local Xauthority file. */ |
6a17f9c2 | 40 | static char *xauthfile = NULL; |
8efc0c15 | 41 | |
42 | /* Server configuration options. */ | |
43 | ServerOptions options; | |
44 | ||
45 | /* Name of the server configuration file. */ | |
46 | char *config_file_name = SERVER_CONFIG_FILE; | |
47 | ||
5260325f | 48 | /* |
49 | * Debug mode flag. This can be set on the command line. If debug | |
50 | * mode is enabled, extra debugging output will be sent to the system | |
51 | * log, the daemon will not go to background, and will exit after processing | |
52 | * the first connection. | |
53 | */ | |
8efc0c15 | 54 | int debug_flag = 0; |
55 | ||
56 | /* Flag indicating that the daemon is being started from inetd. */ | |
57 | int inetd_flag = 0; | |
58 | ||
6a17f9c2 | 59 | /* debug goes to stderr unless inetd_flag is set */ |
60 | int log_stderr = 0; | |
61 | ||
8efc0c15 | 62 | /* argv[0] without path. */ |
63 | char *av0; | |
64 | ||
65 | /* Saved arguments to main(). */ | |
66 | char **saved_argv; | |
67 | ||
aa3378df | 68 | /* |
69 | * This is set to the socket that the server is listening; this is used in | |
70 | * the SIGHUP signal handler. | |
71 | */ | |
8efc0c15 | 72 | int listen_sock; |
73 | ||
aa3378df | 74 | /* |
75 | * the client's version string, passed by sshd2 in compat mode. if != NULL, | |
76 | * sshd will skip the version-number exchange | |
77 | */ | |
5260325f | 78 | char *client_version_string = NULL; |
79 | ||
80 | /* Flags set in auth-rsa from authorized_keys flags. These are set in auth-rsa.c. */ | |
8efc0c15 | 81 | int no_port_forwarding_flag = 0; |
82 | int no_agent_forwarding_flag = 0; | |
83 | int no_x11_forwarding_flag = 0; | |
84 | int no_pty_flag = 0; | |
5260325f | 85 | |
86 | /* RSA authentication "command=" option. */ | |
87 | char *forced_command = NULL; | |
88 | ||
89 | /* RSA authentication "environment=" options. */ | |
90 | struct envstring *custom_environment = NULL; | |
8efc0c15 | 91 | |
92 | /* Session id for the current session. */ | |
93 | unsigned char session_id[16]; | |
94 | ||
aa3378df | 95 | /* |
96 | * Any really sensitive data in the application is contained in this | |
97 | * structure. The idea is that this structure could be locked into memory so | |
98 | * that the pages do not get written into swap. However, there are some | |
99 | * problems. The private key contains BIGNUMs, and we do not (in principle) | |
100 | * have access to the internals of them, and locking just the structure is | |
101 | * not very useful. Currently, memory locking is not implemented. | |
102 | */ | |
5260325f | 103 | struct { |
104 | RSA *private_key; /* Private part of server key. */ | |
105 | RSA *host_key; /* Private part of host key. */ | |
8efc0c15 | 106 | } sensitive_data; |
107 | ||
aa3378df | 108 | /* |
109 | * Flag indicating whether the current session key has been used. This flag | |
110 | * is set whenever the key is used, and cleared when the key is regenerated. | |
111 | */ | |
8efc0c15 | 112 | int key_used = 0; |
113 | ||
114 | /* This is set to true when SIGHUP is received. */ | |
115 | int received_sighup = 0; | |
116 | ||
117 | /* Public side of the server key. This value is regenerated regularly with | |
118 | the private key. */ | |
119 | RSA *public_key; | |
120 | ||
121 | /* Prototypes for various functions defined later in this file. */ | |
e7c0f9d5 | 122 | void do_connection(); |
123 | void do_authentication(char *user); | |
5260325f | 124 | void do_authloop(struct passwd * pw); |
e7c0f9d5 | 125 | void do_fake_authloop(char *user); |
5260325f | 126 | void do_authenticated(struct passwd * pw); |
127 | void do_exec_pty(const char *command, int ptyfd, int ttyfd, | |
128 | const char *ttyname, struct passwd * pw, const char *term, | |
129 | const char *display, const char *auth_proto, | |
130 | const char *auth_data); | |
131 | void do_exec_no_pty(const char *command, struct passwd * pw, | |
132 | const char *display, const char *auth_proto, | |
133 | const char *auth_data); | |
134 | void do_child(const char *command, struct passwd * pw, const char *term, | |
8efc0c15 | 135 | const char *display, const char *auth_proto, |
136 | const char *auth_data, const char *ttyname); | |
e7c0f9d5 | 137 | |
5260325f | 138 | /* |
139 | * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; | |
140 | * the effect is to reread the configuration file (and to regenerate | |
141 | * the server key). | |
142 | */ | |
143 | void | |
144 | sighup_handler(int sig) | |
8efc0c15 | 145 | { |
5260325f | 146 | received_sighup = 1; |
147 | signal(SIGHUP, sighup_handler); | |
8efc0c15 | 148 | } |
149 | ||
5260325f | 150 | /* |
151 | * Called from the main program after receiving SIGHUP. | |
152 | * Restarts the server. | |
153 | */ | |
154 | void | |
155 | sighup_restart() | |
8efc0c15 | 156 | { |
5260325f | 157 | log("Received SIGHUP; restarting."); |
158 | close(listen_sock); | |
159 | execv(saved_argv[0], saved_argv); | |
160 | log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno)); | |
161 | exit(1); | |
8efc0c15 | 162 | } |
163 | ||
5260325f | 164 | /* |
165 | * Generic signal handler for terminating signals in the master daemon. | |
166 | * These close the listen socket; not closing it seems to cause "Address | |
167 | * already in use" problems on some machines, which is inconvenient. | |
168 | */ | |
169 | void | |
170 | sigterm_handler(int sig) | |
8efc0c15 | 171 | { |
5260325f | 172 | log("Received signal %d; terminating.", sig); |
173 | close(listen_sock); | |
174 | exit(255); | |
8efc0c15 | 175 | } |
176 | ||
5260325f | 177 | /* |
178 | * SIGCHLD handler. This is called whenever a child dies. This will then | |
179 | * reap any zombies left by exited c. | |
180 | */ | |
181 | void | |
182 | main_sigchld_handler(int sig) | |
8efc0c15 | 183 | { |
5260325f | 184 | int save_errno = errno; |
185 | int status; | |
5ad13cd7 | 186 | |
5260325f | 187 | while (waitpid(-1, &status, WNOHANG) > 0) |
188 | ; | |
5ad13cd7 | 189 | |
5260325f | 190 | signal(SIGCHLD, main_sigchld_handler); |
191 | errno = save_errno; | |
8efc0c15 | 192 | } |
193 | ||
5260325f | 194 | /* |
195 | * Signal handler for the alarm after the login grace period has expired. | |
196 | */ | |
197 | void | |
198 | grace_alarm_handler(int sig) | |
8efc0c15 | 199 | { |
5260325f | 200 | /* Close the connection. */ |
201 | packet_close(); | |
8efc0c15 | 202 | |
5260325f | 203 | /* Log error and exit. */ |
204 | fatal("Timeout before authentication for %s.", get_remote_ipaddr()); | |
205 | } | |
8efc0c15 | 206 | |
5260325f | 207 | /* |
208 | * convert ssh auth msg type into description | |
209 | */ | |
210 | char * | |
211 | get_authname(int type) | |
8efc0c15 | 212 | { |
5260325f | 213 | switch (type) { |
214 | case SSH_CMSG_AUTH_PASSWORD: | |
215 | return "password"; | |
216 | case SSH_CMSG_AUTH_RSA: | |
217 | return "rsa"; | |
218 | case SSH_CMSG_AUTH_RHOSTS_RSA: | |
219 | return "rhosts-rsa"; | |
220 | case SSH_CMSG_AUTH_RHOSTS: | |
221 | return "rhosts"; | |
222 | #ifdef KRB4 | |
223 | case SSH_CMSG_AUTH_KERBEROS: | |
224 | return "kerberos"; | |
225 | #endif | |
226 | #ifdef SKEY | |
227 | case SSH_CMSG_AUTH_TIS_RESPONSE: | |
228 | return "s/key"; | |
229 | #endif | |
230 | } | |
231 | fatal("get_authname: unknown auth %d: internal error", type); | |
232 | return NULL; | |
8efc0c15 | 233 | } |
234 | ||
5260325f | 235 | /* |
236 | * Signal handler for the key regeneration alarm. Note that this | |
237 | * alarm only occurs in the daemon waiting for connections, and it does not | |
238 | * do anything with the private key or random state before forking. | |
239 | * Thus there should be no concurrency control/asynchronous execution | |
240 | * problems. | |
241 | */ | |
242 | void | |
243 | key_regeneration_alarm(int sig) | |
244 | { | |
245 | int save_errno = errno; | |
246 | ||
247 | /* Check if we should generate a new key. */ | |
248 | if (key_used) { | |
249 | /* This should really be done in the background. */ | |
250 | log("Generating new %d bit RSA key.", options.server_key_bits); | |
251 | ||
252 | if (sensitive_data.private_key != NULL) | |
253 | RSA_free(sensitive_data.private_key); | |
254 | sensitive_data.private_key = RSA_new(); | |
255 | ||
256 | if (public_key != NULL) | |
257 | RSA_free(public_key); | |
258 | public_key = RSA_new(); | |
259 | ||
260 | rsa_generate_key(sensitive_data.private_key, public_key, | |
261 | options.server_key_bits); | |
262 | arc4random_stir(); | |
263 | key_used = 0; | |
264 | log("RSA key generation complete."); | |
265 | } | |
266 | /* Reschedule the alarm. */ | |
267 | signal(SIGALRM, key_regeneration_alarm); | |
268 | alarm(options.key_regeneration_time); | |
269 | errno = save_errno; | |
270 | } | |
8efc0c15 | 271 | |
5260325f | 272 | /* |
273 | * Main program for the daemon. | |
274 | */ | |
8efc0c15 | 275 | int |
276 | main(int ac, char **av) | |
277 | { | |
5260325f | 278 | extern char *optarg; |
279 | extern int optind; | |
280 | int opt, aux, sock_in, sock_out, newsock, i, pid, on = 1; | |
281 | int remote_major, remote_minor; | |
282 | int silentrsa = 0; | |
2d86a6cc | 283 | struct pollfd fds; |
5260325f | 284 | struct sockaddr_in sin; |
285 | char buf[100]; /* Must not be larger than remote_version. */ | |
286 | char remote_version[100]; /* Must be at least as big as buf. */ | |
287 | const char *remote_ip; | |
288 | int remote_port; | |
289 | char *comment; | |
290 | FILE *f; | |
291 | struct linger linger; | |
292 | ||
293 | /* Save argv[0]. */ | |
294 | saved_argv = av; | |
295 | if (strchr(av[0], '/')) | |
296 | av0 = strrchr(av[0], '/') + 1; | |
297 | else | |
298 | av0 = av[0]; | |
299 | ||
300 | /* Initialize configuration options to their default values. */ | |
301 | initialize_server_options(&options); | |
302 | ||
303 | /* Parse command-line arguments. */ | |
304 | while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ")) != EOF) { | |
305 | switch (opt) { | |
306 | case 'f': | |
307 | config_file_name = optarg; | |
308 | break; | |
309 | case 'd': | |
310 | debug_flag = 1; | |
311 | options.log_level = SYSLOG_LEVEL_DEBUG; | |
312 | break; | |
313 | case 'i': | |
314 | inetd_flag = 1; | |
315 | break; | |
316 | case 'Q': | |
317 | silentrsa = 1; | |
318 | break; | |
319 | case 'q': | |
320 | options.log_level = SYSLOG_LEVEL_QUIET; | |
321 | break; | |
322 | case 'b': | |
323 | options.server_key_bits = atoi(optarg); | |
324 | break; | |
325 | case 'p': | |
326 | options.port = atoi(optarg); | |
327 | break; | |
328 | case 'g': | |
329 | options.login_grace_time = atoi(optarg); | |
330 | break; | |
331 | case 'k': | |
332 | options.key_regeneration_time = atoi(optarg); | |
333 | break; | |
334 | case 'h': | |
335 | options.host_key_file = optarg; | |
336 | break; | |
337 | case 'V': | |
338 | client_version_string = optarg; | |
339 | /* only makes sense with inetd_flag, i.e. no listen() */ | |
340 | inetd_flag = 1; | |
341 | break; | |
342 | case '?': | |
343 | default: | |
344 | fprintf(stderr, "sshd version %s\n", SSH_VERSION); | |
0a2ff95d | 345 | #ifdef RSAREF |
346 | fprintf(stderr, "Compiled with RSAref.\n"); | |
347 | #endif | |
5260325f | 348 | fprintf(stderr, "Usage: %s [options]\n", av0); |
349 | fprintf(stderr, "Options:\n"); | |
aa3378df | 350 | fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE); |
5260325f | 351 | fprintf(stderr, " -d Debugging mode\n"); |
352 | fprintf(stderr, " -i Started from inetd\n"); | |
353 | fprintf(stderr, " -q Quiet (no logging)\n"); | |
354 | fprintf(stderr, " -p port Listen on the specified port (default: 22)\n"); | |
355 | fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n"); | |
356 | fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n"); | |
357 | fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n"); | |
358 | fprintf(stderr, " -h file File from which to read host key (default: %s)\n", | |
359 | HOST_KEY_FILE); | |
360 | exit(1); | |
361 | } | |
362 | } | |
363 | ||
364 | /* check if RSA support exists */ | |
365 | if (rsa_alive() == 0) { | |
366 | if (silentrsa == 0) | |
367 | printf("sshd: no RSA support in libssl and libcrypto -- exiting. See ssl(8)\n"); | |
368 | log("no RSA support in libssl and libcrypto -- exiting. See ssl(8)"); | |
369 | exit(1); | |
370 | } | |
371 | /* Read server configuration options from the configuration file. */ | |
372 | read_server_config(&options, config_file_name); | |
373 | ||
374 | /* Fill in default values for those options not explicitly set. */ | |
375 | fill_default_server_options(&options); | |
376 | ||
377 | /* Check certain values for sanity. */ | |
378 | if (options.server_key_bits < 512 || | |
379 | options.server_key_bits > 32768) { | |
380 | fprintf(stderr, "Bad server key size.\n"); | |
381 | exit(1); | |
382 | } | |
383 | if (options.port < 1 || options.port > 65535) { | |
384 | fprintf(stderr, "Bad port number.\n"); | |
385 | exit(1); | |
386 | } | |
387 | /* Check that there are no remaining arguments. */ | |
388 | if (optind < ac) { | |
389 | fprintf(stderr, "Extra argument %s.\n", av[optind]); | |
390 | exit(1); | |
8efc0c15 | 391 | } |
5260325f | 392 | /* Force logging to stderr while loading the private host key |
393 | unless started from inetd */ | |
394 | log_init(av0, options.log_level, options.log_facility, !inetd_flag); | |
395 | ||
396 | debug("sshd version %.100s", SSH_VERSION); | |
397 | ||
398 | sensitive_data.host_key = RSA_new(); | |
399 | errno = 0; | |
400 | /* Load the host key. It must have empty passphrase. */ | |
401 | if (!load_private_key(options.host_key_file, "", | |
402 | sensitive_data.host_key, &comment)) { | |
403 | error("Could not load host key: %.200s: %.100s", | |
404 | options.host_key_file, strerror(errno)); | |
405 | exit(1); | |
406 | } | |
407 | xfree(comment); | |
408 | ||
409 | /* Initialize the log (it is reinitialized below in case we | |
410 | forked). */ | |
411 | if (debug_flag && !inetd_flag) | |
412 | log_stderr = 1; | |
413 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
414 | ||
415 | /* If not in debugging mode, and not started from inetd, | |
416 | disconnect from the controlling terminal, and fork. The | |
417 | original process exits. */ | |
418 | if (!debug_flag && !inetd_flag) { | |
8efc0c15 | 419 | #ifdef TIOCNOTTY |
5260325f | 420 | int fd; |
8efc0c15 | 421 | #endif /* TIOCNOTTY */ |
5260325f | 422 | if (daemon(0, 0) < 0) |
423 | fatal("daemon() failed: %.200s", strerror(errno)); | |
424 | ||
425 | /* Disconnect from the controlling tty. */ | |
8efc0c15 | 426 | #ifdef TIOCNOTTY |
5260325f | 427 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
428 | if (fd >= 0) { | |
429 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
430 | close(fd); | |
431 | } | |
8efc0c15 | 432 | #endif /* TIOCNOTTY */ |
8efc0c15 | 433 | } |
5260325f | 434 | /* Reinitialize the log (because of the fork above). */ |
435 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
436 | ||
437 | /* Check that server and host key lengths differ sufficiently. | |
438 | This is necessary to make double encryption work with rsaref. | |
439 | Oh, I hate software patents. I dont know if this can go? Niels */ | |
440 | if (options.server_key_bits > | |
441 | BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED && | |
442 | options.server_key_bits < | |
443 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | |
444 | options.server_key_bits = | |
445 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED; | |
446 | debug("Forcing server key to %d bits to make it differ from host key.", | |
447 | options.server_key_bits); | |
8efc0c15 | 448 | } |
5260325f | 449 | /* Do not display messages to stdout in RSA code. */ |
450 | rsa_set_verbose(0); | |
451 | ||
452 | /* Initialize the random number generator. */ | |
453 | arc4random_stir(); | |
454 | ||
455 | /* Chdir to the root directory so that the current disk can be | |
456 | unmounted if desired. */ | |
457 | chdir("/"); | |
458 | ||
459 | /* Close connection cleanly after attack. */ | |
460 | cipher_attack_detected = packet_disconnect; | |
461 | ||
462 | /* Start listening for a socket, unless started from inetd. */ | |
463 | if (inetd_flag) { | |
464 | int s1, s2; | |
465 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ | |
466 | s2 = dup(s1); | |
467 | sock_in = dup(0); | |
468 | sock_out = dup(1); | |
469 | /* We intentionally do not close the descriptors 0, 1, and 2 | |
470 | as our code for setting the descriptors won\'t work | |
471 | if ttyfd happens to be one of those. */ | |
472 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); | |
473 | ||
474 | public_key = RSA_new(); | |
475 | sensitive_data.private_key = RSA_new(); | |
476 | ||
477 | log("Generating %d bit RSA key.", options.server_key_bits); | |
478 | rsa_generate_key(sensitive_data.private_key, public_key, | |
479 | options.server_key_bits); | |
480 | arc4random_stir(); | |
481 | log("RSA key generation complete."); | |
482 | } else { | |
483 | /* Create socket for listening. */ | |
484 | listen_sock = socket(AF_INET, SOCK_STREAM, 0); | |
485 | if (listen_sock < 0) | |
486 | fatal("socket: %.100s", strerror(errno)); | |
487 | ||
488 | /* Set socket options. We try to make the port reusable | |
489 | and have it close as fast as possible without waiting | |
490 | in unnecessary wait states on close. */ | |
491 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, | |
492 | sizeof(on)); | |
493 | linger.l_onoff = 1; | |
494 | linger.l_linger = 5; | |
495 | setsockopt(listen_sock, SOL_SOCKET, SO_LINGER, (void *) &linger, | |
496 | sizeof(linger)); | |
497 | ||
5260325f | 498 | memset(&sin, 0, sizeof(sin)); |
499 | sin.sin_family = AF_INET; | |
500 | sin.sin_addr = options.listen_addr; | |
501 | sin.sin_port = htons(options.port); | |
502 | ||
5260325f | 503 | if (bind(listen_sock, (struct sockaddr *) & sin, sizeof(sin)) < 0) { |
504 | error("bind: %.100s", strerror(errno)); | |
505 | shutdown(listen_sock, SHUT_RDWR); | |
506 | close(listen_sock); | |
507 | fatal("Bind to port %d failed.", options.port); | |
508 | } | |
509 | if (!debug_flag) { | |
aa3378df | 510 | /* |
511 | * Record our pid in /etc/sshd_pid to make it easier | |
512 | * to kill the correct sshd. We don\'t want to do | |
513 | * this before the bind above because the bind will | |
514 | * fail if there already is a daemon, and this will | |
515 | * overwrite any old pid in the file. | |
516 | */ | |
5260325f | 517 | f = fopen(SSH_DAEMON_PID_FILE, "w"); |
518 | if (f) { | |
519 | fprintf(f, "%u\n", (unsigned int) getpid()); | |
520 | fclose(f); | |
521 | } | |
8efc0c15 | 522 | } |
8efc0c15 | 523 | |
5260325f | 524 | log("Server listening on port %d.", options.port); |
525 | if (listen(listen_sock, 5) < 0) | |
526 | fatal("listen: %.100s", strerror(errno)); | |
527 | ||
528 | public_key = RSA_new(); | |
529 | sensitive_data.private_key = RSA_new(); | |
530 | ||
531 | log("Generating %d bit RSA key.", options.server_key_bits); | |
532 | rsa_generate_key(sensitive_data.private_key, public_key, | |
533 | options.server_key_bits); | |
534 | arc4random_stir(); | |
535 | log("RSA key generation complete."); | |
536 | ||
537 | /* Schedule server key regeneration alarm. */ | |
538 | signal(SIGALRM, key_regeneration_alarm); | |
539 | alarm(options.key_regeneration_time); | |
540 | ||
541 | /* Arrange to restart on SIGHUP. The handler needs listen_sock. */ | |
542 | signal(SIGHUP, sighup_handler); | |
543 | signal(SIGTERM, sigterm_handler); | |
544 | signal(SIGQUIT, sigterm_handler); | |
545 | ||
546 | /* Arrange SIGCHLD to be caught. */ | |
547 | signal(SIGCHLD, main_sigchld_handler); | |
548 | ||
aa3378df | 549 | /* |
550 | * Stay listening for connections until the system crashes or | |
551 | * the daemon is killed with a signal. | |
552 | */ | |
5260325f | 553 | for (;;) { |
554 | if (received_sighup) | |
555 | sighup_restart(); | |
2d86a6cc | 556 | /* Wait in poll until there is a connection. */ |
557 | memset(&fds, 0, sizeof(fds)); | |
558 | fds.fd = listen_sock; | |
559 | fds.events = POLLIN; | |
560 | if (poll(&fds, 1, -1) == -1) { | |
561 | if (errno == EINTR) | |
562 | continue; | |
563 | fatal("poll: %.100s", strerror(errno)); | |
564 | /*NOTREACHED*/ | |
565 | } | |
566 | if (fds.revents == 0) | |
567 | continue; | |
5260325f | 568 | aux = sizeof(sin); |
569 | newsock = accept(listen_sock, (struct sockaddr *) & sin, &aux); | |
570 | if (received_sighup) | |
571 | sighup_restart(); | |
572 | if (newsock < 0) { | |
573 | if (errno == EINTR) | |
574 | continue; | |
575 | error("accept: %.100s", strerror(errno)); | |
576 | continue; | |
577 | } | |
aa3378df | 578 | /* |
579 | * Got connection. Fork a child to handle it, unless | |
580 | * we are in debugging mode. | |
581 | */ | |
5260325f | 582 | if (debug_flag) { |
aa3378df | 583 | /* |
584 | * In debugging mode. Close the listening | |
585 | * socket, and start processing the | |
586 | * connection without forking. | |
587 | */ | |
5260325f | 588 | debug("Server will not fork when running in debugging mode."); |
589 | close(listen_sock); | |
590 | sock_in = newsock; | |
591 | sock_out = newsock; | |
592 | pid = getpid(); | |
593 | break; | |
594 | } else { | |
aa3378df | 595 | /* |
596 | * Normal production daemon. Fork, and have | |
597 | * the child process the connection. The | |
598 | * parent continues listening. | |
599 | */ | |
5260325f | 600 | if ((pid = fork()) == 0) { |
aa3378df | 601 | /* |
602 | * Child. Close the listening socket, and start using the | |
603 | * accepted socket. Reinitialize logging (since our pid has | |
604 | * changed). We break out of the loop to handle the connection. | |
605 | */ | |
5260325f | 606 | close(listen_sock); |
607 | sock_in = newsock; | |
608 | sock_out = newsock; | |
609 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
610 | break; | |
611 | } | |
612 | } | |
613 | ||
614 | /* Parent. Stay in the loop. */ | |
615 | if (pid < 0) | |
616 | error("fork: %.100s", strerror(errno)); | |
617 | else | |
618 | debug("Forked child %d.", pid); | |
619 | ||
620 | /* Mark that the key has been used (it was "given" to the child). */ | |
621 | key_used = 1; | |
622 | ||
623 | arc4random_stir(); | |
624 | ||
625 | /* Close the new socket (the child is now taking care of it). */ | |
626 | close(newsock); | |
627 | } | |
628 | } | |
8efc0c15 | 629 | |
5260325f | 630 | /* This is the child processing a new connection. */ |
631 | ||
aa3378df | 632 | /* |
633 | * Disable the key regeneration alarm. We will not regenerate the | |
634 | * key since we are no longer in a position to give it to anyone. We | |
635 | * will not restart on SIGHUP since it no longer makes sense. | |
636 | */ | |
5260325f | 637 | alarm(0); |
638 | signal(SIGALRM, SIG_DFL); | |
639 | signal(SIGHUP, SIG_DFL); | |
640 | signal(SIGTERM, SIG_DFL); | |
641 | signal(SIGQUIT, SIG_DFL); | |
642 | signal(SIGCHLD, SIG_DFL); | |
643 | ||
aa3378df | 644 | /* |
645 | * Set socket options for the connection. We want the socket to | |
646 | * close as fast as possible without waiting for anything. If the | |
647 | * connection is not a socket, these will do nothing. | |
648 | */ | |
649 | /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | |
5260325f | 650 | linger.l_onoff = 1; |
651 | linger.l_linger = 5; | |
652 | setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); | |
653 | ||
aa3378df | 654 | /* |
655 | * Register our connection. This turns encryption off because we do | |
656 | * not have a key. | |
657 | */ | |
5260325f | 658 | packet_set_connection(sock_in, sock_out); |
659 | ||
660 | remote_port = get_remote_port(); | |
661 | remote_ip = get_remote_ipaddr(); | |
662 | ||
663 | /* Check whether logins are denied from this host. */ | |
664 | #ifdef LIBWRAP | |
665 | { | |
666 | struct request_info req; | |
8efc0c15 | 667 | |
5260325f | 668 | request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL); |
669 | fromhost(&req); | |
8efc0c15 | 670 | |
5260325f | 671 | if (!hosts_access(&req)) { |
672 | close(sock_in); | |
673 | close(sock_out); | |
674 | refuse(&req); | |
675 | } | |
676 | verbose("Connection from %.500s port %d", eval_client(&req), remote_port); | |
8efc0c15 | 677 | } |
8efc0c15 | 678 | #else |
5260325f | 679 | /* Log the connection. */ |
680 | verbose("Connection from %.500s port %d", remote_ip, remote_port); | |
8efc0c15 | 681 | #endif /* LIBWRAP */ |
682 | ||
aa3378df | 683 | /* |
684 | * We don\'t want to listen forever unless the other side | |
685 | * successfully authenticates itself. So we set up an alarm which is | |
686 | * cleared after successful authentication. A limit of zero | |
687 | * indicates no limit. Note that we don\'t set the alarm in debugging | |
688 | * mode; it is just annoying to have the server exit just when you | |
689 | * are about to discover the bug. | |
690 | */ | |
5260325f | 691 | signal(SIGALRM, grace_alarm_handler); |
692 | if (!debug_flag) | |
693 | alarm(options.login_grace_time); | |
694 | ||
695 | if (client_version_string != NULL) { | |
696 | /* we are exec'ed by sshd2, so skip exchange of protocol version */ | |
697 | strlcpy(buf, client_version_string, sizeof(buf)); | |
698 | } else { | |
699 | /* Send our protocol version identification. */ | |
700 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", | |
701 | PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION); | |
a408af76 | 702 | if (atomicio(write, sock_out, buf, strlen(buf)) != strlen(buf)) |
5260325f | 703 | fatal("Could not write ident string to %s.", get_remote_ipaddr()); |
704 | ||
705 | /* Read other side\'s version identification. */ | |
706 | for (i = 0; i < sizeof(buf) - 1; i++) { | |
707 | if (read(sock_in, &buf[i], 1) != 1) | |
708 | fatal("Did not receive ident string from %s.", get_remote_ipaddr()); | |
709 | if (buf[i] == '\r') { | |
710 | buf[i] = '\n'; | |
711 | buf[i + 1] = 0; | |
712 | break; | |
713 | } | |
714 | if (buf[i] == '\n') { | |
715 | /* buf[i] == '\n' */ | |
716 | buf[i + 1] = 0; | |
717 | break; | |
718 | } | |
719 | } | |
720 | buf[sizeof(buf) - 1] = 0; | |
8efc0c15 | 721 | } |
5260325f | 722 | |
aa3378df | 723 | /* |
724 | * Check that the versions match. In future this might accept | |
725 | * several versions and set appropriate flags to handle them. | |
726 | */ | |
5260325f | 727 | if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor, |
a408af76 | 728 | remote_version) != 3) { |
729 | char *s = "Protocol mismatch.\n"; | |
730 | ||
731 | (void) atomicio(write, sock_out, s, strlen(s)); | |
5260325f | 732 | close(sock_in); |
733 | close(sock_out); | |
734 | fatal("Bad protocol version identification '%.100s' from %s", | |
735 | buf, get_remote_ipaddr()); | |
736 | } | |
737 | debug("Client protocol version %d.%d; client software version %.100s", | |
738 | remote_major, remote_minor, remote_version); | |
739 | if (remote_major != PROTOCOL_MAJOR) { | |
a408af76 | 740 | char *s = "Protocol major versions differ.\n"; |
741 | ||
742 | (void) atomicio(write, sock_out, s, strlen(s)); | |
5260325f | 743 | close(sock_in); |
744 | close(sock_out); | |
745 | fatal("Protocol major versions differ for %s: %d vs. %d", | |
746 | get_remote_ipaddr(), | |
747 | PROTOCOL_MAJOR, remote_major); | |
8efc0c15 | 748 | } |
5260325f | 749 | /* Check that the client has sufficiently high software version. */ |
750 | if (remote_major == 1 && remote_minor < 3) | |
751 | packet_disconnect("Your ssh version is too old and is no longer supported. Please install a newer version."); | |
752 | ||
753 | if (remote_major == 1 && remote_minor == 3) { | |
754 | enable_compat13(); | |
755 | if (strcmp(remote_version, "OpenSSH-1.1") != 0) { | |
756 | debug("Agent forwarding disabled, remote version is not compatible."); | |
757 | no_agent_forwarding_flag = 1; | |
758 | } | |
759 | } | |
aa3378df | 760 | /* |
761 | * Check that the connection comes from a privileged port. Rhosts- | |
762 | * and Rhosts-RSA-Authentication only make sense from priviledged | |
763 | * programs. Of course, if the intruder has root access on his local | |
764 | * machine, he can connect from any port. So do not use these | |
765 | * authentication methods from machines that you do not trust. | |
766 | */ | |
5260325f | 767 | if (remote_port >= IPPORT_RESERVED || |
768 | remote_port < IPPORT_RESERVED / 2) { | |
769 | options.rhosts_authentication = 0; | |
770 | options.rhosts_rsa_authentication = 0; | |
771 | } | |
772 | packet_set_nonblocking(); | |
773 | ||
774 | /* Handle the connection. */ | |
775 | do_connection(); | |
8efc0c15 | 776 | |
777 | #ifdef KRB4 | |
5260325f | 778 | /* Cleanup user's ticket cache file. */ |
779 | if (options.kerberos_ticket_cleanup) | |
780 | (void) dest_tkt(); | |
8efc0c15 | 781 | #endif /* KRB4 */ |
782 | ||
5260325f | 783 | /* Cleanup user's local Xauthority file. */ |
784 | if (xauthfile) | |
785 | unlink(xauthfile); | |
8efc0c15 | 786 | |
5260325f | 787 | /* The connection has been terminated. */ |
788 | verbose("Closing connection to %.100s", remote_ip); | |
8efc0c15 | 789 | |
d94aa2ae | 790 | #ifdef USE_PAM |
a5c9cd31 | 791 | finish_pam(); |
d94aa2ae | 792 | #endif /* USE_PAM */ |
8efc0c15 | 793 | |
5260325f | 794 | packet_close(); |
795 | exit(0); | |
796 | } | |
8efc0c15 | 797 | |
5260325f | 798 | /* |
799 | * Process an incoming connection. Protocol version identifiers have already | |
800 | * been exchanged. This sends server key and performs the key exchange. | |
801 | * Server and host keys will no longer be needed after this functions. | |
802 | */ | |
e7c0f9d5 | 803 | void |
804 | do_connection() | |
8efc0c15 | 805 | { |
5260325f | 806 | int i, len; |
807 | BIGNUM *session_key_int; | |
808 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; | |
809 | unsigned char check_bytes[8]; | |
810 | char *user; | |
811 | unsigned int cipher_type, auth_mask, protocol_flags; | |
95f1eccc | 812 | int plen, slen, ulen; |
5260325f | 813 | u_int32_t rand = 0; |
814 | ||
aa3378df | 815 | /* |
816 | * Generate check bytes that the client must send back in the user | |
817 | * packet in order for it to be accepted; this is used to defy ip | |
818 | * spoofing attacks. Note that this only works against somebody | |
819 | * doing IP spoofing from a remote machine; any machine on the local | |
820 | * network can still see outgoing packets and catch the random | |
821 | * cookie. This only affects rhosts authentication, and this is one | |
822 | * of the reasons why it is inherently insecure. | |
823 | */ | |
5260325f | 824 | for (i = 0; i < 8; i++) { |
825 | if (i % 4 == 0) | |
826 | rand = arc4random(); | |
827 | check_bytes[i] = rand & 0xff; | |
828 | rand >>= 8; | |
829 | } | |
830 | ||
aa3378df | 831 | /* |
832 | * Send our public key. We include in the packet 64 bits of random | |
833 | * data that must be matched in the reply in order to prevent IP | |
834 | * spoofing. | |
835 | */ | |
5260325f | 836 | packet_start(SSH_SMSG_PUBLIC_KEY); |
837 | for (i = 0; i < 8; i++) | |
838 | packet_put_char(check_bytes[i]); | |
839 | ||
840 | /* Store our public server RSA key. */ | |
841 | packet_put_int(BN_num_bits(public_key->n)); | |
842 | packet_put_bignum(public_key->e); | |
843 | packet_put_bignum(public_key->n); | |
844 | ||
845 | /* Store our public host RSA key. */ | |
846 | packet_put_int(BN_num_bits(sensitive_data.host_key->n)); | |
847 | packet_put_bignum(sensitive_data.host_key->e); | |
848 | packet_put_bignum(sensitive_data.host_key->n); | |
849 | ||
850 | /* Put protocol flags. */ | |
851 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); | |
852 | ||
853 | /* Declare which ciphers we support. */ | |
854 | packet_put_int(cipher_mask()); | |
855 | ||
856 | /* Declare supported authentication types. */ | |
857 | auth_mask = 0; | |
858 | if (options.rhosts_authentication) | |
859 | auth_mask |= 1 << SSH_AUTH_RHOSTS; | |
860 | if (options.rhosts_rsa_authentication) | |
861 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; | |
862 | if (options.rsa_authentication) | |
863 | auth_mask |= 1 << SSH_AUTH_RSA; | |
8efc0c15 | 864 | #ifdef KRB4 |
5260325f | 865 | if (options.kerberos_authentication) |
866 | auth_mask |= 1 << SSH_AUTH_KERBEROS; | |
8efc0c15 | 867 | #endif |
868 | #ifdef AFS | |
5260325f | 869 | if (options.kerberos_tgt_passing) |
870 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; | |
871 | if (options.afs_token_passing) | |
872 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; | |
8efc0c15 | 873 | #endif |
5260325f | 874 | #ifdef SKEY |
875 | if (options.skey_authentication == 1) | |
876 | auth_mask |= 1 << SSH_AUTH_TIS; | |
877 | #endif | |
878 | if (options.password_authentication) | |
879 | auth_mask |= 1 << SSH_AUTH_PASSWORD; | |
880 | packet_put_int(auth_mask); | |
881 | ||
882 | /* Send the packet and wait for it to be sent. */ | |
883 | packet_send(); | |
884 | packet_write_wait(); | |
885 | ||
886 | debug("Sent %d bit public key and %d bit host key.", | |
887 | BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n)); | |
888 | ||
889 | /* Read clients reply (cipher type and session key). */ | |
890 | packet_read_expect(&plen, SSH_CMSG_SESSION_KEY); | |
891 | ||
2d86a6cc | 892 | /* Get cipher type and check whether we accept this. */ |
5260325f | 893 | cipher_type = packet_get_char(); |
894 | ||
2d86a6cc | 895 | if (!(cipher_mask() & (1 << cipher_type))) |
896 | packet_disconnect("Warning: client selects unsupported cipher."); | |
897 | ||
5260325f | 898 | /* Get check bytes from the packet. These must match those we |
899 | sent earlier with the public key packet. */ | |
900 | for (i = 0; i < 8; i++) | |
901 | if (check_bytes[i] != packet_get_char()) | |
902 | packet_disconnect("IP Spoofing check bytes do not match."); | |
903 | ||
904 | debug("Encryption type: %.200s", cipher_name(cipher_type)); | |
905 | ||
906 | /* Get the encrypted integer. */ | |
907 | session_key_int = BN_new(); | |
908 | packet_get_bignum(session_key_int, &slen); | |
909 | ||
5260325f | 910 | protocol_flags = packet_get_int(); |
911 | packet_set_protocol_flags(protocol_flags); | |
912 | ||
913 | packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY); | |
914 | ||
aa3378df | 915 | /* |
916 | * Decrypt it using our private server key and private host key (key | |
917 | * with larger modulus first). | |
918 | */ | |
5260325f | 919 | if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) { |
920 | /* Private key has bigger modulus. */ | |
921 | if (BN_num_bits(sensitive_data.private_key->n) < | |
922 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | |
923 | fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", | |
924 | get_remote_ipaddr(), | |
925 | BN_num_bits(sensitive_data.private_key->n), | |
926 | BN_num_bits(sensitive_data.host_key->n), | |
927 | SSH_KEY_BITS_RESERVED); | |
928 | } | |
929 | rsa_private_decrypt(session_key_int, session_key_int, | |
930 | sensitive_data.private_key); | |
931 | rsa_private_decrypt(session_key_int, session_key_int, | |
932 | sensitive_data.host_key); | |
933 | } else { | |
934 | /* Host key has bigger modulus (or they are equal). */ | |
935 | if (BN_num_bits(sensitive_data.host_key->n) < | |
936 | BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) { | |
937 | fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d", | |
938 | get_remote_ipaddr(), | |
939 | BN_num_bits(sensitive_data.host_key->n), | |
940 | BN_num_bits(sensitive_data.private_key->n), | |
941 | SSH_KEY_BITS_RESERVED); | |
942 | } | |
943 | rsa_private_decrypt(session_key_int, session_key_int, | |
944 | sensitive_data.host_key); | |
945 | rsa_private_decrypt(session_key_int, session_key_int, | |
946 | sensitive_data.private_key); | |
947 | } | |
948 | ||
5260325f | 949 | compute_session_id(session_id, check_bytes, |
950 | sensitive_data.host_key->n, | |
951 | sensitive_data.private_key->n); | |
952 | ||
aa3378df | 953 | /* |
954 | * Extract session key from the decrypted integer. The key is in the | |
955 | * least significant 256 bits of the integer; the first byte of the | |
956 | * key is in the highest bits. | |
957 | */ | |
5260325f | 958 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); |
959 | len = BN_num_bytes(session_key_int); | |
960 | if (len < 0 || len > sizeof(session_key)) | |
961 | fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d", | |
962 | get_remote_ipaddr(), | |
963 | len, sizeof(session_key)); | |
964 | memset(session_key, 0, sizeof(session_key)); | |
965 | BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len); | |
966 | ||
967 | /* Xor the first 16 bytes of the session key with the session id. */ | |
968 | for (i = 0; i < 16; i++) | |
969 | session_key[i] ^= session_id[i]; | |
970 | ||
971 | /* Destroy the decrypted integer. It is no longer needed. */ | |
972 | BN_clear_free(session_key_int); | |
973 | ||
974 | /* Set the session key. From this on all communications will be encrypted. */ | |
975 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); | |
976 | ||
977 | /* Destroy our copy of the session key. It is no longer needed. */ | |
978 | memset(session_key, 0, sizeof(session_key)); | |
979 | ||
980 | debug("Received session key; encryption turned on."); | |
981 | ||
982 | /* Send an acknowledgement packet. Note that this packet is sent encrypted. */ | |
983 | packet_start(SSH_SMSG_SUCCESS); | |
984 | packet_send(); | |
985 | packet_write_wait(); | |
986 | ||
987 | /* Get the name of the user that we wish to log in as. */ | |
988 | packet_read_expect(&plen, SSH_CMSG_USER); | |
989 | ||
990 | /* Get the user name. */ | |
95f1eccc | 991 | user = packet_get_string(&ulen); |
992 | packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER); | |
5260325f | 993 | |
994 | /* Destroy the private and public keys. They will no longer be needed. */ | |
995 | RSA_free(public_key); | |
996 | RSA_free(sensitive_data.private_key); | |
997 | RSA_free(sensitive_data.host_key); | |
8efc0c15 | 998 | |
5260325f | 999 | setproctitle("%s", user); |
1000 | /* Do the authentication. */ | |
1001 | do_authentication(user); | |
1002 | } | |
8efc0c15 | 1003 | |
5260325f | 1004 | /* |
1005 | * Check if the user is allowed to log in via ssh. If user is listed in | |
1006 | * DenyUsers or user's primary group is listed in DenyGroups, false will | |
1007 | * be returned. If AllowUsers isn't empty and user isn't listed there, or | |
1008 | * if AllowGroups isn't empty and user isn't listed there, false will be | |
1009 | * returned. Otherwise true is returned. | |
1010 | * XXX This function should also check if user has a valid shell | |
1011 | */ | |
8efc0c15 | 1012 | static int |
5260325f | 1013 | allowed_user(struct passwd * pw) |
8efc0c15 | 1014 | { |
5260325f | 1015 | struct group *grp; |
1016 | int i; | |
1017 | ||
1018 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ | |
1019 | if (!pw) | |
1020 | return 0; | |
1021 | ||
1022 | /* XXX Should check for valid login shell */ | |
1023 | ||
1024 | /* Return false if user is listed in DenyUsers */ | |
1025 | if (options.num_deny_users > 0) { | |
1026 | if (!pw->pw_name) | |
1027 | return 0; | |
1028 | for (i = 0; i < options.num_deny_users; i++) | |
1029 | if (match_pattern(pw->pw_name, options.deny_users[i])) | |
1030 | return 0; | |
1031 | } | |
aa3378df | 1032 | /* Return false if AllowUsers isn't empty and user isn't listed there */ |
5260325f | 1033 | if (options.num_allow_users > 0) { |
1034 | if (!pw->pw_name) | |
1035 | return 0; | |
1036 | for (i = 0; i < options.num_allow_users; i++) | |
1037 | if (match_pattern(pw->pw_name, options.allow_users[i])) | |
1038 | break; | |
1039 | /* i < options.num_allow_users iff we break for loop */ | |
1040 | if (i >= options.num_allow_users) | |
1041 | return 0; | |
1042 | } | |
1043 | /* Get the primary group name if we need it. Return false if it fails */ | |
1044 | if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { | |
1045 | grp = getgrgid(pw->pw_gid); | |
1046 | if (!grp) | |
1047 | return 0; | |
1048 | ||
1049 | /* Return false if user's group is listed in DenyGroups */ | |
1050 | if (options.num_deny_groups > 0) { | |
1051 | if (!grp->gr_name) | |
1052 | return 0; | |
1053 | for (i = 0; i < options.num_deny_groups; i++) | |
1054 | if (match_pattern(grp->gr_name, options.deny_groups[i])) | |
1055 | return 0; | |
1056 | } | |
aa3378df | 1057 | /* |
1058 | * Return false if AllowGroups isn't empty and user's group | |
1059 | * isn't listed there | |
1060 | */ | |
5260325f | 1061 | if (options.num_allow_groups > 0) { |
1062 | if (!grp->gr_name) | |
1063 | return 0; | |
1064 | for (i = 0; i < options.num_allow_groups; i++) | |
1065 | if (match_pattern(grp->gr_name, options.allow_groups[i])) | |
1066 | break; | |
1067 | /* i < options.num_allow_groups iff we break for | |
1068 | loop */ | |
1069 | if (i >= options.num_allow_groups) | |
1070 | return 0; | |
1071 | } | |
1072 | } | |
1073 | /* We found no reason not to let this user try to log on... */ | |
1074 | return 1; | |
8efc0c15 | 1075 | } |
1076 | ||
5260325f | 1077 | /* |
1078 | * Performs authentication of an incoming connection. Session key has already | |
1079 | * been exchanged and encryption is enabled. User is the user name to log | |
1080 | * in as (received from the client). | |
1081 | */ | |
8efc0c15 | 1082 | void |
e7c0f9d5 | 1083 | do_authentication(char *user) |
8efc0c15 | 1084 | { |
5260325f | 1085 | struct passwd *pw, pwcopy; |
e7c0f9d5 | 1086 | |
8efc0c15 | 1087 | #ifdef AFS |
5260325f | 1088 | /* If machine has AFS, set process authentication group. */ |
1089 | if (k_hasafs()) { | |
1090 | k_setpag(); | |
1091 | k_unlog(); | |
1092 | } | |
8efc0c15 | 1093 | #endif /* AFS */ |
5260325f | 1094 | |
1095 | /* Verify that the user is a valid user. */ | |
1096 | pw = getpwnam(user); | |
1097 | if (!pw || !allowed_user(pw)) | |
1098 | do_fake_authloop(user); | |
1099 | ||
1100 | /* Take a copy of the returned structure. */ | |
1101 | memset(&pwcopy, 0, sizeof(pwcopy)); | |
1102 | pwcopy.pw_name = xstrdup(pw->pw_name); | |
1103 | pwcopy.pw_passwd = xstrdup(pw->pw_passwd); | |
1104 | pwcopy.pw_uid = pw->pw_uid; | |
1105 | pwcopy.pw_gid = pw->pw_gid; | |
1106 | pwcopy.pw_dir = xstrdup(pw->pw_dir); | |
1107 | pwcopy.pw_shell = xstrdup(pw->pw_shell); | |
1108 | pw = &pwcopy; | |
8efc0c15 | 1109 | |
d94aa2ae | 1110 | #ifdef USE_PAM |
a5c9cd31 | 1111 | start_pam(pw); |
5aecb327 | 1112 | #endif |
0183ea1c | 1113 | |
aa3378df | 1114 | /* |
1115 | * If we are not running as root, the user must have the same uid as | |
1116 | * the server. | |
1117 | */ | |
5260325f | 1118 | if (getuid() != 0 && pw->pw_uid != getuid()) |
1119 | packet_disconnect("Cannot change user when server not running as root."); | |
8efc0c15 | 1120 | |
5260325f | 1121 | debug("Attempting authentication for %.100s.", user); |
8efc0c15 | 1122 | |
5260325f | 1123 | /* If the user has no password, accept authentication immediately. */ |
1124 | if (options.password_authentication && | |
8efc0c15 | 1125 | #ifdef KRB4 |
5260325f | 1126 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && |
8efc0c15 | 1127 | #endif /* KRB4 */ |
d94aa2ae | 1128 | #ifdef USE_PAM |
a5c9cd31 | 1129 | auth_pam_password(pw, "")) { |
d94aa2ae | 1130 | #else /* USE_PAM */ |
5260325f | 1131 | auth_password(pw, "")) { |
d94aa2ae | 1132 | #endif /* USE_PAM */ |
5260325f | 1133 | /* Authentication with empty password succeeded. */ |
1134 | log("Login for user %s from %.100s, accepted without authentication.", | |
1135 | pw->pw_name, get_remote_ipaddr()); | |
1136 | } else { | |
1137 | /* Loop until the user has been authenticated or the | |
1138 | connection is closed, do_authloop() returns only if | |
1139 | authentication is successfull */ | |
1140 | do_authloop(pw); | |
1141 | } | |
1142 | ||
1143 | /* Check if the user is logging in as root and root logins are disallowed. */ | |
1144 | if (pw->pw_uid == 0 && !options.permit_root_login) { | |
1145 | if (forced_command) | |
1146 | log("Root login accepted for forced command."); | |
1147 | else | |
1148 | packet_disconnect("ROOT LOGIN REFUSED FROM %.200s", | |
1149 | get_canonical_hostname()); | |
1150 | } | |
1151 | /* The user has been authenticated and accepted. */ | |
1152 | packet_start(SSH_SMSG_SUCCESS); | |
1153 | packet_send(); | |
1154 | packet_write_wait(); | |
1155 | ||
1156 | /* Perform session preparation. */ | |
1157 | do_authenticated(pw); | |
e7c0f9d5 | 1158 | } |
8efc0c15 | 1159 | |
5260325f | 1160 | #define AUTH_FAIL_MAX 6 |
1161 | #define AUTH_FAIL_LOG (AUTH_FAIL_MAX/2) | |
1162 | #define AUTH_FAIL_MSG "Too many authentication failures for %.100s" | |
e7c0f9d5 | 1163 | |
5260325f | 1164 | /* |
1165 | * read packets and try to authenticate local user *pw. | |
1166 | * return if authentication is successfull | |
1167 | */ | |
e7c0f9d5 | 1168 | void |
5260325f | 1169 | do_authloop(struct passwd * pw) |
e7c0f9d5 | 1170 | { |
5260325f | 1171 | int attempt = 0; |
1172 | unsigned int bits; | |
1173 | BIGNUM *client_host_key_e, *client_host_key_n; | |
1174 | BIGNUM *n; | |
1175 | char *client_user = NULL, *password = NULL; | |
1176 | char user[1024]; | |
1177 | int plen, dlen, nlen, ulen, elen; | |
1178 | int type = 0; | |
1179 | void (*authlog) (const char *fmt,...) = verbose; | |
e7c0f9d5 | 1180 | |
5260325f | 1181 | /* Indicate that authentication is needed. */ |
1182 | packet_start(SSH_SMSG_FAILURE); | |
1183 | packet_send(); | |
1184 | packet_write_wait(); | |
e7c0f9d5 | 1185 | |
5260325f | 1186 | for (attempt = 1;; attempt++) { |
1187 | int authenticated = 0; | |
1188 | strlcpy(user, "", sizeof user); | |
e7c0f9d5 | 1189 | |
5260325f | 1190 | /* Get a packet from the client. */ |
1191 | type = packet_read(&plen); | |
1192 | ||
1193 | /* Process the packet. */ | |
1194 | switch (type) { | |
e7c0f9d5 | 1195 | #ifdef AFS |
5260325f | 1196 | case SSH_CMSG_HAVE_KERBEROS_TGT: |
1197 | if (!options.kerberos_tgt_passing) { | |
1198 | /* packet_get_all(); */ | |
1199 | verbose("Kerberos tgt passing disabled."); | |
1200 | break; | |
1201 | } else { | |
1202 | /* Accept Kerberos tgt. */ | |
1203 | char *tgt = packet_get_string(&dlen); | |
1204 | packet_integrity_check(plen, 4 + dlen, type); | |
1205 | if (!auth_kerberos_tgt(pw, tgt)) | |
1206 | verbose("Kerberos tgt REFUSED for %s", pw->pw_name); | |
1207 | xfree(tgt); | |
1208 | } | |
1209 | continue; | |
1210 | ||
1211 | case SSH_CMSG_HAVE_AFS_TOKEN: | |
1212 | if (!options.afs_token_passing || !k_hasafs()) { | |
1213 | /* packet_get_all(); */ | |
1214 | verbose("AFS token passing disabled."); | |
1215 | break; | |
1216 | } else { | |
1217 | /* Accept AFS token. */ | |
1218 | char *token_string = packet_get_string(&dlen); | |
1219 | packet_integrity_check(plen, 4 + dlen, type); | |
1220 | if (!auth_afs_token(pw, token_string)) | |
1221 | verbose("AFS token REFUSED for %s", pw->pw_name); | |
1222 | xfree(token_string); | |
1223 | } | |
1224 | continue; | |
8efc0c15 | 1225 | #endif /* AFS */ |
e7c0f9d5 | 1226 | #ifdef KRB4 |
5260325f | 1227 | case SSH_CMSG_AUTH_KERBEROS: |
1228 | if (!options.kerberos_authentication) { | |
1229 | /* packet_get_all(); */ | |
1230 | verbose("Kerberos authentication disabled."); | |
1231 | break; | |
1232 | } else { | |
1233 | /* Try Kerberos v4 authentication. */ | |
1234 | KTEXT_ST auth; | |
1235 | char *tkt_user = NULL; | |
1236 | char *kdata = packet_get_string((unsigned int *) &auth.length); | |
1237 | packet_integrity_check(plen, 4 + auth.length, type); | |
1238 | ||
1239 | if (auth.length < MAX_KTXT_LEN) | |
1240 | memcpy(auth.dat, kdata, auth.length); | |
1241 | xfree(kdata); | |
1242 | ||
1243 | authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user); | |
1244 | ||
1245 | if (authenticated) { | |
1246 | snprintf(user, sizeof user, " tktuser %s", tkt_user); | |
1247 | xfree(tkt_user); | |
1248 | } | |
1249 | } | |
1250 | break; | |
e7c0f9d5 | 1251 | #endif /* KRB4 */ |
5260325f | 1252 | |
1253 | case SSH_CMSG_AUTH_RHOSTS: | |
1254 | if (!options.rhosts_authentication) { | |
1255 | verbose("Rhosts authentication disabled."); | |
1256 | break; | |
1257 | } | |
aa3378df | 1258 | /* |
1259 | * Get client user name. Note that we just have to | |
1260 | * trust the client; this is one reason why rhosts | |
1261 | * authentication is insecure. (Another is | |
1262 | * IP-spoofing on a local network.) | |
1263 | */ | |
5260325f | 1264 | client_user = packet_get_string(&ulen); |
1265 | packet_integrity_check(plen, 4 + ulen, type); | |
1266 | ||
1267 | /* Try to authenticate using /etc/hosts.equiv and | |
1268 | .rhosts. */ | |
1269 | authenticated = auth_rhosts(pw, client_user); | |
1270 | ||
1271 | snprintf(user, sizeof user, " ruser %s", client_user); | |
5260325f | 1272 | break; |
1273 | ||
1274 | case SSH_CMSG_AUTH_RHOSTS_RSA: | |
1275 | if (!options.rhosts_rsa_authentication) { | |
1276 | verbose("Rhosts with RSA authentication disabled."); | |
1277 | break; | |
1278 | } | |
aa3378df | 1279 | /* |
1280 | * Get client user name. Note that we just have to | |
1281 | * trust the client; root on the client machine can | |
1282 | * claim to be any user. | |
1283 | */ | |
5260325f | 1284 | client_user = packet_get_string(&ulen); |
1285 | ||
1286 | /* Get the client host key. */ | |
1287 | client_host_key_e = BN_new(); | |
1288 | client_host_key_n = BN_new(); | |
1289 | bits = packet_get_int(); | |
1290 | packet_get_bignum(client_host_key_e, &elen); | |
1291 | packet_get_bignum(client_host_key_n, &nlen); | |
1292 | ||
1293 | if (bits != BN_num_bits(client_host_key_n)) | |
1294 | error("Warning: keysize mismatch for client_host_key: " | |
1295 | "actual %d, announced %d", BN_num_bits(client_host_key_n), bits); | |
1296 | packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type); | |
1297 | ||
1298 | authenticated = auth_rhosts_rsa(pw, client_user, | |
1299 | client_host_key_e, client_host_key_n); | |
1300 | BN_clear_free(client_host_key_e); | |
1301 | BN_clear_free(client_host_key_n); | |
1302 | ||
1303 | snprintf(user, sizeof user, " ruser %s", client_user); | |
5260325f | 1304 | break; |
1305 | ||
1306 | case SSH_CMSG_AUTH_RSA: | |
1307 | if (!options.rsa_authentication) { | |
1308 | verbose("RSA authentication disabled."); | |
1309 | break; | |
1310 | } | |
1311 | /* RSA authentication requested. */ | |
1312 | n = BN_new(); | |
1313 | packet_get_bignum(n, &nlen); | |
1314 | packet_integrity_check(plen, nlen, type); | |
1315 | authenticated = auth_rsa(pw, n); | |
1316 | BN_clear_free(n); | |
1317 | break; | |
1318 | ||
1319 | case SSH_CMSG_AUTH_PASSWORD: | |
1320 | if (!options.password_authentication) { | |
1321 | verbose("Password authentication disabled."); | |
1322 | break; | |
1323 | } | |
aa3378df | 1324 | /* |
1325 | * Read user password. It is in plain text, but was | |
1326 | * transmitted over the encrypted channel so it is | |
1327 | * not visible to an outside observer. | |
1328 | */ | |
5260325f | 1329 | password = packet_get_string(&dlen); |
1330 | packet_integrity_check(plen, 4 + dlen, type); | |
1331 | ||
d94aa2ae | 1332 | #ifdef USE_PAM |
5260325f | 1333 | /* Do PAM auth with password */ |
a5c9cd31 | 1334 | authenticated = auth_pam_password(pw, password); |
d94aa2ae | 1335 | #else /* USE_PAM */ |
5260325f | 1336 | /* Try authentication with the password. */ |
1337 | authenticated = auth_password(pw, password); | |
d94aa2ae | 1338 | #endif /* USE_PAM */ |
5260325f | 1339 | memset(password, 0, strlen(password)); |
1340 | xfree(password); | |
1341 | break; | |
5260325f | 1342 | |
1343 | #ifdef SKEY | |
1344 | case SSH_CMSG_AUTH_TIS: | |
1345 | debug("rcvd SSH_CMSG_AUTH_TIS"); | |
1346 | if (options.skey_authentication == 1) { | |
1347 | char *skeyinfo = skey_keyinfo(pw->pw_name); | |
1348 | if (skeyinfo == NULL) { | |
1349 | debug("generating fake skeyinfo for %.100s.", pw->pw_name); | |
1350 | skeyinfo = skey_fake_keyinfo(pw->pw_name); | |
1351 | } | |
1352 | if (skeyinfo != NULL) { | |
aa3378df | 1353 | /* we send our s/key- in tis-challenge messages */ |
5260325f | 1354 | debug("sending challenge '%s'", skeyinfo); |
1355 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); | |
1356 | packet_put_string(skeyinfo, strlen(skeyinfo)); | |
1357 | packet_send(); | |
1358 | packet_write_wait(); | |
1359 | continue; | |
1360 | } | |
1361 | } | |
1362 | break; | |
1363 | case SSH_CMSG_AUTH_TIS_RESPONSE: | |
1364 | debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); | |
1365 | if (options.skey_authentication == 1) { | |
1366 | char *response = packet_get_string(&dlen); | |
1367 | debug("skey response == '%s'", response); | |
1368 | packet_integrity_check(plen, 4 + dlen, type); | |
1369 | authenticated = (skey_haskey(pw->pw_name) == 0 && | |
1370 | skey_passcheck(pw->pw_name, response) != -1); | |
1371 | xfree(response); | |
1372 | } | |
1373 | break; | |
1374 | #else | |
1375 | case SSH_CMSG_AUTH_TIS: | |
1376 | /* TIS Authentication is unsupported */ | |
1377 | log("TIS authentication unsupported."); | |
1378 | break; | |
1379 | #endif | |
1380 | ||
1381 | default: | |
aa3378df | 1382 | /* |
1383 | * Any unknown messages will be ignored (and failure | |
1384 | * returned) during authentication. | |
1385 | */ | |
5260325f | 1386 | log("Unknown message during authentication: type %d", type); |
1387 | break; | |
1388 | } | |
1389 | ||
1390 | /* Raise logging level */ | |
1391 | if (authenticated || | |
1392 | attempt == AUTH_FAIL_LOG || | |
1393 | type == SSH_CMSG_AUTH_PASSWORD) | |
1394 | authlog = log; | |
1395 | ||
1396 | authlog("%s %s for %.200s from %.200s port %d%s", | |
1397 | authenticated ? "Accepted" : "Failed", | |
1398 | get_authname(type), | |
1399 | pw->pw_uid == 0 ? "ROOT" : pw->pw_name, | |
1400 | get_remote_ipaddr(), | |
1401 | get_remote_port(), | |
1402 | user); | |
8efc0c15 | 1403 | |
d465f2ca | 1404 | if (authenticated) { |
a5c9cd31 | 1405 | #ifdef USE_PAM |
1406 | if (!do_pam_account(pw->pw_name, client_user)) | |
1407 | { | |
1408 | if (client_user != NULL) | |
1409 | xfree(client_user); | |
5260325f | 1410 | |
a5c9cd31 | 1411 | do_fake_authloop(pw->pw_name); |
1412 | } | |
1413 | #endif /* USE_PAM */ | |
5260325f | 1414 | return; |
d465f2ca | 1415 | } |
5260325f | 1416 | |
a5c9cd31 | 1417 | if (client_user != NULL) |
1418 | xfree(client_user); | |
d465f2ca | 1419 | |
a5c9cd31 | 1420 | if (attempt > AUTH_FAIL_MAX) |
5260325f | 1421 | packet_disconnect(AUTH_FAIL_MSG, pw->pw_name); |
1422 | ||
1423 | /* Send a message indicating that the authentication attempt failed. */ | |
1424 | packet_start(SSH_SMSG_FAILURE); | |
1425 | packet_send(); | |
1426 | packet_write_wait(); | |
1427 | } | |
8efc0c15 | 1428 | } |
1429 | ||
5260325f | 1430 | /* |
1431 | * The user does not exist or access is denied, | |
1432 | * but fake indication that authentication is needed. | |
1433 | */ | |
e7c0f9d5 | 1434 | void |
1435 | do_fake_authloop(char *user) | |
0183ea1c | 1436 | { |
5260325f | 1437 | int attempt = 0; |
1438 | ||
1439 | log("Faking authloop for illegal user %.200s from %.200s port %d", | |
1440 | user, | |
1441 | get_remote_ipaddr(), | |
1442 | get_remote_port()); | |
1443 | ||
1444 | /* Indicate that authentication is needed. */ | |
1445 | packet_start(SSH_SMSG_FAILURE); | |
1446 | packet_send(); | |
1447 | packet_write_wait(); | |
1448 | ||
aa3378df | 1449 | /* |
1450 | * Keep reading packets, and always respond with a failure. This is | |
1451 | * to avoid disclosing whether such a user really exists. | |
1452 | */ | |
5260325f | 1453 | for (attempt = 1;; attempt++) { |
aa3378df | 1454 | /* Read a packet. This will not return if the client disconnects. */ |
5260325f | 1455 | int plen; |
a5c9cd31 | 1456 | #ifndef SKEY |
1457 | (void)packet_read(&plen); | |
1458 | #else /* SKEY */ | |
5260325f | 1459 | int type = packet_read(&plen); |
5260325f | 1460 | int dlen; |
1461 | char *password, *skeyinfo; | |
95f1eccc | 1462 | /* Try to send a fake s/key challenge. */ |
1463 | if (options.skey_authentication == 1 && | |
5260325f | 1464 | (skeyinfo = skey_fake_keyinfo(user)) != NULL) { |
95f1eccc | 1465 | if (type == SSH_CMSG_AUTH_TIS) { |
1466 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); | |
1467 | packet_put_string(skeyinfo, strlen(skeyinfo)); | |
1468 | packet_send(); | |
1469 | packet_write_wait(); | |
1470 | continue; | |
1471 | } else if (type == SSH_CMSG_AUTH_PASSWORD && | |
1472 | options.password_authentication && | |
1473 | (password = packet_get_string(&dlen)) != NULL && | |
1474 | dlen == 5 && | |
1475 | strncasecmp(password, "s/key", 5) == 0 ) { | |
1476 | packet_send_debug(skeyinfo); | |
1477 | } | |
5260325f | 1478 | } |
e7c0f9d5 | 1479 | #endif |
5260325f | 1480 | if (attempt > AUTH_FAIL_MAX) |
1481 | packet_disconnect(AUTH_FAIL_MSG, user); | |
1482 | ||
aa3378df | 1483 | /* |
1484 | * Send failure. This should be indistinguishable from a | |
1485 | * failed authentication. | |
1486 | */ | |
5260325f | 1487 | packet_start(SSH_SMSG_FAILURE); |
1488 | packet_send(); | |
1489 | packet_write_wait(); | |
1490 | } | |
1491 | /* NOTREACHED */ | |
1492 | abort(); | |
0183ea1c | 1493 | } |
1494 | ||
e7c0f9d5 | 1495 | |
5260325f | 1496 | /* |
1497 | * Remove local Xauthority file. | |
1498 | */ | |
6a17f9c2 | 1499 | static void |
1500 | xauthfile_cleanup_proc(void *ignore) | |
1501 | { | |
5260325f | 1502 | debug("xauthfile_cleanup_proc called"); |
6a17f9c2 | 1503 | |
5260325f | 1504 | if (xauthfile != NULL) { |
1505 | unlink(xauthfile); | |
1506 | xfree(xauthfile); | |
1507 | xauthfile = NULL; | |
1508 | } | |
6a17f9c2 | 1509 | } |
1510 | ||
5260325f | 1511 | /* |
1512 | * Prepares for an interactive session. This is called after the user has | |
1513 | * been successfully authenticated. During this message exchange, pseudo | |
1514 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings | |
1515 | * are requested, etc. | |
1516 | */ | |
1517 | void | |
1518 | do_authenticated(struct passwd * pw) | |
8efc0c15 | 1519 | { |
5260325f | 1520 | int type; |
1521 | int compression_level = 0, enable_compression_after_reply = 0; | |
1522 | int have_pty = 0, ptyfd = -1, ttyfd = -1, xauthfd = -1; | |
1523 | int row, col, xpixel, ypixel, screen; | |
1524 | char ttyname[64]; | |
1525 | char *command, *term = NULL, *display = NULL, *proto = NULL, | |
1526 | *data = NULL; | |
1527 | struct group *grp; | |
1528 | gid_t tty_gid; | |
1529 | mode_t tty_mode; | |
1530 | int n_bytes; | |
1531 | ||
aa3378df | 1532 | /* |
1533 | * Cancel the alarm we set to limit the time taken for | |
1534 | * authentication. | |
1535 | */ | |
5260325f | 1536 | alarm(0); |
1537 | ||
aa3378df | 1538 | /* |
1539 | * Inform the channel mechanism that we are the server side and that | |
1540 | * the client may request to connect to any port at all. (The user | |
1541 | * could do it anyway, and we wouldn\'t know what is permitted except | |
1542 | * by the client telling us, so we can equally well trust the client | |
1543 | * not to request anything bogus.) | |
1544 | */ | |
5260325f | 1545 | channel_permit_all_opens(); |
1546 | ||
aa3378df | 1547 | /* |
1548 | * We stay in this loop until the client requests to execute a shell | |
1549 | * or a command. | |
1550 | */ | |
5260325f | 1551 | while (1) { |
1552 | int plen, dlen; | |
1553 | ||
1554 | /* Get a packet from the client. */ | |
1555 | type = packet_read(&plen); | |
1556 | ||
1557 | /* Process the packet. */ | |
1558 | switch (type) { | |
1559 | case SSH_CMSG_REQUEST_COMPRESSION: | |
1560 | packet_integrity_check(plen, 4, type); | |
1561 | compression_level = packet_get_int(); | |
1562 | if (compression_level < 1 || compression_level > 9) { | |
1563 | packet_send_debug("Received illegal compression level %d.", | |
1564 | compression_level); | |
1565 | goto fail; | |
1566 | } | |
1567 | /* Enable compression after we have responded with SUCCESS. */ | |
1568 | enable_compression_after_reply = 1; | |
1569 | break; | |
1570 | ||
1571 | case SSH_CMSG_REQUEST_PTY: | |
1572 | if (no_pty_flag) { | |
1573 | debug("Allocating a pty not permitted for this authentication."); | |
1574 | goto fail; | |
1575 | } | |
1576 | if (have_pty) | |
1577 | packet_disconnect("Protocol error: you already have a pty."); | |
1578 | ||
1579 | debug("Allocating pty."); | |
1580 | ||
1581 | /* Allocate a pty and open it. */ | |
a408af76 | 1582 | if (!pty_allocate(&ptyfd, &ttyfd, ttyname, |
1583 | sizeof(ttyname))) { | |
5260325f | 1584 | error("Failed to allocate pty."); |
1585 | goto fail; | |
1586 | } | |
1587 | /* Determine the group to make the owner of the tty. */ | |
1588 | grp = getgrnam("tty"); | |
1589 | if (grp) { | |
1590 | tty_gid = grp->gr_gid; | |
1591 | tty_mode = S_IRUSR | S_IWUSR | S_IWGRP; | |
1592 | } else { | |
1593 | tty_gid = pw->pw_gid; | |
1594 | tty_mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; | |
1595 | } | |
1596 | ||
1597 | /* Change ownership of the tty. */ | |
1598 | if (chown(ttyname, pw->pw_uid, tty_gid) < 0) | |
1599 | fatal("chown(%.100s, %d, %d) failed: %.100s", | |
1600 | ttyname, pw->pw_uid, tty_gid, strerror(errno)); | |
1601 | if (chmod(ttyname, tty_mode) < 0) | |
1602 | fatal("chmod(%.100s, 0%o) failed: %.100s", | |
1603 | ttyname, tty_mode, strerror(errno)); | |
1604 | ||
1605 | /* Get TERM from the packet. Note that the value may be of arbitrary length. */ | |
1606 | term = packet_get_string(&dlen); | |
1607 | packet_integrity_check(dlen, strlen(term), type); | |
1608 | /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */ | |
1609 | /* Remaining bytes */ | |
1610 | n_bytes = plen - (4 + dlen + 4 * 4); | |
1611 | ||
1612 | if (strcmp(term, "") == 0) | |
1613 | term = NULL; | |
1614 | ||
1615 | /* Get window size from the packet. */ | |
1616 | row = packet_get_int(); | |
1617 | col = packet_get_int(); | |
1618 | xpixel = packet_get_int(); | |
1619 | ypixel = packet_get_int(); | |
1620 | pty_change_window_size(ptyfd, row, col, xpixel, ypixel); | |
1621 | ||
1622 | /* Get tty modes from the packet. */ | |
1623 | tty_parse_modes(ttyfd, &n_bytes); | |
1624 | packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type); | |
1625 | ||
1626 | /* Indicate that we now have a pty. */ | |
1627 | have_pty = 1; | |
8946db53 | 1628 | |
d94aa2ae | 1629 | #ifdef USE_PAM |
8946db53 | 1630 | /* do the pam_open_session since we have the pty */ |
a5c9cd31 | 1631 | do_pam_session(pw->pw_name, ttyname); |
d94aa2ae | 1632 | #endif /* USE_PAM */ |
8946db53 | 1633 | |
5260325f | 1634 | break; |
1635 | ||
1636 | case SSH_CMSG_X11_REQUEST_FORWARDING: | |
1637 | if (!options.x11_forwarding) { | |
1638 | packet_send_debug("X11 forwarding disabled in server configuration file."); | |
1639 | goto fail; | |
1640 | } | |
8efc0c15 | 1641 | #ifdef XAUTH_PATH |
5260325f | 1642 | if (no_x11_forwarding_flag) { |
1643 | packet_send_debug("X11 forwarding not permitted for this authentication."); | |
1644 | goto fail; | |
1645 | } | |
1646 | debug("Received request for X11 forwarding with auth spoofing."); | |
1647 | if (display) | |
1648 | packet_disconnect("Protocol error: X11 display already set."); | |
1649 | { | |
1650 | int proto_len, data_len; | |
1651 | proto = packet_get_string(&proto_len); | |
1652 | data = packet_get_string(&data_len); | |
1653 | packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type); | |
1654 | } | |
1655 | if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER) | |
1656 | screen = packet_get_int(); | |
1657 | else | |
1658 | screen = 0; | |
95f1eccc | 1659 | display = x11_create_display_inet(screen, options.x11_display_offset); |
5260325f | 1660 | if (!display) |
1661 | goto fail; | |
1662 | ||
1663 | /* Setup to always have a local .Xauthority. */ | |
1664 | xauthfile = xmalloc(MAXPATHLEN); | |
1665 | snprintf(xauthfile, MAXPATHLEN, "/tmp/XauthXXXXXX"); | |
1666 | ||
1667 | if ((xauthfd = mkstemp(xauthfile)) != -1) { | |
1668 | fchown(xauthfd, pw->pw_uid, pw->pw_gid); | |
1669 | close(xauthfd); | |
1670 | fatal_add_cleanup(xauthfile_cleanup_proc, NULL); | |
1671 | } else { | |
1672 | xfree(xauthfile); | |
1673 | xauthfile = NULL; | |
1674 | } | |
1675 | break; | |
8efc0c15 | 1676 | #else /* XAUTH_PATH */ |
5260325f | 1677 | packet_send_debug("No xauth program; cannot forward with spoofing."); |
1678 | goto fail; | |
8efc0c15 | 1679 | #endif /* XAUTH_PATH */ |
1680 | ||
5260325f | 1681 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: |
1682 | if (no_agent_forwarding_flag) { | |
1683 | debug("Authentication agent forwarding not permitted for this authentication."); | |
1684 | goto fail; | |
1685 | } | |
1686 | debug("Received authentication agent forwarding request."); | |
1687 | auth_input_request_forwarding(pw); | |
1688 | break; | |
1689 | ||
1690 | case SSH_CMSG_PORT_FORWARD_REQUEST: | |
1691 | if (no_port_forwarding_flag) { | |
1692 | debug("Port forwarding not permitted for this authentication."); | |
1693 | goto fail; | |
1694 | } | |
1695 | debug("Received TCP/IP port forwarding request."); | |
1696 | channel_input_port_forward_request(pw->pw_uid == 0); | |
1697 | break; | |
1698 | ||
1699 | case SSH_CMSG_MAX_PACKET_SIZE: | |
1700 | if (packet_set_maxsize(packet_get_int()) < 0) | |
1701 | goto fail; | |
1702 | break; | |
1703 | ||
1704 | case SSH_CMSG_EXEC_SHELL: | |
1705 | /* Set interactive/non-interactive mode. */ | |
1706 | packet_set_interactive(have_pty || display != NULL, | |
1707 | options.keepalives); | |
1708 | ||
f91bacbd | 1709 | #ifdef USE_PAM |
1710 | do_pam_setcred(); | |
a5c9cd31 | 1711 | #endif /* USE_PAM */ |
5260325f | 1712 | if (forced_command != NULL) |
1713 | goto do_forced_command; | |
1714 | debug("Forking shell."); | |
1715 | packet_integrity_check(plen, 0, type); | |
1716 | if (have_pty) | |
1717 | do_exec_pty(NULL, ptyfd, ttyfd, ttyname, pw, term, display, proto, data); | |
1718 | else | |
1719 | do_exec_no_pty(NULL, pw, display, proto, data); | |
1720 | return; | |
1721 | ||
1722 | case SSH_CMSG_EXEC_CMD: | |
1723 | /* Set interactive/non-interactive mode. */ | |
1724 | packet_set_interactive(have_pty || display != NULL, | |
1725 | options.keepalives); | |
1726 | ||
f91bacbd | 1727 | #ifdef USE_PAM |
1728 | do_pam_setcred(); | |
a5c9cd31 | 1729 | #endif /* USE_PAM */ |
5260325f | 1730 | if (forced_command != NULL) |
1731 | goto do_forced_command; | |
1732 | /* Get command from the packet. */ | |
1733 | { | |
1734 | int dlen; | |
1735 | command = packet_get_string(&dlen); | |
1736 | debug("Executing command '%.500s'", command); | |
1737 | packet_integrity_check(plen, 4 + dlen, type); | |
1738 | } | |
1739 | if (have_pty) | |
1740 | do_exec_pty(command, ptyfd, ttyfd, ttyname, pw, term, display, proto, data); | |
1741 | else | |
1742 | do_exec_no_pty(command, pw, display, proto, data); | |
1743 | xfree(command); | |
1744 | return; | |
1745 | ||
1746 | default: | |
aa3378df | 1747 | /* |
1748 | * Any unknown messages in this phase are ignored, | |
1749 | * and a failure message is returned. | |
1750 | */ | |
5260325f | 1751 | log("Unknown packet type received after authentication: %d", type); |
1752 | goto fail; | |
1753 | } | |
8efc0c15 | 1754 | |
5260325f | 1755 | /* The request was successfully processed. */ |
1756 | packet_start(SSH_SMSG_SUCCESS); | |
1757 | packet_send(); | |
1758 | packet_write_wait(); | |
8efc0c15 | 1759 | |
5260325f | 1760 | /* Enable compression now that we have replied if appropriate. */ |
1761 | if (enable_compression_after_reply) { | |
1762 | enable_compression_after_reply = 0; | |
1763 | packet_start_compression(compression_level); | |
1764 | } | |
1765 | continue; | |
8efc0c15 | 1766 | |
5260325f | 1767 | fail: |
1768 | /* The request failed. */ | |
1769 | packet_start(SSH_SMSG_FAILURE); | |
1770 | packet_send(); | |
1771 | packet_write_wait(); | |
1772 | continue; | |
8efc0c15 | 1773 | |
5260325f | 1774 | do_forced_command: |
aa3378df | 1775 | /* |
1776 | * There is a forced command specified for this login. | |
1777 | * Execute it. | |
1778 | */ | |
5260325f | 1779 | debug("Executing forced command: %.900s", forced_command); |
1780 | if (have_pty) | |
1781 | do_exec_pty(forced_command, ptyfd, ttyfd, ttyname, pw, term, display, proto, data); | |
1782 | else | |
1783 | do_exec_no_pty(forced_command, pw, display, proto, data); | |
1784 | return; | |
1785 | } | |
1786 | } | |
8efc0c15 | 1787 | |
5260325f | 1788 | /* |
1789 | * This is called to fork and execute a command when we have no tty. This | |
1790 | * will call do_child from the child, and server_loop from the parent after | |
1791 | * setting up file descriptors and such. | |
1792 | */ | |
1793 | void | |
1794 | do_exec_no_pty(const char *command, struct passwd * pw, | |
1795 | const char *display, const char *auth_proto, | |
1796 | const char *auth_data) | |
1797 | { | |
1798 | int pid; | |
8efc0c15 | 1799 | |
1800 | #ifdef USE_PIPES | |
5260325f | 1801 | int pin[2], pout[2], perr[2]; |
1802 | /* Allocate pipes for communicating with the program. */ | |
1803 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) | |
1804 | packet_disconnect("Could not create pipes: %.100s", | |
1805 | strerror(errno)); | |
8efc0c15 | 1806 | #else /* USE_PIPES */ |
5260325f | 1807 | int inout[2], err[2]; |
1808 | /* Uses socket pairs to communicate with the program. */ | |
1809 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || | |
1810 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) | |
1811 | packet_disconnect("Could not create socket pairs: %.100s", | |
1812 | strerror(errno)); | |
8efc0c15 | 1813 | #endif /* USE_PIPES */ |
8efc0c15 | 1814 | |
5260325f | 1815 | setproctitle("%s@notty", pw->pw_name); |
8efc0c15 | 1816 | |
5260325f | 1817 | /* Fork the child. */ |
1818 | if ((pid = fork()) == 0) { | |
1819 | /* Child. Reinitialize the log since the pid has changed. */ | |
1820 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
1821 | ||
aa3378df | 1822 | /* |
1823 | * Create a new session and process group since the 4.4BSD | |
1824 | * setlogin() affects the entire process group. | |
1825 | */ | |
5260325f | 1826 | if (setsid() < 0) |
1827 | error("setsid failed: %.100s", strerror(errno)); | |
8efc0c15 | 1828 | |
1829 | #ifdef USE_PIPES | |
aa3378df | 1830 | /* |
1831 | * Redirect stdin. We close the parent side of the socket | |
1832 | * pair, and make the child side the standard input. | |
1833 | */ | |
5260325f | 1834 | close(pin[1]); |
1835 | if (dup2(pin[0], 0) < 0) | |
1836 | perror("dup2 stdin"); | |
1837 | close(pin[0]); | |
1838 | ||
1839 | /* Redirect stdout. */ | |
1840 | close(pout[0]); | |
1841 | if (dup2(pout[1], 1) < 0) | |
1842 | perror("dup2 stdout"); | |
1843 | close(pout[1]); | |
1844 | ||
1845 | /* Redirect stderr. */ | |
1846 | close(perr[0]); | |
1847 | if (dup2(perr[1], 2) < 0) | |
1848 | perror("dup2 stderr"); | |
1849 | close(perr[1]); | |
8efc0c15 | 1850 | #else /* USE_PIPES */ |
aa3378df | 1851 | /* |
1852 | * Redirect stdin, stdout, and stderr. Stdin and stdout will | |
1853 | * use the same socket, as some programs (particularly rdist) | |
1854 | * seem to depend on it. | |
1855 | */ | |
5260325f | 1856 | close(inout[1]); |
1857 | close(err[1]); | |
1858 | if (dup2(inout[0], 0) < 0) /* stdin */ | |
1859 | perror("dup2 stdin"); | |
1860 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ | |
1861 | perror("dup2 stdout"); | |
1862 | if (dup2(err[0], 2) < 0) /* stderr */ | |
1863 | perror("dup2 stderr"); | |
8efc0c15 | 1864 | #endif /* USE_PIPES */ |
1865 | ||
5260325f | 1866 | /* Do processing for the child (exec command etc). */ |
1867 | do_child(command, pw, NULL, display, auth_proto, auth_data, NULL); | |
1868 | /* NOTREACHED */ | |
1869 | } | |
1870 | if (pid < 0) | |
1871 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
8efc0c15 | 1872 | #ifdef USE_PIPES |
5260325f | 1873 | /* We are the parent. Close the child sides of the pipes. */ |
1874 | close(pin[0]); | |
1875 | close(pout[1]); | |
1876 | close(perr[1]); | |
1877 | ||
1878 | /* Enter the interactive session. */ | |
1879 | server_loop(pid, pin[1], pout[0], perr[0]); | |
1880 | /* server_loop has closed pin[1], pout[1], and perr[1]. */ | |
8efc0c15 | 1881 | #else /* USE_PIPES */ |
5260325f | 1882 | /* We are the parent. Close the child sides of the socket pairs. */ |
1883 | close(inout[0]); | |
1884 | close(err[0]); | |
1885 | ||
aa3378df | 1886 | /* |
1887 | * Enter the interactive session. Note: server_loop must be able to | |
1888 | * handle the case that fdin and fdout are the same. | |
1889 | */ | |
5260325f | 1890 | server_loop(pid, inout[1], inout[1], err[1]); |
1891 | /* server_loop has closed inout[1] and err[1]. */ | |
8efc0c15 | 1892 | #endif /* USE_PIPES */ |
1893 | } | |
1894 | ||
5260325f | 1895 | struct pty_cleanup_context { |
1896 | const char *ttyname; | |
1897 | int pid; | |
8efc0c15 | 1898 | }; |
1899 | ||
5260325f | 1900 | /* |
1901 | * Function to perform cleanup if we get aborted abnormally (e.g., due to a | |
1902 | * dropped connection). | |
1903 | */ | |
1904 | void | |
1905 | pty_cleanup_proc(void *context) | |
8efc0c15 | 1906 | { |
5260325f | 1907 | struct pty_cleanup_context *cu = context; |
8efc0c15 | 1908 | |
5260325f | 1909 | debug("pty_cleanup_proc called"); |
8efc0c15 | 1910 | |
5260325f | 1911 | /* Record that the user has logged out. */ |
1912 | record_logout(cu->pid, cu->ttyname); | |
8efc0c15 | 1913 | |
5260325f | 1914 | /* Release the pseudo-tty. */ |
1915 | pty_release(cu->ttyname); | |
8efc0c15 | 1916 | } |
1917 | ||
5260325f | 1918 | /* |
1919 | * This is called to fork and execute a command when we have a tty. This | |
1920 | * will call do_child from the child, and server_loop from the parent after | |
1921 | * setting up file descriptors, controlling tty, updating wtmp, utmp, | |
1922 | * lastlog, and other such operations. | |
1923 | */ | |
1924 | void | |
1925 | do_exec_pty(const char *command, int ptyfd, int ttyfd, | |
1926 | const char *ttyname, struct passwd * pw, const char *term, | |
1927 | const char *display, const char *auth_proto, | |
1928 | const char *auth_data) | |
8efc0c15 | 1929 | { |
5260325f | 1930 | int pid, fdout; |
1931 | const char *hostname; | |
1932 | time_t last_login_time; | |
1933 | char buf[100], *time_string; | |
1934 | FILE *f; | |
1935 | char line[256]; | |
1936 | struct stat st; | |
1937 | int quiet_login; | |
1938 | struct sockaddr_in from; | |
1939 | int fromlen; | |
1940 | struct pty_cleanup_context cleanup_context; | |
1941 | ||
1942 | /* Get remote host name. */ | |
1943 | hostname = get_canonical_hostname(); | |
1944 | ||
aa3378df | 1945 | /* |
1946 | * Get the time when the user last logged in. Buf will be set to | |
1947 | * contain the hostname the last login was from. | |
1948 | */ | |
5260325f | 1949 | if (!options.use_login) { |
1950 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, | |
1951 | buf, sizeof(buf)); | |
8efc0c15 | 1952 | } |
5260325f | 1953 | setproctitle("%s@%s", pw->pw_name, strrchr(ttyname, '/') + 1); |
1954 | ||
1955 | /* Fork the child. */ | |
1956 | if ((pid = fork()) == 0) { | |
1957 | pid = getpid(); | |
1958 | ||
1959 | /* Child. Reinitialize the log because the pid has | |
1960 | changed. */ | |
1961 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
1962 | ||
1963 | /* Close the master side of the pseudo tty. */ | |
1964 | close(ptyfd); | |
1965 | ||
1966 | /* Make the pseudo tty our controlling tty. */ | |
1967 | pty_make_controlling_tty(&ttyfd, ttyname); | |
1968 | ||
1969 | /* Redirect stdin from the pseudo tty. */ | |
1970 | if (dup2(ttyfd, fileno(stdin)) < 0) | |
1971 | error("dup2 stdin failed: %.100s", strerror(errno)); | |
1972 | ||
1973 | /* Redirect stdout to the pseudo tty. */ | |
1974 | if (dup2(ttyfd, fileno(stdout)) < 0) | |
1975 | error("dup2 stdin failed: %.100s", strerror(errno)); | |
1976 | ||
1977 | /* Redirect stderr to the pseudo tty. */ | |
1978 | if (dup2(ttyfd, fileno(stderr)) < 0) | |
1979 | error("dup2 stdin failed: %.100s", strerror(errno)); | |
1980 | ||
1981 | /* Close the extra descriptor for the pseudo tty. */ | |
1982 | close(ttyfd); | |
1983 | ||
aa3378df | 1984 | /* |
1985 | * Get IP address of client. This is needed because we want | |
1986 | * to record where the user logged in from. If the | |
1987 | * connection is not a socket, let the ip address be 0.0.0.0. | |
1988 | */ | |
5260325f | 1989 | memset(&from, 0, sizeof(from)); |
1990 | if (packet_get_connection_in() == packet_get_connection_out()) { | |
1991 | fromlen = sizeof(from); | |
1992 | if (getpeername(packet_get_connection_in(), | |
1993 | (struct sockaddr *) & from, &fromlen) < 0) { | |
1994 | debug("getpeername: %.100s", strerror(errno)); | |
1995 | fatal_cleanup(); | |
1996 | } | |
1997 | } | |
1998 | /* Record that there was a login on that terminal. */ | |
1999 | record_login(pid, ttyname, pw->pw_name, pw->pw_uid, hostname, | |
2000 | &from); | |
8efc0c15 | 2001 | |
5260325f | 2002 | /* Check if .hushlogin exists. */ |
2003 | snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir); | |
2004 | quiet_login = stat(line, &st) >= 0; | |
e1a9c08d | 2005 | |
d94aa2ae | 2006 | #ifdef USE_PAM |
a5c9cd31 | 2007 | if (!quiet_login) |
2008 | print_pam_messages(); | |
2009 | #endif /* USE_PAM */ | |
8efc0c15 | 2010 | |
aa3378df | 2011 | /* |
2012 | * If the user has logged in before, display the time of last | |
2013 | * login. However, don't display anything extra if a command | |
2014 | * has been specified (so that ssh can be used to execute | |
2015 | * commands on a remote machine without users knowing they | |
2016 | * are going to another machine). Login(1) will do this for | |
2017 | * us as well, so check if login(1) is used | |
2018 | */ | |
5260325f | 2019 | if (command == NULL && last_login_time != 0 && !quiet_login && |
2020 | !options.use_login) { | |
2021 | /* Convert the date to a string. */ | |
2022 | time_string = ctime(&last_login_time); | |
2023 | /* Remove the trailing newline. */ | |
2024 | if (strchr(time_string, '\n')) | |
2025 | *strchr(time_string, '\n') = 0; | |
2026 | /* Display the last login time. Host if displayed | |
2027 | if known. */ | |
2028 | if (strcmp(buf, "") == 0) | |
2029 | printf("Last login: %s\r\n", time_string); | |
2030 | else | |
2031 | printf("Last login: %s from %s\r\n", time_string, buf); | |
2032 | } | |
aa3378df | 2033 | /* |
2034 | * Print /etc/motd unless a command was specified or printing | |
2035 | * it was disabled in server options or login(1) will be | |
2036 | * used. Note that some machines appear to print it in | |
2037 | * /etc/profile or similar. | |
2038 | */ | |
5260325f | 2039 | if (command == NULL && options.print_motd && !quiet_login && |
2040 | !options.use_login) { | |
2041 | /* Print /etc/motd if it exists. */ | |
2042 | f = fopen("/etc/motd", "r"); | |
2043 | if (f) { | |
2044 | while (fgets(line, sizeof(line), f)) | |
2045 | fputs(line, stdout); | |
2046 | fclose(f); | |
2047 | } | |
2048 | } | |
2049 | /* Do common processing for the child, such as execing the command. */ | |
2050 | do_child(command, pw, term, display, auth_proto, auth_data, ttyname); | |
2051 | /* NOTREACHED */ | |
8efc0c15 | 2052 | } |
5260325f | 2053 | if (pid < 0) |
2054 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
2055 | /* Parent. Close the slave side of the pseudo tty. */ | |
2056 | close(ttyfd); | |
2057 | ||
aa3378df | 2058 | /* |
2059 | * Create another descriptor of the pty master side for use as the | |
2060 | * standard input. We could use the original descriptor, but this | |
2061 | * simplifies code in server_loop. The descriptor is bidirectional. | |
2062 | */ | |
5260325f | 2063 | fdout = dup(ptyfd); |
2064 | if (fdout < 0) | |
2065 | packet_disconnect("dup failed: %.100s", strerror(errno)); | |
2066 | ||
aa3378df | 2067 | /* |
2068 | * Add a cleanup function to clear the utmp entry and record logout | |
2069 | * time in case we call fatal() (e.g., the connection gets closed). | |
2070 | */ | |
5260325f | 2071 | cleanup_context.pid = pid; |
2072 | cleanup_context.ttyname = ttyname; | |
2073 | fatal_add_cleanup(pty_cleanup_proc, (void *) &cleanup_context); | |
2074 | ||
2075 | /* Enter interactive session. */ | |
2076 | server_loop(pid, ptyfd, fdout, -1); | |
2077 | /* server_loop has not closed ptyfd and fdout. */ | |
2078 | ||
2079 | /* Cancel the cleanup function. */ | |
2080 | fatal_remove_cleanup(pty_cleanup_proc, (void *) &cleanup_context); | |
2081 | ||
2082 | /* Record that the user has logged out. */ | |
2083 | record_logout(pid, ttyname); | |
2084 | ||
2085 | /* Release the pseudo-tty. */ | |
2086 | pty_release(ttyname); | |
2087 | ||
aa3378df | 2088 | /* |
2089 | * Close the server side of the socket pairs. We must do this after | |
2090 | * the pty cleanup, so that another process doesn't get this pty | |
2091 | * while we're still cleaning up. | |
2092 | */ | |
5260325f | 2093 | close(ptyfd); |
2094 | close(fdout); | |
8efc0c15 | 2095 | } |
2096 | ||
5260325f | 2097 | /* |
2098 | * Sets the value of the given variable in the environment. If the variable | |
2099 | * already exists, its value is overriden. | |
2100 | */ | |
2101 | void | |
2102 | child_set_env(char ***envp, unsigned int *envsizep, const char *name, | |
2103 | const char *value) | |
8efc0c15 | 2104 | { |
5260325f | 2105 | unsigned int i, namelen; |
2106 | char **env; | |
2107 | ||
aa3378df | 2108 | /* |
2109 | * Find the slot where the value should be stored. If the variable | |
2110 | * already exists, we reuse the slot; otherwise we append a new slot | |
2111 | * at the end of the array, expanding if necessary. | |
2112 | */ | |
5260325f | 2113 | env = *envp; |
2114 | namelen = strlen(name); | |
2115 | for (i = 0; env[i]; i++) | |
2116 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') | |
2117 | break; | |
2118 | if (env[i]) { | |
aa3378df | 2119 | /* Reuse the slot. */ |
5260325f | 2120 | xfree(env[i]); |
2121 | } else { | |
aa3378df | 2122 | /* New variable. Expand if necessary. */ |
5260325f | 2123 | if (i >= (*envsizep) - 1) { |
2124 | (*envsizep) += 50; | |
2125 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); | |
2126 | } | |
2127 | /* Need to set the NULL pointer at end of array beyond the new slot. */ | |
2128 | env[i + 1] = NULL; | |
8efc0c15 | 2129 | } |
2130 | ||
5260325f | 2131 | /* Allocate space and format the variable in the appropriate slot. */ |
2132 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); | |
2133 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); | |
8efc0c15 | 2134 | } |
2135 | ||
5260325f | 2136 | /* |
2137 | * Reads environment variables from the given file and adds/overrides them | |
2138 | * into the environment. If the file does not exist, this does nothing. | |
2139 | * Otherwise, it must consist of empty lines, comments (line starts with '#') | |
2140 | * and assignments of the form name=value. No other forms are allowed. | |
2141 | */ | |
2142 | void | |
2143 | read_environment_file(char ***env, unsigned int *envsize, | |
2144 | const char *filename) | |
8efc0c15 | 2145 | { |
5260325f | 2146 | FILE *f; |
2147 | char buf[4096]; | |
2148 | char *cp, *value; | |
2149 | ||
5260325f | 2150 | f = fopen(filename, "r"); |
2151 | if (!f) | |
2152 | return; | |
2153 | ||
5260325f | 2154 | while (fgets(buf, sizeof(buf), f)) { |
aa3378df | 2155 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
2156 | ; | |
5260325f | 2157 | if (!*cp || *cp == '#' || *cp == '\n') |
2158 | continue; | |
5260325f | 2159 | if (strchr(cp, '\n')) |
2160 | *strchr(cp, '\n') = '\0'; | |
5260325f | 2161 | value = strchr(cp, '='); |
2162 | if (value == NULL) { | |
2163 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); | |
2164 | continue; | |
2165 | } | |
aa3378df | 2166 | /* Replace the equals sign by nul, and advance value to the value string. */ |
5260325f | 2167 | *value = '\0'; |
2168 | value++; | |
5260325f | 2169 | child_set_env(env, envsize, cp, value); |
2170 | } | |
5260325f | 2171 | fclose(f); |
8efc0c15 | 2172 | } |
2173 | ||
a5c9cd31 | 2174 | #ifdef USE_PAM |
2175 | /* | |
2176 | * Sets any environment variables which have been specified by PAM | |
2177 | */ | |
2178 | void do_pam_environment(char ***env, int *envsize) | |
2179 | { | |
2180 | char *equals, var_name[512], var_val[512]; | |
2181 | char **pam_env; | |
2182 | int i; | |
2183 | ||
2184 | if ((pam_env = fetch_pam_environment()) == NULL) | |
2185 | return; | |
2186 | ||
2187 | for(i = 0; pam_env[i] != NULL; i++) { | |
2188 | if ((equals = strstr(pam_env[i], "=")) == NULL) | |
2189 | continue; | |
2190 | ||
2191 | if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) | |
2192 | { | |
2193 | memset(var_name, '\0', sizeof(var_name)); | |
2194 | memset(var_val, '\0', sizeof(var_val)); | |
2195 | ||
2196 | strncpy(var_name, pam_env[i], equals - pam_env[i]); | |
2197 | strcpy(var_val, equals + 1); | |
2198 | ||
2199 | debug("PAM environment: %s=%s", var_name, var_val); | |
2200 | ||
2201 | child_set_env(env, envsize, var_name, var_val); | |
2202 | } | |
2203 | } | |
2204 | } | |
2205 | #endif /* USE_PAM */ | |
2206 | ||
5260325f | 2207 | /* |
2208 | * Performs common processing for the child, such as setting up the | |
2209 | * environment, closing extra file descriptors, setting the user and group | |
2210 | * ids, and executing the command or shell. | |
2211 | */ | |
2212 | void | |
2213 | do_child(const char *command, struct passwd * pw, const char *term, | |
2214 | const char *display, const char *auth_proto, | |
2215 | const char *auth_data, const char *ttyname) | |
8efc0c15 | 2216 | { |
5260325f | 2217 | const char *shell, *cp = NULL; |
2218 | char buf[256]; | |
2219 | FILE *f; | |
2220 | unsigned int envsize, i; | |
2221 | char **env; | |
2222 | extern char **environ; | |
2223 | struct stat st; | |
2224 | char *argv[10]; | |
8efc0c15 | 2225 | |
d94aa2ae | 2226 | #ifndef USE_PAM /* pam_nologin handles this */ |
5260325f | 2227 | /* Check /etc/nologin. */ |
2228 | f = fopen("/etc/nologin", "r"); | |
2229 | if (f) { | |
2230 | /* /etc/nologin exists. Print its contents and exit. */ | |
2231 | while (fgets(buf, sizeof(buf), f)) | |
2232 | fputs(buf, stderr); | |
2233 | fclose(f); | |
2234 | if (pw->pw_uid != 0) | |
2235 | exit(254); | |
2236 | } | |
d94aa2ae | 2237 | #endif /* USE_PAM */ |
e35c1dc2 | 2238 | |
5260325f | 2239 | /* Set login name in the kernel. */ |
2240 | if (setlogin(pw->pw_name) < 0) | |
2241 | error("setlogin failed: %s", strerror(errno)); | |
8efc0c15 | 2242 | |
5260325f | 2243 | /* Set uid, gid, and groups. */ |
2244 | /* Login(1) does this as well, and it needs uid 0 for the "-h" | |
2245 | switch, so we let login(1) to this for us. */ | |
2246 | if (!options.use_login) { | |
2247 | if (getuid() == 0 || geteuid() == 0) { | |
2248 | if (setgid(pw->pw_gid) < 0) { | |
2249 | perror("setgid"); | |
2250 | exit(1); | |
2251 | } | |
2252 | /* Initialize the group list. */ | |
2253 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { | |
2254 | perror("initgroups"); | |
2255 | exit(1); | |
2256 | } | |
2257 | endgrent(); | |
2258 | ||
2259 | /* Permanently switch to the desired uid. */ | |
2260 | permanently_set_uid(pw->pw_uid); | |
2261 | } | |
2262 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) | |
2263 | fatal("Failed to set uids to %d.", (int) pw->pw_uid); | |
2264 | } | |
aa3378df | 2265 | /* |
2266 | * Get the shell from the password data. An empty shell field is | |
2267 | * legal, and means /bin/sh. | |
2268 | */ | |
5260325f | 2269 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
8efc0c15 | 2270 | |
2271 | #ifdef AFS | |
5260325f | 2272 | /* Try to get AFS tokens for the local cell. */ |
2273 | if (k_hasafs()) { | |
2274 | char cell[64]; | |
2275 | ||
2276 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) | |
2277 | krb_afslog(cell, 0); | |
2278 | ||
2279 | krb_afslog(0, 0); | |
2280 | } | |
8efc0c15 | 2281 | #endif /* AFS */ |
5260325f | 2282 | |
aa3378df | 2283 | /* Initialize the environment. */ |
5260325f | 2284 | envsize = 100; |
2285 | env = xmalloc(envsize * sizeof(char *)); | |
2286 | env[0] = NULL; | |
2287 | ||
2288 | if (!options.use_login) { | |
2289 | /* Set basic environment. */ | |
2290 | child_set_env(&env, &envsize, "USER", pw->pw_name); | |
2291 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); | |
2292 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); | |
2293 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); | |
2294 | ||
2295 | snprintf(buf, sizeof buf, "%.200s/%.50s", | |
2296 | _PATH_MAILDIR, pw->pw_name); | |
2297 | child_set_env(&env, &envsize, "MAIL", buf); | |
2298 | ||
2299 | /* Normal systems set SHELL by default. */ | |
2300 | child_set_env(&env, &envsize, "SHELL", shell); | |
2301 | } | |
5260325f | 2302 | if (getenv("TZ")) |
2303 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); | |
2304 | ||
2305 | /* Set custom environment options from RSA authentication. */ | |
2306 | while (custom_environment) { | |
2307 | struct envstring *ce = custom_environment; | |
2308 | char *s = ce->s; | |
2309 | int i; | |
2310 | for (i = 0; s[i] != '=' && s[i]; i++); | |
2311 | if (s[i] == '=') { | |
2312 | s[i] = 0; | |
2313 | child_set_env(&env, &envsize, s, s + i + 1); | |
2314 | } | |
2315 | custom_environment = ce->next; | |
2316 | xfree(ce->s); | |
2317 | xfree(ce); | |
8efc0c15 | 2318 | } |
8efc0c15 | 2319 | |
5260325f | 2320 | snprintf(buf, sizeof buf, "%.50s %d %d", |
2321 | get_remote_ipaddr(), get_remote_port(), options.port); | |
2322 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); | |
8efc0c15 | 2323 | |
5260325f | 2324 | if (ttyname) |
2325 | child_set_env(&env, &envsize, "SSH_TTY", ttyname); | |
5260325f | 2326 | if (term) |
2327 | child_set_env(&env, &envsize, "TERM", term); | |
5260325f | 2328 | if (display) |
2329 | child_set_env(&env, &envsize, "DISPLAY", display); | |
8efc0c15 | 2330 | |
2331 | #ifdef KRB4 | |
5260325f | 2332 | { |
2333 | extern char *ticket; | |
2334 | ||
2335 | if (ticket) | |
2336 | child_set_env(&env, &envsize, "KRBTKFILE", ticket); | |
2337 | } | |
8efc0c15 | 2338 | #endif /* KRB4 */ |
2339 | ||
d94aa2ae | 2340 | #ifdef USE_PAM |
5260325f | 2341 | /* Pull in any environment variables that may have been set by PAM. */ |
a5c9cd31 | 2342 | do_pam_environment(&env, &envsize); |
d94aa2ae | 2343 | #endif /* USE_PAM */ |
bb0dd7f6 | 2344 | |
5260325f | 2345 | if (xauthfile) |
2346 | child_set_env(&env, &envsize, "XAUTHORITY", xauthfile); | |
2347 | ||
5260325f | 2348 | if (auth_get_socket_name() != NULL) |
2349 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, | |
2350 | auth_get_socket_name()); | |
2351 | ||
aa3378df | 2352 | /* read $HOME/.ssh/environment. */ |
5260325f | 2353 | if (!options.use_login) { |
2354 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", pw->pw_dir); | |
2355 | read_environment_file(&env, &envsize, buf); | |
2356 | } | |
5260325f | 2357 | if (debug_flag) { |
aa3378df | 2358 | /* dump the environment */ |
5260325f | 2359 | fprintf(stderr, "Environment:\n"); |
2360 | for (i = 0; env[i]; i++) | |
2361 | fprintf(stderr, " %.200s\n", env[i]); | |
2362 | } | |
aa3378df | 2363 | /* |
2364 | * Close the connection descriptors; note that this is the child, and | |
2365 | * the server will still have the socket open, and it is important | |
2366 | * that we do not shutdown it. Note that the descriptors cannot be | |
2367 | * closed before building the environment, as we call | |
2368 | * get_remote_ipaddr there. | |
2369 | */ | |
5260325f | 2370 | if (packet_get_connection_in() == packet_get_connection_out()) |
2371 | close(packet_get_connection_in()); | |
2372 | else { | |
2373 | close(packet_get_connection_in()); | |
2374 | close(packet_get_connection_out()); | |
2375 | } | |
aa3378df | 2376 | /* |
2377 | * Close all descriptors related to channels. They will still remain | |
2378 | * open in the parent. | |
2379 | */ | |
2380 | /* XXX better use close-on-exec? -markus */ | |
5260325f | 2381 | channel_close_all(); |
2382 | ||
aa3378df | 2383 | /* |
2384 | * Close any extra file descriptors. Note that there may still be | |
2385 | * descriptors left by system functions. They will be closed later. | |
2386 | */ | |
5260325f | 2387 | endpwent(); |
2388 | endhostent(); | |
2389 | ||
aa3378df | 2390 | /* |
2391 | * Close any extra open file descriptors so that we don\'t have them | |
2392 | * hanging around in clients. Note that we want to do this after | |
2393 | * initgroups, because at least on Solaris 2.3 it leaves file | |
2394 | * descriptors open. | |
2395 | */ | |
5260325f | 2396 | for (i = 3; i < 64; i++) |
2397 | close(i); | |
2398 | ||
2399 | /* Change current directory to the user\'s home directory. */ | |
2400 | if (chdir(pw->pw_dir) < 0) | |
2401 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", | |
2402 | pw->pw_dir, strerror(errno)); | |
2403 | ||
aa3378df | 2404 | /* |
2405 | * Must take new environment into use so that .ssh/rc, /etc/sshrc and | |
2406 | * xauth are run in the proper environment. | |
2407 | */ | |
5260325f | 2408 | environ = env; |
2409 | ||
aa3378df | 2410 | /* |
2411 | * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first | |
2412 | * in this order). | |
2413 | */ | |
5260325f | 2414 | if (!options.use_login) { |
2415 | if (stat(SSH_USER_RC, &st) >= 0) { | |
2416 | if (debug_flag) | |
2417 | fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC); | |
2418 | ||
2419 | f = popen("/bin/sh " SSH_USER_RC, "w"); | |
2420 | if (f) { | |
2421 | if (auth_proto != NULL && auth_data != NULL) | |
2422 | fprintf(f, "%s %s\n", auth_proto, auth_data); | |
2423 | pclose(f); | |
2424 | } else | |
2425 | fprintf(stderr, "Could not run %s\n", SSH_USER_RC); | |
2426 | } else if (stat(SSH_SYSTEM_RC, &st) >= 0) { | |
2427 | if (debug_flag) | |
2428 | fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC); | |
2429 | ||
2430 | f = popen("/bin/sh " SSH_SYSTEM_RC, "w"); | |
2431 | if (f) { | |
2432 | if (auth_proto != NULL && auth_data != NULL) | |
2433 | fprintf(f, "%s %s\n", auth_proto, auth_data); | |
2434 | pclose(f); | |
2435 | } else | |
2436 | fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC); | |
2437 | } | |
8efc0c15 | 2438 | #ifdef XAUTH_PATH |
5260325f | 2439 | else { |
aa3378df | 2440 | /* Add authority data to .Xauthority if appropriate. */ |
5260325f | 2441 | if (auth_proto != NULL && auth_data != NULL) { |
2442 | if (debug_flag) | |
2443 | fprintf(stderr, "Running %.100s add %.100s %.100s %.100s\n", | |
2444 | XAUTH_PATH, display, auth_proto, auth_data); | |
2445 | ||
2446 | f = popen(XAUTH_PATH " -q -", "w"); | |
2447 | if (f) { | |
2448 | fprintf(f, "add %s %s %s\n", display, auth_proto, auth_data); | |
2449 | fclose(f); | |
2450 | } else | |
2451 | fprintf(stderr, "Could not run %s -q -\n", XAUTH_PATH); | |
2452 | } | |
2453 | } | |
8efc0c15 | 2454 | #endif /* XAUTH_PATH */ |
2455 | ||
5260325f | 2456 | /* Get the last component of the shell name. */ |
2457 | cp = strrchr(shell, '/'); | |
2458 | if (cp) | |
2459 | cp++; | |
2460 | else | |
2461 | cp = shell; | |
2462 | } | |
aa3378df | 2463 | /* |
2464 | * If we have no command, execute the shell. In this case, the shell | |
2465 | * name to be passed in argv[0] is preceded by '-' to indicate that | |
2466 | * this is a login shell. | |
2467 | */ | |
5260325f | 2468 | if (!command) { |
2469 | if (!options.use_login) { | |
2470 | char buf[256]; | |
2471 | ||
aa3378df | 2472 | /* |
2473 | * Check for mail if we have a tty and it was enabled | |
2474 | * in server options. | |
2475 | */ | |
5260325f | 2476 | if (ttyname && options.check_mail) { |
2477 | char *mailbox; | |
2478 | struct stat mailstat; | |
2479 | mailbox = getenv("MAIL"); | |
2480 | if (mailbox != NULL) { | |
2481 | if (stat(mailbox, &mailstat) != 0 || mailstat.st_size == 0) | |
2482 | printf("No mail.\n"); | |
2483 | else if (mailstat.st_mtime < mailstat.st_atime) | |
2484 | printf("You have mail.\n"); | |
2485 | else | |
2486 | printf("You have new mail.\n"); | |
2487 | } | |
2488 | } | |
2489 | /* Start the shell. Set initial character to '-'. */ | |
2490 | buf[0] = '-'; | |
2491 | strncpy(buf + 1, cp, sizeof(buf) - 1); | |
2492 | buf[sizeof(buf) - 1] = 0; | |
2493 | ||
2494 | /* Execute the shell. */ | |
2495 | argv[0] = buf; | |
2496 | argv[1] = NULL; | |
2497 | execve(shell, argv, env); | |
2498 | ||
2499 | /* Executing the shell failed. */ | |
2500 | perror(shell); | |
2501 | exit(1); | |
2502 | ||
2503 | } else { | |
2504 | /* Launch login(1). */ | |
2505 | ||
2506 | execl(LOGIN_PROGRAM, "login", "-h", get_remote_ipaddr(), | |
2507 | "-p", "-f", "--", pw->pw_name, NULL); | |
2508 | ||
2509 | /* Login couldn't be executed, die. */ | |
2510 | ||
2511 | perror("login"); | |
2512 | exit(1); | |
2513 | } | |
2514 | } | |
aa3378df | 2515 | /* |
2516 | * Execute the command using the user's shell. This uses the -c | |
2517 | * option to execute the command. | |
2518 | */ | |
5260325f | 2519 | argv[0] = (char *) cp; |
2520 | argv[1] = "-c"; | |
2521 | argv[2] = (char *) command; | |
2522 | argv[3] = NULL; | |
2523 | execve(shell, argv, env); | |
2524 | perror(shell); | |
2525 | exit(1); | |
8efc0c15 | 2526 | } |