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