pop3p.c 2.7 KB

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