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