Annotation of src/usr.bin/ssh/ssh.c, Revision 1.314
1.314 ! djm 1: /* $OpenBSD: ssh.c,v 1.313 2008/05/09 14:26:08 djm 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.31 markus 101: /* Flag indicating whether debug mode is on. This can 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 */
158: static int client_global_request_id = 0;
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:
251: /* Initialize option structure to indicate that no values have been set. */
252: initialize_options(&options);
253:
254: /* Parse command-line arguments. */
255: host = NULL;
256:
1.266 djm 257: again:
1.128 fgsch 258: while ((opt = getopt(ac, av,
1.297 djm 259: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:KL:MNO:PR:S:TVw:XY")) != -1) {
1.31 markus 260: switch (opt) {
1.91 jakob 261: case '1':
262: options.protocol = SSH_PROTO_1;
263: break;
1.47 markus 264: case '2':
265: options.protocol = SSH_PROTO_2;
266: break;
1.37 markus 267: case '4':
1.196 djm 268: options.address_family = AF_INET;
1.37 markus 269: break;
270: case '6':
1.196 djm 271: options.address_family = AF_INET6;
1.37 markus 272: break;
1.31 markus 273: case 'n':
274: stdin_null_flag = 1;
275: break;
276: case 'f':
277: fork_after_authentication_flag = 1;
278: stdin_null_flag = 1;
279: break;
280: case 'x':
281: options.forward_x11 = 0;
282: break;
283: case 'X':
284: options.forward_x11 = 1;
285: break;
1.202 markus 286: case 'Y':
287: options.forward_x11 = 1;
288: options.forward_x11_trusted = 1;
289: break;
1.31 markus 290: case 'g':
291: options.gateway_ports = 1;
292: break;
1.229 djm 293: case 'O':
294: if (strcmp(optarg, "check") == 0)
1.312 djm 295: muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
1.229 djm 296: else if (strcmp(optarg, "exit") == 0)
1.312 djm 297: muxclient_command = SSHMUX_COMMAND_TERMINATE;
1.229 djm 298: else
299: fatal("Invalid multiplex command.");
300: break;
1.183 stevesk 301: case 'P': /* deprecated */
1.31 markus 302: options.use_privileged_port = 0;
303: break;
304: case 'a':
305: options.forward_agent = 0;
1.53 markus 306: break;
307: case 'A':
308: options.forward_agent = 1;
1.31 markus 309: break;
310: case 'k':
1.204 dtucker 311: options.gss_deleg_creds = 0;
1.297 djm 312: break;
313: case 'K':
314: options.gss_authentication = 1;
315: options.gss_deleg_creds = 1;
1.31 markus 316: break;
317: case 'i':
318: if (stat(optarg, &st) < 0) {
1.128 fgsch 319: fprintf(stderr, "Warning: Identity file %s "
1.231 otto 320: "not accessible: %s.\n", optarg,
321: strerror(errno));
1.31 markus 322: break;
323: }
1.128 fgsch 324: if (options.num_identity_files >=
325: SSH_MAX_IDENTITY_FILES)
326: fatal("Too many identity files specified "
327: "(max %d)", SSH_MAX_IDENTITY_FILES);
328: options.identity_files[options.num_identity_files++] =
329: xstrdup(optarg);
1.31 markus 330: break;
1.127 markus 331: case 'I':
332: #ifdef SMARTCARD
1.133 markus 333: options.smartcard_device = xstrdup(optarg);
1.137 jakob 334: #else
1.127 markus 335: fprintf(stderr, "no support for smartcards.\n");
1.137 jakob 336: #endif
1.127 markus 337: break;
1.31 markus 338: case 't':
1.79 markus 339: if (tty_flag)
340: force_tty_flag = 1;
1.31 markus 341: tty_flag = 1;
342: break;
343: case 'v':
1.197 markus 344: if (debug_flag == 0) {
1.66 markus 345: debug_flag = 1;
346: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 347: } else {
348: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
349: options.log_level++;
1.66 markus 350: break;
1.197 markus 351: }
1.227 deraadt 352: /* FALLTHROUGH */
1.31 markus 353: case 'V':
1.209 markus 354: fprintf(stderr, "%s, %s\n",
355: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 356: if (opt == 'V')
357: exit(0);
358: break;
1.255 reyk 359: case 'w':
1.256 reyk 360: if (options.tun_open == -1)
361: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 362: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 363: if (options.tun_local == SSH_TUNID_ERR) {
1.255 reyk 364: fprintf(stderr, "Bad tun device '%s'\n", optarg);
1.257 dtucker 365: exit(255);
1.255 reyk 366: }
367: break;
1.31 markus 368: case 'q':
369: options.log_level = SYSLOG_LEVEL_QUIET;
370: break;
371: case 'e':
372: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 373: (u_char) optarg[1] >= 64 &&
374: (u_char) optarg[1] < 128)
1.78 markus 375: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 376: else if (strlen(optarg) == 1)
1.78 markus 377: options.escape_char = (u_char) optarg[0];
1.31 markus 378: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 379: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 380: else {
1.128 fgsch 381: fprintf(stderr, "Bad escape character '%s'.\n",
382: optarg);
1.257 dtucker 383: exit(255);
1.31 markus 384: }
385: break;
386: case 'c':
1.49 markus 387: if (ciphers_valid(optarg)) {
388: /* SSH2 only */
389: options.ciphers = xstrdup(optarg);
1.224 markus 390: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 391: } else {
392: /* SSH1 only */
1.74 markus 393: options.cipher = cipher_number(optarg);
394: if (options.cipher == -1) {
1.128 fgsch 395: fprintf(stderr,
396: "Unknown cipher type '%s'\n",
397: optarg);
1.257 dtucker 398: exit(255);
1.49 markus 399: }
1.128 fgsch 400: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 401: options.ciphers = "3des-cbc";
1.128 fgsch 402: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 403: options.ciphers = "blowfish-cbc";
1.128 fgsch 404: else
1.74 markus 405: options.ciphers = (char *)-1;
1.95 markus 406: }
407: break;
408: case 'm':
409: if (mac_valid(optarg))
410: options.macs = xstrdup(optarg);
411: else {
1.128 fgsch 412: fprintf(stderr, "Unknown mac type '%s'\n",
413: optarg);
1.257 dtucker 414: exit(255);
1.31 markus 415: }
416: break;
1.214 djm 417: case 'M':
1.242 djm 418: if (options.control_master == SSHCTL_MASTER_YES)
419: options.control_master = SSHCTL_MASTER_ASK;
420: else
421: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 422: break;
1.31 markus 423: case 'p':
1.113 stevesk 424: options.port = a2port(optarg);
425: if (options.port == 0) {
1.109 markus 426: fprintf(stderr, "Bad port '%s'\n", optarg);
1.257 dtucker 427: exit(255);
1.109 markus 428: }
1.31 markus 429: break;
430: case 'l':
431: options.user = optarg;
432: break;
1.141 stevesk 433:
434: case 'L':
1.232 djm 435: if (parse_forward(&fwd, optarg))
436: add_local_forward(&options, &fwd);
437: else {
1.128 fgsch 438: fprintf(stderr,
1.232 djm 439: "Bad local forwarding specification '%s'\n",
1.128 fgsch 440: optarg);
1.257 dtucker 441: exit(255);
1.31 markus 442: }
1.232 djm 443: break;
444:
445: case 'R':
446: if (parse_forward(&fwd, optarg)) {
447: add_remote_forward(&options, &fwd);
448: } else {
1.128 fgsch 449: fprintf(stderr,
1.232 djm 450: "Bad remote forwarding specification "
451: "'%s'\n", optarg);
1.257 dtucker 452: exit(255);
1.31 markus 453: }
454: break;
1.108 markus 455:
456: case 'D':
1.232 djm 457: cp = p = xstrdup(optarg);
458: memset(&fwd, '\0', sizeof(fwd));
459: fwd.connect_host = "socks";
460: if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
461: fprintf(stderr, "Bad dynamic forwarding "
462: "specification '%.100s'\n", optarg);
1.257 dtucker 463: exit(255);
1.232 djm 464: }
465: if (cp != NULL) {
466: fwd.listen_port = a2port(cp);
467: fwd.listen_host = cleanhostname(fwd.listen_host);
468: } else {
469: fwd.listen_port = a2port(fwd.listen_host);
1.249 djm 470: fwd.listen_host = NULL;
1.232 djm 471: }
472:
473: if (fwd.listen_port == 0) {
1.128 fgsch 474: fprintf(stderr, "Bad dynamic port '%s'\n",
475: optarg);
1.257 dtucker 476: exit(255);
1.109 markus 477: }
1.232 djm 478: add_local_forward(&options, &fwd);
479: xfree(p);
1.108 markus 480: break;
481:
1.31 markus 482: case 'C':
483: options.compression = 1;
484: break;
1.45 markus 485: case 'N':
486: no_shell_flag = 1;
487: no_tty_flag = 1;
488: break;
489: case 'T':
490: no_tty_flag = 1;
491: break;
1.31 markus 492: case 'o':
493: dummy = 1;
1.205 markus 494: line = xstrdup(optarg);
1.128 fgsch 495: if (process_config_line(&options, host ? host : "",
1.205 markus 496: line, "command-line", 0, &dummy) != 0)
1.257 dtucker 497: exit(255);
1.205 markus 498: xfree(line);
1.31 markus 499: break;
1.85 djm 500: case 's':
501: subsystem_flag = 1;
1.117 markus 502: break;
1.214 djm 503: case 'S':
504: if (options.control_path != NULL)
505: free(options.control_path);
506: options.control_path = xstrdup(optarg);
507: break;
1.117 markus 508: case 'b':
509: options.bind_address = optarg;
1.85 djm 510: break;
1.139 markus 511: case 'F':
512: config = optarg;
513: break;
1.31 markus 514: default:
515: usage();
1.1 deraadt 516: }
1.31 markus 517: }
518:
1.128 fgsch 519: ac -= optind;
520: av += optind;
521:
522: if (ac > 0 && !host && **av != '-') {
1.188 markus 523: if (strrchr(*av, '@')) {
1.128 fgsch 524: p = xstrdup(*av);
1.188 markus 525: cp = strrchr(p, '@');
1.128 fgsch 526: if (cp == NULL || cp == p)
527: usage();
528: options.user = p;
529: *cp = '\0';
530: host = ++cp;
531: } else
532: host = *av;
1.189 millert 533: if (ac > 1) {
534: optind = optreset = 1;
1.128 fgsch 535: goto again;
536: }
1.189 millert 537: ac--, av++;
1.128 fgsch 538: }
539:
1.31 markus 540: /* Check that we got a host name. */
541: if (!host)
542: usage();
543:
1.54 markus 544: SSLeay_add_all_algorithms();
1.72 markus 545: ERR_load_crypto_strings();
1.31 markus 546:
547: /* Initialize the command to execute on remote host. */
548: buffer_init(&command);
1.1 deraadt 549:
1.33 markus 550: /*
551: * Save the command to execute on the remote host in a buffer. There
552: * is no limit on the length of the command, except by the maximum
553: * packet size. Also sets the tty flag if there is no command.
554: */
1.128 fgsch 555: if (!ac) {
1.31 markus 556: /* No command specified - execute shell on a tty. */
557: tty_flag = 1;
1.85 djm 558: if (subsystem_flag) {
1.128 fgsch 559: fprintf(stderr,
560: "You must specify a subsystem to invoke.\n");
1.85 djm 561: usage();
562: }
1.31 markus 563: } else {
1.128 fgsch 564: /* A command has been specified. Store it into the buffer. */
565: for (i = 0; i < ac; i++) {
566: if (i)
1.31 markus 567: buffer_append(&command, " ", 1);
568: buffer_append(&command, av[i], strlen(av[i]));
569: }
570: }
571:
572: /* Cannot fork to background if no command. */
1.63 markus 573: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 574: fatal("Cannot fork into background without a command to execute.");
575:
576: /* Allocate a tty by default if no command specified. */
577: if (buffer_len(&command) == 0)
578: tty_flag = 1;
579:
1.180 deraadt 580: /* Force no tty */
1.75 markus 581: if (no_tty_flag)
582: tty_flag = 0;
1.31 markus 583: /* Do not allocate a tty if stdin is not a tty. */
1.236 djm 584: if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
1.31 markus 585: if (tty_flag)
1.191 itojun 586: logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 587: tty_flag = 0;
588: }
1.45 markus 589:
1.101 markus 590: /*
591: * Initialize "log" output. Since we are the client all output
592: * actually goes to stderr.
593: */
1.111 markus 594: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
595: SYSLOG_FACILITY_USER, 1);
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.139 markus 606: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
607: _PATH_SSH_USER_CONFFILE);
1.210 djm 608: (void)read_config_file(buf, host, &options, 1);
1.139 markus 609:
1.145 markus 610: /* Read systemwide configuration file after use config. */
1.223 deraadt 611: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
1.210 djm 612: &options, 0);
1.139 markus 613: }
1.31 markus 614:
615: /* Fill configuration defaults. */
616: fill_default_options(&options);
617:
1.196 djm 618: channel_set_af(options.address_family);
619:
1.31 markus 620: /* reinit */
1.101 markus 621: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 622:
623: if (options.user == NULL)
624: options.user = xstrdup(pw->pw_name);
625:
626: if (options.hostname != NULL)
627: host = options.hostname;
1.195 markus 628:
629: /* force lowercase for hostkey matching */
630: if (options.host_key_alias != NULL) {
631: for (p = options.host_key_alias; *p; p++)
632: if (isupper(*p))
1.271 deraadt 633: *p = (char)tolower(*p);
1.195 markus 634: }
1.190 markus 635:
1.246 djm 636: /* Get default port if port has not been set. */
637: if (options.port == 0) {
638: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
639: options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
640: }
641:
1.190 markus 642: if (options.proxy_command != NULL &&
1.304 dtucker 643: strcmp(options.proxy_command, "none") == 0) {
644: xfree(options.proxy_command);
1.190 markus 645: options.proxy_command = NULL;
1.304 dtucker 646: }
1.245 djm 647: if (options.control_path != NULL &&
1.304 dtucker 648: strcmp(options.control_path, "none") == 0) {
649: xfree(options.control_path);
1.245 djm 650: options.control_path = NULL;
1.304 dtucker 651: }
1.31 markus 652:
1.214 djm 653: if (options.control_path != NULL) {
1.275 djm 654: char thishost[NI_MAXHOST];
1.261 djm 655:
1.275 djm 656: if (gethostname(thishost, sizeof(thishost)) == -1)
1.261 djm 657: fatal("gethostname: %s", strerror(errno));
1.241 djm 658: snprintf(buf, sizeof(buf), "%d", options.port);
659: cp = tilde_expand_filename(options.control_path,
660: original_real_uid);
1.304 dtucker 661: xfree(options.control_path);
1.241 djm 662: options.control_path = percent_expand(cp, "p", buf, "h", host,
1.275 djm 663: "r", options.user, "l", thishost, (char *)NULL);
1.241 djm 664: xfree(cp);
1.214 djm 665: }
1.312 djm 666: if (muxclient_command != 0 && options.control_path == NULL)
1.240 djm 667: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 668: if (options.control_path != NULL)
1.312 djm 669: muxclient(options.control_path);
1.214 djm 670:
1.303 djm 671: timeout_ms = options.connection_timeout * 1000;
672:
1.77 markus 673: /* Open a connection to the remote host. */
1.203 djm 674: if (ssh_connect(host, &hostaddr, options.port,
1.303 djm 675: options.address_family, options.connection_attempts, &timeout_ms,
676: options.tcp_keep_alive,
1.177 markus 677: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 678: options.proxy_command) != 0)
1.257 dtucker 679: exit(255);
1.31 markus 680:
1.303 djm 681: if (timeout_ms > 0)
682: debug3("timeout: %d ms remain after connect", timeout_ms);
683:
1.33 markus 684: /*
685: * If we successfully made the connection, load the host private key
686: * in case we will need it later for combined rsa-rhosts
687: * authentication. This must be done before releasing extra
688: * privileges, because the file is only readable by root.
1.174 markus 689: * If we cannot access the private keys, load the public keys
690: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 691: */
1.112 markus 692: sensitive_data.nkeys = 0;
693: sensitive_data.keys = NULL;
1.173 markus 694: sensitive_data.external_keysign = 0;
1.178 markus 695: if (options.rhosts_rsa_authentication ||
696: options.hostbased_authentication) {
1.112 markus 697: sensitive_data.nkeys = 3;
1.274 deraadt 698: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 699: sizeof(Key));
1.177 markus 700:
701: PRIV_START;
1.112 markus 702: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276 dtucker 703: _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.112 markus 704: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
1.276 dtucker 705: _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.112 markus 706: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
1.276 dtucker 707: _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.177 markus 708: PRIV_END;
1.173 markus 709:
1.181 markus 710: if (options.hostbased_authentication == 1 &&
711: sensitive_data.keys[0] == NULL &&
1.173 markus 712: sensitive_data.keys[1] == NULL &&
713: sensitive_data.keys[2] == NULL) {
714: sensitive_data.keys[1] = key_load_public(
715: _PATH_HOST_DSA_KEY_FILE, NULL);
716: sensitive_data.keys[2] = key_load_public(
717: _PATH_HOST_RSA_KEY_FILE, NULL);
718: sensitive_data.external_keysign = 1;
719: }
1.31 markus 720: }
1.33 markus 721: /*
722: * Get rid of any extra privileges that we may have. We will no
723: * longer need them. Also, extra privileges could make it very hard
724: * to read identity files and other non-world-readable files from the
725: * user's home directory if it happens to be on a NFS volume where
726: * root is mapped to nobody.
727: */
1.225 dtucker 728: if (original_effective_uid == 0) {
729: PRIV_START;
730: permanently_set_uid(pw);
731: }
1.31 markus 732:
1.33 markus 733: /*
734: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 735: * directory if it doesn't already exist.
1.33 markus 736: */
1.138 jakob 737: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 738: if (stat(buf, &st) < 0)
1.57 djm 739: if (mkdir(buf, 0700) < 0)
1.31 markus 740: error("Could not create directory '%.200s'.", buf);
741:
1.104 markus 742: /* load options.identity_files */
743: load_public_identity_files();
744:
745: /* Expand ~ in known host file names. */
746: /* XXX mem-leaks: */
1.72 markus 747: options.system_hostfile =
748: tilde_expand_filename(options.system_hostfile, original_real_uid);
749: options.user_hostfile =
750: tilde_expand_filename(options.user_hostfile, original_real_uid);
751: options.system_hostfile2 =
752: tilde_expand_filename(options.system_hostfile2, original_real_uid);
753: options.user_hostfile2 =
754: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 755:
756: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 757:
758: /* Log into the remote system. This never returns if the login fails. */
1.303 djm 759: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
760: pw, timeout_ms);
1.31 markus 761:
1.112 markus 762: /* We no longer need the private host keys. Clear them now. */
763: if (sensitive_data.nkeys != 0) {
764: for (i = 0; i < sensitive_data.nkeys; i++) {
765: if (sensitive_data.keys[i] != NULL) {
766: /* Destroys contents safely */
767: debug3("clear hostkey %d", i);
768: key_free(sensitive_data.keys[i]);
769: sensitive_data.keys[i] = NULL;
770: }
771: }
772: xfree(sensitive_data.keys);
1.134 markus 773: }
774: for (i = 0; i < options.num_identity_files; i++) {
775: if (options.identity_files[i]) {
776: xfree(options.identity_files[i]);
777: options.identity_files[i] = NULL;
778: }
779: if (options.identity_keys[i]) {
780: key_free(options.identity_keys[i]);
781: options.identity_keys[i] = NULL;
782: }
1.112 markus 783: }
1.31 markus 784:
1.45 markus 785: exit_status = compat20 ? ssh_session2() : ssh_session();
786: packet_close();
1.186 djm 787:
1.312 djm 788: if (options.control_path != NULL && muxserver_sock != -1)
1.214 djm 789: unlink(options.control_path);
790:
1.186 djm 791: /*
1.203 djm 792: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 793: * case it hangs and instead rely on init to reap the child
794: */
795: if (proxy_command_pid > 1)
796: kill(proxy_command_pid, SIGHUP);
797:
1.45 markus 798: return exit_status;
799: }
800:
1.126 itojun 801: static void
1.70 markus 802: ssh_init_forwarding(void)
803: {
1.86 markus 804: int success = 0;
1.70 markus 805: int i;
1.86 markus 806:
1.70 markus 807: /* Initiate local TCP/IP port forwardings. */
808: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 809: debug("Local connections to %.200s:%d forwarded to remote "
810: "address %.200s:%d",
1.234 deraadt 811: (options.local_forwards[i].listen_host == NULL) ?
812: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 813: options.local_forwards[i].listen_host,
814: options.local_forwards[i].listen_port,
815: options.local_forwards[i].connect_host,
816: options.local_forwards[i].connect_port);
1.158 markus 817: success += channel_setup_local_fwd_listener(
1.232 djm 818: options.local_forwards[i].listen_host,
819: options.local_forwards[i].listen_port,
820: options.local_forwards[i].connect_host,
821: options.local_forwards[i].connect_port,
1.70 markus 822: options.gateway_ports);
823: }
1.283 markus 824: if (i > 0 && success != i && options.exit_on_forward_failure)
825: fatal("Could not request local forwarding.");
1.86 markus 826: if (i > 0 && success == 0)
827: error("Could not request local forwarding.");
1.70 markus 828:
829: /* Initiate remote TCP/IP port forwardings. */
830: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 831: debug("Remote connections from %.200s:%d forwarded to "
832: "local address %.200s:%d",
1.248 djm 833: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 834: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 835: options.remote_forwards[i].listen_port,
836: options.remote_forwards[i].connect_host,
837: options.remote_forwards[i].connect_port);
1.283 markus 838: if (channel_request_remote_forwarding(
1.232 djm 839: options.remote_forwards[i].listen_host,
840: options.remote_forwards[i].listen_port,
841: options.remote_forwards[i].connect_host,
1.283 markus 842: options.remote_forwards[i].connect_port) < 0) {
843: if (options.exit_on_forward_failure)
844: fatal("Could not request remote forwarding.");
845: else
846: logit("Warning: Could not request remote "
847: "forwarding.");
848: }
1.70 markus 849: }
1.301 djm 850:
851: /* Initiate tunnel forwarding. */
852: if (options.tun_open != SSH_TUNMODE_NO) {
853: if (client_request_tun_fwd(options.tun_open,
854: options.tun_local, options.tun_remote) == -1) {
855: if (options.exit_on_forward_failure)
856: fatal("Could not request tunnel forwarding.");
857: else
858: error("Could not request tunnel forwarding.");
859: }
860: }
1.70 markus 861: }
862:
1.126 itojun 863: static void
1.70 markus 864: check_agent_present(void)
865: {
866: if (options.forward_agent) {
1.254 djm 867: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 868: if (!ssh_agent_present())
1.70 markus 869: options.forward_agent = 0;
870: }
871: }
872:
1.126 itojun 873: static int
1.45 markus 874: ssh_session(void)
875: {
876: int type;
877: int interactive = 0;
878: int have_tty = 0;
879: struct winsize ws;
880: char *cp;
1.243 djm 881: const char *display;
1.45 markus 882:
1.31 markus 883: /* Enable compression if requested. */
884: if (options.compression) {
885: debug("Requesting compression at level %d.", options.compression_level);
886:
887: if (options.compression_level < 1 || options.compression_level > 9)
888: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
889:
890: /* Send the request. */
891: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
892: packet_put_int(options.compression_level);
893: packet_send();
894: packet_write_wait();
1.156 markus 895: type = packet_read();
1.31 markus 896: if (type == SSH_SMSG_SUCCESS)
897: packet_start_compression(options.compression_level);
898: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 899: logit("Warning: Remote host refused compression.");
1.31 markus 900: else
901: packet_disconnect("Protocol error waiting for compression response.");
902: }
903: /* Allocate a pseudo tty if appropriate. */
904: if (tty_flag) {
905: debug("Requesting pty.");
906:
907: /* Start the packet. */
908: packet_start(SSH_CMSG_REQUEST_PTY);
909:
910: /* Store TERM in the packet. There is no limit on the
911: length of the string. */
912: cp = getenv("TERM");
913: if (!cp)
914: cp = "";
1.124 markus 915: packet_put_cstring(cp);
1.31 markus 916:
917: /* Store window size in the packet. */
918: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
919: memset(&ws, 0, sizeof(ws));
1.269 deraadt 920: packet_put_int((u_int)ws.ws_row);
921: packet_put_int((u_int)ws.ws_col);
922: packet_put_int((u_int)ws.ws_xpixel);
923: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 924:
925: /* Store tty modes in the packet. */
1.115 stevesk 926: tty_make_modes(fileno(stdin), NULL);
1.31 markus 927:
928: /* Send the packet, and wait for it to leave. */
929: packet_send();
930: packet_write_wait();
931:
932: /* Read response from the server. */
1.156 markus 933: type = packet_read();
1.43 markus 934: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 935: interactive = 1;
1.45 markus 936: have_tty = 1;
1.43 markus 937: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 938: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 939: else
940: packet_disconnect("Protocol error waiting for pty request response.");
941: }
942: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 943: display = getenv("DISPLAY");
944: if (options.forward_x11 && display != NULL) {
1.150 stevesk 945: char *proto, *data;
1.50 markus 946: /* Get reasonable local authentication information. */
1.243 djm 947: client_x11_get_proto(display, options.xauth_location,
948: options.forward_x11_trusted, &proto, &data);
1.50 markus 949: /* Request forwarding with authentication spoofing. */
1.31 markus 950: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 951: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 952:
953: /* Read response from the server. */
1.156 markus 954: type = packet_read();
1.31 markus 955: if (type == SSH_SMSG_SUCCESS) {
956: interactive = 1;
1.50 markus 957: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 958: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 959: } else {
1.31 markus 960: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 961: }
1.31 markus 962: }
963: /* Tell the packet module whether this is an interactive session. */
1.80 markus 964: packet_set_interactive(interactive);
1.31 markus 965:
966: /* Request authentication agent forwarding if appropriate. */
1.70 markus 967: check_agent_present();
968:
1.31 markus 969: if (options.forward_agent) {
970: debug("Requesting authentication agent forwarding.");
971: auth_request_forwarding();
972:
973: /* Read response from the server. */
1.156 markus 974: type = packet_read();
1.155 markus 975: packet_check_eom();
1.31 markus 976: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 977: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 978: }
979:
1.70 markus 980: /* Initiate port forwardings. */
981: ssh_init_forwarding();
1.305 dtucker 982:
983: /* Execute a local command */
984: if (options.local_command != NULL &&
985: options.permit_local_command)
986: ssh_local_cmd(options.local_command);
1.34 markus 987:
988: /* If requested, let ssh continue in the background. */
1.48 markus 989: if (fork_after_authentication_flag)
1.34 markus 990: if (daemon(1, 1) < 0)
991: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 992:
1.33 markus 993: /*
994: * If a command was specified on the command line, execute the
995: * command now. Otherwise request the server to start a shell.
996: */
1.31 markus 997: if (buffer_len(&command) > 0) {
998: int len = buffer_len(&command);
999: if (len > 900)
1000: len = 900;
1.152 stevesk 1001: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 1002: packet_start(SSH_CMSG_EXEC_CMD);
1003: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1004: packet_send();
1005: packet_write_wait();
1006: } else {
1007: debug("Requesting shell.");
1008: packet_start(SSH_CMSG_EXEC_SHELL);
1009: packet_send();
1010: packet_write_wait();
1011: }
1012:
1013: /* Enter the interactive session. */
1.119 stevesk 1014: return client_loop(have_tty, tty_flag ?
1015: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 1016: }
1017:
1.170 markus 1018: void
1.206 markus 1019: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 1020: {
1021: int i;
1022:
1023: i = client_global_request_id++;
1.206 markus 1024: if (i >= options.num_remote_forwards)
1.170 markus 1025: return;
1026: debug("remote forward %s for: listen %d, connect %s:%d",
1027: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.232 djm 1028: options.remote_forwards[i].listen_port,
1029: options.remote_forwards[i].connect_host,
1030: options.remote_forwards[i].connect_port);
1.283 markus 1031: if (type == SSH2_MSG_REQUEST_FAILURE) {
1032: if (options.exit_on_forward_failure)
1033: fatal("Error: remote port forwarding failed for "
1034: "listen port %d",
1035: options.remote_forwards[i].listen_port);
1036: else
1037: logit("Warning: remote port forwarding failed for "
1038: "listen port %d",
1039: options.remote_forwards[i].listen_port);
1040: }
1.89 markus 1041: }
1042:
1.214 djm 1043: /* request pty/x11/agent/tcpfwd/shell for channel */
1044: static void
1045: ssh_session2_setup(int id, void *arg)
1046: {
1.215 djm 1047: extern char **environ;
1.243 djm 1048: const char *display;
1049: int interactive = tty_flag;
1.215 djm 1050:
1.248 djm 1051: display = getenv("DISPLAY");
1.243 djm 1052: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1053: char *proto, *data;
1.50 markus 1054: /* Get reasonable local authentication information. */
1.243 djm 1055: client_x11_get_proto(display, options.xauth_location,
1056: options.forward_x11_trusted, &proto, &data);
1.50 markus 1057: /* Request forwarding with authentication spoofing. */
1058: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 1059: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1060: interactive = 1;
1.50 markus 1061: /* XXX wait for reply */
1062: }
1063:
1.70 markus 1064: check_agent_present();
1065: if (options.forward_agent) {
1066: debug("Requesting authentication agent forwarding.");
1067: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1068: packet_send();
1.212 djm 1069: }
1070:
1.214 djm 1071: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.311 djm 1072: NULL, fileno(stdin), &command, environ);
1.90 markus 1073:
1.80 markus 1074: packet_set_interactive(interactive);
1.45 markus 1075: }
1076:
1.143 markus 1077: /* open new channel for a session */
1.126 itojun 1078: static int
1.143 markus 1079: ssh_session2_open(void)
1.45 markus 1080: {
1.118 markus 1081: Channel *c;
1082: int window, packetmax, in, out, err;
1.60 markus 1083:
1.62 markus 1084: if (stdin_null_flag) {
1.93 itojun 1085: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1086: } else {
1087: in = dup(STDIN_FILENO);
1088: }
1.60 markus 1089: out = dup(STDOUT_FILENO);
1090: err = dup(STDERR_FILENO);
1.45 markus 1091:
1092: if (in < 0 || out < 0 || err < 0)
1.62 markus 1093: fatal("dup() in/out/err failed");
1.45 markus 1094:
1.69 markus 1095: /* enable nonblocking unless tty */
1096: if (!isatty(in))
1097: set_nonblock(in);
1098: if (!isatty(out))
1099: set_nonblock(out);
1100: if (!isatty(err))
1101: set_nonblock(err);
1102:
1.65 markus 1103: window = CHAN_SES_WINDOW_DEFAULT;
1104: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1105: if (tty_flag) {
1106: window >>= 1;
1107: packetmax >>= 1;
1.45 markus 1108: }
1.118 markus 1109: c = channel_new(
1.45 markus 1110: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1111: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1112: "client-session", /*nonblock*/0);
1.45 markus 1113:
1.143 markus 1114: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1115:
1.122 markus 1116: channel_send_open(c->self);
1.143 markus 1117: if (!no_shell_flag)
1.310 djm 1118: channel_register_open_confirm(c->self,
1119: ssh_session2_setup, NULL);
1.106 markus 1120:
1.118 markus 1121: return c->self;
1.106 markus 1122: }
1123:
1.126 itojun 1124: static int
1.106 markus 1125: ssh_session2(void)
1126: {
1.143 markus 1127: int id = -1;
1.106 markus 1128:
1129: /* XXX should be pre-session */
1130: ssh_init_forwarding();
1131:
1.143 markus 1132: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1133: id = ssh_session2_open();
1.314 ! djm 1134:
! 1135: /* If we don't expect to open a new session, then disallow it */
! 1136: if (options.control_master == SSHCTL_MASTER_NO) {
! 1137: debug("Requesting no-more-sessions@openssh.com");
! 1138: packet_start(SSH2_MSG_GLOBAL_REQUEST);
! 1139: packet_put_cstring("no-more-sessions@openssh.com");
! 1140: packet_put_char(0);
! 1141: packet_send();
! 1142: }
1.255 reyk 1143:
1144: /* Execute a local command */
1145: if (options.local_command != NULL &&
1146: options.permit_local_command)
1147: ssh_local_cmd(options.local_command);
1.301 djm 1148:
1149: /* Start listening for multiplex clients */
1.312 djm 1150: muxserver_listen();
1.106 markus 1151:
1152: /* If requested, let ssh continue in the background. */
1153: if (fork_after_authentication_flag)
1154: if (daemon(1, 1) < 0)
1155: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1156:
1.119 stevesk 1157: return client_loop(tty_flag, tty_flag ?
1158: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1159: }
1160:
1.126 itojun 1161: static void
1.104 markus 1162: load_public_identity_files(void)
1163: {
1.275 djm 1164: char *filename, *cp, thishost[NI_MAXHOST];
1.306 deraadt 1165: char *pwdir = NULL, *pwname = NULL;
1.167 markus 1166: int i = 0;
1.104 markus 1167: Key *public;
1.275 djm 1168: struct passwd *pw;
1.167 markus 1169: #ifdef SMARTCARD
1170: Key **keys;
1.104 markus 1171:
1.133 markus 1172: if (options.smartcard_device != NULL &&
1.167 markus 1173: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.292 stevesk 1174: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
1.167 markus 1175: int count = 0;
1176: for (i = 0; keys[i] != NULL; i++) {
1177: count++;
1178: memmove(&options.identity_files[1], &options.identity_files[0],
1179: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1180: memmove(&options.identity_keys[1], &options.identity_keys[0],
1181: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1182: options.num_identity_files++;
1183: options.identity_keys[0] = keys[i];
1.194 markus 1184: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1185: }
1.168 markus 1186: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1187: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1188: i = count;
1189: xfree(keys);
1.127 markus 1190: }
1.136 jakob 1191: #endif /* SMARTCARD */
1.275 djm 1192: if ((pw = getpwuid(original_real_uid)) == NULL)
1193: fatal("load_public_identity_files: getpwuid failed");
1.307 dtucker 1194: pwname = xstrdup(pw->pw_name);
1195: pwdir = xstrdup(pw->pw_dir);
1.275 djm 1196: if (gethostname(thishost, sizeof(thishost)) == -1)
1197: fatal("load_public_identity_files: gethostname: %s",
1198: strerror(errno));
1.131 millert 1199: for (; i < options.num_identity_files; i++) {
1.275 djm 1200: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1201: original_real_uid);
1.306 deraadt 1202: filename = percent_expand(cp, "d", pwdir,
1203: "u", pwname, "l", thishost, "h", host,
1.275 djm 1204: "r", options.user, (char *)NULL);
1205: xfree(cp);
1.131 millert 1206: public = key_load_public(filename, NULL);
1207: debug("identity file %s type %d", filename,
1208: public ? public->type : -1);
1209: xfree(options.identity_files[i]);
1210: options.identity_files[i] = filename;
1211: options.identity_keys[i] = public;
1212: }
1.306 deraadt 1213: bzero(pwname, strlen(pwname));
1.307 dtucker 1214: xfree(pwname);
1.306 deraadt 1215: bzero(pwdir, strlen(pwdir));
1.307 dtucker 1216: xfree(pwdir);
1.214 djm 1217: }
1218: