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