<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/" >

<channel>
	<title>allow_nets &#8211; Random thoughts</title>
	<atom:link href="https://random.sphere.ro/tag/allow_nets/feed/" rel="self" type="application/rss+xml" />
	<link>https://random.sphere.ro</link>
	<description>for when you get older and memory does&#039;t help you further</description>
	<lastBuildDate>Mon, 20 Jan 2020 20:00:42 +0000</lastBuildDate>
	<language>en-AU</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.5</generator>
	<item>
		<title>dovecot block login per country / ip2location / geoip</title>
		<link>https://random.sphere.ro/dovecot-block-login-per-country-ip2location-geoip/</link>
					<comments>https://random.sphere.ro/dovecot-block-login-per-country-ip2location-geoip/#comments</comments>
		
		<dc:creator><![CDATA[iulian]]></dc:creator>
		<pubDate>Mon, 20 Jan 2020 20:00:42 +0000</pubDate>
				<category><![CDATA[Uncategorised]]></category>
		<category><![CDATA[allow_nets]]></category>
		<category><![CDATA[dovecor]]></category>
		<category><![CDATA[geoip]]></category>
		<category><![CDATA[ip2location]]></category>
		<guid isPermaLink="false">https://random.sphere.ro/?p=105</guid>

					<description><![CDATA[<p>I&#8217;ll need to be honest and say that I&#8217;ve search for a long time an &#8220;easy&#8221; way to restrict IMAP logins / per user / per country. During my search around this topic i&#8217;ve also found allow_nets which is a nice feature but it only allows you to restrict per user / per ip or&#8230;</p>
<p class="read-more"><a class="readmore-btn" href="https://random.sphere.ro/dovecot-block-login-per-country-ip2location-geoip/">Read More<span class="screen-reader-text">  Read More</span></a></p>
<p>The post <a rel="nofollow" href="https://random.sphere.ro/dovecot-block-login-per-country-ip2location-geoip/">dovecot block login per country / ip2location / geoip</a> appeared first on <a rel="nofollow" href="https://random.sphere.ro">Random thoughts</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ll need to be honest and say that I&#8217;ve search for a long time an &#8220;easy&#8221; way to restrict IMAP logins / per user / per country.</p>
<p>During my search around this topic i&#8217;ve also found <em>allow_nets</em> which is a nice feature but it only allows you to restrict per user / per ip or net. Plus the <a href="https://wiki2.dovecot.org/PasswordDatabase/ExtraFields/AllowNets" target="_blank" rel="noopener">wiki tutorial</a> for this feature is not showing a nice way to integrate with dovecot-sql. My approach was to create another field in the mysql table(in my case vpopmail) and when <em>password_query </em> is formed to request also this field. In short words my current sql lookups are like:</p>
<pre>user_query = SELECT pw_dir as home, 89 AS uid, 89 AS gid FROM vpopmail WHERE pw_name = '%n' AND pw_domain = '%d'

password_query = select pw_clear_passwd as password, allow_nets FROM vpopmail LEFT JOIN limits ON vpopmail.pw_domain=limits.domain WHERE pw_name='%n' and pw_domain='%d' and ( !(pw_gid &amp; 8)
and ('%r'!='127.0.0.1' or !(pw_gid &amp; 4)) and ( '%r'!='127.0.0.1' or COALESCE(disable_webmail,0)!=1) and COALESCE(disable_imap,0)!=1);</pre>
<p>Why so big ? Simply because i wanted also the support for vmoduser features which are also a nice set to restrict behaviour per user/domain. For more info about dovecot and vpopmail sql auth you can read on <a href="https://wiki.dovecot.org/AuthDatabase/VPopMail" target="_blank" rel="noopener">dovecot wiki.</a></p>
<p>As i said in the beginning of the article i was also search for ways to block user(s)/country. Apparently it is possible via dovecot authentication policy but i&#8217;ve found it quite painful. You can read more about this method <a href="https://wiki.dovecot.org/Authentication/Policy" target="_blank" rel="noopener">here.</a></p>
<p>My method is base on <a href="https://www.ip2location.com/" target="_blank" rel="noopener">ip2location</a> which has also also to possibility to download the geographical IP data in CSV. Starting from this data you can easily create a database and import the CVS into it.</p>
<pre>CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db1`(
`ip_from` INT(10) UNSIGNED,
`ip_to` INT(10) UNSIGNED,
`country_code` CHAR(2),
`country_name` VARCHAR(64),
PRIMARY KEY (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;</pre>
<pre>LOAD DATA LOCAL
INFILE 'IPCountry.CSV'
INTO TABLE
`ip2location_db1`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';</pre>
<p>You can find more about how to manipulate data from ip2location <a href="https://www.ip2location.com/faqs/db1-ip-country#database" target="_blank" rel="noopener">on their site.</a></p>
<p>In this moment you will have a database with IPs and their location. Using the dovecot variable %r ( remote ip ) we can pass this to the sql query when searching for username/password. For more about dovecot variables i recommend reading <a href="https://wiki1.dovecot.org/Variables" target="_blank" rel="noopener">their site.</a></p>
<p>I though that will be easy but i didn&#8217;t expect the query to became so long. To be honest i&#8217;ve tried also with sql variables and stored procedures but i didn&#8217;t found a way to properly set it into dovecot. If you find it please leave me a comment.</p>
<p>In this moment my username query is looking like this. Please have in mind that allow_country is another field in vpopmail database(or sql db used for authentication) and this will contain data in the fallowing format: RO,DE,NL (example for: Romania, Germany, Netherlands)</p>
<pre>user_query = SELECT pw_dir as home, 89 AS uid, 89 AS gid FROM vpopmail WHERE pw_name = '%n' AND pw_domain = '%d' and allow_country like CONCAT('%%',(select country_code from ip2location.ip2location_db1 where ip_from &lt;= INET_ATON('%r') and ip_to &gt;= INET_ATON('%r')),'%%')</pre>
<p>&nbsp;</p>
<p>This is all, hope that it was instructive.</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://random.sphere.ro/dovecot-block-login-per-country-ip2location-geoip/">dovecot block login per country / ip2location / geoip</a> appeared first on <a rel="nofollow" href="https://random.sphere.ro">Random thoughts</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://random.sphere.ro/dovecot-block-login-per-country-ip2location-geoip/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
