00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _LIBSSH_H
00023 #define _LIBSSH_H
00024
00025 #ifndef _MSC_VER
00026 #include <unistd.h>
00027 #include <inttypes.h>
00028 #else
00029
00030 typedef unsigned int uint32_t;
00031 typedef unsigned short uint16_t;
00032 typedef unsigned char uint8_t;
00033 typedef unsigned long long uint64_t;
00034 #endif
00035
00036 #ifdef _WIN32
00037 #include <winsock2.h>
00038 #else
00039 #include <sys/select.h>
00040 #include <netdb.h>
00041 #endif
00042
00043 #define SSH_STRINGIFY(s) SSH_TOSTRING(s)
00044 #define SSH_TOSTRING(s) #s
00045
00046
00047 #define SSH_VERSION_INT(a, b, c) ((a) << 16 | (b) << 8 | (c))
00048 #define SSH_VERSION_DOT(a, b, c) a ##.## b ##.## c
00049 #define SSH_VERSION(a, b, c) SSH_VERSION_DOT(a, b, c)
00050
00051
00052 #define LIBSSH_VERSION_MAJOR 0
00053 #define LIBSSH_VERSION_MINOR 3
00054 #define LIBSSH_VERSION_MICRO 4
00055
00056 #define LIBSSH_VERSION_INT SSH_VERSION_INT(LIBSSH_VERSION_MAJOR, \
00057 LIBSSH_VERSION_MINOR, \
00058 LIBSSH_VERSION_MICRO)
00059 #define LIBSSH_VERSION SSH_VERSION(LIBSSH_VERSION_MAJOR, \
00060 LIBSSH_VERSION_MINOR, \
00061 LIBSSH_VERSION_MICRO)
00062
00063
00064 #ifdef __GNUC__
00065 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
00066 #else
00067 #define PRINTF_ATTRIBUTE(a,b)
00068 #endif
00069
00070 #ifdef __cplusplus
00071 extern "C" {
00072 #endif
00073
00074 typedef struct string_struct STRING;
00075 typedef struct buffer_struct BUFFER;
00076 typedef struct public_key_struct PUBLIC_KEY;
00077 typedef struct private_key_struct PRIVATE_KEY;
00078 typedef struct ssh_options_struct SSH_OPTIONS;
00079 typedef struct channel_struct CHANNEL;
00080 typedef struct agent_struct AGENT;
00081 typedef struct ssh_session SSH_SESSION;
00082 typedef struct ssh_kbdint SSH_KBDINT;
00083
00084
00085 typedef uint32_t u32;
00086 typedef uint16_t u16;
00087 typedef uint64_t u64;
00088 typedef uint8_t u8;
00089
00090
00091 #ifdef _WIN32
00092 #define socket_t SOCKET
00093 #else
00094 typedef int socket_t;
00095 #endif
00096
00097
00098 #define SSH_KEX 0
00099 #define SSH_HOSTKEYS 1
00100 #define SSH_CRYPT_C_S 2
00101 #define SSH_CRYPT_S_C 3
00102 #define SSH_MAC_C_S 4
00103 #define SSH_MAC_S_C 5
00104 #define SSH_COMP_C_S 6
00105 #define SSH_COMP_S_C 7
00106 #define SSH_LANG_C_S 8
00107 #define SSH_LANG_S_C 9
00108
00109 #define SSH_CRYPT 2
00110 #define SSH_MAC 3
00111 #define SSH_COMP 4
00112 #define SSH_LANG 5
00113
00114 #define SSH_AUTH_SUCCESS 0
00115 #define SSH_AUTH_DENIED 1
00116 #define SSH_AUTH_PARTIAL 2
00117 #define SSH_AUTH_INFO 3
00118 #define SSH_AUTH_ERROR -1
00119
00120 #define SSH_AUTH_METHOD_PASSWORD 0x0001
00121 #define SSH_AUTH_METHOD_PUBLICKEY 0x0002
00122 #define SSH_AUTH_METHOD_HOSTBASED 0x0004
00123 #define SSH_AUTH_METHOD_INTERACTIVE 0x0008
00124
00125
00126
00127 #define SSH_CLOSED (1<<0)
00128 #define SSH_READ_PENDING (1<<1)
00129 #define SSH_CLOSED_ERROR (1<<2)
00130
00131 #define SSH_SERVER_ERROR -1
00132 #define SSH_SERVER_NOT_KNOWN 0
00133 #define SSH_SERVER_KNOWN_OK 1
00134 #define SSH_SERVER_KNOWN_CHANGED 2
00135 #define SSH_SERVER_FOUND_OTHER 3
00136 #define SSH_SERVER_FILE_NOT_FOUND 4
00137
00138 #ifndef MD5_DIGEST_LEN
00139 #define MD5_DIGEST_LEN 16
00140 #endif
00141
00142
00143 #define SSH_NO_ERROR 0
00144 #define SSH_REQUEST_DENIED 1
00145 #define SSH_FATAL 2
00146 #define SSH_EINTR 3
00147
00148
00149 #define SSH_OK 0
00150 #define SSH_ERROR -1
00151 #define SSH_AGAIN -2
00152 #define SSH_EOF -127
00153
00154 const char *ssh_get_error(void *error);
00155 int ssh_get_error_code(void *error);
00156
00157
00158 const char *ssh_version(int req_version);
00159
00166 enum {
00169 SSH_LOG_NOLOG=0,
00172 SSH_LOG_RARE,
00175 SSH_LOG_PROTOCOL,
00178 SSH_LOG_PACKET,
00181 SSH_LOG_FUNCTIONS
00182 };
00185
00186
00187
00188
00189
00190
00191
00192 void ssh_log(SSH_SESSION *session, int prioriry, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
00193
00194
00195 SSH_SESSION *ssh_new(void);
00196 socket_t ssh_get_fd(SSH_SESSION *session);
00197 int ssh_get_version(SSH_SESSION *session);
00198 int ssh_get_status(SSH_SESSION *session);
00199 const char *ssh_get_disconnect_message(SSH_SESSION *session);
00200 void ssh_set_options(SSH_SESSION *session, SSH_OPTIONS *options);
00201 void ssh_set_fd_toread(SSH_SESSION *session);
00202 void ssh_set_fd_towrite(SSH_SESSION *session);
00203 void ssh_set_fd_except(SSH_SESSION *session);
00204 void ssh_set_blocking(SSH_SESSION *session, int blocking);
00205 void ssh_silent_disconnect(SSH_SESSION *session);
00206
00207
00208
00209 int ssh_connect(SSH_SESSION *session);
00210 void ssh_disconnect(SSH_SESSION *session);
00211 int ssh_service_request(SSH_SESSION *session, const char *service);
00212 char *ssh_get_issue_banner(SSH_SESSION *session);
00213 int ssh_get_openssh_version(SSH_SESSION *session);
00214
00215 const char *ssh_copyright(void);
00216
00217
00218
00219
00220
00221 STRING *string_from_char(const char *what);
00222
00223 size_t string_len(STRING *str);
00224 STRING *string_new(size_t size);
00225
00226 int string_fill(STRING *str, const void *data, size_t len);
00227
00228 char *string_to_char(STRING *str);
00229 STRING *string_copy(STRING *str);
00230
00231 void string_burn(STRING *str);
00232 void *string_data(STRING *str);
00233 void string_free(STRING *str);
00234
00235
00236 char *ssh_get_hexa(const unsigned char *what, size_t len);
00237 void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len);
00238 int ssh_get_random(void *where,int len,int strong);
00239
00240
00241 int ssh_get_pubkey_hash(SSH_SESSION *session, unsigned char **hash);
00242 STRING *ssh_get_pubkey(SSH_SESSION *session);
00243
00244
00245 int ssh_fd_poll(SSH_SESSION *session,int *write, int *except);
00246 int ssh_select(CHANNEL **channels, CHANNEL **outchannels, socket_t maxfd,
00247 fd_set *readfds, struct timeval *timeout);
00248
00249 void publickey_free(PUBLIC_KEY *key);
00250
00251
00252
00253 PRIVATE_KEY *privatekey_from_file(SSH_SESSION *session, const char *filename,
00254 int type, const char *passphrase);
00255 STRING *publickey_to_string(PUBLIC_KEY *key);
00256 PUBLIC_KEY *publickey_from_privatekey(PRIVATE_KEY *prv);
00257 void privatekey_free(PRIVATE_KEY *prv);
00258 STRING *publickey_from_file(SSH_SESSION *session, const char *filename,
00259 int *type);
00260 int ssh_is_server_known(SSH_SESSION *session);
00261 int ssh_write_knownhost(SSH_SESSION *session);
00262
00263
00264
00265 CHANNEL *channel_new(SSH_SESSION *session);
00266 int channel_open_forward(CHANNEL *channel, const char *remotehost,
00267 int remoteport, const char *sourcehost, int localport);
00268 int channel_open_session(CHANNEL *channel);
00269 void channel_free(CHANNEL *channel);
00270 int channel_request_pty(CHANNEL *channel);
00271 int channel_request_pty_size(CHANNEL *channel, const char *term,
00272 int cols, int rows);
00273 int channel_change_pty_size(CHANNEL *channel,int cols,int rows);
00274 int channel_request_shell(CHANNEL *channel);
00275 int channel_request_subsystem(CHANNEL *channel, const char *system);
00276 int channel_request_env(CHANNEL *channel, const char *name, const char *value);
00277 int channel_request_exec(CHANNEL *channel, const char *cmd);
00278 int channel_request_sftp(CHANNEL *channel);
00279 int channel_write(CHANNEL *channel, const void *data, u32 len);
00280 int channel_send_eof(CHANNEL *channel);
00281 int channel_is_eof(CHANNEL *channel);
00282 int channel_read(CHANNEL *channel, void *dest, u32 count, int is_stderr);
00283 int channel_read_buffer(CHANNEL *channel, BUFFER *buffer, u32 count,
00284 int is_stderr);
00285 int channel_poll(CHANNEL *channel, int is_stderr);
00286 int channel_close(CHANNEL *channel);
00287 void channel_set_blocking(CHANNEL *channel, int blocking);
00288 int channel_read_nonblocking(CHANNEL *channel, void *dest, u32 count,
00289 int is_stderr);
00290 int channel_is_open(CHANNEL *channel);
00291 int channel_is_closed(CHANNEL *channel);
00292 int channel_select(CHANNEL **readchans, CHANNEL **writechans, CHANNEL **exceptchans, struct
00293 timeval * timeout);
00294 SSH_SESSION *channel_get_session(CHANNEL *channel);
00295 int channel_get_exit_status(CHANNEL *channel);
00296
00297
00311 typedef int (*ssh_auth_callback) (const char *prompt, char *buf, size_t len,
00312 int echo, int verify, void *userdata);
00313
00314 SSH_OPTIONS *ssh_options_new(void);
00315 SSH_OPTIONS *ssh_options_copy(SSH_OPTIONS *opt);
00316 void ssh_options_free(SSH_OPTIONS *opt);
00317 int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
00318 int ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
00319 int ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port);
00320 int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv);
00321 int ssh_options_set_host(SSH_OPTIONS *opt, const char *host);
00322 int ssh_options_set_fd(SSH_OPTIONS *opt, socket_t fd);
00323 int ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port);
00324 int ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir);
00325 int ssh_options_set_known_hosts_file(SSH_OPTIONS *opt, const char *dir);
00326 int ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity);
00327 int ssh_options_set_banner(SSH_OPTIONS *opt, const char *banner);
00328 int ssh_options_set_status_callback(SSH_OPTIONS *opt, void (*callback)
00329 (void *arg, float status), void *arg);
00330 int ssh_options_set_timeout(SSH_OPTIONS *opt, long seconds, long usec);
00331 int ssh_options_allow_ssh1(SSH_OPTIONS *opt, int allow);
00332 int ssh_options_allow_ssh2(SSH_OPTIONS *opt, int allow);
00333 int ssh_options_set_log_function(SSH_OPTIONS *opt,
00334 void (*callback)(const char *message, SSH_SESSION *session, int verbosity));
00335 int ssh_options_set_log_verbosity(SSH_OPTIONS *opt, int verbosity);
00336 int ssh_options_set_dsa_server_key(SSH_OPTIONS *opt, const char *dsakey);
00337 int ssh_options_set_rsa_server_key(SSH_OPTIONS *opt, const char *rsakey);
00338 int ssh_options_set_auth_callback(SSH_OPTIONS *opt, ssh_auth_callback cb,
00339 void *userdata);
00340
00341
00342
00343
00346 BUFFER *buffer_new(void);
00347 void buffer_free(BUFFER *buffer);
00348
00349 void *buffer_get(BUFFER *buffer);
00350
00351 u32 buffer_get_len(BUFFER *buffer);
00352
00353
00354
00355 int ssh_auth_list(SSH_SESSION *session);
00356
00357
00358
00359
00360 int ssh_userauth_list(SSH_SESSION *session, const char *username);
00361 int ssh_userauth_none(SSH_SESSION *session, const char *username);
00362 int ssh_userauth_password(SSH_SESSION *session, const char *username, const char *password);
00363 int ssh_userauth_offer_pubkey(SSH_SESSION *session, const char *username, int type, STRING *publickey);
00364 int ssh_userauth_pubkey(SSH_SESSION *session, const char *username, STRING *publickey, PRIVATE_KEY *privatekey);
00365 int ssh_userauth_agent_pubkey(SSH_SESSION *session, const char *username,
00366 PUBLIC_KEY *publickey);
00367 int ssh_userauth_autopubkey(SSH_SESSION *session, const char *passphrase);
00368 int ssh_userauth_kbdint(SSH_SESSION *session, const char *user, const char *submethods);
00369 int ssh_userauth_kbdint_getnprompts(SSH_SESSION *session);
00370 const char *ssh_userauth_kbdint_getname(SSH_SESSION *session);
00371 const char *ssh_userauth_kbdint_getinstruction(SSH_SESSION *session);
00372 const char *ssh_userauth_kbdint_getprompt(SSH_SESSION *session, unsigned int i, char *echo);
00373 int ssh_userauth_kbdint_setanswer(SSH_SESSION *session, unsigned int i,
00374 const char *answer);
00375
00376
00377 int ssh_mkdir (const char *pathname, mode_t mode);
00378 char *ssh_dirname (const char *path);
00379 char *ssh_basename (const char *path);
00380
00381
00382 int ssh_init(void);
00383 int ssh_finalize(void);
00384
00385 #ifdef __cplusplus
00386 }
00387 #endif
00388 #endif
00389