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