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