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