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