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