libssh  0.5.2
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 
137 /* server data */
138 
139 
140 SSH_PACKET_CALLBACK(ssh_packet_disconnect_callback);
141 SSH_PACKET_CALLBACK(ssh_packet_ignore_callback);
142 
143 /* client.c */
144 
145 int ssh_send_banner(ssh_session session, int is_server);
146 SSH_PACKET_CALLBACK(ssh_packet_dh_reply);
147 SSH_PACKET_CALLBACK(ssh_packet_newkeys);
148 SSH_PACKET_CALLBACK(ssh_packet_service_accept);
149 
150 /* config.c */
151 int ssh_config_parse_file(ssh_session session, const char *filename);
152 
153 /* errors.c */
154 void ssh_set_error(void *error, int code, const char *descr, ...) PRINTF_ATTRIBUTE(3, 4);
155 void ssh_set_error_oom(void *);
156 void ssh_set_error_invalid(void *, const char *);
157 
158 /* in crypt.c */
159 uint32_t packet_decrypt_len(ssh_session session,char *crypted);
160 int packet_decrypt(ssh_session session, void *packet,unsigned int len);
161 unsigned char *packet_encrypt(ssh_session session,void *packet,unsigned int len);
162  /* it returns the hmac buffer if exists*/
163 struct ssh_poll_handle_struct;
164 
165 int packet_hmac_verify(ssh_session session,ssh_buffer buffer,unsigned char *mac);
166 
167 struct ssh_socket_struct;
168 
169 int ssh_packet_socket_callback(const void *data, size_t len, void *user);
170 void ssh_packet_register_socket_callback(ssh_session session, struct ssh_socket_struct *s);
171 void ssh_packet_set_callbacks(ssh_session session, ssh_packet_callbacks callbacks);
172 void ssh_packet_set_default_callbacks(ssh_session session);
173 void ssh_packet_process(ssh_session session, uint8_t type);
174 /* connect.c */
175 socket_t ssh_connect_host(ssh_session session, const char *host,const char
176  *bind_addr, int port, long timeout, long usec);
177 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
178  const char *bind_addr, int port);
179 void ssh_sock_set_nonblocking(socket_t sock);
180 void ssh_sock_set_blocking(socket_t sock);
181 
182 /* in kex.c */
183 extern const char *ssh_kex_nums[];
184 int ssh_send_kex(ssh_session session, int server_kex);
185 void ssh_list_kex(ssh_session session, KEX *kex);
186 int set_kex(ssh_session session);
187 int verify_existing_algo(int algo, const char *name);
188 char **space_tokenize(const char *chain);
189 int ssh_get_kex1(ssh_session session);
190 char *ssh_find_matching(const char *in_d, const char *what_d);
191 
192 
193 /* in base64.c */
194 ssh_buffer base64_to_bin(const char *source);
195 unsigned char *bin_to_base64(const unsigned char *source, int len);
196 
197 /* gzip.c */
198 int compress_buffer(ssh_session session,ssh_buffer buf);
199 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
200 
201 /* crc32.c */
202 uint32_t ssh_crc32(const char *buf, uint32_t len);
203 
204 
205 /* match.c */
206 int match_hostname(const char *host, const char *pattern, unsigned int len);
207 
208 int message_handle(ssh_session session, void *user, uint8_t type, ssh_buffer packet);
209 /* log.c */
210 
211 /* misc.c */
212 #ifdef _WIN32
213 int gettimeofday(struct timeval *__p, void *__t);
214 #endif /* _WIN32 */
215 
216 #ifndef __FUNCTION__
217 #if defined(__SUNPRO_C)
218 #define __FUNCTION__ __func__
219 #endif
220 #endif
221 
222 #define _enter_function(sess) \
223  do {\
224  if((sess)->log_verbosity >= SSH_LOG_FUNCTIONS){ \
225  ssh_log((sess),SSH_LOG_FUNCTIONS,"entering function %s line %d in " __FILE__ , __FUNCTION__,__LINE__);\
226  (sess)->log_indent++; \
227  } \
228  } while(0)
229 
230 #define _leave_function(sess) \
231  do { \
232  if((sess)->log_verbosity >= SSH_LOG_FUNCTIONS){ \
233  (sess)->log_indent--; \
234  ssh_log((sess),SSH_LOG_FUNCTIONS,"leaving function %s line %d in " __FILE__ , __FUNCTION__,__LINE__);\
235  }\
236  } while(0)
237 
238 #ifdef DEBUG_CALLTRACE
239 #define enter_function() _enter_function(session)
240 #define leave_function() _leave_function(session)
241 #else
242 #define enter_function() (void)session
243 #define leave_function() (void)session
244 #endif
245 
246 /* options.c */
247 
248 int ssh_options_set_algo(ssh_session session, int algo, const char *list);
249 int ssh_options_apply(ssh_session session);
250 
251 /* server.c */
252 SSH_PACKET_CALLBACK(ssh_packet_kexdh_init);
253 
255 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
256 
258 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
259 
261 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
262 
264 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
265 
267 #define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
268 
269 #ifdef HAVE_LIBGCRYPT
270 /* gcrypt_missing.c */
271 int my_gcry_dec2bn(bignum *bn, const char *data);
272 char *my_gcry_bn2dec(bignum bn);
273 #endif /* !HAVE_LIBGCRYPT */
274 
275 #ifdef __cplusplus
276 }
277 #endif
278 
279 #endif /* _LIBSSH_PRIV_H */
280 /* vim: set ts=2 sw=2 et cindent: */