PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/src/dcc.c

https://gitlab.com/Exim/exim
C | 519 lines | 374 code | 42 blank | 103 comment | 91 complexity | 612e351b304fc3714b3e8dc2b6705422 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*************************************************
  2. * Exim - an Internet mail transport agent *
  3. *************************************************/
  4. /* Copyright (c) Wolfgang Breyha 2005-2013
  5. * Vienna University Computer Center
  6. * wbreyha@gmx.net
  7. * See the file NOTICE for conditions of use and distribution.
  8. */
  9. /* This patch is based on code from Tom Kistners exiscan (ACL integration) and
  10. * the DCC local_scan patch from Christopher Bodenstein */
  11. /* Code for calling dccifd. Called from acl.c. */
  12. #include "exim.h"
  13. #ifdef EXPERIMENTAL_DCC
  14. #include "dcc.h"
  15. #include "unistd.h"
  16. uschar dcc_header_str[256];
  17. int dcc_ok = 0;
  18. int dcc_rc = 0;
  19. /* This function takes a file descriptor and a buffer as input and
  20. * returns either 0 for success or errno in case of error. */
  21. int flushbuffer (int socket, uschar *buffer)
  22. {
  23. int retval, rsp;
  24. rsp = write(socket, buffer, Ustrlen(buffer));
  25. DEBUG(D_acl)
  26. debug_printf("DCC: Result of the write() = %d\n", rsp);
  27. if(rsp < 0)
  28. {
  29. DEBUG(D_acl)
  30. debug_printf("DCC: Error writing buffer to socket: %s\n", strerror(errno));
  31. retval = errno;
  32. }
  33. else {
  34. DEBUG(D_acl)
  35. debug_printf("DCC: Wrote buffer to socket:\n%s\n", buffer);
  36. retval = 0;
  37. }
  38. return retval;
  39. }
  40. int
  41. dcc_process(uschar **listptr)
  42. {
  43. int sep = 0;
  44. const uschar *list = *listptr;
  45. FILE *data_file;
  46. uschar *dcc_default_ip_option = US"127.0.0.1";
  47. uschar *dcc_helo_option = US"localhost";
  48. uschar *dcc_reject_message = US"Rejected by DCC";
  49. uschar *xtra_hdrs = NULL;
  50. uschar *override_client_ip = NULL;
  51. /* from local_scan */
  52. int i, j, k, c, retval, sockfd, resp, line;
  53. unsigned int portnr;
  54. struct sockaddr_un serv_addr;
  55. struct sockaddr_in serv_addr_in;
  56. struct hostent *ipaddress;
  57. uschar sockpath[128];
  58. uschar sockip[40], client_ip[40];
  59. uschar opts[128];
  60. uschar rcpt[128], from[128];
  61. uschar sendbuf[4096];
  62. uschar recvbuf[4096];
  63. uschar dcc_return_text[1024];
  64. uschar mbox_path[1024];
  65. uschar message_subdir[2];
  66. struct header_line *dcchdr;
  67. uschar *dcc_acl_options;
  68. uschar dcc_acl_options_buffer[10];
  69. uschar dcc_xtra_hdrs[1024];
  70. /* grep 1st option */
  71. if ((dcc_acl_options = string_nextinlist(&list, &sep,
  72. dcc_acl_options_buffer,
  73. sizeof(dcc_acl_options_buffer))) != NULL)
  74. {
  75. /* parse 1st option */
  76. if ( (strcmpic(dcc_acl_options,US"false") == 0) ||
  77. (Ustrcmp(dcc_acl_options,"0") == 0) ) {
  78. /* explicitly no matching */
  79. return FAIL;
  80. };
  81. /* special cases (match anything except empty) */
  82. if ( (strcmpic(dcc_acl_options,US"true") == 0) ||
  83. (Ustrcmp(dcc_acl_options,"*") == 0) ||
  84. (Ustrcmp(dcc_acl_options,"1") == 0) ) {
  85. dcc_acl_options = dcc_acl_options;
  86. };
  87. }
  88. else {
  89. /* empty means "don't match anything" */
  90. return FAIL;
  91. };
  92. sep = 0;
  93. /* if we scanned this message last time, just return */
  94. if ( dcc_ok )
  95. return dcc_rc;
  96. /* open the spooled body */
  97. message_subdir[1] = '\0';
  98. for (i = 0; i < 2; i++) {
  99. message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
  100. sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
  101. data_file = Ufopen(mbox_path,"rb");
  102. if (data_file != NULL)
  103. break;
  104. };
  105. if (data_file == NULL) {
  106. /* error while spooling */
  107. log_write(0, LOG_MAIN|LOG_PANIC,
  108. "dcc acl condition: error while opening spool file");
  109. return DEFER;
  110. };
  111. /* Initialize the variables */
  112. bzero(sockip,sizeof(sockip));
  113. if (dccifd_address) {
  114. if (dccifd_address[0] == '/')
  115. Ustrncpy(sockpath, dccifd_address, sizeof(sockpath));
  116. else
  117. if( sscanf(CS dccifd_address, "%s %u", sockip, &portnr) != 2) {
  118. log_write(0, LOG_MAIN,
  119. "dcc acl condition: warning - invalid dccifd address: '%s'", dccifd_address);
  120. (void)fclose(data_file);
  121. return DEFER;
  122. }
  123. }
  124. /* opts is what we send as dccifd options - see man dccifd */
  125. /* We don't support any other option than 'header' so just copy that */
  126. bzero(opts,sizeof(opts));
  127. Ustrncpy(opts, dccifd_options, sizeof(opts)-1);
  128. /* if $acl_m_dcc_override_client_ip is set use it */
  129. if (((override_client_ip = expand_string(US"$acl_m_dcc_override_client_ip")) != NULL) &&
  130. (override_client_ip[0] != '\0')) {
  131. Ustrncpy(client_ip, override_client_ip, sizeof(client_ip)-1);
  132. DEBUG(D_acl)
  133. debug_printf("DCC: Client IP (overridden): %s\n", client_ip);
  134. }
  135. else if(sender_host_address) {
  136. /* else if $sender_host_address is available use that? */
  137. Ustrncpy(client_ip, sender_host_address, sizeof(client_ip)-1);
  138. DEBUG(D_acl)
  139. debug_printf("DCC: Client IP (sender_host_address): %s\n", client_ip);
  140. }
  141. else {
  142. /* sender_host_address is NULL which means it comes from localhost */
  143. Ustrncpy(client_ip, dcc_default_ip_option, sizeof(client_ip)-1);
  144. DEBUG(D_acl)
  145. debug_printf("DCC: Client IP (default): %s\n", client_ip);
  146. }
  147. /* strncat(opts, my_request, strlen(my_request)); */
  148. Ustrcat(opts, "\n");
  149. Ustrncat(opts, client_ip, sizeof(opts)-Ustrlen(opts)-1);
  150. Ustrncat(opts, "\nHELO ", sizeof(opts)-Ustrlen(opts)-1);
  151. Ustrncat(opts, dcc_helo_option, sizeof(opts)-Ustrlen(opts)-2);
  152. Ustrcat(opts, "\n");
  153. /* initialize the other variables */
  154. dcchdr = header_list;
  155. /* we set the default return value to DEFER */
  156. retval = DEFER;
  157. bzero(sendbuf,sizeof(sendbuf));
  158. bzero(dcc_header_str,sizeof(dcc_header_str));
  159. bzero(rcpt,sizeof(rcpt));
  160. bzero(from,sizeof(from));
  161. /* send a null return path as "<>". */
  162. if (Ustrlen(sender_address) > 0)
  163. Ustrncpy(from, sender_address, sizeof(from));
  164. else
  165. Ustrncpy(from, "<>", sizeof(from));
  166. Ustrncat(from, "\n", sizeof(from)-Ustrlen(from)-1);
  167. /**************************************
  168. * Now creating the socket connection *
  169. **************************************/
  170. /* If sockip contains an ip, we use a tcp socket, otherwise a UNIX socket */
  171. if(Ustrcmp(sockip, "")){
  172. ipaddress = gethostbyname((char *)sockip);
  173. bzero((char *) &serv_addr_in, sizeof(serv_addr_in));
  174. serv_addr_in.sin_family = AF_INET;
  175. bcopy((char *)ipaddress->h_addr, (char *)&serv_addr_in.sin_addr.s_addr, ipaddress->h_length);
  176. serv_addr_in.sin_port = htons(portnr);
  177. if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0){
  178. DEBUG(D_acl)
  179. debug_printf("DCC: Creating TCP socket connection failed: %s\n", strerror(errno));
  180. log_write(0,LOG_PANIC,"DCC: Creating TCP socket connection failed: %s\n", strerror(errno));
  181. /* if we cannot create the socket, defer the mail */
  182. (void)fclose(data_file);
  183. return retval;
  184. }
  185. /* Now connecting the socket (INET) */
  186. if (connect(sockfd, (struct sockaddr *)&serv_addr_in, sizeof(serv_addr_in)) < 0){
  187. DEBUG(D_acl)
  188. debug_printf("DCC: Connecting to TCP socket failed: %s\n", strerror(errno));
  189. log_write(0,LOG_PANIC,"DCC: Connecting to TCP socket failed: %s\n", strerror(errno));
  190. /* if we cannot contact the socket, defer the mail */
  191. (void)fclose(data_file);
  192. return retval;
  193. }
  194. } else {
  195. /* connecting to the dccifd UNIX socket */
  196. bzero((char *)&serv_addr,sizeof(serv_addr));
  197. serv_addr.sun_family = AF_UNIX;
  198. Ustrcpy(serv_addr.sun_path, sockpath);
  199. if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0){
  200. DEBUG(D_acl)
  201. debug_printf("DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
  202. log_write(0,LOG_PANIC,"DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
  203. /* if we cannot create the socket, defer the mail */
  204. (void)fclose(data_file);
  205. return retval;
  206. }
  207. /* Now connecting the socket (UNIX) */
  208. if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
  209. DEBUG(D_acl)
  210. debug_printf("DCC: Connecting to UNIX socket failed: %s\n", strerror(errno));
  211. log_write(0,LOG_PANIC,"DCC: Connecting to UNIX socket failed: %s\n", strerror(errno));
  212. /* if we cannot contact the socket, defer the mail */
  213. (void)fclose(data_file);
  214. return retval;
  215. }
  216. }
  217. /* the socket is open, now send the options to dccifd*/
  218. DEBUG(D_acl)
  219. debug_printf("\nDCC: ---------------------------\nDCC: Socket opened; now sending input\nDCC: -----------------\n");
  220. /* First, fill in the input buffer */
  221. Ustrncpy(sendbuf, opts, sizeof(sendbuf));
  222. Ustrncat(sendbuf, from, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
  223. DEBUG(D_acl)
  224. {
  225. debug_printf("DCC: opts = %s\nDCC: sender = %s\nDCC: rcpt count = %d\n", opts, from, recipients_count);
  226. debug_printf("DCC: Sending options:\nDCC: ****************************\n");
  227. }
  228. /* let's send each of the recipients to dccifd */
  229. for (i = 0; i < recipients_count; i++){
  230. DEBUG(D_acl)
  231. debug_printf("DCC: recipient = %s\n",recipients_list[i].address);
  232. if(Ustrlen(sendbuf) + Ustrlen(recipients_list[i].address) > sizeof(sendbuf))
  233. {
  234. DEBUG(D_acl)
  235. debug_printf("DCC: Writing buffer: %s\n", sendbuf);
  236. flushbuffer(sockfd, sendbuf);
  237. bzero(sendbuf, sizeof(sendbuf));
  238. }
  239. Ustrncat(sendbuf, recipients_list[i].address, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
  240. Ustrncat(sendbuf, "\r\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
  241. }
  242. /* send a blank line between options and message */
  243. Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
  244. /* Now we send the input buffer */
  245. DEBUG(D_acl)
  246. debug_printf("DCC: %s\nDCC: ****************************\n", sendbuf);
  247. flushbuffer(sockfd, sendbuf);
  248. /* now send the message */
  249. /* Clear the input buffer */
  250. bzero(sendbuf, sizeof(sendbuf));
  251. /* First send the headers */
  252. /* Now send the headers */
  253. DEBUG(D_acl)
  254. debug_printf("DCC: Sending headers:\nDCC: ****************************\n");
  255. Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
  256. while((dcchdr=dcchdr->next)) {
  257. if(dcchdr->slen > sizeof(sendbuf)-2) {
  258. /* The size of the header is bigger than the size of
  259. * the input buffer, so split it up in smaller parts. */
  260. flushbuffer(sockfd, sendbuf);
  261. bzero(sendbuf, sizeof(sendbuf));
  262. j = 0;
  263. while(j < dcchdr->slen)
  264. {
  265. for(i = 0; i < sizeof(sendbuf)-2; i++) {
  266. sendbuf[i] = dcchdr->text[j];
  267. j++;
  268. }
  269. flushbuffer(sockfd, sendbuf);
  270. bzero(sendbuf, sizeof(sendbuf));
  271. }
  272. } else if(Ustrlen(sendbuf) + dcchdr->slen > sizeof(sendbuf)-2) {
  273. flushbuffer(sockfd, sendbuf);
  274. bzero(sendbuf, sizeof(sendbuf));
  275. Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
  276. } else {
  277. Ustrncat(sendbuf, dcchdr->text, sizeof(sendbuf)-Ustrlen(sendbuf)-2);
  278. }
  279. }
  280. /* a blank line seperates header from body */
  281. Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
  282. flushbuffer(sockfd, sendbuf);
  283. DEBUG(D_acl)
  284. debug_printf("\nDCC: ****************************\n%s", sendbuf);
  285. /* Clear the input buffer */
  286. bzero(sendbuf, sizeof(sendbuf));
  287. /* now send the body */
  288. DEBUG(D_acl)
  289. debug_printf("DCC: Writing body:\nDCC: ****************************\n");
  290. (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
  291. while((fread(sendbuf, 1, sizeof(sendbuf)-1, data_file)) > 0) {
  292. flushbuffer(sockfd, sendbuf);
  293. bzero(sendbuf, sizeof(sendbuf));
  294. }
  295. DEBUG(D_acl)
  296. debug_printf("\nDCC: ****************************\n");
  297. /* shutdown() the socket */
  298. if(shutdown(sockfd, 1) < 0){
  299. DEBUG(D_acl)
  300. debug_printf("DCC: Couldn't shutdown socket: %s\n", strerror(errno));
  301. log_write(0,LOG_MAIN,"DCC: Couldn't shutdown socket: %s\n", strerror(errno));
  302. /* If there is a problem with the shutdown()
  303. * defer the mail. */
  304. (void)fclose(data_file);
  305. return retval;
  306. }
  307. DEBUG(D_acl)
  308. debug_printf("\nDCC: -------------------------\nDCC: Input sent.\nDCC: -------------------------\n");
  309. /********************************
  310. * receiving output from dccifd *
  311. ********************************/
  312. DEBUG(D_acl)
  313. debug_printf("\nDCC: -------------------------------------\nDCC: Now receiving output from server\nDCC: -----------------------------------\n");
  314. /******************************************************************
  315. * We should get 3 lines: *
  316. * 1/ First line is overall result: either 'A' for Accept, *
  317. * 'R' for Reject, 'S' for accept Some recipients or *
  318. * 'T' for a Temporary error. *
  319. * 2/ Second line contains the list of Accepted/Rejected *
  320. * recipients in the form AARRA (A = accepted, R = rejected). *
  321. * 3/ Third line contains the X-DCC header. *
  322. ******************************************************************/
  323. line = 1; /* we start at the first line of the output */
  324. j = 0; /* will be used as index for the recipients list */
  325. k = 0; /* initializing the index of the X-DCC header: dcc_header_str[k] */
  326. /* Let's read from the socket until there's nothing left to read */
  327. bzero(recvbuf, sizeof(recvbuf));
  328. while((resp = read(sockfd, recvbuf, sizeof(recvbuf)-1)) > 0) {
  329. /* How much did we get from the socket */
  330. c = Ustrlen(recvbuf) + 1;
  331. DEBUG(D_acl)
  332. debug_printf("DCC: Length of the output buffer is: %d\nDCC: Output buffer is:\nDCC: ------------\nDCC: %s\nDCC: -----------\n", c, recvbuf);
  333. /* Now let's read each character and see what we've got */
  334. for(i = 0; i < c; i++) {
  335. /* First check if we reached the end of the line and
  336. * then increment the line counter */
  337. if(recvbuf[i] == '\n') {
  338. line++;
  339. }
  340. else {
  341. /* The first character of the first line is the
  342. * overall response. If there's another character
  343. * on that line it is not correct. */
  344. if(line == 1) {
  345. if(i == 0) {
  346. /* Now get the value and set the
  347. * return value accordingly */
  348. if(recvbuf[i] == 'A') {
  349. DEBUG(D_acl)
  350. debug_printf("DCC: Overall result = A\treturning OK\n");
  351. Ustrcpy(dcc_return_text, "Mail accepted by DCC");
  352. dcc_result = US"A";
  353. retval = OK;
  354. }
  355. else if(recvbuf[i] == 'R') {
  356. DEBUG(D_acl)
  357. debug_printf("DCC: Overall result = R\treturning FAIL\n");
  358. dcc_result = US"R";
  359. retval = FAIL;
  360. if(sender_host_name) {
  361. log_write(0, LOG_MAIN, "H=%s [%s] F=<%s>: rejected by DCC", sender_host_name, sender_host_address, sender_address);
  362. }
  363. else {
  364. log_write(0, LOG_MAIN, "H=[%s] F=<%s>: rejected by DCC", sender_host_address, sender_address);
  365. }
  366. Ustrncpy(dcc_return_text, dcc_reject_message, Ustrlen(dcc_reject_message) + 1);
  367. }
  368. else if(recvbuf[i] == 'S') {
  369. DEBUG(D_acl)
  370. debug_printf("DCC: Overall result = S\treturning OK\n");
  371. Ustrcpy(dcc_return_text, "Not all recipients accepted by DCC");
  372. /* Since we're in an ACL we want a global result
  373. * so we accept for all */
  374. dcc_result = US"A";
  375. retval = OK;
  376. }
  377. else if(recvbuf[i] == 'G') {
  378. DEBUG(D_acl)
  379. debug_printf("DCC: Overall result = G\treturning FAIL\n");
  380. Ustrcpy(dcc_return_text, "Greylisted by DCC");
  381. dcc_result = US"G";
  382. retval = FAIL;
  383. }
  384. else if(recvbuf[i] == 'T') {
  385. DEBUG(D_acl)
  386. debug_printf("DCC: Overall result = T\treturning DEFER\n");
  387. retval = DEFER;
  388. log_write(0,LOG_MAIN,"Temporary error with DCC: %s\n", recvbuf);
  389. Ustrcpy(dcc_return_text, "Temporary error with DCC");
  390. dcc_result = US"T";
  391. }
  392. else {
  393. DEBUG(D_acl)
  394. debug_printf("DCC: Overall result = something else\treturning DEFER\n");
  395. retval = DEFER;
  396. log_write(0,LOG_MAIN,"Unknown DCC response: %s\n", recvbuf);
  397. Ustrcpy(dcc_return_text, "Unknown DCC response");
  398. dcc_result = US"T";
  399. }
  400. }
  401. else {
  402. /* We're on the first line but not on the first character,
  403. * there must be something wrong. */
  404. DEBUG(D_acl)
  405. debug_printf("DCC: Line = %d but i = %d != 0 character is %c - This is wrong!\n", line, i, recvbuf[i]);
  406. log_write(0,LOG_MAIN,"Wrong header from DCC, output is %s\n", recvbuf);
  407. }
  408. }
  409. else if(line == 2) {
  410. /* On the second line we get a list of
  411. * answers for each recipient. We don't care about
  412. * it because we're in an acl and take the
  413. * global result. */
  414. }
  415. else if(line > 2) {
  416. /* The third and following lines are the X-DCC header,
  417. * so we store it in dcc_header_str. */
  418. /* check if we don't get more than we can handle */
  419. if(k < sizeof(dcc_header_str)) {
  420. dcc_header_str[k] = recvbuf[i];
  421. k++;
  422. }
  423. else {
  424. DEBUG(D_acl)
  425. debug_printf("DCC: We got more output than we can store in the X-DCC header. Truncating at 120 characters.\n");
  426. }
  427. }
  428. else {
  429. /* Wrong line number. There must be a problem with the output. */
  430. DEBUG(D_acl)
  431. debug_printf("DCC: Wrong line number in output. Line number is %d\n", line);
  432. }
  433. }
  434. }
  435. /* we reinitialize the output buffer before we read again */
  436. bzero(recvbuf,sizeof(recvbuf));
  437. }
  438. /* We have read everything from the socket */
  439. /* We need to terminate the X-DCC header with a '\n' character. This needs to be k-1
  440. * since dcc_header_str[k] contains '\0'. */
  441. dcc_header_str[k-1] = '\n';
  442. /* Now let's sum up what we've got. */
  443. DEBUG(D_acl)
  444. debug_printf("\nDCC: --------------------------\nDCC: Overall result = %d\nDCC: X-DCC header: %sReturn message: %s\nDCC: dcc_result: %s\n", retval, dcc_header_str, dcc_return_text, dcc_result);
  445. /* We only add the X-DCC header if it starts with X-DCC */
  446. if(!(Ustrncmp(dcc_header_str, "X-DCC", 5))){
  447. dcc_header = dcc_header_str;
  448. if(dcc_direct_add_header) {
  449. header_add(' ' , "%s", dcc_header_str);
  450. /* since the MIME ACL already writes the .eml file to disk without DCC Header we've to erase it */
  451. unspool_mbox();
  452. }
  453. }
  454. else {
  455. DEBUG(D_acl)
  456. debug_printf("DCC: Wrong format of the X-DCC header: %s\n", dcc_header_str);
  457. }
  458. /* check if we should add additional headers passed in acl_m_dcc_add_header */
  459. if(dcc_direct_add_header) {
  460. if (((xtra_hdrs = expand_string(US"$acl_m_dcc_add_header")) != NULL) && (xtra_hdrs[0] != '\0')) {
  461. Ustrncpy(dcc_xtra_hdrs, xtra_hdrs, sizeof(dcc_xtra_hdrs) - 2);
  462. if (dcc_xtra_hdrs[Ustrlen(dcc_xtra_hdrs)-1] != '\n')
  463. Ustrcat(dcc_xtra_hdrs, "\n");
  464. header_add(' ', "%s", dcc_xtra_hdrs);
  465. DEBUG(D_acl)
  466. debug_printf("DCC: adding additional headers in $acl_m_dcc_add_header: %s", dcc_xtra_hdrs);
  467. }
  468. }
  469. dcc_ok = 1;
  470. /* Now return to exim main process */
  471. DEBUG(D_acl)
  472. debug_printf("DCC: Before returning to exim main process:\nDCC: return_text = %s - retval = %d\nDCC: dcc_result = %s\n", dcc_return_text, retval, dcc_result);
  473. (void)fclose(data_file);
  474. dcc_rc = retval;
  475. return dcc_rc;
  476. }
  477. #endif