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