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