Home | History | Annotate | Line # | Download | only in conf
      1  1.1  christos #!/bin/sh
      2  1.1  christos #	$NetBSD: postfix-non-bdb-script,v 1.2 2026/05/09 18:49:13 christos Exp $
      3  1.1  christos #
      4  1.1  christos 
      5  1.1  christos #++
      6  1.1  christos # NAME
      7  1.1  christos #	postfix-non-bdb 1
      8  1.1  christos # SUMMARY
      9  1.1  christos #	Postfix non-Berkeley-DB migration
     10  1.1  christos # SYNOPSIS
     11  1.1  christos #	\fBpostfix non-bdb\fR \fIsubcommand\fR
     12  1.1  christos # DESCRIPTION
     13  1.1  christos #	The "\fBpostfix non-bdb \fIsubcommand\fR" feature edits main.cf
     14  1.1  christos #	and master.cf, to manage the migration of an existing Postfix
     15  1.1  christos #	configuration that uses Berkeley DB type "hash:" or "btree:"
     16  1.1  christos #	tables (which are no longer supported on some OS distributions),
     17  1.1  christos #	to supported types such as "cdb:" or "lmdb:".
     18  1.1  christos #
     19  1.1  christos #	The following subcommands are available:
     20  1.1  christos # .IP \fBstatus\fR
     21  1.1  christos #	Reports the non-Berkeley-DB migration status, without making
     22  1.1  christos #	any changes.
     23  1.1  christos # .IP \fBdisable\fR
     24  1.1  christos #	Edits main.cf and master.cf, to turn off the \fBenable-redirect\fR
     25  1.1  christos #	and \fBenable-reindex\fR features.
     26  1.1  christos # .sp
     27  1.1  christos #	This will break integration with other software such as
     28  1.1  christos #	mailman versions from before May 2025 when they want to
     29  1.1  christos #	use "postmap hash:/path/to/file", for example, to update a
     30  1.1  christos #	mailman-maintained table.
     31  1.1  christos # .IP "\fBenable-redirect\fR (aliasing)"
     32  1.1  christos #	Edits main.cf and master.cf, to enable redirection (aliasing)
     33  1.1  christos #	from Berkeley DB types "hash" and "btree" to the non-Berkeley-DB
     34  1.1  christos #	types specified with $default_database_type and
     35  1.1  christos #	$default_cache_db_type. Custom redirection may be configured
     36  1.1  christos #	with non_bdb_custom_mapping.
     37  1.1  christos # .sp
     38  1.1  christos #	This configuration will not automatically create non-Berkeley-DB
     39  1.1  christos #	indexed database files. Instead, Postfix programs will log an
     40  1.1  christos #	error as they fail to open an indexed database file, and will
     41  1.1  christos #	leave it to the system administrator to run postmap(1) or
     42  1.1  christos #	postalias(1) to create that file.
     43  1.1  christos # .sp
     44  1.1  christos #	This will fix integration with other software such as mailman
     45  1.1  christos #	versions from before May 2025 when they want to use "postmap
     46  1.1  christos #	hash:/path/to/file", for example, to update a mailman-maintained
     47  1.1  christos #	table.
     48  1.1  christos # .sp
     49  1.1  christos #	This subcommand will not make any changes when
     50  1.1  christos #	default_database_type or default_cache_db_type specify a hash:
     51  1.1  christos #	or btree: type.
     52  1.1  christos # .IP \fBenable-reindex\fR
     53  1.1  christos #	Edits main.cf and master.cf, to implement \fBenable-redirect\fR,
     54  1.1  christos #	and to automatically create a non-Berkeley-DB indexed database
     55  1.1  christos #	file when a daemon program wants to access a file that does not
     56  1.1  christos #	yet exist. This uses the nbdb_reindexd(8) daemon to run postmap(1)
     57  1.1  christos #	or postalias(1) as described in "SECURITY" below.
     58  1.1  christos # .sp
     59  1.1  christos #	This subcommand immediately generates non-Berkeley-DB indexed
     60  1.1  christos #	files for unprivileged command-line programs that cannot send
     61  1.1  christos #	requests to the nbdb_reindexd(8) daemon server. This involves
     62  1.1  christos #	"hash:" and "btree:" tables that are used by postqueue(1) and
     63  1.1  christos #	sendmail(1) as specified in authorized_flush_users and
     64  1.1  christos #	authorized_mailq_users, and by sendmail(1) and postdrop(1)
     65  1.1  christos #	as specified in authorized_submit_users and
     66  1.1  christos #	local_login_sender_maps.
     67  1.1  christos # .sp
     68  1.1  christos #	This subcommand will not make any changes when
     69  1.1  christos #	default_database_type or default_cache_db_type specify a hash:
     70  1.1  christos #	or btree: type.
     71  1.1  christos # .sp
     72  1.1  christos #	\fINOTE: \fBenable-reindex\fI should be used only temporarily
     73  1.1  christos #	to generate most of the non-Berkeley-DB indexed files that Postfix
     74  1.1  christos #	needs. Leaving this enabled may expose the system to
     75  1.1  christos #	privilege-escalation attacks. There are no security
     76  1.1  christos #	concerns for using \fBenable-redirect\fR.
     77  1.1  christos # SECURITY
     78  1.1  christos # .ad
     79  1.1  christos # .fi
     80  1.1  christos #	The nbdb_reindexd(8) daemon automatically generates a
     81  1.1  christos #	non-Berkeley-DB indexed file only if the database pathname matches
     82  1.1  christos #	the directory prefixes specified with
     83  1.1  christos #	non_bdb_migration_allow_root_prefixes (for files that must be
     84  1.1  christos #	owned by root), or with non_bdb_migration_allow_user_prefixes
     85  1.1  christos #	(for files that must be owned by a non-root user). Additional
     86  1.1  christos #	restrictions on file and directory ownership and permissions
     87  1.1  christos #	are documented in nbdb_reindexd(8).
     88  1.1  christos # CONFIGURATION PARAMETERS
     89  1.1  christos # .ad 
     90  1.1  christos # .fi
     91  1.1  christos #	The "\fBpostfix non-bdb \fIsubcommand\fR" feature
     92  1.1  christos #	updates the following configuration parameter:
     93  1.1  christos # .IP "\fBnon_bdb_migration_level (disable)\fR"
     94  1.1  christos #	The non-Berkeley-DB migration service level.
     95  1.1  christos # .PP
     96  1.1  christos #	Other relevant parameters:
     97  1.1  christos # .IP "\fBnon_bdb_custom_mapping (empty)\fR"
     98  1.1  christos #	When non-Berkeley-DB migration is enabled, an optional mapping
     99  1.1  christos #	from a hash: or btree: type to a non-Berkeley-DB type.
    100  1.1  christos # .IP "\fBnon_bdb_migration_allow_root_prefixes (see 'postconf -d non_bdb_migration_allow_root_prefixes' output)\fR"
    101  1.1  christos #	A list of trusted pathname prefixes that must be matched when
    102  1.1  christos #	the non-Berkeley-DB migration service (\fBnbdb_reindexd\fR(8)) needs to
    103  1.1  christos #	run \fBpostmap\fR(1) or \fBpostalias\fR(1) commands with "root" privilege.
    104  1.1  christos # .IP "\fBnon_bdb_migration_allow_user_prefixes (see 'postconf -d non_bdb_migration_allow_user_prefixes' output)\fR"
    105  1.1  christos #	A list of trusted pathname prefixes that must be matched when
    106  1.1  christos #	the non-Berkeley-DB migration service (\fBnbdb_reindexd\fR(8)) needs to
    107  1.1  christos #	run \fBpostmap\fR(1) or \fBpostalias\fR(1) commands with non-root privilege.
    108  1.1  christos # SEE ALSO
    109  1.1  christos #	nbdb_reindexd(8) reindexing service
    110  1.1  christos # README FILES
    111  1.1  christos # .ad
    112  1.1  christos # .fi
    113  1.1  christos #	Use "\fBpostconf readme_directory\fR" or
    114  1.1  christos #	"\fBpostconf html_directory\fR" to locate this information.
    115  1.1  christos # .na
    116  1.1  christos # .nf
    117  1.1  christos #	NON_BERKELEYDB_README, migration guide
    118  1.1  christos # LICENSE
    119  1.1  christos # .ad
    120  1.1  christos # .fi
    121  1.1  christos #	The Secure Mailer license must be distributed with this software.
    122  1.1  christos # HISTORY
    123  1.1  christos #	The "\fBpostfix non-bdb\fR" command was introduced with Postfix
    124  1.1  christos #	version 3.11.
    125  1.1  christos # AUTHOR(S)
    126  1.1  christos #	Wietse Venema
    127  1.1  christos #	porcupine.org
    128  1.1  christos #--
    129  1.1  christos 
    130  1.1  christos umask 022
    131  1.1  christos SHELL=/bin/sh
    132  1.1  christos 
    133  1.1  christos case $command_directory in
    134  1.1  christos "") echo This script must be run by the postfix command. 1>&2
    135  1.1  christos     echo Do not run directly. 1>&2 exit 1;; esac
    136  1.1  christos 
    137  1.1  christos cd $command_directory || {
    138  1.1  christos     # Let's hope there's a "postlog" somewhere on the PATH
    139  1.1  christos     FATAL="postlog -p fatal -t $MAIL_LOGTAG/postfix-tls-script"
    140  1.1  christos     msg="no Postfix command directory '${command_directory}'"
    141  1.1  christos     $FATAL "$msg" || { echo "$msg" >&2; sleep 1; }
    142  1.1  christos     exit 1
    143  1.1  christos }
    144  1.1  christos 
    145  1.1  christos postconf=$command_directory/postconf
    146  1.1  christos LOGGER="$command_directory/postlog -t $MAIL_LOGTAG/postfix-non-bdb-script"
    147  1.1  christos INFO="$LOGGER -p info"
    148  1.1  christos WARN="$LOGGER -p warn"
    149  1.1  christos ERROR="$LOGGER -p error"
    150  1.1  christos FATAL="$LOGGER -p fatal"
    151  1.1  christos 
    152  1.1  christos REINDEX_SVC=nbdb_reindex
    153  1.1  christos REINDEX_BIN=nbdb_reindexd
    154  1.1  christos 
    155  1.1  christos # Helper functions.
    156  1.1  christos 
    157  1.1  christos reindex_for_non_daemons() {
    158  1.1  christos     # The following tables are needed by unprivileged command-line
    159  1.1  christos     # tools that cannot send requests to the reindexing service unless
    160  1.1  christos     # they are run by root.
    161  1.1  christos     for type_name in `$postconf -h authorized_flush_users \
    162  1.1  christos         authorized_mailq_users authorized_submit_users \
    163  1.1  christos         local_login_sender_maps`; \
    164  1.1  christos     do
    165  1.1  christos         case $type_name in
    166  1.1  christos             hash:*|btree:*)
    167  1.1  christos                 $INFO Proactively reindexing $type_name
    168  1.1  christos                 postmap $type_name || exit 1;;
    169  1.1  christos         esac
    170  1.1  christos     done
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos validate_redirect_targets() {
    174  1.1  christos     # By default, the Berkeley DB type 'hash' will redirect to
    175  1.1  christos     # $default_database_type, and type 'btree' will redirect to
    176  1.1  christos     # $default_cache_db_type. Require that the targets are not
    177  1.1  christos     # Berkeley DB types.
    178  1.1  christos     for param in default_database_type default_cache_db_type
    179  1.1  christos     do
    180  1.1  christos 	eval type="`$postconf -h $param`"
    181  1.1  christos 	case $type in
    182  1.1  christos 	hash|btree)
    183  1.1  christos 	    $FATAL "parameter $param specifies a Berkeley DB type: '$type'"
    184  1.1  christos 	    exit 1;;
    185  1.1  christos 	esac
    186  1.1  christos     done
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos # Subcommand implementations.
    190  1.1  christos 
    191  1.1  christos status() {
    192  1.1  christos     $postconf -h non_bdb_migration_level
    193  1.1  christos }
    194  1.1  christos 
    195  1.1  christos disable_all() {
    196  1.1  christos     $postconf -X non_bdb_migration_level
    197  1.1  christos     $postconf -MX ${REINDEX_SVC}/unix
    198  1.1  christos }
    199  1.1  christos 
    200  1.1  christos enable_redirect() {
    201  1.1  christos     validate_redirect_targets
    202  1.1  christos     $postconf -MX ${REINDEX_SVC}/unix
    203  1.1  christos     $postconf non_bdb_migration_level=enable-redirect || exit 1
    204  1.1  christos }
    205  1.1  christos 
    206  1.1  christos enable_reindex() {
    207  1.1  christos     validate_redirect_targets
    208  1.1  christos     reindex_for_non_daemons
    209  1.1  christos     $postconf -M \
    210  1.1  christos 	${REINDEX_SVC}/unix="${REINDEX_SVC} unix y n n - 1 ${REINDEX_BIN}" || exit 1
    211  1.1  christos     $postconf non_bdb_migration_level=enable-reindex || exit 1
    212  1.1  christos }
    213  1.1  christos 
    214  1.1  christos usage() {
    215  1.1  christos     $FATAL "usage: postfix non-bdb status (or enable-redirect, or enable-reindex, or disable)"
    216  1.1  christos     exit 1
    217  1.1  christos }
    218  1.1  christos 
    219  1.1  christos #
    220  1.1  christos # Parse JCL
    221  1.1  christos #
    222  1.1  christos case $# in
    223  1.1  christos   1) ;;
    224  1.1  christos   *) usage;;
    225  1.1  christos esac
    226  1.1  christos 
    227  1.1  christos case "$1" in
    228  1.1  christos enable-redirect)
    229  1.1  christos    enable_redirect;;
    230  1.1  christos enable-reindex)
    231  1.1  christos     enable_reindex;;
    232  1.1  christos disable)
    233  1.1  christos     disable_all;;
    234  1.1  christos status)
    235  1.1  christos     status;;
    236  1.1  christos *) usage
    237  1.1  christos esac
    238  1.1  christos 
    239  1.1  christos exit 0
    240