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