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