]>
Commit | Line | Data |
---|---|---|
5262cbfb | 1 | /* $OpenBSD: session.c,v 1.245 2009/01/22 09:46:01 djm Exp $ */ |
3c0ef626 | 2 | /* |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
5 | * | |
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 | * | |
12 | * SSH2 support by Markus Friedl. | |
13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. | |
14 | * | |
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. | |
34 | */ | |
35 | ||
36 | #include "includes.h" | |
3c0ef626 | 37 | |
30460aeb | 38 | #include <sys/types.h> |
39 | #include <sys/param.h> | |
40 | #ifdef HAVE_SYS_STAT_H | |
41 | # include <sys/stat.h> | |
42 | #endif | |
43 | #include <sys/socket.h> | |
44 | #include <sys/un.h> | |
45 | #include <sys/wait.h> | |
46 | ||
47 | #include <arpa/inet.h> | |
48 | ||
49 | #include <errno.h> | |
50 | #include <grp.h> | |
51 | #ifdef HAVE_PATHS_H | |
52 | #include <paths.h> | |
53 | #endif | |
54 | #include <pwd.h> | |
55 | #include <signal.h> | |
56 | #include <stdarg.h> | |
57 | #include <stdio.h> | |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
60 | #include <unistd.h> | |
61 | ||
5156b1a1 | 62 | #include "openbsd-compat/sys-queue.h" |
30460aeb | 63 | #include "xmalloc.h" |
3c0ef626 | 64 | #include "ssh.h" |
65 | #include "ssh1.h" | |
66 | #include "ssh2.h" | |
3c0ef626 | 67 | #include "sshpty.h" |
68 | #include "packet.h" | |
69 | #include "buffer.h" | |
7e82606e | 70 | #include "match.h" |
3c0ef626 | 71 | #include "uidswap.h" |
72 | #include "compat.h" | |
73 | #include "channels.h" | |
30460aeb | 74 | #include "key.h" |
75 | #include "cipher.h" | |
76 | #ifdef GSSAPI | |
77 | #include "ssh-gss.h" | |
78 | #endif | |
79 | #include "hostfile.h" | |
3c0ef626 | 80 | #include "auth.h" |
81 | #include "auth-options.h" | |
82 | #include "pathnames.h" | |
83 | #include "log.h" | |
84 | #include "servconf.h" | |
85 | #include "sshlogin.h" | |
86 | #include "serverloop.h" | |
87 | #include "canohost.h" | |
e74dc197 | 88 | #include "misc.h" |
3c0ef626 | 89 | #include "session.h" |
2ce0bfe4 | 90 | #include "kex.h" |
350391c5 | 91 | #include "monitor_wrap.h" |
e74dc197 | 92 | #include "sftp.h" |
3c0ef626 | 93 | |
540d72c3 | 94 | #if defined(KRB5) && defined(USE_AFS) |
95 | #include <kafs.h> | |
96 | #endif | |
97 | ||
5262cbfb | 98 | #define IS_INTERNAL_SFTP(c) \ |
99 | (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \ | |
100 | (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \ | |
101 | c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \ | |
102 | c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t')) | |
103 | ||
3c0ef626 | 104 | /* func */ |
105 | ||
106 | Session *session_new(void); | |
5156b1a1 | 107 | void session_set_fds(Session *, int, int, int, int); |
540d72c3 | 108 | void session_pty_cleanup(Session *); |
3c0ef626 | 109 | void session_proctitle(Session *); |
110 | int session_setup_x11fwd(Session *); | |
5156b1a1 | 111 | int do_exec_pty(Session *, const char *); |
112 | int do_exec_no_pty(Session *, const char *); | |
113 | int do_exec(Session *, const char *); | |
3c0ef626 | 114 | void do_login(Session *, const char *); |
115 | #ifdef LOGIN_NEEDS_UTMPX | |
116 | static void do_pre_login(Session *s); | |
117 | #endif | |
118 | void do_child(Session *, const char *); | |
119 | void do_motd(void); | |
120 | int check_quietlogin(Session *, const char *); | |
121 | ||
122 | static void do_authenticated1(Authctxt *); | |
123 | static void do_authenticated2(Authctxt *); | |
124 | ||
3c0ef626 | 125 | static int session_pty_req(Session *); |
126 | ||
75be3237 | 127 | #ifdef SESSION_HOOKS |
128 | static void execute_session_hook(char* prog, Authctxt *authctxt, | |
129 | int startup, int save); | |
130 | #endif | |
131 | ||
3c0ef626 | 132 | /* import */ |
133 | extern ServerOptions options; | |
134 | extern char *__progname; | |
135 | extern int log_stderr; | |
136 | extern int debug_flag; | |
137 | extern u_int utmp_len; | |
138 | extern int startup_pipe; | |
139 | extern void destroy_sensitive_data(void); | |
7cac2b65 | 140 | extern Buffer loginmsg; |
3c0ef626 | 141 | |
142 | /* original command from peer. */ | |
143 | const char *original_command = NULL; | |
144 | ||
145 | /* data */ | |
5156b1a1 | 146 | static int sessions_first_unused = -1; |
147 | static int sessions_nalloc = 0; | |
148 | static Session *sessions = NULL; | |
3c0ef626 | 149 | |
e74dc197 | 150 | #define SUBSYSTEM_NONE 0 |
151 | #define SUBSYSTEM_EXT 1 | |
152 | #define SUBSYSTEM_INT_SFTP 2 | |
153 | ||
3c0ef626 | 154 | #ifdef HAVE_LOGIN_CAP |
350391c5 | 155 | login_cap_t *lc; |
3c0ef626 | 156 | #endif |
157 | ||
540d72c3 | 158 | static int is_child = 0; |
159 | ||
44a053a3 | 160 | /* Name and directory of socket for authentication agent forwarding. */ |
161 | static char *auth_sock_name = NULL; | |
162 | static char *auth_sock_dir = NULL; | |
163 | ||
164 | /* removes the agent forwarding socket */ | |
165 | ||
166 | static void | |
540d72c3 | 167 | auth_sock_cleanup_proc(struct passwd *pw) |
44a053a3 | 168 | { |
44a053a3 | 169 | if (auth_sock_name != NULL) { |
170 | temporarily_use_uid(pw); | |
171 | unlink(auth_sock_name); | |
172 | rmdir(auth_sock_dir); | |
173 | auth_sock_name = NULL; | |
174 | restore_uid(); | |
175 | } | |
176 | } | |
177 | ||
178 | static int | |
179 | auth_input_request_forwarding(struct passwd * pw) | |
180 | { | |
181 | Channel *nc; | |
5156b1a1 | 182 | int sock = -1; |
44a053a3 | 183 | struct sockaddr_un sunaddr; |
184 | ||
185 | if (auth_sock_name != NULL) { | |
186 | error("authentication forwarding requested twice."); | |
187 | return 0; | |
188 | } | |
189 | ||
190 | /* Temporarily drop privileged uid for mkdir/bind. */ | |
191 | temporarily_use_uid(pw); | |
192 | ||
193 | /* Allocate a buffer for the socket name, and format the name. */ | |
5156b1a1 | 194 | auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX"); |
44a053a3 | 195 | |
196 | /* Create private directory for socket */ | |
197 | if (mkdtemp(auth_sock_dir) == NULL) { | |
198 | packet_send_debug("Agent forwarding disabled: " | |
199 | "mkdtemp() failed: %.100s", strerror(errno)); | |
200 | restore_uid(); | |
44a053a3 | 201 | xfree(auth_sock_dir); |
44a053a3 | 202 | auth_sock_dir = NULL; |
5156b1a1 | 203 | goto authsock_err; |
44a053a3 | 204 | } |
5156b1a1 | 205 | |
206 | xasprintf(&auth_sock_name, "%s/agent.%ld", | |
207 | auth_sock_dir, (long) getpid()); | |
44a053a3 | 208 | |
44a053a3 | 209 | /* Create the socket. */ |
210 | sock = socket(AF_UNIX, SOCK_STREAM, 0); | |
5156b1a1 | 211 | if (sock < 0) { |
212 | error("socket: %.100s", strerror(errno)); | |
213 | restore_uid(); | |
214 | goto authsock_err; | |
215 | } | |
44a053a3 | 216 | |
217 | /* Bind it to the name. */ | |
218 | memset(&sunaddr, 0, sizeof(sunaddr)); | |
219 | sunaddr.sun_family = AF_UNIX; | |
220 | strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path)); | |
221 | ||
5156b1a1 | 222 | if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) { |
223 | error("bind: %.100s", strerror(errno)); | |
224 | restore_uid(); | |
225 | goto authsock_err; | |
226 | } | |
44a053a3 | 227 | |
228 | /* Restore the privileged uid. */ | |
229 | restore_uid(); | |
230 | ||
231 | /* Start listening on the socket. */ | |
5156b1a1 | 232 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
233 | error("listen: %.100s", strerror(errno)); | |
234 | goto authsock_err; | |
235 | } | |
44a053a3 | 236 | |
237 | /* Allocate a channel for the authentication agent socket. */ | |
a7213e65 | 238 | /* this shouldn't matter if its hpn or not - cjr */ |
44a053a3 | 239 | nc = channel_new("auth socket", |
240 | SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1, | |
241 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, | |
7cac2b65 | 242 | 0, "auth socket", 1); |
5262cbfb | 243 | nc->path = xstrdup(auth_sock_name); |
44a053a3 | 244 | return 1; |
5156b1a1 | 245 | |
246 | authsock_err: | |
247 | if (auth_sock_name != NULL) | |
248 | xfree(auth_sock_name); | |
249 | if (auth_sock_dir != NULL) { | |
250 | rmdir(auth_sock_dir); | |
251 | xfree(auth_sock_dir); | |
252 | } | |
253 | if (sock != -1) | |
254 | close(sock); | |
255 | auth_sock_name = NULL; | |
256 | auth_sock_dir = NULL; | |
257 | return 0; | |
44a053a3 | 258 | } |
259 | ||
540d72c3 | 260 | static void |
261 | display_loginmsg(void) | |
262 | { | |
2ce0bfe4 | 263 | if (buffer_len(&loginmsg) > 0) { |
264 | buffer_append(&loginmsg, "\0", 1); | |
265 | printf("%s", (char *)buffer_ptr(&loginmsg)); | |
266 | buffer_clear(&loginmsg); | |
267 | } | |
540d72c3 | 268 | } |
44a053a3 | 269 | |
3c0ef626 | 270 | void |
271 | do_authenticated(Authctxt *authctxt) | |
272 | { | |
bfe49944 | 273 | setproctitle("%s", authctxt->pw->pw_name); |
274 | ||
3c0ef626 | 275 | /* setup the channel layer */ |
276 | if (!no_port_forwarding_flag && options.allow_tcp_forwarding) | |
277 | channel_permit_all_opens(); | |
278 | ||
279 | if (compat20) | |
280 | do_authenticated2(authctxt); | |
281 | else | |
282 | do_authenticated1(authctxt); | |
283 | ||
75be3237 | 284 | #ifdef SESSION_HOOKS |
285 | if (options.session_hooks_allow && | |
286 | options.session_hooks_shutdown_cmd) | |
287 | { | |
288 | execute_session_hook(options.session_hooks_shutdown_cmd, | |
289 | authctxt, | |
290 | /* startup = */ 0, /* save = */ 0); | |
291 | ||
292 | if (authctxt->session_env_file) | |
293 | { | |
294 | free(authctxt->session_env_file); | |
295 | } | |
296 | } | |
297 | #endif | |
540d72c3 | 298 | |
299 | do_cleanup(authctxt); | |
3c0ef626 | 300 | } |
301 | ||
302 | /* | |
303 | * Prepares for an interactive session. This is called after the user has | |
304 | * been successfully authenticated. During this message exchange, pseudo | |
305 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings | |
306 | * are requested, etc. | |
307 | */ | |
308 | static void | |
309 | do_authenticated1(Authctxt *authctxt) | |
310 | { | |
311 | Session *s; | |
312 | char *command; | |
e9702f7d | 313 | int success, type, screen_flag; |
276b07a3 | 314 | int enable_compression_after_reply = 0; |
315 | u_int proto_len, data_len, dlen, compression_level = 0; | |
3c0ef626 | 316 | |
317 | s = session_new(); | |
dfddba3d | 318 | if (s == NULL) { |
319 | error("no more sessions"); | |
320 | return; | |
321 | } | |
3c0ef626 | 322 | s->authctxt = authctxt; |
323 | s->pw = authctxt->pw; | |
324 | ||
325 | /* | |
326 | * We stay in this loop until the client requests to execute a shell | |
327 | * or a command. | |
328 | */ | |
329 | for (;;) { | |
330 | success = 0; | |
331 | ||
332 | /* Get a packet from the client. */ | |
e9702f7d | 333 | type = packet_read(); |
3c0ef626 | 334 | |
335 | /* Process the packet. */ | |
336 | switch (type) { | |
337 | case SSH_CMSG_REQUEST_COMPRESSION: | |
3c0ef626 | 338 | compression_level = packet_get_int(); |
e9702f7d | 339 | packet_check_eom(); |
3c0ef626 | 340 | if (compression_level < 1 || compression_level > 9) { |
7e82606e | 341 | packet_send_debug("Received invalid compression level %d.", |
e9702f7d | 342 | compression_level); |
3c0ef626 | 343 | break; |
344 | } | |
2ce0bfe4 | 345 | if (options.compression == COMP_NONE) { |
44a053a3 | 346 | debug2("compression disabled"); |
347 | break; | |
348 | } | |
3c0ef626 | 349 | /* Enable compression after we have responded with SUCCESS. */ |
350 | enable_compression_after_reply = 1; | |
351 | success = 1; | |
352 | break; | |
353 | ||
354 | case SSH_CMSG_REQUEST_PTY: | |
355 | success = session_pty_req(s); | |
356 | break; | |
357 | ||
358 | case SSH_CMSG_X11_REQUEST_FORWARDING: | |
359 | s->auth_proto = packet_get_string(&proto_len); | |
360 | s->auth_data = packet_get_string(&data_len); | |
361 | ||
362 | screen_flag = packet_get_protocol_flags() & | |
363 | SSH_PROTOFLAG_SCREEN_NUMBER; | |
364 | debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); | |
365 | ||
366 | if (packet_remaining() == 4) { | |
367 | if (!screen_flag) | |
368 | debug2("Buggy client: " | |
369 | "X11 screen flag missing"); | |
370 | s->screen = packet_get_int(); | |
371 | } else { | |
372 | s->screen = 0; | |
373 | } | |
e9702f7d | 374 | packet_check_eom(); |
3c0ef626 | 375 | success = session_setup_x11fwd(s); |
376 | if (!success) { | |
377 | xfree(s->auth_proto); | |
378 | xfree(s->auth_data); | |
379 | s->auth_proto = NULL; | |
380 | s->auth_data = NULL; | |
381 | } | |
382 | break; | |
383 | ||
384 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: | |
5156b1a1 | 385 | if (!options.allow_agent_forwarding || |
386 | no_agent_forwarding_flag || compat13) { | |
3c0ef626 | 387 | debug("Authentication agent forwarding not permitted for this authentication."); |
388 | break; | |
389 | } | |
390 | debug("Received authentication agent forwarding request."); | |
391 | success = auth_input_request_forwarding(s->pw); | |
392 | break; | |
393 | ||
394 | case SSH_CMSG_PORT_FORWARD_REQUEST: | |
395 | if (no_port_forwarding_flag) { | |
396 | debug("Port forwarding not permitted for this authentication."); | |
397 | break; | |
398 | } | |
399 | if (!options.allow_tcp_forwarding) { | |
400 | debug("Port forwarding not permitted."); | |
401 | break; | |
402 | } | |
403 | debug("Received TCP/IP port forwarding request."); | |
30460aeb | 404 | if (channel_input_port_forward_request(s->pw->pw_uid == 0, |
d3057ca4 | 405 | options.gateway_ports) < 0) { |
30460aeb | 406 | debug("Port forwarding failed."); |
407 | break; | |
408 | } | |
3c0ef626 | 409 | success = 1; |
410 | break; | |
411 | ||
412 | case SSH_CMSG_MAX_PACKET_SIZE: | |
413 | if (packet_set_maxsize(packet_get_int()) > 0) | |
414 | success = 1; | |
415 | break; | |
e9702f7d | 416 | |
3c0ef626 | 417 | case SSH_CMSG_EXEC_SHELL: |
418 | case SSH_CMSG_EXEC_CMD: | |
419 | if (type == SSH_CMSG_EXEC_CMD) { | |
420 | command = packet_get_string(&dlen); | |
421 | debug("Exec command '%.500s'", command); | |
5156b1a1 | 422 | if (do_exec(s, command) != 0) |
423 | packet_disconnect( | |
424 | "command execution failed"); | |
3c0ef626 | 425 | xfree(command); |
426 | } else { | |
5156b1a1 | 427 | if (do_exec(s, NULL) != 0) |
428 | packet_disconnect( | |
429 | "shell execution failed"); | |
3c0ef626 | 430 | } |
e9702f7d | 431 | packet_check_eom(); |
3c0ef626 | 432 | session_close(s); |
433 | return; | |
434 | ||
435 | default: | |
436 | /* | |
437 | * Any unknown messages in this phase are ignored, | |
438 | * and a failure message is returned. | |
439 | */ | |
7cac2b65 | 440 | logit("Unknown packet type received after authentication: %d", type); |
3c0ef626 | 441 | } |
442 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); | |
443 | packet_send(); | |
444 | packet_write_wait(); | |
445 | ||
446 | /* Enable compression now that we have replied if appropriate. */ | |
447 | if (enable_compression_after_reply) { | |
448 | enable_compression_after_reply = 0; | |
449 | packet_start_compression(compression_level); | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
5156b1a1 | 454 | #define USE_PIPES |
3c0ef626 | 455 | /* |
456 | * This is called to fork and execute a command when we have no tty. This | |
457 | * will call do_child from the child, and server_loop from the parent after | |
458 | * setting up file descriptors and such. | |
459 | */ | |
5156b1a1 | 460 | int |
3c0ef626 | 461 | do_exec_no_pty(Session *s, const char *command) |
462 | { | |
44a053a3 | 463 | pid_t pid; |
3c0ef626 | 464 | |
465 | #ifdef USE_PIPES | |
466 | int pin[2], pout[2], perr[2]; | |
5156b1a1 | 467 | |
3c0ef626 | 468 | /* Allocate pipes for communicating with the program. */ |
5156b1a1 | 469 | if (pipe(pin) < 0) { |
470 | error("%s: pipe in: %.100s", __func__, strerror(errno)); | |
471 | return -1; | |
472 | } | |
473 | if (pipe(pout) < 0) { | |
474 | error("%s: pipe out: %.100s", __func__, strerror(errno)); | |
475 | close(pin[0]); | |
476 | close(pin[1]); | |
477 | return -1; | |
478 | } | |
479 | if (pipe(perr) < 0) { | |
480 | error("%s: pipe err: %.100s", __func__, strerror(errno)); | |
481 | close(pin[0]); | |
482 | close(pin[1]); | |
483 | close(pout[0]); | |
484 | close(pout[1]); | |
485 | return -1; | |
486 | } | |
487 | #else | |
3c0ef626 | 488 | int inout[2], err[2]; |
5156b1a1 | 489 | |
3c0ef626 | 490 | /* Uses socket pairs to communicate with the program. */ |
5156b1a1 | 491 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) { |
492 | error("%s: socketpair #1: %.100s", __func__, strerror(errno)); | |
493 | return -1; | |
494 | } | |
495 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) { | |
496 | error("%s: socketpair #2: %.100s", __func__, strerror(errno)); | |
497 | close(inout[0]); | |
498 | close(inout[1]); | |
499 | return -1; | |
500 | } | |
501 | #endif | |
502 | ||
3c0ef626 | 503 | if (s == NULL) |
504 | fatal("do_exec_no_pty: no session"); | |
505 | ||
506 | session_proctitle(s); | |
507 | ||
3c0ef626 | 508 | /* Fork the child. */ |
5156b1a1 | 509 | switch ((pid = fork())) { |
510 | case -1: | |
511 | error("%s: fork: %.100s", __func__, strerror(errno)); | |
512 | #ifdef USE_PIPES | |
513 | close(pin[0]); | |
514 | close(pin[1]); | |
515 | close(pout[0]); | |
516 | close(pout[1]); | |
517 | close(perr[0]); | |
518 | close(perr[1]); | |
519 | #else | |
520 | close(inout[0]); | |
521 | close(inout[1]); | |
522 | close(err[0]); | |
523 | close(err[1]); | |
524 | #endif | |
525 | return -1; | |
526 | case 0: | |
540d72c3 | 527 | is_child = 1; |
d03f4262 | 528 | |
3c0ef626 | 529 | /* Child. Reinitialize the log since the pid has changed. */ |
5156b1a1 | 530 | log_init(__progname, options.log_level, |
531 | options.log_facility, log_stderr); | |
3c0ef626 | 532 | |
533 | /* | |
534 | * Create a new session and process group since the 4.4BSD | |
535 | * setlogin() affects the entire process group. | |
536 | */ | |
537 | if (setsid() < 0) | |
538 | error("setsid failed: %.100s", strerror(errno)); | |
539 | ||
540 | #ifdef USE_PIPES | |
541 | /* | |
542 | * Redirect stdin. We close the parent side of the socket | |
543 | * pair, and make the child side the standard input. | |
544 | */ | |
545 | close(pin[1]); | |
546 | if (dup2(pin[0], 0) < 0) | |
547 | perror("dup2 stdin"); | |
548 | close(pin[0]); | |
549 | ||
550 | /* Redirect stdout. */ | |
551 | close(pout[0]); | |
552 | if (dup2(pout[1], 1) < 0) | |
553 | perror("dup2 stdout"); | |
554 | close(pout[1]); | |
555 | ||
556 | /* Redirect stderr. */ | |
557 | close(perr[0]); | |
558 | if (dup2(perr[1], 2) < 0) | |
559 | perror("dup2 stderr"); | |
560 | close(perr[1]); | |
5156b1a1 | 561 | #else |
3c0ef626 | 562 | /* |
563 | * Redirect stdin, stdout, and stderr. Stdin and stdout will | |
564 | * use the same socket, as some programs (particularly rdist) | |
565 | * seem to depend on it. | |
566 | */ | |
567 | close(inout[1]); | |
568 | close(err[1]); | |
569 | if (dup2(inout[0], 0) < 0) /* stdin */ | |
570 | perror("dup2 stdin"); | |
5156b1a1 | 571 | if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */ |
3c0ef626 | 572 | perror("dup2 stdout"); |
5156b1a1 | 573 | close(inout[0]); |
3c0ef626 | 574 | if (dup2(err[0], 2) < 0) /* stderr */ |
575 | perror("dup2 stderr"); | |
5156b1a1 | 576 | close(err[0]); |
577 | #endif | |
578 | ||
3c0ef626 | 579 | |
d03f4262 | 580 | #ifdef _UNICOS |
581 | cray_init_job(s->pw); /* set up cray jid and tmpdir */ | |
582 | #endif | |
583 | ||
3c0ef626 | 584 | /* Do processing for the child (exec command etc). */ |
585 | do_child(s, command); | |
586 | /* NOTREACHED */ | |
5156b1a1 | 587 | default: |
588 | break; | |
3c0ef626 | 589 | } |
5156b1a1 | 590 | |
d03f4262 | 591 | #ifdef _UNICOS |
592 | signal(WJSIGNAL, cray_job_termination_handler); | |
593 | #endif /* _UNICOS */ | |
3c0ef626 | 594 | #ifdef HAVE_CYGWIN |
595 | if (is_winnt) | |
596 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); | |
597 | #endif | |
5156b1a1 | 598 | |
3c0ef626 | 599 | s->pid = pid; |
600 | /* Set interactive/non-interactive mode. */ | |
601 | packet_set_interactive(s->display != NULL); | |
5156b1a1 | 602 | |
603 | /* | |
604 | * Clear loginmsg, since it's the child's responsibility to display | |
605 | * it to the user, otherwise multiple sessions may accumulate | |
606 | * multiple copies of the login messages. | |
607 | */ | |
608 | buffer_clear(&loginmsg); | |
609 | ||
3c0ef626 | 610 | #ifdef USE_PIPES |
611 | /* We are the parent. Close the child sides of the pipes. */ | |
612 | close(pin[0]); | |
613 | close(pout[1]); | |
614 | close(perr[1]); | |
615 | ||
616 | if (compat20) { | |
7e82606e | 617 | if (s->is_subsystem) { |
618 | close(perr[0]); | |
619 | perr[0] = -1; | |
620 | } | |
5156b1a1 | 621 | session_set_fds(s, pin[1], pout[0], perr[0], 0); |
3c0ef626 | 622 | } else { |
623 | /* Enter the interactive session. */ | |
624 | server_loop(pid, pin[1], pout[0], perr[0]); | |
625 | /* server_loop has closed pin[1], pout[0], and perr[0]. */ | |
626 | } | |
5156b1a1 | 627 | #else |
3c0ef626 | 628 | /* We are the parent. Close the child sides of the socket pairs. */ |
629 | close(inout[0]); | |
630 | close(err[0]); | |
631 | ||
632 | /* | |
633 | * Enter the interactive session. Note: server_loop must be able to | |
634 | * handle the case that fdin and fdout are the same. | |
635 | */ | |
636 | if (compat20) { | |
5156b1a1 | 637 | session_set_fds(s, inout[1], inout[1], |
638 | s->is_subsystem ? -1 : err[1], 0); | |
639 | if (s->is_subsystem) | |
640 | close(err[1]); | |
3c0ef626 | 641 | } else { |
642 | server_loop(pid, inout[1], inout[1], err[1]); | |
643 | /* server_loop has closed inout[1] and err[1]. */ | |
644 | } | |
5156b1a1 | 645 | #endif |
646 | return 0; | |
3c0ef626 | 647 | } |
648 | ||
649 | /* | |
650 | * This is called to fork and execute a command when we have a tty. This | |
651 | * will call do_child from the child, and server_loop from the parent after | |
652 | * setting up file descriptors, controlling tty, updating wtmp, utmp, | |
653 | * lastlog, and other such operations. | |
654 | */ | |
5156b1a1 | 655 | int |
3c0ef626 | 656 | do_exec_pty(Session *s, const char *command) |
657 | { | |
658 | int fdout, ptyfd, ttyfd, ptymaster; | |
659 | pid_t pid; | |
660 | ||
661 | if (s == NULL) | |
662 | fatal("do_exec_pty: no session"); | |
663 | ptyfd = s->ptyfd; | |
664 | ttyfd = s->ttyfd; | |
665 | ||
5156b1a1 | 666 | /* |
667 | * Create another descriptor of the pty master side for use as the | |
668 | * standard input. We could use the original descriptor, but this | |
669 | * simplifies code in server_loop. The descriptor is bidirectional. | |
670 | * Do this before forking (and cleanup in the child) so as to | |
671 | * detect and gracefully fail out-of-fd conditions. | |
672 | */ | |
673 | if ((fdout = dup(ptyfd)) < 0) { | |
674 | error("%s: dup #1: %s", __func__, strerror(errno)); | |
675 | close(ttyfd); | |
676 | close(ptyfd); | |
677 | return -1; | |
678 | } | |
679 | /* we keep a reference to the pty master */ | |
680 | if ((ptymaster = dup(ptyfd)) < 0) { | |
681 | error("%s: dup #2: %s", __func__, strerror(errno)); | |
682 | close(ttyfd); | |
683 | close(ptyfd); | |
684 | close(fdout); | |
685 | return -1; | |
686 | } | |
687 | ||
3c0ef626 | 688 | /* Fork the child. */ |
5156b1a1 | 689 | switch ((pid = fork())) { |
690 | case -1: | |
691 | error("%s: fork: %.100s", __func__, strerror(errno)); | |
692 | close(fdout); | |
693 | close(ptymaster); | |
694 | close(ttyfd); | |
695 | close(ptyfd); | |
696 | return -1; | |
697 | case 0: | |
540d72c3 | 698 | is_child = 1; |
3c0ef626 | 699 | |
5156b1a1 | 700 | close(fdout); |
701 | close(ptymaster); | |
702 | ||
3c0ef626 | 703 | /* Child. Reinitialize the log because the pid has changed. */ |
5156b1a1 | 704 | log_init(__progname, options.log_level, |
705 | options.log_facility, log_stderr); | |
3c0ef626 | 706 | /* Close the master side of the pseudo tty. */ |
707 | close(ptyfd); | |
708 | ||
709 | /* Make the pseudo tty our controlling tty. */ | |
710 | pty_make_controlling_tty(&ttyfd, s->tty); | |
711 | ||
712 | /* Redirect stdin/stdout/stderr from the pseudo tty. */ | |
713 | if (dup2(ttyfd, 0) < 0) | |
714 | error("dup2 stdin: %s", strerror(errno)); | |
715 | if (dup2(ttyfd, 1) < 0) | |
716 | error("dup2 stdout: %s", strerror(errno)); | |
717 | if (dup2(ttyfd, 2) < 0) | |
718 | error("dup2 stderr: %s", strerror(errno)); | |
719 | ||
720 | /* Close the extra descriptor for the pseudo tty. */ | |
721 | close(ttyfd); | |
722 | ||
723 | /* record login, etc. similar to login(1) */ | |
724 | #ifndef HAVE_OSF_SIA | |
d03f4262 | 725 | if (!(options.use_login && command == NULL)) { |
726 | #ifdef _UNICOS | |
727 | cray_init_job(s->pw); /* set up cray jid and tmpdir */ | |
728 | #endif /* _UNICOS */ | |
3c0ef626 | 729 | do_login(s, command); |
d03f4262 | 730 | } |
3c0ef626 | 731 | # ifdef LOGIN_NEEDS_UTMPX |
732 | else | |
733 | do_pre_login(s); | |
734 | # endif | |
735 | #endif | |
5156b1a1 | 736 | /* |
737 | * Do common processing for the child, such as execing | |
738 | * the command. | |
739 | */ | |
740 | do_child(s, command); | |
741 | /* NOTREACHED */ | |
742 | default: | |
743 | break; | |
3c0ef626 | 744 | } |
5156b1a1 | 745 | |
d03f4262 | 746 | #ifdef _UNICOS |
747 | signal(WJSIGNAL, cray_job_termination_handler); | |
748 | #endif /* _UNICOS */ | |
3c0ef626 | 749 | #ifdef HAVE_CYGWIN |
750 | if (is_winnt) | |
751 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); | |
752 | #endif | |
5156b1a1 | 753 | |
3c0ef626 | 754 | s->pid = pid; |
755 | ||
756 | /* Parent. Close the slave side of the pseudo tty. */ | |
757 | close(ttyfd); | |
758 | ||
3c0ef626 | 759 | /* Enter interactive session. */ |
5156b1a1 | 760 | s->ptymaster = ptymaster; |
3c0ef626 | 761 | packet_set_interactive(1); |
762 | if (compat20) { | |
5156b1a1 | 763 | session_set_fds(s, ptyfd, fdout, -1, 1); |
3c0ef626 | 764 | } else { |
765 | server_loop(pid, ptyfd, fdout, -1); | |
766 | /* server_loop _has_ closed ptyfd and fdout. */ | |
767 | } | |
5156b1a1 | 768 | return 0; |
3c0ef626 | 769 | } |
770 | ||
771 | #ifdef LOGIN_NEEDS_UTMPX | |
772 | static void | |
773 | do_pre_login(Session *s) | |
774 | { | |
775 | socklen_t fromlen; | |
776 | struct sockaddr_storage from; | |
777 | pid_t pid = getpid(); | |
778 | ||
779 | /* | |
780 | * Get IP address of client. If the connection is not a socket, let | |
781 | * the address be 0.0.0.0. | |
782 | */ | |
783 | memset(&from, 0, sizeof(from)); | |
d03f4262 | 784 | fromlen = sizeof(from); |
3c0ef626 | 785 | if (packet_connection_is_on_socket()) { |
3c0ef626 | 786 | if (getpeername(packet_get_connection_in(), |
30460aeb | 787 | (struct sockaddr *)&from, &fromlen) < 0) { |
3c0ef626 | 788 | debug("getpeername: %.100s", strerror(errno)); |
540d72c3 | 789 | cleanup_exit(255); |
3c0ef626 | 790 | } |
791 | } | |
792 | ||
793 | record_utmp_only(pid, s->tty, s->pw->pw_name, | |
7cac2b65 | 794 | get_remote_name_or_ip(utmp_len, options.use_dns), |
bfe49944 | 795 | (struct sockaddr *)&from, fromlen); |
3c0ef626 | 796 | } |
797 | #endif | |
798 | ||
799 | /* | |
800 | * This is called to fork and execute a command. If another command is | |
801 | * to be forced, execute that instead. | |
802 | */ | |
5156b1a1 | 803 | int |
3c0ef626 | 804 | do_exec(Session *s, const char *command) |
805 | { | |
5156b1a1 | 806 | int ret; |
807 | ||
30460aeb | 808 | if (options.adm_forced_command) { |
809 | original_command = command; | |
810 | command = options.adm_forced_command; | |
5262cbfb | 811 | if (IS_INTERNAL_SFTP(command)) |
e74dc197 | 812 | s->is_subsystem = SUBSYSTEM_INT_SFTP; |
813 | else if (s->is_subsystem) | |
814 | s->is_subsystem = SUBSYSTEM_EXT; | |
30460aeb | 815 | debug("Forced command (config) '%.900s'", command); |
816 | } else if (forced_command) { | |
3c0ef626 | 817 | original_command = command; |
818 | command = forced_command; | |
5262cbfb | 819 | if (IS_INTERNAL_SFTP(command)) |
e74dc197 | 820 | s->is_subsystem = SUBSYSTEM_INT_SFTP; |
821 | else if (s->is_subsystem) | |
822 | s->is_subsystem = SUBSYSTEM_EXT; | |
30460aeb | 823 | debug("Forced command (key option) '%.900s'", command); |
3c0ef626 | 824 | } |
825 | ||
7cac2b65 | 826 | #if defined(SESSION_HOOKS) |
827 | if (options.session_hooks_allow && | |
828 | (options.session_hooks_startup_cmd || | |
829 | options.session_hooks_shutdown_cmd)) | |
830 | { | |
831 | char env_file[1000]; | |
832 | struct stat st; | |
833 | do | |
834 | { | |
835 | snprintf(env_file, | |
836 | sizeof(env_file), | |
837 | "/tmp/ssh_env_%d%d%d", | |
838 | getuid(), | |
839 | getpid(), | |
840 | rand()); | |
841 | } while (stat(env_file, &st)==0); | |
842 | s->authctxt->session_env_file = strdup(env_file); | |
843 | } | |
844 | #endif | |
845 | ||
dfddba3d | 846 | #ifdef SSH_AUDIT_EVENTS |
847 | if (command != NULL) | |
848 | PRIVSEP(audit_run_command(command)); | |
849 | else if (s->ttyfd == -1) { | |
850 | char *shell = s->pw->pw_shell; | |
851 | ||
852 | if (shell[0] == '\0') /* empty shell means /bin/sh */ | |
853 | shell =_PATH_BSHELL; | |
854 | PRIVSEP(audit_run_command(shell)); | |
855 | } | |
856 | #endif | |
3c0ef626 | 857 | if (s->ttyfd != -1) |
5156b1a1 | 858 | ret = do_exec_pty(s, command); |
3c0ef626 | 859 | else |
5156b1a1 | 860 | ret = do_exec_no_pty(s, command); |
3c0ef626 | 861 | |
862 | original_command = NULL; | |
3c0ef626 | 863 | |
7e82606e | 864 | /* |
865 | * Clear loginmsg: it's the child's responsibility to display | |
866 | * it to the user, otherwise multiple sessions may accumulate | |
867 | * multiple copies of the login messages. | |
868 | */ | |
869 | buffer_clear(&loginmsg); | |
5156b1a1 | 870 | |
871 | return ret; | |
7e82606e | 872 | } |
e9702f7d | 873 | |
3c0ef626 | 874 | /* administrative, login(1)-like work */ |
875 | void | |
876 | do_login(Session *s, const char *command) | |
877 | { | |
3c0ef626 | 878 | socklen_t fromlen; |
879 | struct sockaddr_storage from; | |
3c0ef626 | 880 | struct passwd * pw = s->pw; |
881 | pid_t pid = getpid(); | |
882 | ||
883 | /* | |
884 | * Get IP address of client. If the connection is not a socket, let | |
885 | * the address be 0.0.0.0. | |
886 | */ | |
887 | memset(&from, 0, sizeof(from)); | |
bfe49944 | 888 | fromlen = sizeof(from); |
3c0ef626 | 889 | if (packet_connection_is_on_socket()) { |
3c0ef626 | 890 | if (getpeername(packet_get_connection_in(), |
e9702f7d | 891 | (struct sockaddr *) & from, &fromlen) < 0) { |
3c0ef626 | 892 | debug("getpeername: %.100s", strerror(errno)); |
540d72c3 | 893 | cleanup_exit(255); |
3c0ef626 | 894 | } |
895 | } | |
896 | ||
3c0ef626 | 897 | /* Record that there was a login on that tty from the remote host. */ |
350391c5 | 898 | if (!use_privsep) |
899 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, | |
900 | get_remote_name_or_ip(utmp_len, | |
7cac2b65 | 901 | options.use_dns), |
d03f4262 | 902 | (struct sockaddr *)&from, fromlen); |
3c0ef626 | 903 | |
904 | #ifdef USE_PAM | |
905 | /* | |
906 | * If password change is needed, do it now. | |
907 | * This needs to occur before the ~/.hushlogin check. | |
908 | */ | |
540d72c3 | 909 | if (options.use_pam && !use_privsep && s->authctxt->force_pwchange) { |
910 | display_loginmsg(); | |
3c0ef626 | 911 | do_pam_chauthtok(); |
540d72c3 | 912 | s->authctxt->force_pwchange = 0; |
7cac2b65 | 913 | /* XXX - signal [net] parent to enable forwardings */ |
3c0ef626 | 914 | } |
915 | #endif | |
916 | ||
917 | if (check_quietlogin(s, command)) | |
918 | return; | |
919 | ||
540d72c3 | 920 | display_loginmsg(); |
3c0ef626 | 921 | |
3c0ef626 | 922 | do_motd(); |
923 | } | |
924 | ||
925 | /* | |
926 | * Display the message of the day. | |
927 | */ | |
928 | void | |
929 | do_motd(void) | |
930 | { | |
931 | FILE *f; | |
932 | char buf[256]; | |
933 | ||
934 | if (options.print_motd) { | |
935 | #ifdef HAVE_LOGIN_CAP | |
936 | f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", | |
937 | "/etc/motd"), "r"); | |
938 | #else | |
939 | f = fopen("/etc/motd", "r"); | |
940 | #endif | |
941 | if (f) { | |
942 | while (fgets(buf, sizeof(buf), f)) | |
943 | fputs(buf, stdout); | |
944 | fclose(f); | |
945 | } | |
946 | } | |
947 | } | |
948 | ||
949 | ||
950 | /* | |
951 | * Check for quiet login, either .hushlogin or command given. | |
952 | */ | |
953 | int | |
954 | check_quietlogin(Session *s, const char *command) | |
955 | { | |
956 | char buf[256]; | |
957 | struct passwd *pw = s->pw; | |
958 | struct stat st; | |
959 | ||
960 | /* Return 1 if .hushlogin exists or a command given. */ | |
961 | if (command != NULL) | |
962 | return 1; | |
963 | snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); | |
964 | #ifdef HAVE_LOGIN_CAP | |
965 | if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) | |
966 | return 1; | |
967 | #else | |
968 | if (stat(buf, &st) >= 0) | |
969 | return 1; | |
970 | #endif | |
971 | return 0; | |
972 | } | |
973 | ||
974 | /* | |
975 | * Sets the value of the given variable in the environment. If the variable | |
5262cbfb | 976 | * already exists, its value is overridden. |
3c0ef626 | 977 | */ |
5598e598 | 978 | void |
3c0ef626 | 979 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
e9702f7d | 980 | const char *value) |
3c0ef626 | 981 | { |
3c0ef626 | 982 | char **env; |
29d88157 | 983 | u_int envsize; |
984 | u_int i, namelen; | |
3c0ef626 | 985 | |
7cac2b65 | 986 | /* |
987 | * If we're passed an uninitialized list, allocate a single null | |
988 | * entry before continuing. | |
989 | */ | |
990 | if (*envp == NULL && *envsizep == 0) { | |
991 | *envp = xmalloc(sizeof(char *)); | |
992 | *envp[0] = NULL; | |
993 | *envsizep = 1; | |
994 | } | |
995 | ||
3c0ef626 | 996 | /* |
997 | * Find the slot where the value should be stored. If the variable | |
998 | * already exists, we reuse the slot; otherwise we append a new slot | |
999 | * at the end of the array, expanding if necessary. | |
1000 | */ | |
1001 | env = *envp; | |
1002 | namelen = strlen(name); | |
1003 | for (i = 0; env[i]; i++) | |
1004 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') | |
1005 | break; | |
1006 | if (env[i]) { | |
1007 | /* Reuse the slot. */ | |
1008 | xfree(env[i]); | |
1009 | } else { | |
1010 | /* New variable. Expand if necessary. */ | |
29d88157 | 1011 | envsize = *envsizep; |
1012 | if (i >= envsize - 1) { | |
1013 | if (envsize >= 1000) | |
1014 | fatal("child_set_env: too many env vars"); | |
1015 | envsize += 50; | |
30460aeb | 1016 | env = (*envp) = xrealloc(env, envsize, sizeof(char *)); |
29d88157 | 1017 | *envsizep = envsize; |
3c0ef626 | 1018 | } |
1019 | /* Need to set the NULL pointer at end of array beyond the new slot. */ | |
1020 | env[i + 1] = NULL; | |
1021 | } | |
1022 | ||
1023 | /* Allocate space and format the variable in the appropriate slot. */ | |
1024 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); | |
1025 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); | |
1026 | } | |
1027 | ||
1028 | /* | |
1029 | * Reads environment variables from the given file and adds/overrides them | |
1030 | * into the environment. If the file does not exist, this does nothing. | |
1031 | * Otherwise, it must consist of empty lines, comments (line starts with '#') | |
1032 | * and assignments of the form name=value. No other forms are allowed. | |
1033 | */ | |
1034 | static void | |
1035 | read_environment_file(char ***env, u_int *envsize, | |
e9702f7d | 1036 | const char *filename) |
3c0ef626 | 1037 | { |
1038 | FILE *f; | |
1039 | char buf[4096]; | |
1040 | char *cp, *value; | |
276b07a3 | 1041 | u_int lineno = 0; |
3c0ef626 | 1042 | |
1043 | f = fopen(filename, "r"); | |
1044 | if (!f) | |
1045 | return; | |
1046 | ||
1047 | while (fgets(buf, sizeof(buf), f)) { | |
276b07a3 | 1048 | if (++lineno > 1000) |
1049 | fatal("Too many lines in environment file %s", filename); | |
3c0ef626 | 1050 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
1051 | ; | |
1052 | if (!*cp || *cp == '#' || *cp == '\n') | |
1053 | continue; | |
e74dc197 | 1054 | |
1055 | cp[strcspn(cp, "\n")] = '\0'; | |
1056 | ||
3c0ef626 | 1057 | value = strchr(cp, '='); |
1058 | if (value == NULL) { | |
276b07a3 | 1059 | fprintf(stderr, "Bad line %u in %.100s\n", lineno, |
1060 | filename); | |
3c0ef626 | 1061 | continue; |
1062 | } | |
1063 | /* | |
1064 | * Replace the equals sign by nul, and advance value to | |
1065 | * the value string. | |
1066 | */ | |
1067 | *value = '\0'; | |
1068 | value++; | |
1069 | child_set_env(env, envsize, cp, value); | |
1070 | } | |
1071 | fclose(f); | |
1072 | } | |
1073 | ||
75be3237 | 1074 | #ifdef SESSION_HOOKS |
1075 | #define SSH_SESSION_ENV_FILE "SSH_SESSION_ENV_FILE" | |
1076 | ||
1077 | typedef enum { no_op, execute, clear_env, restore_env, | |
1078 | read_env, save_or_rm_env } session_action_t; | |
1079 | ||
1080 | static session_action_t action_order[2][5] = { | |
1081 | { clear_env, read_env, execute, save_or_rm_env, restore_env }, /*shutdown*/ | |
1082 | { execute, read_env, save_or_rm_env, no_op, no_op } /*startup */ | |
1083 | }; | |
1084 | ||
1085 | static | |
1086 | void execute_session_hook(char* prog, Authctxt *authctxt, | |
1087 | int startup, int save) | |
1088 | { | |
1089 | extern char **environ; | |
1090 | ||
1091 | struct stat st; | |
1092 | char **saved_env, **tmpenv; | |
1093 | char *env_file = authctxt->session_env_file; | |
1094 | int i, status = 0; | |
1095 | ||
1096 | for (i=0; i<5; i++) | |
1097 | { | |
1098 | switch (action_order[startup][i]) | |
1099 | { | |
1100 | case no_op: | |
1101 | break; | |
1102 | ||
1103 | case execute: | |
1104 | { | |
1105 | FILE* fp; | |
1106 | char buf[1000]; | |
1107 | ||
1108 | snprintf(buf, | |
1109 | sizeof(buf), | |
1110 | "%s -c '%s'", | |
1111 | authctxt->pw->pw_shell, | |
1112 | prog); | |
1113 | ||
1114 | debug("executing session hook: [%s]", buf); | |
1115 | setenv(SSH_SESSION_ENV_FILE, env_file, /* overwrite = */ 1); | |
1116 | ||
1117 | /* flusing is recommended in the popen(3) man page, to avoid | |
1118 | intermingling of output */ | |
1119 | fflush(stdout); | |
1120 | fflush(stderr); | |
1121 | if ((fp=popen(buf, "w")) == NULL) | |
1122 | { | |
1123 | perror("Unable to run session hook"); | |
1124 | return; | |
1125 | } | |
1126 | status = pclose(fp); | |
1127 | debug2("session hook executed, status=%d", status); | |
1128 | unsetenv(SSH_SESSION_ENV_FILE); | |
1129 | } | |
1130 | break; | |
1131 | ||
1132 | case clear_env: | |
1133 | saved_env = environ; | |
1134 | tmpenv = (char**) malloc(sizeof(char*)); | |
1135 | tmpenv[0] = NULL; | |
1136 | environ = tmpenv; | |
1137 | break; | |
1138 | ||
1139 | case restore_env: | |
1140 | environ = saved_env; | |
1141 | free(tmpenv); | |
1142 | break; | |
1143 | ||
1144 | case read_env: | |
1145 | if (status==0 && stat(env_file, &st)==0) | |
1146 | { | |
1147 | int envsize = 0; | |
1148 | ||
1149 | debug("reading environment from %s", env_file); | |
1150 | while (environ[envsize++]) ; | |
1151 | read_environment_file(&environ, &envsize, env_file); | |
1152 | } | |
1153 | break; | |
1154 | ||
1155 | case save_or_rm_env: | |
1156 | if (status==0 && save) | |
1157 | { | |
1158 | FILE* fp; | |
1159 | int envcount=0; | |
1160 | ||
1161 | debug2("saving environment to %s", env_file); | |
1162 | if ((fp = fopen(env_file, "w")) == NULL) /* hmm: file perms? */ | |
1163 | { | |
1164 | perror("Unable to save session hook info"); | |
1165 | } | |
1166 | while (environ[envcount]) | |
1167 | { | |
1168 | fprintf(fp, "%s\n", environ[envcount++]); | |
1169 | } | |
1170 | fflush(fp); | |
1171 | fclose(fp); | |
1172 | } | |
1173 | else if (stat(env_file, &st)==0) | |
1174 | { | |
1175 | debug2("removing environment file %s", env_file); | |
1176 | remove(env_file); | |
1177 | } | |
1178 | break; | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | } | |
1183 | #endif | |
1184 | ||
7cac2b65 | 1185 | #ifdef HAVE_ETC_DEFAULT_LOGIN |
1186 | /* | |
1187 | * Return named variable from specified environment, or NULL if not present. | |
1188 | */ | |
1189 | static char * | |
1190 | child_get_env(char **env, const char *name) | |
1191 | { | |
1192 | int i; | |
1193 | size_t len; | |
1194 | ||
1195 | len = strlen(name); | |
1196 | for (i=0; env[i] != NULL; i++) | |
1197 | if (strncmp(name, env[i], len) == 0 && env[i][len] == '=') | |
1198 | return(env[i] + len + 1); | |
1199 | return NULL; | |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * Read /etc/default/login. | |
1204 | * We pick up the PATH (or SUPATH for root) and UMASK. | |
1205 | */ | |
1206 | static void | |
1207 | read_etc_default_login(char ***env, u_int *envsize, uid_t uid) | |
1208 | { | |
1209 | char **tmpenv = NULL, *var; | |
29d88157 | 1210 | u_int i, tmpenvsize = 0; |
540d72c3 | 1211 | u_long mask; |
7cac2b65 | 1212 | |
1213 | /* | |
1214 | * We don't want to copy the whole file to the child's environment, | |
1215 | * so we use a temporary environment and copy the variables we're | |
1216 | * interested in. | |
1217 | */ | |
1218 | read_environment_file(&tmpenv, &tmpenvsize, "/etc/default/login"); | |
1219 | ||
29d88157 | 1220 | if (tmpenv == NULL) |
1221 | return; | |
1222 | ||
7cac2b65 | 1223 | if (uid == 0) |
1224 | var = child_get_env(tmpenv, "SUPATH"); | |
1225 | else | |
1226 | var = child_get_env(tmpenv, "PATH"); | |
1227 | if (var != NULL) | |
1228 | child_set_env(env, envsize, "PATH", var); | |
540d72c3 | 1229 | |
7cac2b65 | 1230 | if ((var = child_get_env(tmpenv, "UMASK")) != NULL) |
1231 | if (sscanf(var, "%5lo", &mask) == 1) | |
540d72c3 | 1232 | umask((mode_t)mask); |
1233 | ||
7cac2b65 | 1234 | for (i = 0; tmpenv[i] != NULL; i++) |
1235 | xfree(tmpenv[i]); | |
1236 | xfree(tmpenv); | |
1237 | } | |
1238 | #endif /* HAVE_ETC_DEFAULT_LOGIN */ | |
1239 | ||
2ce0bfe4 | 1240 | void |
1241 | copy_environment(char **source, char ***env, u_int *envsize) | |
3c0ef626 | 1242 | { |
e9702f7d | 1243 | char *var_name, *var_val; |
3c0ef626 | 1244 | int i; |
1245 | ||
e9702f7d | 1246 | if (source == NULL) |
3c0ef626 | 1247 | return; |
1248 | ||
e9702f7d | 1249 | for(i = 0; source[i] != NULL; i++) { |
1250 | var_name = xstrdup(source[i]); | |
1251 | if ((var_val = strstr(var_name, "=")) == NULL) { | |
1252 | xfree(var_name); | |
3c0ef626 | 1253 | continue; |
3c0ef626 | 1254 | } |
e9702f7d | 1255 | *var_val++ = '\0'; |
3c0ef626 | 1256 | |
e9702f7d | 1257 | debug3("Copy environment: %s=%s", var_name, var_val); |
1258 | child_set_env(env, envsize, var_name, var_val); | |
540d72c3 | 1259 | |
e9702f7d | 1260 | xfree(var_name); |
3c0ef626 | 1261 | } |
3c0ef626 | 1262 | } |
1263 | ||
e9702f7d | 1264 | static char ** |
1265 | do_setup_env(Session *s, const char *shell) | |
3c0ef626 | 1266 | { |
3c0ef626 | 1267 | char buf[256]; |
e9702f7d | 1268 | u_int i, envsize; |
30460aeb | 1269 | char **env, *laddr; |
e9702f7d | 1270 | struct passwd *pw = s->pw; |
30460aeb | 1271 | #ifndef HAVE_LOGIN_CAP |
1272 | char *path = NULL; | |
1273 | #endif | |
3c0ef626 | 1274 | |
1275 | /* Initialize the environment. */ | |
1276 | envsize = 100; | |
30460aeb | 1277 | env = xcalloc(envsize, sizeof(char *)); |
3c0ef626 | 1278 | env[0] = NULL; |
1279 | ||
1280 | #ifdef HAVE_CYGWIN | |
1281 | /* | |
1282 | * The Windows environment contains some setting which are | |
1283 | * important for a running system. They must not be dropped. | |
1284 | */ | |
dfddba3d | 1285 | { |
1286 | char **p; | |
1287 | ||
1288 | p = fetch_windows_environment(); | |
1289 | copy_environment(p, &env, &envsize); | |
1290 | free_windows_environment(p); | |
1291 | } | |
3c0ef626 | 1292 | #endif |
1293 | ||
5598e598 | 1294 | #ifdef GSSAPI |
540d72c3 | 1295 | /* Allow any GSSAPI methods that we've used to alter |
5598e598 | 1296 | * the childs environment as they see fit |
1297 | */ | |
7cac2b65 | 1298 | ssh_gssapi_do_child(&env, &envsize); |
5598e598 | 1299 | #endif |
1300 | ||
3c0ef626 | 1301 | if (!options.use_login) { |
1302 | /* Set basic environment. */ | |
7e82606e | 1303 | for (i = 0; i < s->num_env; i++) |
1304 | child_set_env(&env, &envsize, s->env[i].name, | |
1305 | s->env[i].val); | |
1306 | ||
3c0ef626 | 1307 | child_set_env(&env, &envsize, "USER", pw->pw_name); |
1308 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); | |
bfe49944 | 1309 | #ifdef _AIX |
1310 | child_set_env(&env, &envsize, "LOGIN", pw->pw_name); | |
1311 | #endif | |
3c0ef626 | 1312 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); |
1313 | #ifdef HAVE_LOGIN_CAP | |
d03f4262 | 1314 | if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0) |
1315 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); | |
1316 | else | |
1317 | child_set_env(&env, &envsize, "PATH", getenv("PATH")); | |
3c0ef626 | 1318 | #else /* HAVE_LOGIN_CAP */ |
1319 | # ifndef HAVE_CYGWIN | |
1320 | /* | |
1321 | * There's no standard path on Windows. The path contains | |
1322 | * important components pointing to the system directories, | |
1323 | * needed for loading shared libraries. So the path better | |
1324 | * remains intact here. | |
1325 | */ | |
7cac2b65 | 1326 | # ifdef HAVE_ETC_DEFAULT_LOGIN |
1327 | read_etc_default_login(&env, &envsize, pw->pw_uid); | |
1328 | path = child_get_env(env, "PATH"); | |
1329 | # endif /* HAVE_ETC_DEFAULT_LOGIN */ | |
1330 | if (path == NULL || *path == '\0') { | |
540d72c3 | 1331 | child_set_env(&env, &envsize, "PATH", |
7cac2b65 | 1332 | s->pw->pw_uid == 0 ? |
1333 | SUPERUSER_PATH : _PATH_STDPATH); | |
1334 | } | |
3c0ef626 | 1335 | # endif /* HAVE_CYGWIN */ |
1336 | #endif /* HAVE_LOGIN_CAP */ | |
1337 | ||
1338 | snprintf(buf, sizeof buf, "%.200s/%.50s", | |
1339 | _PATH_MAILDIR, pw->pw_name); | |
1340 | child_set_env(&env, &envsize, "MAIL", buf); | |
1341 | ||
1342 | /* Normal systems set SHELL by default. */ | |
1343 | child_set_env(&env, &envsize, "SHELL", shell); | |
1344 | } | |
1345 | if (getenv("TZ")) | |
1346 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); | |
1347 | ||
1752c22a | 1348 | #ifdef GSI /* GSI shared libs typically installed in non-system locations. */ |
1349 | { | |
1350 | char *cp; | |
1351 | ||
1352 | if ((cp = getenv("LD_LIBRARY_PATH")) != NULL) | |
1353 | child_set_env(&env, &envsize, "LD_LIBRARY_PATH", cp); | |
1354 | if ((cp = getenv("LIBPATH")) != NULL) | |
1355 | child_set_env(&env, &envsize, "LIBPATH", cp); | |
1356 | if ((cp = getenv("SHLIB_PATH")) != NULL) | |
1357 | child_set_env(&env, &envsize, "SHLIB_PATH", cp); | |
1358 | if ((cp = getenv("LD_LIBRARYN32_PATH")) != NULL) | |
1359 | child_set_env(&env, &envsize, "LD_LIBRARYN32_PATH",cp); | |
1360 | if ((cp = getenv("LD_LIBRARY64_PATH")) != NULL) | |
1361 | child_set_env(&env, &envsize, "LD_LIBRARY64_PATH",cp); | |
1362 | } | |
1363 | #endif | |
1364 | ||
3c0ef626 | 1365 | /* Set custom environment options from RSA authentication. */ |
1366 | if (!options.use_login) { | |
1367 | while (custom_environment) { | |
1368 | struct envstring *ce = custom_environment; | |
d03f4262 | 1369 | char *str = ce->s; |
e9702f7d | 1370 | |
d03f4262 | 1371 | for (i = 0; str[i] != '=' && str[i]; i++) |
3c0ef626 | 1372 | ; |
d03f4262 | 1373 | if (str[i] == '=') { |
1374 | str[i] = 0; | |
1375 | child_set_env(&env, &envsize, str, str + i + 1); | |
3c0ef626 | 1376 | } |
1377 | custom_environment = ce->next; | |
1378 | xfree(ce->s); | |
1379 | xfree(ce); | |
1380 | } | |
1381 | } | |
1382 | ||
d03f4262 | 1383 | /* SSH_CLIENT deprecated */ |
3c0ef626 | 1384 | snprintf(buf, sizeof buf, "%.50s %d %d", |
e9702f7d | 1385 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
3c0ef626 | 1386 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
1387 | ||
bfe49944 | 1388 | laddr = get_local_ipaddr(packet_get_connection_in()); |
d03f4262 | 1389 | snprintf(buf, sizeof buf, "%.50s %d %.50s %d", |
bfe49944 | 1390 | get_remote_ipaddr(), get_remote_port(), laddr, get_local_port()); |
1391 | xfree(laddr); | |
d03f4262 | 1392 | child_set_env(&env, &envsize, "SSH_CONNECTION", buf); |
1393 | ||
3c0ef626 | 1394 | if (s->ttyfd != -1) |
1395 | child_set_env(&env, &envsize, "SSH_TTY", s->tty); | |
1396 | if (s->term) | |
1397 | child_set_env(&env, &envsize, "TERM", s->term); | |
1398 | if (s->display) | |
1399 | child_set_env(&env, &envsize, "DISPLAY", s->display); | |
1400 | if (original_command) | |
1401 | child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", | |
1402 | original_command); | |
1403 | ||
d03f4262 | 1404 | #ifdef _UNICOS |
1405 | if (cray_tmpdir[0] != '\0') | |
1406 | child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir); | |
1407 | #endif /* _UNICOS */ | |
1408 | ||
dfddba3d | 1409 | /* |
1410 | * Since we clear KRB5CCNAME at startup, if it's set now then it | |
1411 | * must have been set by a native authentication method (eg AIX or | |
1412 | * SIA), so copy it to the child. | |
1413 | */ | |
1414 | { | |
1415 | char *cp; | |
1416 | ||
1417 | if ((cp = getenv("KRB5CCNAME")) != NULL) | |
1418 | child_set_env(&env, &envsize, "KRB5CCNAME", cp); | |
1419 | } | |
1420 | ||
3c0ef626 | 1421 | #ifdef _AIX |
e9702f7d | 1422 | { |
1423 | char *cp; | |
1424 | ||
1425 | if ((cp = getenv("AUTHSTATE")) != NULL) | |
1426 | child_set_env(&env, &envsize, "AUTHSTATE", cp); | |
e9702f7d | 1427 | read_environment_file(&env, &envsize, "/etc/environment"); |
1428 | } | |
3c0ef626 | 1429 | #endif |
3c0ef626 | 1430 | #ifdef KRB5 |
12a403af | 1431 | if (s->authctxt->krb5_ccname) |
3c0ef626 | 1432 | child_set_env(&env, &envsize, "KRB5CCNAME", |
12a403af | 1433 | s->authctxt->krb5_ccname); |
3c0ef626 | 1434 | #endif |
1435 | #ifdef USE_PAM | |
d03f4262 | 1436 | /* |
1437 | * Pull in any environment variables that may have | |
1438 | * been set by PAM. | |
1439 | */ | |
7cac2b65 | 1440 | if (options.use_pam) { |
540d72c3 | 1441 | char **p; |
d03f4262 | 1442 | |
540d72c3 | 1443 | p = fetch_pam_child_environment(); |
1444 | copy_environment(p, &env, &envsize); | |
1445 | free_pam_environment(p); | |
1446 | ||
1447 | p = fetch_pam_environment(); | |
d03f4262 | 1448 | copy_environment(p, &env, &envsize); |
1449 | free_pam_environment(p); | |
1450 | } | |
3c0ef626 | 1451 | #endif /* USE_PAM */ |
1452 | ||
44a053a3 | 1453 | if (auth_sock_name != NULL) |
3c0ef626 | 1454 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, |
44a053a3 | 1455 | auth_sock_name); |
3c0ef626 | 1456 | |
1457 | /* read $HOME/.ssh/environment. */ | |
d03f4262 | 1458 | if (options.permit_user_env && !options.use_login) { |
3c0ef626 | 1459 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", |
d03f4262 | 1460 | strcmp(pw->pw_dir, "/") ? pw->pw_dir : ""); |
3c0ef626 | 1461 | read_environment_file(&env, &envsize, buf); |
1462 | } | |
1463 | if (debug_flag) { | |
1464 | /* dump the environment */ | |
1465 | fprintf(stderr, "Environment:\n"); | |
1466 | for (i = 0; env[i]; i++) | |
1467 | fprintf(stderr, " %.200s\n", env[i]); | |
1468 | } | |
e9702f7d | 1469 | return env; |
1470 | } | |
1471 | ||
1472 | /* | |
1473 | * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found | |
1474 | * first in this order). | |
1475 | */ | |
1476 | static void | |
1477 | do_rc_files(Session *s, const char *shell) | |
1478 | { | |
1479 | FILE *f = NULL; | |
1480 | char cmd[1024]; | |
1481 | int do_xauth; | |
1482 | struct stat st; | |
1483 | ||
1484 | do_xauth = | |
1485 | s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; | |
1486 | ||
e74dc197 | 1487 | /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */ |
1488 | if (!s->is_subsystem && options.adm_forced_command == NULL && | |
5156b1a1 | 1489 | !no_user_rc && stat(_PATH_SSH_USER_RC, &st) >= 0) { |
e9702f7d | 1490 | snprintf(cmd, sizeof cmd, "%s -c '%s %s'", |
1491 | shell, _PATH_BSHELL, _PATH_SSH_USER_RC); | |
1492 | if (debug_flag) | |
1493 | fprintf(stderr, "Running %s\n", cmd); | |
1494 | f = popen(cmd, "w"); | |
1495 | if (f) { | |
1496 | if (do_xauth) | |
1497 | fprintf(f, "%s %s\n", s->auth_proto, | |
1498 | s->auth_data); | |
1499 | pclose(f); | |
1500 | } else | |
1501 | fprintf(stderr, "Could not run %s\n", | |
1502 | _PATH_SSH_USER_RC); | |
1503 | } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { | |
1504 | if (debug_flag) | |
1505 | fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, | |
1506 | _PATH_SSH_SYSTEM_RC); | |
1507 | f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); | |
1508 | if (f) { | |
1509 | if (do_xauth) | |
1510 | fprintf(f, "%s %s\n", s->auth_proto, | |
1511 | s->auth_data); | |
1512 | pclose(f); | |
1513 | } else | |
1514 | fprintf(stderr, "Could not run %s\n", | |
1515 | _PATH_SSH_SYSTEM_RC); | |
1516 | } else if (do_xauth && options.xauth_location != NULL) { | |
1517 | /* Add authority data to .Xauthority if appropriate. */ | |
1518 | if (debug_flag) { | |
1519 | fprintf(stderr, | |
bfe49944 | 1520 | "Running %.500s remove %.100s\n", |
540d72c3 | 1521 | options.xauth_location, s->auth_display); |
bfe49944 | 1522 | fprintf(stderr, |
1523 | "%.500s add %.100s %.100s %.100s\n", | |
e9702f7d | 1524 | options.xauth_location, s->auth_display, |
1525 | s->auth_proto, s->auth_data); | |
1526 | } | |
1527 | snprintf(cmd, sizeof cmd, "%s -q -", | |
1528 | options.xauth_location); | |
1529 | f = popen(cmd, "w"); | |
1530 | if (f) { | |
bfe49944 | 1531 | fprintf(f, "remove %s\n", |
1532 | s->auth_display); | |
e9702f7d | 1533 | fprintf(f, "add %s %s %s\n", |
1534 | s->auth_display, s->auth_proto, | |
1535 | s->auth_data); | |
1536 | pclose(f); | |
1537 | } else { | |
1538 | fprintf(stderr, "Could not run %s\n", | |
1539 | cmd); | |
1540 | } | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | static void | |
1545 | do_nologin(struct passwd *pw) | |
1546 | { | |
1547 | FILE *f = NULL; | |
1548 | char buf[1024]; | |
1549 | ||
1550 | #ifdef HAVE_LOGIN_CAP | |
1551 | if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) | |
1552 | f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, | |
1553 | _PATH_NOLOGIN), "r"); | |
1554 | #else | |
1555 | if (pw->pw_uid) | |
1556 | f = fopen(_PATH_NOLOGIN, "r"); | |
1557 | #endif | |
1558 | if (f) { | |
1559 | /* /etc/nologin exists. Print its contents and exit. */ | |
7cac2b65 | 1560 | logit("User %.100s not allowed because %s exists", |
d03f4262 | 1561 | pw->pw_name, _PATH_NOLOGIN); |
e9702f7d | 1562 | while (fgets(buf, sizeof(buf), f)) |
1563 | fputs(buf, stderr); | |
1564 | fclose(f); | |
bfe49944 | 1565 | fflush(NULL); |
e9702f7d | 1566 | exit(254); |
1567 | } | |
1568 | } | |
1569 | ||
e74dc197 | 1570 | /* |
1571 | * Chroot into a directory after checking it for safety: all path components | |
1572 | * must be root-owned directories with strict permissions. | |
1573 | */ | |
1574 | static void | |
1575 | safely_chroot(const char *path, uid_t uid) | |
1576 | { | |
1577 | const char *cp; | |
1578 | char component[MAXPATHLEN]; | |
1579 | struct stat st; | |
1580 | ||
1581 | if (*path != '/') | |
1582 | fatal("chroot path does not begin at root"); | |
1583 | if (strlen(path) >= sizeof(component)) | |
1584 | fatal("chroot path too long"); | |
1585 | ||
1586 | /* | |
1587 | * Descend the path, checking that each component is a | |
1588 | * root-owned directory with strict permissions. | |
1589 | */ | |
1590 | for (cp = path; cp != NULL;) { | |
1591 | if ((cp = strchr(cp, '/')) == NULL) | |
1592 | strlcpy(component, path, sizeof(component)); | |
1593 | else { | |
1594 | cp++; | |
1595 | memcpy(component, path, cp - path); | |
1596 | component[cp - path] = '\0'; | |
1597 | } | |
1598 | ||
1599 | debug3("%s: checking '%s'", __func__, component); | |
1600 | ||
1601 | if (stat(component, &st) != 0) | |
1602 | fatal("%s: stat(\"%s\"): %s", __func__, | |
1603 | component, strerror(errno)); | |
1604 | if (st.st_uid != 0 || (st.st_mode & 022) != 0) | |
1605 | fatal("bad ownership or modes for chroot " | |
1606 | "directory %s\"%s\"", | |
1607 | cp == NULL ? "" : "component ", component); | |
1608 | if (!S_ISDIR(st.st_mode)) | |
1609 | fatal("chroot path %s\"%s\" is not a directory", | |
1610 | cp == NULL ? "" : "component ", component); | |
1611 | ||
1612 | } | |
1613 | ||
1614 | if (chdir(path) == -1) | |
1615 | fatal("Unable to chdir to chroot path \"%s\": " | |
1616 | "%s", path, strerror(errno)); | |
1617 | if (chroot(path) == -1) | |
1618 | fatal("chroot(\"%s\"): %s", path, strerror(errno)); | |
1619 | if (chdir("/") == -1) | |
1620 | fatal("%s: chdir(/) after chroot: %s", | |
1621 | __func__, strerror(errno)); | |
1622 | verbose("Changed root directory to \"%s\"", path); | |
1623 | } | |
1624 | ||
e9702f7d | 1625 | /* Set login name, uid, gid, and groups. */ |
350391c5 | 1626 | void |
e9702f7d | 1627 | do_setusercontext(struct passwd *pw) |
1628 | { | |
e74dc197 | 1629 | char *chroot_path, *tmp; |
1630 | ||
1631 | #ifdef WITH_SELINUX | |
1632 | /* Cache selinux status for later use */ | |
1633 | (void)ssh_selinux_enabled(); | |
1634 | #endif | |
1635 | ||
bfe49944 | 1636 | #ifndef HAVE_CYGWIN |
1637 | if (getuid() == 0 || geteuid() == 0) | |
e9702f7d | 1638 | #endif /* HAVE_CYGWIN */ |
bfe49944 | 1639 | { |
1640 | ||
44a053a3 | 1641 | #ifdef HAVE_SETPCRED |
7cac2b65 | 1642 | if (setpcred(pw->pw_name, (char **)NULL) == -1) |
1643 | fatal("Failed to set process credentials"); | |
44a053a3 | 1644 | #endif /* HAVE_SETPCRED */ |
e9702f7d | 1645 | #ifdef HAVE_LOGIN_CAP |
d03f4262 | 1646 | # ifdef __bsdi__ |
276b07a3 | 1647 | setpgid(0, 0); |
d03f4262 | 1648 | # endif |
540d72c3 | 1649 | # ifdef USE_PAM |
1650 | if (options.use_pam) { | |
fa0f0f45 | 1651 | do_pam_setcred(use_privsep); |
540d72c3 | 1652 | } |
1653 | # endif /* USE_PAM */ | |
e9702f7d | 1654 | if (setusercontext(lc, pw, pw->pw_uid, |
e74dc197 | 1655 | (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) { |
e9702f7d | 1656 | perror("unable to set user context"); |
1657 | exit(1); | |
1658 | } | |
1659 | #else | |
1660 | # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) | |
1661 | /* Sets login uid for accounting */ | |
1662 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) | |
1663 | error("setluid: %s", strerror(errno)); | |
1664 | # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ | |
1665 | ||
1666 | if (setlogin(pw->pw_name) < 0) | |
1667 | error("setlogin failed: %s", strerror(errno)); | |
1668 | if (setgid(pw->pw_gid) < 0) { | |
1669 | perror("setgid"); | |
1670 | exit(1); | |
1671 | } | |
1672 | /* Initialize the group list. */ | |
1673 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { | |
1674 | perror("initgroups"); | |
1675 | exit(1); | |
1676 | } | |
1677 | endgrent(); | |
1678 | # ifdef USE_PAM | |
1679 | /* | |
540d72c3 | 1680 | * PAM credentials may take the form of supplementary groups. |
e9702f7d | 1681 | * These will have been wiped by the above initgroups() call. |
1682 | * Reestablish them here. | |
1683 | */ | |
7cac2b65 | 1684 | if (options.use_pam) { |
fa0f0f45 | 1685 | do_pam_setcred(use_privsep); |
7cac2b65 | 1686 | } |
e9702f7d | 1687 | # endif /* USE_PAM */ |
1688 | # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) | |
1689 | irix_setusercontext(pw); | |
e74dc197 | 1690 | # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ |
276b07a3 | 1691 | # ifdef _AIX |
d03f4262 | 1692 | aix_usrinfo(pw); |
276b07a3 | 1693 | # endif /* _AIX */ |
e74dc197 | 1694 | # ifdef USE_LIBIAF |
2ce0bfe4 | 1695 | if (set_id(pw->pw_name) != 0) { |
1696 | exit(1); | |
1697 | } | |
e74dc197 | 1698 | # endif /* USE_LIBIAF */ |
1699 | #endif | |
1700 | ||
1701 | if (options.chroot_directory != NULL && | |
1702 | strcasecmp(options.chroot_directory, "none") != 0) { | |
1703 | tmp = tilde_expand_filename(options.chroot_directory, | |
1704 | pw->pw_uid); | |
1705 | chroot_path = percent_expand(tmp, "h", pw->pw_dir, | |
1706 | "u", pw->pw_name, (char *)NULL); | |
1707 | safely_chroot(chroot_path, pw->pw_uid); | |
1708 | free(tmp); | |
1709 | free(chroot_path); | |
1710 | } | |
1711 | ||
1712 | #ifdef HAVE_LOGIN_CAP | |
1713 | if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) { | |
1714 | perror("unable to set user context (setuser)"); | |
1715 | exit(1); | |
1716 | } | |
1717 | #else | |
e9702f7d | 1718 | /* Permanently switch to the desired uid. */ |
1719 | permanently_set_uid(pw); | |
1720 | #endif | |
1721 | } | |
bfe49944 | 1722 | |
1723 | #ifdef HAVE_CYGWIN | |
1724 | if (is_winnt) | |
1725 | #endif | |
e9702f7d | 1726 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
1727 | fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); | |
30460aeb | 1728 | |
1729 | #ifdef WITH_SELINUX | |
1730 | ssh_selinux_setup_exec_context(pw->pw_name); | |
1731 | #endif | |
e9702f7d | 1732 | } |
1733 | ||
540d72c3 | 1734 | static void |
1735 | do_pwchange(Session *s) | |
1736 | { | |
7e82606e | 1737 | fflush(NULL); |
540d72c3 | 1738 | fprintf(stderr, "WARNING: Your password has expired.\n"); |
1739 | if (s->ttyfd != -1) { | |
7e82606e | 1740 | fprintf(stderr, |
540d72c3 | 1741 | "You must change your password now and login again!\n"); |
dfddba3d | 1742 | #ifdef PASSWD_NEEDS_USERNAME |
1743 | execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name, | |
1744 | (char *)NULL); | |
1745 | #else | |
540d72c3 | 1746 | execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL); |
dfddba3d | 1747 | #endif |
540d72c3 | 1748 | perror("passwd"); |
1749 | } else { | |
1750 | fprintf(stderr, | |
1751 | "Password change required but no TTY available.\n"); | |
1752 | } | |
1753 | exit(1); | |
1754 | } | |
1755 | ||
350391c5 | 1756 | static void |
1757 | launch_login(struct passwd *pw, const char *hostname) | |
1758 | { | |
1759 | /* Launch login(1). */ | |
1760 | ||
44a053a3 | 1761 | execl(LOGIN_PROGRAM, "login", "-h", hostname, |
350391c5 | 1762 | #ifdef xxxLOGIN_NEEDS_TERM |
44a053a3 | 1763 | (s->term ? s->term : "unknown"), |
350391c5 | 1764 | #endif /* LOGIN_NEEDS_TERM */ |
1765 | #ifdef LOGIN_NO_ENDOPT | |
1766 | "-p", "-f", pw->pw_name, (char *)NULL); | |
1767 | #else | |
1768 | "-p", "-f", "--", pw->pw_name, (char *)NULL); | |
1769 | #endif | |
1770 | ||
1771 | /* Login couldn't be executed, die. */ | |
1772 | ||
1773 | perror("login"); | |
1774 | exit(1); | |
1775 | } | |
1776 | ||
540d72c3 | 1777 | static void |
1778 | child_close_fds(void) | |
1779 | { | |
1780 | int i; | |
1781 | ||
1782 | if (packet_get_connection_in() == packet_get_connection_out()) | |
1783 | close(packet_get_connection_in()); | |
1784 | else { | |
1785 | close(packet_get_connection_in()); | |
1786 | close(packet_get_connection_out()); | |
1787 | } | |
1788 | /* | |
1789 | * Close all descriptors related to channels. They will still remain | |
1790 | * open in the parent. | |
1791 | */ | |
1792 | /* XXX better use close-on-exec? -markus */ | |
1793 | channel_close_all(); | |
1794 | ||
1795 | /* | |
1796 | * Close any extra file descriptors. Note that there may still be | |
1797 | * descriptors left by system functions. They will be closed later. | |
1798 | */ | |
1799 | endpwent(); | |
1800 | ||
1801 | /* | |
08822d99 | 1802 | * Close any extra open file descriptors so that we don't have them |
540d72c3 | 1803 | * hanging around in clients. Note that we want to do this after |
1804 | * initgroups, because at least on Solaris 2.3 it leaves file | |
1805 | * descriptors open. | |
1806 | */ | |
1807 | for (i = 3; i < 64; i++) | |
1808 | close(i); | |
1809 | } | |
1810 | ||
e9702f7d | 1811 | /* |
1812 | * Performs common processing for the child, such as setting up the | |
1813 | * environment, closing extra file descriptors, setting the user and group | |
1814 | * ids, and executing the command or shell. | |
1815 | */ | |
e74dc197 | 1816 | #define ARGV_MAX 10 |
e9702f7d | 1817 | void |
1818 | do_child(Session *s, const char *command) | |
1819 | { | |
1820 | extern char **environ; | |
1821 | char **env; | |
e74dc197 | 1822 | char *argv[ARGV_MAX]; |
e9702f7d | 1823 | const char *shell, *shell0, *hostname = NULL; |
1824 | struct passwd *pw = s->pw; | |
5156b1a1 | 1825 | int r = 0; |
e9702f7d | 1826 | |
62eb343a | 1827 | #ifdef AFS_KRB5 |
1828 | /* Default place to look for aklog. */ | |
1829 | #ifdef AKLOG_PATH | |
1830 | #define KPROGDIR AKLOG_PATH | |
1831 | #else | |
1832 | #define KPROGDIR "/usr/bin/aklog" | |
1833 | #endif /* AKLOG_PATH */ | |
1834 | ||
1835 | struct stat st; | |
1836 | char *aklog_path; | |
1837 | #endif /* AFS_KRB5 */ | |
1838 | ||
e9702f7d | 1839 | /* remove hostkey from the child's memory */ |
1840 | destroy_sensitive_data(); | |
1841 | ||
540d72c3 | 1842 | /* Force a password change */ |
1843 | if (s->authctxt->force_pwchange) { | |
1844 | do_setusercontext(pw); | |
1845 | child_close_fds(); | |
1846 | do_pwchange(s); | |
1847 | exit(1); | |
1848 | } | |
1849 | ||
e9702f7d | 1850 | /* login(1) is only called if we execute the login shell */ |
1851 | if (options.use_login && command != NULL) | |
1852 | options.use_login = 0; | |
1853 | ||
d03f4262 | 1854 | #ifdef _UNICOS |
1855 | cray_setup(pw->pw_uid, pw->pw_name, command); | |
1856 | #endif /* _UNICOS */ | |
1857 | ||
e9702f7d | 1858 | /* |
1859 | * Login(1) does this as well, and it needs uid 0 for the "-h" | |
1860 | * switch, so we let login(1) to this for us. | |
1861 | */ | |
1862 | if (!options.use_login) { | |
1863 | #ifdef HAVE_OSF_SIA | |
bfe49944 | 1864 | session_setup_sia(pw, s->ttyfd == -1 ? NULL : s->tty); |
e9702f7d | 1865 | if (!check_quietlogin(s, command)) |
1866 | do_motd(); | |
1867 | #else /* HAVE_OSF_SIA */ | |
08822d99 | 1868 | /* When PAM is enabled we rely on it to do the nologin check */ |
1869 | if (!options.use_pam) | |
1870 | do_nologin(pw); | |
e9702f7d | 1871 | do_setusercontext(pw); |
7e82606e | 1872 | /* |
1873 | * PAM session modules in do_setusercontext may have | |
1874 | * generated messages, so if this in an interactive | |
1875 | * login then display them too. | |
1876 | */ | |
dfddba3d | 1877 | if (!check_quietlogin(s, command)) |
7e82606e | 1878 | display_loginmsg(); |
e9702f7d | 1879 | #endif /* HAVE_OSF_SIA */ |
1880 | } | |
1881 | ||
dfddba3d | 1882 | #ifdef USE_PAM |
8b32eddc | 1883 | if (options.use_pam && !options.use_login && !is_pam_session_open()) { |
1884 | debug3("PAM session not opened, exiting"); | |
dfddba3d | 1885 | display_loginmsg(); |
1886 | exit(254); | |
1887 | } | |
1888 | #endif | |
1889 | ||
e9702f7d | 1890 | /* |
1891 | * Get the shell from the password data. An empty shell field is | |
1892 | * legal, and means /bin/sh. | |
1893 | */ | |
1894 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; | |
bfe49944 | 1895 | |
1896 | /* | |
1897 | * Make sure $SHELL points to the shell from the password file, | |
1898 | * even if shell is overridden from login.conf | |
1899 | */ | |
1900 | env = do_setup_env(s, shell); | |
1901 | ||
e9702f7d | 1902 | #ifdef HAVE_LOGIN_CAP |
1903 | shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); | |
1904 | #endif | |
1905 | ||
3c0ef626 | 1906 | /* we have to stash the hostname before we close our socket. */ |
1907 | if (options.use_login) | |
1908 | hostname = get_remote_name_or_ip(utmp_len, | |
7cac2b65 | 1909 | options.use_dns); |
3c0ef626 | 1910 | /* |
1911 | * Close the connection descriptors; note that this is the child, and | |
1912 | * the server will still have the socket open, and it is important | |
1913 | * that we do not shutdown it. Note that the descriptors cannot be | |
1914 | * closed before building the environment, as we call | |
1915 | * get_remote_ipaddr there. | |
1916 | */ | |
540d72c3 | 1917 | child_close_fds(); |
3c0ef626 | 1918 | |
1919 | /* | |
540d72c3 | 1920 | * Must take new environment into use so that .ssh/rc, |
1921 | * /etc/ssh/sshrc and xauth are run in the proper environment. | |
3c0ef626 | 1922 | */ |
540d72c3 | 1923 | environ = env; |
3c0ef626 | 1924 | |
540d72c3 | 1925 | #if defined(KRB5) && defined(USE_AFS) |
3c0ef626 | 1926 | /* |
540d72c3 | 1927 | * At this point, we check to see if AFS is active and if we have |
1928 | * a valid Kerberos 5 TGT. If so, it seems like a good idea to see | |
1929 | * if we can (and need to) extend the ticket into an AFS token. If | |
1930 | * we don't do this, we run into potential problems if the user's | |
1931 | * home directory is in AFS and it's not world-readable. | |
3c0ef626 | 1932 | */ |
3c0ef626 | 1933 | |
540d72c3 | 1934 | if (options.kerberos_get_afs_token && k_hasafs() && |
2ce0bfe4 | 1935 | (s->authctxt->krb5_ctx != NULL)) { |
540d72c3 | 1936 | char cell[64]; |
1937 | ||
1938 | debug("Getting AFS token"); | |
1939 | ||
1940 | k_setpag(); | |
1941 | ||
1942 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) | |
1943 | krb5_afslog(s->authctxt->krb5_ctx, | |
1944 | s->authctxt->krb5_fwd_ccache, cell, NULL); | |
1945 | ||
1946 | krb5_afslog_home(s->authctxt->krb5_ctx, | |
1947 | s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir); | |
1948 | } | |
1949 | #endif | |
3c0ef626 | 1950 | |
62eb343a | 1951 | #ifdef AFS_KRB5 |
1952 | ||
1953 | /* User has authenticated, and if a ticket was going to be | |
1954 | * passed we would have it. KRB5CCNAME should already be set. | |
1955 | * Now try to get an AFS token using aklog. | |
1956 | */ | |
1957 | if (k_hasafs()) { /* Do we have AFS? */ | |
1958 | ||
1959 | aklog_path = xstrdup(KPROGDIR); | |
1960 | ||
1961 | /* | |
1962 | * Make sure it exists before we try to run it | |
1963 | */ | |
1964 | if (stat(aklog_path, &st) == 0) { | |
1965 | debug("Running %s to get afs token.",aklog_path); | |
1966 | system(aklog_path); | |
1967 | } else { | |
1968 | debug("%s does not exist.",aklog_path); | |
1969 | } | |
1970 | ||
1971 | xfree(aklog_path); | |
1972 | } | |
1973 | #endif /* AFS_KRB5 */ | |
1974 | ||
75be3237 | 1975 | #ifdef SESSION_HOOKS |
1976 | if (options.session_hooks_allow && | |
1977 | options.session_hooks_startup_cmd) | |
1978 | { | |
1979 | execute_session_hook(options.session_hooks_startup_cmd, | |
1980 | s->authctxt, | |
1981 | /* startup = */ 1, | |
1982 | options.session_hooks_shutdown_cmd != NULL); | |
1983 | } | |
1984 | #endif | |
3c0ef626 | 1985 | |
08822d99 | 1986 | /* Change current directory to the user's home directory. */ |
3c0ef626 | 1987 | if (chdir(pw->pw_dir) < 0) { |
5156b1a1 | 1988 | /* Suppress missing homedir warning for chroot case */ |
3c0ef626 | 1989 | #ifdef HAVE_LOGIN_CAP |
5156b1a1 | 1990 | r = login_getcapbool(lc, "requirehome", 0); |
3c0ef626 | 1991 | #endif |
5156b1a1 | 1992 | if (r || options.chroot_directory == NULL) |
1993 | fprintf(stderr, "Could not chdir to home " | |
1994 | "directory %s: %s\n", pw->pw_dir, | |
1995 | strerror(errno)); | |
1996 | if (r) | |
1997 | exit(1); | |
3c0ef626 | 1998 | } |
1999 | ||
e74dc197 | 2000 | closefrom(STDERR_FILENO + 1); |
2001 | ||
e9702f7d | 2002 | if (!options.use_login) |
2003 | do_rc_files(s, shell); | |
3c0ef626 | 2004 | |
2005 | /* restore SIGPIPE for child */ | |
30460aeb | 2006 | signal(SIGPIPE, SIG_DFL); |
3c0ef626 | 2007 | |
e74dc197 | 2008 | if (s->is_subsystem == SUBSYSTEM_INT_SFTP) { |
2009 | extern int optind, optreset; | |
2010 | int i; | |
2011 | char *p, *args; | |
2012 | ||
2013 | setproctitle("%s@internal-sftp-server", s->pw->pw_name); | |
5262cbfb | 2014 | args = xstrdup(command ? command : "sftp-server"); |
e74dc197 | 2015 | for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " "))) |
2016 | if (i < ARGV_MAX - 1) | |
2017 | argv[i++] = p; | |
2018 | argv[i] = NULL; | |
2019 | optind = optreset = 1; | |
2020 | __progname = argv[0]; | |
2021 | exit(sftp_server_main(i, argv, s->pw)); | |
2022 | } | |
2023 | ||
e9702f7d | 2024 | if (options.use_login) { |
350391c5 | 2025 | launch_login(pw, hostname); |
2026 | /* NEVERREACHED */ | |
e9702f7d | 2027 | } |
2028 | ||
2029 | /* Get the last component of the shell name. */ | |
2030 | if ((shell0 = strrchr(shell, '/')) != NULL) | |
2031 | shell0++; | |
2032 | else | |
2033 | shell0 = shell; | |
2034 | ||
3c0ef626 | 2035 | /* |
2036 | * If we have no command, execute the shell. In this case, the shell | |
2037 | * name to be passed in argv[0] is preceded by '-' to indicate that | |
2038 | * this is a login shell. | |
2039 | */ | |
2040 | if (!command) { | |
e9702f7d | 2041 | char argv0[256]; |
3c0ef626 | 2042 | |
e9702f7d | 2043 | /* Start the shell. Set initial character to '-'. */ |
2044 | argv0[0] = '-'; | |
3c0ef626 | 2045 | |
e9702f7d | 2046 | if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1) |
2047 | >= sizeof(argv0) - 1) { | |
2048 | errno = EINVAL; | |
3c0ef626 | 2049 | perror(shell); |
2050 | exit(1); | |
e9702f7d | 2051 | } |
3c0ef626 | 2052 | |
e9702f7d | 2053 | /* Execute the shell. */ |
2054 | argv[0] = argv0; | |
2055 | argv[1] = NULL; | |
5ab1e3bd | 2056 | execve(shell, argv, environ); |
3c0ef626 | 2057 | |
e9702f7d | 2058 | /* Executing the shell failed. */ |
2059 | perror(shell); | |
2060 | exit(1); | |
3c0ef626 | 2061 | } |
2062 | /* | |
2063 | * Execute the command using the user's shell. This uses the -c | |
2064 | * option to execute the command. | |
2065 | */ | |
e9702f7d | 2066 | argv[0] = (char *) shell0; |
3c0ef626 | 2067 | argv[1] = "-c"; |
2068 | argv[2] = (char *) command; | |
2069 | argv[3] = NULL; | |
5ab1e3bd | 2070 | execve(shell, argv, environ); |
3c0ef626 | 2071 | perror(shell); |
2072 | exit(1); | |
2073 | } | |
2074 | ||
5156b1a1 | 2075 | void |
2076 | session_unused(int id) | |
2077 | { | |
2078 | debug3("%s: session id %d unused", __func__, id); | |
2079 | if (id >= options.max_sessions || | |
2080 | id >= sessions_nalloc) { | |
2081 | fatal("%s: insane session id %d (max %d nalloc %d)", | |
2082 | __func__, id, options.max_sessions, sessions_nalloc); | |
2083 | } | |
2084 | bzero(&sessions[id], sizeof(*sessions)); | |
2085 | sessions[id].self = id; | |
2086 | sessions[id].used = 0; | |
2087 | sessions[id].chanid = -1; | |
2088 | sessions[id].ptyfd = -1; | |
2089 | sessions[id].ttyfd = -1; | |
2090 | sessions[id].ptymaster = -1; | |
2091 | sessions[id].x11_chanids = NULL; | |
2092 | sessions[id].next_unused = sessions_first_unused; | |
2093 | sessions_first_unused = id; | |
2094 | } | |
2095 | ||
3c0ef626 | 2096 | Session * |
2097 | session_new(void) | |
2098 | { | |
5156b1a1 | 2099 | Session *s, *tmp; |
2100 | ||
2101 | if (sessions_first_unused == -1) { | |
2102 | if (sessions_nalloc >= options.max_sessions) | |
2103 | return NULL; | |
2104 | debug2("%s: allocate (allocated %d max %d)", | |
2105 | __func__, sessions_nalloc, options.max_sessions); | |
2106 | tmp = xrealloc(sessions, sessions_nalloc + 1, | |
2107 | sizeof(*sessions)); | |
2108 | if (tmp == NULL) { | |
2109 | error("%s: cannot allocate %d sessions", | |
2110 | __func__, sessions_nalloc + 1); | |
2111 | return NULL; | |
3c0ef626 | 2112 | } |
5156b1a1 | 2113 | sessions = tmp; |
2114 | session_unused(sessions_nalloc++); | |
3c0ef626 | 2115 | } |
5156b1a1 | 2116 | |
2117 | if (sessions_first_unused >= sessions_nalloc || | |
2118 | sessions_first_unused < 0) { | |
2119 | fatal("%s: insane first_unused %d max %d nalloc %d", | |
2120 | __func__, sessions_first_unused, options.max_sessions, | |
2121 | sessions_nalloc); | |
3c0ef626 | 2122 | } |
5156b1a1 | 2123 | |
2124 | s = &sessions[sessions_first_unused]; | |
2125 | if (s->used) { | |
2126 | fatal("%s: session %d already used", | |
2127 | __func__, sessions_first_unused); | |
2128 | } | |
2129 | sessions_first_unused = s->next_unused; | |
2130 | s->used = 1; | |
2131 | s->next_unused = -1; | |
2132 | debug("session_new: session %d", s->self); | |
2133 | ||
2134 | return s; | |
3c0ef626 | 2135 | } |
2136 | ||
2137 | static void | |
2138 | session_dump(void) | |
2139 | { | |
2140 | int i; | |
5156b1a1 | 2141 | for (i = 0; i < sessions_nalloc; i++) { |
3c0ef626 | 2142 | Session *s = &sessions[i]; |
5156b1a1 | 2143 | |
2144 | debug("dump: used %d next_unused %d session %d %p " | |
2145 | "channel %d pid %ld", | |
3c0ef626 | 2146 | s->used, |
5156b1a1 | 2147 | s->next_unused, |
3c0ef626 | 2148 | s->self, |
2149 | s, | |
2150 | s->chanid, | |
44a053a3 | 2151 | (long)s->pid); |
3c0ef626 | 2152 | } |
2153 | } | |
2154 | ||
2155 | int | |
2156 | session_open(Authctxt *authctxt, int chanid) | |
2157 | { | |
2158 | Session *s = session_new(); | |
2159 | debug("session_open: channel %d", chanid); | |
2160 | if (s == NULL) { | |
2161 | error("no more sessions"); | |
2162 | return 0; | |
2163 | } | |
2164 | s->authctxt = authctxt; | |
2165 | s->pw = authctxt->pw; | |
540d72c3 | 2166 | if (s->pw == NULL || !authctxt->valid) |
3c0ef626 | 2167 | fatal("no user for session %d", s->self); |
2168 | debug("session_open: session %d: link with channel %d", s->self, chanid); | |
2169 | s->chanid = chanid; | |
2170 | return 1; | |
2171 | } | |
2172 | ||
350391c5 | 2173 | Session * |
2174 | session_by_tty(char *tty) | |
2175 | { | |
2176 | int i; | |
5156b1a1 | 2177 | for (i = 0; i < sessions_nalloc; i++) { |
350391c5 | 2178 | Session *s = &sessions[i]; |
2179 | if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) { | |
2180 | debug("session_by_tty: session %d tty %s", i, tty); | |
2181 | return s; | |
2182 | } | |
2183 | } | |
2184 | debug("session_by_tty: unknown tty %.100s", tty); | |
2185 | session_dump(); | |
2186 | return NULL; | |
2187 | } | |
2188 | ||
3c0ef626 | 2189 | static Session * |
2190 | session_by_channel(int id) | |
2191 | { | |
2192 | int i; | |
5156b1a1 | 2193 | for (i = 0; i < sessions_nalloc; i++) { |
3c0ef626 | 2194 | Session *s = &sessions[i]; |
2195 | if (s->used && s->chanid == id) { | |
5156b1a1 | 2196 | debug("session_by_channel: session %d channel %d", |
2197 | i, id); | |
3c0ef626 | 2198 | return s; |
2199 | } | |
2200 | } | |
2201 | debug("session_by_channel: unknown channel %d", id); | |
2202 | session_dump(); | |
2203 | return NULL; | |
2204 | } | |
2205 | ||
2ce0bfe4 | 2206 | static Session * |
2207 | session_by_x11_channel(int id) | |
2208 | { | |
2209 | int i, j; | |
2210 | ||
5156b1a1 | 2211 | for (i = 0; i < sessions_nalloc; i++) { |
2ce0bfe4 | 2212 | Session *s = &sessions[i]; |
2213 | ||
2214 | if (s->x11_chanids == NULL || !s->used) | |
2215 | continue; | |
2216 | for (j = 0; s->x11_chanids[j] != -1; j++) { | |
2217 | if (s->x11_chanids[j] == id) { | |
2218 | debug("session_by_x11_channel: session %d " | |
2219 | "channel %d", s->self, id); | |
2220 | return s; | |
2221 | } | |
2222 | } | |
2223 | } | |
2224 | debug("session_by_x11_channel: unknown channel %d", id); | |
2225 | session_dump(); | |
2226 | return NULL; | |
2227 | } | |
2228 | ||
3c0ef626 | 2229 | static Session * |
2230 | session_by_pid(pid_t pid) | |
2231 | { | |
2232 | int i; | |
44a053a3 | 2233 | debug("session_by_pid: pid %ld", (long)pid); |
5156b1a1 | 2234 | for (i = 0; i < sessions_nalloc; i++) { |
3c0ef626 | 2235 | Session *s = &sessions[i]; |
2236 | if (s->used && s->pid == pid) | |
2237 | return s; | |
2238 | } | |
44a053a3 | 2239 | error("session_by_pid: unknown pid %ld", (long)pid); |
3c0ef626 | 2240 | session_dump(); |
2241 | return NULL; | |
2242 | } | |
2243 | ||
2244 | static int | |
2245 | session_window_change_req(Session *s) | |
2246 | { | |
2247 | s->col = packet_get_int(); | |
2248 | s->row = packet_get_int(); | |
2249 | s->xpixel = packet_get_int(); | |
2250 | s->ypixel = packet_get_int(); | |
e9702f7d | 2251 | packet_check_eom(); |
3c0ef626 | 2252 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
2253 | return 1; | |
2254 | } | |
2255 | ||
2256 | static int | |
2257 | session_pty_req(Session *s) | |
2258 | { | |
2259 | u_int len; | |
2260 | int n_bytes; | |
2261 | ||
2262 | if (no_pty_flag) { | |
2263 | debug("Allocating a pty not permitted for this authentication."); | |
2264 | return 0; | |
2265 | } | |
2266 | if (s->ttyfd != -1) { | |
2267 | packet_disconnect("Protocol error: you already have a pty."); | |
2268 | return 0; | |
2269 | } | |
2270 | ||
2271 | s->term = packet_get_string(&len); | |
2272 | ||
2273 | if (compat20) { | |
2274 | s->col = packet_get_int(); | |
2275 | s->row = packet_get_int(); | |
2276 | } else { | |
2277 | s->row = packet_get_int(); | |
2278 | s->col = packet_get_int(); | |
2279 | } | |
2280 | s->xpixel = packet_get_int(); | |
2281 | s->ypixel = packet_get_int(); | |
2282 | ||
2283 | if (strcmp(s->term, "") == 0) { | |
2284 | xfree(s->term); | |
2285 | s->term = NULL; | |
2286 | } | |
2287 | ||
2288 | /* Allocate a pty and open it. */ | |
2289 | debug("Allocating pty."); | |
5156b1a1 | 2290 | if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, |
2291 | sizeof(s->tty)))) { | |
3c0ef626 | 2292 | if (s->term) |
2293 | xfree(s->term); | |
2294 | s->term = NULL; | |
2295 | s->ptyfd = -1; | |
2296 | s->ttyfd = -1; | |
2297 | error("session_pty_req: session %d alloc failed", s->self); | |
2298 | return 0; | |
2299 | } | |
2300 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); | |
2301 | ||
2302 | /* for SSH1 the tty modes length is not given */ | |
2303 | if (!compat20) | |
2304 | n_bytes = packet_remaining(); | |
2305 | tty_parse_modes(s->ttyfd, &n_bytes); | |
2306 | ||
350391c5 | 2307 | if (!use_privsep) |
2308 | pty_setowner(s->pw, s->tty); | |
3c0ef626 | 2309 | |
2310 | /* Set window size from the packet. */ | |
2311 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); | |
2312 | ||
e9702f7d | 2313 | packet_check_eom(); |
3c0ef626 | 2314 | session_proctitle(s); |
2315 | return 1; | |
2316 | } | |
2317 | ||
2318 | static int | |
2319 | session_subsystem_req(Session *s) | |
2320 | { | |
2321 | struct stat st; | |
2322 | u_int len; | |
2323 | int success = 0; | |
30460aeb | 2324 | char *prog, *cmd, *subsys = packet_get_string(&len); |
2ce0bfe4 | 2325 | u_int i; |
3c0ef626 | 2326 | |
e9702f7d | 2327 | packet_check_eom(); |
7cac2b65 | 2328 | logit("subsystem request for %.100s", subsys); |
3c0ef626 | 2329 | |
2330 | for (i = 0; i < options.num_subsystems; i++) { | |
2331 | if (strcmp(subsys, options.subsystem_name[i]) == 0) { | |
30460aeb | 2332 | prog = options.subsystem_command[i]; |
2333 | cmd = options.subsystem_args[i]; | |
e74dc197 | 2334 | if (!strcmp(INTERNAL_SFTP_NAME, prog)) { |
2335 | s->is_subsystem = SUBSYSTEM_INT_SFTP; | |
2336 | } else if (stat(prog, &st) < 0) { | |
30460aeb | 2337 | error("subsystem: cannot stat %s: %s", prog, |
3c0ef626 | 2338 | strerror(errno)); |
2339 | break; | |
e74dc197 | 2340 | } else { |
2341 | s->is_subsystem = SUBSYSTEM_EXT; | |
3c0ef626 | 2342 | } |
2343 | debug("subsystem: exec() %s", cmd); | |
5156b1a1 | 2344 | success = do_exec(s, cmd) == 0; |
e9702f7d | 2345 | break; |
3c0ef626 | 2346 | } |
2347 | } | |
2348 | ||
2349 | if (!success) | |
7cac2b65 | 2350 | logit("subsystem request for %.100s failed, subsystem not found", |
3c0ef626 | 2351 | subsys); |
2352 | ||
2353 | xfree(subsys); | |
2354 | return success; | |
2355 | } | |
2356 | ||
2357 | static int | |
2358 | session_x11_req(Session *s) | |
2359 | { | |
2360 | int success; | |
2361 | ||
2ce0bfe4 | 2362 | if (s->auth_proto != NULL || s->auth_data != NULL) { |
2363 | error("session_x11_req: session %d: " | |
08822d99 | 2364 | "x11 forwarding already active", s->self); |
2ce0bfe4 | 2365 | return 0; |
2366 | } | |
3c0ef626 | 2367 | s->single_connection = packet_get_char(); |
2368 | s->auth_proto = packet_get_string(NULL); | |
2369 | s->auth_data = packet_get_string(NULL); | |
2370 | s->screen = packet_get_int(); | |
e9702f7d | 2371 | packet_check_eom(); |
3c0ef626 | 2372 | |
2373 | success = session_setup_x11fwd(s); | |
2374 | if (!success) { | |
2375 | xfree(s->auth_proto); | |
2376 | xfree(s->auth_data); | |
2377 | s->auth_proto = NULL; | |
2378 | s->auth_data = NULL; | |
2379 | } | |
2380 | return success; | |
2381 | } | |
2382 | ||
2383 | static int | |
2384 | session_shell_req(Session *s) | |
2385 | { | |
e9702f7d | 2386 | packet_check_eom(); |
5156b1a1 | 2387 | return do_exec(s, NULL) == 0; |
3c0ef626 | 2388 | } |
2389 | ||
2390 | static int | |
2391 | session_exec_req(Session *s) | |
2392 | { | |
5156b1a1 | 2393 | u_int len, success; |
2394 | ||
3c0ef626 | 2395 | char *command = packet_get_string(&len); |
e9702f7d | 2396 | packet_check_eom(); |
5156b1a1 | 2397 | success = do_exec(s, command) == 0; |
3c0ef626 | 2398 | xfree(command); |
5156b1a1 | 2399 | return success; |
3c0ef626 | 2400 | } |
2401 | ||
7cac2b65 | 2402 | static int |
2403 | session_break_req(Session *s) | |
2404 | { | |
7cac2b65 | 2405 | |
7e82606e | 2406 | packet_get_int(); /* ignored */ |
7cac2b65 | 2407 | packet_check_eom(); |
2408 | ||
5156b1a1 | 2409 | if (s->ttyfd == -1 || tcsendbreak(s->ttyfd, 0) < 0) |
7cac2b65 | 2410 | return 0; |
2411 | return 1; | |
2412 | } | |
2413 | ||
7e82606e | 2414 | static int |
2415 | session_env_req(Session *s) | |
2416 | { | |
2417 | char *name, *val; | |
2418 | u_int name_len, val_len, i; | |
2419 | ||
2420 | name = packet_get_string(&name_len); | |
2421 | val = packet_get_string(&val_len); | |
2422 | packet_check_eom(); | |
2423 | ||
2424 | /* Don't set too many environment variables */ | |
2425 | if (s->num_env > 128) { | |
2426 | debug2("Ignoring env request %s: too many env vars", name); | |
2427 | goto fail; | |
2428 | } | |
2429 | ||
2430 | for (i = 0; i < options.num_accept_env; i++) { | |
2431 | if (match_pattern(name, options.accept_env[i])) { | |
2432 | debug2("Setting env %d: %s=%s", s->num_env, name, val); | |
30460aeb | 2433 | s->env = xrealloc(s->env, s->num_env + 1, |
2434 | sizeof(*s->env)); | |
7e82606e | 2435 | s->env[s->num_env].name = name; |
2436 | s->env[s->num_env].val = val; | |
2437 | s->num_env++; | |
2438 | return (1); | |
2439 | } | |
2440 | } | |
2441 | debug2("Ignoring env request %s: disallowed name", name); | |
2442 | ||
2443 | fail: | |
2444 | xfree(name); | |
2445 | xfree(val); | |
2446 | return (0); | |
2447 | } | |
2448 | ||
3c0ef626 | 2449 | static int |
2450 | session_auth_agent_req(Session *s) | |
2451 | { | |
2452 | static int called = 0; | |
e9702f7d | 2453 | packet_check_eom(); |
5156b1a1 | 2454 | if (no_agent_forwarding_flag || !options.allow_agent_forwarding) { |
3c0ef626 | 2455 | debug("session_auth_agent_req: no_agent_forwarding_flag"); |
2456 | return 0; | |
2457 | } | |
2458 | if (called) { | |
2459 | return 0; | |
2460 | } else { | |
2461 | called = 1; | |
2462 | return auth_input_request_forwarding(s->pw); | |
2463 | } | |
2464 | } | |
2465 | ||
e9702f7d | 2466 | int |
2467 | session_input_channel_req(Channel *c, const char *rtype) | |
3c0ef626 | 2468 | { |
3c0ef626 | 2469 | int success = 0; |
3c0ef626 | 2470 | Session *s; |
3c0ef626 | 2471 | |
e9702f7d | 2472 | if ((s = session_by_channel(c->self)) == NULL) { |
7cac2b65 | 2473 | logit("session_input_channel_req: no session %d req %.100s", |
e9702f7d | 2474 | c->self, rtype); |
2475 | return 0; | |
2476 | } | |
2477 | debug("session_input_channel_req: session %d req %s", s->self, rtype); | |
3c0ef626 | 2478 | |
2479 | /* | |
2480 | * a session is in LARVAL state until a shell, a command | |
2481 | * or a subsystem is executed | |
2482 | */ | |
2483 | if (c->type == SSH_CHANNEL_LARVAL) { | |
2484 | if (strcmp(rtype, "shell") == 0) { | |
2485 | success = session_shell_req(s); | |
2486 | } else if (strcmp(rtype, "exec") == 0) { | |
2487 | success = session_exec_req(s); | |
2488 | } else if (strcmp(rtype, "pty-req") == 0) { | |
0b90ac93 | 2489 | success = session_pty_req(s); |
3c0ef626 | 2490 | } else if (strcmp(rtype, "x11-req") == 0) { |
2491 | success = session_x11_req(s); | |
2492 | } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { | |
2493 | success = session_auth_agent_req(s); | |
2494 | } else if (strcmp(rtype, "subsystem") == 0) { | |
2495 | success = session_subsystem_req(s); | |
7e82606e | 2496 | } else if (strcmp(rtype, "env") == 0) { |
2497 | success = session_env_req(s); | |
3c0ef626 | 2498 | } |
2499 | } | |
2500 | if (strcmp(rtype, "window-change") == 0) { | |
2501 | success = session_window_change_req(s); | |
7e82606e | 2502 | } else if (strcmp(rtype, "break") == 0) { |
2503 | success = session_break_req(s); | |
3c0ef626 | 2504 | } |
7e82606e | 2505 | |
e9702f7d | 2506 | return success; |
3c0ef626 | 2507 | } |
2508 | ||
2509 | void | |
5156b1a1 | 2510 | session_set_fds(Session *s, int fdin, int fdout, int fderr, int is_tty) |
3c0ef626 | 2511 | { |
2512 | if (!compat20) | |
2513 | fatal("session_set_fds: called for proto != 2.0"); | |
2514 | /* | |
2515 | * now that have a child and a pipe to the child, | |
2516 | * we can activate our channel and register the fd's | |
2517 | */ | |
2518 | if (s->chanid == -1) | |
2519 | fatal("no channel for session %d", s->self); | |
d3057ca4 | 2520 | if (options.hpn_disabled) |
6df46d40 | 2521 | channel_set_fds(s->chanid, |
2522 | fdout, fdin, fderr, | |
2523 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, | |
5156b1a1 | 2524 | 1, is_tty, CHAN_SES_WINDOW_DEFAULT); |
d3057ca4 | 2525 | else |
2526 | channel_set_fds(s->chanid, | |
2527 | fdout, fdin, fderr, | |
2528 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, | |
2529 | 1, is_tty, options.hpn_buffer_size); | |
3c0ef626 | 2530 | } |
2531 | ||
2532 | /* | |
2533 | * Function to perform pty cleanup. Also called if we get aborted abnormally | |
2534 | * (e.g., due to a dropped connection). | |
2535 | */ | |
350391c5 | 2536 | void |
540d72c3 | 2537 | session_pty_cleanup2(Session *s) |
3c0ef626 | 2538 | { |
3c0ef626 | 2539 | if (s == NULL) { |
2540 | error("session_pty_cleanup: no session"); | |
2541 | return; | |
2542 | } | |
2543 | if (s->ttyfd == -1) | |
2544 | return; | |
2545 | ||
2546 | debug("session_pty_cleanup: session %d release %s", s->self, s->tty); | |
2547 | ||
2548 | /* Record that the user has logged out. */ | |
2549 | if (s->pid != 0) | |
e9702f7d | 2550 | record_logout(s->pid, s->tty, s->pw->pw_name); |
3c0ef626 | 2551 | |
2552 | /* Release the pseudo-tty. */ | |
350391c5 | 2553 | if (getuid() == 0) |
2554 | pty_release(s->tty); | |
3c0ef626 | 2555 | |
2556 | /* | |
2557 | * Close the server side of the socket pairs. We must do this after | |
2558 | * the pty cleanup, so that another process doesn't get this pty | |
2559 | * while we're still cleaning up. | |
2560 | */ | |
5156b1a1 | 2561 | if (s->ptymaster != -1 && close(s->ptymaster) < 0) |
2562 | error("close(s->ptymaster/%d): %s", | |
2563 | s->ptymaster, strerror(errno)); | |
3c0ef626 | 2564 | |
2565 | /* unlink pty from session */ | |
2566 | s->ttyfd = -1; | |
2567 | } | |
2568 | ||
350391c5 | 2569 | void |
540d72c3 | 2570 | session_pty_cleanup(Session *s) |
350391c5 | 2571 | { |
540d72c3 | 2572 | PRIVSEP(session_pty_cleanup2(s)); |
350391c5 | 2573 | } |
2574 | ||
d03f4262 | 2575 | static char * |
2576 | sig2name(int sig) | |
2577 | { | |
2578 | #define SSH_SIG(x) if (sig == SIG ## x) return #x | |
2579 | SSH_SIG(ABRT); | |
2580 | SSH_SIG(ALRM); | |
2581 | SSH_SIG(FPE); | |
2582 | SSH_SIG(HUP); | |
2583 | SSH_SIG(ILL); | |
2584 | SSH_SIG(INT); | |
2585 | SSH_SIG(KILL); | |
2586 | SSH_SIG(PIPE); | |
2587 | SSH_SIG(QUIT); | |
2588 | SSH_SIG(SEGV); | |
2589 | SSH_SIG(TERM); | |
2590 | SSH_SIG(USR1); | |
2591 | SSH_SIG(USR2); | |
2592 | #undef SSH_SIG | |
2593 | return "SIG@openssh.com"; | |
2594 | } | |
2595 | ||
2ce0bfe4 | 2596 | static void |
2597 | session_close_x11(int id) | |
2598 | { | |
2599 | Channel *c; | |
2600 | ||
08822d99 | 2601 | if ((c = channel_by_id(id)) == NULL) { |
2ce0bfe4 | 2602 | debug("session_close_x11: x11 channel %d missing", id); |
2603 | } else { | |
2604 | /* Detach X11 listener */ | |
2605 | debug("session_close_x11: detach x11 channel %d", id); | |
2606 | channel_cancel_cleanup(id); | |
2607 | if (c->ostate != CHAN_OUTPUT_CLOSED) | |
2608 | chan_mark_dead(c); | |
2609 | } | |
2610 | } | |
2611 | ||
2612 | static void | |
2613 | session_close_single_x11(int id, void *arg) | |
2614 | { | |
2615 | Session *s; | |
2616 | u_int i; | |
2617 | ||
2618 | debug3("session_close_single_x11: channel %d", id); | |
2619 | channel_cancel_cleanup(id); | |
0b90ac93 | 2620 | if ((s = session_by_x11_channel(id)) == NULL) |
2ce0bfe4 | 2621 | fatal("session_close_single_x11: no x11 channel %d", id); |
2622 | for (i = 0; s->x11_chanids[i] != -1; i++) { | |
2623 | debug("session_close_single_x11: session %d: " | |
2624 | "closing channel %d", s->self, s->x11_chanids[i]); | |
2625 | /* | |
2626 | * The channel "id" is already closing, but make sure we | |
2627 | * close all of its siblings. | |
2628 | */ | |
2629 | if (s->x11_chanids[i] != id) | |
2630 | session_close_x11(s->x11_chanids[i]); | |
2631 | } | |
2632 | xfree(s->x11_chanids); | |
2633 | s->x11_chanids = NULL; | |
2634 | if (s->display) { | |
2635 | xfree(s->display); | |
2636 | s->display = NULL; | |
2637 | } | |
2638 | if (s->auth_proto) { | |
2639 | xfree(s->auth_proto); | |
2640 | s->auth_proto = NULL; | |
2641 | } | |
2642 | if (s->auth_data) { | |
2643 | xfree(s->auth_data); | |
2644 | s->auth_data = NULL; | |
2645 | } | |
2646 | if (s->auth_display) { | |
2647 | xfree(s->auth_display); | |
2648 | s->auth_display = NULL; | |
2649 | } | |
2650 | } | |
2651 | ||
3c0ef626 | 2652 | static void |
2653 | session_exit_message(Session *s, int status) | |
2654 | { | |
2655 | Channel *c; | |
e9702f7d | 2656 | |
2657 | if ((c = channel_lookup(s->chanid)) == NULL) | |
3c0ef626 | 2658 | fatal("session_exit_message: session %d: no channel %d", |
2659 | s->self, s->chanid); | |
44a053a3 | 2660 | debug("session_exit_message: session %d channel %d pid %ld", |
2661 | s->self, s->chanid, (long)s->pid); | |
3c0ef626 | 2662 | |
2663 | if (WIFEXITED(status)) { | |
e9702f7d | 2664 | channel_request_start(s->chanid, "exit-status", 0); |
3c0ef626 | 2665 | packet_put_int(WEXITSTATUS(status)); |
2666 | packet_send(); | |
2667 | } else if (WIFSIGNALED(status)) { | |
e9702f7d | 2668 | channel_request_start(s->chanid, "exit-signal", 0); |
d03f4262 | 2669 | packet_put_cstring(sig2name(WTERMSIG(status))); |
3c0ef626 | 2670 | #ifdef WCOREDUMP |
e74dc197 | 2671 | packet_put_char(WCOREDUMP(status)? 1 : 0); |
3c0ef626 | 2672 | #else /* WCOREDUMP */ |
2673 | packet_put_char(0); | |
2674 | #endif /* WCOREDUMP */ | |
2675 | packet_put_cstring(""); | |
2676 | packet_put_cstring(""); | |
2677 | packet_send(); | |
2678 | } else { | |
2679 | /* Some weird exit cause. Just exit. */ | |
2680 | packet_disconnect("wait returned status %04x.", status); | |
2681 | } | |
2682 | ||
2683 | /* disconnect channel */ | |
2684 | debug("session_exit_message: release channel %d", s->chanid); | |
08822d99 | 2685 | |
2686 | /* | |
2687 | * Adjust cleanup callback attachment to send close messages when | |
30460aeb | 2688 | * the channel gets EOF. The session will be then be closed |
08822d99 | 2689 | * by session_close_by_channel when the childs close their fds. |
2690 | */ | |
2691 | channel_register_cleanup(c->self, session_close_by_channel, 1); | |
2692 | ||
3c0ef626 | 2693 | /* |
2694 | * emulate a write failure with 'chan_write_failed', nobody will be | |
2695 | * interested in data we write. | |
2696 | * Note that we must not call 'chan_read_failed', since there could | |
2697 | * be some more data waiting in the pipe. | |
2698 | */ | |
2699 | if (c->ostate != CHAN_OUTPUT_CLOSED) | |
2700 | chan_write_failed(c); | |
3c0ef626 | 2701 | } |
2702 | ||
350391c5 | 2703 | void |
3c0ef626 | 2704 | session_close(Session *s) |
2705 | { | |
2ce0bfe4 | 2706 | u_int i; |
7e82606e | 2707 | |
44a053a3 | 2708 | debug("session_close: session %d pid %ld", s->self, (long)s->pid); |
540d72c3 | 2709 | if (s->ttyfd != -1) |
3c0ef626 | 2710 | session_pty_cleanup(s); |
3c0ef626 | 2711 | if (s->term) |
2712 | xfree(s->term); | |
2713 | if (s->display) | |
2714 | xfree(s->display); | |
2ce0bfe4 | 2715 | if (s->x11_chanids) |
2716 | xfree(s->x11_chanids); | |
e9702f7d | 2717 | if (s->auth_display) |
2718 | xfree(s->auth_display); | |
3c0ef626 | 2719 | if (s->auth_data) |
2720 | xfree(s->auth_data); | |
2721 | if (s->auth_proto) | |
2722 | xfree(s->auth_proto); | |
30460aeb | 2723 | if (s->env != NULL) { |
2724 | for (i = 0; i < s->num_env; i++) { | |
2725 | xfree(s->env[i].name); | |
2726 | xfree(s->env[i].val); | |
2727 | } | |
7e82606e | 2728 | xfree(s->env); |
30460aeb | 2729 | } |
3c0ef626 | 2730 | session_proctitle(s); |
5156b1a1 | 2731 | session_unused(s->self); |
3c0ef626 | 2732 | } |
2733 | ||
2734 | void | |
2735 | session_close_by_pid(pid_t pid, int status) | |
2736 | { | |
2737 | Session *s = session_by_pid(pid); | |
2738 | if (s == NULL) { | |
44a053a3 | 2739 | debug("session_close_by_pid: no session for pid %ld", |
2740 | (long)pid); | |
3c0ef626 | 2741 | return; |
2742 | } | |
2743 | if (s->chanid != -1) | |
2744 | session_exit_message(s, status); | |
08822d99 | 2745 | if (s->ttyfd != -1) |
2746 | session_pty_cleanup(s); | |
4b6dae8f | 2747 | s->pid = 0; |
3c0ef626 | 2748 | } |
2749 | ||
2750 | /* | |
2751 | * this is called when a channel dies before | |
2752 | * the session 'child' itself dies | |
2753 | */ | |
2754 | void | |
2755 | session_close_by_channel(int id, void *arg) | |
2756 | { | |
2757 | Session *s = session_by_channel(id); | |
08822d99 | 2758 | u_int i; |
2ce0bfe4 | 2759 | |
3c0ef626 | 2760 | if (s == NULL) { |
2761 | debug("session_close_by_channel: no session for id %d", id); | |
2762 | return; | |
2763 | } | |
44a053a3 | 2764 | debug("session_close_by_channel: channel %d child %ld", |
2765 | id, (long)s->pid); | |
3c0ef626 | 2766 | if (s->pid != 0) { |
2767 | debug("session_close_by_channel: channel %d: has child", id); | |
2768 | /* | |
2769 | * delay detach of session, but release pty, since | |
2770 | * the fd's to the child are already closed | |
2771 | */ | |
540d72c3 | 2772 | if (s->ttyfd != -1) |
3c0ef626 | 2773 | session_pty_cleanup(s); |
3c0ef626 | 2774 | return; |
2775 | } | |
2776 | /* detach by removing callback */ | |
2777 | channel_cancel_cleanup(s->chanid); | |
08822d99 | 2778 | |
2779 | /* Close any X11 listeners associated with this session */ | |
2780 | if (s->x11_chanids != NULL) { | |
2781 | for (i = 0; s->x11_chanids[i] != -1; i++) { | |
2782 | session_close_x11(s->x11_chanids[i]); | |
2783 | s->x11_chanids[i] = -1; | |
2784 | } | |
2785 | } | |
2786 | ||
3c0ef626 | 2787 | s->chanid = -1; |
2788 | session_close(s); | |
2789 | } | |
2790 | ||
2791 | void | |
350391c5 | 2792 | session_destroy_all(void (*closefunc)(Session *)) |
3c0ef626 | 2793 | { |
2794 | int i; | |
5156b1a1 | 2795 | for (i = 0; i < sessions_nalloc; i++) { |
3c0ef626 | 2796 | Session *s = &sessions[i]; |
350391c5 | 2797 | if (s->used) { |
2798 | if (closefunc != NULL) | |
2799 | closefunc(s); | |
2800 | else | |
2801 | session_close(s); | |
2802 | } | |
3c0ef626 | 2803 | } |
2804 | } | |
2805 | ||
2806 | static char * | |
2807 | session_tty_list(void) | |
2808 | { | |
2809 | static char buf[1024]; | |
2810 | int i; | |
bfe49944 | 2811 | char *cp; |
2812 | ||
3c0ef626 | 2813 | buf[0] = '\0'; |
5156b1a1 | 2814 | for (i = 0; i < sessions_nalloc; i++) { |
3c0ef626 | 2815 | Session *s = &sessions[i]; |
2816 | if (s->used && s->ttyfd != -1) { | |
540d72c3 | 2817 | |
bfe49944 | 2818 | if (strncmp(s->tty, "/dev/", 5) != 0) { |
2819 | cp = strrchr(s->tty, '/'); | |
2820 | cp = (cp == NULL) ? s->tty : cp + 1; | |
2821 | } else | |
2822 | cp = s->tty + 5; | |
540d72c3 | 2823 | |
3c0ef626 | 2824 | if (buf[0] != '\0') |
2825 | strlcat(buf, ",", sizeof buf); | |
bfe49944 | 2826 | strlcat(buf, cp, sizeof buf); |
3c0ef626 | 2827 | } |
2828 | } | |
2829 | if (buf[0] == '\0') | |
2830 | strlcpy(buf, "notty", sizeof buf); | |
2831 | return buf; | |
2832 | } | |
2833 | ||
2834 | void | |
2835 | session_proctitle(Session *s) | |
2836 | { | |
2837 | if (s->pw == NULL) | |
2838 | error("no user for session %d", s->self); | |
2839 | else | |
2840 | setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); | |
2841 | } | |
2842 | ||
2843 | int | |
2844 | session_setup_x11fwd(Session *s) | |
2845 | { | |
2846 | struct stat st; | |
e9702f7d | 2847 | char display[512], auth_display[512]; |
2848 | char hostname[MAXHOSTNAMELEN]; | |
2ce0bfe4 | 2849 | u_int i; |
3c0ef626 | 2850 | |
2851 | if (no_x11_forwarding_flag) { | |
2852 | packet_send_debug("X11 forwarding disabled in user configuration file."); | |
2853 | return 0; | |
2854 | } | |
2855 | if (!options.x11_forwarding) { | |
2856 | debug("X11 forwarding disabled in server configuration file."); | |
2857 | return 0; | |
2858 | } | |
2859 | if (!options.xauth_location || | |
2860 | (stat(options.xauth_location, &st) == -1)) { | |
2861 | packet_send_debug("No xauth program; cannot forward with spoofing."); | |
2862 | return 0; | |
2863 | } | |
2864 | if (options.use_login) { | |
2865 | packet_send_debug("X11 forwarding disabled; " | |
2866 | "not compatible with UseLogin=yes."); | |
2867 | return 0; | |
2868 | } | |
2869 | if (s->display != NULL) { | |
2870 | debug("X11 display already set."); | |
2871 | return 0; | |
2872 | } | |
276b07a3 | 2873 | if (x11_create_display_inet(options.x11_display_offset, |
2874 | options.x11_use_localhost, s->single_connection, | |
d3057ca4 | 2875 | &s->display_number, &s->x11_chanids) == -1) { |
3c0ef626 | 2876 | debug("x11_create_display_inet failed."); |
2877 | return 0; | |
2878 | } | |
2ce0bfe4 | 2879 | for (i = 0; s->x11_chanids[i] != -1; i++) { |
2880 | channel_register_cleanup(s->x11_chanids[i], | |
08822d99 | 2881 | session_close_single_x11, 0); |
2ce0bfe4 | 2882 | } |
e9702f7d | 2883 | |
2884 | /* Set up a suitable value for the DISPLAY variable. */ | |
2885 | if (gethostname(hostname, sizeof(hostname)) < 0) | |
2886 | fatal("gethostname: %.100s", strerror(errno)); | |
2887 | /* | |
2888 | * auth_display must be used as the displayname when the | |
2889 | * authorization entry is added with xauth(1). This will be | |
2890 | * different than the DISPLAY string for localhost displays. | |
2891 | */ | |
2892 | if (options.x11_use_localhost) { | |
276b07a3 | 2893 | snprintf(display, sizeof display, "localhost:%u.%u", |
e9702f7d | 2894 | s->display_number, s->screen); |
276b07a3 | 2895 | snprintf(auth_display, sizeof auth_display, "unix:%u.%u", |
e9702f7d | 2896 | s->display_number, s->screen); |
2897 | s->display = xstrdup(display); | |
2898 | s->auth_display = xstrdup(auth_display); | |
2899 | } else { | |
2900 | #ifdef IPADDR_IN_DISPLAY | |
2901 | struct hostent *he; | |
2902 | struct in_addr my_addr; | |
2903 | ||
2904 | he = gethostbyname(hostname); | |
2905 | if (he == NULL) { | |
2906 | error("Can't get IP address for X11 DISPLAY."); | |
2907 | packet_send_debug("Can't get IP address for X11 DISPLAY."); | |
2908 | return 0; | |
2909 | } | |
2910 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); | |
276b07a3 | 2911 | snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr), |
e9702f7d | 2912 | s->display_number, s->screen); |
2913 | #else | |
276b07a3 | 2914 | snprintf(display, sizeof display, "%.400s:%u.%u", hostname, |
e9702f7d | 2915 | s->display_number, s->screen); |
2916 | #endif | |
2917 | s->display = xstrdup(display); | |
2918 | s->auth_display = xstrdup(display); | |
2919 | } | |
2920 | ||
3c0ef626 | 2921 | return 1; |
2922 | } | |
2923 | ||
2924 | static void | |
2925 | do_authenticated2(Authctxt *authctxt) | |
2926 | { | |
2927 | server_loop2(authctxt); | |
540d72c3 | 2928 | } |
2929 | ||
2930 | void | |
2931 | do_cleanup(Authctxt *authctxt) | |
2932 | { | |
2933 | static int called = 0; | |
2934 | ||
2935 | debug("do_cleanup"); | |
2936 | ||
2937 | /* no cleanup if we're in the child for login shell */ | |
2938 | if (is_child) | |
2939 | return; | |
2940 | ||
2941 | /* avoid double cleanup */ | |
2942 | if (called) | |
2943 | return; | |
2944 | called = 1; | |
2945 | ||
fa0f0f45 | 2946 | if (authctxt == NULL) |
540d72c3 | 2947 | return; |
fa0f0f45 | 2948 | |
2949 | #ifdef USE_PAM | |
2950 | if (options.use_pam) { | |
2951 | sshpam_cleanup(); | |
2952 | sshpam_thread_cleanup(); | |
2953 | } | |
2954 | #endif | |
2955 | ||
2956 | if (!authctxt->authenticated) | |
2957 | return; | |
2958 | ||
540d72c3 | 2959 | #ifdef KRB5 |
2960 | if (options.kerberos_ticket_cleanup && | |
2961 | authctxt->krb5_ctx) | |
2962 | krb5_cleanup_proc(authctxt); | |
5598e598 | 2963 | #endif |
540d72c3 | 2964 | |
2965 | #ifdef GSSAPI | |
2966 | if (compat20 && options.gss_cleanup_creds) | |
2967 | ssh_gssapi_cleanup_creds(); | |
2968 | #endif | |
2969 | ||
540d72c3 | 2970 | /* remove agent socket */ |
2971 | auth_sock_cleanup_proc(authctxt->pw); | |
2972 | ||
2973 | /* | |
2974 | * Cleanup ptys/utmp only if privsep is disabled, | |
2975 | * or if running in monitor. | |
2976 | */ | |
2977 | if (!use_privsep || mm_is_monitor()) | |
2978 | session_destroy_all(session_pty_cleanup2); | |
3c0ef626 | 2979 | } |