libssh  0.5.4
libssh.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2003-2009 by Aris Adamantiadis
5  *
6  * The SSH Library is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * The SSH Library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with the SSH Library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19  * MA 02111-1307, USA.
20  */
21 
22 #ifndef _LIBSSH_H
23 #define _LIBSSH_H
24 
25 #if defined _WIN32 || defined __CYGWIN__
26  #ifdef LIBSSH_STATIC
27  #define LIBSSH_API
28  #else
29  #ifdef LIBSSH_EXPORTS
30  #ifdef __GNUC__
31  #define LIBSSH_API __attribute__((dllexport))
32  #else
33  #define LIBSSH_API __declspec(dllexport)
34  #endif
35  #else
36  #ifdef __GNUC__
37  #define LIBSSH_API __attribute__((dllimport))
38  #else
39  #define LIBSSH_API __declspec(dllimport)
40  #endif
41  #endif
42  #endif
43 #else
44  #if __GNUC__ >= 4 && !defined(__OS2__)
45  #define LIBSSH_API __attribute__((visibility("default")))
46  #else
47  #define LIBSSH_API
48  #endif
49 #endif
50 
51 #ifdef _MSC_VER
52  /* Visual Studio hasn't inttypes.h so it doesn't know uint32_t */
53  typedef int int32_t;
54  typedef unsigned int uint32_t;
55  typedef unsigned short uint16_t;
56  typedef unsigned char uint8_t;
57  typedef unsigned long long uint64_t;
58  typedef int mode_t;
59 #else /* _MSC_VER */
60  #include <unistd.h>
61  #include <inttypes.h>
62 #endif /* _MSC_VER */
63 
64 #ifdef _WIN32
65  #include <winsock2.h>
66 #else /* _WIN32 */
67  #include <sys/select.h> /* for fd_set * */
68  #include <netdb.h>
69 #endif /* _WIN32 */
70 
71 #define SSH_STRINGIFY(s) SSH_TOSTRING(s)
72 #define SSH_TOSTRING(s) #s
73 
74 /* libssh version macros */
75 #define SSH_VERSION_INT(a, b, c) ((a) << 16 | (b) << 8 | (c))
76 #define SSH_VERSION_DOT(a, b, c) a ##.## b ##.## c
77 #define SSH_VERSION(a, b, c) SSH_VERSION_DOT(a, b, c)
78 
79 /* libssh version */
80 #define LIBSSH_VERSION_MAJOR 0
81 #define LIBSSH_VERSION_MINOR 5
82 #define LIBSSH_VERSION_MICRO 2
83 
84 #define LIBSSH_VERSION_INT SSH_VERSION_INT(LIBSSH_VERSION_MAJOR, \
85  LIBSSH_VERSION_MINOR, \
86  LIBSSH_VERSION_MICRO)
87 #define LIBSSH_VERSION SSH_VERSION(LIBSSH_VERSION_MAJOR, \
88  LIBSSH_VERSION_MINOR, \
89  LIBSSH_VERSION_MICRO)
90 
91 /* GCC have printf type attribute check. */
92 #ifdef __GNUC__
93 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
94 #else
95 #define PRINTF_ATTRIBUTE(a,b)
96 #endif /* __GNUC__ */
97 
98 #ifdef __GNUC__
99 #define SSH_DEPRECATED __attribute__ ((deprecated))
100 #else
101 #define SSH_DEPRECATED
102 #endif
103 
104 #ifdef __cplusplus
105 extern "C" {
106 #endif
107 
108 
109 typedef struct ssh_agent_struct* ssh_agent;
110 typedef struct ssh_buffer_struct* ssh_buffer;
111 typedef struct ssh_channel_struct* ssh_channel;
112 typedef struct ssh_message_struct* ssh_message;
113 typedef struct ssh_pcap_file_struct* ssh_pcap_file;
114 typedef struct ssh_private_key_struct* ssh_private_key;
115 typedef struct ssh_public_key_struct* ssh_public_key;
116 typedef struct ssh_key_struct* ssh_key;
117 typedef struct ssh_scp_struct* ssh_scp;
118 typedef struct ssh_session_struct* ssh_session;
119 typedef struct ssh_string_struct* ssh_string;
120 
121 /* Socket type */
122 #ifdef _WIN32
123 #ifndef socket_t
124 typedef SOCKET socket_t;
125 #endif /* socket_t */
126 #else /* _WIN32 */
127 #ifndef socket_t
128 typedef int socket_t;
129 #endif
130 #endif /* _WIN32 */
131 
132 #define SSH_INVALID_SOCKET ((socket_t) -1)
133 
134 /* the offsets of methods */
135 enum ssh_kex_types_e {
136  SSH_KEX=0,
137  SSH_HOSTKEYS,
138  SSH_CRYPT_C_S,
139  SSH_CRYPT_S_C,
140  SSH_MAC_C_S,
141  SSH_MAC_S_C,
142  SSH_COMP_C_S,
143  SSH_COMP_S_C,
144  SSH_LANG_C_S,
145  SSH_LANG_S_C
146 };
147 
148 #define SSH_CRYPT 2
149 #define SSH_MAC 3
150 #define SSH_COMP 4
151 #define SSH_LANG 5
152 
153 enum ssh_auth_e {
154  SSH_AUTH_SUCCESS=0,
155  SSH_AUTH_DENIED,
156  SSH_AUTH_PARTIAL,
157  SSH_AUTH_INFO,
158  SSH_AUTH_AGAIN,
159  SSH_AUTH_ERROR=-1
160 };
161 
162 /* auth flags */
163 #define SSH_AUTH_METHOD_UNKNOWN 0
164 #define SSH_AUTH_METHOD_NONE 0x0001
165 #define SSH_AUTH_METHOD_PASSWORD 0x0002
166 #define SSH_AUTH_METHOD_PUBLICKEY 0x0004
167 #define SSH_AUTH_METHOD_HOSTBASED 0x0008
168 #define SSH_AUTH_METHOD_INTERACTIVE 0x0010
169 
170 /* messages */
171 enum ssh_requests_e {
172  SSH_REQUEST_AUTH=1,
173  SSH_REQUEST_CHANNEL_OPEN,
174  SSH_REQUEST_CHANNEL,
175  SSH_REQUEST_SERVICE,
176  SSH_REQUEST_GLOBAL
177 };
178 
179 enum ssh_channel_type_e {
180  SSH_CHANNEL_UNKNOWN=0,
181  SSH_CHANNEL_SESSION,
182  SSH_CHANNEL_DIRECT_TCPIP,
183  SSH_CHANNEL_FORWARDED_TCPIP,
184  SSH_CHANNEL_X11
185 };
186 
187 enum ssh_channel_requests_e {
188  SSH_CHANNEL_REQUEST_UNKNOWN=0,
189  SSH_CHANNEL_REQUEST_PTY,
190  SSH_CHANNEL_REQUEST_EXEC,
191  SSH_CHANNEL_REQUEST_SHELL,
192  SSH_CHANNEL_REQUEST_ENV,
193  SSH_CHANNEL_REQUEST_SUBSYSTEM,
194  SSH_CHANNEL_REQUEST_WINDOW_CHANGE
195 };
196 
197 enum ssh_global_requests_e {
198  SSH_GLOBAL_REQUEST_UNKNOWN=0,
199  SSH_GLOBAL_REQUEST_TCPIP_FORWARD,
200  SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD,
201 };
202 
203 enum ssh_publickey_state_e {
204  SSH_PUBLICKEY_STATE_ERROR=-1,
205  SSH_PUBLICKEY_STATE_NONE=0,
206  SSH_PUBLICKEY_STATE_VALID=1,
207  SSH_PUBLICKEY_STATE_WRONG=2
208 };
209 
210 /* status flags */
211 #define SSH_CLOSED 0x01
212 #define SSH_READ_PENDING 0x02
213 #define SSH_CLOSED_ERROR 0x04
214 
215 enum ssh_server_known_e {
216  SSH_SERVER_ERROR=-1,
217  SSH_SERVER_NOT_KNOWN=0,
218  SSH_SERVER_KNOWN_OK,
219  SSH_SERVER_KNOWN_CHANGED,
220  SSH_SERVER_FOUND_OTHER,
221  SSH_SERVER_FILE_NOT_FOUND
222 };
223 
224 #ifndef MD5_DIGEST_LEN
225  #define MD5_DIGEST_LEN 16
226 #endif
227 /* errors */
228 
229 enum ssh_error_types_e {
230  SSH_NO_ERROR=0,
231  SSH_REQUEST_DENIED,
232  SSH_FATAL,
233  SSH_EINTR
234 };
235 
236 /* some types for keys */
237 enum ssh_keytypes_e{
238  SSH_KEYTYPE_UNKNOWN=0,
239  SSH_KEYTYPE_DSS=1,
240  SSH_KEYTYPE_RSA,
241  SSH_KEYTYPE_RSA1
242 };
243 
244 /* Error return codes */
245 #define SSH_OK 0 /* No error */
246 #define SSH_ERROR -1 /* Error of some kind */
247 #define SSH_AGAIN -2 /* The nonblocking call must be repeated */
248 #define SSH_EOF -127 /* We have already a eof */
249 
259 enum {
275 };
278 enum ssh_options_e {
279  SSH_OPTIONS_HOST,
280  SSH_OPTIONS_PORT,
281  SSH_OPTIONS_PORT_STR,
282  SSH_OPTIONS_FD,
283  SSH_OPTIONS_USER,
284  SSH_OPTIONS_SSH_DIR,
285  SSH_OPTIONS_IDENTITY,
286  SSH_OPTIONS_ADD_IDENTITY,
287  SSH_OPTIONS_KNOWNHOSTS,
288  SSH_OPTIONS_TIMEOUT,
289  SSH_OPTIONS_TIMEOUT_USEC,
290  SSH_OPTIONS_SSH1,
291  SSH_OPTIONS_SSH2,
292  SSH_OPTIONS_LOG_VERBOSITY,
293  SSH_OPTIONS_LOG_VERBOSITY_STR,
294  SSH_OPTIONS_CIPHERS_C_S,
295  SSH_OPTIONS_CIPHERS_S_C,
296  SSH_OPTIONS_COMPRESSION_C_S,
297  SSH_OPTIONS_COMPRESSION_S_C,
298  SSH_OPTIONS_PROXYCOMMAND,
299  SSH_OPTIONS_BINDADDR,
300  SSH_OPTIONS_STRICTHOSTKEYCHECK,
301  SSH_OPTIONS_COMPRESSION,
302  SSH_OPTIONS_COMPRESSION_LEVEL
303 };
304 
305 enum {
307  SSH_SCP_WRITE,
309  SSH_SCP_READ,
310  SSH_SCP_RECURSIVE=0x10
311 };
312 
313 enum ssh_scp_request_types {
315  SSH_SCP_REQUEST_NEWDIR=1,
317  SSH_SCP_REQUEST_NEWFILE,
319  SSH_SCP_REQUEST_EOF,
321  SSH_SCP_REQUEST_ENDDIR,
323  SSH_SCP_REQUEST_WARNING
324 };
325 
326 LIBSSH_API int ssh_blocking_flush(ssh_session session, int timeout);
327 LIBSSH_API ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms);
328 LIBSSH_API int ssh_channel_change_pty_size(ssh_channel channel,int cols,int rows);
329 LIBSSH_API int ssh_channel_close(ssh_channel channel);
330 LIBSSH_API void ssh_channel_free(ssh_channel channel);
331 LIBSSH_API int ssh_channel_get_exit_status(ssh_channel channel);
332 LIBSSH_API ssh_session ssh_channel_get_session(ssh_channel channel);
333 LIBSSH_API int ssh_channel_is_closed(ssh_channel channel);
334 LIBSSH_API int ssh_channel_is_eof(ssh_channel channel);
335 LIBSSH_API int ssh_channel_is_open(ssh_channel channel);
336 LIBSSH_API ssh_channel ssh_channel_new(ssh_session session);
337 LIBSSH_API int ssh_channel_open_forward(ssh_channel channel, const char *remotehost,
338  int remoteport, const char *sourcehost, int localport);
339 LIBSSH_API int ssh_channel_open_session(ssh_channel channel);
340 LIBSSH_API int ssh_channel_poll(ssh_channel channel, int is_stderr);
341 LIBSSH_API int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_stderr);
342 LIBSSH_API int ssh_channel_read_nonblocking(ssh_channel channel, void *dest, uint32_t count,
343  int is_stderr);
344 LIBSSH_API int ssh_channel_request_env(ssh_channel channel, const char *name, const char *value);
345 LIBSSH_API int ssh_channel_request_exec(ssh_channel channel, const char *cmd);
346 LIBSSH_API int ssh_channel_request_pty(ssh_channel channel);
347 LIBSSH_API int ssh_channel_request_pty_size(ssh_channel channel, const char *term,
348  int cols, int rows);
349 LIBSSH_API int ssh_channel_request_shell(ssh_channel channel);
350 LIBSSH_API int ssh_channel_request_send_signal(ssh_channel channel, const char *signum);
351 LIBSSH_API int ssh_channel_request_sftp(ssh_channel channel);
352 LIBSSH_API int ssh_channel_request_subsystem(ssh_channel channel, const char *subsystem);
353 LIBSSH_API int ssh_channel_request_x11(ssh_channel channel, int single_connection, const char *protocol,
354  const char *cookie, int screen_number);
355 LIBSSH_API int ssh_channel_send_eof(ssh_channel channel);
356 LIBSSH_API int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans, ssh_channel *exceptchans, struct
357  timeval * timeout);
358 LIBSSH_API void ssh_channel_set_blocking(ssh_channel channel, int blocking);
359 LIBSSH_API int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len);
360 LIBSSH_API uint32_t ssh_channel_window_size(ssh_channel channel);
361 
362 LIBSSH_API int ssh_try_publickey_from_file(ssh_session session, const char *keyfile,
363  ssh_string *publickey, int *type);
364 
365 LIBSSH_API int ssh_auth_list(ssh_session session);
366 LIBSSH_API char *ssh_basename (const char *path);
367 LIBSSH_API void ssh_clean_pubkey_hash(unsigned char **hash);
368 LIBSSH_API int ssh_connect(ssh_session session);
369 LIBSSH_API const char *ssh_copyright(void);
370 LIBSSH_API void ssh_disconnect(ssh_session session);
371 LIBSSH_API char *ssh_dirname (const char *path);
372 LIBSSH_API int ssh_finalize(void);
373 LIBSSH_API ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms);
374 LIBSSH_API int ssh_forward_cancel(ssh_session session, const char *address, int port);
375 LIBSSH_API int ssh_forward_listen(ssh_session session, const char *address, int port, int *bound_port);
376 LIBSSH_API void ssh_free(ssh_session session);
377 LIBSSH_API const char *ssh_get_disconnect_message(ssh_session session);
378 LIBSSH_API const char *ssh_get_error(void *error);
379 LIBSSH_API int ssh_get_error_code(void *error);
380 LIBSSH_API socket_t ssh_get_fd(ssh_session session);
381 LIBSSH_API char *ssh_get_hexa(const unsigned char *what, size_t len);
382 LIBSSH_API char *ssh_get_issue_banner(ssh_session session);
383 LIBSSH_API int ssh_get_openssh_version(ssh_session session);
384 LIBSSH_API ssh_string ssh_get_pubkey(ssh_session session);
385 LIBSSH_API int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash);
386 LIBSSH_API int ssh_get_random(void *where,int len,int strong);
387 LIBSSH_API int ssh_get_version(ssh_session session);
388 LIBSSH_API int ssh_get_status(ssh_session session);
389 LIBSSH_API int ssh_init(void);
390 LIBSSH_API int ssh_is_blocking(ssh_session session);
391 LIBSSH_API int ssh_is_connected(ssh_session session);
392 LIBSSH_API int ssh_is_server_known(ssh_session session);
393 LIBSSH_API void ssh_log(ssh_session session, int prioriry, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
394 LIBSSH_API ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg);
395 LIBSSH_API int ssh_message_channel_request_reply_success(ssh_message msg);
396 LIBSSH_API void ssh_message_free(ssh_message msg);
397 LIBSSH_API ssh_message ssh_message_get(ssh_session session);
398 LIBSSH_API int ssh_message_subtype(ssh_message msg);
399 LIBSSH_API int ssh_message_type(ssh_message msg);
400 LIBSSH_API int ssh_mkdir (const char *pathname, mode_t mode);
401 LIBSSH_API ssh_session ssh_new(void);
402 
403 LIBSSH_API int ssh_options_copy(ssh_session src, ssh_session *dest);
404 LIBSSH_API int ssh_options_getopt(ssh_session session, int *argcptr, char **argv);
405 LIBSSH_API int ssh_options_parse_config(ssh_session session, const char *filename);
406 LIBSSH_API int ssh_options_set(ssh_session session, enum ssh_options_e type,
407  const void *value);
408 LIBSSH_API int ssh_pcap_file_close(ssh_pcap_file pcap);
409 LIBSSH_API void ssh_pcap_file_free(ssh_pcap_file pcap);
410 LIBSSH_API ssh_pcap_file ssh_pcap_file_new(void);
411 LIBSSH_API int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename);
412 
413 LIBSSH_API enum ssh_keytypes_e ssh_privatekey_type(ssh_private_key privatekey);
414 
415 LIBSSH_API void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len);
416 LIBSSH_API int ssh_scp_accept_request(ssh_scp scp);
417 LIBSSH_API int ssh_scp_close(ssh_scp scp);
418 LIBSSH_API int ssh_scp_deny_request(ssh_scp scp, const char *reason);
419 LIBSSH_API void ssh_scp_free(ssh_scp scp);
420 LIBSSH_API int ssh_scp_init(ssh_scp scp);
421 LIBSSH_API int ssh_scp_leave_directory(ssh_scp scp);
422 LIBSSH_API ssh_scp ssh_scp_new(ssh_session session, int mode, const char *location);
423 LIBSSH_API int ssh_scp_pull_request(ssh_scp scp);
424 LIBSSH_API int ssh_scp_push_directory(ssh_scp scp, const char *dirname, int mode);
425 LIBSSH_API int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, int perms);
426 LIBSSH_API int ssh_scp_read(ssh_scp scp, void *buffer, size_t size);
427 LIBSSH_API const char *ssh_scp_request_get_filename(ssh_scp scp);
428 LIBSSH_API int ssh_scp_request_get_permissions(ssh_scp scp);
429 LIBSSH_API size_t ssh_scp_request_get_size(ssh_scp scp);
430 LIBSSH_API const char *ssh_scp_request_get_warning(ssh_scp scp);
431 LIBSSH_API int ssh_scp_write(ssh_scp scp, const void *buffer, size_t len);
432 LIBSSH_API int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd,
433  fd_set *readfds, struct timeval *timeout);
434 LIBSSH_API int ssh_service_request(ssh_session session, const char *service);
435 LIBSSH_API void ssh_set_blocking(ssh_session session, int blocking);
436 LIBSSH_API void ssh_set_fd_except(ssh_session session);
437 LIBSSH_API void ssh_set_fd_toread(ssh_session session);
438 LIBSSH_API void ssh_set_fd_towrite(ssh_session session);
439 LIBSSH_API void ssh_silent_disconnect(ssh_session session);
440 LIBSSH_API int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcapfile);
441 #ifndef _WIN32
442 LIBSSH_API int ssh_userauth_agent_pubkey(ssh_session session, const char *username,
443  ssh_public_key publickey);
444 #endif
445 LIBSSH_API int ssh_userauth_autopubkey(ssh_session session, const char *passphrase);
446 LIBSSH_API int ssh_userauth_kbdint(ssh_session session, const char *user, const char *submethods);
447 LIBSSH_API const char *ssh_userauth_kbdint_getinstruction(ssh_session session);
448 LIBSSH_API const char *ssh_userauth_kbdint_getname(ssh_session session);
449 LIBSSH_API int ssh_userauth_kbdint_getnprompts(ssh_session session);
450 LIBSSH_API const char *ssh_userauth_kbdint_getprompt(ssh_session session, unsigned int i, char *echo);
451 LIBSSH_API int ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i,
452  const char *answer);
453 LIBSSH_API int ssh_userauth_list(ssh_session session, const char *username);
454 LIBSSH_API int ssh_userauth_none(ssh_session session, const char *username);
455 LIBSSH_API int ssh_userauth_offer_pubkey(ssh_session session, const char *username, int type, ssh_string publickey);
456 LIBSSH_API int ssh_userauth_password(ssh_session session, const char *username, const char *password);
457 LIBSSH_API int ssh_userauth_pubkey(ssh_session session, const char *username, ssh_string publickey, ssh_private_key privatekey);
458 LIBSSH_API int ssh_userauth_privatekey_file(ssh_session session, const char *username,
459  const char *filename, const char *passphrase);
460 LIBSSH_API const char *ssh_version(int req_version);
461 LIBSSH_API int ssh_write_knownhost(ssh_session session);
462 
463 LIBSSH_API void ssh_string_burn(ssh_string str);
464 LIBSSH_API ssh_string ssh_string_copy(ssh_string str);
465 LIBSSH_API void *ssh_string_data(ssh_string str);
466 LIBSSH_API int ssh_string_fill(ssh_string str, const void *data, size_t len);
467 LIBSSH_API void ssh_string_free(ssh_string str);
468 LIBSSH_API ssh_string ssh_string_from_char(const char *what);
469 LIBSSH_API size_t ssh_string_len(ssh_string str);
470 LIBSSH_API ssh_string ssh_string_new(size_t size);
471 LIBSSH_API char *ssh_string_to_char(ssh_string str);
472 LIBSSH_API void ssh_string_free_char(char *s);
473 
474 LIBSSH_API int ssh_getpass(const char *prompt, char *buf, size_t len, int echo,
475  int verify);
476 
477 #ifndef LIBSSH_LEGACY_0_4
478 #include "libssh/legacy.h"
479 #endif
480 
481 #ifdef __cplusplus
482 }
483 #endif
484 #endif /* _LIBSSH_H */
485 /* vim: set ts=2 sw=2 et cindent: */