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