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