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