blob: 005f7ab17f6f6d87a7f19fde594f4c1e3face9f9 [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 Lindstrom92a2e382001-03-05 06:59:27 +000012 * Copyright (c) 2000 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 Lindstromc85ab8a2001-06-21 03:13:10 +000036RCSID("$OpenBSD: session.c,v 1.90 2001/06/19 12:34:09 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 Miller91606b12000-06-28 08:22:29 +100060#ifdef WITH_IRIX_PROJECT
61#include <proj.h>
62#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000063#ifdef WITH_IRIX_JOBS
64#include <sys/resource.h>
Kevin Stevesef4eea92001-02-05 12:42:17 +000065#endif
Ben Lindstrom49a79c02000-11-17 03:47:20 +000066#ifdef WITH_IRIX_AUDIT
67#include <sat.h>
68#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100069
Damien Miller37023962000-07-11 17:31:38 +100070#if defined(HAVE_USERSEC_H)
71#include <usersec.h>
72#endif
73
Damien Millerbac2d8a2000-09-05 16:13:06 +110074#ifdef HAVE_CYGWIN
75#include <windows.h>
76#include <sys/cygwin.h>
77#define is_winnt (GetVersion() < 0x80000000)
78#endif
79
Damien Millerd17b8d52000-08-09 14:42:28 +100080/* AIX limits */
81#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100082# define S_UFSIZE_HARD S_UFSIZE "_hard"
83# define S_UCPU_HARD S_UCPU "_hard"
84# define S_UDATA_HARD S_UDATA "_hard"
85# define S_USTACK_HARD S_USTACK "_hard"
86# define S_URSS_HARD S_URSS "_hard"
87# define S_UCORE_HARD S_UCORE "_hard"
88# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100089#endif
90
Damien Miller168a7002001-03-17 10:29:50 +110091#ifdef _AIX
92# include <uinfo.h>
93#endif
94
Damien Millerb38eff82000-04-01 11:09:21 +100095/* types */
96
97#define TTYSZ 64
98typedef struct Session Session;
99struct Session {
100 int used;
101 int self;
102 struct passwd *pw;
103 pid_t pid;
104 /* tty */
105 char *term;
106 int ptyfd, ttyfd, ptymaster;
107 int row, col, xpixel, ypixel;
108 char tty[TTYSZ];
109 /* X11 */
110 char *display;
111 int screen;
112 char *auth_proto;
113 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000114 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000115 /* proto 2 */
116 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000117 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000118};
119
120/* func */
121
122Session *session_new(void);
123void session_set_fds(Session *s, int fdin, int fdout, int fderr);
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +0000124void session_pty_cleanup(void *session);
Ben Lindstrom49c12602001-06-13 04:37:36 +0000125int session_pty_req(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000126void session_proctitle(Session *s);
Ben Lindstrom768176b2001-06-09 01:29:12 +0000127int session_setup_x11fwd(Session *s);
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000128void session_close(Session *s);
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000129void do_exec_pty(Session *s, const char *command);
130void do_exec_no_pty(Session *s, const char *command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000131void do_exec(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100132void do_login(Session *s, const char *command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000133#ifdef LOGIN_NEEDS_UTMPX
134void do_pre_login(Session *s);
135#endif
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000136void do_child(Session *s, const char *command);
Damien Millercf205e82001-04-16 18:29:15 +1000137void do_motd(void);
138int check_quietlogin(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000139
Ben Lindstromb31783d2001-03-22 02:02:12 +0000140void do_authenticated1(Authctxt *authctxt);
141void do_authenticated2(Authctxt *authctxt);
142
Damien Millerb38eff82000-04-01 11:09:21 +1000143/* import */
144extern ServerOptions options;
145extern char *__progname;
146extern int log_stderr;
147extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000148extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000149extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000150extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000151
Damien Miller7b28dc52000-09-05 13:34:53 +1100152/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100154
Damien Millerb38eff82000-04-01 11:09:21 +1000155/* data */
156#define MAX_SESSIONS 10
157Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100158
Damien Millerd2c208a2000-05-17 22:00:02 +1000159#ifdef WITH_AIXAUTHENTICATE
160/* AIX's lastlogin message, set in auth1.c */
161char *aixloginmsg;
162#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000163
Damien Millerad833b32000-08-23 10:46:23 +1000164#ifdef HAVE_LOGIN_CAP
165static login_cap_t *lc;
166#endif
167
Ben Lindstromb31783d2001-03-22 02:02:12 +0000168void
169do_authenticated(Authctxt *authctxt)
170{
171 /*
172 * Cancel the alarm we set to limit the time taken for
173 * authentication.
174 */
175 alarm(0);
176 if (startup_pipe != -1) {
177 close(startup_pipe);
178 startup_pipe = -1;
179 }
180#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
181 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
182 error("unable to get login class");
183 return;
184 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000185#ifdef BSD_AUTH
186 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
187 packet_disconnect("Approval failure for %s",
188 authctxt->pw->pw_name);
189 }
190#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000191#endif
192 /* setup the channel layer */
193 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
194 channel_permit_all_opens();
195
196 if (compat20)
197 do_authenticated2(authctxt);
198 else
199 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000200
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000201 /* remove agent socket */
Ben Lindstrom838394c2001-06-09 01:11:59 +0000202 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000203 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000204}
205
Damien Millerb38eff82000-04-01 11:09:21 +1000206/*
Damien Millerb38eff82000-04-01 11:09:21 +1000207 * Prepares for an interactive session. This is called after the user has
208 * been successfully authenticated. During this message exchange, pseudo
209 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
210 * are requested, etc.
211 */
Damien Miller4af51302000-04-16 11:18:38 +1000212void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000213do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000214{
215 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000216 char *command;
Ben Lindstrom49c12602001-06-13 04:37:36 +0000217 int success, type, plen, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000218 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000219 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000220
221 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000222 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000223
Damien Millerb38eff82000-04-01 11:09:21 +1000224 /*
225 * We stay in this loop until the client requests to execute a shell
226 * or a command.
227 */
228 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000229 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000230
231 /* Get a packet from the client. */
232 type = packet_read(&plen);
233
234 /* Process the packet. */
235 switch (type) {
236 case SSH_CMSG_REQUEST_COMPRESSION:
237 packet_integrity_check(plen, 4, type);
238 compression_level = packet_get_int();
239 if (compression_level < 1 || compression_level > 9) {
240 packet_send_debug("Received illegal compression level %d.",
241 compression_level);
242 break;
243 }
244 /* Enable compression after we have responded with SUCCESS. */
245 enable_compression_after_reply = 1;
246 success = 1;
247 break;
248
249 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000250 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000251 break;
252
253 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000254 s->auth_proto = packet_get_string(&proto_len);
255 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000256
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000257 screen_flag = packet_get_protocol_flags() &
258 SSH_PROTOFLAG_SCREEN_NUMBER;
259 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
260
261 if (packet_remaining() == 4) {
262 if (!screen_flag)
263 debug2("Buggy client: "
264 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000265 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000266 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000267 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000268 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000269 packet_done();
270 success = session_setup_x11fwd(s);
271 if (!success) {
272 xfree(s->auth_proto);
273 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000274 s->auth_proto = NULL;
275 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000276 }
Damien Millerb38eff82000-04-01 11:09:21 +1000277 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000278
279 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
280 if (no_agent_forwarding_flag || compat13) {
281 debug("Authentication agent forwarding not permitted for this authentication.");
282 break;
283 }
284 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000285 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000286 break;
287
288 case SSH_CMSG_PORT_FORWARD_REQUEST:
289 if (no_port_forwarding_flag) {
290 debug("Port forwarding not permitted for this authentication.");
291 break;
292 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100293 if (!options.allow_tcp_forwarding) {
294 debug("Port forwarding not permitted.");
295 break;
296 }
Damien Millerb38eff82000-04-01 11:09:21 +1000297 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000298 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000299 success = 1;
300 break;
301
302 case SSH_CMSG_MAX_PACKET_SIZE:
303 if (packet_set_maxsize(packet_get_int()) > 0)
304 success = 1;
305 break;
306
307 case SSH_CMSG_EXEC_SHELL:
308 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000309 if (type == SSH_CMSG_EXEC_CMD) {
310 command = packet_get_string(&dlen);
311 debug("Exec command '%.500s'", command);
312 packet_integrity_check(plen, 4 + dlen, type);
313 } else {
314 command = NULL;
315 packet_integrity_check(plen, 0, type);
316 }
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000317 do_exec(s, command);
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000318 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000319 return;
320
321 default:
322 /*
323 * Any unknown messages in this phase are ignored,
324 * and a failure message is returned.
325 */
326 log("Unknown packet type received after authentication: %d", type);
327 }
328 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
329 packet_send();
330 packet_write_wait();
331
332 /* Enable compression now that we have replied if appropriate. */
333 if (enable_compression_after_reply) {
334 enable_compression_after_reply = 0;
335 packet_start_compression(compression_level);
336 }
337 }
338}
339
340/*
341 * This is called to fork and execute a command when we have no tty. This
342 * will call do_child from the child, and server_loop from the parent after
343 * setting up file descriptors and such.
344 */
Damien Miller4af51302000-04-16 11:18:38 +1000345void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000346do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000347{
348 int pid;
349
350#ifdef USE_PIPES
351 int pin[2], pout[2], perr[2];
352 /* Allocate pipes for communicating with the program. */
353 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
354 packet_disconnect("Could not create pipes: %.100s",
355 strerror(errno));
356#else /* USE_PIPES */
357 int inout[2], err[2];
358 /* Uses socket pairs to communicate with the program. */
359 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
360 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
361 packet_disconnect("Could not create socket pairs: %.100s",
362 strerror(errno));
363#endif /* USE_PIPES */
364 if (s == NULL)
365 fatal("do_exec_no_pty: no session");
366
Damien Millere247cc42000-05-07 12:03:14 +1000367 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000368
Damien Millerc5946332001-02-28 11:46:11 +1100369#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000370 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000371#endif /* USE_PAM */
372
Damien Millerb38eff82000-04-01 11:09:21 +1000373 /* Fork the child. */
374 if ((pid = fork()) == 0) {
375 /* Child. Reinitialize the log since the pid has changed. */
376 log_init(__progname, options.log_level, options.log_facility, log_stderr);
377
378 /*
379 * Create a new session and process group since the 4.4BSD
380 * setlogin() affects the entire process group.
381 */
382 if (setsid() < 0)
383 error("setsid failed: %.100s", strerror(errno));
384
385#ifdef USE_PIPES
386 /*
387 * Redirect stdin. We close the parent side of the socket
388 * pair, and make the child side the standard input.
389 */
390 close(pin[1]);
391 if (dup2(pin[0], 0) < 0)
392 perror("dup2 stdin");
393 close(pin[0]);
394
395 /* Redirect stdout. */
396 close(pout[0]);
397 if (dup2(pout[1], 1) < 0)
398 perror("dup2 stdout");
399 close(pout[1]);
400
401 /* Redirect stderr. */
402 close(perr[0]);
403 if (dup2(perr[1], 2) < 0)
404 perror("dup2 stderr");
405 close(perr[1]);
406#else /* USE_PIPES */
407 /*
408 * Redirect stdin, stdout, and stderr. Stdin and stdout will
409 * use the same socket, as some programs (particularly rdist)
410 * seem to depend on it.
411 */
412 close(inout[1]);
413 close(err[1]);
414 if (dup2(inout[0], 0) < 0) /* stdin */
415 perror("dup2 stdin");
416 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
417 perror("dup2 stdout");
418 if (dup2(err[0], 2) < 0) /* stderr */
419 perror("dup2 stderr");
420#endif /* USE_PIPES */
421
422 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000423 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000424 /* NOTREACHED */
425 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100426#ifdef HAVE_CYGWIN
427 if (is_winnt)
428 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
429#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000430 if (pid < 0)
431 packet_disconnect("fork failed: %.100s", strerror(errno));
432 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000433 /* Set interactive/non-interactive mode. */
434 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000435#ifdef USE_PIPES
436 /* We are the parent. Close the child sides of the pipes. */
437 close(pin[0]);
438 close(pout[1]);
439 close(perr[1]);
440
Damien Millerefb4afe2000-04-12 18:45:05 +1000441 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000442 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000443 } else {
444 /* Enter the interactive session. */
445 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000446 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000447 }
Damien Millerb38eff82000-04-01 11:09:21 +1000448#else /* USE_PIPES */
449 /* We are the parent. Close the child sides of the socket pairs. */
450 close(inout[0]);
451 close(err[0]);
452
453 /*
454 * Enter the interactive session. Note: server_loop must be able to
455 * handle the case that fdin and fdout are the same.
456 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000457 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000458 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000459 } else {
460 server_loop(pid, inout[1], inout[1], err[1]);
461 /* server_loop has closed inout[1] and err[1]. */
462 }
Damien Millerb38eff82000-04-01 11:09:21 +1000463#endif /* USE_PIPES */
464}
465
466/*
467 * This is called to fork and execute a command when we have a tty. This
468 * will call do_child from the child, and server_loop from the parent after
469 * setting up file descriptors, controlling tty, updating wtmp, utmp,
470 * lastlog, and other such operations.
471 */
Damien Miller4af51302000-04-16 11:18:38 +1000472void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000473do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000474{
Damien Millerb38eff82000-04-01 11:09:21 +1000475 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000476 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000477
478 if (s == NULL)
479 fatal("do_exec_pty: no session");
480 ptyfd = s->ptyfd;
481 ttyfd = s->ttyfd;
482
Damien Millerc5946332001-02-28 11:46:11 +1100483#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000484 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000485 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100486#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000487
Damien Millerb38eff82000-04-01 11:09:21 +1000488 /* Fork the child. */
489 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000490 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000491 log_init(__progname, options.log_level, options.log_facility, log_stderr);
492
493 /* Close the master side of the pseudo tty. */
494 close(ptyfd);
495
496 /* Make the pseudo tty our controlling tty. */
497 pty_make_controlling_tty(&ttyfd, s->tty);
498
499 /* Redirect stdin from the pseudo tty. */
500 if (dup2(ttyfd, fileno(stdin)) < 0)
501 error("dup2 stdin failed: %.100s", strerror(errno));
502
503 /* Redirect stdout to the pseudo tty. */
504 if (dup2(ttyfd, fileno(stdout)) < 0)
505 error("dup2 stdin failed: %.100s", strerror(errno));
506
507 /* Redirect stderr to the pseudo tty. */
508 if (dup2(ttyfd, fileno(stderr)) < 0)
509 error("dup2 stdin failed: %.100s", strerror(errno));
510
511 /* Close the extra descriptor for the pseudo tty. */
512 close(ttyfd);
513
Damien Miller942da032000-08-18 13:59:06 +1000514 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000515#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100516 if (!(options.use_login && command == NULL))
517 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000518# ifdef LOGIN_NEEDS_UTMPX
519 else
520 do_pre_login(s);
521# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000522#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000523
Damien Millerb38eff82000-04-01 11:09:21 +1000524 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000525 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000526 /* NOTREACHED */
527 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100528#ifdef HAVE_CYGWIN
529 if (is_winnt)
530 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
531#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000532 if (pid < 0)
533 packet_disconnect("fork failed: %.100s", strerror(errno));
534 s->pid = pid;
535
536 /* Parent. Close the slave side of the pseudo tty. */
537 close(ttyfd);
538
539 /*
540 * Create another descriptor of the pty master side for use as the
541 * standard input. We could use the original descriptor, but this
542 * simplifies code in server_loop. The descriptor is bidirectional.
543 */
544 fdout = dup(ptyfd);
545 if (fdout < 0)
546 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
547
548 /* we keep a reference to the pty master */
549 ptymaster = dup(ptyfd);
550 if (ptymaster < 0)
551 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
552 s->ptymaster = ptymaster;
553
554 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000555 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000556 if (compat20) {
557 session_set_fds(s, ptyfd, fdout, -1);
558 } else {
559 server_loop(pid, ptyfd, fdout, -1);
560 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000561 }
Damien Millerb38eff82000-04-01 11:09:21 +1000562}
563
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000564#ifdef LOGIN_NEEDS_UTMPX
565void
566do_pre_login(Session *s)
567{
568 socklen_t fromlen;
569 struct sockaddr_storage from;
570 pid_t pid = getpid();
571
572 /*
573 * Get IP address of client. If the connection is not a socket, let
574 * the address be 0.0.0.0.
575 */
576 memset(&from, 0, sizeof(from));
577 if (packet_connection_is_on_socket()) {
578 fromlen = sizeof(from);
579 if (getpeername(packet_get_connection_in(),
580 (struct sockaddr *) & from, &fromlen) < 0) {
581 debug("getpeername: %.100s", strerror(errno));
582 fatal_cleanup();
583 }
584 }
585
586 record_utmp_only(pid, s->tty, s->pw->pw_name,
587 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
588 (struct sockaddr *)&from);
589}
590#endif
591
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000592/*
593 * This is called to fork and execute a command. If another command is
594 * to be forced, execute that instead.
595 */
596void
597do_exec(Session *s, const char *command)
598{
599 if (forced_command) {
600 original_command = command;
601 command = forced_command;
602 forced_command = NULL;
603 debug("Forced command '%.900s'", command);
604 }
605
606 if (s->ttyfd != -1)
607 do_exec_pty(s, command);
608 else
609 do_exec_no_pty(s, command);
610
611 if (command != NULL)
612 xfree(command);
613
614 if (original_command != NULL) {
615 xfree(original_command);
616 original_command = NULL;
617 }
618}
619
620
Damien Miller942da032000-08-18 13:59:06 +1000621/* administrative, login(1)-like work */
622void
Damien Miller69b69aa2000-10-28 14:19:58 +1100623do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000624{
Damien Miller942da032000-08-18 13:59:06 +1000625 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100626 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000627 socklen_t fromlen;
628 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000629 time_t last_login_time;
630 struct passwd * pw = s->pw;
631 pid_t pid = getpid();
632
633 /*
634 * Get IP address of client. If the connection is not a socket, let
635 * the address be 0.0.0.0.
636 */
637 memset(&from, 0, sizeof(from));
638 if (packet_connection_is_on_socket()) {
639 fromlen = sizeof(from);
640 if (getpeername(packet_get_connection_in(),
641 (struct sockaddr *) & from, &fromlen) < 0) {
642 debug("getpeername: %.100s", strerror(errno));
643 fatal_cleanup();
644 }
645 }
646
Damien Miller7b28dc52000-09-05 13:34:53 +1100647 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000648 if (options.print_lastlog) {
649 hostname[0] = '\0';
650 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
651 hostname, sizeof(hostname));
652 }
Damien Millere4340be2000-09-16 13:29:08 +1100653
Damien Miller942da032000-08-18 13:59:06 +1000654 /* Record that there was a login on that tty from the remote host. */
655 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000656 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
657 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000658
Kevin Steves092f2ef2000-10-14 13:36:13 +0000659#ifdef USE_PAM
660 /*
661 * If password change is needed, do it now.
662 * This needs to occur before the ~/.hushlogin check.
663 */
Damien Miller646aa602001-02-15 11:51:32 +1100664 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000665 print_pam_messages();
666 do_pam_chauthtok();
667 }
668#endif
669
Damien Millercf205e82001-04-16 18:29:15 +1000670 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000671 return;
672
673#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100674 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000675 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000676#endif /* USE_PAM */
677#ifdef WITH_AIXAUTHENTICATE
678 if (aixloginmsg && *aixloginmsg)
679 printf("%s\n", aixloginmsg);
680#endif /* WITH_AIXAUTHENTICATE */
681
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000682 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000683 time_string = ctime(&last_login_time);
684 if (strchr(time_string, '\n'))
685 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000686 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000687 printf("Last login: %s\r\n", time_string);
688 else
Damien Millere4340be2000-09-16 13:29:08 +1100689 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000690 }
Damien Millercf205e82001-04-16 18:29:15 +1000691
692 do_motd();
693}
694
695/*
696 * Display the message of the day.
697 */
698void
699do_motd(void)
700{
701 FILE *f;
702 char buf[256];
703
Damien Miller942da032000-08-18 13:59:06 +1000704 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000705#ifdef HAVE_LOGIN_CAP
706 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
707 "/etc/motd"), "r");
708#else
Damien Miller942da032000-08-18 13:59:06 +1000709 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000710#endif
Damien Miller942da032000-08-18 13:59:06 +1000711 if (f) {
712 while (fgets(buf, sizeof(buf), f))
713 fputs(buf, stdout);
714 fclose(f);
715 }
716 }
717}
718
Damien Millercf205e82001-04-16 18:29:15 +1000719
720/*
721 * Check for quiet login, either .hushlogin or command given.
722 */
723int
724check_quietlogin(Session *s, const char *command)
725{
726 char buf[256];
727 struct passwd * pw = s->pw;
728 struct stat st;
729
730 /* Return 1 if .hushlogin exists or a command given. */
731 if (command != NULL)
732 return 1;
733 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
734#ifdef HAVE_LOGIN_CAP
735 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
736 return 1;
737#else
738 if (stat(buf, &st) >= 0)
739 return 1;
740#endif
741 return 0;
742}
743
Damien Millerb38eff82000-04-01 11:09:21 +1000744/*
745 * Sets the value of the given variable in the environment. If the variable
746 * already exists, its value is overriden.
747 */
Damien Miller4af51302000-04-16 11:18:38 +1000748void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000749child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000750 const char *value)
751{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000752 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000753 char **env;
754
755 /*
756 * Find the slot where the value should be stored. If the variable
757 * already exists, we reuse the slot; otherwise we append a new slot
758 * at the end of the array, expanding if necessary.
759 */
760 env = *envp;
761 namelen = strlen(name);
762 for (i = 0; env[i]; i++)
763 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
764 break;
765 if (env[i]) {
766 /* Reuse the slot. */
767 xfree(env[i]);
768 } else {
769 /* New variable. Expand if necessary. */
770 if (i >= (*envsizep) - 1) {
771 (*envsizep) += 50;
772 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
773 }
774 /* Need to set the NULL pointer at end of array beyond the new slot. */
775 env[i + 1] = NULL;
776 }
777
778 /* Allocate space and format the variable in the appropriate slot. */
779 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
780 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
781}
782
783/*
784 * Reads environment variables from the given file and adds/overrides them
785 * into the environment. If the file does not exist, this does nothing.
786 * Otherwise, it must consist of empty lines, comments (line starts with '#')
787 * and assignments of the form name=value. No other forms are allowed.
788 */
Damien Miller4af51302000-04-16 11:18:38 +1000789void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000790read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000791 const char *filename)
792{
793 FILE *f;
794 char buf[4096];
795 char *cp, *value;
796
797 f = fopen(filename, "r");
798 if (!f)
799 return;
800
801 while (fgets(buf, sizeof(buf), f)) {
802 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
803 ;
804 if (!*cp || *cp == '#' || *cp == '\n')
805 continue;
806 if (strchr(cp, '\n'))
807 *strchr(cp, '\n') = '\0';
808 value = strchr(cp, '=');
809 if (value == NULL) {
810 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
811 continue;
812 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000813 /*
814 * Replace the equals sign by nul, and advance value to
815 * the value string.
816 */
Damien Millerb38eff82000-04-01 11:09:21 +1000817 *value = '\0';
818 value++;
819 child_set_env(env, envsize, cp, value);
820 }
821 fclose(f);
822}
823
824#ifdef USE_PAM
825/*
826 * Sets any environment variables which have been specified by PAM
827 */
828void do_pam_environment(char ***env, int *envsize)
829{
830 char *equals, var_name[512], var_val[512];
831 char **pam_env;
832 int i;
833
834 if ((pam_env = fetch_pam_environment()) == NULL)
835 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000836
Damien Millerb38eff82000-04-01 11:09:21 +1000837 for(i = 0; pam_env[i] != NULL; i++) {
838 if ((equals = strstr(pam_env[i], "=")) == NULL)
839 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000840
Damien Millerb38eff82000-04-01 11:09:21 +1000841 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
842 memset(var_name, '\0', sizeof(var_name));
843 memset(var_val, '\0', sizeof(var_val));
844
845 strncpy(var_name, pam_env[i], equals - pam_env[i]);
846 strcpy(var_val, equals + 1);
847
Damien Millercb5e44a2000-09-29 12:12:36 +1100848 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000849
850 child_set_env(env, envsize, var_name, var_val);
851 }
852 }
853}
854#endif /* USE_PAM */
855
Damien Millercb5e44a2000-09-29 12:12:36 +1100856#ifdef HAVE_CYGWIN
857void copy_environment(char ***env, int *envsize)
858{
859 char *equals, var_name[512], var_val[512];
860 int i;
861
862 for(i = 0; environ[i] != NULL; i++) {
863 if ((equals = strstr(environ[i], "=")) == NULL)
864 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000865
Damien Millercb5e44a2000-09-29 12:12:36 +1100866 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
867 memset(var_name, '\0', sizeof(var_name));
868 memset(var_val, '\0', sizeof(var_val));
869
870 strncpy(var_name, environ[i], equals - environ[i]);
871 strcpy(var_val, equals + 1);
872
873 debug3("Copy environment: %s=%s", var_name, var_val);
874
875 child_set_env(env, envsize, var_name, var_val);
876 }
877 }
878}
879#endif
880
Damien Miller5fc85652000-07-09 23:53:07 +1000881#if defined(HAVE_GETUSERATTR)
882/*
883 * AIX-specific login initialisation
884 */
885void set_limit(char *user, char *soft, char *hard, int resource, int mult)
886{
887 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000888 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000889
890 getrlimit(resource, &rlim);
891
Damien Miller65964d62000-07-11 09:16:22 +1000892 slim = 0;
893 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
894 if (slim < 0) {
895 rlim.rlim_cur = RLIM_INFINITY;
896 } else if (slim != 0) {
897 /* See the wackiness below */
898 if (rlim.rlim_cur == slim * mult)
899 slim = 0;
900 else
901 rlim.rlim_cur = slim * mult;
902 }
903 }
Damien Miller5fc85652000-07-09 23:53:07 +1000904
Damien Miller65964d62000-07-11 09:16:22 +1000905 hlim = 0;
906 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
907 if (hlim < 0) {
908 rlim.rlim_max = RLIM_INFINITY;
909 } else if (hlim != 0) {
910 rlim.rlim_max = hlim * mult;
911 }
912 }
Damien Miller5fc85652000-07-09 23:53:07 +1000913
Damien Miller65964d62000-07-11 09:16:22 +1000914 /*
915 * XXX For cpu and fsize the soft limit is set to the hard limit
916 * if the hard limit is left at its default value and the soft limit
917 * is changed from its default value, either by requesting it
918 * (slim == 0) or by setting it to the current default. At least
919 * that's how rlogind does it. If you're confused you're not alone.
920 * Bug or feature? AIX 4.3.1.2
921 */
922 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
923 && hlim == 0 && slim != 0)
924 rlim.rlim_max = rlim.rlim_cur;
925 /* A specified hard limit limits the soft limit */
926 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
927 rlim.rlim_cur = rlim.rlim_max;
928 /* A soft limit can increase a hard limit */
929 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000930 rlim.rlim_max = rlim.rlim_cur;
931
932 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000933 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000934}
935
936void set_limits_from_userattr(char *user)
937{
938 int mask;
939 char buf[16];
940
941 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
942 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
943 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
944 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
945 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
946 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
947#if defined(S_UNOFILE)
948 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
949#endif
950
951 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
952 /* Convert decimal to octal */
953 (void) snprintf(buf, sizeof(buf), "%d", mask);
954 if (sscanf(buf, "%o", &mask) == 1)
955 umask(mask);
956 }
957}
958#endif /* defined(HAVE_GETUSERATTR) */
959
Damien Millerb38eff82000-04-01 11:09:21 +1000960/*
961 * Performs common processing for the child, such as setting up the
962 * environment, closing extra file descriptors, setting the user and group
963 * ids, and executing the command or shell.
964 */
Damien Miller4af51302000-04-16 11:18:38 +1000965void
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000966do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000967{
Damien Miller7b28dc52000-09-05 13:34:53 +1100968 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000969 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000970 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +1000971 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +1000972 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000973 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000974 char **env;
975 extern char **environ;
976 struct stat st;
977 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000978 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +1000979#ifdef WITH_IRIX_PROJECT
980 prid_t projid;
981#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +0000982#ifdef WITH_IRIX_JOBS
983 jid_t jid = 0;
984#else
985#ifdef WITH_IRIX_ARRAY
986 int jid = 0;
987#endif /* WITH_IRIX_ARRAY */
988#endif /* WITH_IRIX_JOBS */
989
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000990 do_xauth =
991 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
992
Ben Lindstrom005dd222001-04-18 15:29:33 +0000993 /* remove hostkey from the child's memory */
994 destroy_sensitive_data();
995
Damien Millerd3a18572000-06-07 19:55:44 +1000996 /* login(1) is only called if we execute the login shell */
997 if (options.use_login && command != NULL)
998 options.use_login = 0;
999
Damien Miller364a9bd2001-04-16 18:37:05 +10001000#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001001 if (!options.use_login) {
1002# ifdef HAVE_LOGIN_CAP
1003 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1004 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1005 _PATH_NOLOGIN), "r");
1006# else /* HAVE_LOGIN_CAP */
1007 if (pw->pw_uid)
1008 f = fopen(_PATH_NOLOGIN, "r");
1009# endif /* HAVE_LOGIN_CAP */
1010 if (f) {
1011 /* /etc/nologin exists. Print its contents and exit. */
1012 while (fgets(buf, sizeof(buf), f))
1013 fputs(buf, stderr);
1014 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001015 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001016 }
Damien Millerb38eff82000-04-01 11:09:21 +10001017 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001018#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001019
Damien Millerad833b32000-08-23 10:46:23 +10001020 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001021 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1022 switch, so we let login(1) to this for us. */
1023 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001024#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001025 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001026 if (!check_quietlogin(s, command))
1027 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001028#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001029#ifdef HAVE_CYGWIN
1030 if (is_winnt) {
1031#else
Damien Millerb38eff82000-04-01 11:09:21 +10001032 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001033#endif
Damien Millerad833b32000-08-23 10:46:23 +10001034# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001035 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001036# endif /* HAVE_GETUSERATTR */
1037# ifdef HAVE_LOGIN_CAP
1038 if (setusercontext(lc, pw, pw->pw_uid,
1039 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1040 perror("unable to set user context");
1041 exit(1);
1042 }
1043# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001044#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1045 /* Sets login uid for accounting */
1046 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1047 error("setluid: %s", strerror(errno));
1048#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1049
Damien Millerad833b32000-08-23 10:46:23 +10001050 if (setlogin(pw->pw_name) < 0)
1051 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001052 if (setgid(pw->pw_gid) < 0) {
1053 perror("setgid");
1054 exit(1);
1055 }
1056 /* Initialize the group list. */
1057 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1058 perror("initgroups");
1059 exit(1);
1060 }
1061 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001062# ifdef USE_PAM
1063 /*
1064 * PAM credentials may take the form of
1065 * supplementary groups. These will have been
1066 * wiped by the above initgroups() call.
1067 * Reestablish them here.
1068 */
1069 do_pam_setcred(0);
1070# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001071# ifdef WITH_IRIX_JOBS
1072 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1073 if (jid == -1) {
1074 fatal("Failed to create job container: %.100s",
1075 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001076 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001077# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001078# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001079 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001080 if (jid == 0) {
1081 if (newarraysess() != 0)
1082 fatal("Failed to set up new array session: %.100s",
1083 strerror(errno));
1084 }
Damien Millerad833b32000-08-23 10:46:23 +10001085# endif /* WITH_IRIX_ARRAY */
1086# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001087 /* initialize irix project info */
1088 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1089 debug("Failed to get project id, using projid 0");
1090 projid = 0;
1091 }
Damien Miller91606b12000-06-28 08:22:29 +10001092 if (setprid(projid))
1093 fatal("Failed to initialize project %d for %s: %.100s",
1094 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001095# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001096#ifdef WITH_IRIX_AUDIT
1097 if (sysconf(_SC_AUDIT)) {
1098 debug("Setting sat id to %d", (int) pw->pw_uid);
1099 if (satsetid(pw->pw_uid))
1100 debug("error setting satid: %.100s", strerror(errno));
1101 }
1102#endif /* WITH_IRIX_AUDIT */
1103
Damien Miller168a7002001-03-17 10:29:50 +11001104#ifdef _AIX
1105 /*
1106 * AIX has a "usrinfo" area where logname and
1107 * other stuff is stored - a few applications
1108 * actually use this and die if it's not set
1109 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001110 if (s->ttyfd == -1)
1111 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001112 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001113 2 * strlen(pw->pw_name));
1114 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001115 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001116 if (usrinfo(SETUINFO, cp, i) == -1)
1117 fatal("Couldn't set usrinfo: %s",
1118 strerror(errno));
1119 debug3("AIX/UsrInfo: set len %d", i);
1120 xfree(cp);
1121#endif
1122
Damien Millerb38eff82000-04-01 11:09:21 +10001123 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001124 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001125# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001126 }
Damien Millerad833b32000-08-23 10:46:23 +10001127#endif /* HAVE_OSF_SIA */
1128
Damien Millerbac2d8a2000-09-05 16:13:06 +11001129#ifdef HAVE_CYGWIN
1130 if (is_winnt)
1131#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001132 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001133 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001134 }
1135 /*
1136 * Get the shell from the password data. An empty shell field is
1137 * legal, and means /bin/sh.
1138 */
1139 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001140#ifdef HAVE_LOGIN_CAP
1141 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1142#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001143
1144#ifdef AFS
1145 /* Try to get AFS tokens for the local cell. */
1146 if (k_hasafs()) {
1147 char cell[64];
1148
1149 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1150 krb_afslog(cell, 0);
1151
1152 krb_afslog(0, 0);
1153 }
1154#endif /* AFS */
1155
1156 /* Initialize the environment. */
1157 envsize = 100;
1158 env = xmalloc(envsize * sizeof(char *));
1159 env[0] = NULL;
1160
Damien Millerbac2d8a2000-09-05 16:13:06 +11001161#ifdef HAVE_CYGWIN
1162 /*
1163 * The Windows environment contains some setting which are
1164 * important for a running system. They must not be dropped.
1165 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001166 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001167#endif
1168
Damien Millerb38eff82000-04-01 11:09:21 +10001169 if (!options.use_login) {
1170 /* Set basic environment. */
1171 child_set_env(&env, &envsize, "USER", pw->pw_name);
1172 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1173 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001174#ifdef HAVE_LOGIN_CAP
1175 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1176 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001177#else /* HAVE_LOGIN_CAP */
1178# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001179 /*
1180 * There's no standard path on Windows. The path contains
1181 * important components pointing to the system directories,
1182 * needed for loading shared libraries. So the path better
1183 * remains intact here.
1184 */
Damien Millerb38eff82000-04-01 11:09:21 +10001185 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001186# endif /* HAVE_CYGWIN */
1187#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001188
1189 snprintf(buf, sizeof buf, "%.200s/%.50s",
1190 _PATH_MAILDIR, pw->pw_name);
1191 child_set_env(&env, &envsize, "MAIL", buf);
1192
1193 /* Normal systems set SHELL by default. */
1194 child_set_env(&env, &envsize, "SHELL", shell);
1195 }
1196 if (getenv("TZ"))
1197 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1198
1199 /* Set custom environment options from RSA authentication. */
1200 while (custom_environment) {
1201 struct envstring *ce = custom_environment;
1202 char *s = ce->s;
1203 int i;
1204 for (i = 0; s[i] != '=' && s[i]; i++);
1205 if (s[i] == '=') {
1206 s[i] = 0;
1207 child_set_env(&env, &envsize, s, s + i + 1);
1208 }
1209 custom_environment = ce->next;
1210 xfree(ce->s);
1211 xfree(ce);
1212 }
1213
1214 snprintf(buf, sizeof buf, "%.50s %d %d",
1215 get_remote_ipaddr(), get_remote_port(), get_local_port());
1216 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1217
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001218 if (s->ttyfd != -1)
1219 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1220 if (s->term)
1221 child_set_env(&env, &envsize, "TERM", s->term);
1222 if (s->display)
1223 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001224 if (original_command)
1225 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1226 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001227
1228#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001229 if ((cp = getenv("AUTHSTATE")) != NULL)
1230 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1231 if ((cp = getenv("KRB5CCNAME")) != NULL)
1232 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1233 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001234#endif
1235
1236#ifdef KRB4
1237 {
1238 extern char *ticket;
1239
1240 if (ticket)
1241 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1242 }
1243#endif /* KRB4 */
1244
1245#ifdef USE_PAM
1246 /* Pull in any environment variables that may have been set by PAM. */
1247 do_pam_environment(&env, &envsize);
1248#endif /* USE_PAM */
1249
Damien Millerb38eff82000-04-01 11:09:21 +10001250 if (auth_get_socket_name() != NULL)
1251 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1252 auth_get_socket_name());
1253
1254 /* read $HOME/.ssh/environment. */
1255 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001256 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1257 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001258 read_environment_file(&env, &envsize, buf);
1259 }
1260 if (debug_flag) {
1261 /* dump the environment */
1262 fprintf(stderr, "Environment:\n");
1263 for (i = 0; env[i]; i++)
1264 fprintf(stderr, " %.200s\n", env[i]);
1265 }
Damien Millerad833b32000-08-23 10:46:23 +10001266 /* we have to stash the hostname before we close our socket. */
1267 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001268 hostname = get_remote_name_or_ip(utmp_len,
1269 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001270 /*
1271 * Close the connection descriptors; note that this is the child, and
1272 * the server will still have the socket open, and it is important
1273 * that we do not shutdown it. Note that the descriptors cannot be
1274 * closed before building the environment, as we call
1275 * get_remote_ipaddr there.
1276 */
1277 if (packet_get_connection_in() == packet_get_connection_out())
1278 close(packet_get_connection_in());
1279 else {
1280 close(packet_get_connection_in());
1281 close(packet_get_connection_out());
1282 }
1283 /*
1284 * Close all descriptors related to channels. They will still remain
1285 * open in the parent.
1286 */
1287 /* XXX better use close-on-exec? -markus */
1288 channel_close_all();
1289
1290 /*
1291 * Close any extra file descriptors. Note that there may still be
1292 * descriptors left by system functions. They will be closed later.
1293 */
1294 endpwent();
1295
1296 /*
1297 * Close any extra open file descriptors so that we don\'t have them
1298 * hanging around in clients. Note that we want to do this after
1299 * initgroups, because at least on Solaris 2.3 it leaves file
1300 * descriptors open.
1301 */
1302 for (i = 3; i < 64; i++)
1303 close(i);
1304
1305 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001306 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001307 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1308 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001309#ifdef HAVE_LOGIN_CAP
1310 if (login_getcapbool(lc, "requirehome", 0))
1311 exit(1);
1312#endif
1313 }
Damien Millerb38eff82000-04-01 11:09:21 +10001314
1315 /*
1316 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1317 * xauth are run in the proper environment.
1318 */
1319 environ = env;
1320
1321 /*
1322 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1323 * in this order).
1324 */
1325 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001326 /* ignore _PATH_SSH_USER_RC for subsystems */
1327 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001328 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1329 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001330 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001331 fprintf(stderr, "Running %s\n", cmd);
1332 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001333 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001334 if (do_xauth)
1335 fprintf(f, "%s %s\n", s->auth_proto,
1336 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001337 pclose(f);
1338 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001339 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001340 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001341 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001342 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001343 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1344 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001345 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001346 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001347 if (do_xauth)
1348 fprintf(f, "%s %s\n", s->auth_proto,
1349 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001350 pclose(f);
1351 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001352 fprintf(stderr, "Could not run %s\n",
1353 _PATH_SSH_SYSTEM_RC);
1354 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001355 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001356 char *screen = strchr(s->display, ':');
1357
1358 if (debug_flag) {
1359 fprintf(stderr,
1360 "Running %.100s add "
1361 "%.100s %.100s %.100s\n",
1362 options.xauth_location, s->display,
1363 s->auth_proto, s->auth_data);
1364 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001365 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001366 "Adding %.*s/unix%s %s %s\n",
1367 (int)(screen - s->display),
1368 s->display, screen,
1369 s->auth_proto, s->auth_data);
1370 }
1371 snprintf(cmd, sizeof cmd, "%s -q -",
1372 options.xauth_location);
1373 f = popen(cmd, "w");
1374 if (f) {
1375 fprintf(f, "add %s %s %s\n", s->display,
1376 s->auth_proto, s->auth_data);
1377 if (screen != NULL)
1378 fprintf(f, "add %.*s/unix%s %s %s\n",
1379 (int)(screen - s->display),
1380 s->display, screen,
1381 s->auth_proto,
1382 s->auth_data);
1383 pclose(f);
1384 } else {
1385 fprintf(stderr, "Could not run %s\n",
1386 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001387 }
1388 }
Damien Millerb38eff82000-04-01 11:09:21 +10001389 /* Get the last component of the shell name. */
1390 cp = strrchr(shell, '/');
1391 if (cp)
1392 cp++;
1393 else
1394 cp = shell;
1395 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001396
1397 /* restore SIGPIPE for child */
1398 signal(SIGPIPE, SIG_DFL);
1399
Damien Millerb38eff82000-04-01 11:09:21 +10001400 /*
1401 * If we have no command, execute the shell. In this case, the shell
1402 * name to be passed in argv[0] is preceded by '-' to indicate that
1403 * this is a login shell.
1404 */
1405 if (!command) {
1406 if (!options.use_login) {
1407 char buf[256];
1408
1409 /*
1410 * Check for mail if we have a tty and it was enabled
1411 * in server options.
1412 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001413 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001414 char *mailbox;
1415 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001416
Damien Millerb38eff82000-04-01 11:09:21 +10001417 mailbox = getenv("MAIL");
1418 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001419 if (stat(mailbox, &mailstat) != 0 ||
1420 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001421 printf("No mail.\n");
1422 else if (mailstat.st_mtime < mailstat.st_atime)
1423 printf("You have mail.\n");
1424 else
1425 printf("You have new mail.\n");
1426 }
1427 }
1428 /* Start the shell. Set initial character to '-'. */
1429 buf[0] = '-';
1430 strncpy(buf + 1, cp, sizeof(buf) - 1);
1431 buf[sizeof(buf) - 1] = 0;
1432
1433 /* Execute the shell. */
1434 argv[0] = buf;
1435 argv[1] = NULL;
1436 execve(shell, argv, env);
1437
1438 /* Executing the shell failed. */
1439 perror(shell);
1440 exit(1);
1441
1442 } else {
1443 /* Launch login(1). */
1444
Damien Millerad833b32000-08-23 10:46:23 +10001445 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001446#ifdef LOGIN_NEEDS_TERM
1447 s->term? s->term : "unknown",
1448#endif
Damien Miller942da032000-08-18 13:59:06 +10001449 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001450
1451 /* Login couldn't be executed, die. */
1452
1453 perror("login");
1454 exit(1);
1455 }
1456 }
1457 /*
1458 * Execute the command using the user's shell. This uses the -c
1459 * option to execute the command.
1460 */
1461 argv[0] = (char *) cp;
1462 argv[1] = "-c";
1463 argv[2] = (char *) command;
1464 argv[3] = NULL;
1465 execve(shell, argv, env);
1466 perror(shell);
1467 exit(1);
1468}
1469
1470Session *
1471session_new(void)
1472{
1473 int i;
1474 static int did_init = 0;
1475 if (!did_init) {
1476 debug("session_new: init");
1477 for(i = 0; i < MAX_SESSIONS; i++) {
1478 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001479 }
1480 did_init = 1;
1481 }
1482 for(i = 0; i < MAX_SESSIONS; i++) {
1483 Session *s = &sessions[i];
1484 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001485 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001486 s->chanid = -1;
1487 s->ptyfd = -1;
1488 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001489 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001490 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001491 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001492 return s;
1493 }
1494 }
1495 return NULL;
1496}
1497
1498void
1499session_dump(void)
1500{
1501 int i;
1502 for(i = 0; i < MAX_SESSIONS; i++) {
1503 Session *s = &sessions[i];
1504 debug("dump: used %d session %d %p channel %d pid %d",
1505 s->used,
1506 s->self,
1507 s,
1508 s->chanid,
1509 s->pid);
1510 }
1511}
1512
Damien Millerefb4afe2000-04-12 18:45:05 +10001513int
1514session_open(int chanid)
1515{
1516 Session *s = session_new();
1517 debug("session_open: channel %d", chanid);
1518 if (s == NULL) {
1519 error("no more sessions");
1520 return 0;
1521 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001522 s->pw = auth_get_user();
1523 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001524 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001525 debug("session_open: session %d: link with channel %d", s->self, chanid);
1526 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001527 return 1;
1528}
1529
1530Session *
1531session_by_channel(int id)
1532{
1533 int i;
1534 for(i = 0; i < MAX_SESSIONS; i++) {
1535 Session *s = &sessions[i];
1536 if (s->used && s->chanid == id) {
1537 debug("session_by_channel: session %d channel %d", i, id);
1538 return s;
1539 }
1540 }
1541 debug("session_by_channel: unknown channel %d", id);
1542 session_dump();
1543 return NULL;
1544}
1545
1546Session *
1547session_by_pid(pid_t pid)
1548{
1549 int i;
1550 debug("session_by_pid: pid %d", pid);
1551 for(i = 0; i < MAX_SESSIONS; i++) {
1552 Session *s = &sessions[i];
1553 if (s->used && s->pid == pid)
1554 return s;
1555 }
1556 error("session_by_pid: unknown pid %d", pid);
1557 session_dump();
1558 return NULL;
1559}
1560
1561int
1562session_window_change_req(Session *s)
1563{
1564 s->col = packet_get_int();
1565 s->row = packet_get_int();
1566 s->xpixel = packet_get_int();
1567 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001568 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001569 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1570 return 1;
1571}
1572
1573int
1574session_pty_req(Session *s)
1575{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001576 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001577 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001578
Ben Lindstrom49c12602001-06-13 04:37:36 +00001579 if (no_pty_flag) {
1580 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001581 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001582 }
1583 if (s->ttyfd != -1) {
1584 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001585 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001586 }
1587
Damien Millerefb4afe2000-04-12 18:45:05 +10001588 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001589
1590 if (compat20) {
1591 s->col = packet_get_int();
1592 s->row = packet_get_int();
1593 } else {
1594 s->row = packet_get_int();
1595 s->col = packet_get_int();
1596 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001597 s->xpixel = packet_get_int();
1598 s->ypixel = packet_get_int();
1599
1600 if (strcmp(s->term, "") == 0) {
1601 xfree(s->term);
1602 s->term = NULL;
1603 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001604
Damien Millerefb4afe2000-04-12 18:45:05 +10001605 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001606 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001607 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001608 if (s->term)
1609 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001610 s->term = NULL;
1611 s->ptyfd = -1;
1612 s->ttyfd = -1;
1613 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001614 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001615 }
1616 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001617
1618 /* for SSH1 the tty modes length is not given */
1619 if (!compat20)
1620 n_bytes = packet_remaining();
1621 tty_parse_modes(s->ttyfd, &n_bytes);
1622
Damien Millerefb4afe2000-04-12 18:45:05 +10001623 /*
1624 * Add a cleanup function to clear the utmp entry and record logout
1625 * time in case we call fatal() (e.g., the connection gets closed).
1626 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001627 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001628 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001629
1630 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001631 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1632
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001633 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001634 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001635 return 1;
1636}
1637
Damien Millerbd483e72000-04-30 10:00:53 +10001638int
1639session_subsystem_req(Session *s)
1640{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001641 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001642 int success = 0;
1643 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001644 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001645
1646 packet_done();
1647 log("subsystem request for %s", subsys);
1648
Damien Millerf6d9e222000-06-18 14:50:44 +10001649 for (i = 0; i < options.num_subsystems; i++) {
1650 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1651 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001652 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001653 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001654 success = 1;
1655 }
1656 }
1657
1658 if (!success)
1659 log("subsystem request for %s failed, subsystem not found", subsys);
1660
Damien Millerbd483e72000-04-30 10:00:53 +10001661 xfree(subsys);
1662 return success;
1663}
1664
1665int
1666session_x11_req(Session *s)
1667{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001668 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001669
1670 s->single_connection = packet_get_char();
1671 s->auth_proto = packet_get_string(NULL);
1672 s->auth_data = packet_get_string(NULL);
1673 s->screen = packet_get_int();
1674 packet_done();
1675
Ben Lindstrom768176b2001-06-09 01:29:12 +00001676 success = session_setup_x11fwd(s);
1677 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001678 xfree(s->auth_proto);
1679 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001680 s->auth_proto = NULL;
1681 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001682 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001683 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001684}
1685
Damien Millerf6d9e222000-06-18 14:50:44 +10001686int
1687session_shell_req(Session *s)
1688{
Damien Millerf6d9e222000-06-18 14:50:44 +10001689 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001690 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001691 return 1;
1692}
1693
1694int
1695session_exec_req(Session *s)
1696{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001697 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001698 char *command = packet_get_string(&len);
1699 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001700 do_exec(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001701 return 1;
1702}
1703
Damien Miller0bc1bd82000-11-13 22:57:25 +11001704int
1705session_auth_agent_req(Session *s)
1706{
1707 static int called = 0;
1708 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001709 if (no_agent_forwarding_flag) {
1710 debug("session_auth_agent_req: no_agent_forwarding_flag");
1711 return 0;
1712 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001713 if (called) {
1714 return 0;
1715 } else {
1716 called = 1;
1717 return auth_input_request_forwarding(s->pw);
1718 }
1719}
1720
Damien Millerefb4afe2000-04-12 18:45:05 +10001721void
1722session_input_channel_req(int id, void *arg)
1723{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001724 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001725 int reply;
1726 int success = 0;
1727 char *rtype;
1728 Session *s;
1729 Channel *c;
1730
1731 rtype = packet_get_string(&len);
1732 reply = packet_get_char();
1733
1734 s = session_by_channel(id);
1735 if (s == NULL)
1736 fatal("session_input_channel_req: channel %d: no session", id);
1737 c = channel_lookup(id);
1738 if (c == NULL)
1739 fatal("session_input_channel_req: channel %d: bad channel", id);
1740
1741 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1742 s->self, id, rtype, reply);
1743
1744 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001745 * a session is in LARVAL state until a shell, a command
1746 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001747 */
1748 if (c->type == SSH_CHANNEL_LARVAL) {
1749 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001750 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001751 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001752 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001753 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001754 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001755 } else if (strcmp(rtype, "x11-req") == 0) {
1756 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001757 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1758 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001759 } else if (strcmp(rtype, "subsystem") == 0) {
1760 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001761 }
1762 }
1763 if (strcmp(rtype, "window-change") == 0) {
1764 success = session_window_change_req(s);
1765 }
1766
1767 if (reply) {
1768 packet_start(success ?
1769 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1770 packet_put_int(c->remote_id);
1771 packet_send();
1772 }
1773 xfree(rtype);
1774}
1775
1776void
1777session_set_fds(Session *s, int fdin, int fdout, int fderr)
1778{
1779 if (!compat20)
1780 fatal("session_set_fds: called for proto != 2.0");
1781 /*
1782 * now that have a child and a pipe to the child,
1783 * we can activate our channel and register the fd's
1784 */
1785 if (s->chanid == -1)
1786 fatal("no channel for session %d", s->self);
1787 channel_set_fds(s->chanid,
1788 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001789 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1790 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001791}
1792
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001793/*
1794 * Function to perform pty cleanup. Also called if we get aborted abnormally
1795 * (e.g., due to a dropped connection).
1796 */
Damien Millerb38eff82000-04-01 11:09:21 +10001797void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001798session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001799{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001800 Session *s = session;
1801
1802 if (s == NULL) {
1803 error("session_pty_cleanup: no session");
1804 return;
1805 }
1806 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001807 return;
1808
Kevin Steves43cdef32001-02-11 14:12:08 +00001809 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001810
Damien Millerb38eff82000-04-01 11:09:21 +10001811 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001812 if (s->pid != 0)
1813 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001814
1815 /* Release the pseudo-tty. */
1816 pty_release(s->tty);
1817
1818 /*
1819 * Close the server side of the socket pairs. We must do this after
1820 * the pty cleanup, so that another process doesn't get this pty
1821 * while we're still cleaning up.
1822 */
1823 if (close(s->ptymaster) < 0)
1824 error("close(s->ptymaster): %s", strerror(errno));
1825}
Damien Millerefb4afe2000-04-12 18:45:05 +10001826
1827void
1828session_exit_message(Session *s, int status)
1829{
1830 Channel *c;
1831 if (s == NULL)
1832 fatal("session_close: no session");
1833 c = channel_lookup(s->chanid);
1834 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001835 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001836 s->self, s->chanid);
1837 debug("session_exit_message: session %d channel %d pid %d",
1838 s->self, s->chanid, s->pid);
1839
1840 if (WIFEXITED(status)) {
1841 channel_request_start(s->chanid,
1842 "exit-status", 0);
1843 packet_put_int(WEXITSTATUS(status));
1844 packet_send();
1845 } else if (WIFSIGNALED(status)) {
1846 channel_request_start(s->chanid,
1847 "exit-signal", 0);
1848 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001849#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001850 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001851#else /* WCOREDUMP */
1852 packet_put_char(0);
1853#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001854 packet_put_cstring("");
1855 packet_put_cstring("");
1856 packet_send();
1857 } else {
1858 /* Some weird exit cause. Just exit. */
1859 packet_disconnect("wait returned status %04x.", status);
1860 }
1861
1862 /* disconnect channel */
1863 debug("session_exit_message: release channel %d", s->chanid);
1864 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001865 /*
1866 * emulate a write failure with 'chan_write_failed', nobody will be
1867 * interested in data we write.
1868 * Note that we must not call 'chan_read_failed', since there could
1869 * be some more data waiting in the pipe.
1870 */
Damien Millerbd483e72000-04-30 10:00:53 +10001871 if (c->ostate != CHAN_OUTPUT_CLOSED)
1872 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001873 s->chanid = -1;
1874}
1875
1876void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001877session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001878{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001879 debug("session_close: session %d pid %d", s->self, s->pid);
1880 if (s->ttyfd != -1) {
1881 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1882 session_pty_cleanup(s);
1883 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001884 if (s->term)
1885 xfree(s->term);
1886 if (s->display)
1887 xfree(s->display);
1888 if (s->auth_data)
1889 xfree(s->auth_data);
1890 if (s->auth_proto)
1891 xfree(s->auth_proto);
1892 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001893 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001894}
1895
1896void
1897session_close_by_pid(pid_t pid, int status)
1898{
1899 Session *s = session_by_pid(pid);
1900 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001901 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001902 return;
1903 }
1904 if (s->chanid != -1)
1905 session_exit_message(s, status);
1906 session_close(s);
1907}
1908
1909/*
1910 * this is called when a channel dies before
1911 * the session 'child' itself dies
1912 */
1913void
1914session_close_by_channel(int id, void *arg)
1915{
1916 Session *s = session_by_channel(id);
1917 if (s == NULL) {
1918 debug("session_close_by_channel: no session for channel %d", id);
1919 return;
1920 }
1921 /* disconnect channel */
1922 channel_cancel_cleanup(s->chanid);
1923 s->chanid = -1;
1924
1925 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1926 if (s->pid == 0) {
1927 /* close session immediately */
1928 session_close(s);
1929 } else {
1930 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001931 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001932 error("session_close_by_channel: kill %d: %s",
1933 s->pid, strerror(errno));
1934 }
1935}
1936
Damien Millere247cc42000-05-07 12:03:14 +10001937char *
1938session_tty_list(void)
1939{
1940 static char buf[1024];
1941 int i;
1942 buf[0] = '\0';
1943 for(i = 0; i < MAX_SESSIONS; i++) {
1944 Session *s = &sessions[i];
1945 if (s->used && s->ttyfd != -1) {
1946 if (buf[0] != '\0')
1947 strlcat(buf, ",", sizeof buf);
1948 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1949 }
1950 }
1951 if (buf[0] == '\0')
1952 strlcpy(buf, "notty", sizeof buf);
1953 return buf;
1954}
1955
1956void
1957session_proctitle(Session *s)
1958{
1959 if (s->pw == NULL)
1960 error("no user for session %d", s->self);
1961 else
1962 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1963}
1964
Ben Lindstrom768176b2001-06-09 01:29:12 +00001965int
1966session_setup_x11fwd(Session *s)
1967{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001968 struct stat st;
1969
1970 if (no_x11_forwarding_flag) {
1971 packet_send_debug("X11 forwarding disabled in user configuration file.");
1972 return 0;
1973 }
1974 if (!options.x11_forwarding) {
1975 debug("X11 forwarding disabled in server configuration file.");
1976 return 0;
1977 }
1978 if (!options.xauth_location ||
1979 (stat(options.xauth_location, &st) == -1)) {
1980 packet_send_debug("No xauth program; cannot forward with spoofing.");
1981 return 0;
1982 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00001983 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00001984 debug("X11 display already set.");
1985 return 0;
1986 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001987 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1988 if (s->display == NULL) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00001989 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00001990 return 0;
1991 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001992 return 1;
1993}
1994
Damien Millerefb4afe2000-04-12 18:45:05 +10001995void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001996do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10001997{
Damien Millerefb4afe2000-04-12 18:45:05 +10001998 server_loop2();
1999}