1 /* $NetBSD: postmulti.c,v 1.1.1.4 2013/01/02 18:59:04 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* postmulti 1 6 /* SUMMARY 7 /* Postfix multi-instance manager 8 /* SYNOPSIS 9 /* .fi 10 /* \fBENABLING MULTI-INSTANCE MANAGEMENT:\fR 11 /* 12 /* \fBpostmulti\fR \fB-e init\fR [\fB-v\fR] 13 /* 14 /* \fBITERATOR MODE:\fR 15 /* 16 /* \fBpostmulti\fR \fB-l\fR [\fB-aRv\fR] [\fB-g \fIgroup\fR] 17 /* [\fB-i \fIname\fR] 18 /* 19 /* \fBpostmulti\fR \fB-p\fR [\fB-av\fR] [\fB-g \fIgroup\fR] 20 /* [\fB-i \fIname\fR] \fIcommand...\fR 21 /* 22 /* \fBpostmulti\fR \fB-x\fR [\fB-aRv\fR] [\fB-g \fIgroup\fR] 23 /* [\fB-i \fIname\fR] \fIcommand...\fR 24 /* 25 /* \fBLIFE-CYCLE MANAGEMENT:\fR 26 /* 27 /* \fBpostmulti\fR \fB-e create\fR [\fB-av\fR] 28 /* [\fB-g \fIgroup\fR] [\fB-i \fIname\fR] [\fB-G \fIgroup\fR] 29 /* [\fB-I \fIname\fR] [\fIparam=value\fR ...] 30 /* 31 /* \fBpostmulti\fR \fB-e import\fR [\fB-av\fR] 32 /* [\fB-g \fIgroup\fR] [\fB-i \fIname\fR] [\fB-G \fIgroup\fR] 33 /* [\fB-I \fIname\fR] [\fBconfig_directory=\fI/path\fR] 34 /* 35 /* \fBpostmulti\fR \fB-e destroy\fR [\fB-v\fR] \fB-i \fIname\fR 36 /* 37 /* \fBpostmulti\fR \fB-e deport\fR [\fB-v\fR] \fB-i \fIname\fR 38 /* 39 /* \fBpostmulti\fR \fB-e enable\fR [\fB-v\fR] \fB-i \fIname\fR 40 /* 41 /* \fBpostmulti\fR \fB-e disable\fR [\fB-v\fR] \fB-i \fIname\fR 42 /* 43 /* \fBpostmulti\fR \fB-e assign\fR [\fB-v\fR] \fB-i \fIname\fR 44 /* [\fB-I \fIname\fR] [-G \fIgroup\fR] 45 /* DESCRIPTION 46 /* The \fBpostmulti\fR(1) command allows a Postfix administrator 47 /* to manage multiple Postfix instances on a single host. 48 /* 49 /* \fBpostmulti\fR(1) implements two fundamental modes of 50 /* operation. In \fBiterator\fR mode, it executes the same 51 /* command for multiple Postfix instances. In \fBlife-cycle 52 /* management\fR mode, it adds or deletes one instance, or 53 /* changes the multi-instance status of one instance. 54 /* 55 /* Each mode of operation has its own command syntax. For this 56 /* reason, each mode is documented in separate sections below. 57 /* BACKGROUND 58 /* .ad 59 /* .fi 60 /* A multi-instance configuration consists of one primary 61 /* Postfix instance, and one or more secondary instances whose 62 /* configuration directory pathnames are recorded in the primary 63 /* instance's main.cf file. Postfix instances share program 64 /* files and documentation, but have their own configuration, 65 /* queue and data directories. 66 /* 67 /* Currently, only the default Postfix instance can be used 68 /* as primary instance in a multi-instance configuration. The 69 /* \fBpostmulti\fR(1) command does not currently support a \fB-c\fR 70 /* option to select an alternative primary instance, and exits 71 /* with a fatal error if the \fBMAIL_CONFIG\fR environment 72 /* variable is set to a non-default configuration directory. 73 /* 74 /* See the MULTI_INSTANCE_README tutorial for a more detailed 75 /* discussion of multi-instance management with \fBpostmulti\fR(1). 76 /* ITERATOR MODE 77 /* .ad 78 /* .fi 79 /* In iterator mode, \fBpostmulti\fR performs the same operation 80 /* on all Postfix instances in turn. 81 /* 82 /* If multi-instance support is not enabled, the requested 83 /* command is performed just for the primary instance. 84 /* .PP 85 /* Iterator mode implements the following command options: 86 /* .SH "Instance selection" 87 /* .IP \fB-a\fR 88 /* Perform the operation on all instances. This is the default. 89 /* .IP "\fB-g \fIgroup\fR" 90 /* Perform the operation only for members of the named \fIgroup\fR. 91 /* .IP "\fB-i \fIname\fR" 92 /* Perform the operation only for the instance with the specified 93 /* \fIname\fR. You can specify either the instance name 94 /* or the absolute pathname of the instance's configuration 95 /* directory. Specify "-" to select the primary Postfix instance. 96 /* .IP \fB-R\fR 97 /* Reverse the iteration order. This may be appropriate when 98 /* updating a multi-instance system, where "sink" instances 99 /* are started before "source" instances. 100 /* .sp 101 /* This option cannot be used with \fB-p\fR. 102 /* .SH "List mode" 103 /* .IP \fB-l\fR 104 /* List Postfix instances with their instance name, instance 105 /* group name, enable/disable status and configuration directory. 106 /* .SH "Postfix-wrapper mode" 107 /* .IP \fB-p\fR 108 /* Invoke \fBpostfix(1)\fR to execute the specified \fIcommand\fR. 109 /* This option implements the \fBpostfix-wrapper\fR(5) interface. 110 /* .RS 111 /* .IP \(bu 112 /* With "start"-like commands, "postfix check" is executed for 113 /* instances that are not enabled. The full list of commands 114 /* is specified with the postmulti_start_commands parameter. 115 /* .IP \(bu 116 /* With "stop"-like commands, the iteration order is reversed, 117 /* and disabled instances are skipped. The full list of commands 118 /* is specified with the postmulti_stop_commands parameter. 119 /* .IP \(bu 120 /* With "reload" and other commands that require a started 121 /* instance, disabled instances are skipped. The full list of 122 /* commands is specified with the postmulti_control_commands 123 /* parameter. 124 /* .IP \(bu 125 /* With "status" and other commands that don't require a started 126 /* instance, the command is executed for all instances. 127 /* .RE 128 /* .IP 129 /* The \fB-p\fR option can also be used interactively to 130 /* start/stop/etc. a named instance or instance group. For 131 /* example, to start just the instances in the group "msa", 132 /* invoke \fBpostmulti\fR(1) as follows: 133 /* .RS 134 /* .IP 135 /* # postmulti -g msa -p start 136 /* .RE 137 /* .SH "Command mode" 138 /* .IP \fB-x\fR 139 /* Execute the specified \fIcommand\fR for all Postfix instances. 140 /* The command runs with appropriate environment settings for 141 /* MAIL_CONFIG, command_directory, daemon_directory, 142 /* config_directory, queue_directory, data_directory, 143 /* multi_instance_name, multi_instance_group and 144 /* multi_instance_enable. 145 /* .SH "Other options" 146 /* .IP \fB-v\fR 147 /* Enable verbose logging for debugging purposes. Multiple 148 /* \fB-v\fR options make the software increasingly verbose. 149 /* LIFE-CYCLE MANAGEMENT MODE 150 /* .ad 151 /* .fi 152 /* With the \fB-e\fR option \fBpostmulti\fR(1) can be used to 153 /* add or delete a Postfix instance, and to manage the 154 /* multi-instance status of an existing instance. 155 /* .PP 156 /* The following options are implemented: 157 /* .SH "Existing instance selection" 158 /* .IP \fB-a\fR 159 /* When creating or importing an instance, place the new 160 /* instance at the front of the secondary instance list. 161 /* .IP "\fB-g \fIgroup\fR" 162 /* When creating or importing an instance, place the new 163 /* instance before the first secondary instance that is a 164 /* member of the specified group. 165 /* .IP "\fB-i \fIname\fR" 166 /* When creating or importing an instance, place the new 167 /* instance before the matching secondary instance. 168 /* .sp 169 /* With other life-cycle operations, apply the operation to 170 /* the named existing instance. Specify "-" to select the 171 /* primary Postfix instance. 172 /* .SH "New or existing instance name assignment" 173 /* .IP "\fB-I \fIname\fR" 174 /* Assign the specified instance \fIname\fR to an existing 175 /* instance, newly-created instance, or imported instance. 176 /* Instance 177 /* names other than "-" (which makes the instance "nameless") 178 /* must start with "postfix-". This restriction reduces the 179 /* likelihood of name collisions with system files. 180 /* .IP "\fB-G \fIgroup\fR" 181 /* Assign the specified \fIgroup\fR name to an existing instance 182 /* or to a newly created or imported instance. 183 /* .SH "Instance creation/deletion/status change" 184 /* .IP "\fB-e \fIaction\fR" 185 /* "Edit" managed instances. The following actions are supported: 186 /* .RS 187 /* .IP \fBinit\fR 188 /* This command is required before \fBpostmulti\fR(1) can be 189 /* used to manage Postfix instances. The "postmulti -e init" 190 /* command updates the primary instance's main.cf file by 191 /* setting: 192 /* .RS 193 /* .IP 194 /* .nf 195 /* multi_instance_wrapper = 196 /* ${command_directory}/postmulti -p -- 197 /* multi_instance_enable = yes 198 /* .fi 199 /* .RE 200 /* .IP 201 /* You can set these by other means if you prefer. 202 /* .IP \fBcreate\fR 203 /* Create a new Postfix instance and add it to the 204 /* multi_instance_directories parameter of the primary instance. 205 /* The "\fB-I \fIname\fR" option is recommended to give the 206 /* instance a short name that is used to construct default 207 /* values for the private directories of the new instance. The 208 /* "\fB-G \fIgroup\fR" option may be specified to assign the 209 /* instance to a group, otherwise, the new instance is not a 210 /* member of any groups. 211 /* .sp 212 /* The new instance main.cf is the stock main.cf with the 213 /* parameters that specify the locations of shared files cloned 214 /* from the primary instance. For "nameless" instances, you 215 /* should manually adjust "syslog_name" to yield a unique 216 /* "logtag" starting with "postfix-" that will uniquely identify 217 /* the instance in the mail logs. It is simpler to assign the 218 /* instance a short name with the "\fB-I \fIname\fR" option. 219 /* .sp 220 /* Optional "name=value" arguments specify the instance 221 /* config_directory, queue_directory and data_directory. 222 /* For example: 223 /* .RS 224 /* .IP 225 /* .nf 226 /* # postmulti -I postfix-mumble \e 227 /* -G mygroup -e create \e 228 /* config_directory=/my/config/dir \e 229 /* queue_directory=/my/queue/dir \e 230 /* data_directory=/my/data/dir 231 /* .fi 232 /* .RE 233 /* .IP 234 /* If any of these pathnames is not supplied, the program 235 /* attempts to generate the pathname by taking the corresponding 236 /* primary instance pathname, and by replacing the last pathname 237 /* component by the value of the \fB-I\fR option. 238 /* .sp 239 /* If the instance configuration directory already exists, and 240 /* contains both a main.cf and master.cf file, \fBcreate\fR 241 /* will "import" the instance as-is. For existing instances, 242 /* \fBcreate\fR and \fBimport\fR are identical. 243 /* .IP \fBimport\fR 244 /* Import an existing instance into the list of instances 245 /* managed by the \fBpostmulti\fR(1) multi-instance manager. 246 /* This adds the instance to the multi_instance_directories 247 /* list of the primary instance. If the "\fB-I \fIname\fR" 248 /* option is provided it specifies the new name for the instance 249 /* and is used to define a default location for the instance 250 /* configuration directory (as with \fBcreate\fR above). The 251 /* "\fB-G \fIgroup\fR" option may be used to assign the instance 252 /* to a group. Add a "\fBconfig_directory=\fI/path\fR" argument 253 /* to override a default pathname based on "\fB-I \fIname\fR". 254 /* .IP \fBdestroy\fR 255 /* Destroy a secondary Postfix instance. To be a candidate for 256 /* destruction an instance must be disabled, stopped and its 257 /* queue must not contain any messages. Attempts to destroy 258 /* the primary Postfix instance trigger a fatal error, without 259 /* destroying the instance. 260 /* .sp 261 /* The instance is removed from the primary instance main.cf 262 /* file's alternate_config_directories parameter and its data, 263 /* queue and configuration directories are cleaned of files 264 /* and directories created by the Postfix system. The main.cf 265 /* and master.cf files are removed from the configuration 266 /* directory even if they have been modified since initial 267 /* creation. Finally, the instance is "deported" from the list 268 /* of managed instances. 269 /* .sp 270 /* If other files are present in instance private directories, 271 /* the directories may not be fully removed, a warning is 272 /* logged to alert the administrator. It is expected that an 273 /* instance built using "fresh" directories via the \fBcreate\fR 274 /* action will be fully removed by the \fBdestroy\fR action 275 /* (if first disabled). If the instance configuration and queue 276 /* directories are populated with additional files (access and 277 /* rewriting tables, chroot jail content, etc.) the instance 278 /* directories will not be fully removed. 279 /* .sp 280 /* The \fBdestroy\fR action triggers potentially dangerous 281 /* file removal operations. Make sure the instance's data, 282 /* queue and configuration directories are set correctly and 283 /* do not contain any valuable files. 284 /* .IP \fBdeport\fR 285 /* Deport a secondary instance from the list of managed 286 /* instances. This deletes the instance configuration directory 287 /* from the primary instance's multi_instance_directories list, 288 /* but does not remove any files or directories. 289 /* .IP \fBassign\fR 290 /* Assign a new instance name or a new group name to the 291 /* selected instance. Use "\fB-G -\fR" to specify "no group" 292 /* and "\fB-I -\fR" to specify "no name". If you choose to 293 /* make an instance "nameless", set a suitable syslog_name in 294 /* the corresponding main.cf file. 295 /* .IP \fBenable\fR 296 /* Mark the selected instance as enabled. This just sets the 297 /* multi_instance_enable parameter to "yes" in the instance's 298 /* main.cf file. 299 /* .IP \fBdisable\fR 300 /* Mark the selected instance as disabled. This means that 301 /* the instance will not be started etc. with "postfix start", 302 /* "postmulti -p start" and so on. The instance can still be 303 /* started etc. with "postfix -c config-directory start". 304 /* .SH "Other options" 305 /* .IP \fB-v\fR 306 /* Enable verbose logging for debugging purposes. Multiple 307 /* \fB-v\fR options make the software increasingly verbose. 308 /* .RE 309 /* ENVIRONMENT 310 /* .ad 311 /* .fi 312 /* The \fBpostmulti\fR(1) command exports the following environment 313 /* variables before executing the requested \fIcommand\fR for a given 314 /* instance: 315 /* .IP \fBMAIL_VERBOSE\fR 316 /* This is set when the -v command-line option is present. 317 /* .IP \fBMAIL_CONFIG\fR 318 /* The location of the configuration directory of the instance. 319 /* CONFIGURATION PARAMETERS 320 /* .ad 321 /* .fi 322 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR" 323 /* The default location of the Postfix main.cf and master.cf 324 /* configuration files. 325 /* .IP "\fBdaemon_directory (see 'postconf -d' output)\fR" 326 /* The directory with Postfix support programs and daemon programs. 327 /* .IP "\fBimport_environment (see 'postconf -d' output)\fR" 328 /* The list of environment parameters that a Postfix process will 329 /* import from a non-Postfix parent process. 330 /* .IP "\fBmulti_instance_directories (empty)\fR" 331 /* An optional list of non-default Postfix configuration directories; 332 /* these directories belong to additional Postfix instances that share 333 /* the Postfix executable files and documentation with the default 334 /* Postfix instance, and that are started, stopped, etc., together 335 /* with the default Postfix instance. 336 /* .IP "\fBmulti_instance_group (empty)\fR" 337 /* The optional instance group name of this Postfix instance. 338 /* .IP "\fBmulti_instance_name (empty)\fR" 339 /* The optional instance name of this Postfix instance. 340 /* .IP "\fBmulti_instance_enable (no)\fR" 341 /* Allow this Postfix instance to be started, stopped, etc., by a 342 /* multi-instance manager. 343 /* .IP "\fBpostmulti_start_commands (start)\fR" 344 /* The \fBpostfix\fR(1) commands that the \fBpostmulti\fR(1) instance manager treats 345 /* as "start" commands. 346 /* .IP "\fBpostmulti_stop_commands (see 'postconf -d' output)\fR" 347 /* The \fBpostfix\fR(1) commands that the \fBpostmulti\fR(1) instance manager treats 348 /* as "stop" commands. 349 /* .IP "\fBpostmulti_control_commands (reload flush)\fR" 350 /* The \fBpostfix\fR(1) commands that the \fBpostmulti\fR(1) instance manager 351 /* treats as "control" commands, that operate on running instances. 352 /* .IP "\fBsyslog_facility (mail)\fR" 353 /* The syslog facility of Postfix logging. 354 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR" 355 /* The mail system name that is prepended to the process name in syslog 356 /* records, so that "smtpd" becomes, for example, "postfix/smtpd". 357 /* FILES 358 /* $daemon_directory/main.cf, stock configuration file 359 /* $daemon_directory/master.cf, stock configuration file 360 /* $daemon_directory/postmulti-script, life-cycle helper program 361 /* SEE ALSO 362 /* postfix(1), Postfix control program 363 /* postfix-wrapper(5), Postfix multi-instance API 364 /* README FILES 365 /* Use "\fBpostconf readme_directory\fR" or "\fBpostconf 366 /* html_directory\fR" to locate this information. 367 /* MULTI_INSTANCE_README, Postfix multi-instance management 368 /* HISTORY 369 /* .ad 370 /* .fi 371 /* The \fBpostmulti\fR(1) command was introduced with Postfix 372 /* version 2.6. 373 /* LICENSE 374 /* .ad 375 /* .fi 376 /* The Secure Mailer license must be distributed with this software. 377 /* AUTHOR(S) 378 /* Victor Duchovni 379 /* Morgan Stanley 380 /* 381 /* Wietse Venema 382 /* IBM T.J. Watson Research 383 /* P.O. Box 704 384 /* Yorktown Heights, NY 10598, USA 385 /*--*/ 386 387 /* System library. */ 388 389 #include <sys_defs.h> 390 #include <sys/stat.h> 391 #include <sys/wait.h> 392 #include <vstream.h> 393 #include <stdlib.h> 394 #include <unistd.h> 395 #include <string.h> 396 #include <fcntl.h> 397 #include <syslog.h> 398 #include <errno.h> 399 #include <ctype.h> 400 #ifdef USE_PATHS_H 401 #include <paths.h> 402 #endif 403 #include <stddef.h> 404 405 /* Utility library. */ 406 407 #include <msg.h> 408 #include <msg_vstream.h> 409 #include <msg_syslog.h> 410 #include <vstream.h> 411 #include <vstring_vstream.h> 412 #include <stringops.h> 413 #include <clean_env.h> 414 #include <argv.h> 415 #include <safe.h> 416 #include <mymalloc.h> 417 #include <htable.h> 418 #include <name_code.h> 419 #include <ring.h> 420 #include <warn_stat.h> 421 422 /* Global library. */ 423 424 #include <mail_version.h> 425 #include <mail_params.h> 426 #include <mail_conf.h> 427 428 /* Application-specific. */ 429 430 /* 431 * Configuration parameters, specific to postmulti(1). 432 */ 433 char *var_multi_start_cmds; 434 char *var_multi_stop_cmds; 435 char *var_multi_cntrl_cmds; 436 437 /* 438 * Shared directory pathnames. 439 */ 440 typedef struct { 441 const char *param_name; 442 char **param_value; 443 } SHARED_PATH; 444 445 static SHARED_PATH shared_dir_table[] = { 446 VAR_COMMAND_DIR, &var_command_dir, 447 VAR_DAEMON_DIR, &var_daemon_dir, 448 0, 449 }; 450 451 /* 452 * Actions. 453 */ 454 #define ITER_CMD_POSTFIX (1<<0) /* postfix(1) iterator mode */ 455 #define ITER_CMD_LIST (1<<1) /* listing iterator mode */ 456 #define ITER_CMD_GENERIC (1<<2) /* generic command iterator mode */ 457 458 #define ITER_CMD_MASK_ALL \ 459 (ITER_CMD_POSTFIX | ITER_CMD_LIST | ITER_CMD_GENERIC) 460 461 #define EDIT_CMD_CREATE (1<<4) /* create new instance */ 462 #define EDIT_CMD_IMPORT (1<<5) /* import existing instance */ 463 #define EDIT_CMD_DESTROY (1<<6) /* destroy instance */ 464 #define EDIT_CMD_DEPORT (1<<7) /* export instance */ 465 #define EDIT_CMD_ENABLE (1<<8) /* enable start/stop */ 466 #define EDIT_CMD_DISABLE (1<<9) /* disable start/stop */ 467 #define EDIT_CMD_ASSIGN (1<<10) /* assign name/group */ 468 #define EDIT_CMD_INIT (1<<11) /* hook into main.cf */ 469 470 #define EDIT_CMD_MASK_ADD (EDIT_CMD_CREATE | EDIT_CMD_IMPORT) 471 #define EDIT_CMD_MASK_DEL (EDIT_CMD_DESTROY | EDIT_CMD_DEPORT) 472 #define EDIT_CMD_MASK_ASSIGN (EDIT_CMD_MASK_ADD | EDIT_CMD_ASSIGN) 473 #define EDIT_CMD_MASK_ENB (EDIT_CMD_ENABLE | EDIT_CMD_DISABLE) 474 #define EDIT_CMD_MASK_ALL \ 475 (EDIT_CMD_MASK_ASSIGN | EDIT_CMD_MASK_DEL | EDIT_CMD_MASK_ENB | \ 476 EDIT_CMD_INIT) 477 478 /* 479 * Edit command to number mapping, and vice versa. 480 */ 481 static NAME_CODE edit_command_table[] = { 482 "create", EDIT_CMD_CREATE, 483 "import", EDIT_CMD_IMPORT, 484 "destroy", EDIT_CMD_DESTROY, 485 "deport", EDIT_CMD_DEPORT, 486 "enable", EDIT_CMD_ENABLE, 487 "disable", EDIT_CMD_DISABLE, 488 "assign", EDIT_CMD_ASSIGN, 489 "init", EDIT_CMD_INIT, 490 0, -1, 491 }; 492 493 #define EDIT_CMD_CODE(str) \ 494 name_code(edit_command_table, NAME_CODE_FLAG_STRICT_CASE, (str)) 495 #define EDIT_CMD_STR(code) str_name_code(edit_command_table, (code)) 496 497 /* 498 * Mandatory prefix for non-empty instance names. 499 */ 500 #ifndef NAME_PREFIX 501 #define NAME_PREFIX "postfix-" 502 #endif 503 #define HAS_NAME_PREFIX(name) \ 504 (strncmp((name), NAME_PREFIX, sizeof(NAME_PREFIX)-1) == 0) 505 #define NEED_NAME_PREFIX(name) \ 506 ((name) != 0 && strcmp((name), "-") != 0 && !HAS_NAME_PREFIX(name)) 507 #define NAME_SUFFIX(name) ((name) + sizeof(NAME_PREFIX) - 1) 508 509 /* 510 * In-core instance structure. Only private information is kept here. 511 */ 512 typedef struct instance { 513 RING ring; /* linkage. */ 514 char *config_dir; /* private */ 515 char *queue_dir; /* private */ 516 char *data_dir; /* private */ 517 char *name; /* null or name */ 518 char *gname; /* null or group */ 519 int enabled; /* start/stop enable */ 520 int primary; /* special */ 521 } INSTANCE; 522 523 /* 524 * Managed instance list (edit mode and iterator mode). 525 */ 526 static RING instance_hd[1]; /* instance list head */ 527 528 #define RING_TO_INSTANCE(ring_ptr) RING_TO_APPL(ring_ptr, INSTANCE, ring) 529 #define RING_PTR_OF(x) (&((x)->ring)) 530 531 #define FOREACH_INSTANCE(entry) \ 532 for ((entry) = instance_hd; \ 533 ((entry) = ring_succ(entry)) != instance_hd;) 534 535 #define FOREACH_SECONDARY_INSTANCE(entry) \ 536 for ((entry) = ring_succ(instance_hd); \ 537 ((entry) = ring_succ(entry)) != instance_hd;) 538 539 #define NEXT_ITERATOR_INSTANCE(flags, entry) \ 540 (((flags) & ITER_FLAG_REVERSE) ? ring_pred(entry) : ring_succ(entry)) 541 542 #define FOREACH_ITERATOR_INSTANCE(flags, entry) \ 543 for ((entry) = instance_hd; \ 544 ((entry) = NEXT_ITERATOR_INSTANCE(flags, (entry))) != instance_hd;) 545 546 /* 547 * Instance selection. One can either select all instances, select by 548 * instance name, or select by instance group. 549 */ 550 typedef struct { 551 int type; /* see below */ 552 char *name; /* undefined or name */ 553 } INST_SELECTION; 554 555 #define INST_SEL_NONE 0 /* default: no selection */ 556 #define INST_SEL_ALL 1 /* select all instances */ 557 #define INST_SEL_NAME 2 /* select instance name */ 558 #define INST_SEL_GROUP 3 /* select instance group */ 559 560 /* 561 * Instance name assignment. Each instance may be assigned an instance name 562 * (this must be globally unique within a multi-instance cluster) or an 563 * instance group name (this is intended to be shared). Externally, empty 564 * names may be represented as "-". Internally, we use "" only, to simplify 565 * the code. 566 */ 567 typedef struct { 568 char *name; /* null or assigned instance name */ 569 char *gname; /* null or assigned group name */ 570 } NAME_ASSIGNMENT; 571 572 /* 573 * Iterator controls for non-edit commands. One can reverse the iteration 574 * order, or give special treatment to disabled instances. 575 */ 576 #define ITER_FLAG_DEFAULT 0 /* default setting */ 577 #define ITER_FLAG_REVERSE (1<<0) /* reverse iteration order */ 578 #define ITER_FLAG_CHECK_DISABLED (1<<1) /* check disabled instances */ 579 #define ITER_FLAG_SKIP_DISABLED (1<<2) /* skip disabled instances */ 580 581 /* 582 * Environment export controls for edit commands. postmulti(1) exports only 583 * things that need to be updated. 584 */ 585 #define EXP_FLAG_MULTI_DIRS (1<<0) /* export multi_instance_directories */ 586 #define EXP_FLAG_MULTI_NAME (1<<1) /* export multi_instance_name */ 587 #define EXP_FLAG_MULTI_GROUP (1<<2) /* export multi_instance_group */ 588 589 /* 590 * To detect conflicts, each instance name and each shared or private 591 * pathname is registered in one place, with its owner. Everyone must 592 * register their claims when they join, and will be rejected in case of 593 * conlict. 594 * 595 * Each claim value involves a parameter value (either a directory name or an 596 * instance name). Each claim owner is the config_directory pathname plus 597 * the parameter name. 598 * 599 * XXX: No multi.cf lock file, so this is not race-free. 600 */ 601 static HTABLE *claim_table; 602 603 #define IS_CLAIMED_BY(name) \ 604 (claim_table ? htable_find(claim_table, (name)) : 0) 605 606 /* 607 * Forward references. 608 */ 609 static int iterate_command(int, int, char **, INST_SELECTION *); 610 static int match_instance_selection(INSTANCE *, INST_SELECTION *); 611 612 /* 613 * Convenience. 614 */ 615 #define INSTANCE_NAME(i) ((i)->name ? (i)->name : (i)->config_dir) 616 #define STR(buf) vstring_str(buf) 617 618 /* register_claim - register claim or bust */ 619 620 static void register_claim(const char *instance_path, const char *param_name, 621 const char *param_value) 622 { 623 const char *myname = "register_claim"; 624 char *requestor; 625 const char *owner; 626 627 /* 628 * Sanity checks. 629 */ 630 if (instance_path == 0 || *instance_path == 0) 631 msg_panic("%s: no or empty instance pathname", myname); 632 if (param_name == 0 || *param_name == 0) 633 msg_panic("%s: no or empty parameter name", myname); 634 if (param_value == 0) 635 msg_panic("%s: no parameter value", myname); 636 637 /* 638 * Make a claim or report a conflict. 639 */ 640 if (claim_table == 0) 641 claim_table = htable_create(100); 642 requestor = concatenate(instance_path, ", ", param_name, (char *) 0); 643 if ((owner = htable_find(claim_table, param_value)) == 0) { 644 (void) htable_enter(claim_table, param_value, requestor); 645 } else if (strcmp(owner, requestor) == 0) { 646 myfree(requestor); 647 } else { 648 msg_fatal("instance %s, %s=%s conflicts with instance %s=%s", 649 instance_path, param_name, param_value, owner, param_value); 650 } 651 } 652 653 /* claim_instance_attributes - claim multiple private instance attributes */ 654 655 static void claim_instance_attributes(INSTANCE *ip) 656 { 657 658 /* 659 * Detect instance name or pathname conflicts between this instance and 660 * other instances. XXX: No multi.cf lock file, so this is not race-free. 661 */ 662 if (ip->name) 663 register_claim(ip->config_dir, VAR_MULTI_NAME, ip->name); 664 register_claim(ip->config_dir, VAR_CONFIG_DIR, ip->config_dir); 665 register_claim(ip->config_dir, VAR_QUEUE_DIR, ip->queue_dir); 666 register_claim(ip->config_dir, VAR_DATA_DIR, ip->data_dir); 667 } 668 669 /* alloc_instance - allocate a single instance object */ 670 671 static INSTANCE *alloc_instance(const char *config_dir) 672 { 673 INSTANCE *ip = (INSTANCE *) mymalloc(sizeof(INSTANCE)); 674 675 ring_init(RING_PTR_OF(ip)); 676 ip->config_dir = config_dir ? mystrdup(config_dir) : 0; 677 ip->queue_dir = 0; 678 ip->data_dir = 0; 679 ip->name = 0; 680 ip->gname = 0; 681 ip->enabled = 0; 682 ip->primary = 0; 683 684 return (ip); 685 } 686 687 #if 0 688 689 /* free_instance - free a single instance object */ 690 691 static void free_instance(INSTANCE *ip) 692 { 693 694 /* 695 * If we continue after secondary main.cf file read error, we must be 696 * prepared for the case that some parameters may be missing. 697 */ 698 if (ip->name) 699 myfree(ip->name); 700 if (ip->gname) 701 myfree(ip->gname); 702 if (ip->config_dir) 703 myfree(ip->config_dir); 704 if (ip->queue_dir) 705 myfree(ip->queue_dir); 706 if (ip->data_dir) 707 myfree(ip->data_dir); 708 myfree((char *) ip); 709 } 710 711 #endif 712 713 /* insert_instance - insert instance before selected location, claim names */ 714 715 static void insert_instance(INSTANCE *ip, INST_SELECTION *selection) 716 { 717 RING *old; 718 719 #define append_instance(ip) insert_instance((ip), (INST_SELECTION *) 0) 720 721 /* 722 * Insert instance before the selected site. 723 */ 724 claim_instance_attributes(ip); 725 if (ring_succ(instance_hd) == 0) 726 ring_init(instance_hd); 727 if (selection && selection->type != INST_SEL_NONE) { 728 FOREACH_SECONDARY_INSTANCE(old) { 729 if (match_instance_selection(RING_TO_INSTANCE(old), selection)) { 730 ring_prepend(old, RING_PTR_OF(ip)); 731 return; 732 } 733 } 734 if (selection->type != INST_SEL_ALL) 735 msg_fatal("No matching secondary instances"); 736 } 737 ring_prepend(instance_hd, RING_PTR_OF(ip)); 738 } 739 740 /* create_primary_instance - synthetic entry for primary instance */ 741 742 static INSTANCE *create_primary_instance(void) 743 { 744 INSTANCE *ip = alloc_instance(var_config_dir); 745 746 /* 747 * There is no need to load primary instance paramater settings from 748 * file. We already have the main.cf parameters of interest in memory. 749 */ 750 #define SAVE_INSTANCE_NAME(val) (*(val) ? mystrdup(val) : 0) 751 752 ip->name = SAVE_INSTANCE_NAME(var_multi_name); 753 ip->gname = SAVE_INSTANCE_NAME(var_multi_group); 754 ip->enabled = var_multi_enable; 755 ip->queue_dir = mystrdup(var_queue_dir); 756 ip->data_dir = mystrdup(var_data_dir); 757 ip->primary = 1; 758 return (ip); 759 } 760 761 /* load_instance - read instance parameters from config_dir/main.cf */ 762 763 static INSTANCE *load_instance(INSTANCE *ip) 764 { 765 VSTREAM *pipe; 766 VSTRING *buf; 767 char *name; 768 char *value; 769 ARGV *cmd; 770 int count = 0; 771 static NAME_CODE bool_code[] = { 772 CONFIG_BOOL_YES, 1, 773 CONFIG_BOOL_NO, 0, 774 0, -1, 775 }; 776 777 /* 778 * XXX: We could really use a "postconf -E" to expand values in the 779 * context of the target main.cf! 780 */ 781 #define REQUEST_PARAM_COUNT 5 /* # of requested parameters */ 782 783 cmd = argv_alloc(REQUEST_PARAM_COUNT + 3); 784 name = concatenate(var_command_dir, "/", "postconf", (char *) 0); 785 argv_add(cmd, name, "-c", ip->config_dir, 786 VAR_QUEUE_DIR, VAR_DATA_DIR, 787 VAR_MULTI_NAME, VAR_MULTI_GROUP, VAR_MULTI_ENABLE, 788 (char *) 0); 789 myfree(name); 790 pipe = vstream_popen(O_RDONLY, VSTREAM_POPEN_ARGV, cmd->argv, 791 VSTREAM_POPEN_END); 792 argv_free(cmd); 793 if (pipe == 0) 794 msg_fatal("Cannot parse %s/main.cf file: %m", ip->config_dir); 795 796 /* 797 * Read parameter settings from postconf. See also comments below on 798 * whether we should continue or skip groups after error instead of 799 * bailing out immediately. 800 */ 801 buf = vstring_alloc(100); 802 while (vstring_get_nonl(buf, pipe) != VSTREAM_EOF) { 803 if (split_nameval(STR(buf), &name, &value)) 804 msg_fatal("Invalid %s/main.cf parameter: %s", 805 ip->config_dir, STR(buf)); 806 if (strcmp(name, VAR_QUEUE_DIR) == 0 && ++count) 807 ip->queue_dir = mystrdup(value); 808 else if (strcmp(name, VAR_DATA_DIR) == 0 && ++count) 809 ip->data_dir = mystrdup(value); 810 else if (strcmp(name, VAR_MULTI_NAME) == 0 && ++count) 811 ip->name = SAVE_INSTANCE_NAME(value); 812 else if (strcmp(name, VAR_MULTI_GROUP) == 0 && ++count) 813 ip->gname = SAVE_INSTANCE_NAME(value); 814 else if (strcmp(name, VAR_MULTI_ENABLE) == 0 && ++count) { 815 /* mail_conf_bool(3) is case insensitive! */ 816 ip->enabled = name_code(bool_code, NAME_CODE_FLAG_NONE, value); 817 if (ip->enabled < 0) 818 msg_fatal("Unexpected %s/main.cf entry: %s = %s", 819 ip->config_dir, VAR_MULTI_ENABLE, value); 820 } 821 } 822 vstring_free(buf); 823 824 /* 825 * XXX We should not bail out while reading a bad secondary main.cf file. 826 * When we manage dozens or more instances, the likelihood increases that 827 * some file will be damaged or missing after a system crash. That is not 828 * a good reason to prevent undamaged Postfix instances from starting. 829 */ 830 if (count != REQUEST_PARAM_COUNT) 831 msg_fatal("Failed to obtain all required %s/main.cf parameters", 832 ip->config_dir); 833 834 if (vstream_pclose(pipe)) 835 msg_fatal("Cannot parse %s/main.cf file", ip->config_dir); 836 return (ip); 837 } 838 839 /* load_all_instances - compute list of Postfix instances */ 840 841 static void load_all_instances(void) 842 { 843 INSTANCE *primary_instance; 844 char **cpp; 845 ARGV *secondary_names; 846 847 /* 848 * Avoid unexpected behavior when $multi_instance_directories contains 849 * only comma characters. Count the actual number of elements, before we 850 * decide that the list is empty. 851 */ 852 secondary_names = argv_split(var_multi_conf_dirs, "\t\n\r, "); 853 854 /* 855 * First, the primary instance. This is synthesized out of thin air. 856 */ 857 primary_instance = create_primary_instance(); 858 if (secondary_names->argc == 0) 859 primary_instance->enabled = 1; /* Single-instance mode */ 860 append_instance(primary_instance); 861 862 /* 863 * Next, instances defined in $multi_instance_directories. Note: 864 * load_instance() has side effects on the global config dictionary, but 865 * this does not affect the values that have already been extracted into 866 * C variables. 867 */ 868 for (cpp = secondary_names->argv; *cpp != 0; cpp++) 869 append_instance(load_instance(alloc_instance(*cpp))); 870 871 argv_free(secondary_names); 872 } 873 874 /* match_instance_selection - match all/name/group constraints */ 875 876 static int match_instance_selection(INSTANCE *ip, INST_SELECTION *selection) 877 { 878 char *iname; 879 char *name; 880 881 /* 882 * When selecting (rather than assigning names) an instance, we match by 883 * the instance name, config_directory path, or the instance name suffix 884 * (name without mandatory prefix). Selecting "-" selects the primary 885 * instance. 886 */ 887 switch (selection->type) { 888 case INST_SEL_NONE: 889 return (0); 890 case INST_SEL_ALL: 891 return (1); 892 case INST_SEL_GROUP: 893 return (ip->gname != 0 && strcmp(selection->name, ip->gname) == 0); 894 case INST_SEL_NAME: 895 name = selection->name; 896 if (*name == '/' || ip->name == 0) 897 iname = ip->config_dir; 898 else if (!HAS_NAME_PREFIX(name) && HAS_NAME_PREFIX(ip->name)) 899 iname = NAME_SUFFIX(ip->name); 900 else 901 iname = ip->name; 902 return (strcmp(name, iname) == 0 903 || (ip->primary && strcmp(name, "-") == 0)); 904 default: 905 msg_panic("match_instance_selection: unknown selection type: %d", 906 selection->type); 907 } 908 } 909 910 /* check_setenv - setenv() with extreme prejudice */ 911 912 static void check_setenv(const char *name, const char *value) 913 { 914 #define CLOBBER 1 915 if (setenv(name, value, CLOBBER) < 0) 916 msg_fatal("setenv: %m"); 917 } 918 919 /* prepend_command_path - prepend command_directory to PATH */ 920 921 static void prepend_command_path(void) 922 { 923 char *cmd_path; 924 925 /* 926 * Carefully prepend "$command_directory:" to PATH. We can free the 927 * buffer after check_setenv(), since the value is copied there. 928 */ 929 cmd_path = safe_getenv("PATH"); 930 cmd_path = concatenate(var_command_dir, ":", (cmd_path && *cmd_path) ? 931 cmd_path : ROOT_PATH, (char *) 0); 932 check_setenv("PATH", cmd_path); 933 myfree(cmd_path); 934 } 935 936 /* check_shared_dir_status - check and claim shared directories */ 937 938 static void check_shared_dir_status(void) 939 { 940 struct stat st; 941 const SHARED_PATH *sp; 942 943 for (sp = shared_dir_table; sp->param_name; ++sp) { 944 if (stat(sp->param_value[0], &st) < 0) 945 msg_fatal("%s = '%s': directory not found: %m", 946 sp->param_name, sp->param_value[0]); 947 if (!S_ISDIR(st.st_mode)) 948 msg_fatal("%s = '%s' is not a directory", 949 sp->param_name, sp->param_value[0]); 950 register_claim(var_config_dir, sp->param_name, sp->param_value[0]); 951 } 952 } 953 954 /* check_safe_name - allow instance or group name with only "safe" characters */ 955 956 static int check_safe_name(const char *s) 957 { 958 #define SAFE_PUNCT "!@%-_=+:./" 959 if (*s == 0) 960 return (0); 961 for (; *s; ++s) { 962 if (!ISALNUM(*s) && !strchr(SAFE_PUNCT, *s)) 963 return (0); 964 } 965 return (1); 966 } 967 968 /* check_name_assignments - Check validity of assigned instance or group name */ 969 970 static void check_name_assignments(NAME_ASSIGNMENT *assignment) 971 { 972 973 /* 974 * Syntax check the assigned instance name. This name is also used to 975 * generate directory pathnames, so we must not allow "/" characters. 976 * 977 * The value "" will clear the name and is always valid. The command-line 978 * parser has already converted "-" into "", to simplify implementation. 979 */ 980 if (assignment->name && *assignment->name) { 981 if (!check_safe_name(assignment->name)) 982 msg_fatal("Unsafe characters in new instance name: '%s'", 983 assignment->name); 984 if (strchr(assignment->name, '/')) 985 msg_fatal("Illegal '/' character in new instance name: '%s'", 986 assignment->name); 987 if (NEED_NAME_PREFIX(assignment->name)) 988 msg_fatal("New instance name must start with '%s'", 989 NAME_PREFIX); 990 } 991 992 /* 993 * Syntax check the assigned group name. 994 */ 995 if (assignment->gname && *assignment->gname) { 996 if (!check_safe_name(assignment->gname)) 997 msg_fatal("Unsafe characters in '-G %s'", assignment->gname); 998 } 999 } 1000 1001 /* do_name_assignments - assign instance/group names */ 1002 1003 static int do_name_assignments(INSTANCE *target, NAME_ASSIGNMENT *assignment) 1004 { 1005 int export_flags = 0; 1006 1007 /* 1008 * The command-line parser has already converted "-" into "", to simplify 1009 * implementation. 1010 */ 1011 if (assignment->name 1012 && strcmp(assignment->name, target->name ? target->name : "")) { 1013 register_claim(target->config_dir, VAR_MULTI_NAME, assignment->name); 1014 if (target->name) 1015 myfree(target->name); 1016 target->name = SAVE_INSTANCE_NAME(assignment->name); 1017 export_flags |= EXP_FLAG_MULTI_NAME; 1018 } 1019 if (assignment->gname 1020 && strcmp(assignment->gname, target->gname ? target->gname : "")) { 1021 if (target->gname) 1022 myfree(target->gname); 1023 target->gname = SAVE_INSTANCE_NAME(assignment->gname); 1024 export_flags |= EXP_FLAG_MULTI_GROUP; 1025 } 1026 return (export_flags); 1027 } 1028 1029 /* make_private_path - generate secondary pathname using primary as template */ 1030 1031 static char *make_private_path(const char *param_name, 1032 const char *primary_value, 1033 NAME_ASSIGNMENT *assignment) 1034 { 1035 char *path; 1036 char *base; 1037 char *end; 1038 1039 /* 1040 * The command-line parser has already converted "-" into "", to simplify 1041 * implementation. 1042 */ 1043 if (assignment->name == 0 || *assignment->name == 0) 1044 msg_fatal("Missing %s parameter value", param_name); 1045 1046 if (*primary_value != '/') 1047 msg_fatal("Invalid default %s parameter value: '%s': " 1048 "specify an absolute pathname", 1049 param_name, primary_value); 1050 1051 base = mystrdup(primary_value); 1052 if ((end = strrchr(base, '/')) != 0) { 1053 /* Drop trailing slashes */ 1054 if (end[1] == '\0') { 1055 while (--end > base && *end == '/') 1056 *end = '\0'; 1057 end = strrchr(base, '/'); 1058 } 1059 /* Drop last path component */ 1060 while (end > base && *end == '/') 1061 *end-- = '\0'; 1062 } 1063 path = concatenate(base[1] ? base : "", "/", 1064 assignment->name, (char *) 0); 1065 myfree(base); 1066 return (path); 1067 } 1068 1069 /* assign_new_parameter - assign new instance private name=value */ 1070 1071 static void assign_new_parameter(INSTANCE *new, int edit_cmd, 1072 const char *arg) 1073 { 1074 char *saved_arg; 1075 char *name; 1076 char *value; 1077 char *end; 1078 char **target = 0; 1079 1080 /* 1081 * With "import", only config_directory is specified on the command line 1082 * (either explicitly as config_directory=/path/name, or implicitly as 1083 * instance name). The other private directory pathnames are taken from 1084 * the existing instance's main.cf file. 1085 * 1086 * With "create", all private pathname parameters are specified on the 1087 * command line, or generated from an instance name. 1088 */ 1089 saved_arg = mystrdup(arg); 1090 if (split_nameval(saved_arg, &name, &value)) 1091 msg_fatal("Malformed parameter setting '%s'", arg); 1092 1093 if (strcmp(VAR_CONFIG_DIR, name) == 0) { 1094 target = &new->config_dir; 1095 } else if (edit_cmd != EDIT_CMD_IMPORT) { 1096 if (strcmp(VAR_QUEUE_DIR, name) == 0) { 1097 target = &new->queue_dir; 1098 } else if (strcmp(VAR_DATA_DIR, name) == 0) { 1099 target = &new->data_dir; 1100 } 1101 } 1102 if (target == 0) 1103 msg_fatal("Parameter '%s' not valid with action %s", 1104 name, EDIT_CMD_STR(edit_cmd)); 1105 1106 /* 1107 * Extract and assign the parameter value. We do a limited number of 1108 * checks here. Conflicts between instances are checked by the caller. 1109 * More checks may be implemented in the helper script if inspired. 1110 */ 1111 if (*value != '/') 1112 msg_fatal("Parameter setting '%s' is not an absolute path", name); 1113 1114 /* Tolerate+trim trailing "/" from readline completion */ 1115 for (end = value + strlen(value) - 1; end > value && *end == '/'; --end) 1116 *end = 0; 1117 1118 /* No checks here for "/." or other shoot-foot silliness. */ 1119 if (end == value) 1120 msg_fatal("Parameter setting '%s' is the root directory", name); 1121 1122 if (*target) 1123 myfree(*target); 1124 *target = mystrdup(value); 1125 1126 /* 1127 * Cleanup. 1128 */ 1129 myfree(saved_arg); 1130 } 1131 1132 /* assign_new_parameters - initialize new instance private parameters */ 1133 1134 static void assign_new_parameters(INSTANCE *new, int edit_cmd, 1135 char **argv, NAME_ASSIGNMENT *assignment) 1136 { 1137 const char *owner; 1138 1139 /* 1140 * Sanity check the explicit parameter settings. More stringent checks 1141 * may take place in the helper script. 1142 */ 1143 while (*argv) 1144 assign_new_parameter(new, edit_cmd, *argv++); 1145 1146 /* 1147 * Initialize any missing private directory pathnames, using the primary 1148 * configuration directory parameter values as a template, and using the 1149 * assigned instance name to fill in the blanks. 1150 * 1151 * When importing an existing instance, load private directory pathnames 1152 * from its main.cf file. 1153 */ 1154 if (new->config_dir == 0) 1155 new->config_dir = 1156 make_private_path(VAR_CONFIG_DIR, var_config_dir, assignment); 1157 /* Needed for better-quality error message. */ 1158 if ((owner = IS_CLAIMED_BY(new->config_dir)) != 0) 1159 msg_fatal("new %s=%s is already in use by instance %s=%s", 1160 VAR_CONFIG_DIR, new->config_dir, owner, new->config_dir); 1161 if (edit_cmd != EDIT_CMD_IMPORT) { 1162 if (new->queue_dir == 0) 1163 new->queue_dir = 1164 make_private_path(VAR_QUEUE_DIR, var_queue_dir, assignment); 1165 if (new->data_dir == 0) 1166 new->data_dir = 1167 make_private_path(VAR_DATA_DIR, var_data_dir, assignment); 1168 } else { 1169 load_instance(new); 1170 } 1171 } 1172 1173 /* export_helper_environment - update environment settings for helper command */ 1174 1175 static void export_helper_environment(INSTANCE *target, int export_flags) 1176 { 1177 ARGV *import_env; 1178 VSTRING *multi_dirs; 1179 const SHARED_PATH *sp; 1180 RING *entry; 1181 1182 /* 1183 * Environment import filter, to enforce consistent behavior whether this 1184 * command is started by hand, or at system boot time. This is necessary 1185 * because some shell scripts use environment settings to override 1186 * main.cf settings. 1187 */ 1188 import_env = argv_split(var_import_environ, ", \t\r\n"); 1189 clean_env(import_env->argv); 1190 argv_free(import_env); 1191 1192 /* 1193 * Prepend $command_directory: to PATH. This supposedly ensures that 1194 * naive programs will execute commands from the right Postfix version. 1195 */ 1196 prepend_command_path(); 1197 1198 /* 1199 * The following ensures that Postfix's own programs will target the 1200 * primary instance. 1201 */ 1202 check_setenv(CONF_ENV_PATH, var_config_dir); 1203 1204 /* 1205 * Export the parameter settings that are shared between instances. 1206 */ 1207 for (sp = shared_dir_table; sp->param_name; ++sp) 1208 check_setenv(sp->param_name, sp->param_value[0]); 1209 1210 /* 1211 * Export the target instance's private directory locations. 1212 */ 1213 check_setenv(VAR_CONFIG_DIR, target->config_dir); 1214 check_setenv(VAR_QUEUE_DIR, target->queue_dir); 1215 check_setenv(VAR_DATA_DIR, target->data_dir); 1216 1217 /* 1218 * With operations that add or delete a secondary instance, we export the 1219 * modified multi_instance_directories parameter value for the primary 1220 * Postfix instance. 1221 */ 1222 if (export_flags & EXP_FLAG_MULTI_DIRS) { 1223 multi_dirs = vstring_alloc(100); 1224 FOREACH_SECONDARY_INSTANCE(entry) { 1225 if (VSTRING_LEN(multi_dirs) > 0) 1226 VSTRING_ADDCH(multi_dirs, ' '); 1227 vstring_strcat(multi_dirs, RING_TO_INSTANCE(entry)->config_dir); 1228 } 1229 check_setenv(VAR_MULTI_CONF_DIRS, STR(multi_dirs)); 1230 vstring_free(multi_dirs); 1231 } 1232 1233 /* 1234 * Export updates for the instance name and group. Empty value (or no 1235 * export) means don't update, "-" means clear. 1236 */ 1237 if (export_flags & EXP_FLAG_MULTI_NAME) 1238 check_setenv(VAR_MULTI_NAME, target->name && *target->name ? 1239 target->name : "-"); 1240 1241 if (export_flags & EXP_FLAG_MULTI_GROUP) 1242 check_setenv(VAR_MULTI_GROUP, target->gname && *target->gname ? 1243 target->gname : "-"); 1244 1245 /* 1246 * If we would implement enable/disable commands by exporting the updated 1247 * parameter value, then we could skip commands that have no effect, just 1248 * like we can skip "assign" commands that make no change. 1249 */ 1250 } 1251 1252 /* install_new_instance - install and return newly created instance */ 1253 1254 static INSTANCE *install_new_instance(int edit_cmd, char **argv, 1255 INST_SELECTION *selection, 1256 NAME_ASSIGNMENT *assignment, 1257 int *export_flags) 1258 { 1259 INSTANCE *new; 1260 1261 new = alloc_instance((char *) 0); 1262 check_name_assignments(assignment); 1263 assign_new_parameters(new, edit_cmd, argv, assignment); 1264 *export_flags |= 1265 (do_name_assignments(new, assignment) | EXP_FLAG_MULTI_DIRS); 1266 insert_instance(new, selection); 1267 return (new); 1268 } 1269 1270 /* update_instance - update existing instance, return export flags */ 1271 1272 static int update_instance(INSTANCE *target, NAME_ASSIGNMENT *assignment) 1273 { 1274 int export_flags; 1275 1276 check_name_assignments(assignment); 1277 export_flags = do_name_assignments(target, assignment); 1278 return (export_flags); 1279 } 1280 1281 /* select_existing_instance - return instance selected for management */ 1282 1283 static INSTANCE *select_existing_instance(INST_SELECTION *selection, 1284 int unlink_flag, 1285 int *export_flags) 1286 { 1287 INSTANCE *selected = 0; 1288 RING *entry; 1289 INSTANCE *ip; 1290 1291 #define DONT_UNLINK 0 1292 #define DO_UNLINK 1 1293 1294 if (selection->type != INST_SEL_NAME) 1295 msg_fatal("Select an instance via '-i name'"); 1296 1297 /* Find the selected instance and its predecessor */ 1298 FOREACH_INSTANCE(entry) { 1299 if (match_instance_selection(ip = RING_TO_INSTANCE(entry), selection)) { 1300 selected = ip; 1301 break; 1302 } 1303 } 1304 1305 if (selected == 0) 1306 msg_fatal("No instance named %s", selection->name); 1307 1308 if (unlink_flag) { 1309 /* Splice the target instance out of the list */ 1310 if (ring_pred(entry) == instance_hd) 1311 msg_fatal("Cannot remove the primary instance"); 1312 if (selected->enabled) 1313 msg_fatal("Cannot remove enabled instances"); 1314 ring_detach(entry); 1315 if (export_flags == 0) 1316 msg_panic("select_existing_instance: no export flags"); 1317 *export_flags |= EXP_FLAG_MULTI_DIRS; 1318 } 1319 return (selected); 1320 } 1321 1322 /* manage - create/destroy/... manage instances */ 1323 1324 static NORETURN manage(int edit_cmd, int argc, char **argv, 1325 INST_SELECTION *selection, 1326 NAME_ASSIGNMENT *assignment) 1327 { 1328 char *cmd; 1329 INSTANCE *target; 1330 int export_flags; 1331 1332 /* 1333 * Edit mode is not subject to iterator controls. 1334 */ 1335 #define NO_EXPORT_FLAGS ((int *) 0) 1336 export_flags = 0; 1337 1338 switch (edit_cmd) { 1339 case EDIT_CMD_INIT: 1340 target = create_primary_instance(); 1341 break; 1342 1343 case EDIT_CMD_CREATE: 1344 case EDIT_CMD_IMPORT: 1345 load_all_instances(); 1346 target = install_new_instance(edit_cmd, argv, selection, 1347 assignment, &export_flags); 1348 break; 1349 1350 case EDIT_CMD_ASSIGN: 1351 load_all_instances(); 1352 target = 1353 select_existing_instance(selection, DONT_UNLINK, NO_EXPORT_FLAGS); 1354 export_flags |= update_instance(target, assignment); 1355 if (export_flags == 0) 1356 exit(0); 1357 break; 1358 1359 case EDIT_CMD_DESTROY: 1360 case EDIT_CMD_DEPORT: 1361 load_all_instances(); 1362 target = select_existing_instance(selection, DO_UNLINK, &export_flags); 1363 break; 1364 1365 default: 1366 load_all_instances(); 1367 target = 1368 select_existing_instance(selection, DONT_UNLINK, NO_EXPORT_FLAGS); 1369 break; 1370 } 1371 1372 /* 1373 * Set up the helper script's process environment, and execute the helper 1374 * script. 1375 */ 1376 #define HELPER "postmulti-script" 1377 1378 export_helper_environment(target, export_flags); 1379 cmd = concatenate(var_daemon_dir, "/" HELPER, (char *) 0); 1380 execl(cmd, cmd, "-e", EDIT_CMD_STR(edit_cmd), (char *) 0); 1381 msg_fatal("%s: %m", cmd); 1382 } 1383 1384 /* run_user_command - execute external command with requested MAIL_CONFIG env */ 1385 1386 static int run_user_command(INSTANCE *ip, int iter_cmd, int iter_flags, 1387 char **argv) 1388 { 1389 WAIT_STATUS_T status; 1390 int pid; 1391 int wpid; 1392 1393 /* 1394 * Set up a process environment. The postfix(1) command needs MAIL_CONFIG 1395 * (or the equivalent command-line option); it overrides everything else. 1396 * 1397 * postmulti(1) typically runs various Postfix utilities (postsuper, ...) in 1398 * the context of one or more instances. It can also run various scripts 1399 * on the users PATH. So we can't clobber the user's PATH, but do want to 1400 * make sure that the utilities in $command_directory are always found in 1401 * the right place (or at all). 1402 */ 1403 switch (pid = fork()) { 1404 case -1: 1405 msg_warn("fork %s: %m", argv[0]); 1406 return -1; 1407 case 0: 1408 check_setenv(CONF_ENV_PATH, ip->config_dir); 1409 if (iter_cmd != ITER_CMD_POSTFIX) { 1410 check_setenv(VAR_DAEMON_DIR, var_daemon_dir); 1411 check_setenv(VAR_COMMAND_DIR, var_command_dir); 1412 check_setenv(VAR_CONFIG_DIR, ip->config_dir); 1413 check_setenv(VAR_QUEUE_DIR, ip->queue_dir); 1414 check_setenv(VAR_DATA_DIR, ip->data_dir); 1415 check_setenv(VAR_MULTI_NAME, ip->name ? ip->name : ""); 1416 check_setenv(VAR_MULTI_GROUP, ip->gname ? ip->gname : ""); 1417 check_setenv(VAR_MULTI_ENABLE, ip->enabled ? 1418 CONFIG_BOOL_YES : CONFIG_BOOL_NO); 1419 prepend_command_path(); 1420 } 1421 1422 /* 1423 * Replace: postfix -- start ... With: postfix -- check ... 1424 */ 1425 if (iter_cmd == ITER_CMD_POSTFIX 1426 && (iter_flags & ITER_FLAG_CHECK_DISABLED) && !ip->enabled) 1427 argv[2] = "check"; 1428 1429 execvp(argv[0], argv); 1430 msg_fatal("execvp %s: %m", argv[0]); 1431 default: 1432 do { 1433 wpid = waitpid(pid, &status, 0); 1434 } while (wpid == -1 && errno == EINTR); 1435 return (wpid == -1 ? -1 : 1436 WIFEXITED(status) ? WEXITSTATUS(status) : 1); 1437 } 1438 } 1439 1440 /* word_in_list - look up command in start, stop, or control list */ 1441 1442 static int word_in_list(char *cmdlist, const char *cmd) 1443 { 1444 char *saved; 1445 char *cp; 1446 char *elem; 1447 1448 cp = saved = mystrdup(cmdlist); 1449 while ((elem = mystrtok(&cp, "\t\n\r, ")) != 0 && strcmp(elem, cmd) != 0) 1450 /* void */ ; 1451 myfree(saved); 1452 return (elem != 0); 1453 } 1454 1455 /* iterate_postfix_command - execute postfix(1) command */ 1456 1457 static int iterate_postfix_command(int iter_cmd, int argc, char **argv, 1458 INST_SELECTION *selection) 1459 { 1460 int exit_status; 1461 char *cmd; 1462 ARGV *my_argv; 1463 int iter_flags; 1464 1465 /* 1466 * Override the iterator controls. 1467 */ 1468 if (word_in_list(var_multi_start_cmds, argv[0])) { 1469 iter_flags = ITER_FLAG_CHECK_DISABLED; 1470 } else if (word_in_list(var_multi_stop_cmds, argv[0])) { 1471 iter_flags = ITER_FLAG_SKIP_DISABLED | ITER_FLAG_REVERSE; 1472 } else if (word_in_list(var_multi_cntrl_cmds, argv[0])) { 1473 iter_flags = ITER_FLAG_SKIP_DISABLED; 1474 } else { 1475 iter_flags = 0; 1476 } 1477 1478 /* 1479 * Override the command line in a straightforward manner: prepend 1480 * "postfix --" to the command arguments. Other overrides (environment, 1481 * start -> check) are implemented below the iterator. 1482 */ 1483 #define POSTFIX_CMD "postfix" 1484 1485 my_argv = argv_alloc(argc + 2); 1486 cmd = concatenate(var_command_dir, "/" POSTFIX_CMD, (char *) 0); 1487 argv_add(my_argv, cmd, "--", (char *) 0); 1488 myfree(cmd); 1489 while (*argv) 1490 argv_add(my_argv, *argv++, (char *) 0); 1491 1492 /* 1493 * Execute the command for all applicable Postfix instances. 1494 */ 1495 exit_status = 1496 iterate_command(iter_cmd, iter_flags, my_argv->argv, selection); 1497 1498 argv_free(my_argv); 1499 return (exit_status); 1500 } 1501 1502 /* list_instances - list all selected instances */ 1503 1504 static void list_instances(int iter_flags, INST_SELECTION *selection) 1505 { 1506 RING *entry; 1507 INSTANCE *ip; 1508 1509 /* 1510 * Iterate over the selected instances. 1511 */ 1512 FOREACH_ITERATOR_INSTANCE(iter_flags, entry) { 1513 ip = RING_TO_INSTANCE(entry); 1514 if (match_instance_selection(ip, selection)) 1515 vstream_printf("%-15s %-15s %-9s %s\n", 1516 ip->name ? ip->name : "-", 1517 ip->gname ? ip->gname : "-", 1518 ip->enabled ? "y" : "n", 1519 ip->config_dir); 1520 } 1521 if (vstream_fflush(VSTREAM_OUT)) 1522 msg_fatal("error writing output: %m"); 1523 } 1524 1525 /* iterate_command - execute command for selected instances */ 1526 1527 static int iterate_command(int iter_cmd, int iter_flags, char **argv, 1528 INST_SELECTION *selection) 1529 { 1530 int exit_status = 0; 1531 int matched = 0; 1532 RING *entry; 1533 INSTANCE *ip; 1534 1535 /* 1536 * Iterate over the selected instances. 1537 */ 1538 FOREACH_ITERATOR_INSTANCE(iter_flags, entry) { 1539 ip = RING_TO_INSTANCE(entry); 1540 if ((iter_flags & ITER_FLAG_SKIP_DISABLED) && !ip->enabled) 1541 continue; 1542 if (!match_instance_selection(ip, selection)) 1543 continue; 1544 matched = 1; 1545 1546 /* Run the requested command */ 1547 if (run_user_command(ip, iter_cmd, iter_flags, argv) != 0) 1548 exit_status = 1; 1549 } 1550 if (matched == 0) 1551 msg_fatal("No matching instances"); 1552 1553 return (exit_status); 1554 } 1555 1556 /* iterate - Iterate over all or selected instances */ 1557 1558 static NORETURN iterate(int iter_cmd, int iter_flags, int argc, char **argv, 1559 INST_SELECTION *selection) 1560 { 1561 int exit_status; 1562 1563 /* 1564 * In iterator mode, no selection means wild-card selection. 1565 */ 1566 if (selection->type == INST_SEL_NONE) 1567 selection->type = INST_SEL_ALL; 1568 1569 /* 1570 * Load the in-memory instance table from main.cf files. 1571 */ 1572 load_all_instances(); 1573 1574 /* 1575 * Iterate over the selected instances. 1576 */ 1577 switch (iter_cmd) { 1578 case ITER_CMD_POSTFIX: 1579 exit_status = iterate_postfix_command(iter_cmd, argc, argv, selection); 1580 break; 1581 case ITER_CMD_LIST: 1582 list_instances(iter_flags, selection); 1583 exit_status = 0; 1584 break; 1585 case ITER_CMD_GENERIC: 1586 exit_status = iterate_command(iter_cmd, iter_flags, argv, selection); 1587 break; 1588 default: 1589 msg_panic("iterate: unknown mode: %d", iter_cmd); 1590 } 1591 exit(exit_status); 1592 } 1593 1594 static NORETURN usage(const char *progname) 1595 { 1596 msg_fatal("Usage:" 1597 "%s -l [-v] [-a] [-g group] [-i instance] | " 1598 "%s -p [-v] [-a] [-g group] [-i instance] command... | " 1599 "%s -x [-v] [-a] [-i name] [-g group] command... | " 1600 "%s -e action [-v] [-a] [-i name] [-g group] [-I name] " 1601 "[-G group] [param=value ...]", 1602 progname, progname, progname, progname); 1603 } 1604 1605 MAIL_VERSION_STAMP_DECLARE; 1606 1607 /* main - iterate commands over multiple instance or manage instances */ 1608 1609 int main(int argc, char **argv) 1610 { 1611 int fd; 1612 struct stat st; 1613 char *slash; 1614 char *config_dir; 1615 int ch; 1616 static const CONFIG_STR_TABLE str_table[] = { 1617 VAR_MULTI_START_CMDS, DEF_MULTI_START_CMDS, &var_multi_start_cmds, 0, 0, 1618 VAR_MULTI_STOP_CMDS, DEF_MULTI_STOP_CMDS, &var_multi_stop_cmds, 0, 0, 1619 VAR_MULTI_CNTRL_CMDS, DEF_MULTI_CNTRL_CMDS, &var_multi_cntrl_cmds, 0, 0, 1620 0, 1621 }; 1622 int instance_select_count = 0; 1623 int command_mode_count = 0; 1624 INST_SELECTION selection; 1625 NAME_ASSIGNMENT assignment; 1626 int iter_flags = ITER_FLAG_DEFAULT; 1627 int cmd_mode = 0; 1628 int code; 1629 1630 selection.type = INST_SEL_NONE; 1631 assignment.name = assignment.gname = 0; 1632 1633 /* 1634 * Fingerprint executables and core dumps. 1635 */ 1636 MAIL_VERSION_STAMP_ALLOCATE; 1637 1638 /* 1639 * Be consistent with file permissions. 1640 */ 1641 umask(022); 1642 1643 /* 1644 * To minimize confusion, make sure that the standard file descriptors 1645 * are open before opening anything else. XXX Work around for 44BSD where 1646 * fstat can return EBADF on an open file descriptor. 1647 */ 1648 for (fd = 0; fd < 3; fd++) 1649 if (fstat(fd, &st) == -1 1650 && (close(fd), open("/dev/null", O_RDWR, 0)) != fd) 1651 msg_fatal("open /dev/null: %m"); 1652 1653 /* 1654 * Set up diagnostics. XXX What if stdin is the system console during 1655 * boot time? It seems a bad idea to log startup errors to the console. 1656 * This is UNIX, a system that can run without hand holding. 1657 */ 1658 if ((slash = strrchr(argv[0], '/')) != 0 && slash[1]) 1659 argv[0] = slash + 1; 1660 if (isatty(STDERR_FILENO)) 1661 msg_vstream_init(argv[0], VSTREAM_ERR); 1662 msg_syslog_init(argv[0], LOG_PID, LOG_FACILITY); 1663 1664 /* 1665 * Check the Postfix library version as soon as we enable logging. 1666 */ 1667 MAIL_VERSION_CHECK; 1668 1669 if ((config_dir = getenv(CONF_ENV_PATH)) != 0 1670 && strcmp(config_dir, DEF_CONFIG_DIR) != 0) 1671 msg_fatal("Non-default configuration directory: %s=%s", 1672 CONF_ENV_PATH, config_dir); 1673 1674 /* 1675 * Parse switches. 1676 */ 1677 while ((ch = GETOPT(argc, argv, "ae:g:i:G:I:lpRvx")) > 0) { 1678 switch (ch) { 1679 default: 1680 usage(argv[0]); 1681 /* NOTREACHED */ 1682 case 'a': 1683 if (selection.type != INST_SEL_ALL) 1684 instance_select_count++; 1685 selection.type = INST_SEL_ALL; 1686 break; 1687 case 'e': 1688 if ((code = EDIT_CMD_CODE(optarg)) < 0) 1689 msg_fatal("Invalid '-e' edit action '%s'. Specify '%s', " 1690 "'%s', '%s', '%s', '%s', '%s', '%s', '%s' or '%s'", 1691 optarg, 1692 EDIT_CMD_STR(EDIT_CMD_CREATE), 1693 EDIT_CMD_STR(EDIT_CMD_DESTROY), 1694 EDIT_CMD_STR(EDIT_CMD_IMPORT), 1695 EDIT_CMD_STR(EDIT_CMD_DEPORT), 1696 EDIT_CMD_STR(EDIT_CMD_ENABLE), 1697 EDIT_CMD_STR(EDIT_CMD_DISABLE), 1698 EDIT_CMD_STR(EDIT_CMD_ASSIGN), 1699 EDIT_CMD_STR(EDIT_CMD_INIT), 1700 optarg); 1701 if (cmd_mode != code) 1702 command_mode_count++; 1703 cmd_mode = code; 1704 break; 1705 case 'g': 1706 instance_select_count++; 1707 selection.type = INST_SEL_GROUP; 1708 selection.name = optarg; 1709 break; 1710 case 'i': 1711 instance_select_count++; 1712 selection.type = INST_SEL_NAME; 1713 selection.name = optarg; 1714 break; 1715 case 'G': 1716 if (assignment.gname != 0) 1717 msg_fatal("Specify at most one '-G' option"); 1718 assignment.gname = strcmp(optarg, "-") == 0 ? "" : optarg; 1719 break; 1720 case 'I': 1721 if (assignment.name != 0) 1722 msg_fatal("Specify at most one '-I' option"); 1723 assignment.name = strcmp(optarg, "-") == 0 ? "" : optarg; 1724 break; 1725 case 'l': 1726 if (cmd_mode != ITER_CMD_LIST) 1727 command_mode_count++; 1728 cmd_mode = ITER_CMD_LIST; 1729 break; 1730 case 'p': 1731 if (cmd_mode != ITER_CMD_POSTFIX) 1732 command_mode_count++; 1733 cmd_mode = ITER_CMD_POSTFIX; 1734 break; 1735 case 'R': 1736 iter_flags ^= ITER_FLAG_REVERSE; 1737 break; 1738 case 'v': 1739 msg_verbose++; 1740 check_setenv(CONF_ENV_VERB, ""); 1741 break; 1742 case 'x': 1743 if (cmd_mode != ITER_CMD_GENERIC) 1744 command_mode_count++; 1745 cmd_mode = ITER_CMD_GENERIC; 1746 break; 1747 } 1748 } 1749 1750 /* 1751 * Report missing arguments, or wrong arguments in the wrong context. 1752 */ 1753 if (instance_select_count > 1) 1754 msg_fatal("Specity no more than one of '-a', '-g', '-i'"); 1755 1756 if (command_mode_count != 1) 1757 msg_fatal("Specify exactly one of '-e', '-l', '-p', '-x'"); 1758 1759 if (cmd_mode == ITER_CMD_LIST && argc > optind) 1760 msg_fatal("Command not allowed with '-l'"); 1761 1762 if (cmd_mode == ITER_CMD_POSTFIX || cmd_mode == ITER_CMD_GENERIC) 1763 if (argc == optind) 1764 msg_fatal("Command required with '-p' or '-x' option"); 1765 1766 if (cmd_mode == ITER_CMD_POSTFIX || (cmd_mode & EDIT_CMD_MASK_ALL)) 1767 if (iter_flags != ITER_FLAG_DEFAULT) 1768 msg_fatal("The '-p' and '-e' options preclude the use of '-R'"); 1769 1770 if ((cmd_mode & EDIT_CMD_MASK_ASSIGN) == 0 1771 && (assignment.name || assignment.gname)) { 1772 if ((cmd_mode & EDIT_CMD_MASK_ALL) == 0) 1773 msg_fatal("Cannot assign instance name or group without '-e %s'", 1774 EDIT_CMD_STR(EDIT_CMD_ASSIGN)); 1775 else 1776 msg_fatal("Cannot assign instance name or group with '-e %s'", 1777 EDIT_CMD_STR(cmd_mode)); 1778 } 1779 if (cmd_mode & EDIT_CMD_MASK_ALL) { 1780 if (cmd_mode == EDIT_CMD_ASSIGN 1781 && (assignment.name == 0 && assignment.gname == 0)) 1782 msg_fatal("Specify new instance name or group with '-e %s'", 1783 EDIT_CMD_STR(cmd_mode)); 1784 1785 if ((cmd_mode & ~EDIT_CMD_MASK_ADD) != 0 && argc > optind) 1786 msg_fatal("Parameter overrides not valid with '-e %s'", 1787 EDIT_CMD_STR(cmd_mode)); 1788 } 1789 1790 /* 1791 * Proces main.cf parameters. 1792 */ 1793 mail_conf_read(); 1794 get_mail_conf_str_table(str_table); 1795 1796 /* 1797 * Sanity checks. 1798 */ 1799 check_shared_dir_status(); 1800 1801 /* 1802 * Iterate over selected instances, or manipulate one instance. 1803 */ 1804 if (cmd_mode & ITER_CMD_MASK_ALL) 1805 iterate(cmd_mode, iter_flags, argc - optind, argv + optind, &selection); 1806 else 1807 manage(cmd_mode, argc - optind, argv + optind, &selection, &assignment); 1808 } 1809