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