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