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