[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem about the function ssh_handle_key_exchange()


Hi dear,

I just started to learn this tool by trying the examples provided in the
package. However, the function ssh_handle_key_exchange() on the server side
always gives the same error "Could not sign the session id" when I try to
connect the server by the ssh command or a compiled program utilizing
libssh as well.

I have searched a lot and gone through the archived mail, but still no
clue. Could you help me solve this problem? Have I missed any
"straightforward" setting?

Thanks a lot in advance!

/*******Appendix********/
1. The server-side source and the server output on the terminal are
attached.
2. The source file can be compiled and linked successfully.
3. I use Eclipse on Ubuntu 12.04.4

Regards,
Jeffrey Qingsen WANG

Department of Information Engineering
The Chinese University of Hong Kong
You can login as the user libssh with the password libssh
session cookie: 71:9f:19:1b:b4:9c:43:a3:2d:3c:97:20:de:e8:f3:b5
session cookie: 97:48:3d:ca:18:92:1f:b0:87:54:0c:f1:8e:19:b5:ee
Importing a 2056 bits, 257 bytes object ...
e value: 00F8EEB181EED4B8FC6D86CA484CFC8DE843194364F7009686113DE7B6DA1A96DBE610D484A487BD6C584B54758749C9CD3D7D9764D3707D4BBE5BCA3D3499D2800DEB19CD5C446134037542C65D4935A25C29CDD25CEF043138760C2030BFE72CF2B7B1C7C1766D7CB4E94412B7D454236D6BDCAA9665EC34E5E9EB33AB0F2C0DF6B91D4D8981B078B46C5FECDE5AD3718C9E7C35B4B57A91A8B352BFC05F021E695D23A574067E1602543BFE43A62C319B419BEF39570D4C95589960E85ED71C33F14A7881A08AEEE7151F50164364A5D09AB8690C4FF67D2942A655660D57F0DEE9B17FF4152A2DB92BD3C203C358DB9B8941FEBB7A043EF1573816F5240B91
y value: 00D1F2F77329B4892BE0465EDC842019A7
f value: 00B86006E3085496CBDDB66B3C9614D4AD1BF19295AD27F510FCC9146FDD82331A6A03BDC3C78796C558D5389FD269905D78BB675DEFA921C617908E31756C25C6C65CAE9E243F03FA1450DFB8C82EF8E32B4D8CB9D020841D510F77BBDAC9E10784D4FC828C77949DD73E9D2D77F81B002BD1C1E2251D0072562D83CCD821CFC4605818B2C273324541E0D5D57844E1010B03DDEFB1363BA1AA2DF8C22B8EAE3B4623DF23D87CE3798177A16C10121D3D1B22861D509E674611CF1FDA05D29C4F841DF64E47E9E8A838B89C70529EB0F9D9302D22B9B0277E3E9B8D127244BF8BE4E3A70D3E4D619D1FBF4B6FAA0DFA394ACDB4EB426E0331C8F52958AE98516E
2048 bits, 256 bytes, 1 padding
Session server cookie: 71:9f:19:1b:b4:9c:43:a3:2d:3c:97:20:de:e8:f3:b5
Session client cookie: 97:48:3d:ca:18:92:1f:b0:87:54:0c:f1:8e:19:b5:ee
Shared secret key value: 00A052715A22AAE8600934BF2C57941596E9CA4149F9094E52A9D9C0FC9A58E2F1CC0342E30086ADCE741A1773742EB85D583A278BBDB0EF37E84E55E8BC282EECA6AADA7FD0D5CA966ED0A1A8D04FC56FF35A88411FC3BBFEF894D18A14DB9F7356EF5F134FA6882306197999301D1DA83C1941584E4E36B92044A0F6D96EF7F3E4EF524D913AEB8EC91D60475C37185CE3F03394070CD78E819C6ADFCF5CF73D0CB1C9966F6B44DFE0C9AD7B05F871A36C98F7DC174FDC5153DBE394649E3B397F69A3EEB146B0F86C6522CAE66503DB2C08599D0D9E03C3578EFC245DB04393F4DB50F27C96469FDF2A50F8F4D26CB9E701023E87A4C919EDEF4D8337EF273E
2048 bits, 256 bytes, 1 padding
2048 bits, 256 bytes, 1 padding
2048 bits, 256 bytes, 1 padding
hash buffer: 00:00:00:14:53:53:48:2d:32:2e:30:2d:6c:69:62:73:73:68:2d:30:2e:36:2e:31:00:00:00:14:53:53:48:2d:32:2e:30:2d:6c:69:62:73:73:68:2d:30:2e:36:2e:31:00:00:01:65:14:97:48:3d:ca:18:92:1f:b0:87:54:0c:f1:8e:19:b5:ee:00:00:00:36:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:31:34:2d:73:68:61:31:2c:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:31:2d:73:68:61:31:00:00:00:0f:73:73:68:2d:72:73:61:2c:73:73:68:2d:64:73:73:00:00:00:64:61:65:73:32:35:36:2d:63:74:72:2c:61:65:73:31:39:32:2d:63:74:72:2c:61:65:73:31:32:38:2d:63:74:72:2c:61:65:73:32:35:36:2d:63:62:63:2c:61:65:73:31:39:32:2d:63:62:63:2c:61:65:73:31:32:38:2d:63:62:63:2c:62:6c:6f:77:66:69:73:68:2d:63:62:63:2c:33:64:65:73:2d:63:62:63:2c:64:65:73:2d:63:62:63:2d:73:73:68:31:00:00:00:64:61:65:73:32:35:36:2d:63:74:72:2c:61:65:73:31:39:32:2d:63:74:72:2c:61:65:73:31:32:38:2d:63:74:72:2c:61:65:73:32:35:36:2d:63:62:63:2c:61:65:73:31:39:32:2d:63:62:63:2c:61:65:73:31:32:38:2d:63:62:63:2c:62:6c:6f:77:66:69:73:68:2d:63:62:63:2c:33:64:65:73:2d:63:62:63:2c:64:65:73:2d:63:62:63:2d:73:73:68:31:00:00:00:09:68:6d:61:63:2d:73:68:61:31:00:00:00:09:68:6d:61:63:2d:73:68:61:31:00:00:00:04:6e:6f:6e:65:00:00:00:04:6e:6f:6e:65:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:91:14:71:9f:19:1b:b4:9c:43:a3:2d:3c:97:20:de:e8:f3:b5:00:00:00:36:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:31:34:2d:73:68:61:31:2c:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:31:2d:73:68:61:31:00:00:00:0f:73:73:68:2d:64:73:73:2c:73:73:68:2d:72:73:61:00:00:00:64:61:65:73:32:35:36:2d:63:74:72:2c:61:65:73:31:39:32:2d:63:74:72:2c:61:65:73:31:32:38:2d:63:74:72:2c:61:65:73:32:35:36:2d:63:62:63:2c:61:65:73:31:39:32:2d:63:62:63:2c:61:65:73:31:32:38:2d:63:62:63:2c:62:6c:6f:77:66:69:73:68:2d:63:62:63:2c:33:64:65:73:2d:63:62:63:2c:64:65:73:2d:63:62:63:2d:73:73:68:31:00:00:00:64:61:65:73:32:35:36:2d:63:74:72:2c:61:65:73:31:39:32:2d:63:74:72:2c:61:65:73:31:32:38:2d:63:74:72:2c:61:65:73:32:35:36:2d:63:62:63:2c:61:65:73:31:39:32:2d:63:62:63:2c:61:65:73:31:32:38:2d:63:62:63:2c:62:6c:6f:77:66:69:73:68:2d:63:62:63:2c:33:64:65:73:2d:63:62:63:2c:64:65:73:2d:63:62:63:2d:73:73:68:31:00:00:00:09:68:6d:61:63:2d:73:68:61:31:00:00:00:09:68:6d:61:63:2d:73:68:61:31:00:00:00:1a:6e:6f:6e:65:2c:7a:6c:69:62:2c:7a:6c:69:62:40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:1a:6e:6f:6e:65:2c:7a:6c:69:62:2c:7a:6c:69:62:40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:17:00:00:00:07:73:73:68:2d:72:73:61:00:00:00:03:01:00:01:00:00:01:01:00:c1:f4:50:26:7c:02:22:f1:9a:27:cd:cf:52:5d:86:d2:57:6c:2b:60:53:48:23:63:de:86:2d:ac:ab:5a:18:6a:27:b6:83:f1:a9:cd:0a:f1:a7:df:64:f2:46:18:85:82:c9:34:41:51:33:04:0c:73:52:8f:c5:e3:0a:9a:bd:9a:7c:a9:15:5c:ee:6e:50:dd:56:e2:22:0b:d4:41:f1:d6:3c:75:63:25:7b:78:f0:09:96:25:27:78:c4:ee:b6:ab:4a:1b:bf:a5:d2:78:1d:09:94:6a:04:84:94:48:74:6a:57:b5:84:87:56:38:a3:aa:79:7b:1b:38:71:c3:bc:cf:9b:fc:03:6c:f3:e5:2a:d4:3d:65:ed:88:bc:f4:05:90:c2:d0:4c:26:1b:8c:f4:7d:09:7f:07:a5:f1:a0:a5:00:17:15:e3:23:4e:55:46:40:af:b0:0e:89:cb:2d:b4:9e:15:71:36:0e:62:e7:2f:58:1e:0e:ad:a8:9d:bf:7c:84:60:d6:4f:3d:ac:ea:fa:68:a1:cb:56:cc:a4:1d:97:bb:63:7d:73:cf:09:22:ad:46:0f:80:c0:0b:af:03:2a:c1:a0:fa:b0:57:94:2e:cb:f1:12:c5:22:d0:5e:7f:7a:51:8c:be:64:50:55:a3:6d:48:a8:af:ba:94:cd:df:21:4f:00:00:01:01:00:f8:ee:b1:81:ee:d4:b8:fc:6d:86:ca:48:4c:fc:8d:e8:43:19:43:64:f7:00:96:86:11:3d:e7:b6:da:1a:96:db:e6:10:d4:84:a4:87:bd:6c:58:4b:54:75:87:49:c9:cd:3d:7d:97:64:d3:70:7d:4b:be:5b:ca:3d:34:99:d2:80:0d:eb:19:cd:5c:44:61:34:03:75:42:c6:5d:49:35:a2:5c:29:cd:d2:5c:ef:04:31:38:76:0c:20:30:bf:e7:2c:f2:b7:b1:c7:c1:76:6d:7c:b4:e9:44:12:b7:d4:54:23:6d:6b:dc:aa:96:65:ec:34:e5:e9:eb:33:ab:0f:2c:0d:f6:b9:1d:4d:89:81:b0:78:b4:6c:5f:ec:de:5a:d3:71:8c:9e:7c:35:b4:b5:7a:91:a8:b3:52:bf:c0:5f:02:1e:69:5d:23:a5:74:06:7e:16:02:54:3b:fe:43:a6:2c:31:9b:41:9b:ef:39:57:0d:4c:95:58:99:60:e8:5e:d7:1c:33:f1:4a:78:81:a0:8a:ee:e7:15:1f:50:16:43:64:a5:d0:9a:b8:69:0c:4f:f6:7d:29:42:a6:55:66:0d:57:f0:de:e9:b1:7f:f4:15:2a:2d:b9:2b:d3:c2:03:c3:58:db:9b:89:41:fe:bb:7a:04:3e:f1:57:38:16:f5:24:0b:91:00:00:01:01:00:b8:60:06:e3:08:54:96:cb:dd:b6:6b:3c:96:14:d4:ad:1b:f1:92:95:ad:27:f5:10:fc:c9:14:6f:dd:82:33:1a:6a:03:bd:c3:c7:87:96:c5:58:d5:38:9f:d2:69:90:5d:78:bb:67:5d:ef:a9:21:c6:17:90:8e:31:75:6c:25:c6:c6:5c:ae:9e:24:3f:03:fa:14:50:df:b8:c8:2e:f8:e3:2b:4d:8c:b9:d0:20:84:1d:51:0f:77:bb:da:c9:e1:07:84:d4:fc:82:8c:77:94:9d:d7:3e:9d:2d:77:f8:1b:00:2b:d1:c1:e2:25:1d:00:72:56:2d:83:cc:d8:21:cf:c4:60:58:18:b2:c2:73:32:45:41:e0:d5:d5:78:44:e1:01:0b:03:dd:ef:b1:36:3b:a1:aa:2d:f8:c2:2b:8e:ae:3b:46:23:df:23:d8:7c:e3:79:81:77:a1:6c:10:12:1d:3d:1b:22:86:1d:50:9e:67:46:11:cf:1f:da:05:d2:9c:4f:84:1d:f6:4e:47:e9:e8:a8:38:b8:9c:70:52:9e:b0:f9:d9:30:2d:22:b9:b0:27:7e:3e:9b:8d:12:72:44:bf:8b:e4:e3:a7:0d:3e:4d:61:9d:1f:bf:4b:6f:aa:0d:fa:39:4a:cd:b4:eb:42:6e:03:31:c8:f5:29:58:ae:98:51:6e:00:00:01:01:00:a0:52:71:5a:22:aa:e8:60:09:34:bf:2c:57:94:15:96:e9:ca:41:49:f9:09:4e:52:a9:d9:c0:fc:9a:58:e2:f1:cc:03:42:e3:00:86:ad:ce:74:1a:17:73:74:2e:b8:5d:58:3a:27:8b:bd:b0:ef:37:e8:4e:55:e8:bc:28:2e:ec:a6:aa:da:7f:d0:d5:ca:96:6e:d0:a1:a8:d0:4f:c5:6f:f3:5a:88:41:1f:c3:bb:fe:f8:94:d1:8a:14:db:9f:73:56:ef:5f:13:4f:a6:88:23:06:19:79:99:30:1d:1d:a8:3c:19:41:58:4e:4e:36:b9:20:44:a0:f6:d9:6e:f7:f3:e4:ef:52:4d:91:3a:eb:8e:c9:1d:60:47:5c:37:18:5c:e3:f0:33:94:07:0c:d7:8e:81:9c:6a:df:cf:5c:f7:3d:0c:b1:c9:96:6f:6b:44:df:e0:c9:ad:7b:05:f8:71:a3:6c:98:f7:dc:17:4f:dc:51:53:db:e3:94:64:9e:3b:39:7f:69:a3:ee:b1:46:b0:f8:6c:65:22:ca:e6:65:03:db:2c:08:59:9d:0d:9e:03:c3:57:8e:fc:24:5d:b0:43:93:f4:db:50:f2:7c:96:46:9f:df:2a:50:f8:f4:d2:6c:b9:e7:01:02:3e:87:a4:c9:19:ed:ef:4d:83:37:ef:27:3e
Session hash: 
secret hash: 9f:77:82:e5:62:87:de:55:26:66:d1:b0:56:a4:25:a6:1f:ad:d7:00
session id: 9f:77:82:e5:62:87:de:55:26:66:d1:b0:56:a4:25:a6:1f:ad:d7:00
Hash being signed: 9b:e6:4f:56:cc:0e:c8:78:67:bd:8e:73:3d:5f:3e:5a:b4:76:d9:e2
ssh_handle_key_exchange: Could not sign the session id
/* This is a sample implementation of a libssh based SSH server */
/*
Copyright 2003-2011 Aris Adamantiadis

This file is part of the SSH Library

You are free to copy this file, modify it in any way, consider it being public
domain. This does not apply to the rest of the library though, but it is
allowed to cut-and-paste working code from this file to any license of
program.
The goal is to show the API in action. It's not a reference on how terminal
clients must be made or how a client should react.
*/


#include <libssh/libssh.h>
#include <libssh/server.h>
#include <libssh/callbacks.h>

#ifdef HAVE_ARGP_H
#include <argp.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <poll.h>
#include <pty.h>

#define SSHD_USER "libssh"
#define SSHD_PASSWORD "libssh"

#ifndef KEYS_FOLDER
#ifdef _WIN32
#define KEYS_FOLDER
#else
#define KEYS_FOLDER "/etc/ssh/"
#endif
#endif

#ifdef WITH_PCAP
const char *pcap_file="debug.server.pcap";
ssh_pcap_file pcap;

static void set_pcap(ssh_session session){
	if(!pcap_file)
		return;
	pcap=ssh_pcap_file_new();
	if(ssh_pcap_file_open(pcap,pcap_file) == SSH_ERROR){
		printf("Error opening pcap file\n");
		ssh_pcap_file_free(pcap);
		pcap=NULL;
		return;
	}
	ssh_set_pcap_file(session,pcap);
}

static void cleanup_pcap(){
	ssh_pcap_file_free(pcap);
	pcap=NULL;
}
#endif


static int auth_password(const char *user, const char *password){
    if(strcmp(user, SSHD_USER))
        return 0;
    if(strcmp(password, SSHD_PASSWORD))
        return 0;
    return 1; // authenticated
}
#ifdef HAVE_ARGP_H
const char *argp_program_version = "libssh server example "
  SSH_STRINGIFY(LIBSSH_VERSION);
const char *argp_program_bug_address = "<libssh@xxxxxxxxxx>";

/* Program documentation. */
static char doc[] = "libssh -- a Secure Shell protocol implementation";

/* A description of the arguments we accept. */
static char args_doc[] = "BINDADDR";

static int port = 22;

/* The options we understand. */
static struct argp_option options[] = {
  {
    .name  = "port",
    .key   = 'p',
    .arg   = "PORT",
    .flags = 0,
    .doc   = "Set the port to bind.",
    .group = 0
  },
  {
    .name  = "hostkey",
    .key   = 'k',
    .arg   = "FILE",
    .flags = 0,
    .doc   = "Set the host key.",
    .group = 0
  },
  {
    .name  = "dsakey",
    .key   = 'd',
    .arg   = "FILE",
    .flags = 0,
    .doc   = "Set the dsa key.",
    .group = 0
  },
  {
    .name  = "rsakey",
    .key   = 'r',
    .arg   = "FILE",
    .flags = 0,
    .doc   = "Set the rsa key.",
    .group = 0
  },
  {
    .name  = "verbose",
    .key   = 'v',
    .arg   = NULL,
    .flags = 0,
    .doc   = "Get verbose output.",
    .group = 0
  },
  {NULL, 0, 0, 0, NULL, 0}
};

/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) {
  /* Get the input argument from argp_parse, which we
   * know is a pointer to our arguments structure.
   */
  ssh_bind sshbind = state->input;

  switch (key) {
    case 'p':
      ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT_STR, arg);
      port = atoi(arg);
      break;
    case 'd':
      ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, arg);
      break;
    case 'k':
      ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, arg);
      break;
    case 'r':
      ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, arg);
      break;
    case 'v':
      ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR, "3");
      break;
    case ARGP_KEY_ARG:
      if (state->arg_num >= 1) {
        /* Too many arguments. */
        argp_usage (state);
      }
      ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, arg);
      break;
    case ARGP_KEY_END:
      if (state->arg_num < 1) {
        /* Not enough arguments. */
        argp_usage (state);
      }
      break;
    default:
      return ARGP_ERR_UNKNOWN;
  }

  return 0;
}

/* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
#endif /* HAVE_ARGP_H */

static int authenticate(ssh_session session) {
    ssh_message message;

    do {
        message=ssh_message_get(session);
        if(!message)
            break;
        switch(ssh_message_type(message)){
            case SSH_REQUEST_AUTH:
                switch(ssh_message_subtype(message)){
                    case SSH_AUTH_METHOD_PASSWORD:
                        printf("User %s wants to auth with pass %s\n",
                               ssh_message_auth_user(message),
                               ssh_message_auth_password(message));
                        if(auth_password(ssh_message_auth_user(message),
                           ssh_message_auth_password(message))){
                               ssh_message_auth_reply_success(message,0);
                               ssh_message_free(message);
                               return 1;
                           }
                        ssh_message_auth_set_methods(message,
                                                SSH_AUTH_METHOD_PASSWORD |
                                                SSH_AUTH_METHOD_INTERACTIVE);
                        // not authenticated, send default message
                        ssh_message_reply_default(message);
                        break;

                    case SSH_AUTH_METHOD_NONE:
                    default:
                        printf("User %s wants to auth with unknown auth %d\n",
                               ssh_message_auth_user(message),
                               ssh_message_subtype(message));
                        ssh_message_auth_set_methods(message,
                                                SSH_AUTH_METHOD_PASSWORD |
                                                SSH_AUTH_METHOD_INTERACTIVE);
                        ssh_message_reply_default(message);
                        break;
                }
                break;
            default:
                ssh_message_auth_set_methods(message,
                                                SSH_AUTH_METHOD_PASSWORD |
                                                SSH_AUTH_METHOD_INTERACTIVE);
                ssh_message_reply_default(message);
        }
        ssh_message_free(message);
    } while (1);
    return 0;
}

