Print this page
OS-2204 SunSSH has a maximum of 10 multiplexed sessions
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/ssh/sshd/session.c
+++ new/usr/src/cmd/ssh/sshd/session.c
1 1 /*
2 2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 3 * All rights reserved
4 4 *
5 5 * As far as I am concerned, the code I have written for this software
6 6 * can be used freely for any purpose. Any derived versions of this
7 7 * software must be clearly marked as such, and if the derived work is
8 8 * incompatible with the protocol description in the RFC file, it must be
9 9 * called by a name other than "ssh" or "Secure Shell".
10 10 *
11 11 * SSH2 support by Markus Friedl.
12 12 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
13 13 *
14 14 * Redistribution and use in source and binary forms, with or without
15 15 * modification, are permitted provided that the following conditions
16 16 * are met:
17 17 * 1. Redistributions of source code must retain the above copyright
18 18 * notice, this list of conditions and the following disclaimer.
19 19 * 2. Redistributions in binary form must reproduce the above copyright
20 20 * notice, this list of conditions and the following disclaimer in the
21 21 * documentation and/or other materials provided with the distribution.
22 22 *
23 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
28 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 33 */
34 34 /*
35 35 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
36 36 * Use is subject to license terms.
37 37 */
38 +/*
39 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
40 + */
38 41
39 42 #include "includes.h"
40 43 RCSID("$OpenBSD: session.c,v 1.150 2002/09/16 19:55:33 stevesk Exp $");
41 44
42 45 #ifdef HAVE_DEFOPEN
43 46 #include <deflt.h>
44 47 #include <ulimit.h>
45 48 #endif /* HAVE_DEFOPEN */
46 49
47 50 #ifdef HAVE_LIBGEN_H
48 51 #include <libgen.h>
49 52 #endif
50 53
51 54 #include <priv.h>
52 55
53 56 #include "ssh.h"
54 57 #include "ssh1.h"
55 58 #include "ssh2.h"
56 59 #include "xmalloc.h"
60 +#include "sys-queue.h"
57 61 #include "sshpty.h"
58 62 #include "packet.h"
59 63 #include "buffer.h"
60 64 #include "mpaux.h"
61 65 #include "uidswap.h"
62 66 #include "compat.h"
63 67 #include "channels.h"
64 68 #include "bufaux.h"
65 69 #include "auth.h"
66 70 #include "auth-options.h"
67 71 #include "pathnames.h"
68 72 #include "log.h"
69 73 #include "servconf.h"
70 74 #include "sshlogin.h"
71 75 #include "serverloop.h"
72 76 #include "canohost.h"
73 77 #include "session.h"
74 78 #include "tildexpand.h"
75 79 #include "misc.h"
76 80 #include "sftp.h"
77 81
78 82 #ifdef USE_PAM
79 83 #include <security/pam_appl.h>
80 84 #endif /* USE_PAM */
81 85
82 86 #ifdef GSSAPI
83 87 #include "ssh-gss.h"
84 88 #endif
85 89
86 90 #ifdef ALTPRIVSEP
87 91 #include "altprivsep.h"
88 92 #endif /* ALTPRIVSEP */
89 93
90 94 #ifdef HAVE_CYGWIN
91 95 #include <windows.h>
92 96 #include <sys/cygwin.h>
93 97 #define is_winnt (GetVersion() < 0x80000000)
94 98 #endif
95 99
96 100 /* func */
97 101
98 102 Session *session_new(void);
99 103 void session_set_fds(Session *, int, int, int);
100 104 void session_pty_cleanup(void *);
101 105 void session_xauthfile_cleanup(void *s);
102 106 void session_proctitle(Session *);
103 107 int session_setup_x11fwd(Session *);
104 108 void do_exec_pty(Session *, const char *);
105 109 void do_exec_no_pty(Session *, const char *);
106 110 void do_exec(Session *, const char *);
107 111 void do_login(Session *, const char *);
108 112 void do_child(Session *, const char *);
109 113 void do_motd(void);
110 114 int check_quietlogin(Session *, const char *);
111 115
112 116 static void do_authenticated1(Authctxt *);
113 117 static void do_authenticated2(Authctxt *);
114 118
115 119 static int session_pty_req(Session *);
116 120 static int session_env_req(Session *s);
117 121 static void session_free_env(char ***envp);
118 122 static void safely_chroot(const char *path, uid_t uid);
119 123 static void drop_privs(uid_t uid);
120 124
121 125 #ifdef USE_PAM
122 126 static void session_do_pam(Session *, int);
123 127 #endif /* USE_PAM */
124 128
125 129 /* import */
126 130 extern ServerOptions options;
127 131 extern char *__progname;
128 132 extern int log_stderr;
129 133 extern int debug_flag;
130 134 extern u_int utmp_len;
131 135 extern void destroy_sensitive_data(void);
↓ open down ↓ |
65 lines elided |
↑ open up ↑ |
132 136
133 137 #ifdef GSSAPI
134 138 extern Gssctxt *xxx_gssctxt;
135 139 #endif /* GSSAPI */
136 140
137 141 /* original command from peer. */
138 142 const char *original_command = NULL;
139 143
140 144 /* data */
141 145 #define MAX_SESSIONS 10
142 -Session sessions[MAX_SESSIONS];
146 +static int sessions_next_id = 0;
147 +static LIST_HEAD(sessions_head, Session) sessions =
148 + LIST_HEAD_INITIALIZER(sessions);
143 149
144 150 #define SUBSYSTEM_NONE 0
145 151 #define SUBSYSTEM_EXT 1
146 152 #define SUBSYSTEM_INT_SFTP 2
147 153
148 154 #ifdef HAVE_LOGIN_CAP
149 155 login_cap_t *lc;
150 156 #endif
151 157
152 158 /* Name and directory of socket for authentication agent forwarding. */
153 159 static char *auth_sock_name = NULL;
154 160 static char *auth_sock_dir = NULL;
155 161
156 162 /* removes the agent forwarding socket */
157 163
158 164 static void
159 165 auth_sock_cleanup_proc(void *_pw)
160 166 {
161 167 struct passwd *pw = _pw;
162 168
163 169 if (auth_sock_name != NULL) {
164 170 temporarily_use_uid(pw);
165 171 unlink(auth_sock_name);
166 172 rmdir(auth_sock_dir);
167 173 auth_sock_name = NULL;
168 174 restore_uid();
169 175 }
170 176 }
171 177
172 178 static int
173 179 auth_input_request_forwarding(struct passwd * pw)
174 180 {
175 181 Channel *nc;
176 182 int sock;
177 183 struct sockaddr_un sunaddr;
178 184
179 185 if (auth_sock_name != NULL) {
180 186 error("authentication forwarding requested twice.");
181 187 return 0;
182 188 }
183 189
184 190 /* Temporarily drop privileged uid for mkdir/bind. */
185 191 temporarily_use_uid(pw);
186 192
187 193 /* Allocate a buffer for the socket name, and format the name. */
188 194 auth_sock_name = xmalloc(MAXPATHLEN);
189 195 auth_sock_dir = xmalloc(MAXPATHLEN);
190 196 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
191 197
192 198 /* Create private directory for socket */
193 199 if (mkdtemp(auth_sock_dir) == NULL) {
194 200 packet_send_debug("Agent forwarding disabled: "
195 201 "mkdtemp() failed: %.100s", strerror(errno));
196 202 restore_uid();
197 203 xfree(auth_sock_name);
198 204 xfree(auth_sock_dir);
199 205 auth_sock_name = NULL;
200 206 auth_sock_dir = NULL;
201 207 return 0;
202 208 }
203 209 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%ld",
204 210 auth_sock_dir, (long) getpid());
205 211
206 212 /* delete agent socket on fatal() */
207 213 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
208 214
209 215 /* Create the socket. */
210 216 sock = socket(AF_UNIX, SOCK_STREAM, 0);
211 217 if (sock < 0)
212 218 packet_disconnect("socket: %.100s", strerror(errno));
213 219
214 220 /* Bind it to the name. */
215 221 memset(&sunaddr, 0, sizeof(sunaddr));
216 222 sunaddr.sun_family = AF_UNIX;
217 223 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
218 224
219 225 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
220 226 packet_disconnect("bind: %.100s", strerror(errno));
221 227
222 228 /* Restore the privileged uid. */
223 229 restore_uid();
224 230
225 231 /* Start listening on the socket. */
226 232 if (listen(sock, 5) < 0)
227 233 packet_disconnect("listen: %.100s", strerror(errno));
228 234
229 235 /* Allocate a channel for the authentication agent socket. */
230 236 nc = channel_new("auth socket",
231 237 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
232 238 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
233 239 0, xstrdup("auth socket"), 1);
234 240 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
235 241 return 1;
236 242 }
237 243
238 244
239 245 void
240 246 do_authenticated(Authctxt *authctxt)
241 247 {
242 248 /* setup the channel layer */
243 249 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
244 250 channel_permit_all_opens();
245 251
246 252 if (compat20)
247 253 do_authenticated2(authctxt);
248 254 else
249 255 do_authenticated1(authctxt);
250 256
251 257 /* remove agent socket */
252 258 if (auth_sock_name != NULL)
253 259 auth_sock_cleanup_proc(authctxt->pw);
254 260 #ifdef KRB4
255 261 if (options.kerberos_ticket_cleanup)
256 262 krb4_cleanup_proc(authctxt);
257 263 #endif
258 264 #ifdef KRB5
259 265 if (options.kerberos_ticket_cleanup)
260 266 krb5_cleanup_proc(authctxt);
261 267 #endif
262 268 }
263 269
264 270 /*
265 271 * Prepares for an interactive session. This is called after the user has
266 272 * been successfully authenticated. During this message exchange, pseudo
267 273 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
268 274 * are requested, etc.
269 275 */
270 276 static void
271 277 do_authenticated1(Authctxt *authctxt)
272 278 {
273 279 Session *s;
274 280 char *command;
275 281 int success, type, screen_flag;
276 282 int enable_compression_after_reply = 0;
277 283 u_int proto_len, data_len, dlen, compression_level = 0;
278 284
279 285 s = session_new();
280 286 s->authctxt = authctxt;
281 287 s->pw = authctxt->pw;
282 288
283 289 /*
284 290 * We stay in this loop until the client requests to execute a shell
285 291 * or a command.
286 292 */
287 293 for (;;) {
288 294 success = 0;
289 295
290 296 /* Get a packet from the client. */
291 297 type = packet_read();
292 298
293 299 /* Process the packet. */
294 300 switch (type) {
295 301 case SSH_CMSG_REQUEST_COMPRESSION:
296 302 compression_level = packet_get_int();
297 303 packet_check_eom();
298 304 if (compression_level < 1 || compression_level > 9) {
299 305 packet_send_debug("Received illegal compression level %d.",
300 306 compression_level);
301 307 break;
302 308 }
303 309 if (!options.compression) {
304 310 debug2("compression disabled");
305 311 break;
306 312 }
307 313 /* Enable compression after we have responded with SUCCESS. */
308 314 enable_compression_after_reply = 1;
309 315 success = 1;
310 316 break;
311 317
312 318 case SSH_CMSG_REQUEST_PTY:
313 319 success = session_pty_req(s);
314 320 break;
315 321
316 322 case SSH_CMSG_X11_REQUEST_FORWARDING:
317 323 s->auth_proto = packet_get_string(&proto_len);
318 324 s->auth_data = packet_get_string(&data_len);
319 325
320 326 screen_flag = packet_get_protocol_flags() &
321 327 SSH_PROTOFLAG_SCREEN_NUMBER;
322 328 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
323 329
324 330 if (packet_remaining() == 4) {
325 331 if (!screen_flag)
326 332 debug2("Buggy client: "
327 333 "X11 screen flag missing");
328 334 s->screen = packet_get_int();
329 335 } else {
330 336 s->screen = 0;
331 337 }
332 338 packet_check_eom();
333 339 success = session_setup_x11fwd(s);
334 340 if (!success) {
335 341 xfree(s->auth_proto);
336 342 xfree(s->auth_data);
337 343 s->auth_proto = NULL;
338 344 s->auth_data = NULL;
339 345 }
340 346 break;
341 347
342 348 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
343 349 if (no_agent_forwarding_flag || compat13) {
344 350 debug("Authentication agent forwarding not permitted for this authentication.");
345 351 break;
346 352 }
347 353 debug("Received authentication agent forwarding request.");
348 354 success = auth_input_request_forwarding(s->pw);
349 355 break;
350 356
351 357 case SSH_CMSG_PORT_FORWARD_REQUEST:
352 358 if (no_port_forwarding_flag) {
353 359 debug("Port forwarding not permitted for this authentication.");
354 360 break;
355 361 }
356 362 if (!options.allow_tcp_forwarding) {
357 363 debug("Port forwarding not permitted.");
358 364 break;
359 365 }
360 366 debug("Received TCP/IP port forwarding request.");
361 367 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
362 368 success = 1;
363 369 break;
364 370
365 371 case SSH_CMSG_MAX_PACKET_SIZE:
366 372 if (packet_set_maxsize(packet_get_int()) > 0)
367 373 success = 1;
368 374 break;
369 375
370 376 #if defined(AFS) || defined(KRB5)
371 377 case SSH_CMSG_HAVE_KERBEROS_TGT:
372 378 if (!options.kerberos_tgt_passing) {
373 379 verbose("Kerberos TGT passing disabled.");
374 380 } else {
375 381 char *kdata = packet_get_string(&dlen);
376 382 packet_check_eom();
377 383
378 384 /* XXX - 0x41, see creds_to_radix version */
379 385 if (kdata[0] != 0x41) {
380 386 #ifdef KRB5
381 387 krb5_data tgt;
382 388 tgt.data = kdata;
383 389 tgt.length = dlen;
384 390
385 391 if (auth_krb5_tgt(s->authctxt, &tgt))
386 392 success = 1;
387 393 else
388 394 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
389 395 #endif /* KRB5 */
390 396 } else {
391 397 #ifdef AFS
392 398 if (auth_krb4_tgt(s->authctxt, kdata))
393 399 success = 1;
394 400 else
395 401 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
396 402 #endif /* AFS */
397 403 }
398 404 xfree(kdata);
399 405 }
400 406 break;
401 407 #endif /* AFS || KRB5 */
402 408
403 409 #ifdef AFS
404 410 case SSH_CMSG_HAVE_AFS_TOKEN:
405 411 if (!options.afs_token_passing || !k_hasafs()) {
406 412 verbose("AFS token passing disabled.");
407 413 } else {
408 414 /* Accept AFS token. */
409 415 char *token = packet_get_string(&dlen);
410 416 packet_check_eom();
411 417
412 418 if (auth_afs_token(s->authctxt, token))
413 419 success = 1;
414 420 else
415 421 verbose("AFS token refused for %.100s",
416 422 s->authctxt->user);
417 423 xfree(token);
418 424 }
419 425 break;
420 426 #endif /* AFS */
421 427
422 428 case SSH_CMSG_EXEC_SHELL:
423 429 case SSH_CMSG_EXEC_CMD:
424 430 if (type == SSH_CMSG_EXEC_CMD) {
425 431 command = packet_get_string(&dlen);
426 432 debug("Exec command '%.500s'", command);
427 433 do_exec(s, command);
428 434 xfree(command);
429 435 } else {
430 436 do_exec(s, NULL);
431 437 }
432 438 packet_check_eom();
433 439 session_close(s);
434 440 return;
435 441
436 442 default:
437 443 /*
438 444 * Any unknown messages in this phase are ignored,
439 445 * and a failure message is returned.
440 446 */
441 447 log("Unknown packet type received after authentication: %d", type);
442 448 }
443 449 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
444 450 packet_send();
445 451 packet_write_wait();
446 452
447 453 /* Enable compression now that we have replied if appropriate. */
448 454 if (enable_compression_after_reply) {
449 455 enable_compression_after_reply = 0;
450 456 packet_start_compression(compression_level);
451 457 }
452 458 }
453 459 }
454 460
455 461 /*
456 462 * This is called to fork and execute a command when we have no tty. This
457 463 * will call do_child from the child, and server_loop from the parent after
458 464 * setting up file descriptors and such.
459 465 */
460 466 void
461 467 do_exec_no_pty(Session *s, const char *command)
462 468 {
463 469 pid_t pid;
464 470
465 471 int inout[2], err[2];
466 472 /* Uses socket pairs to communicate with the program. */
467 473 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
468 474 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
469 475 packet_disconnect("Could not create socket pairs: %.100s",
470 476 strerror(errno));
471 477 if (s == NULL)
472 478 fatal("do_exec_no_pty: no session");
473 479
474 480 session_proctitle(s);
475 481
476 482 /* Fork the child. */
477 483 if ((pid = fork()) == 0) {
478 484 fatal_remove_all_cleanups();
479 485
480 486 /* Child. Reinitialize the log since the pid has changed. */
481 487 log_init(__progname, options.log_level, options.log_facility, log_stderr);
482 488
483 489 /*
484 490 * Create a new session and process group since the 4.4BSD
485 491 * setlogin() affects the entire process group.
486 492 */
487 493 if (setsid() < 0)
488 494 error("setsid failed: %.100s", strerror(errno));
489 495
490 496 /*
491 497 * Redirect stdin, stdout, and stderr. Stdin and stdout will
492 498 * use the same socket, as some programs (particularly rdist)
493 499 * seem to depend on it.
494 500 */
495 501 close(inout[1]);
496 502 close(err[1]);
497 503 if (dup2(inout[0], 0) < 0) /* stdin */
498 504 perror("dup2 stdin");
499 505 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
500 506 perror("dup2 stdout");
501 507 if (s->is_subsystem) {
502 508 /*
503 509 * Redirect the subsystem's stderr to /dev/null. We might send it
504 510 * over to the other side but changing that might break existing
505 511 * SSH clients.
506 512 */
507 513 close(err[0]);
508 514 if ((err[0] = open(_PATH_DEVNULL, O_WRONLY)) == -1)
509 515 fatal("Cannot open /dev/null: %.100s", strerror(errno));
510 516 }
511 517 if (dup2(err[0], 2) < 0) /* stderr */
512 518 perror("dup2 stderr");
513 519
514 520 #ifdef _UNICOS
515 521 cray_init_job(s->pw); /* set up cray jid and tmpdir */
516 522 #endif
517 523
518 524 /* Do processing for the child (exec command etc). */
519 525 do_child(s, command);
520 526 /* NOTREACHED */
521 527 }
522 528 #ifdef _UNICOS
523 529 signal(WJSIGNAL, cray_job_termination_handler);
524 530 #endif /* _UNICOS */
525 531 #ifdef HAVE_CYGWIN
526 532 if (is_winnt)
527 533 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
528 534 #endif
529 535 if (pid < 0)
530 536 packet_disconnect("fork failed: %.100s", strerror(errno));
531 537
532 538 s->pid = pid;
533 539 /* Set interactive/non-interactive mode. */
534 540 packet_set_interactive(s->display != NULL);
535 541
536 542 /* We are the parent. Close the child sides of the socket pairs. */
537 543 close(inout[0]);
538 544 close(err[0]);
539 545
540 546 /*
541 547 * Enter the interactive session. Note: server_loop must be able to
542 548 * handle the case that fdin and fdout are the same.
543 549 */
544 550 if (compat20) {
545 551 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
546 552 if (s->is_subsystem)
547 553 close(err[1]);
548 554 /* Don't close channel before sending exit-status! */
549 555 channel_set_wait_for_exit(s->chanid, 1);
550 556 } else {
551 557 server_loop(pid, inout[1], inout[1], err[1]);
552 558 /* server_loop has closed inout[1] and err[1]. */
553 559 }
554 560 }
555 561
556 562 /*
557 563 * This is called to fork and execute a command when we have a tty. This
558 564 * will call do_child from the child, and server_loop from the parent after
559 565 * setting up file descriptors, controlling tty, updating wtmp, utmp,
560 566 * lastlog, and other such operations.
561 567 */
562 568 void
563 569 do_exec_pty(Session *s, const char *command)
564 570 {
565 571 int fdout, ptyfd, ttyfd, ptymaster, pipe_fds[2];
566 572 pid_t pid;
567 573
568 574 if (s == NULL)
569 575 fatal("do_exec_pty: no session");
570 576 ptyfd = s->ptyfd;
571 577 ttyfd = s->ttyfd;
572 578
573 579 #ifdef USE_PAM
574 580 session_do_pam(s, 1); /* pam_open_session() */
575 581 #endif /* USE_PAM */
576 582
577 583 /*
578 584 * This pipe lets sshd wait for child to exec or exit. This is
579 585 * particularly important for ALTPRIVSEP because the child is
580 586 * the one to call the monitor to request a record_login() and
581 587 * we don't want the child and the parent to compete for the
582 588 * monitor's attention. But this is generic code and doesn't
583 589 * hurt to have here even if ALTPRIVSEP is not used.
584 590 */
585 591 if (pipe(pipe_fds) != 0)
586 592 packet_disconnect("pipe failed: %.100s", strerror(errno));
587 593
588 594 (void) fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC);
589 595 (void) fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC);
590 596
591 597 /* Fork the child. */
592 598 if ((pid = fork()) == 0) {
593 599 (void) close(pipe_fds[0]);
594 600
595 601 fatal_remove_all_cleanups();
596 602
597 603 /* Child. Reinitialize the log because the pid has changed. */
598 604 log_init(__progname, options.log_level, options.log_facility, log_stderr);
599 605 /* Close the master side of the pseudo tty. */
600 606 close(ptyfd);
601 607
602 608 /* Make the pseudo tty our controlling tty. */
603 609 pty_make_controlling_tty(&ttyfd, s->tty);
604 610
605 611 /* Redirect stdin/stdout/stderr from the pseudo tty. */
606 612 if (dup2(ttyfd, 0) < 0)
607 613 error("dup2 stdin: %s", strerror(errno));
608 614 if (dup2(ttyfd, 1) < 0)
609 615 error("dup2 stdout: %s", strerror(errno));
610 616 if (dup2(ttyfd, 2) < 0)
611 617 error("dup2 stderr: %s", strerror(errno));
612 618
613 619 /* Close the extra descriptor for the pseudo tty. */
614 620 close(ttyfd);
615 621
616 622 /* record login, etc. similar to login(1) */
617 623 do_login(s, command);
618 624
619 625 /*
620 626 * Close the pipe to the parent so it can re-enter its event
621 627 * loop and service the ptm; if enough debug messages get
622 628 * written to the pty before this happens there will be a
623 629 * deadlock.
624 630 */
625 631 close(pipe_fds[1]);
626 632
627 633 /*
628 634 * do_motd() was called originally in do_login(). However,
629 635 * when the /etc/motd file is large, a deadlock would happen,
630 636 * because
631 637 * - The child is blocked at fputs() to pty, when pty buffer
632 638 * is full.
633 639 * - The parent can not consume the pty buffer, because it is
634 640 * still blocked at read(pipe_fds[0]).
635 641 *
636 642 * To resolve the deadlock issue, we defer do_motd() after
637 643 * close(pipe_fds[1]).
638 644 */
639 645 do_motd();
640 646
641 647 /* Do common processing for the child, such as execing the command. */
642 648 do_child(s, command);
643 649 /* NOTREACHED */
644 650 }
645 651
646 652 /* Wait for child to exec() or exit() */
647 653 (void) close(pipe_fds[1]);
648 654 (void) read(pipe_fds[0], &pipe_fds[1], sizeof(int));
649 655
650 656 #ifdef _UNICOS
651 657 signal(WJSIGNAL, cray_job_termination_handler);
652 658 #endif /* _UNICOS */
653 659 #ifdef HAVE_CYGWIN
654 660 if (is_winnt)
655 661 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
656 662 #endif
657 663 if (pid < 0)
658 664 packet_disconnect("fork failed: %.100s", strerror(errno));
659 665 s->pid = pid;
660 666
661 667 /* Parent. Close the slave side of the pseudo tty. */
662 668 close(ttyfd);
663 669
664 670 /*
665 671 * Create another descriptor of the pty master side for use as the
666 672 * standard input. We could use the original descriptor, but this
667 673 * simplifies code in server_loop. The descriptor is bidirectional.
668 674 */
669 675 fdout = dup(ptyfd);
670 676 if (fdout < 0)
671 677 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
672 678
673 679 /* we keep a reference to the pty master */
674 680 ptymaster = dup(ptyfd);
675 681 if (ptymaster < 0)
676 682 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
677 683 s->ptymaster = ptymaster;
678 684
679 685 /* Enter interactive session. */
680 686 packet_set_interactive(1);
681 687 if (compat20) {
682 688 session_set_fds(s, ptyfd, fdout, -1);
683 689 /* Don't close channel before sending exit-status! */
684 690 channel_set_wait_for_exit(s->chanid, 1);
685 691 } else {
686 692 server_loop(pid, ptyfd, fdout, -1);
687 693 /* server_loop _has_ closed ptyfd and fdout. */
688 694 }
689 695 }
690 696
691 697 /*
692 698 * This is called to fork and execute a command. If another command is
693 699 * to be forced, execute that instead.
694 700 */
695 701 void
696 702 do_exec(Session *s, const char *command)
697 703 {
698 704 if (command)
699 705 s->command = xstrdup(command);
700 706
701 707 if (forced_command) {
702 708 original_command = command;
703 709 command = forced_command;
704 710 debug("Forced command '%.900s'", command);
705 711 }
706 712
707 713 if (s->ttyfd != -1)
708 714 do_exec_pty(s, command);
709 715 else
710 716 do_exec_no_pty(s, command);
711 717
712 718 original_command = NULL;
713 719 }
714 720
715 721
716 722 /* administrative, login(1)-like work */
717 723 void
718 724 do_login(Session *s, const char *command)
719 725 {
720 726 char *time_string;
721 727 #ifndef ALTPRIVSEP
722 728 struct passwd * pw = s->pw;
723 729 #endif /* ALTPRIVSEP*/
724 730 pid_t pid = getpid();
725 731
726 732 /* Record that there was a login on that tty from the remote host. */
727 733 #ifdef ALTPRIVSEP
728 734 debug3("Recording SSHv2 channel login in utmpx/wtmpx");
729 735 altprivsep_record_login(pid, s->tty);
730 736 #endif /* ALTPRIVSEP*/
731 737
732 738 if (check_quietlogin(s, command))
733 739 return;
734 740
735 741 #ifdef USE_PAM
736 742 print_pam_messages();
737 743 #endif /* USE_PAM */
738 744 #ifdef WITH_AIXAUTHENTICATE
739 745 if (aixloginmsg && *aixloginmsg)
740 746 printf("%s\n", aixloginmsg);
741 747 #endif /* WITH_AIXAUTHENTICATE */
742 748
743 749 #ifndef NO_SSH_LASTLOG
744 750 if (options.print_lastlog && s->last_login_time != 0) {
745 751 time_string = ctime(&s->last_login_time);
746 752 if (strchr(time_string, '\n'))
747 753 *strchr(time_string, '\n') = 0;
748 754 if (strcmp(s->hostname, "") == 0)
749 755 printf("Last login: %s\r\n", time_string);
750 756 else
751 757 printf("Last login: %s from %s\r\n", time_string,
752 758 s->hostname);
753 759 }
754 760 #endif /* NO_SSH_LASTLOG */
755 761
756 762 }
757 763
758 764 /*
759 765 * Display the message of the day.
760 766 */
761 767 void
762 768 do_motd(void)
763 769 {
764 770 FILE *f;
765 771 char buf[256];
766 772
767 773 if (options.print_motd) {
768 774 #ifdef HAVE_LOGIN_CAP
769 775 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
770 776 "/etc/motd"), "r");
771 777 #else
772 778 f = fopen("/etc/motd", "r");
773 779 #endif
774 780 if (f) {
775 781 while (fgets(buf, sizeof(buf), f))
776 782 fputs(buf, stdout);
777 783 fclose(f);
778 784 }
779 785 }
780 786 }
781 787
782 788
783 789 /*
784 790 * Check for quiet login, either .hushlogin or command given.
785 791 */
786 792 int
787 793 check_quietlogin(Session *s, const char *command)
788 794 {
789 795 char buf[256];
790 796 struct passwd *pw = s->pw;
791 797 struct stat st;
792 798
793 799 /* Return 1 if .hushlogin exists or a command given. */
794 800 if (command != NULL)
795 801 return 1;
796 802 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
797 803 #ifdef HAVE_LOGIN_CAP
798 804 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
799 805 return 1;
800 806 #else
801 807 if (stat(buf, &st) >= 0)
802 808 return 1;
803 809 #endif
804 810 return 0;
805 811 }
806 812
807 813 /*
808 814 * Sets the value of the given variable in the environment. If the variable
809 815 * already exists, its value is overriden.
810 816 */
811 817 void
812 818 child_set_env(char ***envp, u_int *envsizep, const char *name,
813 819 const char *value)
814 820 {
815 821 debug3("child_set_env(%s, %s)", name, value);
816 822 child_set_env_silent(envp, envsizep, name, value);
817 823 }
818 824
819 825
820 826 void
821 827 child_set_env_silent(char ***envp, u_int *envsizep, const char *name,
822 828 const char *value)
823 829 {
824 830 u_int i, namelen;
825 831 char **env;
826 832
827 833 /*
828 834 * Find the slot where the value should be stored. If the variable
829 835 * already exists, we reuse the slot; otherwise we append a new slot
830 836 * at the end of the array, expanding if necessary.
831 837 */
832 838 env = *envp;
833 839 namelen = strlen(name);
834 840 for (i = 0; env[i]; i++)
835 841 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
836 842 break;
837 843 if (env[i]) {
838 844 /* Reuse the slot. */
839 845 xfree(env[i]);
840 846 } else {
841 847 /* New variable. Expand if necessary. */
842 848 if (i >= (*envsizep) - 1) {
843 849 if (*envsizep >= 1000)
844 850 fatal("child_set_env: too many env vars,"
845 851 " skipping: %.100s", name);
846 852 (*envsizep) += 50;
847 853 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
848 854 }
849 855 /* Need to set the NULL pointer at end of array beyond the new slot. */
850 856 env[i + 1] = NULL;
851 857 }
852 858
853 859 /* Allocate space and format the variable in the appropriate slot. */
854 860 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
855 861 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
856 862 }
857 863
858 864 /*
859 865 * Reads environment variables from the given file and adds/overrides them
860 866 * into the environment. If the file does not exist, this does nothing.
861 867 * Otherwise, it must consist of empty lines, comments (line starts with '#')
862 868 * and assignments of the form name=value. No other forms are allowed.
863 869 */
864 870 static void
865 871 read_environment_file(char ***env, u_int *envsize,
866 872 const char *filename)
867 873 {
868 874 FILE *f;
869 875 char buf[4096];
870 876 char *cp, *value;
871 877 u_int lineno = 0;
872 878
873 879 f = fopen(filename, "r");
874 880 if (!f)
875 881 return;
876 882
877 883 while (fgets(buf, sizeof(buf), f)) {
878 884 if (++lineno > 1000)
879 885 fatal("Too many lines in environment file %s", filename);
880 886 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
881 887 ;
882 888 if (!*cp || *cp == '#' || *cp == '\n')
883 889 continue;
884 890 if (strchr(cp, '\n'))
885 891 *strchr(cp, '\n') = '\0';
886 892 value = strchr(cp, '=');
887 893 if (value == NULL) {
888 894 fprintf(stderr, gettext("Bad line %u in %.100s\n"),
889 895 lineno, filename);
890 896 continue;
891 897 }
892 898 /*
893 899 * Replace the equals sign by nul, and advance value to
894 900 * the value string.
895 901 */
896 902 *value = '\0';
897 903 value++;
898 904 child_set_env(env, envsize, cp, value);
899 905 }
900 906 fclose(f);
901 907 }
902 908
903 909 void copy_environment(char **source, char ***env, u_int *envsize)
904 910 {
905 911 char *var_name, *var_val;
906 912 int i;
907 913
908 914 if (source == NULL)
909 915 return;
910 916
911 917 for(i = 0; source[i] != NULL; i++) {
912 918 var_name = xstrdup(source[i]);
913 919 if ((var_val = strstr(var_name, "=")) == NULL) {
914 920 xfree(var_name);
915 921 continue;
916 922 }
917 923 *var_val++ = '\0';
918 924
919 925 debug3("Copy environment: %s=%s", var_name, var_val);
920 926 child_set_env(env, envsize, var_name, var_val);
921 927
922 928 xfree(var_name);
923 929 }
924 930 }
925 931
926 932 #ifdef HAVE_DEFOPEN
927 933 static
928 934 void
929 935 deflt_do_setup_env(Session *s, const char *shell, char ***env, u_int *envsize)
930 936 {
931 937 int flags;
932 938 char *ptr;
933 939 mode_t Umask = 022;
934 940
935 941 if (defopen(_PATH_DEFAULT_LOGIN))
936 942 return;
937 943
938 944 /* Ignore case */
939 945 flags = defcntl(DC_GETFLAGS, 0);
940 946 TURNOFF(flags, DC_CASE);
941 947 (void) defcntl(DC_SETFLAGS, flags);
942 948
943 949 /* TZ & HZ */
944 950 if ((ptr = defread("TIMEZONE=")) != NULL)
945 951 child_set_env(env, envsize, "TZ", ptr);
946 952 if ((ptr = defread("HZ=")) != NULL)
947 953 child_set_env(env, envsize, "HZ", ptr);
948 954
949 955 /* PATH */
950 956 if (s->pw->pw_uid != 0 && (ptr = defread("PATH=")) != NULL)
951 957 child_set_env(env, envsize, "PATH", ptr);
952 958 if (s->pw->pw_uid == 0 && (ptr = defread("SUPATH=")) != NULL)
953 959 child_set_env(env, envsize, "PATH", ptr);
954 960
955 961 /* SHELL */
956 962 if ((ptr = defread("ALTSHELL=")) != NULL) {
957 963 if (strcasecmp("YES", ptr) == 0)
958 964 child_set_env(env, envsize, "SHELL", shell);
959 965 else
960 966 child_set_env(env, envsize, "SHELL", "");
961 967 }
962 968
963 969 /* UMASK */
964 970 if ((ptr = defread("UMASK=")) != NULL &&
965 971 sscanf(ptr, "%lo", &Umask) == 1 &&
966 972 Umask <= (mode_t)0777)
967 973 (void) umask(Umask);
968 974 else
969 975 (void) umask(022);
970 976
971 977 /* ULIMIT */
972 978 if ((ptr = defread("ULIMIT=")) != NULL && atol(ptr) > 0L &&
973 979 ulimit(UL_SETFSIZE, atol(ptr)) < 0L)
974 980 error("Could not set ULIMIT to %ld from %s\n", atol(ptr),
975 981 _PATH_DEFAULT_LOGIN);
976 982
977 983 (void) defopen(NULL);
978 984 }
979 985 #endif /* HAVE_DEFOPEN */
980 986
981 987 static char **
982 988 do_setup_env(Session *s, const char *shell)
983 989 {
984 990 char buf[256];
985 991 char path_maildir[] = _PATH_MAILDIR;
986 992 u_int i, envsize, pm_len;
987 993 char **env;
988 994 struct passwd *pw = s->pw;
989 995
990 996 /* Initialize the environment. */
991 997 envsize = 100;
992 998 env = xmalloc(envsize * sizeof(char *));
993 999 env[0] = NULL;
994 1000
995 1001 #ifdef HAVE_CYGWIN
996 1002 /*
997 1003 * The Windows environment contains some setting which are
998 1004 * important for a running system. They must not be dropped.
999 1005 */
1000 1006 copy_environment(environ, &env, &envsize);
1001 1007 #endif
1002 1008
1003 1009 #ifdef GSSAPI
1004 1010 /* Allow any GSSAPI methods that we've used to alter
1005 1011 * the childs environment as they see fit
1006 1012 */
1007 1013 ssh_gssapi_do_child(xxx_gssctxt, &env,&envsize);
1008 1014 #endif
1009 1015
1010 1016 /* Set basic environment. */
1011 1017 child_set_env(&env, &envsize, "USER", pw->pw_name);
1012 1018 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1013 1019 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1014 1020 #ifdef HAVE_LOGIN_CAP
1015 1021 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
1016 1022 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1017 1023 else
1018 1024 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1019 1025 #else /* HAVE_LOGIN_CAP */
1020 1026 # ifndef HAVE_CYGWIN
1021 1027 /*
1022 1028 * There's no standard path on Windows. The path contains
1023 1029 * important components pointing to the system directories,
1024 1030 * needed for loading shared libraries. So the path better
1025 1031 * remains intact here.
1026 1032 */
1027 1033 # ifdef SUPERUSER_PATH
1028 1034 child_set_env(&env, &envsize, "PATH",
1029 1035 s->pw->pw_uid == 0 ? SUPERUSER_PATH : _PATH_STDPATH);
1030 1036 # else
1031 1037 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1032 1038 # endif /* SUPERUSER_PATH */
1033 1039 # endif /* HAVE_CYGWIN */
1034 1040 #endif /* HAVE_LOGIN_CAP */
1035 1041
1036 1042 pm_len = strlen(path_maildir);
1037 1043 if (path_maildir[pm_len - 1] == '/' && pm_len > 1)
1038 1044 path_maildir[pm_len - 1] = NULL;
1039 1045 snprintf(buf, sizeof buf, "%.200s/%.50s",
1040 1046 path_maildir, pw->pw_name);
1041 1047 child_set_env(&env, &envsize, "MAIL", buf);
1042 1048
1043 1049 /* Normal systems set SHELL by default. */
1044 1050 child_set_env(&env, &envsize, "SHELL", shell);
1045 1051
1046 1052 #ifdef HAVE_DEFOPEN
1047 1053 deflt_do_setup_env(s, shell, &env, &envsize);
1048 1054 #endif /* HAVE_DEFOPEN */
1049 1055
1050 1056 #define PASS_ENV(x) \
1051 1057 if (getenv(x)) \
1052 1058 child_set_env(&env, &envsize, x, getenv(x));
1053 1059
1054 1060 if (getenv("TZ"))
1055 1061 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1056 1062
1057 1063 if (s->auth_file != NULL)
1058 1064 child_set_env(&env, &envsize, "XAUTHORITY", s->auth_file);
1059 1065
1060 1066 PASS_ENV("LANG")
1061 1067 PASS_ENV("LC_ALL")
1062 1068 PASS_ENV("LC_CTYPE")
1063 1069 PASS_ENV("LC_COLLATE")
1064 1070 PASS_ENV("LC_TIME")
1065 1071 PASS_ENV("LC_NUMERIC")
1066 1072 PASS_ENV("LC_MONETARY")
1067 1073 PASS_ENV("LC_MESSAGES")
1068 1074
1069 1075 #undef PASS_ENV
1070 1076
1071 1077 if (s->env != NULL)
1072 1078 copy_environment(s->env, &env, &envsize);
1073 1079
1074 1080 /* Set custom environment options from RSA authentication. */
1075 1081 while (custom_environment) {
1076 1082 struct envstring *ce = custom_environment;
1077 1083 char *str = ce->s;
1078 1084
1079 1085 for (i = 0; str[i] != '=' && str[i]; i++)
1080 1086 ;
1081 1087 if (str[i] == '=') {
1082 1088 str[i] = 0;
1083 1089 child_set_env(&env, &envsize, str, str + i + 1);
1084 1090 }
1085 1091 custom_environment = ce->next;
1086 1092 xfree(ce->s);
1087 1093 xfree(ce);
1088 1094 }
1089 1095
1090 1096 /* SSH_CLIENT deprecated */
1091 1097 snprintf(buf, sizeof buf, "%.50s %d %d",
1092 1098 get_remote_ipaddr(), get_remote_port(), get_local_port());
1093 1099 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1094 1100
1095 1101 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1096 1102 get_remote_ipaddr(), get_remote_port(),
1097 1103 get_local_ipaddr(packet_get_connection_in()), get_local_port());
1098 1104 child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1099 1105
1100 1106 if (s->ttyfd != -1)
1101 1107 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1102 1108 if (s->term)
1103 1109 child_set_env(&env, &envsize, "TERM", s->term);
1104 1110 if (s->display)
1105 1111 child_set_env(&env, &envsize, "DISPLAY", s->display);
1106 1112 if (original_command)
1107 1113 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1108 1114 original_command);
1109 1115
1110 1116 #ifdef _UNICOS
1111 1117 if (cray_tmpdir[0] != '\0')
1112 1118 child_set_env(&env, &envsize, "TMPDIR", cray_tmpdir);
1113 1119 #endif /* _UNICOS */
1114 1120
1115 1121 #ifdef _AIX
1116 1122 {
1117 1123 char *cp;
1118 1124
1119 1125 if ((cp = getenv("AUTHSTATE")) != NULL)
1120 1126 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1121 1127 if ((cp = getenv("KRB5CCNAME")) != NULL)
1122 1128 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1123 1129 read_environment_file(&env, &envsize, "/etc/environment");
1124 1130 }
1125 1131 #endif
1126 1132 #ifdef KRB4
1127 1133 if (s->authctxt->krb4_ticket_file)
1128 1134 child_set_env(&env, &envsize, "KRBTKFILE",
1129 1135 s->authctxt->krb4_ticket_file);
1130 1136 #endif
1131 1137 #ifdef KRB5
1132 1138 if (s->authctxt->krb5_ticket_file)
1133 1139 child_set_env(&env, &envsize, "KRB5CCNAME",
1134 1140 s->authctxt->krb5_ticket_file);
1135 1141 #endif
1136 1142 #ifdef USE_PAM
1137 1143 /*
1138 1144 * Pull in any environment variables that may have
1139 1145 * been set by PAM.
1140 1146 */
1141 1147 {
1142 1148 char **p;
1143 1149
1144 1150 p = fetch_pam_environment(s->authctxt);
1145 1151 copy_environment(p, &env, &envsize);
1146 1152 free_pam_environment(p);
1147 1153 }
1148 1154 #endif /* USE_PAM */
1149 1155
1150 1156 if (auth_sock_name != NULL)
1151 1157 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1152 1158 auth_sock_name);
1153 1159
1154 1160 /* read $HOME/.ssh/environment. */
1155 1161 if (options.permit_user_env) {
1156 1162 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1157 1163 strcmp(pw->pw_dir, "/") ? pw->pw_dir : "");
1158 1164 read_environment_file(&env, &envsize, buf);
1159 1165 }
1160 1166 if (debug_flag) {
1161 1167 /* dump the environment */
1162 1168 fprintf(stderr, gettext("Environment:\n"));
1163 1169 for (i = 0; env[i]; i++)
1164 1170 fprintf(stderr, " %.200s\n", env[i]);
1165 1171 }
1166 1172 return env;
1167 1173 }
1168 1174
1169 1175 /*
1170 1176 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
1171 1177 * first in this order).
1172 1178 */
1173 1179 static void
1174 1180 do_rc_files(Session *s, const char *shell)
1175 1181 {
1176 1182 FILE *f = NULL;
1177 1183 char cmd[1024];
1178 1184 int do_xauth;
1179 1185 struct stat st;
1180 1186
1181 1187 do_xauth =
1182 1188 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1183 1189
1184 1190 /* ignore _PATH_SSH_USER_RC for subsystems */
1185 1191 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1186 1192 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1187 1193 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1188 1194 if (debug_flag)
1189 1195 fprintf(stderr, "Running %s\n", cmd);
1190 1196 f = popen(cmd, "w");
1191 1197 if (f) {
1192 1198 if (do_xauth)
1193 1199 fprintf(f, "%s %s\n", s->auth_proto,
1194 1200 s->auth_data);
1195 1201 pclose(f);
1196 1202 } else
1197 1203 fprintf(stderr, "Could not run %s\n",
1198 1204 _PATH_SSH_USER_RC);
1199 1205 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1200 1206 if (debug_flag)
1201 1207 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1202 1208 _PATH_SSH_SYSTEM_RC);
1203 1209 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1204 1210 if (f) {
1205 1211 if (do_xauth)
1206 1212 fprintf(f, "%s %s\n", s->auth_proto,
1207 1213 s->auth_data);
1208 1214 pclose(f);
1209 1215 } else
1210 1216 fprintf(stderr, "Could not run %s\n",
1211 1217 _PATH_SSH_SYSTEM_RC);
1212 1218 } else if (do_xauth && options.xauth_location != NULL) {
1213 1219 /* Add authority data to .Xauthority if appropriate. */
1214 1220 if (debug_flag) {
1215 1221 fprintf(stderr,
1216 1222 "Running %.500s add "
1217 1223 "%.100s %.100s %.100s\n",
1218 1224 options.xauth_location, s->auth_display,
1219 1225 s->auth_proto, s->auth_data);
1220 1226 }
1221 1227 snprintf(cmd, sizeof cmd, "%s -q -",
1222 1228 options.xauth_location);
1223 1229 f = popen(cmd, "w");
1224 1230 if (f) {
1225 1231 fprintf(f, "add %s %s %s\n",
1226 1232 s->auth_display, s->auth_proto,
1227 1233 s->auth_data);
1228 1234 pclose(f);
1229 1235 } else {
1230 1236 fprintf(stderr, "Could not run %s\n",
1231 1237 cmd);
1232 1238 }
1233 1239 }
1234 1240 }
1235 1241
1236 1242 /* Disallow logins if /etc/nologin exists. This does not apply to root. */
1237 1243 static void
1238 1244 do_nologin(struct passwd *pw)
1239 1245 {
1240 1246 FILE *f = NULL;
1241 1247 char buf[1024];
1242 1248 struct stat sb;
1243 1249
1244 1250 if (pw->pw_uid == 0)
1245 1251 return;
1246 1252
1247 1253 if (stat(_PATH_NOLOGIN, &sb) == -1)
1248 1254 return;
1249 1255
1250 1256 /* /etc/nologin exists. Print its contents if we can and exit. */
1251 1257 log("User %.100s not allowed because %s exists.", pw->pw_name,
1252 1258 _PATH_NOLOGIN);
1253 1259 if ((f = fopen(_PATH_NOLOGIN, "r")) != NULL) {
1254 1260 while (fgets(buf, sizeof(buf), f))
1255 1261 fputs(buf, stderr);
1256 1262 fclose(f);
1257 1263 }
1258 1264 exit(254);
1259 1265 }
1260 1266
1261 1267 /* Chroot into ChrootDirectory if the option is set. */
1262 1268 void
1263 1269 chroot_if_needed(struct passwd *pw)
1264 1270 {
1265 1271 char *chroot_path, *tmp;
1266 1272
1267 1273 if (chroot_requested(options.chroot_directory)) {
1268 1274 tmp = tilde_expand_filename(options.chroot_directory,
1269 1275 pw->pw_uid);
1270 1276 chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1271 1277 "u", pw->pw_name, (char *)NULL);
1272 1278 safely_chroot(chroot_path, pw->pw_uid);
1273 1279 free(tmp);
1274 1280 free(chroot_path);
1275 1281 }
1276 1282 }
1277 1283
1278 1284 /*
1279 1285 * Chroot into a directory after checking it for safety: all path components
1280 1286 * must be root-owned directories with strict permissions.
1281 1287 */
1282 1288 static void
1283 1289 safely_chroot(const char *path, uid_t uid)
1284 1290 {
1285 1291 const char *cp;
1286 1292 char component[MAXPATHLEN];
1287 1293 struct stat st;
1288 1294
1289 1295 if (*path != '/')
1290 1296 fatal("chroot path does not begin at root");
1291 1297 if (strlen(path) >= sizeof(component))
1292 1298 fatal("chroot path too long");
1293 1299
1294 1300 /*
1295 1301 * Descend the path, checking that each component is a
1296 1302 * root-owned directory with strict permissions.
1297 1303 */
1298 1304 for (cp = path; cp != NULL;) {
1299 1305 if ((cp = strchr(cp, '/')) == NULL)
1300 1306 strlcpy(component, path, sizeof(component));
1301 1307 else {
1302 1308 cp++;
1303 1309 memcpy(component, path, cp - path);
1304 1310 component[cp - path] = '\0';
1305 1311 }
1306 1312
1307 1313 debug3("%s: checking '%s'", __func__, component);
1308 1314
1309 1315 if (stat(component, &st) != 0)
1310 1316 fatal("%s: stat(\"%s\"): %s", __func__,
1311 1317 component, strerror(errno));
1312 1318 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1313 1319 fatal("bad ownership or modes for chroot "
1314 1320 "directory %s\"%s\"",
1315 1321 cp == NULL ? "" : "component ", component);
1316 1322 if (!S_ISDIR(st.st_mode))
1317 1323 fatal("chroot path %s\"%s\" is not a directory",
1318 1324 cp == NULL ? "" : "component ", component);
1319 1325 }
1320 1326
1321 1327 if (chdir(path) == -1)
1322 1328 fatal("Unable to chdir to chroot path \"%s\": "
1323 1329 "%s", path, strerror(errno));
1324 1330 if (chroot(path) == -1)
1325 1331 fatal("chroot(\"%s\"): %s", path, strerror(errno));
1326 1332 if (chdir("/") == -1)
1327 1333 fatal("%s: chdir(/) after chroot: %s",
1328 1334 __func__, strerror(errno));
1329 1335 verbose("Changed root directory to \"%s\"", path);
1330 1336 }
1331 1337
1332 1338 static void
1333 1339 launch_login(struct passwd *pw, const char *hostname)
1334 1340 {
1335 1341 /* Launch login(1). */
1336 1342
1337 1343 execl(LOGIN_PROGRAM, "login", "-h", hostname,
1338 1344 #ifdef xxxLOGIN_NEEDS_TERM
1339 1345 (s->term ? s->term : "unknown"),
1340 1346 #endif /* LOGIN_NEEDS_TERM */
1341 1347 #ifdef LOGIN_NO_ENDOPT
1342 1348 "-p", "-f", pw->pw_name, (char *)NULL);
1343 1349 #else
1344 1350 "-p", "-f", "--", pw->pw_name, (char *)NULL);
1345 1351 #endif
1346 1352
1347 1353 /* Login couldn't be executed, die. */
1348 1354
1349 1355 perror("login");
1350 1356 exit(1);
1351 1357 }
1352 1358
1353 1359 /*
1354 1360 * Performs common processing for the child, such as setting up the
1355 1361 * environment, closing extra file descriptors, setting the user and group
1356 1362 * ids, and executing the command or shell.
1357 1363 */
1358 1364 #define ARGV_MAX 10
1359 1365 void
1360 1366 do_child(Session *s, const char *command)
1361 1367 {
1362 1368 extern char **environ;
1363 1369 char **env;
1364 1370 char *argv[ARGV_MAX];
1365 1371 const char *shell, *shell0;
1366 1372 struct passwd *pw = s->pw;
1367 1373
1368 1374 /* remove hostkey from the child's memory */
1369 1375 destroy_sensitive_data();
1370 1376
1371 1377 do_nologin(pw);
1372 1378 chroot_if_needed(pw);
1373 1379
1374 1380 /*
1375 1381 * Get the shell from the password data. An empty shell field is
1376 1382 * legal, and means /bin/sh.
1377 1383 */
1378 1384 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1379 1385 #ifdef HAVE_LOGIN_CAP
1380 1386 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1381 1387 #endif
1382 1388
1383 1389 env = do_setup_env(s, shell);
1384 1390
1385 1391 /*
1386 1392 * Close the connection descriptors; note that this is the child, and
1387 1393 * the server will still have the socket open, and it is important
1388 1394 * that we do not shutdown it. Note that the descriptors cannot be
1389 1395 * closed before building the environment, as we call
1390 1396 * get_remote_ipaddr there.
1391 1397 */
1392 1398 if (packet_get_connection_in() == packet_get_connection_out())
1393 1399 close(packet_get_connection_in());
1394 1400 else {
1395 1401 close(packet_get_connection_in());
1396 1402 close(packet_get_connection_out());
1397 1403 }
1398 1404 /*
1399 1405 * Close all descriptors related to channels. They will still remain
1400 1406 * open in the parent.
1401 1407 */
1402 1408 /* XXX better use close-on-exec? -markus */
1403 1409 channel_close_all();
1404 1410
1405 1411 /*
1406 1412 * Close any extra file descriptors. Note that there may still be
1407 1413 * descriptors left by system functions. They will be closed later.
1408 1414 */
1409 1415 endpwent();
1410 1416
1411 1417 /*
1412 1418 * Must switch to the new environment variables so that .ssh/rc,
1413 1419 * /etc/ssh/sshrc, and xauth are run in the proper environment.
1414 1420 */
1415 1421 environ = env;
1416 1422
1417 1423 /*
1418 1424 * New environment has been installed. We need to update locale
1419 1425 * so that error messages beyond this point have the proper
1420 1426 * character encoding.
1421 1427 */
1422 1428 (void) setlocale(LC_ALL, "");
1423 1429
1424 1430 /*
1425 1431 * Close any extra open file descriptors so that we don\'t have them
1426 1432 * hanging around in clients. Note that we want to do this after
1427 1433 * initgroups, because at least on Solaris 2.3 it leaves file
1428 1434 * descriptors open.
1429 1435 */
1430 1436 closefrom(STDERR_FILENO + 1);
1431 1437
1432 1438 #ifdef AFS
1433 1439 /* Try to get AFS tokens for the local cell. */
1434 1440 if (k_hasafs()) {
1435 1441 char cell[64];
1436 1442
1437 1443 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1438 1444 krb_afslog(cell, 0);
1439 1445
1440 1446 krb_afslog(0, 0);
1441 1447 }
1442 1448 #endif /* AFS */
1443 1449
1444 1450 /* Change current directory to the user's home directory. */
1445 1451 if (chdir(pw->pw_dir) < 0) {
1446 1452 /* Suppress missing homedir warning for chroot case */
1447 1453 if (!chroot_requested(options.chroot_directory))
1448 1454 fprintf(stderr, "Could not chdir to home "
1449 1455 "directory %s: %s\n", pw->pw_dir,
1450 1456 strerror(errno));
1451 1457 }
1452 1458
1453 1459 do_rc_files(s, shell);
1454 1460
1455 1461 /* restore SIGPIPE for child */
1456 1462 signal(SIGPIPE, SIG_DFL);
1457 1463
1458 1464 if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
1459 1465 int i;
1460 1466 char *p, *args;
1461 1467 extern int optind, optreset;
1462 1468
1463 1469 /* This will set the E/P sets here, simulating exec(2). */
1464 1470 drop_privs(pw->pw_uid);
1465 1471
1466 1472 setproctitle("%s@internal-sftp-server", s->pw->pw_name);
1467 1473 args = xstrdup(command ? command : "sftp-server");
1468 1474
1469 1475 i = 0;
1470 1476 for ((p = strtok(args, " ")); p != NULL; (p = strtok(NULL, " "))) {
1471 1477 if (i < ARGV_MAX - 1)
1472 1478 argv[i++] = p;
1473 1479 }
1474 1480
1475 1481 argv[i] = NULL;
1476 1482 optind = optreset = 1;
1477 1483 __progname = argv[0];
1478 1484 exit(sftp_server_main(i, argv, s->pw));
1479 1485 }
1480 1486
1481 1487 /* Get the last component of the shell name. */
1482 1488 if ((shell0 = strrchr(shell, '/')) != NULL)
1483 1489 shell0++;
1484 1490 else
1485 1491 shell0 = shell;
1486 1492
1487 1493 /*
1488 1494 * If we have no command, execute the shell. In this case, the shell
1489 1495 * name to be passed in argv[0] is preceded by '-' to indicate that
1490 1496 * this is a login shell.
1491 1497 */
1492 1498 if (!command) {
1493 1499 char argv0[256];
1494 1500
1495 1501 /* Start the shell. Set initial character to '-'. */
1496 1502 argv0[0] = '-';
1497 1503
1498 1504 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1499 1505 >= sizeof(argv0) - 1) {
1500 1506 errno = EINVAL;
1501 1507 perror(shell);
1502 1508 exit(1);
1503 1509 }
1504 1510
1505 1511 /* Execute the shell. */
1506 1512 argv[0] = argv0;
1507 1513 argv[1] = NULL;
1508 1514 execve(shell, argv, env);
1509 1515
1510 1516 /* Executing the shell failed. */
1511 1517 perror(shell);
1512 1518 exit(1);
1513 1519 }
1514 1520 /*
1515 1521 * Execute the command using the user's shell. This uses the -c
1516 1522 * option to execute the command.
1517 1523 */
1518 1524 argv[0] = (char *) shell0;
1519 1525 argv[1] = "-c";
↓ open down ↓ |
1367 lines elided |
↑ open up ↑ |
1520 1526 argv[2] = (char *) command;
1521 1527 argv[3] = NULL;
1522 1528 execve(shell, argv, env);
1523 1529 perror(shell);
1524 1530 exit(1);
1525 1531 }
1526 1532
1527 1533 Session *
1528 1534 session_new(void)
1529 1535 {
1530 - int i;
1531 - static int did_init = 0;
1532 - if (!did_init) {
1533 - debug("session_new: init");
1534 - for (i = 0; i < MAX_SESSIONS; i++) {
1535 - sessions[i].used = 0;
1536 - }
1537 - did_init = 1;
1538 - }
1539 - for (i = 0; i < MAX_SESSIONS; i++) {
1540 - Session *s = &sessions[i];
1541 - if (! s->used) {
1542 - memset(s, 0, sizeof(*s));
1543 - s->chanid = -1;
1544 - s->ptyfd = -1;
1545 - s->ttyfd = -1;
1546 - s->used = 1;
1547 - s->self = i;
1548 - s->env = NULL;
1549 - debug("session_new: session %d", i);
1550 - return s;
1551 - }
1552 - }
1553 - return NULL;
1536 + Session *s;
1537 +
1538 + debug("session_new: init");
1539 +
1540 + s = xmalloc(sizeof (*s));
1541 + memset(s, 0, sizeof(*s));
1542 + s->chanid = -1;
1543 + s->ptyfd = -1;
1544 + s->ttyfd = -1;
1545 + s->self = sessions_next_id++;
1546 + s->env = NULL;
1547 + LIST_INSERT_HEAD(&sessions, s, list_entry);
1548 +
1549 + debug("session_new: session %d", s->self);
1550 +
1551 + return (s);
1554 1552 }
1555 1553
1556 1554 static void
1557 1555 session_dump(void)
1558 1556 {
1559 - int i;
1560 - for (i = 0; i < MAX_SESSIONS; i++) {
1561 - Session *s = &sessions[i];
1562 - debug("dump: used %d session %d %p channel %d pid %ld",
1563 - s->used,
1557 + Session *s;
1558 + LIST_FOREACH(s, &sessions, list_entry) {
1559 + debug("dump: session %d %p channel %d pid %ld",
1564 1560 s->self,
1565 1561 s,
1566 1562 s->chanid,
1567 1563 (long)s->pid);
1568 1564 }
1569 1565 }
1570 1566
1571 1567 int
1572 1568 session_open(Authctxt *authctxt, int chanid)
1573 1569 {
1574 1570 Session *s = session_new();
1575 1571 debug("session_open: channel %d", chanid);
1576 1572 if (s == NULL) {
1577 1573 error("no more sessions");
1578 1574 return 0;
1579 1575 }
1580 1576 s->authctxt = authctxt;
1581 1577 s->pw = authctxt->pw;
1582 1578 if (s->pw == NULL)
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
1583 1579 fatal("no user for session %d", s->self);
1584 1580 debug("session_open: session %d: link with channel %d", s->self, chanid);
1585 1581 s->chanid = chanid;
1586 1582 return 1;
1587 1583 }
1588 1584
1589 1585 #ifndef lint
1590 1586 Session *
1591 1587 session_by_tty(char *tty)
1592 1588 {
1593 - int i;
1594 - for (i = 0; i < MAX_SESSIONS; i++) {
1595 - Session *s = &sessions[i];
1596 - if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1597 - debug("session_by_tty: session %d tty %s", i, tty);
1589 + Session *s;
1590 + LIST_FOREACH(s, &sessions, list_entry) {
1591 + if (s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1592 + debug("session_by_tty: session %d tty %s", s->self, tty);
1598 1593 return s;
1599 1594 }
1600 1595 }
1601 1596 debug("session_by_tty: unknown tty %.100s", tty);
1602 1597 session_dump();
1603 1598 return NULL;
1604 1599 }
1605 1600 #endif /* lint */
1606 1601
1607 1602 static Session *
1608 1603 session_by_channel(int id)
1609 1604 {
1610 - int i;
1611 - for (i = 0; i < MAX_SESSIONS; i++) {
1612 - Session *s = &sessions[i];
1613 - if (s->used && s->chanid == id) {
1614 - debug("session_by_channel: session %d channel %d", i, id);
1605 + Session *s;
1606 + LIST_FOREACH(s, &sessions, list_entry) {
1607 + if (s->chanid == id) {
1608 + debug("session_by_channel: session %d channel %d", s->self, id);
1615 1609 return s;
1616 1610 }
1617 1611 }
1618 1612 debug("session_by_channel: unknown channel %d", id);
1619 1613 session_dump();
1620 1614 return NULL;
1621 1615 }
1622 1616
1623 1617 static Session *
1624 1618 session_by_pid(pid_t pid)
1625 1619 {
1626 - int i;
1620 + Session *s;
1627 1621 debug("session_by_pid: pid %ld", (long)pid);
1628 - for (i = 0; i < MAX_SESSIONS; i++) {
1629 - Session *s = &sessions[i];
1630 - if (s->used && s->pid == pid)
1622 + LIST_FOREACH(s, &sessions, list_entry) {
1623 + if (s->pid == pid)
1631 1624 return s;
1632 1625 }
1633 1626 error("session_by_pid: unknown pid %ld", (long)pid);
1634 1627 session_dump();
1635 1628 return NULL;
1636 1629 }
1637 1630
1638 1631 static int
1639 1632 session_window_change_req(Session *s)
1640 1633 {
1641 1634 s->col = packet_get_int();
1642 1635 s->row = packet_get_int();
1643 1636 s->xpixel = packet_get_int();
1644 1637 s->ypixel = packet_get_int();
1645 1638 packet_check_eom();
1646 1639 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1647 1640 return 1;
1648 1641 }
1649 1642
1650 1643 static int
1651 1644 session_pty_req(Session *s)
1652 1645 {
1653 1646 u_int len;
1654 1647 int n_bytes;
1655 1648
1656 1649 if (no_pty_flag) {
1657 1650 debug("Allocating a pty not permitted for this authentication.");
1658 1651 return 0;
1659 1652 }
1660 1653 if (s->ttyfd != -1) {
1661 1654 packet_disconnect("Protocol error: you already have a pty.");
1662 1655 return 0;
1663 1656 }
1664 1657 /* Get the time and hostname when the user last logged in. */
1665 1658 if (options.print_lastlog) {
1666 1659 s->hostname[0] = '\0';
1667 1660 s->last_login_time = get_last_login_time(s->pw->pw_uid,
1668 1661 s->pw->pw_name, s->hostname, sizeof(s->hostname));
1669 1662
1670 1663 /*
1671 1664 * PAM may update the last login date.
1672 1665 *
1673 1666 * Ideally PAM would also show the last login date as a
1674 1667 * PAM_TEXT_INFO conversation message, and then we could just
1675 1668 * always force the use of keyboard-interactive just so we can
1676 1669 * pass any such PAM prompts and messages from the account and
1677 1670 * session stacks, but skip pam_authenticate() if other userauth
1678 1671 * has succeeded and the user's password isn't expired.
1679 1672 *
1680 1673 * Unfortunately this depends on support for keyboard-
1681 1674 * interactive in the client, and support for lastlog messages
1682 1675 * in some PAM module.
1683 1676 *
1684 1677 * As it is Solaris updates the lastlog in PAM, but does
1685 1678 * not show the lastlog date in PAM. If and when this state of
1686 1679 * affairs changes this hack can be reconsidered, and, maybe,
1687 1680 * removed.
1688 1681 *
1689 1682 * So we're stuck with a crude hack: get the lastlog
1690 1683 * time before calling pam_open_session() and store it
1691 1684 * in the Authctxt and then use it here once. After
1692 1685 * that, if the client opens any more pty sessions we'll
1693 1686 * show the last lastlog entry since userauth.
1694 1687 */
1695 1688 if (s->authctxt != NULL && s->authctxt->last_login_time > 0) {
1696 1689 s->last_login_time = s->authctxt->last_login_time;
1697 1690 (void) strlcpy(s->hostname,
1698 1691 s->authctxt->last_login_host,
1699 1692 sizeof(s->hostname));
1700 1693 s->authctxt->last_login_time = 0;
1701 1694 s->authctxt->last_login_host[0] = '\0';
1702 1695 }
1703 1696 }
1704 1697
1705 1698 s->term = packet_get_string(&len);
1706 1699
1707 1700 if (compat20) {
1708 1701 s->col = packet_get_int();
1709 1702 s->row = packet_get_int();
1710 1703 } else {
1711 1704 s->row = packet_get_int();
1712 1705 s->col = packet_get_int();
1713 1706 }
1714 1707 s->xpixel = packet_get_int();
1715 1708 s->ypixel = packet_get_int();
1716 1709
1717 1710 if (strcmp(s->term, "") == 0) {
1718 1711 xfree(s->term);
1719 1712 s->term = NULL;
1720 1713 }
1721 1714
1722 1715 /* Allocate a pty and open it. */
1723 1716 debug("Allocating pty.");
1724 1717 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1725 1718 if (s->term)
1726 1719 xfree(s->term);
1727 1720 s->term = NULL;
1728 1721 s->ptyfd = -1;
1729 1722 s->ttyfd = -1;
1730 1723 error("session_pty_req: session %d alloc failed", s->self);
1731 1724 return 0;
1732 1725 }
1733 1726 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1734 1727
1735 1728 /* for SSH1 the tty modes length is not given */
1736 1729 if (!compat20)
1737 1730 n_bytes = packet_remaining();
1738 1731 tty_parse_modes(s->ttyfd, &n_bytes);
1739 1732
1740 1733 /*
1741 1734 * Add a cleanup function to clear the utmp entry and record logout
1742 1735 * time in case we call fatal() (e.g., the connection gets closed).
1743 1736 */
1744 1737 fatal_add_cleanup(session_pty_cleanup, (void *)s);
1745 1738 pty_setowner(s->pw, s->tty);
1746 1739
1747 1740 /* Set window size from the packet. */
1748 1741 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1749 1742
1750 1743 packet_check_eom();
1751 1744 session_proctitle(s);
1752 1745 return 1;
1753 1746 }
1754 1747
1755 1748 static int
1756 1749 session_subsystem_req(Session *s)
1757 1750 {
1758 1751 struct stat st;
1759 1752 u_int len;
1760 1753 int success = 0;
1761 1754 char *prog, *cmd, *subsys = packet_get_string(&len);
1762 1755 u_int i;
1763 1756
1764 1757 packet_check_eom();
1765 1758 log("subsystem request for %.100s", subsys);
1766 1759
1767 1760 for (i = 0; i < options.num_subsystems; i++) {
1768 1761 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1769 1762 prog = options.subsystem_command[i];
1770 1763 cmd = options.subsystem_args[i];
1771 1764 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
1772 1765 s->is_subsystem = SUBSYSTEM_INT_SFTP;
1773 1766 /*
1774 1767 * We must stat(2) the subsystem before we chroot in
1775 1768 * order to be able to send a proper error message.
1776 1769 */
1777 1770 } else if (chroot_requested(options.chroot_directory)) {
1778 1771 char chdirsub[MAXPATHLEN];
1779 1772
1780 1773 strlcpy(chdirsub, options.chroot_directory,
1781 1774 sizeof (chdirsub));
1782 1775 strlcat(chdirsub, "/", sizeof (chdirsub));
1783 1776 strlcat(chdirsub, prog, sizeof (chdirsub));
1784 1777 if (stat(chdirsub, &st) < 0) {
1785 1778 error("subsystem: cannot stat %s under "
1786 1779 "chroot directory %s: %s", prog,
1787 1780 options.chroot_directory,
1788 1781 strerror(errno));
1789 1782 if (strcmp(subsys, "sftp") == 0)
1790 1783 error("subsystem: please see "
1791 1784 "the Subsystem option in "
1792 1785 "sshd_config(4) for an "
1793 1786 "explanation of '%s'.",
1794 1787 INTERNAL_SFTP_NAME);
1795 1788 break;
1796 1789 }
1797 1790 } else if (stat(prog, &st) < 0) {
1798 1791 error("subsystem: cannot stat %s: %s", prog,
1799 1792 strerror(errno));
1800 1793 break;
1801 1794 } else {
1802 1795 s->is_subsystem = SUBSYSTEM_EXT;
1803 1796 }
1804 1797 debug("subsystem: exec() %s", cmd);
1805 1798 do_exec(s, cmd);
1806 1799 success = 1;
1807 1800 break;
1808 1801 }
1809 1802 }
1810 1803
1811 1804 if (!success)
1812 1805 log("subsystem request for %.100s failed, subsystem not found",
1813 1806 subsys);
1814 1807
1815 1808 xfree(subsys);
1816 1809 return success;
1817 1810 }
1818 1811
1819 1812 /*
1820 1813 * Serve "x11-req" channel request for X11 forwarding for the current session
1821 1814 * channel.
1822 1815 */
1823 1816 static int
1824 1817 session_x11_req(Session *s)
1825 1818 {
1826 1819 int success, fd;
1827 1820 char xauthdir[] = "/tmp/ssh-xauth-XXXXXX";
1828 1821
1829 1822 s->single_connection = packet_get_char();
1830 1823 s->auth_proto = packet_get_string(NULL);
1831 1824 s->auth_data = packet_get_string(NULL);
1832 1825 s->screen = packet_get_int();
1833 1826 packet_check_eom();
1834 1827
1835 1828 success = session_setup_x11fwd(s);
1836 1829 if (!success) {
1837 1830 xfree(s->auth_proto);
1838 1831 xfree(s->auth_data);
1839 1832 s->auth_proto = NULL;
1840 1833 s->auth_data = NULL;
1841 1834 return (success);
1842 1835 }
1843 1836
1844 1837 /*
1845 1838 * Create per session X authority file so that different sessions
1846 1839 * don't contend for one common file. The reason for this is that
1847 1840 * xauth(1) locking doesn't work too well over network filesystems.
1848 1841 *
1849 1842 * If mkdtemp() or open() fails then s->auth_file remains NULL which
1850 1843 * means that we won't set XAUTHORITY variable in child's environment
1851 1844 * and xauth(1) will use the default location for the authority file.
1852 1845 */
1853 1846 if (mkdtemp(xauthdir) != NULL) {
1854 1847 s->auth_file = xmalloc(MAXPATHLEN);
1855 1848 snprintf(s->auth_file, MAXPATHLEN, "%s/xauthfile",
1856 1849 xauthdir);
1857 1850 /*
1858 1851 * we don't want that "creating new authority file" message to
1859 1852 * be printed by xauth(1) so we must create that file
1860 1853 * beforehand.
1861 1854 */
1862 1855 if ((fd = open(s->auth_file, O_CREAT | O_EXCL | O_RDONLY,
1863 1856 S_IRUSR | S_IWUSR)) == -1) {
1864 1857 error("failed to create the temporary X authority "
1865 1858 "file %s: %.100s; will use the default one",
1866 1859 s->auth_file, strerror(errno));
1867 1860 xfree(s->auth_file);
1868 1861 s->auth_file = NULL;
1869 1862 if (rmdir(xauthdir) == -1) {
1870 1863 error("cannot remove xauth directory %s: %.100s",
1871 1864 xauthdir, strerror(errno));
1872 1865 }
1873 1866 } else {
1874 1867 close(fd);
1875 1868 debug("temporary X authority file %s created",
1876 1869 s->auth_file);
1877 1870
1878 1871 /*
1879 1872 * add a cleanup function to remove the temporary
1880 1873 * xauth file in case we call fatal() (e.g., the
1881 1874 * connection gets closed).
1882 1875 */
1883 1876 fatal_add_cleanup(session_xauthfile_cleanup, (void *)s);
1884 1877 }
1885 1878 }
1886 1879 else {
1887 1880 error("failed to create a directory for the temporary X "
1888 1881 "authority file: %.100s; will use the default xauth file",
1889 1882 strerror(errno));
1890 1883 }
1891 1884
1892 1885 return (success);
1893 1886 }
1894 1887
1895 1888 static int
1896 1889 session_shell_req(Session *s)
1897 1890 {
1898 1891 packet_check_eom();
1899 1892 do_exec(s, NULL);
1900 1893 return 1;
1901 1894 }
1902 1895
1903 1896 static int
1904 1897 session_exec_req(Session *s)
1905 1898 {
1906 1899 u_int len;
1907 1900 char *command = packet_get_string(&len);
1908 1901 packet_check_eom();
1909 1902 do_exec(s, command);
1910 1903 xfree(command);
1911 1904 return 1;
1912 1905 }
1913 1906
1914 1907 static int
1915 1908 session_auth_agent_req(Session *s)
1916 1909 {
1917 1910 static int called = 0;
1918 1911 packet_check_eom();
1919 1912 if (no_agent_forwarding_flag) {
1920 1913 debug("session_auth_agent_req: no_agent_forwarding_flag");
1921 1914 return 0;
1922 1915 }
1923 1916 if (called) {
1924 1917 return 0;
1925 1918 } else {
1926 1919 called = 1;
1927 1920 return auth_input_request_forwarding(s->pw);
1928 1921 }
1929 1922 }
1930 1923
1931 1924 static int
1932 1925 session_loc_env_check(char *var, char *val)
1933 1926 {
1934 1927 char *current;
1935 1928 int cat, ret;
1936 1929
1937 1930 if (strcmp(var, "LANG") == 0)
1938 1931 cat = LC_ALL;
1939 1932 else if (strcmp(var, "LC_ALL") == 0)
1940 1933 cat = LC_ALL;
1941 1934 else if (strcmp(var, "LC_CTYPE") == 0)
1942 1935 cat = LC_CTYPE;
1943 1936 else if (strcmp(var, "LC_COLLATE") == 0)
1944 1937 cat = LC_COLLATE;
1945 1938 else if (strcmp(var, "LC_TIME") == 0)
1946 1939 cat = LC_TIME;
1947 1940 else if (strcmp(var, "LC_NUMERIC") == 0)
1948 1941 cat = LC_NUMERIC;
1949 1942 else if (strcmp(var, "LC_MONETARY") == 0)
1950 1943 cat = LC_MONETARY;
1951 1944 else if (strcmp(var, "LC_MESSAGES") == 0)
1952 1945 cat = LC_MESSAGES;
1953 1946
1954 1947 current = setlocale(cat, NULL);
1955 1948
1956 1949 ret = (setlocale(cat, val) != NULL);
1957 1950 (void) setlocale(cat, current);
1958 1951 return (ret);
1959 1952 }
1960 1953
1961 1954 static int
1962 1955 session_env_req(Session *s)
1963 1956 {
1964 1957 Channel *c;
1965 1958 char *var, *val, *e;
1966 1959 char **p;
1967 1960 size_t len;
1968 1961 int ret = 0;
1969 1962
1970 1963 /* Get var/val from the rest of this packet */
1971 1964 var = packet_get_string(NULL);
1972 1965 val = packet_get_string(NULL);
1973 1966
1974 1967 /*
1975 1968 * We'll need the channel ID for the packet_send_debug messages,
1976 1969 * so get it now.
1977 1970 */
1978 1971 if ((c = channel_lookup(s->chanid)) == NULL)
1979 1972 goto done; /* shouldn't happen! */
1980 1973
1981 1974 debug2("Received request for environment variable %s=%s", var, val);
1982 1975
1983 1976 /* For now allow only LANG and LC_* */
1984 1977 if (strcmp(var, "LANG") != 0 && strncmp(var, "LC_", 3) != 0) {
1985 1978 debug2("Rejecting request for environment variable %s", var);
1986 1979 goto done;
1987 1980 }
1988 1981
1989 1982 if (!session_loc_env_check(var, val)) {
1990 1983 packet_send_debug(gettext("Missing locale support for %s=%s"),
1991 1984 var, val);
1992 1985 goto done;
1993 1986 }
1994 1987
1995 1988 packet_send_debug(gettext("Channel %d set: %s=%s"), c->remote_id,
1996 1989 var, val);
1997 1990
1998 1991 /*
1999 1992 * Always append new environment variables without regard to old
2000 1993 * ones being overriden. The way these are actually added to
2001 1994 * the environment of the session process later settings
2002 1995 * override earlier ones; see copy_environment().
2003 1996 */
2004 1997 if (s->env == NULL) {
2005 1998 char **env;
2006 1999
2007 2000 env = xmalloc(sizeof (char **) * 2);
2008 2001 memset(env, 0, sizeof (char **) * 2);
2009 2002
2010 2003 s->env = env;
2011 2004 p = env;
2012 2005 } else {
2013 2006 for (p = s->env; *p != NULL ; p++);
2014 2007
2015 2008 s->env = xrealloc(s->env, (p - s->env + 2) * sizeof (char **));
2016 2009
2017 2010 for (p = s->env; *p != NULL ; p++);
2018 2011 }
2019 2012
2020 2013 len = snprintf(NULL, 0, "%s=%s", var, val);
2021 2014 e = xmalloc(len + 1);
2022 2015 (void) snprintf(e, len + 1, "%s=%s", var, val);
2023 2016
2024 2017 (*p++) = e;
2025 2018 *p = NULL;
2026 2019
2027 2020 ret = 1;
2028 2021
2029 2022 done:
2030 2023 xfree(var);
2031 2024 xfree(val);
2032 2025
2033 2026 return (ret);
2034 2027 }
2035 2028
2036 2029 static void
2037 2030 session_free_env(char ***envp)
2038 2031 {
2039 2032 char **env, **p;
2040 2033
2041 2034 if (envp == NULL || *envp == NULL)
2042 2035 return;
2043 2036
2044 2037 env = *envp;
2045 2038
2046 2039 *envp = NULL;
2047 2040
2048 2041 for (p = env; *p != NULL; p++)
2049 2042 xfree(*p);
2050 2043
2051 2044 xfree(env);
2052 2045 }
2053 2046
2054 2047 int
2055 2048 session_input_channel_req(Channel *c, const char *rtype)
2056 2049 {
2057 2050 int success = 0;
2058 2051 Session *s;
2059 2052
2060 2053 if ((s = session_by_channel(c->self)) == NULL) {
2061 2054 log("session_input_channel_req: no session %d req %.100s",
2062 2055 c->self, rtype);
2063 2056 return 0;
2064 2057 }
2065 2058 debug("session_input_channel_req: session %d req %s", s->self, rtype);
2066 2059
2067 2060 /*
2068 2061 * a session is in LARVAL state until a shell, a command
2069 2062 * or a subsystem is executed
2070 2063 */
2071 2064 if (c->type == SSH_CHANNEL_LARVAL) {
2072 2065 if (strcmp(rtype, "shell") == 0) {
2073 2066 success = session_shell_req(s);
2074 2067 } else if (strcmp(rtype, "exec") == 0) {
2075 2068 success = session_exec_req(s);
2076 2069 } else if (strcmp(rtype, "pty-req") == 0) {
2077 2070 success = session_pty_req(s);
2078 2071 } else if (strcmp(rtype, "x11-req") == 0) {
2079 2072 success = session_x11_req(s);
2080 2073 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
2081 2074 success = session_auth_agent_req(s);
2082 2075 } else if (strcmp(rtype, "subsystem") == 0) {
2083 2076 success = session_subsystem_req(s);
2084 2077 } else if (strcmp(rtype, "env") == 0) {
2085 2078 success = session_env_req(s);
2086 2079 }
2087 2080 }
2088 2081 if (strcmp(rtype, "window-change") == 0) {
2089 2082 success = session_window_change_req(s);
2090 2083 }
2091 2084 return success;
2092 2085 }
2093 2086
2094 2087 void
2095 2088 session_set_fds(Session *s, int fdin, int fdout, int fderr)
2096 2089 {
2097 2090 if (!compat20)
2098 2091 fatal("session_set_fds: called for proto != 2.0");
2099 2092 /*
2100 2093 * now that have a child and a pipe to the child,
2101 2094 * we can activate our channel and register the fd's
2102 2095 */
2103 2096 if (s->chanid == -1)
2104 2097 fatal("no channel for session %d", s->self);
2105 2098 channel_set_fds(s->chanid,
2106 2099 fdout, fdin, fderr,
2107 2100 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
2108 2101 1,
2109 2102 CHAN_SES_WINDOW_DEFAULT);
2110 2103 }
2111 2104
2112 2105 /*
2113 2106 * Function to perform pty cleanup. Also called if we get aborted abnormally
2114 2107 * (e.g., due to a dropped connection).
2115 2108 */
2116 2109 void
2117 2110 session_pty_cleanup2(void *session)
2118 2111 {
2119 2112 Session *s = session;
2120 2113
2121 2114 if (s == NULL) {
2122 2115 error("session_pty_cleanup: no session");
2123 2116 return;
2124 2117 }
2125 2118 if (s->ttyfd == -1)
2126 2119 return;
2127 2120
2128 2121 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
2129 2122
2130 2123 #ifdef USE_PAM
2131 2124 session_do_pam(s, 0);
2132 2125 #endif /* USE_PAM */
2133 2126
2134 2127 /* Record that the user has logged out. */
2135 2128 if (s->pid != 0) {
2136 2129 debug3("Recording SSHv2 channel logout in utmpx/wtmpx");
2137 2130 #ifdef ALTPRIVSEP
2138 2131 altprivsep_record_logout(s->pid);
2139 2132 #endif /* ALTPRIVSEP */
2140 2133 }
2141 2134
2142 2135 /* Release the pseudo-tty. */
2143 2136 if (getuid() == 0)
2144 2137 pty_release(s->tty);
2145 2138
2146 2139 /*
2147 2140 * Close the server side of the socket pairs. We must do this after
2148 2141 * the pty cleanup, so that another process doesn't get this pty
2149 2142 * while we're still cleaning up.
2150 2143 */
2151 2144 if (close(s->ptymaster) < 0)
2152 2145 error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
2153 2146
2154 2147 /* unlink pty from session */
2155 2148 s->ttyfd = -1;
2156 2149 }
2157 2150
2158 2151 void
2159 2152 session_pty_cleanup(void *session)
2160 2153 {
2161 2154 session_pty_cleanup2(session);
2162 2155 }
2163 2156
2164 2157 /*
2165 2158 * We use a different temporary X authority file per every session so we
2166 2159 * should remove those files when fatal() is called.
2167 2160 */
2168 2161 void
2169 2162 session_xauthfile_cleanup(void *session)
2170 2163 {
2171 2164 Session *s = session;
2172 2165
2173 2166 if (s == NULL) {
2174 2167 error("session_xauthfile_cleanup: no session");
2175 2168 return;
2176 2169 }
2177 2170
2178 2171 debug("session_xauthfile_cleanup: session %d removing %s", s->self,
2179 2172 s->auth_file);
2180 2173
2181 2174 if (unlink(s->auth_file) == -1) {
2182 2175 error("session_xauthfile_cleanup: cannot remove xauth file: "
2183 2176 "%.100s", strerror(errno));
2184 2177 return;
2185 2178 }
2186 2179
2187 2180 /* dirname() will modify s->auth_file but that's ok */
2188 2181 if (rmdir(dirname(s->auth_file)) == -1) {
2189 2182 error("session_xauthfile_cleanup: "
2190 2183 "cannot remove xauth directory: %.100s", strerror(errno));
2191 2184 return;
2192 2185 }
2193 2186 }
2194 2187
2195 2188 static char *
2196 2189 sig2name(int sig)
2197 2190 {
2198 2191 #define SSH_SIG(x) if (sig == SIG ## x) return #x
2199 2192 SSH_SIG(ABRT);
2200 2193 SSH_SIG(ALRM);
2201 2194 SSH_SIG(FPE);
2202 2195 SSH_SIG(HUP);
2203 2196 SSH_SIG(ILL);
2204 2197 SSH_SIG(INT);
2205 2198 SSH_SIG(KILL);
2206 2199 SSH_SIG(PIPE);
2207 2200 SSH_SIG(QUIT);
2208 2201 SSH_SIG(SEGV);
2209 2202 SSH_SIG(TERM);
2210 2203 SSH_SIG(USR1);
2211 2204 SSH_SIG(USR2);
2212 2205 #undef SSH_SIG
2213 2206 return "SIG@openssh.com";
2214 2207 }
2215 2208
2216 2209 static void
2217 2210 session_exit_message(Session *s, int status)
2218 2211 {
2219 2212 Channel *c;
2220 2213
2221 2214 if ((c = channel_lookup(s->chanid)) == NULL)
2222 2215 fatal("session_exit_message: session %d: no channel %d",
2223 2216 s->self, s->chanid);
2224 2217 debug("session_exit_message: session %d channel %d pid %ld",
2225 2218 s->self, s->chanid, (long)s->pid);
2226 2219
2227 2220 if (WIFEXITED(status)) {
2228 2221 channel_request_start(s->chanid, "exit-status", 0);
2229 2222 packet_put_int(WEXITSTATUS(status));
2230 2223 packet_send();
2231 2224 } else if (WIFSIGNALED(status)) {
2232 2225 channel_request_start(s->chanid, "exit-signal", 0);
2233 2226 packet_put_cstring(sig2name(WTERMSIG(status)));
2234 2227 #ifdef WCOREDUMP
2235 2228 packet_put_char(WCOREDUMP(status));
2236 2229 #else /* WCOREDUMP */
2237 2230 packet_put_char(0);
2238 2231 #endif /* WCOREDUMP */
2239 2232 packet_put_cstring("");
2240 2233 packet_put_cstring("");
2241 2234 packet_send();
2242 2235 } else {
2243 2236 /* Some weird exit cause. Just exit. */
2244 2237 packet_disconnect("wait returned status %04x.", status);
2245 2238 }
2246 2239
2247 2240 /* Ok to close channel now */
2248 2241 channel_set_wait_for_exit(s->chanid, 0);
2249 2242
2250 2243 /* disconnect channel */
2251 2244 debug("session_exit_message: release channel %d", s->chanid);
2252 2245 channel_cancel_cleanup(s->chanid);
2253 2246 /*
2254 2247 * emulate a write failure with 'chan_write_failed', nobody will be
2255 2248 * interested in data we write.
2256 2249 * Note that we must not call 'chan_read_failed', since there could
2257 2250 * be some more data waiting in the pipe.
2258 2251 */
2259 2252 if (c->ostate != CHAN_OUTPUT_CLOSED)
2260 2253 chan_write_failed(c);
2261 2254 s->chanid = -1;
2262 2255 }
2263 2256
2264 2257 void
2265 2258 session_close(Session *s)
2266 2259 {
2267 2260 debug("session_close: session %d pid %ld", s->self, (long)s->pid);
2268 2261 if (s->ttyfd != -1) {
2269 2262 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
2270 2263 session_pty_cleanup(s);
2271 2264 }
2272 2265 if (s->auth_file != NULL) {
2273 2266 fatal_remove_cleanup(session_xauthfile_cleanup, (void *)s);
2274 2267 session_xauthfile_cleanup(s);
2275 2268 xfree(s->auth_file);
2276 2269 }
2277 2270 if (s->term)
2278 2271 xfree(s->term);
2279 2272 if (s->display)
↓ open down ↓ |
639 lines elided |
↑ open up ↑ |
2280 2273 xfree(s->display);
2281 2274 if (s->auth_display)
2282 2275 xfree(s->auth_display);
2283 2276 if (s->auth_data)
2284 2277 xfree(s->auth_data);
2285 2278 if (s->auth_proto)
2286 2279 xfree(s->auth_proto);
2287 2280 if (s->command)
2288 2281 xfree(s->command);
2289 2282 session_free_env(&s->env);
2290 - s->used = 0;
2283 +
2284 + LIST_REMOVE(s, list_entry);
2291 2285 session_proctitle(s);
2286 + xfree(s);
2292 2287 }
2293 2288
2294 2289 void
2295 2290 session_close_by_pid(pid_t pid, int status)
2296 2291 {
2297 2292 Session *s = session_by_pid(pid);
2298 2293 if (s == NULL) {
2299 2294 debug("session_close_by_pid: no session for pid %ld",
2300 2295 (long)pid);
2301 2296 return;
2302 2297 }
2303 2298 if (s->chanid != -1)
2304 2299 session_exit_message(s, status);
2305 2300 session_close(s);
2306 2301 }
2307 2302
2308 2303 /*
2309 2304 * This is called when a channel dies before the session 'child' itself dies.
2310 2305 * It can happen for example if we exit from an interactive shell before we
2311 2306 * exit from forwarded X11 applications.
2312 2307 */
2313 2308 void
2314 2309 session_close_by_channel(int id, void *arg)
2315 2310 {
2316 2311 Session *s = session_by_channel(id);
2317 2312 if (s == NULL) {
2318 2313 debug("session_close_by_channel: no session for id %d", id);
2319 2314 return;
2320 2315 }
2321 2316 debug("session_close_by_channel: channel %d child %ld",
2322 2317 id, (long)s->pid);
2323 2318 if (s->pid != 0) {
2324 2319 debug("session_close_by_channel: channel %d: has child", id);
2325 2320 /*
2326 2321 * delay detach of session, but release pty, since
2327 2322 * the fd's to the child are already closed
2328 2323 */
2329 2324 if (s->ttyfd != -1) {
2330 2325 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
2331 2326 session_pty_cleanup(s);
2332 2327 }
2333 2328 return;
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
2334 2329 }
2335 2330 /* detach by removing callback */
2336 2331 channel_cancel_cleanup(s->chanid);
2337 2332 s->chanid = -1;
2338 2333 session_close(s);
2339 2334 }
2340 2335
2341 2336 void
2342 2337 session_destroy_all(void (*closefunc)(Session *))
2343 2338 {
2344 - int i;
2345 - for (i = 0; i < MAX_SESSIONS; i++) {
2346 - Session *s = &sessions[i];
2347 - if (s->used) {
2348 - if (closefunc != NULL)
2349 - closefunc(s);
2350 - else
2351 - session_close(s);
2352 - }
2339 + Session *s;
2340 + LIST_FOREACH(s, &sessions, list_entry) {
2341 + if (closefunc != NULL)
2342 + closefunc(s);
2343 + else
2344 + session_close(s);
2353 2345 }
2354 2346 }
2355 2347
2356 2348 static char *
2357 2349 session_tty_list(void)
2358 2350 {
2351 + Session *s;
2359 2352 static char buf[1024];
2360 - int i;
2361 2353 buf[0] = '\0';
2362 - for (i = 0; i < MAX_SESSIONS; i++) {
2363 - Session *s = &sessions[i];
2364 - if (s->used && s->ttyfd != -1) {
2354 + LIST_FOREACH(s, &sessions, list_entry) {
2355 + if (s->ttyfd != -1) {
2365 2356 if (buf[0] != '\0')
2366 2357 strlcat(buf, ",", sizeof buf);
2367 2358 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2368 2359 }
2369 2360 }
2370 2361 if (buf[0] == '\0')
2371 2362 strlcpy(buf, "notty", sizeof buf);
2372 2363 return buf;
2373 2364 }
2374 2365
2375 2366 void
2376 2367 session_proctitle(Session *s)
2377 2368 {
2378 2369 if (s->pw == NULL)
2379 2370 error("no user for session %d", s->self);
2380 2371 else
2381 2372 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2382 2373 }
2383 2374
2384 2375 int
2385 2376 session_setup_x11fwd(Session *s)
2386 2377 {
2387 2378 struct stat st;
2388 2379 char display[512], auth_display[512];
2389 2380 char hostname[MAXHOSTNAMELEN];
2390 2381
2391 2382 if (no_x11_forwarding_flag) {
2392 2383 packet_send_debug("X11 forwarding disabled in user configuration file.");
2393 2384 return 0;
2394 2385 }
2395 2386 if (!options.x11_forwarding) {
2396 2387 debug("X11 forwarding disabled in server configuration file.");
2397 2388 return 0;
2398 2389 }
2399 2390 if (!options.xauth_location ||
2400 2391 (stat(options.xauth_location, &st) == -1)) {
2401 2392 packet_send_debug("No xauth program; cannot forward with spoofing.");
2402 2393 return 0;
2403 2394 }
2404 2395 if (s->display != NULL) {
2405 2396 debug("X11 display already set.");
2406 2397 return 0;
2407 2398 }
2408 2399 if (x11_create_display_inet(options.x11_display_offset,
2409 2400 options.x11_use_localhost, s->single_connection,
2410 2401 &s->display_number) == -1) {
2411 2402 debug("x11_create_display_inet failed.");
2412 2403 return 0;
2413 2404 }
2414 2405
2415 2406 /* Set up a suitable value for the DISPLAY variable. */
2416 2407 if (gethostname(hostname, sizeof(hostname)) < 0)
2417 2408 fatal("gethostname: %.100s", strerror(errno));
2418 2409 /*
2419 2410 * auth_display must be used as the displayname when the
2420 2411 * authorization entry is added with xauth(1). This will be
2421 2412 * different than the DISPLAY string for localhost displays.
2422 2413 */
2423 2414 if (options.x11_use_localhost) {
2424 2415 snprintf(display, sizeof display, "localhost:%u.%u",
2425 2416 s->display_number, s->screen);
2426 2417 snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
2427 2418 s->display_number, s->screen);
2428 2419 s->display = xstrdup(display);
2429 2420 s->auth_display = xstrdup(auth_display);
2430 2421 } else {
2431 2422 #ifdef IPADDR_IN_DISPLAY
2432 2423 struct hostent *he;
2433 2424 struct in_addr my_addr;
2434 2425
2435 2426 he = gethostbyname(hostname);
2436 2427 if (he == NULL) {
2437 2428 error("Can't get IP address for X11 DISPLAY.");
2438 2429 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2439 2430 return 0;
2440 2431 }
2441 2432 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2442 2433 snprintf(display, sizeof display, "%.50s:%u.%u", inet_ntoa(my_addr),
2443 2434 s->display_number, s->screen);
2444 2435 #else
2445 2436 snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
2446 2437 s->display_number, s->screen);
2447 2438 #endif
2448 2439 s->display = xstrdup(display);
2449 2440 s->auth_display = xstrdup(display);
2450 2441 }
2451 2442
2452 2443 return 1;
2453 2444 }
2454 2445
2455 2446 #ifdef USE_PAM
2456 2447 int session_do_pam_conv(int, struct pam_message **,
2457 2448 struct pam_response **, void *);
2458 2449
2459 2450 static struct pam_conv session_pam_conv = {
2460 2451 session_do_pam_conv,
2461 2452 NULL
2462 2453 };
2463 2454
2464 2455 static void
2465 2456 session_do_pam(Session *s, int do_open)
2466 2457 {
2467 2458 int pam_retval;
2468 2459 char *where, *old_tty, *old_tty_copy = NULL;
2469 2460 struct pam_conv old_conv, *old_conv_ptr;
2470 2461
2471 2462 if (!s || !s->authctxt || !s->authctxt->pam || !s->authctxt->pam->h)
2472 2463 return;
2473 2464
2474 2465 /* Save current PAM item values */
2475 2466 where = "getting PAM_CONV";
2476 2467 pam_retval = pam_get_item(s->authctxt->pam->h, PAM_CONV,
2477 2468 (void **) &old_conv_ptr);
2478 2469 if (pam_retval != PAM_SUCCESS)
2479 2470 goto done;
2480 2471 old_conv = *old_conv_ptr;
2481 2472
2482 2473 where = "getting PAM_TTY";
2483 2474 pam_retval = pam_get_item(s->authctxt->pam->h, PAM_TTY,
2484 2475 (void **) &old_tty);
2485 2476 if (pam_retval != PAM_SUCCESS)
2486 2477 goto done;
2487 2478 old_tty_copy = xstrdup(old_tty);
2488 2479
2489 2480 /* Change PAM_TTY and PAM_CONV items */
2490 2481 where = "setting PAM_TTY";
2491 2482 pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, s->tty);
2492 2483 if (pam_retval != PAM_SUCCESS)
2493 2484 goto done;
2494 2485
2495 2486 where = "setting PAM_CONV";
2496 2487 session_pam_conv.appdata_ptr = s;
2497 2488 pam_retval = pam_set_item(s->authctxt->pam->h,
2498 2489 PAM_CONV, &session_pam_conv);
2499 2490 if (pam_retval != PAM_SUCCESS)
2500 2491 goto done;
2501 2492
2502 2493 /* Call pam_open/close_session() */
2503 2494 if (do_open) {
2504 2495 where = "calling pam_open_session()";
2505 2496 pam_retval = pam_open_session(s->authctxt->pam->h, 0);
2506 2497 }
2507 2498 else {
2508 2499 where = "calling pam_close_session()";
2509 2500 pam_retval = pam_close_session(s->authctxt->pam->h, 0);
2510 2501 }
2511 2502
2512 2503 /* Reset PAM_TTY and PAM_CONV items to previous values */
2513 2504 where = "setting PAM_TTY";
2514 2505 pam_retval = pam_set_item(s->authctxt->pam->h, PAM_TTY, old_tty_copy);
2515 2506 if (pam_retval != PAM_SUCCESS)
2516 2507 goto done;
2517 2508
2518 2509 where = "setting PAM_CONV";
2519 2510 pam_retval = pam_set_item(s->authctxt->pam->h, PAM_CONV, &old_conv);
2520 2511 if (pam_retval != PAM_SUCCESS)
2521 2512 goto done;
2522 2513
2523 2514 session_pam_conv.appdata_ptr = NULL;
2524 2515
2525 2516 done:
2526 2517 if (old_tty_copy)
2527 2518 xfree(old_tty_copy);
2528 2519
2529 2520 if (pam_retval == PAM_SUCCESS)
2530 2521 return;
2531 2522
2532 2523 /* fatal()? probably not... */
2533 2524 log("PAM failed[%d] while %s: %s", pam_retval, where,
2534 2525 PAM_STRERROR(s->authctxt->pam->h, pam_retval));
2535 2526 }
2536 2527
2537 2528 int
2538 2529 session_do_pam_conv(int num_prompts,
2539 2530 struct pam_message **prompts,
2540 2531 struct pam_response **resp,
2541 2532 void *app_data)
2542 2533 {
2543 2534 Session *s = (Session *) app_data;
2544 2535
2545 2536 struct pam_response *reply;
2546 2537 int count;
2547 2538 char *prompt;
2548 2539
2549 2540 if (channel_lookup(s->chanid) == NULL)
2550 2541 return PAM_CONV_ERR;
2551 2542
2552 2543 /* PAM will free this later */
2553 2544 reply = xmalloc(num_prompts * sizeof(*reply));
2554 2545
2555 2546 (void) memset(reply, 0, num_prompts * sizeof(*reply));
2556 2547 for (count = 0; count < num_prompts; count++) {
2557 2548 switch(PAM_MSG_MEMBER(prompts, count, msg_style)) {
2558 2549 case PAM_TEXT_INFO:
2559 2550 /* Write to stdout of channel */
2560 2551 prompt = PAM_MSG_MEMBER(prompts, count, msg);
2561 2552 if (prompt != NULL && s->ttyfd != -1) {
2562 2553 debug2("session_do_pam_conv: text info "
2563 2554 "prompt: %s", prompt);
2564 2555 (void) write(s->ttyfd, prompt, strlen(prompt));
2565 2556 (void) write(s->ttyfd, "\n", 1);
2566 2557 }
2567 2558 reply[count].resp = xstrdup("");
2568 2559 reply[count].resp_retcode = PAM_SUCCESS;
2569 2560 break;
2570 2561 case PAM_ERROR_MSG:
2571 2562 /* Write to stderr of channel */
2572 2563 prompt = PAM_MSG_MEMBER(prompts, count, msg);
2573 2564 if (prompt != NULL && s->ttyfd != -1) {
2574 2565 debug2("session_do_pam_conv: error "
2575 2566 "prompt: %s", prompt);
2576 2567 (void) write(s->ttyfd, prompt, strlen(prompt));
2577 2568 (void) write(s->ttyfd, "\n", 1);
2578 2569 }
2579 2570 reply[count].resp = xstrdup("");
2580 2571 reply[count].resp_retcode = PAM_SUCCESS;
2581 2572 break;
2582 2573 case PAM_PROMPT_ECHO_ON:
2583 2574 case PAM_PROMPT_ECHO_OFF:
2584 2575 /*
2585 2576 * XXX Someday add support for echo on/off prompts
2586 2577 * here on sessions with ttys.
2587 2578 */
2588 2579 default:
2589 2580 xfree(reply);
2590 2581 return PAM_CONV_ERR;
2591 2582 }
2592 2583 }
2593 2584
2594 2585 *resp = reply;
2595 2586
2596 2587 return PAM_SUCCESS;
2597 2588 }
2598 2589 #endif /* USE_PAM */
2599 2590
2600 2591 static void
2601 2592 do_authenticated2(Authctxt *authctxt)
2602 2593 {
2603 2594 server_loop2(authctxt);
2604 2595 }
2605 2596
2606 2597 /*
2607 2598 * Drop the privileges. We need this for the in-process SFTP server only. For
2608 2599 * the shell and the external subsystem the exec(2) call will do the P = E = I
2609 2600 * assignment itself. Never change the privileges if the connecting user is
2610 2601 * root. See privileges(5) if the terminology used here is not known to you.
2611 2602 */
2612 2603 static void
2613 2604 drop_privs(uid_t uid)
2614 2605 {
2615 2606 priv_set_t *priv_inherit;
2616 2607
2617 2608 /* If root is connecting we are done. */
2618 2609 if (uid == 0)
2619 2610 return;
2620 2611
2621 2612 if ((priv_inherit = priv_allocset()) == NULL)
2622 2613 fatal("priv_allocset: %s", strerror(errno));
2623 2614 if (getppriv(PRIV_INHERITABLE, priv_inherit) != 0)
2624 2615 fatal("getppriv: %s", strerror(errno));
2625 2616
2626 2617 /*
2627 2618 * This will limit E as well. Note that before this P was a
2628 2619 * superset of I, see permanently_set_uid().
2629 2620 */
2630 2621 if (setppriv(PRIV_SET, PRIV_PERMITTED, priv_inherit) == -1)
2631 2622 fatal("setppriv: %s", strerror(errno));
2632 2623
2633 2624 priv_freeset(priv_inherit);
2634 2625
2635 2626 /*
2636 2627 * By manipulating the P set above we entered a PA mode which we
2637 2628 * do not need to retain in.
2638 2629 */
2639 2630 if (setpflags(PRIV_AWARE, 0) == -1)
2640 2631 fatal("setpflags: %s", strerror(errno));
2641 2632 }
↓ open down ↓ |
267 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX