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