libssh 0.4.8

include/libssh/priv.h

00001 /*
00002  * This file is part of the SSH Library
00003  *
00004  * Copyright (c) 2003-2009 by Aris Adamantiadis
00005  *
00006  * The SSH Library is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * The SSH Library is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with the SSH Library; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019  * MA 02111-1307, USA.
00020  */
00021 
00022 /*
00023  * priv.h file
00024  * This include file contains everything you shouldn't deal with in
00025  * user programs. Consider that anything in this file might change
00026  * without notice; libssh.h file will keep backward compatibility
00027  * on binary & source
00028  */
00029 
00030 #ifndef _LIBSSH_PRIV_H
00031 #define _LIBSSH_PRIV_H
00032 
00033 #include "config.h"
00034 
00035 #ifdef _WIN32
00036 
00037 /* Imitate define of inttypes.h */
00038 # ifndef PRIdS
00039 #  define PRIdS "Id"
00040 # endif
00041 
00042 # ifdef _MSC_VER
00043 #  include <stdio.h>
00044 
00045 /* On Microsoft compilers define inline to __inline on all others use inline */
00046 #  undef inline
00047 #  define inline __inline
00048 
00049 #  undef strdup
00050 #  define strdup _strdup
00051 
00052 #  define strcasecmp _stricmp
00053 #  define strncasecmp _strnicmp
00054 #  define strtoull _strtoui64
00055 #  define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
00056 
00057 #  define usleep(X) Sleep(((X)+1000)/1000)
00058 
00059 #  undef strtok_r
00060 #  define strtok_r strtok_s
00061 
00062 #  if defined(HAVE__SNPRINTF_S)
00063 #   undef snprintf
00064 #   define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
00065 #  else /* HAVE__SNPRINTF_S */
00066 #   if defined(HAVE__SNPRINTF)
00067 #     undef snprintf
00068 #     define snprintf _snprintf
00069 #   else /* HAVE__SNPRINTF */
00070 #    if !defined(HAVE_SNPRINTF)
00071 #     error "no snprintf compatible function found"
00072 #    endif /* HAVE_SNPRINTF */
00073 #   endif /* HAVE__SNPRINTF */
00074 #  endif /* HAVE__SNPRINTF_S */
00075 
00076 #  if defined(HAVE__VSNPRINTF_S)
00077 #   undef vsnprintf
00078 #   define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
00079 #  else /* HAVE__VSNPRINTF_S */
00080 #   if defined(HAVE__VSNPRINTF)
00081 #    undef vsnprintf
00082 #    define vsnprintf _vsnprintf
00083 #   else
00084 #    if !defined(HAVE_VSNPRINTF)
00085 #     error "No vsnprintf compatible function found"
00086 #    endif /* HAVE_VSNPRINTF */
00087 #   endif /* HAVE__VSNPRINTF */
00088 #  endif /* HAVE__VSNPRINTF_S */
00089 
00090 #  ifndef HAVE_STRNCPY
00091 #  define strncpy(d, s, n) strncpy_s((d), (n), (s), _TRUNCATE)
00092 #  endif
00093 # endif /* _MSC_VER */
00094 
00095 #else /* _WIN32 */
00096 
00097 #include <unistd.h>
00098 #define PRIdS "zd"
00099 
00100 #endif /* _WIN32 */
00101 
00102 #include "libssh/libssh.h"
00103 #include "libssh/callbacks.h"
00104 #include "libssh/crypto.h"
00105 /* some constants */
00106 #define MAX_PACKET_LEN 262144
00107 #define ERROR_BUFFERLEN 1024
00108 #define CLIENTBANNER1 "SSH-1.5-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
00109 #define CLIENTBANNER2 "SSH-2.0-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
00110 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
00111 /* some types for public keys */
00112 enum public_key_types_e{
00113   TYPE_DSS=1,
00114   TYPE_RSA,
00115   TYPE_RSA1
00116 };
00117 
00118 #ifdef __cplusplus
00119 extern "C" {
00120 #endif
00121 
00122 
00123 #ifdef HAVE_SYS_TIME_H
00124 #include <sys/time.h>
00125 #endif
00126 
00127 typedef struct kex_struct {
00128   unsigned char cookie[16];
00129   char **methods;
00130 } KEX;
00131 
00132 struct error_struct {
00133 /* error handling */
00134     unsigned int error_code;
00135     char error_buffer[ERROR_BUFFERLEN];
00136 };
00137 
00138 /* TODO: remove that include */
00139 #include "libssh/wrapper.h"
00140 
00141 struct ssh_keys_struct {
00142   const char *privatekey;
00143   const char *publickey;
00144 };
00145 
00146 struct ssh_message_struct;
00147 
00148 
00149 /* server data */
00150 
00151 struct ssh_bind_struct {
00152   struct error_struct error;
00153 
00154   ssh_callbacks callbacks; /* Callbacks to user functions */
00155 
00156   /* options */
00157   char *wanted_methods[10];
00158   char *banner;
00159   char *dsakey;
00160   char *rsakey;
00161   char *bindaddr;
00162   socket_t bindfd;
00163   unsigned int bindport;
00164   unsigned int log_verbosity;
00165 
00166   int blocking;
00167   int toaccept;
00168 };
00169 
00170 
00171 /* client.c */
00172 
00173 int ssh_send_banner(ssh_session session, int is_server);
00174 char *ssh_get_banner(ssh_session session);
00175 
00176 /* config.c */
00177 int ssh_config_parse_file(ssh_session session, const char *filename);
00178 
00179 /* errors.c */
00180 void ssh_set_error(void *error, int code, const char *descr, ...) PRINTF_ATTRIBUTE(3, 4);
00181 void ssh_set_error_oom(void *);
00182 void ssh_set_error_invalid(void *, const char *);
00183 
00184 /* in crypt.c */
00185 uint32_t packet_decrypt_len(ssh_session session,char *crypted);
00186 int packet_decrypt(ssh_session session, void *packet,unsigned int len);
00187 unsigned char *packet_encrypt(ssh_session session,void *packet,unsigned int len);
00188  /* it returns the hmac buffer if exists*/
00189 int packet_hmac_verify(ssh_session session,ssh_buffer buffer,unsigned char *mac);
00190 
00191 /* connect.c */
00192 int ssh_regex_init(void);
00193 void ssh_regex_finalize(void);
00194 ssh_session ssh_session_new(void);
00195 socket_t ssh_connect_host(ssh_session session, const char *host,const char
00196         *bind_addr, int port, long timeout, long usec);
00197 
00198 /* in kex.c */
00199 extern const char *ssh_kex_nums[];
00200 int ssh_send_kex(ssh_session session, int server_kex);
00201 void ssh_list_kex(ssh_session session, KEX *kex);
00202 int set_kex(ssh_session session);
00203 int ssh_get_kex(ssh_session session, int server_kex);
00204 int verify_existing_algo(int algo, const char *name);
00205 char **space_tokenize(const char *chain);
00206 int ssh_get_kex1(ssh_session session);
00207 char *ssh_find_matching(const char *in_d, const char *what_d);
00208 
00209 /* in base64.c */
00210 ssh_buffer base64_to_bin(const char *source);
00211 unsigned char *bin_to_base64(const unsigned char *source, int len);
00212 
00213 /* gzip.c */
00214 int compress_buffer(ssh_session session,ssh_buffer buf);
00215 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
00216 
00217 /* crc32.c */
00218 uint32_t ssh_crc32(const char *buf, uint32_t len);
00219 
00220 /* auth1.c */
00221 int ssh_userauth1_none(ssh_session session, const char *username);
00222 int ssh_userauth1_offer_pubkey(ssh_session session, const char *username,
00223         int type, ssh_string pubkey);
00224 int ssh_userauth1_password(ssh_session session, const char *username,
00225         const char *password);
00226 
00227 /* channels1.c */
00228 int channel_open_session1(ssh_channel channel);
00229 int channel_request_pty_size1(ssh_channel channel, const char *terminal,
00230     int cols, int rows);
00231 int channel_change_pty_size1(ssh_channel channel, int cols, int rows);
00232 int channel_request_shell1(ssh_channel channel);
00233 int channel_request_exec1(ssh_channel channel, const char *cmd);
00234 int channel_handle1(ssh_session session, int type);
00235 int channel_write1(ssh_channel channel, const void *data, int len);
00236 
00237 /* match.c */
00238 int match_hostname(const char *host, const char *pattern, unsigned int len);
00239 
00240 /* log.c */
00241 
00242 /* misc.c */
00243 #ifdef _WIN32
00244 int gettimeofday(struct timeval *__p, void *__t);
00245 #endif /* _WIN32 */
00246 
00247 #ifndef __FUNCTION__
00248 #if defined(__SUNPRO_C)
00249 #define __FUNCTION__ __func__
00250 #endif
00251 #endif
00252 
00253 #define _enter_function(sess) \
00254   do {\
00255     if((sess)->log_verbosity >= SSH_LOG_FUNCTIONS){ \
00256       ssh_log((sess),SSH_LOG_FUNCTIONS,"entering function %s line %d in " __FILE__ , __FUNCTION__,__LINE__);\
00257       (sess)->log_indent++; \
00258     } \
00259   } while(0)
00260 
00261 #define _leave_function(sess) \
00262   do { \
00263     if((sess)->log_verbosity >= SSH_LOG_FUNCTIONS){ \
00264       (sess)->log_indent--; \
00265       ssh_log((sess),SSH_LOG_FUNCTIONS,"leaving function %s line %d in " __FILE__ , __FUNCTION__,__LINE__);\
00266     }\
00267   } while(0)
00268 
00269 #ifdef DEBUG_CALLTRACE
00270 #define enter_function() _enter_function(session)
00271 #define leave_function() _leave_function(session)
00272 #else
00273 #define enter_function() (void)session
00274 #define leave_function() (void)session
00275 #endif
00276 
00277 /* options.c  */
00278 
00279 int ssh_options_set_algo(ssh_session session, int algo, const char *list);
00280 int ssh_options_apply(ssh_session session);
00281 
00283 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
00284 
00286 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
00287 
00289 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
00290 
00292 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
00293 
00295 #define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
00296 
00297 #ifdef HAVE_LIBGCRYPT
00298 /* gcrypt_missing.c */
00299 int my_gcry_dec2bn(bignum *bn, const char *data);
00300 char *my_gcry_bn2dec(bignum bn);
00301 #endif /* !HAVE_LIBGCRYPT */
00302 
00303 #ifdef __cplusplus
00304 }
00305 #endif
00306 
00307 #endif /* _LIBSSH_PRIV_H */
00308 /* vim: set ts=2 sw=2 et cindent: */