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