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