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