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