]>
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" | |
e9702f7d | 36 | RCSID("$OpenBSD: session.c,v 1.128 2002/02/16 00:51:44 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" | |
45 | #include "mpaux.h" | |
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" | |
59 | ||
5598e598 | 60 | #ifdef GSSAPI |
61 | #include "ssh-gss.h" | |
62 | #endif | |
63 | ||
3c0ef626 | 64 | #ifdef HAVE_CYGWIN |
65 | #include <windows.h> | |
66 | #include <sys/cygwin.h> | |
67 | #define is_winnt (GetVersion() < 0x80000000) | |
68 | #endif | |
69 | ||
3c0ef626 | 70 | /* types */ |
71 | ||
72 | #define TTYSZ 64 | |
73 | typedef struct Session Session; | |
74 | struct Session { | |
75 | int used; | |
76 | int self; | |
77 | struct passwd *pw; | |
78 | Authctxt *authctxt; | |
79 | pid_t pid; | |
80 | /* tty */ | |
81 | char *term; | |
82 | int ptyfd, ttyfd, ptymaster; | |
83 | int row, col, xpixel, ypixel; | |
84 | char tty[TTYSZ]; | |
85 | /* X11 */ | |
e9702f7d | 86 | int display_number; |
3c0ef626 | 87 | char *display; |
88 | int screen; | |
e9702f7d | 89 | char *auth_display; |
3c0ef626 | 90 | char *auth_proto; |
91 | char *auth_data; | |
92 | int single_connection; | |
93 | /* proto 2 */ | |
94 | int chanid; | |
95 | int is_subsystem; | |
96 | }; | |
97 | ||
98 | /* func */ | |
99 | ||
100 | Session *session_new(void); | |
101 | void session_set_fds(Session *, int, int, int); | |
102 | static void session_pty_cleanup(void *); | |
103 | void session_proctitle(Session *); | |
104 | int session_setup_x11fwd(Session *); | |
105 | void do_exec_pty(Session *, const char *); | |
106 | void do_exec_no_pty(Session *, const char *); | |
107 | void do_exec(Session *, const char *); | |
108 | void do_login(Session *, const char *); | |
109 | #ifdef LOGIN_NEEDS_UTMPX | |
110 | static void do_pre_login(Session *s); | |
111 | #endif | |
112 | void do_child(Session *, const char *); | |
113 | void do_motd(void); | |
114 | int check_quietlogin(Session *, const char *); | |
115 | ||
116 | static void do_authenticated1(Authctxt *); | |
117 | static void do_authenticated2(Authctxt *); | |
118 | ||
119 | static void session_close(Session *); | |
120 | static int session_pty_req(Session *); | |
121 | ||
122 | /* import */ | |
123 | extern ServerOptions options; | |
124 | extern char *__progname; | |
125 | extern int log_stderr; | |
126 | extern int debug_flag; | |
127 | extern u_int utmp_len; | |
128 | extern int startup_pipe; | |
129 | extern void destroy_sensitive_data(void); | |
130 | ||
131 | /* original command from peer. */ | |
132 | const char *original_command = NULL; | |
133 | ||
134 | /* data */ | |
135 | #define MAX_SESSIONS 10 | |
136 | Session sessions[MAX_SESSIONS]; | |
137 | ||
138 | #ifdef WITH_AIXAUTHENTICATE | |
139 | char *aixloginmsg; | |
140 | #endif /* WITH_AIXAUTHENTICATE */ | |
141 | ||
142 | #ifdef HAVE_LOGIN_CAP | |
143 | static login_cap_t *lc; | |
144 | #endif | |
145 | ||
146 | void | |
147 | do_authenticated(Authctxt *authctxt) | |
148 | { | |
149 | /* | |
150 | * Cancel the alarm we set to limit the time taken for | |
151 | * authentication. | |
152 | */ | |
153 | alarm(0); | |
154 | if (startup_pipe != -1) { | |
155 | close(startup_pipe); | |
156 | startup_pipe = -1; | |
157 | } | |
158 | #if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD) | |
159 | if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) { | |
160 | error("unable to get login class"); | |
161 | return; | |
162 | } | |
163 | #ifdef BSD_AUTH | |
164 | if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) { | |
165 | packet_disconnect("Approval failure for %s", | |
166 | authctxt->pw->pw_name); | |
167 | } | |
168 | #endif | |
169 | #endif | |
170 | #ifdef WITH_AIXAUTHENTICATE | |
171 | /* We don't have a pty yet, so just label the line as "ssh" */ | |
172 | if (loginsuccess(authctxt->user, | |
e9702f7d | 173 | get_canonical_hostname(options.verify_reverse_mapping), |
3c0ef626 | 174 | "ssh", &aixloginmsg) < 0) |
175 | aixloginmsg = NULL; | |
176 | #endif /* WITH_AIXAUTHENTICATE */ | |
177 | ||
178 | /* setup the channel layer */ | |
179 | if (!no_port_forwarding_flag && options.allow_tcp_forwarding) | |
180 | channel_permit_all_opens(); | |
181 | ||
182 | if (compat20) | |
183 | do_authenticated2(authctxt); | |
184 | else | |
185 | do_authenticated1(authctxt); | |
186 | ||
187 | /* remove agent socket */ | |
188 | if (auth_get_socket_name()) | |
189 | auth_sock_cleanup_proc(authctxt->pw); | |
190 | #ifdef KRB4 | |
191 | if (options.kerberos_ticket_cleanup) | |
192 | krb4_cleanup_proc(authctxt); | |
193 | #endif | |
194 | #ifdef KRB5 | |
195 | if (options.kerberos_ticket_cleanup) | |
196 | krb5_cleanup_proc(authctxt); | |
197 | #endif | |
198 | } | |
199 | ||
200 | /* | |
201 | * Prepares for an interactive session. This is called after the user has | |
202 | * been successfully authenticated. During this message exchange, pseudo | |
203 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings | |
204 | * are requested, etc. | |
205 | */ | |
206 | static void | |
207 | do_authenticated1(Authctxt *authctxt) | |
208 | { | |
209 | Session *s; | |
210 | char *command; | |
e9702f7d | 211 | int success, type, screen_flag; |
3c0ef626 | 212 | int compression_level = 0, enable_compression_after_reply = 0; |
213 | u_int proto_len, data_len, dlen; | |
214 | ||
215 | s = session_new(); | |
216 | s->authctxt = authctxt; | |
217 | s->pw = authctxt->pw; | |
218 | ||
219 | /* | |
220 | * We stay in this loop until the client requests to execute a shell | |
221 | * or a command. | |
222 | */ | |
223 | for (;;) { | |
224 | success = 0; | |
225 | ||
226 | /* Get a packet from the client. */ | |
e9702f7d | 227 | type = packet_read(); |
3c0ef626 | 228 | |
229 | /* Process the packet. */ | |
230 | switch (type) { | |
231 | case SSH_CMSG_REQUEST_COMPRESSION: | |
3c0ef626 | 232 | compression_level = packet_get_int(); |
e9702f7d | 233 | packet_check_eom(); |
3c0ef626 | 234 | if (compression_level < 1 || compression_level > 9) { |
235 | packet_send_debug("Received illegal compression level %d.", | |
e9702f7d | 236 | compression_level); |
3c0ef626 | 237 | break; |
238 | } | |
239 | /* Enable compression after we have responded with SUCCESS. */ | |
240 | enable_compression_after_reply = 1; | |
241 | success = 1; | |
242 | break; | |
243 | ||
244 | case SSH_CMSG_REQUEST_PTY: | |
245 | success = session_pty_req(s); | |
246 | break; | |
247 | ||
248 | case SSH_CMSG_X11_REQUEST_FORWARDING: | |
249 | s->auth_proto = packet_get_string(&proto_len); | |
250 | s->auth_data = packet_get_string(&data_len); | |
251 | ||
252 | screen_flag = packet_get_protocol_flags() & | |
253 | SSH_PROTOFLAG_SCREEN_NUMBER; | |
254 | debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); | |
255 | ||
256 | if (packet_remaining() == 4) { | |
257 | if (!screen_flag) | |
258 | debug2("Buggy client: " | |
259 | "X11 screen flag missing"); | |
260 | s->screen = packet_get_int(); | |
261 | } else { | |
262 | s->screen = 0; | |
263 | } | |
e9702f7d | 264 | packet_check_eom(); |
3c0ef626 | 265 | success = session_setup_x11fwd(s); |
266 | if (!success) { | |
267 | xfree(s->auth_proto); | |
268 | xfree(s->auth_data); | |
269 | s->auth_proto = NULL; | |
270 | s->auth_data = NULL; | |
271 | } | |
272 | break; | |
273 | ||
274 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: | |
275 | if (no_agent_forwarding_flag || compat13) { | |
276 | debug("Authentication agent forwarding not permitted for this authentication."); | |
277 | break; | |
278 | } | |
279 | debug("Received authentication agent forwarding request."); | |
280 | success = auth_input_request_forwarding(s->pw); | |
281 | break; | |
282 | ||
283 | case SSH_CMSG_PORT_FORWARD_REQUEST: | |
284 | if (no_port_forwarding_flag) { | |
285 | debug("Port forwarding not permitted for this authentication."); | |
286 | break; | |
287 | } | |
288 | if (!options.allow_tcp_forwarding) { | |
289 | debug("Port forwarding not permitted."); | |
290 | break; | |
291 | } | |
292 | debug("Received TCP/IP port forwarding request."); | |
293 | channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports); | |
294 | success = 1; | |
295 | break; | |
296 | ||
297 | case SSH_CMSG_MAX_PACKET_SIZE: | |
298 | if (packet_set_maxsize(packet_get_int()) > 0) | |
299 | success = 1; | |
300 | break; | |
e9702f7d | 301 | |
3c0ef626 | 302 | #if defined(AFS) || defined(KRB5) |
303 | case SSH_CMSG_HAVE_KERBEROS_TGT: | |
304 | if (!options.kerberos_tgt_passing) { | |
305 | verbose("Kerberos TGT passing disabled."); | |
306 | } else { | |
307 | char *kdata = packet_get_string(&dlen); | |
e9702f7d | 308 | packet_check_eom(); |
309 | ||
3c0ef626 | 310 | /* XXX - 0x41, see creds_to_radix version */ |
311 | if (kdata[0] != 0x41) { | |
312 | #ifdef KRB5 | |
313 | krb5_data tgt; | |
314 | tgt.data = kdata; | |
315 | tgt.length = dlen; | |
e9702f7d | 316 | |
3c0ef626 | 317 | if (auth_krb5_tgt(s->authctxt, &tgt)) |
318 | success = 1; | |
319 | else | |
320 | verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user); | |
321 | #endif /* KRB5 */ | |
322 | } else { | |
323 | #ifdef AFS | |
324 | if (auth_krb4_tgt(s->authctxt, kdata)) | |
325 | success = 1; | |
326 | else | |
327 | verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user); | |
328 | #endif /* AFS */ | |
329 | } | |
330 | xfree(kdata); | |
331 | } | |
332 | break; | |
333 | #endif /* AFS || KRB5 */ | |
e9702f7d | 334 | |
3c0ef626 | 335 | #ifdef AFS |
336 | case SSH_CMSG_HAVE_AFS_TOKEN: | |
337 | if (!options.afs_token_passing || !k_hasafs()) { | |
338 | verbose("AFS token passing disabled."); | |
339 | } else { | |
340 | /* Accept AFS token. */ | |
341 | char *token = packet_get_string(&dlen); | |
e9702f7d | 342 | packet_check_eom(); |
343 | ||
3c0ef626 | 344 | if (auth_afs_token(s->authctxt, token)) |
345 | success = 1; | |
346 | else | |
347 | verbose("AFS token refused for %.100s", | |
348 | s->authctxt->user); | |
349 | xfree(token); | |
350 | } | |
351 | break; | |
352 | #endif /* AFS */ | |
353 | ||
354 | case SSH_CMSG_EXEC_SHELL: | |
355 | case SSH_CMSG_EXEC_CMD: | |
356 | if (type == SSH_CMSG_EXEC_CMD) { | |
357 | command = packet_get_string(&dlen); | |
358 | debug("Exec command '%.500s'", command); | |
359 | do_exec(s, command); | |
360 | xfree(command); | |
361 | } else { | |
362 | do_exec(s, NULL); | |
363 | } | |
e9702f7d | 364 | packet_check_eom(); |
3c0ef626 | 365 | session_close(s); |
366 | return; | |
367 | ||
368 | default: | |
369 | /* | |
370 | * Any unknown messages in this phase are ignored, | |
371 | * and a failure message is returned. | |
372 | */ | |
373 | log("Unknown packet type received after authentication: %d", type); | |
374 | } | |
375 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); | |
376 | packet_send(); | |
377 | packet_write_wait(); | |
378 | ||
379 | /* Enable compression now that we have replied if appropriate. */ | |
380 | if (enable_compression_after_reply) { | |
381 | enable_compression_after_reply = 0; | |
382 | packet_start_compression(compression_level); | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
387 | /* | |
388 | * This is called to fork and execute a command when we have no tty. This | |
389 | * will call do_child from the child, and server_loop from the parent after | |
390 | * setting up file descriptors and such. | |
391 | */ | |
392 | void | |
393 | do_exec_no_pty(Session *s, const char *command) | |
394 | { | |
395 | int pid; | |
396 | ||
397 | #ifdef USE_PIPES | |
398 | int pin[2], pout[2], perr[2]; | |
399 | /* Allocate pipes for communicating with the program. */ | |
400 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) | |
401 | packet_disconnect("Could not create pipes: %.100s", | |
402 | strerror(errno)); | |
403 | #else /* USE_PIPES */ | |
404 | int inout[2], err[2]; | |
405 | /* Uses socket pairs to communicate with the program. */ | |
406 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || | |
407 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) | |
408 | packet_disconnect("Could not create socket pairs: %.100s", | |
409 | strerror(errno)); | |
410 | #endif /* USE_PIPES */ | |
411 | if (s == NULL) | |
412 | fatal("do_exec_no_pty: no session"); | |
413 | ||
414 | session_proctitle(s); | |
415 | ||
5598e598 | 416 | #if defined(GSSAPI) |
417 | temporarily_use_uid(s->pw); | |
418 | ssh_gssapi_storecreds(); | |
419 | restore_uid(); | |
420 | #endif | |
421 | ||
3c0ef626 | 422 | #if defined(USE_PAM) |
423 | do_pam_session(s->pw->pw_name, NULL); | |
424 | do_pam_setcred(1); | |
425 | if (is_pam_password_change_required()) | |
426 | packet_disconnect("Password change required but no " | |
427 | "TTY available"); | |
428 | #endif /* USE_PAM */ | |
429 | ||
430 | /* Fork the child. */ | |
431 | if ((pid = fork()) == 0) { | |
432 | /* Child. Reinitialize the log since the pid has changed. */ | |
433 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
434 | ||
435 | /* | |
436 | * Create a new session and process group since the 4.4BSD | |
437 | * setlogin() affects the entire process group. | |
438 | */ | |
439 | if (setsid() < 0) | |
440 | error("setsid failed: %.100s", strerror(errno)); | |
441 | ||
442 | #ifdef USE_PIPES | |
443 | /* | |
444 | * Redirect stdin. We close the parent side of the socket | |
445 | * pair, and make the child side the standard input. | |
446 | */ | |
447 | close(pin[1]); | |
448 | if (dup2(pin[0], 0) < 0) | |
449 | perror("dup2 stdin"); | |
450 | close(pin[0]); | |
451 | ||
452 | /* Redirect stdout. */ | |
453 | close(pout[0]); | |
454 | if (dup2(pout[1], 1) < 0) | |
455 | perror("dup2 stdout"); | |
456 | close(pout[1]); | |
457 | ||
458 | /* Redirect stderr. */ | |
459 | close(perr[0]); | |
460 | if (dup2(perr[1], 2) < 0) | |
461 | perror("dup2 stderr"); | |
462 | close(perr[1]); | |
463 | #else /* USE_PIPES */ | |
464 | /* | |
465 | * Redirect stdin, stdout, and stderr. Stdin and stdout will | |
466 | * use the same socket, as some programs (particularly rdist) | |
467 | * seem to depend on it. | |
468 | */ | |
469 | close(inout[1]); | |
470 | close(err[1]); | |
471 | if (dup2(inout[0], 0) < 0) /* stdin */ | |
472 | perror("dup2 stdin"); | |
473 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ | |
474 | perror("dup2 stdout"); | |
475 | if (dup2(err[0], 2) < 0) /* stderr */ | |
476 | perror("dup2 stderr"); | |
477 | #endif /* USE_PIPES */ | |
478 | ||
479 | /* Do processing for the child (exec command etc). */ | |
480 | do_child(s, command); | |
481 | /* NOTREACHED */ | |
482 | } | |
483 | #ifdef HAVE_CYGWIN | |
484 | if (is_winnt) | |
485 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); | |
486 | #endif | |
487 | if (pid < 0) | |
488 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
489 | s->pid = pid; | |
490 | /* Set interactive/non-interactive mode. */ | |
491 | packet_set_interactive(s->display != NULL); | |
492 | #ifdef USE_PIPES | |
493 | /* We are the parent. Close the child sides of the pipes. */ | |
494 | close(pin[0]); | |
495 | close(pout[1]); | |
496 | close(perr[1]); | |
497 | ||
498 | if (compat20) { | |
499 | session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]); | |
500 | } else { | |
501 | /* Enter the interactive session. */ | |
502 | server_loop(pid, pin[1], pout[0], perr[0]); | |
503 | /* server_loop has closed pin[1], pout[0], and perr[0]. */ | |
504 | } | |
505 | #else /* USE_PIPES */ | |
506 | /* We are the parent. Close the child sides of the socket pairs. */ | |
507 | close(inout[0]); | |
508 | close(err[0]); | |
509 | ||
510 | /* | |
511 | * Enter the interactive session. Note: server_loop must be able to | |
512 | * handle the case that fdin and fdout are the same. | |
513 | */ | |
514 | if (compat20) { | |
515 | session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]); | |
516 | } else { | |
517 | server_loop(pid, inout[1], inout[1], err[1]); | |
518 | /* server_loop has closed inout[1] and err[1]. */ | |
519 | } | |
520 | #endif /* USE_PIPES */ | |
521 | } | |
522 | ||
523 | /* | |
524 | * This is called to fork and execute a command when we have a tty. This | |
525 | * will call do_child from the child, and server_loop from the parent after | |
526 | * setting up file descriptors, controlling tty, updating wtmp, utmp, | |
527 | * lastlog, and other such operations. | |
528 | */ | |
529 | void | |
530 | do_exec_pty(Session *s, const char *command) | |
531 | { | |
532 | int fdout, ptyfd, ttyfd, ptymaster; | |
533 | pid_t pid; | |
534 | ||
535 | if (s == NULL) | |
536 | fatal("do_exec_pty: no session"); | |
537 | ptyfd = s->ptyfd; | |
538 | ttyfd = s->ttyfd; | |
539 | ||
5598e598 | 540 | #if defined(GSSAPI) |
541 | temporarily_use_uid(s->pw); | |
542 | ssh_gssapi_storecreds(); | |
543 | restore_uid(); | |
544 | #endif | |
545 | ||
3c0ef626 | 546 | #if defined(USE_PAM) |
547 | do_pam_session(s->pw->pw_name, s->tty); | |
548 | do_pam_setcred(1); | |
549 | #endif | |
550 | ||
551 | /* Fork the child. */ | |
552 | if ((pid = fork()) == 0) { | |
553 | ||
554 | /* Child. Reinitialize the log because the pid has changed. */ | |
555 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
556 | /* Close the master side of the pseudo tty. */ | |
557 | close(ptyfd); | |
558 | ||
559 | /* Make the pseudo tty our controlling tty. */ | |
560 | pty_make_controlling_tty(&ttyfd, s->tty); | |
561 | ||
562 | /* Redirect stdin/stdout/stderr from the pseudo tty. */ | |
563 | if (dup2(ttyfd, 0) < 0) | |
564 | error("dup2 stdin: %s", strerror(errno)); | |
565 | if (dup2(ttyfd, 1) < 0) | |
566 | error("dup2 stdout: %s", strerror(errno)); | |
567 | if (dup2(ttyfd, 2) < 0) | |
568 | error("dup2 stderr: %s", strerror(errno)); | |
569 | ||
570 | /* Close the extra descriptor for the pseudo tty. */ | |
571 | close(ttyfd); | |
572 | ||
573 | /* record login, etc. similar to login(1) */ | |
574 | #ifndef HAVE_OSF_SIA | |
575 | if (!(options.use_login && command == NULL)) | |
576 | do_login(s, command); | |
577 | # ifdef LOGIN_NEEDS_UTMPX | |
578 | else | |
579 | do_pre_login(s); | |
580 | # endif | |
581 | #endif | |
582 | ||
583 | /* Do common processing for the child, such as execing the command. */ | |
584 | do_child(s, command); | |
585 | /* NOTREACHED */ | |
586 | } | |
587 | #ifdef HAVE_CYGWIN | |
588 | if (is_winnt) | |
589 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); | |
590 | #endif | |
591 | if (pid < 0) | |
592 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
593 | s->pid = pid; | |
594 | ||
595 | /* Parent. Close the slave side of the pseudo tty. */ | |
596 | close(ttyfd); | |
597 | ||
598 | /* | |
599 | * Create another descriptor of the pty master side for use as the | |
600 | * standard input. We could use the original descriptor, but this | |
601 | * simplifies code in server_loop. The descriptor is bidirectional. | |
602 | */ | |
603 | fdout = dup(ptyfd); | |
604 | if (fdout < 0) | |
605 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); | |
606 | ||
607 | /* we keep a reference to the pty master */ | |
608 | ptymaster = dup(ptyfd); | |
609 | if (ptymaster < 0) | |
610 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); | |
611 | s->ptymaster = ptymaster; | |
612 | ||
613 | /* Enter interactive session. */ | |
614 | packet_set_interactive(1); | |
615 | if (compat20) { | |
616 | session_set_fds(s, ptyfd, fdout, -1); | |
617 | } else { | |
618 | server_loop(pid, ptyfd, fdout, -1); | |
619 | /* server_loop _has_ closed ptyfd and fdout. */ | |
620 | } | |
621 | } | |
622 | ||
623 | #ifdef LOGIN_NEEDS_UTMPX | |
624 | static void | |
625 | do_pre_login(Session *s) | |
626 | { | |
627 | socklen_t fromlen; | |
628 | struct sockaddr_storage from; | |
629 | pid_t pid = getpid(); | |
630 | ||
631 | /* | |
632 | * Get IP address of client. If the connection is not a socket, let | |
633 | * the address be 0.0.0.0. | |
634 | */ | |
635 | memset(&from, 0, sizeof(from)); | |
636 | if (packet_connection_is_on_socket()) { | |
637 | fromlen = sizeof(from); | |
638 | if (getpeername(packet_get_connection_in(), | |
e9702f7d | 639 | (struct sockaddr *) & from, &fromlen) < 0) { |
3c0ef626 | 640 | debug("getpeername: %.100s", strerror(errno)); |
641 | fatal_cleanup(); | |
642 | } | |
643 | } | |
644 | ||
645 | record_utmp_only(pid, s->tty, s->pw->pw_name, | |
e9702f7d | 646 | get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), |
3c0ef626 | 647 | (struct sockaddr *)&from); |
648 | } | |
649 | #endif | |
650 | ||
651 | /* | |
652 | * This is called to fork and execute a command. If another command is | |
653 | * to be forced, execute that instead. | |
654 | */ | |
655 | void | |
656 | do_exec(Session *s, const char *command) | |
657 | { | |
658 | if (forced_command) { | |
659 | original_command = command; | |
660 | command = forced_command; | |
661 | debug("Forced command '%.900s'", command); | |
662 | } | |
663 | ||
664 | if (s->ttyfd != -1) | |
665 | do_exec_pty(s, command); | |
666 | else | |
667 | do_exec_no_pty(s, command); | |
668 | ||
669 | original_command = NULL; | |
670 | } | |
671 | ||
e9702f7d | 672 | |
3c0ef626 | 673 | /* administrative, login(1)-like work */ |
674 | void | |
675 | do_login(Session *s, const char *command) | |
676 | { | |
677 | char *time_string; | |
678 | char hostname[MAXHOSTNAMELEN]; | |
679 | socklen_t fromlen; | |
680 | struct sockaddr_storage from; | |
681 | time_t last_login_time; | |
682 | struct passwd * pw = s->pw; | |
683 | pid_t pid = getpid(); | |
684 | ||
685 | /* | |
686 | * Get IP address of client. If the connection is not a socket, let | |
687 | * the address be 0.0.0.0. | |
688 | */ | |
689 | memset(&from, 0, sizeof(from)); | |
690 | if (packet_connection_is_on_socket()) { | |
691 | fromlen = sizeof(from); | |
692 | if (getpeername(packet_get_connection_in(), | |
e9702f7d | 693 | (struct sockaddr *) & from, &fromlen) < 0) { |
3c0ef626 | 694 | debug("getpeername: %.100s", strerror(errno)); |
695 | fatal_cleanup(); | |
696 | } | |
697 | } | |
698 | ||
699 | /* Get the time and hostname when the user last logged in. */ | |
700 | if (options.print_lastlog) { | |
701 | hostname[0] = '\0'; | |
702 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, | |
703 | hostname, sizeof(hostname)); | |
704 | } | |
705 | ||
706 | /* Record that there was a login on that tty from the remote host. */ | |
707 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, | |
e9702f7d | 708 | get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), |
3c0ef626 | 709 | (struct sockaddr *)&from); |
710 | ||
711 | #ifdef USE_PAM | |
712 | /* | |
713 | * If password change is needed, do it now. | |
714 | * This needs to occur before the ~/.hushlogin check. | |
715 | */ | |
716 | if (is_pam_password_change_required()) { | |
717 | print_pam_messages(); | |
718 | do_pam_chauthtok(); | |
719 | } | |
720 | #endif | |
721 | ||
722 | if (check_quietlogin(s, command)) | |
723 | return; | |
724 | ||
725 | #ifdef USE_PAM | |
726 | if (!is_pam_password_change_required()) | |
727 | print_pam_messages(); | |
728 | #endif /* USE_PAM */ | |
729 | #ifdef WITH_AIXAUTHENTICATE | |
730 | if (aixloginmsg && *aixloginmsg) | |
731 | printf("%s\n", aixloginmsg); | |
732 | #endif /* WITH_AIXAUTHENTICATE */ | |
733 | ||
734 | if (options.print_lastlog && last_login_time != 0) { | |
735 | time_string = ctime(&last_login_time); | |
736 | if (strchr(time_string, '\n')) | |
737 | *strchr(time_string, '\n') = 0; | |
738 | if (strcmp(hostname, "") == 0) | |
739 | printf("Last login: %s\r\n", time_string); | |
740 | else | |
741 | printf("Last login: %s from %s\r\n", time_string, hostname); | |
742 | } | |
743 | ||
744 | do_motd(); | |
745 | } | |
746 | ||
747 | /* | |
748 | * Display the message of the day. | |
749 | */ | |
750 | void | |
751 | do_motd(void) | |
752 | { | |
753 | FILE *f; | |
754 | char buf[256]; | |
755 | ||
756 | if (options.print_motd) { | |
757 | #ifdef HAVE_LOGIN_CAP | |
758 | f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", | |
759 | "/etc/motd"), "r"); | |
760 | #else | |
761 | f = fopen("/etc/motd", "r"); | |
762 | #endif | |
763 | if (f) { | |
764 | while (fgets(buf, sizeof(buf), f)) | |
765 | fputs(buf, stdout); | |
766 | fclose(f); | |
767 | } | |
768 | } | |
769 | } | |
770 | ||
771 | ||
772 | /* | |
773 | * Check for quiet login, either .hushlogin or command given. | |
774 | */ | |
775 | int | |
776 | check_quietlogin(Session *s, const char *command) | |
777 | { | |
778 | char buf[256]; | |
779 | struct passwd *pw = s->pw; | |
780 | struct stat st; | |
781 | ||
782 | /* Return 1 if .hushlogin exists or a command given. */ | |
783 | if (command != NULL) | |
784 | return 1; | |
785 | snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); | |
786 | #ifdef HAVE_LOGIN_CAP | |
787 | if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) | |
788 | return 1; | |
789 | #else | |
790 | if (stat(buf, &st) >= 0) | |
791 | return 1; | |
792 | #endif | |
793 | return 0; | |
794 | } | |
795 | ||
796 | /* | |
797 | * Sets the value of the given variable in the environment. If the variable | |
798 | * already exists, its value is overriden. | |
799 | */ | |
5598e598 | 800 | void |
3c0ef626 | 801 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
e9702f7d | 802 | const char *value) |
3c0ef626 | 803 | { |
804 | u_int i, namelen; | |
805 | char **env; | |
806 | ||
807 | /* | |
808 | * Find the slot where the value should be stored. If the variable | |
809 | * already exists, we reuse the slot; otherwise we append a new slot | |
810 | * at the end of the array, expanding if necessary. | |
811 | */ | |
812 | env = *envp; | |
813 | namelen = strlen(name); | |
814 | for (i = 0; env[i]; i++) | |
815 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') | |
816 | break; | |
817 | if (env[i]) { | |
818 | /* Reuse the slot. */ | |
819 | xfree(env[i]); | |
820 | } else { | |
821 | /* New variable. Expand if necessary. */ | |
822 | if (i >= (*envsizep) - 1) { | |
823 | (*envsizep) += 50; | |
824 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); | |
825 | } | |
826 | /* Need to set the NULL pointer at end of array beyond the new slot. */ | |
827 | env[i + 1] = NULL; | |
828 | } | |
829 | ||
830 | /* Allocate space and format the variable in the appropriate slot. */ | |
831 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); | |
832 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); | |
833 | } | |
834 | ||
835 | /* | |
836 | * Reads environment variables from the given file and adds/overrides them | |
837 | * into the environment. If the file does not exist, this does nothing. | |
838 | * Otherwise, it must consist of empty lines, comments (line starts with '#') | |
839 | * and assignments of the form name=value. No other forms are allowed. | |
840 | */ | |
841 | static void | |
842 | read_environment_file(char ***env, u_int *envsize, | |
e9702f7d | 843 | const char *filename) |
3c0ef626 | 844 | { |
845 | FILE *f; | |
846 | char buf[4096]; | |
847 | char *cp, *value; | |
848 | ||
849 | f = fopen(filename, "r"); | |
850 | if (!f) | |
851 | return; | |
852 | ||
853 | while (fgets(buf, sizeof(buf), f)) { | |
854 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) | |
855 | ; | |
856 | if (!*cp || *cp == '#' || *cp == '\n') | |
857 | continue; | |
858 | if (strchr(cp, '\n')) | |
859 | *strchr(cp, '\n') = '\0'; | |
860 | value = strchr(cp, '='); | |
861 | if (value == NULL) { | |
862 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); | |
863 | continue; | |
864 | } | |
865 | /* | |
866 | * Replace the equals sign by nul, and advance value to | |
867 | * the value string. | |
868 | */ | |
869 | *value = '\0'; | |
870 | value++; | |
871 | child_set_env(env, envsize, cp, value); | |
872 | } | |
873 | fclose(f); | |
874 | } | |
875 | ||
e9702f7d | 876 | void copy_environment(char **source, char ***env, u_int *envsize) |
3c0ef626 | 877 | { |
e9702f7d | 878 | char *var_name, *var_val; |
3c0ef626 | 879 | int i; |
880 | ||
e9702f7d | 881 | if (source == NULL) |
3c0ef626 | 882 | return; |
883 | ||
e9702f7d | 884 | for(i = 0; source[i] != NULL; i++) { |
885 | var_name = xstrdup(source[i]); | |
886 | if ((var_val = strstr(var_name, "=")) == NULL) { | |
887 | xfree(var_name); | |
3c0ef626 | 888 | continue; |
3c0ef626 | 889 | } |
e9702f7d | 890 | *var_val++ = '\0'; |
3c0ef626 | 891 | |
e9702f7d | 892 | debug3("Copy environment: %s=%s", var_name, var_val); |
893 | child_set_env(env, envsize, var_name, var_val); | |
894 | ||
895 | xfree(var_name); | |
3c0ef626 | 896 | } |
3c0ef626 | 897 | } |
898 | ||
e9702f7d | 899 | static char ** |
900 | do_setup_env(Session *s, const char *shell) | |
3c0ef626 | 901 | { |
3c0ef626 | 902 | char buf[256]; |
e9702f7d | 903 | u_int i, envsize; |
3c0ef626 | 904 | char **env; |
e9702f7d | 905 | struct passwd *pw = s->pw; |
3c0ef626 | 906 | |
907 | /* Initialize the environment. */ | |
908 | envsize = 100; | |
909 | env = xmalloc(envsize * sizeof(char *)); | |
910 | env[0] = NULL; | |
911 | ||
912 | #ifdef HAVE_CYGWIN | |
913 | /* | |
914 | * The Windows environment contains some setting which are | |
915 | * important for a running system. They must not be dropped. | |
916 | */ | |
e9702f7d | 917 | copy_environment(environ, &env, &envsize); |
3c0ef626 | 918 | #endif |
919 | ||
5598e598 | 920 | #ifdef GSSAPI |
921 | /* Allow any GSSAPI methods that we've used to alter | |
922 | * the childs environment as they see fit | |
923 | */ | |
924 | ssh_gssapi_do_child(&env,&envsize); | |
925 | #endif | |
926 | ||
3c0ef626 | 927 | if (!options.use_login) { |
928 | /* Set basic environment. */ | |
929 | child_set_env(&env, &envsize, "USER", pw->pw_name); | |
930 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); | |
931 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); | |
932 | #ifdef HAVE_LOGIN_CAP | |
933 | (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH); | |
934 | child_set_env(&env, &envsize, "PATH", getenv("PATH")); | |
935 | #else /* HAVE_LOGIN_CAP */ | |
936 | # ifndef HAVE_CYGWIN | |
937 | /* | |
938 | * There's no standard path on Windows. The path contains | |
939 | * important components pointing to the system directories, | |
940 | * needed for loading shared libraries. So the path better | |
941 | * remains intact here. | |
942 | */ | |
943 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); | |
944 | # endif /* HAVE_CYGWIN */ | |
945 | #endif /* HAVE_LOGIN_CAP */ | |
946 | ||
947 | snprintf(buf, sizeof buf, "%.200s/%.50s", | |
948 | _PATH_MAILDIR, pw->pw_name); | |
949 | child_set_env(&env, &envsize, "MAIL", buf); | |
950 | ||
951 | /* Normal systems set SHELL by default. */ | |
952 | child_set_env(&env, &envsize, "SHELL", shell); | |
953 | } | |
954 | if (getenv("TZ")) | |
955 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); | |
956 | ||
957 | /* Set custom environment options from RSA authentication. */ | |
958 | if (!options.use_login) { | |
959 | while (custom_environment) { | |
960 | struct envstring *ce = custom_environment; | |
961 | char *s = ce->s; | |
e9702f7d | 962 | |
3c0ef626 | 963 | for (i = 0; s[i] != '=' && s[i]; i++) |
964 | ; | |
965 | if (s[i] == '=') { | |
966 | s[i] = 0; | |
967 | child_set_env(&env, &envsize, s, s + i + 1); | |
968 | } | |
969 | custom_environment = ce->next; | |
970 | xfree(ce->s); | |
971 | xfree(ce); | |
972 | } | |
973 | } | |
974 | ||
975 | snprintf(buf, sizeof buf, "%.50s %d %d", | |
e9702f7d | 976 | get_remote_ipaddr(), get_remote_port(), get_local_port()); |
3c0ef626 | 977 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); |
978 | ||
979 | if (s->ttyfd != -1) | |
980 | child_set_env(&env, &envsize, "SSH_TTY", s->tty); | |
981 | if (s->term) | |
982 | child_set_env(&env, &envsize, "TERM", s->term); | |
983 | if (s->display) | |
984 | child_set_env(&env, &envsize, "DISPLAY", s->display); | |
985 | if (original_command) | |
986 | child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", | |
987 | original_command); | |
988 | ||
989 | #ifdef _AIX | |
e9702f7d | 990 | { |
991 | char *cp; | |
992 | ||
993 | if ((cp = getenv("AUTHSTATE")) != NULL) | |
994 | child_set_env(&env, &envsize, "AUTHSTATE", cp); | |
995 | if ((cp = getenv("KRB5CCNAME")) != NULL) | |
996 | child_set_env(&env, &envsize, "KRB5CCNAME", cp); | |
997 | read_environment_file(&env, &envsize, "/etc/environment"); | |
998 | } | |
3c0ef626 | 999 | #endif |
1000 | #ifdef KRB4 | |
1001 | if (s->authctxt->krb4_ticket_file) | |
1002 | child_set_env(&env, &envsize, "KRBTKFILE", | |
1003 | s->authctxt->krb4_ticket_file); | |
1004 | #endif | |
1005 | #ifdef KRB5 | |
1006 | if (s->authctxt->krb5_ticket_file) | |
1007 | child_set_env(&env, &envsize, "KRB5CCNAME", | |
1008 | s->authctxt->krb5_ticket_file); | |
1009 | #endif | |
1010 | #ifdef USE_PAM | |
1011 | /* Pull in any environment variables that may have been set by PAM. */ | |
e9702f7d | 1012 | copy_environment(fetch_pam_environment(), &env, &envsize); |
3c0ef626 | 1013 | #endif /* USE_PAM */ |
1014 | ||
1015 | if (auth_get_socket_name() != NULL) | |
1016 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, | |
e9702f7d | 1017 | auth_get_socket_name()); |
3c0ef626 | 1018 | |
1019 | /* read $HOME/.ssh/environment. */ | |
1020 | if (!options.use_login) { | |
1021 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", | |
1022 | pw->pw_dir); | |
1023 | read_environment_file(&env, &envsize, buf); | |
1024 | } | |
1025 | if (debug_flag) { | |
1026 | /* dump the environment */ | |
1027 | fprintf(stderr, "Environment:\n"); | |
1028 | for (i = 0; env[i]; i++) | |
1029 | fprintf(stderr, " %.200s\n", env[i]); | |
1030 | } | |
e9702f7d | 1031 | return env; |
1032 | } | |
1033 | ||
1034 | /* | |
1035 | * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found | |
1036 | * first in this order). | |
1037 | */ | |
1038 | static void | |
1039 | do_rc_files(Session *s, const char *shell) | |
1040 | { | |
1041 | FILE *f = NULL; | |
1042 | char cmd[1024]; | |
1043 | int do_xauth; | |
1044 | struct stat st; | |
1045 | ||
1046 | do_xauth = | |
1047 | s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; | |
1048 | ||
1049 | /* ignore _PATH_SSH_USER_RC for subsystems */ | |
1050 | if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) { | |
1051 | snprintf(cmd, sizeof cmd, "%s -c '%s %s'", | |
1052 | shell, _PATH_BSHELL, _PATH_SSH_USER_RC); | |
1053 | if (debug_flag) | |
1054 | fprintf(stderr, "Running %s\n", cmd); | |
1055 | f = popen(cmd, "w"); | |
1056 | if (f) { | |
1057 | if (do_xauth) | |
1058 | fprintf(f, "%s %s\n", s->auth_proto, | |
1059 | s->auth_data); | |
1060 | pclose(f); | |
1061 | } else | |
1062 | fprintf(stderr, "Could not run %s\n", | |
1063 | _PATH_SSH_USER_RC); | |
1064 | } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { | |
1065 | if (debug_flag) | |
1066 | fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, | |
1067 | _PATH_SSH_SYSTEM_RC); | |
1068 | f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); | |
1069 | if (f) { | |
1070 | if (do_xauth) | |
1071 | fprintf(f, "%s %s\n", s->auth_proto, | |
1072 | s->auth_data); | |
1073 | pclose(f); | |
1074 | } else | |
1075 | fprintf(stderr, "Could not run %s\n", | |
1076 | _PATH_SSH_SYSTEM_RC); | |
1077 | } else if (do_xauth && options.xauth_location != NULL) { | |
1078 | /* Add authority data to .Xauthority if appropriate. */ | |
1079 | if (debug_flag) { | |
1080 | fprintf(stderr, | |
1081 | "Running %.100s add " | |
1082 | "%.100s %.100s %.100s\n", | |
1083 | options.xauth_location, s->auth_display, | |
1084 | s->auth_proto, s->auth_data); | |
1085 | } | |
1086 | snprintf(cmd, sizeof cmd, "%s -q -", | |
1087 | options.xauth_location); | |
1088 | f = popen(cmd, "w"); | |
1089 | if (f) { | |
1090 | fprintf(f, "add %s %s %s\n", | |
1091 | s->auth_display, s->auth_proto, | |
1092 | s->auth_data); | |
1093 | pclose(f); | |
1094 | } else { | |
1095 | fprintf(stderr, "Could not run %s\n", | |
1096 | cmd); | |
1097 | } | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | static void | |
1102 | do_nologin(struct passwd *pw) | |
1103 | { | |
1104 | FILE *f = NULL; | |
1105 | char buf[1024]; | |
1106 | ||
1107 | #ifdef HAVE_LOGIN_CAP | |
1108 | if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) | |
1109 | f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, | |
1110 | _PATH_NOLOGIN), "r"); | |
1111 | #else | |
1112 | if (pw->pw_uid) | |
1113 | f = fopen(_PATH_NOLOGIN, "r"); | |
1114 | #endif | |
1115 | if (f) { | |
1116 | /* /etc/nologin exists. Print its contents and exit. */ | |
1117 | while (fgets(buf, sizeof(buf), f)) | |
1118 | fputs(buf, stderr); | |
1119 | fclose(f); | |
1120 | exit(254); | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | /* Set login name, uid, gid, and groups. */ | |
1125 | static void | |
1126 | do_setusercontext(struct passwd *pw) | |
1127 | { | |
1128 | #ifdef HAVE_CYGWIN | |
1129 | if (is_winnt) { | |
1130 | #else /* HAVE_CYGWIN */ | |
1131 | if (getuid() == 0 || geteuid() == 0) { | |
1132 | #endif /* HAVE_CYGWIN */ | |
1133 | #ifdef HAVE_GETUSERATTR | |
1134 | set_limits_from_userattr(pw->pw_name); | |
1135 | #endif /* HAVE_GETUSERATTR */ | |
1136 | #ifdef HAVE_LOGIN_CAP | |
1137 | if (setusercontext(lc, pw, pw->pw_uid, | |
1138 | (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) { | |
1139 | perror("unable to set user context"); | |
1140 | exit(1); | |
1141 | } | |
1142 | #else | |
1143 | # if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) | |
1144 | /* Sets login uid for accounting */ | |
1145 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) | |
1146 | error("setluid: %s", strerror(errno)); | |
1147 | # endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ | |
1148 | ||
1149 | if (setlogin(pw->pw_name) < 0) | |
1150 | error("setlogin failed: %s", strerror(errno)); | |
1151 | if (setgid(pw->pw_gid) < 0) { | |
1152 | perror("setgid"); | |
1153 | exit(1); | |
1154 | } | |
1155 | /* Initialize the group list. */ | |
1156 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { | |
1157 | perror("initgroups"); | |
1158 | exit(1); | |
1159 | } | |
1160 | endgrent(); | |
1161 | # ifdef USE_PAM | |
1162 | /* | |
1163 | * PAM credentials may take the form of supplementary groups. | |
1164 | * These will have been wiped by the above initgroups() call. | |
1165 | * Reestablish them here. | |
1166 | */ | |
1167 | do_pam_setcred(0); | |
1168 | # endif /* USE_PAM */ | |
1169 | # if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) | |
1170 | irix_setusercontext(pw); | |
1171 | # endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */ | |
1172 | /* Permanently switch to the desired uid. */ | |
1173 | permanently_set_uid(pw); | |
1174 | #endif | |
1175 | } | |
1176 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) | |
1177 | fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); | |
1178 | } | |
1179 | ||
1180 | /* | |
1181 | * Performs common processing for the child, such as setting up the | |
1182 | * environment, closing extra file descriptors, setting the user and group | |
1183 | * ids, and executing the command or shell. | |
1184 | */ | |
1185 | void | |
1186 | do_child(Session *s, const char *command) | |
1187 | { | |
1188 | extern char **environ; | |
1189 | char **env; | |
1190 | char *argv[10]; | |
1191 | const char *shell, *shell0, *hostname = NULL; | |
1192 | struct passwd *pw = s->pw; | |
1193 | u_int i; | |
1194 | ||
1195 | /* remove hostkey from the child's memory */ | |
1196 | destroy_sensitive_data(); | |
1197 | ||
1198 | /* login(1) is only called if we execute the login shell */ | |
1199 | if (options.use_login && command != NULL) | |
1200 | options.use_login = 0; | |
1201 | ||
1202 | /* | |
1203 | * Login(1) does this as well, and it needs uid 0 for the "-h" | |
1204 | * switch, so we let login(1) to this for us. | |
1205 | */ | |
1206 | if (!options.use_login) { | |
1207 | #ifdef HAVE_OSF_SIA | |
1208 | session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty); | |
1209 | if (!check_quietlogin(s, command)) | |
1210 | do_motd(); | |
1211 | #else /* HAVE_OSF_SIA */ | |
1212 | do_nologin(pw); | |
1213 | # ifdef _AIX | |
1214 | aix_usrinfo(pw, s->tty, s->ttyfd); | |
1215 | # endif /* _AIX */ | |
1216 | do_setusercontext(pw); | |
1217 | #endif /* HAVE_OSF_SIA */ | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Get the shell from the password data. An empty shell field is | |
1222 | * legal, and means /bin/sh. | |
1223 | */ | |
1224 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; | |
1225 | #ifdef HAVE_LOGIN_CAP | |
1226 | shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); | |
1227 | #endif | |
1228 | ||
1229 | env = do_setup_env(s, shell); | |
1230 | ||
3c0ef626 | 1231 | /* we have to stash the hostname before we close our socket. */ |
1232 | if (options.use_login) | |
1233 | hostname = get_remote_name_or_ip(utmp_len, | |
e9702f7d | 1234 | options.verify_reverse_mapping); |
3c0ef626 | 1235 | /* |
1236 | * Close the connection descriptors; note that this is the child, and | |
1237 | * the server will still have the socket open, and it is important | |
1238 | * that we do not shutdown it. Note that the descriptors cannot be | |
1239 | * closed before building the environment, as we call | |
1240 | * get_remote_ipaddr there. | |
1241 | */ | |
1242 | if (packet_get_connection_in() == packet_get_connection_out()) | |
1243 | close(packet_get_connection_in()); | |
1244 | else { | |
1245 | close(packet_get_connection_in()); | |
1246 | close(packet_get_connection_out()); | |
1247 | } | |
1248 | /* | |
1249 | * Close all descriptors related to channels. They will still remain | |
1250 | * open in the parent. | |
1251 | */ | |
1252 | /* XXX better use close-on-exec? -markus */ | |
1253 | channel_close_all(); | |
1254 | ||
1255 | /* | |
1256 | * Close any extra file descriptors. Note that there may still be | |
1257 | * descriptors left by system functions. They will be closed later. | |
1258 | */ | |
1259 | endpwent(); | |
1260 | ||
1261 | /* | |
1262 | * Close any extra open file descriptors so that we don\'t have them | |
1263 | * hanging around in clients. Note that we want to do this after | |
1264 | * initgroups, because at least on Solaris 2.3 it leaves file | |
1265 | * descriptors open. | |
1266 | */ | |
1267 | for (i = 3; i < 64; i++) | |
1268 | close(i); | |
1269 | ||
1270 | /* | |
e9702f7d | 1271 | * Must take new environment into use so that .ssh/rc, |
1272 | * /etc/ssh/sshrc and xauth are run in the proper environment. | |
3c0ef626 | 1273 | */ |
1274 | environ = env; | |
1275 | ||
1276 | #ifdef AFS | |
1277 | /* Try to get AFS tokens for the local cell. */ | |
1278 | if (k_hasafs()) { | |
1279 | char cell[64]; | |
e9702f7d | 1280 | |
3c0ef626 | 1281 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) |
1282 | krb_afslog(cell, 0); | |
e9702f7d | 1283 | |
3c0ef626 | 1284 | krb_afslog(0, 0); |
1285 | } | |
1286 | #endif /* AFS */ | |
1287 | ||
1288 | /* Change current directory to the user\'s home directory. */ | |
1289 | if (chdir(pw->pw_dir) < 0) { | |
1290 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", | |
1291 | pw->pw_dir, strerror(errno)); | |
1292 | #ifdef HAVE_LOGIN_CAP | |
1293 | if (login_getcapbool(lc, "requirehome", 0)) | |
1294 | exit(1); | |
1295 | #endif | |
1296 | } | |
1297 | ||
e9702f7d | 1298 | if (!options.use_login) |
1299 | do_rc_files(s, shell); | |
3c0ef626 | 1300 | |
1301 | /* restore SIGPIPE for child */ | |
1302 | signal(SIGPIPE, SIG_DFL); | |
1303 | ||
e9702f7d | 1304 | if (options.use_login) { |
1305 | /* Launch login(1). */ | |
1306 | ||
1307 | execl(LOGIN_PROGRAM, "login", "-h", hostname, | |
1308 | #ifdef LOGIN_NEEDS_TERM | |
1309 | (s->term ? s->term : "unknown"), | |
1310 | #endif /* LOGIN_NEEDS_TERM */ | |
1311 | "-p", "-f", "--", pw->pw_name, (char *)NULL); | |
1312 | ||
1313 | /* Login couldn't be executed, die. */ | |
1314 | ||
1315 | perror("login"); | |
1316 | exit(1); | |
1317 | } | |
1318 | ||
1319 | /* Get the last component of the shell name. */ | |
1320 | if ((shell0 = strrchr(shell, '/')) != NULL) | |
1321 | shell0++; | |
1322 | else | |
1323 | shell0 = shell; | |
1324 | ||
3c0ef626 | 1325 | /* |
1326 | * If we have no command, execute the shell. In this case, the shell | |
1327 | * name to be passed in argv[0] is preceded by '-' to indicate that | |
1328 | * this is a login shell. | |
1329 | */ | |
1330 | if (!command) { | |
e9702f7d | 1331 | char argv0[256]; |
3c0ef626 | 1332 | |
e9702f7d | 1333 | /* Start the shell. Set initial character to '-'. */ |
1334 | argv0[0] = '-'; | |
3c0ef626 | 1335 | |
e9702f7d | 1336 | if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1) |
1337 | >= sizeof(argv0) - 1) { | |
1338 | errno = EINVAL; | |
3c0ef626 | 1339 | perror(shell); |
1340 | exit(1); | |
e9702f7d | 1341 | } |
3c0ef626 | 1342 | |
e9702f7d | 1343 | /* Execute the shell. */ |
1344 | argv[0] = argv0; | |
1345 | argv[1] = NULL; | |
1346 | execve(shell, argv, env); | |
3c0ef626 | 1347 | |
e9702f7d | 1348 | /* Executing the shell failed. */ |
1349 | perror(shell); | |
1350 | exit(1); | |
3c0ef626 | 1351 | } |
1352 | /* | |
1353 | * Execute the command using the user's shell. This uses the -c | |
1354 | * option to execute the command. | |
1355 | */ | |
e9702f7d | 1356 | argv[0] = (char *) shell0; |
3c0ef626 | 1357 | argv[1] = "-c"; |
1358 | argv[2] = (char *) command; | |
1359 | argv[3] = NULL; | |
1360 | execve(shell, argv, env); | |
1361 | perror(shell); | |
1362 | exit(1); | |
1363 | } | |
1364 | ||
1365 | Session * | |
1366 | session_new(void) | |
1367 | { | |
1368 | int i; | |
1369 | static int did_init = 0; | |
1370 | if (!did_init) { | |
1371 | debug("session_new: init"); | |
e9702f7d | 1372 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1373 | sessions[i].used = 0; |
1374 | } | |
1375 | did_init = 1; | |
1376 | } | |
e9702f7d | 1377 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1378 | Session *s = &sessions[i]; |
1379 | if (! s->used) { | |
1380 | memset(s, 0, sizeof(*s)); | |
1381 | s->chanid = -1; | |
1382 | s->ptyfd = -1; | |
1383 | s->ttyfd = -1; | |
1384 | s->used = 1; | |
1385 | s->self = i; | |
1386 | debug("session_new: session %d", i); | |
1387 | return s; | |
1388 | } | |
1389 | } | |
1390 | return NULL; | |
1391 | } | |
1392 | ||
1393 | static void | |
1394 | session_dump(void) | |
1395 | { | |
1396 | int i; | |
e9702f7d | 1397 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1398 | Session *s = &sessions[i]; |
1399 | debug("dump: used %d session %d %p channel %d pid %d", | |
1400 | s->used, | |
1401 | s->self, | |
1402 | s, | |
1403 | s->chanid, | |
1404 | s->pid); | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | int | |
1409 | session_open(Authctxt *authctxt, int chanid) | |
1410 | { | |
1411 | Session *s = session_new(); | |
1412 | debug("session_open: channel %d", chanid); | |
1413 | if (s == NULL) { | |
1414 | error("no more sessions"); | |
1415 | return 0; | |
1416 | } | |
1417 | s->authctxt = authctxt; | |
1418 | s->pw = authctxt->pw; | |
1419 | if (s->pw == NULL) | |
1420 | fatal("no user for session %d", s->self); | |
1421 | debug("session_open: session %d: link with channel %d", s->self, chanid); | |
1422 | s->chanid = chanid; | |
1423 | return 1; | |
1424 | } | |
1425 | ||
1426 | static Session * | |
1427 | session_by_channel(int id) | |
1428 | { | |
1429 | int i; | |
e9702f7d | 1430 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1431 | Session *s = &sessions[i]; |
1432 | if (s->used && s->chanid == id) { | |
1433 | debug("session_by_channel: session %d channel %d", i, id); | |
1434 | return s; | |
1435 | } | |
1436 | } | |
1437 | debug("session_by_channel: unknown channel %d", id); | |
1438 | session_dump(); | |
1439 | return NULL; | |
1440 | } | |
1441 | ||
1442 | static Session * | |
1443 | session_by_pid(pid_t pid) | |
1444 | { | |
1445 | int i; | |
1446 | debug("session_by_pid: pid %d", pid); | |
e9702f7d | 1447 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1448 | Session *s = &sessions[i]; |
1449 | if (s->used && s->pid == pid) | |
1450 | return s; | |
1451 | } | |
1452 | error("session_by_pid: unknown pid %d", pid); | |
1453 | session_dump(); | |
1454 | return NULL; | |
1455 | } | |
1456 | ||
1457 | static int | |
1458 | session_window_change_req(Session *s) | |
1459 | { | |
1460 | s->col = packet_get_int(); | |
1461 | s->row = packet_get_int(); | |
1462 | s->xpixel = packet_get_int(); | |
1463 | s->ypixel = packet_get_int(); | |
e9702f7d | 1464 | packet_check_eom(); |
3c0ef626 | 1465 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
1466 | return 1; | |
1467 | } | |
1468 | ||
1469 | static int | |
1470 | session_pty_req(Session *s) | |
1471 | { | |
1472 | u_int len; | |
1473 | int n_bytes; | |
1474 | ||
1475 | if (no_pty_flag) { | |
1476 | debug("Allocating a pty not permitted for this authentication."); | |
1477 | return 0; | |
1478 | } | |
1479 | if (s->ttyfd != -1) { | |
1480 | packet_disconnect("Protocol error: you already have a pty."); | |
1481 | return 0; | |
1482 | } | |
1483 | ||
1484 | s->term = packet_get_string(&len); | |
1485 | ||
1486 | if (compat20) { | |
1487 | s->col = packet_get_int(); | |
1488 | s->row = packet_get_int(); | |
1489 | } else { | |
1490 | s->row = packet_get_int(); | |
1491 | s->col = packet_get_int(); | |
1492 | } | |
1493 | s->xpixel = packet_get_int(); | |
1494 | s->ypixel = packet_get_int(); | |
1495 | ||
1496 | if (strcmp(s->term, "") == 0) { | |
1497 | xfree(s->term); | |
1498 | s->term = NULL; | |
1499 | } | |
1500 | ||
1501 | /* Allocate a pty and open it. */ | |
1502 | debug("Allocating pty."); | |
1503 | if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) { | |
1504 | if (s->term) | |
1505 | xfree(s->term); | |
1506 | s->term = NULL; | |
1507 | s->ptyfd = -1; | |
1508 | s->ttyfd = -1; | |
1509 | error("session_pty_req: session %d alloc failed", s->self); | |
1510 | return 0; | |
1511 | } | |
1512 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); | |
1513 | ||
1514 | /* for SSH1 the tty modes length is not given */ | |
1515 | if (!compat20) | |
1516 | n_bytes = packet_remaining(); | |
1517 | tty_parse_modes(s->ttyfd, &n_bytes); | |
1518 | ||
1519 | /* | |
1520 | * Add a cleanup function to clear the utmp entry and record logout | |
1521 | * time in case we call fatal() (e.g., the connection gets closed). | |
1522 | */ | |
1523 | fatal_add_cleanup(session_pty_cleanup, (void *)s); | |
1524 | pty_setowner(s->pw, s->tty); | |
1525 | ||
1526 | /* Set window size from the packet. */ | |
1527 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); | |
1528 | ||
e9702f7d | 1529 | packet_check_eom(); |
3c0ef626 | 1530 | session_proctitle(s); |
1531 | return 1; | |
1532 | } | |
1533 | ||
1534 | static int | |
1535 | session_subsystem_req(Session *s) | |
1536 | { | |
1537 | struct stat st; | |
1538 | u_int len; | |
1539 | int success = 0; | |
1540 | char *cmd, *subsys = packet_get_string(&len); | |
1541 | int i; | |
1542 | ||
e9702f7d | 1543 | packet_check_eom(); |
1544 | log("subsystem request for %.100s", subsys); | |
3c0ef626 | 1545 | |
1546 | for (i = 0; i < options.num_subsystems; i++) { | |
1547 | if (strcmp(subsys, options.subsystem_name[i]) == 0) { | |
1548 | cmd = options.subsystem_command[i]; | |
1549 | if (stat(cmd, &st) < 0) { | |
1550 | error("subsystem: cannot stat %s: %s", cmd, | |
1551 | strerror(errno)); | |
1552 | break; | |
1553 | } | |
1554 | debug("subsystem: exec() %s", cmd); | |
1555 | s->is_subsystem = 1; | |
1556 | do_exec(s, cmd); | |
1557 | success = 1; | |
e9702f7d | 1558 | break; |
3c0ef626 | 1559 | } |
1560 | } | |
1561 | ||
1562 | if (!success) | |
e9702f7d | 1563 | log("subsystem request for %.100s failed, subsystem not found", |
3c0ef626 | 1564 | subsys); |
1565 | ||
1566 | xfree(subsys); | |
1567 | return success; | |
1568 | } | |
1569 | ||
1570 | static int | |
1571 | session_x11_req(Session *s) | |
1572 | { | |
1573 | int success; | |
1574 | ||
1575 | s->single_connection = packet_get_char(); | |
1576 | s->auth_proto = packet_get_string(NULL); | |
1577 | s->auth_data = packet_get_string(NULL); | |
1578 | s->screen = packet_get_int(); | |
e9702f7d | 1579 | packet_check_eom(); |
3c0ef626 | 1580 | |
1581 | success = session_setup_x11fwd(s); | |
1582 | if (!success) { | |
1583 | xfree(s->auth_proto); | |
1584 | xfree(s->auth_data); | |
1585 | s->auth_proto = NULL; | |
1586 | s->auth_data = NULL; | |
1587 | } | |
1588 | return success; | |
1589 | } | |
1590 | ||
1591 | static int | |
1592 | session_shell_req(Session *s) | |
1593 | { | |
e9702f7d | 1594 | packet_check_eom(); |
3c0ef626 | 1595 | do_exec(s, NULL); |
1596 | return 1; | |
1597 | } | |
1598 | ||
1599 | static int | |
1600 | session_exec_req(Session *s) | |
1601 | { | |
1602 | u_int len; | |
1603 | char *command = packet_get_string(&len); | |
e9702f7d | 1604 | packet_check_eom(); |
3c0ef626 | 1605 | do_exec(s, command); |
1606 | xfree(command); | |
1607 | return 1; | |
1608 | } | |
1609 | ||
1610 | static int | |
1611 | session_auth_agent_req(Session *s) | |
1612 | { | |
1613 | static int called = 0; | |
e9702f7d | 1614 | packet_check_eom(); |
3c0ef626 | 1615 | if (no_agent_forwarding_flag) { |
1616 | debug("session_auth_agent_req: no_agent_forwarding_flag"); | |
1617 | return 0; | |
1618 | } | |
1619 | if (called) { | |
1620 | return 0; | |
1621 | } else { | |
1622 | called = 1; | |
1623 | return auth_input_request_forwarding(s->pw); | |
1624 | } | |
1625 | } | |
1626 | ||
e9702f7d | 1627 | int |
1628 | session_input_channel_req(Channel *c, const char *rtype) | |
3c0ef626 | 1629 | { |
3c0ef626 | 1630 | int success = 0; |
3c0ef626 | 1631 | Session *s; |
3c0ef626 | 1632 | |
e9702f7d | 1633 | if ((s = session_by_channel(c->self)) == NULL) { |
1634 | log("session_input_channel_req: no session %d req %.100s", | |
1635 | c->self, rtype); | |
1636 | return 0; | |
1637 | } | |
1638 | debug("session_input_channel_req: session %d req %s", s->self, rtype); | |
3c0ef626 | 1639 | |
1640 | /* | |
1641 | * a session is in LARVAL state until a shell, a command | |
1642 | * or a subsystem is executed | |
1643 | */ | |
1644 | if (c->type == SSH_CHANNEL_LARVAL) { | |
1645 | if (strcmp(rtype, "shell") == 0) { | |
1646 | success = session_shell_req(s); | |
1647 | } else if (strcmp(rtype, "exec") == 0) { | |
1648 | success = session_exec_req(s); | |
1649 | } else if (strcmp(rtype, "pty-req") == 0) { | |
1650 | success = session_pty_req(s); | |
1651 | } else if (strcmp(rtype, "x11-req") == 0) { | |
1652 | success = session_x11_req(s); | |
1653 | } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { | |
1654 | success = session_auth_agent_req(s); | |
1655 | } else if (strcmp(rtype, "subsystem") == 0) { | |
1656 | success = session_subsystem_req(s); | |
1657 | } | |
1658 | } | |
1659 | if (strcmp(rtype, "window-change") == 0) { | |
1660 | success = session_window_change_req(s); | |
1661 | } | |
e9702f7d | 1662 | return success; |
3c0ef626 | 1663 | } |
1664 | ||
1665 | void | |
1666 | session_set_fds(Session *s, int fdin, int fdout, int fderr) | |
1667 | { | |
1668 | if (!compat20) | |
1669 | fatal("session_set_fds: called for proto != 2.0"); | |
1670 | /* | |
1671 | * now that have a child and a pipe to the child, | |
1672 | * we can activate our channel and register the fd's | |
1673 | */ | |
1674 | if (s->chanid == -1) | |
1675 | fatal("no channel for session %d", s->self); | |
1676 | channel_set_fds(s->chanid, | |
1677 | fdout, fdin, fderr, | |
1678 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, | |
e9702f7d | 1679 | 1, |
1680 | CHAN_SES_WINDOW_DEFAULT); | |
3c0ef626 | 1681 | } |
1682 | ||
1683 | /* | |
1684 | * Function to perform pty cleanup. Also called if we get aborted abnormally | |
1685 | * (e.g., due to a dropped connection). | |
1686 | */ | |
1687 | static void | |
1688 | session_pty_cleanup(void *session) | |
1689 | { | |
1690 | Session *s = session; | |
1691 | ||
1692 | if (s == NULL) { | |
1693 | error("session_pty_cleanup: no session"); | |
1694 | return; | |
1695 | } | |
1696 | if (s->ttyfd == -1) | |
1697 | return; | |
1698 | ||
1699 | debug("session_pty_cleanup: session %d release %s", s->self, s->tty); | |
1700 | ||
1701 | /* Record that the user has logged out. */ | |
1702 | if (s->pid != 0) | |
e9702f7d | 1703 | record_logout(s->pid, s->tty, s->pw->pw_name); |
3c0ef626 | 1704 | |
1705 | /* Release the pseudo-tty. */ | |
1706 | pty_release(s->tty); | |
1707 | ||
1708 | /* | |
1709 | * Close the server side of the socket pairs. We must do this after | |
1710 | * the pty cleanup, so that another process doesn't get this pty | |
1711 | * while we're still cleaning up. | |
1712 | */ | |
1713 | if (close(s->ptymaster) < 0) | |
1714 | error("close(s->ptymaster): %s", strerror(errno)); | |
1715 | ||
1716 | /* unlink pty from session */ | |
1717 | s->ttyfd = -1; | |
1718 | } | |
1719 | ||
1720 | static void | |
1721 | session_exit_message(Session *s, int status) | |
1722 | { | |
1723 | Channel *c; | |
e9702f7d | 1724 | |
1725 | if ((c = channel_lookup(s->chanid)) == NULL) | |
3c0ef626 | 1726 | fatal("session_exit_message: session %d: no channel %d", |
1727 | s->self, s->chanid); | |
1728 | debug("session_exit_message: session %d channel %d pid %d", | |
1729 | s->self, s->chanid, s->pid); | |
1730 | ||
1731 | if (WIFEXITED(status)) { | |
e9702f7d | 1732 | channel_request_start(s->chanid, "exit-status", 0); |
3c0ef626 | 1733 | packet_put_int(WEXITSTATUS(status)); |
1734 | packet_send(); | |
1735 | } else if (WIFSIGNALED(status)) { | |
e9702f7d | 1736 | channel_request_start(s->chanid, "exit-signal", 0); |
3c0ef626 | 1737 | packet_put_int(WTERMSIG(status)); |
1738 | #ifdef WCOREDUMP | |
1739 | packet_put_char(WCOREDUMP(status)); | |
1740 | #else /* WCOREDUMP */ | |
1741 | packet_put_char(0); | |
1742 | #endif /* WCOREDUMP */ | |
1743 | packet_put_cstring(""); | |
1744 | packet_put_cstring(""); | |
1745 | packet_send(); | |
1746 | } else { | |
1747 | /* Some weird exit cause. Just exit. */ | |
1748 | packet_disconnect("wait returned status %04x.", status); | |
1749 | } | |
1750 | ||
1751 | /* disconnect channel */ | |
1752 | debug("session_exit_message: release channel %d", s->chanid); | |
1753 | channel_cancel_cleanup(s->chanid); | |
1754 | /* | |
1755 | * emulate a write failure with 'chan_write_failed', nobody will be | |
1756 | * interested in data we write. | |
1757 | * Note that we must not call 'chan_read_failed', since there could | |
1758 | * be some more data waiting in the pipe. | |
1759 | */ | |
1760 | if (c->ostate != CHAN_OUTPUT_CLOSED) | |
1761 | chan_write_failed(c); | |
1762 | s->chanid = -1; | |
1763 | } | |
1764 | ||
1765 | static void | |
1766 | session_close(Session *s) | |
1767 | { | |
1768 | debug("session_close: session %d pid %d", s->self, s->pid); | |
1769 | if (s->ttyfd != -1) { | |
1770 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); | |
1771 | session_pty_cleanup(s); | |
1772 | } | |
1773 | if (s->term) | |
1774 | xfree(s->term); | |
1775 | if (s->display) | |
1776 | xfree(s->display); | |
e9702f7d | 1777 | if (s->auth_display) |
1778 | xfree(s->auth_display); | |
3c0ef626 | 1779 | if (s->auth_data) |
1780 | xfree(s->auth_data); | |
1781 | if (s->auth_proto) | |
1782 | xfree(s->auth_proto); | |
1783 | s->used = 0; | |
1784 | session_proctitle(s); | |
1785 | } | |
1786 | ||
1787 | void | |
1788 | session_close_by_pid(pid_t pid, int status) | |
1789 | { | |
1790 | Session *s = session_by_pid(pid); | |
1791 | if (s == NULL) { | |
1792 | debug("session_close_by_pid: no session for pid %d", pid); | |
1793 | return; | |
1794 | } | |
1795 | if (s->chanid != -1) | |
1796 | session_exit_message(s, status); | |
1797 | session_close(s); | |
1798 | } | |
1799 | ||
1800 | /* | |
1801 | * this is called when a channel dies before | |
1802 | * the session 'child' itself dies | |
1803 | */ | |
1804 | void | |
1805 | session_close_by_channel(int id, void *arg) | |
1806 | { | |
1807 | Session *s = session_by_channel(id); | |
1808 | if (s == NULL) { | |
1809 | debug("session_close_by_channel: no session for id %d", id); | |
1810 | return; | |
1811 | } | |
1812 | debug("session_close_by_channel: channel %d child %d", id, s->pid); | |
1813 | if (s->pid != 0) { | |
1814 | debug("session_close_by_channel: channel %d: has child", id); | |
1815 | /* | |
1816 | * delay detach of session, but release pty, since | |
1817 | * the fd's to the child are already closed | |
1818 | */ | |
1819 | if (s->ttyfd != -1) { | |
1820 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); | |
1821 | session_pty_cleanup(s); | |
1822 | } | |
1823 | return; | |
1824 | } | |
1825 | /* detach by removing callback */ | |
1826 | channel_cancel_cleanup(s->chanid); | |
1827 | s->chanid = -1; | |
1828 | session_close(s); | |
1829 | } | |
1830 | ||
1831 | void | |
1832 | session_destroy_all(void) | |
1833 | { | |
1834 | int i; | |
e9702f7d | 1835 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1836 | Session *s = &sessions[i]; |
e9702f7d | 1837 | if (s->used) |
3c0ef626 | 1838 | session_close(s); |
1839 | } | |
1840 | } | |
1841 | ||
1842 | static char * | |
1843 | session_tty_list(void) | |
1844 | { | |
1845 | static char buf[1024]; | |
1846 | int i; | |
1847 | buf[0] = '\0'; | |
e9702f7d | 1848 | for (i = 0; i < MAX_SESSIONS; i++) { |
3c0ef626 | 1849 | Session *s = &sessions[i]; |
1850 | if (s->used && s->ttyfd != -1) { | |
1851 | if (buf[0] != '\0') | |
1852 | strlcat(buf, ",", sizeof buf); | |
1853 | strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf); | |
1854 | } | |
1855 | } | |
1856 | if (buf[0] == '\0') | |
1857 | strlcpy(buf, "notty", sizeof buf); | |
1858 | return buf; | |
1859 | } | |
1860 | ||
1861 | void | |
1862 | session_proctitle(Session *s) | |
1863 | { | |
1864 | if (s->pw == NULL) | |
1865 | error("no user for session %d", s->self); | |
1866 | else | |
1867 | setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); | |
1868 | } | |
1869 | ||
1870 | int | |
1871 | session_setup_x11fwd(Session *s) | |
1872 | { | |
1873 | struct stat st; | |
e9702f7d | 1874 | char display[512], auth_display[512]; |
1875 | char hostname[MAXHOSTNAMELEN]; | |
3c0ef626 | 1876 | |
1877 | if (no_x11_forwarding_flag) { | |
1878 | packet_send_debug("X11 forwarding disabled in user configuration file."); | |
1879 | return 0; | |
1880 | } | |
1881 | if (!options.x11_forwarding) { | |
1882 | debug("X11 forwarding disabled in server configuration file."); | |
1883 | return 0; | |
1884 | } | |
1885 | if (!options.xauth_location || | |
1886 | (stat(options.xauth_location, &st) == -1)) { | |
1887 | packet_send_debug("No xauth program; cannot forward with spoofing."); | |
1888 | return 0; | |
1889 | } | |
1890 | if (options.use_login) { | |
1891 | packet_send_debug("X11 forwarding disabled; " | |
1892 | "not compatible with UseLogin=yes."); | |
1893 | return 0; | |
1894 | } | |
1895 | if (s->display != NULL) { | |
1896 | debug("X11 display already set."); | |
1897 | return 0; | |
1898 | } | |
e9702f7d | 1899 | s->display_number = x11_create_display_inet(options.x11_display_offset, |
1900 | options.x11_use_localhost, s->single_connection); | |
1901 | if (s->display_number == -1) { | |
3c0ef626 | 1902 | debug("x11_create_display_inet failed."); |
1903 | return 0; | |
1904 | } | |
e9702f7d | 1905 | |
1906 | /* Set up a suitable value for the DISPLAY variable. */ | |
1907 | if (gethostname(hostname, sizeof(hostname)) < 0) | |
1908 | fatal("gethostname: %.100s", strerror(errno)); | |
1909 | /* | |
1910 | * auth_display must be used as the displayname when the | |
1911 | * authorization entry is added with xauth(1). This will be | |
1912 | * different than the DISPLAY string for localhost displays. | |
1913 | */ | |
1914 | if (options.x11_use_localhost) { | |
1915 | snprintf(display, sizeof display, "localhost:%d.%d", | |
1916 | s->display_number, s->screen); | |
1917 | snprintf(auth_display, sizeof auth_display, "unix:%d.%d", | |
1918 | s->display_number, s->screen); | |
1919 | s->display = xstrdup(display); | |
1920 | s->auth_display = xstrdup(auth_display); | |
1921 | } else { | |
1922 | #ifdef IPADDR_IN_DISPLAY | |
1923 | struct hostent *he; | |
1924 | struct in_addr my_addr; | |
1925 | ||
1926 | he = gethostbyname(hostname); | |
1927 | if (he == NULL) { | |
1928 | error("Can't get IP address for X11 DISPLAY."); | |
1929 | packet_send_debug("Can't get IP address for X11 DISPLAY."); | |
1930 | return 0; | |
1931 | } | |
1932 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); | |
1933 | snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr), | |
1934 | s->display_number, s->screen); | |
1935 | #else | |
1936 | snprintf(display, sizeof display, "%.400s:%d.%d", hostname, | |
1937 | s->display_number, s->screen); | |
1938 | #endif | |
1939 | s->display = xstrdup(display); | |
1940 | s->auth_display = xstrdup(display); | |
1941 | } | |
1942 | ||
3c0ef626 | 1943 | return 1; |
1944 | } | |
1945 | ||
1946 | static void | |
1947 | do_authenticated2(Authctxt *authctxt) | |
1948 | { | |
1949 | server_loop2(authctxt); | |
5598e598 | 1950 | #if defined(GSSAPI) |
1951 | ssh_gssapi_cleanup_creds(NULL); | |
1952 | #endif | |
3c0ef626 | 1953 | } |