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