summaryrefslogtreecommitdiff
path: root/lib/libssh.c
blob: 9449ab305f55d2e359f9e6f19afb93327c77d470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 *	BIRD -- Mockup of SSH Library for loading LibSSH using dlopen
 *
 *	(c) 2015 CZ.NIC
 *
 *	This file was part of SSH Library: http://www.libssh.org/
 *	(c) 2003-2009 by Aris Adamantiadis (SSH Library)
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <dlfcn.h>
#include "nest/bird.h"
#include "lib/libssh.h"

#define FILENAME_OF_SHARED_OBJECT_LIBSSH "libssh.so"

struct ssh_function {
  void **fn;
  const char *name;
};

ssh_session (*ssh_new)(void);
void (*ssh_set_blocking)(ssh_session session, int blocking);
int (*ssh_options_set)(ssh_session session, enum ssh_options_e type, const void *value);
int (*ssh_connect)(ssh_session session);
socket_t (*ssh_get_fd)(ssh_session session);
int (*ssh_is_server_known)(ssh_session session);
int (*ssh_userauth_publickey_auto)(ssh_session session, const char *username, const char *passphrase);
const char * (*ssh_get_error)(void *error);
int (*ssh_get_error_code)(void *error);
void (*ssh_disconnect)(ssh_session session);
void (*ssh_free)(ssh_session session);

ssh_channel (*ssh_channel_new)(ssh_session session);
int (*ssh_channel_is_open)(ssh_channel channel);
int (*ssh_channel_close)(ssh_channel channel);
void (*ssh_channel_free)(ssh_channel channel);
int (*ssh_channel_open_session)(ssh_channel channel);
int (*ssh_channel_request_subsystem)(ssh_channel channel, const char *subsystem);
int (*ssh_channel_read_nonblocking)(ssh_channel channel, void *dest, uint32_t count, int is_stderr);
int (*ssh_channel_is_eof)(ssh_channel channel);
int (*ssh_channel_select)(ssh_channel *readchans, ssh_channel *writechans, ssh_channel *exceptchans, struct timeval * timeout);
int (*ssh_channel_write)(ssh_channel channel, const void *data, uint32_t len);

#define SSH_FN(x) { .fn = (void **) &x, .name = #x }
static struct ssh_function all_ssh_fn[] = {
    SSH_FN(ssh_new),
    SSH_FN(ssh_set_blocking),
    SSH_FN(ssh_options_set),
    SSH_FN(ssh_connect),
    SSH_FN(ssh_get_fd),
    SSH_FN(ssh_is_server_known),
    SSH_FN(ssh_userauth_publickey_auto),
    SSH_FN(ssh_get_error),
    SSH_FN(ssh_get_error_code),
    SSH_FN(ssh_disconnect),
    SSH_FN(ssh_free),
    SSH_FN(ssh_channel_new),
    SSH_FN(ssh_channel_is_open),
    SSH_FN(ssh_channel_close),
    SSH_FN(ssh_channel_free),
    SSH_FN(ssh_channel_open_session),
    SSH_FN(ssh_channel_request_subsystem),
    SSH_FN(ssh_channel_read_nonblocking),
    SSH_FN(ssh_channel_is_eof),
    SSH_FN(ssh_channel_select),
    SSH_FN(ssh_channel_write),
};
#undef SSH_FN

static void *libssh;

/**
 * load_libssh - Prepare all ssh_* functions
 *
 * Initialize for use all ssh_* functions. Returns normally NULL.
 * If an error occurs then returns static string with the error description.
 */
const char *
load_libssh(void)
{
  char *err_buf;

  libssh = dlopen(FILENAME_OF_SHARED_OBJECT_LIBSSH, RTLD_LAZY);
  if (!libssh)
  {
    /* This would be probably often repeated problem */
    char *help_msg = "You have to install libssh library.";
    err_buf = mb_alloc(&root_pool, 512); /* FIXME: free memory */
    bsnprintf(err_buf, 512, "%s. %s", dlerror(), help_msg);
    return err_buf;
  }

  dlerror(); /* Clear any existing error */

  for (int i = 0; i < sizeof(all_ssh_fn)/sizeof(all_ssh_fn[0]); i++)
  {
    *all_ssh_fn[i].fn = (void *) dlsym(libssh, all_ssh_fn[i].name);
    err_buf = dlerror();
    if (err_buf)
      return err_buf;
  }

  return NULL;
}