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