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