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