00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _LIBSSH_H
00022 #define _LIBSSH_H
00023 #ifndef _MSC_VER
00024 #include <unistd.h>
00025 #include <inttypes.h>
00026 #else
00027
00028 typedef unsigned int uint32_t;
00029 typedef unsigned short uint16_t;
00030 typedef unsigned char uint8_t;
00031 typedef unsigned long long uint64_t;
00032
00033 #endif
00034 #ifndef _WIN32
00035 #include <sys/select.h>
00036 #include <netdb.h>
00037 #endif
00038 #ifdef _WIN32
00039 #include <winsock2.h>
00040 #endif
00041
00042
00043 #define LIBSSH_VERSION "libssh-0.2.1-svn"
00044
00045 #ifdef __cplusplus
00046 extern "C" {
00047 #endif
00048
00049 typedef struct string_struct STRING;
00050 typedef struct buffer_struct BUFFER;
00051 typedef struct public_key_struct PUBLIC_KEY;
00052 typedef struct private_key_struct PRIVATE_KEY;
00053 typedef struct ssh_options_struct SSH_OPTIONS;
00054 typedef struct channel_struct CHANNEL;
00055 typedef struct ssh_session SSH_SESSION;
00056 typedef struct ssh_kbdint SSH_KBDINT;
00057
00058
00059 typedef uint32_t u32;
00060 typedef uint16_t u16;
00061 typedef uint64_t u64;
00062 typedef uint8_t u8;
00063
00064
00065 #ifdef _WIN32
00066 #define socket_t SOCKET
00067 #else
00068 typedef int socket_t;
00069 #endif
00070
00071
00072 #define SSH_KEX 0
00073 #define SSH_HOSTKEYS 1
00074 #define SSH_CRYPT_C_S 2
00075 #define SSH_CRYPT_S_C 3
00076 #define SSH_MAC_C_S 4
00077 #define SSH_MAC_S_C 5
00078 #define SSH_COMP_C_S 6
00079 #define SSH_COMP_S_C 7
00080 #define SSH_LANG_C_S 8
00081 #define SSH_LANG_S_C 9
00082
00083 #define SSH_CRYPT 2
00084 #define SSH_MAC 3
00085 #define SSH_COMP 4
00086 #define SSH_LANG 5
00087
00088 #define SSH_AUTH_SUCCESS 0
00089 #define SSH_AUTH_DENIED 1
00090 #define SSH_AUTH_PARTIAL 2
00091 #define SSH_AUTH_INFO 3
00092 #define SSH_AUTH_ERROR -1
00093
00094 #define SSH_AUTH_METHOD_PASSWORD 0x0001
00095 #define SSH_AUTH_METHOD_PUBLICKEY 0x0002
00096 #define SSH_AUTH_METHOD_HOSTBASED 0x0004
00097 #define SSH_AUTH_METHOD_INTERACTIVE 0x0008
00098
00099
00100
00101 #define SSH_CLOSED (1<<0)
00102 #define SSH_READ_PENDING (1<<1)
00103 #define SSH_CLOSED_ERROR (1<<2)
00104
00105 #define SSH_SERVER_ERROR -1
00106 #define SSH_SERVER_NOT_KNOWN 0
00107 #define SSH_SERVER_KNOWN_OK 1
00108 #define SSH_SERVER_KNOWN_CHANGED 2
00109 #define SSH_SERVER_FOUND_OTHER 3
00110
00111 #ifndef MD5_DIGEST_LEN
00112 #define MD5_DIGEST_LEN 16
00113 #endif
00114
00115
00116 #define SSH_NO_ERROR 0
00117 #define SSH_REQUEST_DENIED 1
00118 #define SSH_FATAL 2
00119 #define SSH_EINTR 3
00120
00121
00122 #define SSH_OK 0
00123 #define SSH_ERROR -1
00124 #define SSH_AGAIN 1
00125
00126 char *ssh_get_error(void *error);
00127 int ssh_get_error_code(void *error);
00128 void ssh_say(int priority, const char *format, ...);
00129 void ssh_set_verbosity(int num);
00136 enum {
00139 SSH_LOG_NOLOG=0,
00142 SSH_LOG_RARE,
00145 SSH_LOG_PROTOCOL,
00148 SSH_LOG_PACKET,
00151 SSH_LOG_FUNCTIONS
00152 };
00155
00156
00157
00158
00159
00160
00161
00162 void ssh_log(SSH_SESSION *session, int prioriry, char *format, ...);
00163
00164
00165 SSH_SESSION *ssh_new();
00166 void ssh_set_options(SSH_SESSION *session, SSH_OPTIONS *options);
00167 socket_t ssh_get_fd(SSH_SESSION *session);
00168 void ssh_silent_disconnect(SSH_SESSION *session);
00169 int ssh_get_version(SSH_SESSION *session);
00170 void ssh_set_fd_toread(SSH_SESSION *session);
00171 void ssh_set_fd_towrite(SSH_SESSION *session);
00172 void ssh_set_fd_except(SSH_SESSION *session);
00173
00174
00175
00176 int ssh_connect(SSH_SESSION *session);
00177 void ssh_disconnect(SSH_SESSION *session);
00178 int ssh_service_request(SSH_SESSION *session,char *service);
00179 char *ssh_get_issue_banner(SSH_SESSION *session);
00180
00181 const char *ssh_copyright();
00182
00183
00184
00185
00186 STRING *string_from_char(const char *what);
00187
00188 int string_len(STRING *str);
00189 STRING *string_new(unsigned int size);
00190
00191 void string_fill(STRING *str, const void *data,int len);
00192
00193 char *string_to_char(STRING *str);
00194 STRING *string_copy(STRING *str);
00195
00196 void string_burn(STRING *str);
00197 void *string_data(STRING *str);
00198 void string_free(STRING *str);
00199
00200
00201 void ssh_crypto_init();
00202
00203
00204 void ssh_print_hexa(char *descr, const unsigned char *what, int len);
00205 int ssh_get_random(void *where,int len,int strong);
00206
00207
00208 int ssh_get_pubkey_hash(SSH_SESSION *session,unsigned char hash[MD5_DIGEST_LEN]);
00209 STRING *ssh_get_pubkey(SSH_SESSION *session);
00210
00211
00212 int ssh_fd_poll(SSH_SESSION *session,int *write, int *except);
00213 int ssh_select(CHANNEL **channels,CHANNEL **outchannels, socket_t maxfd, fd_set *readfds, struct timeval *timeout);
00214
00215 void publickey_free(PUBLIC_KEY *key);
00216
00217
00218
00219 PRIVATE_KEY *privatekey_from_file(SSH_SESSION *session,char *filename,int type,char *passphrase);
00220 STRING *publickey_to_string(PUBLIC_KEY *key);
00221 PUBLIC_KEY *publickey_from_privatekey(PRIVATE_KEY *prv);
00222 void private_key_free(PRIVATE_KEY *prv);
00223 STRING *publickey_from_file(SSH_SESSION *session, char *filename,int *_type);
00224 STRING *publickey_from_next_file(SSH_SESSION *session,char **pub_keys_path,char **keys_path,
00225 char **privkeyfile,int *type,int *count);
00226 int ssh_is_server_known(SSH_SESSION *session);
00227 int ssh_write_knownhost(SSH_SESSION *session);
00228
00229
00230
00231 CHANNEL *channel_new(SSH_SESSION *session);
00232 int channel_open_forward(CHANNEL *channel,char *remotehost, int remoteport, char *sourcehost, int localport);
00233 int channel_open_session(CHANNEL *channel);
00234 void channel_free(CHANNEL *channel);
00235 int channel_request_pty(CHANNEL *channel);
00236 int channel_request_pty_size(CHANNEL *channel, char *term,int cols, int rows);
00237 int channel_change_pty_size(CHANNEL *channel,int cols,int rows);
00238 int channel_request_shell(CHANNEL *channel);
00239 int channel_request_subsystem(CHANNEL *channel, char *system);
00240 int channel_request_env(CHANNEL *channel,char *name, char *value);
00241 int channel_request_exec(CHANNEL *channel, char *cmd);
00242 int channel_request_sftp(CHANNEL *channel);
00243 int channel_write(CHANNEL *channel,void *data,int len);
00244 int channel_send_eof(CHANNEL *channel);
00245 int channel_is_eof(CHANNEL *channel);
00246 int channel_read(CHANNEL *channel, BUFFER *buffer,int bytes,int is_stderr);
00247 int channel_poll(CHANNEL *channel, int is_stderr);
00248 int channel_close(CHANNEL *channel);
00249 int channel_read_nonblocking(CHANNEL *channel, char *dest, int len, int is_stderr);
00250 int channel_is_open(CHANNEL *channel);
00251 int channel_is_closed(CHANNEL *channel);
00252 int channel_select(CHANNEL **readchans, CHANNEL **writechans, CHANNEL **exceptchans, struct
00253 timeval * timeout);
00254
00255
00256 SSH_OPTIONS *ssh_options_new();
00257 SSH_OPTIONS *ssh_options_copy(SSH_OPTIONS *opt);
00258 int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
00259 void ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
00260 void ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port);
00261 int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv);
00262 void ssh_options_set_host(SSH_OPTIONS *opt, const char *host);
00263 void ssh_options_set_fd(SSH_OPTIONS *opt, socket_t fd);
00264 void ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port);
00265 void ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity);
00266 void ssh_options_set_status_callback(SSH_OPTIONS *opt, void (*callback)
00267 (void *arg, float status), void *arg);
00268 void ssh_options_set_timeout(SSH_OPTIONS *opt, long seconds, long usec);
00269 void ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir);
00270 void ssh_options_set_known_hosts_file(SSH_OPTIONS *opt, const char *dir);
00271 void ssh_options_allow_ssh1(SSH_OPTIONS *opt, int allow);
00272 void ssh_options_allow_ssh2(SSH_OPTIONS *opt, int allow);
00273 void ssh_options_set_dsa_server_key(SSH_OPTIONS *opt, const char *dsakey);
00274 void ssh_options_set_rsa_server_key(SSH_OPTIONS *opt, const char *rsakey);
00275 void ssh_options_set_log_function(SSH_OPTIONS *opt,
00276 void (*callback)(const char *message, SSH_SESSION *session, int verbosity ));
00277 void ssh_options_set_log_verbosity(SSH_OPTIONS *opt, int verbosity);
00278
00279
00280
00281
00284 BUFFER *buffer_new();
00285 void buffer_free(BUFFER *buffer);
00286
00287 void *buffer_get(BUFFER *buffer);
00288
00289 int buffer_get_len(BUFFER *buffer);
00290
00291
00292
00293
00294
00295
00296
00297 int ssh_userauth_list(SSH_SESSION *session, const char *username);
00298 int ssh_userauth_none(SSH_SESSION *session, const char *username);
00299 int ssh_userauth_password(SSH_SESSION *session, const char *username, const char *password);
00300 int ssh_userauth_offer_pubkey(SSH_SESSION *session, const char *username, int type, STRING *publickey);
00301 int ssh_userauth_pubkey(SSH_SESSION *session, const char *username, STRING *publickey, PRIVATE_KEY *privatekey);
00302 int ssh_userauth_autopubkey(SSH_SESSION *session);
00303 int ssh_userauth_kbdint(SSH_SESSION *session, const char *user, const char *submethods);
00304 int ssh_userauth_kbdint_getnprompts(SSH_SESSION *session);
00305 char *ssh_userauth_kbdint_getname(SSH_SESSION *session);
00306 char *ssh_userauth_kbdint_getinstruction(SSH_SESSION *session);
00307 char *ssh_userauth_kbdint_getprompt(SSH_SESSION *session, int i, char *echo);
00308 void ssh_userauth_kbdint_setanswer(SSH_SESSION *session, unsigned int i, const char *answer);
00309
00310
00311
00312 int ssh_finalize();
00313 #ifdef __cplusplus
00314 }
00315 #endif
00316 #endif