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