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