Annotation of src/usr.bin/ssh/session.c, Revision 1.16
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.16 ! markus 11: RCSID("$OpenBSD: session.c,v 1.15 2000/05/30 17:23:37 markus 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: }
1.16 ! markus 251: if (!options.xauth_location) {
! 252: packet_send_debug("No xauth program; cannot forward with spoofing.");
! 253: break;
! 254: }
1.1 markus 255: if (no_x11_forwarding_flag) {
256: packet_send_debug("X11 forwarding not permitted for this authentication.");
257: break;
258: }
259: debug("Received request for X11 forwarding with auth spoofing.");
260: if (s->display != NULL)
261: packet_disconnect("Protocol error: X11 display already set.");
262:
263: s->auth_proto = packet_get_string(&proto_len);
264: s->auth_data = packet_get_string(&data_len);
265: packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
266:
267: if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
268: s->screen = packet_get_int();
269: else
270: s->screen = 0;
271: s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
272:
273: if (s->display == NULL)
274: break;
275:
276: /* Setup to always have a local .Xauthority. */
277: xauthfile = xmalloc(MAXPATHLEN);
278: strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
279: temporarily_use_uid(pw->pw_uid);
280: if (mkdtemp(xauthfile) == NULL) {
281: restore_uid();
282: error("private X11 dir: mkdtemp %s failed: %s",
283: xauthfile, strerror(errno));
284: xfree(xauthfile);
285: xauthfile = NULL;
1.7 markus 286: /* XXXX remove listening channels */
1.1 markus 287: break;
288: }
289: strlcat(xauthfile, "/cookies", MAXPATHLEN);
290: open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
291: restore_uid();
292: fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
293: success = 1;
294: break;
295:
296: case SSH_CMSG_AGENT_REQUEST_FORWARDING:
297: if (no_agent_forwarding_flag || compat13) {
298: debug("Authentication agent forwarding not permitted for this authentication.");
299: break;
300: }
301: debug("Received authentication agent forwarding request.");
1.15 markus 302: success = auth_input_request_forwarding(pw);
1.1 markus 303: break;
304:
305: case SSH_CMSG_PORT_FORWARD_REQUEST:
306: if (no_port_forwarding_flag) {
307: debug("Port forwarding not permitted for this authentication.");
308: break;
309: }
310: debug("Received TCP/IP port forwarding request.");
1.12 markus 311: channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
1.1 markus 312: success = 1;
313: break;
314:
315: case SSH_CMSG_MAX_PACKET_SIZE:
316: if (packet_set_maxsize(packet_get_int()) > 0)
317: success = 1;
318: break;
319:
320: case SSH_CMSG_EXEC_SHELL:
321: case SSH_CMSG_EXEC_CMD:
322: /* Set interactive/non-interactive mode. */
323: packet_set_interactive(have_pty || s->display != NULL,
324: options.keepalives);
325:
326: if (type == SSH_CMSG_EXEC_CMD) {
327: command = packet_get_string(&dlen);
328: debug("Exec command '%.500s'", command);
329: packet_integrity_check(plen, 4 + dlen, type);
330: } else {
331: command = NULL;
332: packet_integrity_check(plen, 0, type);
333: }
334: if (forced_command != NULL) {
335: command = forced_command;
336: debug("Forced command '%.500s'", forced_command);
337: }
338: if (have_pty)
339: do_exec_pty(s, command, pw);
340: else
341: do_exec_no_pty(s, command, pw);
342:
343: if (command != NULL)
344: xfree(command);
345: /* Cleanup user's local Xauthority file. */
346: if (xauthfile)
347: xauthfile_cleanup_proc(NULL);
348: return;
349:
350: default:
351: /*
352: * Any unknown messages in this phase are ignored,
353: * and a failure message is returned.
354: */
355: log("Unknown packet type received after authentication: %d", type);
356: }
357: packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
358: packet_send();
359: packet_write_wait();
360:
361: /* Enable compression now that we have replied if appropriate. */
362: if (enable_compression_after_reply) {
363: enable_compression_after_reply = 0;
364: packet_start_compression(compression_level);
365: }
366: }
367: }
368:
369: /*
370: * This is called to fork and execute a command when we have no tty. This
371: * will call do_child from the child, and server_loop from the parent after
372: * setting up file descriptors and such.
373: */
1.4 markus 374: void
1.1 markus 375: do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
376: {
377: int pid;
378:
379: #ifdef USE_PIPES
380: int pin[2], pout[2], perr[2];
381: /* Allocate pipes for communicating with the program. */
382: if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
383: packet_disconnect("Could not create pipes: %.100s",
384: strerror(errno));
385: #else /* USE_PIPES */
386: int inout[2], err[2];
387: /* Uses socket pairs to communicate with the program. */
388: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
389: socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
390: packet_disconnect("Could not create socket pairs: %.100s",
391: strerror(errno));
392: #endif /* USE_PIPES */
393: if (s == NULL)
394: fatal("do_exec_no_pty: no session");
395:
1.9 markus 396: session_proctitle(s);
1.1 markus 397:
398: /* Fork the child. */
399: if ((pid = fork()) == 0) {
400: /* Child. Reinitialize the log since the pid has changed. */
401: log_init(__progname, options.log_level, options.log_facility, log_stderr);
402:
403: /*
404: * Create a new session and process group since the 4.4BSD
405: * setlogin() affects the entire process group.
406: */
407: if (setsid() < 0)
408: error("setsid failed: %.100s", strerror(errno));
409:
410: #ifdef USE_PIPES
411: /*
412: * Redirect stdin. We close the parent side of the socket
413: * pair, and make the child side the standard input.
414: */
415: close(pin[1]);
416: if (dup2(pin[0], 0) < 0)
417: perror("dup2 stdin");
418: close(pin[0]);
419:
420: /* Redirect stdout. */
421: close(pout[0]);
422: if (dup2(pout[1], 1) < 0)
423: perror("dup2 stdout");
424: close(pout[1]);
425:
426: /* Redirect stderr. */
427: close(perr[0]);
428: if (dup2(perr[1], 2) < 0)
429: perror("dup2 stderr");
430: close(perr[1]);
431: #else /* USE_PIPES */
432: /*
433: * Redirect stdin, stdout, and stderr. Stdin and stdout will
434: * use the same socket, as some programs (particularly rdist)
435: * seem to depend on it.
436: */
437: close(inout[1]);
438: close(err[1]);
439: if (dup2(inout[0], 0) < 0) /* stdin */
440: perror("dup2 stdin");
441: if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
442: perror("dup2 stdout");
443: if (dup2(err[0], 2) < 0) /* stderr */
444: perror("dup2 stderr");
445: #endif /* USE_PIPES */
446:
447: /* Do processing for the child (exec command etc). */
448: do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
449: /* NOTREACHED */
450: }
451: if (pid < 0)
452: packet_disconnect("fork failed: %.100s", strerror(errno));
453: s->pid = pid;
454: #ifdef USE_PIPES
455: /* We are the parent. Close the child sides of the pipes. */
456: close(pin[0]);
457: close(pout[1]);
458: close(perr[1]);
459:
1.2 markus 460: if (compat20) {
1.7 markus 461: session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
1.2 markus 462: } else {
463: /* Enter the interactive session. */
464: server_loop(pid, pin[1], pout[0], perr[0]);
465: /* server_loop has closed pin[1], pout[1], and perr[1]. */
466: }
1.1 markus 467: #else /* USE_PIPES */
468: /* We are the parent. Close the child sides of the socket pairs. */
469: close(inout[0]);
470: close(err[0]);
471:
472: /*
473: * Enter the interactive session. Note: server_loop must be able to
474: * handle the case that fdin and fdout are the same.
475: */
1.2 markus 476: if (compat20) {
1.7 markus 477: session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
1.2 markus 478: } else {
479: server_loop(pid, inout[1], inout[1], err[1]);
480: /* server_loop has closed inout[1] and err[1]. */
481: }
1.1 markus 482: #endif /* USE_PIPES */
483: }
484:
485: /*
486: * This is called to fork and execute a command when we have a tty. This
487: * will call do_child from the child, and server_loop from the parent after
488: * setting up file descriptors, controlling tty, updating wtmp, utmp,
489: * lastlog, and other such operations.
490: */
1.4 markus 491: void
1.1 markus 492: do_exec_pty(Session *s, const char *command, struct passwd * pw)
493: {
494: FILE *f;
495: char buf[100], *time_string;
496: char line[256];
497: const char *hostname;
498: int fdout, ptyfd, ttyfd, ptymaster;
499: int quiet_login;
500: pid_t pid;
501: socklen_t fromlen;
502: struct sockaddr_storage from;
503: struct stat st;
504: time_t last_login_time;
505:
506: if (s == NULL)
507: fatal("do_exec_pty: no session");
508: ptyfd = s->ptyfd;
509: ttyfd = s->ttyfd;
510:
511: /* Get remote host name. */
512: hostname = get_canonical_hostname();
513:
514: /*
515: * Get the time when the user last logged in. Buf will be set to
516: * contain the hostname the last login was from.
517: */
518: if (!options.use_login) {
519: last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
520: buf, sizeof(buf));
521: }
522:
523: /* Fork the child. */
524: if ((pid = fork()) == 0) {
525: pid = getpid();
526:
527: /* Child. Reinitialize the log because the pid has
528: changed. */
529: log_init(__progname, options.log_level, options.log_facility, log_stderr);
530:
531: /* Close the master side of the pseudo tty. */
532: close(ptyfd);
533:
534: /* Make the pseudo tty our controlling tty. */
535: pty_make_controlling_tty(&ttyfd, s->tty);
536:
537: /* Redirect stdin from the pseudo tty. */
538: if (dup2(ttyfd, fileno(stdin)) < 0)
539: error("dup2 stdin failed: %.100s", strerror(errno));
540:
541: /* Redirect stdout to the pseudo tty. */
542: if (dup2(ttyfd, fileno(stdout)) < 0)
543: error("dup2 stdin failed: %.100s", strerror(errno));
544:
545: /* Redirect stderr to the pseudo tty. */
546: if (dup2(ttyfd, fileno(stderr)) < 0)
547: error("dup2 stdin failed: %.100s", strerror(errno));
548:
549: /* Close the extra descriptor for the pseudo tty. */
550: close(ttyfd);
551:
1.11 markus 552: /* XXXX ? move to do_child() ??*/
1.1 markus 553: /*
554: * Get IP address of client. This is needed because we want
555: * to record where the user logged in from. If the
556: * connection is not a socket, let the ip address be 0.0.0.0.
557: */
558: memset(&from, 0, sizeof(from));
559: if (packet_connection_is_on_socket()) {
560: fromlen = sizeof(from);
561: if (getpeername(packet_get_connection_in(),
562: (struct sockaddr *) & from, &fromlen) < 0) {
563: debug("getpeername: %.100s", strerror(errno));
564: fatal_cleanup();
565: }
566: }
567: /* Record that there was a login on that terminal. */
568: record_login(pid, s->tty, pw->pw_name, pw->pw_uid, hostname,
569: (struct sockaddr *)&from);
570:
571: /* Check if .hushlogin exists. */
572: snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir);
573: quiet_login = stat(line, &st) >= 0;
574:
575: /*
576: * If the user has logged in before, display the time of last
577: * login. However, don't display anything extra if a command
578: * has been specified (so that ssh can be used to execute
579: * commands on a remote machine without users knowing they
580: * are going to another machine). Login(1) will do this for
581: * us as well, so check if login(1) is used
582: */
583: if (command == NULL && last_login_time != 0 && !quiet_login &&
584: !options.use_login) {
585: /* Convert the date to a string. */
586: time_string = ctime(&last_login_time);
587: /* Remove the trailing newline. */
588: if (strchr(time_string, '\n'))
589: *strchr(time_string, '\n') = 0;
590: /* Display the last login time. Host if displayed
591: if known. */
592: if (strcmp(buf, "") == 0)
593: printf("Last login: %s\r\n", time_string);
594: else
595: printf("Last login: %s from %s\r\n", time_string, buf);
596: }
597: /*
598: * Print /etc/motd unless a command was specified or printing
599: * it was disabled in server options or login(1) will be
600: * used. Note that some machines appear to print it in
601: * /etc/profile or similar.
602: */
603: if (command == NULL && options.print_motd && !quiet_login &&
604: !options.use_login) {
605: /* Print /etc/motd if it exists. */
606: f = fopen("/etc/motd", "r");
607: if (f) {
608: while (fgets(line, sizeof(line), f))
609: fputs(line, stdout);
610: fclose(f);
611: }
612: }
613: /* Do common processing for the child, such as execing the command. */
1.14 deraadt 614: do_child(command, pw, s->term, s->display, s->auth_proto,
615: s->auth_data, s->tty);
1.1 markus 616: /* NOTREACHED */
617: }
618: if (pid < 0)
619: packet_disconnect("fork failed: %.100s", strerror(errno));
620: s->pid = pid;
621:
622: /* Parent. Close the slave side of the pseudo tty. */
623: close(ttyfd);
624:
625: /*
626: * Create another descriptor of the pty master side for use as the
627: * standard input. We could use the original descriptor, but this
628: * simplifies code in server_loop. The descriptor is bidirectional.
629: */
630: fdout = dup(ptyfd);
631: if (fdout < 0)
632: packet_disconnect("dup #1 failed: %.100s", strerror(errno));
633:
634: /* we keep a reference to the pty master */
635: ptymaster = dup(ptyfd);
636: if (ptymaster < 0)
637: packet_disconnect("dup #2 failed: %.100s", strerror(errno));
638: s->ptymaster = ptymaster;
639:
640: /* Enter interactive session. */
1.2 markus 641: if (compat20) {
642: session_set_fds(s, ptyfd, fdout, -1);
643: } else {
644: server_loop(pid, ptyfd, fdout, -1);
645: /* server_loop _has_ closed ptyfd and fdout. */
646: session_pty_cleanup(s);
647: }
1.1 markus 648: }
649:
650: /*
651: * Sets the value of the given variable in the environment. If the variable
652: * already exists, its value is overriden.
653: */
1.4 markus 654: void
1.1 markus 655: child_set_env(char ***envp, unsigned int *envsizep, const char *name,
656: const char *value)
657: {
658: unsigned int i, namelen;
659: char **env;
660:
661: /*
662: * Find the slot where the value should be stored. If the variable
663: * already exists, we reuse the slot; otherwise we append a new slot
664: * at the end of the array, expanding if necessary.
665: */
666: env = *envp;
667: namelen = strlen(name);
668: for (i = 0; env[i]; i++)
669: if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
670: break;
671: if (env[i]) {
672: /* Reuse the slot. */
673: xfree(env[i]);
674: } else {
675: /* New variable. Expand if necessary. */
676: if (i >= (*envsizep) - 1) {
677: (*envsizep) += 50;
678: env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
679: }
680: /* Need to set the NULL pointer at end of array beyond the new slot. */
681: env[i + 1] = NULL;
682: }
683:
684: /* Allocate space and format the variable in the appropriate slot. */
685: env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
686: snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
687: }
688:
689: /*
690: * Reads environment variables from the given file and adds/overrides them
691: * into the environment. If the file does not exist, this does nothing.
692: * Otherwise, it must consist of empty lines, comments (line starts with '#')
693: * and assignments of the form name=value. No other forms are allowed.
694: */
1.4 markus 695: void
1.1 markus 696: read_environment_file(char ***env, unsigned int *envsize,
697: const char *filename)
698: {
699: FILE *f;
700: char buf[4096];
701: char *cp, *value;
702:
703: f = fopen(filename, "r");
704: if (!f)
705: return;
706:
707: while (fgets(buf, sizeof(buf), f)) {
708: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
709: ;
710: if (!*cp || *cp == '#' || *cp == '\n')
711: continue;
712: if (strchr(cp, '\n'))
713: *strchr(cp, '\n') = '\0';
714: value = strchr(cp, '=');
715: if (value == NULL) {
716: fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
717: continue;
718: }
1.14 deraadt 719: /*
720: * Replace the equals sign by nul, and advance value to
721: * the value string.
722: */
1.1 markus 723: *value = '\0';
724: value++;
725: child_set_env(env, envsize, cp, value);
726: }
727: fclose(f);
728: }
729:
730: /*
731: * Performs common processing for the child, such as setting up the
732: * environment, closing extra file descriptors, setting the user and group
733: * ids, and executing the command or shell.
734: */
1.4 markus 735: void
1.1 markus 736: do_child(const char *command, struct passwd * pw, const char *term,
737: const char *display, const char *auth_proto,
738: const char *auth_data, const char *ttyname)
739: {
740: const char *shell, *cp = NULL;
741: char buf[256];
1.16 ! markus 742: char cmd[1024];
1.1 markus 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);
1.16 ! markus 951: } else if (options.xauth_location != NULL) {
1.1 markus 952: /* Add authority data to .Xauthority if appropriate. */
953: if (auth_proto != NULL && auth_data != NULL) {
1.13 markus 954: char *screen = strchr(display, ':');
955: if (debug_flag) {
1.14 deraadt 956: fprintf(stderr,
957: "Running %.100s add %.100s %.100s %.100s\n",
1.16 ! markus 958: options.xauth_location, display,
! 959: auth_proto, auth_data);
1.13 markus 960: if (screen != NULL)
1.14 deraadt 961: fprintf(stderr,
962: "Adding %.*s/unix%s %s %s\n",
963: screen-display, display,
964: screen, auth_proto, auth_data);
1.13 markus 965: }
1.16 ! markus 966: snprintf(cmd, sizeof cmd, "%s -q -",
! 967: options.xauth_location);
! 968: f = popen(cmd, "w");
1.1 markus 969: if (f) {
1.14 deraadt 970: fprintf(f, "add %s %s %s\n", display,
971: auth_proto, auth_data);
1.13 markus 972: if (screen != NULL)
973: fprintf(f, "add %.*s/unix%s %s %s\n",
1.14 deraadt 974: screen-display, display,
975: screen, auth_proto, auth_data);
1.1 markus 976: pclose(f);
1.16 ! markus 977: } else {
! 978: fprintf(stderr, "Could not run %s\n",
! 979: cmd);
! 980: }
1.1 markus 981: }
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: }