milterfrom.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Milterfrom
  3. *
  4. * Copyright (c) 2017, Max von Buelow
  5. * All rights reserved.
  6. * Contact: https://maxvonbuelow.de
  7. *
  8. * This file is part of the MilterFrom project.
  9. * https://github.com/magcks/milterfrom
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the copyright holder nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sysexits.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <pwd.h>
  42. #include <grp.h>
  43. #include <stdint.h>
  44. #include <syslog.h>
  45. #include "libmilter/mfapi.h"
  46. #include "libmilter/mfdef.h"
  47. struct mlfiPriv {
  48. int is_auth;
  49. char *env_from;
  50. size_t env_from_len;
  51. int reject;
  52. };
  53. #define MLFIPRIV ((struct mlfiPriv*)smfi_getpriv(ctx))
  54. #define VERSION "1.0.1"
  55. extern const char *__progname;
  56. static unsigned long mta_caps = 0;
  57. // Function to extract addresses from the header/envelope fields. If the field
  58. // contains a < with a subsequent >, the inner part is used. If not, the whole
  59. // header field is used. This allows matching "Max Mustermann
  60. // <max.mustermann@example.invalid>".
  61. const char *parse_address(const char *address, size_t *len)
  62. {
  63. size_t inlen = strlen(address);
  64. size_t pos_open = SIZE_MAX, pos_close = SIZE_MAX;
  65. size_t i;
  66. for (i = 0; i < inlen; ++i) {
  67. if (address[i] == '<') pos_open = i;
  68. else if (address[i] == '>') pos_close = i;
  69. }
  70. if (pos_open != SIZE_MAX && pos_close != SIZE_MAX && pos_open < pos_close) {
  71. *len = pos_close - pos_open - 1;
  72. return address + pos_open + 1;
  73. } else {
  74. *len = inlen;
  75. return address;
  76. }
  77. }
  78. void mlfi_cleanup(SMFICTX *ctx)
  79. {
  80. struct mlfiPriv *priv = MLFIPRIV;
  81. if (priv == NULL) return;
  82. free(priv->env_from);
  83. free(priv);
  84. smfi_setpriv(ctx, NULL);
  85. }
  86. sfsistat mlfi_envfrom(SMFICTX *ctx, char **envfrom)
  87. {
  88. struct mlfiPriv *priv;
  89. char *fromcp = NULL;
  90. // Allocate some private memory.
  91. priv = calloc(1, sizeof(*priv));
  92. if (priv == NULL) {
  93. return SMFIS_TEMPFAIL;
  94. }
  95. // Parse envelope from.
  96. size_t len = 0;
  97. const char *from = parse_address(*envfrom, &len);
  98. if (len == 0) {
  99. /* A 0 length from address means a "null reverse-path", which is valid per
  100. * RFC5321. */
  101. return SMFIS_CONTINUE;
  102. }
  103. fromcp = strndup(from, len);
  104. if (fromcp == NULL) {
  105. return SMFIS_TEMPFAIL;
  106. }
  107. // Set private values.
  108. priv->is_auth = smfi_getsymval(ctx, "{auth_type}") ? 1 : 0;
  109. priv->env_from = fromcp;
  110. priv->env_from_len = len;
  111. priv->reject = 0;
  112. smfi_setpriv(ctx, priv);
  113. return SMFIS_CONTINUE;
  114. }
  115. sfsistat mlfi_header(SMFICTX *ctx, char *headerf, char *headerv)
  116. {
  117. struct mlfiPriv *priv = MLFIPRIV;
  118. if (priv == NULL) return SMFIS_CONTINUE;
  119. // Perform checks if the sender is authenticated and the message is not rejected yet (the mail may contain multiple from tags, all have to match!).
  120. if (priv->is_auth && !priv->reject) {
  121. if (strcasecmp(headerf, "from") == 0) {
  122. size_t len = 0;
  123. const char *from = parse_address(headerv, &len);
  124. // Check whether header from matches envelope from and reject if not.
  125. if (len != priv->env_from_len || strncasecmp(from, priv->env_from, len) != 0) {
  126. priv->reject = 1;
  127. syslog(LOG_NOTICE,"Envelope From (%s) and Header From (%s) mismatch ", priv->env_from, from);
  128. }
  129. }
  130. }
  131. return ((mta_caps & SMFIP_NR_HDR) != 0) ? SMFIS_NOREPLY : SMFIS_CONTINUE;
  132. }
  133. sfsistat mlfi_eom(SMFICTX *ctx)
  134. {
  135. struct mlfiPriv *priv = MLFIPRIV;
  136. if (priv == NULL) return SMFIS_CONTINUE;
  137. if (priv->reject) {
  138. smfi_setreply(ctx, "550", "5.7.1", "Rejected due to unmatching envelope and header sender.");
  139. mlfi_cleanup(ctx);
  140. return SMFIS_REJECT;
  141. }
  142. mlfi_cleanup(ctx);
  143. return SMFIS_CONTINUE;
  144. }
  145. sfsistat mlfi_abort(SMFICTX *ctx)
  146. {
  147. mlfi_cleanup(ctx);
  148. return SMFIS_CONTINUE;
  149. }
  150. sfsistat mlfi_negotiate(SMFICTX *ctx, unsigned long f0, unsigned long f1, unsigned long f2, unsigned long f3, unsigned long *pf0, unsigned long *pf1, unsigned long *pf2, unsigned long *pf3)
  151. {
  152. *pf0 = 0;
  153. /* milter protocol steps: all but connect, HELO, RCPT */
  154. *pf1 = SMFIP_NOCONNECT | SMFIP_NOHELO | SMFIP_NORCPT;
  155. mta_caps = f1;
  156. if ((mta_caps & SMFIP_NR_HDR) != 0) *pf1 |= SMFIP_NR_HDR;
  157. *pf2 = 0;
  158. *pf3 = 0;
  159. return SMFIS_CONTINUE;
  160. }
  161. struct smfiDesc smfilter =
  162. {
  163. "Header from check", /* filter name */
  164. SMFI_VERSION, /* version code -- do not change */
  165. 0, /* flags */
  166. NULL, /* connection info filter */
  167. NULL, /* SMTP HELO command filter */
  168. mlfi_envfrom, /* envelope sender filter */
  169. NULL, /* envelope recipient filter */
  170. mlfi_header, /* header filter */
  171. NULL, /* end of header */
  172. NULL, /* body block filter */
  173. mlfi_eom, /* end of message */
  174. mlfi_abort, /* message aborted */
  175. NULL, /* connection cleanup */
  176. NULL, /* unknown/unimplemented SMTP commands */
  177. NULL, /* DATA command filter */
  178. mlfi_negotiate /* option negotiation at connection startup */
  179. };
  180. uid_t get_uid(const char *name)
  181. {
  182. struct passwd *pwd = getpwnam(name);
  183. return pwd == NULL ? -1 : pwd->pw_uid;
  184. }
  185. gid_t get_gid(const char *name)
  186. {
  187. struct group *grp = getgrnam(name);
  188. return grp == NULL ? -1 : grp->gr_gid;
  189. }
  190. static int usage(void) {
  191. fprintf(stderr,"%s: A Milter program version %s to reject emails that have a mismatch between Envelope Sender and email Header From fields for authenticated users. This prevents spoofing that is currently not possible with \"reject_authenticated_sender_login_mismatch\" in Postfix\n", __progname,VERSION);
  192. fprintf(stderr, "%s: usage: %s -s socketfile [options]\n"
  193. "\t-p pidfile \twrite process ID to pidfile name\n"
  194. "\t-d \tdaemonize to background and exit\n"
  195. "\t-u userid \tchange to specified userid\n"
  196. "\t-g groupid \tchange to specific groupid\n"
  197. "\t-v \tprint version number and terminate\n",
  198. __progname,__progname);
  199. return EX_USAGE;
  200. }
  201. int main(int argc, char **argv)
  202. {
  203. int c, daemonize = 0;
  204. uid_t uid = -1; gid_t gid = -1;
  205. mode_t um = -1;
  206. char *pidfilename = NULL, *sockname = NULL;
  207. FILE *pidfile = NULL;
  208. u_int mvmajor;
  209. u_int mvminor;
  210. u_int mvrelease;
  211. while ((c = getopt(argc, argv, "dhvs:p:u:g:m:")) != -1) {
  212. switch (c) {
  213. case 's':
  214. sockname = strdup(optarg);
  215. break;
  216. case 'p':
  217. pidfilename = strdup(optarg);
  218. break;
  219. case 'd':
  220. daemonize = 1;
  221. break;
  222. case 'u':
  223. uid = get_uid(optarg);
  224. break;
  225. case 'g':
  226. gid = get_gid(optarg);
  227. break;
  228. case 'm':
  229. um = strtol(optarg, 0, 8);
  230. break;
  231. case 'h':
  232. return usage();
  233. case 'v':
  234. fprintf(stderr,"%s: v%s\n", __progname, VERSION);
  235. fprintf(stderr,"\tSMFI_VERSION 0x%x\n", SMFI_VERSION);
  236. (void) smfi_version(&mvmajor, &mvminor, &mvrelease);
  237. fprintf(stderr,"\tlibmilter version %d.%d.%d\n",
  238. mvmajor, mvminor, mvrelease);
  239. return EX_USAGE;
  240. }
  241. }
  242. if (!sockname) {
  243. fprintf(stderr, "%s: Missing required -s argument\n", argv[0]);
  244. exit(EX_USAGE);
  245. }
  246. if (pidfilename) {
  247. unlink(pidfilename);
  248. pidfile = fopen(pidfilename, "w");
  249. if (!pidfile)
  250. {
  251. fprintf(stderr, "Could not open pidfile: %s\n", strerror(errno));
  252. exit(1);
  253. }
  254. free(pidfilename);
  255. }
  256. if (um != (mode_t)-1) umask(um);
  257. if (gid != (gid_t)-1) setgid(gid);
  258. if (uid != (uid_t)-1) setuid(uid);
  259. if (daemonize) {
  260. if (daemon(0, 0) == -1) {
  261. fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
  262. exit(EXIT_FAILURE);
  263. }
  264. }
  265. if (pidfile) {
  266. fprintf(pidfile, "%ld\n", (long)getpid());
  267. fclose(pidfile);
  268. }
  269. struct stat junk;
  270. if (stat(sockname, &junk) == 0) unlink(sockname);
  271. smfi_setconn(sockname);
  272. free(sockname);
  273. if (smfi_register(smfilter) == MI_FAILURE) {
  274. fprintf(stderr, "smfi_register failed\n");
  275. exit(EX_UNAVAILABLE);
  276. }
  277. openlog ("milterfrom", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_MAIL);
  278. return smfi_main();
  279. }