php_milter.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Harald Radi <phanto@php.net> |
  16. | Parts based on CGI SAPI Module by |
  17. | Rasmus Lerdorf, Stig Bakken and Zeev Suraski |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include "php.h"
  22. #include "php_globals.h"
  23. #include "php_variables.h"
  24. #include "zend_modules.h"
  25. #ifndef ZTS
  26. #error SRM sapi module is only useable in thread-safe mode
  27. #endif
  28. #include "SAPI.h"
  29. #include <stdio.h>
  30. #include "php.h"
  31. #if HAVE_SYS_TIME_H
  32. #include <sys/time.h>
  33. #endif
  34. #if HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. #if HAVE_SIGNAL_H
  38. #include <signal.h>
  39. #endif
  40. #if HAVE_SETLOCALE
  41. #include <locale.h>
  42. #endif
  43. #include "zend.h"
  44. #include "zend_extensions.h"
  45. #include "php_ini.h"
  46. #include "php_globals.h"
  47. #include "php_main.h"
  48. #include "fopen_wrappers.h"
  49. #include "ext/standard/php_standard.h"
  50. #ifdef __riscos__
  51. #include <unixlib/local.h>
  52. #endif
  53. #include "zend_compile.h"
  54. #include "zend_execute.h"
  55. #include "zend_highlight.h"
  56. #include "zend_indent.h"
  57. #include "libmilter/mfapi.h"
  58. #include "php_getopt.h"
  59. #define OPTSTRING "ac:d:Def:hnp:vVz:?"
  60. #define MG(v) TSRMG(milter_globals_id, zend_milter_globals *, v)
  61. #define IS_NONE "%s(): This function must not be called outside of a milter callback function's scope"
  62. #define NOT_EOM "%s(): This function can only be used inside the milter_eom callback's scope"
  63. #define NOT_INIT "%s(): This function can only be used inside the milter_init callback's scope"
  64. #define MLFI_NONE 0
  65. #define MLFI_CONNECT 1
  66. #define MLFI_HELO 2
  67. #define MLFI_ENVFROM 3
  68. #define MLFI_ENVRCPT 4
  69. #define MLFI_HEADER 5
  70. #define MLFI_EOH 6
  71. #define MLFI_BODY 7
  72. #define MLFI_EOM 8
  73. #define MLFI_ABORT 9
  74. #define MLFI_CLOSE 10
  75. #define MLFI_INIT 11
  76. /* {{{ globals
  77. */
  78. extern char *ap_php_optarg;
  79. extern int ap_php_optind;
  80. static int flag_debug=0;
  81. static char *filename = NULL;
  82. /* per thread */
  83. ZEND_BEGIN_MODULE_GLOBALS(milter)
  84. SMFICTX *ctx;
  85. int state;
  86. int initialized;
  87. ZEND_END_MODULE_GLOBALS(milter)
  88. ZEND_DECLARE_MODULE_GLOBALS(milter)
  89. /* }}} */
  90. /* this method is called only once when the milter starts */
  91. /* {{{ Init Milter
  92. */
  93. static int mlfi_init()
  94. {
  95. int ret = 0;
  96. zend_file_handle file_handle;
  97. zval function_name, retval;
  98. int status;
  99. TSRMLS_FETCH();
  100. /* request startup */
  101. if (php_request_startup(TSRMLS_C)==FAILURE) {
  102. SG(headers_sent) = 1;
  103. SG(request_info).no_headers = 1;
  104. php_request_shutdown((void *) 0);
  105. return -1;
  106. }
  107. /* disable headers */
  108. SG(headers_sent) = 1;
  109. SG(request_info).no_headers = 1;
  110. if (filename == NULL) {
  111. php_printf("No input file specified");
  112. return SMFIS_TEMPFAIL;
  113. }
  114. if (!(file_handle.handle.fp = VCWD_FOPEN(filename, "rb"))) {
  115. php_printf("Could not open input file: %s\n", filename);
  116. return SMFIS_TEMPFAIL;
  117. }
  118. file_handle.type = ZEND_HANDLE_FP;
  119. file_handle.filename = filename;
  120. file_handle.free_filename = 0;
  121. file_handle.opened_path = NULL;
  122. php_execute_script(&file_handle TSRMLS_CC);
  123. /* call userland */
  124. INIT_ZVAL(function_name);
  125. ZVAL_STRING(&function_name, "milter_init", 0);
  126. /* set the milter context for possible use in API functions */
  127. MG(state) = MLFI_INIT;
  128. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 0, NULL TSRMLS_CC);
  129. MG(state) = MLFI_NONE;
  130. MG(initialized) = 1;
  131. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  132. ret = Z_LVAL(retval);
  133. }
  134. php_request_shutdown((void *) 0);
  135. return ret;
  136. }
  137. /* }}} */
  138. /* {{{ Milter callback functions
  139. */
  140. /* connection info filter, is called whenever sendmail connects to the milter */
  141. /* {{{ mlfi_connect()
  142. */
  143. static sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
  144. {
  145. zend_file_handle file_handle;
  146. zval function_name, retval, *param[1];
  147. int status;
  148. TSRMLS_FETCH();
  149. /* request startup */
  150. if (php_request_startup(TSRMLS_C)==FAILURE) {
  151. SG(headers_sent) = 1;
  152. SG(request_info).no_headers = 1;
  153. php_request_shutdown((void *) 0);
  154. return SMFIS_TEMPFAIL;
  155. }
  156. /* disable headers */
  157. SG(headers_sent) = 1;
  158. SG(request_info).no_headers = 1;
  159. if (filename == NULL) {
  160. php_printf("No input file specified");
  161. return SMFIS_TEMPFAIL;
  162. }
  163. if (!(file_handle.handle.fp = VCWD_FOPEN(filename, "rb"))) {
  164. php_printf("Could not open input file: %s\n", filename);
  165. return SMFIS_TEMPFAIL;
  166. }
  167. file_handle.type = ZEND_HANDLE_FP;
  168. file_handle.filename = filename;
  169. file_handle.free_filename = 0;
  170. file_handle.opened_path = NULL;
  171. php_execute_script(&file_handle TSRMLS_CC);
  172. /* call userland */
  173. INIT_ZVAL(function_name);
  174. ALLOC_ZVAL(param[0]);
  175. INIT_PZVAL(param[0]);
  176. ZVAL_STRING(&function_name, "milter_connect", 0);
  177. ZVAL_STRING(param[0], hostname, 1);
  178. /* set the milter context for possible use in API functions */
  179. MG(ctx) = ctx;
  180. MG(state) = MLFI_CONNECT;
  181. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 1, param TSRMLS_CC);
  182. MG(state) = MLFI_NONE;
  183. zval_ptr_dtor(param);
  184. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  185. return Z_LVAL(retval);
  186. }
  187. return SMFIS_CONTINUE;
  188. }
  189. /* }}} */
  190. /* SMTP HELO command filter */
  191. /* {{{ mlfi_helo()
  192. */
  193. static sfsistat mlfi_helo(SMFICTX *ctx, char *helohost)
  194. {
  195. zval function_name, retval, *param[1];
  196. int status;
  197. TSRMLS_FETCH();
  198. /* call userland */
  199. INIT_ZVAL(function_name);
  200. ALLOC_ZVAL(param[0]);
  201. INIT_PZVAL(param[0]);
  202. ZVAL_STRING(&function_name, "milter_helo", 0);
  203. ZVAL_STRING(param[0], helohost, 1);
  204. /* set the milter context for possible use in API functions */
  205. MG(ctx) = ctx;
  206. MG(state) = MLFI_HELO;
  207. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 1, param TSRMLS_CC);
  208. MG(state) = MLFI_NONE;
  209. zval_ptr_dtor(param);
  210. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  211. return Z_LVAL(retval);
  212. }
  213. return SMFIS_CONTINUE;
  214. }
  215. /* }}} */
  216. /* envelope sender filter */
  217. /* {{{ mlfi_envform()
  218. */
  219. static sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv)
  220. {
  221. zval function_name, retval, *param[1];
  222. int status;
  223. TSRMLS_FETCH();
  224. /* call userland */
  225. INIT_ZVAL(function_name);
  226. ALLOC_ZVAL(param[0]);
  227. INIT_PZVAL(param[0]);
  228. ZVAL_STRING(&function_name, "milter_envfrom", 0);
  229. array_init(param[0]);
  230. while (*argv) {
  231. add_next_index_string(param[0], *argv, 1);
  232. argv++;
  233. }
  234. /* set the milter context for possible use in API functions */
  235. MG(ctx) = ctx;
  236. MG(state) = MLFI_ENVFROM;
  237. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 1, param TSRMLS_CC);
  238. MG(state) = MLFI_NONE;
  239. zval_ptr_dtor(param);
  240. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  241. return Z_LVAL(retval);
  242. }
  243. return SMFIS_CONTINUE;
  244. }
  245. /* }}} */
  246. /* envelope recipient filter */
  247. /* {{{ mlfi_envrcpt()
  248. */
  249. static sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv)
  250. {
  251. zval function_name, retval, *param[1];
  252. int status;
  253. TSRMLS_FETCH();
  254. /* call userland */
  255. INIT_ZVAL(function_name);
  256. ALLOC_ZVAL(param[0]);
  257. INIT_PZVAL(param[0]);
  258. ZVAL_STRING(&function_name, "milter_envrcpt", 0);
  259. array_init(param[0]);
  260. while (*argv) {
  261. add_next_index_string(param[0], *argv, 1);
  262. argv++;
  263. }
  264. /* set the milter context for possible use in API functions */
  265. MG(ctx) = ctx;
  266. MG(state) = MLFI_ENVRCPT;
  267. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 1, param TSRMLS_CC);
  268. MG(state) = MLFI_NONE;
  269. zval_ptr_dtor(param);
  270. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  271. return Z_LVAL(retval);
  272. }
  273. return SMFIS_CONTINUE;
  274. }
  275. /* }}} */
  276. /* header filter */
  277. /* {{{ mlfi_header()
  278. */
  279. static sfsistat mlfi_header(SMFICTX *ctx, char *headerf, char *headerv)
  280. {
  281. zval function_name, retval, *param[2];
  282. int status;
  283. TSRMLS_FETCH();
  284. /* call userland */
  285. INIT_ZVAL(function_name);
  286. ALLOC_ZVAL(param[0]);
  287. ALLOC_ZVAL(param[1]);
  288. INIT_PZVAL(param[0]);
  289. INIT_PZVAL(param[1]);
  290. ZVAL_STRING(&function_name, "milter_header", 0);
  291. ZVAL_STRING(param[0], headerf, 1);
  292. ZVAL_STRING(param[1], headerv, 1);
  293. /* set the milter context for possible use in API functions */
  294. MG(ctx) = ctx;
  295. MG(state) = MLFI_HEADER;
  296. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 2, param TSRMLS_CC);
  297. MG(state) = MLFI_NONE;
  298. zval_ptr_dtor(&param[0]);
  299. zval_ptr_dtor(&param[1]);
  300. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  301. return Z_LVAL(retval);
  302. }
  303. return SMFIS_CONTINUE;
  304. }
  305. /* }}} */
  306. /* end of header */
  307. /* {{{ mlfi_eoh()
  308. */
  309. static sfsistat mlfi_eoh(SMFICTX *ctx)
  310. {
  311. zval function_name, retval;
  312. int status;
  313. TSRMLS_FETCH();
  314. /* call userland */
  315. INIT_ZVAL(function_name);
  316. ZVAL_STRING(&function_name, "milter_eoh", 0);
  317. /* set the milter context for possible use in API functions */
  318. MG(ctx) = ctx;
  319. MG(state) = MLFI_EOH;
  320. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 0, NULL TSRMLS_CC);
  321. MG(state) = MLFI_NONE;
  322. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  323. return Z_LVAL(retval);
  324. }
  325. return SMFIS_CONTINUE;
  326. }
  327. /* }}} */
  328. /* body block */
  329. /* {{{ mlfi_body()
  330. */
  331. static sfsistat mlfi_body(SMFICTX *ctx, u_char *bodyp, size_t len)
  332. {
  333. zval function_name, retval, *param[1];
  334. int status;
  335. TSRMLS_FETCH();
  336. /* call userland */
  337. INIT_ZVAL(function_name);
  338. ALLOC_ZVAL(param[0]);
  339. INIT_PZVAL(param[0]);
  340. ZVAL_STRING(&function_name, "milter_body", 0);
  341. ZVAL_STRINGL(param[0], (char*)bodyp, len, 1); /*alex*/
  342. /* set the milter context for possible use in API functions */
  343. MG(ctx) = ctx;
  344. MG(state) = MLFI_BODY;
  345. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 1, param TSRMLS_CC);
  346. MG(state) = MLFI_NONE;
  347. zval_ptr_dtor(param);
  348. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  349. return Z_LVAL(retval);
  350. }
  351. return SMFIS_CONTINUE;
  352. }
  353. /* }}} */
  354. /* end of message */
  355. /* {{{ mlfi_eom()
  356. */
  357. static sfsistat mlfi_eom(SMFICTX *ctx)
  358. {
  359. zval function_name, retval;
  360. int status;
  361. TSRMLS_FETCH();
  362. /* call userland */
  363. INIT_ZVAL(function_name);
  364. ZVAL_STRING(&function_name, "milter_eom", 0);
  365. /* set the milter context for possible use in API functions */
  366. MG(ctx) = ctx;
  367. MG(state) = MLFI_EOM;
  368. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 0, NULL TSRMLS_CC);
  369. MG(state) = MLFI_NONE;
  370. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  371. return Z_LVAL(retval);
  372. }
  373. return SMFIS_CONTINUE;
  374. }
  375. /* }}} */
  376. /* message aborted */
  377. /* {{{ mlfi_abort()
  378. */
  379. static sfsistat mlfi_abort(SMFICTX *ctx)
  380. {
  381. zval function_name, retval;
  382. int status;
  383. TSRMLS_FETCH();
  384. /* call userland */
  385. INIT_ZVAL(function_name);
  386. ZVAL_STRING(&function_name, "milter_abort", 0);
  387. /* set the milter context for possible use in API functions */
  388. MG(ctx) = ctx;
  389. MG(state) = MLFI_ABORT;
  390. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 0, NULL TSRMLS_CC);
  391. MG(state) = MLFI_NONE;
  392. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  393. return Z_LVAL(retval);
  394. }
  395. return SMFIS_CONTINUE;
  396. }
  397. /* }}} */
  398. /* connection cleanup */
  399. /* {{{ mlfi_close()
  400. */
  401. static sfsistat mlfi_close(SMFICTX *ctx)
  402. {
  403. int ret = SMFIS_CONTINUE;
  404. zval function_name, retval;
  405. int status;
  406. TSRMLS_FETCH();
  407. if (!SG(sapi_started) && SUCCESS != php_request_startup(TSRMLS_C)) {
  408. return ret;
  409. }
  410. /* call userland */
  411. INIT_ZVAL(function_name);
  412. ZVAL_STRING(&function_name, "milter_close", 0);
  413. /* set the milter context for possible use in API functions */
  414. MG(ctx) = ctx;
  415. MG(state) = MLFI_CLOSE;
  416. status = call_user_function(CG(function_table), NULL, &function_name, &retval, 0, NULL TSRMLS_CC);
  417. MG(state) = MLFI_NONE;
  418. if (status == SUCCESS && Z_TYPE(retval) == IS_LONG) {
  419. ret = Z_LVAL(retval);
  420. }
  421. php_request_shutdown((void *) 0);
  422. return ret;
  423. }
  424. /* }}} */
  425. /* }}} */
  426. /* {{{ Milter entry struct
  427. */
  428. static struct smfiDesc smfilter = {
  429. "php-milter", /* filter name */
  430. SMFI_VERSION, /* version code -- leave untouched */
  431. 0, /* flags */
  432. mlfi_connect, /* info filter callback */
  433. mlfi_helo, /* HELO filter callback */
  434. mlfi_envfrom, /* envelope filter callback */
  435. mlfi_envrcpt, /* envelope recipient filter callback */
  436. mlfi_header, /* header filter callback */
  437. mlfi_eoh, /* end of header callback */
  438. mlfi_body, /* body filter callback */
  439. mlfi_eom, /* end of message callback */
  440. mlfi_abort, /* message aborted callback */
  441. mlfi_close, /* connection cleanup callback */
  442. };
  443. /* }}} */
  444. /* {{{ PHP Milter API
  445. */
  446. /* {{{ proto void smfi_setflags(long flags)
  447. Sets the flags describing the actions the filter may take. */
  448. PHP_FUNCTION(smfi_setflags)
  449. {
  450. long flags;
  451. /* valid only in the init callback */
  452. if (MG(state) != MLFI_INIT) {
  453. php_error(E_WARNING, NOT_INIT, get_active_function_name(TSRMLS_C));
  454. } else if (zend_parse_parameters(1 TSRMLS_CC, "l", &flags) == SUCCESS) {
  455. flags = flags & (SMFIF_ADDHDRS|SMFIF_CHGHDRS|SMFIF_CHGBODY|SMFIF_ADDRCPT|SMFIF_DELRCPT);
  456. smfilter.xxfi_flags = flags;
  457. }
  458. }
  459. /* }}} */
  460. /* {{{ proto void smfi_settimeout(long timeout)
  461. Sets the number of seconds libmilter will wait for an MTA connection before timing out a socket. */
  462. PHP_FUNCTION(smfi_settimeout)
  463. {
  464. long timeout;
  465. /* valid only in the init callback */
  466. if (MG(state) != MLFI_INIT) {
  467. php_error(E_WARNING, NOT_INIT, get_active_function_name(TSRMLS_C));
  468. } else if (zend_parse_parameters(1 TSRMLS_CC, "l", &timeout) == SUCCESS) {
  469. smfi_settimeout(timeout);
  470. }
  471. }
  472. /* }}} */
  473. /* {{{ proto string smfi_getsymval(string macro)
  474. Returns the value of the given macro or NULL if the macro is not defined. */
  475. PHP_FUNCTION(smfi_getsymval)
  476. {
  477. char *symname, *ret;
  478. int len;
  479. /* valid in any callback */
  480. if (MG(state) == MLFI_NONE) {
  481. php_error(E_WARNING, IS_NONE, get_active_function_name(TSRMLS_C));
  482. } else if (zend_parse_parameters(1 TSRMLS_CC, "s", &symname, &len) == SUCCESS) {
  483. if ((ret = smfi_getsymval(MG(ctx), symname)) != NULL) {
  484. RETURN_STRING(ret, 1);
  485. }
  486. }
  487. RETURN_NULL();
  488. }
  489. /* }}} */
  490. /* {{{ proto bool smfi_setreply(string rcode, string xcode, string message)
  491. Directly set the SMTP error reply code for this connection.
  492. This code will be used on subsequent error replies resulting from actions taken by this filter. */
  493. PHP_FUNCTION(smfi_setreply)
  494. {
  495. char *rcode, *xcode, *message;
  496. int len;
  497. /* valid in any callback */
  498. if (MG(state) == MLFI_NONE) {
  499. php_error(E_WARNING, IS_NONE, get_active_function_name(TSRMLS_C));
  500. } else if (zend_parse_parameters(3 TSRMLS_CC, "sss", &rcode, &len, &xcode, &len, &message, &len) == SUCCESS) {
  501. if (smfi_setreply(MG(ctx), rcode, xcode, message) == MI_SUCCESS) {
  502. RETURN_TRUE;
  503. }
  504. }
  505. RETURN_FALSE;
  506. }
  507. /* }}} */
  508. /* {{{ proto bool smfi_addheader(string headerf, string headerv)
  509. Adds a header to the current message. */
  510. PHP_FUNCTION(smfi_addheader)
  511. {
  512. char *f, *v;
  513. int len;
  514. /* valid only in milter_eom */
  515. if (MG(state) != MLFI_EOM) {
  516. php_error(E_WARNING, NOT_EOM, get_active_function_name(TSRMLS_C));
  517. } else if (zend_parse_parameters(2 TSRMLS_CC, "ss", &f, &len, &v, &len) == SUCCESS) {
  518. if (smfi_addheader(MG(ctx), f, v) == MI_SUCCESS) {
  519. RETURN_TRUE;
  520. }
  521. }
  522. RETURN_FALSE;
  523. }
  524. /* }}} */
  525. /* {{{ proto bool smfi_chgheader(string headerf, string headerv)
  526. Changes a header's value for the current message. */
  527. PHP_FUNCTION(smfi_chgheader)
  528. {
  529. char *f, *v;
  530. long idx;
  531. int len;
  532. /* valid only in milter_eom */
  533. if (MG(state) != MLFI_EOM) {
  534. php_error(E_WARNING, NOT_EOM, get_active_function_name(TSRMLS_C));
  535. } else if (zend_parse_parameters(3 TSRMLS_CC, "sls", &f, &len, &idx, &v, &len) == SUCCESS) {
  536. if (smfi_chgheader(MG(ctx), f, idx, v) == MI_SUCCESS) {
  537. RETURN_TRUE;
  538. }
  539. }
  540. RETURN_FALSE;
  541. }
  542. /* }}} */
  543. /* {{{ proto bool smfi_addrcpt(string rcpt)
  544. Add a recipient to the message envelope. */
  545. PHP_FUNCTION(smfi_addrcpt)
  546. {
  547. char *rcpt;
  548. int len;
  549. /* valid only in milter_eom */
  550. if (MG(state) != MLFI_EOM) {
  551. php_error(E_WARNING, NOT_EOM, get_active_function_name(TSRMLS_C));
  552. } else if (zend_parse_parameters(1 TSRMLS_CC, "s", &rcpt, &len) == SUCCESS) {
  553. if (smfi_addrcpt(MG(ctx), rcpt) == MI_SUCCESS) {
  554. RETURN_TRUE;
  555. }
  556. }
  557. RETURN_FALSE;
  558. }
  559. /* }}} */
  560. /* {{{ proto bool smfi_delrcpt(string rcpt)
  561. Removes the named recipient from the current message's envelope. */
  562. PHP_FUNCTION(smfi_delrcpt)
  563. {
  564. char *rcpt;
  565. int len;
  566. /* valid only in milter_eom */
  567. if (MG(state) != MLFI_EOM) {
  568. php_error(E_WARNING, NOT_EOM, get_active_function_name(TSRMLS_C));
  569. } else if (zend_parse_parameters(1 TSRMLS_CC, "s", &rcpt, &len) == SUCCESS) {
  570. if (smfi_delrcpt(MG(ctx), rcpt) == MI_SUCCESS) {
  571. RETURN_TRUE;
  572. }
  573. }
  574. RETURN_FALSE;
  575. }
  576. /* }}} */
  577. /* {{{ proto bool smfi_replacebody(string body)
  578. Replaces the body of the current message. If called more than once,
  579. subsequent calls result in data being appended to the new body. */
  580. PHP_FUNCTION(smfi_replacebody)
  581. {
  582. char *body;
  583. int len;
  584. /* valid only in milter_eom */
  585. if (MG(state) != MLFI_EOM) {
  586. php_error(E_WARNING, NOT_EOM, get_active_function_name(TSRMLS_C));
  587. } else if (zend_parse_parameters(1 TSRMLS_CC, "s", &body, &len) == SUCCESS) {
  588. if (smfi_replacebody(MG(ctx), (u_char*)body, len) == MI_SUCCESS) {
  589. RETURN_TRUE;
  590. }
  591. }
  592. RETURN_FALSE;
  593. }
  594. /* }}} */
  595. /* {{{ PHP_MINIT_FUNCTION
  596. */
  597. PHP_MINIT_FUNCTION(milter)
  598. {
  599. REGISTER_LONG_CONSTANT("SMFIS_CONTINUE", SMFIS_CONTINUE, CONST_CS | CONST_PERSISTENT);
  600. REGISTER_LONG_CONSTANT("SMFIS_REJECT", SMFIS_REJECT, CONST_CS | CONST_PERSISTENT);
  601. REGISTER_LONG_CONSTANT("SMFIS_DISCARD", SMFIS_DISCARD, CONST_CS | CONST_PERSISTENT);
  602. REGISTER_LONG_CONSTANT("SMFIS_ACCEPT", SMFIS_ACCEPT, CONST_CS | CONST_PERSISTENT);
  603. REGISTER_LONG_CONSTANT("SMFIS_TEMPFAIL", SMFIS_TEMPFAIL, CONST_CS | CONST_PERSISTENT);
  604. REGISTER_LONG_CONSTANT("SMFIF_ADDHDRS", SMFIF_ADDHDRS, CONST_CS | CONST_PERSISTENT);
  605. REGISTER_LONG_CONSTANT("SMFIF_CHGHDRS", SMFIF_CHGHDRS, CONST_CS | CONST_PERSISTENT);
  606. REGISTER_LONG_CONSTANT("SMFIF_CHGBODY", SMFIF_CHGBODY, CONST_CS | CONST_PERSISTENT);
  607. REGISTER_LONG_CONSTANT("SMFIF_ADDRCPT", SMFIF_ADDRCPT, CONST_CS | CONST_PERSISTENT);
  608. REGISTER_LONG_CONSTANT("SMFIF_DELRCPT", SMFIF_DELRCPT, CONST_CS | CONST_PERSISTENT);
  609. ZEND_INIT_MODULE_GLOBALS(milter, NULL, NULL);
  610. MG(state) = MLFI_NONE;
  611. MG(initialized) = 0;
  612. return SUCCESS;
  613. }
  614. /* }}} */
  615. /* {{{ PHP_MINFO_FUNCTION
  616. */
  617. PHP_MINFO_FUNCTION(milter)
  618. {
  619. php_info_print_table_start();
  620. php_info_print_table_header(2, "Milter support", "enabled");
  621. php_info_print_table_end();
  622. }
  623. /* }}} */
  624. /* }}} */
  625. /* {{{ arginfo */
  626. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_setflags, 0, 0, 1)
  627. ZEND_ARG_INFO(0, flags)
  628. ZEND_END_ARG_INFO()
  629. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_settimeout, 0, 0, 1)
  630. ZEND_ARG_INFO(0, timeout)
  631. ZEND_END_ARG_INFO()
  632. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_getsymval, 0, 0, 1)
  633. ZEND_ARG_INFO(0, macro)
  634. ZEND_END_ARG_INFO()
  635. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_setreply, 0, 0, 3)
  636. ZEND_ARG_INFO(0, rcode)
  637. ZEND_ARG_INFO(0, xcode)
  638. ZEND_ARG_INFO(0, message)
  639. ZEND_END_ARG_INFO()
  640. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_addheader, 0, 0, 2)
  641. ZEND_ARG_INFO(0, headerf)
  642. ZEND_ARG_INFO(0, headerv)
  643. ZEND_END_ARG_INFO()
  644. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_chgheader, 0, 0, 2)
  645. ZEND_ARG_INFO(0, headerf)
  646. ZEND_ARG_INFO(0, headerv)
  647. ZEND_END_ARG_INFO()
  648. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_addrcpt, 0, 0, 1)
  649. ZEND_ARG_INFO(0, rcpt)
  650. ZEND_END_ARG_INFO()
  651. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_delrcpt, 0, 0, 1)
  652. ZEND_ARG_INFO(0, rcpt)
  653. ZEND_END_ARG_INFO()
  654. ZEND_BEGIN_ARG_INFO_EX(arginfo_smfi_replacebody, 0, 0, 1)
  655. ZEND_ARG_INFO(0, body)
  656. ZEND_END_ARG_INFO()
  657. /* }}} */
  658. /* {{{ milter_functions[]
  659. */
  660. const static zend_function_entry milter_functions[] = {
  661. PHP_FE(smfi_setflags, arginfo_smfi_setflags)
  662. PHP_FE(smfi_settimeout, arginfo_smfi_settimeout)
  663. PHP_FE(smfi_getsymval, arginfo_smfi_getsymval)
  664. PHP_FE(smfi_setreply, arginfo_smfi_setreply)
  665. PHP_FE(smfi_addheader, arginfo_smfi_addheader)
  666. PHP_FE(smfi_chgheader, arginfo_smfi_chgheader)
  667. PHP_FE(smfi_addrcpt, arginfo_smfi_addrcpt)
  668. PHP_FE(smfi_delrcpt, arginfo_smfi_delrcpt)
  669. PHP_FE(smfi_replacebody, arginfo_smfi_replacebody)
  670. PHP_FE_END
  671. };
  672. /* }}} */
  673. /* {{{ Zend module entry
  674. */
  675. static zend_module_entry php_milter_module = {
  676. STANDARD_MODULE_HEADER,
  677. "Milter",
  678. milter_functions,
  679. PHP_MINIT(milter),
  680. NULL,
  681. NULL,
  682. NULL,
  683. PHP_MINFO(milter),
  684. "0.1.0",
  685. STANDARD_MODULE_PROPERTIES
  686. };
  687. /* }}} */
  688. /* {{{ Milter SAPI
  689. */
  690. static int sapi_milter_ub_write(const char *str, uint str_length TSRMLS_DC)
  691. {
  692. return str_length;
  693. }
  694. static void sapi_milter_flush(void *server_context)
  695. {
  696. }
  697. static void sapi_milter_register_variables(zval *track_vars_array TSRMLS_DC)
  698. {
  699. php_register_variable ("SERVER_SOFTWARE", "Sendmail Milter", track_vars_array TSRMLS_CC);
  700. }
  701. static int sapi_milter_post_read(char *buf, uint count_bytes TSRMLS_DC)
  702. {
  703. return 0;
  704. }
  705. static char* sapi_milter_read_cookies(TSRMLS_D)
  706. {
  707. return NULL;
  708. }
  709. static int sapi_milter_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  710. {
  711. return SAPI_HEADER_SENT_SUCCESSFULLY;
  712. }
  713. static int php_milter_startup(sapi_module_struct *sapi_module)
  714. {
  715. if (php_module_startup(sapi_module, &php_milter_module, 1) == FAILURE) {
  716. return FAILURE;
  717. }
  718. return SUCCESS;
  719. }
  720. /* }}} */
  721. /* {{{ sapi_module_struct milter_sapi_module
  722. */
  723. static sapi_module_struct milter_sapi_module = {
  724. "milter", /* name */
  725. "Sendmail Milter SAPI", /* pretty name */
  726. php_milter_startup, /* startup */
  727. php_module_shutdown_wrapper, /* shutdown */
  728. NULL, /* activate */
  729. NULL, /* deactivate */
  730. sapi_milter_ub_write, /* unbuffered write */
  731. sapi_milter_flush, /* flush */
  732. NULL, /* get uid */
  733. NULL, /* getenv */
  734. php_error, /* error handler */
  735. NULL, /* header handler */
  736. sapi_milter_send_headers, /* send headers handler */
  737. NULL, /* send header handler */
  738. sapi_milter_post_read, /* read POST data */
  739. sapi_milter_read_cookies, /* read Cookies */
  740. sapi_milter_register_variables, /* register server variables */
  741. NULL, /* Log message */
  742. NULL, /* Get request time */
  743. NULL, /* Child terminate */
  744. NULL, /* Block interruptions */
  745. NULL, /* Unblock interruptions */
  746. STANDARD_SAPI_MODULE_PROPERTIES
  747. };
  748. /* }}} */
  749. /****
  750. * ripped from cli, has to be cleaned up !
  751. */
  752. /* {{{ php_milter_usage
  753. */
  754. static void php_milter_usage(char *argv0)
  755. {
  756. char *prog;
  757. prog = strrchr(argv0, '/');
  758. if (prog) {
  759. prog++;
  760. } else {
  761. prog = "php-milter";
  762. }
  763. printf( "Usage: %s [options] [-f] <file> [args...]\n"
  764. " %s [options] [-- args...]\n"
  765. " -a Run interactively\n"
  766. " -c <path>|<file> Look for php.ini file in this directory\n"
  767. " -n No php.ini file will be used\n"
  768. " -d foo[=bar] Define INI entry foo with value 'bar'\n"
  769. " -D run as daemon\n"
  770. " -e Generate extended information for debugger/profiler\n"
  771. " -f <file> Parse <file>.\n"
  772. " -h This help\n"
  773. " -p <socket> path to create socket\n"
  774. " -v Version number\n"
  775. " -V <n> set debug level to n (1 or 2).\n"
  776. " -z <file> Load Zend extension <file>.\n"
  777. " args... Arguments passed to script. Use -- args when first argument \n"
  778. " starts with - or script is read from stdin\n"
  779. , prog, prog);
  780. }
  781. /* }}} */
  782. static void define_command_line_ini_entry(char *arg) /* {{{ */
  783. {
  784. char *name, *value;
  785. name = arg;
  786. value = strchr(arg, '=');
  787. if (value) {
  788. *value = 0;
  789. value++;
  790. } else {
  791. value = "1";
  792. }
  793. zend_alter_ini_entry(name, strlen(name)+1, value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  794. }
  795. /* }}} */
  796. /* {{{ main
  797. */
  798. int main(int argc, char *argv[])
  799. {
  800. char *sock = NULL;
  801. int dofork = 0;
  802. int exit_status = SUCCESS;
  803. int c;
  804. /* temporary locals */
  805. int orig_optind=ap_php_optind;
  806. char *orig_optarg=ap_php_optarg;
  807. int interactive=0;
  808. char *param_error=NULL;
  809. /* end of temporary locals */
  810. void ***tsrm_ls;
  811. #ifdef HAVE_SIGNAL_H
  812. #if defined(SIGPIPE) && defined(SIG_IGN)
  813. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  814. that sockets created via fsockopen()
  815. don't kill PHP if the remote site
  816. closes it. in apache|apxs mode apache
  817. does that for us! thies@thieso.net
  818. 20000419 */
  819. #endif
  820. #endif
  821. tsrm_startup(1, 1, 0, NULL);
  822. tsrm_ls = ts_resource(0);
  823. sapi_startup(&milter_sapi_module);
  824. while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
  825. switch (c) {
  826. case 'c':
  827. milter_sapi_module.php_ini_path_override = strdup(ap_php_optarg);
  828. break;
  829. case 'n':
  830. milter_sapi_module.php_ini_ignore = 1;
  831. break;
  832. }
  833. }
  834. ap_php_optind = orig_optind;
  835. ap_php_optarg = orig_optarg;
  836. milter_sapi_module.executable_location = argv[0];
  837. sapi_module.startup(&milter_sapi_module);
  838. zend_first_try {
  839. while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
  840. switch (c) {
  841. case '?':
  842. php_output_tearup();
  843. SG(headers_sent) = 1;
  844. php_milter_usage(argv[0]);
  845. php_output_teardown();
  846. exit(1);
  847. break;
  848. }
  849. }
  850. ap_php_optind = orig_optind;
  851. ap_php_optarg = orig_optarg;
  852. /* Set some CLI defaults */
  853. SG(options) |= SAPI_OPTION_NO_CHDIR;
  854. zend_alter_ini_entry("html_errors", 12, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  855. zend_alter_ini_entry("max_execution_time", 19, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  856. zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
  857. while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) {
  858. switch (c) {
  859. case 'a': /* interactive mode */
  860. printf("Interactive mode enabled\n\n");
  861. interactive=1;
  862. break;
  863. case 'C': /* don't chdir to the script directory */
  864. /* This is default so NOP */
  865. break;
  866. case 'd': /* define ini entries on command line */
  867. define_command_line_ini_entry(ap_php_optarg);
  868. break;
  869. case 'D': /* daemon */
  870. dofork = 1;
  871. break;
  872. case 'e': /* enable extended info output */
  873. CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
  874. break;
  875. case 'f': /* parse file */
  876. filename = ap_php_optarg;
  877. break;
  878. case 'h': /* help & quit */
  879. case '?':
  880. php_output_tearup();
  881. SG(headers_sent) = 1;
  882. php_milter_usage(argv[0]);
  883. php_output_teardown();
  884. exit(1);
  885. break;
  886. case 'p': /* socket */
  887. sock = strdup(ap_php_optarg);
  888. break;
  889. case 'v': /* show php version & quit */
  890. if (php_request_startup(TSRMLS_C)==FAILURE) {
  891. zend_ini_deactivate(TSRMLS_C);
  892. php_module_shutdown(TSRMLS_C);
  893. sapi_shutdown();
  894. tsrm_shutdown();
  895. exit(1);
  896. }
  897. SG(headers_sent) = 1;
  898. SG(request_info).no_headers = 1;
  899. php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
  900. php_output_teardown();
  901. exit(1);
  902. break;
  903. case 'V': /* verbose */
  904. flag_debug = atoi(ap_php_optarg);
  905. break;
  906. case 'z': /* load extension file */
  907. zend_load_extension(ap_php_optarg);
  908. break;
  909. default:
  910. break;
  911. }
  912. }
  913. if (param_error) {
  914. SG(headers_sent) = 1;
  915. SG(request_info).no_headers = 1;
  916. PUTS(param_error);
  917. exit(1);
  918. }
  919. CG(interactive) = interactive;
  920. /* only set script_file if not set already and not in direct mode and not at end of parameter list */
  921. if (argc > ap_php_optind && !filename) {
  922. filename=argv[ap_php_optind];
  923. ap_php_optind++;
  924. }
  925. /* check if file exists, exit else */
  926. if (dofork) {
  927. switch(fork()) {
  928. case -1: /* Uh-oh, we have a problem forking. */
  929. fprintf(stderr, "Uh-oh, couldn't fork!\n");
  930. exit(errno);
  931. break;
  932. case 0: /* Child */
  933. break;
  934. default: /* Parent */
  935. exit(0);
  936. }
  937. }
  938. if (sock) {
  939. struct stat junk;
  940. if (stat(sock,&junk) == 0) unlink(sock);
  941. }
  942. openlog("php-milter", LOG_PID, LOG_MAIL);
  943. if ((exit_status = mlfi_init())) {
  944. syslog(1, "mlfi_init failed.");
  945. closelog();
  946. goto err;
  947. }
  948. smfi_setconn(sock);
  949. if (smfi_register(smfilter) == MI_FAILURE) {
  950. syslog(1, "smfi_register failed.");
  951. fprintf(stderr, "smfi_register failed\n");
  952. closelog();
  953. goto err;
  954. } else {
  955. exit_status = smfi_main();
  956. }
  957. closelog();
  958. if (milter_sapi_module.php_ini_path_override) {
  959. free(milter_sapi_module.php_ini_path_override);
  960. }
  961. } zend_catch {
  962. exit_status = EG(exit_status);
  963. } zend_end_try();
  964. err:
  965. php_module_shutdown(TSRMLS_C);
  966. sapi_shutdown();
  967. tsrm_shutdown();
  968. exit(exit_status);
  969. }
  970. /* }}} */
  971. /*
  972. * Local variables:
  973. * tab-width: 4
  974. * c-basic-offset: 4
  975. * End:
  976. * vim600: sw=4 ts=4 fdm=marker
  977. * vim<600: sw=4 ts=4
  978. */