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