proxy.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2009 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. */
  6. #define COPYRIGHT "(c)3APA3A, Vladimir Dubrovin & 3proxy.ru\n"\
  7. "Documentation and sources: http://3proxy.ru/\n"\
  8. "Please read license agreement in \'copying\' file.\n"\
  9. "You may not use this program without accepting license agreement"
  10. #ifndef _3PROXY_H_
  11. #define _3PROXY_H_
  12. #include "version.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/timeb.h>
  20. #include <fcntl.h>
  21. #include <time.h>
  22. #define MAXUSERNAME 128
  23. #define _PASSWORD_LEN 256
  24. #define MAXNSERVERS 5
  25. #define ALLOW 0
  26. #define DENY 1
  27. #define REDIRECT 2
  28. #define BANDLIM 3
  29. #define NOBANDLIM 4
  30. #define COUNTIN 5
  31. #define NOCOUNTIN 6
  32. #define COUNTOUT 7
  33. #define NOCOUNTOUT 8
  34. #define UDPBUFSIZE 16384
  35. #define TCPBUFSIZE 8192
  36. #ifdef _WIN32
  37. #include <winsock2.h>
  38. #ifndef _WINCE
  39. #include <io.h>
  40. #else
  41. #include <sys/unistd.h>
  42. #endif
  43. #include <process.h>
  44. #define SASIZETYPE int
  45. #define SHUT_RDWR SD_BOTH
  46. #else
  47. #ifndef FD_SETSIZE
  48. #define FD_SETSIZE 4096
  49. #endif
  50. #include <signal.h>
  51. #include <sys/uio.h>
  52. #include <sys/time.h>
  53. #include <unistd.h>
  54. #include <pthread.h>
  55. #include <syslog.h>
  56. #include <errno.h>
  57. #endif
  58. #ifdef __CYGWIN__
  59. #include <windows.h>
  60. #define daemonize() FreeConsole()
  61. #define SLEEPTIME 1000
  62. #undef _WIN32
  63. #elif _WIN32
  64. #ifdef errno
  65. #undef errno
  66. #endif
  67. #define errno WSAGetLastError()
  68. #ifdef EAGAIN
  69. #undef EAGAIN
  70. #endif
  71. #define EAGAIN WSAEWOULDBLOCK
  72. #ifdef EINTR
  73. #undef EINTR
  74. #endif
  75. #define EINTR WSAEWOULDBLOCK
  76. #define SLEEPTIME 1
  77. #define usleep Sleep
  78. #define pthread_self GetCurrentThreadId
  79. #define getpid GetCurrentProcessId
  80. #define pthread_t unsigned
  81. #ifndef _WINCE
  82. #define daemonize() FreeConsole()
  83. #else
  84. #define daemonize()
  85. #endif
  86. #define socket(x, y, z) WSASocket(x, y, z, NULL, 0, 0)
  87. #define accept(x, y, z) WSAAccept(x, y, z, NULL, 0)
  88. #define ftruncate chsize
  89. #else
  90. #include <pthread.h>
  91. #ifndef PTHREAD_STACK_MIN
  92. #define PTHREAD_STACK_MIN 32768
  93. #define sockerror strerror
  94. #endif
  95. #define daemonize() {if(fork())exit(0); else setsid();}
  96. #define SLEEPTIME 1000
  97. #ifndef O_BINARY
  98. #define O_BINARY 0
  99. #endif
  100. #endif
  101. #ifndef NOODBC
  102. #ifndef _WIN32
  103. #include <sqltypes.h>
  104. #endif
  105. #include <sql.h>
  106. #include <sqlext.h>
  107. #endif
  108. #ifdef _WIN32
  109. #define strcasecmp stricmp
  110. #define strncasecmp strnicmp
  111. #endif
  112. #ifndef SOCKET_ERROR
  113. #define SOCKET_ERROR -1
  114. #endif
  115. #ifndef isnumber
  116. #define isnumber(n) (n >= '0' && n <= '9')
  117. #endif
  118. #ifndef ishex
  119. #define ishex(n) ((n >= '0' && n <= '9') || (n >= 'a' && n<='f') || (n >= 'A' && n <= 'F'))
  120. #endif
  121. #define isallowed(n) ((n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '*' && n <= '/') || n == '_')
  122. #include "structures.h"
  123. extern RESOLVFUNC resolvfunc;
  124. extern int wday;
  125. extern time_t basetime;
  126. extern int timetoexit;
  127. extern struct extparam conf;
  128. int sockmap(struct clientparam * param, int timeo);
  129. int socksend(SOCKET sock, unsigned char * buf, int bufsize, int to);
  130. int socksendto(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  131. int sockrecvfrom(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  132. int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec);
  133. int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec);
  134. int sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec);
  135. int sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec);
  136. int sockgetlinebuf(struct clientparam * param, DIRECTION which, unsigned char * buf, int bufsize, int delim, int to);
  137. int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
  138. int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
  139. extern FILE * stdlog;
  140. void logstdout(struct clientparam * param, const unsigned char *s);
  141. void logsyslog(struct clientparam * param, const unsigned char *s);
  142. void lognone(struct clientparam * param, const unsigned char *s);
  143. #ifndef NOSQL
  144. void logsql(struct clientparam * param, const unsigned char *s);
  145. int init_sql(char * s);
  146. void close_sql();
  147. #endif
  148. int doconnect(struct clientparam * param);
  149. int alwaysauth(struct clientparam * param);
  150. int ipauth(struct clientparam * param);
  151. int doauth(struct clientparam * param);
  152. int strongauth(struct clientparam * param);
  153. void trafcountfunc(struct clientparam *param);
  154. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  155. int scanaddr(const unsigned char *s, unsigned long * ip, unsigned long * mask);
  156. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  157. extern struct nserver nservers[MAXNSERVERS];
  158. struct nserver authnserver;
  159. unsigned long getip(unsigned char *name);
  160. unsigned long getip46(int family, unsigned char *name, struct sockaddr *sa);
  161. unsigned long myresolver(int, unsigned char *, unsigned char *);
  162. unsigned long fakeresolver (int, unsigned char *, unsigned char*);
  163. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  164. void freeparam(struct clientparam * param);
  165. void clearstat(struct clientparam * param);
  166. void dumpcounters(struct trafcount *tl, int counterd);
  167. extern struct auth authfuncs[];
  168. int reload (void);
  169. extern int paused;
  170. extern int demon;
  171. unsigned char * mycrypt(const unsigned char *key, const unsigned char *salt, unsigned char *buf);
  172. unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int tohex);
  173. int de64 (const unsigned char *in, unsigned char *out, int maxlen);
  174. unsigned char* en64 (const unsigned char *in, unsigned char *out, int inlen);
  175. void tohex(unsigned char *in, unsigned char *out, int len);
  176. void fromhex(unsigned char *in, unsigned char *out, int len);
  177. int ftplogin(struct clientparam *param, char *buf, int *inbuf);
  178. int ftpcd(struct clientparam *param, unsigned char* path, char *buf, int *inbuf);
  179. int ftpsyst(struct clientparam *param, unsigned char *buf, unsigned len);
  180. int ftppwd(struct clientparam *param, unsigned char *buf, unsigned len);
  181. int ftptype(struct clientparam *param, unsigned char* f_type);
  182. int ftpres(struct clientparam *param, unsigned char * buf, int len);
  183. SOCKET ftpcommand(struct clientparam *param, unsigned char * command, unsigned char *arg);
  184. int text2unicode(const char * text, char * buf, int buflen);
  185. void unicode2text(const char *unicode, char * buf, int len);
  186. void genchallenge(struct clientparam *param, char * challenge, char *buf);
  187. void mschap(const unsigned char *win_password,
  188. const unsigned char *challenge, unsigned char *response);
  189. struct hashtable;
  190. void hashadd(struct hashtable *ht, const unsigned char* name, unsigned char* value, time_t expires);
  191. void parsehost(int family, unsigned char *host, struct sockaddr *sa);
  192. int parsehostname(char *hostname, struct clientparam *param, unsigned short port);
  193. int parseusername(char *username, struct clientparam *param, int extpasswd);
  194. int parseconnusername(char *username, struct clientparam *param, int extpasswd, unsigned short port);
  195. int ACLmatches(struct ace* acentry, struct clientparam * param);
  196. int checkACL(struct clientparam * param);
  197. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  198. struct ace * copyacl (struct ace *ac);
  199. struct auth * copyauth (struct auth *);
  200. void * itfree(void *data, void * retval);
  201. void freeacl(struct ace *ac);
  202. void freeauth(struct auth *);
  203. void freefilter(struct filter *filter);
  204. void freeconf(struct extparam *confp);
  205. struct passwords * copypwl (struct passwords *pwl);
  206. void freepwl(struct passwords *pw);
  207. void copyfilter(struct filter *, struct srvparam *srv);
  208. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param);
  209. FILTER_ACTION handlereqfilters(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  210. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  211. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  212. FILTER_ACTION handlepredatflt(struct clientparam *param);
  213. FILTER_ACTION handledatfltcli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  214. FILTER_ACTION handledatfltsrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  215. void srvinit(struct srvparam * srv, struct clientparam *param);
  216. void srvinit2(struct srvparam * srv, struct clientparam *param);
  217. void srvfree(struct srvparam * srv);
  218. unsigned char * dologname (unsigned char *buf, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t);
  219. int readconfig(FILE * fp);
  220. int myrand(void * entropy, int len);
  221. #ifdef WITH_STD_MALLOC
  222. #define myalloc malloc
  223. #define myfree free
  224. #define myrealloc realloc
  225. #define mystrdup strdup
  226. #else
  227. void *myalloc(size_t size);
  228. void myfree(void *ptr);
  229. void *myrealloc(void *ptr, size_t size);
  230. char * mystrdup(const char *str);
  231. #endif
  232. extern char *copyright;
  233. #define SERVICES 5
  234. void * dnsprchild(struct clientparam * param);
  235. void * pop3pchild(struct clientparam * param);
  236. void * smtppchild(struct clientparam * param);
  237. void * proxychild(struct clientparam * param);
  238. void * sockschild(struct clientparam * param);
  239. void * tcppmchild(struct clientparam * param);
  240. void * icqprchild(struct clientparam * param);
  241. void * msnprchild(struct clientparam * param);
  242. void * udppmchild(struct clientparam * param);
  243. void * adminchild(struct clientparam * param);
  244. void * ftpprchild(struct clientparam * param);
  245. struct datatype;
  246. struct dictionary;
  247. struct node;
  248. struct property;
  249. extern unsigned char tmpbuf[8192];
  250. extern pthread_mutex_t config_mutex;
  251. extern pthread_mutex_t bandlim_mutex;
  252. extern pthread_mutex_t hash_mutex;
  253. extern pthread_mutex_t tc_mutex;
  254. extern pthread_mutex_t pwl_mutex;
  255. extern pthread_mutex_t log_mutex;
  256. extern struct datatype datatypes[64];
  257. extern struct commands commandhandlers[];
  258. #ifdef _WINCE
  259. char * CEToUnicode (const char *str);
  260. int cesystem(const char *str);
  261. int ceparseargs(const char *str);
  262. extern char * ceargv[32];
  263. #define system(S) cesystem(S)
  264. #endif
  265. #define WEBBANNERS 35
  266. #endif