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