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