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