From c70f5d055d37bba1b26beafec8bfee902dc1b758 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Thu, 3 Jan 2019 12:40:52 +0200 Subject: Version 8.7 * NEWS: Update. * configure.ac: Raise minor version. * doc/functions.texi: Document new functions Include the implementation of the NS resolving MFL functions, as proposed by Jan Rafaj * lib/dns.c (ns_lookup): New function. * lib/dns.h (ns_lookup): New proto. * mflib/dns.mf4 (hasns): New function. * src/builtin/dns.bi (primitive_hasns, getns): New functions. --- NEWS | 23 ++++++++++-- configure.ac | 4 +-- doc/functions.texi | 100 ++++++++++++++++++++++++++++++++++------------------- lib/dns.c | 30 ++++++++++++++++ lib/dns.h | 2 ++ mflib/dns.mf4 | 13 +++++++ src/builtin/dns.bi | 84 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 216 insertions(+), 40 deletions(-) diff --git a/NEWS b/NEWS index bc239bd3..86f6c7a9 100644 --- a/NEWS +++ b/NEWS @@ -1,9 +1,9 @@ -Mailfromd NEWS -- history of user-visible changes. 2018-11-12 +Mailfromd NEWS -- history of user-visible changes. 2019-01-03 See the end of file for copying conditions. Please send Mailfromd bug reports to -Version 8.6.90 (Git) +Version 8.7, 2019-01-03 * The --callout-socket option @@ -15,6 +15,25 @@ overrides. This option is used by mtasim to avoid clobbering the existing callout sockets when starting new mailfromd instance. +* NS lookup NFL functions + +This release implements the following new MFL functions: + +number primitive_hasns (string DOM) + Returns 1 if the domain DOM has at least one NS record and 0 + otherwise. Throws an error if DNS lookup fails. + +require 'dns' +number hasns (string DOM) + Same as above, but returns 0 on DNS lookup failures. + +string getns (string DOM ; number RESOLVE, number SORT) + + Returns a whitespace-separated list of all the NS records for + the domain DOM. If optional parameter RESOLVE is 1, the returned list + contains IP addresses. Optional SORT controls whether the entries are + sorted. + * Bugfixes ** Callout functions return true on checking the null return address (<>) ** Arguments in transaction between mailfromd and calloutd are quoted diff --git a/configure.ac b/configure.ac index 3585d967..54c9ebd8 100644 --- a/configure.ac +++ b/configure.ac @@ -16,8 +16,8 @@ AC_PREREQ(2.63) m4_define([MF_VERSION_MAJOR], 8) -m4_define([MF_VERSION_MINOR], 6) -m4_define([MF_VERSION_PATCH], 90) +m4_define([MF_VERSION_MINOR], 7) +dnl m4_define([MF_VERSION_PATCH], 0) AC_INIT([mailfromd], MF_VERSION_MAJOR.MF_VERSION_MINOR[]m4_ifdef([MF_VERSION_PATCH],.MF_VERSION_PATCH), [bug-mailfromd@gnu.org.ua], diff --git a/doc/functions.texi b/doc/functions.texi index 75eed904..d25911aa 100644 --- a/doc/functions.texi +++ b/doc/functions.texi @@ -5,7 +5,10 @@ @c Invariant Sections, with the Front and Back-Cover texts at your option. This chapter describes library functions available in Mailfromd -version @value{VERSION}. +version @value{VERSION}. For the simplicity of explanation, we use +the word @samp{boolean} to indicate variables of numeric type that are +used as boolean values. For such variables, the term @samp{False} +stands for the numeric 0, and @samp{True} for any non-zero value. @menu * Macro access:: @@ -65,7 +68,7 @@ macro names programmatically, e.g.: @end smallexample @end deftypefn -@deftypefn {Built-in Function} number macro_defined (string @var{name}) +@deftypefn {Built-in Function} boolean macro_defined (string @var{name}) Return true if Sendmail macro @var{name} is defined. @end deftypefn @@ -602,7 +605,7 @@ is returned otherwise. In the latter case, the global variable @code{ctype_mismatch} is set to the index of the first character that is outside of the character class (characters are indexed from 0). -@deftypefn {Built-in Function} number isalnum (string @var{str}) +@deftypefn {Built-in Function} boolean isalnum (string @var{str}) Checks for alphanumeric characters: @smallexample @@ -611,7 +614,7 @@ Checks for alphanumeric characters: @end smallexample @end deftypefn -@deftypefn {Built-in Function} number isalpha (string @var{str}) +@deftypefn {Built-in Function} boolean isalpha (string @var{str}) Checks for an alphabetic character: @smallexample @@ -620,7 +623,7 @@ Checks for an alphabetic character: @end smallexample @end deftypefn -@deftypefn {Built-in Function} number isascii (string @var{str}) +@deftypefn {Built-in Function} boolean isascii (string @var{str}) Checks whether all characters in @var{str} are 7-bit ones, that fit into the @acronym{ASCII} character set. @@ -630,47 +633,47 @@ the @acronym{ASCII} character set. @end smallexample @end deftypefn -@deftypefn {Built-in Function} number isblank (string @var{str}) +@deftypefn {Built-in Function} boolean isblank (string @var{str}) Checks if @var{str} contains only blank characters; that is, spaces or tabs. @end deftypefn -@deftypefn {Built-in Function} number iscntrl (string @var{str}) +@deftypefn {Built-in Function} boolean iscntrl (string @var{str}) Checks for control characters. @end deftypefn -@deftypefn {Built-in Function} number isdigit (string @var{str}) +@deftypefn {Built-in Function} boolean isdigit (string @var{str}) Checks for digits (0 through 9). @end deftypefn -@deftypefn {Built-in Function} number isgraph (string @var{str}) +@deftypefn {Built-in Function} boolean isgraph (string @var{str}) Checks for any printable characters except spaces. @end deftypefn -@deftypefn {Built-in Function} number islower (string @var{str}) +@deftypefn {Built-in Function} boolean islower (string @var{str}) Checks for lower-case characters. @end deftypefn -@deftypefn {Built-in Function} number isprint (string @var{str}) +@deftypefn {Built-in Function} boolean isprint (string @var{str}) Checks for printable characters including space. @end deftypefn -@deftypefn {Built-in Function} number ispunct (string @var{str}) +@deftypefn {Built-in Function} boolean ispunct (string @var{str}) Checks for any printable characters which are not a spaces or alphanumeric characters. @end deftypefn -@deftypefn {Built-in Function} number isspace (string @var{str}) +@deftypefn {Built-in Function} boolean isspace (string @var{str}) Checks for white-space characters, i.e.: space, form-feed (@samp{\f}), newline (@samp{\n}), carriage return (@samp{\r}), horizontal tab (@samp{\t}), and vertical tab (@samp{\v}). @end deftypefn -@deftypefn {Built-in Function} number isupper (string @var{str}) +@deftypefn {Built-in Function} boolean isupper (string @var{str}) Checks for uppercase letters. @end deftypefn -@deftypefn {Built-in Function} number isxdigit (string @var{str}) +@deftypefn {Built-in Function} boolean isxdigit (string @var{str}) Checks for hexadecimal digits, i.e. one of @samp{0}, @samp{1}, @samp{2}, @samp{3}, @samp{4}, @samp{5}, @samp{6}, @samp{7}, @samp{8}, @samp{9}, @samp{a}, @samp{b}, @samp{c}, @samp{d}, @samp{e}, @samp{f}, @@ -717,7 +720,7 @@ email address. @end deftypefn @deftypefn {Library Function} boolean email_valid (string @var{email}) - Returns @var{True} if @var{email} is a valid email address, + Returns @samp{True} (1) if @var{email} is a valid email address, consisting of local and domain parts only. E.g.: @smallexample @@ -1704,7 +1707,7 @@ compativbility. These functions are defined in the module @file{poll.mf}, which you must require prior to using any of them. -@deftypefn {Library Function} number _pollhost @ +@deftypefn {Library Function} boolean _pollhost @ (string @var{ip}, string @var{email}, string @var{domain}, @ string @var{mailfrom}) Poll @acronym{SMTP} host @var{ip} for email address @var{email}, @@ -1716,7 +1719,7 @@ main poll tempfails. The function can throw one of the following exceptions: @code{e_failure}, @code{e_temp_failure}. @end deftypefn -@deftypefn {Library Function} number _pollmx @ +@deftypefn {Library Function} boolean _pollmx @ (string @var{ip}, string @var{email}, string @var{domain}, @ string @var{mailfrom}) Poll @acronym{MX}s of the @var{domain} for email address @var{email}, using @@ -1728,7 +1731,7 @@ if the poll fails. The function can throw one of the following exceptions: @code{e_failure}, @code{e_temp_failure}. @end deftypefn -@deftypefn {Library Function} number stdpoll @ +@deftypefn {Library Function} boolean stdpoll @ (string @var{email}, string @var{domain}, string @var{mailfrom}) Performs standard poll for @var{email}, using @var{domain} as @@ -1740,7 +1743,7 @@ the following exceptions: @code{e_failure}, @code{e_temp_failure}. without explicit @var{host}. @FIXME{more details and references.} @end deftypefn -@deftypefn {Library Function} number strictpoll @ +@deftypefn {Library Function} boolean strictpoll @ (string @var{host}, string @var{email}, @ string @var{domain}, string @var{mailfrom}) @@ -1925,7 +1928,7 @@ records) for the @acronym{IP}v4 address @var{ipstr}. @end deftypefn @deftypefn {Built-in Function} string getmx (string @var{domain} @ - [, number @var{ip}]) + [, boolean @var{ip}]) Returns a whitespace-separated list of @samp{MX} names (if @var{ip} is not given or if it is @code{0}) or @samp{MX} @acronym{IP} addresses (if @code{@var{ip}!=0})) for @var{domain}. Within the returned @@ -2114,6 +2117,30 @@ record are compared against @var{ip}. The function returns true if a matching A record is found. @end deftypefn +@deftypefn {Built-in Function} boolean primitive_hasns (string @var{domain}) +Returns @samp{True} if the domain @var{domain} has at least one +@samp{NS} record. Throws exception if DNS lookup fails. +@end deftypefn + +@deftypefn {Library Function} boolean hasns (string @var{domain}) +Returns @samp{True} if the domain @var{domain} has at least one +@samp{NS} record. Returns @samp{False} if there are no @samp{NS} +records or if the DNS lookup fails. +@end deftypefn + +@deftypefn {Built-in Function} string getns (string @var{domain} ; @ + boolean @var{resolve}, boolean @var{sort}) + Returns a whitespace-separated list of all the @samp{NS} records for +the domain @var{domain}. Optional parameters @var{resolve} and +@var{sort} control the formatting. If @var{resolve} is 0 (the default), the +resulting string will contain IP addresses of the NS servers. If +@var{resolve} is not 0, hostnames will be returned instead. If +@var{sort} is 1, the returned items will be sorted. + +If the @acronym{DNS} query fails, @code{getns} raises an appropriate +exception. +@end deftypefn + @node Geolocation functions @section Geolocation functions @cindex geolocation @@ -2249,7 +2276,7 @@ argument to the database functions (in description below, marked as @anchor{dbmap} @deftypefn {Built-in Function} boolean dbmap (string @var{db}, @ - string @var{key}, [number @var{null}]) + string @var{key}, [boolean @var{null}]) Looks up @var{key} in the @acronym{DBM} file @var{db} and returns @code{true} if it is found. @@ -2259,7 +2286,7 @@ argument to the database functions (in description below, marked as @end deftypefn @deftypefn {Built-in Function} string dbget (string @var{db}, @ - string @var{key} [, string @var{default}, number @var{null}]) + string @var{key} [, string @var{default}, boolean @var{null}]) Looks up @var{key} in the database @var{db} and returns the value associated with it. If the key is not found returns @var{default}, if specified, or empty string otherwise. @@ -2269,7 +2296,7 @@ specified, or empty string otherwise. @deftypefn {Built-in Function} void dbput (string @var{db}, @ string @var{key}, string @var{value} [, @ - number @var{null}, number @var{mode} ]) + boolean @var{null}, number @var{mode} ]) Inserts in the database a record with the given @var{key} and @var{value}. If a record with the given @var{key} already exists, its value is replaced with the supplied one. @@ -2281,7 +2308,7 @@ to explicitly specify the file mode for this database. See also @deftypefn {Built-in Function} void dbinsert (string @var{db}, @ string @var{key}, string @var{value} [, @ - number @var{replace}, number @var{null}, number @var{mode} ]) + boolean @var{replace}, boolean @var{null}, number @var{mode} ]) This is an improved variant of @code{dbput}, which provides a better control on the actions to take if the @var{key} already exists in the database. Namely, if @var{replace} is @samp{True}, the old value is @@ -2290,7 +2317,7 @@ is thrown. @end deftypefn @deftypefn {Built-in Function} void dbdel (string @var{db}, @ - string @var{key} [, number @var{null}, number @var{mode}]) + string @var{key} [, boolean @var{null}, number @var{mode}]) Delete from the database the record with the given @var{key}. If there are no such record, return without signalling error. @@ -2313,7 +2340,7 @@ require safedb The exception-safe interfaces are: @deftypefn {Library Function} string safedbmap (string @var{db}, @ - string @var{key} [, string @var{default}, number @var{null}]) + string @var{key} [, string @var{default}, boolean @var{null}]) This is an exception-safe interface to @code{dbmap}. If a database error occurs while attempting to retrieve the record, @@ -2322,7 +2349,7 @@ not defined. @end deftypefn @deftypefn {Library Function} string safedbget (string @var{db}, @ - string @var{key} [, string @var{default}, number @var{null}]) + string @var{key} [, string @var{default}, boolean @var{null}]) This is an exception-safe interface to @code{dbget}. If a database error occurs while attempting to retrieve the record, @@ -2331,7 +2358,7 @@ not defined. @end deftypefn @deftypefn {Library Function} void safedbput (string @var{db}, @ - string @var{key}, string @var{value} [, number @var{null}]) + string @var{key}, string @var{value} [, boolean @var{null}]) This is an exception-safe interface to @code{dbput}. If a database error occurs while attempting to retrieve the record, @@ -2339,7 +2366,7 @@ the function returns without raising exception. @end deftypefn @deftypefn {Library Function} void safedbdel (string @var{db}, @ - string @var{key} [, number @var{null}]) + string @var{key} [, boolean @var{null}]) This is an exception-safe interface to @code{dbdel}. If a database error occurs while attempting to delete the record, @@ -2422,8 +2449,9 @@ is currently enabled. If @var{fmtid} does not match any known format, @cindex disabling cache @cindex cache, disabling -@deftypefn {Built-in Function} void db_set_active (string @var{fmtid}, number @var{enable}) - Enables the cache database @var{fmtid} if @var{enable} is not null, +@deftypefn {Built-in Function} void db_set_active (string @var{fmtid}, @ + boolean @var{enable}) + Enables the cache database @var{fmtid} if @var{enable} is @samp{True}, or disables it otherwise. For example, to disable @acronym{DNS} caching, do: @@ -2784,12 +2812,12 @@ The function @code{revip} is defined in @ref{revip}. @node System functions @section System functions -@deftypefn {Built-in Function} number access (string @var{pathname}, @ +@deftypefn {Built-in Function} boolean access (string @var{pathname}, @ number @var{mode}) Checks whether the calling process can access the file @var{pathname}. If @var{pathname} is a symbolic link, it is dereferenced. The -function returns 1 (@samp{true}) if the file can be accessed and 0 -(@samp{false}) otherwise@footnote{@emph{Note}, that the return code is +function returns @samp{True} if the file can be accessed and +@samp{False} otherwise@footnote{@emph{Note}, that the return code is inverted in respect to the system function @samp{access(2)}.}. Symbolic values for @var{mode} are provided in module @@ -4833,7 +4861,7 @@ debug_level("db") @result{} 0 @end smallexample @end deftypefn -@deftypefn {Built-in Function} number callout_transcript ([number @var{value}]) +@deftypefn {Built-in Function} boolean callout_transcript ([boolean @var{value}]) Returns the current state of the callout SMTP transcript. The result is 1 if the transcript is enabled and 0 otherwise. The transcript is normally enabled either by the use of the @option{--transcript} diff --git a/lib/dns.c b/lib/dns.c index 708803d9..73499e65 100644 --- a/lib/dns.c +++ b/lib/dns.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "libmf.h" #include "dns.h" @@ -718,3 +719,32 @@ resolve_hostname(const char *host, char **pipbuf) } return dns_to_mf_status(dstat); } + +/* Return NS records for the given DOMAIN. */ +dns_status +ns_lookup(const char *domain, int resolve, struct dns_reply *reply) +{ + dns_status status = dns_failure; + int rc; + adns_answer *ans; + int i; + + rc = adns_synchronous(get_state(), domain, adns_r_ns_raw, + DEFAULT_QFLAGS, + &ans); + if (rc) + return errno_to_dns_status(rc); + status = adns_to_dns_status(ans->status); + if (status != dns_success) + return status; + + dns_reply_init(reply, dns_reply_str, ans->nrrs); + for (i = 0; i < ans->nrrs; i++) + reply->data.str[i] = mu_strdup(ans->rrs.str[i]); + free(ans); + + if (resolve) + status = dns_reply_resolve(reply); + + return status; +} diff --git a/lib/dns.h b/lib/dns.h index 74b4b168..b0127860 100644 --- a/lib/dns.h +++ b/lib/dns.h @@ -70,4 +70,6 @@ dns_status spf_lookup(const char *domain, char **record); dns_status mx_lookup(const char *host, int resolve, struct dns_reply *repl); +dns_status ns_lookup(const char *host, int resolve, struct dns_reply *reply); + #endif diff --git a/mflib/dns.mf4 b/mflib/dns.mf4 index e13130b1..e51656d8 100644 --- a/mflib/dns.mf4 +++ b/mflib/dns.mf4 @@ -59,3 +59,16 @@ do done return primitive_ismx (domain, ipstr) done + +func hasns (string str) + returns number +do + try + do + return primitive_hasns (str) + done + catch * + do + return 0 + done +done diff --git a/src/builtin/dns.bi b/src/builtin/dns.bi index 3f68f459..0e1e3279 100644 --- a/src/builtin/dns.bi +++ b/src/builtin/dns.bi @@ -279,3 +279,87 @@ MF_DEFUN(ptr_validate, NUMBER, STRING s) } END +MF_DEFUN(primitive_hasns, NUMBER, STRING dom) +{ + struct dns_reply repl; + mf_status stat = dns_to_mf_status(ns_lookup(dom, 0, &repl)); + MF_ASSERT(stat == mf_success || stat == mf_not_found, + mf_status_to_exception(stat), + _("cannot get NS records for %s"), + dom); + dns_reply_free(&repl); + if (stat == mf_success) { + MF_RETURN(1); + } + MF_RETURN(0); +} +END + +static int +cmp_ip(void const *a, void const *b) +{ + GACOPYZ_UINT32_T ipa = *(GACOPYZ_UINT32_T const *)a; + GACOPYZ_UINT32_T ipb = *(GACOPYZ_UINT32_T const *)b; + if (ipa < ipb) + return -1; + if (ipa > ipb) + return 1; + return 0; +} + +static int +cmp_str(void const *a, void const *b) +{ + char * const *stra = a; + char * const *strb = b; + return strcmp(*stra, *strb); +} + +MF_DEFUN(getns, STRING, STRING domain, OPTIONAL, NUMBER resolve, NUMBER sort) +{ + mf_status stat; + struct dns_reply reply; + int i; + + stat = dns_to_mf_status(ns_lookup(domain, MF_OPTVAL(resolve), + &reply)); + if (!mf_resolved(stat)) { + MF_THROW(mf_status_to_exception(stat), + _("cannot get MX records for %s"), domain); + } + if (stat == mf_not_found) { + MF_RETURN(""); + } + + MF_OBSTACK_BEGIN(); + if (reply.type == dns_reply_ip) { + if (sort) + qsort(reply.data.ip, + reply.count, + sizeof(reply.data.ip[0]), + cmp_ip); + for (i = 0; i < reply.count; i++) { + struct in_addr s; + s.s_addr = reply.data.ip[i]; + if (i > 0) + MF_OBSTACK_1GROW(' '); + MF_OBSTACK_GROW(inet_ntoa(s)); + } + } else { + if (sort) + qsort(reply.data.str, + reply.count, + sizeof(reply.data.str[0]), + cmp_str); + for (i = 0; i < reply.count; i++) { + if (i > 0) + MF_OBSTACK_1GROW(' '); + MF_OBSTACK_GROW(reply.data.str[i]); + } + } + MF_OBSTACK_1GROW(0); + dns_reply_free(&reply); + MF_RETURN_OBSTACK(); +} +END + -- cgit v1.2.1