1<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 4<html> 5 6<head> 7 8<title>Postfix Lookup Table Overview</title> 9 10<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 11 12</head> 13 14<body> 15 16<h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix 17Lookup Table Overview</h1> 18 19<hr> 20 21<h2>Overview </h2> 22 23This document covers the following topics: 24 25<ul> 26 27<li><a href="#intro">The Postfix lookup table model</a> 28 29<li><a href="#lists">Postfix lists versus tables </a> 30 31<li><a href="#preparing">Preparing Postfix for LDAP or SQL lookups</a> 32 33<li><a href="#detect">Maintaining Postfix lookup table files</a> 34 35<li><a href="#safe_db">Updating Berkeley DB files safely</a> 36 37<li><a href="#types">Postfix lookup table types</a> 38 39</ul> 40 41<h2><a name="intro">The Postfix lookup table model</a></h2> 42 43<p> Postfix uses lookup tables to store and look up information 44for access control, address rewriting and even for content filtering. 45All Postfix lookup tables are specified as "<a href="DATABASE_README.html">type:table</a>", where 46"type" is one of the database types described under "<a 47href="#types">Postfix lookup table types</a>" at the end of this 48document, and where "table" is the lookup table name. The Postfix 49documentation uses the terms "database" and "lookup table" for the 50same thing. </p> 51 52<p> Examples of lookup tables that appear often in the Postfix 53documentation: </p> 54 55<blockquote> 56<pre> 57/etc/postfix/<a href="postconf.5.html">main.cf</a>: 58 <a href="postconf.5.html#alias_maps">alias_maps</a> = <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/aliases (local aliasing) 59 <a href="postconf.5.html#header_checks">header_checks</a> = <a href="regexp_table.5.html">regexp</a>:/etc/postfix/header_checks (content filtering) 60 <a href="postconf.5.html#transport_maps">transport_maps</a> = <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/transport (routing table) 61 <a href="postconf.5.html#virtual_alias_maps">virtual_alias_maps</a> = <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/virtual (address rewriting) 62</pre> 63</blockquote> 64 65<p> All Postfix lookup tables store information as (key, value) 66pairs. This interface may seem simplistic at first, but it turns 67out to be very powerful. The (key, value) query interface completely 68hides the complexities of LDAP or SQL from Postfix. This is a good 69example of connecting complex systems with simple interfaces. </p> 70 71<p> Benefits of the Postfix (key, value) query interface:</p> 72 73<ul> 74 75<li> You can implement Postfix lookup tables first with local 76Berkeley DB files and then switch to LDAP or MySQL without any 77impact on the Postfix configuration itself, as described under "<a 78href="#preparing">Preparing Postfix for LDAP or SQL lookups</a>" 79below. 80 81<li> You can use Berkeley DB files with fixed lookup strings for 82simple address rewriting operations and you can use regular expression 83tables for the more complicated work. In other words, you don't 84have to put everything into the same table. 85 86</ul> 87 88<h2><a name="lists">Postfix lists versus tables </a></h2> 89 90<p> Most Postfix lookup tables are used to look up information. 91Examples are address rewriting (the lookup string is the old address, 92and the result is the new address) or access control (the lookup 93string is the client, sender or recipient, and the result is an 94action such as "reject"). </p> 95 96<p> With some tables, however, Postfix needs to know only if the 97lookup key exists. Any non-empty lookup result value may be used 98here: the lookup result is not used. Examples 99are the <a href="postconf.5.html#local_recipient_maps">local_recipient_maps</a> that determine what local recipients 100Postfix accepts in mail from the network, the <a href="postconf.5.html#mydestination">mydestination</a> parameter 101that specifies what domains Postfix delivers locally for, or the 102<a href="postconf.5.html#mynetworks">mynetworks</a> parameter that specifies the IP addresses of trusted 103clients or client networks. Technically, these are lists, not 104tables. Despite the difference, Postfix lists are described here 105because they use the same underlying infrastructure as Postfix 106lookup tables. </p> 107 108<h2><a name="preparing">Preparing Postfix for LDAP or SQL lookups</a> 109</h2> 110 111<p> LDAP and SQL are complex systems. Trying to set up both Postfix 112and LDAP or SQL at the same time is definitely not a good idea. 113You can save yourself a lot of time by implementing Postfix first 114with local files such as Berkeley DB. Local files have few surprises, 115and are easy to debug with the <a href="postmap.1.html">postmap(1)</a> command: </p> 116 117<blockquote> 118<pre> 119% <b>postmap -q info@example.com <a href="DATABASE_README.html#types">hash</a>:/etc/postfix/virtual </b> 120</pre> 121</blockquote> 122 123<p> Once you have local files working properly you can follow the 124instructions in <a href="ldap_table.5.html">ldap_table(5)</a>, <a href="mysql_table.5.html">mysql_table(5)</a>, <a href="pgsql_table.5.html">pgsql_table(5)</a> 125or <a href="sqlite_table.5.html">sqlite_table(5)</a> 126and replace local file lookups with LDAP or SQL lookups. When you 127do this, you should use the <a href="postmap.1.html">postmap(1)</a> command again, to verify 128that database lookups still produce the exact same results as local 129file lookup: </p> 130 131<blockquote> 132<pre> 133% <b>postmap -q info@example.com <a href="ldap_table.5.html">ldap</a>:/etc/postfix/virtual.cf </b> 134</pre> 135</blockquote> 136 137<p> Be sure to exercise all the partial address or parent domain 138queries that are documented under "table search order" in the 139relevant manual page: <a href="access.5.html">access(5)</a>, <a href="canonical.5.html">canonical(5)</a>, <a href="virtual.5.html">virtual(5)</a>, 140<a href="transport.5.html">transport(5)</a>, or under the relevant configuration parameter: 141<a href="postconf.5.html#mynetworks">mynetworks</a>, <a href="postconf.5.html#relay_domains">relay_domains</a>, <a href="postconf.5.html#parent_domain_matches_subdomains">parent_domain_matches_subdomains</a>. </p> 142 143<h2><a name="detect">Maintaining Postfix lookup table files</a></h2> 144 145<p> When you make changes to a database while the mail system is 146running, it would be desirable if Postfix avoids reading information 147while that information is being changed. It would also be nice if 148you can change a database without having to execute "postfix reload", 149in order to force Postfix to use the new information. Each time 150you do "postfix reload" Postfix loses a lot of performance. 151</p> 152 153<ul> 154 155<li> <p> If you change a network database such as LDAP, NIS or 156SQL, there is no need to execute "postfix reload". The LDAP, NIS 157or SQL server takes care of read/write access conflicts and gives 158the new data to Postfix once that data is available. </p> 159 160<li> <p> If you change a <a href="regexp_table.5.html">regexp</a>:, <a href="pcre_table.5.html">pcre</a>:, <a href="cidr_table.5.html">cidr</a>: or <a href="DATABASE_README.html#types">texthash</a>: file 161then Postfix 162may not pick up the file changes immediately. This is because a 163Postfix process reads the entire file into memory once and never 164examines the file again. </p> 165 166<ul> 167 168<li> <p> If the file is used by a short-running process such as 169<a href="smtpd.8.html">smtpd(8)</a>, <a href="cleanup.8.html">cleanup(8)</a> or <a href="local.8.html">local(8)</a>, there is no need to execute 170"postfix reload" after making a change. </p> 171 172<li> <p> If the file is being used by a long-running process such 173as <a href="trivial-rewrite.8.html">trivial-rewrite(8)</a> on a busy server it may be necessary to 174execute "postfix reload". </p> 175 176</ul> 177 178<li> <p> If you change a local file based database such as DBM or 179Berkeley DB, there is no need to execute "postfix reload". Postfix 180uses file locking to avoid read/write access conflicts, and whenever 181a Postfix daemon process notices that a file has changed it will 182terminate before handling the next client request, so that a new 183process can initialize with the new database. </p> 184 185</ul> 186 187<h2><a name="safe_db">Updating Berkeley DB files safely</a></h2> 188 189<p> Postfix uses file locking to avoid access conflicts while 190updating Berkeley DB or other local database files. This used to 191be safe, but as Berkeley DB has evolved to use more aggressive 192caching, file locking may no longer be sufficient. </p> 193 194<p> Furthermore, file locking would not prevent problems when the 195update fails because the disk is full or something else causes a 196database update to fail. In particular, commands such as <a href="postmap.1.html">postmap(1)</a> 197or <a href="postalias.1.html">postalias(1)</a> overwrite existing files. If the overwrite 198fails in the middle then you have no usable database, and Postfix 199will stop working. This is not an issue with the CDB database type 200available with Postfix 2.2 and later: <a href="CDB_README.html">CDB</a> 201creates a new file, and renames the file upon successful completion. 202</p> 203 204<p> With Berkeley DB and other "one file" databases, it is 205possible to add some extra robustness by using "mv" to REPLACE an 206existing database file instead of overwriting it: </p> 207 208<blockquote> 209<pre> 210# <b>postmap access.in && mv access.in.db access.db</b> 211</pre> 212</blockquote> 213 214<p> This converts the input file "access.in" into the output file 215"access.in.db", and replaces the file "access.db" only when the 216<a href="postmap.1.html">postmap(1)</a> command was successful. Of course typing such commands 217becomes boring quickly, and this is why people use "make" instead, 218as shown below. User input is shown in bold font. </p> 219 220<blockquote> 221<pre> 222# <b>cat Makefile</b> 223all: aliases.db access.db virtual.db ...etcetera... 224 225# Note 1: commands are specified after a TAB character. 226# Note 2: use <a href="postalias.1.html">postalias(1)</a> for local aliases, <a href="postmap.1.html">postmap(1)</a> for the rest. 227aliases.db: aliases.in 228 postalias aliases.in 229 mv aliases.in.db aliases.db 230 231access.db: access.in 232 postmap access.in 233 mv access.in.db access.db 234 235virtual.db: virtual.in 236 postmap virtual.in 237 mv virtual.in.db virtual.db 238 239...etcetera... 240# <b>vi access.in</b> 241...editing session not shown... 242# <b>make</b> 243postmap access.in 244mv access.in.db access.db 245# 246</pre> 247</blockquote> 248 249<p> The "make" command updates only the files that have changed. 250In case of error, the "make" command will stop and will not invoke 251the "mv" command, so that Postfix will keep using the existing 252database file as if nothing happened. </p> 253 254<h2><a name="types">Postfix lookup table types</a> </h2> 255 256<p> To find out what database types your Postfix system supports, 257use the "<b>postconf -m</b>" command. Here is a list of database types 258that are often supported: </p> 259 260<blockquote> 261 262<dl> 263 264<dt> <b>btree</b> </dt> 265 266<dd> A sorted, balanced tree structure. This is available only on 267systems with support for Berkeley DB databases. Database files are 268created with the <a href="postmap.1.html">postmap(1)</a> or <a href="postalias.1.html">postalias(1)</a> command. The lookup 269table name as used in "<a href="DATABASE_README.html#types">btree</a>:table" is the database file name 270without the ".db" suffix. </dd> 271 272<dt> <b>cdb</b> </dt> 273 274<dd> A read-optimized structure with no support for incremental updates. 275Database files are created with the <a href="postmap.1.html">postmap(1)</a> or <a href="postalias.1.html">postalias(1)</a> command. 276The lookup table name as used in "<a href="CDB_README.html">cdb</a>:table" is the database file name 277without the ".cdb" suffix. This feature is available with Postfix 2.2 278and later. </dd> 279 280<dt> <b>cidr</b> </dt> 281 282<dd> A table that associates values with Classless Inter-Domain 283Routing (CIDR) patterns. The table format is described in <a href="cidr_table.5.html">cidr_table(5)</a>. 284</dd> 285 286<dt> <b>dbm</b> </dt> 287 288<dd> An indexed file type based on hashing. This is available only 289on systems with support for DBM databases. Public database files 290are created with the <a href="postmap.1.html">postmap(1)</a> or <a href="postalias.1.html">postalias(1)</a> command, and private 291databases are maintained by Postfix daemons. The lookup table name 292as used in "<a href="DATABASE_README.html#types">dbm</a>:table" is the database file name without the ".dir" 293or ".pag" suffix. </dd> 294 295<dt> <b>environ</b> </dt> 296 297<dd> The UNIX process environment array. The lookup key is the 298variable name. The lookup table name in "<a href="DATABASE_README.html#types">environ</a>:table" is ignored. 299</dd> 300 301<dt> <b>fail</b> </dt> 302 303<dd> A table that reliably fails all requests. The lookup table 304name is used for logging only. This table exists to simplify Postfix 305error tests. </dd> 306 307<dt> <b>hash</b> </dt> 308 309<dd> An indexed file type based on hashing. This is available only 310on systems with support for Berkeley DB databases. Public database 311files are created with the <a href="postmap.1.html">postmap(1)</a> or <a href="postalias.1.html">postalias(1)</a> command, and 312private databases are maintained by Postfix daemons. The database 313name as used in "<a href="DATABASE_README.html#types">hash</a>:table" is the database file name without the 314".db" suffix. </dd> 315 316<dt> <b>inline</b> (read-only) </dt> 317 318<dd> A non-shared, in-memory lookup table. Example: "<a href="DATABASE_README.html#types">inline</a>:{ 319<i>key=value</i>, { <i>key = text with whitespace or comma</i> }}". 320Key-value pairs are separated by whitespace or comma; with a key-value 321pair inside "{}", whitespace is ignored after the opening "{", 322around the "=" between key and value, and before the closing "}". 323Inline tables eliminate the 324need to create a database file for just a few fixed elements. See 325also the <a href="DATABASE_README.html#types">static</a>: map type. </dd> 326 327<dt> <b>internal</b> </dt> 328 329<dd> A non-shared, in-memory hash table. Its contents are lost when 330a process terminates. </dd> 331 332<dt> <b>lmdb</b> </dt> 333 334<dd> OpenLDAP LMDB database. This is available only on systems 335with support for LMDB databases. Public database files are created 336with the <a href="postmap.1.html">postmap(1)</a> or <a href="postalias.1.html">postalias(1)</a> command, and private databases 337are maintained by Postfix daemons. The database name as used in 338"<a href="lmdb_table.5.html">lmdb</a>:table" is the database file name without the ".lmdb" suffix. 339See <a href="lmdb_table.5.html">lmdb_table(5)</a> for details. </dd> 340 341<dt> <b>ldap</b> (read-only) </dt> 342 343<dd> LDAP database client. Configuration details are given in the 344<a href="ldap_table.5.html">ldap_table(5)</a>. </dd> 345 346<dt> <b>memcache</b> </dt> 347 348<dd> Memcache database client. Configuration details are given in 349<a href="memcache_table.5.html">memcache_table(5)</a>. </dd> 350 351<dt> <b>mysql</b> (read-only) </dt> 352 353<dd> MySQL database client. Configuration details are given in 354<a href="mysql_table.5.html">mysql_table(5)</a>. </dd> 355 356<dt> <b>netinfo</b> (read-only) </dt> 357 358<dd> Netinfo database client. </dd> 359 360<dt> <b>nis</b> (read-only) </dt> 361 362<dd> NIS database client. </dd> 363 364<dt> <b>nisplus</b> (read-only) </dt> 365 366<dd> NIS+ database client. Configuration details are given in 367<a href="nisplus_table.5.html">nisplus_table(5)</a>. </dd> 368 369<dt> <b>pcre</b> (read-only) </dt> 370 371<dd> A lookup table based on Perl Compatible Regular Expressions. 372The file format is described in <a href="pcre_table.5.html">pcre_table(5)</a>. The lookup table 373name as used in "<a href="pcre_table.5.html">pcre</a>:table" is the name of the regular expression 374file. </dd> 375 376<dt> <b>pipemap</b> (read-only) </dt> 377 378<dd> A pipeline of lookup tables. Example: 379"<a href="DATABASE_README.html#types">pipemap</a>:{<i>type<sub>1</sub>:name<sub>1</sub>, ..., 380type<sub>n</sub>:name<sub>n</sub></i>}". Each "<a href="DATABASE_README.html#types">pipemap</a>:" query is 381given to the first table. Each lookup result becomes the query for 382the next table in the pipeline, and the last table produces the 383final result. When any table lookup produces no result, the pipeline 384produces no result. The first and last characters of the "<a href="DATABASE_README.html#types">pipemap</a>:" 385table name must be "{" and "}". Within these, individual maps are 386separated with comma or whitespace. </dd> 387 388<dt> <b>pgsql</b> (read-only) </dt> 389 390<dd> PostgreSQL database client. Configuration details are given 391in <a href="pgsql_table.5.html">pgsql_table(5)</a>. </dd> 392 393<dt> <b>proxy</b> </dt> 394 395<dd> Postfix <a href="proxymap.8.html">proxymap(8)</a> client for shared access to Postfix 396databases. The lookup table name syntax is "<a href="proxymap.8.html">proxy</a>:<a href="DATABASE_README.html">type:table</a>". 397</dd> 398 399<dt> <b>randmap</b> (read-only) </dt> 400 401<dd> An in-memory table that performs random selection. Example: 402"<a href="DATABASE_README.html#types">randmap</a>:{<i>result<sub>1</sub>. ..., result<sub>n</sub></i>}". 403Each table query returns a random choice from the specified results. 404The first and last characters of the "<a href="DATABASE_README.html#types">randmap</a>:" table name must be 405"{" and "}". Within these, individual maps are separated with comma 406or whitespace. To give a specific result more weight, specify it 407multiple times. </dd> 408 409<dt> <b>regexp</b> (read-only) </dt> 410 411<dd> A lookup table based on regular expressions. The file format 412is described in <a href="regexp_table.5.html">regexp_table(5)</a>. The lookup table name as used in 413"<a href="regexp_table.5.html">regexp</a>:table" is the name of the regular expression file. </dd> 414 415<dt> <b>sdbm</b> </dt> 416 417<dd> An indexed file type based on hashing. This is available only 418on systems with support for SDBM databases. Public database files 419are created with the <a href="postmap.1.html">postmap(1)</a> or <a href="postalias.1.html">postalias(1)</a> command, and private 420databases are maintained by Postfix daemons. The lookup table name 421as used in "<a href="DATABASE_README.html#types">sdbm</a>:table" is the database file name without the ".dir" 422or ".pag" suffix. </dd> 423 424<dt> <b>socketmap</b> (read-only) </dt> 425 426<dd> Sendmail-style socketmap client. The name of the table is 427either <b>inet</b>:<i>host</i>:<i>port</i>:<i>name</i> for a TCP/IP 428server, or <b>unix</b>:<i>pathname</i>:<i>name</i> for a UNIX-domain 429server. See <a href="socketmap_table.5.html">socketmap_table(5)</a> for details. </dd> 430 431<dt> <b>sqlite</b> (read-only) </dt> 432 433<dd> SQLite database. Configuration details are given in <a href="sqlite_table.5.html">sqlite_table(5)</a>. 434</dd> 435 436<dt> <b>static</b> (read-only) </dt> 437 438<dd> A table that always returns its name as the lookup result. 439For example, "<a href="DATABASE_README.html#types">static</a>:foobar" always returns the string "foobar" as 440lookup result. Specify "<a href="DATABASE_README.html#types">static</a>:{ <i>text with whitespace</i> }" 441when the result contains whitespace; this form ignores whitespace 442after the opening "{" and before the closing "}". See also the 443<a href="DATABASE_README.html#types">inline</a>: map type. </dd> 444 445<dt> <b>tcp</b> </dt> 446 447<dd> TCP/IP client. The protocol is described in <a href="tcp_table.5.html">tcp_table(5)</a>. The 448lookup table name is "<a href="tcp_table.5.html">tcp</a>:host:port" where "host" specifies a 449symbolic hostname or a numeric IP address, and "port" specifies a 450symbolic service name or a numeric port number. </dd> 451 452<dt> <b>texthash</b> (read-only) </dt> 453 454<dd> A table that produces similar results as <a href="DATABASE_README.html#types">hash</a>: files, except 455that you don't have to run the <a href="postmap.1.html">postmap(1)</a> command before you can 456use the file, and that <a href="DATABASE_README.html#types">texthash</a>: does not detect changes after the 457file is read. The lookup table name is "<a href="DATABASE_README.html#types">texthash</a>:filename", where 458the file name is taken literally; no suffix is appended. </dd> 459 460<dt> <b>unionmap</b> (read-only) </dt> 461 462<dd> A table that sends each query to multiple lookup tables and 463that concatenates all found results, separated by comma. The table 464name syntax is the same as for pipemap tables. </dd> 465 466<dt> <b>unix</b> (read-only) </dt> 467 468<dd> A limited view of the UNIX authentication database. The following 469tables are implemented: 470 471<dl> 472 473<dt> <b>unix:passwd.byname</b> </dt> 474 475<dd>The table is the UNIX password database. The key is a login 476name. The result is a password file entry in passwd(5) format. 477</dd> 478 479<dt> <b>unix:group.byname</b> </dt> 480 481<dd> The table is the UNIX group database. The key is a group name. 482The result is a group file entry in group(5) format. </dd> 483 484</dl> </dd> 485 486</dl> 487 488</blockquote> 489 490<p> Other lookup table types may be available depending on how 491Postfix was built. With some Postfix distributions the list is 492dynamically extensible as support for lookup tables is dynamically 493linked into Postfix. </p> 494 495</body> 496 497</html> 498