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