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