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