7 /* Postfix queue control
9 /* \fBpostqueue\fR [\fB-v\fR] [\fB-c \fIconfig_dir\fR] \fB-f\fR
11 /* \fBpostqueue\fR [\fB-v\fR] [\fB-c \fIconfig_dir\fR] \fB-i \fIqueue_id\fR
13 /* \fBpostqueue\fR [\fB-v\fR] [\fB-c \fIconfig_dir\fR] \fB-p\fR
15 /* \fBpostqueue\fR [\fB-v\fR] [\fB-c \fIconfig_dir\fR] \fB-s \fIsite\fR
17 /* The \fBpostqueue\fR(1) command implements the Postfix user interface
18 /* for queue management. It implements operations that are
19 /* traditionally available via the \fBsendmail\fR(1) command.
20 /* See the \fBpostsuper\fR(1) command for queue operations
21 /* that require super-user privileges such as deleting a message
22 /* from the queue or changing the status of a message.
24 /* The following options are recognized:
25 /* .IP "\fB-c \fIconfig_dir\fR"
26 /* The \fBmain.cf\fR configuration file is in the named directory
27 /* instead of the default configuration directory. See also the
28 /* MAIL_CONFIG environment setting below.
30 /* Flush the queue: attempt to deliver all queued mail.
32 /* This option implements the traditional "\fBsendmail -q\fR" command,
33 /* by contacting the Postfix \fBqmgr\fR(8) daemon.
35 /* Warning: flushing undeliverable mail frequently will result in
36 /* poor delivery performance of all other mail.
37 /* .IP "\fB-i \fIqueue_id\fR"
38 /* Schedule immediate delivery of deferred mail with the
39 /* specified queue ID.
41 /* This option implements the traditional \fBsendmail -qI\fR
42 /* command, by contacting the \fBflush\fR(8) server.
44 /* This feature is available with Postfix version 2.4 and later.
46 /* Produce a traditional sendmail-style queue listing.
47 /* This option implements the traditional \fBmailq\fR command,
48 /* by contacting the Postfix \fBshowq\fR(8) daemon.
50 /* Each queue entry shows the queue file ID, message
51 /* size, arrival time, sender, and the recipients that still need to
52 /* be delivered. If mail could not be delivered upon the last attempt,
53 /* the reason for failure is shown. This mode of operation is implemented
54 /* by executing the \fBpostqueue\fR(1) command. The queue ID string
55 /* is followed by an optional status character:
58 /* The message is in the \fBactive\fR queue, i.e. the message is
59 /* selected for delivery.
61 /* The message is in the \fBhold\fR queue, i.e. no further delivery
62 /* attempt will be made until the mail is taken off hold.
64 /* .IP "\fB-s \fIsite\fR"
65 /* Schedule immediate delivery of all mail that is queued for the named
66 /* \fIsite\fR. A numerical site must be specified as a valid RFC 2821
67 /* address literal enclosed in [], just like in email addresses.
68 /* The site must be eligible for the "fast flush" service.
69 /* See \fBflush\fR(8) for more information about the "fast flush"
72 /* This option implements the traditional "\fBsendmail -qR\fIsite\fR"
73 /* command, by contacting the Postfix \fBflush\fR(8) daemon.
75 /* Enable verbose logging for debugging purposes. Multiple \fB-v\fR
76 /* options make the software increasingly verbose. As of Postfix 2.3,
77 /* this option is available for the super-user only.
81 /* This program is designed to run with set-group ID privileges, so
82 /* that it can connect to Postfix daemon processes.
84 /* Problems are logged to \fBsyslogd\fR(8) and to the standard error
90 /* Directory with the \fBmain.cf\fR file. In order to avoid exploitation
91 /* of set-group ID privileges, a non-standard directory is allowed only
95 /* The name is listed in the standard \fBmain.cf\fR file with the
96 /* \fBalternate_config_directories\fR configuration parameter.
98 /* The command is invoked by the super-user.
100 /* CONFIGURATION PARAMETERS
103 /* The following \fBmain.cf\fR parameters are especially relevant to
105 /* The text below provides only a parameter summary. See
106 /* \fBpostconf\fR(5) for more details including examples.
107 /* .IP "\fBalternate_config_directories (empty)\fR"
108 /* A list of non-default Postfix configuration directories that may
109 /* be specified with "-c config_directory" on the command line, or
110 /* via the MAIL_CONFIG environment parameter.
111 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
112 /* The default location of the Postfix main.cf and master.cf
113 /* configuration files.
114 /* .IP "\fBcommand_directory (see 'postconf -d' output)\fR"
115 /* The location of all postfix administrative commands.
116 /* .IP "\fBfast_flush_domains ($relay_domains)\fR"
117 /* Optional list of destinations that are eligible for per-destination
118 /* logfiles with mail that is queued to those destinations.
119 /* .IP "\fBimport_environment (see 'postconf -d' output)\fR"
120 /* The list of environment parameters that a Postfix process will
121 /* import from a non-Postfix parent process.
122 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
123 /* The location of the Postfix top-level queue directory.
124 /* .IP "\fBsyslog_facility (mail)\fR"
125 /* The syslog facility of Postfix logging.
126 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
127 /* The mail system name that is prepended to the process name in syslog
128 /* records, so that "smtpd" becomes, for example, "postfix/smtpd".
129 /* .IP "\fBtrigger_timeout (10s)\fR"
130 /* The time limit for sending a trigger to a Postfix daemon (for
131 /* example, the \fBpickup\fR(8) or \fBqmgr\fR(8) daemon).
133 /* Available in Postfix version 2.2 and later:
134 /* .IP "\fBauthorized_flush_users (static:anyone)\fR"
135 /* List of users who are authorized to flush the queue.
136 /* .IP "\fBauthorized_mailq_users (static:anyone)\fR"
137 /* List of users who are authorized to view the queue.
139 /* /var/spool/postfix, mail queue
141 /* qmgr(8), queue manager
142 /* showq(8), list mail queue
143 /* flush(8), fast flush service
144 /* sendmail(1), Sendmail-compatible user interface
145 /* postsuper(1), privileged queue operations
149 /* Use "\fBpostconf readme_directory\fR" or
150 /* "\fBpostconf html_directory\fR" to locate this information.
153 /* ETRN_README, Postfix ETRN howto
157 /* The Secure Mailer license must be distributed with this software.
161 /* The postqueue command was introduced with Postfix version 1.1.
164 /* IBM T.J. Watson Research
166 /* Yorktown Heights, NY 10598, USA
169 /* System library. */
171 #include <sys_defs.h>
172 #include <sys/stat.h>
177 #include <sysexits.h>
180 /* Utility library. */
183 #include <mymalloc.h>
184 #include <clean_env.h>
186 #include <msg_vstream.h>
187 #include <msg_syslog.h>
191 #include <valid_hostname.h>
193 /* Global library. */
195 #include <mail_proto.h>
196 #include <mail_params.h>
197 #include <mail_version.h>
198 #include <mail_conf.h>
199 #include <mail_task.h>
200 #include <mail_run.h>
201 #include <mail_flush.h>
202 #include <mail_queue.h>
203 #include <flush_clnt.h>
204 #include <smtp_stream.h>
205 #include <user_acl.h>
206 #include <valid_mailhost_addr.h>
207 #include <mail_dict.h>
209 /* Application-specific. */
212 * WARNING WARNING WARNING
214 * This software is designed to run set-gid. In order to avoid exploitation of
215 * privilege, this software should not run any external commands, nor should
216 * it take any information from the user, unless that information can be
217 * properly sanitized. To get an idea of how much information a process can
218 * inherit from a potentially hostile user, examine all the members of the
219 * process structure (typically, in /usr/include/sys/proc.h): the current
220 * directory, open files, timers, signals, environment, command line, umask,
225 * Modes of operation.
227 * XXX To support flush by recipient domain, or for destinations that have no
228 * mapping to logfile, the server has to defend against resource exhaustion
229 * attacks. A malicious user could fork off a postqueue client that starts
230 * an expensive requests and then kills the client immediately; this way she
231 * could create a high Postfix load on the system without ever exceeding her
232 * own per-user process limit. To prevent this, either the server needs to
233 * establish frequent proof of client liveliness with challenge/response, or
234 * the client needs to restrict expensive requests to privileged users only.
236 * We don't have this problem with queue listings. The showq server detects an
237 * EPIPE error after reporting a few queue entries.
239 #define PQ_MODE_DEFAULT 0 /* noop */
240 #define PQ_MODE_MAILQ_LIST 1 /* list mail queue */
241 #define PQ_MODE_FLUSH_QUEUE 2 /* flush queue */
242 #define PQ_MODE_FLUSH_SITE 3 /* flush site */
243 #define PQ_MODE_FLUSH_FILE 4 /* flush message */
246 * Silly little macros (SLMs).
248 #define STR vstring_str
251 * Queue manipulation access lists.
256 static const CONFIG_STR_TABLE str_table
[] = {
257 VAR_FLUSH_ACL
, DEF_FLUSH_ACL
, &var_flush_acl
, 0, 0,
258 VAR_SHOWQ_ACL
, DEF_SHOWQ_ACL
, &var_showq_acl
, 0, 0,
262 /* show_queue - show queue status */
264 static void show_queue(void)
267 char buf
[VSTREAM_BUFSIZE
];
270 uid_t uid
= getuid();
272 if (uid
!= 0 && uid
!= var_owner_uid
273 && (errstr
= check_user_acl_byuid(var_showq_acl
, uid
)) != 0)
274 msg_fatal_status(EX_NOPERM
,
275 "User %s(%ld) is not allowed to view the mail queue",
279 * Connect to the show queue service. Terminate silently when piping into
280 * a program that terminates early.
282 if ((showq
= mail_connect(MAIL_CLASS_PUBLIC
, var_showq_service
, BLOCKING
)) != 0) {
283 while ((n
= vstream_fread(showq
, buf
, sizeof(buf
))) > 0) {
284 if (vstream_fwrite(VSTREAM_OUT
, buf
, n
) != n
285 || vstream_fflush(VSTREAM_OUT
) != 0) {
288 msg_fatal("write error: %m");
291 if (vstream_fclose(showq
) && errno
!= EPIPE
)
292 msg_warn("close: %m");
296 * Don't assume that the mail system is down when the user has
297 * insufficient permission to access the showq socket.
299 else if (errno
== EACCES
) {
300 msg_fatal_status(EX_SOFTWARE
,
301 "Connect to the %s %s service: %m",
302 var_mail_name
, var_showq_service
);
306 * When the mail system is down, the superuser can still access the queue
307 * directly. Just run the showq program in stand-alone mode.
309 else if (geteuid() == 0) {
313 msg_warn("Mail system is down -- accessing queue directly");
314 argv
= argv_alloc(6);
315 argv_add(argv
, var_showq_service
, "-u", "-S", (char *) 0);
316 for (n
= 0; n
< msg_verbose
; n
++)
317 argv_add(argv
, "-v", (char *) 0);
318 argv_terminate(argv
);
319 stat
= mail_run_foreground(var_daemon_dir
, argv
->argv
);
324 * When the mail system is down, unprivileged users are stuck, because by
325 * design the mail system contains no set_uid programs. The only way for
326 * an unprivileged user to cross protection boundaries is to talk to the
330 msg_fatal_status(EX_UNAVAILABLE
,
331 "Queue report unavailable - mail system is down");
335 /* flush_queue - force delivery */
337 static void flush_queue(void)
340 uid_t uid
= getuid();
342 if (uid
!= 0 && uid
!= var_owner_uid
343 && (errstr
= check_user_acl_byuid(var_flush_acl
, uid
)) != 0)
344 msg_fatal_status(EX_NOPERM
,
345 "User %s(%ld) is not allowed to flush the mail queue",
349 * Trigger the flush queue service.
351 if (mail_flush_deferred() < 0)
352 msg_fatal_status(EX_UNAVAILABLE
,
353 "Cannot flush mail queue - mail system is down");
354 if (mail_flush_maildrop() < 0)
355 msg_fatal_status(EX_UNAVAILABLE
,
356 "Cannot flush mail queue - mail system is down");
359 /* flush_site - flush mail for site */
361 static void flush_site(const char *site
)
365 uid_t uid
= getuid();
367 if (uid
!= 0 && uid
!= var_owner_uid
368 && (errstr
= check_user_acl_byuid(var_flush_acl
, uid
)) != 0)
369 msg_fatal_status(EX_NOPERM
,
370 "User %s(%ld) is not allowed to flush the mail queue",
375 switch (status
= flush_send_site(site
)) {
379 msg_fatal_status(EX_USAGE
, "Invalid request: \"%s\"", site
);
380 case FLUSH_STAT_FAIL
:
381 msg_fatal_status(EX_UNAVAILABLE
,
382 "Cannot flush mail queue - mail system is down");
383 case FLUSH_STAT_DENY
:
384 msg_fatal_status(EX_UNAVAILABLE
,
385 "Flush service is not configured for destination \"%s\"",
388 msg_fatal_status(EX_SOFTWARE
,
389 "Unknown flush server reply status %d", status
);
393 /* flush_file - flush mail with specific queue ID */
395 static void flush_file(const char *queue_id
)
399 uid_t uid
= getuid();
401 if (uid
!= 0 && uid
!= var_owner_uid
402 && (errstr
= check_user_acl_byuid(var_flush_acl
, uid
)) != 0)
403 msg_fatal_status(EX_NOPERM
,
404 "User %s(%ld) is not allowed to flush the mail queue",
407 switch (status
= flush_send_file(queue_id
)) {
411 msg_fatal_status(EX_USAGE
, "Invalid request: \"%s\"", queue_id
);
412 case FLUSH_STAT_FAIL
:
413 msg_fatal_status(EX_UNAVAILABLE
,
414 "Cannot flush mail queue - mail system is down");
416 msg_fatal_status(EX_SOFTWARE
,
417 "Unexpected flush server reply status %d", status
);
421 /* unavailable - sanitize exit status from library run-time errors */
423 static void unavailable(void)
425 exit(EX_UNAVAILABLE
);
428 /* usage - scream and die */
430 static NORETURN
usage(void)
432 msg_fatal_status(EX_USAGE
, "usage: postqueue -f | postqueue -i queueid | postqueue -p | postqueue -s site");
435 MAIL_VERSION_STAMP_DECLARE
;
437 /* main - the main program */
439 int main(int argc
, char **argv
)
445 int mode
= PQ_MODE_DEFAULT
;
446 char *site_to_flush
= 0;
447 char *id_to_flush
= 0;
452 * Fingerprint executables and core dumps.
454 MAIL_VERSION_STAMP_ALLOCATE
;
457 * Be consistent with file permissions.
462 * To minimize confusion, make sure that the standard file descriptors
463 * are open before opening anything else. XXX Work around for 44BSD where
464 * fstat can return EBADF on an open file descriptor.
466 for (fd
= 0; fd
< 3; fd
++)
467 if (fstat(fd
, &st
) == -1
468 && (close(fd
), open("/dev/null", O_RDWR
, 0)) != fd
)
469 msg_fatal_status(EX_UNAVAILABLE
, "open /dev/null: %m");
472 * Initialize. Set up logging, read the global configuration file and
473 * extract configuration information. Set up signal handlers so that we
474 * can clean up incomplete output.
476 if ((slash
= strrchr(argv
[0], '/')) != 0 && slash
[1])
478 msg_vstream_init(argv
[0], VSTREAM_ERR
);
479 msg_cleanup(unavailable
);
480 msg_syslog_init(mail_task("postqueue"), LOG_PID
, LOG_FACILITY
);
481 set_mail_conf_str(VAR_PROCNAME
, var_procname
= mystrdup(argv
[0]));
484 * Parse JCL. This program is set-gid and must sanitize all command-line
485 * parameters. The configuration directory argument is validated by the
486 * mail configuration read routine. Don't do complex things until we have
487 * completed initializations.
489 while ((c
= GETOPT(argc
, argv
, "c:fi:ps:v")) > 0) {
491 case 'c': /* non-default configuration */
492 if (setenv(CONF_ENV_PATH
, optarg
, 1) < 0)
493 msg_fatal_status(EX_UNAVAILABLE
, "out of memory");
495 case 'f': /* flush queue */
496 if (mode
!= PQ_MODE_DEFAULT
)
498 mode
= PQ_MODE_FLUSH_QUEUE
;
500 case 'i': /* flush queue file */
501 if (mode
!= PQ_MODE_DEFAULT
)
503 mode
= PQ_MODE_FLUSH_FILE
;
504 id_to_flush
= optarg
;
506 case 'p': /* traditional mailq */
507 if (mode
!= PQ_MODE_DEFAULT
)
509 mode
= PQ_MODE_MAILQ_LIST
;
511 case 's': /* flush site */
512 if (mode
!= PQ_MODE_DEFAULT
)
514 mode
= PQ_MODE_FLUSH_SITE
;
515 site_to_flush
= optarg
;
529 * Further initialization...
532 if (strcmp(var_syslog_name
, DEF_SYSLOG_NAME
) != 0)
533 msg_syslog_init(mail_task("postqueue"), LOG_PID
, LOG_FACILITY
);
534 mail_dict_init(); /* proxy, sql, ldap */
535 get_mail_conf_str_table(str_table
);
538 * This program is designed to be set-gid, which makes it a potential
539 * target for attack. If not running as root, strip the environment so we
540 * don't have to trust the C library. If running as root, don't strip the
541 * environment so that showq can receive non-default configuration
542 * directory info when the mail system is down.
544 if (geteuid() != 0) {
545 import_env
= argv_split(var_import_environ
, ", \t\r\n");
546 clean_env(import_env
->argv
);
547 argv_free(import_env
);
549 if (chdir(var_queue_dir
))
550 msg_fatal_status(EX_UNAVAILABLE
, "chdir %s: %m", var_queue_dir
);
552 signal(SIGPIPE
, SIG_IGN
);
554 /* End of initializations. */
557 * Further input validation.
559 if (site_to_flush
!= 0) {
561 if (*site_to_flush
== '[') {
562 bad_site
= !valid_mailhost_literal(site_to_flush
, DONT_GRIPE
);
564 bad_site
= !valid_hostname(site_to_flush
, DONT_GRIPE
);
567 msg_fatal_status(EX_USAGE
,
568 "Cannot flush mail queue - invalid destination: \"%.100s%s\"",
569 site_to_flush
, strlen(site_to_flush
) > 100 ? "..." : "");
571 if (id_to_flush
!= 0) {
572 if (!mail_queue_id_ok(id_to_flush
))
573 msg_fatal_status(EX_USAGE
,
574 "Cannot flush queue ID - invalid name: \"%.100s%s\"",
575 id_to_flush
, strlen(id_to_flush
) > 100 ? "..." : "");
583 msg_panic("unknown operation mode: %d", mode
);
585 case PQ_MODE_MAILQ_LIST
:
589 case PQ_MODE_FLUSH_SITE
:
590 flush_site(site_to_flush
);
593 case PQ_MODE_FLUSH_FILE
:
594 flush_file(id_to_flush
);
597 case PQ_MODE_FLUSH_QUEUE
:
601 case PQ_MODE_DEFAULT
: