pop3p.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. */
  6. #include "proxy.h"
  7. #define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
  8. void * pop3pchild(struct clientparam* param) {
  9. int i=0, res;
  10. unsigned char buf[320];
  11. unsigned char *se;
  12. if(socksend(param->clisock, (unsigned char *)"+OK Proxy\r\n", 11, conf.timeouts[STRING_S])!=11) {RETURN (611);}
  13. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  14. while(i > 4 && strncasecmp((char *)buf, "USER", 4)){
  15. if(!strncasecmp((char *)buf, "QUIT", 4)){
  16. socksend(param->clisock, (unsigned char *)"+OK\r\n", 5,conf.timeouts[STRING_S]);
  17. RETURN(0);
  18. }
  19. socksend(param->clisock, (unsigned char *)"-ERR need USER first\r\n", 22, conf.timeouts[STRING_S]);
  20. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  21. }
  22. if(i<6) {RETURN(612);}
  23. buf[i] = 0;
  24. if ((se=(unsigned char *)strchr((char *)buf, '\r'))) *se = 0;
  25. if (strncasecmp((char *)buf, "USER ", 5)){RETURN (614);}
  26. if(parseconnusername((char *)buf +5, param, 0, 110)){RETURN(615);}
  27. param->operation = CONNECT;
  28. res = (*param->srv->authfunc)(param);
  29. if(res) {RETURN(res);}
  30. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  31. if( i < 3 ) {RETURN(621);}
  32. buf[i] = 0;
  33. if(strncasecmp((char *)buf, "+OK", 3)||!strncasecmp((char *)buf+4, "PROXY", 5)){RETURN(622);}
  34. if( socksend(param->remsock, (unsigned char *)"USER ", 5, conf.timeouts[STRING_S])!= 5 ||
  35. socksend(param->remsock, param->extusername, (int)strlen((char *)param->extusername), conf.timeouts[STRING_S]) <= 0 ||
  36. socksend(param->remsock, (unsigned char *)"\r\n", 2, conf.timeouts[STRING_S])!=2)
  37. {RETURN(623);}
  38. param->statscli64 += (uint64_t)(strlen((char *)param->extusername) + 7);
  39. param->nwrites++;
  40. RETURN (sockmap(param, 180));
  41. CLEANRET:
  42. if(param->hostname&&param->extusername) {
  43. sprintf((char *)buf, "%.128s@%.128s%c%hu", param->extusername, param->hostname, (*SAPORT(&param->sinsr)==110)?0:':', ntohs(*SAPORT(&param->sinsr)));
  44. (*param->srv->logfunc)(param, buf);
  45. }
  46. else (*param->srv->logfunc)(param, NULL);
  47. if(param->clisock != INVALID_SOCKET) {
  48. if ((param->res > 0 && param->res < 100) || (param->res > 611 && param->res <700)) socksend(param->clisock, (unsigned char *)"-ERR\r\n", 6,conf.timeouts[STRING_S]);
  49. }
  50. freeparam(param);
  51. return (NULL);
  52. }
  53. #ifdef WITHMAIN
  54. struct proxydef childdef = {
  55. pop3pchild,
  56. 110,
  57. 0,
  58. S_POP3P,
  59. " -hdefault_host[:port] - use this host and port as default if no host specified\n"
  60. };
  61. #include "proxymain.c"
  62. #endif