blob: dacb6a099db4d278776feaad665ea5ab6f0c5501 [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.
12 * 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 Miller874d77b2000-10-14 16:23:11 +110036RCSID("$OpenBSD: session.c,v 1.38 2000/10/11 20:27:23 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
38#include "xmalloc.h"
39#include "ssh.h"
40#include "pty.h"
41#include "packet.h"
42#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "mpaux.h"
44#include "servconf.h"
45#include "uidswap.h"
46#include "compat.h"
47#include "channels.h"
48#include "nchan.h"
49
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "bufaux.h"
51#include "ssh2.h"
52#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100053#include "auth-options.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100054
Damien Miller91606b12000-06-28 08:22:29 +100055#ifdef WITH_IRIX_PROJECT
56#include <proj.h>
57#endif /* WITH_IRIX_PROJECT */
58
Damien Miller37023962000-07-11 17:31:38 +100059#if defined(HAVE_USERSEC_H)
60#include <usersec.h>
61#endif
62
Damien Millerb8c656e2000-06-28 15:22:41 +100063#ifdef HAVE_OSF_SIA
64# include <sia.h>
65# include <siad.h>
66#endif
67
Damien Millerbac2d8a2000-09-05 16:13:06 +110068#ifdef HAVE_CYGWIN
69#include <windows.h>
70#include <sys/cygwin.h>
71#define is_winnt (GetVersion() < 0x80000000)
72#endif
73
Damien Millerd17b8d52000-08-09 14:42:28 +100074/* AIX limits */
75#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100076# define S_UFSIZE_HARD S_UFSIZE "_hard"
77# define S_UCPU_HARD S_UCPU "_hard"
78# define S_UDATA_HARD S_UDATA "_hard"
79# define S_USTACK_HARD S_USTACK "_hard"
80# define S_URSS_HARD S_URSS "_hard"
81# define S_UCORE_HARD S_UCORE "_hard"
82# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100083#endif
84
Damien Millerad833b32000-08-23 10:46:23 +100085#ifdef HAVE_LOGIN_CAP
86#include <login_cap.h>
87#endif
88
Damien Millerb38eff82000-04-01 11:09:21 +100089/* types */
90
91#define TTYSZ 64
92typedef struct Session Session;
93struct Session {
94 int used;
95 int self;
Damien Millerbd483e72000-04-30 10:00:53 +100096 int extended;
Damien Millerb38eff82000-04-01 11:09:21 +100097 struct passwd *pw;
98 pid_t pid;
99 /* tty */
100 char *term;
101 int ptyfd, ttyfd, ptymaster;
102 int row, col, xpixel, ypixel;
103 char tty[TTYSZ];
104 /* X11 */
105 char *display;
106 int screen;
107 char *auth_proto;
108 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000109 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000110 /* proto 2 */
111 int chanid;
112};
113
114/* func */
115
116Session *session_new(void);
117void session_set_fds(Session *s, int fdin, int fdout, int fderr);
118void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000119void session_proctitle(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000120void do_exec_pty(Session *s, const char *command, struct passwd * pw);
121void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
Damien Miller942da032000-08-18 13:59:06 +1000122void do_login(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000123
124void
125do_child(const char *command, struct passwd * pw, const char *term,
126 const char *display, const char *auth_proto,
127 const char *auth_data, const char *ttyname);
128
129/* import */
130extern ServerOptions options;
Damien Miller06d84b72000-04-21 16:13:07 +1000131#ifdef HAVE___PROGNAME
Damien Millerb38eff82000-04-01 11:09:21 +1000132extern char *__progname;
Damien Miller06d84b72000-04-21 16:13:07 +1000133#else /* HAVE___PROGNAME */
Damien Miller70fb6712000-05-01 20:59:50 +1000134static const char *__progname = "sshd";
Damien Miller06d84b72000-04-21 16:13:07 +1000135#endif /* HAVE___PROGNAME */
136
Damien Millerb38eff82000-04-01 11:09:21 +1000137extern int log_stderr;
138extern int debug_flag;
Damien Miller942da032000-08-18 13:59:06 +1000139extern unsigned int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000140
Damien Miller37023962000-07-11 17:31:38 +1000141extern int startup_pipe;
142
Damien Millerb38eff82000-04-01 11:09:21 +1000143/* Local Xauthority file. */
144static char *xauthfile;
145
Damien Miller7b28dc52000-09-05 13:34:53 +1100146/* original command from peer. */
147char *original_command = NULL;
148
Damien Millerb38eff82000-04-01 11:09:21 +1000149/* data */
150#define MAX_SESSIONS 10
151Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100152
Damien Millerd2c208a2000-05-17 22:00:02 +1000153#ifdef WITH_AIXAUTHENTICATE
154/* AIX's lastlogin message, set in auth1.c */
155char *aixloginmsg;
156#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000157
Damien Millerad833b32000-08-23 10:46:23 +1000158#ifdef HAVE_LOGIN_CAP
159static login_cap_t *lc;
160#endif
161
Damien Millerb38eff82000-04-01 11:09:21 +1000162/*
163 * Remove local Xauthority file.
164 */
165void
166xauthfile_cleanup_proc(void *ignore)
167{
168 debug("xauthfile_cleanup_proc called");
169
170 if (xauthfile != NULL) {
171 char *p;
172 unlink(xauthfile);
173 p = strrchr(xauthfile, '/');
174 if (p != NULL) {
175 *p = '\0';
176 rmdir(xauthfile);
177 }
178 xfree(xauthfile);
179 xauthfile = NULL;
180 }
181}
182
183/*
184 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
185 * dropped connection).
186 */
Damien Miller4af51302000-04-16 11:18:38 +1000187void
Damien Millerb38eff82000-04-01 11:09:21 +1000188pty_cleanup_proc(void *session)
189{
190 Session *s=session;
191 if (s == NULL)
192 fatal("pty_cleanup_proc: no session");
193 debug("pty_cleanup_proc: %s", s->tty);
194
195 if (s->pid != 0) {
196 /* Record that the user has logged out. */
197 record_logout(s->pid, s->tty);
198 }
199
200 /* Release the pseudo-tty. */
201 pty_release(s->tty);
202}
203
204/*
205 * Prepares for an interactive session. This is called after the user has
206 * been successfully authenticated. During this message exchange, pseudo
207 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
208 * are requested, etc.
209 */
Damien Miller4af51302000-04-16 11:18:38 +1000210void
Damien Millerb38eff82000-04-01 11:09:21 +1000211do_authenticated(struct passwd * pw)
212{
213 Session *s;
Damien Miller7b28dc52000-09-05 13:34:53 +1100214 int type, fd;
Damien Millerb38eff82000-04-01 11:09:21 +1000215 int compression_level = 0, enable_compression_after_reply = 0;
216 int have_pty = 0;
217 char *command;
218 int n_bytes;
219 int plen;
220 unsigned int proto_len, data_len, dlen;
221
222 /*
223 * Cancel the alarm we set to limit the time taken for
224 * authentication.
225 */
226 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000227 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000228 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000229 startup_pipe = -1;
230 }
Damien Millerb38eff82000-04-01 11:09:21 +1000231
232 /*
233 * Inform the channel mechanism that we are the server side and that
234 * the client may request to connect to any port at all. (The user
235 * could do it anyway, and we wouldn\'t know what is permitted except
236 * by the client telling us, so we can equally well trust the client
237 * not to request anything bogus.)
238 */
239 if (!no_port_forwarding_flag)
240 channel_permit_all_opens();
241
242 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000243 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000244
Kevin Steves48b7cc02000-10-07 13:24:00 +0000245#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000246 if ((lc = login_getclass(pw->pw_class)) == NULL) {
247 error("unable to get login class");
248 return;
249 }
250#endif
251
Damien Millerb38eff82000-04-01 11:09:21 +1000252 /*
253 * We stay in this loop until the client requests to execute a shell
254 * or a command.
255 */
256 for (;;) {
257 int success = 0;
258
259 /* Get a packet from the client. */
260 type = packet_read(&plen);
261
262 /* Process the packet. */
263 switch (type) {
264 case SSH_CMSG_REQUEST_COMPRESSION:
265 packet_integrity_check(plen, 4, type);
266 compression_level = packet_get_int();
267 if (compression_level < 1 || compression_level > 9) {
268 packet_send_debug("Received illegal compression level %d.",
269 compression_level);
270 break;
271 }
272 /* Enable compression after we have responded with SUCCESS. */
273 enable_compression_after_reply = 1;
274 success = 1;
275 break;
276
277 case SSH_CMSG_REQUEST_PTY:
278 if (no_pty_flag) {
279 debug("Allocating a pty not permitted for this authentication.");
280 break;
281 }
282 if (have_pty)
283 packet_disconnect("Protocol error: you already have a pty.");
284
285 debug("Allocating pty.");
286
287 /* Allocate a pty and open it. */
288 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
289 sizeof(s->tty))) {
290 error("Failed to allocate pty.");
291 break;
292 }
293 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
294 pty_setowner(pw, s->tty);
295
296 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
297 s->term = packet_get_string(&dlen);
298 packet_integrity_check(dlen, strlen(s->term), type);
299 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
300 /* Remaining bytes */
301 n_bytes = plen - (4 + dlen + 4 * 4);
302
303 if (strcmp(s->term, "") == 0) {
304 xfree(s->term);
305 s->term = NULL;
306 }
307 /* Get window size from the packet. */
308 s->row = packet_get_int();
309 s->col = packet_get_int();
310 s->xpixel = packet_get_int();
311 s->ypixel = packet_get_int();
312 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
313
314 /* Get tty modes from the packet. */
315 tty_parse_modes(s->ttyfd, &n_bytes);
316 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
317
Damien Millere247cc42000-05-07 12:03:14 +1000318 session_proctitle(s);
319
Damien Millerb38eff82000-04-01 11:09:21 +1000320 /* Indicate that we now have a pty. */
321 success = 1;
322 have_pty = 1;
323 break;
324
325 case SSH_CMSG_X11_REQUEST_FORWARDING:
326 if (!options.x11_forwarding) {
327 packet_send_debug("X11 forwarding disabled in server configuration file.");
328 break;
329 }
Damien Miller0c043c12000-06-07 21:22:38 +1000330 if (!options.xauth_location) {
331 packet_send_debug("No xauth program; cannot forward with spoofing.");
332 break;
333 }
Damien Millerb38eff82000-04-01 11:09:21 +1000334 if (no_x11_forwarding_flag) {
335 packet_send_debug("X11 forwarding not permitted for this authentication.");
336 break;
337 }
338 debug("Received request for X11 forwarding with auth spoofing.");
339 if (s->display != NULL)
340 packet_disconnect("Protocol error: X11 display already set.");
341
342 s->auth_proto = packet_get_string(&proto_len);
343 s->auth_data = packet_get_string(&data_len);
344 packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
345
346 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
347 s->screen = packet_get_int();
348 else
349 s->screen = 0;
350 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
351
352 if (s->display == NULL)
353 break;
354
355 /* Setup to always have a local .Xauthority. */
356 xauthfile = xmalloc(MAXPATHLEN);
357 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
358 temporarily_use_uid(pw->pw_uid);
359 if (mkdtemp(xauthfile) == NULL) {
360 restore_uid();
361 error("private X11 dir: mkdtemp %s failed: %s",
362 xauthfile, strerror(errno));
363 xfree(xauthfile);
364 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000365 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000366 break;
367 }
368 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100369 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
370 if (fd >= 0)
371 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000372 restore_uid();
373 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
374 success = 1;
375 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000376
377 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
378 if (no_agent_forwarding_flag || compat13) {
379 debug("Authentication agent forwarding not permitted for this authentication.");
380 break;
381 }
382 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000383 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000384 break;
385
386 case SSH_CMSG_PORT_FORWARD_REQUEST:
387 if (no_port_forwarding_flag) {
388 debug("Port forwarding not permitted for this authentication.");
389 break;
390 }
391 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000392 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000393 success = 1;
394 break;
395
396 case SSH_CMSG_MAX_PACKET_SIZE:
397 if (packet_set_maxsize(packet_get_int()) > 0)
398 success = 1;
399 break;
400
401 case SSH_CMSG_EXEC_SHELL:
402 case SSH_CMSG_EXEC_CMD:
403 /* Set interactive/non-interactive mode. */
404 packet_set_interactive(have_pty || s->display != NULL,
405 options.keepalives);
406
407 if (type == SSH_CMSG_EXEC_CMD) {
408 command = packet_get_string(&dlen);
409 debug("Exec command '%.500s'", command);
410 packet_integrity_check(plen, 4 + dlen, type);
411 } else {
412 command = NULL;
413 packet_integrity_check(plen, 0, type);
414 }
415 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100416 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000417 command = forced_command;
418 debug("Forced command '%.500s'", forced_command);
419 }
420 if (have_pty)
421 do_exec_pty(s, command, pw);
422 else
423 do_exec_no_pty(s, command, pw);
424
425 if (command != NULL)
426 xfree(command);
427 /* Cleanup user's local Xauthority file. */
428 if (xauthfile)
429 xauthfile_cleanup_proc(NULL);
430 return;
431
432 default:
433 /*
434 * Any unknown messages in this phase are ignored,
435 * and a failure message is returned.
436 */
437 log("Unknown packet type received after authentication: %d", type);
438 }
439 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
440 packet_send();
441 packet_write_wait();
442
443 /* Enable compression now that we have replied if appropriate. */
444 if (enable_compression_after_reply) {
445 enable_compression_after_reply = 0;
446 packet_start_compression(compression_level);
447 }
448 }
449}
450
451/*
452 * This is called to fork and execute a command when we have no tty. This
453 * will call do_child from the child, and server_loop from the parent after
454 * setting up file descriptors and such.
455 */
Damien Miller4af51302000-04-16 11:18:38 +1000456void
Damien Millerb38eff82000-04-01 11:09:21 +1000457do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
458{
459 int pid;
460
461#ifdef USE_PIPES
462 int pin[2], pout[2], perr[2];
463 /* Allocate pipes for communicating with the program. */
464 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
465 packet_disconnect("Could not create pipes: %.100s",
466 strerror(errno));
467#else /* USE_PIPES */
468 int inout[2], err[2];
469 /* Uses socket pairs to communicate with the program. */
470 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
471 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
472 packet_disconnect("Could not create socket pairs: %.100s",
473 strerror(errno));
474#endif /* USE_PIPES */
475 if (s == NULL)
476 fatal("do_exec_no_pty: no session");
477
Damien Millercf3888d2000-09-30 14:17:52 +1100478 signal(SIGPIPE, SIG_DFL);
479
Damien Millere247cc42000-05-07 12:03:14 +1000480 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000481
482#ifdef USE_PAM
483 do_pam_setcred();
484#endif /* USE_PAM */
485
486 /* Fork the child. */
487 if ((pid = fork()) == 0) {
488 /* Child. Reinitialize the log since the pid has changed. */
489 log_init(__progname, options.log_level, options.log_facility, log_stderr);
490
491 /*
492 * Create a new session and process group since the 4.4BSD
493 * setlogin() affects the entire process group.
494 */
495 if (setsid() < 0)
496 error("setsid failed: %.100s", strerror(errno));
497
498#ifdef USE_PIPES
499 /*
500 * Redirect stdin. We close the parent side of the socket
501 * pair, and make the child side the standard input.
502 */
503 close(pin[1]);
504 if (dup2(pin[0], 0) < 0)
505 perror("dup2 stdin");
506 close(pin[0]);
507
508 /* Redirect stdout. */
509 close(pout[0]);
510 if (dup2(pout[1], 1) < 0)
511 perror("dup2 stdout");
512 close(pout[1]);
513
514 /* Redirect stderr. */
515 close(perr[0]);
516 if (dup2(perr[1], 2) < 0)
517 perror("dup2 stderr");
518 close(perr[1]);
519#else /* USE_PIPES */
520 /*
521 * Redirect stdin, stdout, and stderr. Stdin and stdout will
522 * use the same socket, as some programs (particularly rdist)
523 * seem to depend on it.
524 */
525 close(inout[1]);
526 close(err[1]);
527 if (dup2(inout[0], 0) < 0) /* stdin */
528 perror("dup2 stdin");
529 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
530 perror("dup2 stdout");
531 if (dup2(err[0], 2) < 0) /* stderr */
532 perror("dup2 stderr");
533#endif /* USE_PIPES */
534
535 /* Do processing for the child (exec command etc). */
536 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
537 /* NOTREACHED */
538 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100539#ifdef HAVE_CYGWIN
540 if (is_winnt)
541 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
542#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000543 if (pid < 0)
544 packet_disconnect("fork failed: %.100s", strerror(errno));
545 s->pid = pid;
546#ifdef USE_PIPES
547 /* We are the parent. Close the child sides of the pipes. */
548 close(pin[0]);
549 close(pout[1]);
550 close(perr[1]);
551
Damien Millerefb4afe2000-04-12 18:45:05 +1000552 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000553 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000554 } else {
555 /* Enter the interactive session. */
556 server_loop(pid, pin[1], pout[0], perr[0]);
557 /* server_loop has closed pin[1], pout[1], and perr[1]. */
558 }
Damien Millerb38eff82000-04-01 11:09:21 +1000559#else /* USE_PIPES */
560 /* We are the parent. Close the child sides of the socket pairs. */
561 close(inout[0]);
562 close(err[0]);
563
564 /*
565 * Enter the interactive session. Note: server_loop must be able to
566 * handle the case that fdin and fdout are the same.
567 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000568 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000569 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000570 } else {
571 server_loop(pid, inout[1], inout[1], err[1]);
572 /* server_loop has closed inout[1] and err[1]. */
573 }
Damien Millerb38eff82000-04-01 11:09:21 +1000574#endif /* USE_PIPES */
575}
576
577/*
578 * This is called to fork and execute a command when we have a tty. This
579 * will call do_child from the child, and server_loop from the parent after
580 * setting up file descriptors, controlling tty, updating wtmp, utmp,
581 * lastlog, and other such operations.
582 */
Damien Miller4af51302000-04-16 11:18:38 +1000583void
Damien Millerb38eff82000-04-01 11:09:21 +1000584do_exec_pty(Session *s, const char *command, struct passwd * pw)
585{
Damien Millerb38eff82000-04-01 11:09:21 +1000586 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000587 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000588
589 if (s == NULL)
590 fatal("do_exec_pty: no session");
591 ptyfd = s->ptyfd;
592 ttyfd = s->ttyfd;
593
Damien Millerb38eff82000-04-01 11:09:21 +1000594#ifdef USE_PAM
595 do_pam_session(pw->pw_name, s->tty);
596 do_pam_setcred();
597#endif /* USE_PAM */
598
599 /* Fork the child. */
600 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000601 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000602 log_init(__progname, options.log_level, options.log_facility, log_stderr);
603
604 /* Close the master side of the pseudo tty. */
605 close(ptyfd);
606
607 /* Make the pseudo tty our controlling tty. */
608 pty_make_controlling_tty(&ttyfd, s->tty);
609
610 /* Redirect stdin from the pseudo tty. */
611 if (dup2(ttyfd, fileno(stdin)) < 0)
612 error("dup2 stdin failed: %.100s", strerror(errno));
613
614 /* Redirect stdout to the pseudo tty. */
615 if (dup2(ttyfd, fileno(stdout)) < 0)
616 error("dup2 stdin failed: %.100s", strerror(errno));
617
618 /* Redirect stderr to the pseudo tty. */
619 if (dup2(ttyfd, fileno(stderr)) < 0)
620 error("dup2 stdin failed: %.100s", strerror(errno));
621
622 /* Close the extra descriptor for the pseudo tty. */
623 close(ttyfd);
624
Damien Miller942da032000-08-18 13:59:06 +1000625 /* record login, etc. similar to login(1) */
626 if (command == NULL && !options.use_login)
627 do_login(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000628
Damien Millerb38eff82000-04-01 11:09:21 +1000629 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000630 do_child(command, pw, s->term, s->display, s->auth_proto,
631 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000632 /* NOTREACHED */
633 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100634#ifdef HAVE_CYGWIN
635 if (is_winnt)
636 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
637#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000638 if (pid < 0)
639 packet_disconnect("fork failed: %.100s", strerror(errno));
640 s->pid = pid;
641
642 /* Parent. Close the slave side of the pseudo tty. */
643 close(ttyfd);
644
645 /*
646 * Create another descriptor of the pty master side for use as the
647 * standard input. We could use the original descriptor, but this
648 * simplifies code in server_loop. The descriptor is bidirectional.
649 */
650 fdout = dup(ptyfd);
651 if (fdout < 0)
652 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
653
654 /* we keep a reference to the pty master */
655 ptymaster = dup(ptyfd);
656 if (ptymaster < 0)
657 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
658 s->ptymaster = ptymaster;
659
660 /* Enter interactive session. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000661 if (compat20) {
662 session_set_fds(s, ptyfd, fdout, -1);
663 } else {
664 server_loop(pid, ptyfd, fdout, -1);
665 /* server_loop _has_ closed ptyfd and fdout. */
666 session_pty_cleanup(s);
667 }
Damien Millerb38eff82000-04-01 11:09:21 +1000668}
669
Damien Miller942da032000-08-18 13:59:06 +1000670const char *
671get_remote_name_or_ip(void)
672{
673 static const char *remote = "";
674 if (utmp_len > 0)
675 remote = get_canonical_hostname();
676 if (utmp_len == 0 || strlen(remote) > utmp_len)
677 remote = get_remote_ipaddr();
678 return remote;
679}
680
681/* administrative, login(1)-like work */
682void
683do_login(Session *s)
684{
685 FILE *f;
686 char *time_string;
687 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100688 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000689 socklen_t fromlen;
690 struct sockaddr_storage from;
691 struct stat st;
692 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. */
711 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
712 hostname, sizeof(hostname));
713
Damien Millere4340be2000-09-16 13:29:08 +1100714 /* Get the time and hostname when the user last logged in. */
715 hostname[0] = '\0';
716 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
717 hostname, sizeof(hostname));
718
Damien Miller942da032000-08-18 13:59:06 +1000719 /* Record that there was a login on that tty from the remote host. */
720 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
721 get_remote_name_or_ip(), (struct sockaddr *)&from);
722
723 /* Done if .hushlogin exists. */
724 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000725#ifdef HAVE_LOGIN_CAP
726 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
727#else
Damien Miller942da032000-08-18 13:59:06 +1000728 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000729#endif
Damien Miller942da032000-08-18 13:59:06 +1000730 return;
731
732#ifdef USE_PAM
733 print_pam_messages();
Damien Miller9d5705a2000-09-16 16:09:27 +1100734 /* If password change is needed, do it now. */
735 do_pam_chauthtok();
Damien Miller942da032000-08-18 13:59:06 +1000736#endif /* USE_PAM */
737#ifdef WITH_AIXAUTHENTICATE
738 if (aixloginmsg && *aixloginmsg)
739 printf("%s\n", aixloginmsg);
740#endif /* WITH_AIXAUTHENTICATE */
741
Damien Miller942da032000-08-18 13:59:06 +1000742 if (last_login_time != 0) {
743 time_string = ctime(&last_login_time);
744 if (strchr(time_string, '\n'))
745 *strchr(time_string, '\n') = 0;
746 if (strcmp(buf, "") == 0)
747 printf("Last login: %s\r\n", time_string);
748 else
Damien Millere4340be2000-09-16 13:29:08 +1100749 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000750 }
751 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000752#ifdef HAVE_LOGIN_CAP
753 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
754 "/etc/motd"), "r");
755#else
Damien Miller942da032000-08-18 13:59:06 +1000756 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000757#endif
Damien Miller942da032000-08-18 13:59:06 +1000758 if (f) {
759 while (fgets(buf, sizeof(buf), f))
760 fputs(buf, stdout);
761 fclose(f);
762 }
763 }
764}
765
Damien Millerb38eff82000-04-01 11:09:21 +1000766/*
767 * Sets the value of the given variable in the environment. If the variable
768 * already exists, its value is overriden.
769 */
Damien Miller4af51302000-04-16 11:18:38 +1000770void
Damien Millerb38eff82000-04-01 11:09:21 +1000771child_set_env(char ***envp, unsigned int *envsizep, const char *name,
772 const char *value)
773{
774 unsigned int i, namelen;
775 char **env;
776
777 /*
778 * Find the slot where the value should be stored. If the variable
779 * already exists, we reuse the slot; otherwise we append a new slot
780 * at the end of the array, expanding if necessary.
781 */
782 env = *envp;
783 namelen = strlen(name);
784 for (i = 0; env[i]; i++)
785 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
786 break;
787 if (env[i]) {
788 /* Reuse the slot. */
789 xfree(env[i]);
790 } else {
791 /* New variable. Expand if necessary. */
792 if (i >= (*envsizep) - 1) {
793 (*envsizep) += 50;
794 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
795 }
796 /* Need to set the NULL pointer at end of array beyond the new slot. */
797 env[i + 1] = NULL;
798 }
799
800 /* Allocate space and format the variable in the appropriate slot. */
801 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
802 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
803}
804
805/*
806 * Reads environment variables from the given file and adds/overrides them
807 * into the environment. If the file does not exist, this does nothing.
808 * Otherwise, it must consist of empty lines, comments (line starts with '#')
809 * and assignments of the form name=value. No other forms are allowed.
810 */
Damien Miller4af51302000-04-16 11:18:38 +1000811void
Damien Millerb38eff82000-04-01 11:09:21 +1000812read_environment_file(char ***env, unsigned int *envsize,
813 const char *filename)
814{
815 FILE *f;
816 char buf[4096];
817 char *cp, *value;
818
819 f = fopen(filename, "r");
820 if (!f)
821 return;
822
823 while (fgets(buf, sizeof(buf), f)) {
824 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
825 ;
826 if (!*cp || *cp == '#' || *cp == '\n')
827 continue;
828 if (strchr(cp, '\n'))
829 *strchr(cp, '\n') = '\0';
830 value = strchr(cp, '=');
831 if (value == NULL) {
832 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
833 continue;
834 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000835 /*
836 * Replace the equals sign by nul, and advance value to
837 * the value string.
838 */
Damien Millerb38eff82000-04-01 11:09:21 +1000839 *value = '\0';
840 value++;
841 child_set_env(env, envsize, cp, value);
842 }
843 fclose(f);
844}
845
846#ifdef USE_PAM
847/*
848 * Sets any environment variables which have been specified by PAM
849 */
850void do_pam_environment(char ***env, int *envsize)
851{
852 char *equals, var_name[512], var_val[512];
853 char **pam_env;
854 int i;
855
856 if ((pam_env = fetch_pam_environment()) == NULL)
857 return;
858
859 for(i = 0; pam_env[i] != NULL; i++) {
860 if ((equals = strstr(pam_env[i], "=")) == NULL)
861 continue;
862
863 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
864 memset(var_name, '\0', sizeof(var_name));
865 memset(var_val, '\0', sizeof(var_val));
866
867 strncpy(var_name, pam_env[i], equals - pam_env[i]);
868 strcpy(var_val, equals + 1);
869
Damien Millercb5e44a2000-09-29 12:12:36 +1100870 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000871
872 child_set_env(env, envsize, var_name, var_val);
873 }
874 }
875}
876#endif /* USE_PAM */
877
Damien Millercb5e44a2000-09-29 12:12:36 +1100878
879#ifdef HAVE_CYGWIN
880void copy_environment(char ***env, int *envsize)
881{
882 char *equals, var_name[512], var_val[512];
883 int i;
884
885 for(i = 0; environ[i] != NULL; i++) {
886 if ((equals = strstr(environ[i], "=")) == NULL)
887 continue;
888
889 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
890 memset(var_name, '\0', sizeof(var_name));
891 memset(var_val, '\0', sizeof(var_val));
892
893 strncpy(var_name, environ[i], equals - environ[i]);
894 strcpy(var_val, equals + 1);
895
896 debug3("Copy environment: %s=%s", var_name, var_val);
897
898 child_set_env(env, envsize, var_name, var_val);
899 }
900 }
901}
902#endif
903
Damien Miller5fc85652000-07-09 23:53:07 +1000904#if defined(HAVE_GETUSERATTR)
905/*
906 * AIX-specific login initialisation
907 */
908void set_limit(char *user, char *soft, char *hard, int resource, int mult)
909{
910 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000911 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000912
913 getrlimit(resource, &rlim);
914
Damien Miller65964d62000-07-11 09:16:22 +1000915 slim = 0;
916 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
917 if (slim < 0) {
918 rlim.rlim_cur = RLIM_INFINITY;
919 } else if (slim != 0) {
920 /* See the wackiness below */
921 if (rlim.rlim_cur == slim * mult)
922 slim = 0;
923 else
924 rlim.rlim_cur = slim * mult;
925 }
926 }
Damien Miller5fc85652000-07-09 23:53:07 +1000927
Damien Miller65964d62000-07-11 09:16:22 +1000928 hlim = 0;
929 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
930 if (hlim < 0) {
931 rlim.rlim_max = RLIM_INFINITY;
932 } else if (hlim != 0) {
933 rlim.rlim_max = hlim * mult;
934 }
935 }
Damien Miller5fc85652000-07-09 23:53:07 +1000936
Damien Miller65964d62000-07-11 09:16:22 +1000937 /*
938 * XXX For cpu and fsize the soft limit is set to the hard limit
939 * if the hard limit is left at its default value and the soft limit
940 * is changed from its default value, either by requesting it
941 * (slim == 0) or by setting it to the current default. At least
942 * that's how rlogind does it. If you're confused you're not alone.
943 * Bug or feature? AIX 4.3.1.2
944 */
945 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
946 && hlim == 0 && slim != 0)
947 rlim.rlim_max = rlim.rlim_cur;
948 /* A specified hard limit limits the soft limit */
949 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
950 rlim.rlim_cur = rlim.rlim_max;
951 /* A soft limit can increase a hard limit */
952 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000953 rlim.rlim_max = rlim.rlim_cur;
954
955 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000956 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000957}
958
959void set_limits_from_userattr(char *user)
960{
961 int mask;
962 char buf[16];
963
964 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
965 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
966 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
967 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
968 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
969 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
970#if defined(S_UNOFILE)
971 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
972#endif
973
974 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
975 /* Convert decimal to octal */
976 (void) snprintf(buf, sizeof(buf), "%d", mask);
977 if (sscanf(buf, "%o", &mask) == 1)
978 umask(mask);
979 }
980}
981#endif /* defined(HAVE_GETUSERATTR) */
982
Damien Millerb38eff82000-04-01 11:09:21 +1000983/*
984 * Performs common processing for the child, such as setting up the
985 * environment, closing extra file descriptors, setting the user and group
986 * ids, and executing the command or shell.
987 */
Damien Miller4af51302000-04-16 11:18:38 +1000988void
Damien Millerb38eff82000-04-01 11:09:21 +1000989do_child(const char *command, struct passwd * pw, const char *term,
990 const char *display, const char *auth_proto,
991 const char *auth_data, const char *ttyname)
992{
Damien Miller7b28dc52000-09-05 13:34:53 +1100993 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000994 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +1000995 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +1000996 FILE *f = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000997 unsigned int envsize, i;
998 char **env;
999 extern char **environ;
1000 struct stat st;
1001 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001002#ifdef WITH_IRIX_PROJECT
1003 prid_t projid;
1004#endif /* WITH_IRIX_PROJECT */
Damien Millerb38eff82000-04-01 11:09:21 +10001005
Damien Millerd3a18572000-06-07 19:55:44 +10001006 /* login(1) is only called if we execute the login shell */
1007 if (options.use_login && command != NULL)
1008 options.use_login = 0;
1009
Damien Millerb38eff82000-04-01 11:09:21 +10001010#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001011 if (!options.use_login) {
1012# ifdef HAVE_LOGIN_CAP
1013 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1014 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1015 _PATH_NOLOGIN), "r");
1016# else /* HAVE_LOGIN_CAP */
1017 if (pw->pw_uid)
1018 f = fopen(_PATH_NOLOGIN, "r");
1019# endif /* HAVE_LOGIN_CAP */
1020 if (f) {
1021 /* /etc/nologin exists. Print its contents and exit. */
1022 while (fgets(buf, sizeof(buf), f))
1023 fputs(buf, stderr);
1024 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001025 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001026 }
Damien Millerb38eff82000-04-01 11:09:21 +10001027 }
1028#endif /* USE_PAM */
1029
Damien Millerad833b32000-08-23 10:46:23 +10001030 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001031 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1032 switch, so we let login(1) to this for us. */
1033 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001034#ifdef HAVE_OSF_SIA
1035 extern char **saved_argv;
1036 extern int saved_argc;
1037 char *host = get_canonical_hostname ();
1038
1039 if (sia_become_user(NULL, saved_argc, saved_argv, host,
1040 pw->pw_name, ttyname, 0, NULL, NULL, SIA_BEU_SETLUID) !=
1041 SIASUCCESS) {
1042 perror("sia_become_user");
1043 exit(1);
1044 }
1045 if (setreuid(geteuid(), geteuid()) < 0) {
1046 perror("setreuid");
1047 exit(1);
1048 }
1049#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001050#ifdef HAVE_CYGWIN
1051 if (is_winnt) {
1052#else
Damien Millerb38eff82000-04-01 11:09:21 +10001053 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001054#endif
Damien Millerad833b32000-08-23 10:46:23 +10001055# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001056 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001057# endif /* HAVE_GETUSERATTR */
1058# ifdef HAVE_LOGIN_CAP
1059 if (setusercontext(lc, pw, pw->pw_uid,
1060 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1061 perror("unable to set user context");
1062 exit(1);
1063 }
1064# else /* HAVE_LOGIN_CAP */
1065 if (setlogin(pw->pw_name) < 0)
1066 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001067 if (setgid(pw->pw_gid) < 0) {
1068 perror("setgid");
1069 exit(1);
1070 }
1071 /* Initialize the group list. */
1072 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1073 perror("initgroups");
1074 exit(1);
1075 }
1076 endgrent();
Damien Millerad833b32000-08-23 10:46:23 +10001077# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001078 /* initialize array session */
1079 if (newarraysess() != 0)
1080 fatal("Failed to set up new array session: %.100s",
1081 strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001082# endif /* WITH_IRIX_ARRAY */
1083# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001084 /* initialize irix project info */
1085 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1086 debug("Failed to get project id, using projid 0");
1087 projid = 0;
1088 }
Damien Miller91606b12000-06-28 08:22:29 +10001089 if (setprid(projid))
1090 fatal("Failed to initialize project %d for %s: %.100s",
1091 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001092# endif /* WITH_IRIX_PROJECT */
Damien Millerb38eff82000-04-01 11:09:21 +10001093 /* Permanently switch to the desired uid. */
1094 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001095# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001096 }
Damien Millerad833b32000-08-23 10:46:23 +10001097#endif /* HAVE_OSF_SIA */
1098
Damien Millerbac2d8a2000-09-05 16:13:06 +11001099#ifdef HAVE_CYGWIN
1100 if (is_winnt)
1101#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001102 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001103 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001104 }
1105 /*
1106 * Get the shell from the password data. An empty shell field is
1107 * legal, and means /bin/sh.
1108 */
1109 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001110#ifdef HAVE_LOGIN_CAP
1111 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1112#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001113
1114#ifdef AFS
1115 /* Try to get AFS tokens for the local cell. */
1116 if (k_hasafs()) {
1117 char cell[64];
1118
1119 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1120 krb_afslog(cell, 0);
1121
1122 krb_afslog(0, 0);
1123 }
1124#endif /* AFS */
1125
1126 /* Initialize the environment. */
1127 envsize = 100;
1128 env = xmalloc(envsize * sizeof(char *));
1129 env[0] = NULL;
1130
Damien Millerbac2d8a2000-09-05 16:13:06 +11001131#ifdef HAVE_CYGWIN
1132 /*
1133 * The Windows environment contains some setting which are
1134 * important for a running system. They must not be dropped.
1135 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001136 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001137#endif
1138
Damien Millerb38eff82000-04-01 11:09:21 +10001139 if (!options.use_login) {
1140 /* Set basic environment. */
1141 child_set_env(&env, &envsize, "USER", pw->pw_name);
1142 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1143 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001144#ifdef HAVE_LOGIN_CAP
1145 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1146 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001147#else /* HAVE_LOGIN_CAP */
1148# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001149 /*
1150 * There's no standard path on Windows. The path contains
1151 * important components pointing to the system directories,
1152 * needed for loading shared libraries. So the path better
1153 * remains intact here.
1154 */
Damien Millerb38eff82000-04-01 11:09:21 +10001155 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001156# endif /* HAVE_CYGWIN */
1157#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001158
1159 snprintf(buf, sizeof buf, "%.200s/%.50s",
1160 _PATH_MAILDIR, pw->pw_name);
1161 child_set_env(&env, &envsize, "MAIL", buf);
1162
1163 /* Normal systems set SHELL by default. */
1164 child_set_env(&env, &envsize, "SHELL", shell);
1165 }
1166 if (getenv("TZ"))
1167 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1168
1169 /* Set custom environment options from RSA authentication. */
1170 while (custom_environment) {
1171 struct envstring *ce = custom_environment;
1172 char *s = ce->s;
1173 int i;
1174 for (i = 0; s[i] != '=' && s[i]; i++);
1175 if (s[i] == '=') {
1176 s[i] = 0;
1177 child_set_env(&env, &envsize, s, s + i + 1);
1178 }
1179 custom_environment = ce->next;
1180 xfree(ce->s);
1181 xfree(ce);
1182 }
1183
1184 snprintf(buf, sizeof buf, "%.50s %d %d",
1185 get_remote_ipaddr(), get_remote_port(), get_local_port());
1186 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1187
1188 if (ttyname)
1189 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1190 if (term)
1191 child_set_env(&env, &envsize, "TERM", term);
1192 if (display)
1193 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001194 if (original_command)
1195 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1196 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001197
1198#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001199 if ((cp = getenv("AUTHSTATE")) != NULL)
1200 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1201 if ((cp = getenv("KRB5CCNAME")) != NULL)
1202 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1203 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001204#endif
1205
1206#ifdef KRB4
1207 {
1208 extern char *ticket;
1209
1210 if (ticket)
1211 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1212 }
1213#endif /* KRB4 */
1214
1215#ifdef USE_PAM
1216 /* Pull in any environment variables that may have been set by PAM. */
1217 do_pam_environment(&env, &envsize);
1218#endif /* USE_PAM */
1219
Damien Millerb38eff82000-04-01 11:09:21 +10001220 if (xauthfile)
1221 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1222 if (auth_get_socket_name() != NULL)
1223 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1224 auth_get_socket_name());
1225
1226 /* read $HOME/.ssh/environment. */
1227 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001228 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1229 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001230 read_environment_file(&env, &envsize, buf);
1231 }
1232 if (debug_flag) {
1233 /* dump the environment */
1234 fprintf(stderr, "Environment:\n");
1235 for (i = 0; env[i]; i++)
1236 fprintf(stderr, " %.200s\n", env[i]);
1237 }
Damien Millerad833b32000-08-23 10:46:23 +10001238 /* we have to stash the hostname before we close our socket. */
1239 if (options.use_login)
1240 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001241 /*
1242 * Close the connection descriptors; note that this is the child, and
1243 * the server will still have the socket open, and it is important
1244 * that we do not shutdown it. Note that the descriptors cannot be
1245 * closed before building the environment, as we call
1246 * get_remote_ipaddr there.
1247 */
1248 if (packet_get_connection_in() == packet_get_connection_out())
1249 close(packet_get_connection_in());
1250 else {
1251 close(packet_get_connection_in());
1252 close(packet_get_connection_out());
1253 }
1254 /*
1255 * Close all descriptors related to channels. They will still remain
1256 * open in the parent.
1257 */
1258 /* XXX better use close-on-exec? -markus */
1259 channel_close_all();
1260
1261 /*
1262 * Close any extra file descriptors. Note that there may still be
1263 * descriptors left by system functions. They will be closed later.
1264 */
1265 endpwent();
1266
1267 /*
1268 * Close any extra open file descriptors so that we don\'t have them
1269 * hanging around in clients. Note that we want to do this after
1270 * initgroups, because at least on Solaris 2.3 it leaves file
1271 * descriptors open.
1272 */
1273 for (i = 3; i < 64; i++)
1274 close(i);
1275
1276 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001277 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001278 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1279 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001280#ifdef HAVE_LOGIN_CAP
1281 if (login_getcapbool(lc, "requirehome", 0))
1282 exit(1);
1283#endif
1284 }
Damien Millerb38eff82000-04-01 11:09:21 +10001285
1286 /*
1287 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1288 * xauth are run in the proper environment.
1289 */
1290 environ = env;
1291
1292 /*
1293 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1294 * in this order).
1295 */
1296 if (!options.use_login) {
1297 if (stat(SSH_USER_RC, &st) >= 0) {
1298 if (debug_flag)
Damien Miller7b413d22000-07-01 13:24:21 +10001299 fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001300
Damien Miller7b413d22000-07-01 13:24:21 +10001301 f = popen(_PATH_BSHELL " " SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001302 if (f) {
1303 if (auth_proto != NULL && auth_data != NULL)
1304 fprintf(f, "%s %s\n", auth_proto, auth_data);
1305 pclose(f);
1306 } else
1307 fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
1308 } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
1309 if (debug_flag)
Damien Miller7b413d22000-07-01 13:24:21 +10001310 fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001311
Damien Miller7b413d22000-07-01 13:24:21 +10001312 f = popen(_PATH_BSHELL " " SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001313 if (f) {
1314 if (auth_proto != NULL && auth_data != NULL)
1315 fprintf(f, "%s %s\n", auth_proto, auth_data);
1316 pclose(f);
1317 } else
1318 fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001319 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001320 /* Add authority data to .Xauthority if appropriate. */
1321 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001322 char *screen = strchr(display, ':');
1323 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001324 fprintf(stderr,
1325 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001326 options.xauth_location, display,
1327 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001328#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001329 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001330 fprintf(stderr,
1331 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001332 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001333 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001334#endif
Damien Millerd999ae22000-05-20 12:49:31 +10001335 }
Damien Miller0c043c12000-06-07 21:22:38 +10001336 snprintf(cmd, sizeof cmd, "%s -q -",
1337 options.xauth_location);
1338 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001339 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001340 fprintf(f, "add %s %s %s\n", display,
1341 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001342#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001343 if (screen != NULL)
1344 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001345 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001346 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001347#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001348 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001349 } else {
1350 fprintf(stderr, "Could not run %s\n",
1351 cmd);
1352 }
Damien Millerb38eff82000-04-01 11:09:21 +10001353 }
1354 }
Damien Millerb38eff82000-04-01 11:09:21 +10001355 /* Get the last component of the shell name. */
1356 cp = strrchr(shell, '/');
1357 if (cp)
1358 cp++;
1359 else
1360 cp = shell;
1361 }
1362 /*
1363 * If we have no command, execute the shell. In this case, the shell
1364 * name to be passed in argv[0] is preceded by '-' to indicate that
1365 * this is a login shell.
1366 */
1367 if (!command) {
1368 if (!options.use_login) {
1369 char buf[256];
1370
1371 /*
1372 * Check for mail if we have a tty and it was enabled
1373 * in server options.
1374 */
1375 if (ttyname && options.check_mail) {
1376 char *mailbox;
1377 struct stat mailstat;
1378 mailbox = getenv("MAIL");
1379 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001380 if (stat(mailbox, &mailstat) != 0 ||
1381 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001382 printf("No mail.\n");
1383 else if (mailstat.st_mtime < mailstat.st_atime)
1384 printf("You have mail.\n");
1385 else
1386 printf("You have new mail.\n");
1387 }
1388 }
1389 /* Start the shell. Set initial character to '-'. */
1390 buf[0] = '-';
1391 strncpy(buf + 1, cp, sizeof(buf) - 1);
1392 buf[sizeof(buf) - 1] = 0;
1393
1394 /* Execute the shell. */
1395 argv[0] = buf;
1396 argv[1] = NULL;
1397 execve(shell, argv, env);
1398
1399 /* Executing the shell failed. */
1400 perror(shell);
1401 exit(1);
1402
1403 } else {
1404 /* Launch login(1). */
1405
Damien Millerad833b32000-08-23 10:46:23 +10001406 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001407 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001408
1409 /* Login couldn't be executed, die. */
1410
1411 perror("login");
1412 exit(1);
1413 }
1414 }
1415 /*
1416 * Execute the command using the user's shell. This uses the -c
1417 * option to execute the command.
1418 */
1419 argv[0] = (char *) cp;
1420 argv[1] = "-c";
1421 argv[2] = (char *) command;
1422 argv[3] = NULL;
1423 execve(shell, argv, env);
1424 perror(shell);
1425 exit(1);
1426}
1427
1428Session *
1429session_new(void)
1430{
1431 int i;
1432 static int did_init = 0;
1433 if (!did_init) {
1434 debug("session_new: init");
1435 for(i = 0; i < MAX_SESSIONS; i++) {
1436 sessions[i].used = 0;
1437 sessions[i].self = i;
1438 }
1439 did_init = 1;
1440 }
1441 for(i = 0; i < MAX_SESSIONS; i++) {
1442 Session *s = &sessions[i];
1443 if (! s->used) {
1444 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001445 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001446 s->chanid = -1;
1447 s->ptyfd = -1;
1448 s->ttyfd = -1;
1449 s->term = NULL;
1450 s->pw = NULL;
1451 s->display = NULL;
1452 s->screen = 0;
1453 s->auth_data = NULL;
1454 s->auth_proto = NULL;
1455 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001456 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001457 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001458 return s;
1459 }
1460 }
1461 return NULL;
1462}
1463
1464void
1465session_dump(void)
1466{
1467 int i;
1468 for(i = 0; i < MAX_SESSIONS; i++) {
1469 Session *s = &sessions[i];
1470 debug("dump: used %d session %d %p channel %d pid %d",
1471 s->used,
1472 s->self,
1473 s,
1474 s->chanid,
1475 s->pid);
1476 }
1477}
1478
Damien Millerefb4afe2000-04-12 18:45:05 +10001479int
1480session_open(int chanid)
1481{
1482 Session *s = session_new();
1483 debug("session_open: channel %d", chanid);
1484 if (s == NULL) {
1485 error("no more sessions");
1486 return 0;
1487 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001488 s->pw = auth_get_user();
1489 if (s->pw == NULL)
Damien Millerbd483e72000-04-30 10:00:53 +10001490 fatal("no user for session %i", s->self);
1491 debug("session_open: session %d: link with channel %d", s->self, chanid);
1492 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001493 return 1;
1494}
1495
1496Session *
1497session_by_channel(int id)
1498{
1499 int i;
1500 for(i = 0; i < MAX_SESSIONS; i++) {
1501 Session *s = &sessions[i];
1502 if (s->used && s->chanid == id) {
1503 debug("session_by_channel: session %d channel %d", i, id);
1504 return s;
1505 }
1506 }
1507 debug("session_by_channel: unknown channel %d", id);
1508 session_dump();
1509 return NULL;
1510}
1511
1512Session *
1513session_by_pid(pid_t pid)
1514{
1515 int i;
1516 debug("session_by_pid: pid %d", pid);
1517 for(i = 0; i < MAX_SESSIONS; i++) {
1518 Session *s = &sessions[i];
1519 if (s->used && s->pid == pid)
1520 return s;
1521 }
1522 error("session_by_pid: unknown pid %d", pid);
1523 session_dump();
1524 return NULL;
1525}
1526
1527int
1528session_window_change_req(Session *s)
1529{
1530 s->col = packet_get_int();
1531 s->row = packet_get_int();
1532 s->xpixel = packet_get_int();
1533 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001534 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001535 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1536 return 1;
1537}
1538
1539int
1540session_pty_req(Session *s)
1541{
1542 unsigned int len;
Damien Miller4af51302000-04-16 11:18:38 +10001543 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001544
Damien Millerf6d9e222000-06-18 14:50:44 +10001545 if (no_pty_flag)
1546 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001547 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001548 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001549 s->term = packet_get_string(&len);
1550 s->col = packet_get_int();
1551 s->row = packet_get_int();
1552 s->xpixel = packet_get_int();
1553 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001554 term_modes = packet_get_string(&len);
1555 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001556
1557 if (strcmp(s->term, "") == 0) {
1558 xfree(s->term);
1559 s->term = NULL;
1560 }
1561 /* Allocate a pty and open it. */
1562 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1563 xfree(s->term);
1564 s->term = NULL;
1565 s->ptyfd = -1;
1566 s->ttyfd = -1;
1567 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001568 xfree(term_modes);
1569 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001570 }
1571 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1572 /*
1573 * Add a cleanup function to clear the utmp entry and record logout
1574 * time in case we call fatal() (e.g., the connection gets closed).
1575 */
1576 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1577 pty_setowner(s->pw, s->tty);
1578 /* Get window size from the packet. */
1579 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1580
Damien Millere247cc42000-05-07 12:03:14 +10001581 session_proctitle(s);
1582
Damien Miller5f056372000-04-16 12:31:48 +10001583 /* XXX parse and set terminal modes */
1584 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001585 return 1;
1586}
1587
Damien Millerbd483e72000-04-30 10:00:53 +10001588int
1589session_subsystem_req(Session *s)
1590{
1591 unsigned int len;
1592 int success = 0;
1593 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001594 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001595
1596 packet_done();
1597 log("subsystem request for %s", subsys);
1598
Damien Millerf6d9e222000-06-18 14:50:44 +10001599 for (i = 0; i < options.num_subsystems; i++) {
1600 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1601 debug("subsystem: exec() %s", options.subsystem_command[i]);
1602 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1603 success = 1;
1604 }
1605 }
1606
1607 if (!success)
1608 log("subsystem request for %s failed, subsystem not found", subsys);
1609
Damien Millerbd483e72000-04-30 10:00:53 +10001610 xfree(subsys);
1611 return success;
1612}
1613
1614int
1615session_x11_req(Session *s)
1616{
Damien Miller7b28dc52000-09-05 13:34:53 +11001617 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001618 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001619 debug("X11 forwarding disabled in user configuration file.");
1620 return 0;
1621 }
Damien Millerbd483e72000-04-30 10:00:53 +10001622 if (!options.x11_forwarding) {
1623 debug("X11 forwarding disabled in server configuration file.");
1624 return 0;
1625 }
1626 if (xauthfile != NULL) {
1627 debug("X11 fwd already started.");
1628 return 0;
1629 }
1630
1631 debug("Received request for X11 forwarding with auth spoofing.");
1632 if (s->display != NULL)
1633 packet_disconnect("Protocol error: X11 display already set.");
1634
1635 s->single_connection = packet_get_char();
1636 s->auth_proto = packet_get_string(NULL);
1637 s->auth_data = packet_get_string(NULL);
1638 s->screen = packet_get_int();
1639 packet_done();
1640
1641 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1642 if (s->display == NULL) {
1643 xfree(s->auth_proto);
1644 xfree(s->auth_data);
1645 return 0;
1646 }
1647 xauthfile = xmalloc(MAXPATHLEN);
1648 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1649 temporarily_use_uid(s->pw->pw_uid);
1650 if (mkdtemp(xauthfile) == NULL) {
1651 restore_uid();
1652 error("private X11 dir: mkdtemp %s failed: %s",
1653 xauthfile, strerror(errno));
1654 xfree(xauthfile);
1655 xauthfile = NULL;
1656 xfree(s->auth_proto);
1657 xfree(s->auth_data);
1658 /* XXXX remove listening channels */
1659 return 0;
1660 }
1661 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001662 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1663 if (fd >= 0)
1664 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001665 restore_uid();
1666 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1667 return 1;
1668}
1669
Damien Millerf6d9e222000-06-18 14:50:44 +10001670int
1671session_shell_req(Session *s)
1672{
1673 /* if forced_command == NULL, the shell is execed */
1674 char *shell = forced_command;
1675 packet_done();
1676 s->extended = 1;
1677 if (s->ttyfd == -1)
1678 do_exec_no_pty(s, shell, s->pw);
1679 else
1680 do_exec_pty(s, shell, s->pw);
1681 return 1;
1682}
1683
1684int
1685session_exec_req(Session *s)
1686{
1687 unsigned int len;
1688 char *command = packet_get_string(&len);
1689 packet_done();
1690 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001691 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001692 command = forced_command;
1693 debug("Forced command '%.500s'", forced_command);
1694 }
1695 s->extended = 1;
1696 if (s->ttyfd == -1)
1697 do_exec_no_pty(s, command, s->pw);
1698 else
1699 do_exec_pty(s, command, s->pw);
1700 if (forced_command == NULL)
1701 xfree(command);
1702 return 1;
1703}
1704
Damien Millerefb4afe2000-04-12 18:45:05 +10001705void
1706session_input_channel_req(int id, void *arg)
1707{
1708 unsigned int len;
1709 int reply;
1710 int success = 0;
1711 char *rtype;
1712 Session *s;
1713 Channel *c;
1714
1715 rtype = packet_get_string(&len);
1716 reply = packet_get_char();
1717
1718 s = session_by_channel(id);
1719 if (s == NULL)
1720 fatal("session_input_channel_req: channel %d: no session", id);
1721 c = channel_lookup(id);
1722 if (c == NULL)
1723 fatal("session_input_channel_req: channel %d: bad channel", id);
1724
1725 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1726 s->self, id, rtype, reply);
1727
1728 /*
1729 * a session is in LARVAL state until a shell
1730 * or programm is executed
1731 */
1732 if (c->type == SSH_CHANNEL_LARVAL) {
1733 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001734 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001735 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001736 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001737 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001738 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001739 } else if (strcmp(rtype, "x11-req") == 0) {
1740 success = session_x11_req(s);
1741 } else if (strcmp(rtype, "subsystem") == 0) {
1742 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001743 }
1744 }
1745 if (strcmp(rtype, "window-change") == 0) {
1746 success = session_window_change_req(s);
1747 }
1748
1749 if (reply) {
1750 packet_start(success ?
1751 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1752 packet_put_int(c->remote_id);
1753 packet_send();
1754 }
1755 xfree(rtype);
1756}
1757
1758void
1759session_set_fds(Session *s, int fdin, int fdout, int fderr)
1760{
1761 if (!compat20)
1762 fatal("session_set_fds: called for proto != 2.0");
1763 /*
1764 * now that have a child and a pipe to the child,
1765 * we can activate our channel and register the fd's
1766 */
1767 if (s->chanid == -1)
1768 fatal("no channel for session %d", s->self);
1769 channel_set_fds(s->chanid,
1770 fdout, fdin, fderr,
1771 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ);
1772}
1773
Damien Millerb38eff82000-04-01 11:09:21 +10001774void
1775session_pty_cleanup(Session *s)
1776{
1777 if (s == NULL || s->ttyfd == -1)
1778 return;
1779
1780 debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1781
1782 /* Cancel the cleanup function. */
1783 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1784
1785 /* Record that the user has logged out. */
1786 record_logout(s->pid, s->tty);
1787
1788 /* Release the pseudo-tty. */
1789 pty_release(s->tty);
1790
1791 /*
1792 * Close the server side of the socket pairs. We must do this after
1793 * the pty cleanup, so that another process doesn't get this pty
1794 * while we're still cleaning up.
1795 */
1796 if (close(s->ptymaster) < 0)
1797 error("close(s->ptymaster): %s", strerror(errno));
1798}
Damien Millerefb4afe2000-04-12 18:45:05 +10001799
1800void
1801session_exit_message(Session *s, int status)
1802{
1803 Channel *c;
1804 if (s == NULL)
1805 fatal("session_close: no session");
1806 c = channel_lookup(s->chanid);
1807 if (c == NULL)
1808 fatal("session_close: session %d: no channel %d",
1809 s->self, s->chanid);
1810 debug("session_exit_message: session %d channel %d pid %d",
1811 s->self, s->chanid, s->pid);
1812
1813 if (WIFEXITED(status)) {
1814 channel_request_start(s->chanid,
1815 "exit-status", 0);
1816 packet_put_int(WEXITSTATUS(status));
1817 packet_send();
1818 } else if (WIFSIGNALED(status)) {
1819 channel_request_start(s->chanid,
1820 "exit-signal", 0);
1821 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001822#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001823 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001824#else /* WCOREDUMP */
1825 packet_put_char(0);
1826#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001827 packet_put_cstring("");
1828 packet_put_cstring("");
1829 packet_send();
1830 } else {
1831 /* Some weird exit cause. Just exit. */
1832 packet_disconnect("wait returned status %04x.", status);
1833 }
1834
1835 /* disconnect channel */
1836 debug("session_exit_message: release channel %d", s->chanid);
1837 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001838 /*
1839 * emulate a write failure with 'chan_write_failed', nobody will be
1840 * interested in data we write.
1841 * Note that we must not call 'chan_read_failed', since there could
1842 * be some more data waiting in the pipe.
1843 */
Damien Millerbd483e72000-04-30 10:00:53 +10001844 if (c->ostate != CHAN_OUTPUT_CLOSED)
1845 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001846 s->chanid = -1;
1847}
1848
1849void
1850session_free(Session *s)
1851{
1852 debug("session_free: session %d pid %d", s->self, s->pid);
1853 if (s->term)
1854 xfree(s->term);
1855 if (s->display)
1856 xfree(s->display);
1857 if (s->auth_data)
1858 xfree(s->auth_data);
1859 if (s->auth_proto)
1860 xfree(s->auth_proto);
1861 s->used = 0;
1862}
1863
1864void
1865session_close(Session *s)
1866{
1867 session_pty_cleanup(s);
1868 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001869 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001870}
1871
1872void
1873session_close_by_pid(pid_t pid, int status)
1874{
1875 Session *s = session_by_pid(pid);
1876 if (s == NULL) {
1877 debug("session_close_by_pid: no session for pid %d", s->pid);
1878 return;
1879 }
1880 if (s->chanid != -1)
1881 session_exit_message(s, status);
1882 session_close(s);
1883}
1884
1885/*
1886 * this is called when a channel dies before
1887 * the session 'child' itself dies
1888 */
1889void
1890session_close_by_channel(int id, void *arg)
1891{
1892 Session *s = session_by_channel(id);
1893 if (s == NULL) {
1894 debug("session_close_by_channel: no session for channel %d", id);
1895 return;
1896 }
1897 /* disconnect channel */
1898 channel_cancel_cleanup(s->chanid);
1899 s->chanid = -1;
1900
1901 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1902 if (s->pid == 0) {
1903 /* close session immediately */
1904 session_close(s);
1905 } else {
1906 /* notify child, delay session cleanup */
Damien Miller099f5052000-06-22 20:57:11 +10001907 if (s->pid <= 1)
Damien Miller7a445bb2000-06-26 13:12:37 +10001908 fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1909 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001910 error("session_close_by_channel: kill %d: %s",
1911 s->pid, strerror(errno));
1912 }
1913}
1914
Damien Millere247cc42000-05-07 12:03:14 +10001915char *
1916session_tty_list(void)
1917{
1918 static char buf[1024];
1919 int i;
1920 buf[0] = '\0';
1921 for(i = 0; i < MAX_SESSIONS; i++) {
1922 Session *s = &sessions[i];
1923 if (s->used && s->ttyfd != -1) {
1924 if (buf[0] != '\0')
1925 strlcat(buf, ",", sizeof buf);
1926 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1927 }
1928 }
1929 if (buf[0] == '\0')
1930 strlcpy(buf, "notty", sizeof buf);
1931 return buf;
1932}
1933
1934void
1935session_proctitle(Session *s)
1936{
1937 if (s->pw == NULL)
1938 error("no user for session %d", s->self);
1939 else
1940 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1941}
1942
Damien Millerefb4afe2000-04-12 18:45:05 +10001943void
1944do_authenticated2(void)
1945{
Damien Miller87d29ed2000-08-30 09:21:22 +11001946#ifdef HAVE_LOGIN_CAP
Damien Millerad833b32000-08-23 10:46:23 +10001947 struct passwd *pw;
Damien Miller87d29ed2000-08-30 09:21:22 +11001948#endif
Damien Millerad833b32000-08-23 10:46:23 +10001949
Damien Millerefb4afe2000-04-12 18:45:05 +10001950 /*
1951 * Cancel the alarm we set to limit the time taken for
1952 * authentication.
1953 */
1954 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10001955 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10001956 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10001957 startup_pipe = -1;
1958 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00001959#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +10001960 pw = auth_get_user();
1961 if ((lc = login_getclass(pw->pw_class)) == NULL) {
1962 error("unable to get login class");
1963 return;
1964 }
1965#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10001966 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10001967 if (xauthfile)
1968 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10001969}