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