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