milterfrom.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "libmilter/mfapi.h"
  44. #include "libmilter/mfdef.h"
  45. struct mlfiPriv {
  46. int is_auth;
  47. char *env_from;
  48. int env_from_len;
  49. int reject;
  50. };
  51. #define MLFIPRIV ((struct mlfiPriv*)smfi_getpriv(ctx))
  52. static unsigned long mta_caps = 0;
  53. // Function to extract addresses from the header/envelope fields.
  54. // If the field contains a < with a subsequent >, the inner part is used. If not, the whole header field is used. This allows matching "Max Mustermann <max.mustermann@example.invalid>" matching.
  55. const char *parse_address(const char *address, int *len)
  56. {
  57. int inlen = strlen(address);
  58. int pos_open = -1, pos_close = -1;
  59. int i;
  60. for (i = 0; i < inlen; ++i) {
  61. if (address[i] == '<') pos_open = i;
  62. else if (address[i] == '>') pos_close = i;
  63. }
  64. if (pos_open != -1 && pos_close != -1 && pos_open < pos_close) {
  65. *len = pos_close - pos_open - 1;
  66. return address + pos_open + 1;
  67. } else {
  68. *len = inlen;
  69. return address;
  70. }
  71. }
  72. void mlfi_cleanup(SMFICTX *ctx)
  73. {
  74. struct mlfiPriv *priv = MLFIPRIV;
  75. if (priv == NULL) return;
  76. free(priv->env_from);
  77. free(priv);
  78. smfi_setpriv(ctx, NULL);
  79. }
  80. sfsistat mlfi_envfrom(SMFICTX *ctx, char **envfrom)
  81. {
  82. struct mlfiPriv *priv;
  83. // Allocate some private memory.
  84. priv = malloc(sizeof *priv);
  85. if (priv == NULL) return SMFIS_TEMPFAIL;
  86. memset(priv, '\0', sizeof *priv);
  87. // Parse envelope from.
  88. int len;
  89. const char *from = parse_address(*envfrom, &len);
  90. char *fromcp = strndup(from, len);
  91. if (fromcp == NULL) return SMFIS_TEMPFAIL;
  92. // Set private values.
  93. priv->is_auth = smfi_getsymval(ctx, "{auth_type}") ? 1 : 0;
  94. priv->env_from = fromcp;
  95. priv->env_from_len = len;
  96. priv->reject = 0;
  97. smfi_setpriv(ctx, priv);
  98. return SMFIS_CONTINUE;
  99. }
  100. sfsistat mlfi_header(SMFICTX *ctx, char *headerf, char *headerv)
  101. {
  102. struct mlfiPriv *priv = MLFIPRIV;
  103. // 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!).
  104. if (priv->is_auth && !priv->reject) {
  105. if (strcasecmp(headerf, "from") == 0) {
  106. int len;
  107. const char *from = parse_address(headerv, &len);
  108. // Check whether header from matches envelope from and reject if not.
  109. if (len != priv->env_from_len || strncasecmp(from, priv->env_from, len) != 0) priv->reject = 1;
  110. }
  111. }
  112. return ((mta_caps & SMFIP_NR_HDR) != 0) ? SMFIS_NOREPLY : SMFIS_CONTINUE;
  113. }
  114. sfsistat mlfi_eom(SMFICTX *ctx)
  115. {
  116. struct mlfiPriv *priv = MLFIPRIV;
  117. if (priv->reject) {
  118. smfi_setreply(ctx, "550", "5.7.1", "Rejected due to unmatching envelope and header sender.");
  119. mlfi_cleanup(ctx);
  120. return SMFIS_REJECT;
  121. }
  122. mlfi_cleanup(ctx);
  123. return SMFIS_CONTINUE;
  124. }
  125. sfsistat mlfi_abort(SMFICTX *ctx)
  126. {
  127. mlfi_cleanup(ctx);
  128. return SMFIS_CONTINUE;
  129. }
  130. 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)
  131. {
  132. *pf0 = 0;
  133. /* milter protocol steps: all but connect, HELO, RCPT */
  134. *pf1 = SMFIP_NOCONNECT | SMFIP_NOHELO | SMFIP_NORCPT;
  135. mta_caps = f1;
  136. if ((mta_caps & SMFIP_NR_HDR) != 0) *pf1 |= SMFIP_NR_HDR;
  137. *pf2 = 0;
  138. *pf3 = 0;
  139. return SMFIS_CONTINUE;
  140. }
  141. struct smfiDesc smfilter =
  142. {
  143. "Header from check", /* filter name */
  144. SMFI_VERSION, /* version code -- do not change */
  145. 0, /* flags */
  146. NULL, /* connection info filter */
  147. NULL, /* SMTP HELO command filter */
  148. mlfi_envfrom, /* envelope sender filter */
  149. NULL, /* envelope recipient filter */
  150. mlfi_header, /* header filter */
  151. NULL, /* end of header */
  152. NULL, /* body block filter */
  153. mlfi_eom, /* end of message */
  154. mlfi_abort, /* message aborted */
  155. NULL, /* connection cleanup */
  156. NULL, /* unknown/unimplemented SMTP commands */
  157. NULL, /* DATA command filter */
  158. mlfi_negotiate /* option negotiation at connection startup */
  159. };
  160. uid_t get_uid(const char *name)
  161. {
  162. struct passwd *pwd = getpwnam(name);
  163. return pwd == NULL ? -1 : pwd->pw_uid;
  164. }
  165. gid_t get_gid(const char *name)
  166. {
  167. struct group *grp = getgrnam(name);
  168. return grp == NULL ? -1 : grp->gr_gid;
  169. }
  170. int main(int argc, char **argv)
  171. {
  172. int c, daemonize = 0;
  173. uid_t uid = -1; gid_t gid = -1;
  174. mode_t um = -1;
  175. char *pidfilename = NULL, *sockname = NULL;
  176. FILE *pidfile = NULL;
  177. while ((c = getopt(argc, argv, "ds:p:u:g:m:")) != -1) {
  178. switch (c) {
  179. case 's':
  180. sockname = strdup(optarg);
  181. break;
  182. case 'p':
  183. pidfilename = strdup(optarg);
  184. break;
  185. case 'd':
  186. daemonize = 1;
  187. break;
  188. case 'u':
  189. uid = get_uid(optarg);
  190. break;
  191. case 'g':
  192. gid = get_gid(optarg);
  193. break;
  194. case 'm':
  195. um = strtol(optarg, 0, 8);
  196. break;
  197. }
  198. }
  199. if (!sockname) {
  200. fprintf(stderr, "%s: Missing required -s argument\n", argv[0]);
  201. exit(EX_USAGE);
  202. }
  203. if (pidfilename) {
  204. unlink(pidfilename);
  205. pidfile = fopen(pidfilename, "w");
  206. if (!pidfile)
  207. {
  208. fprintf(stderr, "Could not open pidfile: %s\n", strerror(errno));
  209. exit(1);
  210. }
  211. free(pidfilename);
  212. }
  213. if (um != (mode_t)-1) umask(um);
  214. if (gid != (gid_t)-1) setgid(gid);
  215. if (uid != (uid_t)-1) setuid(uid);
  216. if (daemonize) {
  217. if (daemon(0, 0) == -1) {
  218. fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
  219. exit(EXIT_FAILURE);
  220. }
  221. }
  222. if (pidfile) {
  223. fprintf(pidfile, "%ld\n", (long)getpid());
  224. fclose(pidfile);
  225. }
  226. struct stat junk;
  227. if (stat(sockname, &junk) == 0) unlink(sockname);
  228. smfi_setconn(sockname);
  229. free(sockname);
  230. if (smfi_register(smfilter) == MI_FAILURE) {
  231. fprintf(stderr, "smfi_register failed\n");
  232. exit(EX_UNAVAILABLE);
  233. }
  234. return smfi_main();
  235. }