static int copy_fd_to_chan(socket_t fd, int revents, void *userdata) {
    ssh_channel chan = (ssh_channel)userdata;
    char buf[2048];
    int sz = 0;

    if(!chan) {
        close(fd);
        return -1;
    }
    if(revents & POLLIN) {
        sz = read(fd, buf, 2048);
        if(sz > 0) {
            ssh_channel_write(chan, buf, sz);
        }
    }
    if(revents & POLLHUP) {
        ssh_channel_close(chan);
        sz = -1;
    }
    return sz;
}

static int copy_chan_to_fd(ssh_session session,
                                           ssh_channel channel,
                                           void *data,
                                           uint32_t len,
                                           int is_stderr,
                                           void *userdata) {
    int fd = *(int*)userdata;
    int sz;
    (void)session;
    (void)channel;
    (void)is_stderr;

    sz = write(fd, data, len);
    return sz;
}

static void chan_close(ssh_session session, ssh_channel channel, void *userdata) {
    int fd = *(int*)userdata;
    (void)session;
    (void)channel;

    close(fd);
}

struct ssh_channel_callbacks_struct cb;



static int main_loop(ssh_channel chan) {
    ssh_session session = ssh_channel_get_session(chan);
    socket_t fd;
    struct termios *term = NULL;
    struct winsize *win = NULL;
    pid_t childpid;
    ssh_event event;
    short events;


    childpid = forkpty(&fd, NULL, term, win);
    if(childpid == 0) {
        execl("/bin/bash", "/bin/bash", (char *)NULL);
        abort();
    }

    cb.userdata = &fd;
    ssh_callbacks_init(&cb);
    ssh_set_channel_callbacks(chan, &cb);

    events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;

    event = ssh_event_new();
    if(event == NULL) {
        printf("Couldn't get a event\n");
        return -1;
    }
    if(ssh_event_add_fd(event, fd, events, copy_fd_to_chan, chan) != SSH_OK) {
        printf("Couldn't add an fd to the event\n");
        return -1;
    }
    if(ssh_event_add_session(event, session) != SSH_OK) {
        printf("Couldn't add the session to the event\n");
        return -1;
    }

    do {
        ssh_event_dopoll(event, 1000);
    } while(!ssh_channel_is_closed(chan));

    ssh_event_remove_fd(event, fd);

    ssh_event_remove_session(event, session);

    ssh_event_free(event);
    return 0;
}


