blob: 4580c3025b0c1b236e64408a17d840cfe17278cb [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"
Damien Millercf205e82001-04-16 18:29:15 +100036RCSID("$OpenBSD: session.c,v 1.73 2001/04/16 08:19:31 djm 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 Millercf205e82001-04-16 18:29:15 +1000131void do_motd(void);
132int check_quietlogin(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000133
Ben Lindstromb31783d2001-03-22 02:02:12 +0000134void do_authenticated1(Authctxt *authctxt);
135void do_authenticated2(Authctxt *authctxt);
136
Damien Millerb38eff82000-04-01 11:09:21 +1000137/* import */
138extern ServerOptions options;
139extern char *__progname;
140extern int log_stderr;
141extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000142extern u_int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000143
Damien Miller37023962000-07-11 17:31:38 +1000144extern int startup_pipe;
145
Damien Millerb38eff82000-04-01 11:09:21 +1000146/* Local Xauthority file. */
147static char *xauthfile;
148
Damien Miller7b28dc52000-09-05 13:34:53 +1100149/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000150char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100151
Damien Millerb38eff82000-04-01 11:09:21 +1000152/* data */
153#define MAX_SESSIONS 10
154Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100155
Damien Millerd2c208a2000-05-17 22:00:02 +1000156#ifdef WITH_AIXAUTHENTICATE
157/* AIX's lastlogin message, set in auth1.c */
158char *aixloginmsg;
159#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000160
Damien Millerad833b32000-08-23 10:46:23 +1000161#ifdef HAVE_LOGIN_CAP
162static login_cap_t *lc;
163#endif
164
Ben Lindstromb31783d2001-03-22 02:02:12 +0000165void
166do_authenticated(Authctxt *authctxt)
167{
168 /*
169 * Cancel the alarm we set to limit the time taken for
170 * authentication.
171 */
172 alarm(0);
173 if (startup_pipe != -1) {
174 close(startup_pipe);
175 startup_pipe = -1;
176 }
177#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
178 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
179 error("unable to get login class");
180 return;
181 }
182#endif
183 /* setup the channel layer */
184 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
185 channel_permit_all_opens();
186
187 if (compat20)
188 do_authenticated2(authctxt);
189 else
190 do_authenticated1(authctxt);
191}
192
Damien Millerb38eff82000-04-01 11:09:21 +1000193/*
194 * Remove local Xauthority file.
195 */
196void
197xauthfile_cleanup_proc(void *ignore)
198{
199 debug("xauthfile_cleanup_proc called");
200
201 if (xauthfile != NULL) {
202 char *p;
203 unlink(xauthfile);
204 p = strrchr(xauthfile, '/');
205 if (p != NULL) {
206 *p = '\0';
207 rmdir(xauthfile);
208 }
209 xfree(xauthfile);
210 xauthfile = NULL;
211 }
212}
213
214/*
215 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
216 * dropped connection).
217 */
Damien Miller4af51302000-04-16 11:18:38 +1000218void
Damien Millerb38eff82000-04-01 11:09:21 +1000219pty_cleanup_proc(void *session)
220{
221 Session *s=session;
222 if (s == NULL)
223 fatal("pty_cleanup_proc: no session");
224 debug("pty_cleanup_proc: %s", s->tty);
225
226 if (s->pid != 0) {
227 /* Record that the user has logged out. */
228 record_logout(s->pid, s->tty);
229 }
230
231 /* Release the pseudo-tty. */
232 pty_release(s->tty);
233}
234
235/*
236 * Prepares for an interactive session. This is called after the user has
237 * been successfully authenticated. During this message exchange, pseudo
238 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
239 * are requested, etc.
240 */
Damien Miller4af51302000-04-16 11:18:38 +1000241void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000242do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000243{
244 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000245 char *command;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000246 int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
247 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000248 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000249
250 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000251 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000252
Damien Millerb38eff82000-04-01 11:09:21 +1000253 /*
254 * We stay in this loop until the client requests to execute a shell
255 * or a command.
256 */
257 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000258 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000259
260 /* Get a packet from the client. */
261 type = packet_read(&plen);
262
263 /* Process the packet. */
264 switch (type) {
265 case SSH_CMSG_REQUEST_COMPRESSION:
266 packet_integrity_check(plen, 4, type);
267 compression_level = packet_get_int();
268 if (compression_level < 1 || compression_level > 9) {
269 packet_send_debug("Received illegal compression level %d.",
270 compression_level);
271 break;
272 }
273 /* Enable compression after we have responded with SUCCESS. */
274 enable_compression_after_reply = 1;
275 success = 1;
276 break;
277
278 case SSH_CMSG_REQUEST_PTY:
279 if (no_pty_flag) {
280 debug("Allocating a pty not permitted for this authentication.");
281 break;
282 }
283 if (have_pty)
284 packet_disconnect("Protocol error: you already have a pty.");
285
286 debug("Allocating pty.");
287
288 /* Allocate a pty and open it. */
289 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
290 sizeof(s->tty))) {
291 error("Failed to allocate pty.");
292 break;
293 }
294 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000295 pty_setowner(s->pw, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000296
297 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
298 s->term = packet_get_string(&dlen);
299 packet_integrity_check(dlen, strlen(s->term), type);
300 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
301 /* Remaining bytes */
302 n_bytes = plen - (4 + dlen + 4 * 4);
303
304 if (strcmp(s->term, "") == 0) {
305 xfree(s->term);
306 s->term = NULL;
307 }
308 /* Get window size from the packet. */
309 s->row = packet_get_int();
310 s->col = packet_get_int();
311 s->xpixel = packet_get_int();
312 s->ypixel = packet_get_int();
313 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
314
315 /* Get tty modes from the packet. */
316 tty_parse_modes(s->ttyfd, &n_bytes);
317 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
318
Damien Millere247cc42000-05-07 12:03:14 +1000319 session_proctitle(s);
320
Damien Millerb38eff82000-04-01 11:09:21 +1000321 /* Indicate that we now have a pty. */
322 success = 1;
323 have_pty = 1;
324 break;
325
326 case SSH_CMSG_X11_REQUEST_FORWARDING:
327 if (!options.x11_forwarding) {
328 packet_send_debug("X11 forwarding disabled in server configuration file.");
329 break;
330 }
Damien Miller0c043c12000-06-07 21:22:38 +1000331 if (!options.xauth_location) {
332 packet_send_debug("No xauth program; cannot forward with spoofing.");
333 break;
334 }
Damien Millerb38eff82000-04-01 11:09:21 +1000335 if (no_x11_forwarding_flag) {
336 packet_send_debug("X11 forwarding not permitted for this authentication.");
337 break;
338 }
339 debug("Received request for X11 forwarding with auth spoofing.");
340 if (s->display != NULL)
341 packet_disconnect("Protocol error: X11 display already set.");
342
343 s->auth_proto = packet_get_string(&proto_len);
344 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000345
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000346 screen_flag = packet_get_protocol_flags() &
347 SSH_PROTOFLAG_SCREEN_NUMBER;
348 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
349
350 if (packet_remaining() == 4) {
351 if (!screen_flag)
352 debug2("Buggy client: "
353 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000354 packet_integrity_check(plen,
355 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000356 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000357 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000358 packet_integrity_check(plen,
359 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000360 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000361 }
Damien Millerb38eff82000-04-01 11:09:21 +1000362 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
363
364 if (s->display == NULL)
365 break;
366
367 /* Setup to always have a local .Xauthority. */
368 xauthfile = xmalloc(MAXPATHLEN);
369 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000370 temporarily_use_uid(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000371 if (mkdtemp(xauthfile) == NULL) {
372 restore_uid();
373 error("private X11 dir: mkdtemp %s failed: %s",
374 xauthfile, strerror(errno));
375 xfree(xauthfile);
376 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000377 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000378 break;
379 }
380 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100381 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
382 if (fd >= 0)
383 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000384 restore_uid();
385 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
386 success = 1;
387 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000388
389 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
390 if (no_agent_forwarding_flag || compat13) {
391 debug("Authentication agent forwarding not permitted for this authentication.");
392 break;
393 }
394 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000395 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000396 break;
397
398 case SSH_CMSG_PORT_FORWARD_REQUEST:
399 if (no_port_forwarding_flag) {
400 debug("Port forwarding not permitted for this authentication.");
401 break;
402 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100403 if (!options.allow_tcp_forwarding) {
404 debug("Port forwarding not permitted.");
405 break;
406 }
Damien Millerb38eff82000-04-01 11:09:21 +1000407 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000408 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000409 success = 1;
410 break;
411
412 case SSH_CMSG_MAX_PACKET_SIZE:
413 if (packet_set_maxsize(packet_get_int()) > 0)
414 success = 1;
415 break;
416
417 case SSH_CMSG_EXEC_SHELL:
418 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000419 if (type == SSH_CMSG_EXEC_CMD) {
420 command = packet_get_string(&dlen);
421 debug("Exec command '%.500s'", command);
422 packet_integrity_check(plen, 4 + dlen, type);
423 } else {
424 command = NULL;
425 packet_integrity_check(plen, 0, type);
426 }
427 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000429 command = forced_command;
430 debug("Forced command '%.500s'", forced_command);
431 }
432 if (have_pty)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000433 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000434 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000435 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000436
437 if (command != NULL)
438 xfree(command);
439 /* Cleanup user's local Xauthority file. */
440 if (xauthfile)
441 xauthfile_cleanup_proc(NULL);
442 return;
443
444 default:
445 /*
446 * Any unknown messages in this phase are ignored,
447 * and a failure message is returned.
448 */
449 log("Unknown packet type received after authentication: %d", type);
450 }
451 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
452 packet_send();
453 packet_write_wait();
454
455 /* Enable compression now that we have replied if appropriate. */
456 if (enable_compression_after_reply) {
457 enable_compression_after_reply = 0;
458 packet_start_compression(compression_level);
459 }
460 }
461}
462
463/*
464 * This is called to fork and execute a command when we have no tty. This
465 * will call do_child from the child, and server_loop from the parent after
466 * setting up file descriptors and such.
467 */
Damien Miller4af51302000-04-16 11:18:38 +1000468void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000469do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000470{
471 int pid;
472
473#ifdef USE_PIPES
474 int pin[2], pout[2], perr[2];
475 /* Allocate pipes for communicating with the program. */
476 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
477 packet_disconnect("Could not create pipes: %.100s",
478 strerror(errno));
479#else /* USE_PIPES */
480 int inout[2], err[2];
481 /* Uses socket pairs to communicate with the program. */
482 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
483 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
484 packet_disconnect("Could not create socket pairs: %.100s",
485 strerror(errno));
486#endif /* USE_PIPES */
487 if (s == NULL)
488 fatal("do_exec_no_pty: no session");
489
Damien Millere247cc42000-05-07 12:03:14 +1000490 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000491
Damien Millerc5946332001-02-28 11:46:11 +1100492#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000493 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000494#endif /* USE_PAM */
495
Damien Millerb38eff82000-04-01 11:09:21 +1000496 /* Fork the child. */
497 if ((pid = fork()) == 0) {
498 /* Child. Reinitialize the log since the pid has changed. */
499 log_init(__progname, options.log_level, options.log_facility, log_stderr);
500
501 /*
502 * Create a new session and process group since the 4.4BSD
503 * setlogin() affects the entire process group.
504 */
505 if (setsid() < 0)
506 error("setsid failed: %.100s", strerror(errno));
507
508#ifdef USE_PIPES
509 /*
510 * Redirect stdin. We close the parent side of the socket
511 * pair, and make the child side the standard input.
512 */
513 close(pin[1]);
514 if (dup2(pin[0], 0) < 0)
515 perror("dup2 stdin");
516 close(pin[0]);
517
518 /* Redirect stdout. */
519 close(pout[0]);
520 if (dup2(pout[1], 1) < 0)
521 perror("dup2 stdout");
522 close(pout[1]);
523
524 /* Redirect stderr. */
525 close(perr[0]);
526 if (dup2(perr[1], 2) < 0)
527 perror("dup2 stderr");
528 close(perr[1]);
529#else /* USE_PIPES */
530 /*
531 * Redirect stdin, stdout, and stderr. Stdin and stdout will
532 * use the same socket, as some programs (particularly rdist)
533 * seem to depend on it.
534 */
535 close(inout[1]);
536 close(err[1]);
537 if (dup2(inout[0], 0) < 0) /* stdin */
538 perror("dup2 stdin");
539 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
540 perror("dup2 stdout");
541 if (dup2(err[0], 2) < 0) /* stderr */
542 perror("dup2 stderr");
543#endif /* USE_PIPES */
544
545 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000546 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000547 /* NOTREACHED */
548 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100549#ifdef HAVE_CYGWIN
550 if (is_winnt)
551 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
552#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000553 if (pid < 0)
554 packet_disconnect("fork failed: %.100s", strerror(errno));
555 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000556 /* Set interactive/non-interactive mode. */
557 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000558#ifdef USE_PIPES
559 /* We are the parent. Close the child sides of the pipes. */
560 close(pin[0]);
561 close(pout[1]);
562 close(perr[1]);
563
Damien Millerefb4afe2000-04-12 18:45:05 +1000564 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000565 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000566 } else {
567 /* Enter the interactive session. */
568 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000569 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000570 }
Damien Millerb38eff82000-04-01 11:09:21 +1000571#else /* USE_PIPES */
572 /* We are the parent. Close the child sides of the socket pairs. */
573 close(inout[0]);
574 close(err[0]);
575
576 /*
577 * Enter the interactive session. Note: server_loop must be able to
578 * handle the case that fdin and fdout are the same.
579 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000580 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000581 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000582 } else {
583 server_loop(pid, inout[1], inout[1], err[1]);
584 /* server_loop has closed inout[1] and err[1]. */
585 }
Damien Millerb38eff82000-04-01 11:09:21 +1000586#endif /* USE_PIPES */
587}
588
589/*
590 * This is called to fork and execute a command when we have a tty. This
591 * will call do_child from the child, and server_loop from the parent after
592 * setting up file descriptors, controlling tty, updating wtmp, utmp,
593 * lastlog, and other such operations.
594 */
Damien Miller4af51302000-04-16 11:18:38 +1000595void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000596do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000597{
Damien Millerb38eff82000-04-01 11:09:21 +1000598 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000599 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000600
601 if (s == NULL)
602 fatal("do_exec_pty: no session");
603 ptyfd = s->ptyfd;
604 ttyfd = s->ttyfd;
605
Damien Millerc5946332001-02-28 11:46:11 +1100606#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000607 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000608 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100609#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000610
Damien Millerb38eff82000-04-01 11:09:21 +1000611 /* Fork the child. */
612 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000613 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000614 log_init(__progname, options.log_level, options.log_facility, log_stderr);
615
616 /* Close the master side of the pseudo tty. */
617 close(ptyfd);
618
619 /* Make the pseudo tty our controlling tty. */
620 pty_make_controlling_tty(&ttyfd, s->tty);
621
622 /* Redirect stdin from the pseudo tty. */
623 if (dup2(ttyfd, fileno(stdin)) < 0)
624 error("dup2 stdin failed: %.100s", strerror(errno));
625
626 /* Redirect stdout to the pseudo tty. */
627 if (dup2(ttyfd, fileno(stdout)) < 0)
628 error("dup2 stdin failed: %.100s", strerror(errno));
629
630 /* Redirect stderr to the pseudo tty. */
631 if (dup2(ttyfd, fileno(stderr)) < 0)
632 error("dup2 stdin failed: %.100s", strerror(errno));
633
634 /* Close the extra descriptor for the pseudo tty. */
635 close(ttyfd);
636
Damien Miller942da032000-08-18 13:59:06 +1000637 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000638#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100639 if (!(options.use_login && command == NULL))
640 do_login(s, command);
Damien Miller364a9bd2001-04-16 18:37:05 +1000641#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000642
Damien Millerb38eff82000-04-01 11:09:21 +1000643 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000644 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000645 /* NOTREACHED */
646 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100647#ifdef HAVE_CYGWIN
648 if (is_winnt)
649 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
650#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000651 if (pid < 0)
652 packet_disconnect("fork failed: %.100s", strerror(errno));
653 s->pid = pid;
654
655 /* Parent. Close the slave side of the pseudo tty. */
656 close(ttyfd);
657
658 /*
659 * Create another descriptor of the pty master side for use as the
660 * standard input. We could use the original descriptor, but this
661 * simplifies code in server_loop. The descriptor is bidirectional.
662 */
663 fdout = dup(ptyfd);
664 if (fdout < 0)
665 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
666
667 /* we keep a reference to the pty master */
668 ptymaster = dup(ptyfd);
669 if (ptymaster < 0)
670 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
671 s->ptymaster = ptymaster;
672
673 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000674 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000675 if (compat20) {
676 session_set_fds(s, ptyfd, fdout, -1);
677 } else {
678 server_loop(pid, ptyfd, fdout, -1);
679 /* server_loop _has_ closed ptyfd and fdout. */
680 session_pty_cleanup(s);
681 }
Damien Millerb38eff82000-04-01 11:09:21 +1000682}
683
Damien Miller942da032000-08-18 13:59:06 +1000684/* administrative, login(1)-like work */
685void
Damien Miller69b69aa2000-10-28 14:19:58 +1100686do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000687{
Damien Miller942da032000-08-18 13:59:06 +1000688 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100689 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000690 socklen_t fromlen;
691 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000692 time_t last_login_time;
693 struct passwd * pw = s->pw;
694 pid_t pid = getpid();
695
696 /*
697 * Get IP address of client. If the connection is not a socket, let
698 * the address be 0.0.0.0.
699 */
700 memset(&from, 0, sizeof(from));
701 if (packet_connection_is_on_socket()) {
702 fromlen = sizeof(from);
703 if (getpeername(packet_get_connection_in(),
704 (struct sockaddr *) & from, &fromlen) < 0) {
705 debug("getpeername: %.100s", strerror(errno));
706 fatal_cleanup();
707 }
708 }
709
Damien Miller7b28dc52000-09-05 13:34:53 +1100710 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000711 if (options.print_lastlog) {
712 hostname[0] = '\0';
713 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
714 hostname, sizeof(hostname));
715 }
Damien Millere4340be2000-09-16 13:29:08 +1100716
Damien Miller942da032000-08-18 13:59:06 +1000717 /* Record that there was a login on that tty from the remote host. */
718 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000719 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
720 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000721
Kevin Steves092f2ef2000-10-14 13:36:13 +0000722#ifdef USE_PAM
723 /*
724 * If password change is needed, do it now.
725 * This needs to occur before the ~/.hushlogin check.
726 */
Damien Miller646aa602001-02-15 11:51:32 +1100727 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000728 print_pam_messages();
729 do_pam_chauthtok();
730 }
731#endif
732
Damien Millercf205e82001-04-16 18:29:15 +1000733 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000734 return;
735
736#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100737 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000738 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000739#endif /* USE_PAM */
740#ifdef WITH_AIXAUTHENTICATE
741 if (aixloginmsg && *aixloginmsg)
742 printf("%s\n", aixloginmsg);
743#endif /* WITH_AIXAUTHENTICATE */
744
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000745 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000746 time_string = ctime(&last_login_time);
747 if (strchr(time_string, '\n'))
748 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000749 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000750 printf("Last login: %s\r\n", time_string);
751 else
Damien Millere4340be2000-09-16 13:29:08 +1100752 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000753 }
Damien Millercf205e82001-04-16 18:29:15 +1000754
755 do_motd();
756}
757
758/*
759 * Display the message of the day.
760 */
761void
762do_motd(void)
763{
764 FILE *f;
765 char buf[256];
766
Damien Miller942da032000-08-18 13:59:06 +1000767 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000768#ifdef HAVE_LOGIN_CAP
769 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
770 "/etc/motd"), "r");
771#else
Damien Miller942da032000-08-18 13:59:06 +1000772 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000773#endif
Damien Miller942da032000-08-18 13:59:06 +1000774 if (f) {
775 while (fgets(buf, sizeof(buf), f))
776 fputs(buf, stdout);
777 fclose(f);
778 }
779 }
780}
781
Damien Millercf205e82001-04-16 18:29:15 +1000782
783/*
784 * Check for quiet login, either .hushlogin or command given.
785 */
786int
787check_quietlogin(Session *s, const char *command)
788{
789 char buf[256];
790 struct passwd * pw = s->pw;
791 struct stat st;
792
793 /* Return 1 if .hushlogin exists or a command given. */
794 if (command != NULL)
795 return 1;
796 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
797#ifdef HAVE_LOGIN_CAP
798 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
799 return 1;
800#else
801 if (stat(buf, &st) >= 0)
802 return 1;
803#endif
804 return 0;
805}
806
Damien Millerb38eff82000-04-01 11:09:21 +1000807/*
808 * Sets the value of the given variable in the environment. If the variable
809 * already exists, its value is overriden.
810 */
Damien Miller4af51302000-04-16 11:18:38 +1000811void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000812child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000813 const char *value)
814{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000815 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000816 char **env;
817
818 /*
819 * Find the slot where the value should be stored. If the variable
820 * already exists, we reuse the slot; otherwise we append a new slot
821 * at the end of the array, expanding if necessary.
822 */
823 env = *envp;
824 namelen = strlen(name);
825 for (i = 0; env[i]; i++)
826 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
827 break;
828 if (env[i]) {
829 /* Reuse the slot. */
830 xfree(env[i]);
831 } else {
832 /* New variable. Expand if necessary. */
833 if (i >= (*envsizep) - 1) {
834 (*envsizep) += 50;
835 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
836 }
837 /* Need to set the NULL pointer at end of array beyond the new slot. */
838 env[i + 1] = NULL;
839 }
840
841 /* Allocate space and format the variable in the appropriate slot. */
842 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
843 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
844}
845
846/*
847 * Reads environment variables from the given file and adds/overrides them
848 * into the environment. If the file does not exist, this does nothing.
849 * Otherwise, it must consist of empty lines, comments (line starts with '#')
850 * and assignments of the form name=value. No other forms are allowed.
851 */
Damien Miller4af51302000-04-16 11:18:38 +1000852void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000853read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000854 const char *filename)
855{
856 FILE *f;
857 char buf[4096];
858 char *cp, *value;
859
860 f = fopen(filename, "r");
861 if (!f)
862 return;
863
864 while (fgets(buf, sizeof(buf), f)) {
865 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
866 ;
867 if (!*cp || *cp == '#' || *cp == '\n')
868 continue;
869 if (strchr(cp, '\n'))
870 *strchr(cp, '\n') = '\0';
871 value = strchr(cp, '=');
872 if (value == NULL) {
873 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
874 continue;
875 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000876 /*
877 * Replace the equals sign by nul, and advance value to
878 * the value string.
879 */
Damien Millerb38eff82000-04-01 11:09:21 +1000880 *value = '\0';
881 value++;
882 child_set_env(env, envsize, cp, value);
883 }
884 fclose(f);
885}
886
887#ifdef USE_PAM
888/*
889 * Sets any environment variables which have been specified by PAM
890 */
891void do_pam_environment(char ***env, int *envsize)
892{
893 char *equals, var_name[512], var_val[512];
894 char **pam_env;
895 int i;
896
897 if ((pam_env = fetch_pam_environment()) == NULL)
898 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000899
Damien Millerb38eff82000-04-01 11:09:21 +1000900 for(i = 0; pam_env[i] != NULL; i++) {
901 if ((equals = strstr(pam_env[i], "=")) == NULL)
902 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000903
Damien Millerb38eff82000-04-01 11:09:21 +1000904 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
905 memset(var_name, '\0', sizeof(var_name));
906 memset(var_val, '\0', sizeof(var_val));
907
908 strncpy(var_name, pam_env[i], equals - pam_env[i]);
909 strcpy(var_val, equals + 1);
910
Damien Millercb5e44a2000-09-29 12:12:36 +1100911 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000912
913 child_set_env(env, envsize, var_name, var_val);
914 }
915 }
916}
917#endif /* USE_PAM */
918
Damien Millercb5e44a2000-09-29 12:12:36 +1100919#ifdef HAVE_CYGWIN
920void copy_environment(char ***env, int *envsize)
921{
922 char *equals, var_name[512], var_val[512];
923 int i;
924
925 for(i = 0; environ[i] != NULL; i++) {
926 if ((equals = strstr(environ[i], "=")) == NULL)
927 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000928
Damien Millercb5e44a2000-09-29 12:12:36 +1100929 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
930 memset(var_name, '\0', sizeof(var_name));
931 memset(var_val, '\0', sizeof(var_val));
932
933 strncpy(var_name, environ[i], equals - environ[i]);
934 strcpy(var_val, equals + 1);
935
936 debug3("Copy environment: %s=%s", var_name, var_val);
937
938 child_set_env(env, envsize, var_name, var_val);
939 }
940 }
941}
942#endif
943
Damien Miller5fc85652000-07-09 23:53:07 +1000944#if defined(HAVE_GETUSERATTR)
945/*
946 * AIX-specific login initialisation
947 */
948void set_limit(char *user, char *soft, char *hard, int resource, int mult)
949{
950 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000951 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000952
953 getrlimit(resource, &rlim);
954
Damien Miller65964d62000-07-11 09:16:22 +1000955 slim = 0;
956 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
957 if (slim < 0) {
958 rlim.rlim_cur = RLIM_INFINITY;
959 } else if (slim != 0) {
960 /* See the wackiness below */
961 if (rlim.rlim_cur == slim * mult)
962 slim = 0;
963 else
964 rlim.rlim_cur = slim * mult;
965 }
966 }
Damien Miller5fc85652000-07-09 23:53:07 +1000967
Damien Miller65964d62000-07-11 09:16:22 +1000968 hlim = 0;
969 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
970 if (hlim < 0) {
971 rlim.rlim_max = RLIM_INFINITY;
972 } else if (hlim != 0) {
973 rlim.rlim_max = hlim * mult;
974 }
975 }
Damien Miller5fc85652000-07-09 23:53:07 +1000976
Damien Miller65964d62000-07-11 09:16:22 +1000977 /*
978 * XXX For cpu and fsize the soft limit is set to the hard limit
979 * if the hard limit is left at its default value and the soft limit
980 * is changed from its default value, either by requesting it
981 * (slim == 0) or by setting it to the current default. At least
982 * that's how rlogind does it. If you're confused you're not alone.
983 * Bug or feature? AIX 4.3.1.2
984 */
985 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
986 && hlim == 0 && slim != 0)
987 rlim.rlim_max = rlim.rlim_cur;
988 /* A specified hard limit limits the soft limit */
989 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
990 rlim.rlim_cur = rlim.rlim_max;
991 /* A soft limit can increase a hard limit */
992 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000993 rlim.rlim_max = rlim.rlim_cur;
994
995 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000996 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000997}
998
999void set_limits_from_userattr(char *user)
1000{
1001 int mask;
1002 char buf[16];
1003
1004 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1005 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1006 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1007 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1008 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1009 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1010#if defined(S_UNOFILE)
1011 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1012#endif
1013
1014 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1015 /* Convert decimal to octal */
1016 (void) snprintf(buf, sizeof(buf), "%d", mask);
1017 if (sscanf(buf, "%o", &mask) == 1)
1018 umask(mask);
1019 }
1020}
1021#endif /* defined(HAVE_GETUSERATTR) */
1022
Damien Millerb38eff82000-04-01 11:09:21 +10001023/*
1024 * Performs common processing for the child, such as setting up the
1025 * environment, closing extra file descriptors, setting the user and group
1026 * ids, and executing the command or shell.
1027 */
Damien Miller4af51302000-04-16 11:18:38 +10001028void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001029do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001030{
Damien Miller7b28dc52000-09-05 13:34:53 +11001031 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001032 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001033 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001034 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001035 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001036 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001037 char **env;
1038 extern char **environ;
1039 struct stat st;
1040 char *argv[10];
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001041 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
Damien Miller91606b12000-06-28 08:22:29 +10001042#ifdef WITH_IRIX_PROJECT
1043 prid_t projid;
1044#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001045#ifdef WITH_IRIX_JOBS
1046 jid_t jid = 0;
1047#else
1048#ifdef WITH_IRIX_ARRAY
1049 int jid = 0;
1050#endif /* WITH_IRIX_ARRAY */
1051#endif /* WITH_IRIX_JOBS */
1052
Damien Millerd3a18572000-06-07 19:55:44 +10001053 /* login(1) is only called if we execute the login shell */
1054 if (options.use_login && command != NULL)
1055 options.use_login = 0;
1056
Damien Miller364a9bd2001-04-16 18:37:05 +10001057#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001058 if (!options.use_login) {
1059# ifdef HAVE_LOGIN_CAP
1060 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1061 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1062 _PATH_NOLOGIN), "r");
1063# else /* HAVE_LOGIN_CAP */
1064 if (pw->pw_uid)
1065 f = fopen(_PATH_NOLOGIN, "r");
1066# endif /* HAVE_LOGIN_CAP */
1067 if (f) {
1068 /* /etc/nologin exists. Print its contents and exit. */
1069 while (fgets(buf, sizeof(buf), f))
1070 fputs(buf, stderr);
1071 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001072 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001073 }
Damien Millerb38eff82000-04-01 11:09:21 +10001074 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001075#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001076
Damien Millerad833b32000-08-23 10:46:23 +10001077 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001078 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1079 switch, so we let login(1) to this for us. */
1080 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001081#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001082 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001083 if (!check_quietlogin(s, command))
1084 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001085#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001086#ifdef HAVE_CYGWIN
1087 if (is_winnt) {
1088#else
Damien Millerb38eff82000-04-01 11:09:21 +10001089 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001090#endif
Damien Millerad833b32000-08-23 10:46:23 +10001091# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001092 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001093# endif /* HAVE_GETUSERATTR */
1094# ifdef HAVE_LOGIN_CAP
1095 if (setusercontext(lc, pw, pw->pw_uid,
1096 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1097 perror("unable to set user context");
1098 exit(1);
1099 }
Damien Miller60396b02001-02-18 17:01:00 +11001100#ifdef BSD_AUTH
1101 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1102 error("approval failure for %s", pw->pw_name);
1103 fprintf(stderr, "Approval failure");
1104 exit(1);
1105 }
1106#endif
Damien Millerad833b32000-08-23 10:46:23 +10001107# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001108#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1109 /* Sets login uid for accounting */
1110 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1111 error("setluid: %s", strerror(errno));
1112#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1113
Damien Millerad833b32000-08-23 10:46:23 +10001114 if (setlogin(pw->pw_name) < 0)
1115 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001116 if (setgid(pw->pw_gid) < 0) {
1117 perror("setgid");
1118 exit(1);
1119 }
1120 /* Initialize the group list. */
1121 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1122 perror("initgroups");
1123 exit(1);
1124 }
1125 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001126# ifdef USE_PAM
1127 /*
1128 * PAM credentials may take the form of
1129 * supplementary groups. These will have been
1130 * wiped by the above initgroups() call.
1131 * Reestablish them here.
1132 */
1133 do_pam_setcred(0);
1134# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001135# ifdef WITH_IRIX_JOBS
1136 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1137 if (jid == -1) {
1138 fatal("Failed to create job container: %.100s",
1139 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001140 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001141# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001142# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001143 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001144 if (jid == 0) {
1145 if (newarraysess() != 0)
1146 fatal("Failed to set up new array session: %.100s",
1147 strerror(errno));
1148 }
Damien Millerad833b32000-08-23 10:46:23 +10001149# endif /* WITH_IRIX_ARRAY */
1150# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001151 /* initialize irix project info */
1152 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1153 debug("Failed to get project id, using projid 0");
1154 projid = 0;
1155 }
Damien Miller91606b12000-06-28 08:22:29 +10001156 if (setprid(projid))
1157 fatal("Failed to initialize project %d for %s: %.100s",
1158 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001159# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001160#ifdef WITH_IRIX_AUDIT
1161 if (sysconf(_SC_AUDIT)) {
1162 debug("Setting sat id to %d", (int) pw->pw_uid);
1163 if (satsetid(pw->pw_uid))
1164 debug("error setting satid: %.100s", strerror(errno));
1165 }
1166#endif /* WITH_IRIX_AUDIT */
1167
Damien Miller168a7002001-03-17 10:29:50 +11001168#ifdef _AIX
1169 /*
1170 * AIX has a "usrinfo" area where logname and
1171 * other stuff is stored - a few applications
1172 * actually use this and die if it's not set
1173 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001174 if (s->ttyfd == -1)
1175 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001176 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001177 2 * strlen(pw->pw_name));
1178 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001179 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001180 if (usrinfo(SETUINFO, cp, i) == -1)
1181 fatal("Couldn't set usrinfo: %s",
1182 strerror(errno));
1183 debug3("AIX/UsrInfo: set len %d", i);
1184 xfree(cp);
1185#endif
1186
Damien Millerb38eff82000-04-01 11:09:21 +10001187 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001188 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001189# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001190 }
Damien Millerad833b32000-08-23 10:46:23 +10001191#endif /* HAVE_OSF_SIA */
1192
Damien Millerbac2d8a2000-09-05 16:13:06 +11001193#ifdef HAVE_CYGWIN
1194 if (is_winnt)
1195#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001196 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001197 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001198 }
1199 /*
1200 * Get the shell from the password data. An empty shell field is
1201 * legal, and means /bin/sh.
1202 */
1203 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001204#ifdef HAVE_LOGIN_CAP
1205 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1206#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001207
1208#ifdef AFS
1209 /* Try to get AFS tokens for the local cell. */
1210 if (k_hasafs()) {
1211 char cell[64];
1212
1213 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1214 krb_afslog(cell, 0);
1215
1216 krb_afslog(0, 0);
1217 }
1218#endif /* AFS */
1219
1220 /* Initialize the environment. */
1221 envsize = 100;
1222 env = xmalloc(envsize * sizeof(char *));
1223 env[0] = NULL;
1224
Damien Millerbac2d8a2000-09-05 16:13:06 +11001225#ifdef HAVE_CYGWIN
1226 /*
1227 * The Windows environment contains some setting which are
1228 * important for a running system. They must not be dropped.
1229 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001230 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001231#endif
1232
Damien Millerb38eff82000-04-01 11:09:21 +10001233 if (!options.use_login) {
1234 /* Set basic environment. */
1235 child_set_env(&env, &envsize, "USER", pw->pw_name);
1236 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1237 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001238#ifdef HAVE_LOGIN_CAP
1239 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1240 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001241#else /* HAVE_LOGIN_CAP */
1242# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001243 /*
1244 * There's no standard path on Windows. The path contains
1245 * important components pointing to the system directories,
1246 * needed for loading shared libraries. So the path better
1247 * remains intact here.
1248 */
Damien Millerb38eff82000-04-01 11:09:21 +10001249 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001250# endif /* HAVE_CYGWIN */
1251#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001252
1253 snprintf(buf, sizeof buf, "%.200s/%.50s",
1254 _PATH_MAILDIR, pw->pw_name);
1255 child_set_env(&env, &envsize, "MAIL", buf);
1256
1257 /* Normal systems set SHELL by default. */
1258 child_set_env(&env, &envsize, "SHELL", shell);
1259 }
1260 if (getenv("TZ"))
1261 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1262
1263 /* Set custom environment options from RSA authentication. */
1264 while (custom_environment) {
1265 struct envstring *ce = custom_environment;
1266 char *s = ce->s;
1267 int i;
1268 for (i = 0; s[i] != '=' && s[i]; i++);
1269 if (s[i] == '=') {
1270 s[i] = 0;
1271 child_set_env(&env, &envsize, s, s + i + 1);
1272 }
1273 custom_environment = ce->next;
1274 xfree(ce->s);
1275 xfree(ce);
1276 }
1277
1278 snprintf(buf, sizeof buf, "%.50s %d %d",
1279 get_remote_ipaddr(), get_remote_port(), get_local_port());
1280 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1281
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001282 if (s->ttyfd != -1)
1283 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1284 if (s->term)
1285 child_set_env(&env, &envsize, "TERM", s->term);
1286 if (s->display)
1287 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001288 if (original_command)
1289 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1290 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001291
1292#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001293 if ((cp = getenv("AUTHSTATE")) != NULL)
1294 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1295 if ((cp = getenv("KRB5CCNAME")) != NULL)
1296 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1297 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001298#endif
1299
1300#ifdef KRB4
1301 {
1302 extern char *ticket;
1303
1304 if (ticket)
1305 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1306 }
1307#endif /* KRB4 */
1308
1309#ifdef USE_PAM
1310 /* Pull in any environment variables that may have been set by PAM. */
1311 do_pam_environment(&env, &envsize);
1312#endif /* USE_PAM */
1313
Damien Millerb38eff82000-04-01 11:09:21 +10001314 if (xauthfile)
1315 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1316 if (auth_get_socket_name() != NULL)
1317 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1318 auth_get_socket_name());
1319
1320 /* read $HOME/.ssh/environment. */
1321 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001322 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1323 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001324 read_environment_file(&env, &envsize, buf);
1325 }
1326 if (debug_flag) {
1327 /* dump the environment */
1328 fprintf(stderr, "Environment:\n");
1329 for (i = 0; env[i]; i++)
1330 fprintf(stderr, " %.200s\n", env[i]);
1331 }
Damien Millerad833b32000-08-23 10:46:23 +10001332 /* we have to stash the hostname before we close our socket. */
1333 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001334 hostname = get_remote_name_or_ip(utmp_len,
1335 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001336 /*
1337 * Close the connection descriptors; note that this is the child, and
1338 * the server will still have the socket open, and it is important
1339 * that we do not shutdown it. Note that the descriptors cannot be
1340 * closed before building the environment, as we call
1341 * get_remote_ipaddr there.
1342 */
1343 if (packet_get_connection_in() == packet_get_connection_out())
1344 close(packet_get_connection_in());
1345 else {
1346 close(packet_get_connection_in());
1347 close(packet_get_connection_out());
1348 }
1349 /*
1350 * Close all descriptors related to channels. They will still remain
1351 * open in the parent.
1352 */
1353 /* XXX better use close-on-exec? -markus */
1354 channel_close_all();
1355
1356 /*
1357 * Close any extra file descriptors. Note that there may still be
1358 * descriptors left by system functions. They will be closed later.
1359 */
1360 endpwent();
1361
1362 /*
1363 * Close any extra open file descriptors so that we don\'t have them
1364 * hanging around in clients. Note that we want to do this after
1365 * initgroups, because at least on Solaris 2.3 it leaves file
1366 * descriptors open.
1367 */
1368 for (i = 3; i < 64; i++)
1369 close(i);
1370
1371 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001372 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001373 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1374 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001375#ifdef HAVE_LOGIN_CAP
1376 if (login_getcapbool(lc, "requirehome", 0))
1377 exit(1);
1378#endif
1379 }
Damien Millerb38eff82000-04-01 11:09:21 +10001380
1381 /*
1382 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1383 * xauth are run in the proper environment.
1384 */
1385 environ = env;
1386
1387 /*
1388 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1389 * in this order).
1390 */
1391 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001392 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001393 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001394 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1395 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001396 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001397 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001398 if (do_xauth)
1399 fprintf(f, "%s %s\n", s->auth_proto,
1400 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001401 pclose(f);
1402 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001403 fprintf(stderr, "Could not run %s\n",
1404 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001405 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001406 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001407 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1408 _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001409
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001410 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001411 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001412 if (do_xauth)
1413 fprintf(f, "%s %s\n", s->auth_proto,
1414 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001415 pclose(f);
1416 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001417 fprintf(stderr, "Could not run %s\n",
1418 _PATH_SSH_SYSTEM_RC);
1419 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001420 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001421 char *screen = strchr(s->display, ':');
1422
1423 if (debug_flag) {
1424 fprintf(stderr,
1425 "Running %.100s add "
1426 "%.100s %.100s %.100s\n",
1427 options.xauth_location, s->display,
1428 s->auth_proto, s->auth_data);
1429 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001430 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001431 "Adding %.*s/unix%s %s %s\n",
1432 (int)(screen - s->display),
1433 s->display, screen,
1434 s->auth_proto, s->auth_data);
1435 }
1436 snprintf(cmd, sizeof cmd, "%s -q -",
1437 options.xauth_location);
1438 f = popen(cmd, "w");
1439 if (f) {
1440 fprintf(f, "add %s %s %s\n", s->display,
1441 s->auth_proto, s->auth_data);
1442 if (screen != NULL)
1443 fprintf(f, "add %.*s/unix%s %s %s\n",
1444 (int)(screen - s->display),
1445 s->display, screen,
1446 s->auth_proto,
1447 s->auth_data);
1448 pclose(f);
1449 } else {
1450 fprintf(stderr, "Could not run %s\n",
1451 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001452 }
1453 }
Damien Millerb38eff82000-04-01 11:09:21 +10001454 /* Get the last component of the shell name. */
1455 cp = strrchr(shell, '/');
1456 if (cp)
1457 cp++;
1458 else
1459 cp = shell;
1460 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001461
1462 /* restore SIGPIPE for child */
1463 signal(SIGPIPE, SIG_DFL);
1464
Damien Millerb38eff82000-04-01 11:09:21 +10001465 /*
1466 * If we have no command, execute the shell. In this case, the shell
1467 * name to be passed in argv[0] is preceded by '-' to indicate that
1468 * this is a login shell.
1469 */
1470 if (!command) {
1471 if (!options.use_login) {
1472 char buf[256];
1473
1474 /*
1475 * Check for mail if we have a tty and it was enabled
1476 * in server options.
1477 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001478 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001479 char *mailbox;
1480 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001481
Damien Millerb38eff82000-04-01 11:09:21 +10001482 mailbox = getenv("MAIL");
1483 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001484 if (stat(mailbox, &mailstat) != 0 ||
1485 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001486 printf("No mail.\n");
1487 else if (mailstat.st_mtime < mailstat.st_atime)
1488 printf("You have mail.\n");
1489 else
1490 printf("You have new mail.\n");
1491 }
1492 }
1493 /* Start the shell. Set initial character to '-'. */
1494 buf[0] = '-';
1495 strncpy(buf + 1, cp, sizeof(buf) - 1);
1496 buf[sizeof(buf) - 1] = 0;
1497
1498 /* Execute the shell. */
1499 argv[0] = buf;
1500 argv[1] = NULL;
1501 execve(shell, argv, env);
1502
1503 /* Executing the shell failed. */
1504 perror(shell);
1505 exit(1);
1506
1507 } else {
1508 /* Launch login(1). */
1509
Damien Millerad833b32000-08-23 10:46:23 +10001510 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001511 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001512
1513 /* Login couldn't be executed, die. */
1514
1515 perror("login");
1516 exit(1);
1517 }
1518 }
1519 /*
1520 * Execute the command using the user's shell. This uses the -c
1521 * option to execute the command.
1522 */
1523 argv[0] = (char *) cp;
1524 argv[1] = "-c";
1525 argv[2] = (char *) command;
1526 argv[3] = NULL;
1527 execve(shell, argv, env);
1528 perror(shell);
1529 exit(1);
1530}
1531
1532Session *
1533session_new(void)
1534{
1535 int i;
1536 static int did_init = 0;
1537 if (!did_init) {
1538 debug("session_new: init");
1539 for(i = 0; i < MAX_SESSIONS; i++) {
1540 sessions[i].used = 0;
1541 sessions[i].self = i;
1542 }
1543 did_init = 1;
1544 }
1545 for(i = 0; i < MAX_SESSIONS; i++) {
1546 Session *s = &sessions[i];
1547 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001548 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001549 s->chanid = -1;
1550 s->ptyfd = -1;
1551 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001552 s->used = 1;
Damien Miller15b29522000-10-14 12:33:48 +11001553 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001554 return s;
1555 }
1556 }
1557 return NULL;
1558}
1559
1560void
1561session_dump(void)
1562{
1563 int i;
1564 for(i = 0; i < MAX_SESSIONS; i++) {
1565 Session *s = &sessions[i];
1566 debug("dump: used %d session %d %p channel %d pid %d",
1567 s->used,
1568 s->self,
1569 s,
1570 s->chanid,
1571 s->pid);
1572 }
1573}
1574
Damien Millerefb4afe2000-04-12 18:45:05 +10001575int
1576session_open(int chanid)
1577{
1578 Session *s = session_new();
1579 debug("session_open: channel %d", chanid);
1580 if (s == NULL) {
1581 error("no more sessions");
1582 return 0;
1583 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001584 s->pw = auth_get_user();
1585 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001586 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001587 debug("session_open: session %d: link with channel %d", s->self, chanid);
1588 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001589 return 1;
1590}
1591
1592Session *
1593session_by_channel(int id)
1594{
1595 int i;
1596 for(i = 0; i < MAX_SESSIONS; i++) {
1597 Session *s = &sessions[i];
1598 if (s->used && s->chanid == id) {
1599 debug("session_by_channel: session %d channel %d", i, id);
1600 return s;
1601 }
1602 }
1603 debug("session_by_channel: unknown channel %d", id);
1604 session_dump();
1605 return NULL;
1606}
1607
1608Session *
1609session_by_pid(pid_t pid)
1610{
1611 int i;
1612 debug("session_by_pid: pid %d", pid);
1613 for(i = 0; i < MAX_SESSIONS; i++) {
1614 Session *s = &sessions[i];
1615 if (s->used && s->pid == pid)
1616 return s;
1617 }
1618 error("session_by_pid: unknown pid %d", pid);
1619 session_dump();
1620 return NULL;
1621}
1622
1623int
1624session_window_change_req(Session *s)
1625{
1626 s->col = packet_get_int();
1627 s->row = packet_get_int();
1628 s->xpixel = packet_get_int();
1629 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001630 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001631 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1632 return 1;
1633}
1634
1635int
1636session_pty_req(Session *s)
1637{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001638 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001639 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001640
Damien Millerf6d9e222000-06-18 14:50:44 +10001641 if (no_pty_flag)
1642 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001643 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001644 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001645 s->term = packet_get_string(&len);
1646 s->col = packet_get_int();
1647 s->row = packet_get_int();
1648 s->xpixel = packet_get_int();
1649 s->ypixel = packet_get_int();
1650
1651 if (strcmp(s->term, "") == 0) {
1652 xfree(s->term);
1653 s->term = NULL;
1654 }
1655 /* Allocate a pty and open it. */
1656 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1657 xfree(s->term);
1658 s->term = NULL;
1659 s->ptyfd = -1;
1660 s->ttyfd = -1;
1661 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001662 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001663 }
1664 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1665 /*
1666 * Add a cleanup function to clear the utmp entry and record logout
1667 * time in case we call fatal() (e.g., the connection gets closed).
1668 */
1669 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1670 pty_setowner(s->pw, s->tty);
1671 /* Get window size from the packet. */
1672 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1673
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001674 /* Get tty modes from the packet. */
1675 tty_parse_modes(s->ttyfd, &n_bytes);
1676 packet_done();
1677
Damien Millere247cc42000-05-07 12:03:14 +10001678 session_proctitle(s);
1679
Damien Millerefb4afe2000-04-12 18:45:05 +10001680 return 1;
1681}
1682
Damien Millerbd483e72000-04-30 10:00:53 +10001683int
1684session_subsystem_req(Session *s)
1685{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001686 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001687 int success = 0;
1688 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001689 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001690
1691 packet_done();
1692 log("subsystem request for %s", subsys);
1693
Damien Millerf6d9e222000-06-18 14:50:44 +10001694 for (i = 0; i < options.num_subsystems; i++) {
1695 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1696 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001697 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001698 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001699 success = 1;
1700 }
1701 }
1702
1703 if (!success)
1704 log("subsystem request for %s failed, subsystem not found", subsys);
1705
Damien Millerbd483e72000-04-30 10:00:53 +10001706 xfree(subsys);
1707 return success;
1708}
1709
1710int
1711session_x11_req(Session *s)
1712{
Damien Miller7b28dc52000-09-05 13:34:53 +11001713 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001714 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001715 debug("X11 forwarding disabled in user configuration file.");
1716 return 0;
1717 }
Damien Millerbd483e72000-04-30 10:00:53 +10001718 if (!options.x11_forwarding) {
1719 debug("X11 forwarding disabled in server configuration file.");
1720 return 0;
1721 }
1722 if (xauthfile != NULL) {
1723 debug("X11 fwd already started.");
1724 return 0;
1725 }
1726
1727 debug("Received request for X11 forwarding with auth spoofing.");
1728 if (s->display != NULL)
1729 packet_disconnect("Protocol error: X11 display already set.");
1730
1731 s->single_connection = packet_get_char();
1732 s->auth_proto = packet_get_string(NULL);
1733 s->auth_data = packet_get_string(NULL);
1734 s->screen = packet_get_int();
1735 packet_done();
1736
1737 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1738 if (s->display == NULL) {
1739 xfree(s->auth_proto);
1740 xfree(s->auth_data);
1741 return 0;
1742 }
1743 xauthfile = xmalloc(MAXPATHLEN);
1744 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001745 temporarily_use_uid(s->pw);
Damien Millerbd483e72000-04-30 10:00:53 +10001746 if (mkdtemp(xauthfile) == NULL) {
1747 restore_uid();
1748 error("private X11 dir: mkdtemp %s failed: %s",
1749 xauthfile, strerror(errno));
1750 xfree(xauthfile);
1751 xauthfile = NULL;
1752 xfree(s->auth_proto);
1753 xfree(s->auth_data);
1754 /* XXXX remove listening channels */
1755 return 0;
1756 }
1757 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001758 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1759 if (fd >= 0)
1760 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001761 restore_uid();
1762 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1763 return 1;
1764}
1765
Damien Millerf6d9e222000-06-18 14:50:44 +10001766int
1767session_shell_req(Session *s)
1768{
1769 /* if forced_command == NULL, the shell is execed */
1770 char *shell = forced_command;
1771 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001772 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001773 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001774 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001775 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001776 return 1;
1777}
1778
1779int
1780session_exec_req(Session *s)
1781{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001782 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001783 char *command = packet_get_string(&len);
1784 packet_done();
1785 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001786 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001787 command = forced_command;
1788 debug("Forced command '%.500s'", forced_command);
1789 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001790 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001791 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001792 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001793 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001794 if (forced_command == NULL)
1795 xfree(command);
1796 return 1;
1797}
1798
Damien Miller0bc1bd82000-11-13 22:57:25 +11001799int
1800session_auth_agent_req(Session *s)
1801{
1802 static int called = 0;
1803 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001804 if (no_agent_forwarding_flag) {
1805 debug("session_auth_agent_req: no_agent_forwarding_flag");
1806 return 0;
1807 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001808 if (called) {
1809 return 0;
1810 } else {
1811 called = 1;
1812 return auth_input_request_forwarding(s->pw);
1813 }
1814}
1815
Damien Millerefb4afe2000-04-12 18:45:05 +10001816void
1817session_input_channel_req(int id, void *arg)
1818{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001819 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001820 int reply;
1821 int success = 0;
1822 char *rtype;
1823 Session *s;
1824 Channel *c;
1825
1826 rtype = packet_get_string(&len);
1827 reply = packet_get_char();
1828
1829 s = session_by_channel(id);
1830 if (s == NULL)
1831 fatal("session_input_channel_req: channel %d: no session", id);
1832 c = channel_lookup(id);
1833 if (c == NULL)
1834 fatal("session_input_channel_req: channel %d: bad channel", id);
1835
1836 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1837 s->self, id, rtype, reply);
1838
1839 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001840 * a session is in LARVAL state until a shell, a command
1841 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001842 */
1843 if (c->type == SSH_CHANNEL_LARVAL) {
1844 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001845 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001846 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001847 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001848 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001849 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001850 } else if (strcmp(rtype, "x11-req") == 0) {
1851 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001852 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1853 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001854 } else if (strcmp(rtype, "subsystem") == 0) {
1855 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001856 }
1857 }
1858 if (strcmp(rtype, "window-change") == 0) {
1859 success = session_window_change_req(s);
1860 }
1861
1862 if (reply) {
1863 packet_start(success ?
1864 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1865 packet_put_int(c->remote_id);
1866 packet_send();
1867 }
1868 xfree(rtype);
1869}
1870
1871void
1872session_set_fds(Session *s, int fdin, int fdout, int fderr)
1873{
1874 if (!compat20)
1875 fatal("session_set_fds: called for proto != 2.0");
1876 /*
1877 * now that have a child and a pipe to the child,
1878 * we can activate our channel and register the fd's
1879 */
1880 if (s->chanid == -1)
1881 fatal("no channel for session %d", s->self);
1882 channel_set_fds(s->chanid,
1883 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001884 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1885 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001886}
1887
Damien Millerb38eff82000-04-01 11:09:21 +10001888void
1889session_pty_cleanup(Session *s)
1890{
1891 if (s == NULL || s->ttyfd == -1)
1892 return;
1893
Kevin Steves43cdef32001-02-11 14:12:08 +00001894 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001895
1896 /* Cancel the cleanup function. */
1897 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1898
1899 /* Record that the user has logged out. */
1900 record_logout(s->pid, s->tty);
1901
1902 /* Release the pseudo-tty. */
1903 pty_release(s->tty);
1904
1905 /*
1906 * Close the server side of the socket pairs. We must do this after
1907 * the pty cleanup, so that another process doesn't get this pty
1908 * while we're still cleaning up.
1909 */
1910 if (close(s->ptymaster) < 0)
1911 error("close(s->ptymaster): %s", strerror(errno));
1912}
Damien Millerefb4afe2000-04-12 18:45:05 +10001913
1914void
1915session_exit_message(Session *s, int status)
1916{
1917 Channel *c;
1918 if (s == NULL)
1919 fatal("session_close: no session");
1920 c = channel_lookup(s->chanid);
1921 if (c == NULL)
1922 fatal("session_close: session %d: no channel %d",
1923 s->self, s->chanid);
1924 debug("session_exit_message: session %d channel %d pid %d",
1925 s->self, s->chanid, s->pid);
1926
1927 if (WIFEXITED(status)) {
1928 channel_request_start(s->chanid,
1929 "exit-status", 0);
1930 packet_put_int(WEXITSTATUS(status));
1931 packet_send();
1932 } else if (WIFSIGNALED(status)) {
1933 channel_request_start(s->chanid,
1934 "exit-signal", 0);
1935 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001936#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001937 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001938#else /* WCOREDUMP */
1939 packet_put_char(0);
1940#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001941 packet_put_cstring("");
1942 packet_put_cstring("");
1943 packet_send();
1944 } else {
1945 /* Some weird exit cause. Just exit. */
1946 packet_disconnect("wait returned status %04x.", status);
1947 }
1948
1949 /* disconnect channel */
1950 debug("session_exit_message: release channel %d", s->chanid);
1951 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001952 /*
1953 * emulate a write failure with 'chan_write_failed', nobody will be
1954 * interested in data we write.
1955 * Note that we must not call 'chan_read_failed', since there could
1956 * be some more data waiting in the pipe.
1957 */
Damien Millerbd483e72000-04-30 10:00:53 +10001958 if (c->ostate != CHAN_OUTPUT_CLOSED)
1959 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001960 s->chanid = -1;
1961}
1962
1963void
1964session_free(Session *s)
1965{
1966 debug("session_free: session %d pid %d", s->self, s->pid);
1967 if (s->term)
1968 xfree(s->term);
1969 if (s->display)
1970 xfree(s->display);
1971 if (s->auth_data)
1972 xfree(s->auth_data);
1973 if (s->auth_proto)
1974 xfree(s->auth_proto);
1975 s->used = 0;
1976}
1977
1978void
1979session_close(Session *s)
1980{
1981 session_pty_cleanup(s);
1982 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001983 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001984}
1985
1986void
1987session_close_by_pid(pid_t pid, int status)
1988{
1989 Session *s = session_by_pid(pid);
1990 if (s == NULL) {
1991 debug("session_close_by_pid: no session for pid %d", s->pid);
1992 return;
1993 }
1994 if (s->chanid != -1)
1995 session_exit_message(s, status);
1996 session_close(s);
1997}
1998
1999/*
2000 * this is called when a channel dies before
2001 * the session 'child' itself dies
2002 */
2003void
2004session_close_by_channel(int id, void *arg)
2005{
2006 Session *s = session_by_channel(id);
2007 if (s == NULL) {
2008 debug("session_close_by_channel: no session for channel %d", id);
2009 return;
2010 }
2011 /* disconnect channel */
2012 channel_cancel_cleanup(s->chanid);
2013 s->chanid = -1;
2014
2015 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
2016 if (s->pid == 0) {
2017 /* close session immediately */
2018 session_close(s);
2019 } else {
2020 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10002021 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10002022 error("session_close_by_channel: kill %d: %s",
2023 s->pid, strerror(errno));
2024 }
2025}
2026
Damien Millere247cc42000-05-07 12:03:14 +10002027char *
2028session_tty_list(void)
2029{
2030 static char buf[1024];
2031 int i;
2032 buf[0] = '\0';
2033 for(i = 0; i < MAX_SESSIONS; i++) {
2034 Session *s = &sessions[i];
2035 if (s->used && s->ttyfd != -1) {
2036 if (buf[0] != '\0')
2037 strlcat(buf, ",", sizeof buf);
2038 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2039 }
2040 }
2041 if (buf[0] == '\0')
2042 strlcpy(buf, "notty", sizeof buf);
2043 return buf;
2044}
2045
2046void
2047session_proctitle(Session *s)
2048{
2049 if (s->pw == NULL)
2050 error("no user for session %d", s->self);
2051 else
2052 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2053}
2054
Damien Millerefb4afe2000-04-12 18:45:05 +10002055void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002056do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002057{
Ben Lindstromb31783d2001-03-22 02:02:12 +00002058
Damien Millerefb4afe2000-04-12 18:45:05 +10002059 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002060 if (xauthfile)
2061 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002062}