Annotation of src/usr.bin/ssh/ssh.c, Revision 1.201.2.1
1.1 deraadt 1: /*
1.32 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Ssh client program. This program can be used to log into a remote machine.
6: * The software supports strong authentication, encryption, and forwarding
7: * of X11, TCP/IP, and authentication connections.
8: *
1.64 deraadt 9: * As far as I am concerned, the code I have written for this software
10: * can be used freely for any purpose. Any derived versions of this
11: * software must be clearly marked as such, and if the derived work is
12: * incompatible with the protocol description in the RFC file, it must be
13: * called by a name other than "ssh" or "Secure Shell".
14: *
15: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.201.2.1! brad 16: * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
1.64 deraadt 17: *
18: * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
19: * in Canada (German citizen).
20: *
21: * Redistribution and use in source and binary forms, with or without
22: * modification, are permitted provided that the following conditions
23: * are met:
24: * 1. Redistributions of source code must retain the above copyright
25: * notice, this list of conditions and the following disclaimer.
26: * 2. Redistributions in binary form must reproduce the above copyright
27: * notice, this list of conditions and the following disclaimer in the
28: * documentation and/or other materials provided with the distribution.
29: *
30: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 deraadt 40: */
1.1 deraadt 41:
42: #include "includes.h"
1.201.2.1! brad 43: RCSID("$OpenBSD: ssh.c,v 1.206 2003/12/16 15:49:51 markus Exp $");
1.49 markus 44:
45: #include <openssl/evp.h>
1.72 markus 46: #include <openssl/err.h>
1.1 deraadt 47:
1.84 markus 48: #include "ssh.h"
49: #include "ssh1.h"
50: #include "ssh2.h"
51: #include "compat.h"
52: #include "cipher.h"
1.1 deraadt 53: #include "xmalloc.h"
54: #include "packet.h"
55: #include "buffer.h"
1.123 markus 56: #include "channels.h"
1.49 markus 57: #include "key.h"
1.58 markus 58: #include "authfd.h"
1.49 markus 59: #include "authfile.h"
1.83 markus 60: #include "pathnames.h"
1.81 markus 61: #include "clientloop.h"
1.84 markus 62: #include "log.h"
63: #include "readconf.h"
64: #include "sshconnect.h"
65: #include "tildexpand.h"
1.89 markus 66: #include "dispatch.h"
1.84 markus 67: #include "misc.h"
1.95 markus 68: #include "kex.h"
69: #include "mac.h"
1.115 stevesk 70: #include "sshtty.h"
1.49 markus 71:
1.127 markus 72: #ifdef SMARTCARD
73: #include "scard.h"
1.137 jakob 74: #endif
1.127 markus 75:
1.49 markus 76: extern char *__progname;
1.1 deraadt 77:
1.31 markus 78: /* Flag indicating whether debug mode is on. This can be set on the command line. */
1.1 deraadt 79: int debug_flag = 0;
80:
1.46 markus 81: /* Flag indicating whether a tty should be allocated */
1.1 deraadt 82: int tty_flag = 0;
1.79 markus 83: int no_tty_flag = 0;
84: int force_tty_flag = 0;
1.1 deraadt 85:
1.45 markus 86: /* don't exec a shell */
87: int no_shell_flag = 0;
88:
1.33 markus 89: /*
90: * Flag indicating that nothing should be read from stdin. This can be set
91: * on the command line.
92: */
1.1 deraadt 93: int stdin_null_flag = 0;
94:
1.33 markus 95: /*
96: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 97: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 98: * background.
99: */
1.1 deraadt 100: int fork_after_authentication_flag = 0;
101:
1.33 markus 102: /*
103: * General data structure for command line options and options configurable
104: * in configuration files. See readconf.h.
105: */
1.1 deraadt 106: Options options;
107:
1.139 markus 108: /* optional user configfile */
109: char *config = NULL;
110:
1.33 markus 111: /*
112: * Name of the host we are connecting to. This is the name given on the
113: * command line, or the HostName specified for the user-supplied name in a
114: * configuration file.
115: */
1.1 deraadt 116: char *host;
117:
1.22 provos 118: /* socket address the host resolves to */
1.37 markus 119: struct sockaddr_storage hostaddr;
1.1 deraadt 120:
1.112 markus 121: /* Private host keys. */
1.173 markus 122: Sensitive sensitive_data;
1.1 deraadt 123:
1.10 dugsong 124: /* Original real UID. */
125: uid_t original_real_uid;
1.177 markus 126: uid_t original_effective_uid;
1.1 deraadt 127:
1.45 markus 128: /* command to be executed */
129: Buffer command;
130:
1.85 djm 131: /* Should we execute a command or invoke a subsystem? */
132: int subsystem_flag = 0;
133:
1.170 markus 134: /* # of replies received for global requests */
135: static int client_global_request_id = 0;
136:
1.186 djm 137: /* pid of proxycommand child process */
138: pid_t proxy_command_pid = 0;
139:
1.1 deraadt 140: /* Prints a help message to the user. This function never returns. */
141:
1.126 itojun 142: static void
1.93 itojun 143: usage(void)
1.1 deraadt 144: {
1.76 markus 145: fprintf(stderr, "Usage: %s [options] host [command]\n", __progname);
1.31 markus 146: fprintf(stderr, "Options:\n");
147: fprintf(stderr, " -l user Log in using this user name.\n");
1.93 itojun 148: fprintf(stderr, " -n Redirect input from " _PATH_DEVNULL ".\n");
1.139 markus 149: fprintf(stderr, " -F config Config file (default: ~/%s).\n",
150: _PATH_SSH_USER_CONFFILE);
1.53 markus 151: fprintf(stderr, " -A Enable authentication agent forwarding.\n");
1.120 markus 152: fprintf(stderr, " -a Disable authentication agent forwarding (default).\n");
1.88 stevesk 153: fprintf(stderr, " -X Enable X11 connection forwarding.\n");
1.201.2.1! brad 154: fprintf(stderr, " -Y Enable trusted X11 connection forwarding.\n");
1.120 markus 155: fprintf(stderr, " -x Disable X11 connection forwarding (default).\n");
1.99 deraadt 156: fprintf(stderr, " -i file Identity for public key authentication "
157: "(default: ~/.ssh/identity)\n");
1.135 jakob 158: #ifdef SMARTCARD
159: fprintf(stderr, " -I reader Set smartcard reader.\n");
1.137 jakob 160: #endif
1.31 markus 161: fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
1.45 markus 162: fprintf(stderr, " -T Do not allocate a tty.\n");
1.31 markus 163: fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
1.66 markus 164: fprintf(stderr, " Multiple -v increases verbosity.\n");
1.31 markus 165: fprintf(stderr, " -V Display version number only.\n");
166: fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
167: fprintf(stderr, " -f Fork into background after authentication.\n");
168: fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
169:
1.120 markus 170: fprintf(stderr, " -c cipher Select encryption algorithm\n");
1.102 stevesk 171: fprintf(stderr, " -m macs Specify MAC algorithms for protocol version 2.\n");
1.31 markus 172: fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
173: fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
174: fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
1.76 markus 175: fprintf(stderr, " These cause %s to listen for connections on a port, and\n", __progname);
1.31 markus 176: fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
1.140 stevesk 177: fprintf(stderr, " -D port Enable dynamic application-level port forwarding.\n");
1.31 markus 178: fprintf(stderr, " -C Enable compression.\n");
1.45 markus 179: fprintf(stderr, " -N Do not execute a shell or command.\n");
1.31 markus 180: fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
1.92 jakob 181: fprintf(stderr, " -1 Force protocol version 1.\n");
182: fprintf(stderr, " -2 Force protocol version 2.\n");
1.37 markus 183: fprintf(stderr, " -4 Use IPv4 only.\n");
184: fprintf(stderr, " -6 Use IPv6 only.\n");
1.31 markus 185: fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
1.85 djm 186: fprintf(stderr, " -s Invoke command (mandatory) as SSH2 subsystem.\n");
1.120 markus 187: fprintf(stderr, " -b addr Local IP address.\n");
1.31 markus 188: exit(1);
1.1 deraadt 189: }
190:
1.126 itojun 191: static int ssh_session(void);
192: static int ssh_session2(void);
193: static void load_public_identity_files(void);
1.45 markus 194:
1.32 deraadt 195: /*
196: * Main program for the ssh client.
197: */
1.2 provos 198: int
199: main(int ac, char **av)
1.1 deraadt 200: {
1.178 markus 201: int i, opt, exit_status;
1.35 markus 202: u_short fwd_port, fwd_host_port;
1.141 stevesk 203: char sfwd_port[6], sfwd_host_port[6];
1.201.2.1! brad 204: char *p, *cp, *line, buf[256];
1.31 markus 205: struct stat st;
1.98 markus 206: struct passwd *pw;
1.45 markus 207: int dummy;
1.144 stevesk 208: extern int optind, optreset;
209: extern char *optarg;
1.31 markus 210:
1.33 markus 211: /*
212: * Save the original real uid. It will be needed later (uid-swapping
213: * may clobber the real uid).
214: */
1.31 markus 215: original_real_uid = getuid();
216: original_effective_uid = geteuid();
217:
1.184 stevesk 218: /*
219: * Use uid-swapping to give up root privileges for the duration of
220: * option processing. We will re-instantiate the rights when we are
221: * ready to create the privileged port, and will permanently drop
222: * them when the port has been created (actually, when the connection
223: * has been made, as we may need to create the port several times).
224: */
225: PRIV_END;
226:
1.31 markus 227: /* If we are installed setuid root be careful to not drop core. */
228: if (original_real_uid != original_effective_uid) {
229: struct rlimit rlim;
230: rlim.rlim_cur = rlim.rlim_max = 0;
231: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
232: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 233: }
1.107 markus 234: /* Get user data. */
235: pw = getpwuid(original_real_uid);
236: if (!pw) {
1.191 itojun 237: logit("You don't exist, go away!");
1.107 markus 238: exit(1);
239: }
240: /* Take a copy of the returned structure. */
241: pw = pwcopy(pw);
1.31 markus 242:
1.33 markus 243: /*
244: * Set our umask to something reasonable, as some files are created
245: * with the default umask. This will make them world-readable but
246: * writable only by the owner, which is ok for all files for which we
247: * don't set the modes explicitly.
248: */
1.31 markus 249: umask(022);
250:
251: /* Initialize option structure to indicate that no values have been set. */
252: initialize_options(&options);
253:
254: /* Parse command-line arguments. */
255: host = NULL;
256:
1.128 fgsch 257: again:
258: while ((opt = getopt(ac, av,
1.201.2.1! brad 259: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVXY")) != -1) {
1.31 markus 260: switch (opt) {
1.91 jakob 261: case '1':
262: options.protocol = SSH_PROTO_1;
263: break;
1.47 markus 264: case '2':
265: options.protocol = SSH_PROTO_2;
266: break;
1.37 markus 267: case '4':
1.196 djm 268: options.address_family = AF_INET;
1.37 markus 269: break;
270: case '6':
1.196 djm 271: options.address_family = AF_INET6;
1.37 markus 272: break;
1.31 markus 273: case 'n':
274: stdin_null_flag = 1;
275: break;
276: case 'f':
277: fork_after_authentication_flag = 1;
278: stdin_null_flag = 1;
279: break;
280: case 'x':
281: options.forward_x11 = 0;
282: break;
283: case 'X':
284: options.forward_x11 = 1;
285: break;
1.201.2.1! brad 286: case 'Y':
! 287: options.forward_x11 = 1;
! 288: options.forward_x11_trusted = 1;
! 289: break;
1.31 markus 290: case 'g':
291: options.gateway_ports = 1;
292: break;
1.183 stevesk 293: case 'P': /* deprecated */
1.31 markus 294: options.use_privileged_port = 0;
295: break;
296: case 'a':
297: options.forward_agent = 0;
1.53 markus 298: break;
299: case 'A':
300: options.forward_agent = 1;
1.31 markus 301: break;
302: case 'k':
1.201.2.1! brad 303: options.gss_deleg_creds = 0;
1.31 markus 304: break;
305: case 'i':
306: if (stat(optarg, &st) < 0) {
1.128 fgsch 307: fprintf(stderr, "Warning: Identity file %s "
308: "does not exist.\n", optarg);
1.31 markus 309: break;
310: }
1.128 fgsch 311: if (options.num_identity_files >=
312: SSH_MAX_IDENTITY_FILES)
313: fatal("Too many identity files specified "
314: "(max %d)", SSH_MAX_IDENTITY_FILES);
315: options.identity_files[options.num_identity_files++] =
316: xstrdup(optarg);
1.31 markus 317: break;
1.127 markus 318: case 'I':
319: #ifdef SMARTCARD
1.133 markus 320: options.smartcard_device = xstrdup(optarg);
1.137 jakob 321: #else
1.127 markus 322: fprintf(stderr, "no support for smartcards.\n");
1.137 jakob 323: #endif
1.127 markus 324: break;
1.31 markus 325: case 't':
1.79 markus 326: if (tty_flag)
327: force_tty_flag = 1;
1.31 markus 328: tty_flag = 1;
329: break;
330: case 'v':
1.197 markus 331: if (debug_flag == 0) {
1.66 markus 332: debug_flag = 1;
333: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 334: } else {
335: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
336: options.log_level++;
1.66 markus 337: break;
1.197 markus 338: }
1.66 markus 339: /* fallthrough */
1.31 markus 340: case 'V':
1.96 deraadt 341: fprintf(stderr,
1.193 djm 342: "%s, SSH protocols %d.%d/%d.%d, %s\n",
1.46 markus 343: SSH_VERSION,
344: PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
1.96 deraadt 345: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
1.193 djm 346: SSLeay_version(SSLEAY_VERSION));
1.31 markus 347: if (opt == 'V')
348: exit(0);
349: break;
350: case 'q':
351: options.log_level = SYSLOG_LEVEL_QUIET;
352: break;
353: case 'e':
354: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 355: (u_char) optarg[1] >= 64 &&
356: (u_char) optarg[1] < 128)
1.78 markus 357: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 358: else if (strlen(optarg) == 1)
1.78 markus 359: options.escape_char = (u_char) optarg[0];
1.31 markus 360: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 361: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 362: else {
1.128 fgsch 363: fprintf(stderr, "Bad escape character '%s'.\n",
364: optarg);
1.31 markus 365: exit(1);
366: }
367: break;
368: case 'c':
1.49 markus 369: if (ciphers_valid(optarg)) {
370: /* SSH2 only */
371: options.ciphers = xstrdup(optarg);
1.51 markus 372: options.cipher = SSH_CIPHER_ILLEGAL;
1.49 markus 373: } else {
374: /* SSH1 only */
1.74 markus 375: options.cipher = cipher_number(optarg);
376: if (options.cipher == -1) {
1.128 fgsch 377: fprintf(stderr,
378: "Unknown cipher type '%s'\n",
379: optarg);
1.49 markus 380: exit(1);
381: }
1.128 fgsch 382: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 383: options.ciphers = "3des-cbc";
1.128 fgsch 384: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 385: options.ciphers = "blowfish-cbc";
1.128 fgsch 386: else
1.74 markus 387: options.ciphers = (char *)-1;
1.95 markus 388: }
389: break;
390: case 'm':
391: if (mac_valid(optarg))
392: options.macs = xstrdup(optarg);
393: else {
1.128 fgsch 394: fprintf(stderr, "Unknown mac type '%s'\n",
395: optarg);
1.95 markus 396: exit(1);
1.31 markus 397: }
398: break;
399: case 'p':
1.113 stevesk 400: options.port = a2port(optarg);
401: if (options.port == 0) {
1.109 markus 402: fprintf(stderr, "Bad port '%s'\n", optarg);
403: exit(1);
404: }
1.31 markus 405: break;
406: case 'l':
407: options.user = optarg;
408: break;
1.141 stevesk 409:
410: case 'L':
1.31 markus 411: case 'R':
1.141 stevesk 412: if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]",
413: sfwd_port, buf, sfwd_host_port) != 3 &&
414: sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]",
415: sfwd_port, buf, sfwd_host_port) != 3) {
1.128 fgsch 416: fprintf(stderr,
1.141 stevesk 417: "Bad forwarding specification '%s'\n",
1.128 fgsch 418: optarg);
1.31 markus 419: usage();
420: /* NOTREACHED */
421: }
1.141 stevesk 422: if ((fwd_port = a2port(sfwd_port)) == 0 ||
1.165 markus 423: (fwd_host_port = a2port(sfwd_host_port)) == 0) {
1.128 fgsch 424: fprintf(stderr,
1.141 stevesk 425: "Bad forwarding port(s) '%s'\n", optarg);
426: exit(1);
1.31 markus 427: }
1.141 stevesk 428: if (opt == 'L')
429: add_local_forward(&options, fwd_port, buf,
430: fwd_host_port);
431: else if (opt == 'R')
432: add_remote_forward(&options, fwd_port, buf,
1.151 deraadt 433: fwd_host_port);
1.31 markus 434: break;
1.108 markus 435:
436: case 'D':
1.113 stevesk 437: fwd_port = a2port(optarg);
438: if (fwd_port == 0) {
1.128 fgsch 439: fprintf(stderr, "Bad dynamic port '%s'\n",
440: optarg);
1.109 markus 441: exit(1);
442: }
1.200 markus 443: add_local_forward(&options, fwd_port, "socks", 0);
1.108 markus 444: break;
445:
1.31 markus 446: case 'C':
447: options.compression = 1;
448: break;
1.45 markus 449: case 'N':
450: no_shell_flag = 1;
451: no_tty_flag = 1;
452: break;
453: case 'T':
454: no_tty_flag = 1;
455: break;
1.31 markus 456: case 'o':
457: dummy = 1;
1.201.2.1! brad 458: line = xstrdup(optarg);
1.128 fgsch 459: if (process_config_line(&options, host ? host : "",
1.201.2.1! brad 460: line, "command-line", 0, &dummy) != 0)
1.31 markus 461: exit(1);
1.201.2.1! brad 462: xfree(line);
1.31 markus 463: break;
1.85 djm 464: case 's':
465: subsystem_flag = 1;
1.117 markus 466: break;
467: case 'b':
468: options.bind_address = optarg;
1.85 djm 469: break;
1.139 markus 470: case 'F':
471: config = optarg;
472: break;
1.31 markus 473: default:
474: usage();
1.1 deraadt 475: }
1.31 markus 476: }
477:
1.128 fgsch 478: ac -= optind;
479: av += optind;
480:
481: if (ac > 0 && !host && **av != '-') {
1.188 markus 482: if (strrchr(*av, '@')) {
1.128 fgsch 483: p = xstrdup(*av);
1.188 markus 484: cp = strrchr(p, '@');
1.128 fgsch 485: if (cp == NULL || cp == p)
486: usage();
487: options.user = p;
488: *cp = '\0';
489: host = ++cp;
490: } else
491: host = *av;
1.189 millert 492: if (ac > 1) {
493: optind = optreset = 1;
1.128 fgsch 494: goto again;
495: }
1.189 millert 496: ac--, av++;
1.128 fgsch 497: }
498:
1.31 markus 499: /* Check that we got a host name. */
500: if (!host)
501: usage();
502:
1.54 markus 503: SSLeay_add_all_algorithms();
1.72 markus 504: ERR_load_crypto_strings();
1.31 markus 505:
506: /* Initialize the command to execute on remote host. */
507: buffer_init(&command);
1.1 deraadt 508:
1.33 markus 509: /*
510: * Save the command to execute on the remote host in a buffer. There
511: * is no limit on the length of the command, except by the maximum
512: * packet size. Also sets the tty flag if there is no command.
513: */
1.128 fgsch 514: if (!ac) {
1.31 markus 515: /* No command specified - execute shell on a tty. */
516: tty_flag = 1;
1.85 djm 517: if (subsystem_flag) {
1.128 fgsch 518: fprintf(stderr,
519: "You must specify a subsystem to invoke.\n");
1.85 djm 520: usage();
521: }
1.31 markus 522: } else {
1.128 fgsch 523: /* A command has been specified. Store it into the buffer. */
524: for (i = 0; i < ac; i++) {
525: if (i)
1.31 markus 526: buffer_append(&command, " ", 1);
527: buffer_append(&command, av[i], strlen(av[i]));
528: }
529: }
530:
531: /* Cannot fork to background if no command. */
1.63 markus 532: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 533: fatal("Cannot fork into background without a command to execute.");
534:
535: /* Allocate a tty by default if no command specified. */
536: if (buffer_len(&command) == 0)
537: tty_flag = 1;
538:
1.180 deraadt 539: /* Force no tty */
1.75 markus 540: if (no_tty_flag)
541: tty_flag = 0;
1.31 markus 542: /* Do not allocate a tty if stdin is not a tty. */
1.79 markus 543: if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31 markus 544: if (tty_flag)
1.191 itojun 545: logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 546: tty_flag = 0;
547: }
1.45 markus 548:
1.101 markus 549: /*
550: * Initialize "log" output. Since we are the client all output
551: * actually goes to stderr.
552: */
1.111 markus 553: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
554: SYSLOG_FACILITY_USER, 1);
1.31 markus 555:
1.139 markus 556: /*
557: * Read per-user configuration file. Ignore the system wide config
558: * file if the user specifies a config file on the command line.
559: */
560: if (config != NULL) {
1.142 stevesk 561: if (!read_config_file(config, host, &options))
562: fatal("Can't open user config file %.100s: "
563: "%.100s", config, strerror(errno));
1.139 markus 564: } else {
565: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
566: _PATH_SSH_USER_CONFFILE);
1.145 markus 567: (void)read_config_file(buf, host, &options);
1.139 markus 568:
1.145 markus 569: /* Read systemwide configuration file after use config. */
1.142 stevesk 570: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
1.139 markus 571: }
1.31 markus 572:
573: /* Fill configuration defaults. */
574: fill_default_options(&options);
575:
1.196 djm 576: channel_set_af(options.address_family);
577:
1.31 markus 578: /* reinit */
1.101 markus 579: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 580:
581: if (options.user == NULL)
582: options.user = xstrdup(pw->pw_name);
583:
584: if (options.hostname != NULL)
585: host = options.hostname;
1.195 markus 586:
587: /* force lowercase for hostkey matching */
588: if (options.host_key_alias != NULL) {
589: for (p = options.host_key_alias; *p; p++)
590: if (isupper(*p))
591: *p = tolower(*p);
592: }
1.190 markus 593:
594: if (options.proxy_command != NULL &&
595: strcmp(options.proxy_command, "none") == 0)
596: options.proxy_command = NULL;
1.31 markus 597:
1.77 markus 598: /* Open a connection to the remote host. */
1.201.2.1! brad 599: if (ssh_connect(host, &hostaddr, options.port,
1.196 djm 600: options.address_family, options.connection_attempts,
1.177 markus 601: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 602: options.proxy_command) != 0)
1.178 markus 603: exit(1);
1.31 markus 604:
1.33 markus 605: /*
606: * If we successfully made the connection, load the host private key
607: * in case we will need it later for combined rsa-rhosts
608: * authentication. This must be done before releasing extra
609: * privileges, because the file is only readable by root.
1.174 markus 610: * If we cannot access the private keys, load the public keys
611: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 612: */
1.112 markus 613: sensitive_data.nkeys = 0;
614: sensitive_data.keys = NULL;
1.173 markus 615: sensitive_data.external_keysign = 0;
1.178 markus 616: if (options.rhosts_rsa_authentication ||
617: options.hostbased_authentication) {
1.112 markus 618: sensitive_data.nkeys = 3;
1.180 deraadt 619: sensitive_data.keys = xmalloc(sensitive_data.nkeys *
620: sizeof(Key));
1.177 markus 621:
622: PRIV_START;
1.112 markus 623: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105 markus 624: _PATH_HOST_KEY_FILE, "", NULL);
1.112 markus 625: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
626: _PATH_HOST_DSA_KEY_FILE, "", NULL);
627: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
628: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.177 markus 629: PRIV_END;
1.173 markus 630:
1.181 markus 631: if (options.hostbased_authentication == 1 &&
632: sensitive_data.keys[0] == NULL &&
1.173 markus 633: sensitive_data.keys[1] == NULL &&
634: sensitive_data.keys[2] == NULL) {
635: sensitive_data.keys[1] = key_load_public(
636: _PATH_HOST_DSA_KEY_FILE, NULL);
637: sensitive_data.keys[2] = key_load_public(
638: _PATH_HOST_RSA_KEY_FILE, NULL);
639: sensitive_data.external_keysign = 1;
640: }
1.31 markus 641: }
1.33 markus 642: /*
643: * Get rid of any extra privileges that we may have. We will no
644: * longer need them. Also, extra privileges could make it very hard
645: * to read identity files and other non-world-readable files from the
646: * user's home directory if it happens to be on a NFS volume where
647: * root is mapped to nobody.
648: */
1.177 markus 649: seteuid(original_real_uid);
650: setuid(original_real_uid);
1.31 markus 651:
1.33 markus 652: /*
653: * Now that we are back to our own permissions, create ~/.ssh
654: * directory if it doesn\'t already exist.
655: */
1.138 jakob 656: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 657: if (stat(buf, &st) < 0)
1.57 djm 658: if (mkdir(buf, 0700) < 0)
1.31 markus 659: error("Could not create directory '%.200s'.", buf);
660:
1.104 markus 661: /* load options.identity_files */
662: load_public_identity_files();
663:
664: /* Expand ~ in known host file names. */
665: /* XXX mem-leaks: */
1.72 markus 666: options.system_hostfile =
667: tilde_expand_filename(options.system_hostfile, original_real_uid);
668: options.user_hostfile =
669: tilde_expand_filename(options.user_hostfile, original_real_uid);
670: options.system_hostfile2 =
671: tilde_expand_filename(options.system_hostfile2, original_real_uid);
672: options.user_hostfile2 =
673: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 674:
675: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 676:
677: /* Log into the remote system. This never returns if the login fails. */
1.173 markus 678: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 679:
1.112 markus 680: /* We no longer need the private host keys. Clear them now. */
681: if (sensitive_data.nkeys != 0) {
682: for (i = 0; i < sensitive_data.nkeys; i++) {
683: if (sensitive_data.keys[i] != NULL) {
684: /* Destroys contents safely */
685: debug3("clear hostkey %d", i);
686: key_free(sensitive_data.keys[i]);
687: sensitive_data.keys[i] = NULL;
688: }
689: }
690: xfree(sensitive_data.keys);
1.134 markus 691: }
692: for (i = 0; i < options.num_identity_files; i++) {
693: if (options.identity_files[i]) {
694: xfree(options.identity_files[i]);
695: options.identity_files[i] = NULL;
696: }
697: if (options.identity_keys[i]) {
698: key_free(options.identity_keys[i]);
699: options.identity_keys[i] = NULL;
700: }
1.112 markus 701: }
1.31 markus 702:
1.45 markus 703: exit_status = compat20 ? ssh_session2() : ssh_session();
704: packet_close();
1.186 djm 705:
706: /*
1.201.2.1! brad 707: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 708: * case it hangs and instead rely on init to reap the child
709: */
710: if (proxy_command_pid > 1)
711: kill(proxy_command_pid, SIGHUP);
712:
1.45 markus 713: return exit_status;
714: }
715:
1.201.2.1! brad 716: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
! 717:
1.126 itojun 718: static void
1.150 stevesk 719: x11_get_proto(char **_proto, char **_data)
1.50 markus 720: {
1.201.2.1! brad 721: char cmd[1024];
1.50 markus 722: char line[512];
1.201.2.1! brad 723: char xdisplay[512];
1.150 stevesk 724: static char proto[512], data[512];
1.50 markus 725: FILE *f;
1.201.2.1! brad 726: int got_data = 0, generated = 0, do_unlink = 0, i;
! 727: char *display, *xauthdir, *xauthfile;
1.182 stevesk 728: struct stat st;
1.50 markus 729:
1.201.2.1! brad 730: xauthdir = xauthfile = NULL;
1.150 stevesk 731: *_proto = proto;
732: *_data = data;
733: proto[0] = data[0] = '\0';
1.201.2.1! brad 734:
1.182 stevesk 735: if (!options.xauth_location ||
736: (stat(options.xauth_location, &st) == -1)) {
737: debug("No xauth program.");
738: } else {
739: if ((display = getenv("DISPLAY")) == NULL) {
740: debug("x11_get_proto: DISPLAY not set");
741: return;
742: }
1.201.2.1! brad 743: /*
! 744: * Handle FamilyLocal case where $DISPLAY does
! 745: * not match an authorization entry. For this we
! 746: * just try "xauth list unix:displaynum.screennum".
! 747: * XXX: "localhost" match to determine FamilyLocal
! 748: * is not perfect.
! 749: */
! 750: if (strncmp(display, "localhost:", 10) == 0) {
! 751: snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
! 752: display + 10);
! 753: display = xdisplay;
! 754: }
! 755: if (options.forward_x11_trusted == 0) {
! 756: xauthdir = xmalloc(MAXPATHLEN);
! 757: xauthfile = xmalloc(MAXPATHLEN);
! 758: strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
! 759: if (mkdtemp(xauthdir) != NULL) {
! 760: do_unlink = 1;
! 761: snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
! 762: xauthdir);
! 763: snprintf(cmd, sizeof(cmd),
! 764: "%s -f %s generate %s " SSH_X11_PROTO
! 765: " untrusted timeout 120 2>" _PATH_DEVNULL,
! 766: options.xauth_location, xauthfile, display);
! 767: debug2("x11_get_proto: %s", cmd);
! 768: if (system(cmd) == 0)
! 769: generated = 1;
! 770: }
! 771: }
! 772: snprintf(cmd, sizeof(cmd),
! 773: "%s %s%s list %s . 2>" _PATH_DEVNULL,
! 774: options.xauth_location,
! 775: generated ? "-f " : "" ,
! 776: generated ? xauthfile : "",
! 777: display);
! 778: debug2("x11_get_proto: %s", cmd);
! 779: f = popen(cmd, "r");
1.55 markus 780: if (f && fgets(line, sizeof(line), f) &&
1.150 stevesk 781: sscanf(line, "%*s %511s %511s", proto, data) == 2)
1.55 markus 782: got_data = 1;
783: if (f)
784: pclose(f);
785: }
1.201.2.1! brad 786:
! 787: if (do_unlink) {
! 788: unlink(xauthfile);
! 789: rmdir(xauthdir);
! 790: }
! 791: if (xauthdir)
! 792: xfree(xauthdir);
! 793: if (xauthfile)
! 794: xfree(xauthfile);
! 795:
1.50 markus 796: /*
797: * If we didn't get authentication data, just make up some
798: * data. The forwarding code will check the validity of the
799: * response anyway, and substitute this data. The X11
800: * server, however, will ignore this fake data and use
801: * whatever authentication mechanisms it was using otherwise
802: * for the local connection.
803: */
804: if (!got_data) {
805: u_int32_t rand = 0;
806:
1.201.2.1! brad 807: logit("Warning: No xauth data; "
! 808: "using fake authentication data for X11 forwarding.");
! 809: strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.50 markus 810: for (i = 0; i < 16; i++) {
811: if (i % 4 == 0)
812: rand = arc4random();
1.201.2.1! brad 813: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
! 814: rand & 0xff);
1.50 markus 815: rand >>= 8;
816: }
817: }
818: }
819:
1.126 itojun 820: static void
1.70 markus 821: ssh_init_forwarding(void)
822: {
1.86 markus 823: int success = 0;
1.70 markus 824: int i;
1.86 markus 825:
1.70 markus 826: /* Initiate local TCP/IP port forwardings. */
827: for (i = 0; i < options.num_local_forwards; i++) {
828: debug("Connections to local port %d forwarded to remote address %.200s:%d",
829: options.local_forwards[i].port,
830: options.local_forwards[i].host,
831: options.local_forwards[i].host_port);
1.158 markus 832: success += channel_setup_local_fwd_listener(
1.70 markus 833: options.local_forwards[i].port,
834: options.local_forwards[i].host,
835: options.local_forwards[i].host_port,
836: options.gateway_ports);
837: }
1.86 markus 838: if (i > 0 && success == 0)
839: error("Could not request local forwarding.");
1.70 markus 840:
841: /* Initiate remote TCP/IP port forwardings. */
842: for (i = 0; i < options.num_remote_forwards; i++) {
843: debug("Connections to remote port %d forwarded to local address %.200s:%d",
844: options.remote_forwards[i].port,
845: options.remote_forwards[i].host,
846: options.remote_forwards[i].host_port);
847: channel_request_remote_forwarding(
848: options.remote_forwards[i].port,
849: options.remote_forwards[i].host,
850: options.remote_forwards[i].host_port);
851: }
852: }
853:
1.126 itojun 854: static void
1.70 markus 855: check_agent_present(void)
856: {
857: if (options.forward_agent) {
858: /* Clear agent forwarding if we don\'t have an agent. */
1.185 stevesk 859: if (!ssh_agent_present())
1.70 markus 860: options.forward_agent = 0;
861: }
862: }
863:
1.126 itojun 864: static int
1.45 markus 865: ssh_session(void)
866: {
867: int type;
868: int interactive = 0;
869: int have_tty = 0;
870: struct winsize ws;
871: char *cp;
872:
1.31 markus 873: /* Enable compression if requested. */
874: if (options.compression) {
875: debug("Requesting compression at level %d.", options.compression_level);
876:
877: if (options.compression_level < 1 || options.compression_level > 9)
878: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
879:
880: /* Send the request. */
881: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
882: packet_put_int(options.compression_level);
883: packet_send();
884: packet_write_wait();
1.156 markus 885: type = packet_read();
1.31 markus 886: if (type == SSH_SMSG_SUCCESS)
887: packet_start_compression(options.compression_level);
888: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 889: logit("Warning: Remote host refused compression.");
1.31 markus 890: else
891: packet_disconnect("Protocol error waiting for compression response.");
892: }
893: /* Allocate a pseudo tty if appropriate. */
894: if (tty_flag) {
895: debug("Requesting pty.");
896:
897: /* Start the packet. */
898: packet_start(SSH_CMSG_REQUEST_PTY);
899:
900: /* Store TERM in the packet. There is no limit on the
901: length of the string. */
902: cp = getenv("TERM");
903: if (!cp)
904: cp = "";
1.124 markus 905: packet_put_cstring(cp);
1.31 markus 906:
907: /* Store window size in the packet. */
908: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
909: memset(&ws, 0, sizeof(ws));
910: packet_put_int(ws.ws_row);
911: packet_put_int(ws.ws_col);
912: packet_put_int(ws.ws_xpixel);
913: packet_put_int(ws.ws_ypixel);
914:
915: /* Store tty modes in the packet. */
1.115 stevesk 916: tty_make_modes(fileno(stdin), NULL);
1.31 markus 917:
918: /* Send the packet, and wait for it to leave. */
919: packet_send();
920: packet_write_wait();
921:
922: /* Read response from the server. */
1.156 markus 923: type = packet_read();
1.43 markus 924: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 925: interactive = 1;
1.45 markus 926: have_tty = 1;
1.43 markus 927: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 928: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 929: else
930: packet_disconnect("Protocol error waiting for pty request response.");
931: }
932: /* Request X11 forwarding if enabled and DISPLAY is set. */
933: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.150 stevesk 934: char *proto, *data;
1.50 markus 935: /* Get reasonable local authentication information. */
1.150 stevesk 936: x11_get_proto(&proto, &data);
1.50 markus 937: /* Request forwarding with authentication spoofing. */
1.31 markus 938: debug("Requesting X11 forwarding with authentication spoofing.");
1.50 markus 939: x11_request_forwarding_with_spoofing(0, proto, data);
1.31 markus 940:
941: /* Read response from the server. */
1.156 markus 942: type = packet_read();
1.31 markus 943: if (type == SSH_SMSG_SUCCESS) {
944: interactive = 1;
1.50 markus 945: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 946: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 947: } else {
1.31 markus 948: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 949: }
1.31 markus 950: }
951: /* Tell the packet module whether this is an interactive session. */
1.80 markus 952: packet_set_interactive(interactive);
1.31 markus 953:
954: /* Request authentication agent forwarding if appropriate. */
1.70 markus 955: check_agent_present();
956:
1.31 markus 957: if (options.forward_agent) {
958: debug("Requesting authentication agent forwarding.");
959: auth_request_forwarding();
960:
961: /* Read response from the server. */
1.156 markus 962: type = packet_read();
1.155 markus 963: packet_check_eom();
1.31 markus 964: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 965: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 966: }
967:
1.70 markus 968: /* Initiate port forwardings. */
969: ssh_init_forwarding();
1.34 markus 970:
971: /* If requested, let ssh continue in the background. */
1.48 markus 972: if (fork_after_authentication_flag)
1.34 markus 973: if (daemon(1, 1) < 0)
974: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 975:
1.33 markus 976: /*
977: * If a command was specified on the command line, execute the
978: * command now. Otherwise request the server to start a shell.
979: */
1.31 markus 980: if (buffer_len(&command) > 0) {
981: int len = buffer_len(&command);
982: if (len > 900)
983: len = 900;
1.152 stevesk 984: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 985: packet_start(SSH_CMSG_EXEC_CMD);
986: packet_put_string(buffer_ptr(&command), buffer_len(&command));
987: packet_send();
988: packet_write_wait();
989: } else {
990: debug("Requesting shell.");
991: packet_start(SSH_CMSG_EXEC_SHELL);
992: packet_send();
993: packet_write_wait();
994: }
995:
996: /* Enter the interactive session. */
1.119 stevesk 997: return client_loop(have_tty, tty_flag ?
998: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 999: }
1000:
1.126 itojun 1001: static void
1.157 markus 1002: client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 1003: {
1004: int id, len;
1005:
1006: id = packet_get_int();
1007: len = buffer_len(&command);
1.100 markus 1008: if (len > 900)
1009: len = 900;
1.155 markus 1010: packet_check_eom();
1.89 markus 1011: if (type == SSH2_MSG_CHANNEL_FAILURE)
1012: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 1013: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 1014: }
1015:
1016: void
1.201.2.1! brad 1017: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 1018: {
1019: int i;
1020:
1021: i = client_global_request_id++;
1.201.2.1! brad 1022: if (i >= options.num_remote_forwards)
1.170 markus 1023: return;
1024: debug("remote forward %s for: listen %d, connect %s:%d",
1025: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1026: options.remote_forwards[i].port,
1027: options.remote_forwards[i].host,
1028: options.remote_forwards[i].host_port);
1029: if (type == SSH2_MSG_REQUEST_FAILURE)
1.191 itojun 1030: logit("Warning: remote port forwarding failed for listen port %d",
1.170 markus 1031: options.remote_forwards[i].port);
1.89 markus 1032: }
1033:
1.143 markus 1034: /* request pty/x11/agent/tcpfwd/shell for channel */
1.126 itojun 1035: static void
1.143 markus 1036: ssh_session2_setup(int id, void *arg)
1.45 markus 1037: {
1038: int len;
1.80 markus 1039: int interactive = 0;
1.115 stevesk 1040: struct termios tio;
1.80 markus 1041:
1.187 markus 1042: debug2("ssh_session2_setup: id %d", id);
1.31 markus 1043:
1.45 markus 1044: if (tty_flag) {
1045: struct winsize ws;
1046: char *cp;
1047: cp = getenv("TERM");
1048: if (!cp)
1049: cp = "";
1050: /* Store window size in the packet. */
1051: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1052: memset(&ws, 0, sizeof(ws));
1053:
1054: channel_request_start(id, "pty-req", 0);
1055: packet_put_cstring(cp);
1056: packet_put_int(ws.ws_col);
1057: packet_put_int(ws.ws_row);
1058: packet_put_int(ws.ws_xpixel);
1059: packet_put_int(ws.ws_ypixel);
1.115 stevesk 1060: tio = get_saved_tio();
1061: tty_make_modes(/*ignored*/ 0, &tio);
1.45 markus 1062: packet_send();
1.80 markus 1063: interactive = 1;
1.45 markus 1064: /* XXX wait for reply */
1065: }
1.50 markus 1066: if (options.forward_x11 &&
1067: getenv("DISPLAY") != NULL) {
1.150 stevesk 1068: char *proto, *data;
1.50 markus 1069: /* Get reasonable local authentication information. */
1.150 stevesk 1070: x11_get_proto(&proto, &data);
1.50 markus 1071: /* Request forwarding with authentication spoofing. */
1072: debug("Requesting X11 forwarding with authentication spoofing.");
1073: x11_request_forwarding_with_spoofing(id, proto, data);
1.80 markus 1074: interactive = 1;
1.50 markus 1075: /* XXX wait for reply */
1076: }
1077:
1.70 markus 1078: check_agent_present();
1079: if (options.forward_agent) {
1080: debug("Requesting authentication agent forwarding.");
1081: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1082: packet_send();
1083: }
1084:
1.45 markus 1085: len = buffer_len(&command);
1086: if (len > 0) {
1087: if (len > 900)
1088: len = 900;
1.85 djm 1089: if (subsystem_flag) {
1.152 stevesk 1090: debug("Sending subsystem: %.*s", len, (u_char *)buffer_ptr(&command));
1.89 markus 1091: channel_request_start(id, "subsystem", /*want reply*/ 1);
1092: /* register callback for reply */
1.172 deraadt 1093: /* XXX we assume that client_loop has already been called */
1.89 markus 1094: dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
1095: dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
1.85 djm 1096: } else {
1.152 stevesk 1097: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.85 djm 1098: channel_request_start(id, "exec", 0);
1099: }
1.100 markus 1100: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1.45 markus 1101: packet_send();
1102: } else {
1.161 markus 1103: channel_request_start(id, "shell", 0);
1104: packet_send();
1.45 markus 1105: }
1.90 markus 1106:
1.80 markus 1107: packet_set_interactive(interactive);
1.45 markus 1108: }
1109:
1.143 markus 1110: /* open new channel for a session */
1.126 itojun 1111: static int
1.143 markus 1112: ssh_session2_open(void)
1.45 markus 1113: {
1.118 markus 1114: Channel *c;
1115: int window, packetmax, in, out, err;
1.60 markus 1116:
1.62 markus 1117: if (stdin_null_flag) {
1.93 itojun 1118: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1119: } else {
1120: in = dup(STDIN_FILENO);
1121: }
1.60 markus 1122: out = dup(STDOUT_FILENO);
1123: err = dup(STDERR_FILENO);
1.45 markus 1124:
1125: if (in < 0 || out < 0 || err < 0)
1.62 markus 1126: fatal("dup() in/out/err failed");
1.45 markus 1127:
1.69 markus 1128: /* enable nonblocking unless tty */
1129: if (!isatty(in))
1130: set_nonblock(in);
1131: if (!isatty(out))
1132: set_nonblock(out);
1133: if (!isatty(err))
1134: set_nonblock(err);
1135:
1.65 markus 1136: window = CHAN_SES_WINDOW_DEFAULT;
1137: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1138: if (tty_flag) {
1139: window >>= 1;
1140: packetmax >>= 1;
1.45 markus 1141: }
1.118 markus 1142: c = channel_new(
1.45 markus 1143: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1144: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1145: "client-session", /*nonblock*/0);
1.45 markus 1146:
1.143 markus 1147: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1148:
1.122 markus 1149: channel_send_open(c->self);
1.143 markus 1150: if (!no_shell_flag)
1.160 markus 1151: channel_register_confirm(c->self, ssh_session2_setup);
1.106 markus 1152:
1.118 markus 1153: return c->self;
1.106 markus 1154: }
1155:
1.126 itojun 1156: static int
1.106 markus 1157: ssh_session2(void)
1158: {
1.143 markus 1159: int id = -1;
1.106 markus 1160:
1161: /* XXX should be pre-session */
1162: ssh_init_forwarding();
1163:
1.143 markus 1164: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1165: id = ssh_session2_open();
1.106 markus 1166:
1167: /* If requested, let ssh continue in the background. */
1168: if (fork_after_authentication_flag)
1169: if (daemon(1, 1) < 0)
1170: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1171:
1.119 stevesk 1172: return client_loop(tty_flag, tty_flag ?
1173: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1174: }
1175:
1.126 itojun 1176: static void
1.104 markus 1177: load_public_identity_files(void)
1178: {
1179: char *filename;
1.167 markus 1180: int i = 0;
1.104 markus 1181: Key *public;
1.167 markus 1182: #ifdef SMARTCARD
1183: Key **keys;
1.104 markus 1184:
1.133 markus 1185: if (options.smartcard_device != NULL &&
1.167 markus 1186: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1187: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1188: int count = 0;
1189: for (i = 0; keys[i] != NULL; i++) {
1190: count++;
1191: memmove(&options.identity_files[1], &options.identity_files[0],
1192: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1193: memmove(&options.identity_keys[1], &options.identity_keys[0],
1194: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1195: options.num_identity_files++;
1196: options.identity_keys[0] = keys[i];
1.194 markus 1197: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1198: }
1.168 markus 1199: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1200: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1201: i = count;
1202: xfree(keys);
1.127 markus 1203: }
1.136 jakob 1204: #endif /* SMARTCARD */
1.131 millert 1205: for (; i < options.num_identity_files; i++) {
1206: filename = tilde_expand_filename(options.identity_files[i],
1207: original_real_uid);
1208: public = key_load_public(filename, NULL);
1209: debug("identity file %s type %d", filename,
1210: public ? public->type : -1);
1211: xfree(options.identity_files[i]);
1212: options.identity_files[i] = filename;
1213: options.identity_keys[i] = public;
1214: }
1.1 deraadt 1215: }