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