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