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