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