blob: fbdc419eb5d73d4d6e67487e8d275c8ee3c58cc1 [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 Lindstrom0a7ca6c2001-06-21 03:17:42 +000036RCSID("$OpenBSD: session.c,v 1.92 2001/06/19 15:40:45 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. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000153const char *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);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000312 do_exec(s, command);
313 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000314 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000315 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000316 }
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000317 packet_done();
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
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000611 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000612}
613
614
Damien Miller942da032000-08-18 13:59:06 +1000615/* administrative, login(1)-like work */
616void
Damien Miller69b69aa2000-10-28 14:19:58 +1100617do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000618{
Damien Miller942da032000-08-18 13:59:06 +1000619 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100620 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000621 socklen_t fromlen;
622 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000623 time_t last_login_time;
624 struct passwd * pw = s->pw;
625 pid_t pid = getpid();
626
627 /*
628 * Get IP address of client. If the connection is not a socket, let
629 * the address be 0.0.0.0.
630 */
631 memset(&from, 0, sizeof(from));
632 if (packet_connection_is_on_socket()) {
633 fromlen = sizeof(from);
634 if (getpeername(packet_get_connection_in(),
635 (struct sockaddr *) & from, &fromlen) < 0) {
636 debug("getpeername: %.100s", strerror(errno));
637 fatal_cleanup();
638 }
639 }
640
Damien Miller7b28dc52000-09-05 13:34:53 +1100641 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000642 if (options.print_lastlog) {
643 hostname[0] = '\0';
644 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
645 hostname, sizeof(hostname));
646 }
Damien Millere4340be2000-09-16 13:29:08 +1100647
Damien Miller942da032000-08-18 13:59:06 +1000648 /* Record that there was a login on that tty from the remote host. */
649 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000650 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
651 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000652
Kevin Steves092f2ef2000-10-14 13:36:13 +0000653#ifdef USE_PAM
654 /*
655 * If password change is needed, do it now.
656 * This needs to occur before the ~/.hushlogin check.
657 */
Damien Miller646aa602001-02-15 11:51:32 +1100658 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000659 print_pam_messages();
660 do_pam_chauthtok();
661 }
662#endif
663
Damien Millercf205e82001-04-16 18:29:15 +1000664 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000665 return;
666
667#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100668 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000669 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000670#endif /* USE_PAM */
671#ifdef WITH_AIXAUTHENTICATE
672 if (aixloginmsg && *aixloginmsg)
673 printf("%s\n", aixloginmsg);
674#endif /* WITH_AIXAUTHENTICATE */
675
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000676 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000677 time_string = ctime(&last_login_time);
678 if (strchr(time_string, '\n'))
679 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000680 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000681 printf("Last login: %s\r\n", time_string);
682 else
Damien Millere4340be2000-09-16 13:29:08 +1100683 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000684 }
Damien Millercf205e82001-04-16 18:29:15 +1000685
686 do_motd();
687}
688
689/*
690 * Display the message of the day.
691 */
692void
693do_motd(void)
694{
695 FILE *f;
696 char buf[256];
697
Damien Miller942da032000-08-18 13:59:06 +1000698 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000699#ifdef HAVE_LOGIN_CAP
700 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
701 "/etc/motd"), "r");
702#else
Damien Miller942da032000-08-18 13:59:06 +1000703 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000704#endif
Damien Miller942da032000-08-18 13:59:06 +1000705 if (f) {
706 while (fgets(buf, sizeof(buf), f))
707 fputs(buf, stdout);
708 fclose(f);
709 }
710 }
711}
712
Damien Millercf205e82001-04-16 18:29:15 +1000713
714/*
715 * Check for quiet login, either .hushlogin or command given.
716 */
717int
718check_quietlogin(Session *s, const char *command)
719{
720 char buf[256];
721 struct passwd * pw = s->pw;
722 struct stat st;
723
724 /* Return 1 if .hushlogin exists or a command given. */
725 if (command != NULL)
726 return 1;
727 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
728#ifdef HAVE_LOGIN_CAP
729 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
730 return 1;
731#else
732 if (stat(buf, &st) >= 0)
733 return 1;
734#endif
735 return 0;
736}
737
Damien Millerb38eff82000-04-01 11:09:21 +1000738/*
739 * Sets the value of the given variable in the environment. If the variable
740 * already exists, its value is overriden.
741 */
Damien Miller4af51302000-04-16 11:18:38 +1000742void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000743child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000744 const char *value)
745{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000746 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000747 char **env;
748
749 /*
750 * Find the slot where the value should be stored. If the variable
751 * already exists, we reuse the slot; otherwise we append a new slot
752 * at the end of the array, expanding if necessary.
753 */
754 env = *envp;
755 namelen = strlen(name);
756 for (i = 0; env[i]; i++)
757 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
758 break;
759 if (env[i]) {
760 /* Reuse the slot. */
761 xfree(env[i]);
762 } else {
763 /* New variable. Expand if necessary. */
764 if (i >= (*envsizep) - 1) {
765 (*envsizep) += 50;
766 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
767 }
768 /* Need to set the NULL pointer at end of array beyond the new slot. */
769 env[i + 1] = NULL;
770 }
771
772 /* Allocate space and format the variable in the appropriate slot. */
773 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
774 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
775}
776
777/*
778 * Reads environment variables from the given file and adds/overrides them
779 * into the environment. If the file does not exist, this does nothing.
780 * Otherwise, it must consist of empty lines, comments (line starts with '#')
781 * and assignments of the form name=value. No other forms are allowed.
782 */
Damien Miller4af51302000-04-16 11:18:38 +1000783void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000784read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000785 const char *filename)
786{
787 FILE *f;
788 char buf[4096];
789 char *cp, *value;
790
791 f = fopen(filename, "r");
792 if (!f)
793 return;
794
795 while (fgets(buf, sizeof(buf), f)) {
796 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
797 ;
798 if (!*cp || *cp == '#' || *cp == '\n')
799 continue;
800 if (strchr(cp, '\n'))
801 *strchr(cp, '\n') = '\0';
802 value = strchr(cp, '=');
803 if (value == NULL) {
804 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
805 continue;
806 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000807 /*
808 * Replace the equals sign by nul, and advance value to
809 * the value string.
810 */
Damien Millerb38eff82000-04-01 11:09:21 +1000811 *value = '\0';
812 value++;
813 child_set_env(env, envsize, cp, value);
814 }
815 fclose(f);
816}
817
818#ifdef USE_PAM
819/*
820 * Sets any environment variables which have been specified by PAM
821 */
822void do_pam_environment(char ***env, int *envsize)
823{
824 char *equals, var_name[512], var_val[512];
825 char **pam_env;
826 int i;
827
828 if ((pam_env = fetch_pam_environment()) == NULL)
829 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000830
Damien Millerb38eff82000-04-01 11:09:21 +1000831 for(i = 0; pam_env[i] != NULL; i++) {
832 if ((equals = strstr(pam_env[i], "=")) == NULL)
833 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000834
Damien Millerb38eff82000-04-01 11:09:21 +1000835 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
836 memset(var_name, '\0', sizeof(var_name));
837 memset(var_val, '\0', sizeof(var_val));
838
839 strncpy(var_name, pam_env[i], equals - pam_env[i]);
840 strcpy(var_val, equals + 1);
841
Damien Millercb5e44a2000-09-29 12:12:36 +1100842 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000843
844 child_set_env(env, envsize, var_name, var_val);
845 }
846 }
847}
848#endif /* USE_PAM */
849
Damien Millercb5e44a2000-09-29 12:12:36 +1100850#ifdef HAVE_CYGWIN
851void copy_environment(char ***env, int *envsize)
852{
853 char *equals, var_name[512], var_val[512];
854 int i;
855
856 for(i = 0; environ[i] != NULL; i++) {
857 if ((equals = strstr(environ[i], "=")) == NULL)
858 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000859
Damien Millercb5e44a2000-09-29 12:12:36 +1100860 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
861 memset(var_name, '\0', sizeof(var_name));
862 memset(var_val, '\0', sizeof(var_val));
863
864 strncpy(var_name, environ[i], equals - environ[i]);
865 strcpy(var_val, equals + 1);
866
867 debug3("Copy environment: %s=%s", var_name, var_val);
868
869 child_set_env(env, envsize, var_name, var_val);
870 }
871 }
872}
873#endif
874
Damien Miller5fc85652000-07-09 23:53:07 +1000875#if defined(HAVE_GETUSERATTR)
876/*
877 * AIX-specific login initialisation
878 */
879void set_limit(char *user, char *soft, char *hard, int resource, int mult)
880{
881 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000882 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000883
884 getrlimit(resource, &rlim);
885
Damien Miller65964d62000-07-11 09:16:22 +1000886 slim = 0;
887 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
888 if (slim < 0) {
889 rlim.rlim_cur = RLIM_INFINITY;
890 } else if (slim != 0) {
891 /* See the wackiness below */
892 if (rlim.rlim_cur == slim * mult)
893 slim = 0;
894 else
895 rlim.rlim_cur = slim * mult;
896 }
897 }
Damien Miller5fc85652000-07-09 23:53:07 +1000898
Damien Miller65964d62000-07-11 09:16:22 +1000899 hlim = 0;
900 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
901 if (hlim < 0) {
902 rlim.rlim_max = RLIM_INFINITY;
903 } else if (hlim != 0) {
904 rlim.rlim_max = hlim * mult;
905 }
906 }
Damien Miller5fc85652000-07-09 23:53:07 +1000907
Damien Miller65964d62000-07-11 09:16:22 +1000908 /*
909 * XXX For cpu and fsize the soft limit is set to the hard limit
910 * if the hard limit is left at its default value and the soft limit
911 * is changed from its default value, either by requesting it
912 * (slim == 0) or by setting it to the current default. At least
913 * that's how rlogind does it. If you're confused you're not alone.
914 * Bug or feature? AIX 4.3.1.2
915 */
916 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
917 && hlim == 0 && slim != 0)
918 rlim.rlim_max = rlim.rlim_cur;
919 /* A specified hard limit limits the soft limit */
920 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
921 rlim.rlim_cur = rlim.rlim_max;
922 /* A soft limit can increase a hard limit */
923 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000924 rlim.rlim_max = rlim.rlim_cur;
925
926 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000927 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000928}
929
930void set_limits_from_userattr(char *user)
931{
932 int mask;
933 char buf[16];
934
935 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
936 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
937 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
938 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
939 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
940 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
941#if defined(S_UNOFILE)
942 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
943#endif
944
945 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
946 /* Convert decimal to octal */
947 (void) snprintf(buf, sizeof(buf), "%d", mask);
948 if (sscanf(buf, "%o", &mask) == 1)
949 umask(mask);
950 }
951}
952#endif /* defined(HAVE_GETUSERATTR) */
953
Damien Millerb38eff82000-04-01 11:09:21 +1000954/*
955 * Performs common processing for the child, such as setting up the
956 * environment, closing extra file descriptors, setting the user and group
957 * ids, and executing the command or shell.
958 */
Damien Miller4af51302000-04-16 11:18:38 +1000959void
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000960do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000961{
Damien Miller7b28dc52000-09-05 13:34:53 +1100962 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000963 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000964 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +1000965 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +1000966 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000967 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000968 char **env;
969 extern char **environ;
970 struct stat st;
971 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000972 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +1000973#ifdef WITH_IRIX_PROJECT
974 prid_t projid;
975#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +0000976#ifdef WITH_IRIX_JOBS
977 jid_t jid = 0;
978#else
979#ifdef WITH_IRIX_ARRAY
980 int jid = 0;
981#endif /* WITH_IRIX_ARRAY */
982#endif /* WITH_IRIX_JOBS */
983
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000984 do_xauth =
985 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
986
Ben Lindstrom005dd222001-04-18 15:29:33 +0000987 /* remove hostkey from the child's memory */
988 destroy_sensitive_data();
989
Damien Millerd3a18572000-06-07 19:55:44 +1000990 /* login(1) is only called if we execute the login shell */
991 if (options.use_login && command != NULL)
992 options.use_login = 0;
993
Damien Miller364a9bd2001-04-16 18:37:05 +1000994#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +1000995 if (!options.use_login) {
996# ifdef HAVE_LOGIN_CAP
997 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
998 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
999 _PATH_NOLOGIN), "r");
1000# else /* HAVE_LOGIN_CAP */
1001 if (pw->pw_uid)
1002 f = fopen(_PATH_NOLOGIN, "r");
1003# endif /* HAVE_LOGIN_CAP */
1004 if (f) {
1005 /* /etc/nologin exists. Print its contents and exit. */
1006 while (fgets(buf, sizeof(buf), f))
1007 fputs(buf, stderr);
1008 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001009 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001010 }
Damien Millerb38eff82000-04-01 11:09:21 +10001011 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001012#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001013
Damien Millerad833b32000-08-23 10:46:23 +10001014 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001015 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1016 switch, so we let login(1) to this for us. */
1017 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001018#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001019 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001020 if (!check_quietlogin(s, command))
1021 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001022#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001023#ifdef HAVE_CYGWIN
1024 if (is_winnt) {
1025#else
Damien Millerb38eff82000-04-01 11:09:21 +10001026 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001027#endif
Damien Millerad833b32000-08-23 10:46:23 +10001028# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001029 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001030# endif /* HAVE_GETUSERATTR */
1031# ifdef HAVE_LOGIN_CAP
1032 if (setusercontext(lc, pw, pw->pw_uid,
1033 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1034 perror("unable to set user context");
1035 exit(1);
1036 }
1037# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001038#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1039 /* Sets login uid for accounting */
1040 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1041 error("setluid: %s", strerror(errno));
1042#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1043
Damien Millerad833b32000-08-23 10:46:23 +10001044 if (setlogin(pw->pw_name) < 0)
1045 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001046 if (setgid(pw->pw_gid) < 0) {
1047 perror("setgid");
1048 exit(1);
1049 }
1050 /* Initialize the group list. */
1051 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1052 perror("initgroups");
1053 exit(1);
1054 }
1055 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001056# ifdef USE_PAM
1057 /*
1058 * PAM credentials may take the form of
1059 * supplementary groups. These will have been
1060 * wiped by the above initgroups() call.
1061 * Reestablish them here.
1062 */
1063 do_pam_setcred(0);
1064# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001065# ifdef WITH_IRIX_JOBS
1066 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1067 if (jid == -1) {
1068 fatal("Failed to create job container: %.100s",
1069 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001070 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001071# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001072# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001073 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001074 if (jid == 0) {
1075 if (newarraysess() != 0)
1076 fatal("Failed to set up new array session: %.100s",
1077 strerror(errno));
1078 }
Damien Millerad833b32000-08-23 10:46:23 +10001079# endif /* WITH_IRIX_ARRAY */
1080# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001081 /* initialize irix project info */
1082 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1083 debug("Failed to get project id, using projid 0");
1084 projid = 0;
1085 }
Damien Miller91606b12000-06-28 08:22:29 +10001086 if (setprid(projid))
1087 fatal("Failed to initialize project %d for %s: %.100s",
1088 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001089# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001090#ifdef WITH_IRIX_AUDIT
1091 if (sysconf(_SC_AUDIT)) {
1092 debug("Setting sat id to %d", (int) pw->pw_uid);
1093 if (satsetid(pw->pw_uid))
1094 debug("error setting satid: %.100s", strerror(errno));
1095 }
1096#endif /* WITH_IRIX_AUDIT */
1097
Damien Miller168a7002001-03-17 10:29:50 +11001098#ifdef _AIX
1099 /*
1100 * AIX has a "usrinfo" area where logname and
1101 * other stuff is stored - a few applications
1102 * actually use this and die if it's not set
1103 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001104 if (s->ttyfd == -1)
1105 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001106 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001107 2 * strlen(pw->pw_name));
1108 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001109 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001110 if (usrinfo(SETUINFO, cp, i) == -1)
1111 fatal("Couldn't set usrinfo: %s",
1112 strerror(errno));
1113 debug3("AIX/UsrInfo: set len %d", i);
1114 xfree(cp);
1115#endif
1116
Damien Millerb38eff82000-04-01 11:09:21 +10001117 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001118 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001119# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001120 }
Damien Millerad833b32000-08-23 10:46:23 +10001121#endif /* HAVE_OSF_SIA */
1122
Damien Millerbac2d8a2000-09-05 16:13:06 +11001123#ifdef HAVE_CYGWIN
1124 if (is_winnt)
1125#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001126 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001127 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001128 }
1129 /*
1130 * Get the shell from the password data. An empty shell field is
1131 * legal, and means /bin/sh.
1132 */
1133 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001134#ifdef HAVE_LOGIN_CAP
1135 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1136#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001137
1138#ifdef AFS
1139 /* Try to get AFS tokens for the local cell. */
1140 if (k_hasafs()) {
1141 char cell[64];
1142
1143 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1144 krb_afslog(cell, 0);
1145
1146 krb_afslog(0, 0);
1147 }
1148#endif /* AFS */
1149
1150 /* Initialize the environment. */
1151 envsize = 100;
1152 env = xmalloc(envsize * sizeof(char *));
1153 env[0] = NULL;
1154
Damien Millerbac2d8a2000-09-05 16:13:06 +11001155#ifdef HAVE_CYGWIN
1156 /*
1157 * The Windows environment contains some setting which are
1158 * important for a running system. They must not be dropped.
1159 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001160 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001161#endif
1162
Damien Millerb38eff82000-04-01 11:09:21 +10001163 if (!options.use_login) {
1164 /* Set basic environment. */
1165 child_set_env(&env, &envsize, "USER", pw->pw_name);
1166 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1167 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001168#ifdef HAVE_LOGIN_CAP
1169 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1170 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001171#else /* HAVE_LOGIN_CAP */
1172# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001173 /*
1174 * There's no standard path on Windows. The path contains
1175 * important components pointing to the system directories,
1176 * needed for loading shared libraries. So the path better
1177 * remains intact here.
1178 */
Damien Millerb38eff82000-04-01 11:09:21 +10001179 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001180# endif /* HAVE_CYGWIN */
1181#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001182
1183 snprintf(buf, sizeof buf, "%.200s/%.50s",
1184 _PATH_MAILDIR, pw->pw_name);
1185 child_set_env(&env, &envsize, "MAIL", buf);
1186
1187 /* Normal systems set SHELL by default. */
1188 child_set_env(&env, &envsize, "SHELL", shell);
1189 }
1190 if (getenv("TZ"))
1191 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1192
1193 /* Set custom environment options from RSA authentication. */
1194 while (custom_environment) {
1195 struct envstring *ce = custom_environment;
1196 char *s = ce->s;
1197 int i;
1198 for (i = 0; s[i] != '=' && s[i]; i++);
1199 if (s[i] == '=') {
1200 s[i] = 0;
1201 child_set_env(&env, &envsize, s, s + i + 1);
1202 }
1203 custom_environment = ce->next;
1204 xfree(ce->s);
1205 xfree(ce);
1206 }
1207
1208 snprintf(buf, sizeof buf, "%.50s %d %d",
1209 get_remote_ipaddr(), get_remote_port(), get_local_port());
1210 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1211
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001212 if (s->ttyfd != -1)
1213 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1214 if (s->term)
1215 child_set_env(&env, &envsize, "TERM", s->term);
1216 if (s->display)
1217 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001218 if (original_command)
1219 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1220 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001221
1222#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001223 if ((cp = getenv("AUTHSTATE")) != NULL)
1224 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1225 if ((cp = getenv("KRB5CCNAME")) != NULL)
1226 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1227 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001228#endif
1229
1230#ifdef KRB4
1231 {
1232 extern char *ticket;
1233
1234 if (ticket)
1235 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1236 }
1237#endif /* KRB4 */
1238
1239#ifdef USE_PAM
1240 /* Pull in any environment variables that may have been set by PAM. */
1241 do_pam_environment(&env, &envsize);
1242#endif /* USE_PAM */
1243
Damien Millerb38eff82000-04-01 11:09:21 +10001244 if (auth_get_socket_name() != NULL)
1245 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1246 auth_get_socket_name());
1247
1248 /* read $HOME/.ssh/environment. */
1249 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001250 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1251 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001252 read_environment_file(&env, &envsize, buf);
1253 }
1254 if (debug_flag) {
1255 /* dump the environment */
1256 fprintf(stderr, "Environment:\n");
1257 for (i = 0; env[i]; i++)
1258 fprintf(stderr, " %.200s\n", env[i]);
1259 }
Damien Millerad833b32000-08-23 10:46:23 +10001260 /* we have to stash the hostname before we close our socket. */
1261 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001262 hostname = get_remote_name_or_ip(utmp_len,
1263 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001264 /*
1265 * Close the connection descriptors; note that this is the child, and
1266 * the server will still have the socket open, and it is important
1267 * that we do not shutdown it. Note that the descriptors cannot be
1268 * closed before building the environment, as we call
1269 * get_remote_ipaddr there.
1270 */
1271 if (packet_get_connection_in() == packet_get_connection_out())
1272 close(packet_get_connection_in());
1273 else {
1274 close(packet_get_connection_in());
1275 close(packet_get_connection_out());
1276 }
1277 /*
1278 * Close all descriptors related to channels. They will still remain
1279 * open in the parent.
1280 */
1281 /* XXX better use close-on-exec? -markus */
1282 channel_close_all();
1283
1284 /*
1285 * Close any extra file descriptors. Note that there may still be
1286 * descriptors left by system functions. They will be closed later.
1287 */
1288 endpwent();
1289
1290 /*
1291 * Close any extra open file descriptors so that we don\'t have them
1292 * hanging around in clients. Note that we want to do this after
1293 * initgroups, because at least on Solaris 2.3 it leaves file
1294 * descriptors open.
1295 */
1296 for (i = 3; i < 64; i++)
1297 close(i);
1298
1299 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001300 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001301 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1302 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001303#ifdef HAVE_LOGIN_CAP
1304 if (login_getcapbool(lc, "requirehome", 0))
1305 exit(1);
1306#endif
1307 }
Damien Millerb38eff82000-04-01 11:09:21 +10001308
1309 /*
1310 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1311 * xauth are run in the proper environment.
1312 */
1313 environ = env;
1314
1315 /*
1316 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1317 * in this order).
1318 */
1319 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001320 /* ignore _PATH_SSH_USER_RC for subsystems */
1321 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001322 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1323 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001324 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001325 fprintf(stderr, "Running %s\n", cmd);
1326 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001327 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001328 if (do_xauth)
1329 fprintf(f, "%s %s\n", s->auth_proto,
1330 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001331 pclose(f);
1332 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001333 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001334 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001335 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001336 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001337 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1338 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001339 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001340 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001341 if (do_xauth)
1342 fprintf(f, "%s %s\n", s->auth_proto,
1343 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001344 pclose(f);
1345 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001346 fprintf(stderr, "Could not run %s\n",
1347 _PATH_SSH_SYSTEM_RC);
1348 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001349 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001350 char *screen = strchr(s->display, ':');
1351
1352 if (debug_flag) {
1353 fprintf(stderr,
1354 "Running %.100s add "
1355 "%.100s %.100s %.100s\n",
1356 options.xauth_location, s->display,
1357 s->auth_proto, s->auth_data);
1358 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001359 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001360 "Adding %.*s/unix%s %s %s\n",
1361 (int)(screen - s->display),
1362 s->display, screen,
1363 s->auth_proto, s->auth_data);
1364 }
1365 snprintf(cmd, sizeof cmd, "%s -q -",
1366 options.xauth_location);
1367 f = popen(cmd, "w");
1368 if (f) {
1369 fprintf(f, "add %s %s %s\n", s->display,
1370 s->auth_proto, s->auth_data);
1371 if (screen != NULL)
1372 fprintf(f, "add %.*s/unix%s %s %s\n",
1373 (int)(screen - s->display),
1374 s->display, screen,
1375 s->auth_proto,
1376 s->auth_data);
1377 pclose(f);
1378 } else {
1379 fprintf(stderr, "Could not run %s\n",
1380 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001381 }
1382 }
Damien Millerb38eff82000-04-01 11:09:21 +10001383 /* Get the last component of the shell name. */
1384 cp = strrchr(shell, '/');
1385 if (cp)
1386 cp++;
1387 else
1388 cp = shell;
1389 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001390
1391 /* restore SIGPIPE for child */
1392 signal(SIGPIPE, SIG_DFL);
1393
Damien Millerb38eff82000-04-01 11:09:21 +10001394 /*
1395 * If we have no command, execute the shell. In this case, the shell
1396 * name to be passed in argv[0] is preceded by '-' to indicate that
1397 * this is a login shell.
1398 */
1399 if (!command) {
1400 if (!options.use_login) {
1401 char buf[256];
1402
1403 /*
1404 * Check for mail if we have a tty and it was enabled
1405 * in server options.
1406 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001407 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001408 char *mailbox;
1409 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001410
Damien Millerb38eff82000-04-01 11:09:21 +10001411 mailbox = getenv("MAIL");
1412 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001413 if (stat(mailbox, &mailstat) != 0 ||
1414 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001415 printf("No mail.\n");
1416 else if (mailstat.st_mtime < mailstat.st_atime)
1417 printf("You have mail.\n");
1418 else
1419 printf("You have new mail.\n");
1420 }
1421 }
1422 /* Start the shell. Set initial character to '-'. */
1423 buf[0] = '-';
1424 strncpy(buf + 1, cp, sizeof(buf) - 1);
1425 buf[sizeof(buf) - 1] = 0;
1426
1427 /* Execute the shell. */
1428 argv[0] = buf;
1429 argv[1] = NULL;
1430 execve(shell, argv, env);
1431
1432 /* Executing the shell failed. */
1433 perror(shell);
1434 exit(1);
1435
1436 } else {
1437 /* Launch login(1). */
1438
Damien Millerad833b32000-08-23 10:46:23 +10001439 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001440#ifdef LOGIN_NEEDS_TERM
1441 s->term? s->term : "unknown",
1442#endif
Damien Miller942da032000-08-18 13:59:06 +10001443 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001444
1445 /* Login couldn't be executed, die. */
1446
1447 perror("login");
1448 exit(1);
1449 }
1450 }
1451 /*
1452 * Execute the command using the user's shell. This uses the -c
1453 * option to execute the command.
1454 */
1455 argv[0] = (char *) cp;
1456 argv[1] = "-c";
1457 argv[2] = (char *) command;
1458 argv[3] = NULL;
1459 execve(shell, argv, env);
1460 perror(shell);
1461 exit(1);
1462}
1463
1464Session *
1465session_new(void)
1466{
1467 int i;
1468 static int did_init = 0;
1469 if (!did_init) {
1470 debug("session_new: init");
1471 for(i = 0; i < MAX_SESSIONS; i++) {
1472 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001473 }
1474 did_init = 1;
1475 }
1476 for(i = 0; i < MAX_SESSIONS; i++) {
1477 Session *s = &sessions[i];
1478 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001479 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001480 s->chanid = -1;
1481 s->ptyfd = -1;
1482 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001483 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001484 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001485 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001486 return s;
1487 }
1488 }
1489 return NULL;
1490}
1491
1492void
1493session_dump(void)
1494{
1495 int i;
1496 for(i = 0; i < MAX_SESSIONS; i++) {
1497 Session *s = &sessions[i];
1498 debug("dump: used %d session %d %p channel %d pid %d",
1499 s->used,
1500 s->self,
1501 s,
1502 s->chanid,
1503 s->pid);
1504 }
1505}
1506
Damien Millerefb4afe2000-04-12 18:45:05 +10001507int
1508session_open(int chanid)
1509{
1510 Session *s = session_new();
1511 debug("session_open: channel %d", chanid);
1512 if (s == NULL) {
1513 error("no more sessions");
1514 return 0;
1515 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001516 s->pw = auth_get_user();
1517 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001518 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001519 debug("session_open: session %d: link with channel %d", s->self, chanid);
1520 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001521 return 1;
1522}
1523
1524Session *
1525session_by_channel(int id)
1526{
1527 int i;
1528 for(i = 0; i < MAX_SESSIONS; i++) {
1529 Session *s = &sessions[i];
1530 if (s->used && s->chanid == id) {
1531 debug("session_by_channel: session %d channel %d", i, id);
1532 return s;
1533 }
1534 }
1535 debug("session_by_channel: unknown channel %d", id);
1536 session_dump();
1537 return NULL;
1538}
1539
1540Session *
1541session_by_pid(pid_t pid)
1542{
1543 int i;
1544 debug("session_by_pid: pid %d", pid);
1545 for(i = 0; i < MAX_SESSIONS; i++) {
1546 Session *s = &sessions[i];
1547 if (s->used && s->pid == pid)
1548 return s;
1549 }
1550 error("session_by_pid: unknown pid %d", pid);
1551 session_dump();
1552 return NULL;
1553}
1554
1555int
1556session_window_change_req(Session *s)
1557{
1558 s->col = packet_get_int();
1559 s->row = packet_get_int();
1560 s->xpixel = packet_get_int();
1561 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001562 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001563 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1564 return 1;
1565}
1566
1567int
1568session_pty_req(Session *s)
1569{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001570 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001571 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001572
Ben Lindstrom49c12602001-06-13 04:37:36 +00001573 if (no_pty_flag) {
1574 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001575 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001576 }
1577 if (s->ttyfd != -1) {
1578 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001579 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001580 }
1581
Damien Millerefb4afe2000-04-12 18:45:05 +10001582 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001583
1584 if (compat20) {
1585 s->col = packet_get_int();
1586 s->row = packet_get_int();
1587 } else {
1588 s->row = packet_get_int();
1589 s->col = packet_get_int();
1590 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001591 s->xpixel = packet_get_int();
1592 s->ypixel = packet_get_int();
1593
1594 if (strcmp(s->term, "") == 0) {
1595 xfree(s->term);
1596 s->term = NULL;
1597 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001598
Damien Millerefb4afe2000-04-12 18:45:05 +10001599 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001600 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001601 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001602 if (s->term)
1603 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001604 s->term = NULL;
1605 s->ptyfd = -1;
1606 s->ttyfd = -1;
1607 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001608 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001609 }
1610 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001611
1612 /* for SSH1 the tty modes length is not given */
1613 if (!compat20)
1614 n_bytes = packet_remaining();
1615 tty_parse_modes(s->ttyfd, &n_bytes);
1616
Damien Millerefb4afe2000-04-12 18:45:05 +10001617 /*
1618 * Add a cleanup function to clear the utmp entry and record logout
1619 * time in case we call fatal() (e.g., the connection gets closed).
1620 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001621 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001622 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001623
1624 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001625 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1626
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001627 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001628 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001629 return 1;
1630}
1631
Damien Millerbd483e72000-04-30 10:00:53 +10001632int
1633session_subsystem_req(Session *s)
1634{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001635 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001636 int success = 0;
1637 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001638 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001639
1640 packet_done();
1641 log("subsystem request for %s", subsys);
1642
Damien Millerf6d9e222000-06-18 14:50:44 +10001643 for (i = 0; i < options.num_subsystems; i++) {
1644 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1645 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001646 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001647 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001648 success = 1;
1649 }
1650 }
1651
1652 if (!success)
1653 log("subsystem request for %s failed, subsystem not found", subsys);
1654
Damien Millerbd483e72000-04-30 10:00:53 +10001655 xfree(subsys);
1656 return success;
1657}
1658
1659int
1660session_x11_req(Session *s)
1661{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001662 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001663
1664 s->single_connection = packet_get_char();
1665 s->auth_proto = packet_get_string(NULL);
1666 s->auth_data = packet_get_string(NULL);
1667 s->screen = packet_get_int();
1668 packet_done();
1669
Ben Lindstrom768176b2001-06-09 01:29:12 +00001670 success = session_setup_x11fwd(s);
1671 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001672 xfree(s->auth_proto);
1673 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001674 s->auth_proto = NULL;
1675 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001676 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001677 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001678}
1679
Damien Millerf6d9e222000-06-18 14:50:44 +10001680int
1681session_shell_req(Session *s)
1682{
Damien Millerf6d9e222000-06-18 14:50:44 +10001683 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001684 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001685 return 1;
1686}
1687
1688int
1689session_exec_req(Session *s)
1690{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001691 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001692 char *command = packet_get_string(&len);
1693 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001694 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001695 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001696 return 1;
1697}
1698
Damien Miller0bc1bd82000-11-13 22:57:25 +11001699int
1700session_auth_agent_req(Session *s)
1701{
1702 static int called = 0;
1703 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001704 if (no_agent_forwarding_flag) {
1705 debug("session_auth_agent_req: no_agent_forwarding_flag");
1706 return 0;
1707 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001708 if (called) {
1709 return 0;
1710 } else {
1711 called = 1;
1712 return auth_input_request_forwarding(s->pw);
1713 }
1714}
1715
Damien Millerefb4afe2000-04-12 18:45:05 +10001716void
1717session_input_channel_req(int id, void *arg)
1718{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001719 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001720 int reply;
1721 int success = 0;
1722 char *rtype;
1723 Session *s;
1724 Channel *c;
1725
1726 rtype = packet_get_string(&len);
1727 reply = packet_get_char();
1728
1729 s = session_by_channel(id);
1730 if (s == NULL)
1731 fatal("session_input_channel_req: channel %d: no session", id);
1732 c = channel_lookup(id);
1733 if (c == NULL)
1734 fatal("session_input_channel_req: channel %d: bad channel", id);
1735
1736 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1737 s->self, id, rtype, reply);
1738
1739 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001740 * a session is in LARVAL state until a shell, a command
1741 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001742 */
1743 if (c->type == SSH_CHANNEL_LARVAL) {
1744 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001745 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001746 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001747 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001748 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001749 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001750 } else if (strcmp(rtype, "x11-req") == 0) {
1751 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001752 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1753 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001754 } else if (strcmp(rtype, "subsystem") == 0) {
1755 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001756 }
1757 }
1758 if (strcmp(rtype, "window-change") == 0) {
1759 success = session_window_change_req(s);
1760 }
1761
1762 if (reply) {
1763 packet_start(success ?
1764 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1765 packet_put_int(c->remote_id);
1766 packet_send();
1767 }
1768 xfree(rtype);
1769}
1770
1771void
1772session_set_fds(Session *s, int fdin, int fdout, int fderr)
1773{
1774 if (!compat20)
1775 fatal("session_set_fds: called for proto != 2.0");
1776 /*
1777 * now that have a child and a pipe to the child,
1778 * we can activate our channel and register the fd's
1779 */
1780 if (s->chanid == -1)
1781 fatal("no channel for session %d", s->self);
1782 channel_set_fds(s->chanid,
1783 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001784 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1785 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001786}
1787
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001788/*
1789 * Function to perform pty cleanup. Also called if we get aborted abnormally
1790 * (e.g., due to a dropped connection).
1791 */
Damien Millerb38eff82000-04-01 11:09:21 +10001792void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001793session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001794{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001795 Session *s = session;
1796
1797 if (s == NULL) {
1798 error("session_pty_cleanup: no session");
1799 return;
1800 }
1801 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001802 return;
1803
Kevin Steves43cdef32001-02-11 14:12:08 +00001804 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001805
Damien Millerb38eff82000-04-01 11:09:21 +10001806 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001807 if (s->pid != 0)
1808 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001809
1810 /* Release the pseudo-tty. */
1811 pty_release(s->tty);
1812
1813 /*
1814 * Close the server side of the socket pairs. We must do this after
1815 * the pty cleanup, so that another process doesn't get this pty
1816 * while we're still cleaning up.
1817 */
1818 if (close(s->ptymaster) < 0)
1819 error("close(s->ptymaster): %s", strerror(errno));
1820}
Damien Millerefb4afe2000-04-12 18:45:05 +10001821
1822void
1823session_exit_message(Session *s, int status)
1824{
1825 Channel *c;
1826 if (s == NULL)
1827 fatal("session_close: no session");
1828 c = channel_lookup(s->chanid);
1829 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001830 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001831 s->self, s->chanid);
1832 debug("session_exit_message: session %d channel %d pid %d",
1833 s->self, s->chanid, s->pid);
1834
1835 if (WIFEXITED(status)) {
1836 channel_request_start(s->chanid,
1837 "exit-status", 0);
1838 packet_put_int(WEXITSTATUS(status));
1839 packet_send();
1840 } else if (WIFSIGNALED(status)) {
1841 channel_request_start(s->chanid,
1842 "exit-signal", 0);
1843 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001844#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001845 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001846#else /* WCOREDUMP */
1847 packet_put_char(0);
1848#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001849 packet_put_cstring("");
1850 packet_put_cstring("");
1851 packet_send();
1852 } else {
1853 /* Some weird exit cause. Just exit. */
1854 packet_disconnect("wait returned status %04x.", status);
1855 }
1856
1857 /* disconnect channel */
1858 debug("session_exit_message: release channel %d", s->chanid);
1859 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001860 /*
1861 * emulate a write failure with 'chan_write_failed', nobody will be
1862 * interested in data we write.
1863 * Note that we must not call 'chan_read_failed', since there could
1864 * be some more data waiting in the pipe.
1865 */
Damien Millerbd483e72000-04-30 10:00:53 +10001866 if (c->ostate != CHAN_OUTPUT_CLOSED)
1867 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001868 s->chanid = -1;
1869}
1870
1871void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001872session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001873{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001874 debug("session_close: session %d pid %d", s->self, s->pid);
1875 if (s->ttyfd != -1) {
1876 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1877 session_pty_cleanup(s);
1878 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001879 if (s->term)
1880 xfree(s->term);
1881 if (s->display)
1882 xfree(s->display);
1883 if (s->auth_data)
1884 xfree(s->auth_data);
1885 if (s->auth_proto)
1886 xfree(s->auth_proto);
1887 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001888 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001889}
1890
1891void
1892session_close_by_pid(pid_t pid, int status)
1893{
1894 Session *s = session_by_pid(pid);
1895 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001896 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001897 return;
1898 }
1899 if (s->chanid != -1)
1900 session_exit_message(s, status);
1901 session_close(s);
1902}
1903
1904/*
1905 * this is called when a channel dies before
1906 * the session 'child' itself dies
1907 */
1908void
1909session_close_by_channel(int id, void *arg)
1910{
1911 Session *s = session_by_channel(id);
1912 if (s == NULL) {
1913 debug("session_close_by_channel: no session for channel %d", id);
1914 return;
1915 }
1916 /* disconnect channel */
1917 channel_cancel_cleanup(s->chanid);
1918 s->chanid = -1;
1919
1920 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1921 if (s->pid == 0) {
1922 /* close session immediately */
1923 session_close(s);
1924 } else {
1925 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001926 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001927 error("session_close_by_channel: kill %d: %s",
1928 s->pid, strerror(errno));
1929 }
1930}
1931
Damien Millere247cc42000-05-07 12:03:14 +10001932char *
1933session_tty_list(void)
1934{
1935 static char buf[1024];
1936 int i;
1937 buf[0] = '\0';
1938 for(i = 0; i < MAX_SESSIONS; i++) {
1939 Session *s = &sessions[i];
1940 if (s->used && s->ttyfd != -1) {
1941 if (buf[0] != '\0')
1942 strlcat(buf, ",", sizeof buf);
1943 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1944 }
1945 }
1946 if (buf[0] == '\0')
1947 strlcpy(buf, "notty", sizeof buf);
1948 return buf;
1949}
1950
1951void
1952session_proctitle(Session *s)
1953{
1954 if (s->pw == NULL)
1955 error("no user for session %d", s->self);
1956 else
1957 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1958}
1959
Ben Lindstrom768176b2001-06-09 01:29:12 +00001960int
1961session_setup_x11fwd(Session *s)
1962{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001963 struct stat st;
1964
1965 if (no_x11_forwarding_flag) {
1966 packet_send_debug("X11 forwarding disabled in user configuration file.");
1967 return 0;
1968 }
1969 if (!options.x11_forwarding) {
1970 debug("X11 forwarding disabled in server configuration file.");
1971 return 0;
1972 }
1973 if (!options.xauth_location ||
1974 (stat(options.xauth_location, &st) == -1)) {
1975 packet_send_debug("No xauth program; cannot forward with spoofing.");
1976 return 0;
1977 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00001978 if (options.use_login) {
1979 packet_send_debug("X11 forwarding disabled; "
1980 "not compatible with UseLogin=yes.");
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}