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