Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 3 | * All rights reserved |
| 4 | */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 5 | /* |
| 6 | * SSH2 support by Markus Friedl. |
| 7 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 8 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 9 | |
| 10 | #include "includes.h" |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | RCSID("$OpenBSD: session.c,v 1.4 2000/04/14 10:30:33 markus Exp $"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 12 | |
| 13 | #include "xmalloc.h" |
| 14 | #include "ssh.h" |
| 15 | #include "pty.h" |
| 16 | #include "packet.h" |
| 17 | #include "buffer.h" |
| 18 | #include "cipher.h" |
| 19 | #include "mpaux.h" |
| 20 | #include "servconf.h" |
| 21 | #include "uidswap.h" |
| 22 | #include "compat.h" |
| 23 | #include "channels.h" |
| 24 | #include "nchan.h" |
| 25 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 26 | #include "bufaux.h" |
| 27 | #include "ssh2.h" |
| 28 | #include "auth.h" |
| 29 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 30 | /* types */ |
| 31 | |
| 32 | #define TTYSZ 64 |
| 33 | typedef struct Session Session; |
| 34 | struct Session { |
| 35 | int used; |
| 36 | int self; |
| 37 | struct passwd *pw; |
| 38 | pid_t pid; |
| 39 | /* tty */ |
| 40 | char *term; |
| 41 | int ptyfd, ttyfd, ptymaster; |
| 42 | int row, col, xpixel, ypixel; |
| 43 | char tty[TTYSZ]; |
| 44 | /* X11 */ |
| 45 | char *display; |
| 46 | int screen; |
| 47 | char *auth_proto; |
| 48 | char *auth_data; |
| 49 | /* proto 2 */ |
| 50 | int chanid; |
| 51 | }; |
| 52 | |
| 53 | /* func */ |
| 54 | |
| 55 | Session *session_new(void); |
| 56 | void session_set_fds(Session *s, int fdin, int fdout, int fderr); |
| 57 | void session_pty_cleanup(Session *s); |
| 58 | void do_exec_pty(Session *s, const char *command, struct passwd * pw); |
| 59 | void do_exec_no_pty(Session *s, const char *command, struct passwd * pw); |
| 60 | |
| 61 | void |
| 62 | do_child(const char *command, struct passwd * pw, const char *term, |
| 63 | const char *display, const char *auth_proto, |
| 64 | const char *auth_data, const char *ttyname); |
| 65 | |
| 66 | /* import */ |
| 67 | extern ServerOptions options; |
| 68 | extern char *__progname; |
| 69 | extern int log_stderr; |
| 70 | extern int debug_flag; |
| 71 | |
| 72 | /* Local Xauthority file. */ |
| 73 | static char *xauthfile; |
| 74 | |
| 75 | /* data */ |
| 76 | #define MAX_SESSIONS 10 |
| 77 | Session sessions[MAX_SESSIONS]; |
| 78 | |
| 79 | /* Flags set in auth-rsa from authorized_keys flags. These are set in auth-rsa.c. */ |
| 80 | int no_port_forwarding_flag = 0; |
| 81 | int no_agent_forwarding_flag = 0; |
| 82 | int no_x11_forwarding_flag = 0; |
| 83 | int no_pty_flag = 0; |
| 84 | |
| 85 | /* RSA authentication "command=" option. */ |
| 86 | char *forced_command = NULL; |
| 87 | |
| 88 | /* RSA authentication "environment=" options. */ |
| 89 | struct envstring *custom_environment = NULL; |
| 90 | |
| 91 | /* |
| 92 | * Remove local Xauthority file. |
| 93 | */ |
| 94 | void |
| 95 | xauthfile_cleanup_proc(void *ignore) |
| 96 | { |
| 97 | debug("xauthfile_cleanup_proc called"); |
| 98 | |
| 99 | if (xauthfile != NULL) { |
| 100 | char *p; |
| 101 | unlink(xauthfile); |
| 102 | p = strrchr(xauthfile, '/'); |
| 103 | if (p != NULL) { |
| 104 | *p = '\0'; |
| 105 | rmdir(xauthfile); |
| 106 | } |
| 107 | xfree(xauthfile); |
| 108 | xauthfile = NULL; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Function to perform cleanup if we get aborted abnormally (e.g., due to a |
| 114 | * dropped connection). |
| 115 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 116 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 117 | pty_cleanup_proc(void *session) |
| 118 | { |
| 119 | Session *s=session; |
| 120 | if (s == NULL) |
| 121 | fatal("pty_cleanup_proc: no session"); |
| 122 | debug("pty_cleanup_proc: %s", s->tty); |
| 123 | |
| 124 | if (s->pid != 0) { |
| 125 | /* Record that the user has logged out. */ |
| 126 | record_logout(s->pid, s->tty); |
| 127 | } |
| 128 | |
| 129 | /* Release the pseudo-tty. */ |
| 130 | pty_release(s->tty); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Prepares for an interactive session. This is called after the user has |
| 135 | * been successfully authenticated. During this message exchange, pseudo |
| 136 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings |
| 137 | * are requested, etc. |
| 138 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 139 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 140 | do_authenticated(struct passwd * pw) |
| 141 | { |
| 142 | Session *s; |
| 143 | int type; |
| 144 | int compression_level = 0, enable_compression_after_reply = 0; |
| 145 | int have_pty = 0; |
| 146 | char *command; |
| 147 | int n_bytes; |
| 148 | int plen; |
| 149 | unsigned int proto_len, data_len, dlen; |
| 150 | |
| 151 | /* |
| 152 | * Cancel the alarm we set to limit the time taken for |
| 153 | * authentication. |
| 154 | */ |
| 155 | alarm(0); |
| 156 | |
| 157 | /* |
| 158 | * Inform the channel mechanism that we are the server side and that |
| 159 | * the client may request to connect to any port at all. (The user |
| 160 | * could do it anyway, and we wouldn\'t know what is permitted except |
| 161 | * by the client telling us, so we can equally well trust the client |
| 162 | * not to request anything bogus.) |
| 163 | */ |
| 164 | if (!no_port_forwarding_flag) |
| 165 | channel_permit_all_opens(); |
| 166 | |
| 167 | s = session_new(); |
| 168 | |
| 169 | /* |
| 170 | * We stay in this loop until the client requests to execute a shell |
| 171 | * or a command. |
| 172 | */ |
| 173 | for (;;) { |
| 174 | int success = 0; |
| 175 | |
| 176 | /* Get a packet from the client. */ |
| 177 | type = packet_read(&plen); |
| 178 | |
| 179 | /* Process the packet. */ |
| 180 | switch (type) { |
| 181 | case SSH_CMSG_REQUEST_COMPRESSION: |
| 182 | packet_integrity_check(plen, 4, type); |
| 183 | compression_level = packet_get_int(); |
| 184 | if (compression_level < 1 || compression_level > 9) { |
| 185 | packet_send_debug("Received illegal compression level %d.", |
| 186 | compression_level); |
| 187 | break; |
| 188 | } |
| 189 | /* Enable compression after we have responded with SUCCESS. */ |
| 190 | enable_compression_after_reply = 1; |
| 191 | success = 1; |
| 192 | break; |
| 193 | |
| 194 | case SSH_CMSG_REQUEST_PTY: |
| 195 | if (no_pty_flag) { |
| 196 | debug("Allocating a pty not permitted for this authentication."); |
| 197 | break; |
| 198 | } |
| 199 | if (have_pty) |
| 200 | packet_disconnect("Protocol error: you already have a pty."); |
| 201 | |
| 202 | debug("Allocating pty."); |
| 203 | |
| 204 | /* Allocate a pty and open it. */ |
| 205 | if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, |
| 206 | sizeof(s->tty))) { |
| 207 | error("Failed to allocate pty."); |
| 208 | break; |
| 209 | } |
| 210 | fatal_add_cleanup(pty_cleanup_proc, (void *)s); |
| 211 | pty_setowner(pw, s->tty); |
| 212 | |
| 213 | /* Get TERM from the packet. Note that the value may be of arbitrary length. */ |
| 214 | s->term = packet_get_string(&dlen); |
| 215 | packet_integrity_check(dlen, strlen(s->term), type); |
| 216 | /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */ |
| 217 | /* Remaining bytes */ |
| 218 | n_bytes = plen - (4 + dlen + 4 * 4); |
| 219 | |
| 220 | if (strcmp(s->term, "") == 0) { |
| 221 | xfree(s->term); |
| 222 | s->term = NULL; |
| 223 | } |
| 224 | /* Get window size from the packet. */ |
| 225 | s->row = packet_get_int(); |
| 226 | s->col = packet_get_int(); |
| 227 | s->xpixel = packet_get_int(); |
| 228 | s->ypixel = packet_get_int(); |
| 229 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 230 | |
| 231 | /* Get tty modes from the packet. */ |
| 232 | tty_parse_modes(s->ttyfd, &n_bytes); |
| 233 | packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type); |
| 234 | |
| 235 | /* Indicate that we now have a pty. */ |
| 236 | success = 1; |
| 237 | have_pty = 1; |
| 238 | break; |
| 239 | |
| 240 | case SSH_CMSG_X11_REQUEST_FORWARDING: |
| 241 | if (!options.x11_forwarding) { |
| 242 | packet_send_debug("X11 forwarding disabled in server configuration file."); |
| 243 | break; |
| 244 | } |
| 245 | #ifdef XAUTH_PATH |
| 246 | if (no_x11_forwarding_flag) { |
| 247 | packet_send_debug("X11 forwarding not permitted for this authentication."); |
| 248 | break; |
| 249 | } |
| 250 | debug("Received request for X11 forwarding with auth spoofing."); |
| 251 | if (s->display != NULL) |
| 252 | packet_disconnect("Protocol error: X11 display already set."); |
| 253 | |
| 254 | s->auth_proto = packet_get_string(&proto_len); |
| 255 | s->auth_data = packet_get_string(&data_len); |
| 256 | packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type); |
| 257 | |
| 258 | if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER) |
| 259 | s->screen = packet_get_int(); |
| 260 | else |
| 261 | s->screen = 0; |
| 262 | s->display = x11_create_display_inet(s->screen, options.x11_display_offset); |
| 263 | |
| 264 | if (s->display == NULL) |
| 265 | break; |
| 266 | |
| 267 | /* Setup to always have a local .Xauthority. */ |
| 268 | xauthfile = xmalloc(MAXPATHLEN); |
| 269 | strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN); |
| 270 | temporarily_use_uid(pw->pw_uid); |
| 271 | if (mkdtemp(xauthfile) == NULL) { |
| 272 | restore_uid(); |
| 273 | error("private X11 dir: mkdtemp %s failed: %s", |
| 274 | xauthfile, strerror(errno)); |
| 275 | xfree(xauthfile); |
| 276 | xauthfile = NULL; |
| 277 | break; |
| 278 | } |
| 279 | strlcat(xauthfile, "/cookies", MAXPATHLEN); |
| 280 | open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 281 | restore_uid(); |
| 282 | fatal_add_cleanup(xauthfile_cleanup_proc, NULL); |
| 283 | success = 1; |
| 284 | break; |
| 285 | #else /* XAUTH_PATH */ |
| 286 | packet_send_debug("No xauth program; cannot forward with spoofing."); |
| 287 | break; |
| 288 | #endif /* XAUTH_PATH */ |
| 289 | |
| 290 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: |
| 291 | if (no_agent_forwarding_flag || compat13) { |
| 292 | debug("Authentication agent forwarding not permitted for this authentication."); |
| 293 | break; |
| 294 | } |
| 295 | debug("Received authentication agent forwarding request."); |
| 296 | auth_input_request_forwarding(pw); |
| 297 | success = 1; |
| 298 | break; |
| 299 | |
| 300 | case SSH_CMSG_PORT_FORWARD_REQUEST: |
| 301 | if (no_port_forwarding_flag) { |
| 302 | debug("Port forwarding not permitted for this authentication."); |
| 303 | break; |
| 304 | } |
| 305 | debug("Received TCP/IP port forwarding request."); |
| 306 | channel_input_port_forward_request(pw->pw_uid == 0); |
| 307 | success = 1; |
| 308 | break; |
| 309 | |
| 310 | case SSH_CMSG_MAX_PACKET_SIZE: |
| 311 | if (packet_set_maxsize(packet_get_int()) > 0) |
| 312 | success = 1; |
| 313 | break; |
| 314 | |
| 315 | case SSH_CMSG_EXEC_SHELL: |
| 316 | case SSH_CMSG_EXEC_CMD: |
| 317 | /* Set interactive/non-interactive mode. */ |
| 318 | packet_set_interactive(have_pty || s->display != NULL, |
| 319 | options.keepalives); |
| 320 | |
| 321 | if (type == SSH_CMSG_EXEC_CMD) { |
| 322 | command = packet_get_string(&dlen); |
| 323 | debug("Exec command '%.500s'", command); |
| 324 | packet_integrity_check(plen, 4 + dlen, type); |
| 325 | } else { |
| 326 | command = NULL; |
| 327 | packet_integrity_check(plen, 0, type); |
| 328 | } |
| 329 | if (forced_command != NULL) { |
| 330 | command = forced_command; |
| 331 | debug("Forced command '%.500s'", forced_command); |
| 332 | } |
| 333 | if (have_pty) |
| 334 | do_exec_pty(s, command, pw); |
| 335 | else |
| 336 | do_exec_no_pty(s, command, pw); |
| 337 | |
| 338 | if (command != NULL) |
| 339 | xfree(command); |
| 340 | /* Cleanup user's local Xauthority file. */ |
| 341 | if (xauthfile) |
| 342 | xauthfile_cleanup_proc(NULL); |
| 343 | return; |
| 344 | |
| 345 | default: |
| 346 | /* |
| 347 | * Any unknown messages in this phase are ignored, |
| 348 | * and a failure message is returned. |
| 349 | */ |
| 350 | log("Unknown packet type received after authentication: %d", type); |
| 351 | } |
| 352 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); |
| 353 | packet_send(); |
| 354 | packet_write_wait(); |
| 355 | |
| 356 | /* Enable compression now that we have replied if appropriate. */ |
| 357 | if (enable_compression_after_reply) { |
| 358 | enable_compression_after_reply = 0; |
| 359 | packet_start_compression(compression_level); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * This is called to fork and execute a command when we have no tty. This |
| 366 | * will call do_child from the child, and server_loop from the parent after |
| 367 | * setting up file descriptors and such. |
| 368 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 369 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 370 | do_exec_no_pty(Session *s, const char *command, struct passwd * pw) |
| 371 | { |
| 372 | int pid; |
| 373 | |
| 374 | #ifdef USE_PIPES |
| 375 | int pin[2], pout[2], perr[2]; |
| 376 | /* Allocate pipes for communicating with the program. */ |
| 377 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) |
| 378 | packet_disconnect("Could not create pipes: %.100s", |
| 379 | strerror(errno)); |
| 380 | #else /* USE_PIPES */ |
| 381 | int inout[2], err[2]; |
| 382 | /* Uses socket pairs to communicate with the program. */ |
| 383 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || |
| 384 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) |
| 385 | packet_disconnect("Could not create socket pairs: %.100s", |
| 386 | strerror(errno)); |
| 387 | #endif /* USE_PIPES */ |
| 388 | if (s == NULL) |
| 389 | fatal("do_exec_no_pty: no session"); |
| 390 | |
| 391 | setproctitle("%s@notty", pw->pw_name); |
| 392 | |
| 393 | #ifdef USE_PAM |
| 394 | do_pam_setcred(); |
| 395 | #endif /* USE_PAM */ |
| 396 | |
| 397 | /* Fork the child. */ |
| 398 | if ((pid = fork()) == 0) { |
| 399 | /* Child. Reinitialize the log since the pid has changed. */ |
| 400 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
| 401 | |
| 402 | /* |
| 403 | * Create a new session and process group since the 4.4BSD |
| 404 | * setlogin() affects the entire process group. |
| 405 | */ |
| 406 | if (setsid() < 0) |
| 407 | error("setsid failed: %.100s", strerror(errno)); |
| 408 | |
| 409 | #ifdef USE_PIPES |
| 410 | /* |
| 411 | * Redirect stdin. We close the parent side of the socket |
| 412 | * pair, and make the child side the standard input. |
| 413 | */ |
| 414 | close(pin[1]); |
| 415 | if (dup2(pin[0], 0) < 0) |
| 416 | perror("dup2 stdin"); |
| 417 | close(pin[0]); |
| 418 | |
| 419 | /* Redirect stdout. */ |
| 420 | close(pout[0]); |
| 421 | if (dup2(pout[1], 1) < 0) |
| 422 | perror("dup2 stdout"); |
| 423 | close(pout[1]); |
| 424 | |
| 425 | /* Redirect stderr. */ |
| 426 | close(perr[0]); |
| 427 | if (dup2(perr[1], 2) < 0) |
| 428 | perror("dup2 stderr"); |
| 429 | close(perr[1]); |
| 430 | #else /* USE_PIPES */ |
| 431 | /* |
| 432 | * Redirect stdin, stdout, and stderr. Stdin and stdout will |
| 433 | * use the same socket, as some programs (particularly rdist) |
| 434 | * seem to depend on it. |
| 435 | */ |
| 436 | close(inout[1]); |
| 437 | close(err[1]); |
| 438 | if (dup2(inout[0], 0) < 0) /* stdin */ |
| 439 | perror("dup2 stdin"); |
| 440 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ |
| 441 | perror("dup2 stdout"); |
| 442 | if (dup2(err[0], 2) < 0) /* stderr */ |
| 443 | perror("dup2 stderr"); |
| 444 | #endif /* USE_PIPES */ |
| 445 | |
| 446 | /* Do processing for the child (exec command etc). */ |
| 447 | do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL); |
| 448 | /* NOTREACHED */ |
| 449 | } |
| 450 | if (pid < 0) |
| 451 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 452 | s->pid = pid; |
| 453 | #ifdef USE_PIPES |
| 454 | /* We are the parent. Close the child sides of the pipes. */ |
| 455 | close(pin[0]); |
| 456 | close(pout[1]); |
| 457 | close(perr[1]); |
| 458 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 459 | if (compat20) { |
| 460 | session_set_fds(s, pin[1], pout[0], perr[0]); |
| 461 | } else { |
| 462 | /* Enter the interactive session. */ |
| 463 | server_loop(pid, pin[1], pout[0], perr[0]); |
| 464 | /* server_loop has closed pin[1], pout[1], and perr[1]. */ |
| 465 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 466 | #else /* USE_PIPES */ |
| 467 | /* We are the parent. Close the child sides of the socket pairs. */ |
| 468 | close(inout[0]); |
| 469 | close(err[0]); |
| 470 | |
| 471 | /* |
| 472 | * Enter the interactive session. Note: server_loop must be able to |
| 473 | * handle the case that fdin and fdout are the same. |
| 474 | */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 475 | if (compat20) { |
| 476 | session_set_fds(s, inout[1], inout[1], err[1]); |
| 477 | } else { |
| 478 | server_loop(pid, inout[1], inout[1], err[1]); |
| 479 | /* server_loop has closed inout[1] and err[1]. */ |
| 480 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 481 | #endif /* USE_PIPES */ |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * This is called to fork and execute a command when we have a tty. This |
| 486 | * will call do_child from the child, and server_loop from the parent after |
| 487 | * setting up file descriptors, controlling tty, updating wtmp, utmp, |
| 488 | * lastlog, and other such operations. |
| 489 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 490 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 491 | do_exec_pty(Session *s, const char *command, struct passwd * pw) |
| 492 | { |
| 493 | FILE *f; |
| 494 | char buf[100], *time_string; |
| 495 | char line[256]; |
| 496 | const char *hostname; |
| 497 | int fdout, ptyfd, ttyfd, ptymaster; |
| 498 | int quiet_login; |
| 499 | pid_t pid; |
| 500 | socklen_t fromlen; |
| 501 | struct sockaddr_storage from; |
| 502 | struct stat st; |
| 503 | time_t last_login_time; |
| 504 | |
| 505 | if (s == NULL) |
| 506 | fatal("do_exec_pty: no session"); |
| 507 | ptyfd = s->ptyfd; |
| 508 | ttyfd = s->ttyfd; |
| 509 | |
| 510 | /* Get remote host name. */ |
| 511 | hostname = get_canonical_hostname(); |
| 512 | |
| 513 | /* |
| 514 | * Get the time when the user last logged in. Buf will be set to |
| 515 | * contain the hostname the last login was from. |
| 516 | */ |
| 517 | if (!options.use_login) { |
| 518 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, |
| 519 | buf, sizeof(buf)); |
| 520 | } |
| 521 | setproctitle("%s@%s", pw->pw_name, strrchr(s->tty, '/') + 1); |
| 522 | |
| 523 | #ifdef USE_PAM |
| 524 | do_pam_session(pw->pw_name, s->tty); |
| 525 | do_pam_setcred(); |
| 526 | #endif /* USE_PAM */ |
| 527 | |
| 528 | /* Fork the child. */ |
| 529 | if ((pid = fork()) == 0) { |
| 530 | pid = getpid(); |
| 531 | |
| 532 | /* Child. Reinitialize the log because the pid has |
| 533 | changed. */ |
| 534 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
| 535 | |
| 536 | /* Close the master side of the pseudo tty. */ |
| 537 | close(ptyfd); |
| 538 | |
| 539 | /* Make the pseudo tty our controlling tty. */ |
| 540 | pty_make_controlling_tty(&ttyfd, s->tty); |
| 541 | |
| 542 | /* Redirect stdin from the pseudo tty. */ |
| 543 | if (dup2(ttyfd, fileno(stdin)) < 0) |
| 544 | error("dup2 stdin failed: %.100s", strerror(errno)); |
| 545 | |
| 546 | /* Redirect stdout to the pseudo tty. */ |
| 547 | if (dup2(ttyfd, fileno(stdout)) < 0) |
| 548 | error("dup2 stdin failed: %.100s", strerror(errno)); |
| 549 | |
| 550 | /* Redirect stderr to the pseudo tty. */ |
| 551 | if (dup2(ttyfd, fileno(stderr)) < 0) |
| 552 | error("dup2 stdin failed: %.100s", strerror(errno)); |
| 553 | |
| 554 | /* Close the extra descriptor for the pseudo tty. */ |
| 555 | close(ttyfd); |
| 556 | |
| 557 | ///XXXX ? move to do_child() ?? |
| 558 | /* |
| 559 | * Get IP address of client. This is needed because we want |
| 560 | * to record where the user logged in from. If the |
| 561 | * connection is not a socket, let the ip address be 0.0.0.0. |
| 562 | */ |
| 563 | memset(&from, 0, sizeof(from)); |
| 564 | if (packet_connection_is_on_socket()) { |
| 565 | fromlen = sizeof(from); |
| 566 | if (getpeername(packet_get_connection_in(), |
| 567 | (struct sockaddr *) & from, &fromlen) < 0) { |
| 568 | debug("getpeername: %.100s", strerror(errno)); |
| 569 | fatal_cleanup(); |
| 570 | } |
| 571 | } |
| 572 | /* Record that there was a login on that terminal. */ |
| 573 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, hostname, |
| 574 | (struct sockaddr *)&from); |
| 575 | |
| 576 | /* Check if .hushlogin exists. */ |
| 577 | snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir); |
| 578 | quiet_login = stat(line, &st) >= 0; |
| 579 | |
| 580 | #ifdef USE_PAM |
| 581 | if (!quiet_login) |
| 582 | print_pam_messages(); |
| 583 | #endif /* USE_PAM */ |
| 584 | |
| 585 | /* |
| 586 | * If the user has logged in before, display the time of last |
| 587 | * login. However, don't display anything extra if a command |
| 588 | * has been specified (so that ssh can be used to execute |
| 589 | * commands on a remote machine without users knowing they |
| 590 | * are going to another machine). Login(1) will do this for |
| 591 | * us as well, so check if login(1) is used |
| 592 | */ |
| 593 | if (command == NULL && last_login_time != 0 && !quiet_login && |
| 594 | !options.use_login) { |
| 595 | /* Convert the date to a string. */ |
| 596 | time_string = ctime(&last_login_time); |
| 597 | /* Remove the trailing newline. */ |
| 598 | if (strchr(time_string, '\n')) |
| 599 | *strchr(time_string, '\n') = 0; |
| 600 | /* Display the last login time. Host if displayed |
| 601 | if known. */ |
| 602 | if (strcmp(buf, "") == 0) |
| 603 | printf("Last login: %s\r\n", time_string); |
| 604 | else |
| 605 | printf("Last login: %s from %s\r\n", time_string, buf); |
| 606 | } |
| 607 | /* |
| 608 | * Print /etc/motd unless a command was specified or printing |
| 609 | * it was disabled in server options or login(1) will be |
| 610 | * used. Note that some machines appear to print it in |
| 611 | * /etc/profile or similar. |
| 612 | */ |
| 613 | if (command == NULL && options.print_motd && !quiet_login && |
| 614 | !options.use_login) { |
| 615 | /* Print /etc/motd if it exists. */ |
| 616 | f = fopen("/etc/motd", "r"); |
| 617 | if (f) { |
| 618 | while (fgets(line, sizeof(line), f)) |
| 619 | fputs(line, stdout); |
| 620 | fclose(f); |
| 621 | } |
| 622 | } |
| 623 | /* Do common processing for the child, such as execing the command. */ |
| 624 | do_child(command, pw, s->term, s->display, s->auth_proto, s->auth_data, s->tty); |
| 625 | /* NOTREACHED */ |
| 626 | } |
| 627 | if (pid < 0) |
| 628 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 629 | s->pid = pid; |
| 630 | |
| 631 | /* Parent. Close the slave side of the pseudo tty. */ |
| 632 | close(ttyfd); |
| 633 | |
| 634 | /* |
| 635 | * Create another descriptor of the pty master side for use as the |
| 636 | * standard input. We could use the original descriptor, but this |
| 637 | * simplifies code in server_loop. The descriptor is bidirectional. |
| 638 | */ |
| 639 | fdout = dup(ptyfd); |
| 640 | if (fdout < 0) |
| 641 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); |
| 642 | |
| 643 | /* we keep a reference to the pty master */ |
| 644 | ptymaster = dup(ptyfd); |
| 645 | if (ptymaster < 0) |
| 646 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); |
| 647 | s->ptymaster = ptymaster; |
| 648 | |
| 649 | /* Enter interactive session. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 650 | if (compat20) { |
| 651 | session_set_fds(s, ptyfd, fdout, -1); |
| 652 | } else { |
| 653 | server_loop(pid, ptyfd, fdout, -1); |
| 654 | /* server_loop _has_ closed ptyfd and fdout. */ |
| 655 | session_pty_cleanup(s); |
| 656 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Sets the value of the given variable in the environment. If the variable |
| 661 | * already exists, its value is overriden. |
| 662 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 663 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 664 | child_set_env(char ***envp, unsigned int *envsizep, const char *name, |
| 665 | const char *value) |
| 666 | { |
| 667 | unsigned int i, namelen; |
| 668 | char **env; |
| 669 | |
| 670 | /* |
| 671 | * Find the slot where the value should be stored. If the variable |
| 672 | * already exists, we reuse the slot; otherwise we append a new slot |
| 673 | * at the end of the array, expanding if necessary. |
| 674 | */ |
| 675 | env = *envp; |
| 676 | namelen = strlen(name); |
| 677 | for (i = 0; env[i]; i++) |
| 678 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') |
| 679 | break; |
| 680 | if (env[i]) { |
| 681 | /* Reuse the slot. */ |
| 682 | xfree(env[i]); |
| 683 | } else { |
| 684 | /* New variable. Expand if necessary. */ |
| 685 | if (i >= (*envsizep) - 1) { |
| 686 | (*envsizep) += 50; |
| 687 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); |
| 688 | } |
| 689 | /* Need to set the NULL pointer at end of array beyond the new slot. */ |
| 690 | env[i + 1] = NULL; |
| 691 | } |
| 692 | |
| 693 | /* Allocate space and format the variable in the appropriate slot. */ |
| 694 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); |
| 695 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Reads environment variables from the given file and adds/overrides them |
| 700 | * into the environment. If the file does not exist, this does nothing. |
| 701 | * Otherwise, it must consist of empty lines, comments (line starts with '#') |
| 702 | * and assignments of the form name=value. No other forms are allowed. |
| 703 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 704 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 705 | read_environment_file(char ***env, unsigned int *envsize, |
| 706 | const char *filename) |
| 707 | { |
| 708 | FILE *f; |
| 709 | char buf[4096]; |
| 710 | char *cp, *value; |
| 711 | |
| 712 | f = fopen(filename, "r"); |
| 713 | if (!f) |
| 714 | return; |
| 715 | |
| 716 | while (fgets(buf, sizeof(buf), f)) { |
| 717 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
| 718 | ; |
| 719 | if (!*cp || *cp == '#' || *cp == '\n') |
| 720 | continue; |
| 721 | if (strchr(cp, '\n')) |
| 722 | *strchr(cp, '\n') = '\0'; |
| 723 | value = strchr(cp, '='); |
| 724 | if (value == NULL) { |
| 725 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); |
| 726 | continue; |
| 727 | } |
| 728 | /* Replace the equals sign by nul, and advance value to the value string. */ |
| 729 | *value = '\0'; |
| 730 | value++; |
| 731 | child_set_env(env, envsize, cp, value); |
| 732 | } |
| 733 | fclose(f); |
| 734 | } |
| 735 | |
| 736 | #ifdef USE_PAM |
| 737 | /* |
| 738 | * Sets any environment variables which have been specified by PAM |
| 739 | */ |
| 740 | void do_pam_environment(char ***env, int *envsize) |
| 741 | { |
| 742 | char *equals, var_name[512], var_val[512]; |
| 743 | char **pam_env; |
| 744 | int i; |
| 745 | |
| 746 | if ((pam_env = fetch_pam_environment()) == NULL) |
| 747 | return; |
| 748 | |
| 749 | for(i = 0; pam_env[i] != NULL; i++) { |
| 750 | if ((equals = strstr(pam_env[i], "=")) == NULL) |
| 751 | continue; |
| 752 | |
| 753 | if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) { |
| 754 | memset(var_name, '\0', sizeof(var_name)); |
| 755 | memset(var_val, '\0', sizeof(var_val)); |
| 756 | |
| 757 | strncpy(var_name, pam_env[i], equals - pam_env[i]); |
| 758 | strcpy(var_val, equals + 1); |
| 759 | |
| 760 | debug("PAM environment: %s=%s", var_name, var_val); |
| 761 | |
| 762 | child_set_env(env, envsize, var_name, var_val); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | #endif /* USE_PAM */ |
| 767 | |
| 768 | /* |
| 769 | * Performs common processing for the child, such as setting up the |
| 770 | * environment, closing extra file descriptors, setting the user and group |
| 771 | * ids, and executing the command or shell. |
| 772 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 773 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 774 | do_child(const char *command, struct passwd * pw, const char *term, |
| 775 | const char *display, const char *auth_proto, |
| 776 | const char *auth_data, const char *ttyname) |
| 777 | { |
| 778 | const char *shell, *cp = NULL; |
| 779 | char buf[256]; |
| 780 | FILE *f; |
| 781 | unsigned int envsize, i; |
| 782 | char **env; |
| 783 | extern char **environ; |
| 784 | struct stat st; |
| 785 | char *argv[10]; |
| 786 | |
| 787 | #ifndef USE_PAM /* pam_nologin handles this */ |
| 788 | f = fopen("/etc/nologin", "r"); |
| 789 | if (f) { |
| 790 | /* /etc/nologin exists. Print its contents and exit. */ |
| 791 | while (fgets(buf, sizeof(buf), f)) |
| 792 | fputs(buf, stderr); |
| 793 | fclose(f); |
| 794 | if (pw->pw_uid != 0) |
| 795 | exit(254); |
| 796 | } |
| 797 | #endif /* USE_PAM */ |
| 798 | |
| 799 | /* Set login name in the kernel. */ |
| 800 | if (setlogin(pw->pw_name) < 0) |
| 801 | error("setlogin failed: %s", strerror(errno)); |
| 802 | |
| 803 | /* Set uid, gid, and groups. */ |
| 804 | /* Login(1) does this as well, and it needs uid 0 for the "-h" |
| 805 | switch, so we let login(1) to this for us. */ |
| 806 | if (!options.use_login) { |
| 807 | if (getuid() == 0 || geteuid() == 0) { |
| 808 | if (setgid(pw->pw_gid) < 0) { |
| 809 | perror("setgid"); |
| 810 | exit(1); |
| 811 | } |
| 812 | /* Initialize the group list. */ |
| 813 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { |
| 814 | perror("initgroups"); |
| 815 | exit(1); |
| 816 | } |
| 817 | endgrent(); |
| 818 | |
| 819 | /* Permanently switch to the desired uid. */ |
| 820 | permanently_set_uid(pw->pw_uid); |
| 821 | } |
| 822 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
| 823 | fatal("Failed to set uids to %d.", (int) pw->pw_uid); |
| 824 | } |
| 825 | /* |
| 826 | * Get the shell from the password data. An empty shell field is |
| 827 | * legal, and means /bin/sh. |
| 828 | */ |
| 829 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
| 830 | |
| 831 | #ifdef AFS |
| 832 | /* Try to get AFS tokens for the local cell. */ |
| 833 | if (k_hasafs()) { |
| 834 | char cell[64]; |
| 835 | |
| 836 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) |
| 837 | krb_afslog(cell, 0); |
| 838 | |
| 839 | krb_afslog(0, 0); |
| 840 | } |
| 841 | #endif /* AFS */ |
| 842 | |
| 843 | /* Initialize the environment. */ |
| 844 | envsize = 100; |
| 845 | env = xmalloc(envsize * sizeof(char *)); |
| 846 | env[0] = NULL; |
| 847 | |
| 848 | if (!options.use_login) { |
| 849 | /* Set basic environment. */ |
| 850 | child_set_env(&env, &envsize, "USER", pw->pw_name); |
| 851 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); |
| 852 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); |
| 853 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); |
| 854 | |
| 855 | snprintf(buf, sizeof buf, "%.200s/%.50s", |
| 856 | _PATH_MAILDIR, pw->pw_name); |
| 857 | child_set_env(&env, &envsize, "MAIL", buf); |
| 858 | |
| 859 | /* Normal systems set SHELL by default. */ |
| 860 | child_set_env(&env, &envsize, "SHELL", shell); |
| 861 | } |
| 862 | if (getenv("TZ")) |
| 863 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); |
| 864 | |
| 865 | /* Set custom environment options from RSA authentication. */ |
| 866 | while (custom_environment) { |
| 867 | struct envstring *ce = custom_environment; |
| 868 | char *s = ce->s; |
| 869 | int i; |
| 870 | for (i = 0; s[i] != '=' && s[i]; i++); |
| 871 | if (s[i] == '=') { |
| 872 | s[i] = 0; |
| 873 | child_set_env(&env, &envsize, s, s + i + 1); |
| 874 | } |
| 875 | custom_environment = ce->next; |
| 876 | xfree(ce->s); |
| 877 | xfree(ce); |
| 878 | } |
| 879 | |
| 880 | snprintf(buf, sizeof buf, "%.50s %d %d", |
| 881 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
| 882 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
| 883 | |
| 884 | if (ttyname) |
| 885 | child_set_env(&env, &envsize, "SSH_TTY", ttyname); |
| 886 | if (term) |
| 887 | child_set_env(&env, &envsize, "TERM", term); |
| 888 | if (display) |
| 889 | child_set_env(&env, &envsize, "DISPLAY", display); |
| 890 | |
| 891 | #ifdef _AIX |
| 892 | { |
| 893 | char *authstate,*krb5cc; |
| 894 | |
| 895 | if ((authstate = getenv("AUTHSTATE")) != NULL) |
| 896 | child_set_env(&env,&envsize,"AUTHSTATE",authstate); |
| 897 | |
| 898 | if ((krb5cc = getenv("KRB5CCNAME")) != NULL) |
| 899 | child_set_env(&env,&envsize,"KRB5CCNAME",krb5cc); |
| 900 | } |
| 901 | #endif |
| 902 | |
| 903 | #ifdef KRB4 |
| 904 | { |
| 905 | extern char *ticket; |
| 906 | |
| 907 | if (ticket) |
| 908 | child_set_env(&env, &envsize, "KRBTKFILE", ticket); |
| 909 | } |
| 910 | #endif /* KRB4 */ |
| 911 | |
| 912 | #ifdef USE_PAM |
| 913 | /* Pull in any environment variables that may have been set by PAM. */ |
| 914 | do_pam_environment(&env, &envsize); |
| 915 | #endif /* USE_PAM */ |
| 916 | |
| 917 | read_environment_file(&env,&envsize,"/etc/environment"); |
| 918 | |
| 919 | if (xauthfile) |
| 920 | child_set_env(&env, &envsize, "XAUTHORITY", xauthfile); |
| 921 | if (auth_get_socket_name() != NULL) |
| 922 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, |
| 923 | auth_get_socket_name()); |
| 924 | |
| 925 | /* read $HOME/.ssh/environment. */ |
| 926 | if (!options.use_login) { |
| 927 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", pw->pw_dir); |
| 928 | read_environment_file(&env, &envsize, buf); |
| 929 | } |
| 930 | if (debug_flag) { |
| 931 | /* dump the environment */ |
| 932 | fprintf(stderr, "Environment:\n"); |
| 933 | for (i = 0; env[i]; i++) |
| 934 | fprintf(stderr, " %.200s\n", env[i]); |
| 935 | } |
| 936 | /* |
| 937 | * Close the connection descriptors; note that this is the child, and |
| 938 | * the server will still have the socket open, and it is important |
| 939 | * that we do not shutdown it. Note that the descriptors cannot be |
| 940 | * closed before building the environment, as we call |
| 941 | * get_remote_ipaddr there. |
| 942 | */ |
| 943 | if (packet_get_connection_in() == packet_get_connection_out()) |
| 944 | close(packet_get_connection_in()); |
| 945 | else { |
| 946 | close(packet_get_connection_in()); |
| 947 | close(packet_get_connection_out()); |
| 948 | } |
| 949 | /* |
| 950 | * Close all descriptors related to channels. They will still remain |
| 951 | * open in the parent. |
| 952 | */ |
| 953 | /* XXX better use close-on-exec? -markus */ |
| 954 | channel_close_all(); |
| 955 | |
| 956 | /* |
| 957 | * Close any extra file descriptors. Note that there may still be |
| 958 | * descriptors left by system functions. They will be closed later. |
| 959 | */ |
| 960 | endpwent(); |
| 961 | |
| 962 | /* |
| 963 | * Close any extra open file descriptors so that we don\'t have them |
| 964 | * hanging around in clients. Note that we want to do this after |
| 965 | * initgroups, because at least on Solaris 2.3 it leaves file |
| 966 | * descriptors open. |
| 967 | */ |
| 968 | for (i = 3; i < 64; i++) |
| 969 | close(i); |
| 970 | |
| 971 | /* Change current directory to the user\'s home directory. */ |
| 972 | if (chdir(pw->pw_dir) < 0) |
| 973 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", |
| 974 | pw->pw_dir, strerror(errno)); |
| 975 | |
| 976 | /* |
| 977 | * Must take new environment into use so that .ssh/rc, /etc/sshrc and |
| 978 | * xauth are run in the proper environment. |
| 979 | */ |
| 980 | environ = env; |
| 981 | |
| 982 | /* |
| 983 | * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first |
| 984 | * in this order). |
| 985 | */ |
| 986 | if (!options.use_login) { |
| 987 | if (stat(SSH_USER_RC, &st) >= 0) { |
| 988 | if (debug_flag) |
| 989 | fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC); |
| 990 | |
| 991 | f = popen("/bin/sh " SSH_USER_RC, "w"); |
| 992 | if (f) { |
| 993 | if (auth_proto != NULL && auth_data != NULL) |
| 994 | fprintf(f, "%s %s\n", auth_proto, auth_data); |
| 995 | pclose(f); |
| 996 | } else |
| 997 | fprintf(stderr, "Could not run %s\n", SSH_USER_RC); |
| 998 | } else if (stat(SSH_SYSTEM_RC, &st) >= 0) { |
| 999 | if (debug_flag) |
| 1000 | fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC); |
| 1001 | |
| 1002 | f = popen("/bin/sh " SSH_SYSTEM_RC, "w"); |
| 1003 | if (f) { |
| 1004 | if (auth_proto != NULL && auth_data != NULL) |
| 1005 | fprintf(f, "%s %s\n", auth_proto, auth_data); |
| 1006 | pclose(f); |
| 1007 | } else |
| 1008 | fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC); |
| 1009 | } |
| 1010 | #ifdef XAUTH_PATH |
| 1011 | else { |
| 1012 | /* Add authority data to .Xauthority if appropriate. */ |
| 1013 | if (auth_proto != NULL && auth_data != NULL) { |
| 1014 | if (debug_flag) |
| 1015 | fprintf(stderr, "Running %.100s add %.100s %.100s %.100s\n", |
| 1016 | XAUTH_PATH, display, auth_proto, auth_data); |
| 1017 | |
| 1018 | f = popen(XAUTH_PATH " -q -", "w"); |
| 1019 | if (f) { |
| 1020 | fprintf(f, "add %s %s %s\n", display, auth_proto, auth_data); |
| 1021 | pclose(f); |
| 1022 | } else |
| 1023 | fprintf(stderr, "Could not run %s -q -\n", XAUTH_PATH); |
| 1024 | } |
| 1025 | } |
| 1026 | #endif /* XAUTH_PATH */ |
| 1027 | |
| 1028 | /* Get the last component of the shell name. */ |
| 1029 | cp = strrchr(shell, '/'); |
| 1030 | if (cp) |
| 1031 | cp++; |
| 1032 | else |
| 1033 | cp = shell; |
| 1034 | } |
| 1035 | /* |
| 1036 | * If we have no command, execute the shell. In this case, the shell |
| 1037 | * name to be passed in argv[0] is preceded by '-' to indicate that |
| 1038 | * this is a login shell. |
| 1039 | */ |
| 1040 | if (!command) { |
| 1041 | if (!options.use_login) { |
| 1042 | char buf[256]; |
| 1043 | |
| 1044 | /* |
| 1045 | * Check for mail if we have a tty and it was enabled |
| 1046 | * in server options. |
| 1047 | */ |
| 1048 | if (ttyname && options.check_mail) { |
| 1049 | char *mailbox; |
| 1050 | struct stat mailstat; |
| 1051 | mailbox = getenv("MAIL"); |
| 1052 | if (mailbox != NULL) { |
| 1053 | if (stat(mailbox, &mailstat) != 0 || mailstat.st_size == 0) |
| 1054 | printf("No mail.\n"); |
| 1055 | else if (mailstat.st_mtime < mailstat.st_atime) |
| 1056 | printf("You have mail.\n"); |
| 1057 | else |
| 1058 | printf("You have new mail.\n"); |
| 1059 | } |
| 1060 | } |
| 1061 | /* Start the shell. Set initial character to '-'. */ |
| 1062 | buf[0] = '-'; |
| 1063 | strncpy(buf + 1, cp, sizeof(buf) - 1); |
| 1064 | buf[sizeof(buf) - 1] = 0; |
| 1065 | |
| 1066 | /* Execute the shell. */ |
| 1067 | argv[0] = buf; |
| 1068 | argv[1] = NULL; |
| 1069 | execve(shell, argv, env); |
| 1070 | |
| 1071 | /* Executing the shell failed. */ |
| 1072 | perror(shell); |
| 1073 | exit(1); |
| 1074 | |
| 1075 | } else { |
| 1076 | /* Launch login(1). */ |
| 1077 | |
| 1078 | execl("/usr/bin/login", "login", "-h", get_remote_ipaddr(), |
| 1079 | "-p", "-f", "--", pw->pw_name, NULL); |
| 1080 | |
| 1081 | /* Login couldn't be executed, die. */ |
| 1082 | |
| 1083 | perror("login"); |
| 1084 | exit(1); |
| 1085 | } |
| 1086 | } |
| 1087 | /* |
| 1088 | * Execute the command using the user's shell. This uses the -c |
| 1089 | * option to execute the command. |
| 1090 | */ |
| 1091 | argv[0] = (char *) cp; |
| 1092 | argv[1] = "-c"; |
| 1093 | argv[2] = (char *) command; |
| 1094 | argv[3] = NULL; |
| 1095 | execve(shell, argv, env); |
| 1096 | perror(shell); |
| 1097 | exit(1); |
| 1098 | } |
| 1099 | |
| 1100 | Session * |
| 1101 | session_new(void) |
| 1102 | { |
| 1103 | int i; |
| 1104 | static int did_init = 0; |
| 1105 | if (!did_init) { |
| 1106 | debug("session_new: init"); |
| 1107 | for(i = 0; i < MAX_SESSIONS; i++) { |
| 1108 | sessions[i].used = 0; |
| 1109 | sessions[i].self = i; |
| 1110 | } |
| 1111 | did_init = 1; |
| 1112 | } |
| 1113 | for(i = 0; i < MAX_SESSIONS; i++) { |
| 1114 | Session *s = &sessions[i]; |
| 1115 | if (! s->used) { |
| 1116 | s->pid = 0; |
| 1117 | s->chanid = -1; |
| 1118 | s->ptyfd = -1; |
| 1119 | s->ttyfd = -1; |
| 1120 | s->term = NULL; |
| 1121 | s->pw = NULL; |
| 1122 | s->display = NULL; |
| 1123 | s->screen = 0; |
| 1124 | s->auth_data = NULL; |
| 1125 | s->auth_proto = NULL; |
| 1126 | s->used = 1; |
| 1127 | debug("session_new: session %d", i); |
| 1128 | return s; |
| 1129 | } |
| 1130 | } |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | |
| 1134 | void |
| 1135 | session_dump(void) |
| 1136 | { |
| 1137 | int i; |
| 1138 | for(i = 0; i < MAX_SESSIONS; i++) { |
| 1139 | Session *s = &sessions[i]; |
| 1140 | debug("dump: used %d session %d %p channel %d pid %d", |
| 1141 | s->used, |
| 1142 | s->self, |
| 1143 | s, |
| 1144 | s->chanid, |
| 1145 | s->pid); |
| 1146 | } |
| 1147 | } |
| 1148 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1149 | int |
| 1150 | session_open(int chanid) |
| 1151 | { |
| 1152 | Session *s = session_new(); |
| 1153 | debug("session_open: channel %d", chanid); |
| 1154 | if (s == NULL) { |
| 1155 | error("no more sessions"); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | debug("session_open: session %d: link with channel %d", s->self, chanid); |
| 1159 | s->chanid = chanid; |
| 1160 | s->pw = auth_get_user(); |
| 1161 | if (s->pw == NULL) |
| 1162 | fatal("no user for session %i channel %d", |
| 1163 | s->self, s->chanid); |
| 1164 | return 1; |
| 1165 | } |
| 1166 | |
| 1167 | Session * |
| 1168 | session_by_channel(int id) |
| 1169 | { |
| 1170 | int i; |
| 1171 | for(i = 0; i < MAX_SESSIONS; i++) { |
| 1172 | Session *s = &sessions[i]; |
| 1173 | if (s->used && s->chanid == id) { |
| 1174 | debug("session_by_channel: session %d channel %d", i, id); |
| 1175 | return s; |
| 1176 | } |
| 1177 | } |
| 1178 | debug("session_by_channel: unknown channel %d", id); |
| 1179 | session_dump(); |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | |
| 1183 | Session * |
| 1184 | session_by_pid(pid_t pid) |
| 1185 | { |
| 1186 | int i; |
| 1187 | debug("session_by_pid: pid %d", pid); |
| 1188 | for(i = 0; i < MAX_SESSIONS; i++) { |
| 1189 | Session *s = &sessions[i]; |
| 1190 | if (s->used && s->pid == pid) |
| 1191 | return s; |
| 1192 | } |
| 1193 | error("session_by_pid: unknown pid %d", pid); |
| 1194 | session_dump(); |
| 1195 | return NULL; |
| 1196 | } |
| 1197 | |
| 1198 | int |
| 1199 | session_window_change_req(Session *s) |
| 1200 | { |
| 1201 | s->col = packet_get_int(); |
| 1202 | s->row = packet_get_int(); |
| 1203 | s->xpixel = packet_get_int(); |
| 1204 | s->ypixel = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1205 | packet_done(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1206 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1207 | return 1; |
| 1208 | } |
| 1209 | |
| 1210 | int |
| 1211 | session_pty_req(Session *s) |
| 1212 | { |
| 1213 | unsigned int len; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1214 | char *term_modes; /* encoded terminal modes */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1215 | |
| 1216 | if (s->ttyfd != -1) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1217 | return 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1218 | s->term = packet_get_string(&len); |
| 1219 | s->col = packet_get_int(); |
| 1220 | s->row = packet_get_int(); |
| 1221 | s->xpixel = packet_get_int(); |
| 1222 | s->ypixel = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1223 | term_modes = packet_get_string(&len); |
| 1224 | packet_done(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1225 | |
| 1226 | if (strcmp(s->term, "") == 0) { |
| 1227 | xfree(s->term); |
| 1228 | s->term = NULL; |
| 1229 | } |
| 1230 | /* Allocate a pty and open it. */ |
| 1231 | if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) { |
| 1232 | xfree(s->term); |
| 1233 | s->term = NULL; |
| 1234 | s->ptyfd = -1; |
| 1235 | s->ttyfd = -1; |
| 1236 | error("session_pty_req: session %d alloc failed", s->self); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1237 | xfree(term_modes); |
| 1238 | return 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1239 | } |
| 1240 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); |
| 1241 | /* |
| 1242 | * Add a cleanup function to clear the utmp entry and record logout |
| 1243 | * time in case we call fatal() (e.g., the connection gets closed). |
| 1244 | */ |
| 1245 | fatal_add_cleanup(pty_cleanup_proc, (void *)s); |
| 1246 | pty_setowner(s->pw, s->tty); |
| 1247 | /* Get window size from the packet. */ |
| 1248 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1249 | |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 1250 | /* XXX parse and set terminal modes */ |
| 1251 | xfree(term_modes); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1252 | return 1; |
| 1253 | } |
| 1254 | |
| 1255 | void |
| 1256 | session_input_channel_req(int id, void *arg) |
| 1257 | { |
| 1258 | unsigned int len; |
| 1259 | int reply; |
| 1260 | int success = 0; |
| 1261 | char *rtype; |
| 1262 | Session *s; |
| 1263 | Channel *c; |
| 1264 | |
| 1265 | rtype = packet_get_string(&len); |
| 1266 | reply = packet_get_char(); |
| 1267 | |
| 1268 | s = session_by_channel(id); |
| 1269 | if (s == NULL) |
| 1270 | fatal("session_input_channel_req: channel %d: no session", id); |
| 1271 | c = channel_lookup(id); |
| 1272 | if (c == NULL) |
| 1273 | fatal("session_input_channel_req: channel %d: bad channel", id); |
| 1274 | |
| 1275 | debug("session_input_channel_req: session %d channel %d request %s reply %d", |
| 1276 | s->self, id, rtype, reply); |
| 1277 | |
| 1278 | /* |
| 1279 | * a session is in LARVAL state until a shell |
| 1280 | * or programm is executed |
| 1281 | */ |
| 1282 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 1283 | if (strcmp(rtype, "shell") == 0) { |
| 1284 | if (s->ttyfd == -1) |
| 1285 | do_exec_no_pty(s, NULL, s->pw); |
| 1286 | else |
| 1287 | do_exec_pty(s, NULL, s->pw); |
| 1288 | success = 1; |
| 1289 | } else if (strcmp(rtype, "exec") == 0) { |
| 1290 | char *command = packet_get_string(&len); |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 1291 | packet_done(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1292 | if (s->ttyfd == -1) |
| 1293 | do_exec_no_pty(s, command, s->pw); |
| 1294 | else |
| 1295 | do_exec_pty(s, command, s->pw); |
| 1296 | xfree(command); |
| 1297 | success = 1; |
| 1298 | } else if (strcmp(rtype, "pty-req") == 0) { |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 1299 | success = session_pty_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | if (strcmp(rtype, "window-change") == 0) { |
| 1303 | success = session_window_change_req(s); |
| 1304 | } |
| 1305 | |
| 1306 | if (reply) { |
| 1307 | packet_start(success ? |
| 1308 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
| 1309 | packet_put_int(c->remote_id); |
| 1310 | packet_send(); |
| 1311 | } |
| 1312 | xfree(rtype); |
| 1313 | } |
| 1314 | |
| 1315 | void |
| 1316 | session_set_fds(Session *s, int fdin, int fdout, int fderr) |
| 1317 | { |
| 1318 | if (!compat20) |
| 1319 | fatal("session_set_fds: called for proto != 2.0"); |
| 1320 | /* |
| 1321 | * now that have a child and a pipe to the child, |
| 1322 | * we can activate our channel and register the fd's |
| 1323 | */ |
| 1324 | if (s->chanid == -1) |
| 1325 | fatal("no channel for session %d", s->self); |
| 1326 | channel_set_fds(s->chanid, |
| 1327 | fdout, fdin, fderr, |
| 1328 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ); |
| 1329 | } |
| 1330 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1331 | void |
| 1332 | session_pty_cleanup(Session *s) |
| 1333 | { |
| 1334 | if (s == NULL || s->ttyfd == -1) |
| 1335 | return; |
| 1336 | |
| 1337 | debug("session_pty_cleanup: session %i release %s", s->self, s->tty); |
| 1338 | |
| 1339 | /* Cancel the cleanup function. */ |
| 1340 | fatal_remove_cleanup(pty_cleanup_proc, (void *)s); |
| 1341 | |
| 1342 | /* Record that the user has logged out. */ |
| 1343 | record_logout(s->pid, s->tty); |
| 1344 | |
| 1345 | /* Release the pseudo-tty. */ |
| 1346 | pty_release(s->tty); |
| 1347 | |
| 1348 | /* |
| 1349 | * Close the server side of the socket pairs. We must do this after |
| 1350 | * the pty cleanup, so that another process doesn't get this pty |
| 1351 | * while we're still cleaning up. |
| 1352 | */ |
| 1353 | if (close(s->ptymaster) < 0) |
| 1354 | error("close(s->ptymaster): %s", strerror(errno)); |
| 1355 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1356 | |
| 1357 | void |
| 1358 | session_exit_message(Session *s, int status) |
| 1359 | { |
| 1360 | Channel *c; |
| 1361 | if (s == NULL) |
| 1362 | fatal("session_close: no session"); |
| 1363 | c = channel_lookup(s->chanid); |
| 1364 | if (c == NULL) |
| 1365 | fatal("session_close: session %d: no channel %d", |
| 1366 | s->self, s->chanid); |
| 1367 | debug("session_exit_message: session %d channel %d pid %d", |
| 1368 | s->self, s->chanid, s->pid); |
| 1369 | |
| 1370 | if (WIFEXITED(status)) { |
| 1371 | channel_request_start(s->chanid, |
| 1372 | "exit-status", 0); |
| 1373 | packet_put_int(WEXITSTATUS(status)); |
| 1374 | packet_send(); |
| 1375 | } else if (WIFSIGNALED(status)) { |
| 1376 | channel_request_start(s->chanid, |
| 1377 | "exit-signal", 0); |
| 1378 | packet_put_int(WTERMSIG(status)); |
| 1379 | packet_put_char(WCOREDUMP(status)); |
| 1380 | packet_put_cstring(""); |
| 1381 | packet_put_cstring(""); |
| 1382 | packet_send(); |
| 1383 | } else { |
| 1384 | /* Some weird exit cause. Just exit. */ |
| 1385 | packet_disconnect("wait returned status %04x.", status); |
| 1386 | } |
| 1387 | |
| 1388 | /* disconnect channel */ |
| 1389 | debug("session_exit_message: release channel %d", s->chanid); |
| 1390 | channel_cancel_cleanup(s->chanid); |
| 1391 | if (c->istate == CHAN_INPUT_OPEN) |
| 1392 | chan_read_failed(c); |
| 1393 | chan_write_failed(c); |
| 1394 | s->chanid = -1; |
| 1395 | } |
| 1396 | |
| 1397 | void |
| 1398 | session_free(Session *s) |
| 1399 | { |
| 1400 | debug("session_free: session %d pid %d", s->self, s->pid); |
| 1401 | if (s->term) |
| 1402 | xfree(s->term); |
| 1403 | if (s->display) |
| 1404 | xfree(s->display); |
| 1405 | if (s->auth_data) |
| 1406 | xfree(s->auth_data); |
| 1407 | if (s->auth_proto) |
| 1408 | xfree(s->auth_proto); |
| 1409 | s->used = 0; |
| 1410 | } |
| 1411 | |
| 1412 | void |
| 1413 | session_close(Session *s) |
| 1414 | { |
| 1415 | session_pty_cleanup(s); |
| 1416 | session_free(s); |
| 1417 | } |
| 1418 | |
| 1419 | void |
| 1420 | session_close_by_pid(pid_t pid, int status) |
| 1421 | { |
| 1422 | Session *s = session_by_pid(pid); |
| 1423 | if (s == NULL) { |
| 1424 | debug("session_close_by_pid: no session for pid %d", s->pid); |
| 1425 | return; |
| 1426 | } |
| 1427 | if (s->chanid != -1) |
| 1428 | session_exit_message(s, status); |
| 1429 | session_close(s); |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * this is called when a channel dies before |
| 1434 | * the session 'child' itself dies |
| 1435 | */ |
| 1436 | void |
| 1437 | session_close_by_channel(int id, void *arg) |
| 1438 | { |
| 1439 | Session *s = session_by_channel(id); |
| 1440 | if (s == NULL) { |
| 1441 | debug("session_close_by_channel: no session for channel %d", id); |
| 1442 | return; |
| 1443 | } |
| 1444 | /* disconnect channel */ |
| 1445 | channel_cancel_cleanup(s->chanid); |
| 1446 | s->chanid = -1; |
| 1447 | |
| 1448 | debug("session_close_by_channel: channel %d kill %d", id, s->pid); |
| 1449 | if (s->pid == 0) { |
| 1450 | /* close session immediately */ |
| 1451 | session_close(s); |
| 1452 | } else { |
| 1453 | /* notify child, delay session cleanup */ |
| 1454 | if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0) |
| 1455 | error("session_close_by_channel: kill %d: %s", |
| 1456 | s->pid, strerror(errno)); |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | void |
| 1461 | do_authenticated2(void) |
| 1462 | { |
| 1463 | /* |
| 1464 | * Cancel the alarm we set to limit the time taken for |
| 1465 | * authentication. |
| 1466 | */ |
| 1467 | alarm(0); |
| 1468 | log("do_authenticated2"); |
| 1469 | server_loop2(); |
| 1470 | } |