smtpp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. $Id: smtpp.c,v 1.13 2011-06-10 20:48:46 vlad Exp $
  6. */
  7. #include "proxy.h"
  8. #define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
  9. char ehlo[] = "250-Proxy\r\n"
  10. "250-AUTH PLAIN LOGIN\r\n"
  11. "250-8BITMIME\r\n"
  12. "250 DSN\r\n";
  13. int readreply (struct clientparam* param) {
  14. unsigned char * buf;
  15. int res, i, bufsize = 640;
  16. if(!(buf = myalloc(bufsize))) return 0;
  17. do {
  18. i = sockgetlinebuf(param, SERVER, buf, bufsize-1, '\n', conf.timeouts[STRING_L]);
  19. if(i < 1) break;
  20. #ifndef WITHMAIN
  21. res = handlehdrfilterssrv(param, &buf, &bufsize, 0, &i);
  22. if(res != PASS) {
  23. myfree(buf);
  24. return -1;
  25. }
  26. #endif
  27. socksend(param->clisock, buf, i, conf.timeouts[STRING_S]);
  28. } while (i > 3 && buf[3] == '-');
  29. if(i < 3) {
  30. myfree(buf);
  31. return 0;
  32. }
  33. buf[i] = 0;
  34. res = atoi(buf);
  35. myfree(buf);
  36. return res;
  37. }
  38. int readcommand (struct clientparam* param) {
  39. unsigned char * buf;
  40. int res, i, bufsize = 320;
  41. int ret = 1;
  42. if(!(buf = myalloc(bufsize))) return 0;
  43. i = sockgetlinebuf(param, CLIENT, buf, bufsize-1, '\n', conf.timeouts[STRING_L]);
  44. if(i < 4) return 0;
  45. #ifndef WITHMAIN
  46. if(!strncasecmp(buf, "MAIL", 4) || !strncasecmp(buf, "RCPT", 4) || !strncasecmp(buf, "STARTTLS", 8) || !strncasecmp(buf, "TURN", 4)){
  47. res = handlehdrfilterscli(param, &buf, &bufsize, 0, &i);
  48. if(res != PASS) {
  49. myfree(buf);
  50. if(res == HANDLED) return 2;
  51. return -1;
  52. }
  53. }
  54. #endif
  55. socksend(param->remsock, buf, i, conf.timeouts[STRING_S]);
  56. myfree(buf);
  57. if(!strncasecmp(buf, "STARTTLS", 8) || !strncasecmp(buf, "TURN", 4)){
  58. ret = 22;
  59. }
  60. return ret;
  61. }
  62. int readdata (struct clientparam* param) {
  63. unsigned char * buf;
  64. int res, i, bufsize = 4096;
  65. if(!(buf = myalloc(bufsize))) return 0;
  66. while ((i = sockgetlinebuf(param, CLIENT, buf, bufsize-1, '\n', conf.timeouts[STRING_L])) > 0 && !(i==3 && buf[0] == '.')){
  67. #ifndef WITHMAIN
  68. res = handledatfltcli(param, &buf, &bufsize, 0, &i);
  69. if(res != PASS) {
  70. myfree(buf);
  71. if(res == HANDLED) return 1;
  72. return -1;
  73. }
  74. #endif
  75. socksendto(param->remsock, &param->sins, buf, i, conf.timeouts[STRING_S]);
  76. }
  77. if(i < 1) {
  78. myfree(buf);
  79. return 0;
  80. }
  81. socksend(param->remsock, buf, i, conf.timeouts[STRING_S]);
  82. myfree(buf);
  83. return 1;
  84. }
  85. void * smtppchild(struct clientparam* param) {
  86. int i=0, res;
  87. unsigned char buf[320];
  88. unsigned char username[256];
  89. unsigned long ul;
  90. char * command = NULL;
  91. int login = 0;
  92. if(socksend(param->clisock, (unsigned char *)"220 Proxy\r\n", 11, conf.timeouts[STRING_S])!=11) {RETURN (611);}
  93. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  94. while(i > 4 && (strncasecmp((char *)buf, "AUTH PLAIN", 10) || !(login = 2)) && (strncasecmp((char *)buf, "AUTH LOGIN", 10) || !(login = 1))){
  95. if(!strncasecmp((char *)buf, "QUIT", 4)){
  96. socksend(param->clisock, (unsigned char *)"221 Proxy\r\n", 11,conf.timeouts[STRING_S]);
  97. RETURN(0);
  98. }
  99. else if(!strncasecmp((char *)buf, "HELO ", 5)){
  100. socksend(param->clisock, (unsigned char *)"250 Proxy\r\n", 11,conf.timeouts[STRING_S]);
  101. }
  102. else if(!strncasecmp((char *)buf, "EHLO ", 5)){
  103. socksend(param->clisock, (unsigned char *)ehlo, sizeof(ehlo) - 1,conf.timeouts[STRING_S]);
  104. }
  105. else if(!param->hostname) socksend(param->clisock, (unsigned char *)"571 need AUTH first\r\n", 22, conf.timeouts[STRING_S]);
  106. else {
  107. login = -1;
  108. buf[i] = 0;
  109. command = mystrdup(buf);
  110. break;
  111. }
  112. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  113. }
  114. if(!login) {RETURN(662);}
  115. if(login == 1){
  116. socksend(param->clisock, "334 VXNlcm5hbWU6\r\n", 18,conf.timeouts[STRING_S]);
  117. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  118. if(i < 3) {RETURN(663);}
  119. buf[i-2] = 0;
  120. i = de64(buf,username,255);
  121. if(i < 1) {RETURN(664);}
  122. username[i] = 0;
  123. parseconnusername(username, param, 0, 25);
  124. socksend(param->clisock, "334 UGFzc3dvcmQ6\r\n", 18,conf.timeouts[STRING_S]);
  125. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  126. if(i < 2) {RETURN(665);}
  127. buf[i-2] = 0;
  128. i = de64(buf,username,255);
  129. if(i < 0) {RETURN(666);}
  130. username[i] = 0;
  131. if(param->extpassword) myfree(param->extpassword);
  132. param->extpassword = mystrdup(username);
  133. }
  134. else if(login == 2){
  135. if(i > 13) {
  136. buf[i-2] = 0;
  137. i = de64(buf+11,username,255);
  138. }
  139. else {
  140. socksend(param->clisock, "334\r\n", 5,conf.timeouts[STRING_S]);
  141. i = sockgetlinebuf(param, CLIENT, buf, sizeof(buf) - 10, '\n', conf.timeouts[STRING_S]);
  142. if(i < 3) {RETURN(667);}
  143. buf[i-2] = 0;
  144. i = de64(buf,username,255);
  145. }
  146. if(i < 3 || *username) {RETURN(668);}
  147. username[i] = 0;
  148. parseconnusername(username+1, param, 0, 25);
  149. res = (int)strlen(username+1) + 2;
  150. if(res < i){
  151. if(param->extpassword) myfree(param->extpassword);
  152. param->extpassword = mystrdup(username + res);
  153. }
  154. }
  155. #ifndef WITHMAIN
  156. {
  157. int action, reqbufsize, reqsize;
  158. reqbufsize = reqsize = (int)strlen(param->hostname) + 1;
  159. action = handlereqfilters(param, &param->hostname, &reqbufsize, 0, &reqsize);
  160. if(action == HANDLED){
  161. RETURN(0);
  162. }
  163. if(action != PASS) RETURN(617);
  164. }
  165. #endif
  166. param->operation = CONNECT;
  167. res = (*param->srv->authfunc)(param);
  168. if(res) {RETURN(res);}
  169. do {
  170. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  171. } while (i > 3 && buf[3] == '-');
  172. if( i < 3 ) {RETURN(671);}
  173. buf[i] = 0;
  174. if(strncasecmp((char *)buf, "220", 3)||!strncasecmp((char *)buf+4, "PROXY", 5)){RETURN(672);}
  175. ul = param->extip;
  176. i = sprintf(buf, "EHLO [%lu.%lu.%lu.%lu]\r\n", ((ul&0xFF000000)>>24), ((ul&0x00FF0000)>>16), ((ul&0x0000FF00)>>8), ((ul&0x000000FF)));
  177. if(socksend(param->remsock, buf, i, conf.timeouts[STRING_S])!= i) {RETURN(673);}
  178. param->statscli+=i;
  179. param->nwrites++;
  180. login = 0;
  181. do {
  182. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  183. if(i < 1) break;
  184. buf[i] = 0;
  185. if(strstr((char *)buf, "LOGIN")) login |= 1;
  186. if(strstr((char *)buf, "PLAIN")) login |= 2;
  187. } while (i > 3 && buf[3] == '-');
  188. if(i<3) {RETURN(672);}
  189. if(!command || (param->extusername && param->extpassword)){
  190. if(!param->extusername || !*param->extusername || !param->extpassword || !*param->extpassword || !login){
  191. socksend(param->clisock, "235 auth required\r\n", 19,conf.timeouts[STRING_S]);
  192. }
  193. if ((login & 1)) {
  194. socksend(param->remsock, "AUTH LOGIN\r\n", 12, conf.timeouts[STRING_S]);
  195. param->statscli+=12;
  196. param->nwrites++;
  197. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  198. if(i<4 || strncasecmp((char *)buf, "334", 3)) {RETURN(680);}
  199. en64(param->extusername, buf, (int)strlen(param->extusername));
  200. socksend(param->remsock, buf, (int)strlen(buf), conf.timeouts[STRING_S]);
  201. socksend(param->remsock, "\r\n", 2, conf.timeouts[STRING_S]);
  202. param->statscli+=(i+2);
  203. param->nwrites+=2;
  204. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  205. if(i<4 || strncasecmp((char *)buf, "334", 3)) {RETURN(681);}
  206. en64(param->extpassword, buf, (int)strlen(param->extpassword));
  207. socksend(param->remsock, buf, (int)strlen(buf), conf.timeouts[STRING_S]);
  208. socksend(param->remsock, "\r\n", 2, conf.timeouts[STRING_S]);
  209. param->statscli+=(i+2);
  210. param->nwrites+=2;
  211. }
  212. else if((login & 2)){
  213. socksend(param->remsock, "AUTH PLAIN\r\n", 12, conf.timeouts[STRING_S]);
  214. param->statscli+=12;
  215. param->nwrites++;
  216. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  217. if(i<4 || strncasecmp((char *)buf, "334", 3)) {RETURN(682);}
  218. *username = 0;
  219. i = (int)strlen(param->extusername) + 1;
  220. memcpy(username+1, param->extusername, i);
  221. i++;
  222. res = (int)strlen(param->extpassword);
  223. memcpy(username + i, param->extpassword, res);
  224. i+=res;
  225. en64(username, buf, i);
  226. i = (int)strlen(buf);
  227. socksend(param->remsock, buf, i, conf.timeouts[STRING_S]);
  228. socksend(param->remsock, "\r\n", 2, conf.timeouts[STRING_S]);
  229. param->statscli+=(i+2);
  230. param->nwrites+=2;
  231. }
  232. if(command) {
  233. i = sockgetlinebuf(param, SERVER, buf, sizeof(buf) - 1, '\n', conf.timeouts[STRING_L]);
  234. }
  235. res = 1;
  236. }
  237. if(command) {
  238. res = 1;
  239. #ifndef WITHMAIN
  240. if(!strncasecmp(command, "MAIL", 4) || !strncasecmp(command, "RCPT", 4) || !strncasecmp(command, "STARTTLS", 8) || !strncasecmp(command, "TURN", 4)){
  241. res = (int)strlen(command);
  242. command[res] = 0;
  243. res = handlehdrfilterscli(param, &command, &res, 0, &res);
  244. if(res != PASS) {
  245. if(res == HANDLED) res = 2;
  246. else RETURN(677);
  247. }
  248. else if(!strncasecmp(command, "STARTTLS", 8) || !strncasecmp(command, "TURN", 4))
  249. res = 22;
  250. }
  251. #endif
  252. i = (int)strlen(command);
  253. if(res != 2) socksend(param->remsock, command, i, conf.timeouts[STRING_S]);
  254. }
  255. #ifndef WITHMAIN
  256. if(param->nhdrfilterscli || param->nhdrfilterssrv || param->ndatfilterscli || param->ndatfilterssrv){
  257. do {
  258. if(res == 22) RETURN (sockmap(param, 180));
  259. if(res != 2 && (res = readreply(param)) <= 0) break;
  260. if(res == 221) RETURN(0);
  261. if(res == 354) res = readdata(param);
  262. else res = readcommand(param);
  263. } while(res > 0);
  264. if(res == 22)
  265. if(res >= 0) RETURN(0);
  266. RETURN(676);
  267. }
  268. else
  269. #endif
  270. RETURN (sockmap(param, 180));
  271. CLEANRET:
  272. if(param->hostname&&param->extusername) {
  273. sprintf((char *)buf, "%.64s@%.128s%c%hu", param->extusername, param->hostname, (ntohs(param->sins.sin_port)==25)?0:':', ntohs(param->sins.sin_port));
  274. (*param->srv->logfunc)(param, buf);
  275. }
  276. else (*param->srv->logfunc)(param, NULL);
  277. if(param->clisock != INVALID_SOCKET) {
  278. if ((param->res > 0 && param->res < 100) || (param->res > 661 && param->res <700)) socksend(param->clisock, (unsigned char *)"571 \r\n", 6,conf.timeouts[STRING_S]);
  279. }
  280. if(command) myfree(command);
  281. freeparam(param);
  282. return (NULL);
  283. }
  284. #ifdef WITHMAIN
  285. struct proxydef childdef = {
  286. smtppchild,
  287. 25,
  288. 0,
  289. S_SMTPP,
  290. " -hdefault_host[:port] - use this host and port as default if no host specified\n"
  291. };
  292. #include "proxymain.c"
  293. #endif