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