Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 3 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 | * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 11 | * SSH2 support by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 12 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 33 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 34 | |
| 35 | #include "includes.h" |
Damien Miller | 05eda43 | 2002-02-10 18:32:28 +1100 | [diff] [blame^] | 36 | RCSID("$OpenBSD: session.c,v 1.125 2002/02/09 17:37:34 deraadt Exp $"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 37 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 38 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 39 | #include "ssh1.h" |
| 40 | #include "ssh2.h" |
| 41 | #include "xmalloc.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 42 | #include "sshpty.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 43 | #include "packet.h" |
| 44 | #include "buffer.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 45 | #include "mpaux.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 46 | #include "uidswap.h" |
| 47 | #include "compat.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 48 | #include "channels.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 49 | #include "bufaux.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 50 | #include "auth.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 51 | #include "auth-options.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 52 | #include "pathnames.h" |
| 53 | #include "log.h" |
| 54 | #include "servconf.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 55 | #include "sshlogin.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 56 | #include "serverloop.h" |
| 57 | #include "canohost.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 58 | #include "session.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 59 | |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 60 | #ifdef WITH_IRIX_PROJECT |
| 61 | #include <proj.h> |
| 62 | #endif /* WITH_IRIX_PROJECT */ |
Ben Lindstrom | 980754c | 2000-11-12 00:04:24 +0000 | [diff] [blame] | 63 | #ifdef WITH_IRIX_JOBS |
| 64 | #include <sys/resource.h> |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 65 | #endif |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 66 | #ifdef WITH_IRIX_AUDIT |
| 67 | #include <sat.h> |
| 68 | #endif /* WITH_IRIX_AUDIT */ |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 69 | |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 70 | #if defined(HAVE_USERSEC_H) |
| 71 | #include <usersec.h> |
| 72 | #endif |
| 73 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 74 | #ifdef HAVE_CYGWIN |
| 75 | #include <windows.h> |
| 76 | #include <sys/cygwin.h> |
| 77 | #define is_winnt (GetVersion() < 0x80000000) |
| 78 | #endif |
| 79 | |
Damien Miller | d17b8d5 | 2000-08-09 14:42:28 +1000 | [diff] [blame] | 80 | /* AIX limits */ |
| 81 | #if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE) |
Damien Miller | 0da2eaa | 2000-08-15 11:32:59 +1000 | [diff] [blame] | 82 | # define S_UFSIZE_HARD S_UFSIZE "_hard" |
| 83 | # define S_UCPU_HARD S_UCPU "_hard" |
| 84 | # define S_UDATA_HARD S_UDATA "_hard" |
| 85 | # define S_USTACK_HARD S_USTACK "_hard" |
| 86 | # define S_URSS_HARD S_URSS "_hard" |
| 87 | # define S_UCORE_HARD S_UCORE "_hard" |
| 88 | # define S_UNOFILE_HARD S_UNOFILE "_hard" |
Damien Miller | d17b8d5 | 2000-08-09 14:42:28 +1000 | [diff] [blame] | 89 | #endif |
| 90 | |
Damien Miller | 168a700 | 2001-03-17 10:29:50 +1100 | [diff] [blame] | 91 | #ifdef _AIX |
| 92 | # include <uinfo.h> |
| 93 | #endif |
| 94 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 95 | /* types */ |
| 96 | |
| 97 | #define TTYSZ 64 |
| 98 | typedef struct Session Session; |
| 99 | struct Session { |
| 100 | int used; |
| 101 | int self; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 102 | struct passwd *pw; |
| 103 | Authctxt *authctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 104 | pid_t pid; |
| 105 | /* tty */ |
| 106 | char *term; |
| 107 | int ptyfd, ttyfd, ptymaster; |
| 108 | int row, col, xpixel, ypixel; |
| 109 | char tty[TTYSZ]; |
| 110 | /* X11 */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 111 | int display_number; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 112 | char *display; |
| 113 | int screen; |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 114 | char *auth_display; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 115 | char *auth_proto; |
| 116 | char *auth_data; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 117 | int single_connection; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 118 | /* proto 2 */ |
| 119 | int chanid; |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 120 | int is_subsystem; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /* func */ |
| 124 | |
| 125 | Session *session_new(void); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 126 | void session_set_fds(Session *, int, int, int); |
| 127 | static void session_pty_cleanup(void *); |
| 128 | void session_proctitle(Session *); |
| 129 | int session_setup_x11fwd(Session *); |
| 130 | void do_exec_pty(Session *, const char *); |
| 131 | void do_exec_no_pty(Session *, const char *); |
| 132 | void do_exec(Session *, const char *); |
| 133 | void do_login(Session *, const char *); |
Kevin Steves | a0957d6 | 2001-09-27 19:50:26 +0000 | [diff] [blame] | 134 | #ifdef LOGIN_NEEDS_UTMPX |
| 135 | static void do_pre_login(Session *s); |
| 136 | #endif |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 137 | void do_child(Session *, const char *); |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 138 | void do_motd(void); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 139 | int check_quietlogin(Session *, const char *); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 140 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 141 | static void do_authenticated1(Authctxt *); |
| 142 | static void do_authenticated2(Authctxt *); |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 143 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 144 | static void session_close(Session *); |
| 145 | static int session_pty_req(Session *); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 146 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 147 | /* import */ |
| 148 | extern ServerOptions options; |
| 149 | extern char *__progname; |
| 150 | extern int log_stderr; |
| 151 | extern int debug_flag; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 152 | extern u_int utmp_len; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 153 | extern int startup_pipe; |
Ben Lindstrom | 005dd22 | 2001-04-18 15:29:33 +0000 | [diff] [blame] | 154 | extern void destroy_sensitive_data(void); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 155 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 156 | /* original command from peer. */ |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 157 | const char *original_command = NULL; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 158 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 159 | /* data */ |
| 160 | #define MAX_SESSIONS 10 |
| 161 | Session sessions[MAX_SESSIONS]; |
Damien Miller | 15e7d4b | 2000-09-29 10:57:35 +1100 | [diff] [blame] | 162 | |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 163 | #ifdef WITH_AIXAUTHENTICATE |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 164 | char *aixloginmsg; |
| 165 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 166 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 167 | #ifdef HAVE_LOGIN_CAP |
| 168 | static login_cap_t *lc; |
| 169 | #endif |
| 170 | |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 171 | void |
| 172 | do_authenticated(Authctxt *authctxt) |
| 173 | { |
| 174 | /* |
| 175 | * Cancel the alarm we set to limit the time taken for |
| 176 | * authentication. |
| 177 | */ |
| 178 | alarm(0); |
| 179 | if (startup_pipe != -1) { |
| 180 | close(startup_pipe); |
| 181 | startup_pipe = -1; |
| 182 | } |
| 183 | #if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD) |
| 184 | if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) { |
| 185 | error("unable to get login class"); |
| 186 | return; |
| 187 | } |
Ben Lindstrom | 005dd22 | 2001-04-18 15:29:33 +0000 | [diff] [blame] | 188 | #ifdef BSD_AUTH |
| 189 | if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) { |
| 190 | packet_disconnect("Approval failure for %s", |
| 191 | authctxt->pw->pw_name); |
| 192 | } |
| 193 | #endif |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 194 | #endif |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 195 | #ifdef WITH_AIXAUTHENTICATE |
| 196 | /* We don't have a pty yet, so just label the line as "ssh" */ |
| 197 | if (loginsuccess(authctxt->user, |
Damien Miller | f3451a2 | 2002-02-05 12:40:46 +1100 | [diff] [blame] | 198 | get_canonical_hostname(options.verify_reverse_mapping), |
Damien Miller | e49d096 | 2001-11-13 23:46:18 +1100 | [diff] [blame] | 199 | "ssh", &aixloginmsg) < 0) |
| 200 | aixloginmsg = NULL; |
| 201 | #endif /* WITH_AIXAUTHENTICATE */ |
| 202 | |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 203 | /* setup the channel layer */ |
| 204 | if (!no_port_forwarding_flag && options.allow_tcp_forwarding) |
| 205 | channel_permit_all_opens(); |
| 206 | |
| 207 | if (compat20) |
| 208 | do_authenticated2(authctxt); |
| 209 | else |
| 210 | do_authenticated1(authctxt); |
Ben Lindstrom | 838394c | 2001-06-09 01:11:59 +0000 | [diff] [blame] | 211 | |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 212 | /* remove agent socket */ |
Ben Lindstrom | 838394c | 2001-06-09 01:11:59 +0000 | [diff] [blame] | 213 | if (auth_get_socket_name()) |
Ben Lindstrom | 983c098 | 2001-06-09 01:20:06 +0000 | [diff] [blame] | 214 | auth_sock_cleanup_proc(authctxt->pw); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 215 | #ifdef KRB4 |
| 216 | if (options.kerberos_ticket_cleanup) |
| 217 | krb4_cleanup_proc(authctxt); |
| 218 | #endif |
| 219 | #ifdef KRB5 |
| 220 | if (options.kerberos_ticket_cleanup) |
| 221 | krb5_cleanup_proc(authctxt); |
| 222 | #endif |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 225 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 226 | * Prepares for an interactive session. This is called after the user has |
| 227 | * been successfully authenticated. During this message exchange, pseudo |
| 228 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings |
| 229 | * are requested, etc. |
| 230 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 231 | static void |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 232 | do_authenticated1(Authctxt *authctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 233 | { |
| 234 | Session *s; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 235 | char *command; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 236 | int success, type, screen_flag; |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 237 | int compression_level = 0, enable_compression_after_reply = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 238 | u_int proto_len, data_len, dlen; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 239 | |
| 240 | s = session_new(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 241 | s->authctxt = authctxt; |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 242 | s->pw = authctxt->pw; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 243 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 244 | /* |
| 245 | * We stay in this loop until the client requests to execute a shell |
| 246 | * or a command. |
| 247 | */ |
| 248 | for (;;) { |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 249 | success = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 250 | |
| 251 | /* Get a packet from the client. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 252 | type = packet_read(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 253 | |
| 254 | /* Process the packet. */ |
| 255 | switch (type) { |
| 256 | case SSH_CMSG_REQUEST_COMPRESSION: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 257 | compression_level = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 258 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 259 | if (compression_level < 1 || compression_level > 9) { |
| 260 | packet_send_debug("Received illegal compression level %d.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 261 | compression_level); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | /* Enable compression after we have responded with SUCCESS. */ |
| 265 | enable_compression_after_reply = 1; |
| 266 | success = 1; |
| 267 | break; |
| 268 | |
| 269 | case SSH_CMSG_REQUEST_PTY: |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 270 | success = session_pty_req(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case SSH_CMSG_X11_REQUEST_FORWARDING: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 274 | s->auth_proto = packet_get_string(&proto_len); |
| 275 | s->auth_data = packet_get_string(&data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 276 | |
Ben Lindstrom | 7603b2d | 2001-02-26 20:13:32 +0000 | [diff] [blame] | 277 | screen_flag = packet_get_protocol_flags() & |
| 278 | SSH_PROTOFLAG_SCREEN_NUMBER; |
| 279 | debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); |
| 280 | |
| 281 | if (packet_remaining() == 4) { |
| 282 | if (!screen_flag) |
| 283 | debug2("Buggy client: " |
| 284 | "X11 screen flag missing"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 285 | s->screen = packet_get_int(); |
Ben Lindstrom | 8dcdeb8 | 2001-02-16 16:02:14 +0000 | [diff] [blame] | 286 | } else { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 287 | s->screen = 0; |
Ben Lindstrom | 8dcdeb8 | 2001-02-16 16:02:14 +0000 | [diff] [blame] | 288 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 289 | packet_check_eom(); |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 290 | success = session_setup_x11fwd(s); |
| 291 | if (!success) { |
| 292 | xfree(s->auth_proto); |
| 293 | xfree(s->auth_data); |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 294 | s->auth_proto = NULL; |
| 295 | s->auth_data = NULL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 296 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 297 | break; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 298 | |
| 299 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: |
| 300 | if (no_agent_forwarding_flag || compat13) { |
| 301 | debug("Authentication agent forwarding not permitted for this authentication."); |
| 302 | break; |
| 303 | } |
| 304 | debug("Received authentication agent forwarding request."); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 305 | success = auth_input_request_forwarding(s->pw); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 306 | break; |
| 307 | |
| 308 | case SSH_CMSG_PORT_FORWARD_REQUEST: |
| 309 | if (no_port_forwarding_flag) { |
| 310 | debug("Port forwarding not permitted for this authentication."); |
| 311 | break; |
| 312 | } |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 313 | if (!options.allow_tcp_forwarding) { |
| 314 | debug("Port forwarding not permitted."); |
| 315 | break; |
| 316 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 317 | debug("Received TCP/IP port forwarding request."); |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 318 | channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 319 | success = 1; |
| 320 | break; |
| 321 | |
| 322 | case SSH_CMSG_MAX_PACKET_SIZE: |
| 323 | if (packet_set_maxsize(packet_get_int()) > 0) |
| 324 | success = 1; |
| 325 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 326 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 327 | #if defined(AFS) || defined(KRB5) |
| 328 | case SSH_CMSG_HAVE_KERBEROS_TGT: |
| 329 | if (!options.kerberos_tgt_passing) { |
| 330 | verbose("Kerberos TGT passing disabled."); |
| 331 | } else { |
| 332 | char *kdata = packet_get_string(&dlen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 333 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 334 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 335 | /* XXX - 0x41, see creds_to_radix version */ |
| 336 | if (kdata[0] != 0x41) { |
| 337 | #ifdef KRB5 |
| 338 | krb5_data tgt; |
| 339 | tgt.data = kdata; |
| 340 | tgt.length = dlen; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 341 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 342 | if (auth_krb5_tgt(s->authctxt, &tgt)) |
| 343 | success = 1; |
| 344 | else |
| 345 | verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user); |
| 346 | #endif /* KRB5 */ |
| 347 | } else { |
| 348 | #ifdef AFS |
| 349 | if (auth_krb4_tgt(s->authctxt, kdata)) |
| 350 | success = 1; |
| 351 | else |
| 352 | verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user); |
| 353 | #endif /* AFS */ |
| 354 | } |
| 355 | xfree(kdata); |
| 356 | } |
| 357 | break; |
| 358 | #endif /* AFS || KRB5 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 359 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 360 | #ifdef AFS |
| 361 | case SSH_CMSG_HAVE_AFS_TOKEN: |
| 362 | if (!options.afs_token_passing || !k_hasafs()) { |
| 363 | verbose("AFS token passing disabled."); |
| 364 | } else { |
| 365 | /* Accept AFS token. */ |
| 366 | char *token = packet_get_string(&dlen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 367 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 368 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 369 | if (auth_afs_token(s->authctxt, token)) |
| 370 | success = 1; |
| 371 | else |
| 372 | verbose("AFS token refused for %.100s", |
| 373 | s->authctxt->user); |
| 374 | xfree(token); |
| 375 | } |
| 376 | break; |
| 377 | #endif /* AFS */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 378 | |
| 379 | case SSH_CMSG_EXEC_SHELL: |
| 380 | case SSH_CMSG_EXEC_CMD: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 381 | if (type == SSH_CMSG_EXEC_CMD) { |
| 382 | command = packet_get_string(&dlen); |
| 383 | debug("Exec command '%.500s'", command); |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 384 | do_exec(s, command); |
| 385 | xfree(command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 386 | } else { |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 387 | do_exec(s, NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 388 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 389 | packet_check_eom(); |
Ben Lindstrom | cb3929d | 2001-06-09 01:34:15 +0000 | [diff] [blame] | 390 | session_close(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 391 | return; |
| 392 | |
| 393 | default: |
| 394 | /* |
| 395 | * Any unknown messages in this phase are ignored, |
| 396 | * and a failure message is returned. |
| 397 | */ |
| 398 | log("Unknown packet type received after authentication: %d", type); |
| 399 | } |
| 400 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); |
| 401 | packet_send(); |
| 402 | packet_write_wait(); |
| 403 | |
| 404 | /* Enable compression now that we have replied if appropriate. */ |
| 405 | if (enable_compression_after_reply) { |
| 406 | enable_compression_after_reply = 0; |
| 407 | packet_start_compression(compression_level); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * This is called to fork and execute a command when we have no tty. This |
| 414 | * will call do_child from the child, and server_loop from the parent after |
| 415 | * setting up file descriptors and such. |
| 416 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 417 | void |
Ben Lindstrom | b4c961d | 2001-03-22 01:25:37 +0000 | [diff] [blame] | 418 | do_exec_no_pty(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 419 | { |
| 420 | int pid; |
| 421 | |
| 422 | #ifdef USE_PIPES |
| 423 | int pin[2], pout[2], perr[2]; |
| 424 | /* Allocate pipes for communicating with the program. */ |
| 425 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) |
| 426 | packet_disconnect("Could not create pipes: %.100s", |
| 427 | strerror(errno)); |
| 428 | #else /* USE_PIPES */ |
| 429 | int inout[2], err[2]; |
| 430 | /* Uses socket pairs to communicate with the program. */ |
| 431 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || |
| 432 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) |
| 433 | packet_disconnect("Could not create socket pairs: %.100s", |
| 434 | strerror(errno)); |
| 435 | #endif /* USE_PIPES */ |
| 436 | if (s == NULL) |
| 437 | fatal("do_exec_no_pty: no session"); |
| 438 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 439 | session_proctitle(s); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 440 | |
Damien Miller | c594633 | 2001-02-28 11:46:11 +1100 | [diff] [blame] | 441 | #if defined(USE_PAM) |
Damien Miller | 665af9c | 2001-06-27 09:34:15 +1000 | [diff] [blame] | 442 | do_pam_session(s->pw->pw_name, NULL); |
Damien Miller | f9e9300 | 2001-03-27 16:12:24 +1000 | [diff] [blame] | 443 | do_pam_setcred(1); |
Damien Miller | c3aa3dd | 2001-10-28 22:34:52 +1100 | [diff] [blame] | 444 | if (is_pam_password_change_required()) |
| 445 | packet_disconnect("Password change required but no " |
| 446 | "TTY available"); |
Kevin Steves | ff793a2 | 2001-02-21 16:36:51 +0000 | [diff] [blame] | 447 | #endif /* USE_PAM */ |
| 448 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 449 | /* Fork the child. */ |
| 450 | if ((pid = fork()) == 0) { |
| 451 | /* Child. Reinitialize the log since the pid has changed. */ |
| 452 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
| 453 | |
| 454 | /* |
| 455 | * Create a new session and process group since the 4.4BSD |
| 456 | * setlogin() affects the entire process group. |
| 457 | */ |
| 458 | if (setsid() < 0) |
| 459 | error("setsid failed: %.100s", strerror(errno)); |
| 460 | |
| 461 | #ifdef USE_PIPES |
| 462 | /* |
| 463 | * Redirect stdin. We close the parent side of the socket |
| 464 | * pair, and make the child side the standard input. |
| 465 | */ |
| 466 | close(pin[1]); |
| 467 | if (dup2(pin[0], 0) < 0) |
| 468 | perror("dup2 stdin"); |
| 469 | close(pin[0]); |
| 470 | |
| 471 | /* Redirect stdout. */ |
| 472 | close(pout[0]); |
| 473 | if (dup2(pout[1], 1) < 0) |
| 474 | perror("dup2 stdout"); |
| 475 | close(pout[1]); |
| 476 | |
| 477 | /* Redirect stderr. */ |
| 478 | close(perr[0]); |
| 479 | if (dup2(perr[1], 2) < 0) |
| 480 | perror("dup2 stderr"); |
| 481 | close(perr[1]); |
| 482 | #else /* USE_PIPES */ |
| 483 | /* |
| 484 | * Redirect stdin, stdout, and stderr. Stdin and stdout will |
| 485 | * use the same socket, as some programs (particularly rdist) |
| 486 | * seem to depend on it. |
| 487 | */ |
| 488 | close(inout[1]); |
| 489 | close(err[1]); |
| 490 | if (dup2(inout[0], 0) < 0) /* stdin */ |
| 491 | perror("dup2 stdin"); |
| 492 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ |
| 493 | perror("dup2 stdout"); |
| 494 | if (dup2(err[0], 2) < 0) /* stderr */ |
| 495 | perror("dup2 stderr"); |
| 496 | #endif /* USE_PIPES */ |
| 497 | |
| 498 | /* Do processing for the child (exec command etc). */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 499 | do_child(s, command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 500 | /* NOTREACHED */ |
| 501 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 502 | #ifdef HAVE_CYGWIN |
| 503 | if (is_winnt) |
| 504 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); |
| 505 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 506 | if (pid < 0) |
| 507 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 508 | s->pid = pid; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 509 | /* Set interactive/non-interactive mode. */ |
| 510 | packet_set_interactive(s->display != NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 511 | #ifdef USE_PIPES |
| 512 | /* We are the parent. Close the child sides of the pipes. */ |
| 513 | close(pin[0]); |
| 514 | close(pout[1]); |
| 515 | close(perr[1]); |
| 516 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 517 | if (compat20) { |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 518 | session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 519 | } else { |
| 520 | /* Enter the interactive session. */ |
| 521 | server_loop(pid, pin[1], pout[0], perr[0]); |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 522 | /* server_loop has closed pin[1], pout[0], and perr[0]. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 523 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 524 | #else /* USE_PIPES */ |
| 525 | /* We are the parent. Close the child sides of the socket pairs. */ |
| 526 | close(inout[0]); |
| 527 | close(err[0]); |
| 528 | |
| 529 | /* |
| 530 | * Enter the interactive session. Note: server_loop must be able to |
| 531 | * handle the case that fdin and fdout are the same. |
| 532 | */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 533 | if (compat20) { |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 534 | session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 535 | } else { |
| 536 | server_loop(pid, inout[1], inout[1], err[1]); |
| 537 | /* server_loop has closed inout[1] and err[1]. */ |
| 538 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 539 | #endif /* USE_PIPES */ |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * This is called to fork and execute a command when we have a tty. This |
| 544 | * will call do_child from the child, and server_loop from the parent after |
| 545 | * setting up file descriptors, controlling tty, updating wtmp, utmp, |
| 546 | * lastlog, and other such operations. |
| 547 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 548 | void |
Ben Lindstrom | b4c961d | 2001-03-22 01:25:37 +0000 | [diff] [blame] | 549 | do_exec_pty(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 550 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 551 | int fdout, ptyfd, ttyfd, ptymaster; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 552 | pid_t pid; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 553 | |
| 554 | if (s == NULL) |
| 555 | fatal("do_exec_pty: no session"); |
| 556 | ptyfd = s->ptyfd; |
| 557 | ttyfd = s->ttyfd; |
| 558 | |
Damien Miller | c594633 | 2001-02-28 11:46:11 +1100 | [diff] [blame] | 559 | #if defined(USE_PAM) |
Ben Lindstrom | b31783d | 2001-03-22 02:02:12 +0000 | [diff] [blame] | 560 | do_pam_session(s->pw->pw_name, s->tty); |
Damien Miller | f9e9300 | 2001-03-27 16:12:24 +1000 | [diff] [blame] | 561 | do_pam_setcred(1); |
Damien Miller | 5a76131 | 2001-02-27 09:28:23 +1100 | [diff] [blame] | 562 | #endif |
Kevin Steves | ff793a2 | 2001-02-21 16:36:51 +0000 | [diff] [blame] | 563 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 564 | /* Fork the child. */ |
| 565 | if ((pid = fork()) == 0) { |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 566 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 567 | /* Child. Reinitialize the log because the pid has changed. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 568 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 569 | /* Close the master side of the pseudo tty. */ |
| 570 | close(ptyfd); |
| 571 | |
| 572 | /* Make the pseudo tty our controlling tty. */ |
| 573 | pty_make_controlling_tty(&ttyfd, s->tty); |
| 574 | |
Damien Miller | 9c75142 | 2001-10-10 15:02:46 +1000 | [diff] [blame] | 575 | /* Redirect stdin/stdout/stderr from the pseudo tty. */ |
| 576 | if (dup2(ttyfd, 0) < 0) |
| 577 | error("dup2 stdin: %s", strerror(errno)); |
| 578 | if (dup2(ttyfd, 1) < 0) |
| 579 | error("dup2 stdout: %s", strerror(errno)); |
| 580 | if (dup2(ttyfd, 2) < 0) |
| 581 | error("dup2 stderr: %s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 582 | |
| 583 | /* Close the extra descriptor for the pseudo tty. */ |
| 584 | close(ttyfd); |
| 585 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 586 | /* record login, etc. similar to login(1) */ |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 587 | #ifndef HAVE_OSF_SIA |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 588 | if (!(options.use_login && command == NULL)) |
| 589 | do_login(s, command); |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 590 | # ifdef LOGIN_NEEDS_UTMPX |
| 591 | else |
| 592 | do_pre_login(s); |
| 593 | # endif |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 594 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 595 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 596 | /* Do common processing for the child, such as execing the command. */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 597 | do_child(s, command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 598 | /* NOTREACHED */ |
| 599 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 600 | #ifdef HAVE_CYGWIN |
| 601 | if (is_winnt) |
| 602 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); |
| 603 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 604 | if (pid < 0) |
| 605 | packet_disconnect("fork failed: %.100s", strerror(errno)); |
| 606 | s->pid = pid; |
| 607 | |
| 608 | /* Parent. Close the slave side of the pseudo tty. */ |
| 609 | close(ttyfd); |
| 610 | |
| 611 | /* |
| 612 | * Create another descriptor of the pty master side for use as the |
| 613 | * standard input. We could use the original descriptor, but this |
| 614 | * simplifies code in server_loop. The descriptor is bidirectional. |
| 615 | */ |
| 616 | fdout = dup(ptyfd); |
| 617 | if (fdout < 0) |
| 618 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); |
| 619 | |
| 620 | /* we keep a reference to the pty master */ |
| 621 | ptymaster = dup(ptyfd); |
| 622 | if (ptymaster < 0) |
| 623 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); |
| 624 | s->ptymaster = ptymaster; |
| 625 | |
| 626 | /* Enter interactive session. */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 627 | packet_set_interactive(1); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 628 | if (compat20) { |
| 629 | session_set_fds(s, ptyfd, fdout, -1); |
| 630 | } else { |
| 631 | server_loop(pid, ptyfd, fdout, -1); |
| 632 | /* server_loop _has_ closed ptyfd and fdout. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 633 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 634 | } |
| 635 | |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 636 | #ifdef LOGIN_NEEDS_UTMPX |
Damien Miller | 599d8eb | 2001-09-15 12:25:53 +1000 | [diff] [blame] | 637 | static void |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 638 | do_pre_login(Session *s) |
| 639 | { |
| 640 | socklen_t fromlen; |
| 641 | struct sockaddr_storage from; |
| 642 | pid_t pid = getpid(); |
| 643 | |
| 644 | /* |
| 645 | * Get IP address of client. If the connection is not a socket, let |
| 646 | * the address be 0.0.0.0. |
| 647 | */ |
| 648 | memset(&from, 0, sizeof(from)); |
| 649 | if (packet_connection_is_on_socket()) { |
| 650 | fromlen = sizeof(from); |
| 651 | if (getpeername(packet_get_connection_in(), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 652 | (struct sockaddr *) & from, &fromlen) < 0) { |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 653 | debug("getpeername: %.100s", strerror(errno)); |
| 654 | fatal_cleanup(); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | record_utmp_only(pid, s->tty, s->pw->pw_name, |
Damien Miller | f3451a2 | 2002-02-05 12:40:46 +1100 | [diff] [blame] | 659 | get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 660 | (struct sockaddr *)&from); |
| 661 | } |
| 662 | #endif |
| 663 | |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 664 | /* |
| 665 | * This is called to fork and execute a command. If another command is |
| 666 | * to be forced, execute that instead. |
| 667 | */ |
| 668 | void |
| 669 | do_exec(Session *s, const char *command) |
| 670 | { |
| 671 | if (forced_command) { |
| 672 | original_command = command; |
| 673 | command = forced_command; |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 674 | debug("Forced command '%.900s'", command); |
| 675 | } |
| 676 | |
| 677 | if (s->ttyfd != -1) |
| 678 | do_exec_pty(s, command); |
| 679 | else |
| 680 | do_exec_no_pty(s, command); |
| 681 | |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 682 | original_command = NULL; |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 685 | /* administrative, login(1)-like work */ |
| 686 | void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 687 | do_login(Session *s, const char *command) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 688 | { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 689 | char *time_string; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 690 | char hostname[MAXHOSTNAMELEN]; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 691 | socklen_t fromlen; |
| 692 | struct sockaddr_storage from; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 693 | time_t last_login_time; |
| 694 | struct passwd * pw = s->pw; |
| 695 | pid_t pid = getpid(); |
| 696 | |
| 697 | /* |
| 698 | * Get IP address of client. If the connection is not a socket, let |
| 699 | * the address be 0.0.0.0. |
| 700 | */ |
| 701 | memset(&from, 0, sizeof(from)); |
| 702 | if (packet_connection_is_on_socket()) { |
| 703 | fromlen = sizeof(from); |
| 704 | if (getpeername(packet_get_connection_in(), |
| 705 | (struct sockaddr *) & from, &fromlen) < 0) { |
| 706 | debug("getpeername: %.100s", strerror(errno)); |
| 707 | fatal_cleanup(); |
| 708 | } |
| 709 | } |
| 710 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 711 | /* Get the time and hostname when the user last logged in. */ |
Ben Lindstrom | 7bfff36 | 2001-03-26 05:45:53 +0000 | [diff] [blame] | 712 | if (options.print_lastlog) { |
| 713 | hostname[0] = '\0'; |
| 714 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, |
| 715 | hostname, sizeof(hostname)); |
| 716 | } |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 717 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 718 | /* Record that there was a login on that tty from the remote host. */ |
| 719 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, |
Damien Miller | c5d8635 | 2002-02-05 12:13:41 +1100 | [diff] [blame] | 720 | get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), |
Ben Lindstrom | f15a386 | 2001-04-05 23:32:17 +0000 | [diff] [blame] | 721 | (struct sockaddr *)&from); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 722 | |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 723 | #ifdef USE_PAM |
| 724 | /* |
| 725 | * If password change is needed, do it now. |
| 726 | * This needs to occur before the ~/.hushlogin check. |
| 727 | */ |
Damien Miller | 646aa60 | 2001-02-15 11:51:32 +1100 | [diff] [blame] | 728 | if (is_pam_password_change_required()) { |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 729 | print_pam_messages(); |
| 730 | do_pam_chauthtok(); |
| 731 | } |
| 732 | #endif |
| 733 | |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 734 | if (check_quietlogin(s, command)) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 735 | return; |
| 736 | |
| 737 | #ifdef USE_PAM |
Damien Miller | 646aa60 | 2001-02-15 11:51:32 +1100 | [diff] [blame] | 738 | if (!is_pam_password_change_required()) |
Kevin Steves | 092f2ef | 2000-10-14 13:36:13 +0000 | [diff] [blame] | 739 | print_pam_messages(); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 740 | #endif /* USE_PAM */ |
| 741 | #ifdef WITH_AIXAUTHENTICATE |
| 742 | if (aixloginmsg && *aixloginmsg) |
| 743 | printf("%s\n", aixloginmsg); |
| 744 | #endif /* WITH_AIXAUTHENTICATE */ |
| 745 | |
Ben Lindstrom | 7bfff36 | 2001-03-26 05:45:53 +0000 | [diff] [blame] | 746 | if (options.print_lastlog && last_login_time != 0) { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 747 | time_string = ctime(&last_login_time); |
| 748 | if (strchr(time_string, '\n')) |
| 749 | *strchr(time_string, '\n') = 0; |
Kevin Steves | c368a3c | 2000-10-14 16:10:06 +0000 | [diff] [blame] | 750 | if (strcmp(hostname, "") == 0) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 751 | printf("Last login: %s\r\n", time_string); |
| 752 | else |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 753 | printf("Last login: %s from %s\r\n", time_string, hostname); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 754 | } |
Damien Miller | cf205e8 | 2001-04-16 18:29:15 +1000 | [diff] [blame] | 755 | |
| 756 | do_motd(); |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Display the message of the day. |
| 761 | */ |
| 762 | void |
| 763 | do_motd(void) |
| 764 | { |
| 765 | FILE *f; |
| 766 | char buf[256]; |
| 767 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 768 | if (options.print_motd) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 769 | #ifdef HAVE_LOGIN_CAP |
| 770 | f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", |
| 771 | "/etc/motd"), "r"); |
| 772 | #else |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 773 | f = fopen("/etc/motd", "r"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 774 | #endif |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 775 | if (f) { |
| 776 | while (fgets(buf, sizeof(buf), f)) |
| 777 | fputs(buf, stdout); |
| 778 | fclose(f); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
Kevin Steves | 8f63caa | 2001-07-04 18:23:02 +0000 | [diff] [blame] | 783 | |
| 784 | /* |
| 785 | * Check for quiet login, either .hushlogin or command given. |
| 786 | */ |
| 787 | int |
| 788 | check_quietlogin(Session *s, const char *command) |
| 789 | { |
| 790 | char buf[256]; |
| 791 | struct passwd *pw = s->pw; |
| 792 | struct stat st; |
| 793 | |
| 794 | /* Return 1 if .hushlogin exists or a command given. */ |
| 795 | if (command != NULL) |
| 796 | return 1; |
| 797 | snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); |
| 798 | #ifdef HAVE_LOGIN_CAP |
| 799 | if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) |
| 800 | return 1; |
| 801 | #else |
| 802 | if (stat(buf, &st) >= 0) |
| 803 | return 1; |
| 804 | #endif |
| 805 | return 0; |
| 806 | } |
| 807 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 808 | /* |
| 809 | * Sets the value of the given variable in the environment. If the variable |
| 810 | * already exists, its value is overriden. |
| 811 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 812 | static void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 813 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 814 | const char *value) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 815 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 816 | u_int i, namelen; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 817 | char **env; |
| 818 | |
| 819 | /* |
| 820 | * Find the slot where the value should be stored. If the variable |
| 821 | * already exists, we reuse the slot; otherwise we append a new slot |
| 822 | * at the end of the array, expanding if necessary. |
| 823 | */ |
| 824 | env = *envp; |
| 825 | namelen = strlen(name); |
| 826 | for (i = 0; env[i]; i++) |
| 827 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') |
| 828 | break; |
| 829 | if (env[i]) { |
| 830 | /* Reuse the slot. */ |
| 831 | xfree(env[i]); |
| 832 | } else { |
| 833 | /* New variable. Expand if necessary. */ |
| 834 | if (i >= (*envsizep) - 1) { |
| 835 | (*envsizep) += 50; |
| 836 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); |
| 837 | } |
| 838 | /* Need to set the NULL pointer at end of array beyond the new slot. */ |
| 839 | env[i + 1] = NULL; |
| 840 | } |
| 841 | |
| 842 | /* Allocate space and format the variable in the appropriate slot. */ |
| 843 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); |
| 844 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Reads environment variables from the given file and adds/overrides them |
| 849 | * into the environment. If the file does not exist, this does nothing. |
| 850 | * Otherwise, it must consist of empty lines, comments (line starts with '#') |
| 851 | * and assignments of the form name=value. No other forms are allowed. |
| 852 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 853 | static void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 854 | read_environment_file(char ***env, u_int *envsize, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 855 | const char *filename) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 856 | { |
| 857 | FILE *f; |
| 858 | char buf[4096]; |
| 859 | char *cp, *value; |
| 860 | |
| 861 | f = fopen(filename, "r"); |
| 862 | if (!f) |
| 863 | return; |
| 864 | |
| 865 | while (fgets(buf, sizeof(buf), f)) { |
| 866 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
| 867 | ; |
| 868 | if (!*cp || *cp == '#' || *cp == '\n') |
| 869 | continue; |
| 870 | if (strchr(cp, '\n')) |
| 871 | *strchr(cp, '\n') = '\0'; |
| 872 | value = strchr(cp, '='); |
| 873 | if (value == NULL) { |
| 874 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); |
| 875 | continue; |
| 876 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 877 | /* |
| 878 | * Replace the equals sign by nul, and advance value to |
| 879 | * the value string. |
| 880 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 881 | *value = '\0'; |
| 882 | value++; |
| 883 | child_set_env(env, envsize, cp, value); |
| 884 | } |
| 885 | fclose(f); |
| 886 | } |
| 887 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 888 | void copy_environment(char **source, char ***env, u_int *envsize) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 889 | { |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 890 | char *var_name, *var_val; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 891 | int i; |
| 892 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 893 | if (source == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 894 | return; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 895 | |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 896 | for(i = 0; source[i] != NULL; i++) { |
| 897 | var_name = xstrdup(source[i]); |
| 898 | if ((var_val = strstr(var_name, "=")) == NULL) { |
| 899 | xfree(var_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 900 | continue; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 901 | } |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 902 | *var_val++ = '\0'; |
| 903 | |
| 904 | debug3("Copy environment: %s=%s", var_name, var_val); |
| 905 | child_set_env(env, envsize, var_name, var_val); |
| 906 | |
| 907 | xfree(var_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 908 | } |
| 909 | } |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 910 | |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 911 | #if defined(HAVE_GETUSERATTR) |
| 912 | /* |
| 913 | * AIX-specific login initialisation |
| 914 | */ |
| 915 | void set_limit(char *user, char *soft, char *hard, int resource, int mult) |
| 916 | { |
| 917 | struct rlimit rlim; |
Damien Miller | 65964d6 | 2000-07-11 09:16:22 +1000 | [diff] [blame] | 918 | int slim, hlim; |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 919 | |
| 920 | getrlimit(resource, &rlim); |
| 921 | |
Damien Miller | 65964d6 | 2000-07-11 09:16:22 +1000 | [diff] [blame] | 922 | slim = 0; |
| 923 | if (getuserattr(user, soft, &slim, SEC_INT) != -1) { |
| 924 | if (slim < 0) { |
| 925 | rlim.rlim_cur = RLIM_INFINITY; |
| 926 | } else if (slim != 0) { |
| 927 | /* See the wackiness below */ |
| 928 | if (rlim.rlim_cur == slim * mult) |
| 929 | slim = 0; |
| 930 | else |
| 931 | rlim.rlim_cur = slim * mult; |
| 932 | } |
| 933 | } |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 934 | |
Damien Miller | 65964d6 | 2000-07-11 09:16:22 +1000 | [diff] [blame] | 935 | hlim = 0; |
| 936 | if (getuserattr(user, hard, &hlim, SEC_INT) != -1) { |
| 937 | if (hlim < 0) { |
| 938 | rlim.rlim_max = RLIM_INFINITY; |
| 939 | } else if (hlim != 0) { |
| 940 | rlim.rlim_max = hlim * mult; |
| 941 | } |
| 942 | } |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 943 | |
Damien Miller | 65964d6 | 2000-07-11 09:16:22 +1000 | [diff] [blame] | 944 | /* |
| 945 | * XXX For cpu and fsize the soft limit is set to the hard limit |
| 946 | * if the hard limit is left at its default value and the soft limit |
| 947 | * is changed from its default value, either by requesting it |
| 948 | * (slim == 0) or by setting it to the current default. At least |
| 949 | * that's how rlogind does it. If you're confused you're not alone. |
| 950 | * Bug or feature? AIX 4.3.1.2 |
| 951 | */ |
| 952 | if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu")) |
| 953 | && hlim == 0 && slim != 0) |
| 954 | rlim.rlim_max = rlim.rlim_cur; |
| 955 | /* A specified hard limit limits the soft limit */ |
| 956 | else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max) |
| 957 | rlim.rlim_cur = rlim.rlim_max; |
| 958 | /* A soft limit can increase a hard limit */ |
| 959 | else if (rlim.rlim_cur > rlim.rlim_max) |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 960 | rlim.rlim_max = rlim.rlim_cur; |
| 961 | |
| 962 | if (setrlimit(resource, &rlim) != 0) |
Damien Miller | 65964d6 | 2000-07-11 09:16:22 +1000 | [diff] [blame] | 963 | error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno)); |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | void set_limits_from_userattr(char *user) |
| 967 | { |
| 968 | int mask; |
| 969 | char buf[16]; |
| 970 | |
| 971 | set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512); |
| 972 | set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1); |
| 973 | set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512); |
| 974 | set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512); |
| 975 | set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512); |
| 976 | set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512); |
| 977 | #if defined(S_UNOFILE) |
| 978 | set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1); |
| 979 | #endif |
| 980 | |
| 981 | if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) { |
| 982 | /* Convert decimal to octal */ |
| 983 | (void) snprintf(buf, sizeof(buf), "%d", mask); |
| 984 | if (sscanf(buf, "%o", &mask) == 1) |
| 985 | umask(mask); |
| 986 | } |
| 987 | } |
| 988 | #endif /* defined(HAVE_GETUSERATTR) */ |
| 989 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 990 | /* |
| 991 | * Performs common processing for the child, such as setting up the |
| 992 | * environment, closing extra file descriptors, setting the user and group |
| 993 | * ids, and executing the command or shell. |
| 994 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 995 | void |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 996 | do_child(Session *s, const char *command) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 997 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 998 | const char *shell, *hostname = NULL, *cp = NULL; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 999 | struct passwd *pw = s->pw; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1000 | char buf[256]; |
Damien Miller | 0c043c1 | 2000-06-07 21:22:38 +1000 | [diff] [blame] | 1001 | char cmd[1024]; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1002 | FILE *f = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1003 | u_int envsize, i; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1004 | char **env; |
| 1005 | extern char **environ; |
| 1006 | struct stat st; |
| 1007 | char *argv[10]; |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 1008 | int do_xauth; |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 1009 | #ifdef WITH_IRIX_PROJECT |
| 1010 | prid_t projid; |
| 1011 | #endif /* WITH_IRIX_PROJECT */ |
Ben Lindstrom | 980754c | 2000-11-12 00:04:24 +0000 | [diff] [blame] | 1012 | #ifdef WITH_IRIX_JOBS |
| 1013 | jid_t jid = 0; |
| 1014 | #else |
| 1015 | #ifdef WITH_IRIX_ARRAY |
| 1016 | int jid = 0; |
| 1017 | #endif /* WITH_IRIX_ARRAY */ |
| 1018 | #endif /* WITH_IRIX_JOBS */ |
| 1019 | |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 1020 | do_xauth = |
| 1021 | s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; |
| 1022 | |
Ben Lindstrom | 005dd22 | 2001-04-18 15:29:33 +0000 | [diff] [blame] | 1023 | /* remove hostkey from the child's memory */ |
| 1024 | destroy_sensitive_data(); |
| 1025 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1026 | /* login(1) is only called if we execute the login shell */ |
| 1027 | if (options.use_login && command != NULL) |
| 1028 | options.use_login = 0; |
| 1029 | |
Damien Miller | c62f1fc | 2001-07-14 11:54:05 +1000 | [diff] [blame] | 1030 | #if !defined(HAVE_OSF_SIA) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1031 | if (!options.use_login) { |
| 1032 | # ifdef HAVE_LOGIN_CAP |
| 1033 | if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) |
| 1034 | f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, |
| 1035 | _PATH_NOLOGIN), "r"); |
| 1036 | # else /* HAVE_LOGIN_CAP */ |
| 1037 | if (pw->pw_uid) |
| 1038 | f = fopen(_PATH_NOLOGIN, "r"); |
| 1039 | # endif /* HAVE_LOGIN_CAP */ |
| 1040 | if (f) { |
| 1041 | /* /etc/nologin exists. Print its contents and exit. */ |
| 1042 | while (fgets(buf, sizeof(buf), f)) |
| 1043 | fputs(buf, stderr); |
| 1044 | fclose(f); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1045 | exit(254); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1046 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1047 | } |
Damien Miller | c62f1fc | 2001-07-14 11:54:05 +1000 | [diff] [blame] | 1048 | #endif /* HAVE_OSF_SIA */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1049 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1050 | /* Set login name, uid, gid, and groups. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1051 | /* Login(1) does this as well, and it needs uid 0 for the "-h" |
| 1052 | switch, so we let login(1) to this for us. */ |
| 1053 | if (!options.use_login) { |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 1054 | #ifdef HAVE_OSF_SIA |
Damien Miller | b69407d | 2001-03-21 16:13:03 +1100 | [diff] [blame] | 1055 | session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty); |
Damien Miller | 364a9bd | 2001-04-16 18:37:05 +1000 | [diff] [blame] | 1056 | if (!check_quietlogin(s, command)) |
| 1057 | do_motd(); |
Kevin Steves | 7fafa5c | 2001-02-13 18:45:00 +0000 | [diff] [blame] | 1058 | #else /* HAVE_OSF_SIA */ |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1059 | #ifdef HAVE_CYGWIN |
| 1060 | if (is_winnt) { |
| 1061 | #else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1062 | if (getuid() == 0 || geteuid() == 0) { |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1063 | #endif |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1064 | # ifdef HAVE_GETUSERATTR |
Damien Miller | 5fc8565 | 2000-07-09 23:53:07 +1000 | [diff] [blame] | 1065 | set_limits_from_userattr(pw->pw_name); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1066 | # endif /* HAVE_GETUSERATTR */ |
| 1067 | # ifdef HAVE_LOGIN_CAP |
| 1068 | if (setusercontext(lc, pw, pw->pw_uid, |
| 1069 | (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) { |
| 1070 | perror("unable to set user context"); |
| 1071 | exit(1); |
| 1072 | } |
| 1073 | # else /* HAVE_LOGIN_CAP */ |
Ben Lindstrom | e1bd29b | 2001-02-21 20:00:28 +0000 | [diff] [blame] | 1074 | #if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) |
| 1075 | /* Sets login uid for accounting */ |
| 1076 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) |
| 1077 | error("setluid: %s", strerror(errno)); |
| 1078 | #endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ |
| 1079 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1080 | if (setlogin(pw->pw_name) < 0) |
| 1081 | error("setlogin failed: %s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1082 | if (setgid(pw->pw_gid) < 0) { |
| 1083 | perror("setgid"); |
| 1084 | exit(1); |
| 1085 | } |
| 1086 | /* Initialize the group list. */ |
| 1087 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { |
| 1088 | perror("initgroups"); |
| 1089 | exit(1); |
| 1090 | } |
| 1091 | endgrent(); |
Damien Miller | f9e9300 | 2001-03-27 16:12:24 +1000 | [diff] [blame] | 1092 | # ifdef USE_PAM |
| 1093 | /* |
| 1094 | * PAM credentials may take the form of |
| 1095 | * supplementary groups. These will have been |
| 1096 | * wiped by the above initgroups() call. |
| 1097 | * Reestablish them here. |
| 1098 | */ |
| 1099 | do_pam_setcred(0); |
| 1100 | # endif /* USE_PAM */ |
Ben Lindstrom | 980754c | 2000-11-12 00:04:24 +0000 | [diff] [blame] | 1101 | # ifdef WITH_IRIX_JOBS |
| 1102 | jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive"); |
| 1103 | if (jid == -1) { |
| 1104 | fatal("Failed to create job container: %.100s", |
| 1105 | strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1106 | } |
Ben Lindstrom | 980754c | 2000-11-12 00:04:24 +0000 | [diff] [blame] | 1107 | # endif /* WITH_IRIX_JOBS */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1108 | # ifdef WITH_IRIX_ARRAY |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 1109 | /* initialize array session */ |
Ben Lindstrom | 980754c | 2000-11-12 00:04:24 +0000 | [diff] [blame] | 1110 | if (jid == 0) { |
| 1111 | if (newarraysess() != 0) |
| 1112 | fatal("Failed to set up new array session: %.100s", |
| 1113 | strerror(errno)); |
| 1114 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1115 | # endif /* WITH_IRIX_ARRAY */ |
| 1116 | # ifdef WITH_IRIX_PROJECT |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 1117 | /* initialize irix project info */ |
| 1118 | if ((projid = getdfltprojuser(pw->pw_name)) == -1) { |
| 1119 | debug("Failed to get project id, using projid 0"); |
| 1120 | projid = 0; |
| 1121 | } |
Damien Miller | 91606b1 | 2000-06-28 08:22:29 +1000 | [diff] [blame] | 1122 | if (setprid(projid)) |
| 1123 | fatal("Failed to initialize project %d for %s: %.100s", |
| 1124 | (int)projid, pw->pw_name, strerror(errno)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1125 | # endif /* WITH_IRIX_PROJECT */ |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 1126 | #ifdef WITH_IRIX_AUDIT |
| 1127 | if (sysconf(_SC_AUDIT)) { |
| 1128 | debug("Setting sat id to %d", (int) pw->pw_uid); |
| 1129 | if (satsetid(pw->pw_uid)) |
| 1130 | debug("error setting satid: %.100s", strerror(errno)); |
| 1131 | } |
| 1132 | #endif /* WITH_IRIX_AUDIT */ |
| 1133 | |
Damien Miller | 168a700 | 2001-03-17 10:29:50 +1100 | [diff] [blame] | 1134 | #ifdef _AIX |
| 1135 | /* |
| 1136 | * AIX has a "usrinfo" area where logname and |
| 1137 | * other stuff is stored - a few applications |
| 1138 | * actually use this and die if it's not set |
| 1139 | */ |
Damien Miller | bebd8be | 2001-03-22 11:58:15 +1100 | [diff] [blame] | 1140 | if (s->ttyfd == -1) |
| 1141 | s->tty[0] = '\0'; |
Damien Miller | be08176 | 2001-03-21 11:11:57 +1100 | [diff] [blame] | 1142 | cp = xmalloc(22 + strlen(s->tty) + |
Damien Miller | 168a700 | 2001-03-17 10:29:50 +1100 | [diff] [blame] | 1143 | 2 * strlen(pw->pw_name)); |
| 1144 | i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c", |
Damien Miller | bebd8be | 2001-03-22 11:58:15 +1100 | [diff] [blame] | 1145 | pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0); |
Damien Miller | 168a700 | 2001-03-17 10:29:50 +1100 | [diff] [blame] | 1146 | if (usrinfo(SETUINFO, cp, i) == -1) |
| 1147 | fatal("Couldn't set usrinfo: %s", |
| 1148 | strerror(errno)); |
| 1149 | debug3("AIX/UsrInfo: set len %d", i); |
| 1150 | xfree(cp); |
| 1151 | #endif |
| 1152 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1153 | /* Permanently switch to the desired uid. */ |
Ben Lindstrom | 3fcf1a2 | 2001-04-08 18:26:59 +0000 | [diff] [blame] | 1154 | permanently_set_uid(pw); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1155 | # endif /* HAVE_LOGIN_CAP */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1156 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1157 | #endif /* HAVE_OSF_SIA */ |
| 1158 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1159 | #ifdef HAVE_CYGWIN |
| 1160 | if (is_winnt) |
| 1161 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1162 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 1163 | fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1164 | } |
| 1165 | /* |
| 1166 | * Get the shell from the password data. An empty shell field is |
| 1167 | * legal, and means /bin/sh. |
| 1168 | */ |
| 1169 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1170 | #ifdef HAVE_LOGIN_CAP |
| 1171 | shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); |
| 1172 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1173 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1174 | /* Initialize the environment. */ |
| 1175 | envsize = 100; |
| 1176 | env = xmalloc(envsize * sizeof(char *)); |
| 1177 | env[0] = NULL; |
| 1178 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1179 | #ifdef HAVE_CYGWIN |
| 1180 | /* |
| 1181 | * The Windows environment contains some setting which are |
| 1182 | * important for a running system. They must not be dropped. |
| 1183 | */ |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 1184 | copy_environment(environ, &env, &envsize); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1185 | #endif |
| 1186 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1187 | if (!options.use_login) { |
| 1188 | /* Set basic environment. */ |
| 1189 | child_set_env(&env, &envsize, "USER", pw->pw_name); |
| 1190 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); |
| 1191 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1192 | #ifdef HAVE_LOGIN_CAP |
| 1193 | (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH); |
| 1194 | child_set_env(&env, &envsize, "PATH", getenv("PATH")); |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1195 | #else /* HAVE_LOGIN_CAP */ |
| 1196 | # ifndef HAVE_CYGWIN |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1197 | /* |
| 1198 | * There's no standard path on Windows. The path contains |
| 1199 | * important components pointing to the system directories, |
| 1200 | * needed for loading shared libraries. So the path better |
| 1201 | * remains intact here. |
| 1202 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1203 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1204 | # endif /* HAVE_CYGWIN */ |
| 1205 | #endif /* HAVE_LOGIN_CAP */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1206 | |
| 1207 | snprintf(buf, sizeof buf, "%.200s/%.50s", |
| 1208 | _PATH_MAILDIR, pw->pw_name); |
| 1209 | child_set_env(&env, &envsize, "MAIL", buf); |
| 1210 | |
| 1211 | /* Normal systems set SHELL by default. */ |
| 1212 | child_set_env(&env, &envsize, "SHELL", shell); |
| 1213 | } |
| 1214 | if (getenv("TZ")) |
| 1215 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); |
| 1216 | |
| 1217 | /* Set custom environment options from RSA authentication. */ |
Ben Lindstrom | 38b951c | 2001-12-06 17:47:47 +0000 | [diff] [blame] | 1218 | if (!options.use_login) { |
| 1219 | while (custom_environment) { |
| 1220 | struct envstring *ce = custom_environment; |
| 1221 | char *s = ce->s; |
| 1222 | int i; |
| 1223 | for (i = 0; s[i] != '=' && s[i]; i++) |
| 1224 | ; |
| 1225 | if (s[i] == '=') { |
| 1226 | s[i] = 0; |
| 1227 | child_set_env(&env, &envsize, s, s + i + 1); |
| 1228 | } |
| 1229 | custom_environment = ce->next; |
| 1230 | xfree(ce->s); |
| 1231 | xfree(ce); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1232 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | snprintf(buf, sizeof buf, "%.50s %d %d", |
| 1236 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
| 1237 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
| 1238 | |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1239 | if (s->ttyfd != -1) |
| 1240 | child_set_env(&env, &envsize, "SSH_TTY", s->tty); |
| 1241 | if (s->term) |
| 1242 | child_set_env(&env, &envsize, "TERM", s->term); |
| 1243 | if (s->display) |
| 1244 | child_set_env(&env, &envsize, "DISPLAY", s->display); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1245 | if (original_command) |
| 1246 | child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", |
| 1247 | original_command); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1248 | |
| 1249 | #ifdef _AIX |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1250 | if ((cp = getenv("AUTHSTATE")) != NULL) |
| 1251 | child_set_env(&env, &envsize, "AUTHSTATE", cp); |
| 1252 | if ((cp = getenv("KRB5CCNAME")) != NULL) |
| 1253 | child_set_env(&env, &envsize, "KRB5CCNAME", cp); |
| 1254 | read_environment_file(&env, &envsize, "/etc/environment"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1255 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1256 | #ifdef KRB4 |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1257 | if (s->authctxt->krb4_ticket_file) |
| 1258 | child_set_env(&env, &envsize, "KRBTKFILE", |
| 1259 | s->authctxt->krb4_ticket_file); |
| 1260 | #endif |
| 1261 | #ifdef KRB5 |
| 1262 | if (s->authctxt->krb5_ticket_file) |
| 1263 | child_set_env(&env, &envsize, "KRB5CCNAME", |
| 1264 | s->authctxt->krb5_ticket_file); |
| 1265 | #endif |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1266 | #ifdef USE_PAM |
| 1267 | /* Pull in any environment variables that may have been set by PAM. */ |
Damien Miller | bb9ffc1 | 2002-01-08 10:59:32 +1100 | [diff] [blame] | 1268 | copy_environment(fetch_pam_environment(), &env, &envsize); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1269 | #endif /* USE_PAM */ |
| 1270 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1271 | if (auth_get_socket_name() != NULL) |
| 1272 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1273 | auth_get_socket_name()); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1274 | |
| 1275 | /* read $HOME/.ssh/environment. */ |
| 1276 | if (!options.use_login) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 1277 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", |
| 1278 | pw->pw_dir); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1279 | read_environment_file(&env, &envsize, buf); |
| 1280 | } |
| 1281 | if (debug_flag) { |
| 1282 | /* dump the environment */ |
| 1283 | fprintf(stderr, "Environment:\n"); |
| 1284 | for (i = 0; env[i]; i++) |
| 1285 | fprintf(stderr, " %.200s\n", env[i]); |
| 1286 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1287 | /* we have to stash the hostname before we close our socket. */ |
| 1288 | if (options.use_login) |
Ben Lindstrom | f15a386 | 2001-04-05 23:32:17 +0000 | [diff] [blame] | 1289 | hostname = get_remote_name_or_ip(utmp_len, |
Damien Miller | c5d8635 | 2002-02-05 12:13:41 +1100 | [diff] [blame] | 1290 | options.verify_reverse_mapping); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1291 | /* |
| 1292 | * Close the connection descriptors; note that this is the child, and |
| 1293 | * the server will still have the socket open, and it is important |
| 1294 | * that we do not shutdown it. Note that the descriptors cannot be |
| 1295 | * closed before building the environment, as we call |
| 1296 | * get_remote_ipaddr there. |
| 1297 | */ |
| 1298 | if (packet_get_connection_in() == packet_get_connection_out()) |
| 1299 | close(packet_get_connection_in()); |
| 1300 | else { |
| 1301 | close(packet_get_connection_in()); |
| 1302 | close(packet_get_connection_out()); |
| 1303 | } |
| 1304 | /* |
| 1305 | * Close all descriptors related to channels. They will still remain |
| 1306 | * open in the parent. |
| 1307 | */ |
| 1308 | /* XXX better use close-on-exec? -markus */ |
| 1309 | channel_close_all(); |
| 1310 | |
| 1311 | /* |
| 1312 | * Close any extra file descriptors. Note that there may still be |
| 1313 | * descriptors left by system functions. They will be closed later. |
| 1314 | */ |
| 1315 | endpwent(); |
| 1316 | |
| 1317 | /* |
| 1318 | * Close any extra open file descriptors so that we don\'t have them |
| 1319 | * hanging around in clients. Note that we want to do this after |
| 1320 | * initgroups, because at least on Solaris 2.3 it leaves file |
| 1321 | * descriptors open. |
| 1322 | */ |
| 1323 | for (i = 3; i < 64; i++) |
| 1324 | close(i); |
| 1325 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1326 | /* |
Damien Miller | 05eda43 | 2002-02-10 18:32:28 +1100 | [diff] [blame^] | 1327 | * Must take new environment into use so that .ssh/rc, |
| 1328 | * /etc/ssh/sshrc and xauth are run in the proper environment. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1329 | */ |
| 1330 | environ = env; |
| 1331 | |
Ben Lindstrom | 37e41c9 | 2001-09-16 22:17:15 +0000 | [diff] [blame] | 1332 | #ifdef AFS |
| 1333 | /* Try to get AFS tokens for the local cell. */ |
| 1334 | if (k_hasafs()) { |
| 1335 | char cell[64]; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1336 | |
Ben Lindstrom | 37e41c9 | 2001-09-16 22:17:15 +0000 | [diff] [blame] | 1337 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) |
| 1338 | krb_afslog(cell, 0); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1339 | |
Ben Lindstrom | 37e41c9 | 2001-09-16 22:17:15 +0000 | [diff] [blame] | 1340 | krb_afslog(0, 0); |
| 1341 | } |
| 1342 | #endif /* AFS */ |
| 1343 | |
Damien Miller | 139d4cd | 2001-10-10 15:07:44 +1000 | [diff] [blame] | 1344 | /* Change current directory to the user\'s home directory. */ |
| 1345 | if (chdir(pw->pw_dir) < 0) { |
| 1346 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", |
| 1347 | pw->pw_dir, strerror(errno)); |
| 1348 | #ifdef HAVE_LOGIN_CAP |
| 1349 | if (login_getcapbool(lc, "requirehome", 0)) |
| 1350 | exit(1); |
| 1351 | #endif |
| 1352 | } |
| 1353 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1354 | /* |
Damien Miller | 05eda43 | 2002-02-10 18:32:28 +1100 | [diff] [blame^] | 1355 | * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found |
| 1356 | * first in this order). |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1357 | */ |
| 1358 | if (!options.use_login) { |
Ben Lindstrom | 005dd22 | 2001-04-18 15:29:33 +0000 | [diff] [blame] | 1359 | /* ignore _PATH_SSH_USER_RC for subsystems */ |
| 1360 | if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) { |
Ben Lindstrom | 60402fd | 2001-05-03 22:37:26 +0000 | [diff] [blame] | 1361 | snprintf(cmd, sizeof cmd, "%s -c '%s %s'", |
| 1362 | shell, _PATH_BSHELL, _PATH_SSH_USER_RC); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1363 | if (debug_flag) |
Ben Lindstrom | 60402fd | 2001-05-03 22:37:26 +0000 | [diff] [blame] | 1364 | fprintf(stderr, "Running %s\n", cmd); |
| 1365 | f = popen(cmd, "w"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1366 | if (f) { |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1367 | if (do_xauth) |
| 1368 | fprintf(f, "%s %s\n", s->auth_proto, |
| 1369 | s->auth_data); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1370 | pclose(f); |
| 1371 | } else |
Ben Lindstrom | 5428bea | 2001-05-06 02:53:25 +0000 | [diff] [blame] | 1372 | fprintf(stderr, "Could not run %s\n", |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1373 | _PATH_SSH_USER_RC); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1374 | } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1375 | if (debug_flag) |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1376 | fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, |
| 1377 | _PATH_SSH_SYSTEM_RC); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1378 | f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1379 | if (f) { |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1380 | if (do_xauth) |
| 1381 | fprintf(f, "%s %s\n", s->auth_proto, |
| 1382 | s->auth_data); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1383 | pclose(f); |
| 1384 | } else |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1385 | fprintf(stderr, "Could not run %s\n", |
| 1386 | _PATH_SSH_SYSTEM_RC); |
| 1387 | } else if (do_xauth && options.xauth_location != NULL) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1388 | /* Add authority data to .Xauthority if appropriate. */ |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1389 | if (debug_flag) { |
| 1390 | fprintf(stderr, |
| 1391 | "Running %.100s add " |
| 1392 | "%.100s %.100s %.100s\n", |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1393 | options.xauth_location, s->auth_display, |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1394 | s->auth_proto, s->auth_data); |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1395 | } |
| 1396 | snprintf(cmd, sizeof cmd, "%s -q -", |
| 1397 | options.xauth_location); |
| 1398 | f = popen(cmd, "w"); |
| 1399 | if (f) { |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1400 | fprintf(f, "add %s %s %s\n", |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1401 | s->auth_display, s->auth_proto, |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1402 | s->auth_data); |
Ben Lindstrom | 86fe868 | 2001-03-17 00:32:57 +0000 | [diff] [blame] | 1403 | pclose(f); |
| 1404 | } else { |
| 1405 | fprintf(stderr, "Could not run %s\n", |
| 1406 | cmd); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1407 | } |
| 1408 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1409 | /* Get the last component of the shell name. */ |
| 1410 | cp = strrchr(shell, '/'); |
| 1411 | if (cp) |
| 1412 | cp++; |
| 1413 | else |
| 1414 | cp = shell; |
| 1415 | } |
Ben Lindstrom | de71cda | 2001-03-24 00:43:26 +0000 | [diff] [blame] | 1416 | |
| 1417 | /* restore SIGPIPE for child */ |
| 1418 | signal(SIGPIPE, SIG_DFL); |
| 1419 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1420 | /* |
| 1421 | * If we have no command, execute the shell. In this case, the shell |
| 1422 | * name to be passed in argv[0] is preceded by '-' to indicate that |
| 1423 | * this is a login shell. |
| 1424 | */ |
| 1425 | if (!command) { |
| 1426 | if (!options.use_login) { |
| 1427 | char buf[256]; |
| 1428 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1429 | /* Start the shell. Set initial character to '-'. */ |
| 1430 | buf[0] = '-'; |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 1431 | strlcpy(buf + 1, cp, sizeof(buf) - 1); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1432 | |
| 1433 | /* Execute the shell. */ |
| 1434 | argv[0] = buf; |
| 1435 | argv[1] = NULL; |
| 1436 | execve(shell, argv, env); |
| 1437 | |
| 1438 | /* Executing the shell failed. */ |
| 1439 | perror(shell); |
| 1440 | exit(1); |
| 1441 | |
| 1442 | } else { |
| 1443 | /* Launch login(1). */ |
| 1444 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1445 | execl(LOGIN_PROGRAM, "login", "-h", hostname, |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 1446 | #ifdef LOGIN_NEEDS_TERM |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1447 | s->term? s->term : "unknown", |
Ben Lindstrom | 97c677d | 2001-05-08 20:33:05 +0000 | [diff] [blame] | 1448 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1449 | "-p", "-f", "--", pw->pw_name, (char *)NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1450 | |
| 1451 | /* Login couldn't be executed, die. */ |
| 1452 | |
| 1453 | perror("login"); |
| 1454 | exit(1); |
| 1455 | } |
| 1456 | } |
| 1457 | /* |
| 1458 | * Execute the command using the user's shell. This uses the -c |
| 1459 | * option to execute the command. |
| 1460 | */ |
| 1461 | argv[0] = (char *) cp; |
| 1462 | argv[1] = "-c"; |
| 1463 | argv[2] = (char *) command; |
| 1464 | argv[3] = NULL; |
| 1465 | execve(shell, argv, env); |
| 1466 | perror(shell); |
| 1467 | exit(1); |
| 1468 | } |
| 1469 | |
| 1470 | Session * |
| 1471 | session_new(void) |
| 1472 | { |
| 1473 | int i; |
| 1474 | static int did_init = 0; |
| 1475 | if (!did_init) { |
| 1476 | debug("session_new: init"); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1477 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1478 | sessions[i].used = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1479 | } |
| 1480 | did_init = 1; |
| 1481 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1482 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1483 | Session *s = &sessions[i]; |
| 1484 | if (! s->used) { |
Ben Lindstrom | 6029432 | 2001-03-26 05:38:25 +0000 | [diff] [blame] | 1485 | memset(s, 0, sizeof(*s)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1486 | s->chanid = -1; |
| 1487 | s->ptyfd = -1; |
| 1488 | s->ttyfd = -1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1489 | s->used = 1; |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1490 | s->self = i; |
Damien Miller | 15b2952 | 2000-10-14 12:33:48 +1100 | [diff] [blame] | 1491 | debug("session_new: session %d", i); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1492 | return s; |
| 1493 | } |
| 1494 | } |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1498 | static void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1499 | session_dump(void) |
| 1500 | { |
| 1501 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1502 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1503 | Session *s = &sessions[i]; |
| 1504 | debug("dump: used %d session %d %p channel %d pid %d", |
| 1505 | s->used, |
| 1506 | s->self, |
| 1507 | s, |
| 1508 | s->chanid, |
| 1509 | s->pid); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1513 | int |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1514 | session_open(Authctxt *authctxt, int chanid) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1515 | { |
| 1516 | Session *s = session_new(); |
| 1517 | debug("session_open: channel %d", chanid); |
| 1518 | if (s == NULL) { |
| 1519 | error("no more sessions"); |
| 1520 | return 0; |
| 1521 | } |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 1522 | s->authctxt = authctxt; |
| 1523 | s->pw = authctxt->pw; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1524 | if (s->pw == NULL) |
Kevin Steves | 43cdef3 | 2001-02-11 14:12:08 +0000 | [diff] [blame] | 1525 | fatal("no user for session %d", s->self); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1526 | debug("session_open: session %d: link with channel %d", s->self, chanid); |
| 1527 | s->chanid = chanid; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1528 | return 1; |
| 1529 | } |
| 1530 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1531 | static Session * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1532 | session_by_channel(int id) |
| 1533 | { |
| 1534 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1535 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1536 | Session *s = &sessions[i]; |
| 1537 | if (s->used && s->chanid == id) { |
| 1538 | debug("session_by_channel: session %d channel %d", i, id); |
| 1539 | return s; |
| 1540 | } |
| 1541 | } |
| 1542 | debug("session_by_channel: unknown channel %d", id); |
| 1543 | session_dump(); |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1547 | static Session * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1548 | session_by_pid(pid_t pid) |
| 1549 | { |
| 1550 | int i; |
| 1551 | debug("session_by_pid: pid %d", pid); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1552 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1553 | Session *s = &sessions[i]; |
| 1554 | if (s->used && s->pid == pid) |
| 1555 | return s; |
| 1556 | } |
| 1557 | error("session_by_pid: unknown pid %d", pid); |
| 1558 | session_dump(); |
| 1559 | return NULL; |
| 1560 | } |
| 1561 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1562 | static int |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1563 | session_window_change_req(Session *s) |
| 1564 | { |
| 1565 | s->col = packet_get_int(); |
| 1566 | s->row = packet_get_int(); |
| 1567 | s->xpixel = packet_get_int(); |
| 1568 | s->ypixel = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1569 | packet_check_eom(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1570 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1571 | return 1; |
| 1572 | } |
| 1573 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1574 | static int |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1575 | session_pty_req(Session *s) |
| 1576 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1577 | u_int len; |
Ben Lindstrom | ae8e2d3 | 2001-04-14 23:13:02 +0000 | [diff] [blame] | 1578 | int n_bytes; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1579 | |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1580 | if (no_pty_flag) { |
| 1581 | debug("Allocating a pty not permitted for this authentication."); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1582 | return 0; |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1583 | } |
| 1584 | if (s->ttyfd != -1) { |
| 1585 | packet_disconnect("Protocol error: you already have a pty."); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1586 | return 0; |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1589 | s->term = packet_get_string(&len); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1590 | |
| 1591 | if (compat20) { |
| 1592 | s->col = packet_get_int(); |
| 1593 | s->row = packet_get_int(); |
| 1594 | } else { |
| 1595 | s->row = packet_get_int(); |
| 1596 | s->col = packet_get_int(); |
| 1597 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1598 | s->xpixel = packet_get_int(); |
| 1599 | s->ypixel = packet_get_int(); |
| 1600 | |
| 1601 | if (strcmp(s->term, "") == 0) { |
| 1602 | xfree(s->term); |
| 1603 | s->term = NULL; |
| 1604 | } |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1605 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1606 | /* Allocate a pty and open it. */ |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1607 | debug("Allocating pty."); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1608 | if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) { |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1609 | if (s->term) |
| 1610 | xfree(s->term); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1611 | s->term = NULL; |
| 1612 | s->ptyfd = -1; |
| 1613 | s->ttyfd = -1; |
| 1614 | error("session_pty_req: session %d alloc failed", s->self); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1615 | return 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1616 | } |
| 1617 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* for SSH1 the tty modes length is not given */ |
| 1620 | if (!compat20) |
| 1621 | n_bytes = packet_remaining(); |
| 1622 | tty_parse_modes(s->ttyfd, &n_bytes); |
| 1623 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1624 | /* |
| 1625 | * Add a cleanup function to clear the utmp entry and record logout |
| 1626 | * time in case we call fatal() (e.g., the connection gets closed). |
| 1627 | */ |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1628 | fatal_add_cleanup(session_pty_cleanup, (void *)s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1629 | pty_setowner(s->pw, s->tty); |
Ben Lindstrom | 49c1260 | 2001-06-13 04:37:36 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* Set window size from the packet. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1632 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
| 1633 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1634 | packet_check_eom(); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1635 | session_proctitle(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1636 | return 1; |
| 1637 | } |
| 1638 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1639 | static int |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1640 | session_subsystem_req(Session *s) |
| 1641 | { |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1642 | struct stat st; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1643 | u_int len; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1644 | int success = 0; |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1645 | char *cmd, *subsys = packet_get_string(&len); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1646 | int i; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1647 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1648 | packet_check_eom(); |
Damien Miller | baa0870 | 2002-02-05 12:14:10 +1100 | [diff] [blame] | 1649 | log("subsystem request for %.100s", subsys); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1650 | |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1651 | for (i = 0; i < options.num_subsystems; i++) { |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1652 | if (strcmp(subsys, options.subsystem_name[i]) == 0) { |
| 1653 | cmd = options.subsystem_command[i]; |
| 1654 | if (stat(cmd, &st) < 0) { |
| 1655 | error("subsystem: cannot stat %s: %s", cmd, |
| 1656 | strerror(errno)); |
| 1657 | break; |
| 1658 | } |
| 1659 | debug("subsystem: exec() %s", cmd); |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 1660 | s->is_subsystem = 1; |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1661 | do_exec(s, cmd); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1662 | success = 1; |
Damien Miller | 5fab4b9 | 2002-02-05 12:15:07 +1100 | [diff] [blame] | 1663 | break; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | if (!success) |
Damien Miller | baa0870 | 2002-02-05 12:14:10 +1100 | [diff] [blame] | 1668 | log("subsystem request for %.100s failed, subsystem not found", |
Damien Miller | ae45246 | 2001-10-10 15:08:06 +1000 | [diff] [blame] | 1669 | subsys); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1670 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1671 | xfree(subsys); |
| 1672 | return success; |
| 1673 | } |
| 1674 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1675 | static int |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1676 | session_x11_req(Session *s) |
| 1677 | { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1678 | int success; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1679 | |
| 1680 | s->single_connection = packet_get_char(); |
| 1681 | s->auth_proto = packet_get_string(NULL); |
| 1682 | s->auth_data = packet_get_string(NULL); |
| 1683 | s->screen = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1684 | packet_check_eom(); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1685 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1686 | success = session_setup_x11fwd(s); |
| 1687 | if (!success) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1688 | xfree(s->auth_proto); |
| 1689 | xfree(s->auth_data); |
Ben Lindstrom | 88259fb | 2001-06-12 00:21:34 +0000 | [diff] [blame] | 1690 | s->auth_proto = NULL; |
| 1691 | s->auth_data = NULL; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1692 | } |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1693 | return success; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1694 | } |
| 1695 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1696 | static int |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1697 | session_shell_req(Session *s) |
| 1698 | { |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1699 | packet_check_eom(); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 1700 | do_exec(s, NULL); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1701 | return 1; |
| 1702 | } |
| 1703 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1704 | static int |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1705 | session_exec_req(Session *s) |
| 1706 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1707 | u_int len; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1708 | char *command = packet_get_string(&len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1709 | packet_check_eom(); |
Ben Lindstrom | c85ab8a | 2001-06-21 03:13:10 +0000 | [diff] [blame] | 1710 | do_exec(s, command); |
Ben Lindstrom | 0a7ca6c | 2001-06-21 03:17:42 +0000 | [diff] [blame] | 1711 | xfree(command); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1712 | return 1; |
| 1713 | } |
| 1714 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1715 | static int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1716 | session_auth_agent_req(Session *s) |
| 1717 | { |
| 1718 | static int called = 0; |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1719 | packet_check_eom(); |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 1720 | if (no_agent_forwarding_flag) { |
| 1721 | debug("session_auth_agent_req: no_agent_forwarding_flag"); |
| 1722 | return 0; |
| 1723 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1724 | if (called) { |
| 1725 | return 0; |
| 1726 | } else { |
| 1727 | called = 1; |
| 1728 | return auth_input_request_forwarding(s->pw); |
| 1729 | } |
| 1730 | } |
| 1731 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 1732 | int |
| 1733 | session_input_channel_req(Channel *c, const char *rtype) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1734 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1735 | int success = 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1736 | Session *s; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1737 | |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 1738 | if ((s = session_by_channel(c->self)) == NULL) { |
| 1739 | log("session_input_channel_req: no session %d req %.100s", |
| 1740 | c->self, rtype); |
| 1741 | return 0; |
| 1742 | } |
| 1743 | debug("session_input_channel_req: session %d req %s", s->self, rtype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1744 | |
| 1745 | /* |
Ben Lindstrom | fc9b07d | 2001-03-22 01:27:23 +0000 | [diff] [blame] | 1746 | * a session is in LARVAL state until a shell, a command |
| 1747 | * or a subsystem is executed |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1748 | */ |
| 1749 | if (c->type == SSH_CHANNEL_LARVAL) { |
| 1750 | if (strcmp(rtype, "shell") == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1751 | success = session_shell_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1752 | } else if (strcmp(rtype, "exec") == 0) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 1753 | success = session_exec_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1754 | } else if (strcmp(rtype, "pty-req") == 0) { |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 1755 | success = session_pty_req(s); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1756 | } else if (strcmp(rtype, "x11-req") == 0) { |
| 1757 | success = session_x11_req(s); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1758 | } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { |
| 1759 | success = session_auth_agent_req(s); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1760 | } else if (strcmp(rtype, "subsystem") == 0) { |
| 1761 | success = session_subsystem_req(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | if (strcmp(rtype, "window-change") == 0) { |
| 1765 | success = session_window_change_req(s); |
| 1766 | } |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 1767 | return success; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | void |
| 1771 | session_set_fds(Session *s, int fdin, int fdout, int fderr) |
| 1772 | { |
| 1773 | if (!compat20) |
| 1774 | fatal("session_set_fds: called for proto != 2.0"); |
| 1775 | /* |
| 1776 | * now that have a child and a pipe to the child, |
| 1777 | * we can activate our channel and register the fd's |
| 1778 | */ |
| 1779 | if (s->chanid == -1) |
| 1780 | fatal("no channel for session %d", s->self); |
| 1781 | channel_set_fds(s->chanid, |
| 1782 | fdout, fdin, fderr, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1783 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, |
| 1784 | 1); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1785 | } |
| 1786 | |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1787 | /* |
| 1788 | * Function to perform pty cleanup. Also called if we get aborted abnormally |
| 1789 | * (e.g., due to a dropped connection). |
| 1790 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1791 | static void |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1792 | session_pty_cleanup(void *session) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1793 | { |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1794 | Session *s = session; |
| 1795 | |
| 1796 | if (s == NULL) { |
| 1797 | error("session_pty_cleanup: no session"); |
| 1798 | return; |
| 1799 | } |
| 1800 | if (s->ttyfd == -1) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1801 | return; |
| 1802 | |
Kevin Steves | 43cdef3 | 2001-02-11 14:12:08 +0000 | [diff] [blame] | 1803 | debug("session_pty_cleanup: session %d release %s", s->self, s->tty); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1804 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1805 | /* Record that the user has logged out. */ |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1806 | if (s->pid != 0) |
| 1807 | record_logout(s->pid, s->tty); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1808 | |
| 1809 | /* Release the pseudo-tty. */ |
| 1810 | pty_release(s->tty); |
| 1811 | |
| 1812 | /* |
| 1813 | * Close the server side of the socket pairs. We must do this after |
| 1814 | * the pty cleanup, so that another process doesn't get this pty |
| 1815 | * while we're still cleaning up. |
| 1816 | */ |
| 1817 | if (close(s->ptymaster) < 0) |
| 1818 | error("close(s->ptymaster): %s", strerror(errno)); |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 1819 | |
| 1820 | /* unlink pty from session */ |
| 1821 | s->ttyfd = -1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1822 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1823 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1824 | static void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1825 | session_exit_message(Session *s, int status) |
| 1826 | { |
| 1827 | Channel *c; |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 1828 | |
| 1829 | if ((c = channel_lookup(s->chanid)) == NULL) |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1830 | fatal("session_exit_message: session %d: no channel %d", |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1831 | s->self, s->chanid); |
| 1832 | debug("session_exit_message: session %d channel %d pid %d", |
| 1833 | s->self, s->chanid, s->pid); |
| 1834 | |
| 1835 | if (WIFEXITED(status)) { |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 1836 | channel_request_start(s->chanid, "exit-status", 0); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1837 | packet_put_int(WEXITSTATUS(status)); |
| 1838 | packet_send(); |
| 1839 | } else if (WIFSIGNALED(status)) { |
Damien Miller | f3dcf1f | 2002-02-08 22:06:48 +1100 | [diff] [blame] | 1840 | channel_request_start(s->chanid, "exit-signal", 0); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1841 | packet_put_int(WTERMSIG(status)); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 1842 | #ifdef WCOREDUMP |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1843 | packet_put_char(WCOREDUMP(status)); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 1844 | #else /* WCOREDUMP */ |
| 1845 | packet_put_char(0); |
| 1846 | #endif /* WCOREDUMP */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1847 | packet_put_cstring(""); |
| 1848 | packet_put_cstring(""); |
| 1849 | packet_send(); |
| 1850 | } else { |
| 1851 | /* Some weird exit cause. Just exit. */ |
| 1852 | packet_disconnect("wait returned status %04x.", status); |
| 1853 | } |
| 1854 | |
| 1855 | /* disconnect channel */ |
| 1856 | debug("session_exit_message: release channel %d", s->chanid); |
| 1857 | channel_cancel_cleanup(s->chanid); |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 1858 | /* |
| 1859 | * emulate a write failure with 'chan_write_failed', nobody will be |
| 1860 | * interested in data we write. |
| 1861 | * Note that we must not call 'chan_read_failed', since there could |
| 1862 | * be some more data waiting in the pipe. |
| 1863 | */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1864 | if (c->ostate != CHAN_OUTPUT_CLOSED) |
| 1865 | chan_write_failed(c); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1866 | s->chanid = -1; |
| 1867 | } |
| 1868 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1869 | static void |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1870 | session_close(Session *s) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1871 | { |
Ben Lindstrom | 7eaf8e4 | 2001-06-13 04:35:43 +0000 | [diff] [blame] | 1872 | debug("session_close: session %d pid %d", s->self, s->pid); |
| 1873 | if (s->ttyfd != -1) { |
| 1874 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); |
| 1875 | session_pty_cleanup(s); |
| 1876 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1877 | if (s->term) |
| 1878 | xfree(s->term); |
| 1879 | if (s->display) |
| 1880 | xfree(s->display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 1881 | if (s->auth_display) |
| 1882 | xfree(s->auth_display); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1883 | if (s->auth_data) |
| 1884 | xfree(s->auth_data); |
| 1885 | if (s->auth_proto) |
| 1886 | xfree(s->auth_proto); |
| 1887 | s->used = 0; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1888 | session_proctitle(s); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | void |
| 1892 | session_close_by_pid(pid_t pid, int status) |
| 1893 | { |
| 1894 | Session *s = session_by_pid(pid); |
| 1895 | if (s == NULL) { |
Ben Lindstrom | 7a83722 | 2001-06-13 19:23:32 +0000 | [diff] [blame] | 1896 | debug("session_close_by_pid: no session for pid %d", pid); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1897 | return; |
| 1898 | } |
| 1899 | if (s->chanid != -1) |
| 1900 | session_exit_message(s, status); |
| 1901 | session_close(s); |
| 1902 | } |
| 1903 | |
| 1904 | /* |
| 1905 | * this is called when a channel dies before |
| 1906 | * the session 'child' itself dies |
| 1907 | */ |
| 1908 | void |
| 1909 | session_close_by_channel(int id, void *arg) |
| 1910 | { |
| 1911 | Session *s = session_by_channel(id); |
| 1912 | if (s == NULL) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1913 | debug("session_close_by_channel: no session for id %d", id); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1914 | return; |
| 1915 | } |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1916 | debug("session_close_by_channel: channel %d child %d", id, s->pid); |
| 1917 | if (s->pid != 0) { |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1918 | debug("session_close_by_channel: channel %d: has child", id); |
Damien Miller | 0585d51 | 2001-10-12 11:35:50 +1000 | [diff] [blame] | 1919 | /* |
| 1920 | * delay detach of session, but release pty, since |
| 1921 | * the fd's to the child are already closed |
| 1922 | */ |
| 1923 | if (s->ttyfd != -1) { |
| 1924 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); |
| 1925 | session_pty_cleanup(s); |
| 1926 | } |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1927 | return; |
| 1928 | } |
| 1929 | /* detach by removing callback */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1930 | channel_cancel_cleanup(s->chanid); |
| 1931 | s->chanid = -1; |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1932 | session_close(s); |
| 1933 | } |
| 1934 | |
| 1935 | void |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1936 | session_destroy_all(void) |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1937 | { |
| 1938 | int i; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1939 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1940 | Session *s = &sessions[i]; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1941 | if (s->used) |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1942 | session_close(s); |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 1943 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1944 | } |
| 1945 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1946 | static char * |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1947 | session_tty_list(void) |
| 1948 | { |
| 1949 | static char buf[1024]; |
| 1950 | int i; |
| 1951 | buf[0] = '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1952 | for (i = 0; i < MAX_SESSIONS; i++) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1953 | Session *s = &sessions[i]; |
| 1954 | if (s->used && s->ttyfd != -1) { |
| 1955 | if (buf[0] != '\0') |
| 1956 | strlcat(buf, ",", sizeof buf); |
| 1957 | strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf); |
| 1958 | } |
| 1959 | } |
| 1960 | if (buf[0] == '\0') |
| 1961 | strlcpy(buf, "notty", sizeof buf); |
| 1962 | return buf; |
| 1963 | } |
| 1964 | |
| 1965 | void |
| 1966 | session_proctitle(Session *s) |
| 1967 | { |
| 1968 | if (s->pw == NULL) |
| 1969 | error("no user for session %d", s->self); |
| 1970 | else |
| 1971 | setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); |
| 1972 | } |
| 1973 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1974 | int |
| 1975 | session_setup_x11fwd(Session *s) |
| 1976 | { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1977 | struct stat st; |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 1978 | char display[512], auth_display[512]; |
| 1979 | char hostname[MAXHOSTNAMELEN]; |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 1980 | |
| 1981 | if (no_x11_forwarding_flag) { |
| 1982 | packet_send_debug("X11 forwarding disabled in user configuration file."); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | if (!options.x11_forwarding) { |
| 1986 | debug("X11 forwarding disabled in server configuration file."); |
| 1987 | return 0; |
| 1988 | } |
| 1989 | if (!options.xauth_location || |
| 1990 | (stat(options.xauth_location, &st) == -1)) { |
| 1991 | packet_send_debug("No xauth program; cannot forward with spoofing."); |
| 1992 | return 0; |
| 1993 | } |
Ben Lindstrom | 699776e | 2001-06-21 03:14:49 +0000 | [diff] [blame] | 1994 | if (options.use_login) { |
| 1995 | packet_send_debug("X11 forwarding disabled; " |
| 1996 | "not compatible with UseLogin=yes."); |
| 1997 | return 0; |
| 1998 | } |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 1999 | if (s->display != NULL) { |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2000 | debug("X11 display already set."); |
| 2001 | return 0; |
| 2002 | } |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2003 | s->display_number = x11_create_display_inet(options.x11_display_offset, |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 2004 | options.x11_use_localhost, s->single_connection); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2005 | if (s->display_number == -1) { |
Ben Lindstrom | 2bcdf06 | 2001-06-13 04:41:41 +0000 | [diff] [blame] | 2006 | debug("x11_create_display_inet failed."); |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2009 | |
| 2010 | /* Set up a suitable value for the DISPLAY variable. */ |
| 2011 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 2012 | fatal("gethostname: %.100s", strerror(errno)); |
| 2013 | /* |
| 2014 | * auth_display must be used as the displayname when the |
| 2015 | * authorization entry is added with xauth(1). This will be |
| 2016 | * different than the DISPLAY string for localhost displays. |
| 2017 | */ |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 2018 | if (options.x11_use_localhost) { |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2019 | snprintf(display, sizeof display, "localhost:%d.%d", |
| 2020 | s->display_number, s->screen); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2021 | snprintf(auth_display, sizeof auth_display, "unix:%d.%d", |
| 2022 | s->display_number, s->screen); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2023 | s->display = xstrdup(display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2024 | s->auth_display = xstrdup(auth_display); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2025 | } else { |
| 2026 | #ifdef IPADDR_IN_DISPLAY |
| 2027 | struct hostent *he; |
| 2028 | struct in_addr my_addr; |
| 2029 | |
| 2030 | he = gethostbyname(hostname); |
| 2031 | if (he == NULL) { |
| 2032 | error("Can't get IP address for X11 DISPLAY."); |
| 2033 | packet_send_debug("Can't get IP address for X11 DISPLAY."); |
| 2034 | return 0; |
| 2035 | } |
| 2036 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
| 2037 | snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr), |
| 2038 | s->display_number, s->screen); |
| 2039 | #else |
| 2040 | snprintf(display, sizeof display, "%.400s:%d.%d", hostname, |
| 2041 | s->display_number, s->screen); |
| 2042 | #endif |
| 2043 | s->display = xstrdup(display); |
Damien Miller | 512bccb | 2002-02-05 12:11:02 +1100 | [diff] [blame] | 2044 | s->auth_display = xstrdup(display); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Ben Lindstrom | 768176b | 2001-06-09 01:29:12 +0000 | [diff] [blame] | 2047 | return 1; |
| 2048 | } |
| 2049 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2050 | static void |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 2051 | do_authenticated2(Authctxt *authctxt) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2052 | { |
Ben Lindstrom | bddd551 | 2001-07-04 04:53:53 +0000 | [diff] [blame] | 2053 | server_loop2(authctxt); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 2054 | } |