blob: 006e4304d6f87abbc6d751921a5b993ef5949ae1 [file] [log] [blame]
Damien Millerd5fe0ba2006-08-30 11:07:39 +10001/* $OpenBSD: session.c,v 1.219 2006/08/29 10:40:19 djm Exp $ */
Damien Millerb38eff82000-04-01 11:09:21 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
Damien Millerefb4afe2000-04-12 18:45:05 +100012 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100034 */
Damien Millerb38eff82000-04-01 11:09:21 +100035
36#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110037
38#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100039#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110040#ifdef HAVE_SYS_STAT_H
41# include <sys/stat.h>
42#endif
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/un.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100045#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
Damien Miller1cdde6f2006-07-24 14:07:35 +100047#include <arpa/inet.h>
48
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller427a1d52006-07-10 20:20:33 +100050#include <grp.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110051#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110052#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110053#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100054#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110055#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100056#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100057#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100058#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100059#include <unistd.h>
Damien Millerb38eff82000-04-01 11:09:21 +100060
Damien Millerd7834352006-08-05 12:39:39 +100061#include "xmalloc.h"
Damien Millerb38eff82000-04-01 11:09:21 +100062#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "ssh1.h"
64#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000065#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100066#include "packet.h"
67#include "buffer.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100068#include "match.h"
Damien Millerb38eff82000-04-01 11:09:21 +100069#include "uidswap.h"
70#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000071#include "channels.h"
Damien Millerd7834352006-08-05 12:39:39 +100072#include "key.h"
73#include "cipher.h"
74#ifdef GSSAPI
75#include "ssh-gss.h"
76#endif
77#include "hostfile.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100078#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100079#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "pathnames.h"
81#include "log.h"
82#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000083#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084#include "serverloop.h"
85#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000086#include "session.h"
Damien Miller9786e6e2005-07-26 21:54:56 +100087#include "kex.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088#include "monitor_wrap.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100089
Darren Tucker3c78c5e2004-01-23 22:03:10 +110090#if defined(KRB5) && defined(USE_AFS)
Damien Miller8f341f82004-01-21 11:00:46 +110091#include <kafs.h>
92#endif
93
Damien Millerb38eff82000-04-01 11:09:21 +100094/* func */
95
96Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +000097void session_set_fds(Session *, int, int, int);
Darren Tucker3e33cec2003-10-02 16:12:36 +100098void session_pty_cleanup(Session *);
Kevin Steves8f63caa2001-07-04 18:23:02 +000099void session_proctitle(Session *);
100int session_setup_x11fwd(Session *);
101void do_exec_pty(Session *, const char *);
102void do_exec_no_pty(Session *, const char *);
103void do_exec(Session *, const char *);
104void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000105#ifdef LOGIN_NEEDS_UTMPX
106static void do_pre_login(Session *s);
107#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000108void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000109void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000110int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000111
Ben Lindstrombba81212001-06-25 05:01:22 +0000112static void do_authenticated1(Authctxt *);
113static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000114
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000116
Damien Millerb38eff82000-04-01 11:09:21 +1000117/* import */
118extern ServerOptions options;
119extern char *__progname;
120extern int log_stderr;
121extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000122extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000123extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000124extern void destroy_sensitive_data(void);
Darren Tuckerb9aa0a02003-07-08 22:59:59 +1000125extern Buffer loginmsg;
Damien Miller37023962000-07-11 17:31:38 +1000126
Damien Miller7b28dc52000-09-05 13:34:53 +1100127/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000128const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100129
Damien Millerb38eff82000-04-01 11:09:21 +1000130/* data */
131#define MAX_SESSIONS 10
132Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100133
Damien Millerad833b32000-08-23 10:46:23 +1000134#ifdef HAVE_LOGIN_CAP
Ben Lindstromb481e132002-03-22 01:35:47 +0000135login_cap_t *lc;
Damien Millerad833b32000-08-23 10:46:23 +1000136#endif
137
Darren Tucker3e33cec2003-10-02 16:12:36 +1000138static int is_child = 0;
139
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000140/* Name and directory of socket for authentication agent forwarding. */
141static char *auth_sock_name = NULL;
142static char *auth_sock_dir = NULL;
143
144/* removes the agent forwarding socket */
145
146static void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000147auth_sock_cleanup_proc(struct passwd *pw)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000148{
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000149 if (auth_sock_name != NULL) {
150 temporarily_use_uid(pw);
151 unlink(auth_sock_name);
152 rmdir(auth_sock_dir);
153 auth_sock_name = NULL;
154 restore_uid();
155 }
156}
157
158static int
159auth_input_request_forwarding(struct passwd * pw)
160{
161 Channel *nc;
162 int sock;
163 struct sockaddr_un sunaddr;
164
165 if (auth_sock_name != NULL) {
166 error("authentication forwarding requested twice.");
167 return 0;
168 }
169
170 /* Temporarily drop privileged uid for mkdir/bind. */
171 temporarily_use_uid(pw);
172
173 /* Allocate a buffer for the socket name, and format the name. */
174 auth_sock_name = xmalloc(MAXPATHLEN);
175 auth_sock_dir = xmalloc(MAXPATHLEN);
Darren Tucker072a7b12003-10-15 16:10:25 +1000176 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000177
178 /* Create private directory for socket */
179 if (mkdtemp(auth_sock_dir) == NULL) {
180 packet_send_debug("Agent forwarding disabled: "
181 "mkdtemp() failed: %.100s", strerror(errno));
182 restore_uid();
183 xfree(auth_sock_name);
184 xfree(auth_sock_dir);
185 auth_sock_name = NULL;
186 auth_sock_dir = NULL;
187 return 0;
188 }
Ben Lindstromce0f6342002-06-11 16:42:49 +0000189 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
190 auth_sock_dir, (long) getpid());
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000191
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000192 /* Create the socket. */
193 sock = socket(AF_UNIX, SOCK_STREAM, 0);
194 if (sock < 0)
195 packet_disconnect("socket: %.100s", strerror(errno));
196
197 /* Bind it to the name. */
198 memset(&sunaddr, 0, sizeof(sunaddr));
199 sunaddr.sun_family = AF_UNIX;
200 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
201
Damien Miller90967402006-03-26 14:07:26 +1100202 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000203 packet_disconnect("bind: %.100s", strerror(errno));
204
205 /* Restore the privileged uid. */
206 restore_uid();
207
208 /* Start listening on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +1100209 if (listen(sock, SSH_LISTEN_BACKLOG) < 0)
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000210 packet_disconnect("listen: %.100s", strerror(errno));
211
212 /* Allocate a channel for the authentication agent socket. */
213 nc = channel_new("auth socket",
214 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
215 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000216 0, "auth socket", 1);
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000217 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
218 return 1;
219}
220
Darren Tucker1921ed92004-02-10 13:23:28 +1100221static void
222display_loginmsg(void)
223{
Damien Miller46d38de2005-07-17 17:02:09 +1000224 if (buffer_len(&loginmsg) > 0) {
225 buffer_append(&loginmsg, "\0", 1);
226 printf("%s", (char *)buffer_ptr(&loginmsg));
227 buffer_clear(&loginmsg);
228 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100229}
Ben Lindstrom8bb6f362002-06-11 15:59:02 +0000230
Ben Lindstromb31783d2001-03-22 02:02:12 +0000231void
232do_authenticated(Authctxt *authctxt)
233{
Damien Miller97f39ae2003-02-24 11:57:01 +1100234 setproctitle("%s", authctxt->pw->pw_name);
235
Ben Lindstromb31783d2001-03-22 02:02:12 +0000236 /* setup the channel layer */
237 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
238 channel_permit_all_opens();
239
240 if (compat20)
241 do_authenticated2(authctxt);
242 else
243 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000244
Darren Tucker3e33cec2003-10-02 16:12:36 +1000245 do_cleanup(authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000246}
247
Damien Millerb38eff82000-04-01 11:09:21 +1000248/*
Damien Millerb38eff82000-04-01 11:09:21 +1000249 * Prepares for an interactive session. This is called after the user has
250 * been successfully authenticated. During this message exchange, pseudo
251 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
252 * are requested, etc.
253 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000254static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000255do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000256{
257 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000258 char *command;
Damien Millerdff50992002-01-22 23:16:32 +1100259 int success, type, screen_flag;
Ben Lindstrome23f4a32002-06-23 21:40:16 +0000260 int enable_compression_after_reply = 0;
261 u_int proto_len, data_len, dlen, compression_level = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000262
263 s = session_new();
Darren Tucker172a5e82005-01-20 10:55:46 +1100264 if (s == NULL) {
265 error("no more sessions");
266 return;
267 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000268 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000269 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000270
Damien Millerb38eff82000-04-01 11:09:21 +1000271 /*
272 * We stay in this loop until the client requests to execute a shell
273 * or a command.
274 */
275 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000276 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000277
278 /* Get a packet from the client. */
Damien Millerdff50992002-01-22 23:16:32 +1100279 type = packet_read();
Damien Millerb38eff82000-04-01 11:09:21 +1000280
281 /* Process the packet. */
282 switch (type) {
283 case SSH_CMSG_REQUEST_COMPRESSION:
Damien Millerb38eff82000-04-01 11:09:21 +1000284 compression_level = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100285 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +1000286 if (compression_level < 1 || compression_level > 9) {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000287 packet_send_debug("Received invalid compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100288 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000289 break;
290 }
Damien Miller9786e6e2005-07-26 21:54:56 +1000291 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000292 debug2("compression disabled");
293 break;
294 }
Damien Millerb38eff82000-04-01 11:09:21 +1000295 /* Enable compression after we have responded with SUCCESS. */
296 enable_compression_after_reply = 1;
297 success = 1;
298 break;
299
300 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000301 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000302 break;
303
304 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000305 s->auth_proto = packet_get_string(&proto_len);
306 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000307
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000308 screen_flag = packet_get_protocol_flags() &
309 SSH_PROTOFLAG_SCREEN_NUMBER;
310 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
311
312 if (packet_remaining() == 4) {
313 if (!screen_flag)
314 debug2("Buggy client: "
315 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000316 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000317 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000318 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000319 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100320 packet_check_eom();
Ben Lindstrom768176b2001-06-09 01:29:12 +0000321 success = session_setup_x11fwd(s);
322 if (!success) {
323 xfree(s->auth_proto);
324 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000325 s->auth_proto = NULL;
326 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000327 }
Damien Millerb38eff82000-04-01 11:09:21 +1000328 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000329
330 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
331 if (no_agent_forwarding_flag || compat13) {
332 debug("Authentication agent forwarding not permitted for this authentication.");
333 break;
334 }
335 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000336 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000337 break;
338
339 case SSH_CMSG_PORT_FORWARD_REQUEST:
340 if (no_port_forwarding_flag) {
341 debug("Port forwarding not permitted for this authentication.");
342 break;
343 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100344 if (!options.allow_tcp_forwarding) {
345 debug("Port forwarding not permitted.");
346 break;
347 }
Damien Millerb38eff82000-04-01 11:09:21 +1000348 debug("Received TCP/IP port forwarding request.");
Darren Tuckere7d4b192006-07-12 22:17:10 +1000349 if (channel_input_port_forward_request(s->pw->pw_uid == 0,
350 options.gateway_ports) < 0) {
351 debug("Port forwarding failed.");
352 break;
353 }
Damien Millerb38eff82000-04-01 11:09:21 +1000354 success = 1;
355 break;
356
357 case SSH_CMSG_MAX_PACKET_SIZE:
358 if (packet_set_maxsize(packet_get_int()) > 0)
359 success = 1;
360 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100361
Damien Millerb38eff82000-04-01 11:09:21 +1000362 case SSH_CMSG_EXEC_SHELL:
363 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000364 if (type == SSH_CMSG_EXEC_CMD) {
365 command = packet_get_string(&dlen);
366 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000367 do_exec(s, command);
368 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000369 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000370 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000371 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100372 packet_check_eom();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000373 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000374 return;
375
376 default:
377 /*
378 * Any unknown messages in this phase are ignored,
379 * and a failure message is returned.
380 */
Damien Miller996acd22003-04-09 20:59:48 +1000381 logit("Unknown packet type received after authentication: %d", type);
Damien Millerb38eff82000-04-01 11:09:21 +1000382 }
383 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
384 packet_send();
385 packet_write_wait();
386
387 /* Enable compression now that we have replied if appropriate. */
388 if (enable_compression_after_reply) {
389 enable_compression_after_reply = 0;
390 packet_start_compression(compression_level);
391 }
392 }
393}
394
395/*
396 * This is called to fork and execute a command when we have no tty. This
397 * will call do_child from the child, and server_loop from the parent after
398 * setting up file descriptors and such.
399 */
Damien Miller4af51302000-04-16 11:18:38 +1000400void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000401do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000402{
Ben Lindstromce0f6342002-06-11 16:42:49 +0000403 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000404
405#ifdef USE_PIPES
406 int pin[2], pout[2], perr[2];
407 /* Allocate pipes for communicating with the program. */
408 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
409 packet_disconnect("Could not create pipes: %.100s",
410 strerror(errno));
411#else /* USE_PIPES */
412 int inout[2], err[2];
413 /* Uses socket pairs to communicate with the program. */
414 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
415 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
416 packet_disconnect("Could not create socket pairs: %.100s",
417 strerror(errno));
418#endif /* USE_PIPES */
419 if (s == NULL)
420 fatal("do_exec_no_pty: no session");
421
Damien Millere247cc42000-05-07 12:03:14 +1000422 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000423
Damien Millerc5946332001-02-28 11:46:11 +1100424#if defined(USE_PAM)
Darren Tucker1825f262004-02-24 00:01:27 +1100425 if (options.use_pam && !use_privsep)
Damien Miller4e448a32003-05-14 15:11:48 +1000426 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000427#endif /* USE_PAM */
428
Damien Millerb38eff82000-04-01 11:09:21 +1000429 /* Fork the child. */
430 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000431 is_child = 1;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000432
Damien Millerb38eff82000-04-01 11:09:21 +1000433 /* Child. Reinitialize the log since the pid has changed. */
434 log_init(__progname, options.log_level, options.log_facility, log_stderr);
435
436 /*
437 * Create a new session and process group since the 4.4BSD
438 * setlogin() affects the entire process group.
439 */
440 if (setsid() < 0)
441 error("setsid failed: %.100s", strerror(errno));
442
443#ifdef USE_PIPES
444 /*
445 * Redirect stdin. We close the parent side of the socket
446 * pair, and make the child side the standard input.
447 */
448 close(pin[1]);
449 if (dup2(pin[0], 0) < 0)
450 perror("dup2 stdin");
451 close(pin[0]);
452
453 /* Redirect stdout. */
454 close(pout[0]);
455 if (dup2(pout[1], 1) < 0)
456 perror("dup2 stdout");
457 close(pout[1]);
458
459 /* Redirect stderr. */
460 close(perr[0]);
461 if (dup2(perr[1], 2) < 0)
462 perror("dup2 stderr");
463 close(perr[1]);
464#else /* USE_PIPES */
465 /*
466 * Redirect stdin, stdout, and stderr. Stdin and stdout will
467 * use the same socket, as some programs (particularly rdist)
468 * seem to depend on it.
469 */
470 close(inout[1]);
471 close(err[1]);
472 if (dup2(inout[0], 0) < 0) /* stdin */
473 perror("dup2 stdin");
474 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
475 perror("dup2 stdout");
476 if (dup2(err[0], 2) < 0) /* stderr */
477 perror("dup2 stderr");
478#endif /* USE_PIPES */
479
Tim Rice81ed5182002-09-25 17:38:46 -0700480#ifdef _UNICOS
481 cray_init_job(s->pw); /* set up cray jid and tmpdir */
482#endif
483
Damien Millerb38eff82000-04-01 11:09:21 +1000484 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000485 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000486 /* NOTREACHED */
487 }
Tim Rice81ed5182002-09-25 17:38:46 -0700488#ifdef _UNICOS
489 signal(WJSIGNAL, cray_job_termination_handler);
490#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100491#ifdef HAVE_CYGWIN
492 if (is_winnt)
493 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
494#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000495 if (pid < 0)
496 packet_disconnect("fork failed: %.100s", strerror(errno));
497 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000498 /* Set interactive/non-interactive mode. */
499 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000500#ifdef USE_PIPES
501 /* We are the parent. Close the child sides of the pipes. */
502 close(pin[0]);
503 close(pout[1]);
504 close(perr[1]);
505
Damien Millerefb4afe2000-04-12 18:45:05 +1000506 if (compat20) {
Darren Tucker723e9452004-06-22 12:57:08 +1000507 if (s->is_subsystem) {
508 close(perr[0]);
509 perr[0] = -1;
510 }
511 session_set_fds(s, pin[1], pout[0], perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000512 } else {
513 /* Enter the interactive session. */
514 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000515 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000516 }
Damien Millerb38eff82000-04-01 11:09:21 +1000517#else /* USE_PIPES */
518 /* We are the parent. Close the child sides of the socket pairs. */
519 close(inout[0]);
520 close(err[0]);
521
522 /*
Darren Tuckerb3850592004-03-27 16:44:21 +1100523 * Clear loginmsg, since it's the child's responsibility to display
524 * it to the user, otherwise multiple sessions may accumulate
525 * multiple copies of the login messages.
526 */
527 buffer_clear(&loginmsg);
528
529 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000530 * Enter the interactive session. Note: server_loop must be able to
531 * handle the case that fdin and fdout are the same.
532 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000533 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000534 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000535 } else {
536 server_loop(pid, inout[1], inout[1], err[1]);
537 /* server_loop has closed inout[1] and err[1]. */
538 }
Damien Millerb38eff82000-04-01 11:09:21 +1000539#endif /* USE_PIPES */
540}
541
542/*
543 * This is called to fork and execute a command when we have a tty. This
544 * will call do_child from the child, and server_loop from the parent after
545 * setting up file descriptors, controlling tty, updating wtmp, utmp,
546 * lastlog, and other such operations.
547 */
Damien Miller4af51302000-04-16 11:18:38 +1000548void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000549do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000550{
Damien Millerb38eff82000-04-01 11:09:21 +1000551 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000552 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000553
554 if (s == NULL)
555 fatal("do_exec_pty: no session");
556 ptyfd = s->ptyfd;
557 ttyfd = s->ttyfd;
558
Damien Millerc5946332001-02-28 11:46:11 +1100559#if defined(USE_PAM)
Damien Miller4e448a32003-05-14 15:11:48 +1000560 if (options.use_pam) {
Damien Miller341c6e62003-09-02 23:18:52 +1000561 do_pam_set_tty(s->tty);
Darren Tuckeref3a4a22004-02-06 15:30:50 +1100562 if (!use_privsep)
563 do_pam_setcred(1);
Damien Miller4e448a32003-05-14 15:11:48 +1000564 }
Damien Miller5a761312001-02-27 09:28:23 +1100565#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000566
Damien Millerb38eff82000-04-01 11:09:21 +1000567 /* Fork the child. */
568 if ((pid = fork()) == 0) {
Darren Tucker3e33cec2003-10-02 16:12:36 +1000569 is_child = 1;
Ben Lindstrombddd5512001-07-04 04:53:53 +0000570
Damien Miller942da032000-08-18 13:59:06 +1000571 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000572 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000573 /* Close the master side of the pseudo tty. */
574 close(ptyfd);
575
576 /* Make the pseudo tty our controlling tty. */
577 pty_make_controlling_tty(&ttyfd, s->tty);
578
Damien Miller9c751422001-10-10 15:02:46 +1000579 /* Redirect stdin/stdout/stderr from the pseudo tty. */
580 if (dup2(ttyfd, 0) < 0)
581 error("dup2 stdin: %s", strerror(errno));
582 if (dup2(ttyfd, 1) < 0)
583 error("dup2 stdout: %s", strerror(errno));
584 if (dup2(ttyfd, 2) < 0)
585 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000586
587 /* Close the extra descriptor for the pseudo tty. */
588 close(ttyfd);
589
Damien Miller942da032000-08-18 13:59:06 +1000590 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000591#ifndef HAVE_OSF_SIA
Tim Rice81ed5182002-09-25 17:38:46 -0700592 if (!(options.use_login && command == NULL)) {
593#ifdef _UNICOS
594 cray_init_job(s->pw); /* set up cray jid and tmpdir */
595#endif /* _UNICOS */
Damien Miller69b69aa2000-10-28 14:19:58 +1100596 do_login(s, command);
Tim Rice81ed5182002-09-25 17:38:46 -0700597 }
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000598# ifdef LOGIN_NEEDS_UTMPX
599 else
600 do_pre_login(s);
601# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000602#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000603
Damien Millerb38eff82000-04-01 11:09:21 +1000604 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000605 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000606 /* NOTREACHED */
607 }
Tim Rice81ed5182002-09-25 17:38:46 -0700608#ifdef _UNICOS
609 signal(WJSIGNAL, cray_job_termination_handler);
610#endif /* _UNICOS */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100611#ifdef HAVE_CYGWIN
612 if (is_winnt)
613 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
614#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000615 if (pid < 0)
616 packet_disconnect("fork failed: %.100s", strerror(errno));
617 s->pid = pid;
618
619 /* Parent. Close the slave side of the pseudo tty. */
620 close(ttyfd);
621
622 /*
623 * Create another descriptor of the pty master side for use as the
624 * standard input. We could use the original descriptor, but this
625 * simplifies code in server_loop. The descriptor is bidirectional.
626 */
627 fdout = dup(ptyfd);
628 if (fdout < 0)
629 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
630
631 /* we keep a reference to the pty master */
632 ptymaster = dup(ptyfd);
633 if (ptymaster < 0)
634 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
635 s->ptymaster = ptymaster;
636
637 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000638 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000639 if (compat20) {
640 session_set_fds(s, ptyfd, fdout, -1);
641 } else {
642 server_loop(pid, ptyfd, fdout, -1);
643 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000644 }
Damien Millerb38eff82000-04-01 11:09:21 +1000645}
646
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000647#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000648static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000649do_pre_login(Session *s)
650{
651 socklen_t fromlen;
652 struct sockaddr_storage from;
653 pid_t pid = getpid();
654
655 /*
656 * Get IP address of client. If the connection is not a socket, let
657 * the address be 0.0.0.0.
658 */
659 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +1000660 fromlen = sizeof(from);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000661 if (packet_connection_is_on_socket()) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000662 if (getpeername(packet_get_connection_in(),
Damien Miller90967402006-03-26 14:07:26 +1100663 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000664 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000665 cleanup_exit(255);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000666 }
667 }
668
669 record_utmp_only(pid, s->tty, s->pw->pw_name,
Damien Miller3a961dc2003-06-03 10:25:48 +1000670 get_remote_name_or_ip(utmp_len, options.use_dns),
Kevin Steves678ee512003-01-01 23:43:55 +0000671 (struct sockaddr *)&from, fromlen);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000672}
673#endif
674
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000675/*
676 * This is called to fork and execute a command. If another command is
677 * to be forced, execute that instead.
678 */
679void
680do_exec(Session *s, const char *command)
681{
Damien Millere2754432006-07-24 14:06:47 +1000682 if (options.adm_forced_command) {
683 original_command = command;
684 command = options.adm_forced_command;
685 debug("Forced command (config) '%.900s'", command);
686 } else if (forced_command) {
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000687 original_command = command;
688 command = forced_command;
Damien Millere2754432006-07-24 14:06:47 +1000689 debug("Forced command (key option) '%.900s'", command);
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000690 }
691
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100692#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100693 if (command != NULL)
694 PRIVSEP(audit_run_command(command));
695 else if (s->ttyfd == -1) {
696 char *shell = s->pw->pw_shell;
697
698 if (shell[0] == '\0') /* empty shell means /bin/sh */
699 shell =_PATH_BSHELL;
700 PRIVSEP(audit_run_command(shell));
701 }
702#endif
703
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000704 if (s->ttyfd != -1)
705 do_exec_pty(s, command);
706 else
707 do_exec_no_pty(s, command);
708
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000709 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000710
Darren Tucker09991742004-07-17 17:05:14 +1000711 /*
712 * Clear loginmsg: it's the child's responsibility to display
713 * it to the user, otherwise multiple sessions may accumulate
714 * multiple copies of the login messages.
715 */
716 buffer_clear(&loginmsg);
717}
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000718
Damien Miller942da032000-08-18 13:59:06 +1000719/* administrative, login(1)-like work */
720void
Damien Miller69b69aa2000-10-28 14:19:58 +1100721do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000722{
Damien Miller942da032000-08-18 13:59:06 +1000723 socklen_t fromlen;
724 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000725 struct passwd * pw = s->pw;
726 pid_t pid = getpid();
727
728 /*
729 * Get IP address of client. If the connection is not a socket, let
730 * the address be 0.0.0.0.
731 */
732 memset(&from, 0, sizeof(from));
Kevin Steves678ee512003-01-01 23:43:55 +0000733 fromlen = sizeof(from);
Damien Miller942da032000-08-18 13:59:06 +1000734 if (packet_connection_is_on_socket()) {
Damien Miller942da032000-08-18 13:59:06 +1000735 if (getpeername(packet_get_connection_in(),
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000736 (struct sockaddr *) & from, &fromlen) < 0) {
Damien Miller942da032000-08-18 13:59:06 +1000737 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +1000738 cleanup_exit(255);
Damien Miller942da032000-08-18 13:59:06 +1000739 }
740 }
741
742 /* Record that there was a login on that tty from the remote host. */
Ben Lindstrom2bf56e22002-04-02 20:32:46 +0000743 if (!use_privsep)
744 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
745 get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +1000746 options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +1000747 (struct sockaddr *)&from, fromlen);
Damien Miller942da032000-08-18 13:59:06 +1000748
Kevin Steves092f2ef2000-10-14 13:36:13 +0000749#ifdef USE_PAM
750 /*
751 * If password change is needed, do it now.
752 * This needs to occur before the ~/.hushlogin check.
753 */
Darren Tucker1921ed92004-02-10 13:23:28 +1100754 if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) {
755 display_loginmsg();
Kevin Steves092f2ef2000-10-14 13:36:13 +0000756 do_pam_chauthtok();
Darren Tucker1921ed92004-02-10 13:23:28 +1100757 s->authctxt->force_pwchange = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000758 /* XXX - signal [net] parent to enable forwardings */
Kevin Steves092f2ef2000-10-14 13:36:13 +0000759 }
760#endif
761
Damien Millercf205e82001-04-16 18:29:15 +1000762 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000763 return;
764
Darren Tucker1921ed92004-02-10 13:23:28 +1100765 display_loginmsg();
Damien Miller942da032000-08-18 13:59:06 +1000766
Damien Millercf205e82001-04-16 18:29:15 +1000767 do_motd();
768}
769
770/*
771 * Display the message of the day.
772 */
773void
774do_motd(void)
775{
776 FILE *f;
777 char buf[256];
778
Damien Miller942da032000-08-18 13:59:06 +1000779 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000780#ifdef HAVE_LOGIN_CAP
781 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
782 "/etc/motd"), "r");
783#else
Damien Miller942da032000-08-18 13:59:06 +1000784 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000785#endif
Damien Miller942da032000-08-18 13:59:06 +1000786 if (f) {
787 while (fgets(buf, sizeof(buf), f))
788 fputs(buf, stdout);
789 fclose(f);
790 }
791 }
792}
793
Kevin Steves8f63caa2001-07-04 18:23:02 +0000794
795/*
796 * Check for quiet login, either .hushlogin or command given.
797 */
798int
799check_quietlogin(Session *s, const char *command)
800{
801 char buf[256];
802 struct passwd *pw = s->pw;
803 struct stat st;
804
805 /* Return 1 if .hushlogin exists or a command given. */
806 if (command != NULL)
807 return 1;
808 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
809#ifdef HAVE_LOGIN_CAP
810 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
811 return 1;
812#else
813 if (stat(buf, &st) >= 0)
814 return 1;
815#endif
816 return 0;
817}
818
Damien Millerb38eff82000-04-01 11:09:21 +1000819/*
820 * Sets the value of the given variable in the environment. If the variable
821 * already exists, its value is overriden.
822 */
Darren Tucker0efd1552003-08-26 11:49:55 +1000823void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000824child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100825 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000826{
Damien Millerb38eff82000-04-01 11:09:21 +1000827 char **env;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000828 u_int envsize;
829 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000830
831 /*
Darren Tuckere1a790d2003-09-16 11:52:19 +1000832 * If we're passed an uninitialized list, allocate a single null
833 * entry before continuing.
834 */
835 if (*envp == NULL && *envsizep == 0) {
836 *envp = xmalloc(sizeof(char *));
837 *envp[0] = NULL;
838 *envsizep = 1;
839 }
840
841 /*
Damien Millerb38eff82000-04-01 11:09:21 +1000842 * Find the slot where the value should be stored. If the variable
843 * already exists, we reuse the slot; otherwise we append a new slot
844 * at the end of the array, expanding if necessary.
845 */
846 env = *envp;
847 namelen = strlen(name);
848 for (i = 0; env[i]; i++)
849 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
850 break;
851 if (env[i]) {
852 /* Reuse the slot. */
853 xfree(env[i]);
854 } else {
855 /* New variable. Expand if necessary. */
Darren Tuckerfb16b242003-09-22 21:04:23 +1000856 envsize = *envsizep;
857 if (i >= envsize - 1) {
858 if (envsize >= 1000)
859 fatal("child_set_env: too many env vars");
860 envsize += 50;
Damien Miller36812092006-03-26 14:22:47 +1100861 env = (*envp) = xrealloc(env, envsize, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000862 *envsizep = envsize;
Damien Millerb38eff82000-04-01 11:09:21 +1000863 }
864 /* Need to set the NULL pointer at end of array beyond the new slot. */
865 env[i + 1] = NULL;
866 }
867
868 /* Allocate space and format the variable in the appropriate slot. */
869 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
870 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
871}
872
873/*
874 * Reads environment variables from the given file and adds/overrides them
875 * into the environment. If the file does not exist, this does nothing.
876 * Otherwise, it must consist of empty lines, comments (line starts with '#')
877 * and assignments of the form name=value. No other forms are allowed.
878 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000879static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000880read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100881 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000882{
883 FILE *f;
884 char buf[4096];
885 char *cp, *value;
Damien Miller990070a2002-06-26 23:51:06 +1000886 u_int lineno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000887
888 f = fopen(filename, "r");
889 if (!f)
890 return;
891
892 while (fgets(buf, sizeof(buf), f)) {
Damien Miller990070a2002-06-26 23:51:06 +1000893 if (++lineno > 1000)
894 fatal("Too many lines in environment file %s", filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000895 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
896 ;
897 if (!*cp || *cp == '#' || *cp == '\n')
898 continue;
899 if (strchr(cp, '\n'))
900 *strchr(cp, '\n') = '\0';
901 value = strchr(cp, '=');
902 if (value == NULL) {
Damien Miller990070a2002-06-26 23:51:06 +1000903 fprintf(stderr, "Bad line %u in %.100s\n", lineno,
904 filename);
Damien Millerb38eff82000-04-01 11:09:21 +1000905 continue;
906 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000907 /*
908 * Replace the equals sign by nul, and advance value to
909 * the value string.
910 */
Damien Millerb38eff82000-04-01 11:09:21 +1000911 *value = '\0';
912 value++;
913 child_set_env(env, envsize, cp, value);
914 }
915 fclose(f);
916}
917
Darren Tuckere1a790d2003-09-16 11:52:19 +1000918#ifdef HAVE_ETC_DEFAULT_LOGIN
919/*
920 * Return named variable from specified environment, or NULL if not present.
921 */
922static char *
923child_get_env(char **env, const char *name)
924{
925 int i;
926 size_t len;
927
928 len = strlen(name);
929 for (i=0; env[i] != NULL; i++)
930 if (strncmp(name, env[i], len) == 0 && env[i][len] == '=')
931 return(env[i] + len + 1);
932 return NULL;
933}
934
935/*
936 * Read /etc/default/login.
937 * We pick up the PATH (or SUPATH for root) and UMASK.
938 */
939static void
940read_etc_default_login(char ***env, u_int *envsize, uid_t uid)
941{
942 char **tmpenv = NULL, *var;
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000943 u_int i, tmpenvsize = 0;
Darren Tuckerf391ba62003-10-02 20:07:09 +1000944 u_long mask;
Darren Tuckere1a790d2003-09-16 11:52:19 +1000945
946 /*
947 * We don't want to copy the whole file to the child's environment,
948 * so we use a temporary environment and copy the variables we're
949 * interested in.
950 */
951 read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login");
952
Darren Tuckerc11b1e82003-09-19 20:56:51 +1000953 if (tmpenv == NULL)
954 return;
955
Darren Tuckere1a790d2003-09-16 11:52:19 +1000956 if (uid == 0)
957 var = child_get_env(tmpenv, "SUPATH");
958 else
959 var = child_get_env(tmpenv, "PATH");
960 if (var != NULL)
961 child_set_env(env, envsize, "PATH", var);
Damien Miller787b2ec2003-11-21 23:56:47 +1100962
Darren Tuckere1a790d2003-09-16 11:52:19 +1000963 if ((var = child_get_env(tmpenv, "UMASK")) != NULL)
964 if (sscanf(var, "%5lo", &mask) == 1)
Darren Tuckerf391ba62003-10-02 20:07:09 +1000965 umask((mode_t)mask);
Damien Miller787b2ec2003-11-21 23:56:47 +1100966
Darren Tuckere1a790d2003-09-16 11:52:19 +1000967 for (i = 0; tmpenv[i] != NULL; i++)
968 xfree(tmpenv[i]);
969 xfree(tmpenv);
970}
971#endif /* HAVE_ETC_DEFAULT_LOGIN */
972
Damien Miller7dff8692005-05-26 11:34:51 +1000973void
974copy_environment(char **source, char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000975{
Damien Millerbb9ffc12002-01-08 10:59:32 +1100976 char *var_name, *var_val;
Damien Millerb38eff82000-04-01 11:09:21 +1000977 int i;
978
Damien Millerbb9ffc12002-01-08 10:59:32 +1100979 if (source == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +1000980 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000981
Damien Millerbb9ffc12002-01-08 10:59:32 +1100982 for(i = 0; source[i] != NULL; i++) {
983 var_name = xstrdup(source[i]);
984 if ((var_val = strstr(var_name, "=")) == NULL) {
985 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000986 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000987 }
Damien Millerbb9ffc12002-01-08 10:59:32 +1100988 *var_val++ = '\0';
989
990 debug3("Copy environment: %s=%s", var_name, var_val);
991 child_set_env(env, envsize, var_name, var_val);
Damien Miller787b2ec2003-11-21 23:56:47 +1100992
Damien Millerbb9ffc12002-01-08 10:59:32 +1100993 xfree(var_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000994 }
995}
Damien Millercb5e44a2000-09-29 12:12:36 +1100996
Ben Lindstrom4e97e852002-02-19 21:50:43 +0000997static char **
998do_setup_env(Session *s, const char *shell)
Damien Millerb38eff82000-04-01 11:09:21 +1000999{
Damien Millerb38eff82000-04-01 11:09:21 +10001000 char buf[256];
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001001 u_int i, envsize;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001002 char **env, *laddr;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001003 struct passwd *pw = s->pw;
Damien Millerad5ecbf2006-07-24 15:03:06 +10001004#ifndef HAVE_LOGIN_CAP
1005 char *path = NULL;
1006#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001007
Damien Millerb38eff82000-04-01 11:09:21 +10001008 /* Initialize the environment. */
1009 envsize = 100;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001010 env = xcalloc(envsize, sizeof(char *));
Damien Millerb38eff82000-04-01 11:09:21 +10001011 env[0] = NULL;
1012
Damien Millerbac2d8a2000-09-05 16:13:06 +11001013#ifdef HAVE_CYGWIN
1014 /*
1015 * The Windows environment contains some setting which are
1016 * important for a running system. They must not be dropped.
1017 */
Darren Tucker14c372d2004-08-30 20:42:08 +10001018 {
1019 char **p;
1020
1021 p = fetch_windows_environment();
1022 copy_environment(p, &env, &envsize);
1023 free_windows_environment(p);
1024 }
Damien Millerbac2d8a2000-09-05 16:13:06 +11001025#endif
1026
Darren Tucker0efd1552003-08-26 11:49:55 +10001027#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +11001028 /* Allow any GSSAPI methods that we've used to alter
Darren Tucker0efd1552003-08-26 11:49:55 +10001029 * the childs environment as they see fit
1030 */
1031 ssh_gssapi_do_child(&env, &envsize);
1032#endif
1033
Damien Millerb38eff82000-04-01 11:09:21 +10001034 if (!options.use_login) {
1035 /* Set basic environment. */
Darren Tucker46bc0752004-05-02 22:11:30 +10001036 for (i = 0; i < s->num_env; i++)
Darren Tuckerfc959702004-07-17 16:12:08 +10001037 child_set_env(&env, &envsize, s->env[i].name,
Darren Tucker46bc0752004-05-02 22:11:30 +10001038 s->env[i].val);
1039
Damien Millerb38eff82000-04-01 11:09:21 +10001040 child_set_env(&env, &envsize, "USER", pw->pw_name);
1041 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
Damien Millerdfedbf82003-01-03 14:52:53 +11001042#ifdef _AIX
1043 child_set_env(&env, &envsize, "LOGIN", pw->pw_name);
1044#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001045 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001046#ifdef HAVE_LOGIN_CAP
Ben Lindstromb9051ec2002-07-23 21:11:09 +00001047 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1048 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1049 else
1050 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001051#else /* HAVE_LOGIN_CAP */
1052# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001053 /*
1054 * There's no standard path on Windows. The path contains
1055 * important components pointing to the system directories,
1056 * needed for loading shared libraries. So the path better
1057 * remains intact here.
1058 */
Darren Tuckere1a790d2003-09-16 11:52:19 +10001059# ifdef HAVE_ETC_DEFAULT_LOGIN
1060 read_etc_default_login(&env, &envsize, pw->pw_uid);
1061 path = child_get_env(env, "PATH");
1062# endif /* HAVE_ETC_DEFAULT_LOGIN */
1063 if (path == NULL || *path == '\0') {
Damien Millera8e06ce2003-11-21 23:48:55 +11001064 child_set_env(&env, &envsize, "PATH",
Darren Tuckere1a790d2003-09-16 11:52:19 +10001065 s->pw->pw_uid == 0 ?
1066 SUPERUSER_PATH : _PATH_STDPATH);
1067 }
Damien Millercb5e44a2000-09-29 12:12:36 +11001068# endif /* HAVE_CYGWIN */
1069#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001070
1071 snprintf(buf, sizeof buf, "%.200s/%.50s",
1072 _PATH_MAILDIR, pw->pw_name);
1073 child_set_env(&env, &envsize, "MAIL", buf);
1074
1075 /* Normal systems set SHELL by default. */
1076 child_set_env(&env, &envsize, "SHELL", shell);
1077 }
1078 if (getenv("TZ"))
1079 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1080
1081 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001082 if (!options.use_login) {
1083 while (custom_environment) {
1084 struct envstring *ce = custom_environment;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001085 char *str = ce->s;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001086
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001087 for (i = 0; str[i] != '=' && str[i]; i++)
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001088 ;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +00001089 if (str[i] == '=') {
1090 str[i] = 0;
1091 child_set_env(&env, &envsize, str, str + i + 1);
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001092 }
1093 custom_environment = ce->next;
1094 xfree(ce->s);
1095 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001096 }
Damien Millerb38eff82000-04-01 11:09:21 +10001097 }
1098
Damien Millerf37e2462002-09-19 11:47:55 +10001099 /* SSH_CLIENT deprecated */
Damien Millerb38eff82000-04-01 11:09:21 +10001100 snprintf(buf, sizeof buf, "%.50s %d %d",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001101 get_remote_ipaddr(), get_remote_port(), get_local_port());
Damien Millerb38eff82000-04-01 11:09:21 +10001102 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1103
Damien Miller00111382003-03-10 11:21:17 +11001104 laddr = get_local_ipaddr(packet_get_connection_in());
Damien Millerf37e2462002-09-19 11:47:55 +10001105 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
Damien Miller00111382003-03-10 11:21:17 +11001106 get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
1107 xfree(laddr);
Damien Millerf37e2462002-09-19 11:47:55 +10001108 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1109
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001110 if (s->ttyfd != -1)
1111 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1112 if (s->term)
1113 child_set_env(&env, &envsize, "TERM", s->term);
1114 if (s->display)
1115 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 if (original_command)
1117 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1118 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001119
Tim Rice81ed5182002-09-25 17:38:46 -07001120#ifdef _UNICOS
1121 if (cray_tmpdir[0] != '\0')
1122 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1123#endif /* _UNICOS */
1124
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001125 /*
1126 * Since we clear KRB5CCNAME at startup, if it's set now then it
1127 * must have been set by a native authentication method (eg AIX or
1128 * SIA), so copy it to the child.
1129 */
1130 {
1131 char *cp;
1132
1133 if ((cp = getenv("KRB5CCNAME")) != NULL)
1134 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1135 }
1136
Damien Millerb38eff82000-04-01 11:09:21 +10001137#ifdef _AIX
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001138 {
1139 char *cp;
1140
1141 if ((cp = getenv("AUTHSTATE")) != NULL)
1142 child_set_env(&env, &envsize, "AUTHSTATE", cp);
Ben Lindstrom839ac4f2002-02-24 20:42:46 +00001143 read_environment_file(&env, &envsize, "/etc/environment");
1144 }
Damien Millerb38eff82000-04-01 11:09:21 +10001145#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +00001146#ifdef KRB5
Damien Miller9c870f92004-04-16 22:47:55 +10001147 if (s->authctxt->krb5_ccname)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001148 child_set_env(&env, &envsize, "KRB5CCNAME",
Damien Miller9c870f92004-04-16 22:47:55 +10001149 s->authctxt->krb5_ccname);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001150#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001151#ifdef USE_PAM
Kevin Steves38b050a2002-07-23 00:44:07 +00001152 /*
1153 * Pull in any environment variables that may have
1154 * been set by PAM.
1155 */
Damien Miller4e448a32003-05-14 15:11:48 +10001156 if (options.use_pam) {
Damien Millerc756e9b2003-11-17 21:41:42 +11001157 char **p;
Damien Miller787b2ec2003-11-21 23:56:47 +11001158
Damien Millerc756e9b2003-11-17 21:41:42 +11001159 p = fetch_pam_child_environment();
1160 copy_environment(p, &env, &envsize);
1161 free_pam_environment(p);
Kevin Steves38b050a2002-07-23 00:44:07 +00001162
Damien Millerc756e9b2003-11-17 21:41:42 +11001163 p = fetch_pam_environment();
Kevin Steves38b050a2002-07-23 00:44:07 +00001164 copy_environment(p, &env, &envsize);
1165 free_pam_environment(p);
1166 }
Damien Millerb38eff82000-04-01 11:09:21 +10001167#endif /* USE_PAM */
1168
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001169 if (auth_sock_name != NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001170 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Ben Lindstrom8bb6f362002-06-11 15:59:02 +00001171 auth_sock_name);
Damien Millerb38eff82000-04-01 11:09:21 +10001172
1173 /* read $HOME/.ssh/environment. */
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001174 if (options.permit_user_env && !options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001175 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
Damien Millere9994cb2002-09-10 21:43:53 +10001176 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
Damien Millerb38eff82000-04-01 11:09:21 +10001177 read_environment_file(&env, &envsize, buf);
1178 }
1179 if (debug_flag) {
1180 /* dump the environment */
1181 fprintf(stderr, "Environment:\n");
1182 for (i = 0; env[i]; i++)
1183 fprintf(stderr, " %.200s\n", env[i]);
1184 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001185 return env;
1186}
1187
1188/*
1189 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1190 * first in this order).
1191 */
1192static void
1193do_rc_files(Session *s, const char *shell)
1194{
1195 FILE *f = NULL;
1196 char cmd[1024];
1197 int do_xauth;
1198 struct stat st;
1199
1200 do_xauth =
1201 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1202
1203 /* ignore _PATH_SSH_USER_RC for subsystems */
1204 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1205 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1206 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1207 if (debug_flag)
1208 fprintf(stderr, "Running %s\n", cmd);
1209 f = popen(cmd, "w");
1210 if (f) {
1211 if (do_xauth)
1212 fprintf(f, "%s %s\n", s->auth_proto,
1213 s->auth_data);
1214 pclose(f);
1215 } else
1216 fprintf(stderr, "Could not run %s\n",
1217 _PATH_SSH_USER_RC);
1218 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1219 if (debug_flag)
1220 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1221 _PATH_SSH_SYSTEM_RC);
1222 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1223 if (f) {
1224 if (do_xauth)
1225 fprintf(f, "%s %s\n", s->auth_proto,
1226 s->auth_data);
1227 pclose(f);
1228 } else
1229 fprintf(stderr, "Could not run %s\n",
1230 _PATH_SSH_SYSTEM_RC);
1231 } else if (do_xauth && options.xauth_location != NULL) {
1232 /* Add authority data to .Xauthority if appropriate. */
1233 if (debug_flag) {
1234 fprintf(stderr,
Ben Lindstrom611797e2002-12-23 02:15:57 +00001235 "Running %.500s remove %.100s\n",
Darren Tucker3e33cec2003-10-02 16:12:36 +10001236 options.xauth_location, s->auth_display);
Ben Lindstrom611797e2002-12-23 02:15:57 +00001237 fprintf(stderr,
1238 "%.500s add %.100s %.100s %.100s\n",
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001239 options.xauth_location, s->auth_display,
1240 s->auth_proto, s->auth_data);
1241 }
1242 snprintf(cmd, sizeof cmd, "%s -q -",
1243 options.xauth_location);
1244 f = popen(cmd, "w");
1245 if (f) {
Ben Lindstrom611797e2002-12-23 02:15:57 +00001246 fprintf(f, "remove %s\n",
1247 s->auth_display);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001248 fprintf(f, "add %s %s %s\n",
1249 s->auth_display, s->auth_proto,
1250 s->auth_data);
1251 pclose(f);
1252 } else {
1253 fprintf(stderr, "Could not run %s\n",
1254 cmd);
1255 }
1256 }
1257}
1258
1259static void
1260do_nologin(struct passwd *pw)
1261{
1262 FILE *f = NULL;
1263 char buf[1024];
1264
1265#ifdef HAVE_LOGIN_CAP
1266 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1267 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1268 _PATH_NOLOGIN), "r");
1269#else
1270 if (pw->pw_uid)
1271 f = fopen(_PATH_NOLOGIN, "r");
1272#endif
1273 if (f) {
1274 /* /etc/nologin exists. Print its contents and exit. */
Damien Miller996acd22003-04-09 20:59:48 +10001275 logit("User %.100s not allowed because %s exists",
Damien Millera6eb2b72002-09-19 11:50:48 +10001276 pw->pw_name, _PATH_NOLOGIN);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001277 while (fgets(buf, sizeof(buf), f))
1278 fputs(buf, stderr);
1279 fclose(f);
Damien Millerf25c18d2003-01-07 17:38:58 +11001280 fflush(NULL);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001281 exit(254);
1282 }
1283}
1284
1285/* Set login name, uid, gid, and groups. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001286void
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001287do_setusercontext(struct passwd *pw)
1288{
Damien Miller1a3ccb02003-02-24 13:04:01 +11001289#ifndef HAVE_CYGWIN
1290 if (getuid() == 0 || geteuid() == 0)
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001291#endif /* HAVE_CYGWIN */
Damien Miller1a3ccb02003-02-24 13:04:01 +11001292 {
1293
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001294#ifdef HAVE_SETPCRED
Darren Tucker793e8172003-07-08 21:01:04 +10001295 if (setpcred(pw->pw_name, (char **)NULL) == -1)
1296 fatal("Failed to set process credentials");
Ben Lindstromf0bfa832002-06-21 00:01:18 +00001297#endif /* HAVE_SETPCRED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001298#ifdef HAVE_LOGIN_CAP
Ben Lindstrom938b8282002-07-15 17:58:34 +00001299# ifdef __bsdi__
Damien Millerf18cd162002-06-26 19:12:59 +10001300 setpgid(0, 0);
Ben Lindstrom938b8282002-07-15 17:58:34 +00001301# endif
Darren Tuckerc97b01a2005-02-16 16:47:37 +11001302#ifdef GSSAPI
1303 if (options.gss_authentication) {
1304 temporarily_use_uid(pw);
1305 ssh_gssapi_storecreds();
1306 restore_uid();
1307 }
1308#endif
Damien Millerd3526362004-01-23 14:16:26 +11001309# ifdef USE_PAM
1310 if (options.use_pam) {
1311 do_pam_session();
1312 do_pam_setcred(0);
1313 }
1314# endif /* USE_PAM */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001315 if (setusercontext(lc, pw, pw->pw_uid,
1316 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1317 perror("unable to set user context");
1318 exit(1);
1319 }
1320#else
1321# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1322 /* Sets login uid for accounting */
1323 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1324 error("setluid: %s", strerror(errno));
1325# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1326
1327 if (setlogin(pw->pw_name) < 0)
1328 error("setlogin failed: %s", strerror(errno));
1329 if (setgid(pw->pw_gid) < 0) {
1330 perror("setgid");
1331 exit(1);
1332 }
1333 /* Initialize the group list. */
1334 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1335 perror("initgroups");
1336 exit(1);
1337 }
1338 endgrent();
Darren Tuckerc97b01a2005-02-16 16:47:37 +11001339#ifdef GSSAPI
1340 if (options.gss_authentication) {
1341 temporarily_use_uid(pw);
1342 ssh_gssapi_storecreds();
1343 restore_uid();
1344 }
1345#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001346# ifdef USE_PAM
1347 /*
Damien Millera8e06ce2003-11-21 23:48:55 +11001348 * PAM credentials may take the form of supplementary groups.
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001349 * These will have been wiped by the above initgroups() call.
1350 * Reestablish them here.
1351 */
Damien Miller341c6e62003-09-02 23:18:52 +10001352 if (options.use_pam) {
1353 do_pam_session();
Damien Miller4e448a32003-05-14 15:11:48 +10001354 do_pam_setcred(0);
Damien Miller341c6e62003-09-02 23:18:52 +10001355 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001356# endif /* USE_PAM */
1357# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)
1358 irix_setusercontext(pw);
1359# endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */
Ben Lindstromb129be62002-06-25 17:12:26 +00001360# ifdef _AIX
Ben Lindstrom51b24882002-07-04 03:08:40 +00001361 aix_usrinfo(pw);
Ben Lindstromb129be62002-06-25 17:12:26 +00001362# endif /* _AIX */
Tim Rice66fd2172005-08-31 09:59:49 -07001363#if defined(HAVE_LIBIAF) && !defined(BROKEN_LIBIAF)
Tim Rice2291c002005-08-26 13:15:19 -07001364 if (set_id(pw->pw_name) != 0) {
1365 exit(1);
1366 }
Tim Rice66fd2172005-08-31 09:59:49 -07001367#endif /* HAVE_LIBIAF && !BROKEN_LIBIAF */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001368 /* Permanently switch to the desired uid. */
1369 permanently_set_uid(pw);
1370#endif
1371 }
Damien Miller1a3ccb02003-02-24 13:04:01 +11001372
1373#ifdef HAVE_CYGWIN
1374 if (is_winnt)
1375#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001376 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1377 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Miller73b42d22006-04-22 21:26:08 +10001378
1379#ifdef WITH_SELINUX
1380 ssh_selinux_setup_exec_context(pw->pw_name);
1381#endif
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001382}
1383
Ben Lindstrom08105192002-03-22 02:50:06 +00001384static void
Darren Tucker23bc8d02004-02-06 16:24:31 +11001385do_pwchange(Session *s)
1386{
Darren Tucker09991742004-07-17 17:05:14 +10001387 fflush(NULL);
Darren Tucker23bc8d02004-02-06 16:24:31 +11001388 fprintf(stderr, "WARNING: Your password has expired.\n");
1389 if (s->ttyfd != -1) {
Darren Tuckerfc959702004-07-17 16:12:08 +10001390 fprintf(stderr,
Darren Tucker23bc8d02004-02-06 16:24:31 +11001391 "You must change your password now and login again!\n");
Darren Tucker33370e02005-02-09 22:17:28 +11001392#ifdef PASSWD_NEEDS_USERNAME
1393 execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
1394 (char *)NULL);
1395#else
Darren Tucker23bc8d02004-02-06 16:24:31 +11001396 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
Darren Tucker33370e02005-02-09 22:17:28 +11001397#endif
Darren Tucker23bc8d02004-02-06 16:24:31 +11001398 perror("passwd");
1399 } else {
1400 fprintf(stderr,
1401 "Password change required but no TTY available.\n");
1402 }
1403 exit(1);
1404}
1405
1406static void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407launch_login(struct passwd *pw, const char *hostname)
1408{
1409 /* Launch login(1). */
1410
Ben Lindstrom378a4172002-06-07 14:49:56 +00001411 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Kevin Stevesb4799a32002-03-24 23:19:54 +00001412#ifdef xxxLOGIN_NEEDS_TERM
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001413 (s->term ? s->term : "unknown"),
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414#endif /* LOGIN_NEEDS_TERM */
Kevin Steves5feaaef2002-04-23 20:45:55 +00001415#ifdef LOGIN_NO_ENDOPT
1416 "-p", "-f", pw->pw_name, (char *)NULL);
1417#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Kevin Steves5feaaef2002-04-23 20:45:55 +00001419#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420
1421 /* Login couldn't be executed, die. */
1422
1423 perror("login");
1424 exit(1);
1425}
1426
Darren Tucker23bc8d02004-02-06 16:24:31 +11001427static void
1428child_close_fds(void)
1429{
1430 int i;
1431
1432 if (packet_get_connection_in() == packet_get_connection_out())
1433 close(packet_get_connection_in());
1434 else {
1435 close(packet_get_connection_in());
1436 close(packet_get_connection_out());
1437 }
1438 /*
1439 * Close all descriptors related to channels. They will still remain
1440 * open in the parent.
1441 */
1442 /* XXX better use close-on-exec? -markus */
1443 channel_close_all();
1444
1445 /*
1446 * Close any extra file descriptors. Note that there may still be
1447 * descriptors left by system functions. They will be closed later.
1448 */
1449 endpwent();
1450
1451 /*
Damien Miller788f2122005-11-05 15:14:59 +11001452 * Close any extra open file descriptors so that we don't have them
Darren Tucker23bc8d02004-02-06 16:24:31 +11001453 * hanging around in clients. Note that we want to do this after
1454 * initgroups, because at least on Solaris 2.3 it leaves file
1455 * descriptors open.
1456 */
1457 for (i = 3; i < 64; i++)
1458 close(i);
1459}
1460
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001461/*
1462 * Performs common processing for the child, such as setting up the
1463 * environment, closing extra file descriptors, setting the user and group
1464 * ids, and executing the command or shell.
1465 */
1466void
1467do_child(Session *s, const char *command)
1468{
1469 extern char **environ;
1470 char **env;
1471 char *argv[10];
1472 const char *shell, *shell0, *hostname = NULL;
1473 struct passwd *pw = s->pw;
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001474
1475 /* remove hostkey from the child's memory */
1476 destroy_sensitive_data();
1477
Darren Tucker23bc8d02004-02-06 16:24:31 +11001478 /* Force a password change */
1479 if (s->authctxt->force_pwchange) {
1480 do_setusercontext(pw);
1481 child_close_fds();
1482 do_pwchange(s);
1483 exit(1);
1484 }
1485
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001486 /* login(1) is only called if we execute the login shell */
1487 if (options.use_login && command != NULL)
1488 options.use_login = 0;
1489
Tim Rice81ed5182002-09-25 17:38:46 -07001490#ifdef _UNICOS
1491 cray_setup(pw->pw_uid, pw->pw_name, command);
1492#endif /* _UNICOS */
1493
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001494 /*
1495 * Login(1) does this as well, and it needs uid 0 for the "-h"
1496 * switch, so we let login(1) to this for us.
1497 */
1498 if (!options.use_login) {
1499#ifdef HAVE_OSF_SIA
Ben Lindstromc8c548d2003-03-21 01:18:09 +00001500 session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001501 if (!check_quietlogin(s, command))
1502 do_motd();
1503#else /* HAVE_OSF_SIA */
Darren Tucker42308a42005-10-30 15:31:55 +11001504 /* When PAM is enabled we rely on it to do the nologin check */
1505 if (!options.use_pam)
1506 do_nologin(pw);
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001507 do_setusercontext(pw);
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001508 /*
1509 * PAM session modules in do_setusercontext may have
1510 * generated messages, so if this in an interactive
1511 * login then display them too.
1512 */
Darren Tuckera2a3ed02004-09-11 23:09:53 +10001513 if (!check_quietlogin(s, command))
Darren Tucker0a44d1e2004-07-01 09:48:29 +10001514 display_loginmsg();
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001515#endif /* HAVE_OSF_SIA */
1516 }
1517
Darren Tucker69687f42004-09-11 22:17:26 +10001518#ifdef USE_PAM
Darren Tucker48554152005-04-21 19:50:55 +10001519 if (options.use_pam && !options.use_login && !is_pam_session_open()) {
1520 debug3("PAM session not opened, exiting");
Darren Tucker69687f42004-09-11 22:17:26 +10001521 display_loginmsg();
1522 exit(254);
1523 }
1524#endif
1525
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001526 /*
1527 * Get the shell from the password data. An empty shell field is
1528 * legal, and means /bin/sh.
1529 */
1530 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Ben Lindstrom46767602002-12-23 02:26:08 +00001531
1532 /*
1533 * Make sure $SHELL points to the shell from the password file,
1534 * even if shell is overridden from login.conf
1535 */
1536 env = do_setup_env(s, shell);
1537
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001538#ifdef HAVE_LOGIN_CAP
1539 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1540#endif
1541
Damien Millerad833b32000-08-23 10:46:23 +10001542 /* we have to stash the hostname before we close our socket. */
1543 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001544 hostname = get_remote_name_or_ip(utmp_len,
Damien Miller3a961dc2003-06-03 10:25:48 +10001545 options.use_dns);
Damien Millerb38eff82000-04-01 11:09:21 +10001546 /*
1547 * Close the connection descriptors; note that this is the child, and
1548 * the server will still have the socket open, and it is important
1549 * that we do not shutdown it. Note that the descriptors cannot be
1550 * closed before building the environment, as we call
1551 * get_remote_ipaddr there.
1552 */
Darren Tucker23bc8d02004-02-06 16:24:31 +11001553 child_close_fds();
Damien Millerb38eff82000-04-01 11:09:21 +10001554
Damien Millerb38eff82000-04-01 11:09:21 +10001555 /*
Damien Miller05eda432002-02-10 18:32:28 +11001556 * Must take new environment into use so that .ssh/rc,
1557 * /etc/ssh/sshrc and xauth are run in the proper environment.
Damien Millerb38eff82000-04-01 11:09:21 +10001558 */
1559 environ = env;
1560
Darren Tucker3c78c5e2004-01-23 22:03:10 +11001561#if defined(KRB5) && defined(USE_AFS)
Darren Tucker22ef5082003-12-31 11:37:34 +11001562 /*
1563 * At this point, we check to see if AFS is active and if we have
1564 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1565 * if we can (and need to) extend the ticket into an AFS token. If
1566 * we don't do this, we run into potential problems if the user's
1567 * home directory is in AFS and it's not world-readable.
1568 */
1569
1570 if (options.kerberos_get_afs_token && k_hasafs() &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001571 (s->authctxt->krb5_ctx != NULL)) {
Darren Tucker22ef5082003-12-31 11:37:34 +11001572 char cell[64];
1573
1574 debug("Getting AFS token");
1575
1576 k_setpag();
1577
1578 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1579 krb5_afslog(s->authctxt->krb5_ctx,
1580 s->authctxt->krb5_fwd_ccache, cell, NULL);
1581
1582 krb5_afslog_home(s->authctxt->krb5_ctx,
1583 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1584 }
1585#endif
1586
Damien Miller788f2122005-11-05 15:14:59 +11001587 /* Change current directory to the user's home directory. */
Damien Miller139d4cd2001-10-10 15:07:44 +10001588 if (chdir(pw->pw_dir) < 0) {
1589 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1590 pw->pw_dir, strerror(errno));
1591#ifdef HAVE_LOGIN_CAP
1592 if (login_getcapbool(lc, "requirehome", 0))
1593 exit(1);
1594#endif
1595 }
1596
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001597 if (!options.use_login)
1598 do_rc_files(s, shell);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001599
1600 /* restore SIGPIPE for child */
Damien Miller9ab00b42006-08-05 12:40:11 +10001601 signal(SIGPIPE, SIG_DFL);
Ben Lindstromde71cda2001-03-24 00:43:26 +00001602
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001603 if (options.use_login) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604 launch_login(pw, hostname);
1605 /* NEVERREACHED */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001606 }
1607
1608 /* Get the last component of the shell name. */
1609 if ((shell0 = strrchr(shell, '/')) != NULL)
1610 shell0++;
1611 else
1612 shell0 = shell;
1613
Damien Millerb38eff82000-04-01 11:09:21 +10001614 /*
1615 * If we have no command, execute the shell. In this case, the shell
1616 * name to be passed in argv[0] is preceded by '-' to indicate that
1617 * this is a login shell.
1618 */
1619 if (!command) {
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001620 char argv0[256];
Damien Millerb38eff82000-04-01 11:09:21 +10001621
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001622 /* Start the shell. Set initial character to '-'. */
1623 argv0[0] = '-';
Damien Millerb38eff82000-04-01 11:09:21 +10001624
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001625 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1626 >= sizeof(argv0) - 1) {
1627 errno = EINVAL;
Damien Millerb38eff82000-04-01 11:09:21 +10001628 perror(shell);
1629 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001630 }
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001631
1632 /* Execute the shell. */
1633 argv[0] = argv0;
1634 argv[1] = NULL;
1635 execve(shell, argv, env);
1636
1637 /* Executing the shell failed. */
1638 perror(shell);
1639 exit(1);
Damien Millerb38eff82000-04-01 11:09:21 +10001640 }
1641 /*
1642 * Execute the command using the user's shell. This uses the -c
1643 * option to execute the command.
1644 */
Ben Lindstrom4e97e852002-02-19 21:50:43 +00001645 argv[0] = (char *) shell0;
Damien Millerb38eff82000-04-01 11:09:21 +10001646 argv[1] = "-c";
1647 argv[2] = (char *) command;
1648 argv[3] = NULL;
1649 execve(shell, argv, env);
1650 perror(shell);
1651 exit(1);
1652}
1653
1654Session *
1655session_new(void)
1656{
1657 int i;
1658 static int did_init = 0;
1659 if (!did_init) {
1660 debug("session_new: init");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001661 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001662 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001663 }
1664 did_init = 1;
1665 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001666 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001667 Session *s = &sessions[i];
1668 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001669 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001670 s->chanid = -1;
1671 s->ptyfd = -1;
1672 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001673 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001674 s->self = i;
Damien Miller2b9b0452005-07-17 17:19:24 +10001675 s->x11_chanids = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001676 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001677 return s;
1678 }
1679 }
1680 return NULL;
1681}
1682
Ben Lindstrombba81212001-06-25 05:01:22 +00001683static void
Damien Millerb38eff82000-04-01 11:09:21 +10001684session_dump(void)
1685{
1686 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001687 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001688 Session *s = &sessions[i];
Ben Lindstromce0f6342002-06-11 16:42:49 +00001689 debug("dump: used %d session %d %p channel %d pid %ld",
Damien Millerb38eff82000-04-01 11:09:21 +10001690 s->used,
1691 s->self,
1692 s,
1693 s->chanid,
Ben Lindstromce0f6342002-06-11 16:42:49 +00001694 (long)s->pid);
Damien Millerb38eff82000-04-01 11:09:21 +10001695 }
1696}
1697
Damien Millerefb4afe2000-04-12 18:45:05 +10001698int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001699session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001700{
1701 Session *s = session_new();
1702 debug("session_open: channel %d", chanid);
1703 if (s == NULL) {
1704 error("no more sessions");
1705 return 0;
1706 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001707 s->authctxt = authctxt;
1708 s->pw = authctxt->pw;
Damien Miller3e3b5142003-11-17 21:13:40 +11001709 if (s->pw == NULL || !authctxt->valid)
Kevin Steves43cdef32001-02-11 14:12:08 +00001710 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001711 debug("session_open: session %d: link with channel %d", s->self, chanid);
1712 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001713 return 1;
1714}
1715
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001716Session *
1717session_by_tty(char *tty)
1718{
1719 int i;
1720 for (i = 0; i < MAX_SESSIONS; i++) {
1721 Session *s = &sessions[i];
1722 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1723 debug("session_by_tty: session %d tty %s", i, tty);
1724 return s;
1725 }
1726 }
1727 debug("session_by_tty: unknown tty %.100s", tty);
1728 session_dump();
1729 return NULL;
1730}
1731
Ben Lindstrombba81212001-06-25 05:01:22 +00001732static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001733session_by_channel(int id)
1734{
1735 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001736 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001737 Session *s = &sessions[i];
1738 if (s->used && s->chanid == id) {
1739 debug("session_by_channel: session %d channel %d", i, id);
1740 return s;
1741 }
1742 }
1743 debug("session_by_channel: unknown channel %d", id);
1744 session_dump();
1745 return NULL;
1746}
1747
Ben Lindstrombba81212001-06-25 05:01:22 +00001748static Session *
Damien Miller2b9b0452005-07-17 17:19:24 +10001749session_by_x11_channel(int id)
1750{
1751 int i, j;
1752
1753 for (i = 0; i < MAX_SESSIONS; i++) {
1754 Session *s = &sessions[i];
1755
1756 if (s->x11_chanids == NULL || !s->used)
1757 continue;
1758 for (j = 0; s->x11_chanids[j] != -1; j++) {
1759 if (s->x11_chanids[j] == id) {
1760 debug("session_by_x11_channel: session %d "
1761 "channel %d", s->self, id);
1762 return s;
1763 }
1764 }
1765 }
1766 debug("session_by_x11_channel: unknown channel %d", id);
1767 session_dump();
1768 return NULL;
1769}
1770
1771static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001772session_by_pid(pid_t pid)
1773{
1774 int i;
Ben Lindstromce0f6342002-06-11 16:42:49 +00001775 debug("session_by_pid: pid %ld", (long)pid);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001776 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001777 Session *s = &sessions[i];
1778 if (s->used && s->pid == pid)
1779 return s;
1780 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001781 error("session_by_pid: unknown pid %ld", (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001782 session_dump();
1783 return NULL;
1784}
1785
Ben Lindstrombba81212001-06-25 05:01:22 +00001786static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001787session_window_change_req(Session *s)
1788{
1789 s->col = packet_get_int();
1790 s->row = packet_get_int();
1791 s->xpixel = packet_get_int();
1792 s->ypixel = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001793 packet_check_eom();
Damien Millerefb4afe2000-04-12 18:45:05 +10001794 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1795 return 1;
1796}
1797
Ben Lindstrombba81212001-06-25 05:01:22 +00001798static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001799session_pty_req(Session *s)
1800{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001801 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001802 int n_bytes;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001803
Ben Lindstrom49c12602001-06-13 04:37:36 +00001804 if (no_pty_flag) {
1805 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001806 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001807 }
1808 if (s->ttyfd != -1) {
1809 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001810 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001811 }
1812
Damien Millerefb4afe2000-04-12 18:45:05 +10001813 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001814
1815 if (compat20) {
1816 s->col = packet_get_int();
1817 s->row = packet_get_int();
1818 } else {
1819 s->row = packet_get_int();
1820 s->col = packet_get_int();
1821 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001822 s->xpixel = packet_get_int();
1823 s->ypixel = packet_get_int();
1824
1825 if (strcmp(s->term, "") == 0) {
1826 xfree(s->term);
1827 s->term = NULL;
1828 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001829
Damien Millerefb4afe2000-04-12 18:45:05 +10001830 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001831 debug("Allocating pty.");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001832 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001833 if (s->term)
1834 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001835 s->term = NULL;
1836 s->ptyfd = -1;
1837 s->ttyfd = -1;
1838 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001839 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001840 }
1841 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001842
1843 /* for SSH1 the tty modes length is not given */
1844 if (!compat20)
1845 n_bytes = packet_remaining();
1846 tty_parse_modes(s->ttyfd, &n_bytes);
1847
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001848 if (!use_privsep)
1849 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001850
1851 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001852 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1853
Damien Miller48b03fc2002-01-22 23:11:40 +11001854 packet_check_eom();
Damien Millere247cc42000-05-07 12:03:14 +10001855 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001856 return 1;
1857}
1858
Ben Lindstrombba81212001-06-25 05:01:22 +00001859static int
Damien Millerbd483e72000-04-30 10:00:53 +10001860session_subsystem_req(Session *s)
1861{
Damien Millerae452462001-10-10 15:08:06 +10001862 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001863 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001864 int success = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001865 char *prog, *cmd, *subsys = packet_get_string(&len);
Damien Millereccb9de2005-06-17 12:59:34 +10001866 u_int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001867
Damien Miller48b03fc2002-01-22 23:11:40 +11001868 packet_check_eom();
Damien Miller996acd22003-04-09 20:59:48 +10001869 logit("subsystem request for %.100s", subsys);
Damien Millerbd483e72000-04-30 10:00:53 +10001870
Damien Millerf6d9e222000-06-18 14:50:44 +10001871 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001872 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
Damien Miller917f9b62006-07-10 20:36:47 +10001873 prog = options.subsystem_command[i];
1874 cmd = options.subsystem_args[i];
1875 if (stat(prog, &st) < 0) {
1876 error("subsystem: cannot stat %s: %s", prog,
Damien Millerae452462001-10-10 15:08:06 +10001877 strerror(errno));
1878 break;
1879 }
1880 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001881 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001882 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001883 success = 1;
Damien Miller5fab4b92002-02-05 12:15:07 +11001884 break;
Damien Millerf6d9e222000-06-18 14:50:44 +10001885 }
1886 }
1887
1888 if (!success)
Damien Miller996acd22003-04-09 20:59:48 +10001889 logit("subsystem request for %.100s failed, subsystem not found",
Damien Millerae452462001-10-10 15:08:06 +10001890 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001891
Damien Millerbd483e72000-04-30 10:00:53 +10001892 xfree(subsys);
1893 return success;
1894}
1895
Ben Lindstrombba81212001-06-25 05:01:22 +00001896static int
Damien Millerbd483e72000-04-30 10:00:53 +10001897session_x11_req(Session *s)
1898{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001899 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001900
Damien Miller2b9b0452005-07-17 17:19:24 +10001901 if (s->auth_proto != NULL || s->auth_data != NULL) {
1902 error("session_x11_req: session %d: "
Darren Tucker63551872005-12-20 16:14:15 +11001903 "x11 forwarding already active", s->self);
Damien Miller2b9b0452005-07-17 17:19:24 +10001904 return 0;
1905 }
Damien Millerbd483e72000-04-30 10:00:53 +10001906 s->single_connection = packet_get_char();
1907 s->auth_proto = packet_get_string(NULL);
1908 s->auth_data = packet_get_string(NULL);
1909 s->screen = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001910 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10001911
Ben Lindstrom768176b2001-06-09 01:29:12 +00001912 success = session_setup_x11fwd(s);
1913 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001914 xfree(s->auth_proto);
1915 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001916 s->auth_proto = NULL;
1917 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001918 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001919 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001920}
1921
Ben Lindstrombba81212001-06-25 05:01:22 +00001922static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001923session_shell_req(Session *s)
1924{
Damien Miller48b03fc2002-01-22 23:11:40 +11001925 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001926 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001927 return 1;
1928}
1929
Ben Lindstrombba81212001-06-25 05:01:22 +00001930static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001931session_exec_req(Session *s)
1932{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001933 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001934 char *command = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001935 packet_check_eom();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001936 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001937 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001938 return 1;
1939}
1940
Ben Lindstrombba81212001-06-25 05:01:22 +00001941static int
Damien Miller54c45982003-05-15 10:20:13 +10001942session_break_req(Session *s)
1943{
Damien Miller54c45982003-05-15 10:20:13 +10001944
Darren Tucker1f8311c2004-05-13 16:39:33 +10001945 packet_get_int(); /* ignored */
Damien Miller54c45982003-05-15 10:20:13 +10001946 packet_check_eom();
1947
Darren Tucker3bdbd842003-08-13 20:31:05 +10001948 if (s->ttyfd == -1 ||
1949 tcsendbreak(s->ttyfd, 0) < 0)
Damien Miller54c45982003-05-15 10:20:13 +10001950 return 0;
Damien Miller54c45982003-05-15 10:20:13 +10001951 return 1;
1952}
1953
1954static int
Darren Tucker46bc0752004-05-02 22:11:30 +10001955session_env_req(Session *s)
1956{
1957 char *name, *val;
1958 u_int name_len, val_len, i;
1959
1960 name = packet_get_string(&name_len);
1961 val = packet_get_string(&val_len);
1962 packet_check_eom();
1963
1964 /* Don't set too many environment variables */
1965 if (s->num_env > 128) {
1966 debug2("Ignoring env request %s: too many env vars", name);
1967 goto fail;
1968 }
1969
1970 for (i = 0; i < options.num_accept_env; i++) {
1971 if (match_pattern(name, options.accept_env[i])) {
1972 debug2("Setting env %d: %s=%s", s->num_env, name, val);
Damien Miller36812092006-03-26 14:22:47 +11001973 s->env = xrealloc(s->env, s->num_env + 1,
1974 sizeof(*s->env));
Darren Tucker46bc0752004-05-02 22:11:30 +10001975 s->env[s->num_env].name = name;
1976 s->env[s->num_env].val = val;
1977 s->num_env++;
1978 return (1);
1979 }
1980 }
1981 debug2("Ignoring env request %s: disallowed name", name);
1982
1983 fail:
1984 xfree(name);
1985 xfree(val);
1986 return (0);
1987}
1988
1989static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001990session_auth_agent_req(Session *s)
1991{
1992 static int called = 0;
Damien Miller48b03fc2002-01-22 23:11:40 +11001993 packet_check_eom();
Ben Lindstrom14920292000-11-21 21:24:55 +00001994 if (no_agent_forwarding_flag) {
1995 debug("session_auth_agent_req: no_agent_forwarding_flag");
1996 return 0;
1997 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001998 if (called) {
1999 return 0;
2000 } else {
2001 called = 1;
2002 return auth_input_request_forwarding(s->pw);
2003 }
2004}
2005
Damien Millerc7ef63d2002-02-05 12:21:42 +11002006int
2007session_input_channel_req(Channel *c, const char *rtype)
Damien Millerefb4afe2000-04-12 18:45:05 +10002008{
Damien Millerefb4afe2000-04-12 18:45:05 +10002009 int success = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002010 Session *s;
Damien Millerefb4afe2000-04-12 18:45:05 +10002011
Damien Millerc7ef63d2002-02-05 12:21:42 +11002012 if ((s = session_by_channel(c->self)) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10002013 logit("session_input_channel_req: no session %d req %.100s",
Damien Millerc7ef63d2002-02-05 12:21:42 +11002014 c->self, rtype);
2015 return 0;
2016 }
2017 debug("session_input_channel_req: session %d req %s", s->self, rtype);
Damien Millerefb4afe2000-04-12 18:45:05 +10002018
2019 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00002020 * a session is in LARVAL state until a shell, a command
2021 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10002022 */
2023 if (c->type == SSH_CHANNEL_LARVAL) {
2024 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002025 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002026 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10002027 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002028 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10002029 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002030 } else if (strcmp(rtype, "x11-req") == 0) {
2031 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002032 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2033 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10002034 } else if (strcmp(rtype, "subsystem") == 0) {
2035 success = session_subsystem_req(s);
Darren Tucker46bc0752004-05-02 22:11:30 +10002036 } else if (strcmp(rtype, "env") == 0) {
2037 success = session_env_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002038 }
2039 }
2040 if (strcmp(rtype, "window-change") == 0) {
2041 success = session_window_change_req(s);
Damien Millera6b1d162004-06-30 22:41:07 +10002042 } else if (strcmp(rtype, "break") == 0) {
2043 success = session_break_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002044 }
Damien Millera6b1d162004-06-30 22:41:07 +10002045
Damien Millerc7ef63d2002-02-05 12:21:42 +11002046 return success;
Damien Millerefb4afe2000-04-12 18:45:05 +10002047}
2048
2049void
2050session_set_fds(Session *s, int fdin, int fdout, int fderr)
2051{
2052 if (!compat20)
2053 fatal("session_set_fds: called for proto != 2.0");
2054 /*
2055 * now that have a child and a pipe to the child,
2056 * we can activate our channel and register the fd's
2057 */
2058 if (s->chanid == -1)
2059 fatal("no channel for session %d", s->self);
2060 channel_set_fds(s->chanid,
2061 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11002062 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
Damien Miller19a59452002-02-19 15:20:57 +11002063 1,
2064 CHAN_SES_WINDOW_DEFAULT);
Damien Millerefb4afe2000-04-12 18:45:05 +10002065}
2066
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002067/*
2068 * Function to perform pty cleanup. Also called if we get aborted abnormally
2069 * (e.g., due to a dropped connection).
2070 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002071void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002072session_pty_cleanup2(Session *s)
Damien Millerb38eff82000-04-01 11:09:21 +10002073{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002074 if (s == NULL) {
2075 error("session_pty_cleanup: no session");
2076 return;
2077 }
2078 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10002079 return;
2080
Kevin Steves43cdef32001-02-11 14:12:08 +00002081 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002082
Damien Millerb38eff82000-04-01 11:09:21 +10002083 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002084 if (s->pid != 0)
Tim Ricee06ae4a2002-02-24 17:56:46 -08002085 record_logout(s->pid, s->tty, s->pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +10002086
2087 /* Release the pseudo-tty. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002088 if (getuid() == 0)
2089 pty_release(s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10002090
2091 /*
2092 * Close the server side of the socket pairs. We must do this after
2093 * the pty cleanup, so that another process doesn't get this pty
2094 * while we're still cleaning up.
2095 */
2096 if (close(s->ptymaster) < 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002097 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10002098
2099 /* unlink pty from session */
2100 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002101}
Damien Millerefb4afe2000-04-12 18:45:05 +10002102
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002103void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002104session_pty_cleanup(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002105{
Darren Tucker3e33cec2003-10-02 16:12:36 +10002106 PRIVSEP(session_pty_cleanup2(s));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002107}
2108
Damien Miller5a80bba2002-09-04 16:39:02 +10002109static char *
2110sig2name(int sig)
2111{
2112#define SSH_SIG(x) if (sig == SIG ## x) return #x
2113 SSH_SIG(ABRT);
2114 SSH_SIG(ALRM);
2115 SSH_SIG(FPE);
2116 SSH_SIG(HUP);
2117 SSH_SIG(ILL);
2118 SSH_SIG(INT);
2119 SSH_SIG(KILL);
2120 SSH_SIG(PIPE);
2121 SSH_SIG(QUIT);
2122 SSH_SIG(SEGV);
2123 SSH_SIG(TERM);
2124 SSH_SIG(USR1);
2125 SSH_SIG(USR2);
2126#undef SSH_SIG
2127 return "SIG@openssh.com";
2128}
2129
Ben Lindstrombba81212001-06-25 05:01:22 +00002130static void
Damien Miller2b9b0452005-07-17 17:19:24 +10002131session_close_x11(int id)
2132{
2133 Channel *c;
2134
Damien Millerd47c62a2005-12-13 19:33:57 +11002135 if ((c = channel_by_id(id)) == NULL) {
Damien Miller2b9b0452005-07-17 17:19:24 +10002136 debug("session_close_x11: x11 channel %d missing", id);
2137 } else {
2138 /* Detach X11 listener */
2139 debug("session_close_x11: detach x11 channel %d", id);
2140 channel_cancel_cleanup(id);
2141 if (c->ostate != CHAN_OUTPUT_CLOSED)
2142 chan_mark_dead(c);
2143 }
2144}
2145
2146static void
2147session_close_single_x11(int id, void *arg)
2148{
2149 Session *s;
2150 u_int i;
2151
2152 debug3("session_close_single_x11: channel %d", id);
2153 channel_cancel_cleanup(id);
2154 if ((s = session_by_x11_channel(id)) == NULL)
2155 fatal("session_close_single_x11: no x11 channel %d", id);
2156 for (i = 0; s->x11_chanids[i] != -1; i++) {
2157 debug("session_close_single_x11: session %d: "
2158 "closing channel %d", s->self, s->x11_chanids[i]);
2159 /*
2160 * The channel "id" is already closing, but make sure we
2161 * close all of its siblings.
2162 */
2163 if (s->x11_chanids[i] != id)
2164 session_close_x11(s->x11_chanids[i]);
2165 }
2166 xfree(s->x11_chanids);
2167 s->x11_chanids = NULL;
2168 if (s->display) {
2169 xfree(s->display);
2170 s->display = NULL;
2171 }
2172 if (s->auth_proto) {
2173 xfree(s->auth_proto);
2174 s->auth_proto = NULL;
2175 }
2176 if (s->auth_data) {
2177 xfree(s->auth_data);
2178 s->auth_data = NULL;
2179 }
2180 if (s->auth_display) {
2181 xfree(s->auth_display);
2182 s->auth_display = NULL;
2183 }
2184}
2185
2186static void
Damien Millerefb4afe2000-04-12 18:45:05 +10002187session_exit_message(Session *s, int status)
2188{
2189 Channel *c;
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002190
2191 if ((c = channel_lookup(s->chanid)) == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002192 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10002193 s->self, s->chanid);
Ben Lindstromce0f6342002-06-11 16:42:49 +00002194 debug("session_exit_message: session %d channel %d pid %ld",
2195 s->self, s->chanid, (long)s->pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002196
2197 if (WIFEXITED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002198 channel_request_start(s->chanid, "exit-status", 0);
Damien Millerefb4afe2000-04-12 18:45:05 +10002199 packet_put_int(WEXITSTATUS(status));
2200 packet_send();
2201 } else if (WIFSIGNALED(status)) {
Damien Millerf3dcf1f2002-02-08 22:06:48 +11002202 channel_request_start(s->chanid, "exit-signal", 0);
Damien Miller5a80bba2002-09-04 16:39:02 +10002203 packet_put_cstring(sig2name(WTERMSIG(status)));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002204#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10002205 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10002206#else /* WCOREDUMP */
2207 packet_put_char(0);
2208#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10002209 packet_put_cstring("");
2210 packet_put_cstring("");
2211 packet_send();
2212 } else {
2213 /* Some weird exit cause. Just exit. */
2214 packet_disconnect("wait returned status %04x.", status);
2215 }
2216
2217 /* disconnect channel */
2218 debug("session_exit_message: release channel %d", s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002219
2220 /*
2221 * Adjust cleanup callback attachment to send close messages when
Damien Millerc91e5562006-03-26 13:58:55 +11002222 * the channel gets EOF. The session will be then be closed
Damien Miller39eda6e2005-11-05 14:52:50 +11002223 * by session_close_by_channel when the childs close their fds.
2224 */
2225 channel_register_cleanup(c->self, session_close_by_channel, 1);
2226
Damien Miller166fca82000-04-20 07:42:21 +10002227 /*
2228 * emulate a write failure with 'chan_write_failed', nobody will be
2229 * interested in data we write.
2230 * Note that we must not call 'chan_read_failed', since there could
2231 * be some more data waiting in the pipe.
2232 */
Damien Millerbd483e72000-04-30 10:00:53 +10002233 if (c->ostate != CHAN_OUTPUT_CLOSED)
2234 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002235}
2236
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002237void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002238session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10002239{
Damien Millereccb9de2005-06-17 12:59:34 +10002240 u_int i;
Darren Tucker46bc0752004-05-02 22:11:30 +10002241
Ben Lindstromce0f6342002-06-11 16:42:49 +00002242 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002243 if (s->ttyfd != -1)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00002244 session_pty_cleanup(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002245 if (s->term)
2246 xfree(s->term);
2247 if (s->display)
2248 xfree(s->display);
Damien Miller2b9b0452005-07-17 17:19:24 +10002249 if (s->x11_chanids)
2250 xfree(s->x11_chanids);
Damien Miller512bccb2002-02-05 12:11:02 +11002251 if (s->auth_display)
2252 xfree(s->auth_display);
Damien Millerefb4afe2000-04-12 18:45:05 +10002253 if (s->auth_data)
2254 xfree(s->auth_data);
2255 if (s->auth_proto)
2256 xfree(s->auth_proto);
2257 s->used = 0;
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002258 if (s->env != NULL) {
2259 for (i = 0; i < s->num_env; i++) {
2260 xfree(s->env[i].name);
2261 xfree(s->env[i].val);
2262 }
Darren Tucker46bc0752004-05-02 22:11:30 +10002263 xfree(s->env);
Damien Millerd5fe0ba2006-08-30 11:07:39 +10002264 }
Damien Millere247cc42000-05-07 12:03:14 +10002265 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002266}
2267
2268void
2269session_close_by_pid(pid_t pid, int status)
2270{
2271 Session *s = session_by_pid(pid);
2272 if (s == NULL) {
Ben Lindstromce0f6342002-06-11 16:42:49 +00002273 debug("session_close_by_pid: no session for pid %ld",
2274 (long)pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10002275 return;
2276 }
2277 if (s->chanid != -1)
2278 session_exit_message(s, status);
Damien Miller39eda6e2005-11-05 14:52:50 +11002279 if (s->ttyfd != -1)
2280 session_pty_cleanup(s);
Tim Rice83d2f5f2006-02-07 15:17:44 -08002281 s->pid = 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10002282}
2283
2284/*
2285 * this is called when a channel dies before
2286 * the session 'child' itself dies
2287 */
2288void
2289session_close_by_channel(int id, void *arg)
2290{
2291 Session *s = session_by_channel(id);
Damien Miller39eda6e2005-11-05 14:52:50 +11002292 u_int i;
Damien Miller2b9b0452005-07-17 17:19:24 +10002293
Damien Millerefb4afe2000-04-12 18:45:05 +10002294 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10002295 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10002296 return;
2297 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00002298 debug("session_close_by_channel: channel %d child %ld",
2299 id, (long)s->pid);
Damien Miller3ec27592001-10-12 11:35:04 +10002300 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10002301 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10002302 /*
2303 * delay detach of session, but release pty, since
2304 * the fd's to the child are already closed
2305 */
Darren Tucker3e33cec2003-10-02 16:12:36 +10002306 if (s->ttyfd != -1)
Damien Miller0585d512001-10-12 11:35:50 +10002307 session_pty_cleanup(s);
Damien Miller3ec27592001-10-12 11:35:04 +10002308 return;
2309 }
2310 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10002311 channel_cancel_cleanup(s->chanid);
Damien Miller39eda6e2005-11-05 14:52:50 +11002312
2313 /* Close any X11 listeners associated with this session */
2314 if (s->x11_chanids != NULL) {
2315 for (i = 0; s->x11_chanids[i] != -1; i++) {
2316 session_close_x11(s->x11_chanids[i]);
2317 s->x11_chanids[i] = -1;
2318 }
2319 }
2320
Damien Millerefb4afe2000-04-12 18:45:05 +10002321 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10002322 session_close(s);
2323}
2324
2325void
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002326session_destroy_all(void (*closefunc)(Session *))
Damien Miller52b77be2001-10-10 15:14:37 +10002327{
2328 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002329 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002330 Session *s = &sessions[i];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002331 if (s->used) {
2332 if (closefunc != NULL)
2333 closefunc(s);
2334 else
2335 session_close(s);
2336 }
Damien Miller52b77be2001-10-10 15:14:37 +10002337 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002338}
2339
Ben Lindstrombba81212001-06-25 05:01:22 +00002340static char *
Damien Millere247cc42000-05-07 12:03:14 +10002341session_tty_list(void)
2342{
2343 static char buf[1024];
2344 int i;
Damien Millera8ed44b2003-01-10 09:53:12 +11002345 char *cp;
2346
Damien Millere247cc42000-05-07 12:03:14 +10002347 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002348 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002349 Session *s = &sessions[i];
2350 if (s->used && s->ttyfd != -1) {
Damien Miller787b2ec2003-11-21 23:56:47 +11002351
Damien Millera8ed44b2003-01-10 09:53:12 +11002352 if (strncmp(s->tty, "/dev/", 5) != 0) {
2353 cp = strrchr(s->tty, '/');
2354 cp = (cp == NULL) ? s->tty : cp + 1;
2355 } else
2356 cp = s->tty + 5;
Damien Miller787b2ec2003-11-21 23:56:47 +11002357
Damien Millere247cc42000-05-07 12:03:14 +10002358 if (buf[0] != '\0')
2359 strlcat(buf, ",", sizeof buf);
Damien Millera8ed44b2003-01-10 09:53:12 +11002360 strlcat(buf, cp, sizeof buf);
Damien Millere247cc42000-05-07 12:03:14 +10002361 }
2362 }
2363 if (buf[0] == '\0')
2364 strlcpy(buf, "notty", sizeof buf);
2365 return buf;
2366}
2367
2368void
2369session_proctitle(Session *s)
2370{
2371 if (s->pw == NULL)
2372 error("no user for session %d", s->self);
2373 else
2374 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2375}
2376
Ben Lindstrom768176b2001-06-09 01:29:12 +00002377int
2378session_setup_x11fwd(Session *s)
2379{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002380 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002381 char display[512], auth_display[512];
2382 char hostname[MAXHOSTNAMELEN];
Damien Miller2b9b0452005-07-17 17:19:24 +10002383 u_int i;
Ben Lindstrom768176b2001-06-09 01:29:12 +00002384
2385 if (no_x11_forwarding_flag) {
2386 packet_send_debug("X11 forwarding disabled in user configuration file.");
2387 return 0;
2388 }
2389 if (!options.x11_forwarding) {
2390 debug("X11 forwarding disabled in server configuration file.");
2391 return 0;
2392 }
2393 if (!options.xauth_location ||
2394 (stat(options.xauth_location, &st) == -1)) {
2395 packet_send_debug("No xauth program; cannot forward with spoofing.");
2396 return 0;
2397 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002398 if (options.use_login) {
2399 packet_send_debug("X11 forwarding disabled; "
2400 "not compatible with UseLogin=yes.");
2401 return 0;
2402 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002403 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002404 debug("X11 display already set.");
2405 return 0;
2406 }
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002407 if (x11_create_display_inet(options.x11_display_offset,
2408 options.x11_use_localhost, s->single_connection,
Damien Miller2b9b0452005-07-17 17:19:24 +10002409 &s->display_number, &s->x11_chanids) == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002410 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002411 return 0;
2412 }
Damien Miller2b9b0452005-07-17 17:19:24 +10002413 for (i = 0; s->x11_chanids[i] != -1; i++) {
2414 channel_register_cleanup(s->x11_chanids[i],
Damien Miller39eda6e2005-11-05 14:52:50 +11002415 session_close_single_x11, 0);
Damien Miller2b9b0452005-07-17 17:19:24 +10002416 }
Kevin Steves366298c2001-12-19 17:58:01 +00002417
2418 /* Set up a suitable value for the DISPLAY variable. */
2419 if (gethostname(hostname, sizeof(hostname)) < 0)
2420 fatal("gethostname: %.100s", strerror(errno));
2421 /*
2422 * auth_display must be used as the displayname when the
2423 * authorization entry is added with xauth(1). This will be
2424 * different than the DISPLAY string for localhost displays.
2425 */
Damien Miller95c249f2002-02-05 12:11:34 +11002426 if (options.x11_use_localhost) {
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002427 snprintf(display, sizeof display, "localhost:%u.%u",
Kevin Steves366298c2001-12-19 17:58:01 +00002428 s->display_number, s->screen);
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002429 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
Damien Miller512bccb2002-02-05 12:11:02 +11002430 s->display_number, s->screen);
Kevin Steves366298c2001-12-19 17:58:01 +00002431 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002432 s->auth_display = xstrdup(auth_display);
Kevin Steves366298c2001-12-19 17:58:01 +00002433 } else {
2434#ifdef IPADDR_IN_DISPLAY
2435 struct hostent *he;
2436 struct in_addr my_addr;
2437
2438 he = gethostbyname(hostname);
2439 if (he == NULL) {
2440 error("Can't get IP address for X11 DISPLAY.");
2441 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2442 return 0;
2443 }
2444 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002445 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
Kevin Steves366298c2001-12-19 17:58:01 +00002446 s->display_number, s->screen);
2447#else
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002448 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
Kevin Steves366298c2001-12-19 17:58:01 +00002449 s->display_number, s->screen);
2450#endif
2451 s->display = xstrdup(display);
Damien Miller512bccb2002-02-05 12:11:02 +11002452 s->auth_display = xstrdup(display);
Kevin Steves366298c2001-12-19 17:58:01 +00002453 }
2454
Ben Lindstrom768176b2001-06-09 01:29:12 +00002455 return 1;
2456}
2457
Ben Lindstrombba81212001-06-25 05:01:22 +00002458static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002459do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002460{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002461 server_loop2(authctxt);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002462}
2463
2464void
2465do_cleanup(Authctxt *authctxt)
2466{
2467 static int called = 0;
2468
2469 debug("do_cleanup");
2470
2471 /* no cleanup if we're in the child for login shell */
2472 if (is_child)
2473 return;
2474
2475 /* avoid double cleanup */
2476 if (called)
2477 return;
2478 called = 1;
2479
Damien Miller3f8123c2006-08-19 00:32:46 +10002480 if (authctxt == NULL || !authctxt->authenticated)
Darren Tucker3e33cec2003-10-02 16:12:36 +10002481 return;
2482#ifdef KRB5
2483 if (options.kerberos_ticket_cleanup &&
2484 authctxt->krb5_ctx)
2485 krb5_cleanup_proc(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +10002486#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002487
2488#ifdef GSSAPI
2489 if (compat20 && options.gss_cleanup_creds)
2490 ssh_gssapi_cleanup_creds();
2491#endif
2492
Darren Tucker8846a072003-10-07 11:30:15 +10002493#ifdef USE_PAM
2494 if (options.use_pam) {
2495 sshpam_cleanup();
2496 sshpam_thread_cleanup();
2497 }
2498#endif
2499
Darren Tucker3e33cec2003-10-02 16:12:36 +10002500 /* remove agent socket */
2501 auth_sock_cleanup_proc(authctxt->pw);
2502
2503 /*
2504 * Cleanup ptys/utmp only if privsep is disabled,
2505 * or if running in monitor.
2506 */
2507 if (!use_privsep || mm_is_monitor())
2508 session_destroy_all(session_pty_cleanup2);
Damien Millerefb4afe2000-04-12 18:45:05 +10002509}