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