00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _LIBSSH_PRIV_H
00030 #define _LIBSSH_PRIV_H
00031
00032 #include "config.h"
00033
00034 #if !defined(HAVE_STRTOULL)
00035 # if defined(HAVE___STRTOULL)
00036 # define strtoull __strtoull
00037 # elif defined(HAVE__STRTOUI64)
00038 # define strtoull _strtoui64
00039 # elif defined(__hpux) && defined(__LP64__)
00040 # define strtoull strtoul
00041 # else
00042 # error "no strtoull function found"
00043 # endif
00044 #endif
00045
00046 #ifdef _WIN32
00047
00048
00049 # ifndef PRIdS
00050 # define PRIdS "Id"
00051 # endif
00052
00053 # ifndef PRIu64
00054 # if __WORDSIZE == 64
00055 # define PRIu64 "lu"
00056 # else
00057 # define PRIu64 "llu"
00058 # endif
00059 # endif
00060
00061 # ifdef _MSC_VER
00062 # include <stdio.h>
00063
00064
00065 # undef inline
00066 # define inline __inline
00067
00068 # define strcasecmp _stricmp
00069 # define strncasecmp _strnicmp
00070 # if ! defined(HAVE_ISBLANK)
00071 # define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
00072 # endif
00073
00074 # define usleep(X) Sleep(((X)+1000)/1000)
00075
00076 # undef strtok_r
00077 # define strtok_r strtok_s
00078
00079 # if defined(HAVE__SNPRINTF_S)
00080 # undef snprintf
00081 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
00082 # else
00083 # if defined(HAVE__SNPRINTF)
00084 # undef snprintf
00085 # define snprintf _snprintf
00086 # else
00087 # if !defined(HAVE_SNPRINTF)
00088 # error "no snprintf compatible function found"
00089 # endif
00090 # endif
00091 # endif
00092
00093 # if defined(HAVE__VSNPRINTF_S)
00094 # undef vsnprintf
00095 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
00096 # else
00097 # if defined(HAVE__VSNPRINTF)
00098 # undef vsnprintf
00099 # define vsnprintf _vsnprintf
00100 # else
00101 # if !defined(HAVE_VSNPRINTF)
00102 # error "No vsnprintf compatible function found"
00103 # endif
00104 # endif
00105 # endif
00106
00107 # endif
00108
00109 struct timeval;
00110 int gettimeofday(struct timeval *__p, void *__t);
00111
00112 #define _XCLOSESOCKET closesocket
00113
00114 #else
00115
00116 #include <unistd.h>
00117 #define PRIdS "zd"
00118
00119 #define _XCLOSESOCKET close
00120
00121 #endif
00122
00123 #include "libssh/libssh.h"
00124 #include "libssh/callbacks.h"
00125
00126
00127 #ifndef MAX_PACKAT_LEN
00128 #define MAX_PACKET_LEN 262144
00129 #endif
00130 #ifndef ERROR_BUFFERLEN
00131 #define ERROR_BUFFERLEN 1024
00132 #endif
00133 #ifndef CLIENTBANNER1
00134 #define CLIENTBANNER1 "SSH-1.5-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
00135 #endif
00136 #ifndef CLIENTBANNER2
00137 #define CLIENTBANNER2 "SSH-2.0-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
00138 #endif
00139 #ifndef KBDINT_MAX_PROMPT
00140 #define KBDINT_MAX_PROMPT 256
00141 #endif
00142 #ifndef MAX_BUF_SIZE
00143 #define MAX_BUF_SIZE 4096
00144 #endif
00145
00146 #ifndef HAVE_COMPILER__FUNC__
00147 # ifdef HAVE_COMPILER__FUNCTION__
00148 # define __func__ __FUNCTION__
00149 # else
00150 # error "Your system must provide a __func__ macro"
00151 # endif
00152 #endif
00153
00154 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE)
00155 # define LIBSSH_THREAD __thread
00156 #elif defined(HAVE_MSC_THREAD_LOCAL_STORAGE)
00157 # define LIBSSH_THREAD __declspec(thread)
00158 #else
00159 # define LIBSSH_THREAD
00160 #endif
00161
00162
00163
00164
00165
00166
00167 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
00168 # define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory")
00169 #else
00170 # define LIBSSH_MEM_PROTECTION
00171 #endif
00172
00173 #ifdef HAVE_SYS_TIME_H
00174 #include <sys/time.h>
00175 #endif
00176
00177
00178 struct ssh_common_struct;
00179 struct ssh_kex_struct;
00180
00181 int ssh_get_key_params(ssh_session session, ssh_key *privkey);
00182
00183
00184 void ssh_log_function(int verbosity,
00185 const char *function,
00186 const char *buffer);
00187 #define SSH_LOG(priority, ...) \
00188 _ssh_log(priority, __func__, __VA_ARGS__)
00189
00190
00191 void ssh_log_common(struct ssh_common_struct *common,
00192 int verbosity,
00193 const char *function,
00194 const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
00195
00196
00197
00198
00199
00200 struct error_struct {
00201 int error_code;
00202 char error_buffer[ERROR_BUFFERLEN];
00203 };
00204
00205 #define ssh_set_error(error, code, ...) \
00206 _ssh_set_error(error, code, __func__, __VA_ARGS__)
00207 void _ssh_set_error(void *error,
00208 int code,
00209 const char *function,
00210 const char *descr, ...) PRINTF_ATTRIBUTE(4, 5);
00211
00212 #define ssh_set_error_oom(error) \
00213 _ssh_set_error_oom(error, __func__)
00214 void _ssh_set_error_oom(void *error, const char *function);
00215
00216 #define ssh_set_error_invalid(error) \
00217 _ssh_set_error_invalid(error, __func__)
00218 void _ssh_set_error_invalid(void *error, const char *function);
00219
00220
00221
00222 #ifdef WITH_SERVER
00223 int ssh_auth_reply_default(ssh_session session,int partial);
00224 int ssh_auth_reply_success(ssh_session session, int partial);
00225 #endif
00226
00227
00228 int ssh_send_banner(ssh_session session, int is_server);
00229
00230
00231 socket_t ssh_connect_host(ssh_session session, const char *host,const char
00232 *bind_addr, int port, long timeout, long usec);
00233 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
00234 const char *bind_addr, int port);
00235
00236
00237 ssh_buffer base64_to_bin(const char *source);
00238 unsigned char *bin_to_base64(const unsigned char *source, int len);
00239
00240
00241 int compress_buffer(ssh_session session,ssh_buffer buf);
00242 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
00243
00244
00245 int match_hostname(const char *host, const char *pattern, unsigned int len);
00246
00247 #ifndef MIN
00248 #define MIN(a,b) ((a) < (b) ? (a) : (b))
00249 #endif
00250
00252 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
00253
00255 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
00256
00258 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
00259
00261 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
00262
00263
00264
00265
00266 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
00267
00268 # define BURN_STRING(x) do { \
00269 if ((x) != NULL) \
00270 memset((x), '\0', strlen((x))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
00271 } while(0)
00272
00274 # define BURN_BUFFER(x, size) do { \
00275 if ((x) != NULL) \
00276 memset((x), '\0', (size)); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
00277 } while(0)
00278 #else
00279
00280 # define BURN_STRING(x) do { \
00281 if ((x) != NULL) memset((x), '\0', strlen((x))); \
00282 } while(0)
00283
00285 # define BURN_BUFFER(x, size) do { \
00286 if ((x) != NULL) \
00287 memset((x), '\0', (size)); \
00288 } while(0)
00289 #endif
00290
00303 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
00304
00308 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
00309
00313 #ifdef HAVE_GCC_NARG_MACRO
00314
00315
00316
00317
00318
00319
00320 #define VA_APPLY_VARIADIC_MACRO(macro, tuple) macro tuple
00321
00322 #define __VA_NARG__(...) \
00323 (__VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) - 1)
00324 #define __VA_NARG_(...) \
00325 VA_APPLY_VARIADIC_MACRO(__VA_ARG_N, (__VA_ARGS__))
00326 #define __VA_ARG_N( \
00327 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
00328 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
00329 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
00330 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
00331 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
00332 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
00333 _61,_62,_63,N,...) N
00334 #define __RSEQ_N() \
00335 63, 62, 61, 60, \
00336 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
00337 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
00338 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
00339 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
00340 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
00341 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
00342 #else
00343
00344 #define __VA_NARG__(...) (-1)
00345 #endif
00346
00347 #define CLOSE_SOCKET(s) do { if ((s) != SSH_INVALID_SOCKET) { _XCLOSESOCKET(s); (s) = SSH_INVALID_SOCKET;} } while(0)
00348
00349 #endif
00350