Annotation of src/usr.bin/ssh/session.c, Revision 1.36
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.36 ! markus 11: RCSID("$OpenBSD: session.c,v 1.35 2000/09/04 19:07:21 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. */
1.36 ! markus 628: hostname[0] = '\0';
1.35 markus 629: last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
630: hostname, sizeof(hostname));
631:
1.24 markus 632: /* Record that there was a login on that tty from the remote host. */
633: record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
1.25 markus 634: get_remote_name_or_ip(), (struct sockaddr *)&from);
1.24 markus 635:
636: /* Done if .hushlogin exists. */
637: snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
1.28 millert 638: #ifdef HAVE_LOGIN_CAP
639: if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
640: #else
1.24 markus 641: if (stat(buf, &st) >= 0)
1.28 millert 642: #endif
1.24 markus 643: return;
644: if (last_login_time != 0) {
645: time_string = ctime(&last_login_time);
646: if (strchr(time_string, '\n'))
647: *strchr(time_string, '\n') = 0;
648: if (strcmp(buf, "") == 0)
649: printf("Last login: %s\r\n", time_string);
650: else
1.36 ! markus 651: printf("Last login: %s from %s\r\n", time_string, hostname);
1.24 markus 652: }
653: if (options.print_motd) {
1.28 millert 654: #ifdef HAVE_LOGIN_CAP
655: f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
656: "/etc/motd"), "r");
657: #else
1.24 markus 658: f = fopen("/etc/motd", "r");
1.28 millert 659: #endif
1.24 markus 660: if (f) {
661: while (fgets(buf, sizeof(buf), f))
662: fputs(buf, stdout);
663: fclose(f);
664: }
1.2 markus 665: }
1.1 markus 666: }
667:
668: /*
669: * Sets the value of the given variable in the environment. If the variable
670: * already exists, its value is overriden.
671: */
1.4 markus 672: void
1.1 markus 673: child_set_env(char ***envp, unsigned int *envsizep, const char *name,
674: const char *value)
675: {
676: unsigned int i, namelen;
677: char **env;
678:
679: /*
680: * Find the slot where the value should be stored. If the variable
681: * already exists, we reuse the slot; otherwise we append a new slot
682: * at the end of the array, expanding if necessary.
683: */
684: env = *envp;
685: namelen = strlen(name);
686: for (i = 0; env[i]; i++)
687: if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
688: break;
689: if (env[i]) {
690: /* Reuse the slot. */
691: xfree(env[i]);
692: } else {
693: /* New variable. Expand if necessary. */
694: if (i >= (*envsizep) - 1) {
695: (*envsizep) += 50;
696: env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
697: }
698: /* Need to set the NULL pointer at end of array beyond the new slot. */
699: env[i + 1] = NULL;
700: }
701:
702: /* Allocate space and format the variable in the appropriate slot. */
703: env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
704: snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
705: }
706:
707: /*
708: * Reads environment variables from the given file and adds/overrides them
709: * into the environment. If the file does not exist, this does nothing.
710: * Otherwise, it must consist of empty lines, comments (line starts with '#')
711: * and assignments of the form name=value. No other forms are allowed.
712: */
1.4 markus 713: void
1.1 markus 714: read_environment_file(char ***env, unsigned int *envsize,
715: const char *filename)
716: {
717: FILE *f;
718: char buf[4096];
719: char *cp, *value;
720:
721: f = fopen(filename, "r");
722: if (!f)
723: return;
724:
725: while (fgets(buf, sizeof(buf), f)) {
726: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
727: ;
728: if (!*cp || *cp == '#' || *cp == '\n')
729: continue;
730: if (strchr(cp, '\n'))
731: *strchr(cp, '\n') = '\0';
732: value = strchr(cp, '=');
733: if (value == NULL) {
734: fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
735: continue;
736: }
1.14 deraadt 737: /*
738: * Replace the equals sign by nul, and advance value to
739: * the value string.
740: */
1.1 markus 741: *value = '\0';
742: value++;
743: child_set_env(env, envsize, cp, value);
744: }
745: fclose(f);
746: }
747:
748: /*
749: * Performs common processing for the child, such as setting up the
750: * environment, closing extra file descriptors, setting the user and group
751: * ids, and executing the command or shell.
752: */
1.4 markus 753: void
1.1 markus 754: do_child(const char *command, struct passwd * pw, const char *term,
755: const char *display, const char *auth_proto,
756: const char *auth_data, const char *ttyname)
757: {
1.32 markus 758: const char *shell, *hostname = NULL, *cp = NULL;
1.1 markus 759: char buf[256];
1.16 markus 760: char cmd[1024];
1.27 millert 761: FILE *f = NULL;
1.1 markus 762: unsigned int envsize, i;
763: char **env;
764: extern char **environ;
765: struct stat st;
766: char *argv[10];
1.17 markus 767:
768: /* login(1) is only called if we execute the login shell */
769: if (options.use_login && command != NULL)
770: options.use_login = 0;
1.1 markus 771:
1.27 millert 772: if (!options.use_login) {
1.28 millert 773: #ifdef HAVE_LOGIN_CAP
774: if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
775: f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
776: _PATH_NOLOGIN), "r");
777: #else
1.27 millert 778: if (pw->pw_uid)
779: f = fopen(_PATH_NOLOGIN, "r");
1.28 millert 780: #endif
1.27 millert 781: if (f) {
782: /* /etc/nologin exists. Print its contents and exit. */
783: while (fgets(buf, sizeof(buf), f))
784: fputs(buf, stderr);
785: fclose(f);
1.1 markus 786: exit(254);
1.27 millert 787: }
1.1 markus 788: }
1.28 millert 789: /* Set login name, uid, gid, and groups. */
1.1 markus 790: /* Login(1) does this as well, and it needs uid 0 for the "-h"
791: switch, so we let login(1) to this for us. */
792: if (!options.use_login) {
793: if (getuid() == 0 || geteuid() == 0) {
1.28 millert 794: #ifdef HAVE_LOGIN_CAP
795: if (setusercontext(lc, pw, pw->pw_uid,
796: (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
797: perror("unable to set user context");
798: exit(1);
799:
800: }
801: #else
802: if (setlogin(pw->pw_name) < 0)
803: error("setlogin failed: %s", strerror(errno));
1.1 markus 804: if (setgid(pw->pw_gid) < 0) {
805: perror("setgid");
806: exit(1);
807: }
808: /* Initialize the group list. */
809: if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
810: perror("initgroups");
811: exit(1);
812: }
813: endgrent();
814:
815: /* Permanently switch to the desired uid. */
816: permanently_set_uid(pw->pw_uid);
1.28 millert 817: #endif
1.1 markus 818: }
819: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1.31 deraadt 820: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1.1 markus 821: }
822: /*
823: * Get the shell from the password data. An empty shell field is
824: * legal, and means /bin/sh.
825: */
826: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.28 millert 827: #ifdef HAVE_LOGIN_CAP
828: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
829: #endif
1.1 markus 830:
831: #ifdef AFS
832: /* Try to get AFS tokens for the local cell. */
833: if (k_hasafs()) {
834: char cell[64];
835:
836: if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
837: krb_afslog(cell, 0);
838:
839: krb_afslog(0, 0);
840: }
841: #endif /* AFS */
842:
843: /* Initialize the environment. */
844: envsize = 100;
845: env = xmalloc(envsize * sizeof(char *));
846: env[0] = NULL;
847:
848: if (!options.use_login) {
849: /* Set basic environment. */
850: child_set_env(&env, &envsize, "USER", pw->pw_name);
851: child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
852: child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.28 millert 853: #ifdef HAVE_LOGIN_CAP
1.29 millert 854: (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
855: child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.28 millert 856: #else
1.29 millert 857: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1.28 millert 858: #endif
1.1 markus 859:
860: snprintf(buf, sizeof buf, "%.200s/%.50s",
861: _PATH_MAILDIR, pw->pw_name);
862: child_set_env(&env, &envsize, "MAIL", buf);
863:
864: /* Normal systems set SHELL by default. */
865: child_set_env(&env, &envsize, "SHELL", shell);
866: }
867: if (getenv("TZ"))
868: child_set_env(&env, &envsize, "TZ", getenv("TZ"));
869:
870: /* Set custom environment options from RSA authentication. */
871: while (custom_environment) {
872: struct envstring *ce = custom_environment;
873: char *s = ce->s;
874: int i;
875: for (i = 0; s[i] != '=' && s[i]; i++);
876: if (s[i] == '=') {
877: s[i] = 0;
878: child_set_env(&env, &envsize, s, s + i + 1);
879: }
880: custom_environment = ce->next;
881: xfree(ce->s);
882: xfree(ce);
883: }
884:
885: snprintf(buf, sizeof buf, "%.50s %d %d",
886: get_remote_ipaddr(), get_remote_port(), get_local_port());
887: child_set_env(&env, &envsize, "SSH_CLIENT", buf);
888:
889: if (ttyname)
890: child_set_env(&env, &envsize, "SSH_TTY", ttyname);
891: if (term)
892: child_set_env(&env, &envsize, "TERM", term);
893: if (display)
894: child_set_env(&env, &envsize, "DISPLAY", display);
1.34 markus 895: if (original_command)
896: child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
897: original_command);
1.1 markus 898:
899: #ifdef KRB4
900: {
901: extern char *ticket;
902:
903: if (ticket)
904: child_set_env(&env, &envsize, "KRBTKFILE", ticket);
905: }
906: #endif /* KRB4 */
907:
908: if (xauthfile)
909: child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
910: if (auth_get_socket_name() != NULL)
911: child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
912: auth_get_socket_name());
913:
914: /* read $HOME/.ssh/environment. */
915: if (!options.use_login) {
1.14 deraadt 916: snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
917: pw->pw_dir);
1.1 markus 918: read_environment_file(&env, &envsize, buf);
919: }
920: if (debug_flag) {
921: /* dump the environment */
922: fprintf(stderr, "Environment:\n");
923: for (i = 0; env[i]; i++)
924: fprintf(stderr, " %.200s\n", env[i]);
925: }
1.26 millert 926: /* we have to stash the hostname before we close our socket. */
927: if (options.use_login)
928: hostname = get_remote_name_or_ip();
1.1 markus 929: /*
930: * Close the connection descriptors; note that this is the child, and
931: * the server will still have the socket open, and it is important
932: * that we do not shutdown it. Note that the descriptors cannot be
933: * closed before building the environment, as we call
934: * get_remote_ipaddr there.
935: */
936: if (packet_get_connection_in() == packet_get_connection_out())
937: close(packet_get_connection_in());
938: else {
939: close(packet_get_connection_in());
940: close(packet_get_connection_out());
941: }
942: /*
943: * Close all descriptors related to channels. They will still remain
944: * open in the parent.
945: */
946: /* XXX better use close-on-exec? -markus */
947: channel_close_all();
948:
949: /*
950: * Close any extra file descriptors. Note that there may still be
951: * descriptors left by system functions. They will be closed later.
952: */
953: endpwent();
954:
955: /*
956: * Close any extra open file descriptors so that we don\'t have them
957: * hanging around in clients. Note that we want to do this after
958: * initgroups, because at least on Solaris 2.3 it leaves file
959: * descriptors open.
960: */
961: for (i = 3; i < 64; i++)
962: close(i);
963:
964: /* Change current directory to the user\'s home directory. */
1.28 millert 965: if (chdir(pw->pw_dir) < 0) {
1.1 markus 966: fprintf(stderr, "Could not chdir to home directory %s: %s\n",
967: pw->pw_dir, strerror(errno));
1.28 millert 968: #ifdef HAVE_LOGIN_CAP
969: if (login_getcapbool(lc, "requirehome", 0))
970: exit(1);
971: #endif
972: }
1.1 markus 973:
974: /*
975: * Must take new environment into use so that .ssh/rc, /etc/sshrc and
976: * xauth are run in the proper environment.
977: */
978: environ = env;
979:
980: /*
981: * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
982: * in this order).
983: */
984: if (!options.use_login) {
985: if (stat(SSH_USER_RC, &st) >= 0) {
986: if (debug_flag)
987: fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC);
988:
989: f = popen("/bin/sh " SSH_USER_RC, "w");
990: if (f) {
991: if (auth_proto != NULL && auth_data != NULL)
992: fprintf(f, "%s %s\n", auth_proto, auth_data);
993: pclose(f);
994: } else
995: fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
996: } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
997: if (debug_flag)
998: fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC);
999:
1000: f = popen("/bin/sh " SSH_SYSTEM_RC, "w");
1001: if (f) {
1002: if (auth_proto != NULL && auth_data != NULL)
1003: fprintf(f, "%s %s\n", auth_proto, auth_data);
1004: pclose(f);
1005: } else
1006: fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
1.16 markus 1007: } else if (options.xauth_location != NULL) {
1.1 markus 1008: /* Add authority data to .Xauthority if appropriate. */
1009: if (auth_proto != NULL && auth_data != NULL) {
1.13 markus 1010: char *screen = strchr(display, ':');
1011: if (debug_flag) {
1.14 deraadt 1012: fprintf(stderr,
1013: "Running %.100s add %.100s %.100s %.100s\n",
1.16 markus 1014: options.xauth_location, display,
1015: auth_proto, auth_data);
1.13 markus 1016: if (screen != NULL)
1.14 deraadt 1017: fprintf(stderr,
1018: "Adding %.*s/unix%s %s %s\n",
1.30 deraadt 1019: (int)(screen-display), display,
1.14 deraadt 1020: screen, auth_proto, auth_data);
1.13 markus 1021: }
1.16 markus 1022: snprintf(cmd, sizeof cmd, "%s -q -",
1023: options.xauth_location);
1024: f = popen(cmd, "w");
1.1 markus 1025: if (f) {
1.14 deraadt 1026: fprintf(f, "add %s %s %s\n", display,
1027: auth_proto, auth_data);
1.13 markus 1028: if (screen != NULL)
1029: fprintf(f, "add %.*s/unix%s %s %s\n",
1.30 deraadt 1030: (int)(screen-display), display,
1.14 deraadt 1031: screen, auth_proto, auth_data);
1.1 markus 1032: pclose(f);
1.16 markus 1033: } else {
1034: fprintf(stderr, "Could not run %s\n",
1035: cmd);
1036: }
1.1 markus 1037: }
1038: }
1039: /* Get the last component of the shell name. */
1040: cp = strrchr(shell, '/');
1041: if (cp)
1042: cp++;
1043: else
1044: cp = shell;
1045: }
1046: /*
1047: * If we have no command, execute the shell. In this case, the shell
1048: * name to be passed in argv[0] is preceded by '-' to indicate that
1049: * this is a login shell.
1050: */
1051: if (!command) {
1052: if (!options.use_login) {
1053: char buf[256];
1054:
1055: /*
1056: * Check for mail if we have a tty and it was enabled
1057: * in server options.
1058: */
1059: if (ttyname && options.check_mail) {
1060: char *mailbox;
1061: struct stat mailstat;
1062: mailbox = getenv("MAIL");
1063: if (mailbox != NULL) {
1.14 deraadt 1064: if (stat(mailbox, &mailstat) != 0 ||
1065: mailstat.st_size == 0)
1.1 markus 1066: printf("No mail.\n");
1067: else if (mailstat.st_mtime < mailstat.st_atime)
1068: printf("You have mail.\n");
1069: else
1070: printf("You have new mail.\n");
1071: }
1072: }
1073: /* Start the shell. Set initial character to '-'. */
1074: buf[0] = '-';
1075: strncpy(buf + 1, cp, sizeof(buf) - 1);
1076: buf[sizeof(buf) - 1] = 0;
1077:
1078: /* Execute the shell. */
1079: argv[0] = buf;
1080: argv[1] = NULL;
1081: execve(shell, argv, env);
1082:
1083: /* Executing the shell failed. */
1084: perror(shell);
1085: exit(1);
1086:
1087: } else {
1088: /* Launch login(1). */
1089:
1.26 millert 1090: execl("/usr/bin/login", "login", "-h", hostname,
1.25 markus 1091: "-p", "-f", "--", pw->pw_name, NULL);
1.1 markus 1092:
1093: /* Login couldn't be executed, die. */
1094:
1095: perror("login");
1096: exit(1);
1097: }
1098: }
1099: /*
1100: * Execute the command using the user's shell. This uses the -c
1101: * option to execute the command.
1102: */
1103: argv[0] = (char *) cp;
1104: argv[1] = "-c";
1105: argv[2] = (char *) command;
1106: argv[3] = NULL;
1107: execve(shell, argv, env);
1108: perror(shell);
1109: exit(1);
1110: }
1111:
1112: Session *
1113: session_new(void)
1114: {
1115: int i;
1116: static int did_init = 0;
1117: if (!did_init) {
1118: debug("session_new: init");
1119: for(i = 0; i < MAX_SESSIONS; i++) {
1120: sessions[i].used = 0;
1121: sessions[i].self = i;
1122: }
1123: did_init = 1;
1124: }
1125: for(i = 0; i < MAX_SESSIONS; i++) {
1126: Session *s = &sessions[i];
1127: if (! s->used) {
1128: s->pid = 0;
1.7 markus 1129: s->extended = 0;
1.1 markus 1130: s->chanid = -1;
1131: s->ptyfd = -1;
1132: s->ttyfd = -1;
1133: s->term = NULL;
1134: s->pw = NULL;
1135: s->display = NULL;
1136: s->screen = 0;
1137: s->auth_data = NULL;
1138: s->auth_proto = NULL;
1139: s->used = 1;
1.7 markus 1140: s->pw = NULL;
1.1 markus 1141: debug("session_new: session %d", i);
1142: return s;
1143: }
1144: }
1145: return NULL;
1146: }
1147:
1148: void
1149: session_dump(void)
1150: {
1151: int i;
1152: for(i = 0; i < MAX_SESSIONS; i++) {
1153: Session *s = &sessions[i];
1154: debug("dump: used %d session %d %p channel %d pid %d",
1155: s->used,
1156: s->self,
1157: s,
1158: s->chanid,
1159: s->pid);
1160: }
1161: }
1162:
1.2 markus 1163: int
1164: session_open(int chanid)
1165: {
1166: Session *s = session_new();
1167: debug("session_open: channel %d", chanid);
1168: if (s == NULL) {
1169: error("no more sessions");
1170: return 0;
1171: }
1.7 markus 1172: s->pw = auth_get_user();
1173: if (s->pw == NULL)
1174: fatal("no user for session %i", s->self);
1.2 markus 1175: debug("session_open: session %d: link with channel %d", s->self, chanid);
1176: s->chanid = chanid;
1177: return 1;
1178: }
1179:
1180: Session *
1181: session_by_channel(int id)
1182: {
1183: int i;
1184: for(i = 0; i < MAX_SESSIONS; i++) {
1185: Session *s = &sessions[i];
1186: if (s->used && s->chanid == id) {
1187: debug("session_by_channel: session %d channel %d", i, id);
1188: return s;
1189: }
1190: }
1191: debug("session_by_channel: unknown channel %d", id);
1192: session_dump();
1193: return NULL;
1194: }
1195:
1196: Session *
1197: session_by_pid(pid_t pid)
1198: {
1199: int i;
1200: debug("session_by_pid: pid %d", pid);
1201: for(i = 0; i < MAX_SESSIONS; i++) {
1202: Session *s = &sessions[i];
1203: if (s->used && s->pid == pid)
1204: return s;
1205: }
1206: error("session_by_pid: unknown pid %d", pid);
1207: session_dump();
1208: return NULL;
1209: }
1210:
1211: int
1212: session_window_change_req(Session *s)
1213: {
1214: s->col = packet_get_int();
1215: s->row = packet_get_int();
1216: s->xpixel = packet_get_int();
1217: s->ypixel = packet_get_int();
1.3 markus 1218: packet_done();
1.2 markus 1219: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1220: return 1;
1221: }
1222:
1223: int
1224: session_pty_req(Session *s)
1225: {
1226: unsigned int len;
1.3 markus 1227: char *term_modes; /* encoded terminal modes */
1.2 markus 1228:
1.19 markus 1229: if (no_pty_flag)
1230: return 0;
1.2 markus 1231: if (s->ttyfd != -1)
1.3 markus 1232: return 0;
1.2 markus 1233: s->term = packet_get_string(&len);
1234: s->col = packet_get_int();
1235: s->row = packet_get_int();
1236: s->xpixel = packet_get_int();
1237: s->ypixel = packet_get_int();
1.3 markus 1238: term_modes = packet_get_string(&len);
1239: packet_done();
1.2 markus 1240:
1241: if (strcmp(s->term, "") == 0) {
1242: xfree(s->term);
1243: s->term = NULL;
1244: }
1245: /* Allocate a pty and open it. */
1246: if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1247: xfree(s->term);
1248: s->term = NULL;
1249: s->ptyfd = -1;
1250: s->ttyfd = -1;
1251: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1252: xfree(term_modes);
1253: return 0;
1.2 markus 1254: }
1255: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1256: /*
1257: * Add a cleanup function to clear the utmp entry and record logout
1258: * time in case we call fatal() (e.g., the connection gets closed).
1259: */
1260: fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1261: pty_setowner(s->pw, s->tty);
1262: /* Get window size from the packet. */
1263: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1264:
1.9 markus 1265: session_proctitle(s);
1266:
1.3 markus 1267: /* XXX parse and set terminal modes */
1268: xfree(term_modes);
1.2 markus 1269: return 1;
1270: }
1271:
1.7 markus 1272: int
1273: session_subsystem_req(Session *s)
1274: {
1275: unsigned int len;
1276: int success = 0;
1277: char *subsys = packet_get_string(&len);
1.18 jakob 1278: int i;
1.7 markus 1279:
1280: packet_done();
1281: log("subsystem request for %s", subsys);
1.18 jakob 1282:
1283: for (i = 0; i < options.num_subsystems; i++) {
1284: if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1285: debug("subsystem: exec() %s", options.subsystem_command[i]);
1286: do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1287: success = 1;
1288: }
1289: }
1290:
1291: if (!success)
1292: log("subsystem request for %s failed, subsystem not found", subsys);
1.7 markus 1293:
1294: xfree(subsys);
1295: return success;
1296: }
1297:
1298: int
1299: session_x11_req(Session *s)
1300: {
1.33 markus 1301: int fd;
1.22 deraadt 1302: if (no_x11_forwarding_flag) {
1.19 markus 1303: debug("X11 forwarding disabled in user configuration file.");
1304: return 0;
1305: }
1.7 markus 1306: if (!options.x11_forwarding) {
1307: debug("X11 forwarding disabled in server configuration file.");
1308: return 0;
1309: }
1310: if (xauthfile != NULL) {
1311: debug("X11 fwd already started.");
1312: return 0;
1313: }
1314:
1315: debug("Received request for X11 forwarding with auth spoofing.");
1316: if (s->display != NULL)
1317: packet_disconnect("Protocol error: X11 display already set.");
1318:
1319: s->single_connection = packet_get_char();
1320: s->auth_proto = packet_get_string(NULL);
1321: s->auth_data = packet_get_string(NULL);
1322: s->screen = packet_get_int();
1323: packet_done();
1324:
1325: s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1326: if (s->display == NULL) {
1327: xfree(s->auth_proto);
1328: xfree(s->auth_data);
1329: return 0;
1330: }
1331: xauthfile = xmalloc(MAXPATHLEN);
1332: strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1333: temporarily_use_uid(s->pw->pw_uid);
1334: if (mkdtemp(xauthfile) == NULL) {
1335: restore_uid();
1336: error("private X11 dir: mkdtemp %s failed: %s",
1337: xauthfile, strerror(errno));
1338: xfree(xauthfile);
1339: xauthfile = NULL;
1340: xfree(s->auth_proto);
1341: xfree(s->auth_data);
1342: /* XXXX remove listening channels */
1343: return 0;
1344: }
1345: strlcat(xauthfile, "/cookies", MAXPATHLEN);
1.33 markus 1346: fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1347: if (fd >= 0)
1348: close(fd);
1.7 markus 1349: restore_uid();
1350: fatal_add_cleanup(xauthfile_cleanup_proc, s);
1351: return 1;
1352: }
1353:
1.19 markus 1354: int
1355: session_shell_req(Session *s)
1356: {
1357: /* if forced_command == NULL, the shell is execed */
1358: char *shell = forced_command;
1359: packet_done();
1360: s->extended = 1;
1361: if (s->ttyfd == -1)
1362: do_exec_no_pty(s, shell, s->pw);
1363: else
1364: do_exec_pty(s, shell, s->pw);
1365: return 1;
1366: }
1367:
1368: int
1369: session_exec_req(Session *s)
1370: {
1.20 markus 1371: unsigned int len;
1.19 markus 1372: char *command = packet_get_string(&len);
1373: packet_done();
1374: if (forced_command) {
1.34 markus 1375: original_command = command;
1.19 markus 1376: command = forced_command;
1377: debug("Forced command '%.500s'", forced_command);
1378: }
1379: s->extended = 1;
1380: if (s->ttyfd == -1)
1381: do_exec_no_pty(s, command, s->pw);
1382: else
1383: do_exec_pty(s, command, s->pw);
1384: if (forced_command == NULL)
1385: xfree(command);
1386: return 1;
1387: }
1388:
1.2 markus 1389: void
1390: session_input_channel_req(int id, void *arg)
1391: {
1392: unsigned int len;
1393: int reply;
1394: int success = 0;
1395: char *rtype;
1396: Session *s;
1397: Channel *c;
1398:
1399: rtype = packet_get_string(&len);
1400: reply = packet_get_char();
1401:
1402: s = session_by_channel(id);
1403: if (s == NULL)
1404: fatal("session_input_channel_req: channel %d: no session", id);
1405: c = channel_lookup(id);
1406: if (c == NULL)
1407: fatal("session_input_channel_req: channel %d: bad channel", id);
1408:
1409: debug("session_input_channel_req: session %d channel %d request %s reply %d",
1410: s->self, id, rtype, reply);
1411:
1412: /*
1413: * a session is in LARVAL state until a shell
1414: * or programm is executed
1415: */
1416: if (c->type == SSH_CHANNEL_LARVAL) {
1417: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1418: success = session_shell_req(s);
1.2 markus 1419: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1420: success = session_exec_req(s);
1.2 markus 1421: } else if (strcmp(rtype, "pty-req") == 0) {
1.3 markus 1422: success = session_pty_req(s);
1.7 markus 1423: } else if (strcmp(rtype, "x11-req") == 0) {
1424: success = session_x11_req(s);
1425: } else if (strcmp(rtype, "subsystem") == 0) {
1426: success = session_subsystem_req(s);
1.2 markus 1427: }
1428: }
1429: if (strcmp(rtype, "window-change") == 0) {
1430: success = session_window_change_req(s);
1431: }
1432:
1433: if (reply) {
1434: packet_start(success ?
1435: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1436: packet_put_int(c->remote_id);
1437: packet_send();
1438: }
1439: xfree(rtype);
1440: }
1441:
1442: void
1443: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1444: {
1445: if (!compat20)
1446: fatal("session_set_fds: called for proto != 2.0");
1447: /*
1448: * now that have a child and a pipe to the child,
1449: * we can activate our channel and register the fd's
1450: */
1451: if (s->chanid == -1)
1452: fatal("no channel for session %d", s->self);
1453: channel_set_fds(s->chanid,
1454: fdout, fdin, fderr,
1455: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ);
1456: }
1457:
1.1 markus 1458: void
1459: session_pty_cleanup(Session *s)
1460: {
1461: if (s == NULL || s->ttyfd == -1)
1462: return;
1463:
1464: debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1465:
1466: /* Cancel the cleanup function. */
1467: fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1468:
1469: /* Record that the user has logged out. */
1470: record_logout(s->pid, s->tty);
1471:
1472: /* Release the pseudo-tty. */
1473: pty_release(s->tty);
1474:
1475: /*
1476: * Close the server side of the socket pairs. We must do this after
1477: * the pty cleanup, so that another process doesn't get this pty
1478: * while we're still cleaning up.
1479: */
1480: if (close(s->ptymaster) < 0)
1481: error("close(s->ptymaster): %s", strerror(errno));
1.2 markus 1482: }
1483:
1484: void
1485: session_exit_message(Session *s, int status)
1486: {
1487: Channel *c;
1488: if (s == NULL)
1489: fatal("session_close: no session");
1490: c = channel_lookup(s->chanid);
1491: if (c == NULL)
1492: fatal("session_close: session %d: no channel %d",
1493: s->self, s->chanid);
1494: debug("session_exit_message: session %d channel %d pid %d",
1495: s->self, s->chanid, s->pid);
1496:
1497: if (WIFEXITED(status)) {
1498: channel_request_start(s->chanid,
1499: "exit-status", 0);
1500: packet_put_int(WEXITSTATUS(status));
1501: packet_send();
1502: } else if (WIFSIGNALED(status)) {
1503: channel_request_start(s->chanid,
1504: "exit-signal", 0);
1505: packet_put_int(WTERMSIG(status));
1506: packet_put_char(WCOREDUMP(status));
1507: packet_put_cstring("");
1508: packet_put_cstring("");
1509: packet_send();
1510: } else {
1511: /* Some weird exit cause. Just exit. */
1512: packet_disconnect("wait returned status %04x.", status);
1513: }
1514:
1515: /* disconnect channel */
1516: debug("session_exit_message: release channel %d", s->chanid);
1517: channel_cancel_cleanup(s->chanid);
1.5 markus 1518: /*
1519: * emulate a write failure with 'chan_write_failed', nobody will be
1520: * interested in data we write.
1521: * Note that we must not call 'chan_read_failed', since there could
1522: * be some more data waiting in the pipe.
1523: */
1.8 markus 1524: if (c->ostate != CHAN_OUTPUT_CLOSED)
1525: chan_write_failed(c);
1.2 markus 1526: s->chanid = -1;
1527: }
1528:
1529: void
1530: session_free(Session *s)
1531: {
1532: debug("session_free: session %d pid %d", s->self, s->pid);
1533: if (s->term)
1534: xfree(s->term);
1535: if (s->display)
1536: xfree(s->display);
1537: if (s->auth_data)
1538: xfree(s->auth_data);
1539: if (s->auth_proto)
1540: xfree(s->auth_proto);
1541: s->used = 0;
1542: }
1543:
1544: void
1545: session_close(Session *s)
1546: {
1547: session_pty_cleanup(s);
1548: session_free(s);
1.9 markus 1549: session_proctitle(s);
1.2 markus 1550: }
1551:
1552: void
1553: session_close_by_pid(pid_t pid, int status)
1554: {
1555: Session *s = session_by_pid(pid);
1556: if (s == NULL) {
1557: debug("session_close_by_pid: no session for pid %d", s->pid);
1558: return;
1559: }
1560: if (s->chanid != -1)
1561: session_exit_message(s, status);
1562: session_close(s);
1563: }
1564:
1565: /*
1566: * this is called when a channel dies before
1567: * the session 'child' itself dies
1568: */
1569: void
1570: session_close_by_channel(int id, void *arg)
1571: {
1572: Session *s = session_by_channel(id);
1573: if (s == NULL) {
1574: debug("session_close_by_channel: no session for channel %d", id);
1575: return;
1576: }
1577: /* disconnect channel */
1578: channel_cancel_cleanup(s->chanid);
1579: s->chanid = -1;
1580:
1581: debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1582: if (s->pid == 0) {
1583: /* close session immediately */
1584: session_close(s);
1585: } else {
1586: /* notify child, delay session cleanup */
1587: if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
1588: error("session_close_by_channel: kill %d: %s",
1589: s->pid, strerror(errno));
1590: }
1.9 markus 1591: }
1592:
1593: char *
1594: session_tty_list(void)
1595: {
1596: static char buf[1024];
1597: int i;
1598: buf[0] = '\0';
1599: for(i = 0; i < MAX_SESSIONS; i++) {
1600: Session *s = &sessions[i];
1601: if (s->used && s->ttyfd != -1) {
1602: if (buf[0] != '\0')
1603: strlcat(buf, ",", sizeof buf);
1604: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1605: }
1606: }
1607: if (buf[0] == '\0')
1608: strlcpy(buf, "notty", sizeof buf);
1609: return buf;
1610: }
1611:
1612: void
1613: session_proctitle(Session *s)
1614: {
1615: if (s->pw == NULL)
1616: error("no user for session %d", s->self);
1617: else
1618: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.2 markus 1619: }
1620:
1621: void
1622: do_authenticated2(void)
1623: {
1.28 millert 1624: struct passwd *pw;
1625:
1.2 markus 1626: /*
1627: * Cancel the alarm we set to limit the time taken for
1628: * authentication.
1629: */
1630: alarm(0);
1.23 deraadt 1631: if (startup_pipe != -1) {
1632: close(startup_pipe);
1633: startup_pipe = -1;
1634: }
1.28 millert 1635: #ifdef HAVE_LOGIN_CAP
1636: pw = auth_get_user();
1637: if ((lc = login_getclass(pw->pw_class)) == NULL) {
1638: error("unable to get login class");
1639: return;
1640: }
1641: #endif
1.2 markus 1642: server_loop2();
1.7 markus 1643: if (xauthfile)
1644: xauthfile_cleanup_proc(NULL);
1.1 markus 1645: }