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