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