blob: 4b58131a7de578f1e4980b8553d263afbfa5f990 [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 Miller69b69aa2000-10-28 14:19:58 +110036RCSID("$OpenBSD: session.c,v 1.42 2000/10/27 07:32:18 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 Miller69b69aa2000-10-28 14:19:58 +1100122void do_login(Session *s, const char *command);
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 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100239 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000240 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 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100391 if (!options.allow_tcp_forwarding) {
392 debug("Port forwarding not permitted.");
393 break;
394 }
Damien Millerb38eff82000-04-01 11:09:21 +1000395 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000396 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000397 success = 1;
398 break;
399
400 case SSH_CMSG_MAX_PACKET_SIZE:
401 if (packet_set_maxsize(packet_get_int()) > 0)
402 success = 1;
403 break;
404
405 case SSH_CMSG_EXEC_SHELL:
406 case SSH_CMSG_EXEC_CMD:
407 /* Set interactive/non-interactive mode. */
408 packet_set_interactive(have_pty || s->display != NULL,
409 options.keepalives);
410
411 if (type == SSH_CMSG_EXEC_CMD) {
412 command = packet_get_string(&dlen);
413 debug("Exec command '%.500s'", command);
414 packet_integrity_check(plen, 4 + dlen, type);
415 } else {
416 command = NULL;
417 packet_integrity_check(plen, 0, type);
418 }
419 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100420 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000421 command = forced_command;
422 debug("Forced command '%.500s'", forced_command);
423 }
424 if (have_pty)
425 do_exec_pty(s, command, pw);
426 else
427 do_exec_no_pty(s, command, pw);
428
429 if (command != NULL)
430 xfree(command);
431 /* Cleanup user's local Xauthority file. */
432 if (xauthfile)
433 xauthfile_cleanup_proc(NULL);
434 return;
435
436 default:
437 /*
438 * Any unknown messages in this phase are ignored,
439 * and a failure message is returned.
440 */
441 log("Unknown packet type received after authentication: %d", type);
442 }
443 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
444 packet_send();
445 packet_write_wait();
446
447 /* Enable compression now that we have replied if appropriate. */
448 if (enable_compression_after_reply) {
449 enable_compression_after_reply = 0;
450 packet_start_compression(compression_level);
451 }
452 }
453}
454
455/*
456 * This is called to fork and execute a command when we have no tty. This
457 * will call do_child from the child, and server_loop from the parent after
458 * setting up file descriptors and such.
459 */
Damien Miller4af51302000-04-16 11:18:38 +1000460void
Damien Millerb38eff82000-04-01 11:09:21 +1000461do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
462{
463 int pid;
464
465#ifdef USE_PIPES
466 int pin[2], pout[2], perr[2];
467 /* Allocate pipes for communicating with the program. */
468 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
469 packet_disconnect("Could not create pipes: %.100s",
470 strerror(errno));
471#else /* USE_PIPES */
472 int inout[2], err[2];
473 /* Uses socket pairs to communicate with the program. */
474 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
475 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
476 packet_disconnect("Could not create socket pairs: %.100s",
477 strerror(errno));
478#endif /* USE_PIPES */
479 if (s == NULL)
480 fatal("do_exec_no_pty: no session");
481
Damien Millercf3888d2000-09-30 14:17:52 +1100482 signal(SIGPIPE, SIG_DFL);
483
Damien Millere247cc42000-05-07 12:03:14 +1000484 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000485
486#ifdef USE_PAM
487 do_pam_setcred();
488#endif /* USE_PAM */
489
490 /* Fork the child. */
491 if ((pid = fork()) == 0) {
492 /* Child. Reinitialize the log since the pid has changed. */
493 log_init(__progname, options.log_level, options.log_facility, log_stderr);
494
495 /*
496 * Create a new session and process group since the 4.4BSD
497 * setlogin() affects the entire process group.
498 */
499 if (setsid() < 0)
500 error("setsid failed: %.100s", strerror(errno));
501
502#ifdef USE_PIPES
503 /*
504 * Redirect stdin. We close the parent side of the socket
505 * pair, and make the child side the standard input.
506 */
507 close(pin[1]);
508 if (dup2(pin[0], 0) < 0)
509 perror("dup2 stdin");
510 close(pin[0]);
511
512 /* Redirect stdout. */
513 close(pout[0]);
514 if (dup2(pout[1], 1) < 0)
515 perror("dup2 stdout");
516 close(pout[1]);
517
518 /* Redirect stderr. */
519 close(perr[0]);
520 if (dup2(perr[1], 2) < 0)
521 perror("dup2 stderr");
522 close(perr[1]);
523#else /* USE_PIPES */
524 /*
525 * Redirect stdin, stdout, and stderr. Stdin and stdout will
526 * use the same socket, as some programs (particularly rdist)
527 * seem to depend on it.
528 */
529 close(inout[1]);
530 close(err[1]);
531 if (dup2(inout[0], 0) < 0) /* stdin */
532 perror("dup2 stdin");
533 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
534 perror("dup2 stdout");
535 if (dup2(err[0], 2) < 0) /* stderr */
536 perror("dup2 stderr");
537#endif /* USE_PIPES */
538
539 /* Do processing for the child (exec command etc). */
540 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
541 /* NOTREACHED */
542 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100543#ifdef HAVE_CYGWIN
544 if (is_winnt)
545 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
546#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000547 if (pid < 0)
548 packet_disconnect("fork failed: %.100s", strerror(errno));
549 s->pid = pid;
550#ifdef USE_PIPES
551 /* We are the parent. Close the child sides of the pipes. */
552 close(pin[0]);
553 close(pout[1]);
554 close(perr[1]);
555
Damien Millerefb4afe2000-04-12 18:45:05 +1000556 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000557 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000558 } else {
559 /* Enter the interactive session. */
560 server_loop(pid, pin[1], pout[0], perr[0]);
561 /* server_loop has closed pin[1], pout[1], and perr[1]. */
562 }
Damien Millerb38eff82000-04-01 11:09:21 +1000563#else /* USE_PIPES */
564 /* We are the parent. Close the child sides of the socket pairs. */
565 close(inout[0]);
566 close(err[0]);
567
568 /*
569 * Enter the interactive session. Note: server_loop must be able to
570 * handle the case that fdin and fdout are the same.
571 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000572 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000573 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000574 } else {
575 server_loop(pid, inout[1], inout[1], err[1]);
576 /* server_loop has closed inout[1] and err[1]. */
577 }
Damien Millerb38eff82000-04-01 11:09:21 +1000578#endif /* USE_PIPES */
579}
580
581/*
582 * This is called to fork and execute a command when we have a tty. This
583 * will call do_child from the child, and server_loop from the parent after
584 * setting up file descriptors, controlling tty, updating wtmp, utmp,
585 * lastlog, and other such operations.
586 */
Damien Miller4af51302000-04-16 11:18:38 +1000587void
Damien Millerb38eff82000-04-01 11:09:21 +1000588do_exec_pty(Session *s, const char *command, struct passwd * pw)
589{
Damien Millerb38eff82000-04-01 11:09:21 +1000590 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000591 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000592
593 if (s == NULL)
594 fatal("do_exec_pty: no session");
595 ptyfd = s->ptyfd;
596 ttyfd = s->ttyfd;
597
Damien Millerb38eff82000-04-01 11:09:21 +1000598#ifdef USE_PAM
599 do_pam_session(pw->pw_name, s->tty);
600 do_pam_setcred();
601#endif /* USE_PAM */
602
603 /* Fork the child. */
604 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000605 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000606 log_init(__progname, options.log_level, options.log_facility, log_stderr);
607
608 /* Close the master side of the pseudo tty. */
609 close(ptyfd);
610
611 /* Make the pseudo tty our controlling tty. */
612 pty_make_controlling_tty(&ttyfd, s->tty);
613
614 /* Redirect stdin from the pseudo tty. */
615 if (dup2(ttyfd, fileno(stdin)) < 0)
616 error("dup2 stdin failed: %.100s", strerror(errno));
617
618 /* Redirect stdout to the pseudo tty. */
619 if (dup2(ttyfd, fileno(stdout)) < 0)
620 error("dup2 stdin failed: %.100s", strerror(errno));
621
622 /* Redirect stderr to the pseudo tty. */
623 if (dup2(ttyfd, fileno(stderr)) < 0)
624 error("dup2 stdin failed: %.100s", strerror(errno));
625
626 /* Close the extra descriptor for the pseudo tty. */
627 close(ttyfd);
628
Damien Miller942da032000-08-18 13:59:06 +1000629 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100630 if (!(options.use_login && command == NULL))
631 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000632
Damien Millerb38eff82000-04-01 11:09:21 +1000633 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000634 do_child(command, pw, s->term, s->display, s->auth_proto,
635 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000636 /* NOTREACHED */
637 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100638#ifdef HAVE_CYGWIN
639 if (is_winnt)
640 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
641#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000642 if (pid < 0)
643 packet_disconnect("fork failed: %.100s", strerror(errno));
644 s->pid = pid;
645
646 /* Parent. Close the slave side of the pseudo tty. */
647 close(ttyfd);
648
649 /*
650 * Create another descriptor of the pty master side for use as the
651 * standard input. We could use the original descriptor, but this
652 * simplifies code in server_loop. The descriptor is bidirectional.
653 */
654 fdout = dup(ptyfd);
655 if (fdout < 0)
656 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
657
658 /* we keep a reference to the pty master */
659 ptymaster = dup(ptyfd);
660 if (ptymaster < 0)
661 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
662 s->ptymaster = ptymaster;
663
664 /* Enter interactive session. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000665 if (compat20) {
666 session_set_fds(s, ptyfd, fdout, -1);
667 } else {
668 server_loop(pid, ptyfd, fdout, -1);
669 /* server_loop _has_ closed ptyfd and fdout. */
670 session_pty_cleanup(s);
671 }
Damien Millerb38eff82000-04-01 11:09:21 +1000672}
673
Damien Miller942da032000-08-18 13:59:06 +1000674const char *
675get_remote_name_or_ip(void)
676{
677 static const char *remote = "";
678 if (utmp_len > 0)
679 remote = get_canonical_hostname();
680 if (utmp_len == 0 || strlen(remote) > utmp_len)
681 remote = get_remote_ipaddr();
682 return remote;
683}
684
685/* administrative, login(1)-like work */
686void
Damien Miller69b69aa2000-10-28 14:19:58 +1100687do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000688{
689 FILE *f;
690 char *time_string;
691 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100692 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000693 socklen_t fromlen;
694 struct sockaddr_storage from;
695 struct stat st;
696 time_t last_login_time;
697 struct passwd * pw = s->pw;
698 pid_t pid = getpid();
699
700 /*
701 * Get IP address of client. If the connection is not a socket, let
702 * the address be 0.0.0.0.
703 */
704 memset(&from, 0, sizeof(from));
705 if (packet_connection_is_on_socket()) {
706 fromlen = sizeof(from);
707 if (getpeername(packet_get_connection_in(),
708 (struct sockaddr *) & from, &fromlen) < 0) {
709 debug("getpeername: %.100s", strerror(errno));
710 fatal_cleanup();
711 }
712 }
713
Damien Miller7b28dc52000-09-05 13:34:53 +1100714 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100715 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
Kevin Steves092f2ef2000-10-14 13:36:13 +0000723#ifdef USE_PAM
724 /*
725 * If password change is needed, do it now.
726 * This needs to occur before the ~/.hushlogin check.
727 */
728 if (pam_password_change_required()) {
729 print_pam_messages();
730 do_pam_chauthtok();
731 }
732#endif
733
Damien Miller69b69aa2000-10-28 14:19:58 +1100734 /* Done if .hushlogin exists or a command given. */
735 if (command != NULL)
736 return;
Damien Miller942da032000-08-18 13:59:06 +1000737 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000738#ifdef HAVE_LOGIN_CAP
739 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
740#else
Damien Miller942da032000-08-18 13:59:06 +1000741 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000742#endif
Damien Miller942da032000-08-18 13:59:06 +1000743 return;
744
745#ifdef USE_PAM
Kevin Steves092f2ef2000-10-14 13:36:13 +0000746 if (!pam_password_change_required())
747 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000748#endif /* USE_PAM */
749#ifdef WITH_AIXAUTHENTICATE
750 if (aixloginmsg && *aixloginmsg)
751 printf("%s\n", aixloginmsg);
752#endif /* WITH_AIXAUTHENTICATE */
753
Damien Miller942da032000-08-18 13:59:06 +1000754 if (last_login_time != 0) {
755 time_string = ctime(&last_login_time);
756 if (strchr(time_string, '\n'))
757 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000758 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000759 printf("Last login: %s\r\n", time_string);
760 else
Damien Millere4340be2000-09-16 13:29:08 +1100761 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000762 }
763 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000764#ifdef HAVE_LOGIN_CAP
765 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
766 "/etc/motd"), "r");
767#else
Damien Miller942da032000-08-18 13:59:06 +1000768 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000769#endif
Damien Miller942da032000-08-18 13:59:06 +1000770 if (f) {
771 while (fgets(buf, sizeof(buf), f))
772 fputs(buf, stdout);
773 fclose(f);
774 }
775 }
776}
777
Damien Millerb38eff82000-04-01 11:09:21 +1000778/*
779 * Sets the value of the given variable in the environment. If the variable
780 * already exists, its value is overriden.
781 */
Damien Miller4af51302000-04-16 11:18:38 +1000782void
Damien Millerb38eff82000-04-01 11:09:21 +1000783child_set_env(char ***envp, unsigned int *envsizep, const char *name,
784 const char *value)
785{
786 unsigned int i, namelen;
787 char **env;
788
789 /*
790 * Find the slot where the value should be stored. If the variable
791 * already exists, we reuse the slot; otherwise we append a new slot
792 * at the end of the array, expanding if necessary.
793 */
794 env = *envp;
795 namelen = strlen(name);
796 for (i = 0; env[i]; i++)
797 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
798 break;
799 if (env[i]) {
800 /* Reuse the slot. */
801 xfree(env[i]);
802 } else {
803 /* New variable. Expand if necessary. */
804 if (i >= (*envsizep) - 1) {
805 (*envsizep) += 50;
806 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
807 }
808 /* Need to set the NULL pointer at end of array beyond the new slot. */
809 env[i + 1] = NULL;
810 }
811
812 /* Allocate space and format the variable in the appropriate slot. */
813 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
814 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
815}
816
817/*
818 * Reads environment variables from the given file and adds/overrides them
819 * into the environment. If the file does not exist, this does nothing.
820 * Otherwise, it must consist of empty lines, comments (line starts with '#')
821 * and assignments of the form name=value. No other forms are allowed.
822 */
Damien Miller4af51302000-04-16 11:18:38 +1000823void
Damien Millerb38eff82000-04-01 11:09:21 +1000824read_environment_file(char ***env, unsigned int *envsize,
825 const char *filename)
826{
827 FILE *f;
828 char buf[4096];
829 char *cp, *value;
830
831 f = fopen(filename, "r");
832 if (!f)
833 return;
834
835 while (fgets(buf, sizeof(buf), f)) {
836 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
837 ;
838 if (!*cp || *cp == '#' || *cp == '\n')
839 continue;
840 if (strchr(cp, '\n'))
841 *strchr(cp, '\n') = '\0';
842 value = strchr(cp, '=');
843 if (value == NULL) {
844 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
845 continue;
846 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000847 /*
848 * Replace the equals sign by nul, and advance value to
849 * the value string.
850 */
Damien Millerb38eff82000-04-01 11:09:21 +1000851 *value = '\0';
852 value++;
853 child_set_env(env, envsize, cp, value);
854 }
855 fclose(f);
856}
857
858#ifdef USE_PAM
859/*
860 * Sets any environment variables which have been specified by PAM
861 */
862void do_pam_environment(char ***env, int *envsize)
863{
864 char *equals, var_name[512], var_val[512];
865 char **pam_env;
866 int i;
867
868 if ((pam_env = fetch_pam_environment()) == NULL)
869 return;
870
871 for(i = 0; pam_env[i] != NULL; i++) {
872 if ((equals = strstr(pam_env[i], "=")) == NULL)
873 continue;
874
875 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
876 memset(var_name, '\0', sizeof(var_name));
877 memset(var_val, '\0', sizeof(var_val));
878
879 strncpy(var_name, pam_env[i], equals - pam_env[i]);
880 strcpy(var_val, equals + 1);
881
Damien Millercb5e44a2000-09-29 12:12:36 +1100882 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000883
884 child_set_env(env, envsize, var_name, var_val);
885 }
886 }
887}
888#endif /* USE_PAM */
889
Damien Millercb5e44a2000-09-29 12:12:36 +1100890
891#ifdef HAVE_CYGWIN
892void copy_environment(char ***env, int *envsize)
893{
894 char *equals, var_name[512], var_val[512];
895 int i;
896
897 for(i = 0; environ[i] != NULL; i++) {
898 if ((equals = strstr(environ[i], "=")) == NULL)
899 continue;
900
901 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
902 memset(var_name, '\0', sizeof(var_name));
903 memset(var_val, '\0', sizeof(var_val));
904
905 strncpy(var_name, environ[i], equals - environ[i]);
906 strcpy(var_val, equals + 1);
907
908 debug3("Copy environment: %s=%s", var_name, var_val);
909
910 child_set_env(env, envsize, var_name, var_val);
911 }
912 }
913}
914#endif
915
Damien Miller5fc85652000-07-09 23:53:07 +1000916#if defined(HAVE_GETUSERATTR)
917/*
918 * AIX-specific login initialisation
919 */
920void set_limit(char *user, char *soft, char *hard, int resource, int mult)
921{
922 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000923 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000924
925 getrlimit(resource, &rlim);
926
Damien Miller65964d62000-07-11 09:16:22 +1000927 slim = 0;
928 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
929 if (slim < 0) {
930 rlim.rlim_cur = RLIM_INFINITY;
931 } else if (slim != 0) {
932 /* See the wackiness below */
933 if (rlim.rlim_cur == slim * mult)
934 slim = 0;
935 else
936 rlim.rlim_cur = slim * mult;
937 }
938 }
Damien Miller5fc85652000-07-09 23:53:07 +1000939
Damien Miller65964d62000-07-11 09:16:22 +1000940 hlim = 0;
941 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
942 if (hlim < 0) {
943 rlim.rlim_max = RLIM_INFINITY;
944 } else if (hlim != 0) {
945 rlim.rlim_max = hlim * mult;
946 }
947 }
Damien Miller5fc85652000-07-09 23:53:07 +1000948
Damien Miller65964d62000-07-11 09:16:22 +1000949 /*
950 * XXX For cpu and fsize the soft limit is set to the hard limit
951 * if the hard limit is left at its default value and the soft limit
952 * is changed from its default value, either by requesting it
953 * (slim == 0) or by setting it to the current default. At least
954 * that's how rlogind does it. If you're confused you're not alone.
955 * Bug or feature? AIX 4.3.1.2
956 */
957 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
958 && hlim == 0 && slim != 0)
959 rlim.rlim_max = rlim.rlim_cur;
960 /* A specified hard limit limits the soft limit */
961 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
962 rlim.rlim_cur = rlim.rlim_max;
963 /* A soft limit can increase a hard limit */
964 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000965 rlim.rlim_max = rlim.rlim_cur;
966
967 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000968 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000969}
970
971void set_limits_from_userattr(char *user)
972{
973 int mask;
974 char buf[16];
975
976 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
977 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
978 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
979 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
980 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
981 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
982#if defined(S_UNOFILE)
983 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
984#endif
985
986 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
987 /* Convert decimal to octal */
988 (void) snprintf(buf, sizeof(buf), "%d", mask);
989 if (sscanf(buf, "%o", &mask) == 1)
990 umask(mask);
991 }
992}
993#endif /* defined(HAVE_GETUSERATTR) */
994
Damien Millerb38eff82000-04-01 11:09:21 +1000995/*
996 * Performs common processing for the child, such as setting up the
997 * environment, closing extra file descriptors, setting the user and group
998 * ids, and executing the command or shell.
999 */
Damien Miller4af51302000-04-16 11:18:38 +10001000void
Damien Millerb38eff82000-04-01 11:09:21 +10001001do_child(const char *command, struct passwd * pw, const char *term,
1002 const char *display, const char *auth_proto,
1003 const char *auth_data, const char *ttyname)
1004{
Damien Miller7b28dc52000-09-05 13:34:53 +11001005 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001006 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001007 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001008 FILE *f = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001009 unsigned int envsize, i;
1010 char **env;
1011 extern char **environ;
1012 struct stat st;
1013 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001014#ifdef WITH_IRIX_PROJECT
1015 prid_t projid;
1016#endif /* WITH_IRIX_PROJECT */
Damien Millerb38eff82000-04-01 11:09:21 +10001017
Damien Millerd3a18572000-06-07 19:55:44 +10001018 /* login(1) is only called if we execute the login shell */
1019 if (options.use_login && command != NULL)
1020 options.use_login = 0;
1021
Damien Millerb38eff82000-04-01 11:09:21 +10001022#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001023 if (!options.use_login) {
1024# ifdef HAVE_LOGIN_CAP
1025 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1026 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1027 _PATH_NOLOGIN), "r");
1028# else /* HAVE_LOGIN_CAP */
1029 if (pw->pw_uid)
1030 f = fopen(_PATH_NOLOGIN, "r");
1031# endif /* HAVE_LOGIN_CAP */
1032 if (f) {
1033 /* /etc/nologin exists. Print its contents and exit. */
1034 while (fgets(buf, sizeof(buf), f))
1035 fputs(buf, stderr);
1036 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001037 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001038 }
Damien Millerb38eff82000-04-01 11:09:21 +10001039 }
1040#endif /* USE_PAM */
1041
Damien Millerad833b32000-08-23 10:46:23 +10001042 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001043 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1044 switch, so we let login(1) to this for us. */
1045 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001046#ifdef HAVE_OSF_SIA
1047 extern char **saved_argv;
1048 extern int saved_argc;
1049 char *host = get_canonical_hostname ();
1050
1051 if (sia_become_user(NULL, saved_argc, saved_argv, host,
1052 pw->pw_name, ttyname, 0, NULL, NULL, SIA_BEU_SETLUID) !=
1053 SIASUCCESS) {
1054 perror("sia_become_user");
1055 exit(1);
1056 }
1057 if (setreuid(geteuid(), geteuid()) < 0) {
1058 perror("setreuid");
1059 exit(1);
1060 }
1061#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001062#ifdef HAVE_CYGWIN
1063 if (is_winnt) {
1064#else
Damien Millerb38eff82000-04-01 11:09:21 +10001065 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001066#endif
Damien Millerad833b32000-08-23 10:46:23 +10001067# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001068 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001069# endif /* HAVE_GETUSERATTR */
1070# ifdef HAVE_LOGIN_CAP
1071 if (setusercontext(lc, pw, pw->pw_uid,
1072 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1073 perror("unable to set user context");
1074 exit(1);
1075 }
1076# else /* HAVE_LOGIN_CAP */
1077 if (setlogin(pw->pw_name) < 0)
1078 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001079 if (setgid(pw->pw_gid) < 0) {
1080 perror("setgid");
1081 exit(1);
1082 }
1083 /* Initialize the group list. */
1084 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1085 perror("initgroups");
1086 exit(1);
1087 }
1088 endgrent();
Damien Millerad833b32000-08-23 10:46:23 +10001089# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001090 /* initialize array session */
1091 if (newarraysess() != 0)
1092 fatal("Failed to set up new array session: %.100s",
1093 strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001094# endif /* WITH_IRIX_ARRAY */
1095# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001096 /* initialize irix project info */
1097 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1098 debug("Failed to get project id, using projid 0");
1099 projid = 0;
1100 }
Damien Miller91606b12000-06-28 08:22:29 +10001101 if (setprid(projid))
1102 fatal("Failed to initialize project %d for %s: %.100s",
1103 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001104# endif /* WITH_IRIX_PROJECT */
Damien Millerb38eff82000-04-01 11:09:21 +10001105 /* Permanently switch to the desired uid. */
1106 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001107# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001108 }
Damien Millerad833b32000-08-23 10:46:23 +10001109#endif /* HAVE_OSF_SIA */
1110
Damien Millerbac2d8a2000-09-05 16:13:06 +11001111#ifdef HAVE_CYGWIN
1112 if (is_winnt)
1113#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001114 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001115 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001116 }
1117 /*
1118 * Get the shell from the password data. An empty shell field is
1119 * legal, and means /bin/sh.
1120 */
1121 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001122#ifdef HAVE_LOGIN_CAP
1123 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1124#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001125
1126#ifdef AFS
1127 /* Try to get AFS tokens for the local cell. */
1128 if (k_hasafs()) {
1129 char cell[64];
1130
1131 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1132 krb_afslog(cell, 0);
1133
1134 krb_afslog(0, 0);
1135 }
1136#endif /* AFS */
1137
1138 /* Initialize the environment. */
1139 envsize = 100;
1140 env = xmalloc(envsize * sizeof(char *));
1141 env[0] = NULL;
1142
Damien Millerbac2d8a2000-09-05 16:13:06 +11001143#ifdef HAVE_CYGWIN
1144 /*
1145 * The Windows environment contains some setting which are
1146 * important for a running system. They must not be dropped.
1147 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001148 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001149#endif
1150
Damien Millerb38eff82000-04-01 11:09:21 +10001151 if (!options.use_login) {
1152 /* Set basic environment. */
1153 child_set_env(&env, &envsize, "USER", pw->pw_name);
1154 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1155 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001156#ifdef HAVE_LOGIN_CAP
1157 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1158 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001159#else /* HAVE_LOGIN_CAP */
1160# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001161 /*
1162 * There's no standard path on Windows. The path contains
1163 * important components pointing to the system directories,
1164 * needed for loading shared libraries. So the path better
1165 * remains intact here.
1166 */
Damien Millerb38eff82000-04-01 11:09:21 +10001167 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001168# endif /* HAVE_CYGWIN */
1169#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001170
1171 snprintf(buf, sizeof buf, "%.200s/%.50s",
1172 _PATH_MAILDIR, pw->pw_name);
1173 child_set_env(&env, &envsize, "MAIL", buf);
1174
1175 /* Normal systems set SHELL by default. */
1176 child_set_env(&env, &envsize, "SHELL", shell);
1177 }
1178 if (getenv("TZ"))
1179 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1180
1181 /* Set custom environment options from RSA authentication. */
1182 while (custom_environment) {
1183 struct envstring *ce = custom_environment;
1184 char *s = ce->s;
1185 int i;
1186 for (i = 0; s[i] != '=' && s[i]; i++);
1187 if (s[i] == '=') {
1188 s[i] = 0;
1189 child_set_env(&env, &envsize, s, s + i + 1);
1190 }
1191 custom_environment = ce->next;
1192 xfree(ce->s);
1193 xfree(ce);
1194 }
1195
1196 snprintf(buf, sizeof buf, "%.50s %d %d",
1197 get_remote_ipaddr(), get_remote_port(), get_local_port());
1198 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1199
1200 if (ttyname)
1201 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1202 if (term)
1203 child_set_env(&env, &envsize, "TERM", term);
1204 if (display)
1205 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001206 if (original_command)
1207 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1208 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001209
1210#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001211 if ((cp = getenv("AUTHSTATE")) != NULL)
1212 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1213 if ((cp = getenv("KRB5CCNAME")) != NULL)
1214 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1215 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001216#endif
1217
1218#ifdef KRB4
1219 {
1220 extern char *ticket;
1221
1222 if (ticket)
1223 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1224 }
1225#endif /* KRB4 */
1226
1227#ifdef USE_PAM
1228 /* Pull in any environment variables that may have been set by PAM. */
1229 do_pam_environment(&env, &envsize);
1230#endif /* USE_PAM */
1231
Damien Millerb38eff82000-04-01 11:09:21 +10001232 if (xauthfile)
1233 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1234 if (auth_get_socket_name() != NULL)
1235 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1236 auth_get_socket_name());
1237
1238 /* read $HOME/.ssh/environment. */
1239 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001240 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1241 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001242 read_environment_file(&env, &envsize, buf);
1243 }
1244 if (debug_flag) {
1245 /* dump the environment */
1246 fprintf(stderr, "Environment:\n");
1247 for (i = 0; env[i]; i++)
1248 fprintf(stderr, " %.200s\n", env[i]);
1249 }
Damien Millerad833b32000-08-23 10:46:23 +10001250 /* we have to stash the hostname before we close our socket. */
1251 if (options.use_login)
1252 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001253 /*
1254 * Close the connection descriptors; note that this is the child, and
1255 * the server will still have the socket open, and it is important
1256 * that we do not shutdown it. Note that the descriptors cannot be
1257 * closed before building the environment, as we call
1258 * get_remote_ipaddr there.
1259 */
1260 if (packet_get_connection_in() == packet_get_connection_out())
1261 close(packet_get_connection_in());
1262 else {
1263 close(packet_get_connection_in());
1264 close(packet_get_connection_out());
1265 }
1266 /*
1267 * Close all descriptors related to channels. They will still remain
1268 * open in the parent.
1269 */
1270 /* XXX better use close-on-exec? -markus */
1271 channel_close_all();
1272
1273 /*
1274 * Close any extra file descriptors. Note that there may still be
1275 * descriptors left by system functions. They will be closed later.
1276 */
1277 endpwent();
1278
1279 /*
1280 * Close any extra open file descriptors so that we don\'t have them
1281 * hanging around in clients. Note that we want to do this after
1282 * initgroups, because at least on Solaris 2.3 it leaves file
1283 * descriptors open.
1284 */
1285 for (i = 3; i < 64; i++)
1286 close(i);
1287
1288 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001289 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001290 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1291 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001292#ifdef HAVE_LOGIN_CAP
1293 if (login_getcapbool(lc, "requirehome", 0))
1294 exit(1);
1295#endif
1296 }
Damien Millerb38eff82000-04-01 11:09:21 +10001297
1298 /*
1299 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1300 * xauth are run in the proper environment.
1301 */
1302 environ = env;
1303
1304 /*
1305 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1306 * in this order).
1307 */
1308 if (!options.use_login) {
1309 if (stat(SSH_USER_RC, &st) >= 0) {
1310 if (debug_flag)
Damien Miller7b413d22000-07-01 13:24:21 +10001311 fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001312
Damien Miller7b413d22000-07-01 13:24:21 +10001313 f = popen(_PATH_BSHELL " " SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001314 if (f) {
1315 if (auth_proto != NULL && auth_data != NULL)
1316 fprintf(f, "%s %s\n", auth_proto, auth_data);
1317 pclose(f);
1318 } else
1319 fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
1320 } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
1321 if (debug_flag)
Damien Miller7b413d22000-07-01 13:24:21 +10001322 fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001323
Damien Miller7b413d22000-07-01 13:24:21 +10001324 f = popen(_PATH_BSHELL " " SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001325 if (f) {
1326 if (auth_proto != NULL && auth_data != NULL)
1327 fprintf(f, "%s %s\n", auth_proto, auth_data);
1328 pclose(f);
1329 } else
1330 fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001331 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001332 /* Add authority data to .Xauthority if appropriate. */
1333 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001334 char *screen = strchr(display, ':');
1335 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001336 fprintf(stderr,
1337 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001338 options.xauth_location, display,
1339 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001340#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001341 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001342 fprintf(stderr,
1343 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001344 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001345 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001346#endif
Damien Millerd999ae22000-05-20 12:49:31 +10001347 }
Damien Miller0c043c12000-06-07 21:22:38 +10001348 snprintf(cmd, sizeof cmd, "%s -q -",
1349 options.xauth_location);
1350 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001351 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001352 fprintf(f, "add %s %s %s\n", display,
1353 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001354#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001355 if (screen != NULL)
1356 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001357 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001358 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001359#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001360 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001361 } else {
1362 fprintf(stderr, "Could not run %s\n",
1363 cmd);
1364 }
Damien Millerb38eff82000-04-01 11:09:21 +10001365 }
1366 }
Damien Millerb38eff82000-04-01 11:09:21 +10001367 /* Get the last component of the shell name. */
1368 cp = strrchr(shell, '/');
1369 if (cp)
1370 cp++;
1371 else
1372 cp = shell;
1373 }
1374 /*
1375 * If we have no command, execute the shell. In this case, the shell
1376 * name to be passed in argv[0] is preceded by '-' to indicate that
1377 * this is a login shell.
1378 */
1379 if (!command) {
1380 if (!options.use_login) {
1381 char buf[256];
1382
1383 /*
1384 * Check for mail if we have a tty and it was enabled
1385 * in server options.
1386 */
1387 if (ttyname && options.check_mail) {
1388 char *mailbox;
1389 struct stat mailstat;
1390 mailbox = getenv("MAIL");
1391 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001392 if (stat(mailbox, &mailstat) != 0 ||
1393 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001394 printf("No mail.\n");
1395 else if (mailstat.st_mtime < mailstat.st_atime)
1396 printf("You have mail.\n");
1397 else
1398 printf("You have new mail.\n");
1399 }
1400 }
1401 /* Start the shell. Set initial character to '-'. */
1402 buf[0] = '-';
1403 strncpy(buf + 1, cp, sizeof(buf) - 1);
1404 buf[sizeof(buf) - 1] = 0;
1405
1406 /* Execute the shell. */
1407 argv[0] = buf;
1408 argv[1] = NULL;
1409 execve(shell, argv, env);
1410
1411 /* Executing the shell failed. */
1412 perror(shell);
1413 exit(1);
1414
1415 } else {
1416 /* Launch login(1). */
1417
Damien Millerad833b32000-08-23 10:46:23 +10001418 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001419 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001420
1421 /* Login couldn't be executed, die. */
1422
1423 perror("login");
1424 exit(1);
1425 }
1426 }
1427 /*
1428 * Execute the command using the user's shell. This uses the -c
1429 * option to execute the command.
1430 */
1431 argv[0] = (char *) cp;
1432 argv[1] = "-c";
1433 argv[2] = (char *) command;
1434 argv[3] = NULL;
1435 execve(shell, argv, env);
1436 perror(shell);
1437 exit(1);
1438}
1439
1440Session *
1441session_new(void)
1442{
1443 int i;
1444 static int did_init = 0;
1445 if (!did_init) {
1446 debug("session_new: init");
1447 for(i = 0; i < MAX_SESSIONS; i++) {
1448 sessions[i].used = 0;
1449 sessions[i].self = i;
1450 }
1451 did_init = 1;
1452 }
1453 for(i = 0; i < MAX_SESSIONS; i++) {
1454 Session *s = &sessions[i];
1455 if (! s->used) {
1456 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001457 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001458 s->chanid = -1;
1459 s->ptyfd = -1;
1460 s->ttyfd = -1;
1461 s->term = NULL;
1462 s->pw = NULL;
1463 s->display = NULL;
1464 s->screen = 0;
1465 s->auth_data = NULL;
1466 s->auth_proto = NULL;
1467 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001468 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001469 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001470 return s;
1471 }
1472 }
1473 return NULL;
1474}
1475
1476void
1477session_dump(void)
1478{
1479 int i;
1480 for(i = 0; i < MAX_SESSIONS; i++) {
1481 Session *s = &sessions[i];
1482 debug("dump: used %d session %d %p channel %d pid %d",
1483 s->used,
1484 s->self,
1485 s,
1486 s->chanid,
1487 s->pid);
1488 }
1489}
1490
Damien Millerefb4afe2000-04-12 18:45:05 +10001491int
1492session_open(int chanid)
1493{
1494 Session *s = session_new();
1495 debug("session_open: channel %d", chanid);
1496 if (s == NULL) {
1497 error("no more sessions");
1498 return 0;
1499 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001500 s->pw = auth_get_user();
1501 if (s->pw == NULL)
Damien Millerbd483e72000-04-30 10:00:53 +10001502 fatal("no user for session %i", s->self);
1503 debug("session_open: session %d: link with channel %d", s->self, chanid);
1504 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001505 return 1;
1506}
1507
1508Session *
1509session_by_channel(int id)
1510{
1511 int i;
1512 for(i = 0; i < MAX_SESSIONS; i++) {
1513 Session *s = &sessions[i];
1514 if (s->used && s->chanid == id) {
1515 debug("session_by_channel: session %d channel %d", i, id);
1516 return s;
1517 }
1518 }
1519 debug("session_by_channel: unknown channel %d", id);
1520 session_dump();
1521 return NULL;
1522}
1523
1524Session *
1525session_by_pid(pid_t pid)
1526{
1527 int i;
1528 debug("session_by_pid: pid %d", pid);
1529 for(i = 0; i < MAX_SESSIONS; i++) {
1530 Session *s = &sessions[i];
1531 if (s->used && s->pid == pid)
1532 return s;
1533 }
1534 error("session_by_pid: unknown pid %d", pid);
1535 session_dump();
1536 return NULL;
1537}
1538
1539int
1540session_window_change_req(Session *s)
1541{
1542 s->col = packet_get_int();
1543 s->row = packet_get_int();
1544 s->xpixel = packet_get_int();
1545 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001546 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001547 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1548 return 1;
1549}
1550
1551int
1552session_pty_req(Session *s)
1553{
1554 unsigned int len;
Damien Miller4af51302000-04-16 11:18:38 +10001555 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001556
Damien Millerf6d9e222000-06-18 14:50:44 +10001557 if (no_pty_flag)
1558 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001559 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001560 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001561 s->term = packet_get_string(&len);
1562 s->col = packet_get_int();
1563 s->row = packet_get_int();
1564 s->xpixel = packet_get_int();
1565 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001566 term_modes = packet_get_string(&len);
1567 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001568
1569 if (strcmp(s->term, "") == 0) {
1570 xfree(s->term);
1571 s->term = NULL;
1572 }
1573 /* Allocate a pty and open it. */
1574 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1575 xfree(s->term);
1576 s->term = NULL;
1577 s->ptyfd = -1;
1578 s->ttyfd = -1;
1579 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001580 xfree(term_modes);
1581 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001582 }
1583 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1584 /*
1585 * Add a cleanup function to clear the utmp entry and record logout
1586 * time in case we call fatal() (e.g., the connection gets closed).
1587 */
1588 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1589 pty_setowner(s->pw, s->tty);
1590 /* Get window size from the packet. */
1591 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1592
Damien Millere247cc42000-05-07 12:03:14 +10001593 session_proctitle(s);
1594
Damien Miller5f056372000-04-16 12:31:48 +10001595 /* XXX parse and set terminal modes */
1596 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001597 return 1;
1598}
1599
Damien Millerbd483e72000-04-30 10:00:53 +10001600int
1601session_subsystem_req(Session *s)
1602{
1603 unsigned int len;
1604 int success = 0;
1605 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001606 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001607
1608 packet_done();
1609 log("subsystem request for %s", subsys);
1610
Damien Millerf6d9e222000-06-18 14:50:44 +10001611 for (i = 0; i < options.num_subsystems; i++) {
1612 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1613 debug("subsystem: exec() %s", options.subsystem_command[i]);
1614 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1615 success = 1;
1616 }
1617 }
1618
1619 if (!success)
1620 log("subsystem request for %s failed, subsystem not found", subsys);
1621
Damien Millerbd483e72000-04-30 10:00:53 +10001622 xfree(subsys);
1623 return success;
1624}
1625
1626int
1627session_x11_req(Session *s)
1628{
Damien Miller7b28dc52000-09-05 13:34:53 +11001629 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001630 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001631 debug("X11 forwarding disabled in user configuration file.");
1632 return 0;
1633 }
Damien Millerbd483e72000-04-30 10:00:53 +10001634 if (!options.x11_forwarding) {
1635 debug("X11 forwarding disabled in server configuration file.");
1636 return 0;
1637 }
1638 if (xauthfile != NULL) {
1639 debug("X11 fwd already started.");
1640 return 0;
1641 }
1642
1643 debug("Received request for X11 forwarding with auth spoofing.");
1644 if (s->display != NULL)
1645 packet_disconnect("Protocol error: X11 display already set.");
1646
1647 s->single_connection = packet_get_char();
1648 s->auth_proto = packet_get_string(NULL);
1649 s->auth_data = packet_get_string(NULL);
1650 s->screen = packet_get_int();
1651 packet_done();
1652
1653 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1654 if (s->display == NULL) {
1655 xfree(s->auth_proto);
1656 xfree(s->auth_data);
1657 return 0;
1658 }
1659 xauthfile = xmalloc(MAXPATHLEN);
1660 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1661 temporarily_use_uid(s->pw->pw_uid);
1662 if (mkdtemp(xauthfile) == NULL) {
1663 restore_uid();
1664 error("private X11 dir: mkdtemp %s failed: %s",
1665 xauthfile, strerror(errno));
1666 xfree(xauthfile);
1667 xauthfile = NULL;
1668 xfree(s->auth_proto);
1669 xfree(s->auth_data);
1670 /* XXXX remove listening channels */
1671 return 0;
1672 }
1673 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001674 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1675 if (fd >= 0)
1676 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001677 restore_uid();
1678 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1679 return 1;
1680}
1681
Damien Millerf6d9e222000-06-18 14:50:44 +10001682int
1683session_shell_req(Session *s)
1684{
1685 /* if forced_command == NULL, the shell is execed */
1686 char *shell = forced_command;
1687 packet_done();
1688 s->extended = 1;
1689 if (s->ttyfd == -1)
1690 do_exec_no_pty(s, shell, s->pw);
1691 else
1692 do_exec_pty(s, shell, s->pw);
1693 return 1;
1694}
1695
1696int
1697session_exec_req(Session *s)
1698{
1699 unsigned int len;
1700 char *command = packet_get_string(&len);
1701 packet_done();
1702 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001703 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001704 command = forced_command;
1705 debug("Forced command '%.500s'", forced_command);
1706 }
1707 s->extended = 1;
1708 if (s->ttyfd == -1)
1709 do_exec_no_pty(s, command, s->pw);
1710 else
1711 do_exec_pty(s, command, s->pw);
1712 if (forced_command == NULL)
1713 xfree(command);
1714 return 1;
1715}
1716
Damien Millerefb4afe2000-04-12 18:45:05 +10001717void
1718session_input_channel_req(int id, void *arg)
1719{
1720 unsigned int len;
1721 int reply;
1722 int success = 0;
1723 char *rtype;
1724 Session *s;
1725 Channel *c;
1726
1727 rtype = packet_get_string(&len);
1728 reply = packet_get_char();
1729
1730 s = session_by_channel(id);
1731 if (s == NULL)
1732 fatal("session_input_channel_req: channel %d: no session", id);
1733 c = channel_lookup(id);
1734 if (c == NULL)
1735 fatal("session_input_channel_req: channel %d: bad channel", id);
1736
1737 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1738 s->self, id, rtype, reply);
1739
1740 /*
1741 * a session is in LARVAL state until a shell
1742 * or programm is executed
1743 */
1744 if (c->type == SSH_CHANNEL_LARVAL) {
1745 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001746 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001747 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001748 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001749 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001750 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001751 } else if (strcmp(rtype, "x11-req") == 0) {
1752 success = session_x11_req(s);
1753 } else if (strcmp(rtype, "subsystem") == 0) {
1754 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001755 }
1756 }
1757 if (strcmp(rtype, "window-change") == 0) {
1758 success = session_window_change_req(s);
1759 }
1760
1761 if (reply) {
1762 packet_start(success ?
1763 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1764 packet_put_int(c->remote_id);
1765 packet_send();
1766 }
1767 xfree(rtype);
1768}
1769
1770void
1771session_set_fds(Session *s, int fdin, int fdout, int fderr)
1772{
1773 if (!compat20)
1774 fatal("session_set_fds: called for proto != 2.0");
1775 /*
1776 * now that have a child and a pipe to the child,
1777 * we can activate our channel and register the fd's
1778 */
1779 if (s->chanid == -1)
1780 fatal("no channel for session %d", s->self);
1781 channel_set_fds(s->chanid,
1782 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001783 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1784 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001785}
1786
Damien Millerb38eff82000-04-01 11:09:21 +10001787void
1788session_pty_cleanup(Session *s)
1789{
1790 if (s == NULL || s->ttyfd == -1)
1791 return;
1792
1793 debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1794
1795 /* Cancel the cleanup function. */
1796 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1797
1798 /* Record that the user has logged out. */
1799 record_logout(s->pid, s->tty);
1800
1801 /* Release the pseudo-tty. */
1802 pty_release(s->tty);
1803
1804 /*
1805 * Close the server side of the socket pairs. We must do this after
1806 * the pty cleanup, so that another process doesn't get this pty
1807 * while we're still cleaning up.
1808 */
1809 if (close(s->ptymaster) < 0)
1810 error("close(s->ptymaster): %s", strerror(errno));
1811}
Damien Millerefb4afe2000-04-12 18:45:05 +10001812
1813void
1814session_exit_message(Session *s, int status)
1815{
1816 Channel *c;
1817 if (s == NULL)
1818 fatal("session_close: no session");
1819 c = channel_lookup(s->chanid);
1820 if (c == NULL)
1821 fatal("session_close: session %d: no channel %d",
1822 s->self, s->chanid);
1823 debug("session_exit_message: session %d channel %d pid %d",
1824 s->self, s->chanid, s->pid);
1825
1826 if (WIFEXITED(status)) {
1827 channel_request_start(s->chanid,
1828 "exit-status", 0);
1829 packet_put_int(WEXITSTATUS(status));
1830 packet_send();
1831 } else if (WIFSIGNALED(status)) {
1832 channel_request_start(s->chanid,
1833 "exit-signal", 0);
1834 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001835#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001836 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001837#else /* WCOREDUMP */
1838 packet_put_char(0);
1839#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001840 packet_put_cstring("");
1841 packet_put_cstring("");
1842 packet_send();
1843 } else {
1844 /* Some weird exit cause. Just exit. */
1845 packet_disconnect("wait returned status %04x.", status);
1846 }
1847
1848 /* disconnect channel */
1849 debug("session_exit_message: release channel %d", s->chanid);
1850 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001851 /*
1852 * emulate a write failure with 'chan_write_failed', nobody will be
1853 * interested in data we write.
1854 * Note that we must not call 'chan_read_failed', since there could
1855 * be some more data waiting in the pipe.
Damien Miller59939352000-10-15 12:21:32 +11001856 * djm - This is no longer true as we have allowed one pass through
1857 * the select loop before killing the connection
Damien Miller166fca82000-04-20 07:42:21 +10001858 */
Damien Millerbd483e72000-04-30 10:00:53 +10001859 if (c->ostate != CHAN_OUTPUT_CLOSED)
1860 chan_write_failed(c);
Damien Miller59939352000-10-15 12:21:32 +11001861 if (c->istate != CHAN_INPUT_CLOSED)
1862 chan_read_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001863 s->chanid = -1;
1864}
1865
1866void
1867session_free(Session *s)
1868{
1869 debug("session_free: session %d pid %d", s->self, s->pid);
1870 if (s->term)
1871 xfree(s->term);
1872 if (s->display)
1873 xfree(s->display);
1874 if (s->auth_data)
1875 xfree(s->auth_data);
1876 if (s->auth_proto)
1877 xfree(s->auth_proto);
1878 s->used = 0;
1879}
1880
1881void
1882session_close(Session *s)
1883{
1884 session_pty_cleanup(s);
1885 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001886 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001887}
1888
1889void
1890session_close_by_pid(pid_t pid, int status)
1891{
1892 Session *s = session_by_pid(pid);
1893 if (s == NULL) {
1894 debug("session_close_by_pid: no session for pid %d", s->pid);
1895 return;
1896 }
1897 if (s->chanid != -1)
1898 session_exit_message(s, status);
1899 session_close(s);
1900}
1901
1902/*
1903 * this is called when a channel dies before
1904 * the session 'child' itself dies
1905 */
1906void
1907session_close_by_channel(int id, void *arg)
1908{
1909 Session *s = session_by_channel(id);
1910 if (s == NULL) {
1911 debug("session_close_by_channel: no session for channel %d", id);
1912 return;
1913 }
1914 /* disconnect channel */
1915 channel_cancel_cleanup(s->chanid);
1916 s->chanid = -1;
1917
1918 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1919 if (s->pid == 0) {
1920 /* close session immediately */
1921 session_close(s);
1922 } else {
1923 /* notify child, delay session cleanup */
Damien Miller099f5052000-06-22 20:57:11 +10001924 if (s->pid <= 1)
Damien Miller7a445bb2000-06-26 13:12:37 +10001925 fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1926 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001927 error("session_close_by_channel: kill %d: %s",
1928 s->pid, strerror(errno));
1929 }
1930}
1931
Damien Millere247cc42000-05-07 12:03:14 +10001932char *
1933session_tty_list(void)
1934{
1935 static char buf[1024];
1936 int i;
1937 buf[0] = '\0';
1938 for(i = 0; i < MAX_SESSIONS; i++) {
1939 Session *s = &sessions[i];
1940 if (s->used && s->ttyfd != -1) {
1941 if (buf[0] != '\0')
1942 strlcat(buf, ",", sizeof buf);
1943 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1944 }
1945 }
1946 if (buf[0] == '\0')
1947 strlcpy(buf, "notty", sizeof buf);
1948 return buf;
1949}
1950
1951void
1952session_proctitle(Session *s)
1953{
1954 if (s->pw == NULL)
1955 error("no user for session %d", s->self);
1956 else
1957 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1958}
1959
Damien Millerefb4afe2000-04-12 18:45:05 +10001960void
1961do_authenticated2(void)
1962{
Damien Miller87d29ed2000-08-30 09:21:22 +11001963#ifdef HAVE_LOGIN_CAP
Damien Millerad833b32000-08-23 10:46:23 +10001964 struct passwd *pw;
Damien Miller87d29ed2000-08-30 09:21:22 +11001965#endif
Damien Millerad833b32000-08-23 10:46:23 +10001966
Damien Millerefb4afe2000-04-12 18:45:05 +10001967 /*
1968 * Cancel the alarm we set to limit the time taken for
1969 * authentication.
1970 */
1971 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10001972 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10001973 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10001974 startup_pipe = -1;
1975 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00001976#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +10001977 pw = auth_get_user();
1978 if ((lc = login_getclass(pw->pw_class)) == NULL) {
1979 error("unable to get login class");
1980 return;
1981 }
1982#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10001983 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10001984 if (xauthfile)
1985 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10001986}