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