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