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