proxy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.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 <fcntl.h>
  20. #include <time.h>
  21. #define MAXUSERNAME 128
  22. #define _PASSWORD_LEN 256
  23. #define MAXNSERVERS 5
  24. #define UDPBUFSIZE 16384
  25. #define TCPBUFSIZE 8192
  26. #define SRVBUFSIZE (param->srv->bufsize?param->srv->bufsize:((param->service == S_UDPPM)?UDPBUFSIZE:TCPBUFSIZE))
  27. #ifdef _WIN32
  28. #include <winsock2.h>
  29. #include <sys/timeb.h>
  30. #ifndef _WINCE
  31. #include <io.h>
  32. #else
  33. #include <sys/unistd.h>
  34. #endif
  35. #include <process.h>
  36. #define SASIZETYPE int
  37. #define SHUT_RDWR SD_BOTH
  38. #else
  39. #ifndef FD_SETSIZE
  40. #define FD_SETSIZE 4096
  41. #endif
  42. #include <signal.h>
  43. #include <sys/uio.h>
  44. #include <sys/time.h>
  45. #include <unistd.h>
  46. #include <pthread.h>
  47. #include <syslog.h>
  48. #include <errno.h>
  49. #endif
  50. #ifdef __CYGWIN__
  51. #include <windows.h>
  52. #define daemonize() FreeConsole()
  53. #define SLEEPTIME 1000
  54. #undef _WIN32
  55. #elif _WIN32
  56. #ifdef errno
  57. #undef errno
  58. #endif
  59. #define errno WSAGetLastError()
  60. #ifdef EAGAIN
  61. #undef EAGAIN
  62. #endif
  63. #define EAGAIN WSAEWOULDBLOCK
  64. #ifdef EINTR
  65. #undef EINTR
  66. #endif
  67. #ifndef EINPROGRESS
  68. #define EINPROGRESS WSAEWOULDBLOCK
  69. #endif
  70. #define EINTR WSAEWOULDBLOCK
  71. #define SLEEPTIME 1
  72. #define usleep Sleep
  73. #define pthread_self GetCurrentThreadId
  74. #define getpid GetCurrentProcessId
  75. #define pthread_t unsigned
  76. #ifndef _WINCE
  77. #define daemonize() FreeConsole()
  78. #else
  79. #define daemonize()
  80. #endif
  81. #define socket(x, y, z) WSASocket(x, y, z, NULL, 0, 0)
  82. #define accept(x, y, z) WSAAccept(x, y, z, NULL, 0)
  83. #define ftruncate chsize
  84. #else
  85. #include <pthread.h>
  86. #ifndef PTHREAD_STACK_MIN
  87. #define PTHREAD_STACK_MIN 32768
  88. #define sockerror strerror
  89. #endif
  90. void daemonize(void);
  91. #define SLEEPTIME 1000
  92. #ifndef O_BINARY
  93. #define O_BINARY 0
  94. #endif
  95. #endif
  96. #ifndef NOODBC
  97. #ifndef _WIN32
  98. #include <sqltypes.h>
  99. #endif
  100. #include <sql.h>
  101. #include <sqlext.h>
  102. #endif
  103. #ifdef _WIN32
  104. #define strcasecmp stricmp
  105. #define strncasecmp strnicmp
  106. #endif
  107. #ifndef SOCKET_ERROR
  108. #define SOCKET_ERROR -1
  109. #endif
  110. #ifndef isnumber
  111. #define isnumber(n) (n >= '0' && n <= '9')
  112. #endif
  113. #ifndef ishex
  114. #define ishex(n) ((n >= '0' && n <= '9') || (n >= 'a' && n<='f') || (n >= 'A' && n <= 'F'))
  115. #endif
  116. #define isallowed(n) ((n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '*' && n <= '/') || n == '_')
  117. #include "structures.h"
  118. #define MAXRADIUS 5
  119. extern RESOLVFUNC resolvfunc;
  120. extern int wday;
  121. extern time_t basetime;
  122. extern int timetoexit;
  123. extern struct extparam conf;
  124. int sockmap(struct clientparam * param, int timeo);
  125. int splicemap(struct clientparam * param, int timeo);
  126. int socksend(SOCKET sock, unsigned char * buf, int bufsize, int to);
  127. int socksendto(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  128. int sockrecvfrom(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  129. int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec);
  130. int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec);
  131. int sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec);
  132. int sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec);
  133. int sockgetlinebuf(struct clientparam * param, DIRECTION which, unsigned char * buf, int bufsize, int delim, int to);
  134. int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
  135. int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
  136. extern FILE * stdlog;
  137. void logstdout(struct clientparam * param, const unsigned char *s);
  138. void logsyslog(struct clientparam * param, const unsigned char *s);
  139. void lognone(struct clientparam * param, const unsigned char *s);
  140. void logradius(struct clientparam * param, const unsigned char *s);
  141. #ifndef NOSQL
  142. void logsql(struct clientparam * param, const unsigned char *s);
  143. int init_sql(char * s);
  144. void close_sql();
  145. #endif
  146. int doconnect(struct clientparam * param);
  147. int alwaysauth(struct clientparam * param);
  148. int ipauth(struct clientparam * param);
  149. int doauth(struct clientparam * param);
  150. int strongauth(struct clientparam * param);
  151. void trafcountfunc(struct clientparam *param);
  152. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  153. int scanaddr(const unsigned char *s, unsigned long * ip, unsigned long * mask);
  154. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  155. extern struct nserver nservers[MAXNSERVERS];
  156. extern struct nserver authnserver;
  157. unsigned long getip(unsigned char *name);
  158. unsigned long getip46(int family, unsigned char *name, struct sockaddr *sa);
  159. unsigned long myresolver(int, unsigned char *, unsigned char *);
  160. unsigned long fakeresolver (int, unsigned char *, unsigned char*);
  161. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  162. void freeparam(struct clientparam * param);
  163. void clearstat(struct clientparam * param);
  164. void dumpcounters(struct trafcount *tl, int counterd);
  165. int startconnlims (struct clientparam *param);
  166. void stopconnlims (struct clientparam *param);
  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. int 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. extern int havelog;
  198. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  199. struct ace * copyacl (struct ace *ac);
  200. struct auth * copyauth (struct auth *);
  201. void * itfree(void *data, void * retval);
  202. void freeacl(struct ace *ac);
  203. void freeauth(struct auth *);
  204. void freefilter(struct filter *filter);
  205. void freeconf(struct extparam *confp);
  206. struct passwords * copypwl (struct passwords *pwl);
  207. void freepwl(struct passwords *pw);
  208. void copyfilter(struct filter *, struct srvparam *srv);
  209. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param);
  210. FILTER_ACTION handlereqfilters(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  211. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  212. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  213. FILTER_ACTION handlepredatflt(struct clientparam *param);
  214. FILTER_ACTION handledatfltcli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  215. FILTER_ACTION handledatfltsrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  216. void srvinit(struct srvparam * srv, struct clientparam *param);
  217. void srvinit2(struct srvparam * srv, struct clientparam *param);
  218. void srvfree(struct srvparam * srv);
  219. unsigned char * dologname (unsigned char *buf, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t);
  220. int readconfig(FILE * fp);
  221. int connectwithpoll(SOCKET sock, struct sockaddr *sa, SASIZETYPE size);
  222. int myrand(void * entropy, int len);
  223. #ifdef WITH_STD_MALLOC
  224. #define myalloc malloc
  225. #define myfree free
  226. #define myrealloc realloc
  227. #define mystrdup strdup
  228. #else
  229. void *myalloc(size_t size);
  230. void myfree(void *ptr);
  231. void *myrealloc(void *ptr, size_t size);
  232. char * mystrdup(const char *str);
  233. #endif
  234. extern char *copyright;
  235. #define SERVICES 5
  236. void * dnsprchild(struct clientparam * param);
  237. void * pop3pchild(struct clientparam * param);
  238. void * smtppchild(struct clientparam * param);
  239. void * proxychild(struct clientparam * param);
  240. void * sockschild(struct clientparam * param);
  241. void * tcppmchild(struct clientparam * param);
  242. void * icqprchild(struct clientparam * param);
  243. void * msnprchild(struct clientparam * param);
  244. void * udppmchild(struct clientparam * param);
  245. void * adminchild(struct clientparam * param);
  246. void * ftpprchild(struct clientparam * param);
  247. struct datatype;
  248. struct dictionary;
  249. struct node;
  250. struct property;
  251. extern unsigned char tmpbuf[8192];
  252. extern pthread_mutex_t config_mutex;
  253. extern pthread_mutex_t bandlim_mutex;
  254. extern pthread_mutex_t connlim_mutex;
  255. extern pthread_mutex_t hash_mutex;
  256. extern pthread_mutex_t tc_mutex;
  257. extern pthread_mutex_t pwl_mutex;
  258. extern pthread_mutex_t log_mutex;
  259. extern pthread_mutex_t rad_mutex;
  260. extern struct datatype datatypes[64];
  261. extern struct commands commandhandlers[];
  262. #ifdef WITHSPLICE
  263. #define mapsocket(a,b) (a->srv->usesplice?splicemap(a,b):sockmap(a,b))
  264. #else
  265. #define mapsocket(a,b) sockmap(a,b)
  266. #endif
  267. extern struct radserver {
  268. #ifdef NOIPV6
  269. struct sockaddr_in authaddr, logaddr;
  270. #else
  271. struct sockaddr_in6 authaddr, logaddr;
  272. #endif
  273. /*
  274. SOCKET logsock;
  275. */
  276. } radiuslist[MAXRADIUS];
  277. extern char radiussecret[64];
  278. extern int nradservers;
  279. extern struct socketoptions {
  280. int opt;
  281. char * optname;
  282. } sockopts[];
  283. void setopts(SOCKET s, int opts);
  284. #ifdef _WINCE
  285. char * CEToUnicode (const char *str);
  286. int cesystem(const char *str);
  287. int ceparseargs(const char *str);
  288. extern char * ceargv[32];
  289. #define system(S) cesystem(S)
  290. #endif
  291. #define WEBBANNERS 35
  292. #endif