comparison src/context.cpp @ 321:e172dc10fe24

add dkim white/black listing
author Carl Byington <carl@five-ten-sg.com>
date Sat, 17 Dec 2016 13:47:28 -0800
parents e27c24c1974a
children 9f8411f3919c
comparison
equal deleted inserted replaced
320:e27c24c1974a 321:e172dc10fe24
67 const char *token_uribl; 67 const char *token_uribl;
68 const char *token_verify; 68 const char *token_verify;
69 const char *token_white; 69 const char *token_white;
70 const char *token_white_regex; 70 const char *token_white_regex;
71 const char *token_yes; 71 const char *token_yes;
72 const char *token_dkim_signer;
73 const char *token_dkim_from;
74 const char *token_signed_white;
75 const char *token_signed_black;
76 const char *token_require_signed;
72 77
73 const char *token_myhostname; 78 const char *token_myhostname;
74 #ifndef HOST_NAME_MAX 79 #ifndef HOST_NAME_MAX
75 #define HOST_NAME_MAX 255 80 #define HOST_NAME_MAX 255
76 #endif 81 #endif
605 610
606 DELAYWHITE::DELAYWHITE(const char *loto_, WHITELISTERP w_, CONTEXTP con_) { 611 DELAYWHITE::DELAYWHITE(const char *loto_, WHITELISTERP w_, CONTEXTP con_) {
607 loto = loto_; 612 loto = loto_;
608 w = w_; 613 w = w_;
609 con = con_; 614 con = con_;
615 }
616
617
618 DKIM::DKIM(const char *action_, const char *signer_) {
619 action = action_;
620 signer = signer_;
610 } 621 }
611 622
612 623
613 DNSBL::DNSBL(const char *n, const char *s, const char *m) { 624 DNSBL::DNSBL(const char *n, const char *s, const char *m) {
614 name = n; 625 name = n;
761 daily_address_multiple = 3; 772 daily_address_multiple = 3;
762 } 773 }
763 774
764 775
765 CONTEXT::~CONTEXT() { 776 CONTEXT::~CONTEXT() {
777 for (dkimp_map::iterator i=dkim_from_names.begin(); i!=dkim_from_names.end(); i++) {
778 DKIMP d = (*i).second;
779 // delete the underlying DKIM objects.
780 delete d;
781 }
766 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { 782 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
767 DNSBLP d = (*i).second; 783 DNSBLP d = (*i).second;
768 // delete the underlying DNSBL objects. 784 // delete the underlying DNSBL objects.
769 delete d; 785 delete d;
770 } 786 }
983 if (i != children.end()) return (*i).second; 999 if (i != children.end()) return (*i).second;
984 return NULL; 1000 return NULL;
985 } 1001 }
986 1002
987 1003
1004 const char *CONTEXT::find_dkim_signer(const char *name) {
1005 string_map::iterator i = dkim_signer_names.find(name);
1006 if (i != dkim_signer_names.end()) return (*i).second;
1007 if (parent) return parent->find_dkim_signer(name);
1008 return NULL;
1009 }
1010
1011
1012 DKIMP CONTEXT::find_dkim_from(const char *name) {
1013 dkimp_map::iterator i = dkim_from_names.find(name);
1014 if (i != dkim_from_names.end()) return (*i).second;
1015 if (parent) return parent->find_dkim_from(name);
1016 return NULL;
1017 }
1018
1019
988 DNSBLP CONTEXT::find_dnsbl(const char *name) { 1020 DNSBLP CONTEXT::find_dnsbl(const char *name) {
989 dnsblp_map::iterator i = dnsbl_names.find(name); 1021 dnsblp_map::iterator i = dnsbl_names.find(name);
990 if (i != dnsbl_names.end()) return (*i).second; 1022 if (i != dnsbl_names.end()) return (*i).second;
991 if (parent) return parent->find_dnsbl(name); 1023 if (parent) return parent->find_dnsbl(name);
992 return NULL; 1024 return NULL;
1196 spamass |= (spamassassin_limit != 0); 1228 spamass |= (spamassassin_limit != 0);
1197 } 1229 }
1198 else { 1230 else {
1199 printf("%s content off {}; \n", indent); 1231 printf("%s content off {}; \n", indent);
1200 } 1232 }
1233
1234 printf("%s dkim_signer { \n", indent);
1235 for (string_map::iterator i=dkim_signer_names.begin(); i!=dkim_signer_names.end(); i++) {
1236 const char *n = (*i).first;
1237 const char *a = (*i).second;
1238 printf("%s %s %s; \n", indent, n, a);
1239 }
1240 printf("%s } \n", indent);
1241
1242 printf("%s dkim_from { \n", indent);
1243 for (dkimp_map::iterator i=dkim_from_names.begin(); i!=dkim_from_names.end(); i++) {
1244 const char *n = (*i).first;
1245 DKIM &d = *(*i).second;
1246 printf("%s %s %s %s; \n", indent, n, d.action, d.signer);
1247 }
1248
1249 printf("%s } \n", indent);
1201 1250
1202 printf("%s env_to { \t// %s\n", indent, fullname); 1251 printf("%s env_to { \t// %s\n", indent, fullname);
1203 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { 1252 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) {
1204 printf("%s %s; \n", indent, *i); 1253 printf("%s %s; \n", indent, *i);
1205 } 1254 }
1808 } 1857 }
1809 1858
1810 1859
1811 //////////////////////////////////////////////// 1860 ////////////////////////////////////////////////
1812 // 1861 //
1862 bool parse_dkim_signer(TOKEN &tok, CONFIG &dc, CONTEXT &me);
1863 bool parse_dkim_signer(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
1864 if (!tsa(tok, token_lbrace)) return false;
1865 while (true) {
1866 const char *have = tok.next();
1867 if (!have) break;
1868 if (have == token_rbrace) break;
1869 if (have == token_semi) {
1870 // optional separators
1871 }
1872 else {
1873 const char *signer = have;
1874 const char *action = tok.next();
1875 if ((action == token_white) || (action == token_black)) {
1876 me.add_dkim_signer(signer, action);
1877 }
1878 else {
1879 tok.token_error("white/black", action);
1880 }
1881 }
1882 }
1883 return tsa(tok, token_semi);
1884 }
1885
1886
1887 ////////////////////////////////////////////////
1888 //
1889 bool parse_dkim_from(TOKEN &tok, CONFIG &dc, CONTEXT &me);
1890 bool parse_dkim_from(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
1891 if (!tsa(tok, token_lbrace)) return false;
1892 while (true) {
1893 const char *have = tok.next();
1894 if (!have) break;
1895 if (have == token_rbrace) break;
1896 if (have == token_semi) {
1897 // optional separators
1898 }
1899 else {
1900 const char *from = have;
1901 const char *action = tok.next();
1902 if ((action == token_signed_white) || (action == token_signed_black) || (action == token_require_signed)) {
1903 const char *signer = tok.next();
1904 if (!signer) break;
1905 else me.add_dkim_from(from, action, signer);
1906 }
1907 else {
1908 tok.token_error("signed_white/signed_black/require_signed", action);
1909 }
1910 }
1911 }
1912 return tsa(tok, token_semi);
1913 }
1914
1915
1916 ////////////////////////////////////////////////
1917 //
1813 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); 1918 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent);
1814 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { 1919 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) {
1815 const char *name = tok.next(); 1920 const char *name = tok.next();
1816 if (!tsa(tok, token_lbrace)) return false; 1921 if (!tsa(tok, token_lbrace)) return false;
1817 CONTEXTP con = new CONTEXT(parent, name); 1922 CONTEXTP con = new CONTEXT(parent, name);
1830 if (!parse_dnswl(tok, dc, *con)) return false; 1935 if (!parse_dnswl(tok, dc, *con)) return false;
1831 } 1936 }
1832 else if (have == token_dnswll) { 1937 else if (have == token_dnswll) {
1833 if (!parse_dnswll(tok, dc, *con)) return false; 1938 if (!parse_dnswll(tok, dc, *con)) return false;
1834 } 1939 }
1835 else if (have == token_requirerdns) {
1836 if (!parse_requirerdns(tok, dc, *con)) return false;
1837 }
1838 else if (have == token_content) { 1940 else if (have == token_content) {
1839 if (!parse_content(tok, dc, *con)) return false; 1941 if (!parse_content(tok, dc, *con)) return false;
1840 } 1942 }
1841 else if (have == token_envto) { 1943 else if (have == token_envto) {
1842 if (!parse_envto(tok, dc, *con)) return false; 1944 if (!parse_envto(tok, dc, *con)) return false;
1853 else if (have == token_autowhite) { 1955 else if (have == token_autowhite) {
1854 if (!parse_autowhite(tok, dc, *con)) return false; 1956 if (!parse_autowhite(tok, dc, *con)) return false;
1855 } 1957 }
1856 else if (have == token_envfrom) { 1958 else if (have == token_envfrom) {
1857 if (!parse_envfrom(tok, dc, *con)) return false; 1959 if (!parse_envfrom(tok, dc, *con)) return false;
1960 }
1961 else if (have == token_dkim_signer) {
1962 if (!parse_dkim_signer(tok, dc, *con)) return false;
1963 }
1964 else if (have == token_dkim_from) {
1965 if (!parse_dkim_from(tok, dc, *con)) return false;
1858 } 1966 }
1859 else if (have == token_rate) { 1967 else if (have == token_rate) {
1860 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context"); 1968 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context");
1861 if (!parse_rate(tok, dc, *con)) return false; 1969 if (!parse_rate(tok, dc, *con)) return false;
1970 }
1971 else if (have == token_requirerdns) {
1972 if (!parse_requirerdns(tok, dc, *con)) return false;
1862 } 1973 }
1863 else if (have == token_context) { 1974 else if (have == token_context) {
1864 if (!parse_context(tok, dc, con)) return false; 1975 if (!parse_context(tok, dc, con)) return false;
1865 } 1976 }
1866 else { 1977 else {
1907 2018
1908 //////////////////////////////////////////////// 2019 ////////////////////////////////////////////////
1909 // init the tokens 2020 // init the tokens
1910 // 2021 //
1911 void token_init() { 2022 void token_init() {
1912 token_asterisk = register_string("*"); 2023 token_asterisk = register_string("*");
1913 token_autowhite = register_string("autowhite"); 2024 token_autowhite = register_string("autowhite");
1914 token_bang = register_string("!"); 2025 token_bang = register_string("!");
1915 token_black = register_string("black"); 2026 token_black = register_string("black");
1916 token_content = register_string("content"); 2027 token_content = register_string("content");
1917 token_context = register_string("context"); 2028 token_context = register_string("context");
1918 token_dccbulk = register_string("dcc_bulk_threshold"); 2029 token_dccbulk = register_string("dcc_bulk_threshold");
1919 token_dccfrom = register_string("dcc_from"); 2030 token_dccfrom = register_string("dcc_from");
1920 token_dccgrey = register_string("dcc_greylist"); 2031 token_dccgrey = register_string("dcc_greylist");
1921 token_dccto = register_string("dcc_to"); 2032 token_dccto = register_string("dcc_to");
1922 token_default = register_string("default"); 2033 token_default = register_string("default");
1923 token_dnsbl = register_string("dnsbl"); 2034 token_dnsbl = register_string("dnsbl");
1924 token_dnsbll = register_string("dnsbl_list"); 2035 token_dnsbll = register_string("dnsbl_list");
1925 token_dnswl = register_string("dnswl"); 2036 token_dnswl = register_string("dnswl");
1926 token_dnswll = register_string("dnswl_list"); 2037 token_dnswll = register_string("dnswl_list");
1927 token_envfrom = register_string("env_from"); 2038 token_envfrom = register_string("env_from");
1928 token_envto = register_string("env_to"); 2039 token_envto = register_string("env_to");
1929 token_filter = register_string("filter"); 2040 token_filter = register_string("filter");
1930 token_generic = register_string("generic"); 2041 token_generic = register_string("generic");
1931 token_host_limit = register_string("host_limit"); 2042 token_host_limit = register_string("host_limit");
1932 token_html_limit = register_string("html_limit"); 2043 token_html_limit = register_string("html_limit");
1933 token_html_tags = register_string("html_tags"); 2044 token_html_tags = register_string("html_tags");
1934 token_ignore = register_string("ignore"); 2045 token_ignore = register_string("ignore");
1935 token_include = register_string("include"); 2046 token_include = register_string("include");
1936 token_inherit = register_string("inherit"); 2047 token_inherit = register_string("inherit");
1937 token_lbrace = register_string("{"); 2048 token_lbrace = register_string("{");
1938 token_mailhost = register_string("mail_host"); 2049 token_mailhost = register_string("mail_host");
1939 token_many = register_string("many"); 2050 token_many = register_string("many");
1940 token_no = register_string("no"); 2051 token_no = register_string("no");
1941 token_off = register_string("off"); 2052 token_off = register_string("off");
1942 token_ok = register_string("ok"); 2053 token_ok = register_string("ok");
1943 token_ok2 = register_string("ok2"); 2054 token_ok2 = register_string("ok2");
1944 token_on = register_string("on"); 2055 token_on = register_string("on");
1945 token_period = register_string("."); 2056 token_period = register_string(".");
1946 token_rate = register_string("rate_limit"); 2057 token_rate = register_string("rate_limit");
1947 token_rbrace = register_string("}"); 2058 token_rbrace = register_string("}");
1948 token_require = register_string("require_match"); 2059 token_require = register_string("require_match");
1949 token_requirerdns = register_string("require_rdns"); 2060 token_requirerdns = register_string("require_rdns");
1950 token_semi = register_string(";"); 2061 token_semi = register_string(";");
1951 token_soft = register_string("soft"); 2062 token_soft = register_string("soft");
1952 token_spamassassin = register_string("spamassassin"); 2063 token_spamassassin = register_string("spamassassin");
1953 token_substitute = register_string("substitute"); 2064 token_substitute = register_string("substitute");
1954 token_tld = register_string("tld"); 2065 token_tld = register_string("tld");
1955 token_unknown = register_string("unknown"); 2066 token_unknown = register_string("unknown");
1956 token_uribl = register_string("uribl"); 2067 token_uribl = register_string("uribl");
1957 token_verify = register_string("verify"); 2068 token_verify = register_string("verify");
1958 token_white = register_string("white"); 2069 token_white = register_string("white");
1959 token_white_regex = register_string("white_regex"); 2070 token_white_regex = register_string("white_regex");
1960 token_yes = register_string("yes"); 2071 token_yes = register_string("yes");
2072 token_dkim_signer = register_string("dkim_signer");
2073 token_dkim_from = register_string("dkim_from");
2074 token_signed_white = register_string("signed_white");
2075 token_signed_black = register_string("signed_black");
2076 token_require_signed = register_string("require_signed");
1961 2077
1962 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { 2078 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) {
1963 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); 2079 strncpy(myhostname, "localhost", HOST_NAME_MAX+1);
1964 } 2080 }
1965 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination 2081 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination