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