2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * SSH2 support by Markus Friedl.
7 * Copyright (c) 2000 Markus Friedl. All rights reserved.
11 RCSID("$OpenBSD: session.c,v 1.20 2000/06/18 04:42:54 markus Exp $");
29 #include "auth-options.h"
31 #ifdef WITH_IRIX_PROJECT
33 #endif /* WITH_IRIX_PROJECT */
38 typedef struct Session Session;
47 int ptyfd, ttyfd, ptymaster;
48 int row, col, xpixel, ypixel;
55 int single_connection;
62 Session *session_new(void);
63 void session_set_fds(Session *s, int fdin, int fdout, int fderr);
64 void session_pty_cleanup(Session *s);
65 void session_proctitle(Session *s);
66 void do_exec_pty(Session *s, const char *command, struct passwd * pw);
67 void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
70 do_child(const char *command, struct passwd * pw, const char *term,
71 const char *display, const char *auth_proto,
72 const char *auth_data, const char *ttyname);
75 extern ServerOptions options;
76 #ifdef HAVE___PROGNAME
77 extern char *__progname;
78 #else /* HAVE___PROGNAME */
79 static const char *__progname = "sshd";
80 #endif /* HAVE___PROGNAME */
82 extern int log_stderr;
83 extern int debug_flag;
85 /* Local Xauthority file. */
86 static char *xauthfile;
89 #define MAX_SESSIONS 10
90 Session sessions[MAX_SESSIONS];
91 #ifdef WITH_AIXAUTHENTICATE
92 /* AIX's lastlogin message, set in auth1.c */
94 #endif /* WITH_AIXAUTHENTICATE */
97 * Remove local Xauthority file.
100 xauthfile_cleanup_proc(void *ignore)
102 debug("xauthfile_cleanup_proc called");
104 if (xauthfile != NULL) {
107 p = strrchr(xauthfile, '/');
118 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
119 * dropped connection).
122 pty_cleanup_proc(void *session)
126 fatal("pty_cleanup_proc: no session");
127 debug("pty_cleanup_proc: %s", s->tty);
130 /* Record that the user has logged out. */
131 record_logout(s->pid, s->tty);
134 /* Release the pseudo-tty. */
139 * Prepares for an interactive session. This is called after the user has
140 * been successfully authenticated. During this message exchange, pseudo
141 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
142 * are requested, etc.
145 do_authenticated(struct passwd * pw)
149 int compression_level = 0, enable_compression_after_reply = 0;
154 unsigned int proto_len, data_len, dlen;
157 * Cancel the alarm we set to limit the time taken for
163 * Inform the channel mechanism that we are the server side and that
164 * the client may request to connect to any port at all. (The user
165 * could do it anyway, and we wouldn\'t know what is permitted except
166 * by the client telling us, so we can equally well trust the client
167 * not to request anything bogus.)
169 if (!no_port_forwarding_flag)
170 channel_permit_all_opens();
176 * We stay in this loop until the client requests to execute a shell
182 /* Get a packet from the client. */
183 type = packet_read(&plen);
185 /* Process the packet. */
187 case SSH_CMSG_REQUEST_COMPRESSION:
188 packet_integrity_check(plen, 4, type);
189 compression_level = packet_get_int();
190 if (compression_level < 1 || compression_level > 9) {
191 packet_send_debug("Received illegal compression level %d.",
195 /* Enable compression after we have responded with SUCCESS. */
196 enable_compression_after_reply = 1;
200 case SSH_CMSG_REQUEST_PTY:
202 debug("Allocating a pty not permitted for this authentication.");
206 packet_disconnect("Protocol error: you already have a pty.");
208 debug("Allocating pty.");
210 /* Allocate a pty and open it. */
211 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
213 error("Failed to allocate pty.");
216 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
217 pty_setowner(pw, s->tty);
219 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
220 s->term = packet_get_string(&dlen);
221 packet_integrity_check(dlen, strlen(s->term), type);
222 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
223 /* Remaining bytes */
224 n_bytes = plen - (4 + dlen + 4 * 4);
226 if (strcmp(s->term, "") == 0) {
230 /* Get window size from the packet. */
231 s->row = packet_get_int();
232 s->col = packet_get_int();
233 s->xpixel = packet_get_int();
234 s->ypixel = packet_get_int();
235 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
237 /* Get tty modes from the packet. */
238 tty_parse_modes(s->ttyfd, &n_bytes);
239 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
241 session_proctitle(s);
243 /* Indicate that we now have a pty. */
248 case SSH_CMSG_X11_REQUEST_FORWARDING:
249 if (!options.x11_forwarding) {
250 packet_send_debug("X11 forwarding disabled in server configuration file.");
253 if (!options.xauth_location) {
254 packet_send_debug("No xauth program; cannot forward with spoofing.");
257 if (no_x11_forwarding_flag) {
258 packet_send_debug("X11 forwarding not permitted for this authentication.");
261 debug("Received request for X11 forwarding with auth spoofing.");
262 if (s->display != NULL)
263 packet_disconnect("Protocol error: X11 display already set.");
265 s->auth_proto = packet_get_string(&proto_len);
266 s->auth_data = packet_get_string(&data_len);
267 packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
269 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
270 s->screen = packet_get_int();
273 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
275 if (s->display == NULL)
278 /* Setup to always have a local .Xauthority. */
279 xauthfile = xmalloc(MAXPATHLEN);
280 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
281 temporarily_use_uid(pw->pw_uid);
282 if (mkdtemp(xauthfile) == NULL) {
284 error("private X11 dir: mkdtemp %s failed: %s",
285 xauthfile, strerror(errno));
288 /* XXXX remove listening channels */
291 strlcat(xauthfile, "/cookies", MAXPATHLEN);
292 open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
294 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
298 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
299 if (no_agent_forwarding_flag || compat13) {
300 debug("Authentication agent forwarding not permitted for this authentication.");
303 debug("Received authentication agent forwarding request.");
304 success = auth_input_request_forwarding(pw);
307 case SSH_CMSG_PORT_FORWARD_REQUEST:
308 if (no_port_forwarding_flag) {
309 debug("Port forwarding not permitted for this authentication.");
312 debug("Received TCP/IP port forwarding request.");
313 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
317 case SSH_CMSG_MAX_PACKET_SIZE:
318 if (packet_set_maxsize(packet_get_int()) > 0)
322 case SSH_CMSG_EXEC_SHELL:
323 case SSH_CMSG_EXEC_CMD:
324 /* Set interactive/non-interactive mode. */
325 packet_set_interactive(have_pty || s->display != NULL,
328 if (type == SSH_CMSG_EXEC_CMD) {
329 command = packet_get_string(&dlen);
330 debug("Exec command '%.500s'", command);
331 packet_integrity_check(plen, 4 + dlen, type);
334 packet_integrity_check(plen, 0, type);
336 if (forced_command != NULL) {
337 command = forced_command;
338 debug("Forced command '%.500s'", forced_command);
341 do_exec_pty(s, command, pw);
343 do_exec_no_pty(s, command, pw);
347 /* Cleanup user's local Xauthority file. */
349 xauthfile_cleanup_proc(NULL);
354 * Any unknown messages in this phase are ignored,
355 * and a failure message is returned.
357 log("Unknown packet type received after authentication: %d", type);
359 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
363 /* Enable compression now that we have replied if appropriate. */
364 if (enable_compression_after_reply) {
365 enable_compression_after_reply = 0;
366 packet_start_compression(compression_level);
372 * This is called to fork and execute a command when we have no tty. This
373 * will call do_child from the child, and server_loop from the parent after
374 * setting up file descriptors and such.
377 do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
382 int pin[2], pout[2], perr[2];
383 /* Allocate pipes for communicating with the program. */
384 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
385 packet_disconnect("Could not create pipes: %.100s",
387 #else /* USE_PIPES */
388 int inout[2], err[2];
389 /* Uses socket pairs to communicate with the program. */
390 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
391 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
392 packet_disconnect("Could not create socket pairs: %.100s",
394 #endif /* USE_PIPES */
396 fatal("do_exec_no_pty: no session");
398 session_proctitle(s);
404 /* Fork the child. */
405 if ((pid = fork()) == 0) {
406 /* Child. Reinitialize the log since the pid has changed. */
407 log_init(__progname, options.log_level, options.log_facility, log_stderr);
410 * Create a new session and process group since the 4.4BSD
411 * setlogin() affects the entire process group.
414 error("setsid failed: %.100s", strerror(errno));
418 * Redirect stdin. We close the parent side of the socket
419 * pair, and make the child side the standard input.
422 if (dup2(pin[0], 0) < 0)
423 perror("dup2 stdin");
426 /* Redirect stdout. */
428 if (dup2(pout[1], 1) < 0)
429 perror("dup2 stdout");
432 /* Redirect stderr. */
434 if (dup2(perr[1], 2) < 0)
435 perror("dup2 stderr");
437 #else /* USE_PIPES */
439 * Redirect stdin, stdout, and stderr. Stdin and stdout will
440 * use the same socket, as some programs (particularly rdist)
441 * seem to depend on it.
445 if (dup2(inout[0], 0) < 0) /* stdin */
446 perror("dup2 stdin");
447 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
448 perror("dup2 stdout");
449 if (dup2(err[0], 2) < 0) /* stderr */
450 perror("dup2 stderr");
451 #endif /* USE_PIPES */
453 /* Do processing for the child (exec command etc). */
454 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
458 packet_disconnect("fork failed: %.100s", strerror(errno));
461 /* We are the parent. Close the child sides of the pipes. */
467 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
469 /* Enter the interactive session. */
470 server_loop(pid, pin[1], pout[0], perr[0]);
471 /* server_loop has closed pin[1], pout[1], and perr[1]. */
473 #else /* USE_PIPES */
474 /* We are the parent. Close the child sides of the socket pairs. */
479 * Enter the interactive session. Note: server_loop must be able to
480 * handle the case that fdin and fdout are the same.
483 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
485 server_loop(pid, inout[1], inout[1], err[1]);
486 /* server_loop has closed inout[1] and err[1]. */
488 #endif /* USE_PIPES */
492 * This is called to fork and execute a command when we have a tty. This
493 * will call do_child from the child, and server_loop from the parent after
494 * setting up file descriptors, controlling tty, updating wtmp, utmp,
495 * lastlog, and other such operations.
498 do_exec_pty(Session *s, const char *command, struct passwd * pw)
501 char buf[100], *time_string;
503 const char *hostname;
504 int fdout, ptyfd, ttyfd, ptymaster;
508 struct sockaddr_storage from;
510 time_t last_login_time;
513 fatal("do_exec_pty: no session");
517 /* Get remote host name. */
518 hostname = get_canonical_hostname();
521 * Get the time when the user last logged in. Buf will be set to
522 * contain the hostname the last login was from.
524 if (!options.use_login) {
525 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
530 do_pam_session(pw->pw_name, s->tty);
534 /* Fork the child. */
535 if ((pid = fork()) == 0) {
538 /* Child. Reinitialize the log because the pid has
540 log_init(__progname, options.log_level, options.log_facility, log_stderr);
542 /* Close the master side of the pseudo tty. */
545 /* Make the pseudo tty our controlling tty. */
546 pty_make_controlling_tty(&ttyfd, s->tty);
548 /* Redirect stdin from the pseudo tty. */
549 if (dup2(ttyfd, fileno(stdin)) < 0)
550 error("dup2 stdin failed: %.100s", strerror(errno));
552 /* Redirect stdout to the pseudo tty. */
553 if (dup2(ttyfd, fileno(stdout)) < 0)
554 error("dup2 stdin failed: %.100s", strerror(errno));
556 /* Redirect stderr to the pseudo tty. */
557 if (dup2(ttyfd, fileno(stderr)) < 0)
558 error("dup2 stdin failed: %.100s", strerror(errno));
560 /* Close the extra descriptor for the pseudo tty. */
563 /* XXXX ? move to do_child() ??*/
565 * Get IP address of client. This is needed because we want
566 * to record where the user logged in from. If the
567 * connection is not a socket, let the ip address be 0.0.0.0.
569 memset(&from, 0, sizeof(from));
570 if (packet_connection_is_on_socket()) {
571 fromlen = sizeof(from);
572 if (getpeername(packet_get_connection_in(),
573 (struct sockaddr *) & from, &fromlen) < 0) {
574 debug("getpeername: %.100s", strerror(errno));
578 /* Record that there was a login on that terminal. */
579 record_login(pid, s->tty, pw->pw_name, pw->pw_uid, hostname,
580 (struct sockaddr *)&from);
582 /* Check if .hushlogin exists. */
583 snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir);
584 quiet_login = stat(line, &st) >= 0;
588 print_pam_messages();
592 * If the user has logged in before, display the time of last
593 * login. However, don't display anything extra if a command
594 * has been specified (so that ssh can be used to execute
595 * commands on a remote machine without users knowing they
596 * are going to another machine). Login(1) will do this for
597 * us as well, so check if login(1) is used
599 if (command == NULL && last_login_time != 0 && !quiet_login &&
600 !options.use_login) {
601 /* Convert the date to a string. */
602 time_string = ctime(&last_login_time);
603 /* Remove the trailing newline. */
604 if (strchr(time_string, '\n'))
605 *strchr(time_string, '\n') = 0;
606 /* Display the last login time. Host if displayed
608 if (strcmp(buf, "") == 0)
609 printf("Last login: %s\r\n", time_string);
611 printf("Last login: %s from %s\r\n", time_string, buf);
614 * Print /etc/motd unless a command was specified or printing
615 * it was disabled in server options or login(1) will be
616 * used. Note that some machines appear to print it in
617 * /etc/profile or similar.
619 if (command == NULL && options.print_motd && !quiet_login &&
620 !options.use_login) {
621 /* Print /etc/motd if it exists. */
622 f = fopen("/etc/motd", "r");
624 while (fgets(line, sizeof(line), f))
629 #if defined(WITH_AIXAUTHENTICATE)
631 * AIX handles the lastlog info differently. Display it here.
633 if (command == NULL && aixloginmsg && *aixloginmsg &&
634 !quiet_login && !options.use_login) {
635 printf("%s\n", aixloginmsg);
638 /* Do common processing for the child, such as execing the command. */
639 do_child(command, pw, s->term, s->display, s->auth_proto,
640 s->auth_data, s->tty);
644 packet_disconnect("fork failed: %.100s", strerror(errno));
647 /* Parent. Close the slave side of the pseudo tty. */
651 * Create another descriptor of the pty master side for use as the
652 * standard input. We could use the original descriptor, but this
653 * simplifies code in server_loop. The descriptor is bidirectional.
657 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
659 /* we keep a reference to the pty master */
660 ptymaster = dup(ptyfd);
662 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
663 s->ptymaster = ptymaster;
665 /* Enter interactive session. */
667 session_set_fds(s, ptyfd, fdout, -1);
669 server_loop(pid, ptyfd, fdout, -1);
670 /* server_loop _has_ closed ptyfd and fdout. */
671 session_pty_cleanup(s);
676 * Sets the value of the given variable in the environment. If the variable
677 * already exists, its value is overriden.
680 child_set_env(char ***envp, unsigned int *envsizep, const char *name,
683 unsigned int i, namelen;
687 * Find the slot where the value should be stored. If the variable
688 * already exists, we reuse the slot; otherwise we append a new slot
689 * at the end of the array, expanding if necessary.
692 namelen = strlen(name);
693 for (i = 0; env[i]; i++)
694 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
697 /* Reuse the slot. */
700 /* New variable. Expand if necessary. */
701 if (i >= (*envsizep) - 1) {
703 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
705 /* Need to set the NULL pointer at end of array beyond the new slot. */
709 /* Allocate space and format the variable in the appropriate slot. */
710 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
711 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
715 * Reads environment variables from the given file and adds/overrides them
716 * into the environment. If the file does not exist, this does nothing.
717 * Otherwise, it must consist of empty lines, comments (line starts with '#')
718 * and assignments of the form name=value. No other forms are allowed.
721 read_environment_file(char ***env, unsigned int *envsize,
722 const char *filename)
728 f = fopen(filename, "r");
732 while (fgets(buf, sizeof(buf), f)) {
733 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
735 if (!*cp || *cp == '#' || *cp == '\n')
737 if (strchr(cp, '\n'))
738 *strchr(cp, '\n') = '\0';
739 value = strchr(cp, '=');
741 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
745 * Replace the equals sign by nul, and advance value to
750 child_set_env(env, envsize, cp, value);
757 * Sets any environment variables which have been specified by PAM
759 void do_pam_environment(char ***env, int *envsize)
761 char *equals, var_name[512], var_val[512];
765 if ((pam_env = fetch_pam_environment()) == NULL)
768 for(i = 0; pam_env[i] != NULL; i++) {
769 if ((equals = strstr(pam_env[i], "=")) == NULL)
772 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
773 memset(var_name, '\0', sizeof(var_name));
774 memset(var_val, '\0', sizeof(var_val));
776 strncpy(var_name, pam_env[i], equals - pam_env[i]);
777 strcpy(var_val, equals + 1);
779 debug("PAM environment: %s=%s", var_name, var_val);
781 child_set_env(env, envsize, var_name, var_val);
788 * Performs common processing for the child, such as setting up the
789 * environment, closing extra file descriptors, setting the user and group
790 * ids, and executing the command or shell.
793 do_child(const char *command, struct passwd * pw, const char *term,
794 const char *display, const char *auth_proto,
795 const char *auth_data, const char *ttyname)
797 const char *shell, *cp = NULL;
801 unsigned int envsize, i;
803 extern char **environ;
806 #ifdef WITH_IRIX_PROJECT
808 #endif /* WITH_IRIX_PROJECT */
810 /* login(1) is only called if we execute the login shell */
811 if (options.use_login && command != NULL)
812 options.use_login = 0;
814 #ifndef USE_PAM /* pam_nologin handles this */
815 f = fopen("/etc/nologin", "r");
817 /* /etc/nologin exists. Print its contents and exit. */
818 while (fgets(buf, sizeof(buf), f))
826 /* Set login name in the kernel. */
827 if (setlogin(pw->pw_name) < 0)
828 error("setlogin failed: %s", strerror(errno));
830 /* Set uid, gid, and groups. */
831 /* Login(1) does this as well, and it needs uid 0 for the "-h"
832 switch, so we let login(1) to this for us. */
833 if (!options.use_login) {
834 if (getuid() == 0 || geteuid() == 0) {
835 if (setgid(pw->pw_gid) < 0) {
839 /* Initialize the group list. */
840 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
841 perror("initgroups");
846 #ifdef WITH_IRIX_ARRAY
847 /* initialize array session */
848 if (newarraysess() != 0)
849 fatal("Failed to set up new array session: %.100s",
851 #endif /* WITH_IRIX_ARRAY */
853 #ifdef WITH_IRIX_PROJECT
854 /* initialize irix project info */
855 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
856 debug("Failed to get project id, using projid 0");
861 fatal("Failed to initialize project %d for %s: %.100s",
862 (int)projid, pw->pw_name, strerror(errno));
863 #endif /* WITH_IRIX_PROJECT */
865 /* Permanently switch to the desired uid. */
866 permanently_set_uid(pw->pw_uid);
868 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
869 fatal("Failed to set uids to %d.", (int) pw->pw_uid);
872 * Get the shell from the password data. An empty shell field is
873 * legal, and means /bin/sh.
875 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
878 /* Try to get AFS tokens for the local cell. */
882 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
889 /* Initialize the environment. */
891 env = xmalloc(envsize * sizeof(char *));
894 if (!options.use_login) {
895 /* Set basic environment. */
896 child_set_env(&env, &envsize, "USER", pw->pw_name);
897 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
898 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
899 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
901 snprintf(buf, sizeof buf, "%.200s/%.50s",
902 _PATH_MAILDIR, pw->pw_name);
903 child_set_env(&env, &envsize, "MAIL", buf);
905 /* Normal systems set SHELL by default. */
906 child_set_env(&env, &envsize, "SHELL", shell);
909 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
911 /* Set custom environment options from RSA authentication. */
912 while (custom_environment) {
913 struct envstring *ce = custom_environment;
916 for (i = 0; s[i] != '=' && s[i]; i++);
919 child_set_env(&env, &envsize, s, s + i + 1);
921 custom_environment = ce->next;
926 snprintf(buf, sizeof buf, "%.50s %d %d",
927 get_remote_ipaddr(), get_remote_port(), get_local_port());
928 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
931 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
933 child_set_env(&env, &envsize, "TERM", term);
935 child_set_env(&env, &envsize, "DISPLAY", display);
939 char *authstate,*krb5cc;
941 if ((authstate = getenv("AUTHSTATE")) != NULL)
942 child_set_env(&env,&envsize,"AUTHSTATE",authstate);
944 if ((krb5cc = getenv("KRB5CCNAME")) != NULL)
945 child_set_env(&env,&envsize,"KRB5CCNAME",krb5cc);
954 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
959 /* Pull in any environment variables that may have been set by PAM. */
960 do_pam_environment(&env, &envsize);
963 read_environment_file(&env,&envsize,"/etc/environment");
966 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
967 if (auth_get_socket_name() != NULL)
968 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
969 auth_get_socket_name());
971 /* read $HOME/.ssh/environment. */
972 if (!options.use_login) {
973 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
975 read_environment_file(&env, &envsize, buf);
978 /* dump the environment */
979 fprintf(stderr, "Environment:\n");
980 for (i = 0; env[i]; i++)
981 fprintf(stderr, " %.200s\n", env[i]);
984 * Close the connection descriptors; note that this is the child, and
985 * the server will still have the socket open, and it is important
986 * that we do not shutdown it. Note that the descriptors cannot be
987 * closed before building the environment, as we call
988 * get_remote_ipaddr there.
990 if (packet_get_connection_in() == packet_get_connection_out())
991 close(packet_get_connection_in());
993 close(packet_get_connection_in());
994 close(packet_get_connection_out());
997 * Close all descriptors related to channels. They will still remain
998 * open in the parent.
1000 /* XXX better use close-on-exec? -markus */
1001 channel_close_all();
1004 * Close any extra file descriptors. Note that there may still be
1005 * descriptors left by system functions. They will be closed later.
1010 * Close any extra open file descriptors so that we don\'t have them
1011 * hanging around in clients. Note that we want to do this after
1012 * initgroups, because at least on Solaris 2.3 it leaves file
1015 for (i = 3; i < 64; i++)
1018 /* Change current directory to the user\'s home directory. */
1019 if (chdir(pw->pw_dir) < 0)
1020 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1021 pw->pw_dir, strerror(errno));
1024 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1025 * xauth are run in the proper environment.
1030 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1033 if (!options.use_login) {
1034 if (stat(SSH_USER_RC, &st) >= 0) {
1036 fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC);
1038 f = popen("/bin/sh " SSH_USER_RC, "w");
1040 if (auth_proto != NULL && auth_data != NULL)
1041 fprintf(f, "%s %s\n", auth_proto, auth_data);
1044 fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
1045 } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
1047 fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC);
1049 f = popen("/bin/sh " SSH_SYSTEM_RC, "w");
1051 if (auth_proto != NULL && auth_data != NULL)
1052 fprintf(f, "%s %s\n", auth_proto, auth_data);
1055 fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
1056 } else if (options.xauth_location != NULL) {
1057 /* Add authority data to .Xauthority if appropriate. */
1058 if (auth_proto != NULL && auth_data != NULL) {
1059 char *screen = strchr(display, ':');
1062 "Running %.100s add %.100s %.100s %.100s\n",
1063 options.xauth_location, display,
1064 auth_proto, auth_data);
1067 "Adding %.*s/unix%s %s %s\n",
1068 screen-display, display,
1069 screen, auth_proto, auth_data);
1071 snprintf(cmd, sizeof cmd, "%s -q -",
1072 options.xauth_location);
1073 f = popen(cmd, "w");
1075 fprintf(f, "add %s %s %s\n", display,
1076 auth_proto, auth_data);
1078 fprintf(f, "add %.*s/unix%s %s %s\n",
1079 screen-display, display,
1080 screen, auth_proto, auth_data);
1083 fprintf(stderr, "Could not run %s\n",
1088 /* Get the last component of the shell name. */
1089 cp = strrchr(shell, '/');
1096 * If we have no command, execute the shell. In this case, the shell
1097 * name to be passed in argv[0] is preceded by '-' to indicate that
1098 * this is a login shell.
1101 if (!options.use_login) {
1105 * Check for mail if we have a tty and it was enabled
1106 * in server options.
1108 if (ttyname && options.check_mail) {
1110 struct stat mailstat;
1111 mailbox = getenv("MAIL");
1112 if (mailbox != NULL) {
1113 if (stat(mailbox, &mailstat) != 0 ||
1114 mailstat.st_size == 0)
1115 printf("No mail.\n");
1116 else if (mailstat.st_mtime < mailstat.st_atime)
1117 printf("You have mail.\n");
1119 printf("You have new mail.\n");
1122 /* Start the shell. Set initial character to '-'. */
1124 strncpy(buf + 1, cp, sizeof(buf) - 1);
1125 buf[sizeof(buf) - 1] = 0;
1127 /* Execute the shell. */
1130 execve(shell, argv, env);
1132 /* Executing the shell failed. */
1137 /* Launch login(1). */
1139 execl("/usr/bin/login", "login", "-h", get_remote_ipaddr(),
1140 "-p", "-f", "--", pw->pw_name, NULL);
1142 /* Login couldn't be executed, die. */
1149 * Execute the command using the user's shell. This uses the -c
1150 * option to execute the command.
1152 argv[0] = (char *) cp;
1154 argv[2] = (char *) command;
1156 execve(shell, argv, env);
1165 static int did_init = 0;
1167 debug("session_new: init");
1168 for(i = 0; i < MAX_SESSIONS; i++) {
1169 sessions[i].used = 0;
1170 sessions[i].self = i;
1174 for(i = 0; i < MAX_SESSIONS; i++) {
1175 Session *s = &sessions[i];
1186 s->auth_data = NULL;
1187 s->auth_proto = NULL;
1190 debug("session_new: session %d", i);
1201 for(i = 0; i < MAX_SESSIONS; i++) {
1202 Session *s = &sessions[i];
1203 debug("dump: used %d session %d %p channel %d pid %d",
1213 session_open(int chanid)
1215 Session *s = session_new();
1216 debug("session_open: channel %d", chanid);
1218 error("no more sessions");
1221 s->pw = auth_get_user();
1223 fatal("no user for session %i", s->self);
1224 debug("session_open: session %d: link with channel %d", s->self, chanid);
1230 session_by_channel(int id)
1233 for(i = 0; i < MAX_SESSIONS; i++) {
1234 Session *s = &sessions[i];
1235 if (s->used && s->chanid == id) {
1236 debug("session_by_channel: session %d channel %d", i, id);
1240 debug("session_by_channel: unknown channel %d", id);
1246 session_by_pid(pid_t pid)
1249 debug("session_by_pid: pid %d", pid);
1250 for(i = 0; i < MAX_SESSIONS; i++) {
1251 Session *s = &sessions[i];
1252 if (s->used && s->pid == pid)
1255 error("session_by_pid: unknown pid %d", pid);
1261 session_window_change_req(Session *s)
1263 s->col = packet_get_int();
1264 s->row = packet_get_int();
1265 s->xpixel = packet_get_int();
1266 s->ypixel = packet_get_int();
1268 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1273 session_pty_req(Session *s)
1276 char *term_modes; /* encoded terminal modes */
1282 s->term = packet_get_string(&len);
1283 s->col = packet_get_int();
1284 s->row = packet_get_int();
1285 s->xpixel = packet_get_int();
1286 s->ypixel = packet_get_int();
1287 term_modes = packet_get_string(&len);
1290 if (strcmp(s->term, "") == 0) {
1294 /* Allocate a pty and open it. */
1295 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1300 error("session_pty_req: session %d alloc failed", s->self);
1304 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1306 * Add a cleanup function to clear the utmp entry and record logout
1307 * time in case we call fatal() (e.g., the connection gets closed).
1309 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1310 pty_setowner(s->pw, s->tty);
1311 /* Get window size from the packet. */
1312 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1314 session_proctitle(s);
1316 /* XXX parse and set terminal modes */
1322 session_subsystem_req(Session *s)
1326 char *subsys = packet_get_string(&len);
1330 log("subsystem request for %s", subsys);
1332 for (i = 0; i < options.num_subsystems; i++) {
1333 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1334 debug("subsystem: exec() %s", options.subsystem_command[i]);
1335 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1341 log("subsystem request for %s failed, subsystem not found", subsys);
1348 session_x11_req(Session *s)
1350 if (!no_port_forwarding_flag) {
1351 debug("X11 forwarding disabled in user configuration file.");
1354 if (!options.x11_forwarding) {
1355 debug("X11 forwarding disabled in server configuration file.");
1358 if (xauthfile != NULL) {
1359 debug("X11 fwd already started.");
1363 debug("Received request for X11 forwarding with auth spoofing.");
1364 if (s->display != NULL)
1365 packet_disconnect("Protocol error: X11 display already set.");
1367 s->single_connection = packet_get_char();
1368 s->auth_proto = packet_get_string(NULL);
1369 s->auth_data = packet_get_string(NULL);
1370 s->screen = packet_get_int();
1373 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1374 if (s->display == NULL) {
1375 xfree(s->auth_proto);
1376 xfree(s->auth_data);
1379 xauthfile = xmalloc(MAXPATHLEN);
1380 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1381 temporarily_use_uid(s->pw->pw_uid);
1382 if (mkdtemp(xauthfile) == NULL) {
1384 error("private X11 dir: mkdtemp %s failed: %s",
1385 xauthfile, strerror(errno));
1388 xfree(s->auth_proto);
1389 xfree(s->auth_data);
1390 /* XXXX remove listening channels */
1393 strlcat(xauthfile, "/cookies", MAXPATHLEN);
1394 open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1396 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1401 session_shell_req(Session *s)
1403 /* if forced_command == NULL, the shell is execed */
1404 char *shell = forced_command;
1408 do_exec_no_pty(s, shell, s->pw);
1410 do_exec_pty(s, shell, s->pw);
1415 session_exec_req(Session *s)
1418 char *command = packet_get_string(&len);
1420 if (forced_command) {
1422 command = forced_command;
1423 debug("Forced command '%.500s'", forced_command);
1427 do_exec_no_pty(s, command, s->pw);
1429 do_exec_pty(s, command, s->pw);
1430 if (forced_command == NULL)
1436 session_input_channel_req(int id, void *arg)
1445 rtype = packet_get_string(&len);
1446 reply = packet_get_char();
1448 s = session_by_channel(id);
1450 fatal("session_input_channel_req: channel %d: no session", id);
1451 c = channel_lookup(id);
1453 fatal("session_input_channel_req: channel %d: bad channel", id);
1455 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1456 s->self, id, rtype, reply);
1459 * a session is in LARVAL state until a shell
1460 * or programm is executed
1462 if (c->type == SSH_CHANNEL_LARVAL) {
1463 if (strcmp(rtype, "shell") == 0) {
1464 success = session_shell_req(s);
1465 } else if (strcmp(rtype, "exec") == 0) {
1466 success = session_exec_req(s);
1467 } else if (strcmp(rtype, "pty-req") == 0) {
1468 success = session_pty_req(s);
1469 } else if (strcmp(rtype, "x11-req") == 0) {
1470 success = session_x11_req(s);
1471 } else if (strcmp(rtype, "subsystem") == 0) {
1472 success = session_subsystem_req(s);
1475 if (strcmp(rtype, "window-change") == 0) {
1476 success = session_window_change_req(s);
1480 packet_start(success ?
1481 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1482 packet_put_int(c->remote_id);
1489 session_set_fds(Session *s, int fdin, int fdout, int fderr)
1492 fatal("session_set_fds: called for proto != 2.0");
1494 * now that have a child and a pipe to the child,
1495 * we can activate our channel and register the fd's
1497 if (s->chanid == -1)
1498 fatal("no channel for session %d", s->self);
1499 channel_set_fds(s->chanid,
1501 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ);
1505 session_pty_cleanup(Session *s)
1507 if (s == NULL || s->ttyfd == -1)
1510 debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1512 /* Cancel the cleanup function. */
1513 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1515 /* Record that the user has logged out. */
1516 record_logout(s->pid, s->tty);
1518 /* Release the pseudo-tty. */
1519 pty_release(s->tty);
1522 * Close the server side of the socket pairs. We must do this after
1523 * the pty cleanup, so that another process doesn't get this pty
1524 * while we're still cleaning up.
1526 if (close(s->ptymaster) < 0)
1527 error("close(s->ptymaster): %s", strerror(errno));
1531 session_exit_message(Session *s, int status)
1535 fatal("session_close: no session");
1536 c = channel_lookup(s->chanid);
1538 fatal("session_close: session %d: no channel %d",
1539 s->self, s->chanid);
1540 debug("session_exit_message: session %d channel %d pid %d",
1541 s->self, s->chanid, s->pid);
1543 if (WIFEXITED(status)) {
1544 channel_request_start(s->chanid,
1546 packet_put_int(WEXITSTATUS(status));
1548 } else if (WIFSIGNALED(status)) {
1549 channel_request_start(s->chanid,
1551 packet_put_int(WTERMSIG(status));
1553 packet_put_char(WCOREDUMP(status));
1554 #else /* WCOREDUMP */
1556 #endif /* WCOREDUMP */
1557 packet_put_cstring("");
1558 packet_put_cstring("");
1561 /* Some weird exit cause. Just exit. */
1562 packet_disconnect("wait returned status %04x.", status);
1565 /* disconnect channel */
1566 debug("session_exit_message: release channel %d", s->chanid);
1567 channel_cancel_cleanup(s->chanid);
1569 * emulate a write failure with 'chan_write_failed', nobody will be
1570 * interested in data we write.
1571 * Note that we must not call 'chan_read_failed', since there could
1572 * be some more data waiting in the pipe.
1574 if (c->ostate != CHAN_OUTPUT_CLOSED)
1575 chan_write_failed(c);
1580 session_free(Session *s)
1582 debug("session_free: session %d pid %d", s->self, s->pid);
1588 xfree(s->auth_data);
1590 xfree(s->auth_proto);
1595 session_close(Session *s)
1597 session_pty_cleanup(s);
1599 session_proctitle(s);
1603 session_close_by_pid(pid_t pid, int status)
1605 Session *s = session_by_pid(pid);
1607 debug("session_close_by_pid: no session for pid %d", s->pid);
1610 if (s->chanid != -1)
1611 session_exit_message(s, status);
1616 * this is called when a channel dies before
1617 * the session 'child' itself dies
1620 session_close_by_channel(int id, void *arg)
1622 Session *s = session_by_channel(id);
1624 debug("session_close_by_channel: no session for channel %d", id);
1627 /* disconnect channel */
1628 channel_cancel_cleanup(s->chanid);
1631 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1633 /* close session immediately */
1636 /* notify child, delay session cleanup */
1638 fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1639 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
1640 error("session_close_by_channel: kill %d: %s",
1641 s->pid, strerror(errno));
1646 session_tty_list(void)
1648 static char buf[1024];
1651 for(i = 0; i < MAX_SESSIONS; i++) {
1652 Session *s = &sessions[i];
1653 if (s->used && s->ttyfd != -1) {
1655 strlcat(buf, ",", sizeof buf);
1656 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1660 strlcpy(buf, "notty", sizeof buf);
1665 session_proctitle(Session *s)
1668 error("no user for session %d", s->self);
1670 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1674 do_authenticated2(void)
1677 * Cancel the alarm we set to limit the time taken for
1683 xauthfile_cleanup_proc(NULL);