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