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