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