Annotation of src/usr.bin/ssh/session.c, Revision 1.9
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.9 ! markus 11: RCSID("$OpenBSD: session.c,v 1.8 2000/04/29 16:06:08 markus Exp $");
1.1 markus 12:
13: #include "xmalloc.h"
14: #include "ssh.h"
15: #include "pty.h"
16: #include "packet.h"
17: #include "buffer.h"
18: #include "cipher.h"
19: #include "mpaux.h"
20: #include "servconf.h"
21: #include "uidswap.h"
22: #include "compat.h"
23: #include "channels.h"
24: #include "nchan.h"
25:
1.2 markus 26: #include "bufaux.h"
27: #include "ssh2.h"
28: #include "auth.h"
29:
1.1 markus 30: /* types */
31:
32: #define TTYSZ 64
33: typedef struct Session Session;
34: struct Session {
35: int used;
36: int self;
1.7 markus 37: int extended;
1.1 markus 38: struct passwd *pw;
39: pid_t pid;
40: /* tty */
41: char *term;
42: int ptyfd, ttyfd, ptymaster;
43: int row, col, xpixel, ypixel;
44: char tty[TTYSZ];
45: /* X11 */
46: char *display;
47: int screen;
48: char *auth_proto;
49: char *auth_data;
1.7 markus 50: int single_connection;
1.1 markus 51: /* proto 2 */
52: int chanid;
53: };
54:
55: /* func */
56:
57: Session *session_new(void);
58: void session_set_fds(Session *s, int fdin, int fdout, int fderr);
59: void session_pty_cleanup(Session *s);
1.9 ! markus 60: void session_proctitle(Session *s);
1.1 markus 61: void do_exec_pty(Session *s, const char *command, struct passwd * pw);
62: void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
63:
64: void
65: do_child(const char *command, struct passwd * pw, const char *term,
66: const char *display, const char *auth_proto,
67: const char *auth_data, const char *ttyname);
68:
69: /* import */
70: extern ServerOptions options;
71: extern char *__progname;
72: extern int log_stderr;
73: extern int debug_flag;
74:
75: /* Local Xauthority file. */
76: static char *xauthfile;
77:
78: /* data */
79: #define MAX_SESSIONS 10
80: Session sessions[MAX_SESSIONS];
81:
82: /* Flags set in auth-rsa from authorized_keys flags. These are set in auth-rsa.c. */
83: int no_port_forwarding_flag = 0;
84: int no_agent_forwarding_flag = 0;
85: int no_x11_forwarding_flag = 0;
86: int no_pty_flag = 0;
87:
88: /* RSA authentication "command=" option. */
89: char *forced_command = NULL;
90:
91: /* RSA authentication "environment=" options. */
92: struct envstring *custom_environment = NULL;
93:
94: /*
95: * Remove local Xauthority file.
96: */
97: void
98: xauthfile_cleanup_proc(void *ignore)
99: {
100: debug("xauthfile_cleanup_proc called");
101:
102: if (xauthfile != NULL) {
103: char *p;
104: unlink(xauthfile);
105: p = strrchr(xauthfile, '/');
106: if (p != NULL) {
107: *p = '\0';
108: rmdir(xauthfile);
109: }
110: xfree(xauthfile);
111: xauthfile = NULL;
112: }
113: }
114:
115: /*
116: * Function to perform cleanup if we get aborted abnormally (e.g., due to a
117: * dropped connection).
118: */
1.4 markus 119: void
1.1 markus 120: pty_cleanup_proc(void *session)
121: {
122: Session *s=session;
123: if (s == NULL)
124: fatal("pty_cleanup_proc: no session");
125: debug("pty_cleanup_proc: %s", s->tty);
126:
127: if (s->pid != 0) {
128: /* Record that the user has logged out. */
129: record_logout(s->pid, s->tty);
130: }
131:
132: /* Release the pseudo-tty. */
133: pty_release(s->tty);
134: }
135:
136: /*
137: * Prepares for an interactive session. This is called after the user has
138: * been successfully authenticated. During this message exchange, pseudo
139: * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
140: * are requested, etc.
141: */
1.4 markus 142: void
1.1 markus 143: do_authenticated(struct passwd * pw)
144: {
145: Session *s;
146: int type;
147: int compression_level = 0, enable_compression_after_reply = 0;
148: int have_pty = 0;
149: char *command;
150: int n_bytes;
151: int plen;
152: unsigned int proto_len, data_len, dlen;
153:
154: /*
155: * Cancel the alarm we set to limit the time taken for
156: * authentication.
157: */
158: alarm(0);
159:
160: /*
161: * Inform the channel mechanism that we are the server side and that
162: * the client may request to connect to any port at all. (The user
163: * could do it anyway, and we wouldn\'t know what is permitted except
164: * by the client telling us, so we can equally well trust the client
165: * not to request anything bogus.)
166: */
167: if (!no_port_forwarding_flag)
168: channel_permit_all_opens();
169:
170: s = session_new();
1.7 markus 171: s->pw = pw;
1.1 markus 172:
173: /*
174: * We stay in this loop until the client requests to execute a shell
175: * or a command.
176: */
177: for (;;) {
178: int success = 0;
179:
180: /* Get a packet from the client. */
181: type = packet_read(&plen);
182:
183: /* Process the packet. */
184: switch (type) {
185: case SSH_CMSG_REQUEST_COMPRESSION:
186: packet_integrity_check(plen, 4, type);
187: compression_level = packet_get_int();
188: if (compression_level < 1 || compression_level > 9) {
189: packet_send_debug("Received illegal compression level %d.",
190: compression_level);
191: break;
192: }
193: /* Enable compression after we have responded with SUCCESS. */
194: enable_compression_after_reply = 1;
195: success = 1;
196: break;
197:
198: case SSH_CMSG_REQUEST_PTY:
199: if (no_pty_flag) {
200: debug("Allocating a pty not permitted for this authentication.");
201: break;
202: }
203: if (have_pty)
204: packet_disconnect("Protocol error: you already have a pty.");
205:
206: debug("Allocating pty.");
207:
208: /* Allocate a pty and open it. */
209: if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
210: sizeof(s->tty))) {
211: error("Failed to allocate pty.");
212: break;
213: }
214: fatal_add_cleanup(pty_cleanup_proc, (void *)s);
215: pty_setowner(pw, s->tty);
216:
217: /* Get TERM from the packet. Note that the value may be of arbitrary length. */
218: s->term = packet_get_string(&dlen);
219: packet_integrity_check(dlen, strlen(s->term), type);
220: /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
221: /* Remaining bytes */
222: n_bytes = plen - (4 + dlen + 4 * 4);
223:
224: if (strcmp(s->term, "") == 0) {
225: xfree(s->term);
226: s->term = NULL;
227: }
228: /* Get window size from the packet. */
229: s->row = packet_get_int();
230: s->col = packet_get_int();
231: s->xpixel = packet_get_int();
232: s->ypixel = packet_get_int();
233: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
234:
235: /* Get tty modes from the packet. */
236: tty_parse_modes(s->ttyfd, &n_bytes);
237: packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
238:
239: /* Indicate that we now have a pty. */
240: success = 1;
241: have_pty = 1;
242: break;
243:
244: case SSH_CMSG_X11_REQUEST_FORWARDING:
245: if (!options.x11_forwarding) {
246: packet_send_debug("X11 forwarding disabled in server configuration file.");
247: break;
248: }
249: #ifdef XAUTH_PATH
250: if (no_x11_forwarding_flag) {
251: packet_send_debug("X11 forwarding not permitted for this authentication.");
252: break;
253: }
254: debug("Received request for X11 forwarding with auth spoofing.");
255: if (s->display != NULL)
256: packet_disconnect("Protocol error: X11 display already set.");
257:
258: s->auth_proto = packet_get_string(&proto_len);
259: s->auth_data = packet_get_string(&data_len);
260: packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
261:
262: if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
263: s->screen = packet_get_int();
264: else
265: s->screen = 0;
266: s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
267:
268: if (s->display == NULL)
269: break;
270:
271: /* Setup to always have a local .Xauthority. */
272: xauthfile = xmalloc(MAXPATHLEN);
273: strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
274: temporarily_use_uid(pw->pw_uid);
275: if (mkdtemp(xauthfile) == NULL) {
276: restore_uid();
277: error("private X11 dir: mkdtemp %s failed: %s",
278: xauthfile, strerror(errno));
279: xfree(xauthfile);
280: xauthfile = NULL;
1.7 markus 281: /* XXXX remove listening channels */
1.1 markus 282: break;
283: }
284: strlcat(xauthfile, "/cookies", MAXPATHLEN);
285: open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
286: restore_uid();
287: fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
288: success = 1;
289: break;
290: #else /* XAUTH_PATH */
291: packet_send_debug("No xauth program; cannot forward with spoofing.");
292: break;
293: #endif /* XAUTH_PATH */
294:
295: case SSH_CMSG_AGENT_REQUEST_FORWARDING:
296: if (no_agent_forwarding_flag || compat13) {
297: debug("Authentication agent forwarding not permitted for this authentication.");
298: break;
299: }
300: debug("Received authentication agent forwarding request.");
301: auth_input_request_forwarding(pw);
302: success = 1;
303: break;
304:
305: case SSH_CMSG_PORT_FORWARD_REQUEST:
306: if (no_port_forwarding_flag) {
307: debug("Port forwarding not permitted for this authentication.");
308: break;
309: }
310: debug("Received TCP/IP port forwarding request.");
311: channel_input_port_forward_request(pw->pw_uid == 0);
312: success = 1;
313: break;
314:
315: case SSH_CMSG_MAX_PACKET_SIZE:
316: if (packet_set_maxsize(packet_get_int()) > 0)
317: success = 1;
318: break;
319:
320: case SSH_CMSG_EXEC_SHELL:
321: case SSH_CMSG_EXEC_CMD:
322: /* Set interactive/non-interactive mode. */
323: packet_set_interactive(have_pty || s->display != NULL,
324: options.keepalives);
325:
326: if (type == SSH_CMSG_EXEC_CMD) {
327: command = packet_get_string(&dlen);
328: debug("Exec command '%.500s'", command);
329: packet_integrity_check(plen, 4 + dlen, type);
330: } else {
331: command = NULL;
332: packet_integrity_check(plen, 0, type);
333: }
334: if (forced_command != NULL) {
335: command = forced_command;
336: debug("Forced command '%.500s'", forced_command);
337: }
338: if (have_pty)
339: do_exec_pty(s, command, pw);
340: else
341: do_exec_no_pty(s, command, pw);
342:
343: if (command != NULL)
344: xfree(command);
345: /* Cleanup user's local Xauthority file. */
346: if (xauthfile)
347: xauthfile_cleanup_proc(NULL);
348: return;
349:
350: default:
351: /*
352: * Any unknown messages in this phase are ignored,
353: * and a failure message is returned.
354: */
355: log("Unknown packet type received after authentication: %d", type);
356: }
357: packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
358: packet_send();
359: packet_write_wait();
360:
361: /* Enable compression now that we have replied if appropriate. */
362: if (enable_compression_after_reply) {
363: enable_compression_after_reply = 0;
364: packet_start_compression(compression_level);
365: }
366: }
367: }
368:
369: /*
370: * This is called to fork and execute a command when we have no tty. This
371: * will call do_child from the child, and server_loop from the parent after
372: * setting up file descriptors and such.
373: */
1.4 markus 374: void
1.1 markus 375: do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
376: {
377: int pid;
378:
379: #ifdef USE_PIPES
380: int pin[2], pout[2], perr[2];
381: /* Allocate pipes for communicating with the program. */
382: if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
383: packet_disconnect("Could not create pipes: %.100s",
384: strerror(errno));
385: #else /* USE_PIPES */
386: int inout[2], err[2];
387: /* Uses socket pairs to communicate with the program. */
388: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
389: socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
390: packet_disconnect("Could not create socket pairs: %.100s",
391: strerror(errno));
392: #endif /* USE_PIPES */
393: if (s == NULL)
394: fatal("do_exec_no_pty: no session");
395:
1.9 ! markus 396: session_proctitle(s);
1.1 markus 397:
398: /* Fork the child. */
399: if ((pid = fork()) == 0) {
400: /* Child. Reinitialize the log since the pid has changed. */
401: log_init(__progname, options.log_level, options.log_facility, log_stderr);
402:
403: /*
404: * Create a new session and process group since the 4.4BSD
405: * setlogin() affects the entire process group.
406: */
407: if (setsid() < 0)
408: error("setsid failed: %.100s", strerror(errno));
409:
410: #ifdef USE_PIPES
411: /*
412: * Redirect stdin. We close the parent side of the socket
413: * pair, and make the child side the standard input.
414: */
415: close(pin[1]);
416: if (dup2(pin[0], 0) < 0)
417: perror("dup2 stdin");
418: close(pin[0]);
419:
420: /* Redirect stdout. */
421: close(pout[0]);
422: if (dup2(pout[1], 1) < 0)
423: perror("dup2 stdout");
424: close(pout[1]);
425:
426: /* Redirect stderr. */
427: close(perr[0]);
428: if (dup2(perr[1], 2) < 0)
429: perror("dup2 stderr");
430: close(perr[1]);
431: #else /* USE_PIPES */
432: /*
433: * Redirect stdin, stdout, and stderr. Stdin and stdout will
434: * use the same socket, as some programs (particularly rdist)
435: * seem to depend on it.
436: */
437: close(inout[1]);
438: close(err[1]);
439: if (dup2(inout[0], 0) < 0) /* stdin */
440: perror("dup2 stdin");
441: if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
442: perror("dup2 stdout");
443: if (dup2(err[0], 2) < 0) /* stderr */
444: perror("dup2 stderr");
445: #endif /* USE_PIPES */
446:
447: /* Do processing for the child (exec command etc). */
448: do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
449: /* NOTREACHED */
450: }
451: if (pid < 0)
452: packet_disconnect("fork failed: %.100s", strerror(errno));
453: s->pid = pid;
454: #ifdef USE_PIPES
455: /* We are the parent. Close the child sides of the pipes. */
456: close(pin[0]);
457: close(pout[1]);
458: close(perr[1]);
459:
1.2 markus 460: if (compat20) {
1.7 markus 461: session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
1.2 markus 462: } else {
463: /* Enter the interactive session. */
464: server_loop(pid, pin[1], pout[0], perr[0]);
465: /* server_loop has closed pin[1], pout[1], and perr[1]. */
466: }
1.1 markus 467: #else /* USE_PIPES */
468: /* We are the parent. Close the child sides of the socket pairs. */
469: close(inout[0]);
470: close(err[0]);
471:
472: /*
473: * Enter the interactive session. Note: server_loop must be able to
474: * handle the case that fdin and fdout are the same.
475: */
1.2 markus 476: if (compat20) {
1.7 markus 477: session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
1.2 markus 478: } else {
479: server_loop(pid, inout[1], inout[1], err[1]);
480: /* server_loop has closed inout[1] and err[1]. */
481: }
1.1 markus 482: #endif /* USE_PIPES */
483: }
484:
485: /*
486: * This is called to fork and execute a command when we have a tty. This
487: * will call do_child from the child, and server_loop from the parent after
488: * setting up file descriptors, controlling tty, updating wtmp, utmp,
489: * lastlog, and other such operations.
490: */
1.4 markus 491: void
1.1 markus 492: do_exec_pty(Session *s, const char *command, struct passwd * pw)
493: {
494: FILE *f;
495: char buf[100], *time_string;
496: char line[256];
497: const char *hostname;
498: int fdout, ptyfd, ttyfd, ptymaster;
499: int quiet_login;
500: pid_t pid;
501: socklen_t fromlen;
502: struct sockaddr_storage from;
503: struct stat st;
504: time_t last_login_time;
505:
506: if (s == NULL)
507: fatal("do_exec_pty: no session");
508: ptyfd = s->ptyfd;
509: ttyfd = s->ttyfd;
510:
511: /* Get remote host name. */
512: hostname = get_canonical_hostname();
513:
514: /*
515: * Get the time when the user last logged in. Buf will be set to
516: * contain the hostname the last login was from.
517: */
518: if (!options.use_login) {
519: last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
520: buf, sizeof(buf));
521: }
522:
523: /* Fork the child. */
524: if ((pid = fork()) == 0) {
525: pid = getpid();
526:
527: /* Child. Reinitialize the log because the pid has
528: changed. */
529: log_init(__progname, options.log_level, options.log_facility, log_stderr);
530:
531: /* Close the master side of the pseudo tty. */
532: close(ptyfd);
533:
534: /* Make the pseudo tty our controlling tty. */
535: pty_make_controlling_tty(&ttyfd, s->tty);
536:
537: /* Redirect stdin from the pseudo tty. */
538: if (dup2(ttyfd, fileno(stdin)) < 0)
539: error("dup2 stdin failed: %.100s", strerror(errno));
540:
541: /* Redirect stdout to the pseudo tty. */
542: if (dup2(ttyfd, fileno(stdout)) < 0)
543: error("dup2 stdin failed: %.100s", strerror(errno));
544:
545: /* Redirect stderr to the pseudo tty. */
546: if (dup2(ttyfd, fileno(stderr)) < 0)
547: error("dup2 stdin failed: %.100s", strerror(errno));
548:
549: /* Close the extra descriptor for the pseudo tty. */
550: close(ttyfd);
551:
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.9 ! markus 1187: session_proctitle(s);
! 1188:
1.3 markus 1189: /* XXX parse and set terminal modes */
1190: xfree(term_modes);
1.2 markus 1191: return 1;
1192: }
1193:
1.7 markus 1194: int
1195: session_subsystem_req(Session *s)
1196: {
1197: unsigned int len;
1198: int success = 0;
1199: char *subsys = packet_get_string(&len);
1200:
1201: packet_done();
1202: log("subsystem request for %s", subsys);
1203:
1204: xfree(subsys);
1205: return success;
1206: }
1207:
1208: int
1209: session_x11_req(Session *s)
1210: {
1211: if (!options.x11_forwarding) {
1212: debug("X11 forwarding disabled in server configuration file.");
1213: return 0;
1214: }
1215: if (xauthfile != NULL) {
1216: debug("X11 fwd already started.");
1217: return 0;
1218: }
1219:
1220: debug("Received request for X11 forwarding with auth spoofing.");
1221: if (s->display != NULL)
1222: packet_disconnect("Protocol error: X11 display already set.");
1223:
1224: s->single_connection = packet_get_char();
1225: s->auth_proto = packet_get_string(NULL);
1226: s->auth_data = packet_get_string(NULL);
1227: s->screen = packet_get_int();
1228: packet_done();
1229:
1230: s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1231: if (s->display == NULL) {
1232: xfree(s->auth_proto);
1233: xfree(s->auth_data);
1234: return 0;
1235: }
1236: xauthfile = xmalloc(MAXPATHLEN);
1237: strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1238: temporarily_use_uid(s->pw->pw_uid);
1239: if (mkdtemp(xauthfile) == NULL) {
1240: restore_uid();
1241: error("private X11 dir: mkdtemp %s failed: %s",
1242: xauthfile, strerror(errno));
1243: xfree(xauthfile);
1244: xauthfile = NULL;
1245: xfree(s->auth_proto);
1246: xfree(s->auth_data);
1247: /* XXXX remove listening channels */
1248: return 0;
1249: }
1250: strlcat(xauthfile, "/cookies", MAXPATHLEN);
1251: open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1252: restore_uid();
1253: fatal_add_cleanup(xauthfile_cleanup_proc, s);
1254: return 1;
1255: }
1256:
1.2 markus 1257: void
1258: session_input_channel_req(int id, void *arg)
1259: {
1260: unsigned int len;
1261: int reply;
1262: int success = 0;
1263: char *rtype;
1264: Session *s;
1265: Channel *c;
1266:
1267: rtype = packet_get_string(&len);
1268: reply = packet_get_char();
1269:
1270: s = session_by_channel(id);
1271: if (s == NULL)
1272: fatal("session_input_channel_req: channel %d: no session", id);
1273: c = channel_lookup(id);
1274: if (c == NULL)
1275: fatal("session_input_channel_req: channel %d: bad channel", id);
1276:
1277: debug("session_input_channel_req: session %d channel %d request %s reply %d",
1278: s->self, id, rtype, reply);
1279:
1280: /*
1281: * a session is in LARVAL state until a shell
1282: * or programm is executed
1283: */
1284: if (c->type == SSH_CHANNEL_LARVAL) {
1285: if (strcmp(rtype, "shell") == 0) {
1.3 markus 1286: packet_done();
1.7 markus 1287: s->extended = 1;
1.2 markus 1288: if (s->ttyfd == -1)
1289: do_exec_no_pty(s, NULL, s->pw);
1290: else
1291: do_exec_pty(s, NULL, s->pw);
1292: success = 1;
1293: } else if (strcmp(rtype, "exec") == 0) {
1294: char *command = packet_get_string(&len);
1.3 markus 1295: packet_done();
1.7 markus 1296: s->extended = 1;
1.2 markus 1297: if (s->ttyfd == -1)
1298: do_exec_no_pty(s, command, s->pw);
1299: else
1300: do_exec_pty(s, command, s->pw);
1301: xfree(command);
1302: success = 1;
1303: } else if (strcmp(rtype, "pty-req") == 0) {
1.3 markus 1304: success = session_pty_req(s);
1.7 markus 1305: } else if (strcmp(rtype, "x11-req") == 0) {
1306: success = session_x11_req(s);
1307: } else if (strcmp(rtype, "subsystem") == 0) {
1308: success = session_subsystem_req(s);
1.2 markus 1309: }
1310: }
1311: if (strcmp(rtype, "window-change") == 0) {
1312: success = session_window_change_req(s);
1313: }
1314:
1315: if (reply) {
1316: packet_start(success ?
1317: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1318: packet_put_int(c->remote_id);
1319: packet_send();
1320: }
1321: xfree(rtype);
1322: }
1323:
1324: void
1325: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1326: {
1327: if (!compat20)
1328: fatal("session_set_fds: called for proto != 2.0");
1329: /*
1330: * now that have a child and a pipe to the child,
1331: * we can activate our channel and register the fd's
1332: */
1333: if (s->chanid == -1)
1334: fatal("no channel for session %d", s->self);
1335: channel_set_fds(s->chanid,
1336: fdout, fdin, fderr,
1337: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ);
1338: }
1339:
1.1 markus 1340: void
1341: session_pty_cleanup(Session *s)
1342: {
1343: if (s == NULL || s->ttyfd == -1)
1344: return;
1345:
1346: debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1347:
1348: /* Cancel the cleanup function. */
1349: fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1350:
1351: /* Record that the user has logged out. */
1352: record_logout(s->pid, s->tty);
1353:
1354: /* Release the pseudo-tty. */
1355: pty_release(s->tty);
1356:
1357: /*
1358: * Close the server side of the socket pairs. We must do this after
1359: * the pty cleanup, so that another process doesn't get this pty
1360: * while we're still cleaning up.
1361: */
1362: if (close(s->ptymaster) < 0)
1363: error("close(s->ptymaster): %s", strerror(errno));
1.2 markus 1364: }
1365:
1366: void
1367: session_exit_message(Session *s, int status)
1368: {
1369: Channel *c;
1370: if (s == NULL)
1371: fatal("session_close: no session");
1372: c = channel_lookup(s->chanid);
1373: if (c == NULL)
1374: fatal("session_close: session %d: no channel %d",
1375: s->self, s->chanid);
1376: debug("session_exit_message: session %d channel %d pid %d",
1377: s->self, s->chanid, s->pid);
1378:
1379: if (WIFEXITED(status)) {
1380: channel_request_start(s->chanid,
1381: "exit-status", 0);
1382: packet_put_int(WEXITSTATUS(status));
1383: packet_send();
1384: } else if (WIFSIGNALED(status)) {
1385: channel_request_start(s->chanid,
1386: "exit-signal", 0);
1387: packet_put_int(WTERMSIG(status));
1388: packet_put_char(WCOREDUMP(status));
1389: packet_put_cstring("");
1390: packet_put_cstring("");
1391: packet_send();
1392: } else {
1393: /* Some weird exit cause. Just exit. */
1394: packet_disconnect("wait returned status %04x.", status);
1395: }
1396:
1397: /* disconnect channel */
1398: debug("session_exit_message: release channel %d", s->chanid);
1399: channel_cancel_cleanup(s->chanid);
1.5 markus 1400: /*
1401: * emulate a write failure with 'chan_write_failed', nobody will be
1402: * interested in data we write.
1403: * Note that we must not call 'chan_read_failed', since there could
1404: * be some more data waiting in the pipe.
1405: */
1.8 markus 1406: if (c->ostate != CHAN_OUTPUT_CLOSED)
1407: chan_write_failed(c);
1.2 markus 1408: s->chanid = -1;
1409: }
1410:
1411: void
1412: session_free(Session *s)
1413: {
1414: debug("session_free: session %d pid %d", s->self, s->pid);
1415: if (s->term)
1416: xfree(s->term);
1417: if (s->display)
1418: xfree(s->display);
1419: if (s->auth_data)
1420: xfree(s->auth_data);
1421: if (s->auth_proto)
1422: xfree(s->auth_proto);
1423: s->used = 0;
1424: }
1425:
1426: void
1427: session_close(Session *s)
1428: {
1429: session_pty_cleanup(s);
1430: session_free(s);
1.9 ! markus 1431: session_proctitle(s);
1.2 markus 1432: }
1433:
1434: void
1435: session_close_by_pid(pid_t pid, int status)
1436: {
1437: Session *s = session_by_pid(pid);
1438: if (s == NULL) {
1439: debug("session_close_by_pid: no session for pid %d", s->pid);
1440: return;
1441: }
1442: if (s->chanid != -1)
1443: session_exit_message(s, status);
1444: session_close(s);
1445: }
1446:
1447: /*
1448: * this is called when a channel dies before
1449: * the session 'child' itself dies
1450: */
1451: void
1452: session_close_by_channel(int id, void *arg)
1453: {
1454: Session *s = session_by_channel(id);
1455: if (s == NULL) {
1456: debug("session_close_by_channel: no session for channel %d", id);
1457: return;
1458: }
1459: /* disconnect channel */
1460: channel_cancel_cleanup(s->chanid);
1461: s->chanid = -1;
1462:
1463: debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1464: if (s->pid == 0) {
1465: /* close session immediately */
1466: session_close(s);
1467: } else {
1468: /* notify child, delay session cleanup */
1469: if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
1470: error("session_close_by_channel: kill %d: %s",
1471: s->pid, strerror(errno));
1472: }
1.9 ! markus 1473: }
! 1474:
! 1475: char *
! 1476: session_tty_list(void)
! 1477: {
! 1478: static char buf[1024];
! 1479: int i;
! 1480: buf[0] = '\0';
! 1481: for(i = 0; i < MAX_SESSIONS; i++) {
! 1482: Session *s = &sessions[i];
! 1483: if (s->used && s->ttyfd != -1) {
! 1484: if (buf[0] != '\0')
! 1485: strlcat(buf, ",", sizeof buf);
! 1486: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
! 1487: }
! 1488: }
! 1489: if (buf[0] == '\0')
! 1490: strlcpy(buf, "notty", sizeof buf);
! 1491: return buf;
! 1492: }
! 1493:
! 1494: void
! 1495: session_proctitle(Session *s)
! 1496: {
! 1497: if (s->pw == NULL)
! 1498: error("no user for session %d", s->self);
! 1499: else
! 1500: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.2 markus 1501: }
1502:
1503: void
1504: do_authenticated2(void)
1505: {
1506: /*
1507: * Cancel the alarm we set to limit the time taken for
1508: * authentication.
1509: */
1510: alarm(0);
1511: server_loop2();
1.7 markus 1512: if (xauthfile)
1513: xauthfile_cleanup_proc(NULL);
1.1 markus 1514: }