int main(int argc, char **argv){
    ssh_session session;
    ssh_bind sshbind;
    ssh_message message;
    ssh_channel chan=0;
    int auth=0;
    int shell=0;
    int r;

	cb.channel_data_function = copy_chan_to_fd;
    cb.channel_eof_function = chan_close;
    cb.channel_close_function = chan_close;
	cb.userdata = NULL;

    sshbind=ssh_bind_new();
    session=ssh_new();
    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT_STR, "2048");

    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY,
                                            KEYS_FOLDER "ssh_host_dsa_key");

    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY,
                                            KEYS_FOLDER "ssh_host_rsa_key");

#ifdef HAVE_ARGP_H
    /*
     * Parse our arguments; every option seen by parse_opt will
     * be reflected in arguments.
     */
    argp_parse (&argp, argc, argv, 0, 0, sshbind);
#else
    (void) argc;
    (void) argv;
#endif
#ifdef WITH_PCAP
    set_pcap(session);
#endif

    if(ssh_bind_listen(sshbind)<0){
        printf("Error listening to socket: %s\n", ssh_get_error(sshbind));
        return 1;
    }

    printf("You can login as the user %s with the password %s\n", SSHD_USER,
                                                            SSHD_PASSWORD);
    r = ssh_bind_accept(sshbind, session);
    if(r==SSH_ERROR){
      printf("Error accepting a connection: %s\n", ssh_get_error(sshbind));
      return 1;
    }
    if (ssh_handle_key_exchange(session)) {
        printf("ssh_handle_key_exchange: %s\n", ssh_get_error(session));
        return 1;
    }

    /* proceed to authentication */
    auth = authenticate(session);
    if(!auth){
        printf("Authentication error: %s\n", ssh_get_error(session));
        ssh_disconnect(session);
        return 1;
    }


    /* wait for a channel session */
    do {
        message = ssh_message_get(session);
        if(message){
            if(ssh_message_type(message) == SSH_REQUEST_CHANNEL_OPEN &&
                    ssh_message_subtype(message) == SSH_CHANNEL_SESSION) {
                chan = ssh_message_channel_request_open_reply_accept(message);
                ssh_message_free(message);
                break;
            } else {
                ssh_message_reply_default(message);
                ssh_message_free(message);
            }
        } else {
            break;
        }
    } while(!chan);

    if(!chan) {
        printf("Error: cleint did not ask for a channel session (%s)\n",
                                                    ssh_get_error(session));
        ssh_finalize();
        return 1;
    }


    /* wait for a shell */
    do {
        message = ssh_message_get(session);
        if(message != NULL) {
            if(ssh_message_type(message) == SSH_REQUEST_CHANNEL) {
                if(ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_SHELL) {
                    shell = 1;
                    ssh_message_channel_request_reply_success(message);
                    ssh_message_free(message);
                    break;
                } else if(ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_PTY) {
                    ssh_message_channel_request_reply_success(message);
                    ssh_message_free(message);
                    continue;
                }
            }
            ssh_message_reply_default(message);
            ssh_message_free(message);
        } else {
            break;
        }
    } while(!shell);

    if(!shell) {
        printf("Error: No shell requested (%s)\n", ssh_get_error(session));
        return 1;
    }

    printf("it works !\n");

    main_loop(chan);

    ssh_disconnect(session);
    ssh_bind_free(sshbind);
#ifdef WITH_PCAP
    cleanup_pcap();
#endif
    ssh_finalize();
    return 0;
}

Archive administrator: postmaster@lists.cynapses.org