blob: 674ff5862b57e0690dd6cabcb00f6716decfb1b7 [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 Lindstrome37f63f2002-02-19 21:58:19 +000036RCSID("$OpenBSD: session.c,v 1.128 2002/02/16 00:51:44 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"
Damien Millerefb4afe2000-04-12 18:45:05 +100059
Damien Miller37023962000-07-11 17:31:38 +100060#if defined(HAVE_USERSEC_H)
61#include <usersec.h>
62#endif
63
Damien Millerbac2d8a2000-09-05 16:13:06 +110064#ifdef HAVE_CYGWIN
65#include <windows.h>
66#include <sys/cygwin.h>
67#define is_winnt (GetVersion() < 0x80000000)
68#endif
69
Damien Millerb38eff82000-04-01 11:09:21 +100070/* types */
71
72#define TTYSZ 64
73typedef struct Session Session;
74struct Session {
75 int used;
76 int self;
Ben Lindstromec95ed92001-07-04 04:21:14 +000077 struct passwd *pw;
78 Authctxt *authctxt;
Damien Millerb38eff82000-04-01 11:09:21 +100079 pid_t pid;
80 /* tty */
81 char *term;
82 int ptyfd, ttyfd, ptymaster;
83 int row, col, xpixel, ypixel;
84 char tty[TTYSZ];
85 /* X11 */
Kevin Steves366298c2001-12-19 17:58:01 +000086 int display_number;
Damien Millerb38eff82000-04-01 11:09:21 +100087 char *display;
88 int screen;
Damien Miller512bccb2002-02-05 12:11:02 +110089 char *auth_display;
Damien Millerb38eff82000-04-01 11:09:21 +100090 char *auth_proto;
91 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +100092 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +100093 /* proto 2 */
94 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +000095 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +100096};
97
98/* func */
99
100Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000101void session_set_fds(Session *, int, int, int);
102static void session_pty_cleanup(void *);
103void session_proctitle(Session *);
104int session_setup_x11fwd(Session *);
105void do_exec_pty(Session *, const char *);
106void do_exec_no_pty(Session *, const char *);
107void do_exec(Session *, const char *);
108void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000109#ifdef LOGIN_NEEDS_UTMPX
110static void do_pre_login(Session *s);
111#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000112void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000113void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000114int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000115
Ben Lindstrombba81212001-06-25 05:01:22 +0000116static void do_authenticated1(Authctxt *);
117static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000118
Ben Lindstrombba81212001-06-25 05:01:22 +0000119static void session_close(Session *);
120static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000121
Damien Millerb38eff82000-04-01 11:09:21 +1000122/* import */
123extern ServerOptions options;
124extern char *__progname;
125extern int log_stderr;
126extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000127extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000128extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000129extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000130
Damien Miller7b28dc52000-09-05 13:34:53 +1100131/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000132const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100133
Damien Millerb38eff82000-04-01 11:09:21 +1000134/* data */
135#define MAX_SESSIONS 10
136Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100137
Damien Millerd2c208a2000-05-17 22:00:02 +1000138#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000139char *aixloginmsg;
140#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000141
Damien Millerad833b32000-08-23 10:46:23 +1000142#ifdef HAVE_LOGIN_CAP
143static login_cap_t *lc;
144#endif
145
Ben Lindstromb31783d2001-03-22 02:02:12 +0000146void
147do_authenticated(Authctxt *authctxt)
148{
149 /*
150 * Cancel the alarm we set to limit the time taken for
151 * authentication.
152 */
153 alarm(0);
154 if (startup_pipe != -1) {
155 close(startup_pipe);
156 startup_pipe = -1;
157 }
158#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
159 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
160 error("unable to get login class");
161 return;
162 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000163#ifdef BSD_AUTH
164 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
165 packet_disconnect("Approval failure for %s",
166 authctxt->pw->pw_name);
167 }
168#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000169#endif
Damien Millere49d0962001-11-13 23:46:18 +1100170#ifdef WITH_AIXAUTHENTICATE
171 /* We don't have a pty yet, so just label the line as "ssh" */
172 if (loginsuccess(authctxt->user,
Damien Millerf3451a22002-02-05 12:40:46 +1100173 get_canonical_hostname(options.verify_reverse_mapping),
Damien Millere49d0962001-11-13 23:46:18 +1100174 "ssh", &aixloginmsg) < 0)
175 aixloginmsg = NULL;
176#endif /* WITH_AIXAUTHENTICATE */
177
Ben Lindstromb31783d2001-03-22 02:02:12 +0000178 /* setup the channel layer */
179 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
180 channel_permit_all_opens();
181
182 if (compat20)
183 do_authenticated2(authctxt);
184 else
185 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000186
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000187 /* remove agent socket */
Ben Lindstrom838394c2001-06-09 01:11:59 +0000188 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000189 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000190#ifdef KRB4
191 if (options.kerberos_ticket_cleanup)
192 krb4_cleanup_proc(authctxt);
193#endif
194#ifdef KRB5
195 if (options.kerberos_ticket_cleanup)
196 krb5_cleanup_proc(authctxt);
197#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000198}
199
Damien Millerb38eff82000-04-01 11:09:21 +1000200/*
Damien Millerb38eff82000-04-01 11:09:21 +1000201 * Prepares for an interactive session. This is called after the user has
202 * been successfully authenticated. During this message exchange, pseudo
203 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
204 * are requested, etc.
205 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000207do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000208{
209 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000210 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100211 int success, type, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000212 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000213 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000214
215 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000216 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000217 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000218
Damien Millerb38eff82000-04-01 11:09:21 +1000219 /*
220 * We stay in this loop until the client requests to execute a shell
221 * or a command.
222 */
223 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000224 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000225
226 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100227 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000228
229 /* Process the packet. */
230 switch (type) {
231 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000232 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100233 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000234 if (compression_level < 1 || compression_level > 9) {
235 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100236 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000237 break;
238 }
239 /* Enable compression after we have responded with SUCCESS. */
240 enable_compression_after_reply = 1;
241 success = 1;
242 break;
243
244 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000245 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000246 break;
247
248 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000249 s->auth_proto = packet_get_string(&proto_len);
250 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000251
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000252 screen_flag = packet_get_protocol_flags() &
253 SSH_PROTOFLAG_SCREEN_NUMBER;
254 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
255
256 if (packet_remaining() == 4) {
257 if (!screen_flag)
258 debug2("Buggy client: "
259 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000260 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000261 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000262 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000263 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100264 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000265 success = session_setup_x11fwd(s);
266 if (!success) {
267 xfree(s->auth_proto);
268 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000269 s->auth_proto = NULL;
270 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000271 }
Damien Millerb38eff82000-04-01 11:09:21 +1000272 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000273
274 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
275 if (no_agent_forwarding_flag || compat13) {
276 debug("Authentication agent forwarding not permitted for this authentication.");
277 break;
278 }
279 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000280 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000281 break;
282
283 case SSH_CMSG_PORT_FORWARD_REQUEST:
284 if (no_port_forwarding_flag) {
285 debug("Port forwarding not permitted for this authentication.");
286 break;
287 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100288 if (!options.allow_tcp_forwarding) {
289 debug("Port forwarding not permitted.");
290 break;
291 }
Damien Millerb38eff82000-04-01 11:09:21 +1000292 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000293 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000294 success = 1;
295 break;
296
297 case SSH_CMSG_MAX_PACKET_SIZE:
298 if (packet_set_maxsize(packet_get_int()) > 0)
299 success = 1;
300 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100301
Ben Lindstromec95ed92001-07-04 04:21:14 +0000302#if defined(AFS) || defined(KRB5)
303 case SSH_CMSG_HAVE_KERBEROS_TGT:
304 if (!options.kerberos_tgt_passing) {
305 verbose("Kerberos TGT passing disabled.");
306 } else {
307 char *kdata = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100308 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100309
Ben Lindstromec95ed92001-07-04 04:21:14 +0000310 /* XXX - 0x41, see creds_to_radix version */
311 if (kdata[0] != 0x41) {
312#ifdef KRB5
313 krb5_data tgt;
314 tgt.data = kdata;
315 tgt.length = dlen;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100316
Ben Lindstromec95ed92001-07-04 04:21:14 +0000317 if (auth_krb5_tgt(s->authctxt, &tgt))
318 success = 1;
319 else
320 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
321#endif /* KRB5 */
322 } else {
323#ifdef AFS
324 if (auth_krb4_tgt(s->authctxt, kdata))
325 success = 1;
326 else
327 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
328#endif /* AFS */
329 }
330 xfree(kdata);
331 }
332 break;
333#endif /* AFS || KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100334
Ben Lindstromec95ed92001-07-04 04:21:14 +0000335#ifdef AFS
336 case SSH_CMSG_HAVE_AFS_TOKEN:
337 if (!options.afs_token_passing || !k_hasafs()) {
338 verbose("AFS token passing disabled.");
339 } else {
340 /* Accept AFS token. */
341 char *token = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100342 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100343
Ben Lindstromec95ed92001-07-04 04:21:14 +0000344 if (auth_afs_token(s->authctxt, token))
345 success = 1;
346 else
347 verbose("AFS token refused for %.100s",
348 s->authctxt->user);
349 xfree(token);
350 }
351 break;
352#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000353
354 case SSH_CMSG_EXEC_SHELL:
355 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000356 if (type == SSH_CMSG_EXEC_CMD) {
357 command = packet_get_string(&dlen);
358 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000359 do_exec(s, command);
360 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000361 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000362 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000363 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100364 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000365 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000366 return;
367
368 default:
369 /*
370 * Any unknown messages in this phase are ignored,
371 * and a failure message is returned.
372 */
373 log("Unknown packet type received after authentication: %d", type);
374 }
375 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
376 packet_send();
377 packet_write_wait();
378
379 /* Enable compression now that we have replied if appropriate. */
380 if (enable_compression_after_reply) {
381 enable_compression_after_reply = 0;
382 packet_start_compression(compression_level);
383 }
384 }
385}
386
387/*
388 * This is called to fork and execute a command when we have no tty. This
389 * will call do_child from the child, and server_loop from the parent after
390 * setting up file descriptors and such.
391 */
Damien Miller4af51302000-04-16 11:18:38 +1000392void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000393do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000394{
395 int pid;
396
397#ifdef USE_PIPES
398 int pin[2], pout[2], perr[2];
399 /* Allocate pipes for communicating with the program. */
400 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
401 packet_disconnect("Could not create pipes: %.100s",
402 strerror(errno));
403#else /* USE_PIPES */
404 int inout[2], err[2];
405 /* Uses socket pairs to communicate with the program. */
406 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
407 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
408 packet_disconnect("Could not create socket pairs: %.100s",
409 strerror(errno));
410#endif /* USE_PIPES */
411 if (s == NULL)
412 fatal("do_exec_no_pty: no session");
413
Damien Millere247cc42000-05-07 12:03:14 +1000414 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000415
Damien Millerc5946332001-02-28 11:46:11 +1100416#if defined(USE_PAM)
Damien Miller665af9c2001-06-27 09:34:15 +1000417 do_pam_session(s->pw->pw_name, NULL);
Damien Millerf9e93002001-03-27 16:12:24 +1000418 do_pam_setcred(1);
Damien Millerc3aa3dd2001-10-28 22:34:52 +1100419 if (is_pam_password_change_required())
420 packet_disconnect("Password change required but no "
421 "TTY available");
Kevin Stevesff793a22001-02-21 16:36:51 +0000422#endif /* USE_PAM */
423
Damien Millerb38eff82000-04-01 11:09:21 +1000424 /* Fork the child. */
425 if ((pid = fork()) == 0) {
426 /* Child. Reinitialize the log since the pid has changed. */
427 log_init(__progname, options.log_level, options.log_facility, log_stderr);
428
429 /*
430 * Create a new session and process group since the 4.4BSD
431 * setlogin() affects the entire process group.
432 */
433 if (setsid() < 0)
434 error("setsid failed: %.100s", strerror(errno));
435
436#ifdef USE_PIPES
437 /*
438 * Redirect stdin. We close the parent side of the socket
439 * pair, and make the child side the standard input.
440 */
441 close(pin[1]);
442 if (dup2(pin[0], 0) < 0)
443 perror("dup2 stdin");
444 close(pin[0]);
445
446 /* Redirect stdout. */
447 close(pout[0]);
448 if (dup2(pout[1], 1) < 0)
449 perror("dup2 stdout");
450 close(pout[1]);
451
452 /* Redirect stderr. */
453 close(perr[0]);
454 if (dup2(perr[1], 2) < 0)
455 perror("dup2 stderr");
456 close(perr[1]);
457#else /* USE_PIPES */
458 /*
459 * Redirect stdin, stdout, and stderr. Stdin and stdout will
460 * use the same socket, as some programs (particularly rdist)
461 * seem to depend on it.
462 */
463 close(inout[1]);
464 close(err[1]);
465 if (dup2(inout[0], 0) < 0) /* stdin */
466 perror("dup2 stdin");
467 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
468 perror("dup2 stdout");
469 if (dup2(err[0], 2) < 0) /* stderr */
470 perror("dup2 stderr");
471#endif /* USE_PIPES */
472
473 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000474 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000475 /* NOTREACHED */
476 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100477#ifdef HAVE_CYGWIN
478 if (is_winnt)
479 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
480#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000481 if (pid < 0)
482 packet_disconnect("fork failed: %.100s", strerror(errno));
483 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000484 /* Set interactive/non-interactive mode. */
485 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000486#ifdef USE_PIPES
487 /* We are the parent. Close the child sides of the pipes. */
488 close(pin[0]);
489 close(pout[1]);
490 close(perr[1]);
491
Damien Millerefb4afe2000-04-12 18:45:05 +1000492 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000493 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000494 } else {
495 /* Enter the interactive session. */
496 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000497 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000498 }
Damien Millerb38eff82000-04-01 11:09:21 +1000499#else /* USE_PIPES */
500 /* We are the parent. Close the child sides of the socket pairs. */
501 close(inout[0]);
502 close(err[0]);
503
504 /*
505 * Enter the interactive session. Note: server_loop must be able to
506 * handle the case that fdin and fdout are the same.
507 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000508 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000509 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000510 } else {
511 server_loop(pid, inout[1], inout[1], err[1]);
512 /* server_loop has closed inout[1] and err[1]. */
513 }
Damien Millerb38eff82000-04-01 11:09:21 +1000514#endif /* USE_PIPES */
515}
516
517/*
518 * This is called to fork and execute a command when we have a tty. This
519 * will call do_child from the child, and server_loop from the parent after
520 * setting up file descriptors, controlling tty, updating wtmp, utmp,
521 * lastlog, and other such operations.
522 */
Damien Miller4af51302000-04-16 11:18:38 +1000523void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000524do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000525{
Damien Millerb38eff82000-04-01 11:09:21 +1000526 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000527 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000528
529 if (s == NULL)
530 fatal("do_exec_pty: no session");
531 ptyfd = s->ptyfd;
532 ttyfd = s->ttyfd;
533
Damien Millerc5946332001-02-28 11:46:11 +1100534#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000535 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000536 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100537#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000538
Damien Millerb38eff82000-04-01 11:09:21 +1000539 /* Fork the child. */
540 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000541
Damien Miller942da032000-08-18 13:59:06 +1000542 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000543 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000544 /* Close the master side of the pseudo tty. */
545 close(ptyfd);
546
547 /* Make the pseudo tty our controlling tty. */
548 pty_make_controlling_tty(&ttyfd, s->tty);
549
Damien Miller9c751422001-10-10 15:02:46 +1000550 /* Redirect stdin/stdout/stderr from the pseudo tty. */
551 if (dup2(ttyfd, 0) < 0)
552 error("dup2 stdin: %s", strerror(errno));
553 if (dup2(ttyfd, 1) < 0)
554 error("dup2 stdout: %s", strerror(errno));
555 if (dup2(ttyfd, 2) < 0)
556 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000557
558 /* Close the extra descriptor for the pseudo tty. */
559 close(ttyfd);
560
Damien Miller942da032000-08-18 13:59:06 +1000561 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000562#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100563 if (!(options.use_login && command == NULL))
564 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000565# ifdef LOGIN_NEEDS_UTMPX
566 else
567 do_pre_login(s);
568# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000569#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000570
Damien Millerb38eff82000-04-01 11:09:21 +1000571 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000572 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000573 /* NOTREACHED */
574 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100575#ifdef HAVE_CYGWIN
576 if (is_winnt)
577 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
578#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000579 if (pid < 0)
580 packet_disconnect("fork failed: %.100s", strerror(errno));
581 s->pid = pid;
582
583 /* Parent. Close the slave side of the pseudo tty. */
584 close(ttyfd);
585
586 /*
587 * Create another descriptor of the pty master side for use as the
588 * standard input. We could use the original descriptor, but this
589 * simplifies code in server_loop. The descriptor is bidirectional.
590 */
591 fdout = dup(ptyfd);
592 if (fdout < 0)
593 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
594
595 /* we keep a reference to the pty master */
596 ptymaster = dup(ptyfd);
597 if (ptymaster < 0)
598 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
599 s->ptymaster = ptymaster;
600
601 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000602 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000603 if (compat20) {
604 session_set_fds(s, ptyfd, fdout, -1);
605 } else {
606 server_loop(pid, ptyfd, fdout, -1);
607 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000608 }
Damien Millerb38eff82000-04-01 11:09:21 +1000609}
610
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000611#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000612static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000613do_pre_login(Session *s)
614{
615 socklen_t fromlen;
616 struct sockaddr_storage from;
617 pid_t pid = getpid();
618
619 /*
620 * Get IP address of client. If the connection is not a socket, let
621 * the address be 0.0.0.0.
622 */
623 memset(&from, 0, sizeof(from));
624 if (packet_connection_is_on_socket()) {
625 fromlen = sizeof(from);
626 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100627 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000628 debug("getpeername: %.100s", strerror(errno));
629 fatal_cleanup();
630 }
631 }
632
633 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Millerf3451a22002-02-05 12:40:46 +1100634 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000635 (struct sockaddr *)&from);
636}
637#endif
638
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000639/*
640 * This is called to fork and execute a command. If another command is
641 * to be forced, execute that instead.
642 */
643void
644do_exec(Session *s, const char *command)
645{
646 if (forced_command) {
647 original_command = command;
648 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000649 debug("Forced command '%.900s'", command);
650 }
651
652 if (s->ttyfd != -1)
653 do_exec_pty(s, command);
654 else
655 do_exec_no_pty(s, command);
656
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000657 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000658}
659
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000660
Damien Miller942da032000-08-18 13:59:06 +1000661/* administrative, login(1)-like work */
662void
Damien Miller69b69aa2000-10-28 14:19:58 +1100663do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000664{
Damien Miller942da032000-08-18 13:59:06 +1000665 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100666 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000667 socklen_t fromlen;
668 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000669 time_t last_login_time;
670 struct passwd * pw = s->pw;
671 pid_t pid = getpid();
672
673 /*
674 * Get IP address of client. If the connection is not a socket, let
675 * the address be 0.0.0.0.
676 */
677 memset(&from, 0, sizeof(from));
678 if (packet_connection_is_on_socket()) {
679 fromlen = sizeof(from);
680 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000681 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000682 debug("getpeername: %.100s", strerror(errno));
683 fatal_cleanup();
684 }
685 }
686
Damien Miller7b28dc52000-09-05 13:34:53 +1100687 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000688 if (options.print_lastlog) {
689 hostname[0] = '\0';
690 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
691 hostname, sizeof(hostname));
692 }
Damien Millere4340be2000-09-16 13:29:08 +1100693
Damien Miller942da032000-08-18 13:59:06 +1000694 /* Record that there was a login on that tty from the remote host. */
695 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Millerc5d86352002-02-05 12:13:41 +1100696 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
Ben Lindstromf15a3862001-04-05 23:32:17 +0000697 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000698
Kevin Steves092f2ef2000-10-14 13:36:13 +0000699#ifdef USE_PAM
700 /*
701 * If password change is needed, do it now.
702 * This needs to occur before the ~/.hushlogin check.
703 */
Damien Miller646aa602001-02-15 11:51:32 +1100704 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000705 print_pam_messages();
706 do_pam_chauthtok();
707 }
708#endif
709
Damien Millercf205e82001-04-16 18:29:15 +1000710 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000711 return;
712
713#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100714 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000715 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000716#endif /* USE_PAM */
717#ifdef WITH_AIXAUTHENTICATE
718 if (aixloginmsg && *aixloginmsg)
719 printf("%s\n", aixloginmsg);
720#endif /* WITH_AIXAUTHENTICATE */
721
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000722 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000723 time_string = ctime(&last_login_time);
724 if (strchr(time_string, '\n'))
725 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000726 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000727 printf("Last login: %s\r\n", time_string);
728 else
Damien Millere4340be2000-09-16 13:29:08 +1100729 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000730 }
Damien Millercf205e82001-04-16 18:29:15 +1000731
732 do_motd();
733}
734
735/*
736 * Display the message of the day.
737 */
738void
739do_motd(void)
740{
741 FILE *f;
742 char buf[256];
743
Damien Miller942da032000-08-18 13:59:06 +1000744 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000745#ifdef HAVE_LOGIN_CAP
746 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
747 "/etc/motd"), "r");
748#else
Damien Miller942da032000-08-18 13:59:06 +1000749 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000750#endif
Damien Miller942da032000-08-18 13:59:06 +1000751 if (f) {
752 while (fgets(buf, sizeof(buf), f))
753 fputs(buf, stdout);
754 fclose(f);
755 }
756 }
757}
758
Kevin Steves8f63caa2001-07-04 18:23:02 +0000759
760/*
761 * Check for quiet login, either .hushlogin or command given.
762 */
763int
764check_quietlogin(Session *s, const char *command)
765{
766 char buf[256];
767 struct passwd *pw = s->pw;
768 struct stat st;
769
770 /* Return 1 if .hushlogin exists or a command given. */
771 if (command != NULL)
772 return 1;
773 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
774#ifdef HAVE_LOGIN_CAP
775 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
776 return 1;
777#else
778 if (stat(buf, &st) >= 0)
779 return 1;
780#endif
781 return 0;
782}
783
Damien Millerb38eff82000-04-01 11:09:21 +1000784/*
785 * Sets the value of the given variable in the environment. If the variable
786 * already exists, its value is overriden.
787 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000789child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100790 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000791{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000792 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000793 char **env;
794
795 /*
796 * Find the slot where the value should be stored. If the variable
797 * already exists, we reuse the slot; otherwise we append a new slot
798 * at the end of the array, expanding if necessary.
799 */
800 env = *envp;
801 namelen = strlen(name);
802 for (i = 0; env[i]; i++)
803 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
804 break;
805 if (env[i]) {
806 /* Reuse the slot. */
807 xfree(env[i]);
808 } else {
809 /* New variable. Expand if necessary. */
810 if (i >= (*envsizep) - 1) {
811 (*envsizep) += 50;
812 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
813 }
814 /* Need to set the NULL pointer at end of array beyond the new slot. */
815 env[i + 1] = NULL;
816 }
817
818 /* Allocate space and format the variable in the appropriate slot. */
819 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
820 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
821}
822
823/*
824 * Reads environment variables from the given file and adds/overrides them
825 * into the environment. If the file does not exist, this does nothing.
826 * Otherwise, it must consist of empty lines, comments (line starts with '#')
827 * and assignments of the form name=value. No other forms are allowed.
828 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000830read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100831 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000832{
833 FILE *f;
834 char buf[4096];
835 char *cp, *value;
836
837 f = fopen(filename, "r");
838 if (!f)
839 return;
840
841 while (fgets(buf, sizeof(buf), f)) {
842 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
843 ;
844 if (!*cp || *cp == '#' || *cp == '\n')
845 continue;
846 if (strchr(cp, '\n'))
847 *strchr(cp, '\n') = '\0';
848 value = strchr(cp, '=');
849 if (value == NULL) {
850 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
851 continue;
852 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000853 /*
854 * Replace the equals sign by nul, and advance value to
855 * the value string.
856 */
Damien Millerb38eff82000-04-01 11:09:21 +1000857 *value = '\0';
858 value++;
859 child_set_env(env, envsize, cp, value);
860 }
861 fclose(f);
862}
863
Damien Millerbb9ffc12002-01-08 10:59:32 +1100864void copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000865{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100866 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000867 int i;
868
Damien Millerbb9ffc12002-01-08 10:59:32 +1100869 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000870 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000871
Damien Millerbb9ffc12002-01-08 10:59:32 +1100872 for(i = 0; source[i] != NULL; i++) {
873 var_name = xstrdup(source[i]);
874 if ((var_val = strstr(var_name, "=")) == NULL) {
875 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000876 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000877 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100878 *var_val++ = '\0';
879
880 debug3("Copy environment: %s=%s", var_name, var_val);
881 child_set_env(env, envsize, var_name, var_val);
882
883 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000884 }
885}
Damien Millercb5e44a2000-09-29 12:12:36 +1100886
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000887static char **
888do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000889{
Damien Millerb38eff82000-04-01 11:09:21 +1000890 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000891 u_int i, envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000892 char **env;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000893 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000894
Damien Millerb38eff82000-04-01 11:09:21 +1000895 /* Initialize the environment. */
896 envsize = 100;
897 env = xmalloc(envsize * sizeof(char *));
898 env[0] = NULL;
899
Damien Millerbac2d8a2000-09-05 16:13:06 +1100900#ifdef HAVE_CYGWIN
901 /*
902 * The Windows environment contains some setting which are
903 * important for a running system. They must not be dropped.
904 */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100905 copy_environment(environ, &env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100906#endif
907
Damien Millerb38eff82000-04-01 11:09:21 +1000908 if (!options.use_login) {
909 /* Set basic environment. */
910 child_set_env(&env, &envsize, "USER", pw->pw_name);
911 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
912 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000913#ifdef HAVE_LOGIN_CAP
914 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
915 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +1100916#else /* HAVE_LOGIN_CAP */
917# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +1100918 /*
919 * There's no standard path on Windows. The path contains
920 * important components pointing to the system directories,
921 * needed for loading shared libraries. So the path better
922 * remains intact here.
923 */
Damien Millerb38eff82000-04-01 11:09:21 +1000924 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +1100925# endif /* HAVE_CYGWIN */
926#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +1000927
928 snprintf(buf, sizeof buf, "%.200s/%.50s",
929 _PATH_MAILDIR, pw->pw_name);
930 child_set_env(&env, &envsize, "MAIL", buf);
931
932 /* Normal systems set SHELL by default. */
933 child_set_env(&env, &envsize, "SHELL", shell);
934 }
935 if (getenv("TZ"))
936 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
937
938 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000939 if (!options.use_login) {
940 while (custom_environment) {
941 struct envstring *ce = custom_environment;
942 char *s = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000943
Ben Lindstrom38b951c2001-12-06 17:47:47 +0000944 for (i = 0; s[i] != '=' && s[i]; i++)
945 ;
946 if (s[i] == '=') {
947 s[i] = 0;
948 child_set_env(&env, &envsize, s, s + i + 1);
949 }
950 custom_environment = ce->next;
951 xfree(ce->s);
952 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +1000953 }
Damien Millerb38eff82000-04-01 11:09:21 +1000954 }
955
956 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000957 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +1000958 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
959
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000960 if (s->ttyfd != -1)
961 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
962 if (s->term)
963 child_set_env(&env, &envsize, "TERM", s->term);
964 if (s->display)
965 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +1100966 if (original_command)
967 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
968 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +1000969
970#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +1100971 if ((cp = getenv("AUTHSTATE")) != NULL)
972 child_set_env(&env, &envsize, "AUTHSTATE", cp);
973 if ((cp = getenv("KRB5CCNAME")) != NULL)
974 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
975 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +1000976#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000977#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +0000978 if (s->authctxt->krb4_ticket_file)
979 child_set_env(&env, &envsize, "KRBTKFILE",
980 s->authctxt->krb4_ticket_file);
981#endif
982#ifdef KRB5
983 if (s->authctxt->krb5_ticket_file)
984 child_set_env(&env, &envsize, "KRB5CCNAME",
985 s->authctxt->krb5_ticket_file);
986#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000987#ifdef USE_PAM
988 /* Pull in any environment variables that may have been set by PAM. */
Damien Millerbb9ffc12002-01-08 10:59:32 +1100989 copy_environment(fetch_pam_environment(), &env, &envsize);
Damien Millerb38eff82000-04-01 11:09:21 +1000990#endif /* USE_PAM */
991
Damien Millerb38eff82000-04-01 11:09:21 +1000992 if (auth_get_socket_name() != NULL)
993 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100994 auth_get_socket_name());
Damien Millerb38eff82000-04-01 11:09:21 +1000995
996 /* read $HOME/.ssh/environment. */
997 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000998 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
999 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001000 read_environment_file(&env, &envsize, buf);
1001 }
1002 if (debug_flag) {
1003 /* dump the environment */
1004 fprintf(stderr, "Environment:\n");
1005 for (i = 0; env[i]; i++)
1006 fprintf(stderr, " %.200s\n", env[i]);
1007 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001008 return env;
1009}
1010
1011/*
1012 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1013 * first in this order).
1014 */
1015static void
1016do_rc_files(Session *s, const char *shell)
1017{
1018 FILE *f = NULL;
1019 char cmd[1024];
1020 int do_xauth;
1021 struct stat st;
1022
1023 do_xauth =
1024 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1025
1026 /* ignore _PATH_SSH_USER_RC for subsystems */
1027 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1028 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1029 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1030 if (debug_flag)
1031 fprintf(stderr, "Running %s\n", cmd);
1032 f = popen(cmd, "w");
1033 if (f) {
1034 if (do_xauth)
1035 fprintf(f, "%s %s\n", s->auth_proto,
1036 s->auth_data);
1037 pclose(f);
1038 } else
1039 fprintf(stderr, "Could not run %s\n",
1040 _PATH_SSH_USER_RC);
1041 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1042 if (debug_flag)
1043 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1044 _PATH_SSH_SYSTEM_RC);
1045 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1046 if (f) {
1047 if (do_xauth)
1048 fprintf(f, "%s %s\n", s->auth_proto,
1049 s->auth_data);
1050 pclose(f);
1051 } else
1052 fprintf(stderr, "Could not run %s\n",
1053 _PATH_SSH_SYSTEM_RC);
1054 } else if (do_xauth && options.xauth_location != NULL) {
1055 /* Add authority data to .Xauthority if appropriate. */
1056 if (debug_flag) {
1057 fprintf(stderr,
1058 "Running %.100s add "
1059 "%.100s %.100s %.100s\n",
1060 options.xauth_location, s->auth_display,
1061 s->auth_proto, s->auth_data);
1062 }
1063 snprintf(cmd, sizeof cmd, "%s -q -",
1064 options.xauth_location);
1065 f = popen(cmd, "w");
1066 if (f) {
1067 fprintf(f, "add %s %s %s\n",
1068 s->auth_display, s->auth_proto,
1069 s->auth_data);
1070 pclose(f);
1071 } else {
1072 fprintf(stderr, "Could not run %s\n",
1073 cmd);
1074 }
1075 }
1076}
1077
1078static void
1079do_nologin(struct passwd *pw)
1080{
1081 FILE *f = NULL;
1082 char buf[1024];
1083
1084#ifdef HAVE_LOGIN_CAP
1085 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1086 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1087 _PATH_NOLOGIN), "r");
1088#else
1089 if (pw->pw_uid)
1090 f = fopen(_PATH_NOLOGIN, "r");
1091#endif
1092 if (f) {
1093 /* /etc/nologin exists. Print its contents and exit. */
1094 while (fgets(buf, sizeof(buf), f))
1095 fputs(buf, stderr);
1096 fclose(f);
1097 exit(254);
1098 }
1099}
1100
1101/* Set login name, uid, gid, and groups. */
1102static void
1103do_setusercontext(struct passwd *pw)
1104{
1105#ifdef HAVE_CYGWIN
Ben Lindstrom3107efc2002-02-21 15:37:02 +00001106 if (is_winnt) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001107#else /* HAVE_CYGWIN */
1108 if (getuid() == 0 || geteuid() == 0) {
1109#endif /* HAVE_CYGWIN */
1110#ifdef HAVE_GETUSERATTR
1111 set_limits_from_userattr(pw->pw_name);
1112#endif /* HAVE_GETUSERATTR */
1113#ifdef HAVE_LOGIN_CAP
1114 if (setusercontext(lc, pw, pw->pw_uid,
1115 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1116 perror("unable to set user context");
1117 exit(1);
1118 }
1119#else
1120# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1121 /* Sets login uid for accounting */
1122 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1123 error("setluid: %s", strerror(errno));
1124# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1125
1126 if (setlogin(pw->pw_name) < 0)
1127 error("setlogin failed: %s", strerror(errno));
1128 if (setgid(pw->pw_gid) < 0) {
1129 perror("setgid");
1130 exit(1);
1131 }
1132 /* Initialize the group list. */
1133 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1134 perror("initgroups");
1135 exit(1);
1136 }
1137 endgrent();
1138# ifdef USE_PAM
1139 /*
1140 * PAM credentials may take the form of supplementary groups.
1141 * These will have been wiped by the above initgroups() call.
1142 * Reestablish them here.
1143 */
1144 do_pam_setcred(0);
1145# endif /* USE_PAM */
1146# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1147 irix_setusercontext(pw);
1148# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
1149#ifdef _AIX
1150 aix_usrinfo(s)
1151#endif
1152 /* Permanently switch to the desired uid. */
1153 permanently_set_uid(pw);
1154#endif
1155 }
1156 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1157 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1158}
1159
1160/*
1161 * Performs common processing for the child, such as setting up the
1162 * environment, closing extra file descriptors, setting the user and group
1163 * ids, and executing the command or shell.
1164 */
1165void
1166do_child(Session *s, const char *command)
1167{
1168 extern char **environ;
1169 char **env;
1170 char *argv[10];
1171 const char *shell, *shell0, *hostname = NULL;
1172 struct passwd *pw = s->pw;
1173 u_int i;
1174
1175 /* remove hostkey from the child's memory */
1176 destroy_sensitive_data();
1177
1178 /* login(1) is only called if we execute the login shell */
1179 if (options.use_login && command != NULL)
1180 options.use_login = 0;
1181
1182 /*
1183 * Login(1) does this as well, and it needs uid 0 for the "-h"
1184 * switch, so we let login(1) to this for us.
1185 */
1186 if (!options.use_login) {
1187#ifdef HAVE_OSF_SIA
1188 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
1189 if (!check_quietlogin(s, command))
1190 do_motd();
1191#else /* HAVE_OSF_SIA */
1192 do_nologin(pw);
1193 do_setusercontext(pw);
1194#endif /* HAVE_OSF_SIA */
1195 }
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;
1202#ifdef HAVE_LOGIN_CAP
1203 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1204#endif
1205
1206 env = do_setup_env(s, shell);
1207
Damien Millerad833b32000-08-23 10:46:23 +10001208 /* we have to stash the hostname before we close our socket. */
1209 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001210 hostname = get_remote_name_or_ip(utmp_len,
Damien Millerc5d86352002-02-05 12:13:41 +11001211 options.verify_reverse_mapping);
Damien Millerb38eff82000-04-01 11:09:21 +10001212 /*
1213 * Close the connection descriptors; note that this is the child, and
1214 * the server will still have the socket open, and it is important
1215 * that we do not shutdown it. Note that the descriptors cannot be
1216 * closed before building the environment, as we call
1217 * get_remote_ipaddr there.
1218 */
1219 if (packet_get_connection_in() == packet_get_connection_out())
1220 close(packet_get_connection_in());
1221 else {
1222 close(packet_get_connection_in());
1223 close(packet_get_connection_out());
1224 }
1225 /*
1226 * Close all descriptors related to channels. They will still remain
1227 * open in the parent.
1228 */
1229 /* XXX better use close-on-exec? -markus */
1230 channel_close_all();
1231
1232 /*
1233 * Close any extra file descriptors. Note that there may still be
1234 * descriptors left by system functions. They will be closed later.
1235 */
1236 endpwent();
1237
1238 /*
1239 * Close any extra open file descriptors so that we don\'t have them
1240 * hanging around in clients. Note that we want to do this after
1241 * initgroups, because at least on Solaris 2.3 it leaves file
1242 * descriptors open.
1243 */
1244 for (i = 3; i < 64; i++)
1245 close(i);
1246
Damien Millerb38eff82000-04-01 11:09:21 +10001247 /*
Damien Miller05eda432002-02-10 18:32:28 +11001248 * Must take new environment into use so that .ssh/rc,
1249 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001250 */
1251 environ = env;
1252
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001253#ifdef AFS
1254 /* Try to get AFS tokens for the local cell. */
1255 if (k_hasafs()) {
1256 char cell[64];
Damien Miller9f0f5c62001-12-21 14:45:46 +11001257
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001258 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1259 krb_afslog(cell, 0);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001260
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001261 krb_afslog(0, 0);
1262 }
1263#endif /* AFS */
1264
Damien Miller139d4cd2001-10-10 15:07:44 +10001265 /* Change current directory to the user\'s home directory. */
1266 if (chdir(pw->pw_dir) < 0) {
1267 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1268 pw->pw_dir, strerror(errno));
1269#ifdef HAVE_LOGIN_CAP
1270 if (login_getcapbool(lc, "requirehome", 0))
1271 exit(1);
1272#endif
1273 }
1274
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001275 if (!options.use_login)
1276 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001277
1278 /* restore SIGPIPE for child */
1279 signal(SIGPIPE, SIG_DFL);
1280
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001281 if (options.use_login) {
1282 /* Launch login(1). */
1283
1284 execl(LOGIN_PROGRAM, "login", "-h", hostname,
1285#ifdef LOGIN_NEEDS_TERM
1286 (s->term ? s->term : "unknown"),
1287#endif /* LOGIN_NEEDS_TERM */
1288 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1289
1290 /* Login couldn't be executed, die. */
1291
1292 perror("login");
1293 exit(1);
1294 }
1295
1296 /* Get the last component of the shell name. */
1297 if ((shell0 = strrchr(shell, '/')) != NULL)
1298 shell0++;
1299 else
1300 shell0 = shell;
1301
Damien Millerb38eff82000-04-01 11:09:21 +10001302 /*
1303 * If we have no command, execute the shell. In this case, the shell
1304 * name to be passed in argv[0] is preceded by '-' to indicate that
1305 * this is a login shell.
1306 */
1307 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001308 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001309
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001310 /* Start the shell. Set initial character to '-'. */
1311 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001312
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001313 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1314 >= sizeof(argv0) - 1) {
1315 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001316 perror(shell);
1317 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001318 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001319
1320 /* Execute the shell. */
1321 argv[0] = argv0;
1322 argv[1] = NULL;
1323 execve(shell, argv, env);
1324
1325 /* Executing the shell failed. */
1326 perror(shell);
1327 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001328 }
1329 /*
1330 * Execute the command using the user's shell. This uses the -c
1331 * option to execute the command.
1332 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001333 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001334 argv[1] = "-c";
1335 argv[2] = (char *) command;
1336 argv[3] = NULL;
1337 execve(shell, argv, env);
1338 perror(shell);
1339 exit(1);
1340}
1341
1342Session *
1343session_new(void)
1344{
1345 int i;
1346 static int did_init = 0;
1347 if (!did_init) {
1348 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001349 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001350 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001351 }
1352 did_init = 1;
1353 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001354 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001355 Session *s = &sessions[i];
1356 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001357 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001358 s->chanid = -1;
1359 s->ptyfd = -1;
1360 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001361 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001362 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001363 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001364 return s;
1365 }
1366 }
1367 return NULL;
1368}
1369
Ben Lindstrombba81212001-06-25 05:01:22 +00001370static void
Damien Millerb38eff82000-04-01 11:09:21 +10001371session_dump(void)
1372{
1373 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001374 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001375 Session *s = &sessions[i];
1376 debug("dump: used %d session %d %p channel %d pid %d",
1377 s->used,
1378 s->self,
1379 s,
1380 s->chanid,
1381 s->pid);
1382 }
1383}
1384
Damien Millerefb4afe2000-04-12 18:45:05 +10001385int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001386session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001387{
1388 Session *s = session_new();
1389 debug("session_open: channel %d", chanid);
1390 if (s == NULL) {
1391 error("no more sessions");
1392 return 0;
1393 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001394 s->authctxt = authctxt;
1395 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001396 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001397 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001398 debug("session_open: session %d: link with channel %d", s->self, chanid);
1399 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001400 return 1;
1401}
1402
Ben Lindstrombba81212001-06-25 05:01:22 +00001403static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001404session_by_channel(int id)
1405{
1406 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001407 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001408 Session *s = &sessions[i];
1409 if (s->used && s->chanid == id) {
1410 debug("session_by_channel: session %d channel %d", i, id);
1411 return s;
1412 }
1413 }
1414 debug("session_by_channel: unknown channel %d", id);
1415 session_dump();
1416 return NULL;
1417}
1418
Ben Lindstrombba81212001-06-25 05:01:22 +00001419static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001420session_by_pid(pid_t pid)
1421{
1422 int i;
1423 debug("session_by_pid: pid %d", pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001424 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001425 Session *s = &sessions[i];
1426 if (s->used && s->pid == pid)
1427 return s;
1428 }
1429 error("session_by_pid: unknown pid %d", pid);
1430 session_dump();
1431 return NULL;
1432}
1433
Ben Lindstrombba81212001-06-25 05:01:22 +00001434static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001435session_window_change_req(Session *s)
1436{
1437 s->col = packet_get_int();
1438 s->row = packet_get_int();
1439 s->xpixel = packet_get_int();
1440 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001441 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001442 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1443 return 1;
1444}
1445
Ben Lindstrombba81212001-06-25 05:01:22 +00001446static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001447session_pty_req(Session *s)
1448{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001449 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001450 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001451
Ben Lindstrom49c12602001-06-13 04:37:36 +00001452 if (no_pty_flag) {
1453 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001454 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001455 }
1456 if (s->ttyfd != -1) {
1457 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001458 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001459 }
1460
Damien Millerefb4afe2000-04-12 18:45:05 +10001461 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001462
1463 if (compat20) {
1464 s->col = packet_get_int();
1465 s->row = packet_get_int();
1466 } else {
1467 s->row = packet_get_int();
1468 s->col = packet_get_int();
1469 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001470 s->xpixel = packet_get_int();
1471 s->ypixel = packet_get_int();
1472
1473 if (strcmp(s->term, "") == 0) {
1474 xfree(s->term);
1475 s->term = NULL;
1476 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001477
Damien Millerefb4afe2000-04-12 18:45:05 +10001478 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001479 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001480 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001481 if (s->term)
1482 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001483 s->term = NULL;
1484 s->ptyfd = -1;
1485 s->ttyfd = -1;
1486 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001487 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001488 }
1489 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001490
1491 /* for SSH1 the tty modes length is not given */
1492 if (!compat20)
1493 n_bytes = packet_remaining();
1494 tty_parse_modes(s->ttyfd, &n_bytes);
1495
Damien Millerefb4afe2000-04-12 18:45:05 +10001496 /*
1497 * Add a cleanup function to clear the utmp entry and record logout
1498 * time in case we call fatal() (e.g., the connection gets closed).
1499 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001500 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001501 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001502
1503 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001504 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1505
Damien Miller48b03fc2002-01-22 23:11:40 +11001506 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001507 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001508 return 1;
1509}
1510
Ben Lindstrombba81212001-06-25 05:01:22 +00001511static int
Damien Millerbd483e72000-04-30 10:00:53 +10001512session_subsystem_req(Session *s)
1513{
Damien Millerae452462001-10-10 15:08:06 +10001514 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001515 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001516 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001517 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001518 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001519
Damien Miller48b03fc2002-01-22 23:11:40 +11001520 packet_check_eom();
Damien Millerbaa08702002-02-05 12:14:10 +11001521 log("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001522
Damien Millerf6d9e222000-06-18 14:50:44 +10001523 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001524 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1525 cmd = options.subsystem_command[i];
1526 if (stat(cmd, &st) < 0) {
1527 error("subsystem: cannot stat %s: %s", cmd,
1528 strerror(errno));
1529 break;
1530 }
1531 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001532 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001533 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001534 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001535 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001536 }
1537 }
1538
1539 if (!success)
Damien Millerbaa08702002-02-05 12:14:10 +11001540 log("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001541 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001542
Damien Millerbd483e72000-04-30 10:00:53 +10001543 xfree(subsys);
1544 return success;
1545}
1546
Ben Lindstrombba81212001-06-25 05:01:22 +00001547static int
Damien Millerbd483e72000-04-30 10:00:53 +10001548session_x11_req(Session *s)
1549{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001550 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001551
1552 s->single_connection = packet_get_char();
1553 s->auth_proto = packet_get_string(NULL);
1554 s->auth_data = packet_get_string(NULL);
1555 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001556 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001557
Ben Lindstrom768176b2001-06-09 01:29:12 +00001558 success = session_setup_x11fwd(s);
1559 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001560 xfree(s->auth_proto);
1561 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001562 s->auth_proto = NULL;
1563 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001564 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001565 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001566}
1567
Ben Lindstrombba81212001-06-25 05:01:22 +00001568static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001569session_shell_req(Session *s)
1570{
Damien Miller48b03fc2002-01-22 23:11:40 +11001571 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001572 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001573 return 1;
1574}
1575
Ben Lindstrombba81212001-06-25 05:01:22 +00001576static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001577session_exec_req(Session *s)
1578{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001579 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001580 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001581 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001582 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001583 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001584 return 1;
1585}
1586
Ben Lindstrombba81212001-06-25 05:01:22 +00001587static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001588session_auth_agent_req(Session *s)
1589{
1590 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001591 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001592 if (no_agent_forwarding_flag) {
1593 debug("session_auth_agent_req: no_agent_forwarding_flag");
1594 return 0;
1595 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001596 if (called) {
1597 return 0;
1598 } else {
1599 called = 1;
1600 return auth_input_request_forwarding(s->pw);
1601 }
1602}
1603
Damien Millerc7ef63d2002-02-05 12:21:42 +11001604int
1605session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10001606{
Damien Millerefb4afe2000-04-12 18:45:05 +10001607 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001608 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10001609
Damien Millerc7ef63d2002-02-05 12:21:42 +11001610 if ((s = session_by_channel(c->self)) == NULL) {
1611 log("session_input_channel_req: no session %d req %.100s",
1612 c->self, rtype);
1613 return 0;
1614 }
1615 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10001616
1617 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001618 * a session is in LARVAL state until a shell, a command
1619 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001620 */
1621 if (c->type == SSH_CHANNEL_LARVAL) {
1622 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001623 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001624 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001625 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001626 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001627 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001628 } else if (strcmp(rtype, "x11-req") == 0) {
1629 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001630 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1631 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001632 } else if (strcmp(rtype, "subsystem") == 0) {
1633 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001634 }
1635 }
1636 if (strcmp(rtype, "window-change") == 0) {
1637 success = session_window_change_req(s);
1638 }
Damien Millerc7ef63d2002-02-05 12:21:42 +11001639 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10001640}
1641
1642void
1643session_set_fds(Session *s, int fdin, int fdout, int fderr)
1644{
1645 if (!compat20)
1646 fatal("session_set_fds: called for proto != 2.0");
1647 /*
1648 * now that have a child and a pipe to the child,
1649 * we can activate our channel and register the fd's
1650 */
1651 if (s->chanid == -1)
1652 fatal("no channel for session %d", s->self);
1653 channel_set_fds(s->chanid,
1654 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001655 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11001656 1,
1657 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10001658}
1659
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001660/*
1661 * Function to perform pty cleanup. Also called if we get aborted abnormally
1662 * (e.g., due to a dropped connection).
1663 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001664static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001665session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001666{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001667 Session *s = session;
1668
1669 if (s == NULL) {
1670 error("session_pty_cleanup: no session");
1671 return;
1672 }
1673 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001674 return;
1675
Kevin Steves43cdef32001-02-11 14:12:08 +00001676 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001677
Damien Millerb38eff82000-04-01 11:09:21 +10001678 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001679 if (s->pid != 0)
1680 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001681
1682 /* Release the pseudo-tty. */
1683 pty_release(s->tty);
1684
1685 /*
1686 * Close the server side of the socket pairs. We must do this after
1687 * the pty cleanup, so that another process doesn't get this pty
1688 * while we're still cleaning up.
1689 */
1690 if (close(s->ptymaster) < 0)
1691 error("close(s->ptymaster): %s", strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001692
1693 /* unlink pty from session */
1694 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001695}
Damien Millerefb4afe2000-04-12 18:45:05 +10001696
Ben Lindstrombba81212001-06-25 05:01:22 +00001697static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001698session_exit_message(Session *s, int status)
1699{
1700 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001701
1702 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001703 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001704 s->self, s->chanid);
1705 debug("session_exit_message: session %d channel %d pid %d",
1706 s->self, s->chanid, s->pid);
1707
1708 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001709 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001710 packet_put_int(WEXITSTATUS(status));
1711 packet_send();
1712 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11001713 channel_request_start(s->chanid, "exit-signal", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10001714 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001715#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001716 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001717#else /* WCOREDUMP */
1718 packet_put_char(0);
1719#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001720 packet_put_cstring("");
1721 packet_put_cstring("");
1722 packet_send();
1723 } else {
1724 /* Some weird exit cause. Just exit. */
1725 packet_disconnect("wait returned status %04x.", status);
1726 }
1727
1728 /* disconnect channel */
1729 debug("session_exit_message: release channel %d", s->chanid);
1730 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001731 /*
1732 * emulate a write failure with 'chan_write_failed', nobody will be
1733 * interested in data we write.
1734 * Note that we must not call 'chan_read_failed', since there could
1735 * be some more data waiting in the pipe.
1736 */
Damien Millerbd483e72000-04-30 10:00:53 +10001737 if (c->ostate != CHAN_OUTPUT_CLOSED)
1738 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001739 s->chanid = -1;
1740}
1741
Ben Lindstrombba81212001-06-25 05:01:22 +00001742static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001743session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001744{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001745 debug("session_close: session %d pid %d", s->self, s->pid);
1746 if (s->ttyfd != -1) {
1747 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1748 session_pty_cleanup(s);
1749 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001750 if (s->term)
1751 xfree(s->term);
1752 if (s->display)
1753 xfree(s->display);
Damien Miller512bccb2002-02-05 12:11:02 +11001754 if (s->auth_display)
1755 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10001756 if (s->auth_data)
1757 xfree(s->auth_data);
1758 if (s->auth_proto)
1759 xfree(s->auth_proto);
1760 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001761 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001762}
1763
1764void
1765session_close_by_pid(pid_t pid, int status)
1766{
1767 Session *s = session_by_pid(pid);
1768 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001769 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001770 return;
1771 }
1772 if (s->chanid != -1)
1773 session_exit_message(s, status);
1774 session_close(s);
1775}
1776
1777/*
1778 * this is called when a channel dies before
1779 * the session 'child' itself dies
1780 */
1781void
1782session_close_by_channel(int id, void *arg)
1783{
1784 Session *s = session_by_channel(id);
1785 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001786 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001787 return;
1788 }
Damien Miller3ec27592001-10-12 11:35:04 +10001789 debug("session_close_by_channel: channel %d child %d", id, s->pid);
1790 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001791 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001792 /*
1793 * delay detach of session, but release pty, since
1794 * the fd's to the child are already closed
1795 */
1796 if (s->ttyfd != -1) {
1797 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1798 session_pty_cleanup(s);
1799 }
Damien Miller3ec27592001-10-12 11:35:04 +10001800 return;
1801 }
1802 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10001803 channel_cancel_cleanup(s->chanid);
1804 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10001805 session_close(s);
1806}
1807
1808void
Damien Miller3ec27592001-10-12 11:35:04 +10001809session_destroy_all(void)
Damien Miller52b77be2001-10-10 15:14:37 +10001810{
1811 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001812 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10001813 Session *s = &sessions[i];
Damien Miller9f0f5c62001-12-21 14:45:46 +11001814 if (s->used)
Damien Miller52b77be2001-10-10 15:14:37 +10001815 session_close(s);
Damien Miller52b77be2001-10-10 15:14:37 +10001816 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001817}
1818
Ben Lindstrombba81212001-06-25 05:01:22 +00001819static char *
Damien Millere247cc42000-05-07 12:03:14 +10001820session_tty_list(void)
1821{
1822 static char buf[1024];
1823 int i;
1824 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11001825 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10001826 Session *s = &sessions[i];
1827 if (s->used && s->ttyfd != -1) {
1828 if (buf[0] != '\0')
1829 strlcat(buf, ",", sizeof buf);
1830 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1831 }
1832 }
1833 if (buf[0] == '\0')
1834 strlcpy(buf, "notty", sizeof buf);
1835 return buf;
1836}
1837
1838void
1839session_proctitle(Session *s)
1840{
1841 if (s->pw == NULL)
1842 error("no user for session %d", s->self);
1843 else
1844 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1845}
1846
Ben Lindstrom768176b2001-06-09 01:29:12 +00001847int
1848session_setup_x11fwd(Session *s)
1849{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001850 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00001851 char display[512], auth_display[512];
1852 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00001853
1854 if (no_x11_forwarding_flag) {
1855 packet_send_debug("X11 forwarding disabled in user configuration file.");
1856 return 0;
1857 }
1858 if (!options.x11_forwarding) {
1859 debug("X11 forwarding disabled in server configuration file.");
1860 return 0;
1861 }
1862 if (!options.xauth_location ||
1863 (stat(options.xauth_location, &st) == -1)) {
1864 packet_send_debug("No xauth program; cannot forward with spoofing.");
1865 return 0;
1866 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00001867 if (options.use_login) {
1868 packet_send_debug("X11 forwarding disabled; "
1869 "not compatible with UseLogin=yes.");
1870 return 0;
1871 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00001872 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00001873 debug("X11 display already set.");
1874 return 0;
1875 }
Kevin Steves366298c2001-12-19 17:58:01 +00001876 s->display_number = x11_create_display_inet(options.x11_display_offset,
Damien Miller95c249f2002-02-05 12:11:34 +11001877 options.x11_use_localhost, s->single_connection);
Kevin Steves366298c2001-12-19 17:58:01 +00001878 if (s->display_number == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00001879 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00001880 return 0;
1881 }
Kevin Steves366298c2001-12-19 17:58:01 +00001882
1883 /* Set up a suitable value for the DISPLAY variable. */
1884 if (gethostname(hostname, sizeof(hostname)) < 0)
1885 fatal("gethostname: %.100s", strerror(errno));
1886 /*
1887 * auth_display must be used as the displayname when the
1888 * authorization entry is added with xauth(1). This will be
1889 * different than the DISPLAY string for localhost displays.
1890 */
Damien Miller95c249f2002-02-05 12:11:34 +11001891 if (options.x11_use_localhost) {
Kevin Steves366298c2001-12-19 17:58:01 +00001892 snprintf(display, sizeof display, "localhost:%d.%d",
1893 s->display_number, s->screen);
Damien Miller512bccb2002-02-05 12:11:02 +11001894 snprintf(auth_display, sizeof auth_display, "unix:%d.%d",
1895 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00001896 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11001897 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00001898 } else {
1899#ifdef IPADDR_IN_DISPLAY
1900 struct hostent *he;
1901 struct in_addr my_addr;
1902
1903 he = gethostbyname(hostname);
1904 if (he == NULL) {
1905 error("Can't get IP address for X11 DISPLAY.");
1906 packet_send_debug("Can't get IP address for X11 DISPLAY.");
1907 return 0;
1908 }
1909 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1910 snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr),
1911 s->display_number, s->screen);
1912#else
1913 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
1914 s->display_number, s->screen);
1915#endif
1916 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11001917 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00001918 }
1919
Ben Lindstrom768176b2001-06-09 01:29:12 +00001920 return 1;
1921}
1922
Ben Lindstrombba81212001-06-25 05:01:22 +00001923static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001924do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10001925{
Ben Lindstrombddd5512001-07-04 04:53:53 +00001926 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10001927}