blob: 3daddbe11a0c2f2e80c8f1817733ce75d4f35b3f [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
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 Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
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 Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Ben Lindstrom2bf56e22002-04-02 20:32:46 +000036RCSID("$OpenBSD: session.c,v 1.133 2002/03/28 15:34:51 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100045#include "mpaux.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "uidswap.h"
47#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000048#include "channels.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100051#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
53#include "log.h"
54#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000055#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "serverloop.h"
57#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000058#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059#include "monitor_wrap.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Damien Millerbac2d8a2000-09-05 16:13:06 +110061#ifdef HAVE_CYGWIN
62#include <windows.h>
63#include <sys/cygwin.h>
64#define is_winnt (GetVersion() < 0x80000000)
65#endif
66
Damien Millerb38eff82000-04-01 11:09:21 +100067/* func */
68
69Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000070void session_set_fds(Session *, int, int, int);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071void session_pty_cleanup(void *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000072void session_proctitle(Session *);
73int session_setup_x11fwd(Session *);
74void do_exec_pty(Session *, const char *);
75void do_exec_no_pty(Session *, const char *);
76void do_exec(Session *, const char *);
77void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +000078#ifdef LOGIN_NEEDS_UTMPX
79static void do_pre_login(Session *s);
80#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +000081void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +100082void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000083int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +100084
Ben Lindstrombba81212001-06-25 05:01:22 +000085static void do_authenticated1(Authctxt *);
86static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000087
Ben Lindstrombba81212001-06-25 05:01:22 +000088static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +000089
Damien Millerb38eff82000-04-01 11:09:21 +100090/* import */
91extern ServerOptions options;
92extern char *__progname;
93extern int log_stderr;
94extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +000095extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +100096extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +000097extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +100098
Damien Miller7b28dc52000-09-05 13:34:53 +110099/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000100const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100101
Damien Millerb38eff82000-04-01 11:09:21 +1000102/* data */
103#define MAX_SESSIONS 10
104Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100105
Damien Millerd2c208a2000-05-17 22:00:02 +1000106#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000107char *aixloginmsg;
108#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000109
Damien Millerad833b32000-08-23 10:46:23 +1000110#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000111login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000112#endif
113
Ben Lindstromb31783d2001-03-22 02:02:12 +0000114void
115do_authenticated(Authctxt *authctxt)
116{
117 /*
118 * Cancel the alarm we set to limit the time taken for
119 * authentication.
120 */
121 alarm(0);
122 if (startup_pipe != -1) {
123 close(startup_pipe);
124 startup_pipe = -1;
125 }
Damien Millere49d0962001-11-13 23:46:18 +1100126#ifdef WITH_AIXAUTHENTICATE
127 /* We don't have a pty yet, so just label the line as "ssh" */
128 if (loginsuccess(authctxt->user,
Damien Millerf3451a22002-02-05 12:40:46 +1100129 get_canonical_hostname(options.verify_reverse_mapping),
Damien Millere49d0962001-11-13 23:46:18 +1100130 "ssh", &aixloginmsg) < 0)
131 aixloginmsg = NULL;
132#endif /* WITH_AIXAUTHENTICATE */
133
Ben Lindstromb31783d2001-03-22 02:02:12 +0000134 /* setup the channel layer */
135 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
136 channel_permit_all_opens();
137
138 if (compat20)
139 do_authenticated2(authctxt);
140 else
141 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000142
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000143 /* remove agent socket */
Ben Lindstrom838394c2001-06-09 01:11:59 +0000144 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000145 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000146#ifdef KRB4
147 if (options.kerberos_ticket_cleanup)
148 krb4_cleanup_proc(authctxt);
149#endif
150#ifdef KRB5
151 if (options.kerberos_ticket_cleanup)
152 krb5_cleanup_proc(authctxt);
153#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000154}
155
Damien Millerb38eff82000-04-01 11:09:21 +1000156/*
Damien Millerb38eff82000-04-01 11:09:21 +1000157 * Prepares for an interactive session. This is called after the user has
158 * been successfully authenticated. During this message exchange, pseudo
159 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
160 * are requested, etc.
161 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000163do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000164{
165 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000166 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100167 int success, type, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000168 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000169 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000170
171 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000172 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000173 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000174
Damien Millerb38eff82000-04-01 11:09:21 +1000175 /*
176 * We stay in this loop until the client requests to execute a shell
177 * or a command.
178 */
179 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000180 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000181
182 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100183 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000184
185 /* Process the packet. */
186 switch (type) {
187 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000188 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100189 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000190 if (compression_level < 1 || compression_level > 9) {
191 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100192 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000193 break;
194 }
195 /* Enable compression after we have responded with SUCCESS. */
196 enable_compression_after_reply = 1;
197 success = 1;
198 break;
199
200 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000201 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000202 break;
203
204 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000205 s->auth_proto = packet_get_string(&proto_len);
206 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000207
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000208 screen_flag = packet_get_protocol_flags() &
209 SSH_PROTOFLAG_SCREEN_NUMBER;
210 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
211
212 if (packet_remaining() == 4) {
213 if (!screen_flag)
214 debug2("Buggy client: "
215 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000216 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000217 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000218 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000219 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100220 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000221 success = session_setup_x11fwd(s);
222 if (!success) {
223 xfree(s->auth_proto);
224 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000225 s->auth_proto = NULL;
226 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000227 }
Damien Millerb38eff82000-04-01 11:09:21 +1000228 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000229
230 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
231 if (no_agent_forwarding_flag || compat13) {
232 debug("Authentication agent forwarding not permitted for this authentication.");
233 break;
234 }
235 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000236 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000237 break;
238
239 case SSH_CMSG_PORT_FORWARD_REQUEST:
240 if (no_port_forwarding_flag) {
241 debug("Port forwarding not permitted for this authentication.");
242 break;
243 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100244 if (!options.allow_tcp_forwarding) {
245 debug("Port forwarding not permitted.");
246 break;
247 }
Damien Millerb38eff82000-04-01 11:09:21 +1000248 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000249 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000250 success = 1;
251 break;
252
253 case SSH_CMSG_MAX_PACKET_SIZE:
254 if (packet_set_maxsize(packet_get_int()) > 0)
255 success = 1;
256 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100257
Ben Lindstromec95ed92001-07-04 04:21:14 +0000258#if defined(AFS) || defined(KRB5)
259 case SSH_CMSG_HAVE_KERBEROS_TGT:
260 if (!options.kerberos_tgt_passing) {
261 verbose("Kerberos TGT passing disabled.");
262 } else {
263 char *kdata = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100264 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100265
Ben Lindstromec95ed92001-07-04 04:21:14 +0000266 /* XXX - 0x41, see creds_to_radix version */
267 if (kdata[0] != 0x41) {
268#ifdef KRB5
269 krb5_data tgt;
270 tgt.data = kdata;
271 tgt.length = dlen;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100272
Ben Lindstromec95ed92001-07-04 04:21:14 +0000273 if (auth_krb5_tgt(s->authctxt, &tgt))
274 success = 1;
275 else
276 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
277#endif /* KRB5 */
278 } else {
279#ifdef AFS
280 if (auth_krb4_tgt(s->authctxt, kdata))
281 success = 1;
282 else
283 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
284#endif /* AFS */
285 }
286 xfree(kdata);
287 }
288 break;
289#endif /* AFS || KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100290
Ben Lindstromec95ed92001-07-04 04:21:14 +0000291#ifdef AFS
292 case SSH_CMSG_HAVE_AFS_TOKEN:
293 if (!options.afs_token_passing || !k_hasafs()) {
294 verbose("AFS token passing disabled.");
295 } else {
296 /* Accept AFS token. */
297 char *token = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100298 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100299
Ben Lindstromec95ed92001-07-04 04:21:14 +0000300 if (auth_afs_token(s->authctxt, token))
301 success = 1;
302 else
303 verbose("AFS token refused for %.100s",
304 s->authctxt->user);
305 xfree(token);
306 }
307 break;
308#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000309
310 case SSH_CMSG_EXEC_SHELL:
311 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000312 if (type == SSH_CMSG_EXEC_CMD) {
313 command = packet_get_string(&dlen);
314 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000315 do_exec(s, command);
316 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000317 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000318 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000319 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100320 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000321 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000322 return;
323
324 default:
325 /*
326 * Any unknown messages in this phase are ignored,
327 * and a failure message is returned.
328 */
329 log("Unknown packet type received after authentication: %d", type);
330 }
331 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
332 packet_send();
333 packet_write_wait();
334
335 /* Enable compression now that we have replied if appropriate. */
336 if (enable_compression_after_reply) {
337 enable_compression_after_reply = 0;
338 packet_start_compression(compression_level);
339 }
340 }
341}
342
343/*
344 * This is called to fork and execute a command when we have no tty. This
345 * will call do_child from the child, and server_loop from the parent after
346 * setting up file descriptors and such.
347 */
Damien Miller4af51302000-04-16 11:18:38 +1000348void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000349do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000350{
351 int pid;
352
353#ifdef USE_PIPES
354 int pin[2], pout[2], perr[2];
355 /* Allocate pipes for communicating with the program. */
356 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
357 packet_disconnect("Could not create pipes: %.100s",
358 strerror(errno));
359#else /* USE_PIPES */
360 int inout[2], err[2];
361 /* Uses socket pairs to communicate with the program. */
362 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
363 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
364 packet_disconnect("Could not create socket pairs: %.100s",
365 strerror(errno));
366#endif /* USE_PIPES */
367 if (s == NULL)
368 fatal("do_exec_no_pty: no session");
369
Damien Millere247cc42000-05-07 12:03:14 +1000370 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000371
Damien Millerc5946332001-02-28 11:46:11 +1100372#if defined(USE_PAM)
Damien Miller665af9c2001-06-27 09:34:15 +1000373 do_pam_session(s->pw->pw_name, NULL);
Damien Millerf9e93002001-03-27 16:12:24 +1000374 do_pam_setcred(1);
Damien Millerc3aa3dd2001-10-28 22:34:52 +1100375 if (is_pam_password_change_required())
376 packet_disconnect("Password change required but no "
377 "TTY available");
Kevin Stevesff793a22001-02-21 16:36:51 +0000378#endif /* USE_PAM */
379
Damien Millerb38eff82000-04-01 11:09:21 +1000380 /* Fork the child. */
381 if ((pid = fork()) == 0) {
382 /* Child. Reinitialize the log since the pid has changed. */
383 log_init(__progname, options.log_level, options.log_facility, log_stderr);
384
385 /*
386 * Create a new session and process group since the 4.4BSD
387 * setlogin() affects the entire process group.
388 */
389 if (setsid() < 0)
390 error("setsid failed: %.100s", strerror(errno));
391
392#ifdef USE_PIPES
393 /*
394 * Redirect stdin. We close the parent side of the socket
395 * pair, and make the child side the standard input.
396 */
397 close(pin[1]);
398 if (dup2(pin[0], 0) < 0)
399 perror("dup2 stdin");
400 close(pin[0]);
401
402 /* Redirect stdout. */
403 close(pout[0]);
404 if (dup2(pout[1], 1) < 0)
405 perror("dup2 stdout");
406 close(pout[1]);
407
408 /* Redirect stderr. */
409 close(perr[0]);
410 if (dup2(perr[1], 2) < 0)
411 perror("dup2 stderr");
412 close(perr[1]);
413#else /* USE_PIPES */
414 /*
415 * Redirect stdin, stdout, and stderr. Stdin and stdout will
416 * use the same socket, as some programs (particularly rdist)
417 * seem to depend on it.
418 */
419 close(inout[1]);
420 close(err[1]);
421 if (dup2(inout[0], 0) < 0) /* stdin */
422 perror("dup2 stdin");
423 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
424 perror("dup2 stdout");
425 if (dup2(err[0], 2) < 0) /* stderr */
426 perror("dup2 stderr");
427#endif /* USE_PIPES */
428
429 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000430 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000431 /* NOTREACHED */
432 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100433#ifdef HAVE_CYGWIN
434 if (is_winnt)
435 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
436#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000437 if (pid < 0)
438 packet_disconnect("fork failed: %.100s", strerror(errno));
439 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000440 /* Set interactive/non-interactive mode. */
441 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000442#ifdef USE_PIPES
443 /* We are the parent. Close the child sides of the pipes. */
444 close(pin[0]);
445 close(pout[1]);
446 close(perr[1]);
447
Damien Millerefb4afe2000-04-12 18:45:05 +1000448 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000449 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000450 } else {
451 /* Enter the interactive session. */
452 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000453 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000454 }
Damien Millerb38eff82000-04-01 11:09:21 +1000455#else /* USE_PIPES */
456 /* We are the parent. Close the child sides of the socket pairs. */
457 close(inout[0]);
458 close(err[0]);
459
460 /*
461 * Enter the interactive session. Note: server_loop must be able to
462 * handle the case that fdin and fdout are the same.
463 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000464 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000465 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000466 } else {
467 server_loop(pid, inout[1], inout[1], err[1]);
468 /* server_loop has closed inout[1] and err[1]. */
469 }
Damien Millerb38eff82000-04-01 11:09:21 +1000470#endif /* USE_PIPES */
471}
472
473/*
474 * This is called to fork and execute a command when we have a tty. This
475 * will call do_child from the child, and server_loop from the parent after
476 * setting up file descriptors, controlling tty, updating wtmp, utmp,
477 * lastlog, and other such operations.
478 */
Damien Miller4af51302000-04-16 11:18:38 +1000479void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000480do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000481{
Damien Millerb38eff82000-04-01 11:09:21 +1000482 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000483 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000484
485 if (s == NULL)
486 fatal("do_exec_pty: no session");
487 ptyfd = s->ptyfd;
488 ttyfd = s->ttyfd;
489
Damien Millerc5946332001-02-28 11:46:11 +1100490#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000491 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000492 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100493#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000494
Damien Millerb38eff82000-04-01 11:09:21 +1000495 /* Fork the child. */
496 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000497
Damien Miller942da032000-08-18 13:59:06 +1000498 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000499 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000500 /* Close the master side of the pseudo tty. */
501 close(ptyfd);
502
503 /* Make the pseudo tty our controlling tty. */
504 pty_make_controlling_tty(&ttyfd, s->tty);
505
Damien Miller9c751422001-10-10 15:02:46 +1000506 /* Redirect stdin/stdout/stderr from the pseudo tty. */
507 if (dup2(ttyfd, 0) < 0)
508 error("dup2 stdin: %s", strerror(errno));
509 if (dup2(ttyfd, 1) < 0)
510 error("dup2 stdout: %s", strerror(errno));
511 if (dup2(ttyfd, 2) < 0)
512 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000513
514 /* Close the extra descriptor for the pseudo tty. */
515 close(ttyfd);
516
Damien Miller942da032000-08-18 13:59:06 +1000517 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000518#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100519 if (!(options.use_login && command == NULL))
520 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000521# ifdef LOGIN_NEEDS_UTMPX
522 else
523 do_pre_login(s);
524# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000525#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000526
Damien Millerb38eff82000-04-01 11:09:21 +1000527 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000528 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000529 /* NOTREACHED */
530 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100531#ifdef HAVE_CYGWIN
532 if (is_winnt)
533 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
534#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000535 if (pid < 0)
536 packet_disconnect("fork failed: %.100s", strerror(errno));
537 s->pid = pid;
538
539 /* Parent. Close the slave side of the pseudo tty. */
540 close(ttyfd);
541
542 /*
543 * Create another descriptor of the pty master side for use as the
544 * standard input. We could use the original descriptor, but this
545 * simplifies code in server_loop. The descriptor is bidirectional.
546 */
547 fdout = dup(ptyfd);
548 if (fdout < 0)
549 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
550
551 /* we keep a reference to the pty master */
552 ptymaster = dup(ptyfd);
553 if (ptymaster < 0)
554 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
555 s->ptymaster = ptymaster;
556
557 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000558 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000559 if (compat20) {
560 session_set_fds(s, ptyfd, fdout, -1);
561 } else {
562 server_loop(pid, ptyfd, fdout, -1);
563 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000564 }
Damien Millerb38eff82000-04-01 11:09:21 +1000565}
566
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000567#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000568static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000569do_pre_login(Session *s)
570{
571 socklen_t fromlen;
572 struct sockaddr_storage from;
573 pid_t pid = getpid();
574
575 /*
576 * Get IP address of client. If the connection is not a socket, let
577 * the address be 0.0.0.0.
578 */
579 memset(&from, 0, sizeof(from));
580 if (packet_connection_is_on_socket()) {
581 fromlen = sizeof(from);
582 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100583 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000584 debug("getpeername: %.100s", strerror(errno));
585 fatal_cleanup();
586 }
587 }
588
589 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Millerf3451a22002-02-05 12:40:46 +1100590 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000591 (struct sockaddr *)&from);
592}
593#endif
594
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000595/*
596 * This is called to fork and execute a command. If another command is
597 * to be forced, execute that instead.
598 */
599void
600do_exec(Session *s, const char *command)
601{
602 if (forced_command) {
603 original_command = command;
604 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000605 debug("Forced command '%.900s'", command);
606 }
607
608 if (s->ttyfd != -1)
609 do_exec_pty(s, command);
610 else
611 do_exec_no_pty(s, command);
612
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000613 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000614}
615
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000616
Damien Miller942da032000-08-18 13:59:06 +1000617/* administrative, login(1)-like work */
618void
Damien Miller69b69aa2000-10-28 14:19:58 +1100619do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000620{
Damien Miller942da032000-08-18 13:59:06 +1000621 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100622 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000623 socklen_t fromlen;
624 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000625 time_t last_login_time;
626 struct passwd * pw = s->pw;
627 pid_t pid = getpid();
628
629 /*
630 * Get IP address of client. If the connection is not a socket, let
631 * the address be 0.0.0.0.
632 */
633 memset(&from, 0, sizeof(from));
634 if (packet_connection_is_on_socket()) {
635 fromlen = sizeof(from);
636 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000637 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000638 debug("getpeername: %.100s", strerror(errno));
639 fatal_cleanup();
640 }
641 }
642
Damien Miller7b28dc52000-09-05 13:34:53 +1100643 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000644 if (options.print_lastlog) {
645 hostname[0] = '\0';
646 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
647 hostname, sizeof(hostname));
648 }
Damien Millere4340be2000-09-16 13:29:08 +1100649
Damien Miller942da032000-08-18 13:59:06 +1000650 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000651 if (!use_privsep)
652 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
653 get_remote_name_or_ip(utmp_len,
654 options.verify_reverse_mapping),
655 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000656
Kevin Steves092f2ef2000-10-14 13:36:13 +0000657#ifdef USE_PAM
658 /*
659 * If password change is needed, do it now.
660 * This needs to occur before the ~/.hushlogin check.
661 */
Damien Miller646aa602001-02-15 11:51:32 +1100662 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000663 print_pam_messages();
664 do_pam_chauthtok();
665 }
666#endif
667
Damien Millercf205e82001-04-16 18:29:15 +1000668 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000669 return;
670
671#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100672 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000673 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000674#endif /* USE_PAM */
675#ifdef WITH_AIXAUTHENTICATE
676 if (aixloginmsg && *aixloginmsg)
677 printf("%s\n", aixloginmsg);
678#endif /* WITH_AIXAUTHENTICATE */
679
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000680 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000681 time_string = ctime(&last_login_time);
682 if (strchr(time_string, '\n'))
683 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000684 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000685 printf("Last login: %s\r\n", time_string);
686 else
Damien Millere4340be2000-09-16 13:29:08 +1100687 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000688 }
Damien Millercf205e82001-04-16 18:29:15 +1000689
690 do_motd();
691}
692
693/*
694 * Display the message of the day.
695 */
696void
697do_motd(void)
698{
699 FILE *f;
700 char buf[256];
701
Damien Miller942da032000-08-18 13:59:06 +1000702 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000703#ifdef HAVE_LOGIN_CAP
704 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
705 "/etc/motd"), "r");
706#else
Damien Miller942da032000-08-18 13:59:06 +1000707 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000708#endif
Damien Miller942da032000-08-18 13:59:06 +1000709 if (f) {
710 while (fgets(buf, sizeof(buf), f))
711 fputs(buf, stdout);
712 fclose(f);
713 }
714 }
715}
716
Kevin Steves8f63caa2001-07-04 18:23:02 +0000717
718/*
719 * Check for quiet login, either .hushlogin or command given.
720 */
721int
722check_quietlogin(Session *s, const char *command)
723{
724 char buf[256];
725 struct passwd *pw = s->pw;
726 struct stat st;
727
728 /* Return 1 if .hushlogin exists or a command given. */
729 if (command != NULL)
730 return 1;
731 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
732#ifdef HAVE_LOGIN_CAP
733 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
734 return 1;
735#else
736 if (stat(buf, &st) >= 0)
737 return 1;
738#endif
739 return 0;
740}
741
Damien Millerb38eff82000-04-01 11:09:21 +1000742/*
743 * Sets the value of the given variable in the environment. If the variable
744 * already exists, its value is overriden.
745 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000746static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000747child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100748 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000749{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000750 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000751 char **env;
752
753 /*
754 * Find the slot where the value should be stored. If the variable
755 * already exists, we reuse the slot; otherwise we append a new slot
756 * at the end of the array, expanding if necessary.
757 */
758 env = *envp;
759 namelen = strlen(name);
760 for (i = 0; env[i]; i++)
761 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
762 break;
763 if (env[i]) {
764 /* Reuse the slot. */
765 xfree(env[i]);
766 } else {
767 /* New variable. Expand if necessary. */
768 if (i >= (*envsizep) - 1) {
769 (*envsizep) += 50;
770 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
771 }
772 /* Need to set the NULL pointer at end of array beyond the new slot. */
773 env[i + 1] = NULL;
774 }
775
776 /* Allocate space and format the variable in the appropriate slot. */
777 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
778 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
779}
780
781/*
782 * Reads environment variables from the given file and adds/overrides them
783 * into the environment. If the file does not exist, this does nothing.
784 * Otherwise, it must consist of empty lines, comments (line starts with '#')
785 * and assignments of the form name=value. No other forms are allowed.
786 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000787static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000788read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100789 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000790{
791 FILE *f;
792 char buf[4096];
793 char *cp, *value;
794
795 f = fopen(filename, "r");
796 if (!f)
797 return;
798
799 while (fgets(buf, sizeof(buf), f)) {
800 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
801 ;
802 if (!*cp || *cp == '#' || *cp == '\n')
803 continue;
804 if (strchr(cp, '\n'))
805 *strchr(cp, '\n') = '\0';
806 value = strchr(cp, '=');
807 if (value == NULL) {
808 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
809 continue;
810 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000811 /*
812 * Replace the equals sign by nul, and advance value to
813 * the value string.
814 */
Damien Millerb38eff82000-04-01 11:09:21 +1000815 *value = '\0';
816 value++;
817 child_set_env(env, envsize, cp, value);
818 }
819 fclose(f);
820}
821
Damien Millerbb9ffc12002-01-08 10:59:32 +1100822void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000823{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100824 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000825 int i;
826
Damien Millerbb9ffc12002-01-08 10:59:32 +1100827 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000828 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000829
Damien Millerbb9ffc12002-01-08 10:59:32 +1100830 for(i = 0; source[i] != NULL; i++) {
831 var_name = xstrdup(source[i]);
832 if ((var_val = strstr(var_name, "=")) == NULL) {
833 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000834 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000835 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100836 *var_val++ = '\0';
837
838 debug3("Copy environment: %s=%s", var_name, var_val);
839 child_set_env(env, envsize, var_name, var_val);
840
841 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000842 }
843}
Damien Millercb5e44a2000-09-29 12:12:36 +1100844
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000845static char **
846do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000847{
Damien Millerb38eff82000-04-01 11:09:21 +1000848 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000849 u_int i, envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000850 char **env;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000851 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000852
Damien Millerb38eff82000-04-01 11:09:21 +1000853 /* Initialize the environment. */
854 envsize = 100;
855 env = xmalloc(envsize * sizeof(char *));
856 env[0] = NULL;
857
Damien Millerbac2d8a2000-09-05 16:13:06 +1100858#ifdef HAVE_CYGWIN
859 /*
860 * The Windows environment contains some setting which are
861 * important for a running system. They must not be dropped.
862 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100863 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100864#endif
865
Damien Millerb38eff82000-04-01 11:09:21 +1000866 if (!options.use_login) {
867 /* Set basic environment. */
868 child_set_env(&env, &envsize, "USER", pw->pw_name);
869 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
870 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000871#ifdef HAVE_LOGIN_CAP
872 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
873 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +1100874#else /* HAVE_LOGIN_CAP */
875# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +1100876 /*
877 * There's no standard path on Windows. The path contains
878 * important components pointing to the system directories,
879 * needed for loading shared libraries. So the path better
880 * remains intact here.
881 */
Damien Millerb38eff82000-04-01 11:09:21 +1000882 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +1100883# endif /* HAVE_CYGWIN */
884#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +1000885
886 snprintf(buf, sizeof buf, "%.200s/%.50s",
887 _PATH_MAILDIR, pw->pw_name);
888 child_set_env(&env, &envsize, "MAIL", buf);
889
890 /* Normal systems set SHELL by default. */
891 child_set_env(&env, &envsize, "SHELL", shell);
892 }
893 if (getenv("TZ"))
894 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
895
896 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000897 if (!options.use_login) {
898 while (custom_environment) {
899 struct envstring *ce = custom_environment;
900 char *s = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000901
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000902 for (i = 0; s[i] != '=' && s[i]; i++)
903 ;
904 if (s[i] == '=') {
905 s[i] = 0;
906 child_set_env(&env, &envsize, s, s + i + 1);
907 }
908 custom_environment = ce->next;
909 xfree(ce->s);
910 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +1000911 }
Damien Millerb38eff82000-04-01 11:09:21 +1000912 }
913
914 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000915 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +1000916 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
917
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000918 if (s->ttyfd != -1)
919 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
920 if (s->term)
921 child_set_env(&env, &envsize, "TERM", s->term);
922 if (s->display)
923 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +1100924 if (original_command)
925 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
926 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +1000927
928#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +0000929 {
930 char *cp;
931
932 if ((cp = getenv("AUTHSTATE")) != NULL)
933 child_set_env(&env, &envsize, "AUTHSTATE", cp);
934 if ((cp = getenv("KRB5CCNAME")) != NULL)
935 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
936 read_environment_file(&env, &envsize, "/etc/environment");
937 }
Damien Millerb38eff82000-04-01 11:09:21 +1000938#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000939#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +0000940 if (s->authctxt->krb4_ticket_file)
941 child_set_env(&env, &envsize, "KRBTKFILE",
942 s->authctxt->krb4_ticket_file);
943#endif
944#ifdef KRB5
945 if (s->authctxt->krb5_ticket_file)
946 child_set_env(&env, &envsize, "KRB5CCNAME",
947 s->authctxt->krb5_ticket_file);
948#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000949#ifdef USE_PAM
950 /* Pull in any environment variables that may have been set by PAM. */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100951 copy_environment(fetch_pam_environment(), &env, &envsize);
Damien Millerb38eff82000-04-01 11:09:21 +1000952#endif /* USE_PAM */
953
Damien Millerb38eff82000-04-01 11:09:21 +1000954 if (auth_get_socket_name() != NULL)
955 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100956 auth_get_socket_name());
Damien Millerb38eff82000-04-01 11:09:21 +1000957
958 /* read $HOME/.ssh/environment. */
959 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000960 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
961 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +1000962 read_environment_file(&env, &envsize, buf);
963 }
964 if (debug_flag) {
965 /* dump the environment */
966 fprintf(stderr, "Environment:\n");
967 for (i = 0; env[i]; i++)
968 fprintf(stderr, " %.200s\n", env[i]);
969 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000970 return env;
971}
972
973/*
974 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
975 * first in this order).
976 */
977static void
978do_rc_files(Session *s, const char *shell)
979{
980 FILE *f = NULL;
981 char cmd[1024];
982 int do_xauth;
983 struct stat st;
984
985 do_xauth =
986 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
987
988 /* ignore _PATH_SSH_USER_RC for subsystems */
989 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
990 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
991 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
992 if (debug_flag)
993 fprintf(stderr, "Running %s\n", cmd);
994 f = popen(cmd, "w");
995 if (f) {
996 if (do_xauth)
997 fprintf(f, "%s %s\n", s->auth_proto,
998 s->auth_data);
999 pclose(f);
1000 } else
1001 fprintf(stderr, "Could not run %s\n",
1002 _PATH_SSH_USER_RC);
1003 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1004 if (debug_flag)
1005 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1006 _PATH_SSH_SYSTEM_RC);
1007 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1008 if (f) {
1009 if (do_xauth)
1010 fprintf(f, "%s %s\n", s->auth_proto,
1011 s->auth_data);
1012 pclose(f);
1013 } else
1014 fprintf(stderr, "Could not run %s\n",
1015 _PATH_SSH_SYSTEM_RC);
1016 } else if (do_xauth && options.xauth_location != NULL) {
1017 /* Add authority data to .Xauthority if appropriate. */
1018 if (debug_flag) {
1019 fprintf(stderr,
1020 "Running %.100s add "
1021 "%.100s %.100s %.100s\n",
1022 options.xauth_location, s->auth_display,
1023 s->auth_proto, s->auth_data);
1024 }
1025 snprintf(cmd, sizeof cmd, "%s -q -",
1026 options.xauth_location);
1027 f = popen(cmd, "w");
1028 if (f) {
1029 fprintf(f, "add %s %s %s\n",
1030 s->auth_display, s->auth_proto,
1031 s->auth_data);
1032 pclose(f);
1033 } else {
1034 fprintf(stderr, "Could not run %s\n",
1035 cmd);
1036 }
1037 }
1038}
1039
1040static void
1041do_nologin(struct passwd *pw)
1042{
1043 FILE *f = NULL;
1044 char buf[1024];
1045
1046#ifdef HAVE_LOGIN_CAP
1047 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1048 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1049 _PATH_NOLOGIN), "r");
1050#else
1051 if (pw->pw_uid)
1052 f = fopen(_PATH_NOLOGIN, "r");
1053#endif
1054 if (f) {
1055 /* /etc/nologin exists. Print its contents and exit. */
1056 while (fgets(buf, sizeof(buf), f))
1057 fputs(buf, stderr);
1058 fclose(f);
1059 exit(254);
1060 }
1061}
1062
1063/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001065do_setusercontext(struct passwd *pw)
1066{
1067#ifdef HAVE_CYGWIN
Ben Lindstrom3107efc2002-02-21 15:37:02 +00001068 if (is_winnt) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001069#else /* HAVE_CYGWIN */
1070 if (getuid() == 0 || geteuid() == 0) {
1071#endif /* HAVE_CYGWIN */
1072#ifdef HAVE_GETUSERATTR
1073 set_limits_from_userattr(pw->pw_name);
1074#endif /* HAVE_GETUSERATTR */
1075#ifdef HAVE_LOGIN_CAP
1076 if (setusercontext(lc, pw, pw->pw_uid,
1077 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1078 perror("unable to set user context");
1079 exit(1);
1080 }
1081#else
1082# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1083 /* Sets login uid for accounting */
1084 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1085 error("setluid: %s", strerror(errno));
1086# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1087
1088 if (setlogin(pw->pw_name) < 0)
1089 error("setlogin failed: %s", strerror(errno));
1090 if (setgid(pw->pw_gid) < 0) {
1091 perror("setgid");
1092 exit(1);
1093 }
1094 /* Initialize the group list. */
1095 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1096 perror("initgroups");
1097 exit(1);
1098 }
1099 endgrent();
1100# ifdef USE_PAM
1101 /*
1102 * PAM credentials may take the form of supplementary groups.
1103 * These will have been wiped by the above initgroups() call.
1104 * Reestablish them here.
1105 */
1106 do_pam_setcred(0);
1107# endif /* USE_PAM */
1108# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1109 irix_setusercontext(pw);
1110# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001111 /* Permanently switch to the desired uid. */
1112 permanently_set_uid(pw);
1113#endif
1114 }
1115 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1116 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1117}
1118
Ben Lindstrom08105192002-03-22 02:50:06 +00001119static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120launch_login(struct passwd *pw, const char *hostname)
1121{
1122 /* Launch login(1). */
1123
1124 execl("/usr/bin/login", "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001125#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126 (s->term ? s->term : "unknown"),
1127#endif /* LOGIN_NEEDS_TERM */
1128 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1129
1130 /* Login couldn't be executed, die. */
1131
1132 perror("login");
1133 exit(1);
1134}
1135
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001136/*
1137 * Performs common processing for the child, such as setting up the
1138 * environment, closing extra file descriptors, setting the user and group
1139 * ids, and executing the command or shell.
1140 */
1141void
1142do_child(Session *s, const char *command)
1143{
1144 extern char **environ;
1145 char **env;
1146 char *argv[10];
1147 const char *shell, *shell0, *hostname = NULL;
1148 struct passwd *pw = s->pw;
1149 u_int i;
1150
1151 /* remove hostkey from the child's memory */
1152 destroy_sensitive_data();
1153
1154 /* login(1) is only called if we execute the login shell */
1155 if (options.use_login && command != NULL)
1156 options.use_login = 0;
1157
1158 /*
1159 * Login(1) does this as well, and it needs uid 0 for the "-h"
1160 * switch, so we let login(1) to this for us.
1161 */
1162 if (!options.use_login) {
1163#ifdef HAVE_OSF_SIA
1164 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
1165 if (!check_quietlogin(s, command))
1166 do_motd();
1167#else /* HAVE_OSF_SIA */
1168 do_nologin(pw);
Ben Lindstromc0041352002-02-25 15:48:02 +00001169# ifdef _AIX
1170 aix_usrinfo(pw, s->tty, s->ttyfd);
1171# endif /* _AIX */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001172 do_setusercontext(pw);
1173#endif /* HAVE_OSF_SIA */
1174 }
1175
1176 /*
1177 * Get the shell from the password data. An empty shell field is
1178 * legal, and means /bin/sh.
1179 */
1180 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1181#ifdef HAVE_LOGIN_CAP
1182 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1183#endif
1184
1185 env = do_setup_env(s, shell);
1186
Damien Millerad833b32000-08-23 10:46:23 +10001187 /* we have to stash the hostname before we close our socket. */
1188 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001189 hostname = get_remote_name_or_ip(utmp_len,
Damien Millerc5d86352002-02-05 12:13:41 +11001190 options.verify_reverse_mapping);
Damien Millerb38eff82000-04-01 11:09:21 +10001191 /*
1192 * Close the connection descriptors; note that this is the child, and
1193 * the server will still have the socket open, and it is important
1194 * that we do not shutdown it. Note that the descriptors cannot be
1195 * closed before building the environment, as we call
1196 * get_remote_ipaddr there.
1197 */
1198 if (packet_get_connection_in() == packet_get_connection_out())
1199 close(packet_get_connection_in());
1200 else {
1201 close(packet_get_connection_in());
1202 close(packet_get_connection_out());
1203 }
1204 /*
1205 * Close all descriptors related to channels. They will still remain
1206 * open in the parent.
1207 */
1208 /* XXX better use close-on-exec? -markus */
1209 channel_close_all();
1210
1211 /*
1212 * Close any extra file descriptors. Note that there may still be
1213 * descriptors left by system functions. They will be closed later.
1214 */
1215 endpwent();
1216
1217 /*
1218 * Close any extra open file descriptors so that we don\'t have them
1219 * hanging around in clients. Note that we want to do this after
1220 * initgroups, because at least on Solaris 2.3 it leaves file
1221 * descriptors open.
1222 */
1223 for (i = 3; i < 64; i++)
1224 close(i);
1225
Damien Millerb38eff82000-04-01 11:09:21 +10001226 /*
Damien Miller05eda432002-02-10 18:32:28 +11001227 * Must take new environment into use so that .ssh/rc,
1228 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001229 */
1230 environ = env;
1231
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001232#ifdef AFS
1233 /* Try to get AFS tokens for the local cell. */
1234 if (k_hasafs()) {
1235 char cell[64];
Damien Miller9f0f5c62001-12-21 14:45:46 +11001236
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001237 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1238 krb_afslog(cell, 0);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001239
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001240 krb_afslog(0, 0);
1241 }
1242#endif /* AFS */
1243
Damien Miller139d4cd2001-10-10 15:07:44 +10001244 /* Change current directory to the user\'s home directory. */
1245 if (chdir(pw->pw_dir) < 0) {
1246 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1247 pw->pw_dir, strerror(errno));
1248#ifdef HAVE_LOGIN_CAP
1249 if (login_getcapbool(lc, "requirehome", 0))
1250 exit(1);
1251#endif
1252 }
1253
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001254 if (!options.use_login)
1255 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001256
1257 /* restore SIGPIPE for child */
1258 signal(SIGPIPE, SIG_DFL);
1259
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001260 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 launch_login(pw, hostname);
1262 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001263 }
1264
1265 /* Get the last component of the shell name. */
1266 if ((shell0 = strrchr(shell, '/')) != NULL)
1267 shell0++;
1268 else
1269 shell0 = shell;
1270
Damien Millerb38eff82000-04-01 11:09:21 +10001271 /*
1272 * If we have no command, execute the shell. In this case, the shell
1273 * name to be passed in argv[0] is preceded by '-' to indicate that
1274 * this is a login shell.
1275 */
1276 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001277 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001278
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001279 /* Start the shell. Set initial character to '-'. */
1280 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001281
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001282 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1283 >= sizeof(argv0) - 1) {
1284 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001285 perror(shell);
1286 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001287 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001288
1289 /* Execute the shell. */
1290 argv[0] = argv0;
1291 argv[1] = NULL;
1292 execve(shell, argv, env);
1293
1294 /* Executing the shell failed. */
1295 perror(shell);
1296 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001297 }
1298 /*
1299 * Execute the command using the user's shell. This uses the -c
1300 * option to execute the command.
1301 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001302 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001303 argv[1] = "-c";
1304 argv[2] = (char *) command;
1305 argv[3] = NULL;
1306 execve(shell, argv, env);
1307 perror(shell);
1308 exit(1);
1309}
1310
1311Session *
1312session_new(void)
1313{
1314 int i;
1315 static int did_init = 0;
1316 if (!did_init) {
1317 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001318 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001319 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001320 }
1321 did_init = 1;
1322 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001323 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001324 Session *s = &sessions[i];
1325 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001326 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001327 s->chanid = -1;
1328 s->ptyfd = -1;
1329 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001330 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001331 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001332 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001333 return s;
1334 }
1335 }
1336 return NULL;
1337}
1338
Ben Lindstrombba81212001-06-25 05:01:22 +00001339static void
Damien Millerb38eff82000-04-01 11:09:21 +10001340session_dump(void)
1341{
1342 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001343 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001344 Session *s = &sessions[i];
1345 debug("dump: used %d session %d %p channel %d pid %d",
1346 s->used,
1347 s->self,
1348 s,
1349 s->chanid,
1350 s->pid);
1351 }
1352}
1353
Damien Millerefb4afe2000-04-12 18:45:05 +10001354int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001355session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001356{
1357 Session *s = session_new();
1358 debug("session_open: channel %d", chanid);
1359 if (s == NULL) {
1360 error("no more sessions");
1361 return 0;
1362 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001363 s->authctxt = authctxt;
1364 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001365 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001366 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001367 debug("session_open: session %d: link with channel %d", s->self, chanid);
1368 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001369 return 1;
1370}
1371
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372Session *
1373session_by_tty(char *tty)
1374{
1375 int i;
1376 for (i = 0; i < MAX_SESSIONS; i++) {
1377 Session *s = &sessions[i];
1378 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1379 debug("session_by_tty: session %d tty %s", i, tty);
1380 return s;
1381 }
1382 }
1383 debug("session_by_tty: unknown tty %.100s", tty);
1384 session_dump();
1385 return NULL;
1386}
1387
Ben Lindstrombba81212001-06-25 05:01:22 +00001388static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001389session_by_channel(int id)
1390{
1391 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001392 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001393 Session *s = &sessions[i];
1394 if (s->used && s->chanid == id) {
1395 debug("session_by_channel: session %d channel %d", i, id);
1396 return s;
1397 }
1398 }
1399 debug("session_by_channel: unknown channel %d", id);
1400 session_dump();
1401 return NULL;
1402}
1403
Ben Lindstrombba81212001-06-25 05:01:22 +00001404static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001405session_by_pid(pid_t pid)
1406{
1407 int i;
1408 debug("session_by_pid: pid %d", pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001409 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001410 Session *s = &sessions[i];
1411 if (s->used && s->pid == pid)
1412 return s;
1413 }
1414 error("session_by_pid: unknown pid %d", pid);
1415 session_dump();
1416 return NULL;
1417}
1418
Ben Lindstrombba81212001-06-25 05:01:22 +00001419static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001420session_window_change_req(Session *s)
1421{
1422 s->col = packet_get_int();
1423 s->row = packet_get_int();
1424 s->xpixel = packet_get_int();
1425 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001426 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001427 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1428 return 1;
1429}
1430
Ben Lindstrombba81212001-06-25 05:01:22 +00001431static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001432session_pty_req(Session *s)
1433{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001434 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001435 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001436
Ben Lindstrom49c12602001-06-13 04:37:36 +00001437 if (no_pty_flag) {
1438 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001439 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001440 }
1441 if (s->ttyfd != -1) {
1442 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001443 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001444 }
1445
Damien Millerefb4afe2000-04-12 18:45:05 +10001446 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001447
1448 if (compat20) {
1449 s->col = packet_get_int();
1450 s->row = packet_get_int();
1451 } else {
1452 s->row = packet_get_int();
1453 s->col = packet_get_int();
1454 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001455 s->xpixel = packet_get_int();
1456 s->ypixel = packet_get_int();
1457
1458 if (strcmp(s->term, "") == 0) {
1459 xfree(s->term);
1460 s->term = NULL;
1461 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001462
Damien Millerefb4afe2000-04-12 18:45:05 +10001463 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001464 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001466 if (s->term)
1467 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001468 s->term = NULL;
1469 s->ptyfd = -1;
1470 s->ttyfd = -1;
1471 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001472 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001473 }
1474 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001475
1476 /* for SSH1 the tty modes length is not given */
1477 if (!compat20)
1478 n_bytes = packet_remaining();
1479 tty_parse_modes(s->ttyfd, &n_bytes);
1480
Damien Millerefb4afe2000-04-12 18:45:05 +10001481 /*
1482 * Add a cleanup function to clear the utmp entry and record logout
1483 * time in case we call fatal() (e.g., the connection gets closed).
1484 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001485 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 if (!use_privsep)
1487 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001488
1489 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001490 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1491
Damien Miller48b03fc2002-01-22 23:11:40 +11001492 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001493 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001494 return 1;
1495}
1496
Ben Lindstrombba81212001-06-25 05:01:22 +00001497static int
Damien Millerbd483e72000-04-30 10:00:53 +10001498session_subsystem_req(Session *s)
1499{
Damien Millerae452462001-10-10 15:08:06 +10001500 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001501 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001502 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001503 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001504 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001505
Damien Miller48b03fc2002-01-22 23:11:40 +11001506 packet_check_eom();
Damien Millerbaa08702002-02-05 12:14:10 +11001507 log("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001508
Damien Millerf6d9e222000-06-18 14:50:44 +10001509 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001510 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1511 cmd = options.subsystem_command[i];
1512 if (stat(cmd, &st) < 0) {
1513 error("subsystem: cannot stat %s: %s", cmd,
1514 strerror(errno));
1515 break;
1516 }
1517 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001518 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001519 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001520 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001521 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001522 }
1523 }
1524
1525 if (!success)
Damien Millerbaa08702002-02-05 12:14:10 +11001526 log("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001527 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001528
Damien Millerbd483e72000-04-30 10:00:53 +10001529 xfree(subsys);
1530 return success;
1531}
1532
Ben Lindstrombba81212001-06-25 05:01:22 +00001533static int
Damien Millerbd483e72000-04-30 10:00:53 +10001534session_x11_req(Session *s)
1535{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001536 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001537
1538 s->single_connection = packet_get_char();
1539 s->auth_proto = packet_get_string(NULL);
1540 s->auth_data = packet_get_string(NULL);
1541 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001542 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001543
Ben Lindstrom768176b2001-06-09 01:29:12 +00001544 success = session_setup_x11fwd(s);
1545 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001546 xfree(s->auth_proto);
1547 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001548 s->auth_proto = NULL;
1549 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001550 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001551 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001552}
1553
Ben Lindstrombba81212001-06-25 05:01:22 +00001554static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001555session_shell_req(Session *s)
1556{
Damien Miller48b03fc2002-01-22 23:11:40 +11001557 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001558 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001559 return 1;
1560}
1561
Ben Lindstrombba81212001-06-25 05:01:22 +00001562static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001563session_exec_req(Session *s)
1564{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001565 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001566 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001567 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001568 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001569 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001570 return 1;
1571}
1572
Ben Lindstrombba81212001-06-25 05:01:22 +00001573static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001574session_auth_agent_req(Session *s)
1575{
1576 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001577 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001578 if (no_agent_forwarding_flag) {
1579 debug("session_auth_agent_req: no_agent_forwarding_flag");
1580 return 0;
1581 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001582 if (called) {
1583 return 0;
1584 } else {
1585 called = 1;
1586 return auth_input_request_forwarding(s->pw);
1587 }
1588}
1589
Damien Millerc7ef63d2002-02-05 12:21:42 +11001590int
1591session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001592{
Damien Millerefb4afe2000-04-12 18:45:05 +10001593 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001594 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001595
Damien Millerc7ef63d2002-02-05 12:21:42 +11001596 if ((s = session_by_channel(c->self)) == NULL) {
1597 log("session_input_channel_req: no session %d req %.100s",
1598 c->self, rtype);
1599 return 0;
1600 }
1601 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001602
1603 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001604 * a session is in LARVAL state until a shell, a command
1605 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001606 */
1607 if (c->type == SSH_CHANNEL_LARVAL) {
1608 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001609 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001610 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001611 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001612 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001613 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001614 } else if (strcmp(rtype, "x11-req") == 0) {
1615 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001616 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1617 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001618 } else if (strcmp(rtype, "subsystem") == 0) {
1619 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001620 }
1621 }
1622 if (strcmp(rtype, "window-change") == 0) {
1623 success = session_window_change_req(s);
1624 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001625 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001626}
1627
1628void
1629session_set_fds(Session *s, int fdin, int fdout, int fderr)
1630{
1631 if (!compat20)
1632 fatal("session_set_fds: called for proto != 2.0");
1633 /*
1634 * now that have a child and a pipe to the child,
1635 * we can activate our channel and register the fd's
1636 */
1637 if (s->chanid == -1)
1638 fatal("no channel for session %d", s->self);
1639 channel_set_fds(s->chanid,
1640 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001641 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001642 1,
1643 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001644}
1645
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001646/*
1647 * Function to perform pty cleanup. Also called if we get aborted abnormally
1648 * (e.g., due to a dropped connection).
1649 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001650void
1651session_pty_cleanup2(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001652{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001653 Session *s = session;
1654
1655 if (s == NULL) {
1656 error("session_pty_cleanup: no session");
1657 return;
1658 }
1659 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001660 return;
1661
Kevin Steves43cdef32001-02-11 14:12:08 +00001662 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001663
Damien Millerb38eff82000-04-01 11:09:21 +10001664 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001665 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08001666 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001667
1668 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 if (getuid() == 0)
1670 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001671
1672 /*
1673 * Close the server side of the socket pairs. We must do this after
1674 * the pty cleanup, so that another process doesn't get this pty
1675 * while we're still cleaning up.
1676 */
1677 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001679
1680 /* unlink pty from session */
1681 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001682}
Damien Millerefb4afe2000-04-12 18:45:05 +10001683
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684void
1685session_pty_cleanup(void *session)
1686{
1687 PRIVSEP(session_pty_cleanup2(session));
1688}
1689
Ben Lindstrombba81212001-06-25 05:01:22 +00001690static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001691session_exit_message(Session *s, int status)
1692{
1693 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001694
1695 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001696 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001697 s->self, s->chanid);
1698 debug("session_exit_message: session %d channel %d pid %d",
1699 s->self, s->chanid, s->pid);
1700
1701 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001702 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001703 packet_put_int(WEXITSTATUS(status));
1704 packet_send();
1705 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001706 channel_request_start(s->chanid, "exit-signal", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001707 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001708#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001709 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001710#else /* WCOREDUMP */
1711 packet_put_char(0);
1712#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001713 packet_put_cstring("");
1714 packet_put_cstring("");
1715 packet_send();
1716 } else {
1717 /* Some weird exit cause. Just exit. */
1718 packet_disconnect("wait returned status %04x.", status);
1719 }
1720
1721 /* disconnect channel */
1722 debug("session_exit_message: release channel %d", s->chanid);
1723 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001724 /*
1725 * emulate a write failure with 'chan_write_failed', nobody will be
1726 * interested in data we write.
1727 * Note that we must not call 'chan_read_failed', since there could
1728 * be some more data waiting in the pipe.
1729 */
Damien Millerbd483e72000-04-30 10:00:53 +10001730 if (c->ostate != CHAN_OUTPUT_CLOSED)
1731 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001732 s->chanid = -1;
1733}
1734
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001735void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001736session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001737{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001738 debug("session_close: session %d pid %d", s->self, s->pid);
1739 if (s->ttyfd != -1) {
1740 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1741 session_pty_cleanup(s);
1742 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001743 if (s->term)
1744 xfree(s->term);
1745 if (s->display)
1746 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11001747 if (s->auth_display)
1748 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10001749 if (s->auth_data)
1750 xfree(s->auth_data);
1751 if (s->auth_proto)
1752 xfree(s->auth_proto);
1753 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001754 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001755}
1756
1757void
1758session_close_by_pid(pid_t pid, int status)
1759{
1760 Session *s = session_by_pid(pid);
1761 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001762 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001763 return;
1764 }
1765 if (s->chanid != -1)
1766 session_exit_message(s, status);
1767 session_close(s);
1768}
1769
1770/*
1771 * this is called when a channel dies before
1772 * the session 'child' itself dies
1773 */
1774void
1775session_close_by_channel(int id, void *arg)
1776{
1777 Session *s = session_by_channel(id);
1778 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001779 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001780 return;
1781 }
Damien Miller3ec27592001-10-12 11:35:04 +10001782 debug("session_close_by_channel: channel %d child %d", id, s->pid);
1783 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001784 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001785 /*
1786 * delay detach of session, but release pty, since
1787 * the fd's to the child are already closed
1788 */
1789 if (s->ttyfd != -1) {
1790 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1791 session_pty_cleanup(s);
1792 }
Damien Miller3ec27592001-10-12 11:35:04 +10001793 return;
1794 }
1795 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10001796 channel_cancel_cleanup(s->chanid);
1797 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10001798 session_close(s);
1799}
1800
1801void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001802session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10001803{
1804 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001805 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10001806 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001807 if (s->used) {
1808 if (closefunc != NULL)
1809 closefunc(s);
1810 else
1811 session_close(s);
1812 }
Damien Miller52b77be2001-10-10 15:14:37 +10001813 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001814}
1815
Ben Lindstrombba81212001-06-25 05:01:22 +00001816static char *
Damien Millere247cc42000-05-07 12:03:14 +10001817session_tty_list(void)
1818{
1819 static char buf[1024];
1820 int i;
1821 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11001822 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10001823 Session *s = &sessions[i];
1824 if (s->used && s->ttyfd != -1) {
1825 if (buf[0] != '\0')
1826 strlcat(buf, ",", sizeof buf);
1827 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1828 }
1829 }
1830 if (buf[0] == '\0')
1831 strlcpy(buf, "notty", sizeof buf);
1832 return buf;
1833}
1834
1835void
1836session_proctitle(Session *s)
1837{
1838 if (s->pw == NULL)
1839 error("no user for session %d", s->self);
1840 else
1841 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1842}
1843
Ben Lindstrom768176b2001-06-09 01:29:12 +00001844int
1845session_setup_x11fwd(Session *s)
1846{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001847 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00001848 char display[512], auth_display[512];
1849 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00001850
1851 if (no_x11_forwarding_flag) {
1852 packet_send_debug("X11 forwarding disabled in user configuration file.");
1853 return 0;
1854 }
1855 if (!options.x11_forwarding) {
1856 debug("X11 forwarding disabled in server configuration file.");
1857 return 0;
1858 }
1859 if (!options.xauth_location ||
1860 (stat(options.xauth_location, &st) == -1)) {
1861 packet_send_debug("No xauth program; cannot forward with spoofing.");
1862 return 0;
1863 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00001864 if (options.use_login) {
1865 packet_send_debug("X11 forwarding disabled; "
1866 "not compatible with UseLogin=yes.");
1867 return 0;
1868 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00001869 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00001870 debug("X11 display already set.");
1871 return 0;
1872 }
Kevin Steves366298c2001-12-19 17:58:01 +00001873 s->display_number = x11_create_display_inet(options.x11_display_offset,
Damien Miller95c249f2002-02-05 12:11:34 +11001874 options.x11_use_localhost, s->single_connection);
Kevin Steves366298c2001-12-19 17:58:01 +00001875 if (s->display_number == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00001876 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00001877 return 0;
1878 }
Kevin Steves366298c2001-12-19 17:58:01 +00001879
1880 /* Set up a suitable value for the DISPLAY variable. */
1881 if (gethostname(hostname, sizeof(hostname)) < 0)
1882 fatal("gethostname: %.100s", strerror(errno));
1883 /*
1884 * auth_display must be used as the displayname when the
1885 * authorization entry is added with xauth(1). This will be
1886 * different than the DISPLAY string for localhost displays.
1887 */
Damien Miller95c249f2002-02-05 12:11:34 +11001888 if (options.x11_use_localhost) {
Kevin Steves366298c2001-12-19 17:58:01 +00001889 snprintf(display, sizeof display, "localhost:%d.%d",
1890 s->display_number, s->screen);
Damien Miller512bccb2002-02-05 12:11:02 +11001891 snprintf(auth_display, sizeof auth_display, "unix:%d.%d",
1892 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00001893 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11001894 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00001895 } else {
1896#ifdef IPADDR_IN_DISPLAY
1897 struct hostent *he;
1898 struct in_addr my_addr;
1899
1900 he = gethostbyname(hostname);
1901 if (he == NULL) {
1902 error("Can't get IP address for X11 DISPLAY.");
1903 packet_send_debug("Can't get IP address for X11 DISPLAY.");
1904 return 0;
1905 }
1906 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1907 snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr),
1908 s->display_number, s->screen);
1909#else
1910 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
1911 s->display_number, s->screen);
1912#endif
1913 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11001914 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00001915 }
1916
Ben Lindstrom768176b2001-06-09 01:29:12 +00001917 return 1;
1918}
1919
Ben Lindstrombba81212001-06-25 05:01:22 +00001920static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001921do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10001922{
Ben Lindstrombddd5512001-07-04 04:53:53 +00001923 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001924}