1 /* 2 * This file is part of the SSH Library 3 * 4 * Copyright (c) 2003-2021 by Aris Adamantiadis and the libssh team 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #ifndef _LIBSSH_H 22 #define _LIBSSH_H 23 24 #include <libssh/libssh_version.h> 25 26 #if defined _WIN32 || defined __CYGWIN__ 27 #ifdef LIBSSH_STATIC 28 #define LIBSSH_API 29 #else 30 #ifdef LIBSSH_EXPORTS 31 #ifdef __GNUC__ 32 #define LIBSSH_API __attribute__((dllexport)) 33 #else 34 #define LIBSSH_API __declspec(dllexport) 35 #endif 36 #else 37 #ifdef __GNUC__ 38 #define LIBSSH_API __attribute__((dllimport)) 39 #else 40 #define LIBSSH_API __declspec(dllimport) 41 #endif 42 #endif 43 #endif 44 #else 45 #if __GNUC__ >= 4 && !defined(__OS2__) 46 #define LIBSSH_API __attribute__((visibility("default"))) 47 #else 48 #define LIBSSH_API 49 #endif 50 #endif 51 52 #ifdef _MSC_VER 53 /* Visual Studio hasn't inttypes.h so it doesn't know uint32_t */ 54 typedef int int32_t; 55 typedef unsigned int uint32_t; 56 typedef unsigned short uint16_t; 57 typedef unsigned char uint8_t; 58 typedef unsigned long long uint64_t; 59 typedef int mode_t; 60 #else /* _MSC_VER */ 61 #include <unistd.h> 62 #include <inttypes.h> 63 #include <sys/types.h> 64 #endif /* _MSC_VER */ 65 66 #ifdef _WIN32 67 #include <winsock2.h> 68 #else /* _WIN32 */ 69 #include <sys/select.h> /* for fd_set * */ 70 #include <netdb.h> 71 #endif /* _WIN32 */ 72 73 #define SSH_STRINGIFY(s) SSH_TOSTRING(s) 74 #define SSH_TOSTRING(s) #s 75 76 /* GCC have printf type attribute check. */ 77 #ifdef __GNUC__ 78 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) 79 #else 80 #define PRINTF_ATTRIBUTE(a,b) 81 #endif /* __GNUC__ */ 82 83 #ifdef __GNUC__ 84 #define SSH_DEPRECATED __attribute__ ((deprecated)) 85 #else 86 #define SSH_DEPRECATED 87 #endif 88 89 #ifdef __cplusplus 90 extern "C" { 91 #endif 92 93 struct ssh_counter_struct { 94 uint64_t in_bytes; 95 uint64_t out_bytes; 96 uint64_t in_packets; 97 uint64_t out_packets; 98 }; 99 typedef struct ssh_counter_struct *ssh_counter; 100 101 typedef struct ssh_agent_struct* ssh_agent; 102 typedef struct ssh_buffer_struct* ssh_buffer; 103 typedef struct ssh_channel_struct* ssh_channel; 104 typedef struct ssh_message_struct* ssh_message; 105 typedef struct ssh_pcap_file_struct* ssh_pcap_file; 106 typedef struct ssh_key_struct* ssh_key; 107 typedef struct ssh_scp_struct* ssh_scp; 108 typedef struct ssh_session_struct* ssh_session; 109 typedef struct ssh_string_struct* ssh_string; 110 typedef struct ssh_event_struct* ssh_event; 111 typedef struct ssh_connector_struct * ssh_connector; 112 typedef void* ssh_gssapi_creds; 113 114 /* Socket type */ 115 #ifdef _WIN32 116 #ifndef socket_t 117 typedef SOCKET socket_t; 118 #endif /* socket_t */ 119 #else /* _WIN32 */ 120 #ifndef socket_t 121 typedef int socket_t; 122 #endif 123 #endif /* _WIN32 */ 124 125 #define SSH_INVALID_SOCKET ((socket_t) -1) 126 127 /* the offsets of methods */ 128 enum ssh_kex_types_e { 129 SSH_KEX=0, 130 SSH_HOSTKEYS, 131 SSH_CRYPT_C_S, 132 SSH_CRYPT_S_C, 133 SSH_MAC_C_S, 134 SSH_MAC_S_C, 135 SSH_COMP_C_S, 136 SSH_COMP_S_C, 137 SSH_LANG_C_S, 138 SSH_LANG_S_C 139 }; 140 141 #define SSH_CRYPT 2 142 #define SSH_MAC 3 143 #define SSH_COMP 4 144 #define SSH_LANG 5 145 146 enum ssh_auth_e { 147 SSH_AUTH_SUCCESS=0, 148 SSH_AUTH_DENIED, 149 SSH_AUTH_PARTIAL, 150 SSH_AUTH_INFO, 151 SSH_AUTH_AGAIN, 152 SSH_AUTH_ERROR=-1 153 }; 154 155 /* auth flags */ 156 #define SSH_AUTH_METHOD_UNKNOWN 0x0000u 157 #define SSH_AUTH_METHOD_NONE 0x0001u 158 #define SSH_AUTH_METHOD_PASSWORD 0x0002u 159 #define SSH_AUTH_METHOD_PUBLICKEY 0x0004u 160 #define SSH_AUTH_METHOD_HOSTBASED 0x0008u 161 #define SSH_AUTH_METHOD_INTERACTIVE 0x0010u 162 #define SSH_AUTH_METHOD_GSSAPI_MIC 0x0020u 163 164 /* messages */ 165 enum ssh_requests_e { 166 SSH_REQUEST_AUTH=1, 167 SSH_REQUEST_CHANNEL_OPEN, 168 SSH_REQUEST_CHANNEL, 169 SSH_REQUEST_SERVICE, 170 SSH_REQUEST_GLOBAL 171 }; 172 173 enum ssh_channel_type_e { 174 SSH_CHANNEL_UNKNOWN=0, 175 SSH_CHANNEL_SESSION, 176 SSH_CHANNEL_DIRECT_TCPIP, 177 SSH_CHANNEL_FORWARDED_TCPIP, 178 SSH_CHANNEL_X11, 179 SSH_CHANNEL_AUTH_AGENT 180 }; 181 182 enum ssh_channel_requests_e { 183 SSH_CHANNEL_REQUEST_UNKNOWN=0, 184 SSH_CHANNEL_REQUEST_PTY, 185 SSH_CHANNEL_REQUEST_EXEC, 186 SSH_CHANNEL_REQUEST_SHELL, 187 SSH_CHANNEL_REQUEST_ENV, 188 SSH_CHANNEL_REQUEST_SUBSYSTEM, 189 SSH_CHANNEL_REQUEST_WINDOW_CHANGE, 190 SSH_CHANNEL_REQUEST_X11 191 }; 192 193 enum ssh_global_requests_e { 194 SSH_GLOBAL_REQUEST_UNKNOWN=0, 195 SSH_GLOBAL_REQUEST_TCPIP_FORWARD, 196 SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD, 197 SSH_GLOBAL_REQUEST_KEEPALIVE 198 }; 199 200 enum ssh_publickey_state_e { 201 SSH_PUBLICKEY_STATE_ERROR=-1, 202 SSH_PUBLICKEY_STATE_NONE=0, 203 SSH_PUBLICKEY_STATE_VALID=1, 204 SSH_PUBLICKEY_STATE_WRONG=2 205 }; 206 207 /* Status flags */ 208 /** Socket is closed */ 209 #define SSH_CLOSED 0x01 210 /** Reading to socket won't block */ 211 #define SSH_READ_PENDING 0x02 212 /** Session was closed due to an error */ 213 #define SSH_CLOSED_ERROR 0x04 214 /** Output buffer not empty */ 215 #define SSH_WRITE_PENDING 0x08 216 217 enum ssh_server_known_e { 218 SSH_SERVER_ERROR=-1, 219 SSH_SERVER_NOT_KNOWN=0, 220 SSH_SERVER_KNOWN_OK, 221 SSH_SERVER_KNOWN_CHANGED, 222 SSH_SERVER_FOUND_OTHER, 223 SSH_SERVER_FILE_NOT_FOUND 224 }; 225 226 enum ssh_known_hosts_e { 227 /** 228 * There had been an error checking the host. 229 */ 230 SSH_KNOWN_HOSTS_ERROR = -2, 231 232 /** 233 * The known host file does not exist. The host is thus unknown. File will 234 * be created if host key is accepted. 235 */ 236 SSH_KNOWN_HOSTS_NOT_FOUND = -1, 237 238 /** 239 * The server is unknown. User should confirm the public key hash is 240 * correct. 241 */ 242 SSH_KNOWN_HOSTS_UNKNOWN = 0, 243 244 /** 245 * The server is known and has not changed. 246 */ 247 SSH_KNOWN_HOSTS_OK, 248 249 /** 250 * The server key has changed. Either you are under attack or the 251 * administrator changed the key. You HAVE to warn the user about a 252 * possible attack. 253 */ 254 SSH_KNOWN_HOSTS_CHANGED, 255 256 /** 257 * The server gave use a key of a type while we had an other type recorded. 258 * It is a possible attack. 259 */ 260 SSH_KNOWN_HOSTS_OTHER, 261 }; 262 263 #ifndef MD5_DIGEST_LEN 264 #define MD5_DIGEST_LEN 16 265 #endif 266 /* errors */ 267 268 enum ssh_error_types_e { 269 SSH_NO_ERROR=0, 270 SSH_REQUEST_DENIED, 271 SSH_FATAL, 272 SSH_EINTR 273 }; 274 275 /* some types for keys */ 276 enum ssh_keytypes_e{ 277 SSH_KEYTYPE_UNKNOWN=0, 278 SSH_KEYTYPE_DSS=1, 279 SSH_KEYTYPE_RSA, 280 SSH_KEYTYPE_RSA1, 281 SSH_KEYTYPE_ECDSA, /* deprecated */ 282 SSH_KEYTYPE_ED25519, 283 SSH_KEYTYPE_DSS_CERT01, 284 SSH_KEYTYPE_RSA_CERT01, 285 SSH_KEYTYPE_ECDSA_P256, 286 SSH_KEYTYPE_ECDSA_P384, 287 SSH_KEYTYPE_ECDSA_P521, 288 SSH_KEYTYPE_ECDSA_P256_CERT01, 289 SSH_KEYTYPE_ECDSA_P384_CERT01, 290 SSH_KEYTYPE_ECDSA_P521_CERT01, 291 SSH_KEYTYPE_ED25519_CERT01, 292 }; 293 294 enum ssh_keycmp_e { 295 SSH_KEY_CMP_PUBLIC = 0, 296 SSH_KEY_CMP_PRIVATE 297 }; 298 299 #define SSH_ADDRSTRLEN 46 300 301 struct ssh_knownhosts_entry { 302 char *hostname; 303 char *unparsed; 304 ssh_key publickey; 305 char *comment; 306 }; 307 308 309 /* Error return codes */ 310 #define SSH_OK 0 /* No error */ 311 #define SSH_ERROR -1 /* Error of some kind */ 312 #define SSH_AGAIN -2 /* The nonblocking call must be repeated */ 313 #define SSH_EOF -127 /* We have already a eof */ 314 315 /** 316 * @addtogroup libssh_log 317 * 318 * @{ 319 */ 320 321 enum { 322 /** No logging at all 323 */ 324 SSH_LOG_NOLOG=0, 325 /** Only warnings 326 */ 327 SSH_LOG_WARNING, 328 /** High level protocol information 329 */ 330 SSH_LOG_PROTOCOL, 331 /** Lower level protocol infomations, packet level 332 */ 333 SSH_LOG_PACKET, 334 /** Every function path 335 */ 336 SSH_LOG_FUNCTIONS 337 }; 338 /** @} */ 339 #define SSH_LOG_RARE SSH_LOG_WARNING 340 341 /** 342 * @name Logging levels 343 * 344 * @brief Debug levels for logging. 345 * @{ 346 */ 347 348 /** No logging at all */ 349 #define SSH_LOG_NONE 0 350 /** Show only warnings */ 351 #define SSH_LOG_WARN 1 352 /** Get some information what's going on */ 353 #define SSH_LOG_INFO 2 354 /** Get detailed debuging information **/ 355 #define SSH_LOG_DEBUG 3 356 /** Get trace output, packet information, ... */ 357 #define SSH_LOG_TRACE 4 358 359 /** @} */ 360 361 enum ssh_options_e { 362 SSH_OPTIONS_HOST, 363 SSH_OPTIONS_PORT, 364 SSH_OPTIONS_PORT_STR, 365 SSH_OPTIONS_FD, 366 SSH_OPTIONS_USER, 367 SSH_OPTIONS_SSH_DIR, 368 SSH_OPTIONS_IDENTITY, 369 SSH_OPTIONS_ADD_IDENTITY, 370 SSH_OPTIONS_KNOWNHOSTS, 371 SSH_OPTIONS_TIMEOUT, 372 SSH_OPTIONS_TIMEOUT_USEC, 373 SSH_OPTIONS_SSH1, 374 SSH_OPTIONS_SSH2, 375 SSH_OPTIONS_LOG_VERBOSITY, 376 SSH_OPTIONS_LOG_VERBOSITY_STR, 377 SSH_OPTIONS_CIPHERS_C_S, 378 SSH_OPTIONS_CIPHERS_S_C, 379 SSH_OPTIONS_COMPRESSION_C_S, 380 SSH_OPTIONS_COMPRESSION_S_C, 381 SSH_OPTIONS_PROXYCOMMAND, 382 SSH_OPTIONS_BINDADDR, 383 SSH_OPTIONS_STRICTHOSTKEYCHECK, 384 SSH_OPTIONS_COMPRESSION, 385 SSH_OPTIONS_COMPRESSION_LEVEL, 386 SSH_OPTIONS_KEY_EXCHANGE, 387 SSH_OPTIONS_HOSTKEYS, 388 SSH_OPTIONS_GSSAPI_SERVER_IDENTITY, 389 SSH_OPTIONS_GSSAPI_CLIENT_IDENTITY, 390 SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS, 391 SSH_OPTIONS_HMAC_C_S, 392 SSH_OPTIONS_HMAC_S_C, 393 SSH_OPTIONS_PASSWORD_AUTH, 394 SSH_OPTIONS_PUBKEY_AUTH, 395 SSH_OPTIONS_KBDINT_AUTH, 396 SSH_OPTIONS_GSSAPI_AUTH, 397 SSH_OPTIONS_GLOBAL_KNOWNHOSTS, 398 SSH_OPTIONS_NODELAY, 399 SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES, 400 SSH_OPTIONS_PROCESS_CONFIG, 401 SSH_OPTIONS_REKEY_DATA, 402 SSH_OPTIONS_REKEY_TIME, 403 }; 404 405 enum { 406 /** Code is going to write/create remote files */ 407 SSH_SCP_WRITE, 408 /** Code is going to read remote files */ 409 SSH_SCP_READ, 410 SSH_SCP_RECURSIVE=0x10 411 }; 412 413 enum ssh_scp_request_types { 414 /** A new directory is going to be pulled */ 415 SSH_SCP_REQUEST_NEWDIR=1, 416 /** A new file is going to be pulled */ 417 SSH_SCP_REQUEST_NEWFILE, 418 /** End of requests */ 419 SSH_SCP_REQUEST_EOF, 420 /** End of directory */ 421 SSH_SCP_REQUEST_ENDDIR, 422 /** Warning received */ 423 SSH_SCP_REQUEST_WARNING 424 }; 425 426 enum ssh_connector_flags_e { 427 /** Only the standard stream of the channel */ 428 SSH_CONNECTOR_STDOUT = 1, 429 SSH_CONNECTOR_STDINOUT = 1, 430 /** Only the exception stream of the channel */ 431 SSH_CONNECTOR_STDERR = 2, 432 /** Merge both standard and exception streams */ 433 SSH_CONNECTOR_BOTH = 3 434 }; 435 436 LIBSSH_API int ssh_blocking_flush(ssh_session session, int timeout); 437 LIBSSH_API ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms); 438 LIBSSH_API int ssh_channel_change_pty_size(ssh_channel channel,int cols,int rows); 439 LIBSSH_API int ssh_channel_close(ssh_channel channel); 440 LIBSSH_API void ssh_channel_free(ssh_channel channel); 441 LIBSSH_API int ssh_channel_get_exit_status(ssh_channel channel); 442 LIBSSH_API ssh_session ssh_channel_get_session(ssh_channel channel); 443 LIBSSH_API int ssh_channel_is_closed(ssh_channel channel); 444 LIBSSH_API int ssh_channel_is_eof(ssh_channel channel); 445 LIBSSH_API int ssh_channel_is_open(ssh_channel channel); 446 LIBSSH_API ssh_channel ssh_channel_new(ssh_session session); 447 LIBSSH_API int ssh_channel_open_auth_agent(ssh_channel channel); 448 LIBSSH_API int ssh_channel_open_forward(ssh_channel channel, const char *remotehost, 449 int remoteport, const char *sourcehost, int localport); 450 LIBSSH_API int ssh_channel_open_forward_unix(ssh_channel channel, const char *remotepath, 451 const char *sourcehost, int localport); 452 LIBSSH_API int ssh_channel_open_session(ssh_channel channel); 453 LIBSSH_API int ssh_channel_open_x11(ssh_channel channel, const char *orig_addr, int orig_port); 454 LIBSSH_API int ssh_channel_poll(ssh_channel channel, int is_stderr); 455 LIBSSH_API int ssh_channel_poll_timeout(ssh_channel channel, int timeout, int is_stderr); 456 LIBSSH_API int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_stderr); 457 LIBSSH_API int ssh_channel_read_timeout(ssh_channel channel, void *dest, uint32_t count, int is_stderr, int timeout_ms); 458 LIBSSH_API int ssh_channel_read_nonblocking(ssh_channel channel, void *dest, uint32_t count, 459 int is_stderr); 460 LIBSSH_API int ssh_channel_request_env(ssh_channel channel, const char *name, const char *value); 461 LIBSSH_API int ssh_channel_request_exec(ssh_channel channel, const char *cmd); 462 LIBSSH_API int ssh_channel_request_pty(ssh_channel channel); 463 LIBSSH_API int ssh_channel_request_pty_size(ssh_channel channel, const char *term, 464 int cols, int rows); 465 LIBSSH_API int ssh_channel_request_shell(ssh_channel channel); 466 LIBSSH_API int ssh_channel_request_send_signal(ssh_channel channel, const char *signum); 467 LIBSSH_API int ssh_channel_request_send_break(ssh_channel channel, uint32_t length); 468 LIBSSH_API int ssh_channel_request_sftp(ssh_channel channel); 469 LIBSSH_API int ssh_channel_request_subsystem(ssh_channel channel, const char *subsystem); 470 LIBSSH_API int ssh_channel_request_x11(ssh_channel channel, int single_connection, const char *protocol, 471 const char *cookie, int screen_number); 472 LIBSSH_API int ssh_channel_request_auth_agent(ssh_channel channel); 473 LIBSSH_API int ssh_channel_send_eof(ssh_channel channel); 474 LIBSSH_API int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans, ssh_channel *exceptchans, struct 475 timeval * timeout); 476 LIBSSH_API void ssh_channel_set_blocking(ssh_channel channel, int blocking); 477 LIBSSH_API void ssh_channel_set_counter(ssh_channel channel, 478 ssh_counter counter); 479 LIBSSH_API int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len); 480 LIBSSH_API int ssh_channel_write_stderr(ssh_channel channel, 481 const void *data, 482 uint32_t len); 483 LIBSSH_API uint32_t ssh_channel_window_size(ssh_channel channel); 484 485 LIBSSH_API char *ssh_basename (const char *path); 486 LIBSSH_API void ssh_clean_pubkey_hash(unsigned char **hash); 487 LIBSSH_API int ssh_connect(ssh_session session); 488 489 LIBSSH_API ssh_connector ssh_connector_new(ssh_session session); 490 LIBSSH_API void ssh_connector_free(ssh_connector connector); 491 LIBSSH_API int ssh_connector_set_in_channel(ssh_connector connector, 492 ssh_channel channel, 493 enum ssh_connector_flags_e flags); 494 LIBSSH_API int ssh_connector_set_out_channel(ssh_connector connector, 495 ssh_channel channel, 496 enum ssh_connector_flags_e flags); 497 LIBSSH_API void ssh_connector_set_in_fd(ssh_connector connector, socket_t fd); 498 LIBSSH_API void ssh_connector_set_out_fd(ssh_connector connector, socket_t fd); 499 500 LIBSSH_API const char *ssh_copyright(void); 501 LIBSSH_API void ssh_disconnect(ssh_session session); 502 LIBSSH_API char *ssh_dirname (const char *path); 503 LIBSSH_API int ssh_finalize(void); 504 505 /* REVERSE PORT FORWARDING */ 506 LIBSSH_API ssh_channel ssh_channel_accept_forward(ssh_session session, 507 int timeout_ms, 508 int *destination_port); 509 LIBSSH_API int ssh_channel_cancel_forward(ssh_session session, 510 const char *address, 511 int port); 512 LIBSSH_API int ssh_channel_listen_forward(ssh_session session, 513 const char *address, 514 int port, 515 int *bound_port); 516 517 LIBSSH_API void ssh_free(ssh_session session); 518 LIBSSH_API const char *ssh_get_disconnect_message(ssh_session session); 519 LIBSSH_API const char *ssh_get_error(void *error); 520 LIBSSH_API int ssh_get_error_code(void *error); 521 LIBSSH_API socket_t ssh_get_fd(ssh_session session); 522 LIBSSH_API char *ssh_get_hexa(const unsigned char *what, size_t len); 523 LIBSSH_API char *ssh_get_issue_banner(ssh_session session); 524 LIBSSH_API int ssh_get_openssh_version(ssh_session session); 525 526 LIBSSH_API int ssh_get_server_publickey(ssh_session session, ssh_key *key); 527 528 enum ssh_publickey_hash_type { 529 SSH_PUBLICKEY_HASH_SHA1, 530 SSH_PUBLICKEY_HASH_MD5, 531 SSH_PUBLICKEY_HASH_SHA256 532 }; 533 LIBSSH_API int ssh_get_publickey_hash(const ssh_key key, 534 enum ssh_publickey_hash_type type, 535 unsigned char **hash, 536 size_t *hlen); 537 538 /* DEPRECATED FUNCTIONS */ 539 SSH_DEPRECATED LIBSSH_API int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash); 540 SSH_DEPRECATED LIBSSH_API ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms); 541 SSH_DEPRECATED LIBSSH_API int ssh_forward_cancel(ssh_session session, const char *address, int port); 542 SSH_DEPRECATED LIBSSH_API int ssh_forward_listen(ssh_session session, const char *address, int port, int *bound_port); 543 SSH_DEPRECATED LIBSSH_API int ssh_get_publickey(ssh_session session, ssh_key *key); 544 SSH_DEPRECATED LIBSSH_API int ssh_write_knownhost(ssh_session session); 545 SSH_DEPRECATED LIBSSH_API char *ssh_dump_knownhost(ssh_session session); 546 SSH_DEPRECATED LIBSSH_API int ssh_is_server_known(ssh_session session); 547 SSH_DEPRECATED LIBSSH_API void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len); 548 549 550 551 LIBSSH_API int ssh_get_random(void *where,int len,int strong); 552 LIBSSH_API int ssh_get_version(ssh_session session); 553 LIBSSH_API int ssh_get_status(ssh_session session); 554 LIBSSH_API int ssh_get_poll_flags(ssh_session session); 555 LIBSSH_API int ssh_init(void); 556 LIBSSH_API int ssh_is_blocking(ssh_session session); 557 LIBSSH_API int ssh_is_connected(ssh_session session); 558 559 /* KNOWN HOSTS */ 560 LIBSSH_API void ssh_knownhosts_entry_free(struct ssh_knownhosts_entry *entry); 561 #define SSH_KNOWNHOSTS_ENTRY_FREE(e) do { \ 562 if ((e) != NULL) { \ 563 ssh_knownhosts_entry_free(e); \ 564 e = NULL; \ 565 } \ 566 } while(0) 567 568 LIBSSH_API int ssh_known_hosts_parse_line(const char *host, 569 const char *line, 570 struct ssh_knownhosts_entry **entry); 571 LIBSSH_API enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session); 572 573 LIBSSH_API int ssh_session_export_known_hosts_entry(ssh_session session, 574 char **pentry_string); 575 LIBSSH_API int ssh_session_update_known_hosts(ssh_session session); 576 577 LIBSSH_API enum ssh_known_hosts_e ssh_session_get_known_hosts_entry(ssh_session session, 578 struct ssh_knownhosts_entry **pentry); 579 LIBSSH_API enum ssh_known_hosts_e ssh_session_is_known_server(ssh_session session); 580 581 /* LOGGING */ 582 LIBSSH_API int ssh_set_log_level(int level); 583 LIBSSH_API int ssh_get_log_level(void); 584 LIBSSH_API void *ssh_get_log_userdata(void); 585 LIBSSH_API int ssh_set_log_userdata(void *data); 586 LIBSSH_API void _ssh_log(int verbosity, 587 const char *function, 588 const char *format, ...) PRINTF_ATTRIBUTE(3, 4); 589 590 /* legacy */ 591 SSH_DEPRECATED LIBSSH_API void ssh_log(ssh_session session, 592 int prioriry, 593 const char *format, ...) PRINTF_ATTRIBUTE(3, 4); 594 595 LIBSSH_API ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg); 596 LIBSSH_API int ssh_message_channel_request_open_reply_accept_channel(ssh_message msg, ssh_channel chan); 597 LIBSSH_API int ssh_message_channel_request_reply_success(ssh_message msg); 598 #define SSH_MESSAGE_FREE(x) \ 599 do { if ((x) != NULL) { ssh_message_free(x); (x) = NULL; } } while(0) 600 LIBSSH_API void ssh_message_free(ssh_message msg); 601 LIBSSH_API ssh_message ssh_message_get(ssh_session session); 602 LIBSSH_API int ssh_message_subtype(ssh_message msg); 603 LIBSSH_API int ssh_message_type(ssh_message msg); 604 LIBSSH_API int ssh_mkdir (const char *pathname, mode_t mode); 605 LIBSSH_API ssh_session ssh_new(void); 606 607 LIBSSH_API int ssh_options_copy(ssh_session src, ssh_session *dest); 608 LIBSSH_API int ssh_options_getopt(ssh_session session, int *argcptr, char **argv); 609 LIBSSH_API int ssh_options_parse_config(ssh_session session, const char *filename); 610 LIBSSH_API int ssh_options_set(ssh_session session, enum ssh_options_e type, 611 const void *value); 612 LIBSSH_API int ssh_options_get(ssh_session session, enum ssh_options_e type, 613 char **value); 614 LIBSSH_API int ssh_options_get_port(ssh_session session, unsigned int * port_target); 615 LIBSSH_API int ssh_pcap_file_close(ssh_pcap_file pcap); 616 LIBSSH_API void ssh_pcap_file_free(ssh_pcap_file pcap); 617 LIBSSH_API ssh_pcap_file ssh_pcap_file_new(void); 618 LIBSSH_API int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename); 619 620 /** 621 * @addtogroup libssh_auth 622 * 623 * @{ 624 */ 625 626 /** 627 * @brief SSH authentication callback for password and publickey auth. 628 * 629 * @param prompt Prompt to be displayed. 630 * @param buf Buffer to save the password. You should null-terminate it. 631 * @param len Length of the buffer. 632 * @param echo Enable or disable the echo of what you type. 633 * @param verify Should the password be verified? 634 * @param userdata Userdata to be passed to the callback function. Useful 635 * for GUI applications. 636 * 637 * @return 0 on success, < 0 on error. 638 */ 639 typedef int (*ssh_auth_callback) (const char *prompt, char *buf, size_t len, 640 int echo, int verify, void *userdata); 641 642 /** @} */ 643 644 LIBSSH_API ssh_key ssh_key_new(void); 645 #define SSH_KEY_FREE(x) \ 646 do { if ((x) != NULL) { ssh_key_free(x); x = NULL; } } while(0) 647 LIBSSH_API void ssh_key_free (ssh_key key); 648 LIBSSH_API enum ssh_keytypes_e ssh_key_type(const ssh_key key); 649 LIBSSH_API const char *ssh_key_type_to_char(enum ssh_keytypes_e type); 650 LIBSSH_API enum ssh_keytypes_e ssh_key_type_from_name(const char *name); 651 LIBSSH_API int ssh_key_is_public(const ssh_key k); 652 LIBSSH_API int ssh_key_is_private(const ssh_key k); 653 LIBSSH_API int ssh_key_cmp(const ssh_key k1, 654 const ssh_key k2, 655 enum ssh_keycmp_e what); 656 657 LIBSSH_API int ssh_pki_generate(enum ssh_keytypes_e type, int parameter, 658 ssh_key *pkey); 659 LIBSSH_API int ssh_pki_import_privkey_base64(const char *b64_key, 660 const char *passphrase, 661 ssh_auth_callback auth_fn, 662 void *auth_data, 663 ssh_key *pkey); 664 LIBSSH_API int ssh_pki_export_privkey_base64(const ssh_key privkey, 665 const char *passphrase, 666 ssh_auth_callback auth_fn, 667 void *auth_data, 668 char **b64_key); 669 LIBSSH_API int ssh_pki_import_privkey_file(const char *filename, 670 const char *passphrase, 671 ssh_auth_callback auth_fn, 672 void *auth_data, 673 ssh_key *pkey); 674 LIBSSH_API int ssh_pki_export_privkey_file(const ssh_key privkey, 675 const char *passphrase, 676 ssh_auth_callback auth_fn, 677 void *auth_data, 678 const char *filename); 679 680 LIBSSH_API int ssh_pki_copy_cert_to_privkey(const ssh_key cert_key, 681 ssh_key privkey); 682 683 LIBSSH_API int ssh_pki_import_pubkey_base64(const char *b64_key, 684 enum ssh_keytypes_e type, 685 ssh_key *pkey); 686 LIBSSH_API int ssh_pki_import_pubkey_file(const char *filename, 687 ssh_key *pkey); 688 689 LIBSSH_API int ssh_pki_import_cert_base64(const char *b64_cert, 690 enum ssh_keytypes_e type, 691 ssh_key *pkey); 692 LIBSSH_API int ssh_pki_import_cert_file(const char *filename, 693 ssh_key *pkey); 694 695 LIBSSH_API int ssh_pki_export_privkey_to_pubkey(const ssh_key privkey, 696 ssh_key *pkey); 697 LIBSSH_API int ssh_pki_export_pubkey_base64(const ssh_key key, 698 char **b64_key); 699 LIBSSH_API int ssh_pki_export_pubkey_file(const ssh_key key, 700 const char *filename); 701 702 LIBSSH_API const char *ssh_pki_key_ecdsa_name(const ssh_key key); 703 704 LIBSSH_API char *ssh_get_fingerprint_hash(enum ssh_publickey_hash_type type, 705 unsigned char *hash, 706 size_t len); 707 LIBSSH_API void ssh_print_hash(enum ssh_publickey_hash_type type, unsigned char *hash, size_t len); 708 LIBSSH_API int ssh_send_ignore (ssh_session session, const char *data); 709 LIBSSH_API int ssh_send_debug (ssh_session session, const char *message, int always_display); 710 LIBSSH_API void ssh_gssapi_set_creds(ssh_session session, const ssh_gssapi_creds creds); 711 LIBSSH_API int ssh_scp_accept_request(ssh_scp scp); 712 LIBSSH_API int ssh_scp_close(ssh_scp scp); 713 LIBSSH_API int ssh_scp_deny_request(ssh_scp scp, const char *reason); 714 LIBSSH_API void ssh_scp_free(ssh_scp scp); 715 LIBSSH_API int ssh_scp_init(ssh_scp scp); 716 LIBSSH_API int ssh_scp_leave_directory(ssh_scp scp); 717 LIBSSH_API ssh_scp ssh_scp_new(ssh_session session, int mode, const char *location); 718 LIBSSH_API int ssh_scp_pull_request(ssh_scp scp); 719 LIBSSH_API int ssh_scp_push_directory(ssh_scp scp, const char *dirname, int mode); 720 LIBSSH_API int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, int perms); 721 LIBSSH_API int ssh_scp_push_file64(ssh_scp scp, const char *filename, uint64_t size, int perms); 722 LIBSSH_API int ssh_scp_read(ssh_scp scp, void *buffer, size_t size); 723 LIBSSH_API const char *ssh_scp_request_get_filename(ssh_scp scp); 724 LIBSSH_API int ssh_scp_request_get_permissions(ssh_scp scp); 725 LIBSSH_API size_t ssh_scp_request_get_size(ssh_scp scp); 726 LIBSSH_API uint64_t ssh_scp_request_get_size64(ssh_scp scp); 727 LIBSSH_API const char *ssh_scp_request_get_warning(ssh_scp scp); 728 LIBSSH_API int ssh_scp_write(ssh_scp scp, const void *buffer, size_t len); 729 LIBSSH_API int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd, 730 fd_set *readfds, struct timeval *timeout); 731 LIBSSH_API int ssh_service_request(ssh_session session, const char *service); 732 LIBSSH_API int ssh_set_agent_channel(ssh_session session, ssh_channel channel); 733 LIBSSH_API int ssh_set_agent_socket(ssh_session session, socket_t fd); 734 LIBSSH_API void ssh_set_blocking(ssh_session session, int blocking); 735 LIBSSH_API void ssh_set_counters(ssh_session session, ssh_counter scounter, 736 ssh_counter rcounter); 737 LIBSSH_API void ssh_set_fd_except(ssh_session session); 738 LIBSSH_API void ssh_set_fd_toread(ssh_session session); 739 LIBSSH_API void ssh_set_fd_towrite(ssh_session session); 740 LIBSSH_API void ssh_silent_disconnect(ssh_session session); 741 LIBSSH_API int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcapfile); 742 743 /* USERAUTH */ 744 LIBSSH_API int ssh_userauth_none(ssh_session session, const char *username); 745 LIBSSH_API int ssh_userauth_list(ssh_session session, const char *username); 746 LIBSSH_API int ssh_userauth_try_publickey(ssh_session session, 747 const char *username, 748 const ssh_key pubkey); 749 LIBSSH_API int ssh_userauth_publickey(ssh_session session, 750 const char *username, 751 const ssh_key privkey); 752 #ifndef _WIN32 753 LIBSSH_API int ssh_userauth_agent(ssh_session session, 754 const char *username); 755 #endif 756 LIBSSH_API int ssh_userauth_publickey_auto(ssh_session session, 757 const char *username, 758 const char *passphrase); 759 LIBSSH_API int ssh_userauth_password(ssh_session session, 760 const char *username, 761 const char *password); 762 763 LIBSSH_API int ssh_userauth_kbdint(ssh_session session, const char *user, const char *submethods); 764 LIBSSH_API const char *ssh_userauth_kbdint_getinstruction(ssh_session session); 765 LIBSSH_API const char *ssh_userauth_kbdint_getname(ssh_session session); 766 LIBSSH_API int ssh_userauth_kbdint_getnprompts(ssh_session session); 767 LIBSSH_API const char *ssh_userauth_kbdint_getprompt(ssh_session session, unsigned int i, char *echo); 768 LIBSSH_API int ssh_userauth_kbdint_getnanswers(ssh_session session); 769 LIBSSH_API const char *ssh_userauth_kbdint_getanswer(ssh_session session, unsigned int i); 770 LIBSSH_API int ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i, 771 const char *answer); 772 LIBSSH_API int ssh_userauth_gssapi(ssh_session session); 773 LIBSSH_API const char *ssh_version(int req_version); 774 775 LIBSSH_API void ssh_string_burn(ssh_string str); 776 LIBSSH_API ssh_string ssh_string_copy(ssh_string str); 777 LIBSSH_API void *ssh_string_data(ssh_string str); 778 LIBSSH_API int ssh_string_fill(ssh_string str, const void *data, size_t len); 779 #define SSH_STRING_FREE(x) \ 780 do { if ((x) != NULL) { ssh_string_free(x); x = NULL; } } while(0) 781 LIBSSH_API void ssh_string_free(ssh_string str); 782 LIBSSH_API ssh_string ssh_string_from_char(const char *what); 783 LIBSSH_API size_t ssh_string_len(ssh_string str); 784 LIBSSH_API ssh_string ssh_string_new(size_t size); 785 LIBSSH_API const char *ssh_string_get_char(ssh_string str); 786 LIBSSH_API char *ssh_string_to_char(ssh_string str); 787 #define SSH_STRING_FREE_CHAR(x) \ 788 do { if ((x) != NULL) { ssh_string_free_char(x); x = NULL; } } while(0) 789 LIBSSH_API void ssh_string_free_char(char *s); 790 791 LIBSSH_API int ssh_getpass(const char *prompt, char *buf, size_t len, int echo, 792 int verify); 793 794 795 typedef int (*ssh_event_callback)(socket_t fd, int revents, void *userdata); 796 797 LIBSSH_API ssh_event ssh_event_new(void); 798 LIBSSH_API int ssh_event_add_fd(ssh_event event, socket_t fd, short events, 799 ssh_event_callback cb, void *userdata); 800 LIBSSH_API int ssh_event_add_session(ssh_event event, ssh_session session); 801 LIBSSH_API int ssh_event_add_connector(ssh_event event, ssh_connector connector); 802 LIBSSH_API int ssh_event_dopoll(ssh_event event, int timeout); 803 LIBSSH_API int ssh_event_remove_fd(ssh_event event, socket_t fd); 804 LIBSSH_API int ssh_event_remove_session(ssh_event event, ssh_session session); 805 LIBSSH_API int ssh_event_remove_connector(ssh_event event, ssh_connector connector); 806 LIBSSH_API void ssh_event_free(ssh_event event); 807 LIBSSH_API const char* ssh_get_clientbanner(ssh_session session); 808 LIBSSH_API const char* ssh_get_serverbanner(ssh_session session); 809 LIBSSH_API const char* ssh_get_kex_algo(ssh_session session); 810 LIBSSH_API const char* ssh_get_cipher_in(ssh_session session); 811 LIBSSH_API const char* ssh_get_cipher_out(ssh_session session); 812 LIBSSH_API const char* ssh_get_hmac_in(ssh_session session); 813 LIBSSH_API const char* ssh_get_hmac_out(ssh_session session); 814 815 LIBSSH_API ssh_buffer ssh_buffer_new(void); 816 LIBSSH_API void ssh_buffer_free(ssh_buffer buffer); 817 #define SSH_BUFFER_FREE(x) \ 818 do { if ((x) != NULL) { ssh_buffer_free(x); x = NULL; } } while(0) 819 LIBSSH_API int ssh_buffer_reinit(ssh_buffer buffer); 820 LIBSSH_API int ssh_buffer_add_data(ssh_buffer buffer, const void *data, uint32_t len); 821 LIBSSH_API uint32_t ssh_buffer_get_data(ssh_buffer buffer, void *data, uint32_t requestedlen); 822 LIBSSH_API void *ssh_buffer_get(ssh_buffer buffer); 823 LIBSSH_API uint32_t ssh_buffer_get_len(ssh_buffer buffer); 824 825 #ifndef LIBSSH_LEGACY_0_4 826 #include "libssh/legacy.h" 827 #endif 828 829 #ifdef __cplusplus 830 } 831 #endif 832 #endif /* _LIBSSH_H */ 833