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