opendkim-lua(3)
โจ opendkim-lua โ the opendkim-lua(3) era โจ
okay so bestie, welcome to the opendkim-lua(3) library functions manual, aka the group chat where you learn how to program the OpenDKIM filter with Lua scripts ๐
this is part one, so hydrate before the deep lore hits ๐
๐ฆ NAME ๐ฆ
opendkim-lua โ programming the OpenDKIM filter using Lua scripts, no cap
๐ DESCRIPTION ๐
okay so picture this: the OpenDKIM filter has hooks that run your scripts so you can make policy decisions. outbound? that's about which signatures to add. inbound? that's verification and acceptance of messages. the hooks take the form of multiple Lua scripts, and if you define them, they get run at the important points while a message is being processed ๐
for the full dish on the Lua language itself, go consult the Lua programming references (there's a starting point linked below ๐). this man page is only about using Lua in the context of OpenDKIM โ specifically the functions and global variables OpenDKIM hands you for your own homegrown scripts, on top of whatever Lua gives you by default โจ
heads up bestie: unless it says otherwise, all the functions described below return a single result. on success they hand you the requested data, and on error they return the Lua constant nil. that's the vibe check ๐ญ
๐ the four scripting hooks ๐
there are four scripting hooks, and they are, in order:
setupโ this one runs after all the headers for the message have been received, but before any DKIM operations have started. so at this point you can peek at the available header fields and decide whether the message should be signed or verified (or both ๐ซถ) and, if you're signing, which key(s) to use for adding signatures and which signature features you want. main character decision energy.screenโ this one runs after the DKIM verification context has been established. the whole point here is giving you a chance to compare the message header fields against the available DKIM signatures and figure out which ones (if any) should be ignored during verification. like, for example: maybe you decide only signatures added by domains that exactly match the one in theFrom:domain are acceptable, and the rest should just be ignored. iconic filtering behaviour, honestly.statisticsโ this one runs after all the DKIM verification and signing work is done, but before any final message handling happens. its whole purpose is letting you look at the message or its signatures and make arbitrary extra statistical observations that the statistics module should record. (๐ note: this feature is experimental โ so, y'know, treat it accordingly ๐)finalโ this one runs after all the DKIM verification and signing work has been completed. now you get to look at the results of all the signature evaluations and decide whether the message should be accepted, rejected, discarded, quarantined, etc. the whole โจfate of the messageโจ is in your hands. and if the message gets accepted, any signatures you requested earlier get added to the message before it's released ๐
๐ GLOBAL VARIABLES ๐
the following global variable(s) are provided for all user scripts, periodt:
ctxโ this is a generic context pointer referring to the context in which the filtering operation is being performed. it represents a single message in progress, plus the connection that accepted it. literally the main character of your script's world ๐ธ
โจ SETUP SCRIPT FUNCTIONS โจ
Okay bestie, these are the functions Lua gets handed so it can actually do stuff with a message as it rolls through the setup script. Each one's a little tool in your kit ๐
๐ odkim.check_popauth(ctx)
Returns 1 if the SMTP client represented by ctx is coming from an IP address found in the POPAUTH database (if it's enabled and configured), and 0 otherwise. If the POPAUTH database isn't enabled or isn't configured at all, you get the Lua constant nil โ so like, "no answer" rather than "no". ๐
๐ฆ odkim.db_check(db, string)
Returns 1 if db refers to a valid database handle (peep odkim.get_dbhandle() below, iykyk) and string is found in that database, and 0 otherwise. If something goes wrong, the Lua constant nil is returned. ๐ญ
๐ธ odkim.db_close(db)
Closes the specified data set. Returns 1. The current implementation will conduct data set garbage collection when the script terminates, so this isn't strictly necessary โ but it is recommended. Be a tidy icon, close your handles. โจ
๐ odkim.db_open(name[, icase])
Opens the data set specified by name. If icase is provided and is "true", then queries into the database will be case-insensitive. See the opendkim(8) man page for the whole deal on specifying a data set. On success it returns a handle you can pass to odkim.db_check(); on failure it raises an exception. No cap, it just throws. ๐
โญ odkim.export(ctx, name, value[, name2, value2[, ...]])
Exports variables with their corresponding values so they'll be available to later scripts. Pass the pair, bestie โ name then value, as many times as you want. ๐ซถ
๐ odkim.get_clienthost(ctx)
Returns the name of the host on the other end of the SMTP connection sending the current message. Usually that's a hostname, but it might be an IP address in square brackets if the SMTP client's IP address doesn't have a name associated with it. It's giving "we tried to look it up, this is what we got". ๐
๐ง odkim.get_clientip(ctx)
Returns the IP address of the client on the other end of the SMTP connection sending the current message, as a string. Both IPv4 and IPv6 addresses are supported โ equal opportunity networking, we love to see it. ๐
๐ odkim.get_dbhandle(ctx, db)
Returns a handle for the requested database that you can use in later queries. The value of db should be one of:
DB_MTASโ database of MTA names whose mail should be signedDB_MACROSโ database of MTA macro checks to be done when determining signingDB_DOMAINSโ database of domains to be signedDB_SIGNINGTABLEโ database of signing table entriesDB_THIRDPARTYโ database of third party signatures to be trustedDB_DONTSIGNTOโ database of recipients whose mail should not be signed
If the requested database isn't set in the current configuration file, you get a Lua nil. Sad but true. ๐ญ
๐ odkim.get_envfrom(ctx)
Retrieves the SMTP envelope sender address for the message represented by ctx. The envelope one, bestie, not the header one โ know the difference. ๐
๐ธ odkim.get_fromdomain(ctx)
Retrieves the domain name of the sender of the message represented by ctx. Just the domain. Main character energy. ๐
๐ odkim.get_header(ctx, name, n)
Retrieves the string contained in instance n of the header field called name from the message represented by ctx, or the Lua constant nil if there was no such header field. Header field numbering starts at 0, so use 0 for the first instance, 1 for the second, etc. For example:
fromaddr = odkim.get_header(ctx, "From", 0)
That'll return the value of the first (and hopefully only) From header field. Negative values of n count backwards from the end of the set of header fields, so:
rcvd = odkim.get_header(ctx, "Received", -2)
will retrieve the second-last Received: header field on the message. Negative indexing in a man page?? We love a plot twist. ๐ฅ
๐ซ odkim.get_mtasymbol(ctx, name)
Retrieves the value of the symbol called name from the MTA connection represented by ctx, or the Lua constant nil if the requested symbol wasn't available at the time of the request. Timing is everything, ngl. ๐
๐ฆ odkim.get_rcpt(ctx, n)
Returns the nth envelope recipient for the message represented by ctx. Recipient numbering starts at 0, so for the first recipient, use 0 for n. If n references an out-of-range value, the Lua constant nil is returned. Zero-indexed, bestie, don't embarrass yourself. ๐
๐ odkim.get_rcptarray(ctx)
Returns the envelope recipients for the message represented by ctx in a single Lua array. All of them. At once. Iconic. โจ
๐ odkim.internal_ip(ctx)
Returns 1 if the SMTP client is coming from an internal IP address, and 0 otherwise. Are they one of us or not. ๐ค
๐ odkim.log(ctx, log)
Logs the string log if the current configuration requested logging. (Checking the current configuration is why the ctx parameter is required โ it's not just there for vibes, okay.) ๐
๐ธ odkim.rcpt_count(ctx)
Returns the count of envelope recipients on the message. A number. Simple. Slay. ๐ข
๐ฅ odkim.replace_header(ctx, name, n, newval)
Retrieves the value of instance n of header field name in the message referenced by ctx and replaces it with the string in newval. See odkim.get_header() above for more info on possible parameter values for n.
Now listen carefully, because this one's important โ ๏ธ โ this only changes the content of the header field used when generating or verifying the signature; the actual delivered message is not modified. You can use this to anticipate how an intermediate mail transfer agent might alter the message, thus correcting an avoidable signature invalidation. You're basically reading the future. ๐ฎ
๐ฆ odkim.resign(ctx)
Arranges that the arriving message will be verified and then re-signed in a single operation. Returns 1 on success or the Lua constant nil on failure. One gesture, two jobs โ multitasking queen. ๐
๐ง odkim.set_result(ctx, result)
Arranges to have the MTA return a specific result code in response to the message represented by ctx. The value of result must be one of:
SMFIS_TEMPFAILโ temporary failure/rejectionSMFIS_ACCEPTโ accept without further processingSMFIS_DISCARDโ accept but discard the messageSMFIS_REJECTโ permanent failure/rejection
Returns 1 on success or the Lua constant nil on failure.
โ ๏ธ Heads up bestie: returning any of these codes indicates a final message disposition. The MTA gets told immediately to take the specified action, and no further filter processing will occur. So likeโฆ choose wisely. There is no undo. ๐
โญ odkim.sign(ctx[, keyname[, signer[, signlen]]])
Requests that the filter sign the message represented by ctx using the specified keyname. The key name gets translated into an actual domain, selector and private key via a query to the KeyTable (see the opendkim.conf(5) page for the deets).
The keyname may be omitted if the KeyTable isn't defined, meaning the single signing domain, selector and key should be used to sign. Returns 1 on success and 0 on failure.
If a signer is specified, the string there gets included in the generated signature's "i=" tag. If a signlen is specified, the signature will cover that many bytes of the message body. And plot twist โ the order of these last two parameters is interchangeable. Go off, flexible API. ๐
๐ odkim.signfor(ctx, address[, multi])
Applies whatever signatures would be applied by default if the candidate message had the specified address in the message's From: field. The multi parameter, if "true" (default is "false"), allows the application of multiple signatures.
Returns the number of signatures applied โ which may be zero. Zero is a valid answer, don't take it personally. ๐
๐ odkim.spam(ctx)
Tags the message as spam, for use in developing reputation about domains that signed the message. Returns nothing.
Note: Feature is experimental. ๐งช
๐
odkim.use_ltag(ctx)
Requests that all signatures added to the message represented by ctx include "l=" (body length) tags. Always returns the Lua constant nil. Weird return, iconic behaviour. โจ
๐ odkim.verify(ctx)
Requests that the message represented by ctx be subjected to DKIM signature verification. Returns the Lua constant nil on success, or an error string on failure. So check that return value, bestie, fr fr. ๐
๐ฅ odkim.xtag(ctx, tag, value)
Requests that all signatures added to the message represented by ctx include the named extension tag and value. Returns the number of signatures successfully modified, or -1 on error.
An error can occur if the named tag is one already explicitly supported by the DKIM library, or if there's a syntax error in the tag or value. So don't try to outsmart the library โ it already knows that tag. ๐
๐ SCREEN SCRIPT FUNCTIONS ๐
okay so the screen script has all these functions available to it from above โ you already know them, bestie: odkim.db_check, odkim.db_close, odkim.db_open, odkim.export, odkim.get_dbhandle, odkim.get_envfrom, odkim.get_fromdomain, odkim.get_header, odkim.get_mtasymbol, odkim.get_rcpt, odkim.get_rcptarray, odkim.internal_ip, odkim.log, odkim.rcpt_count, and odkim.spam. slay โจ
The screen script also gets this extra little bag of tricks:
odkim.get_sigarray(ctx)โ returns the complete set of signature handles found in the message represented byctx, as a Lua array, or the Lua constantnilin case of an error. ๐odkim.get_sigcount(ctx)โ returns the number of signatures found in the message represented byctx, or the Lua constantnilin case of an error. ๐ฅodkim.get_sighandle(ctx, n)โ returns a handle representing an internal copy of the nth signature found on the message represented byctx.nmust be a number greater than or equal to zero (representing the first signature) and less than the number of signatures on the message, which can be determined usingodkim.get_sigcountabove. The requested handle is returned on success, or the Lua constantnilis returned on failure. ๐odkim.parse_field(string)โ parses the contents of a header field, provided asstring, into user and domain parts, discarding whitespace and comment components. Returns two strings, the user part and the domain part, or the Lua constantnilin case of a parsing error. ๐ฆodkim.sig_getdomain(sig)โ returns the name of the domain in the signature handle specified bysig, previously returned by a call toodkim.get_sighandle(). This is taken from the signature's"d="tag. ๐odkim.sig_getidentity(sig)โ returns the identity of the agent adding the signature handle specified bysig, previously returned by a call toodkim.get_sighandle(). This is taken from the signature's"i="tag. This may be a default value and not one that was explicitly part of the signature. If the identity could not be determined, the Lua constantnilis returned. ๐ธodkim.sig_ignore(sig)โ instructs the verification code to ignore completely the signature specified bysig, previously returned by a call toodkim.get_sighandle(). Any pending verification of the message will act as if that signature was not present on the message. Always returns the Lua constantnil. ๐
๐ STATISTICS SCRIPT FUNCTIONS ๐
the statistics script has these functions available to it, whose descriptions can be found above: odkim.export, odkim.get_envfrom, odkim.get_header, odkim.get_mtasymbol, odkim.get_rcpt, odkim.get_rcptarray, odkim.get_sigarray, odkim.get_sigcount, odkim.get_sighandle, odkim.internal_ip, odkim.log, odkim.parse_field, odkim.rcpt_count, odkim.sig_getdomain, odkim.sig_getidentity, and odkim.spam. iykyk ๐
The following functions are also available, defined in the next section: odkim.rbl_check, odkim.rcpt_count, odkim.sig_bhresult, odkim.sig_bodylength, odkim.sig_canonlength, and odkim.sig_result. ๐
The following additional function is provided for this script:
odkim.stats(ctx, name, value)โ records the additional statistic callednamewith its associatedvaluefor the message represented byctx. ๐โจ
๐ FINAL SCRIPT FUNCTIONS ๐
the final script has these functions available to it, whose descriptions can be found above: odkim.get_clienthost, odkim.get_clientip, odkim.get_envfrom, odkim.get_fromdomain, odkim.get_header, odkim.get_mtasymbol, odkim.get_rcpt, odkim.get_rcptarray, odkim.get_sigarray, odkim.get_sigcount, odkim.get_sighandle, odkim.internal_ip, odkim.log, odkim.parse_field, odkim.rcpt_count, odkim.set_result, odkim.sig_getdomain, odkim.sig_getidentity, odkim.spam, and odkim.xtags. the whole squad is here ๐ซถ
The following additional functions are provided for this script:
odkim.add_header(ctx, name, value)โ adds a new header field callednamewith the specifiedvalueto the message represented byctx. Returns1on success, or the Lua constantnilon failure. ๐odkim.add_rcpt(ctx, addr)โ addsaddras an envelope recipient to the message represented byctx. Returns1on success, or the Lua constantnilon failure. ๐odkim.del_header(ctx, name, n)โ deletes the nth instance (starting from0) of the header field callednamefrom the message represented byctx. Returns1on success, or the Lua constantnilon failure. โ๏ธodkim.del_rcpt(ctx, addr)โ deletesaddrfrom the list of envelope recipients on the message represented byctx, and adds a newX-Original-Recipient:header field containing the deleted address. Returns1on success, or the Lua constantnilon failure. ๐๏ธodkim.quarantine(ctx, reason)โ asks the MTA to quarantine the message represented byctxusingreasonas a text string indicating the reason for the request. Returns1on success or the Lua constantnilon failure. ๐งodkim.rbl_check(ctx, query, qroot[, timeout])โ makes an RBL query. The root of the RBL is assumed to be atqrootand the subject of the query isquery, so the query performed will bequery.qroot. The context handlectxmust also be provided as it contains a handle to the established DNS service. The optionaltimeoutparameter is the timeout to use, in seconds. Returnsnilon error, no values if the requested record was not present in the RBL, or the four octets of the RBL entry if it was. The octets are returned in big-endian order. (Note: Feature is experimental.) ๐งชodkim.set_reply(ctx, rcode, xcode, message)โ instructs the MTA to return the specified SMTP reply to the client sending the message represented byctx.rcodemust be a three-digit SMTP reply code starting with4or5(for temporary or permanent failures, respectively);xcodemust be the empty string or a valid extended reply code (see RFC2034) matchingrcode; andmessagemust be the text portion of the SMTP reply to be sent. Returns1on success or the Lua constantnilon failure. ๐จodkim.sig_bhresult(sig)โ returns the result code corresponding to the body hash evaluation of the signature handle specified bysig, previously returned by a call toodkim.get_sighandle(). Valid values are theDKIM_SIGBH_*constants defined in the libopendkim header filedkim.h. ๐odkim.sig_bodylength(sig)โ returns the total length of the message signed bysig, previously returned by a call toodkim.get_sighandle(), or the Lua constantnilif this value could not be determined. ๐odkim.sig_canonlength(sig)โ returns the canonicalized length of the message signed bysig, previously returned by a call toodkim.get_sighandle(), or the Lua constantnilif this value could not be determined. Note that this may be less than the value returned byodkim.get_bodylength()if the signature only covered part of the message. ๐งฎodkim.sig_result(sig)โ returns the result code corresponding to the signature handle specified bysig, previously returned by a call toodkim.get_sighandle(). Valid values are the constants withDKIM_SIGERROR_prefixes as defined in the libopendkim header filedkim.h. โญ
๐ VERSION ๐
This man page covers version 2.11.0 of OpenDKIM. periodt ๐
๐ COPYRIGHT ๐
Copyright (c) 2009-2015, The Trusted Domain Project. All rights reserved. ๐ซถ
๐ SEE ALSO ๐
Lua โ http://www.lua.org ๐ฆ
mwah ๐ that's the whole page, bestie โ footer source/version vibes: The Trusted Domain Project, page opendkim-lua(3), version 2.11.0. no cap, that's a wrap ๐โจ
๐ฆ where this page comes from
- opensuse-leap 16.0: opendkim 2.11.0-bp160.1.4 ยท BSD-3-Clause AND Sendmail
- opensuse-leap 16.0: opendkim 2.11.0-bp160.1.4 ยท BSD-3-Clause AND Sendmail
- opensuse-leap 16.0: opendkim 2.11.0-bp160.1.4 ยท BSD-3-Clause AND Sendmail
- opensuse-leap 16.0: opendkim 2.11.0-bp160.1.4 ยท BSD-3-Clause AND Sendmail
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms