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