blob: ac026e4fdb7e83b3c695c34260b5242dd378adb0 [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 Lindstrom7bfff362001-03-26 05:45:53 +000036RCSID("$OpenBSD: session.c,v 1.69 2001/03/25 13:16:11 stevesk 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"
48#include "channels.h"
49#include "nchan.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100051#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100052#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "pathnames.h"
54#include "log.h"
55#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000056#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "serverloop.h"
58#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000059#include "session.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Damien Miller91606b12000-06-28 08:22:29 +100061#ifdef WITH_IRIX_PROJECT
62#include <proj.h>
63#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000064#ifdef WITH_IRIX_JOBS
65#include <sys/resource.h>
Kevin Stevesef4eea92001-02-05 12:42:17 +000066#endif
Ben Lindstrom49a79c02000-11-17 03:47:20 +000067#ifdef WITH_IRIX_AUDIT
68#include <sat.h>
69#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100070
Damien Miller37023962000-07-11 17:31:38 +100071#if defined(HAVE_USERSEC_H)
72#include <usersec.h>
73#endif
74
Damien Millerbac2d8a2000-09-05 16:13:06 +110075#ifdef HAVE_CYGWIN
76#include <windows.h>
77#include <sys/cygwin.h>
78#define is_winnt (GetVersion() < 0x80000000)
79#endif
80
Damien Millerd17b8d52000-08-09 14:42:28 +100081/* AIX limits */
82#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100083# define S_UFSIZE_HARD S_UFSIZE "_hard"
84# define S_UCPU_HARD S_UCPU "_hard"
85# define S_UDATA_HARD S_UDATA "_hard"
86# define S_USTACK_HARD S_USTACK "_hard"
87# define S_URSS_HARD S_URSS "_hard"
88# define S_UCORE_HARD S_UCORE "_hard"
89# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100090#endif
91
Damien Miller168a7002001-03-17 10:29:50 +110092#ifdef _AIX
93# include <uinfo.h>
94#endif
95
Damien Millerb38eff82000-04-01 11:09:21 +100096/* types */
97
98#define TTYSZ 64
99typedef struct Session Session;
100struct Session {
101 int used;
102 int self;
103 struct passwd *pw;
104 pid_t pid;
105 /* tty */
106 char *term;
107 int ptyfd, ttyfd, ptymaster;
108 int row, col, xpixel, ypixel;
109 char tty[TTYSZ];
110 /* X11 */
111 char *display;
112 int screen;
113 char *auth_proto;
114 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000115 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000116 /* proto 2 */
117 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000118 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000119};
120
121/* func */
122
123Session *session_new(void);
124void session_set_fds(Session *s, int fdin, int fdout, int fderr);
125void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000126void session_proctitle(Session *s);
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000127void do_exec_pty(Session *s, const char *command);
128void do_exec_no_pty(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100129void do_login(Session *s, const char *command);
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000130void do_child(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000131
Ben Lindstromb31783d2001-03-22 02:02:12 +0000132void do_authenticated1(Authctxt *authctxt);
133void do_authenticated2(Authctxt *authctxt);
134
Damien Millerb38eff82000-04-01 11:09:21 +1000135/* import */
136extern ServerOptions options;
137extern char *__progname;
138extern int log_stderr;
139extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000140extern u_int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000141
Damien Miller37023962000-07-11 17:31:38 +1000142extern int startup_pipe;
143
Damien Millerb38eff82000-04-01 11:09:21 +1000144/* Local Xauthority file. */
145static char *xauthfile;
146
Damien Miller7b28dc52000-09-05 13:34:53 +1100147/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000148char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100149
Damien Millerb38eff82000-04-01 11:09:21 +1000150/* data */
151#define MAX_SESSIONS 10
152Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100153
Damien Millerd2c208a2000-05-17 22:00:02 +1000154#ifdef WITH_AIXAUTHENTICATE
155/* AIX's lastlogin message, set in auth1.c */
156char *aixloginmsg;
157#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000158
Damien Millerad833b32000-08-23 10:46:23 +1000159#ifdef HAVE_LOGIN_CAP
160static login_cap_t *lc;
161#endif
162
Ben Lindstromb31783d2001-03-22 02:02:12 +0000163void
164do_authenticated(Authctxt *authctxt)
165{
166 /*
167 * Cancel the alarm we set to limit the time taken for
168 * authentication.
169 */
170 alarm(0);
171 if (startup_pipe != -1) {
172 close(startup_pipe);
173 startup_pipe = -1;
174 }
175#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
176 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
177 error("unable to get login class");
178 return;
179 }
180#endif
181 /* setup the channel layer */
182 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
183 channel_permit_all_opens();
184
185 if (compat20)
186 do_authenticated2(authctxt);
187 else
188 do_authenticated1(authctxt);
189}
190
Damien Millerb38eff82000-04-01 11:09:21 +1000191/*
192 * Remove local Xauthority file.
193 */
194void
195xauthfile_cleanup_proc(void *ignore)
196{
197 debug("xauthfile_cleanup_proc called");
198
199 if (xauthfile != NULL) {
200 char *p;
201 unlink(xauthfile);
202 p = strrchr(xauthfile, '/');
203 if (p != NULL) {
204 *p = '\0';
205 rmdir(xauthfile);
206 }
207 xfree(xauthfile);
208 xauthfile = NULL;
209 }
210}
211
212/*
213 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
214 * dropped connection).
215 */
Damien Miller4af51302000-04-16 11:18:38 +1000216void
Damien Millerb38eff82000-04-01 11:09:21 +1000217pty_cleanup_proc(void *session)
218{
219 Session *s=session;
220 if (s == NULL)
221 fatal("pty_cleanup_proc: no session");
222 debug("pty_cleanup_proc: %s", s->tty);
223
224 if (s->pid != 0) {
225 /* Record that the user has logged out. */
226 record_logout(s->pid, s->tty);
227 }
228
229 /* Release the pseudo-tty. */
230 pty_release(s->tty);
231}
232
233/*
234 * Prepares for an interactive session. This is called after the user has
235 * been successfully authenticated. During this message exchange, pseudo
236 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
237 * are requested, etc.
238 */
Damien Miller4af51302000-04-16 11:18:38 +1000239void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000240do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000241{
242 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000243 char *command;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000244 int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
245 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000246 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000247
248 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000249 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000250
Damien Millerb38eff82000-04-01 11:09:21 +1000251 /*
252 * We stay in this loop until the client requests to execute a shell
253 * or a command.
254 */
255 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000256 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000257
258 /* Get a packet from the client. */
259 type = packet_read(&plen);
260
261 /* Process the packet. */
262 switch (type) {
263 case SSH_CMSG_REQUEST_COMPRESSION:
264 packet_integrity_check(plen, 4, type);
265 compression_level = packet_get_int();
266 if (compression_level < 1 || compression_level > 9) {
267 packet_send_debug("Received illegal compression level %d.",
268 compression_level);
269 break;
270 }
271 /* Enable compression after we have responded with SUCCESS. */
272 enable_compression_after_reply = 1;
273 success = 1;
274 break;
275
276 case SSH_CMSG_REQUEST_PTY:
277 if (no_pty_flag) {
278 debug("Allocating a pty not permitted for this authentication.");
279 break;
280 }
281 if (have_pty)
282 packet_disconnect("Protocol error: you already have a pty.");
283
284 debug("Allocating pty.");
285
286 /* Allocate a pty and open it. */
287 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
288 sizeof(s->tty))) {
289 error("Failed to allocate pty.");
290 break;
291 }
292 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000293 pty_setowner(s->pw, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000294
295 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
296 s->term = packet_get_string(&dlen);
297 packet_integrity_check(dlen, strlen(s->term), type);
298 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
299 /* Remaining bytes */
300 n_bytes = plen - (4 + dlen + 4 * 4);
301
302 if (strcmp(s->term, "") == 0) {
303 xfree(s->term);
304 s->term = NULL;
305 }
306 /* Get window size from the packet. */
307 s->row = packet_get_int();
308 s->col = packet_get_int();
309 s->xpixel = packet_get_int();
310 s->ypixel = packet_get_int();
311 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
312
313 /* Get tty modes from the packet. */
314 tty_parse_modes(s->ttyfd, &n_bytes);
315 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
316
Damien Millere247cc42000-05-07 12:03:14 +1000317 session_proctitle(s);
318
Damien Millerb38eff82000-04-01 11:09:21 +1000319 /* Indicate that we now have a pty. */
320 success = 1;
321 have_pty = 1;
322 break;
323
324 case SSH_CMSG_X11_REQUEST_FORWARDING:
325 if (!options.x11_forwarding) {
326 packet_send_debug("X11 forwarding disabled in server configuration file.");
327 break;
328 }
Damien Miller0c043c12000-06-07 21:22:38 +1000329 if (!options.xauth_location) {
330 packet_send_debug("No xauth program; cannot forward with spoofing.");
331 break;
332 }
Damien Millerb38eff82000-04-01 11:09:21 +1000333 if (no_x11_forwarding_flag) {
334 packet_send_debug("X11 forwarding not permitted for this authentication.");
335 break;
336 }
337 debug("Received request for X11 forwarding with auth spoofing.");
338 if (s->display != NULL)
339 packet_disconnect("Protocol error: X11 display already set.");
340
341 s->auth_proto = packet_get_string(&proto_len);
342 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000343
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000344 screen_flag = packet_get_protocol_flags() &
345 SSH_PROTOFLAG_SCREEN_NUMBER;
346 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
347
348 if (packet_remaining() == 4) {
349 if (!screen_flag)
350 debug2("Buggy client: "
351 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000352 packet_integrity_check(plen,
353 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000354 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000355 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000356 packet_integrity_check(plen,
357 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000358 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000359 }
Damien Millerb38eff82000-04-01 11:09:21 +1000360 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
361
362 if (s->display == NULL)
363 break;
364
365 /* Setup to always have a local .Xauthority. */
366 xauthfile = xmalloc(MAXPATHLEN);
367 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000368 temporarily_use_uid(s->pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +1000369 if (mkdtemp(xauthfile) == NULL) {
370 restore_uid();
371 error("private X11 dir: mkdtemp %s failed: %s",
372 xauthfile, strerror(errno));
373 xfree(xauthfile);
374 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000375 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000376 break;
377 }
378 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100379 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
380 if (fd >= 0)
381 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000382 restore_uid();
383 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
384 success = 1;
385 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000386
387 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
388 if (no_agent_forwarding_flag || compat13) {
389 debug("Authentication agent forwarding not permitted for this authentication.");
390 break;
391 }
392 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000393 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000394 break;
395
396 case SSH_CMSG_PORT_FORWARD_REQUEST:
397 if (no_port_forwarding_flag) {
398 debug("Port forwarding not permitted for this authentication.");
399 break;
400 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100401 if (!options.allow_tcp_forwarding) {
402 debug("Port forwarding not permitted.");
403 break;
404 }
Damien Millerb38eff82000-04-01 11:09:21 +1000405 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000406 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000407 success = 1;
408 break;
409
410 case SSH_CMSG_MAX_PACKET_SIZE:
411 if (packet_set_maxsize(packet_get_int()) > 0)
412 success = 1;
413 break;
414
415 case SSH_CMSG_EXEC_SHELL:
416 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000417 if (type == SSH_CMSG_EXEC_CMD) {
418 command = packet_get_string(&dlen);
419 debug("Exec command '%.500s'", command);
420 packet_integrity_check(plen, 4 + dlen, type);
421 } else {
422 command = NULL;
423 packet_integrity_check(plen, 0, type);
424 }
425 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100426 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000427 command = forced_command;
428 debug("Forced command '%.500s'", forced_command);
429 }
430 if (have_pty)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000431 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000432 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000433 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000434
435 if (command != NULL)
436 xfree(command);
437 /* Cleanup user's local Xauthority file. */
438 if (xauthfile)
439 xauthfile_cleanup_proc(NULL);
440 return;
441
442 default:
443 /*
444 * Any unknown messages in this phase are ignored,
445 * and a failure message is returned.
446 */
447 log("Unknown packet type received after authentication: %d", type);
448 }
449 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
450 packet_send();
451 packet_write_wait();
452
453 /* Enable compression now that we have replied if appropriate. */
454 if (enable_compression_after_reply) {
455 enable_compression_after_reply = 0;
456 packet_start_compression(compression_level);
457 }
458 }
459}
460
461/*
462 * This is called to fork and execute a command when we have no tty. This
463 * will call do_child from the child, and server_loop from the parent after
464 * setting up file descriptors and such.
465 */
Damien Miller4af51302000-04-16 11:18:38 +1000466void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000467do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000468{
469 int pid;
470
471#ifdef USE_PIPES
472 int pin[2], pout[2], perr[2];
473 /* Allocate pipes for communicating with the program. */
474 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
475 packet_disconnect("Could not create pipes: %.100s",
476 strerror(errno));
477#else /* USE_PIPES */
478 int inout[2], err[2];
479 /* Uses socket pairs to communicate with the program. */
480 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
481 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
482 packet_disconnect("Could not create socket pairs: %.100s",
483 strerror(errno));
484#endif /* USE_PIPES */
485 if (s == NULL)
486 fatal("do_exec_no_pty: no session");
487
Damien Millere247cc42000-05-07 12:03:14 +1000488 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000489
Damien Millerc5946332001-02-28 11:46:11 +1100490#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000491 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000492#endif /* USE_PAM */
493
Damien Millerb38eff82000-04-01 11:09:21 +1000494 /* Fork the child. */
495 if ((pid = fork()) == 0) {
496 /* Child. Reinitialize the log since the pid has changed. */
497 log_init(__progname, options.log_level, options.log_facility, log_stderr);
498
499 /*
500 * Create a new session and process group since the 4.4BSD
501 * setlogin() affects the entire process group.
502 */
503 if (setsid() < 0)
504 error("setsid failed: %.100s", strerror(errno));
505
506#ifdef USE_PIPES
507 /*
508 * Redirect stdin. We close the parent side of the socket
509 * pair, and make the child side the standard input.
510 */
511 close(pin[1]);
512 if (dup2(pin[0], 0) < 0)
513 perror("dup2 stdin");
514 close(pin[0]);
515
516 /* Redirect stdout. */
517 close(pout[0]);
518 if (dup2(pout[1], 1) < 0)
519 perror("dup2 stdout");
520 close(pout[1]);
521
522 /* Redirect stderr. */
523 close(perr[0]);
524 if (dup2(perr[1], 2) < 0)
525 perror("dup2 stderr");
526 close(perr[1]);
527#else /* USE_PIPES */
528 /*
529 * Redirect stdin, stdout, and stderr. Stdin and stdout will
530 * use the same socket, as some programs (particularly rdist)
531 * seem to depend on it.
532 */
533 close(inout[1]);
534 close(err[1]);
535 if (dup2(inout[0], 0) < 0) /* stdin */
536 perror("dup2 stdin");
537 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
538 perror("dup2 stdout");
539 if (dup2(err[0], 2) < 0) /* stderr */
540 perror("dup2 stderr");
541#endif /* USE_PIPES */
542
543 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000544 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000545 /* NOTREACHED */
546 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100547#ifdef HAVE_CYGWIN
548 if (is_winnt)
549 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
550#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000551 if (pid < 0)
552 packet_disconnect("fork failed: %.100s", strerror(errno));
553 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000554 /* Set interactive/non-interactive mode. */
555 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000556#ifdef USE_PIPES
557 /* We are the parent. Close the child sides of the pipes. */
558 close(pin[0]);
559 close(pout[1]);
560 close(perr[1]);
561
Damien Millerefb4afe2000-04-12 18:45:05 +1000562 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000563 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000564 } else {
565 /* Enter the interactive session. */
566 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000567 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000568 }
Damien Millerb38eff82000-04-01 11:09:21 +1000569#else /* USE_PIPES */
570 /* We are the parent. Close the child sides of the socket pairs. */
571 close(inout[0]);
572 close(err[0]);
573
574 /*
575 * Enter the interactive session. Note: server_loop must be able to
576 * handle the case that fdin and fdout are the same.
577 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000578 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000579 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000580 } else {
581 server_loop(pid, inout[1], inout[1], err[1]);
582 /* server_loop has closed inout[1] and err[1]. */
583 }
Damien Millerb38eff82000-04-01 11:09:21 +1000584#endif /* USE_PIPES */
585}
586
587/*
588 * This is called to fork and execute a command when we have a tty. This
589 * will call do_child from the child, and server_loop from the parent after
590 * setting up file descriptors, controlling tty, updating wtmp, utmp,
591 * lastlog, and other such operations.
592 */
Damien Miller4af51302000-04-16 11:18:38 +1000593void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000594do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000595{
Damien Millerb38eff82000-04-01 11:09:21 +1000596 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000597 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000598
599 if (s == NULL)
600 fatal("do_exec_pty: no session");
601 ptyfd = s->ptyfd;
602 ttyfd = s->ttyfd;
603
Damien Millerc5946332001-02-28 11:46:11 +1100604#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000605 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000606 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100607#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000608
Damien Millerb38eff82000-04-01 11:09:21 +1000609 /* Fork the child. */
610 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000611 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000612 log_init(__progname, options.log_level, options.log_facility, log_stderr);
613
614 /* Close the master side of the pseudo tty. */
615 close(ptyfd);
616
617 /* Make the pseudo tty our controlling tty. */
618 pty_make_controlling_tty(&ttyfd, s->tty);
619
620 /* Redirect stdin from the pseudo tty. */
621 if (dup2(ttyfd, fileno(stdin)) < 0)
622 error("dup2 stdin failed: %.100s", strerror(errno));
623
624 /* Redirect stdout to the pseudo tty. */
625 if (dup2(ttyfd, fileno(stdout)) < 0)
626 error("dup2 stdin failed: %.100s", strerror(errno));
627
628 /* Redirect stderr to the pseudo tty. */
629 if (dup2(ttyfd, fileno(stderr)) < 0)
630 error("dup2 stdin failed: %.100s", strerror(errno));
631
632 /* Close the extra descriptor for the pseudo tty. */
633 close(ttyfd);
634
Damien Miller942da032000-08-18 13:59:06 +1000635 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100636 if (!(options.use_login && command == NULL))
637 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000638
Damien Millerb38eff82000-04-01 11:09:21 +1000639 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000640 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000641 /* NOTREACHED */
642 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100643#ifdef HAVE_CYGWIN
644 if (is_winnt)
645 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
646#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000647 if (pid < 0)
648 packet_disconnect("fork failed: %.100s", strerror(errno));
649 s->pid = pid;
650
651 /* Parent. Close the slave side of the pseudo tty. */
652 close(ttyfd);
653
654 /*
655 * Create another descriptor of the pty master side for use as the
656 * standard input. We could use the original descriptor, but this
657 * simplifies code in server_loop. The descriptor is bidirectional.
658 */
659 fdout = dup(ptyfd);
660 if (fdout < 0)
661 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
662
663 /* we keep a reference to the pty master */
664 ptymaster = dup(ptyfd);
665 if (ptymaster < 0)
666 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
667 s->ptymaster = ptymaster;
668
669 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000670 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000671 if (compat20) {
672 session_set_fds(s, ptyfd, fdout, -1);
673 } else {
674 server_loop(pid, ptyfd, fdout, -1);
675 /* server_loop _has_ closed ptyfd and fdout. */
676 session_pty_cleanup(s);
677 }
Damien Millerb38eff82000-04-01 11:09:21 +1000678}
679
Damien Miller942da032000-08-18 13:59:06 +1000680const char *
681get_remote_name_or_ip(void)
682{
683 static const char *remote = "";
684 if (utmp_len > 0)
Damien Miller33804262001-02-04 23:20:18 +1100685 remote = get_canonical_hostname(options.reverse_mapping_check);
Damien Miller942da032000-08-18 13:59:06 +1000686 if (utmp_len == 0 || strlen(remote) > utmp_len)
687 remote = get_remote_ipaddr();
688 return remote;
689}
690
691/* administrative, login(1)-like work */
692void
Damien Miller69b69aa2000-10-28 14:19:58 +1100693do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000694{
695 FILE *f;
696 char *time_string;
697 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100698 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000699 socklen_t fromlen;
700 struct sockaddr_storage from;
701 struct stat st;
702 time_t last_login_time;
703 struct passwd * pw = s->pw;
704 pid_t pid = getpid();
705
706 /*
707 * Get IP address of client. If the connection is not a socket, let
708 * the address be 0.0.0.0.
709 */
710 memset(&from, 0, sizeof(from));
711 if (packet_connection_is_on_socket()) {
712 fromlen = sizeof(from);
713 if (getpeername(packet_get_connection_in(),
714 (struct sockaddr *) & from, &fromlen) < 0) {
715 debug("getpeername: %.100s", strerror(errno));
716 fatal_cleanup();
717 }
718 }
719
Damien Miller7b28dc52000-09-05 13:34:53 +1100720 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000721 if (options.print_lastlog) {
722 hostname[0] = '\0';
723 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
724 hostname, sizeof(hostname));
725 }
Damien Millere4340be2000-09-16 13:29:08 +1100726
Damien Miller942da032000-08-18 13:59:06 +1000727 /* Record that there was a login on that tty from the remote host. */
728 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
729 get_remote_name_or_ip(), (struct sockaddr *)&from);
730
Kevin Steves092f2ef2000-10-14 13:36:13 +0000731#ifdef USE_PAM
732 /*
733 * If password change is needed, do it now.
734 * This needs to occur before the ~/.hushlogin check.
735 */
Damien Miller646aa602001-02-15 11:51:32 +1100736 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000737 print_pam_messages();
738 do_pam_chauthtok();
739 }
740#endif
741
Damien Miller69b69aa2000-10-28 14:19:58 +1100742 /* Done if .hushlogin exists or a command given. */
743 if (command != NULL)
744 return;
Damien Miller942da032000-08-18 13:59:06 +1000745 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000746#ifdef HAVE_LOGIN_CAP
747 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
748#else
Damien Miller942da032000-08-18 13:59:06 +1000749 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000750#endif
Damien Miller942da032000-08-18 13:59:06 +1000751 return;
752
753#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100754 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000755 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000756#endif /* USE_PAM */
757#ifdef WITH_AIXAUTHENTICATE
758 if (aixloginmsg && *aixloginmsg)
759 printf("%s\n", aixloginmsg);
760#endif /* WITH_AIXAUTHENTICATE */
761
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000762 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000763 time_string = ctime(&last_login_time);
764 if (strchr(time_string, '\n'))
765 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000766 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000767 printf("Last login: %s\r\n", time_string);
768 else
Damien Millere4340be2000-09-16 13:29:08 +1100769 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000770 }
771 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000772#ifdef HAVE_LOGIN_CAP
773 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
774 "/etc/motd"), "r");
775#else
Damien Miller942da032000-08-18 13:59:06 +1000776 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000777#endif
Damien Miller942da032000-08-18 13:59:06 +1000778 if (f) {
779 while (fgets(buf, sizeof(buf), f))
780 fputs(buf, stdout);
781 fclose(f);
782 }
783 }
784}
785
Damien Millerb38eff82000-04-01 11:09:21 +1000786/*
787 * Sets the value of the given variable in the environment. If the variable
788 * already exists, its value is overriden.
789 */
Damien Miller4af51302000-04-16 11:18:38 +1000790void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000791child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000792 const char *value)
793{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000794 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000795 char **env;
796
797 /*
798 * Find the slot where the value should be stored. If the variable
799 * already exists, we reuse the slot; otherwise we append a new slot
800 * at the end of the array, expanding if necessary.
801 */
802 env = *envp;
803 namelen = strlen(name);
804 for (i = 0; env[i]; i++)
805 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
806 break;
807 if (env[i]) {
808 /* Reuse the slot. */
809 xfree(env[i]);
810 } else {
811 /* New variable. Expand if necessary. */
812 if (i >= (*envsizep) - 1) {
813 (*envsizep) += 50;
814 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
815 }
816 /* Need to set the NULL pointer at end of array beyond the new slot. */
817 env[i + 1] = NULL;
818 }
819
820 /* Allocate space and format the variable in the appropriate slot. */
821 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
822 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
823}
824
825/*
826 * Reads environment variables from the given file and adds/overrides them
827 * into the environment. If the file does not exist, this does nothing.
828 * Otherwise, it must consist of empty lines, comments (line starts with '#')
829 * and assignments of the form name=value. No other forms are allowed.
830 */
Damien Miller4af51302000-04-16 11:18:38 +1000831void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000832read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000833 const char *filename)
834{
835 FILE *f;
836 char buf[4096];
837 char *cp, *value;
838
839 f = fopen(filename, "r");
840 if (!f)
841 return;
842
843 while (fgets(buf, sizeof(buf), f)) {
844 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
845 ;
846 if (!*cp || *cp == '#' || *cp == '\n')
847 continue;
848 if (strchr(cp, '\n'))
849 *strchr(cp, '\n') = '\0';
850 value = strchr(cp, '=');
851 if (value == NULL) {
852 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
853 continue;
854 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000855 /*
856 * Replace the equals sign by nul, and advance value to
857 * the value string.
858 */
Damien Millerb38eff82000-04-01 11:09:21 +1000859 *value = '\0';
860 value++;
861 child_set_env(env, envsize, cp, value);
862 }
863 fclose(f);
864}
865
866#ifdef USE_PAM
867/*
868 * Sets any environment variables which have been specified by PAM
869 */
870void do_pam_environment(char ***env, int *envsize)
871{
872 char *equals, var_name[512], var_val[512];
873 char **pam_env;
874 int i;
875
876 if ((pam_env = fetch_pam_environment()) == NULL)
877 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000878
Damien Millerb38eff82000-04-01 11:09:21 +1000879 for(i = 0; pam_env[i] != NULL; i++) {
880 if ((equals = strstr(pam_env[i], "=")) == NULL)
881 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000882
Damien Millerb38eff82000-04-01 11:09:21 +1000883 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
884 memset(var_name, '\0', sizeof(var_name));
885 memset(var_val, '\0', sizeof(var_val));
886
887 strncpy(var_name, pam_env[i], equals - pam_env[i]);
888 strcpy(var_val, equals + 1);
889
Damien Millercb5e44a2000-09-29 12:12:36 +1100890 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000891
892 child_set_env(env, envsize, var_name, var_val);
893 }
894 }
895}
896#endif /* USE_PAM */
897
Damien Millercb5e44a2000-09-29 12:12:36 +1100898#ifdef HAVE_CYGWIN
899void copy_environment(char ***env, int *envsize)
900{
901 char *equals, var_name[512], var_val[512];
902 int i;
903
904 for(i = 0; environ[i] != NULL; i++) {
905 if ((equals = strstr(environ[i], "=")) == NULL)
906 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000907
Damien Millercb5e44a2000-09-29 12:12:36 +1100908 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
909 memset(var_name, '\0', sizeof(var_name));
910 memset(var_val, '\0', sizeof(var_val));
911
912 strncpy(var_name, environ[i], equals - environ[i]);
913 strcpy(var_val, equals + 1);
914
915 debug3("Copy environment: %s=%s", var_name, var_val);
916
917 child_set_env(env, envsize, var_name, var_val);
918 }
919 }
920}
921#endif
922
Damien Miller5fc85652000-07-09 23:53:07 +1000923#if defined(HAVE_GETUSERATTR)
924/*
925 * AIX-specific login initialisation
926 */
927void set_limit(char *user, char *soft, char *hard, int resource, int mult)
928{
929 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000930 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000931
932 getrlimit(resource, &rlim);
933
Damien Miller65964d62000-07-11 09:16:22 +1000934 slim = 0;
935 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
936 if (slim < 0) {
937 rlim.rlim_cur = RLIM_INFINITY;
938 } else if (slim != 0) {
939 /* See the wackiness below */
940 if (rlim.rlim_cur == slim * mult)
941 slim = 0;
942 else
943 rlim.rlim_cur = slim * mult;
944 }
945 }
Damien Miller5fc85652000-07-09 23:53:07 +1000946
Damien Miller65964d62000-07-11 09:16:22 +1000947 hlim = 0;
948 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
949 if (hlim < 0) {
950 rlim.rlim_max = RLIM_INFINITY;
951 } else if (hlim != 0) {
952 rlim.rlim_max = hlim * mult;
953 }
954 }
Damien Miller5fc85652000-07-09 23:53:07 +1000955
Damien Miller65964d62000-07-11 09:16:22 +1000956 /*
957 * XXX For cpu and fsize the soft limit is set to the hard limit
958 * if the hard limit is left at its default value and the soft limit
959 * is changed from its default value, either by requesting it
960 * (slim == 0) or by setting it to the current default. At least
961 * that's how rlogind does it. If you're confused you're not alone.
962 * Bug or feature? AIX 4.3.1.2
963 */
964 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
965 && hlim == 0 && slim != 0)
966 rlim.rlim_max = rlim.rlim_cur;
967 /* A specified hard limit limits the soft limit */
968 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
969 rlim.rlim_cur = rlim.rlim_max;
970 /* A soft limit can increase a hard limit */
971 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000972 rlim.rlim_max = rlim.rlim_cur;
973
974 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000975 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000976}
977
978void set_limits_from_userattr(char *user)
979{
980 int mask;
981 char buf[16];
982
983 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
984 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
985 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
986 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
987 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
988 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
989#if defined(S_UNOFILE)
990 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
991#endif
992
993 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
994 /* Convert decimal to octal */
995 (void) snprintf(buf, sizeof(buf), "%d", mask);
996 if (sscanf(buf, "%o", &mask) == 1)
997 umask(mask);
998 }
999}
1000#endif /* defined(HAVE_GETUSERATTR) */
1001
Damien Millerb38eff82000-04-01 11:09:21 +10001002/*
1003 * Performs common processing for the child, such as setting up the
1004 * environment, closing extra file descriptors, setting the user and group
1005 * ids, and executing the command or shell.
1006 */
Damien Miller4af51302000-04-16 11:18:38 +10001007void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001008do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001009{
Damien Miller7b28dc52000-09-05 13:34:53 +11001010 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001011 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001012 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001013 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001014 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001015 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001016 char **env;
1017 extern char **environ;
1018 struct stat st;
1019 char *argv[10];
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001020 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
Damien Miller91606b12000-06-28 08:22:29 +10001021#ifdef WITH_IRIX_PROJECT
1022 prid_t projid;
1023#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001024#ifdef WITH_IRIX_JOBS
1025 jid_t jid = 0;
1026#else
1027#ifdef WITH_IRIX_ARRAY
1028 int jid = 0;
1029#endif /* WITH_IRIX_ARRAY */
1030#endif /* WITH_IRIX_JOBS */
1031
Damien Millerd3a18572000-06-07 19:55:44 +10001032 /* login(1) is only called if we execute the login shell */
1033 if (options.use_login && command != NULL)
1034 options.use_login = 0;
1035
Damien Millerb38eff82000-04-01 11:09:21 +10001036#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001037 if (!options.use_login) {
1038# ifdef HAVE_LOGIN_CAP
1039 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1040 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1041 _PATH_NOLOGIN), "r");
1042# else /* HAVE_LOGIN_CAP */
1043 if (pw->pw_uid)
1044 f = fopen(_PATH_NOLOGIN, "r");
1045# endif /* HAVE_LOGIN_CAP */
1046 if (f) {
1047 /* /etc/nologin exists. Print its contents and exit. */
1048 while (fgets(buf, sizeof(buf), f))
1049 fputs(buf, stderr);
1050 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001051 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001052 }
Damien Millerb38eff82000-04-01 11:09:21 +10001053 }
1054#endif /* USE_PAM */
1055
Damien Millerad833b32000-08-23 10:46:23 +10001056 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001057 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1058 switch, so we let login(1) to this for us. */
1059 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001060#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001061 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001062#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001063#ifdef HAVE_CYGWIN
1064 if (is_winnt) {
1065#else
Damien Millerb38eff82000-04-01 11:09:21 +10001066 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001067#endif
Damien Millerad833b32000-08-23 10:46:23 +10001068# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001069 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001070# endif /* HAVE_GETUSERATTR */
1071# ifdef HAVE_LOGIN_CAP
1072 if (setusercontext(lc, pw, pw->pw_uid,
1073 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1074 perror("unable to set user context");
1075 exit(1);
1076 }
Damien Miller60396b02001-02-18 17:01:00 +11001077#ifdef BSD_AUTH
1078 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1079 error("approval failure for %s", pw->pw_name);
1080 fprintf(stderr, "Approval failure");
1081 exit(1);
1082 }
1083#endif
Damien Millerad833b32000-08-23 10:46:23 +10001084# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001085#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1086 /* Sets login uid for accounting */
1087 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1088 error("setluid: %s", strerror(errno));
1089#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1090
Damien Millerad833b32000-08-23 10:46:23 +10001091 if (setlogin(pw->pw_name) < 0)
1092 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001093 if (setgid(pw->pw_gid) < 0) {
1094 perror("setgid");
1095 exit(1);
1096 }
1097 /* Initialize the group list. */
1098 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1099 perror("initgroups");
1100 exit(1);
1101 }
1102 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001103# ifdef USE_PAM
1104 /*
1105 * PAM credentials may take the form of
1106 * supplementary groups. These will have been
1107 * wiped by the above initgroups() call.
1108 * Reestablish them here.
1109 */
1110 do_pam_setcred(0);
1111# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001112# ifdef WITH_IRIX_JOBS
1113 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1114 if (jid == -1) {
1115 fatal("Failed to create job container: %.100s",
1116 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001117 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001118# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001119# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001120 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001121 if (jid == 0) {
1122 if (newarraysess() != 0)
1123 fatal("Failed to set up new array session: %.100s",
1124 strerror(errno));
1125 }
Damien Millerad833b32000-08-23 10:46:23 +10001126# endif /* WITH_IRIX_ARRAY */
1127# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001128 /* initialize irix project info */
1129 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1130 debug("Failed to get project id, using projid 0");
1131 projid = 0;
1132 }
Damien Miller91606b12000-06-28 08:22:29 +10001133 if (setprid(projid))
1134 fatal("Failed to initialize project %d for %s: %.100s",
1135 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001136# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001137#ifdef WITH_IRIX_AUDIT
1138 if (sysconf(_SC_AUDIT)) {
1139 debug("Setting sat id to %d", (int) pw->pw_uid);
1140 if (satsetid(pw->pw_uid))
1141 debug("error setting satid: %.100s", strerror(errno));
1142 }
1143#endif /* WITH_IRIX_AUDIT */
1144
Damien Miller168a7002001-03-17 10:29:50 +11001145#ifdef _AIX
1146 /*
1147 * AIX has a "usrinfo" area where logname and
1148 * other stuff is stored - a few applications
1149 * actually use this and die if it's not set
1150 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001151 if (s->ttyfd == -1)
1152 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001153 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001154 2 * strlen(pw->pw_name));
1155 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001156 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001157 if (usrinfo(SETUINFO, cp, i) == -1)
1158 fatal("Couldn't set usrinfo: %s",
1159 strerror(errno));
1160 debug3("AIX/UsrInfo: set len %d", i);
1161 xfree(cp);
1162#endif
1163
Damien Millerb38eff82000-04-01 11:09:21 +10001164 /* Permanently switch to the desired uid. */
1165 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001166# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001167 }
Damien Millerad833b32000-08-23 10:46:23 +10001168#endif /* HAVE_OSF_SIA */
1169
Damien Millerbac2d8a2000-09-05 16:13:06 +11001170#ifdef HAVE_CYGWIN
1171 if (is_winnt)
1172#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001173 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001174 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001175 }
1176 /*
1177 * Get the shell from the password data. An empty shell field is
1178 * legal, and means /bin/sh.
1179 */
1180 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001181#ifdef HAVE_LOGIN_CAP
1182 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1183#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001184
1185#ifdef AFS
1186 /* Try to get AFS tokens for the local cell. */
1187 if (k_hasafs()) {
1188 char cell[64];
1189
1190 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1191 krb_afslog(cell, 0);
1192
1193 krb_afslog(0, 0);
1194 }
1195#endif /* AFS */
1196
1197 /* Initialize the environment. */
1198 envsize = 100;
1199 env = xmalloc(envsize * sizeof(char *));
1200 env[0] = NULL;
1201
Damien Millerbac2d8a2000-09-05 16:13:06 +11001202#ifdef HAVE_CYGWIN
1203 /*
1204 * The Windows environment contains some setting which are
1205 * important for a running system. They must not be dropped.
1206 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001207 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001208#endif
1209
Damien Millerb38eff82000-04-01 11:09:21 +10001210 if (!options.use_login) {
1211 /* Set basic environment. */
1212 child_set_env(&env, &envsize, "USER", pw->pw_name);
1213 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1214 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001215#ifdef HAVE_LOGIN_CAP
1216 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1217 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001218#else /* HAVE_LOGIN_CAP */
1219# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001220 /*
1221 * There's no standard path on Windows. The path contains
1222 * important components pointing to the system directories,
1223 * needed for loading shared libraries. So the path better
1224 * remains intact here.
1225 */
Damien Millerb38eff82000-04-01 11:09:21 +10001226 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001227# endif /* HAVE_CYGWIN */
1228#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001229
1230 snprintf(buf, sizeof buf, "%.200s/%.50s",
1231 _PATH_MAILDIR, pw->pw_name);
1232 child_set_env(&env, &envsize, "MAIL", buf);
1233
1234 /* Normal systems set SHELL by default. */
1235 child_set_env(&env, &envsize, "SHELL", shell);
1236 }
1237 if (getenv("TZ"))
1238 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1239
1240 /* Set custom environment options from RSA authentication. */
1241 while (custom_environment) {
1242 struct envstring *ce = custom_environment;
1243 char *s = ce->s;
1244 int i;
1245 for (i = 0; s[i] != '=' && s[i]; i++);
1246 if (s[i] == '=') {
1247 s[i] = 0;
1248 child_set_env(&env, &envsize, s, s + i + 1);
1249 }
1250 custom_environment = ce->next;
1251 xfree(ce->s);
1252 xfree(ce);
1253 }
1254
1255 snprintf(buf, sizeof buf, "%.50s %d %d",
1256 get_remote_ipaddr(), get_remote_port(), get_local_port());
1257 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1258
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001259 if (s->ttyfd != -1)
1260 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1261 if (s->term)
1262 child_set_env(&env, &envsize, "TERM", s->term);
1263 if (s->display)
1264 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001265 if (original_command)
1266 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1267 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001268
1269#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001270 if ((cp = getenv("AUTHSTATE")) != NULL)
1271 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1272 if ((cp = getenv("KRB5CCNAME")) != NULL)
1273 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1274 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001275#endif
1276
1277#ifdef KRB4
1278 {
1279 extern char *ticket;
1280
1281 if (ticket)
1282 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1283 }
1284#endif /* KRB4 */
1285
1286#ifdef USE_PAM
1287 /* Pull in any environment variables that may have been set by PAM. */
1288 do_pam_environment(&env, &envsize);
1289#endif /* USE_PAM */
1290
Damien Millerb38eff82000-04-01 11:09:21 +10001291 if (xauthfile)
1292 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1293 if (auth_get_socket_name() != NULL)
1294 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1295 auth_get_socket_name());
1296
1297 /* read $HOME/.ssh/environment. */
1298 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001299 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1300 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001301 read_environment_file(&env, &envsize, buf);
1302 }
1303 if (debug_flag) {
1304 /* dump the environment */
1305 fprintf(stderr, "Environment:\n");
1306 for (i = 0; env[i]; i++)
1307 fprintf(stderr, " %.200s\n", env[i]);
1308 }
Damien Millerad833b32000-08-23 10:46:23 +10001309 /* we have to stash the hostname before we close our socket. */
1310 if (options.use_login)
1311 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001312 /*
1313 * Close the connection descriptors; note that this is the child, and
1314 * the server will still have the socket open, and it is important
1315 * that we do not shutdown it. Note that the descriptors cannot be
1316 * closed before building the environment, as we call
1317 * get_remote_ipaddr there.
1318 */
1319 if (packet_get_connection_in() == packet_get_connection_out())
1320 close(packet_get_connection_in());
1321 else {
1322 close(packet_get_connection_in());
1323 close(packet_get_connection_out());
1324 }
1325 /*
1326 * Close all descriptors related to channels. They will still remain
1327 * open in the parent.
1328 */
1329 /* XXX better use close-on-exec? -markus */
1330 channel_close_all();
1331
1332 /*
1333 * Close any extra file descriptors. Note that there may still be
1334 * descriptors left by system functions. They will be closed later.
1335 */
1336 endpwent();
1337
1338 /*
1339 * Close any extra open file descriptors so that we don\'t have them
1340 * hanging around in clients. Note that we want to do this after
1341 * initgroups, because at least on Solaris 2.3 it leaves file
1342 * descriptors open.
1343 */
1344 for (i = 3; i < 64; i++)
1345 close(i);
1346
1347 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001348 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001349 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1350 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001351#ifdef HAVE_LOGIN_CAP
1352 if (login_getcapbool(lc, "requirehome", 0))
1353 exit(1);
1354#endif
1355 }
Damien Millerb38eff82000-04-01 11:09:21 +10001356
1357 /*
1358 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1359 * xauth are run in the proper environment.
1360 */
1361 environ = env;
1362
1363 /*
1364 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1365 * in this order).
1366 */
1367 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001368 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001369 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001370 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1371 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001372 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001373 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001374 if (do_xauth)
1375 fprintf(f, "%s %s\n", s->auth_proto,
1376 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001377 pclose(f);
1378 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001379 fprintf(stderr, "Could not run %s\n",
1380 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001381 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001382 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001383 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1384 _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001385
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001386 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001387 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001388 if (do_xauth)
1389 fprintf(f, "%s %s\n", s->auth_proto,
1390 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001391 pclose(f);
1392 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001393 fprintf(stderr, "Could not run %s\n",
1394 _PATH_SSH_SYSTEM_RC);
1395 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001396 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001397 char *screen = strchr(s->display, ':');
1398
1399 if (debug_flag) {
1400 fprintf(stderr,
1401 "Running %.100s add "
1402 "%.100s %.100s %.100s\n",
1403 options.xauth_location, s->display,
1404 s->auth_proto, s->auth_data);
1405 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001406 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001407 "Adding %.*s/unix%s %s %s\n",
1408 (int)(screen - s->display),
1409 s->display, screen,
1410 s->auth_proto, s->auth_data);
1411 }
1412 snprintf(cmd, sizeof cmd, "%s -q -",
1413 options.xauth_location);
1414 f = popen(cmd, "w");
1415 if (f) {
1416 fprintf(f, "add %s %s %s\n", s->display,
1417 s->auth_proto, s->auth_data);
1418 if (screen != NULL)
1419 fprintf(f, "add %.*s/unix%s %s %s\n",
1420 (int)(screen - s->display),
1421 s->display, screen,
1422 s->auth_proto,
1423 s->auth_data);
1424 pclose(f);
1425 } else {
1426 fprintf(stderr, "Could not run %s\n",
1427 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001428 }
1429 }
Damien Millerb38eff82000-04-01 11:09:21 +10001430 /* Get the last component of the shell name. */
1431 cp = strrchr(shell, '/');
1432 if (cp)
1433 cp++;
1434 else
1435 cp = shell;
1436 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001437
1438 /* restore SIGPIPE for child */
1439 signal(SIGPIPE, SIG_DFL);
1440
Damien Millerb38eff82000-04-01 11:09:21 +10001441 /*
1442 * If we have no command, execute the shell. In this case, the shell
1443 * name to be passed in argv[0] is preceded by '-' to indicate that
1444 * this is a login shell.
1445 */
1446 if (!command) {
1447 if (!options.use_login) {
1448 char buf[256];
1449
1450 /*
1451 * Check for mail if we have a tty and it was enabled
1452 * in server options.
1453 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001454 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001455 char *mailbox;
1456 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001457
Damien Millerb38eff82000-04-01 11:09:21 +10001458 mailbox = getenv("MAIL");
1459 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001460 if (stat(mailbox, &mailstat) != 0 ||
1461 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001462 printf("No mail.\n");
1463 else if (mailstat.st_mtime < mailstat.st_atime)
1464 printf("You have mail.\n");
1465 else
1466 printf("You have new mail.\n");
1467 }
1468 }
1469 /* Start the shell. Set initial character to '-'. */
1470 buf[0] = '-';
1471 strncpy(buf + 1, cp, sizeof(buf) - 1);
1472 buf[sizeof(buf) - 1] = 0;
1473
1474 /* Execute the shell. */
1475 argv[0] = buf;
1476 argv[1] = NULL;
1477 execve(shell, argv, env);
1478
1479 /* Executing the shell failed. */
1480 perror(shell);
1481 exit(1);
1482
1483 } else {
1484 /* Launch login(1). */
1485
Damien Millerad833b32000-08-23 10:46:23 +10001486 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001487 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001488
1489 /* Login couldn't be executed, die. */
1490
1491 perror("login");
1492 exit(1);
1493 }
1494 }
1495 /*
1496 * Execute the command using the user's shell. This uses the -c
1497 * option to execute the command.
1498 */
1499 argv[0] = (char *) cp;
1500 argv[1] = "-c";
1501 argv[2] = (char *) command;
1502 argv[3] = NULL;
1503 execve(shell, argv, env);
1504 perror(shell);
1505 exit(1);
1506}
1507
1508Session *
1509session_new(void)
1510{
1511 int i;
1512 static int did_init = 0;
1513 if (!did_init) {
1514 debug("session_new: init");
1515 for(i = 0; i < MAX_SESSIONS; i++) {
1516 sessions[i].used = 0;
1517 sessions[i].self = i;
1518 }
1519 did_init = 1;
1520 }
1521 for(i = 0; i < MAX_SESSIONS; i++) {
1522 Session *s = &sessions[i];
1523 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001524 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001525 s->chanid = -1;
1526 s->ptyfd = -1;
1527 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001528 s->used = 1;
Damien Miller15b29522000-10-14 12:33:48 +11001529 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001530 return s;
1531 }
1532 }
1533 return NULL;
1534}
1535
1536void
1537session_dump(void)
1538{
1539 int i;
1540 for(i = 0; i < MAX_SESSIONS; i++) {
1541 Session *s = &sessions[i];
1542 debug("dump: used %d session %d %p channel %d pid %d",
1543 s->used,
1544 s->self,
1545 s,
1546 s->chanid,
1547 s->pid);
1548 }
1549}
1550
Damien Millerefb4afe2000-04-12 18:45:05 +10001551int
1552session_open(int chanid)
1553{
1554 Session *s = session_new();
1555 debug("session_open: channel %d", chanid);
1556 if (s == NULL) {
1557 error("no more sessions");
1558 return 0;
1559 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001560 s->pw = auth_get_user();
1561 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001562 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001563 debug("session_open: session %d: link with channel %d", s->self, chanid);
1564 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001565 return 1;
1566}
1567
1568Session *
1569session_by_channel(int id)
1570{
1571 int i;
1572 for(i = 0; i < MAX_SESSIONS; i++) {
1573 Session *s = &sessions[i];
1574 if (s->used && s->chanid == id) {
1575 debug("session_by_channel: session %d channel %d", i, id);
1576 return s;
1577 }
1578 }
1579 debug("session_by_channel: unknown channel %d", id);
1580 session_dump();
1581 return NULL;
1582}
1583
1584Session *
1585session_by_pid(pid_t pid)
1586{
1587 int i;
1588 debug("session_by_pid: pid %d", pid);
1589 for(i = 0; i < MAX_SESSIONS; i++) {
1590 Session *s = &sessions[i];
1591 if (s->used && s->pid == pid)
1592 return s;
1593 }
1594 error("session_by_pid: unknown pid %d", pid);
1595 session_dump();
1596 return NULL;
1597}
1598
1599int
1600session_window_change_req(Session *s)
1601{
1602 s->col = packet_get_int();
1603 s->row = packet_get_int();
1604 s->xpixel = packet_get_int();
1605 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001606 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001607 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1608 return 1;
1609}
1610
1611int
1612session_pty_req(Session *s)
1613{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001614 u_int len;
Damien Miller4af51302000-04-16 11:18:38 +10001615 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001616
Damien Millerf6d9e222000-06-18 14:50:44 +10001617 if (no_pty_flag)
1618 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001619 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001620 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001621 s->term = packet_get_string(&len);
1622 s->col = packet_get_int();
1623 s->row = packet_get_int();
1624 s->xpixel = packet_get_int();
1625 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001626 term_modes = packet_get_string(&len);
1627 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001628
1629 if (strcmp(s->term, "") == 0) {
1630 xfree(s->term);
1631 s->term = NULL;
1632 }
1633 /* Allocate a pty and open it. */
1634 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1635 xfree(s->term);
1636 s->term = NULL;
1637 s->ptyfd = -1;
1638 s->ttyfd = -1;
1639 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001640 xfree(term_modes);
1641 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001642 }
1643 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1644 /*
1645 * Add a cleanup function to clear the utmp entry and record logout
1646 * time in case we call fatal() (e.g., the connection gets closed).
1647 */
1648 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1649 pty_setowner(s->pw, s->tty);
1650 /* Get window size from the packet. */
1651 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1652
Damien Millere247cc42000-05-07 12:03:14 +10001653 session_proctitle(s);
1654
Damien Miller5f056372000-04-16 12:31:48 +10001655 /* XXX parse and set terminal modes */
1656 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001657 return 1;
1658}
1659
Damien Millerbd483e72000-04-30 10:00:53 +10001660int
1661session_subsystem_req(Session *s)
1662{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001663 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001664 int success = 0;
1665 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001666 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001667
1668 packet_done();
1669 log("subsystem request for %s", subsys);
1670
Damien Millerf6d9e222000-06-18 14:50:44 +10001671 for (i = 0; i < options.num_subsystems; i++) {
1672 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1673 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001674 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001675 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001676 success = 1;
1677 }
1678 }
1679
1680 if (!success)
1681 log("subsystem request for %s failed, subsystem not found", subsys);
1682
Damien Millerbd483e72000-04-30 10:00:53 +10001683 xfree(subsys);
1684 return success;
1685}
1686
1687int
1688session_x11_req(Session *s)
1689{
Damien Miller7b28dc52000-09-05 13:34:53 +11001690 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001691 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001692 debug("X11 forwarding disabled in user configuration file.");
1693 return 0;
1694 }
Damien Millerbd483e72000-04-30 10:00:53 +10001695 if (!options.x11_forwarding) {
1696 debug("X11 forwarding disabled in server configuration file.");
1697 return 0;
1698 }
1699 if (xauthfile != NULL) {
1700 debug("X11 fwd already started.");
1701 return 0;
1702 }
1703
1704 debug("Received request for X11 forwarding with auth spoofing.");
1705 if (s->display != NULL)
1706 packet_disconnect("Protocol error: X11 display already set.");
1707
1708 s->single_connection = packet_get_char();
1709 s->auth_proto = packet_get_string(NULL);
1710 s->auth_data = packet_get_string(NULL);
1711 s->screen = packet_get_int();
1712 packet_done();
1713
1714 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1715 if (s->display == NULL) {
1716 xfree(s->auth_proto);
1717 xfree(s->auth_data);
1718 return 0;
1719 }
1720 xauthfile = xmalloc(MAXPATHLEN);
1721 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1722 temporarily_use_uid(s->pw->pw_uid);
1723 if (mkdtemp(xauthfile) == NULL) {
1724 restore_uid();
1725 error("private X11 dir: mkdtemp %s failed: %s",
1726 xauthfile, strerror(errno));
1727 xfree(xauthfile);
1728 xauthfile = NULL;
1729 xfree(s->auth_proto);
1730 xfree(s->auth_data);
1731 /* XXXX remove listening channels */
1732 return 0;
1733 }
1734 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001735 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1736 if (fd >= 0)
1737 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001738 restore_uid();
1739 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1740 return 1;
1741}
1742
Damien Millerf6d9e222000-06-18 14:50:44 +10001743int
1744session_shell_req(Session *s)
1745{
1746 /* if forced_command == NULL, the shell is execed */
1747 char *shell = forced_command;
1748 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001749 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001750 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001751 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001752 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001753 return 1;
1754}
1755
1756int
1757session_exec_req(Session *s)
1758{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001759 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001760 char *command = packet_get_string(&len);
1761 packet_done();
1762 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001763 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001764 command = forced_command;
1765 debug("Forced command '%.500s'", forced_command);
1766 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001767 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001768 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001769 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001770 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001771 if (forced_command == NULL)
1772 xfree(command);
1773 return 1;
1774}
1775
Damien Miller0bc1bd82000-11-13 22:57:25 +11001776int
1777session_auth_agent_req(Session *s)
1778{
1779 static int called = 0;
1780 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001781 if (no_agent_forwarding_flag) {
1782 debug("session_auth_agent_req: no_agent_forwarding_flag");
1783 return 0;
1784 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001785 if (called) {
1786 return 0;
1787 } else {
1788 called = 1;
1789 return auth_input_request_forwarding(s->pw);
1790 }
1791}
1792
Damien Millerefb4afe2000-04-12 18:45:05 +10001793void
1794session_input_channel_req(int id, void *arg)
1795{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001796 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001797 int reply;
1798 int success = 0;
1799 char *rtype;
1800 Session *s;
1801 Channel *c;
1802
1803 rtype = packet_get_string(&len);
1804 reply = packet_get_char();
1805
1806 s = session_by_channel(id);
1807 if (s == NULL)
1808 fatal("session_input_channel_req: channel %d: no session", id);
1809 c = channel_lookup(id);
1810 if (c == NULL)
1811 fatal("session_input_channel_req: channel %d: bad channel", id);
1812
1813 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1814 s->self, id, rtype, reply);
1815
1816 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001817 * a session is in LARVAL state until a shell, a command
1818 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001819 */
1820 if (c->type == SSH_CHANNEL_LARVAL) {
1821 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001822 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001823 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001824 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001825 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001826 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001827 } else if (strcmp(rtype, "x11-req") == 0) {
1828 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001829 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1830 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001831 } else if (strcmp(rtype, "subsystem") == 0) {
1832 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001833 }
1834 }
1835 if (strcmp(rtype, "window-change") == 0) {
1836 success = session_window_change_req(s);
1837 }
1838
1839 if (reply) {
1840 packet_start(success ?
1841 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1842 packet_put_int(c->remote_id);
1843 packet_send();
1844 }
1845 xfree(rtype);
1846}
1847
1848void
1849session_set_fds(Session *s, int fdin, int fdout, int fderr)
1850{
1851 if (!compat20)
1852 fatal("session_set_fds: called for proto != 2.0");
1853 /*
1854 * now that have a child and a pipe to the child,
1855 * we can activate our channel and register the fd's
1856 */
1857 if (s->chanid == -1)
1858 fatal("no channel for session %d", s->self);
1859 channel_set_fds(s->chanid,
1860 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001861 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1862 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001863}
1864
Damien Millerb38eff82000-04-01 11:09:21 +10001865void
1866session_pty_cleanup(Session *s)
1867{
1868 if (s == NULL || s->ttyfd == -1)
1869 return;
1870
Kevin Steves43cdef32001-02-11 14:12:08 +00001871 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001872
1873 /* Cancel the cleanup function. */
1874 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1875
1876 /* Record that the user has logged out. */
1877 record_logout(s->pid, s->tty);
1878
1879 /* Release the pseudo-tty. */
1880 pty_release(s->tty);
1881
1882 /*
1883 * Close the server side of the socket pairs. We must do this after
1884 * the pty cleanup, so that another process doesn't get this pty
1885 * while we're still cleaning up.
1886 */
1887 if (close(s->ptymaster) < 0)
1888 error("close(s->ptymaster): %s", strerror(errno));
1889}
Damien Millerefb4afe2000-04-12 18:45:05 +10001890
1891void
1892session_exit_message(Session *s, int status)
1893{
1894 Channel *c;
1895 if (s == NULL)
1896 fatal("session_close: no session");
1897 c = channel_lookup(s->chanid);
1898 if (c == NULL)
1899 fatal("session_close: session %d: no channel %d",
1900 s->self, s->chanid);
1901 debug("session_exit_message: session %d channel %d pid %d",
1902 s->self, s->chanid, s->pid);
1903
1904 if (WIFEXITED(status)) {
1905 channel_request_start(s->chanid,
1906 "exit-status", 0);
1907 packet_put_int(WEXITSTATUS(status));
1908 packet_send();
1909 } else if (WIFSIGNALED(status)) {
1910 channel_request_start(s->chanid,
1911 "exit-signal", 0);
1912 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001913#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001914 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001915#else /* WCOREDUMP */
1916 packet_put_char(0);
1917#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001918 packet_put_cstring("");
1919 packet_put_cstring("");
1920 packet_send();
1921 } else {
1922 /* Some weird exit cause. Just exit. */
1923 packet_disconnect("wait returned status %04x.", status);
1924 }
1925
1926 /* disconnect channel */
1927 debug("session_exit_message: release channel %d", s->chanid);
1928 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001929 /*
1930 * emulate a write failure with 'chan_write_failed', nobody will be
1931 * interested in data we write.
1932 * Note that we must not call 'chan_read_failed', since there could
1933 * be some more data waiting in the pipe.
1934 */
Damien Millerbd483e72000-04-30 10:00:53 +10001935 if (c->ostate != CHAN_OUTPUT_CLOSED)
1936 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001937 s->chanid = -1;
1938}
1939
1940void
1941session_free(Session *s)
1942{
1943 debug("session_free: session %d pid %d", s->self, s->pid);
1944 if (s->term)
1945 xfree(s->term);
1946 if (s->display)
1947 xfree(s->display);
1948 if (s->auth_data)
1949 xfree(s->auth_data);
1950 if (s->auth_proto)
1951 xfree(s->auth_proto);
1952 s->used = 0;
1953}
1954
1955void
1956session_close(Session *s)
1957{
1958 session_pty_cleanup(s);
1959 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001960 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001961}
1962
1963void
1964session_close_by_pid(pid_t pid, int status)
1965{
1966 Session *s = session_by_pid(pid);
1967 if (s == NULL) {
1968 debug("session_close_by_pid: no session for pid %d", s->pid);
1969 return;
1970 }
1971 if (s->chanid != -1)
1972 session_exit_message(s, status);
1973 session_close(s);
1974}
1975
1976/*
1977 * this is called when a channel dies before
1978 * the session 'child' itself dies
1979 */
1980void
1981session_close_by_channel(int id, void *arg)
1982{
1983 Session *s = session_by_channel(id);
1984 if (s == NULL) {
1985 debug("session_close_by_channel: no session for channel %d", id);
1986 return;
1987 }
1988 /* disconnect channel */
1989 channel_cancel_cleanup(s->chanid);
1990 s->chanid = -1;
1991
1992 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1993 if (s->pid == 0) {
1994 /* close session immediately */
1995 session_close(s);
1996 } else {
1997 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001998 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001999 error("session_close_by_channel: kill %d: %s",
2000 s->pid, strerror(errno));
2001 }
2002}
2003
Damien Millere247cc42000-05-07 12:03:14 +10002004char *
2005session_tty_list(void)
2006{
2007 static char buf[1024];
2008 int i;
2009 buf[0] = '\0';
2010 for(i = 0; i < MAX_SESSIONS; i++) {
2011 Session *s = &sessions[i];
2012 if (s->used && s->ttyfd != -1) {
2013 if (buf[0] != '\0')
2014 strlcat(buf, ",", sizeof buf);
2015 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2016 }
2017 }
2018 if (buf[0] == '\0')
2019 strlcpy(buf, "notty", sizeof buf);
2020 return buf;
2021}
2022
2023void
2024session_proctitle(Session *s)
2025{
2026 if (s->pw == NULL)
2027 error("no user for session %d", s->self);
2028 else
2029 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2030}
2031
Damien Millerefb4afe2000-04-12 18:45:05 +10002032void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002033do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002034{
Ben Lindstromb31783d2001-03-22 02:02:12 +00002035
Damien Millerefb4afe2000-04-12 18:45:05 +10002036 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002037 if (xauthfile)
2038 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002039}