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