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