commit f90cb29: [Minor] Lua_spf: Allow string representation of the IP address

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Dec 2 17:42:06 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-12-02 17:36:08 +0000
URL: https://github.com/rspamd/rspamd/commit/f90cb298025f4ebf08b541b65b157d464e056d63

[Minor] Lua_spf: Allow string representation of the IP address

---
 src/lua/lua_spf.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/lua/lua_spf.c b/src/lua/lua_spf.c
index 655691092..478a7bbc2 100644
--- a/src/lua/lua_spf.c
+++ b/src/lua/lua_spf.c
@@ -386,7 +386,7 @@ spf_check_element (lua_State *L, struct spf_resolved *rec, struct spf_addr *addr
  * 1. Boolean check result
  * 2. If result is `false` then the second value is the error flag (e.g. rspamd_spf.flags.temp_fail), otherwise it will be an SPF method
  * 3. If result is `false` then this will be an error string, otherwise - an SPF string (e.g. `mx` or `ip4:x.y.z.1`)
- * @param {rspamd_ip} ip address
+ * @param {rspamd_ip|string} ip address
  * @return {result,flag_or_policy,error_or_addr} - triplet
 */
 static gint
@@ -395,8 +395,29 @@ lua_spf_record_check_ip (lua_State *L)
 	struct spf_resolved *record =
 			* (struct spf_resolved **)rspamd_lua_check_udata (L, 1,
 					SPF_RECORD_CLASS);
-	struct rspamd_lua_ip *ip = lua_check_ip (L, 2);
+	struct rspamd_lua_ip *ip = NULL;
 	gint nres = 0;
+	gboolean need_free_ip = FALSE;
+
+	if (lua_type (L, 2) == LUA_TUSERDATA) {
+		ip = lua_check_ip (L, 2);
+	}
+	else if (lua_type (L, 2) == LUA_TSTRING) {
+		const gchar *ip_str;
+		gsize iplen;
+
+		ip = g_malloc0 (sizeof (struct rspamd_lua_ip));
+		ip_str = lua_tolstring (L, 2, &iplen);
+
+		if (!rspamd_parse_inet_address (&ip->addr,
+				ip_str, iplen, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
+			g_free (ip);
+			ip = NULL;
+		}
+		else {
+			need_free_ip = TRUE;
+		}
+	}
 
 	if (record && ip && ip->addr) {
 		for (guint i = 0; i < record->elts->len; i ++) {
@@ -410,6 +431,10 @@ lua_spf_record_check_ip (lua_State *L)
 		return luaL_error (L, "invalid arguments");
 	}
 
+	if (need_free_ip) {
+		g_free (ip);
+	}
+
 	lua_pushboolean (L, false);
 	lua_pushinteger (L, RSPAMD_SPF_RESOLVED_NA);
 	lua_pushstring (L, "no result");


More information about the Commits mailing list