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