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