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