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