]>
Commit | Line | Data |
---|---|---|
7368a6c8 | 1 | /* |
2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
3 | * All rights reserved | |
4 | */ | |
5 | ||
6 | #include "includes.h" | |
7 | RCSID("$OpenBSD: session.c,v 1.1 2000/03/28 21:15:45 markus Exp $"); | |
8 | ||
9 | #include "xmalloc.h" | |
10 | #include "ssh.h" | |
11 | #include "pty.h" | |
12 | #include "packet.h" | |
13 | #include "buffer.h" | |
14 | #include "cipher.h" | |
15 | #include "mpaux.h" | |
16 | #include "servconf.h" | |
17 | #include "uidswap.h" | |
18 | #include "compat.h" | |
19 | #include "channels.h" | |
20 | #include "nchan.h" | |
21 | ||
22 | /* types */ | |
23 | ||
24 | #define TTYSZ 64 | |
25 | typedef struct Session Session; | |
26 | struct Session { | |
27 | int used; | |
28 | int self; | |
29 | struct passwd *pw; | |
30 | pid_t pid; | |
31 | /* tty */ | |
32 | char *term; | |
33 | int ptyfd, ttyfd, ptymaster; | |
34 | int row, col, xpixel, ypixel; | |
35 | char tty[TTYSZ]; | |
36 | /* X11 */ | |
37 | char *display; | |
38 | int screen; | |
39 | char *auth_proto; | |
40 | char *auth_data; | |
41 | /* proto 2 */ | |
42 | int chanid; | |
43 | }; | |
44 | ||
45 | /* func */ | |
46 | ||
47 | Session *session_new(void); | |
48 | void session_set_fds(Session *s, int fdin, int fdout, int fderr); | |
49 | void session_pty_cleanup(Session *s); | |
50 | void do_exec_pty(Session *s, const char *command, struct passwd * pw); | |
51 | void do_exec_no_pty(Session *s, const char *command, struct passwd * pw); | |
52 | ||
53 | void | |
54 | do_child(const char *command, struct passwd * pw, const char *term, | |
55 | const char *display, const char *auth_proto, | |
56 | const char *auth_data, const char *ttyname); | |
57 | ||
58 | /* import */ | |
59 | extern ServerOptions options; | |
60 | extern char *__progname; | |
61 | extern int log_stderr; | |
62 | extern int debug_flag; | |
63 | ||
64 | /* Local Xauthority file. */ | |
65 | static char *xauthfile; | |
66 | ||
67 | /* data */ | |
68 | #define MAX_SESSIONS 10 | |
69 | Session sessions[MAX_SESSIONS]; | |
70 | ||
71 | /* Flags set in auth-rsa from authorized_keys flags. These are set in auth-rsa.c. */ | |
72 | int no_port_forwarding_flag = 0; | |
73 | int no_agent_forwarding_flag = 0; | |
74 | int no_x11_forwarding_flag = 0; | |
75 | int no_pty_flag = 0; | |
76 | ||
77 | /* RSA authentication "command=" option. */ | |
78 | char *forced_command = NULL; | |
79 | ||
80 | /* RSA authentication "environment=" options. */ | |
81 | struct envstring *custom_environment = NULL; | |
82 | ||
83 | /* | |
84 | * Remove local Xauthority file. | |
85 | */ | |
86 | void | |
87 | xauthfile_cleanup_proc(void *ignore) | |
88 | { | |
89 | debug("xauthfile_cleanup_proc called"); | |
90 | ||
91 | if (xauthfile != NULL) { | |
92 | char *p; | |
93 | unlink(xauthfile); | |
94 | p = strrchr(xauthfile, '/'); | |
95 | if (p != NULL) { | |
96 | *p = '\0'; | |
97 | rmdir(xauthfile); | |
98 | } | |
99 | xfree(xauthfile); | |
100 | xauthfile = NULL; | |
101 | } | |
102 | } | |
103 | ||
104 | /* | |
105 | * Function to perform cleanup if we get aborted abnormally (e.g., due to a | |
106 | * dropped connection). | |
107 | */ | |
108 | void | |
109 | pty_cleanup_proc(void *session) | |
110 | { | |
111 | Session *s=session; | |
112 | if (s == NULL) | |
113 | fatal("pty_cleanup_proc: no session"); | |
114 | debug("pty_cleanup_proc: %s", s->tty); | |
115 | ||
116 | if (s->pid != 0) { | |
117 | /* Record that the user has logged out. */ | |
118 | record_logout(s->pid, s->tty); | |
119 | } | |
120 | ||
121 | /* Release the pseudo-tty. */ | |
122 | pty_release(s->tty); | |
123 | } | |
124 | ||
125 | /* | |
126 | * Prepares for an interactive session. This is called after the user has | |
127 | * been successfully authenticated. During this message exchange, pseudo | |
128 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings | |
129 | * are requested, etc. | |
130 | */ | |
131 | void | |
132 | do_authenticated(struct passwd * pw) | |
133 | { | |
134 | Session *s; | |
135 | int type; | |
136 | int compression_level = 0, enable_compression_after_reply = 0; | |
137 | int have_pty = 0; | |
138 | char *command; | |
139 | int n_bytes; | |
140 | int plen; | |
141 | unsigned int proto_len, data_len, dlen; | |
142 | ||
143 | /* | |
144 | * Cancel the alarm we set to limit the time taken for | |
145 | * authentication. | |
146 | */ | |
147 | alarm(0); | |
148 | ||
149 | /* | |
150 | * Inform the channel mechanism that we are the server side and that | |
151 | * the client may request to connect to any port at all. (The user | |
152 | * could do it anyway, and we wouldn\'t know what is permitted except | |
153 | * by the client telling us, so we can equally well trust the client | |
154 | * not to request anything bogus.) | |
155 | */ | |
156 | if (!no_port_forwarding_flag) | |
157 | channel_permit_all_opens(); | |
158 | ||
159 | s = session_new(); | |
160 | ||
161 | /* | |
162 | * We stay in this loop until the client requests to execute a shell | |
163 | * or a command. | |
164 | */ | |
165 | for (;;) { | |
166 | int success = 0; | |
167 | ||
168 | /* Get a packet from the client. */ | |
169 | type = packet_read(&plen); | |
170 | ||
171 | /* Process the packet. */ | |
172 | switch (type) { | |
173 | case SSH_CMSG_REQUEST_COMPRESSION: | |
174 | packet_integrity_check(plen, 4, type); | |
175 | compression_level = packet_get_int(); | |
176 | if (compression_level < 1 || compression_level > 9) { | |
177 | packet_send_debug("Received illegal compression level %d.", | |
178 | compression_level); | |
179 | break; | |
180 | } | |
181 | /* Enable compression after we have responded with SUCCESS. */ | |
182 | enable_compression_after_reply = 1; | |
183 | success = 1; | |
184 | break; | |
185 | ||
186 | case SSH_CMSG_REQUEST_PTY: | |
187 | if (no_pty_flag) { | |
188 | debug("Allocating a pty not permitted for this authentication."); | |
189 | break; | |
190 | } | |
191 | if (have_pty) | |
192 | packet_disconnect("Protocol error: you already have a pty."); | |
193 | ||
194 | debug("Allocating pty."); | |
195 | ||
196 | /* Allocate a pty and open it. */ | |
197 | if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, | |
198 | sizeof(s->tty))) { | |
199 | error("Failed to allocate pty."); | |
200 | break; | |
201 | } | |
202 | fatal_add_cleanup(pty_cleanup_proc, (void *)s); | |
203 | pty_setowner(pw, s->tty); | |
204 | ||
205 | /* Get TERM from the packet. Note that the value may be of arbitrary length. */ | |
206 | s->term = packet_get_string(&dlen); | |
207 | packet_integrity_check(dlen, strlen(s->term), type); | |
208 | /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */ | |
209 | /* Remaining bytes */ | |
210 | n_bytes = plen - (4 + dlen + 4 * 4); | |
211 | ||
212 | if (strcmp(s->term, "") == 0) { | |
213 | xfree(s->term); | |
214 | s->term = NULL; | |
215 | } | |
216 | /* Get window size from the packet. */ | |
217 | s->row = packet_get_int(); | |
218 | s->col = packet_get_int(); | |
219 | s->xpixel = packet_get_int(); | |
220 | s->ypixel = packet_get_int(); | |
221 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); | |
222 | ||
223 | /* Get tty modes from the packet. */ | |
224 | tty_parse_modes(s->ttyfd, &n_bytes); | |
225 | packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type); | |
226 | ||
227 | /* Indicate that we now have a pty. */ | |
228 | success = 1; | |
229 | have_pty = 1; | |
230 | break; | |
231 | ||
232 | case SSH_CMSG_X11_REQUEST_FORWARDING: | |
233 | if (!options.x11_forwarding) { | |
234 | packet_send_debug("X11 forwarding disabled in server configuration file."); | |
235 | break; | |
236 | } | |
237 | #ifdef XAUTH_PATH | |
238 | if (no_x11_forwarding_flag) { | |
239 | packet_send_debug("X11 forwarding not permitted for this authentication."); | |
240 | break; | |
241 | } | |
242 | debug("Received request for X11 forwarding with auth spoofing."); | |
243 | if (s->display != NULL) | |
244 | packet_disconnect("Protocol error: X11 display already set."); | |
245 | ||
246 | s->auth_proto = packet_get_string(&proto_len); | |
247 | s->auth_data = packet_get_string(&data_len); | |
248 | packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type); | |
249 | ||
250 | if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER) | |
251 | s->screen = packet_get_int(); | |
252 | else | |
253 | s->screen = 0; | |
254 | s->display = x11_create_display_inet(s->screen, options.x11_display_offset); | |
255 | ||
256 | if (s->display == NULL) | |
257 | break; | |
258 | ||
259 | /* Setup to always have a local .Xauthority. */ | |
260 | xauthfile = xmalloc(MAXPATHLEN); | |
261 | strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN); | |
262 | temporarily_use_uid(pw->pw_uid); | |
263 | if (mkdtemp(xauthfile) == NULL) { | |
264 | restore_uid(); | |
265 | error("private X11 dir: mkdtemp %s failed: %s", | |
266 | xauthfile, strerror(errno)); | |
267 | xfree(xauthfile); | |
268 | xauthfile = NULL; | |
269 | break; | |
270 | } | |
271 | strlcat(xauthfile, "/cookies", MAXPATHLEN); | |
272 | open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600); | |
273 | restore_uid(); | |
274 | fatal_add_cleanup(xauthfile_cleanup_proc, NULL); | |
275 | success = 1; | |
276 | break; | |
277 | #else /* XAUTH_PATH */ | |
278 | packet_send_debug("No xauth program; cannot forward with spoofing."); | |
279 | break; | |
280 | #endif /* XAUTH_PATH */ | |
281 | ||
282 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: | |
283 | if (no_agent_forwarding_flag || compat13) { | |
284 | debug("Authentication agent forwarding not permitted for this authentication."); | |
285 | break; | |
286 | } | |
287 | debug("Received authentication agent forwarding request."); | |
288 | auth_input_request_forwarding(pw); | |
289 | success = 1; | |
290 | break; | |
291 | ||
292 | case SSH_CMSG_PORT_FORWARD_REQUEST: | |
293 | if (no_port_forwarding_flag) { | |
294 | debug("Port forwarding not permitted for this authentication."); | |
295 | break; | |
296 | } | |
297 | debug("Received TCP/IP port forwarding request."); | |
298 | channel_input_port_forward_request(pw->pw_uid == 0); | |
299 | success = 1; | |
300 | break; | |
301 | ||
302 | case SSH_CMSG_MAX_PACKET_SIZE: | |
303 | if (packet_set_maxsize(packet_get_int()) > 0) | |
304 | success = 1; | |
305 | break; | |
306 | ||
307 | case SSH_CMSG_EXEC_SHELL: | |
308 | case SSH_CMSG_EXEC_CMD: | |
309 | /* Set interactive/non-interactive mode. */ | |
310 | packet_set_interactive(have_pty || s->display != NULL, | |
311 | options.keepalives); | |
312 | ||
313 | if (type == SSH_CMSG_EXEC_CMD) { | |
314 | command = packet_get_string(&dlen); | |
315 | debug("Exec command '%.500s'", command); | |
316 | packet_integrity_check(plen, 4 + dlen, type); | |
317 | } else { | |
318 | command = NULL; | |
319 | packet_integrity_check(plen, 0, type); | |
320 | } | |
321 | if (forced_command != NULL) { | |
322 | command = forced_command; | |
323 | debug("Forced command '%.500s'", forced_command); | |
324 | } | |
325 | if (have_pty) | |
326 | do_exec_pty(s, command, pw); | |
327 | else | |
328 | do_exec_no_pty(s, command, pw); | |
329 | ||
330 | if (command != NULL) | |
331 | xfree(command); | |
332 | /* Cleanup user's local Xauthority file. */ | |
333 | if (xauthfile) | |
334 | xauthfile_cleanup_proc(NULL); | |
335 | return; | |
336 | ||
337 | default: | |
338 | /* | |
339 | * Any unknown messages in this phase are ignored, | |
340 | * and a failure message is returned. | |
341 | */ | |
342 | log("Unknown packet type received after authentication: %d", type); | |
343 | } | |
344 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); | |
345 | packet_send(); | |
346 | packet_write_wait(); | |
347 | ||
348 | /* Enable compression now that we have replied if appropriate. */ | |
349 | if (enable_compression_after_reply) { | |
350 | enable_compression_after_reply = 0; | |
351 | packet_start_compression(compression_level); | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
356 | /* | |
357 | * This is called to fork and execute a command when we have no tty. This | |
358 | * will call do_child from the child, and server_loop from the parent after | |
359 | * setting up file descriptors and such. | |
360 | */ | |
361 | void | |
362 | do_exec_no_pty(Session *s, const char *command, struct passwd * pw) | |
363 | { | |
364 | int pid; | |
365 | ||
366 | #ifdef USE_PIPES | |
367 | int pin[2], pout[2], perr[2]; | |
368 | /* Allocate pipes for communicating with the program. */ | |
369 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) | |
370 | packet_disconnect("Could not create pipes: %.100s", | |
371 | strerror(errno)); | |
372 | #else /* USE_PIPES */ | |
373 | int inout[2], err[2]; | |
374 | /* Uses socket pairs to communicate with the program. */ | |
375 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || | |
376 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) | |
377 | packet_disconnect("Could not create socket pairs: %.100s", | |
378 | strerror(errno)); | |
379 | #endif /* USE_PIPES */ | |
380 | if (s == NULL) | |
381 | fatal("do_exec_no_pty: no session"); | |
382 | ||
383 | setproctitle("%s@notty", pw->pw_name); | |
384 | ||
385 | #ifdef USE_PAM | |
386 | do_pam_setcred(); | |
387 | #endif /* USE_PAM */ | |
388 | ||
389 | /* Fork the child. */ | |
390 | if ((pid = fork()) == 0) { | |
391 | /* Child. Reinitialize the log since the pid has changed. */ | |
392 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
393 | ||
394 | /* | |
395 | * Create a new session and process group since the 4.4BSD | |
396 | * setlogin() affects the entire process group. | |
397 | */ | |
398 | if (setsid() < 0) | |
399 | error("setsid failed: %.100s", strerror(errno)); | |
400 | ||
401 | #ifdef USE_PIPES | |
402 | /* | |
403 | * Redirect stdin. We close the parent side of the socket | |
404 | * pair, and make the child side the standard input. | |
405 | */ | |
406 | close(pin[1]); | |
407 | if (dup2(pin[0], 0) < 0) | |
408 | perror("dup2 stdin"); | |
409 | close(pin[0]); | |
410 | ||
411 | /* Redirect stdout. */ | |
412 | close(pout[0]); | |
413 | if (dup2(pout[1], 1) < 0) | |
414 | perror("dup2 stdout"); | |
415 | close(pout[1]); | |
416 | ||
417 | /* Redirect stderr. */ | |
418 | close(perr[0]); | |
419 | if (dup2(perr[1], 2) < 0) | |
420 | perror("dup2 stderr"); | |
421 | close(perr[1]); | |
422 | #else /* USE_PIPES */ | |
423 | /* | |
424 | * Redirect stdin, stdout, and stderr. Stdin and stdout will | |
425 | * use the same socket, as some programs (particularly rdist) | |
426 | * seem to depend on it. | |
427 | */ | |
428 | close(inout[1]); | |
429 | close(err[1]); | |
430 | if (dup2(inout[0], 0) < 0) /* stdin */ | |
431 | perror("dup2 stdin"); | |
432 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ | |
433 | perror("dup2 stdout"); | |
434 | if (dup2(err[0], 2) < 0) /* stderr */ | |
435 | perror("dup2 stderr"); | |
436 | #endif /* USE_PIPES */ | |
437 | ||
438 | /* Do processing for the child (exec command etc). */ | |
439 | do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL); | |
440 | /* NOTREACHED */ | |
441 | } | |
442 | if (pid < 0) | |
443 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
444 | s->pid = pid; | |
445 | #ifdef USE_PIPES | |
446 | /* We are the parent. Close the child sides of the pipes. */ | |
447 | close(pin[0]); | |
448 | close(pout[1]); | |
449 | close(perr[1]); | |
450 | ||
451 | /* Enter the interactive session. */ | |
452 | server_loop(pid, pin[1], pout[0], perr[0]); | |
453 | /* server_loop has closed pin[1], pout[1], and perr[1]. */ | |
454 | #else /* USE_PIPES */ | |
455 | /* We are the parent. Close the child sides of the socket pairs. */ | |
456 | close(inout[0]); | |
457 | close(err[0]); | |
458 | ||
459 | /* | |
460 | * Enter the interactive session. Note: server_loop must be able to | |
461 | * handle the case that fdin and fdout are the same. | |
462 | */ | |
463 | server_loop(pid, inout[1], inout[1], err[1]); | |
464 | /* server_loop has closed inout[1] and err[1]. */ | |
465 | #endif /* USE_PIPES */ | |
466 | } | |
467 | ||
468 | /* | |
469 | * This is called to fork and execute a command when we have a tty. This | |
470 | * will call do_child from the child, and server_loop from the parent after | |
471 | * setting up file descriptors, controlling tty, updating wtmp, utmp, | |
472 | * lastlog, and other such operations. | |
473 | */ | |
474 | void | |
475 | do_exec_pty(Session *s, const char *command, struct passwd * pw) | |
476 | { | |
477 | FILE *f; | |
478 | char buf[100], *time_string; | |
479 | char line[256]; | |
480 | const char *hostname; | |
481 | int fdout, ptyfd, ttyfd, ptymaster; | |
482 | int quiet_login; | |
483 | pid_t pid; | |
484 | socklen_t fromlen; | |
485 | struct sockaddr_storage from; | |
486 | struct stat st; | |
487 | time_t last_login_time; | |
488 | ||
489 | if (s == NULL) | |
490 | fatal("do_exec_pty: no session"); | |
491 | ptyfd = s->ptyfd; | |
492 | ttyfd = s->ttyfd; | |
493 | ||
494 | /* Get remote host name. */ | |
495 | hostname = get_canonical_hostname(); | |
496 | ||
497 | /* | |
498 | * Get the time when the user last logged in. Buf will be set to | |
499 | * contain the hostname the last login was from. | |
500 | */ | |
501 | if (!options.use_login) { | |
502 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, | |
503 | buf, sizeof(buf)); | |
504 | } | |
505 | setproctitle("%s@%s", pw->pw_name, strrchr(s->tty, '/') + 1); | |
506 | ||
507 | #ifdef USE_PAM | |
508 | do_pam_session(pw->pw_name, s->tty); | |
509 | do_pam_setcred(); | |
510 | #endif /* USE_PAM */ | |
511 | ||
512 | /* Fork the child. */ | |
513 | if ((pid = fork()) == 0) { | |
514 | pid = getpid(); | |
515 | ||
516 | /* Child. Reinitialize the log because the pid has | |
517 | changed. */ | |
518 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
519 | ||
520 | /* Close the master side of the pseudo tty. */ | |
521 | close(ptyfd); | |
522 | ||
523 | /* Make the pseudo tty our controlling tty. */ | |
524 | pty_make_controlling_tty(&ttyfd, s->tty); | |
525 | ||
526 | /* Redirect stdin from the pseudo tty. */ | |
527 | if (dup2(ttyfd, fileno(stdin)) < 0) | |
528 | error("dup2 stdin failed: %.100s", strerror(errno)); | |
529 | ||
530 | /* Redirect stdout to the pseudo tty. */ | |
531 | if (dup2(ttyfd, fileno(stdout)) < 0) | |
532 | error("dup2 stdin failed: %.100s", strerror(errno)); | |
533 | ||
534 | /* Redirect stderr to the pseudo tty. */ | |
535 | if (dup2(ttyfd, fileno(stderr)) < 0) | |
536 | error("dup2 stdin failed: %.100s", strerror(errno)); | |
537 | ||
538 | /* Close the extra descriptor for the pseudo tty. */ | |
539 | close(ttyfd); | |
540 | ||
541 | ///XXXX ? move to do_child() ?? | |
542 | /* | |
543 | * Get IP address of client. This is needed because we want | |
544 | * to record where the user logged in from. If the | |
545 | * connection is not a socket, let the ip address be 0.0.0.0. | |
546 | */ | |
547 | memset(&from, 0, sizeof(from)); | |
548 | if (packet_connection_is_on_socket()) { | |
549 | fromlen = sizeof(from); | |
550 | if (getpeername(packet_get_connection_in(), | |
551 | (struct sockaddr *) & from, &fromlen) < 0) { | |
552 | debug("getpeername: %.100s", strerror(errno)); | |
553 | fatal_cleanup(); | |
554 | } | |
555 | } | |
556 | /* Record that there was a login on that terminal. */ | |
557 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, hostname, | |
558 | (struct sockaddr *)&from); | |
559 | ||
560 | /* Check if .hushlogin exists. */ | |
561 | snprintf(line, sizeof line, "%.200s/.hushlogin", pw->pw_dir); | |
562 | quiet_login = stat(line, &st) >= 0; | |
563 | ||
564 | #ifdef USE_PAM | |
565 | if (!quiet_login) | |
566 | print_pam_messages(); | |
567 | #endif /* USE_PAM */ | |
568 | ||
569 | /* | |
570 | * If the user has logged in before, display the time of last | |
571 | * login. However, don't display anything extra if a command | |
572 | * has been specified (so that ssh can be used to execute | |
573 | * commands on a remote machine without users knowing they | |
574 | * are going to another machine). Login(1) will do this for | |
575 | * us as well, so check if login(1) is used | |
576 | */ | |
577 | if (command == NULL && last_login_time != 0 && !quiet_login && | |
578 | !options.use_login) { | |
579 | /* Convert the date to a string. */ | |
580 | time_string = ctime(&last_login_time); | |
581 | /* Remove the trailing newline. */ | |
582 | if (strchr(time_string, '\n')) | |
583 | *strchr(time_string, '\n') = 0; | |
584 | /* Display the last login time. Host if displayed | |
585 | if known. */ | |
586 | if (strcmp(buf, "") == 0) | |
587 | printf("Last login: %s\r\n", time_string); | |
588 | else | |
589 | printf("Last login: %s from %s\r\n", time_string, buf); | |
590 | } | |
591 | /* | |
592 | * Print /etc/motd unless a command was specified or printing | |
593 | * it was disabled in server options or login(1) will be | |
594 | * used. Note that some machines appear to print it in | |
595 | * /etc/profile or similar. | |
596 | */ | |
597 | if (command == NULL && options.print_motd && !quiet_login && | |
598 | !options.use_login) { | |
599 | /* Print /etc/motd if it exists. */ | |
600 | f = fopen("/etc/motd", "r"); | |
601 | if (f) { | |
602 | while (fgets(line, sizeof(line), f)) | |
603 | fputs(line, stdout); | |
604 | fclose(f); | |
605 | } | |
606 | } | |
607 | /* Do common processing for the child, such as execing the command. */ | |
608 | do_child(command, pw, s->term, s->display, s->auth_proto, s->auth_data, s->tty); | |
609 | /* NOTREACHED */ | |
610 | } | |
611 | if (pid < 0) | |
612 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
613 | s->pid = pid; | |
614 | ||
615 | /* Parent. Close the slave side of the pseudo tty. */ | |
616 | close(ttyfd); | |
617 | ||
618 | /* | |
619 | * Create another descriptor of the pty master side for use as the | |
620 | * standard input. We could use the original descriptor, but this | |
621 | * simplifies code in server_loop. The descriptor is bidirectional. | |
622 | */ | |
623 | fdout = dup(ptyfd); | |
624 | if (fdout < 0) | |
625 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); | |
626 | ||
627 | /* we keep a reference to the pty master */ | |
628 | ptymaster = dup(ptyfd); | |
629 | if (ptymaster < 0) | |
630 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); | |
631 | s->ptymaster = ptymaster; | |
632 | ||
633 | /* Enter interactive session. */ | |
634 | server_loop(pid, ptyfd, fdout, -1); | |
635 | /* server_loop _has_ closed ptyfd and fdout. */ | |
636 | session_pty_cleanup(s); | |
637 | } | |
638 | ||
639 | /* | |
640 | * Sets the value of the given variable in the environment. If the variable | |
641 | * already exists, its value is overriden. | |
642 | */ | |
643 | void | |
644 | child_set_env(char ***envp, unsigned int *envsizep, const char *name, | |
645 | const char *value) | |
646 | { | |
647 | unsigned int i, namelen; | |
648 | char **env; | |
649 | ||
650 | /* | |
651 | * Find the slot where the value should be stored. If the variable | |
652 | * already exists, we reuse the slot; otherwise we append a new slot | |
653 | * at the end of the array, expanding if necessary. | |
654 | */ | |
655 | env = *envp; | |
656 | namelen = strlen(name); | |
657 | for (i = 0; env[i]; i++) | |
658 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') | |
659 | break; | |
660 | if (env[i]) { | |
661 | /* Reuse the slot. */ | |
662 | xfree(env[i]); | |
663 | } else { | |
664 | /* New variable. Expand if necessary. */ | |
665 | if (i >= (*envsizep) - 1) { | |
666 | (*envsizep) += 50; | |
667 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); | |
668 | } | |
669 | /* Need to set the NULL pointer at end of array beyond the new slot. */ | |
670 | env[i + 1] = NULL; | |
671 | } | |
672 | ||
673 | /* Allocate space and format the variable in the appropriate slot. */ | |
674 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); | |
675 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); | |
676 | } | |
677 | ||
678 | /* | |
679 | * Reads environment variables from the given file and adds/overrides them | |
680 | * into the environment. If the file does not exist, this does nothing. | |
681 | * Otherwise, it must consist of empty lines, comments (line starts with '#') | |
682 | * and assignments of the form name=value. No other forms are allowed. | |
683 | */ | |
684 | void | |
685 | read_environment_file(char ***env, unsigned int *envsize, | |
686 | const char *filename) | |
687 | { | |
688 | FILE *f; | |
689 | char buf[4096]; | |
690 | char *cp, *value; | |
691 | ||
692 | f = fopen(filename, "r"); | |
693 | if (!f) | |
694 | return; | |
695 | ||
696 | while (fgets(buf, sizeof(buf), f)) { | |
697 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) | |
698 | ; | |
699 | if (!*cp || *cp == '#' || *cp == '\n') | |
700 | continue; | |
701 | if (strchr(cp, '\n')) | |
702 | *strchr(cp, '\n') = '\0'; | |
703 | value = strchr(cp, '='); | |
704 | if (value == NULL) { | |
705 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); | |
706 | continue; | |
707 | } | |
708 | /* Replace the equals sign by nul, and advance value to the value string. */ | |
709 | *value = '\0'; | |
710 | value++; | |
711 | child_set_env(env, envsize, cp, value); | |
712 | } | |
713 | fclose(f); | |
714 | } | |
715 | ||
716 | #ifdef USE_PAM | |
717 | /* | |
718 | * Sets any environment variables which have been specified by PAM | |
719 | */ | |
720 | void do_pam_environment(char ***env, int *envsize) | |
721 | { | |
722 | char *equals, var_name[512], var_val[512]; | |
723 | char **pam_env; | |
724 | int i; | |
725 | ||
726 | if ((pam_env = fetch_pam_environment()) == NULL) | |
727 | return; | |
728 | ||
729 | for(i = 0; pam_env[i] != NULL; i++) { | |
730 | if ((equals = strstr(pam_env[i], "=")) == NULL) | |
731 | continue; | |
732 | ||
733 | if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) { | |
734 | memset(var_name, '\0', sizeof(var_name)); | |
735 | memset(var_val, '\0', sizeof(var_val)); | |
736 | ||
737 | strncpy(var_name, pam_env[i], equals - pam_env[i]); | |
738 | strcpy(var_val, equals + 1); | |
739 | ||
740 | debug("PAM environment: %s=%s", var_name, var_val); | |
741 | ||
742 | child_set_env(env, envsize, var_name, var_val); | |
743 | } | |
744 | } | |
745 | } | |
746 | #endif /* USE_PAM */ | |
747 | ||
748 | /* | |
749 | * Performs common processing for the child, such as setting up the | |
750 | * environment, closing extra file descriptors, setting the user and group | |
751 | * ids, and executing the command or shell. | |
752 | */ | |
753 | void | |
754 | do_child(const char *command, struct passwd * pw, const char *term, | |
755 | const char *display, const char *auth_proto, | |
756 | const char *auth_data, const char *ttyname) | |
757 | { | |
758 | const char *shell, *cp = NULL; | |
759 | char buf[256]; | |
760 | FILE *f; | |
761 | unsigned int envsize, i; | |
762 | char **env; | |
763 | extern char **environ; | |
764 | struct stat st; | |
765 | char *argv[10]; | |
766 | ||
767 | #ifndef USE_PAM /* pam_nologin handles this */ | |
768 | f = fopen("/etc/nologin", "r"); | |
769 | if (f) { | |
770 | /* /etc/nologin exists. Print its contents and exit. */ | |
771 | while (fgets(buf, sizeof(buf), f)) | |
772 | fputs(buf, stderr); | |
773 | fclose(f); | |
774 | if (pw->pw_uid != 0) | |
775 | exit(254); | |
776 | } | |
777 | #endif /* USE_PAM */ | |
778 | ||
779 | /* Set login name in the kernel. */ | |
780 | if (setlogin(pw->pw_name) < 0) | |
781 | error("setlogin failed: %s", strerror(errno)); | |
782 | ||
783 | /* Set uid, gid, and groups. */ | |
784 | /* Login(1) does this as well, and it needs uid 0 for the "-h" | |
785 | switch, so we let login(1) to this for us. */ | |
786 | if (!options.use_login) { | |
787 | if (getuid() == 0 || geteuid() == 0) { | |
788 | if (setgid(pw->pw_gid) < 0) { | |
789 | perror("setgid"); | |
790 | exit(1); | |
791 | } | |
792 | /* Initialize the group list. */ | |
793 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { | |
794 | perror("initgroups"); | |
795 | exit(1); | |
796 | } | |
797 | endgrent(); | |
798 | ||
799 | /* Permanently switch to the desired uid. */ | |
800 | permanently_set_uid(pw->pw_uid); | |
801 | } | |
802 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) | |
803 | fatal("Failed to set uids to %d.", (int) pw->pw_uid); | |
804 | } | |
805 | /* | |
806 | * Get the shell from the password data. An empty shell field is | |
807 | * legal, and means /bin/sh. | |
808 | */ | |
809 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; | |
810 | ||
811 | #ifdef AFS | |
812 | /* Try to get AFS tokens for the local cell. */ | |
813 | if (k_hasafs()) { | |
814 | char cell[64]; | |
815 | ||
816 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) | |
817 | krb_afslog(cell, 0); | |
818 | ||
819 | krb_afslog(0, 0); | |
820 | } | |
821 | #endif /* AFS */ | |
822 | ||
823 | /* Initialize the environment. */ | |
824 | envsize = 100; | |
825 | env = xmalloc(envsize * sizeof(char *)); | |
826 | env[0] = NULL; | |
827 | ||
828 | if (!options.use_login) { | |
829 | /* Set basic environment. */ | |
830 | child_set_env(&env, &envsize, "USER", pw->pw_name); | |
831 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); | |
832 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); | |
833 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); | |
834 | ||
835 | snprintf(buf, sizeof buf, "%.200s/%.50s", | |
836 | _PATH_MAILDIR, pw->pw_name); | |
837 | child_set_env(&env, &envsize, "MAIL", buf); | |
838 | ||
839 | /* Normal systems set SHELL by default. */ | |
840 | child_set_env(&env, &envsize, "SHELL", shell); | |
841 | } | |
842 | if (getenv("TZ")) | |
843 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); | |
844 | ||
845 | /* Set custom environment options from RSA authentication. */ | |
846 | while (custom_environment) { | |
847 | struct envstring *ce = custom_environment; | |
848 | char *s = ce->s; | |
849 | int i; | |
850 | for (i = 0; s[i] != '=' && s[i]; i++); | |
851 | if (s[i] == '=') { | |
852 | s[i] = 0; | |
853 | child_set_env(&env, &envsize, s, s + i + 1); | |
854 | } | |
855 | custom_environment = ce->next; | |
856 | xfree(ce->s); | |
857 | xfree(ce); | |
858 | } | |
859 | ||
860 | snprintf(buf, sizeof buf, "%.50s %d %d", | |
861 | get_remote_ipaddr(), get_remote_port(), get_local_port()); | |
862 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); | |
863 | ||
864 | if (ttyname) | |
865 | child_set_env(&env, &envsize, "SSH_TTY", ttyname); | |
866 | if (term) | |
867 | child_set_env(&env, &envsize, "TERM", term); | |
868 | if (display) | |
869 | child_set_env(&env, &envsize, "DISPLAY", display); | |
870 | ||
871 | #ifdef _AIX | |
872 | { | |
873 | char *authstate,*krb5cc; | |
874 | ||
875 | if ((authstate = getenv("AUTHSTATE")) != NULL) | |
876 | child_set_env(&env,&envsize,"AUTHSTATE",authstate); | |
877 | ||
878 | if ((krb5cc = getenv("KRB5CCNAME")) != NULL) | |
879 | child_set_env(&env,&envsize,"KRB5CCNAME",krb5cc); | |
880 | } | |
881 | #endif | |
882 | ||
883 | #ifdef KRB4 | |
884 | { | |
885 | extern char *ticket; | |
886 | ||
887 | if (ticket) | |
888 | child_set_env(&env, &envsize, "KRBTKFILE", ticket); | |
889 | } | |
890 | #endif /* KRB4 */ | |
891 | ||
892 | #ifdef USE_PAM | |
893 | /* Pull in any environment variables that may have been set by PAM. */ | |
894 | do_pam_environment(&env, &envsize); | |
895 | #endif /* USE_PAM */ | |
896 | ||
897 | read_environment_file(&env,&envsize,"/etc/environment"); | |
898 | ||
899 | if (xauthfile) | |
900 | child_set_env(&env, &envsize, "XAUTHORITY", xauthfile); | |
901 | if (auth_get_socket_name() != NULL) | |
902 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, | |
903 | auth_get_socket_name()); | |
904 | ||
905 | /* read $HOME/.ssh/environment. */ | |
906 | if (!options.use_login) { | |
907 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", pw->pw_dir); | |
908 | read_environment_file(&env, &envsize, buf); | |
909 | } | |
910 | if (debug_flag) { | |
911 | /* dump the environment */ | |
912 | fprintf(stderr, "Environment:\n"); | |
913 | for (i = 0; env[i]; i++) | |
914 | fprintf(stderr, " %.200s\n", env[i]); | |
915 | } | |
916 | /* | |
917 | * Close the connection descriptors; note that this is the child, and | |
918 | * the server will still have the socket open, and it is important | |
919 | * that we do not shutdown it. Note that the descriptors cannot be | |
920 | * closed before building the environment, as we call | |
921 | * get_remote_ipaddr there. | |
922 | */ | |
923 | if (packet_get_connection_in() == packet_get_connection_out()) | |
924 | close(packet_get_connection_in()); | |
925 | else { | |
926 | close(packet_get_connection_in()); | |
927 | close(packet_get_connection_out()); | |
928 | } | |
929 | /* | |
930 | * Close all descriptors related to channels. They will still remain | |
931 | * open in the parent. | |
932 | */ | |
933 | /* XXX better use close-on-exec? -markus */ | |
934 | channel_close_all(); | |
935 | ||
936 | /* | |
937 | * Close any extra file descriptors. Note that there may still be | |
938 | * descriptors left by system functions. They will be closed later. | |
939 | */ | |
940 | endpwent(); | |
941 | ||
942 | /* | |
943 | * Close any extra open file descriptors so that we don\'t have them | |
944 | * hanging around in clients. Note that we want to do this after | |
945 | * initgroups, because at least on Solaris 2.3 it leaves file | |
946 | * descriptors open. | |
947 | */ | |
948 | for (i = 3; i < 64; i++) | |
949 | close(i); | |
950 | ||
951 | /* Change current directory to the user\'s home directory. */ | |
952 | if (chdir(pw->pw_dir) < 0) | |
953 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", | |
954 | pw->pw_dir, strerror(errno)); | |
955 | ||
956 | /* | |
957 | * Must take new environment into use so that .ssh/rc, /etc/sshrc and | |
958 | * xauth are run in the proper environment. | |
959 | */ | |
960 | environ = env; | |
961 | ||
962 | /* | |
963 | * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first | |
964 | * in this order). | |
965 | */ | |
966 | if (!options.use_login) { | |
967 | if (stat(SSH_USER_RC, &st) >= 0) { | |
968 | if (debug_flag) | |
969 | fprintf(stderr, "Running /bin/sh %s\n", SSH_USER_RC); | |
970 | ||
971 | f = popen("/bin/sh " SSH_USER_RC, "w"); | |
972 | if (f) { | |
973 | if (auth_proto != NULL && auth_data != NULL) | |
974 | fprintf(f, "%s %s\n", auth_proto, auth_data); | |
975 | pclose(f); | |
976 | } else | |
977 | fprintf(stderr, "Could not run %s\n", SSH_USER_RC); | |
978 | } else if (stat(SSH_SYSTEM_RC, &st) >= 0) { | |
979 | if (debug_flag) | |
980 | fprintf(stderr, "Running /bin/sh %s\n", SSH_SYSTEM_RC); | |
981 | ||
982 | f = popen("/bin/sh " SSH_SYSTEM_RC, "w"); | |
983 | if (f) { | |
984 | if (auth_proto != NULL && auth_data != NULL) | |
985 | fprintf(f, "%s %s\n", auth_proto, auth_data); | |
986 | pclose(f); | |
987 | } else | |
988 | fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC); | |
989 | } | |
990 | #ifdef XAUTH_PATH | |
991 | else { | |
992 | /* Add authority data to .Xauthority if appropriate. */ | |
993 | if (auth_proto != NULL && auth_data != NULL) { | |
994 | if (debug_flag) | |
995 | fprintf(stderr, "Running %.100s add %.100s %.100s %.100s\n", | |
996 | XAUTH_PATH, display, auth_proto, auth_data); | |
997 | ||
998 | f = popen(XAUTH_PATH " -q -", "w"); | |
999 | if (f) { | |
1000 | fprintf(f, "add %s %s %s\n", display, auth_proto, auth_data); | |
1001 | pclose(f); | |
1002 | } else | |
1003 | fprintf(stderr, "Could not run %s -q -\n", XAUTH_PATH); | |
1004 | } | |
1005 | } | |
1006 | #endif /* XAUTH_PATH */ | |
1007 | ||
1008 | /* Get the last component of the shell name. */ | |
1009 | cp = strrchr(shell, '/'); | |
1010 | if (cp) | |
1011 | cp++; | |
1012 | else | |
1013 | cp = shell; | |
1014 | } | |
1015 | /* | |
1016 | * If we have no command, execute the shell. In this case, the shell | |
1017 | * name to be passed in argv[0] is preceded by '-' to indicate that | |
1018 | * this is a login shell. | |
1019 | */ | |
1020 | if (!command) { | |
1021 | if (!options.use_login) { | |
1022 | char buf[256]; | |
1023 | ||
1024 | /* | |
1025 | * Check for mail if we have a tty and it was enabled | |
1026 | * in server options. | |
1027 | */ | |
1028 | if (ttyname && options.check_mail) { | |
1029 | char *mailbox; | |
1030 | struct stat mailstat; | |
1031 | mailbox = getenv("MAIL"); | |
1032 | if (mailbox != NULL) { | |
1033 | if (stat(mailbox, &mailstat) != 0 || mailstat.st_size == 0) | |
1034 | printf("No mail.\n"); | |
1035 | else if (mailstat.st_mtime < mailstat.st_atime) | |
1036 | printf("You have mail.\n"); | |
1037 | else | |
1038 | printf("You have new mail.\n"); | |
1039 | } | |
1040 | } | |
1041 | /* Start the shell. Set initial character to '-'. */ | |
1042 | buf[0] = '-'; | |
1043 | strncpy(buf + 1, cp, sizeof(buf) - 1); | |
1044 | buf[sizeof(buf) - 1] = 0; | |
1045 | ||
1046 | /* Execute the shell. */ | |
1047 | argv[0] = buf; | |
1048 | argv[1] = NULL; | |
1049 | execve(shell, argv, env); | |
1050 | ||
1051 | /* Executing the shell failed. */ | |
1052 | perror(shell); | |
1053 | exit(1); | |
1054 | ||
1055 | } else { | |
1056 | /* Launch login(1). */ | |
1057 | ||
1058 | execl("/usr/bin/login", "login", "-h", get_remote_ipaddr(), | |
1059 | "-p", "-f", "--", pw->pw_name, NULL); | |
1060 | ||
1061 | /* Login couldn't be executed, die. */ | |
1062 | ||
1063 | perror("login"); | |
1064 | exit(1); | |
1065 | } | |
1066 | } | |
1067 | /* | |
1068 | * Execute the command using the user's shell. This uses the -c | |
1069 | * option to execute the command. | |
1070 | */ | |
1071 | argv[0] = (char *) cp; | |
1072 | argv[1] = "-c"; | |
1073 | argv[2] = (char *) command; | |
1074 | argv[3] = NULL; | |
1075 | execve(shell, argv, env); | |
1076 | perror(shell); | |
1077 | exit(1); | |
1078 | } | |
1079 | ||
1080 | Session * | |
1081 | session_new(void) | |
1082 | { | |
1083 | int i; | |
1084 | static int did_init = 0; | |
1085 | if (!did_init) { | |
1086 | debug("session_new: init"); | |
1087 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1088 | sessions[i].used = 0; | |
1089 | sessions[i].self = i; | |
1090 | } | |
1091 | did_init = 1; | |
1092 | } | |
1093 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1094 | Session *s = &sessions[i]; | |
1095 | if (! s->used) { | |
1096 | s->pid = 0; | |
1097 | s->chanid = -1; | |
1098 | s->ptyfd = -1; | |
1099 | s->ttyfd = -1; | |
1100 | s->term = NULL; | |
1101 | s->pw = NULL; | |
1102 | s->display = NULL; | |
1103 | s->screen = 0; | |
1104 | s->auth_data = NULL; | |
1105 | s->auth_proto = NULL; | |
1106 | s->used = 1; | |
1107 | debug("session_new: session %d", i); | |
1108 | return s; | |
1109 | } | |
1110 | } | |
1111 | return NULL; | |
1112 | } | |
1113 | ||
1114 | void | |
1115 | session_dump(void) | |
1116 | { | |
1117 | int i; | |
1118 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1119 | Session *s = &sessions[i]; | |
1120 | debug("dump: used %d session %d %p channel %d pid %d", | |
1121 | s->used, | |
1122 | s->self, | |
1123 | s, | |
1124 | s->chanid, | |
1125 | s->pid); | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | void | |
1130 | session_pty_cleanup(Session *s) | |
1131 | { | |
1132 | if (s == NULL || s->ttyfd == -1) | |
1133 | return; | |
1134 | ||
1135 | debug("session_pty_cleanup: session %i release %s", s->self, s->tty); | |
1136 | ||
1137 | /* Cancel the cleanup function. */ | |
1138 | fatal_remove_cleanup(pty_cleanup_proc, (void *)s); | |
1139 | ||
1140 | /* Record that the user has logged out. */ | |
1141 | record_logout(s->pid, s->tty); | |
1142 | ||
1143 | /* Release the pseudo-tty. */ | |
1144 | pty_release(s->tty); | |
1145 | ||
1146 | /* | |
1147 | * Close the server side of the socket pairs. We must do this after | |
1148 | * the pty cleanup, so that another process doesn't get this pty | |
1149 | * while we're still cleaning up. | |
1150 | */ | |
1151 | if (close(s->ptymaster) < 0) | |
1152 | error("close(s->ptymaster): %s", strerror(errno)); | |
1153 | } |