libssh  0.8.0
priv.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2003-2009 by Aris Adamantiadis
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 /*
22  * priv.h file
23  * This include file contains everything you shouldn't deal with in
24  * user programs. Consider that anything in this file might change
25  * without notice; libssh.h file will keep backward compatibility
26  * on binary & source
27  */
28 
29 #ifndef _LIBSSH_PRIV_H
30 #define _LIBSSH_PRIV_H
31 
32 #include "config.h"
33 
34 #if !defined(HAVE_STRTOULL)
35 # if defined(HAVE___STRTOULL)
36 # define strtoull __strtoull
37 # elif defined(HAVE__STRTOUI64)
38 # define strtoull _strtoui64
39 # elif defined(__hpux) && defined(__LP64__)
40 # define strtoull strtoul
41 # else
42 # error "no strtoull function found"
43 # endif
44 #endif /* !defined(HAVE_STRTOULL) */
45 
46 #ifdef HAVE_BYTESWAP_H
47 #include <byteswap.h>
48 #endif
49 
50 #ifndef bswap_32
51 #define bswap_32(x) \
52  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
53  (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
54 #endif
55 
56 #ifdef _WIN32
57 
58 /* Imitate define of inttypes.h */
59 # ifndef PRIdS
60 # define PRIdS "Id"
61 # endif
62 
63 # ifndef PRIu64
64 # if __WORDSIZE == 64
65 # define PRIu64 "lu"
66 # else
67 # define PRIu64 "llu"
68 # endif /* __WORDSIZE */
69 # endif /* PRIu64 */
70 
71 # ifdef _MSC_VER
72 # include <stdio.h>
73 # include <stdarg.h> /* va_copy define check */
74 
75 /* On Microsoft compilers define inline to __inline on all others use inline */
76 # undef inline
77 # define inline __inline
78 
79 # ifndef va_copy
80 # define va_copy(dest, src) (dest = src)
81 # endif
82 
83 # define strcasecmp _stricmp
84 # define strncasecmp _strnicmp
85 # if ! defined(HAVE_ISBLANK)
86 # define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
87 # endif
88 
89 # define usleep(X) Sleep(((X)+1000)/1000)
90 
91 # undef strtok_r
92 # define strtok_r strtok_s
93 
94 # if defined(HAVE__SNPRINTF_S)
95 # undef snprintf
96 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
97 # else /* HAVE__SNPRINTF_S */
98 # if defined(HAVE__SNPRINTF)
99 # undef snprintf
100 # define snprintf _snprintf
101 # else /* HAVE__SNPRINTF */
102 # if !defined(HAVE_SNPRINTF)
103 # error "no snprintf compatible function found"
104 # endif /* HAVE_SNPRINTF */
105 # endif /* HAVE__SNPRINTF */
106 # endif /* HAVE__SNPRINTF_S */
107 
108 # if defined(HAVE__VSNPRINTF_S)
109 # undef vsnprintf
110 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
111 # else /* HAVE__VSNPRINTF_S */
112 # if defined(HAVE__VSNPRINTF)
113 # undef vsnprintf
114 # define vsnprintf _vsnprintf
115 # else
116 # if !defined(HAVE_VSNPRINTF)
117 # error "No vsnprintf compatible function found"
118 # endif /* HAVE_VSNPRINTF */
119 # endif /* HAVE__VSNPRINTF */
120 # endif /* HAVE__VSNPRINTF_S */
121 
122 # endif /* _MSC_VER */
123 
124 struct timeval;
125 int gettimeofday(struct timeval *__p, void *__t);
126 
127 #define _XCLOSESOCKET closesocket
128 
129 #else /* _WIN32 */
130 
131 #include <unistd.h>
132 #define PRIdS "zd"
133 
134 #define _XCLOSESOCKET close
135 
136 #endif /* _WIN32 */
137 
138 #include "libssh/libssh.h"
139 #include "libssh/callbacks.h"
140 
141 /* some constants */
142 #ifndef MAX_PACKAT_LEN
143 #define MAX_PACKET_LEN 262144
144 #endif
145 #ifndef ERROR_BUFFERLEN
146 #define ERROR_BUFFERLEN 1024
147 #endif
148 #ifndef CLIENTBANNER1
149 #define CLIENTBANNER1 "SSH-1.5-libssh_" SSH_STRINGIFY(LIBSSH_VERSION)
150 #endif
151 #ifndef CLIENTBANNER2
152 #define CLIENTBANNER2 "SSH-2.0-libssh_" SSH_STRINGIFY(LIBSSH_VERSION)
153 #endif
154 #ifndef KBDINT_MAX_PROMPT
155 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
156 #endif
157 #ifndef MAX_BUF_SIZE
158 #define MAX_BUF_SIZE 4096
159 #endif
160 
161 #ifndef HAVE_COMPILER__FUNC__
162 # ifdef HAVE_COMPILER__FUNCTION__
163 # define __func__ __FUNCTION__
164 # else
165 # error "Your system must provide a __func__ macro"
166 # endif
167 #endif
168 
169 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE)
170 # define LIBSSH_THREAD __thread
171 #elif defined(HAVE_MSC_THREAD_LOCAL_STORAGE)
172 # define LIBSSH_THREAD __declspec(thread)
173 #else
174 # define LIBSSH_THREAD
175 #endif
176 
177 /*
178  * This makes sure that the compiler doesn't optimize out the code
179  *
180  * Use it in a macro where the provided variable is 'x'.
181  */
182 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
183 # define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory")
184 #else
185 # define LIBSSH_MEM_PROTECTION
186 #endif
187 
188 /* forward declarations */
189 struct ssh_common_struct;
190 struct ssh_kex_struct;
191 
192 int ssh_get_key_params(ssh_session session, ssh_key *privkey);
193 
194 /* LOGGING */
195 void ssh_log_function(int verbosity,
196  const char *function,
197  const char *buffer);
198 #define SSH_LOG(priority, ...) \
199  _ssh_log(priority, __func__, __VA_ARGS__)
200 
201 /* LEGACY */
202 void ssh_log_common(struct ssh_common_struct *common,
203  int verbosity,
204  const char *function,
205  const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
206 
207 
208 /* ERROR HANDLING */
209 
210 /* error handling structure */
211 struct error_struct {
212  int error_code;
213  char error_buffer[ERROR_BUFFERLEN];
214 };
215 
216 #define ssh_set_error(error, code, ...) \
217  _ssh_set_error(error, code, __func__, __VA_ARGS__)
218 void _ssh_set_error(void *error,
219  int code,
220  const char *function,
221  const char *descr, ...) PRINTF_ATTRIBUTE(4, 5);
222 
223 #define ssh_set_error_oom(error) \
224  _ssh_set_error_oom(error, __func__)
225 void _ssh_set_error_oom(void *error, const char *function);
226 
227 #define ssh_set_error_invalid(error) \
228  _ssh_set_error_invalid(error, __func__)
229 void _ssh_set_error_invalid(void *error, const char *function);
230 
231 
232 /* server.c */
233 #ifdef WITH_SERVER
234 int ssh_auth_reply_default(ssh_session session,int partial);
235 int ssh_auth_reply_success(ssh_session session, int partial);
236 #endif
237 /* client.c */
238 
239 int ssh_send_banner(ssh_session session, int is_server);
240 
241 /* connect.c */
242 socket_t ssh_connect_host(ssh_session session, const char *host,const char
243  *bind_addr, int port, long timeout, long usec);
244 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
245  const char *bind_addr, int port);
246 
247 /* in base64.c */
248 ssh_buffer base64_to_bin(const char *source);
249 unsigned char *bin_to_base64(const unsigned char *source, int len);
250 
251 /* gzip.c */
252 int compress_buffer(ssh_session session,ssh_buffer buf);
253 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
254 
255 /* match.c */
256 int match_hostname(const char *host, const char *pattern, unsigned int len);
257 
258 /* connector.c */
259 int ssh_connector_set_event(ssh_connector connector, ssh_event event);
260 int ssh_connector_remove_event(ssh_connector connector);
261 
262 #ifndef MIN
263 #define MIN(a,b) ((a) < (b) ? (a) : (b))
264 #endif
265 
266 #ifndef MAX
267 #define MAX(a,b) ((a) > (b) ? (a) : (b))
268 #endif
269 
271 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
272 
274 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
275 
277 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
278 
280 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
281 
282 /*
283  * See http://llvm.org/bugs/show_bug.cgi?id=15495
284  */
285 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
286 
287 # define BURN_STRING(x) do { \
288  if ((x) != NULL) \
289  memset((x), '\0', strlen((x))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
290  } while(0)
291 
293 # define BURN_BUFFER(x, size) do { \
294  if ((x) != NULL) \
295  memset((x), '\0', (size)); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
296  } while(0)
297 #else /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
298 
299 # define BURN_STRING(x) do { \
300  if ((x) != NULL) memset((x), '\0', strlen((x))); \
301  } while(0)
302 
304 # define BURN_BUFFER(x, size) do { \
305  if ((x) != NULL) \
306  memset((x), '\0', (size)); \
307  } while(0)
308 #endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
309 
322 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
323 
327 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
328 
332 #ifdef HAVE_GCC_NARG_MACRO
333 /*
334  * Since MSVC 2010 there is a bug in passing __VA_ARGS__ to subsequent
335  * macros as a single token, which results in:
336  * warning C4003: not enough actual parameters for macro '_VA_ARG_N'
337  * and incorrect behavior. This fixes issue.
338  */
339 #define VA_APPLY_VARIADIC_MACRO(macro, tuple) macro tuple
340 
341 #define __VA_NARG__(...) \
342  (__VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) - 1)
343 #define __VA_NARG_(...) \
344  VA_APPLY_VARIADIC_MACRO(__VA_ARG_N, (__VA_ARGS__))
345 #define __VA_ARG_N( \
346  _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
347  _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
348  _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
349  _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
350  _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
351  _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
352  _61,_62,_63,N,...) N
353 #define __RSEQ_N() \
354  63, 62, 61, 60, \
355  59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
356  49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
357  39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
358  29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
359  19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
360  9, 8, 7, 6, 5, 4, 3, 2, 1, 0
361 #else
362 /* clang does not support the above construction */
363 #define __VA_NARG__(...) (-1)
364 #endif
365 
366 #define CLOSE_SOCKET(s) do { if ((s) != SSH_INVALID_SOCKET) { _XCLOSESOCKET(s); (s) = SSH_INVALID_SOCKET;} } while(0)
367 
368 #ifndef HAVE_HTONLL
369 # ifdef WORDS_BIGENDIAN
370 # define htonll(x) (x)
371 # else
372 # define htonll(x) \
373  (((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
374 # endif
375 #endif
376 
377 #ifndef HAVE_NTOHLL
378 # ifdef WORDS_BIGENDIAN
379 # define ntohll(x) (x)
380 # else
381 # define ntohll(x) \
382  (((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
383 # endif
384 #endif
385 
386 #ifndef FALL_THROUGH
387 # ifdef HAVE_FALLTHROUGH_ATTRIBUTE
388 # define FALL_THROUGH __attribute__ ((fallthrough))
389 # else /* HAVE_FALLTHROUGH_ATTRIBUTE */
390 # define FALL_THROUGH
391 # endif /* HAVE_FALLTHROUGH_ATTRIBUTE */
392 #endif /* FALL_THROUGH */
393 
394 void ssh_agent_state_free(void *data);
395 
396 #endif /* _LIBSSH_PRIV_H */
397 /* vim: set ts=4 sw=4 et cindent: */