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