libssh  0.5.4
priv.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2003-2009 by Aris Adamantiadis
5  *
6  * The SSH Library is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * The SSH Library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with the SSH Library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19  * MA 02111-1307, USA.
20  */
21 
22 /*
23  * priv.h file
24  * This include file contains everything you shouldn't deal with in
25  * user programs. Consider that anything in this file might change
26  * without notice; libssh.h file will keep backward compatibility
27  * on binary & source
28  */
29 
30 #ifndef _LIBSSH_PRIV_H
31 #define _LIBSSH_PRIV_H
32 
33 #include "config.h"
34 
35 #ifdef _WIN32
36 
37 /* Imitate define of inttypes.h */
38 # ifndef PRIdS
39 # define PRIdS "Id"
40 # endif
41 
42 # ifdef _MSC_VER
43 # include <stdio.h>
44 
45 /* On Microsoft compilers define inline to __inline on all others use inline */
46 # undef inline
47 # define inline __inline
48 
49 # define strcasecmp _stricmp
50 # define strncasecmp _strnicmp
51 # define strtoull _strtoui64
52 # define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
53 
54 # define usleep(X) Sleep(((X)+1000)/1000)
55 
56 # undef strtok_r
57 # define strtok_r strtok_s
58 
59 # if defined(HAVE__SNPRINTF_S)
60 # undef snprintf
61 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
62 # else /* HAVE__SNPRINTF_S */
63 # if defined(HAVE__SNPRINTF)
64 # undef snprintf
65 # define snprintf _snprintf
66 # else /* HAVE__SNPRINTF */
67 # if !defined(HAVE_SNPRINTF)
68 # error "no snprintf compatible function found"
69 # endif /* HAVE_SNPRINTF */
70 # endif /* HAVE__SNPRINTF */
71 # endif /* HAVE__SNPRINTF_S */
72 
73 # if defined(HAVE__VSNPRINTF_S)
74 # undef vsnprintf
75 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
76 # else /* HAVE__VSNPRINTF_S */
77 # if defined(HAVE__VSNPRINTF)
78 # undef vsnprintf
79 # define vsnprintf _vsnprintf
80 # else
81 # if !defined(HAVE_VSNPRINTF)
82 # error "No vsnprintf compatible function found"
83 # endif /* HAVE_VSNPRINTF */
84 # endif /* HAVE__VSNPRINTF */
85 # endif /* HAVE__VSNPRINTF_S */
86 
87 # endif /* _MSC_VER */
88 
89 #else /* _WIN32 */
90 
91 #include <unistd.h>
92 #define PRIdS "zd"
93 
94 #endif /* _WIN32 */
95 
96 #include "libssh/libssh.h"
97 #include "libssh/callbacks.h"
98 #include "libssh/crypto.h"
99 
100 /* some constants */
101 #define MAX_PACKET_LEN 262144
102 #define ERROR_BUFFERLEN 1024
103 #define CLIENTBANNER1 "SSH-1.5-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
104 #define CLIENTBANNER2 "SSH-2.0-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
105 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
106 
107 #ifdef __cplusplus
108 extern "C" {
109 #endif
110 
111 
112 #ifdef HAVE_SYS_TIME_H
113 #include <sys/time.h>
114 #endif
115 
116 typedef struct kex_struct {
117  unsigned char cookie[16];
118  char **methods;
119 } KEX;
120 
121 struct error_struct {
122 /* error handling */
123  int error_code;
124  char error_buffer[ERROR_BUFFERLEN];
125 };
126 
127 /* TODO: remove that include */
128 #include "libssh/wrapper.h"
129 
130 struct ssh_keys_struct {
131  const char *privatekey;
132  const char *publickey;
133 };
134 
135 struct ssh_message_struct;
136 struct ssh_common_struct;
137 
138 /* server data */
139 
140 
141 SSH_PACKET_CALLBACK(ssh_packet_disconnect_callback);
142 SSH_PACKET_CALLBACK(ssh_packet_ignore_callback);
143 
144 /* client.c */
145 
146 int ssh_send_banner(ssh_session session, int is_server);
147 SSH_PACKET_CALLBACK(ssh_packet_dh_reply);
148 SSH_PACKET_CALLBACK(ssh_packet_newkeys);
149 SSH_PACKET_CALLBACK(ssh_packet_service_accept);
150 
151 /* config.c */
152 int ssh_config_parse_file(ssh_session session, const char *filename);
153 
154 /* errors.c */
155 void ssh_set_error(void *error, int code, const char *descr, ...) PRINTF_ATTRIBUTE(3, 4);
156 void ssh_set_error_oom(void *);
157 void ssh_set_error_invalid(void *, const char *);
158 
159 /* in crypt.c */
160 uint32_t packet_decrypt_len(ssh_session session,char *crypted);
161 int packet_decrypt(ssh_session session, void *packet,unsigned int len);
162 unsigned char *packet_encrypt(ssh_session session,void *packet,unsigned int len);
163  /* it returns the hmac buffer if exists*/
164 struct ssh_poll_handle_struct;
165 
166 int packet_hmac_verify(ssh_session session,ssh_buffer buffer,unsigned char *mac);
167 
168 struct ssh_socket_struct;
169 
170 int ssh_packet_socket_callback(const void *data, size_t len, void *user);
171 void ssh_packet_register_socket_callback(ssh_session session, struct ssh_socket_struct *s);
172 void ssh_packet_set_callbacks(ssh_session session, ssh_packet_callbacks callbacks);
173 void ssh_packet_set_default_callbacks(ssh_session session);
174 void ssh_packet_process(ssh_session session, uint8_t type);
175 /* connect.c */
176 socket_t ssh_connect_host(ssh_session session, const char *host,const char
177  *bind_addr, int port, long timeout, long usec);
178 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
179  const char *bind_addr, int port);
180 void ssh_sock_set_nonblocking(socket_t sock);
181 void ssh_sock_set_blocking(socket_t sock);
182 
183 /* in kex.c */
184 extern const char *ssh_kex_nums[];
185 int ssh_send_kex(ssh_session session, int server_kex);
186 void ssh_list_kex(ssh_session session, KEX *kex);
187 int set_kex(ssh_session session);
188 int verify_existing_algo(int algo, const char *name);
189 char **space_tokenize(const char *chain);
190 int ssh_get_kex1(ssh_session session);
191 char *ssh_find_matching(const char *in_d, const char *what_d);
192 
193 
194 /* in base64.c */
195 ssh_buffer base64_to_bin(const char *source);
196 unsigned char *bin_to_base64(const unsigned char *source, int len);
197 
198 /* gzip.c */
199 int compress_buffer(ssh_session session,ssh_buffer buf);
200 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
201 
202 /* crc32.c */
203 uint32_t ssh_crc32(const char *buf, uint32_t len);
204 
205 
206 /* match.c */
207 int match_hostname(const char *host, const char *pattern, unsigned int len);
208 
209 int message_handle(ssh_session session, void *user, uint8_t type, ssh_buffer packet);
210 /* log.c */
211 
212 void ssh_log_common(struct ssh_common_struct *common, int verbosity,
213  const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
214 
215 /* misc.c */
216 #ifdef _WIN32
217 int gettimeofday(struct timeval *__p, void *__t);
218 #endif /* _WIN32 */
219 
220 #ifndef __FUNCTION__
221 #if defined(__SUNPRO_C)
222 #define __FUNCTION__ __func__
223 #endif
224 #endif
225 
226 #define _enter_function(sess) \
227  do {\
228  if((sess)->common.log_verbosity >= SSH_LOG_FUNCTIONS){ \
229  ssh_log((sess),SSH_LOG_FUNCTIONS,"entering function %s line %d in " __FILE__ , __FUNCTION__,__LINE__);\
230  (sess)->common.log_indent++; \
231  } \
232  } while(0)
233 
234 #define _leave_function(sess) \
235  do { \
236  if((sess)->common.log_verbosity >= SSH_LOG_FUNCTIONS){ \
237  (sess)->common.log_indent--; \
238  ssh_log((sess),SSH_LOG_FUNCTIONS,"leaving function %s line %d in " __FILE__ , __FUNCTION__,__LINE__);\
239  }\
240  } while(0)
241 
242 #ifdef DEBUG_CALLTRACE
243 #define enter_function() _enter_function(session)
244 #define leave_function() _leave_function(session)
245 #else
246 #define enter_function() (void)session
247 #define leave_function() (void)session
248 #endif
249 
250 /* options.c */
251 
252 int ssh_options_set_algo(ssh_session session, int algo, const char *list);
253 int ssh_options_apply(ssh_session session);
254 
255 /* server.c */
256 SSH_PACKET_CALLBACK(ssh_packet_kexdh_init);
257 
259 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
260 
262 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
263 
265 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
266 
268 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
269 
271 #define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
272 
273 #ifdef HAVE_LIBGCRYPT
274 /* gcrypt_missing.c */
275 int my_gcry_dec2bn(bignum *bn, const char *data);
276 char *my_gcry_bn2dec(bignum bn);
277 #endif /* !HAVE_LIBGCRYPT */
278 
279 #ifdef __cplusplus
280 }
281 #endif
282 
283 #endif /* _LIBSSH_PRIV_H */
284 /* vim: set ts=2 sw=2 et cindent: */