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