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