Annotation of src/usr.bin/ssh/ssh.c, Revision 1.316
1.316 ! djm 1: /* $OpenBSD: ssh.c,v 1.315 2008/06/12 04:06:00 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.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:
635: if (options.hostname != NULL)
636: host = options.hostname;
1.195 markus 637:
638: /* force lowercase for hostkey matching */
639: if (options.host_key_alias != NULL) {
640: for (p = options.host_key_alias; *p; p++)
641: if (isupper(*p))
1.271 deraadt 642: *p = (char)tolower(*p);
1.195 markus 643: }
1.190 markus 644:
1.246 djm 645: /* Get default port if port has not been set. */
646: if (options.port == 0) {
647: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
648: options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
649: }
650:
1.190 markus 651: if (options.proxy_command != NULL &&
1.304 dtucker 652: strcmp(options.proxy_command, "none") == 0) {
653: xfree(options.proxy_command);
1.190 markus 654: options.proxy_command = NULL;
1.304 dtucker 655: }
1.245 djm 656: if (options.control_path != NULL &&
1.304 dtucker 657: strcmp(options.control_path, "none") == 0) {
658: xfree(options.control_path);
1.245 djm 659: options.control_path = NULL;
1.304 dtucker 660: }
1.31 markus 661:
1.214 djm 662: if (options.control_path != NULL) {
1.275 djm 663: char thishost[NI_MAXHOST];
1.261 djm 664:
1.275 djm 665: if (gethostname(thishost, sizeof(thishost)) == -1)
1.261 djm 666: fatal("gethostname: %s", strerror(errno));
1.241 djm 667: snprintf(buf, sizeof(buf), "%d", options.port);
668: cp = tilde_expand_filename(options.control_path,
669: original_real_uid);
1.304 dtucker 670: xfree(options.control_path);
1.241 djm 671: options.control_path = percent_expand(cp, "p", buf, "h", host,
1.275 djm 672: "r", options.user, "l", thishost, (char *)NULL);
1.241 djm 673: xfree(cp);
1.214 djm 674: }
1.312 djm 675: if (muxclient_command != 0 && options.control_path == NULL)
1.240 djm 676: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 677: if (options.control_path != NULL)
1.312 djm 678: muxclient(options.control_path);
1.214 djm 679:
1.303 djm 680: timeout_ms = options.connection_timeout * 1000;
681:
1.77 markus 682: /* Open a connection to the remote host. */
1.203 djm 683: if (ssh_connect(host, &hostaddr, options.port,
1.303 djm 684: options.address_family, options.connection_attempts, &timeout_ms,
685: options.tcp_keep_alive,
1.177 markus 686: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 687: options.proxy_command) != 0)
1.257 dtucker 688: exit(255);
1.31 markus 689:
1.303 djm 690: if (timeout_ms > 0)
691: debug3("timeout: %d ms remain after connect", timeout_ms);
692:
1.33 markus 693: /*
694: * If we successfully made the connection, load the host private key
695: * in case we will need it later for combined rsa-rhosts
696: * authentication. This must be done before releasing extra
697: * privileges, because the file is only readable by root.
1.174 markus 698: * If we cannot access the private keys, load the public keys
699: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 700: */
1.112 markus 701: sensitive_data.nkeys = 0;
702: sensitive_data.keys = NULL;
1.173 markus 703: sensitive_data.external_keysign = 0;
1.178 markus 704: if (options.rhosts_rsa_authentication ||
705: options.hostbased_authentication) {
1.112 markus 706: sensitive_data.nkeys = 3;
1.274 deraadt 707: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 708: sizeof(Key));
1.177 markus 709:
710: PRIV_START;
1.112 markus 711: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276 dtucker 712: _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.112 markus 713: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
1.276 dtucker 714: _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.112 markus 715: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
1.276 dtucker 716: _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.177 markus 717: PRIV_END;
1.173 markus 718:
1.181 markus 719: if (options.hostbased_authentication == 1 &&
720: sensitive_data.keys[0] == NULL &&
1.173 markus 721: sensitive_data.keys[1] == NULL &&
722: sensitive_data.keys[2] == NULL) {
723: sensitive_data.keys[1] = key_load_public(
724: _PATH_HOST_DSA_KEY_FILE, NULL);
725: sensitive_data.keys[2] = key_load_public(
726: _PATH_HOST_RSA_KEY_FILE, NULL);
727: sensitive_data.external_keysign = 1;
728: }
1.31 markus 729: }
1.33 markus 730: /*
731: * Get rid of any extra privileges that we may have. We will no
732: * longer need them. Also, extra privileges could make it very hard
733: * to read identity files and other non-world-readable files from the
734: * user's home directory if it happens to be on a NFS volume where
735: * root is mapped to nobody.
736: */
1.225 dtucker 737: if (original_effective_uid == 0) {
738: PRIV_START;
739: permanently_set_uid(pw);
740: }
1.31 markus 741:
1.33 markus 742: /*
743: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 744: * directory if it doesn't already exist.
1.33 markus 745: */
1.316 ! djm 746: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir,
! 747: strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 748: if (stat(buf, &st) < 0)
1.57 djm 749: if (mkdir(buf, 0700) < 0)
1.31 markus 750: error("Could not create directory '%.200s'.", buf);
751:
1.104 markus 752: /* load options.identity_files */
753: load_public_identity_files();
754:
755: /* Expand ~ in known host file names. */
756: /* XXX mem-leaks: */
1.72 markus 757: options.system_hostfile =
758: tilde_expand_filename(options.system_hostfile, original_real_uid);
759: options.user_hostfile =
760: tilde_expand_filename(options.user_hostfile, original_real_uid);
761: options.system_hostfile2 =
762: tilde_expand_filename(options.system_hostfile2, original_real_uid);
763: options.user_hostfile2 =
764: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 765:
766: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 767:
1.316 ! djm 768: /* Log into the remote system. Never returns if the login fails. */
1.303 djm 769: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
770: pw, timeout_ms);
1.31 markus 771:
1.112 markus 772: /* We no longer need the private host keys. Clear them now. */
773: if (sensitive_data.nkeys != 0) {
774: for (i = 0; i < sensitive_data.nkeys; i++) {
775: if (sensitive_data.keys[i] != NULL) {
776: /* Destroys contents safely */
777: debug3("clear hostkey %d", i);
778: key_free(sensitive_data.keys[i]);
779: sensitive_data.keys[i] = NULL;
780: }
781: }
782: xfree(sensitive_data.keys);
1.134 markus 783: }
784: for (i = 0; i < options.num_identity_files; i++) {
785: if (options.identity_files[i]) {
786: xfree(options.identity_files[i]);
787: options.identity_files[i] = NULL;
788: }
789: if (options.identity_keys[i]) {
790: key_free(options.identity_keys[i]);
791: options.identity_keys[i] = NULL;
792: }
1.112 markus 793: }
1.31 markus 794:
1.45 markus 795: exit_status = compat20 ? ssh_session2() : ssh_session();
796: packet_close();
1.186 djm 797:
1.312 djm 798: if (options.control_path != NULL && muxserver_sock != -1)
1.214 djm 799: unlink(options.control_path);
800:
1.186 djm 801: /*
1.203 djm 802: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 803: * case it hangs and instead rely on init to reap the child
804: */
805: if (proxy_command_pid > 1)
806: kill(proxy_command_pid, SIGHUP);
807:
1.45 markus 808: return exit_status;
809: }
810:
1.315 djm 811: /* Callback for remote forward global requests */
812: static void
813: ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
814: {
815: Forward *rfwd = (Forward *)ctxt;
816:
817: debug("remote forward %s for: listen %d, connect %s:%d",
818: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
819: rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
820: if (type == SSH2_MSG_REQUEST_FAILURE) {
821: if (options.exit_on_forward_failure)
822: fatal("Error: remote port forwarding failed for "
823: "listen port %d", rfwd->listen_port);
824: else
825: logit("Warning: remote port forwarding failed for "
826: "listen port %d", rfwd->listen_port);
827: }
828: if (++remote_forward_confirms_received == options.num_remote_forwards)
829: debug("All remote forwarding requests processed");
830: /* XXX fork-after-authentication */
831: }
832:
1.126 itojun 833: static void
1.70 markus 834: ssh_init_forwarding(void)
835: {
1.86 markus 836: int success = 0;
1.70 markus 837: int i;
1.86 markus 838:
1.70 markus 839: /* Initiate local TCP/IP port forwardings. */
840: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 841: debug("Local connections to %.200s:%d forwarded to remote "
842: "address %.200s:%d",
1.234 deraadt 843: (options.local_forwards[i].listen_host == NULL) ?
844: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 845: options.local_forwards[i].listen_host,
846: options.local_forwards[i].listen_port,
847: options.local_forwards[i].connect_host,
848: options.local_forwards[i].connect_port);
1.158 markus 849: success += channel_setup_local_fwd_listener(
1.232 djm 850: options.local_forwards[i].listen_host,
851: options.local_forwards[i].listen_port,
852: options.local_forwards[i].connect_host,
853: options.local_forwards[i].connect_port,
1.70 markus 854: options.gateway_ports);
855: }
1.283 markus 856: if (i > 0 && success != i && options.exit_on_forward_failure)
857: fatal("Could not request local forwarding.");
1.86 markus 858: if (i > 0 && success == 0)
859: error("Could not request local forwarding.");
1.70 markus 860:
861: /* Initiate remote TCP/IP port forwardings. */
862: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 863: debug("Remote connections from %.200s:%d forwarded to "
864: "local address %.200s:%d",
1.248 djm 865: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 866: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 867: options.remote_forwards[i].listen_port,
868: options.remote_forwards[i].connect_host,
869: options.remote_forwards[i].connect_port);
1.283 markus 870: if (channel_request_remote_forwarding(
1.232 djm 871: options.remote_forwards[i].listen_host,
872: options.remote_forwards[i].listen_port,
873: options.remote_forwards[i].connect_host,
1.283 markus 874: options.remote_forwards[i].connect_port) < 0) {
875: if (options.exit_on_forward_failure)
876: fatal("Could not request remote forwarding.");
877: else
878: logit("Warning: Could not request remote "
879: "forwarding.");
880: }
1.315 djm 881: client_register_global_confirm(ssh_confirm_remote_forward,
882: &options.remote_forwards[i]);
1.70 markus 883: }
1.301 djm 884:
885: /* Initiate tunnel forwarding. */
886: if (options.tun_open != SSH_TUNMODE_NO) {
887: if (client_request_tun_fwd(options.tun_open,
888: options.tun_local, options.tun_remote) == -1) {
889: if (options.exit_on_forward_failure)
890: fatal("Could not request tunnel forwarding.");
891: else
892: error("Could not request tunnel forwarding.");
893: }
894: }
1.70 markus 895: }
896:
1.126 itojun 897: static void
1.70 markus 898: check_agent_present(void)
899: {
900: if (options.forward_agent) {
1.254 djm 901: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 902: if (!ssh_agent_present())
1.70 markus 903: options.forward_agent = 0;
904: }
905: }
906:
1.126 itojun 907: static int
1.45 markus 908: ssh_session(void)
909: {
910: int type;
911: int interactive = 0;
912: int have_tty = 0;
913: struct winsize ws;
914: char *cp;
1.243 djm 915: const char *display;
1.45 markus 916:
1.31 markus 917: /* Enable compression if requested. */
918: if (options.compression) {
1.316 ! djm 919: debug("Requesting compression at level %d.",
! 920: options.compression_level);
1.31 markus 921:
1.316 ! djm 922: if (options.compression_level < 1 ||
! 923: options.compression_level > 9)
! 924: fatal("Compression level must be from 1 (fast) to "
! 925: "9 (slow, best).");
1.31 markus 926:
927: /* Send the request. */
928: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
929: packet_put_int(options.compression_level);
930: packet_send();
931: packet_write_wait();
1.156 markus 932: type = packet_read();
1.31 markus 933: if (type == SSH_SMSG_SUCCESS)
934: packet_start_compression(options.compression_level);
935: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 936: logit("Warning: Remote host refused compression.");
1.31 markus 937: else
1.316 ! djm 938: packet_disconnect("Protocol error waiting for "
! 939: "compression response.");
1.31 markus 940: }
941: /* Allocate a pseudo tty if appropriate. */
942: if (tty_flag) {
943: debug("Requesting pty.");
944:
945: /* Start the packet. */
946: packet_start(SSH_CMSG_REQUEST_PTY);
947:
948: /* Store TERM in the packet. There is no limit on the
949: length of the string. */
950: cp = getenv("TERM");
951: if (!cp)
952: cp = "";
1.124 markus 953: packet_put_cstring(cp);
1.31 markus 954:
955: /* Store window size in the packet. */
956: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
957: memset(&ws, 0, sizeof(ws));
1.269 deraadt 958: packet_put_int((u_int)ws.ws_row);
959: packet_put_int((u_int)ws.ws_col);
960: packet_put_int((u_int)ws.ws_xpixel);
961: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 962:
963: /* Store tty modes in the packet. */
1.115 stevesk 964: tty_make_modes(fileno(stdin), NULL);
1.31 markus 965:
966: /* Send the packet, and wait for it to leave. */
967: packet_send();
968: packet_write_wait();
969:
970: /* Read response from the server. */
1.156 markus 971: type = packet_read();
1.43 markus 972: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 973: interactive = 1;
1.45 markus 974: have_tty = 1;
1.43 markus 975: } else if (type == SSH_SMSG_FAILURE)
1.316 ! djm 976: logit("Warning: Remote host failed or refused to "
! 977: "allocate a pseudo tty.");
1.31 markus 978: else
1.316 ! djm 979: packet_disconnect("Protocol error waiting for pty "
! 980: "request response.");
1.31 markus 981: }
982: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 983: display = getenv("DISPLAY");
984: if (options.forward_x11 && display != NULL) {
1.150 stevesk 985: char *proto, *data;
1.50 markus 986: /* Get reasonable local authentication information. */
1.243 djm 987: client_x11_get_proto(display, options.xauth_location,
988: options.forward_x11_trusted, &proto, &data);
1.50 markus 989: /* Request forwarding with authentication spoofing. */
1.316 ! djm 990: debug("Requesting X11 forwarding with authentication "
! 991: "spoofing.");
1.243 djm 992: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 993:
994: /* Read response from the server. */
1.156 markus 995: type = packet_read();
1.31 markus 996: if (type == SSH_SMSG_SUCCESS) {
997: interactive = 1;
1.50 markus 998: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 999: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 1000: } else {
1.316 ! djm 1001: packet_disconnect("Protocol error waiting for X11 "
! 1002: "forwarding");
1.50 markus 1003: }
1.31 markus 1004: }
1005: /* Tell the packet module whether this is an interactive session. */
1.80 markus 1006: packet_set_interactive(interactive);
1.31 markus 1007:
1008: /* Request authentication agent forwarding if appropriate. */
1.70 markus 1009: check_agent_present();
1010:
1.31 markus 1011: if (options.forward_agent) {
1012: debug("Requesting authentication agent forwarding.");
1013: auth_request_forwarding();
1014:
1015: /* Read response from the server. */
1.156 markus 1016: type = packet_read();
1.155 markus 1017: packet_check_eom();
1.31 markus 1018: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 1019: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 1020: }
1021:
1.70 markus 1022: /* Initiate port forwardings. */
1023: ssh_init_forwarding();
1.305 dtucker 1024:
1025: /* Execute a local command */
1026: if (options.local_command != NULL &&
1027: options.permit_local_command)
1028: ssh_local_cmd(options.local_command);
1.34 markus 1029:
1030: /* If requested, let ssh continue in the background. */
1.48 markus 1031: if (fork_after_authentication_flag)
1.34 markus 1032: if (daemon(1, 1) < 0)
1033: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1034:
1.33 markus 1035: /*
1036: * If a command was specified on the command line, execute the
1037: * command now. Otherwise request the server to start a shell.
1038: */
1.31 markus 1039: if (buffer_len(&command) > 0) {
1040: int len = buffer_len(&command);
1041: if (len > 900)
1042: len = 900;
1.316 ! djm 1043: debug("Sending command: %.*s", len,
! 1044: (u_char *)buffer_ptr(&command));
1.31 markus 1045: packet_start(SSH_CMSG_EXEC_CMD);
1046: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1047: packet_send();
1048: packet_write_wait();
1049: } else {
1050: debug("Requesting shell.");
1051: packet_start(SSH_CMSG_EXEC_SHELL);
1052: packet_send();
1053: packet_write_wait();
1054: }
1055:
1056: /* Enter the interactive session. */
1.119 stevesk 1057: return client_loop(have_tty, tty_flag ?
1058: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.89 markus 1059: }
1060:
1.214 djm 1061: /* request pty/x11/agent/tcpfwd/shell for channel */
1062: static void
1063: ssh_session2_setup(int id, void *arg)
1064: {
1.215 djm 1065: extern char **environ;
1.243 djm 1066: const char *display;
1067: int interactive = tty_flag;
1.215 djm 1068:
1.248 djm 1069: display = getenv("DISPLAY");
1.243 djm 1070: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1071: char *proto, *data;
1.50 markus 1072: /* Get reasonable local authentication information. */
1.243 djm 1073: client_x11_get_proto(display, options.xauth_location,
1074: options.forward_x11_trusted, &proto, &data);
1.50 markus 1075: /* Request forwarding with authentication spoofing. */
1.316 ! djm 1076: debug("Requesting X11 forwarding with authentication "
! 1077: "spoofing.");
1.243 djm 1078: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1079: interactive = 1;
1.50 markus 1080: /* XXX wait for reply */
1081: }
1082:
1.70 markus 1083: check_agent_present();
1084: if (options.forward_agent) {
1085: debug("Requesting authentication agent forwarding.");
1086: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1087: packet_send();
1.212 djm 1088: }
1089:
1.214 djm 1090: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.311 djm 1091: NULL, fileno(stdin), &command, environ);
1.90 markus 1092:
1.80 markus 1093: packet_set_interactive(interactive);
1.45 markus 1094: }
1095:
1.143 markus 1096: /* open new channel for a session */
1.126 itojun 1097: static int
1.143 markus 1098: ssh_session2_open(void)
1.45 markus 1099: {
1.118 markus 1100: Channel *c;
1101: int window, packetmax, in, out, err;
1.60 markus 1102:
1.62 markus 1103: if (stdin_null_flag) {
1.93 itojun 1104: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1105: } else {
1106: in = dup(STDIN_FILENO);
1107: }
1.60 markus 1108: out = dup(STDOUT_FILENO);
1109: err = dup(STDERR_FILENO);
1.45 markus 1110:
1111: if (in < 0 || out < 0 || err < 0)
1.62 markus 1112: fatal("dup() in/out/err failed");
1.45 markus 1113:
1.69 markus 1114: /* enable nonblocking unless tty */
1115: if (!isatty(in))
1116: set_nonblock(in);
1117: if (!isatty(out))
1118: set_nonblock(out);
1119: if (!isatty(err))
1120: set_nonblock(err);
1121:
1.65 markus 1122: window = CHAN_SES_WINDOW_DEFAULT;
1123: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1124: if (tty_flag) {
1125: window >>= 1;
1126: packetmax >>= 1;
1.45 markus 1127: }
1.118 markus 1128: c = channel_new(
1.45 markus 1129: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1130: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1131: "client-session", /*nonblock*/0);
1.45 markus 1132:
1.143 markus 1133: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1134:
1.122 markus 1135: channel_send_open(c->self);
1.143 markus 1136: if (!no_shell_flag)
1.310 djm 1137: channel_register_open_confirm(c->self,
1138: ssh_session2_setup, NULL);
1.106 markus 1139:
1.118 markus 1140: return c->self;
1.106 markus 1141: }
1142:
1.126 itojun 1143: static int
1.106 markus 1144: ssh_session2(void)
1145: {
1.143 markus 1146: int id = -1;
1.106 markus 1147:
1148: /* XXX should be pre-session */
1149: ssh_init_forwarding();
1150:
1.143 markus 1151: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1152: id = ssh_session2_open();
1.314 djm 1153:
1154: /* If we don't expect to open a new session, then disallow it */
1155: if (options.control_master == SSHCTL_MASTER_NO) {
1156: debug("Requesting no-more-sessions@openssh.com");
1157: packet_start(SSH2_MSG_GLOBAL_REQUEST);
1158: packet_put_cstring("no-more-sessions@openssh.com");
1159: packet_put_char(0);
1160: packet_send();
1161: }
1.255 reyk 1162:
1163: /* Execute a local command */
1164: if (options.local_command != NULL &&
1165: options.permit_local_command)
1166: ssh_local_cmd(options.local_command);
1.301 djm 1167:
1168: /* Start listening for multiplex clients */
1.312 djm 1169: muxserver_listen();
1.106 markus 1170:
1171: /* If requested, let ssh continue in the background. */
1172: if (fork_after_authentication_flag)
1173: if (daemon(1, 1) < 0)
1174: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1175:
1.119 stevesk 1176: return client_loop(tty_flag, tty_flag ?
1177: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1178: }
1179:
1.126 itojun 1180: static void
1.104 markus 1181: load_public_identity_files(void)
1182: {
1.275 djm 1183: char *filename, *cp, thishost[NI_MAXHOST];
1.306 deraadt 1184: char *pwdir = NULL, *pwname = NULL;
1.167 markus 1185: int i = 0;
1.104 markus 1186: Key *public;
1.275 djm 1187: struct passwd *pw;
1.167 markus 1188: #ifdef SMARTCARD
1189: Key **keys;
1.104 markus 1190:
1.133 markus 1191: if (options.smartcard_device != NULL &&
1.167 markus 1192: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.292 stevesk 1193: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
1.167 markus 1194: int count = 0;
1195: for (i = 0; keys[i] != NULL; i++) {
1196: count++;
1.316 ! djm 1197: memmove(&options.identity_files[1],
! 1198: &options.identity_files[0],
1.167 markus 1199: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1.316 ! djm 1200: memmove(&options.identity_keys[1],
! 1201: &options.identity_keys[0],
1.167 markus 1202: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1203: options.num_identity_files++;
1204: options.identity_keys[0] = keys[i];
1.194 markus 1205: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1206: }
1.168 markus 1207: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1208: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1209: i = count;
1210: xfree(keys);
1.127 markus 1211: }
1.136 jakob 1212: #endif /* SMARTCARD */
1.275 djm 1213: if ((pw = getpwuid(original_real_uid)) == NULL)
1214: fatal("load_public_identity_files: getpwuid failed");
1.307 dtucker 1215: pwname = xstrdup(pw->pw_name);
1216: pwdir = xstrdup(pw->pw_dir);
1.275 djm 1217: if (gethostname(thishost, sizeof(thishost)) == -1)
1218: fatal("load_public_identity_files: gethostname: %s",
1219: strerror(errno));
1.131 millert 1220: for (; i < options.num_identity_files; i++) {
1.275 djm 1221: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1222: original_real_uid);
1.306 deraadt 1223: filename = percent_expand(cp, "d", pwdir,
1224: "u", pwname, "l", thishost, "h", host,
1.275 djm 1225: "r", options.user, (char *)NULL);
1226: xfree(cp);
1.131 millert 1227: public = key_load_public(filename, NULL);
1228: debug("identity file %s type %d", filename,
1229: public ? public->type : -1);
1230: xfree(options.identity_files[i]);
1231: options.identity_files[i] = filename;
1232: options.identity_keys[i] = public;
1233: }
1.306 deraadt 1234: bzero(pwname, strlen(pwname));
1.307 dtucker 1235: xfree(pwname);
1.306 deraadt 1236: bzero(pwdir, strlen(pwdir));
1.307 dtucker 1237: xfree(pwdir);
1.214 djm 1238: }