Annotation of src/usr.bin/ssh/ssh.c, Revision 1.288
1.288 ! stevesk 1: /* $OpenBSD: ssh.c,v 1.287 2006/07/12 22:42:32 stevesk Exp $ */
1.1 deraadt 2: /*
1.32 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Ssh client program. This program can be used to log into a remote machine.
7: * The software supports strong authentication, encryption, and forwarding
8: * of X11, TCP/IP, and authentication connections.
9: *
1.64 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
16: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.202 markus 17: * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
1.64 deraadt 18: *
19: * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20: * in Canada (German citizen).
21: *
22: * Redistribution and use in source and binary forms, with or without
23: * modification, are permitted provided that the following conditions
24: * are met:
25: * 1. Redistributions of source code must retain the above copyright
26: * notice, this list of conditions and the following disclaimer.
27: * 2. Redistributions in binary form must reproduce the above copyright
28: * notice, this list of conditions and the following disclaimer in the
29: * documentation and/or other materials provided with the distribution.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 deraadt 41: */
1.1 deraadt 42:
43: #include "includes.h"
1.259 stevesk 44:
45: #include <sys/resource.h>
1.260 stevesk 46: #include <sys/ioctl.h>
1.262 stevesk 47: #include <sys/types.h>
1.280 stevesk 48: #include <sys/socket.h>
1.262 stevesk 49: #include <sys/un.h>
1.264 stevesk 50: #include <sys/stat.h>
1.258 stevesk 51:
1.265 stevesk 52: #include <ctype.h>
1.285 stevesk 53: #include <errno.h>
1.281 stevesk 54: #include <fcntl.h>
1.286 stevesk 55: #include <netdb.h>
1.258 stevesk 56: #include <paths.h>
1.279 stevesk 57: #include <pwd.h>
1.263 stevesk 58: #include <signal.h>
1.287 stevesk 59: #include <stddef.h>
1.288 ! stevesk 60: #include <unistd.h>
1.49 markus 61:
62: #include <openssl/evp.h>
1.72 markus 63: #include <openssl/err.h>
1.1 deraadt 64:
1.84 markus 65: #include "ssh.h"
66: #include "ssh1.h"
67: #include "ssh2.h"
68: #include "compat.h"
69: #include "cipher.h"
1.1 deraadt 70: #include "xmalloc.h"
71: #include "packet.h"
72: #include "buffer.h"
1.214 djm 73: #include "bufaux.h"
1.123 markus 74: #include "channels.h"
1.49 markus 75: #include "key.h"
1.58 markus 76: #include "authfd.h"
1.49 markus 77: #include "authfile.h"
1.83 markus 78: #include "pathnames.h"
1.214 djm 79: #include "dispatch.h"
1.81 markus 80: #include "clientloop.h"
1.84 markus 81: #include "log.h"
82: #include "readconf.h"
83: #include "sshconnect.h"
84: #include "misc.h"
1.95 markus 85: #include "kex.h"
86: #include "mac.h"
1.213 deraadt 87: #include "sshpty.h"
1.212 djm 88: #include "match.h"
1.214 djm 89: #include "msg.h"
90: #include "monitor_fdpass.h"
1.225 dtucker 91: #include "uidswap.h"
1.278 stevesk 92: #include "version.h"
1.49 markus 93:
1.127 markus 94: #ifdef SMARTCARD
95: #include "scard.h"
1.137 jakob 96: #endif
1.127 markus 97:
1.49 markus 98: extern char *__progname;
1.1 deraadt 99:
1.31 markus 100: /* Flag indicating whether debug mode is on. This can be set on the command line. */
1.1 deraadt 101: int debug_flag = 0;
102:
1.46 markus 103: /* Flag indicating whether a tty should be allocated */
1.1 deraadt 104: int tty_flag = 0;
1.79 markus 105: int no_tty_flag = 0;
106: int force_tty_flag = 0;
1.1 deraadt 107:
1.45 markus 108: /* don't exec a shell */
109: int no_shell_flag = 0;
110:
1.33 markus 111: /*
112: * Flag indicating that nothing should be read from stdin. This can be set
113: * on the command line.
114: */
1.1 deraadt 115: int stdin_null_flag = 0;
116:
1.33 markus 117: /*
118: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 119: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 120: * background.
121: */
1.1 deraadt 122: int fork_after_authentication_flag = 0;
123:
1.33 markus 124: /*
125: * General data structure for command line options and options configurable
126: * in configuration files. See readconf.h.
127: */
1.1 deraadt 128: Options options;
129:
1.139 markus 130: /* optional user configfile */
131: char *config = NULL;
132:
1.33 markus 133: /*
134: * Name of the host we are connecting to. This is the name given on the
135: * command line, or the HostName specified for the user-supplied name in a
136: * configuration file.
137: */
1.1 deraadt 138: char *host;
139:
1.22 provos 140: /* socket address the host resolves to */
1.37 markus 141: struct sockaddr_storage hostaddr;
1.1 deraadt 142:
1.112 markus 143: /* Private host keys. */
1.173 markus 144: Sensitive sensitive_data;
1.1 deraadt 145:
1.10 dugsong 146: /* Original real UID. */
147: uid_t original_real_uid;
1.177 markus 148: uid_t original_effective_uid;
1.1 deraadt 149:
1.45 markus 150: /* command to be executed */
151: Buffer command;
152:
1.85 djm 153: /* Should we execute a command or invoke a subsystem? */
154: int subsystem_flag = 0;
155:
1.170 markus 156: /* # of replies received for global requests */
157: static int client_global_request_id = 0;
158:
1.186 djm 159: /* pid of proxycommand child process */
160: pid_t proxy_command_pid = 0;
161:
1.214 djm 162: /* fd to control socket */
163: int control_fd = -1;
164:
1.229 djm 165: /* Multiplexing control command */
1.240 djm 166: static u_int mux_command = 0;
1.229 djm 167:
1.214 djm 168: /* Only used in control client mode */
169: volatile sig_atomic_t control_client_terminate = 0;
170: u_int control_server_pid = 0;
171:
1.1 deraadt 172: /* Prints a help message to the user. This function never returns. */
173:
1.126 itojun 174: static void
1.93 itojun 175: usage(void)
1.1 deraadt 176: {
1.208 markus 177: fprintf(stderr,
1.230 jmc 178: "usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
1.251 jmc 179: " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
1.233 jmc 180: " [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
1.232 djm 181: " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
1.233 jmc 182: " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
1.277 stevesk 183: " [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
1.208 markus 184: );
1.257 dtucker 185: exit(255);
1.1 deraadt 186: }
187:
1.126 itojun 188: static int ssh_session(void);
189: static int ssh_session2(void);
190: static void load_public_identity_files(void);
1.214 djm 191: static void control_client(const char *path);
1.45 markus 192:
1.32 deraadt 193: /*
194: * Main program for the ssh client.
195: */
1.2 provos 196: int
197: main(int ac, char **av)
1.1 deraadt 198: {
1.178 markus 199: int i, opt, exit_status;
1.205 markus 200: char *p, *cp, *line, buf[256];
1.31 markus 201: struct stat st;
1.98 markus 202: struct passwd *pw;
1.45 markus 203: int dummy;
1.144 stevesk 204: extern int optind, optreset;
205: extern char *optarg;
1.244 djm 206: struct servent *sp;
1.232 djm 207: Forward fwd;
1.250 djm 208:
209: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
210: sanitise_stdfd();
1.31 markus 211:
1.33 markus 212: /*
213: * Save the original real uid. It will be needed later (uid-swapping
214: * may clobber the real uid).
215: */
1.31 markus 216: original_real_uid = getuid();
217: original_effective_uid = geteuid();
218:
1.184 stevesk 219: /*
220: * Use uid-swapping to give up root privileges for the duration of
221: * option processing. We will re-instantiate the rights when we are
222: * ready to create the privileged port, and will permanently drop
223: * them when the port has been created (actually, when the connection
224: * has been made, as we may need to create the port several times).
225: */
226: PRIV_END;
227:
1.31 markus 228: /* If we are installed setuid root be careful to not drop core. */
229: if (original_real_uid != original_effective_uid) {
230: struct rlimit rlim;
231: rlim.rlim_cur = rlim.rlim_max = 0;
232: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
233: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 234: }
1.107 markus 235: /* Get user data. */
236: pw = getpwuid(original_real_uid);
237: if (!pw) {
1.191 itojun 238: logit("You don't exist, go away!");
1.257 dtucker 239: exit(255);
1.107 markus 240: }
241: /* Take a copy of the returned structure. */
242: pw = pwcopy(pw);
1.31 markus 243:
1.33 markus 244: /*
245: * Set our umask to something reasonable, as some files are created
246: * with the default umask. This will make them world-readable but
247: * writable only by the owner, which is ok for all files for which we
248: * don't set the modes explicitly.
249: */
1.31 markus 250: umask(022);
251:
252: /* Initialize option structure to indicate that no values have been set. */
253: initialize_options(&options);
254:
255: /* Parse command-line arguments. */
256: host = NULL;
257:
1.266 djm 258: again:
1.128 fgsch 259: while ((opt = getopt(ac, av,
1.255 reyk 260: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVw:XY")) != -1) {
1.31 markus 261: switch (opt) {
1.91 jakob 262: case '1':
263: options.protocol = SSH_PROTO_1;
264: break;
1.47 markus 265: case '2':
266: options.protocol = SSH_PROTO_2;
267: break;
1.37 markus 268: case '4':
1.196 djm 269: options.address_family = AF_INET;
1.37 markus 270: break;
271: case '6':
1.196 djm 272: options.address_family = AF_INET6;
1.37 markus 273: break;
1.31 markus 274: case 'n':
275: stdin_null_flag = 1;
276: break;
277: case 'f':
278: fork_after_authentication_flag = 1;
279: stdin_null_flag = 1;
280: break;
281: case 'x':
282: options.forward_x11 = 0;
283: break;
284: case 'X':
285: options.forward_x11 = 1;
286: break;
1.202 markus 287: case 'Y':
288: options.forward_x11 = 1;
289: options.forward_x11_trusted = 1;
290: break;
1.31 markus 291: case 'g':
292: options.gateway_ports = 1;
293: break;
1.229 djm 294: case 'O':
295: if (strcmp(optarg, "check") == 0)
296: mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
297: else if (strcmp(optarg, "exit") == 0)
298: mux_command = SSHMUX_COMMAND_TERMINATE;
299: else
300: fatal("Invalid multiplex command.");
301: break;
1.183 stevesk 302: case 'P': /* deprecated */
1.31 markus 303: options.use_privileged_port = 0;
304: break;
305: case 'a':
306: options.forward_agent = 0;
1.53 markus 307: break;
308: case 'A':
309: options.forward_agent = 1;
1.31 markus 310: break;
311: case 'k':
1.204 dtucker 312: options.gss_deleg_creds = 0;
1.31 markus 313: break;
314: case 'i':
315: if (stat(optarg, &st) < 0) {
1.128 fgsch 316: fprintf(stderr, "Warning: Identity file %s "
1.231 otto 317: "not accessible: %s.\n", optarg,
318: strerror(errno));
1.31 markus 319: break;
320: }
1.128 fgsch 321: if (options.num_identity_files >=
322: SSH_MAX_IDENTITY_FILES)
323: fatal("Too many identity files specified "
324: "(max %d)", SSH_MAX_IDENTITY_FILES);
325: options.identity_files[options.num_identity_files++] =
326: xstrdup(optarg);
1.31 markus 327: break;
1.127 markus 328: case 'I':
329: #ifdef SMARTCARD
1.133 markus 330: options.smartcard_device = xstrdup(optarg);
1.137 jakob 331: #else
1.127 markus 332: fprintf(stderr, "no support for smartcards.\n");
1.137 jakob 333: #endif
1.127 markus 334: break;
1.31 markus 335: case 't':
1.79 markus 336: if (tty_flag)
337: force_tty_flag = 1;
1.31 markus 338: tty_flag = 1;
339: break;
340: case 'v':
1.197 markus 341: if (debug_flag == 0) {
1.66 markus 342: debug_flag = 1;
343: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 344: } else {
345: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
346: options.log_level++;
1.66 markus 347: break;
1.197 markus 348: }
1.227 deraadt 349: /* FALLTHROUGH */
1.31 markus 350: case 'V':
1.209 markus 351: fprintf(stderr, "%s, %s\n",
352: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 353: if (opt == 'V')
354: exit(0);
355: break;
1.255 reyk 356: case 'w':
1.256 reyk 357: if (options.tun_open == -1)
358: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 359: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 360: if (options.tun_local == SSH_TUNID_ERR) {
1.255 reyk 361: fprintf(stderr, "Bad tun device '%s'\n", optarg);
1.257 dtucker 362: exit(255);
1.255 reyk 363: }
364: break;
1.31 markus 365: case 'q':
366: options.log_level = SYSLOG_LEVEL_QUIET;
367: break;
368: case 'e':
369: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 370: (u_char) optarg[1] >= 64 &&
371: (u_char) optarg[1] < 128)
1.78 markus 372: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 373: else if (strlen(optarg) == 1)
1.78 markus 374: options.escape_char = (u_char) optarg[0];
1.31 markus 375: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 376: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 377: else {
1.128 fgsch 378: fprintf(stderr, "Bad escape character '%s'.\n",
379: optarg);
1.257 dtucker 380: exit(255);
1.31 markus 381: }
382: break;
383: case 'c':
1.49 markus 384: if (ciphers_valid(optarg)) {
385: /* SSH2 only */
386: options.ciphers = xstrdup(optarg);
1.224 markus 387: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 388: } else {
389: /* SSH1 only */
1.74 markus 390: options.cipher = cipher_number(optarg);
391: if (options.cipher == -1) {
1.128 fgsch 392: fprintf(stderr,
393: "Unknown cipher type '%s'\n",
394: optarg);
1.257 dtucker 395: exit(255);
1.49 markus 396: }
1.128 fgsch 397: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 398: options.ciphers = "3des-cbc";
1.128 fgsch 399: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 400: options.ciphers = "blowfish-cbc";
1.128 fgsch 401: else
1.74 markus 402: options.ciphers = (char *)-1;
1.95 markus 403: }
404: break;
405: case 'm':
406: if (mac_valid(optarg))
407: options.macs = xstrdup(optarg);
408: else {
1.128 fgsch 409: fprintf(stderr, "Unknown mac type '%s'\n",
410: optarg);
1.257 dtucker 411: exit(255);
1.31 markus 412: }
413: break;
1.214 djm 414: case 'M':
1.242 djm 415: if (options.control_master == SSHCTL_MASTER_YES)
416: options.control_master = SSHCTL_MASTER_ASK;
417: else
418: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 419: break;
1.31 markus 420: case 'p':
1.113 stevesk 421: options.port = a2port(optarg);
422: if (options.port == 0) {
1.109 markus 423: fprintf(stderr, "Bad port '%s'\n", optarg);
1.257 dtucker 424: exit(255);
1.109 markus 425: }
1.31 markus 426: break;
427: case 'l':
428: options.user = optarg;
429: break;
1.141 stevesk 430:
431: case 'L':
1.232 djm 432: if (parse_forward(&fwd, optarg))
433: add_local_forward(&options, &fwd);
434: else {
1.128 fgsch 435: fprintf(stderr,
1.232 djm 436: "Bad local forwarding specification '%s'\n",
1.128 fgsch 437: optarg);
1.257 dtucker 438: exit(255);
1.31 markus 439: }
1.232 djm 440: break;
441:
442: case 'R':
443: if (parse_forward(&fwd, optarg)) {
444: add_remote_forward(&options, &fwd);
445: } else {
1.128 fgsch 446: fprintf(stderr,
1.232 djm 447: "Bad remote forwarding specification "
448: "'%s'\n", optarg);
1.257 dtucker 449: exit(255);
1.31 markus 450: }
451: break;
1.108 markus 452:
453: case 'D':
1.232 djm 454: cp = p = xstrdup(optarg);
455: memset(&fwd, '\0', sizeof(fwd));
456: fwd.connect_host = "socks";
457: if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
458: fprintf(stderr, "Bad dynamic forwarding "
459: "specification '%.100s'\n", optarg);
1.257 dtucker 460: exit(255);
1.232 djm 461: }
462: if (cp != NULL) {
463: fwd.listen_port = a2port(cp);
464: fwd.listen_host = cleanhostname(fwd.listen_host);
465: } else {
466: fwd.listen_port = a2port(fwd.listen_host);
1.249 djm 467: fwd.listen_host = NULL;
1.232 djm 468: }
469:
470: if (fwd.listen_port == 0) {
1.128 fgsch 471: fprintf(stderr, "Bad dynamic port '%s'\n",
472: optarg);
1.257 dtucker 473: exit(255);
1.109 markus 474: }
1.232 djm 475: add_local_forward(&options, &fwd);
476: xfree(p);
1.108 markus 477: break;
478:
1.31 markus 479: case 'C':
480: options.compression = 1;
481: break;
1.45 markus 482: case 'N':
483: no_shell_flag = 1;
484: no_tty_flag = 1;
485: break;
486: case 'T':
487: no_tty_flag = 1;
488: break;
1.31 markus 489: case 'o':
490: dummy = 1;
1.205 markus 491: line = xstrdup(optarg);
1.128 fgsch 492: if (process_config_line(&options, host ? host : "",
1.205 markus 493: line, "command-line", 0, &dummy) != 0)
1.257 dtucker 494: exit(255);
1.205 markus 495: xfree(line);
1.31 markus 496: break;
1.85 djm 497: case 's':
498: subsystem_flag = 1;
1.117 markus 499: break;
1.214 djm 500: case 'S':
501: if (options.control_path != NULL)
502: free(options.control_path);
503: options.control_path = xstrdup(optarg);
504: break;
1.117 markus 505: case 'b':
506: options.bind_address = optarg;
1.85 djm 507: break;
1.139 markus 508: case 'F':
509: config = optarg;
510: break;
1.31 markus 511: default:
512: usage();
1.1 deraadt 513: }
1.31 markus 514: }
515:
1.128 fgsch 516: ac -= optind;
517: av += optind;
518:
519: if (ac > 0 && !host && **av != '-') {
1.188 markus 520: if (strrchr(*av, '@')) {
1.128 fgsch 521: p = xstrdup(*av);
1.188 markus 522: cp = strrchr(p, '@');
1.128 fgsch 523: if (cp == NULL || cp == p)
524: usage();
525: options.user = p;
526: *cp = '\0';
527: host = ++cp;
528: } else
529: host = *av;
1.189 millert 530: if (ac > 1) {
531: optind = optreset = 1;
1.128 fgsch 532: goto again;
533: }
1.189 millert 534: ac--, av++;
1.128 fgsch 535: }
536:
1.31 markus 537: /* Check that we got a host name. */
538: if (!host)
539: usage();
540:
1.54 markus 541: SSLeay_add_all_algorithms();
1.72 markus 542: ERR_load_crypto_strings();
1.31 markus 543:
544: /* Initialize the command to execute on remote host. */
545: buffer_init(&command);
1.1 deraadt 546:
1.33 markus 547: /*
548: * Save the command to execute on the remote host in a buffer. There
549: * is no limit on the length of the command, except by the maximum
550: * packet size. Also sets the tty flag if there is no command.
551: */
1.128 fgsch 552: if (!ac) {
1.31 markus 553: /* No command specified - execute shell on a tty. */
554: tty_flag = 1;
1.85 djm 555: if (subsystem_flag) {
1.128 fgsch 556: fprintf(stderr,
557: "You must specify a subsystem to invoke.\n");
1.85 djm 558: usage();
559: }
1.31 markus 560: } else {
1.128 fgsch 561: /* A command has been specified. Store it into the buffer. */
562: for (i = 0; i < ac; i++) {
563: if (i)
1.31 markus 564: buffer_append(&command, " ", 1);
565: buffer_append(&command, av[i], strlen(av[i]));
566: }
567: }
568:
569: /* Cannot fork to background if no command. */
1.63 markus 570: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 571: fatal("Cannot fork into background without a command to execute.");
572:
573: /* Allocate a tty by default if no command specified. */
574: if (buffer_len(&command) == 0)
575: tty_flag = 1;
576:
1.180 deraadt 577: /* Force no tty */
1.75 markus 578: if (no_tty_flag)
579: tty_flag = 0;
1.31 markus 580: /* Do not allocate a tty if stdin is not a tty. */
1.236 djm 581: if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
1.31 markus 582: if (tty_flag)
1.191 itojun 583: logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 584: tty_flag = 0;
585: }
1.45 markus 586:
1.101 markus 587: /*
588: * Initialize "log" output. Since we are the client all output
589: * actually goes to stderr.
590: */
1.111 markus 591: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
592: SYSLOG_FACILITY_USER, 1);
1.31 markus 593:
1.139 markus 594: /*
595: * Read per-user configuration file. Ignore the system wide config
596: * file if the user specifies a config file on the command line.
597: */
598: if (config != NULL) {
1.211 djm 599: if (!read_config_file(config, host, &options, 0))
1.142 stevesk 600: fatal("Can't open user config file %.100s: "
601: "%.100s", config, strerror(errno));
1.139 markus 602: } else {
603: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
604: _PATH_SSH_USER_CONFFILE);
1.210 djm 605: (void)read_config_file(buf, host, &options, 1);
1.139 markus 606:
1.145 markus 607: /* Read systemwide configuration file after use config. */
1.223 deraadt 608: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
1.210 djm 609: &options, 0);
1.139 markus 610: }
1.31 markus 611:
612: /* Fill configuration defaults. */
613: fill_default_options(&options);
614:
1.196 djm 615: channel_set_af(options.address_family);
616:
1.31 markus 617: /* reinit */
1.101 markus 618: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 619:
620: if (options.user == NULL)
621: options.user = xstrdup(pw->pw_name);
622:
623: if (options.hostname != NULL)
624: host = options.hostname;
1.195 markus 625:
626: /* force lowercase for hostkey matching */
627: if (options.host_key_alias != NULL) {
628: for (p = options.host_key_alias; *p; p++)
629: if (isupper(*p))
1.271 deraadt 630: *p = (char)tolower(*p);
1.195 markus 631: }
1.190 markus 632:
1.246 djm 633: /* Get default port if port has not been set. */
634: if (options.port == 0) {
635: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
636: options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
637: }
638:
1.190 markus 639: if (options.proxy_command != NULL &&
640: strcmp(options.proxy_command, "none") == 0)
641: options.proxy_command = NULL;
1.245 djm 642: if (options.control_path != NULL &&
643: strcmp(options.control_path, "none") == 0)
644: options.control_path = NULL;
1.31 markus 645:
1.214 djm 646: if (options.control_path != NULL) {
1.275 djm 647: char thishost[NI_MAXHOST];
1.261 djm 648:
1.275 djm 649: if (gethostname(thishost, sizeof(thishost)) == -1)
1.261 djm 650: fatal("gethostname: %s", strerror(errno));
1.241 djm 651: snprintf(buf, sizeof(buf), "%d", options.port);
652: cp = tilde_expand_filename(options.control_path,
653: original_real_uid);
654: options.control_path = percent_expand(cp, "p", buf, "h", host,
1.275 djm 655: "r", options.user, "l", thishost, (char *)NULL);
1.241 djm 656: xfree(cp);
1.214 djm 657: }
1.240 djm 658: if (mux_command != 0 && options.control_path == NULL)
659: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 660: if (options.control_path != NULL)
1.237 jakob 661: control_client(options.control_path);
1.214 djm 662:
1.77 markus 663: /* Open a connection to the remote host. */
1.203 djm 664: if (ssh_connect(host, &hostaddr, options.port,
1.196 djm 665: options.address_family, options.connection_attempts,
1.177 markus 666: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 667: options.proxy_command) != 0)
1.257 dtucker 668: exit(255);
1.31 markus 669:
1.33 markus 670: /*
671: * If we successfully made the connection, load the host private key
672: * in case we will need it later for combined rsa-rhosts
673: * authentication. This must be done before releasing extra
674: * privileges, because the file is only readable by root.
1.174 markus 675: * If we cannot access the private keys, load the public keys
676: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 677: */
1.112 markus 678: sensitive_data.nkeys = 0;
679: sensitive_data.keys = NULL;
1.173 markus 680: sensitive_data.external_keysign = 0;
1.178 markus 681: if (options.rhosts_rsa_authentication ||
682: options.hostbased_authentication) {
1.112 markus 683: sensitive_data.nkeys = 3;
1.274 deraadt 684: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 685: sizeof(Key));
1.177 markus 686:
687: PRIV_START;
1.112 markus 688: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276 dtucker 689: _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.112 markus 690: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
1.276 dtucker 691: _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.112 markus 692: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
1.276 dtucker 693: _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.177 markus 694: PRIV_END;
1.173 markus 695:
1.181 markus 696: if (options.hostbased_authentication == 1 &&
697: sensitive_data.keys[0] == NULL &&
1.173 markus 698: sensitive_data.keys[1] == NULL &&
699: sensitive_data.keys[2] == NULL) {
700: sensitive_data.keys[1] = key_load_public(
701: _PATH_HOST_DSA_KEY_FILE, NULL);
702: sensitive_data.keys[2] = key_load_public(
703: _PATH_HOST_RSA_KEY_FILE, NULL);
704: sensitive_data.external_keysign = 1;
705: }
1.31 markus 706: }
1.33 markus 707: /*
708: * Get rid of any extra privileges that we may have. We will no
709: * longer need them. Also, extra privileges could make it very hard
710: * to read identity files and other non-world-readable files from the
711: * user's home directory if it happens to be on a NFS volume where
712: * root is mapped to nobody.
713: */
1.225 dtucker 714: if (original_effective_uid == 0) {
715: PRIV_START;
716: permanently_set_uid(pw);
717: }
1.31 markus 718:
1.33 markus 719: /*
720: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 721: * directory if it doesn't already exist.
1.33 markus 722: */
1.138 jakob 723: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 724: if (stat(buf, &st) < 0)
1.57 djm 725: if (mkdir(buf, 0700) < 0)
1.31 markus 726: error("Could not create directory '%.200s'.", buf);
727:
1.104 markus 728: /* load options.identity_files */
729: load_public_identity_files();
730:
731: /* Expand ~ in known host file names. */
732: /* XXX mem-leaks: */
1.72 markus 733: options.system_hostfile =
734: tilde_expand_filename(options.system_hostfile, original_real_uid);
735: options.user_hostfile =
736: tilde_expand_filename(options.user_hostfile, original_real_uid);
737: options.system_hostfile2 =
738: tilde_expand_filename(options.system_hostfile2, original_real_uid);
739: options.user_hostfile2 =
740: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 741:
742: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 743:
744: /* Log into the remote system. This never returns if the login fails. */
1.173 markus 745: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 746:
1.112 markus 747: /* We no longer need the private host keys. Clear them now. */
748: if (sensitive_data.nkeys != 0) {
749: for (i = 0; i < sensitive_data.nkeys; i++) {
750: if (sensitive_data.keys[i] != NULL) {
751: /* Destroys contents safely */
752: debug3("clear hostkey %d", i);
753: key_free(sensitive_data.keys[i]);
754: sensitive_data.keys[i] = NULL;
755: }
756: }
757: xfree(sensitive_data.keys);
1.134 markus 758: }
759: for (i = 0; i < options.num_identity_files; i++) {
760: if (options.identity_files[i]) {
761: xfree(options.identity_files[i]);
762: options.identity_files[i] = NULL;
763: }
764: if (options.identity_keys[i]) {
765: key_free(options.identity_keys[i]);
766: options.identity_keys[i] = NULL;
767: }
1.112 markus 768: }
1.31 markus 769:
1.45 markus 770: exit_status = compat20 ? ssh_session2() : ssh_session();
771: packet_close();
1.186 djm 772:
1.214 djm 773: if (options.control_path != NULL && control_fd != -1)
774: unlink(options.control_path);
775:
1.186 djm 776: /*
1.203 djm 777: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 778: * case it hangs and instead rely on init to reap the child
779: */
780: if (proxy_command_pid > 1)
781: kill(proxy_command_pid, SIGHUP);
782:
1.45 markus 783: return exit_status;
784: }
785:
1.126 itojun 786: static void
1.70 markus 787: ssh_init_forwarding(void)
788: {
1.86 markus 789: int success = 0;
1.70 markus 790: int i;
1.86 markus 791:
1.70 markus 792: /* Initiate local TCP/IP port forwardings. */
793: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 794: debug("Local connections to %.200s:%d forwarded to remote "
795: "address %.200s:%d",
1.234 deraadt 796: (options.local_forwards[i].listen_host == NULL) ?
797: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 798: options.local_forwards[i].listen_host,
799: options.local_forwards[i].listen_port,
800: options.local_forwards[i].connect_host,
801: options.local_forwards[i].connect_port);
1.158 markus 802: success += channel_setup_local_fwd_listener(
1.232 djm 803: options.local_forwards[i].listen_host,
804: options.local_forwards[i].listen_port,
805: options.local_forwards[i].connect_host,
806: options.local_forwards[i].connect_port,
1.70 markus 807: options.gateway_ports);
808: }
1.283 markus 809: if (i > 0 && success != i && options.exit_on_forward_failure)
810: fatal("Could not request local forwarding.");
1.86 markus 811: if (i > 0 && success == 0)
812: error("Could not request local forwarding.");
1.70 markus 813:
814: /* Initiate remote TCP/IP port forwardings. */
815: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 816: debug("Remote connections from %.200s:%d forwarded to "
817: "local address %.200s:%d",
1.248 djm 818: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 819: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 820: options.remote_forwards[i].listen_port,
821: options.remote_forwards[i].connect_host,
822: options.remote_forwards[i].connect_port);
1.283 markus 823: if (channel_request_remote_forwarding(
1.232 djm 824: options.remote_forwards[i].listen_host,
825: options.remote_forwards[i].listen_port,
826: options.remote_forwards[i].connect_host,
1.283 markus 827: options.remote_forwards[i].connect_port) < 0) {
828: if (options.exit_on_forward_failure)
829: fatal("Could not request remote forwarding.");
830: else
831: logit("Warning: Could not request remote "
832: "forwarding.");
833: }
1.70 markus 834: }
835: }
836:
1.126 itojun 837: static void
1.70 markus 838: check_agent_present(void)
839: {
840: if (options.forward_agent) {
1.254 djm 841: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 842: if (!ssh_agent_present())
1.70 markus 843: options.forward_agent = 0;
844: }
845: }
846:
1.126 itojun 847: static int
1.45 markus 848: ssh_session(void)
849: {
850: int type;
851: int interactive = 0;
852: int have_tty = 0;
853: struct winsize ws;
854: char *cp;
1.243 djm 855: const char *display;
1.45 markus 856:
1.31 markus 857: /* Enable compression if requested. */
858: if (options.compression) {
859: debug("Requesting compression at level %d.", options.compression_level);
860:
861: if (options.compression_level < 1 || options.compression_level > 9)
862: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
863:
864: /* Send the request. */
865: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
866: packet_put_int(options.compression_level);
867: packet_send();
868: packet_write_wait();
1.156 markus 869: type = packet_read();
1.31 markus 870: if (type == SSH_SMSG_SUCCESS)
871: packet_start_compression(options.compression_level);
872: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 873: logit("Warning: Remote host refused compression.");
1.31 markus 874: else
875: packet_disconnect("Protocol error waiting for compression response.");
876: }
877: /* Allocate a pseudo tty if appropriate. */
878: if (tty_flag) {
879: debug("Requesting pty.");
880:
881: /* Start the packet. */
882: packet_start(SSH_CMSG_REQUEST_PTY);
883:
884: /* Store TERM in the packet. There is no limit on the
885: length of the string. */
886: cp = getenv("TERM");
887: if (!cp)
888: cp = "";
1.124 markus 889: packet_put_cstring(cp);
1.31 markus 890:
891: /* Store window size in the packet. */
892: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
893: memset(&ws, 0, sizeof(ws));
1.269 deraadt 894: packet_put_int((u_int)ws.ws_row);
895: packet_put_int((u_int)ws.ws_col);
896: packet_put_int((u_int)ws.ws_xpixel);
897: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 898:
899: /* Store tty modes in the packet. */
1.115 stevesk 900: tty_make_modes(fileno(stdin), NULL);
1.31 markus 901:
902: /* Send the packet, and wait for it to leave. */
903: packet_send();
904: packet_write_wait();
905:
906: /* Read response from the server. */
1.156 markus 907: type = packet_read();
1.43 markus 908: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 909: interactive = 1;
1.45 markus 910: have_tty = 1;
1.43 markus 911: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 912: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 913: else
914: packet_disconnect("Protocol error waiting for pty request response.");
915: }
916: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 917: display = getenv("DISPLAY");
918: if (options.forward_x11 && display != NULL) {
1.150 stevesk 919: char *proto, *data;
1.50 markus 920: /* Get reasonable local authentication information. */
1.243 djm 921: client_x11_get_proto(display, options.xauth_location,
922: options.forward_x11_trusted, &proto, &data);
1.50 markus 923: /* Request forwarding with authentication spoofing. */
1.31 markus 924: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 925: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 926:
927: /* Read response from the server. */
1.156 markus 928: type = packet_read();
1.31 markus 929: if (type == SSH_SMSG_SUCCESS) {
930: interactive = 1;
1.50 markus 931: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 932: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 933: } else {
1.31 markus 934: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 935: }
1.31 markus 936: }
937: /* Tell the packet module whether this is an interactive session. */
1.80 markus 938: packet_set_interactive(interactive);
1.31 markus 939:
940: /* Request authentication agent forwarding if appropriate. */
1.70 markus 941: check_agent_present();
942:
1.31 markus 943: if (options.forward_agent) {
944: debug("Requesting authentication agent forwarding.");
945: auth_request_forwarding();
946:
947: /* Read response from the server. */
1.156 markus 948: type = packet_read();
1.155 markus 949: packet_check_eom();
1.31 markus 950: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 951: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 952: }
953:
1.70 markus 954: /* Initiate port forwardings. */
955: ssh_init_forwarding();
1.34 markus 956:
957: /* If requested, let ssh continue in the background. */
1.48 markus 958: if (fork_after_authentication_flag)
1.34 markus 959: if (daemon(1, 1) < 0)
960: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 961:
1.33 markus 962: /*
963: * If a command was specified on the command line, execute the
964: * command now. Otherwise request the server to start a shell.
965: */
1.31 markus 966: if (buffer_len(&command) > 0) {
967: int len = buffer_len(&command);
968: if (len > 900)
969: len = 900;
1.152 stevesk 970: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 971: packet_start(SSH_CMSG_EXEC_CMD);
972: packet_put_string(buffer_ptr(&command), buffer_len(&command));
973: packet_send();
974: packet_write_wait();
975: } else {
976: debug("Requesting shell.");
977: packet_start(SSH_CMSG_EXEC_SHELL);
978: packet_send();
979: packet_write_wait();
980: }
981:
982: /* Enter the interactive session. */
1.119 stevesk 983: return client_loop(have_tty, tty_flag ?
984: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 985: }
986:
1.126 itojun 987: static void
1.214 djm 988: ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 989: {
990: int id, len;
991:
992: id = packet_get_int();
993: len = buffer_len(&command);
1.100 markus 994: if (len > 900)
995: len = 900;
1.155 markus 996: packet_check_eom();
1.89 markus 997: if (type == SSH2_MSG_CHANNEL_FAILURE)
998: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 999: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 1000: }
1001:
1002: void
1.206 markus 1003: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 1004: {
1005: int i;
1006:
1007: i = client_global_request_id++;
1.206 markus 1008: if (i >= options.num_remote_forwards)
1.170 markus 1009: return;
1010: debug("remote forward %s for: listen %d, connect %s:%d",
1011: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.232 djm 1012: options.remote_forwards[i].listen_port,
1013: options.remote_forwards[i].connect_host,
1014: options.remote_forwards[i].connect_port);
1.283 markus 1015: if (type == SSH2_MSG_REQUEST_FAILURE) {
1016: if (options.exit_on_forward_failure)
1017: fatal("Error: remote port forwarding failed for "
1018: "listen port %d",
1019: options.remote_forwards[i].listen_port);
1020: else
1021: logit("Warning: remote port forwarding failed for "
1022: "listen port %d",
1023: options.remote_forwards[i].listen_port);
1024: }
1.89 markus 1025: }
1026:
1.126 itojun 1027: static void
1.214 djm 1028: ssh_control_listener(void)
1.45 markus 1029: {
1.214 djm 1030: struct sockaddr_un addr;
1031: mode_t old_umask;
1.223 deraadt 1032:
1.242 djm 1033: if (options.control_path == NULL ||
1034: options.control_master == SSHCTL_MASTER_NO)
1.214 djm 1035: return;
1036:
1.242 djm 1037: debug("setting up multiplex master socket");
1038:
1.214 djm 1039: memset(&addr, '\0', sizeof(addr));
1040: addr.sun_family = AF_UNIX;
1041: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1042: strlen(options.control_path) + 1;
1043:
1044: if (strlcpy(addr.sun_path, options.control_path,
1045: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1046: fatal("ControlPath too long");
1047:
1048: if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1.252 stevesk 1049: fatal("%s socket(): %s", __func__, strerror(errno));
1.214 djm 1050:
1051: old_umask = umask(0177);
1.270 deraadt 1052: if (bind(control_fd, (struct sockaddr *)&addr, addr.sun_len) == -1) {
1.214 djm 1053: control_fd = -1;
1.238 djm 1054: if (errno == EINVAL || errno == EADDRINUSE)
1.214 djm 1055: fatal("ControlSocket %s already exists",
1056: options.control_path);
1057: else
1.252 stevesk 1058: fatal("%s bind(): %s", __func__, strerror(errno));
1.214 djm 1059: }
1060: umask(old_umask);
1.80 markus 1061:
1.214 djm 1062: if (listen(control_fd, 64) == -1)
1.252 stevesk 1063: fatal("%s listen(): %s", __func__, strerror(errno));
1.31 markus 1064:
1.214 djm 1065: set_nonblock(control_fd);
1066: }
1.45 markus 1067:
1.214 djm 1068: /* request pty/x11/agent/tcpfwd/shell for channel */
1069: static void
1070: ssh_session2_setup(int id, void *arg)
1071: {
1.215 djm 1072: extern char **environ;
1.243 djm 1073: const char *display;
1074: int interactive = tty_flag;
1.215 djm 1075:
1.248 djm 1076: display = getenv("DISPLAY");
1.243 djm 1077: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1078: char *proto, *data;
1.50 markus 1079: /* Get reasonable local authentication information. */
1.243 djm 1080: client_x11_get_proto(display, options.xauth_location,
1081: options.forward_x11_trusted, &proto, &data);
1.50 markus 1082: /* Request forwarding with authentication spoofing. */
1083: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 1084: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1085: interactive = 1;
1.50 markus 1086: /* XXX wait for reply */
1087: }
1088:
1.70 markus 1089: check_agent_present();
1090: if (options.forward_agent) {
1091: debug("Requesting authentication agent forwarding.");
1092: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1093: packet_send();
1.212 djm 1094: }
1095:
1.256 reyk 1096: if (options.tun_open != SSH_TUNMODE_NO) {
1.255 reyk 1097: Channel *c;
1098: int fd;
1099:
1100: debug("Requesting tun.");
1.256 reyk 1101: if ((fd = tun_open(options.tun_local,
1102: options.tun_open)) >= 0) {
1.255 reyk 1103: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1104: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1105: 0, "tun", 1);
1106: c->datagram = 1;
1107: packet_start(SSH2_MSG_CHANNEL_OPEN);
1108: packet_put_cstring("tun@openssh.com");
1109: packet_put_int(c->self);
1110: packet_put_int(c->local_window_max);
1111: packet_put_int(c->local_maxpacket);
1.256 reyk 1112: packet_put_int(options.tun_open);
1.255 reyk 1113: packet_put_int(options.tun_remote);
1114: packet_send();
1115: }
1116: }
1117:
1.214 djm 1118: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.215 djm 1119: NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
1.90 markus 1120:
1.80 markus 1121: packet_set_interactive(interactive);
1.45 markus 1122: }
1123:
1.143 markus 1124: /* open new channel for a session */
1.126 itojun 1125: static int
1.143 markus 1126: ssh_session2_open(void)
1.45 markus 1127: {
1.118 markus 1128: Channel *c;
1129: int window, packetmax, in, out, err;
1.60 markus 1130:
1.62 markus 1131: if (stdin_null_flag) {
1.93 itojun 1132: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1133: } else {
1134: in = dup(STDIN_FILENO);
1135: }
1.60 markus 1136: out = dup(STDOUT_FILENO);
1137: err = dup(STDERR_FILENO);
1.45 markus 1138:
1139: if (in < 0 || out < 0 || err < 0)
1.62 markus 1140: fatal("dup() in/out/err failed");
1.45 markus 1141:
1.69 markus 1142: /* enable nonblocking unless tty */
1143: if (!isatty(in))
1144: set_nonblock(in);
1145: if (!isatty(out))
1146: set_nonblock(out);
1147: if (!isatty(err))
1148: set_nonblock(err);
1149:
1.65 markus 1150: window = CHAN_SES_WINDOW_DEFAULT;
1151: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1152: if (tty_flag) {
1153: window >>= 1;
1154: packetmax >>= 1;
1.45 markus 1155: }
1.118 markus 1156: c = channel_new(
1.45 markus 1157: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1158: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1159: "client-session", /*nonblock*/0);
1.45 markus 1160:
1.143 markus 1161: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1162:
1.122 markus 1163: channel_send_open(c->self);
1.143 markus 1164: if (!no_shell_flag)
1.214 djm 1165: channel_register_confirm(c->self, ssh_session2_setup, NULL);
1.106 markus 1166:
1.118 markus 1167: return c->self;
1.106 markus 1168: }
1169:
1.126 itojun 1170: static int
1.106 markus 1171: ssh_session2(void)
1172: {
1.143 markus 1173: int id = -1;
1.106 markus 1174:
1175: /* XXX should be pre-session */
1176: ssh_init_forwarding();
1.214 djm 1177: ssh_control_listener();
1.106 markus 1178:
1.143 markus 1179: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1180: id = ssh_session2_open();
1.255 reyk 1181:
1182: /* Execute a local command */
1183: if (options.local_command != NULL &&
1184: options.permit_local_command)
1185: ssh_local_cmd(options.local_command);
1.106 markus 1186:
1187: /* If requested, let ssh continue in the background. */
1188: if (fork_after_authentication_flag)
1189: if (daemon(1, 1) < 0)
1190: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1191:
1.119 stevesk 1192: return client_loop(tty_flag, tty_flag ?
1193: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1194: }
1195:
1.126 itojun 1196: static void
1.104 markus 1197: load_public_identity_files(void)
1198: {
1.275 djm 1199: char *filename, *cp, thishost[NI_MAXHOST];
1.167 markus 1200: int i = 0;
1.104 markus 1201: Key *public;
1.275 djm 1202: struct passwd *pw;
1.167 markus 1203: #ifdef SMARTCARD
1204: Key **keys;
1.104 markus 1205:
1.133 markus 1206: if (options.smartcard_device != NULL &&
1.167 markus 1207: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1208: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1209: int count = 0;
1210: for (i = 0; keys[i] != NULL; i++) {
1211: count++;
1212: memmove(&options.identity_files[1], &options.identity_files[0],
1213: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1214: memmove(&options.identity_keys[1], &options.identity_keys[0],
1215: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1216: options.num_identity_files++;
1217: options.identity_keys[0] = keys[i];
1.194 markus 1218: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1219: }
1.168 markus 1220: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1221: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1222: i = count;
1223: xfree(keys);
1.127 markus 1224: }
1.136 jakob 1225: #endif /* SMARTCARD */
1.275 djm 1226: if ((pw = getpwuid(original_real_uid)) == NULL)
1227: fatal("load_public_identity_files: getpwuid failed");
1228: if (gethostname(thishost, sizeof(thishost)) == -1)
1229: fatal("load_public_identity_files: gethostname: %s",
1230: strerror(errno));
1.131 millert 1231: for (; i < options.num_identity_files; i++) {
1.275 djm 1232: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1233: original_real_uid);
1.275 djm 1234: filename = percent_expand(cp, "d", pw->pw_dir,
1235: "u", pw->pw_name, "l", thishost, "h", host,
1236: "r", options.user, (char *)NULL);
1237: xfree(cp);
1.131 millert 1238: public = key_load_public(filename, NULL);
1239: debug("identity file %s type %d", filename,
1240: public ? public->type : -1);
1241: xfree(options.identity_files[i]);
1242: options.identity_files[i] = filename;
1243: options.identity_keys[i] = public;
1244: }
1.214 djm 1245: }
1246:
1247: static void
1248: control_client_sighandler(int signo)
1249: {
1250: control_client_terminate = signo;
1251: }
1252:
1253: static void
1254: control_client_sigrelay(int signo)
1255: {
1256: if (control_server_pid > 1)
1257: kill(control_server_pid, signo);
1258: }
1259:
1.220 djm 1260: static int
1261: env_permitted(char *env)
1262: {
1.282 dtucker 1263: int i, ret;
1.220 djm 1264: char name[1024], *cp;
1265:
1.282 dtucker 1266: if ((cp = strchr(env, '=')) == NULL || cp == env)
1.220 djm 1267: return (0);
1.284 stevesk 1268: ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
1.282 dtucker 1269: if (ret <= 0 || (size_t)ret >= sizeof(name))
1270: fatal("env_permitted: name '%.100s...' too long", env);
1.220 djm 1271:
1272: for (i = 0; i < options.num_send_env; i++)
1273: if (match_pattern(name, options.send_env[i]))
1274: return (1);
1275:
1276: return (0);
1277: }
1278:
1.214 djm 1279: static void
1280: control_client(const char *path)
1281: {
1282: struct sockaddr_un addr;
1.228 djm 1283: int i, r, fd, sock, exitval, num_env;
1.214 djm 1284: Buffer m;
1.229 djm 1285: char *term;
1.215 djm 1286: extern char **environ;
1.229 djm 1287: u_int flags;
1.242 djm 1288:
1289: if (mux_command == 0)
1290: mux_command = SSHMUX_COMMAND_OPEN;
1291:
1292: switch (options.control_master) {
1293: case SSHCTL_MASTER_AUTO:
1294: case SSHCTL_MASTER_AUTO_ASK:
1295: debug("auto-mux: Trying existing master");
1296: /* FALLTHROUGH */
1297: case SSHCTL_MASTER_NO:
1298: break;
1299: default:
1300: return;
1301: }
1.228 djm 1302:
1.214 djm 1303: memset(&addr, '\0', sizeof(addr));
1304: addr.sun_family = AF_UNIX;
1305: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1306: strlen(path) + 1;
1307:
1308: if (strlcpy(addr.sun_path, path,
1309: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1310: fatal("ControlPath too long");
1311:
1312: if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1313: fatal("%s socket(): %s", __func__, strerror(errno));
1314:
1.270 deraadt 1315: if (connect(sock, (struct sockaddr *)&addr, addr.sun_len) == -1) {
1.240 djm 1316: if (mux_command != SSHMUX_COMMAND_OPEN) {
1317: fatal("Control socket connect(%.100s): %s", path,
1318: strerror(errno));
1319: }
1.239 djm 1320: if (errno == ENOENT)
1.268 deraadt 1321: debug("Control socket \"%.100s\" does not exist", path);
1.239 djm 1322: else {
1.268 deraadt 1323: error("Control socket connect(%.100s): %s", path,
1.239 djm 1324: strerror(errno));
1325: }
1.268 deraadt 1326: close(sock);
1327: return;
1328: }
1.248 djm 1329:
1.268 deraadt 1330: if (stdin_null_flag) {
1331: if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1332: fatal("open(/dev/null): %s", strerror(errno));
1333: if (dup2(fd, STDIN_FILENO) == -1)
1334: fatal("dup2: %s", strerror(errno));
1335: if (fd > STDERR_FILENO)
1336: close(fd);
1337: }
1.248 djm 1338:
1.247 djm 1339: term = getenv("TERM");
1.229 djm 1340:
1341: flags = 0;
1342: if (tty_flag)
1343: flags |= SSHMUX_FLAG_TTY;
1344: if (subsystem_flag)
1345: flags |= SSHMUX_FLAG_SUBSYS;
1.247 djm 1346: if (options.forward_x11)
1347: flags |= SSHMUX_FLAG_X11_FWD;
1348: if (options.forward_agent)
1349: flags |= SSHMUX_FLAG_AGENT_FWD;
1.214 djm 1350:
1351: buffer_init(&m);
1352:
1.229 djm 1353: /* Send our command to server */
1354: buffer_put_int(&m, mux_command);
1355: buffer_put_int(&m, flags);
1.247 djm 1356: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.229 djm 1357: fatal("%s: msg_send", __func__);
1358: buffer_clear(&m);
1359:
1360: /* Get authorisation status and PID of controlee */
1.214 djm 1361: if (ssh_msg_recv(sock, &m) == -1)
1362: fatal("%s: msg_recv", __func__);
1.247 djm 1363: if (buffer_get_char(&m) != SSHMUX_VER)
1.214 djm 1364: fatal("%s: wrong version", __func__);
1.216 djm 1365: if (buffer_get_int(&m) != 1)
1366: fatal("Connection to master denied");
1.214 djm 1367: control_server_pid = buffer_get_int(&m);
1368:
1369: buffer_clear(&m);
1370:
1.229 djm 1371: switch (mux_command) {
1372: case SSHMUX_COMMAND_ALIVE_CHECK:
1.234 deraadt 1373: fprintf(stderr, "Master running (pid=%d)\r\n",
1.229 djm 1374: control_server_pid);
1375: exit(0);
1376: case SSHMUX_COMMAND_TERMINATE:
1377: fprintf(stderr, "Exit request sent.\r\n");
1378: exit(0);
1379: case SSHMUX_COMMAND_OPEN:
1380: /* continue below */
1381: break;
1382: default:
1383: fatal("silly mux_command %d", mux_command);
1384: }
1385:
1386: /* SSHMUX_COMMAND_OPEN */
1.247 djm 1387: buffer_put_cstring(&m, term ? term : "");
1.214 djm 1388: buffer_append(&command, "\0", 1);
1389: buffer_put_cstring(&m, buffer_ptr(&command));
1.215 djm 1390:
1.220 djm 1391: if (options.num_send_env == 0 || environ == NULL) {
1392: buffer_put_int(&m, 0);
1.223 deraadt 1393: } else {
1.220 djm 1394: /* Pass environment */
1395: num_env = 0;
1396: for (i = 0; environ[i] != NULL; i++)
1397: if (env_permitted(environ[i]))
1398: num_env++; /* Count */
1.223 deraadt 1399:
1.220 djm 1400: buffer_put_int(&m, num_env);
1401:
1.222 dtucker 1402: for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1403: if (env_permitted(environ[i])) {
1404: num_env--;
1.220 djm 1405: buffer_put_cstring(&m, environ[i]);
1.222 dtucker 1406: }
1.220 djm 1407: }
1.214 djm 1408:
1.247 djm 1409: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.214 djm 1410: fatal("%s: msg_send", __func__);
1411:
1412: mm_send_fd(sock, STDIN_FILENO);
1413: mm_send_fd(sock, STDOUT_FILENO);
1414: mm_send_fd(sock, STDERR_FILENO);
1415:
1416: /* Wait for reply, so master has a chance to gather ttymodes */
1417: buffer_clear(&m);
1418: if (ssh_msg_recv(sock, &m) == -1)
1419: fatal("%s: msg_recv", __func__);
1.247 djm 1420: if (buffer_get_char(&m) != SSHMUX_VER)
1.229 djm 1421: fatal("%s: wrong version", __func__);
1.214 djm 1422: buffer_free(&m);
1.218 djm 1423:
1.226 djm 1424: signal(SIGHUP, control_client_sighandler);
1.218 djm 1425: signal(SIGINT, control_client_sighandler);
1426: signal(SIGTERM, control_client_sighandler);
1427: signal(SIGWINCH, control_client_sigrelay);
1.214 djm 1428:
1429: if (tty_flag)
1430: enter_raw_mode();
1431:
1432: /* Stick around until the controlee closes the client_fd */
1433: exitval = 0;
1434: for (;!control_client_terminate;) {
1435: r = read(sock, &exitval, sizeof(exitval));
1436: if (r == 0) {
1437: debug2("Received EOF from master");
1438: break;
1439: }
1440: if (r > 0)
1441: debug2("Received exit status from master %d", exitval);
1442: if (r == -1 && errno != EINTR)
1443: fatal("%s: read %s", __func__, strerror(errno));
1444: }
1445:
1446: if (control_client_terminate)
1447: debug2("Exiting on signal %d", control_client_terminate);
1448:
1449: close(sock);
1450:
1451: leave_raw_mode();
1452:
1453: if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1454: fprintf(stderr, "Connection to master closed.\r\n");
1455:
1456: exit(exitval);
1.1 deraadt 1457: }