Postfix3.3.1
sendmail.c
[詳解]
1 /*++
2 /* NAME
3 /* sendmail 1
4 /* SUMMARY
5 /* Postfix to Sendmail compatibility interface
6 /* SYNOPSIS
7 /* \fBsendmail\fR [\fIoption ...\fR] [\fIrecipient ...\fR]
8 /*
9 /* \fBmailq\fR
10 /* \fBsendmail -bp\fR
11 /*
12 /* \fBnewaliases\fR
13 /* \fBsendmail -I\fR
14 /* DESCRIPTION
15 /* The Postfix \fBsendmail\fR(1) command implements the Postfix
16 /* to Sendmail compatibility interface.
17 /* For the sake of compatibility with existing applications, some
18 /* Sendmail command-line options are recognized but silently ignored.
19 /*
20 /* By default, Postfix \fBsendmail\fR(1) reads a message from
21 /* standard input
22 /* until EOF or until it reads a line with only a \fB.\fR character,
23 /* and arranges for delivery. Postfix \fBsendmail\fR(1) relies on the
24 /* \fBpostdrop\fR(1) command to create a queue file in the \fBmaildrop\fR
25 /* directory.
26 /*
27 /* Specific command aliases are provided for other common modes of
28 /* operation:
29 /* .IP \fBmailq\fR
30 /* List the mail queue. Each entry shows the queue file ID, message
31 /* size, arrival time, sender, and the recipients that still need to
32 /* be delivered. If mail could not be delivered upon the last attempt,
33 /* the reason for failure is shown. The queue ID string is
34 /* followed by an optional status character:
35 /* .RS
36 /* .IP \fB*\fR
37 /* The message is in the \fBactive\fR queue, i.e. the message is
38 /* selected for delivery.
39 /* .IP \fB!\fR
40 /* The message is in the \fBhold\fR queue, i.e. no further delivery
41 /* attempt will be made until the mail is taken off hold.
42 /* .RE
43 /* .IP
44 /* This mode of operation is implemented by executing the
45 /* \fBpostqueue\fR(1) command.
46 /* .IP \fBnewaliases\fR
47 /* Initialize the alias database. If no input file is specified (with
48 /* the \fB-oA\fR option, see below), the program processes the file(s)
49 /* specified with the \fBalias_database\fR configuration parameter.
50 /* If no alias database type is specified, the program uses the type
51 /* specified with the \fBdefault_database_type\fR configuration parameter.
52 /* This mode of operation is implemented by running the \fBpostalias\fR(1)
53 /* command.
54 /* .sp
55 /* Note: it may take a minute or so before an alias database update
56 /* becomes visible. Use the "\fBpostfix reload\fR" command to eliminate
57 /* this delay.
58 /* .PP
59 /* These and other features can be selected by specifying the
60 /* appropriate combination of command-line options. Some features are
61 /* controlled by parameters in the \fBmain.cf\fR configuration file.
62 /*
63 /* The following options are recognized:
64 /* .IP "\fB-Am\fR (ignored)"
65 /* .IP "\fB-Ac\fR (ignored)"
66 /* Postfix sendmail uses the same configuration file regardless of
67 /* whether or not a message is an initial submission.
68 /* .IP "\fB-B \fIbody_type\fR"
69 /* The message body MIME type: \fB7BIT\fR or \fB8BITMIME\fR.
70 /* .IP \fB-bd\fR
71 /* Go into daemon mode. This mode of operation is implemented by
72 /* executing the "\fBpostfix start\fR" command.
73 /* .IP "\fB-bh\fR (ignored)"
74 /* .IP "\fB-bH\fR (ignored)"
75 /* Postfix has no persistent host status database.
76 /* .IP \fB-bi\fR
77 /* Initialize alias database. See the \fBnewaliases\fR
78 /* command above.
79 /* .IP \fB-bl\fR
80 /* Go into daemon mode. To accept only local connections as
81 /* with Sendmail\'s \fB-bl\fR option, specify "\fBinet_interfaces
82 /* = loopback\fR" in the Postfix \fBmain.cf\fR configuration
83 /* file.
84 /* .IP \fB-bm\fR
85 /* Read mail from standard input and arrange for delivery.
86 /* This is the default mode of operation.
87 /* .IP \fB-bp\fR
88 /* List the mail queue. See the \fBmailq\fR command above.
89 /* .IP \fB-bs\fR
90 /* Stand-alone SMTP server mode. Read SMTP commands from
91 /* standard input, and write responses to standard output.
92 /* In stand-alone SMTP server mode, mail relaying and other
93 /* access controls are disabled by default. To enable them,
94 /* run the process as the \fBmail_owner\fR user.
95 /* .sp
96 /* This mode of operation is implemented by running the
97 /* \fBsmtpd\fR(8) daemon.
98 /* .IP \fB-bv\fR
99 /* Do not collect or deliver a message. Instead, send an email
100 /* report after verifying each recipient address. This is useful
101 /* for testing address rewriting and routing configurations.
102 /* .sp
103 /* This feature is available in Postfix version 2.1 and later.
104 /* .IP "\fB-C \fIconfig_file\fR"
105 /* .IP "\fB-C \fIconfig_dir\fR"
106 /* The path name of the Postfix \fBmain.cf\fR file, or of its
107 /* parent directory. This information is ignored with Postfix
108 /* versions before 2.3.
109 /*
110 /* With Postfix version 3.2 and later, a non-default directory
111 /* must be authorized in the default \fBmain.cf\fR file, through
112 /* the alternate_config_directories or multi_instance_directories
113 /* parameters.
114 /*
115 /* With all Postfix versions, you can specify a directory pathname
116 /* with the MAIL_CONFIG environment variable to override the
117 /* location of configuration files.
118 /* .IP "\fB-F \fIfull_name\fR"
119 /* Set the sender full name. This overrides the NAME environment
120 /* variable, and is used only with messages that
121 /* have no \fBFrom:\fR message header.
122 /* .IP "\fB-f \fIsender\fR"
123 /* Set the envelope sender address. This is the address where
124 /* delivery problems are sent to. With Postfix versions before 2.1, the
125 /* \fBErrors-To:\fR message header overrides the error return address.
126 /* .IP \fB-G\fR
127 /* Gateway (relay) submission, as opposed to initial user
128 /* submission. Either do not rewrite addresses at all, or
129 /* update incomplete addresses with the domain information
130 /* specified with \fBremote_header_rewrite_domain\fR.
131 /*
132 /* This option is ignored before Postfix version 2.3.
133 /* .IP "\fB-h \fIhop_count\fR (ignored)"
134 /* Hop count limit. Use the \fBhopcount_limit\fR configuration
135 /* parameter instead.
136 /* .IP \fB-I\fR
137 /* Initialize alias database. See the \fBnewaliases\fR
138 /* command above.
139 /* .IP "\fB-i\fR"
140 /* When reading a message from standard input, don\'t treat a line
141 /* with only a \fB.\fR character as the end of input.
142 /* .IP "\fB-L \fIlabel\fR (ignored)"
143 /* The logging label. Use the \fBsyslog_name\fR configuration
144 /* parameter instead.
145 /* .IP "\fB-m\fR (ignored)"
146 /* Backwards compatibility.
147 /* .IP "\fB-N \fIdsn\fR (default: 'delay, failure')"
148 /* Delivery status notification control. Specify either a
149 /* comma-separated list with one or more of \fBfailure\fR (send
150 /* notification when delivery fails), \fBdelay\fR (send
151 /* notification when delivery is delayed), or \fBsuccess\fR
152 /* (send notification when the message is delivered); or specify
153 /* \fBnever\fR (don't send any notifications at all).
154 /*
155 /* This feature is available in Postfix 2.3 and later.
156 /* .IP "\fB-n\fR (ignored)"
157 /* Backwards compatibility.
158 /* .IP "\fB-oA\fIalias_database\fR"
159 /* Non-default alias database. Specify \fIpathname\fR or
160 /* \fItype\fR:\fIpathname\fR. See \fBpostalias\fR(1) for
161 /* details.
162 /* .IP "\fB-O \fIoption=value\fR (ignored)"
163 /* Set the named \fIoption\fR to \fIvalue\fR. Use the equivalent
164 /* configuration parameter in \fBmain.cf\fR instead.
165 /* .IP "\fB-o7\fR (ignored)"
166 /* .IP "\fB-o8\fR (ignored)"
167 /* To send 8-bit or binary content, use an appropriate MIME encapsulation
168 /* and specify the appropriate \fB-B\fR command-line option.
169 /* .IP "\fB-oi\fR"
170 /* When reading a message from standard input, don\'t treat a line
171 /* with only a \fB.\fR character as the end of input.
172 /* .IP "\fB-om\fR (ignored)"
173 /* The sender is never eliminated from alias etc. expansions.
174 /* .IP "\fB-o \fIx value\fR (ignored)"
175 /* Set option \fIx\fR to \fIvalue\fR. Use the equivalent
176 /* configuration parameter in \fBmain.cf\fR instead.
177 /* .IP "\fB-r \fIsender\fR"
178 /* Set the envelope sender address. This is the address where
179 /* delivery problems are sent to. With Postfix versions before 2.1, the
180 /* \fBErrors-To:\fR message header overrides the error return address.
181 /* .IP "\fB-R \fIreturn\fR"
182 /* Delivery status notification control. Specify "hdrs" to
183 /* return only the header when a message bounces, "full" to
184 /* return a full copy (the default behavior).
185 /*
186 /* The \fB-R\fR option specifies an upper bound; Postfix will
187 /* return only the header, when a full copy would exceed the
188 /* bounce_size_limit setting.
189 /*
190 /* This option is ignored before Postfix version 2.10.
191 /* .IP \fB-q\fR
192 /* Attempt to deliver all queued mail. This is implemented by
193 /* executing the \fBpostqueue\fR(1) command.
194 /*
195 /* Warning: flushing undeliverable mail frequently will result in
196 /* poor delivery performance of all other mail.
197 /* .IP "\fB-q\fIinterval\fR (ignored)"
198 /* The interval between queue runs. Use the \fBqueue_run_delay\fR
199 /* configuration parameter instead.
200 /* .IP \fB-qI\fIqueueid\fR
201 /* Schedule immediate delivery of mail with the specified queue
202 /* ID. This option is implemented by executing the
203 /* \fBpostqueue\fR(1) command, and is available with Postfix
204 /* version 2.4 and later.
205 /* .IP \fB-qR\fIsite\fR
206 /* Schedule immediate delivery of all mail that is queued for the named
207 /* \fIsite\fR. This option accepts only \fIsite\fR names that are
208 /* eligible for the "fast flush" service, and is implemented by
209 /* executing the \fBpostqueue\fR(1) command.
210 /* See \fBflush\fR(8) for more information about the "fast flush"
211 /* service.
212 /* .IP \fB-qS\fIsite\fR
213 /* This command is not implemented. Use the slower "\fBsendmail -q\fR"
214 /* command instead.
215 /* .IP \fB-t\fR
216 /* Extract recipients from message headers. These are added to any
217 /* recipients specified on the command line.
218 /*
219 /* With Postfix versions prior to 2.1, this option requires that
220 /* no recipient addresses are specified on the command line.
221 /* .IP "\fB-U\fR (ignored)"
222 /* Initial user submission.
223 /* .IP "\fB-V \fIenvid\fR"
224 /* Specify the envelope ID for notification by servers that
225 /* support DSN.
226 /*
227 /* This feature is available in Postfix 2.3 and later.
228 /* .IP "\fB-XV\fR (Postfix 2.2 and earlier: \fB-V\fR)"
229 /* Variable Envelope Return Path. Given an envelope sender address
230 /* of the form \fIowner-listname\fR@\fIorigin\fR, each recipient
231 /* \fIuser\fR@\fIdomain\fR receives mail with a personalized envelope
232 /* sender address.
233 /* .sp
234 /* By default, the personalized envelope sender address is
235 /* \fIowner-listname\fB+\fIuser\fB=\fIdomain\fR@\fIorigin\fR. The default
236 /* \fB+\fR and \fB=\fR characters are configurable with the
237 /* \fBdefault_verp_delimiters\fR configuration parameter.
238 /* .IP "\fB-XV\fIxy\fR (Postfix 2.2 and earlier: \fB-V\fIxy\fR)"
239 /* As \fB-XV\fR, but uses \fIx\fR and \fIy\fR as the VERP delimiter
240 /* characters, instead of the characters specified with the
241 /* \fBdefault_verp_delimiters\fR configuration parameter.
242 /* .IP \fB-v\fR
243 /* Send an email report of the first delivery attempt (Postfix
244 /* versions 2.1 and later). Mail delivery
245 /* always happens in the background. When multiple \fB-v\fR
246 /* options are given, enable verbose logging for debugging purposes.
247 /* .IP "\fB-X \fIlog_file\fR (ignored)"
248 /* Log mailer traffic. Use the \fBdebug_peer_list\fR and
249 /* \fBdebug_peer_level\fR configuration parameters instead.
250 /* SECURITY
251 /* .ad
252 /* .fi
253 /* By design, this program is not set-user (or group) id. However,
254 /* it must handle data from untrusted, possibly remote, users.
255 /* Thus, the usual precautions need to be taken against malicious
256 /* inputs.
257 /* DIAGNOSTICS
258 /* Problems are logged to \fBsyslogd\fR(8) and to the standard error
259 /* stream.
260 /* ENVIRONMENT
261 /* .ad
262 /* .fi
263 /* .IP \fBMAIL_CONFIG\fR
264 /* Directory with Postfix configuration files.
265 /* .IP "\fBMAIL_VERBOSE\fR (value does not matter)"
266 /* Enable verbose logging for debugging purposes.
267 /* .IP "\fBMAIL_DEBUG\fR (value does not matter)"
268 /* Enable debugging with an external command, as specified with the
269 /* \fBdebugger_command\fR configuration parameter.
270 /* .IP \fBNAME\fR
271 /* The sender full name. This is used only with messages that
272 /* have no \fBFrom:\fR message header. See also the \fB-F\fR
273 /* option above.
274 /* CONFIGURATION PARAMETERS
275 /* .ad
276 /* .fi
277 /* The following \fBmain.cf\fR parameters are especially relevant to
278 /* this program.
279 /* The text below provides only a parameter summary. See
280 /* \fBpostconf\fR(5) for more details including examples.
281 /* COMPATIBILITY CONTROLS
282 /* .ad
283 /* .fi
284 /* Available with Postfix 2.9 and later:
285 /* .IP "\fBsendmail_fix_line_endings (always)\fR"
286 /* Controls how the Postfix sendmail command converts email message
287 /* line endings from <CR><LF> into UNIX format (<LF>).
288 /* TROUBLE SHOOTING CONTROLS
289 /* .ad
290 /* .fi
291 /* The DEBUG_README file gives examples of how to trouble shoot a
292 /* Postfix system.
293 /* .IP "\fBdebugger_command (empty)\fR"
294 /* The external command to execute when a Postfix daemon program is
295 /* invoked with the -D option.
296 /* .IP "\fBdebug_peer_level (2)\fR"
297 /* The increment in verbose logging level when a remote client or
298 /* server matches a pattern in the debug_peer_list parameter.
299 /* .IP "\fBdebug_peer_list (empty)\fR"
300 /* Optional list of remote client or server hostname or network
301 /* address patterns that cause the verbose logging level to increase
302 /* by the amount specified in $debug_peer_level.
303 /* ACCESS CONTROLS
304 /* .ad
305 /* .fi
306 /* Available in Postfix version 2.2 and later:
307 /* .IP "\fBauthorized_flush_users (static:anyone)\fR"
308 /* List of users who are authorized to flush the queue.
309 /* .IP "\fBauthorized_mailq_users (static:anyone)\fR"
310 /* List of users who are authorized to view the queue.
311 /* .IP "\fBauthorized_submit_users (static:anyone)\fR"
312 /* List of users who are authorized to submit mail with the \fBsendmail\fR(1)
313 /* command (and with the privileged \fBpostdrop\fR(1) helper command).
314 /* RESOURCE AND RATE CONTROLS
315 /* .ad
316 /* .fi
317 /* .IP "\fBbounce_size_limit (50000)\fR"
318 /* The maximal amount of original message text that is sent in a
319 /* non-delivery notification.
320 /* .IP "\fBfork_attempts (5)\fR"
321 /* The maximal number of attempts to fork() a child process.
322 /* .IP "\fBfork_delay (1s)\fR"
323 /* The delay between attempts to fork() a child process.
324 /* .IP "\fBhopcount_limit (50)\fR"
325 /* The maximal number of Received: message headers that is allowed
326 /* in the primary message headers.
327 /* .IP "\fBqueue_run_delay (300s)\fR"
328 /* The time between deferred queue scans by the queue manager;
329 /* prior to Postfix 2.4 the default value was 1000s.
330 /* FAST FLUSH CONTROLS
331 /* .ad
332 /* .fi
333 /* The ETRN_README file describes configuration and operation
334 /* details for the Postfix "fast flush" service.
335 /* .IP "\fBfast_flush_domains ($relay_domains)\fR"
336 /* Optional list of destinations that are eligible for per-destination
337 /* logfiles with mail that is queued to those destinations.
338 /* VERP CONTROLS
339 /* .ad
340 /* .fi
341 /* The VERP_README file describes configuration and operation
342 /* details of Postfix support for variable envelope return
343 /* path addresses.
344 /* .IP "\fBdefault_verp_delimiters (+=)\fR"
345 /* The two default VERP delimiter characters.
346 /* .IP "\fBverp_delimiter_filter (-=+)\fR"
347 /* The characters Postfix accepts as VERP delimiter characters on the
348 /* Postfix \fBsendmail\fR(1) command line and in SMTP commands.
349 /* MISCELLANEOUS CONTROLS
350 /* .ad
351 /* .fi
352 /* .IP "\fBalias_database (see 'postconf -d' output)\fR"
353 /* The alias databases for \fBlocal\fR(8) delivery that are updated with
354 /* "\fBnewaliases\fR" or with "\fBsendmail -bi\fR".
355 /* .IP "\fBcommand_directory (see 'postconf -d' output)\fR"
356 /* The location of all postfix administrative commands.
357 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
358 /* The default location of the Postfix main.cf and master.cf
359 /* configuration files.
360 /* .IP "\fBdaemon_directory (see 'postconf -d' output)\fR"
361 /* The directory with Postfix support programs and daemon programs.
362 /* .IP "\fBdefault_database_type (see 'postconf -d' output)\fR"
363 /* The default database type for use in \fBnewaliases\fR(1), \fBpostalias\fR(1)
364 /* and \fBpostmap\fR(1) commands.
365 /* .IP "\fBdelay_warning_time (0h)\fR"
366 /* The time after which the sender receives a copy of the message
367 /* headers of mail that is still queued.
368 /* .IP "\fBimport_environment (see 'postconf -d' output)\fR"
369 /* The list of environment parameters that a privileged Postfix
370 /* process will import from a non-Postfix parent process, or name=value
371 /* environment overrides.
372 /* .IP "\fBmail_owner (postfix)\fR"
373 /* The UNIX system account that owns the Postfix queue and most Postfix
374 /* daemon processes.
375 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
376 /* The location of the Postfix top-level queue directory.
377 /* .IP "\fBremote_header_rewrite_domain (empty)\fR"
378 /* Don't rewrite message headers from remote clients at all when
379 /* this parameter is empty; otherwise, rewrite message headers and
380 /* append the specified domain name to incomplete addresses.
381 /* .IP "\fBsyslog_facility (mail)\fR"
382 /* The syslog facility of Postfix logging.
383 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
384 /* A prefix that is prepended to the process name in syslog
385 /* records, so that, for example, "smtpd" becomes "prefix/smtpd".
386 /* .PP
387 /* Postfix 3.2 and later:
388 /* .IP "\fBalternate_config_directories (empty)\fR"
389 /* A list of non-default Postfix configuration directories that may
390 /* be specified with "-c config_directory" on the command line (in the
391 /* case of \fBsendmail\fR(1), with the "-C" option), or via the MAIL_CONFIG
392 /* environment parameter.
393 /* .IP "\fBmulti_instance_directories (empty)\fR"
394 /* An optional list of non-default Postfix configuration directories;
395 /* these directories belong to additional Postfix instances that share
396 /* the Postfix executable files and documentation with the default
397 /* Postfix instance, and that are started, stopped, etc., together
398 /* with the default Postfix instance.
399 /* FILES
400 /* /var/spool/postfix, mail queue
401 /* /etc/postfix, configuration files
402 /* SEE ALSO
403 /* pickup(8), mail pickup daemon
404 /* qmgr(8), queue manager
405 /* smtpd(8), SMTP server
406 /* flush(8), fast flush service
407 /* postsuper(1), queue maintenance
408 /* postalias(1), create/update/query alias database
409 /* postdrop(1), mail posting utility
410 /* postfix(1), mail system control
411 /* postqueue(1), mail queue control
412 /* syslogd(8), system logging
413 /* README_FILES
414 /* .ad
415 /* .fi
416 /* Use "\fBpostconf readme_directory\fR" or
417 /* "\fBpostconf html_directory\fR" to locate this information.
418 /* .na
419 /* .nf
420 /* DEBUG_README, Postfix debugging howto
421 /* ETRN_README, Postfix ETRN howto
422 /* VERP_README, Postfix VERP howto
423 /* LICENSE
424 /* .ad
425 /* .fi
426 /* The Secure Mailer license must be distributed with this software.
427 /* AUTHOR(S)
428 /* Wietse Venema
429 /* IBM T.J. Watson Research
430 /* P.O. Box 704
431 /* Yorktown Heights, NY 10598, USA
432 /*
433 /* Wietse Venema
434 /* Google, Inc.
435 /* 111 8th Avenue
436 /* New York, NY 10011, USA
437 /*--*/
438 
439 /* System library. */
440 
441 #include <sys_defs.h>
442 #include <sys/stat.h>
443 #include <unistd.h>
444 #include <string.h>
445 #include <stdio.h> /* remove() */
446 #include <stdlib.h>
447 #include <signal.h>
448 #include <fcntl.h>
449 #include <syslog.h>
450 #include <time.h>
451 #include <errno.h>
452 #include <ctype.h>
453 #include <stdarg.h>
454 #include <sysexits.h>
455 
456 /* Utility library. */
457 
458 #include <msg.h>
459 #include <mymalloc.h>
460 #include <vstream.h>
461 #include <msg_vstream.h>
462 #include <msg_syslog.h>
463 #include <vstring_vstream.h>
464 #include <username.h>
465 #include <fullname.h>
466 #include <argv.h>
467 #include <safe.h>
468 #include <iostuff.h>
469 #include <stringops.h>
470 #include <set_ugid.h>
471 #include <connect.h>
472 #include <split_at.h>
473 #include <name_code.h>
474 #include <warn_stat.h>
475 #include <clean_env.h>
476 
477 /* Global library. */
478 
479 #include <mail_queue.h>
480 #include <mail_proto.h>
481 #include <mail_params.h>
482 #include <mail_version.h>
483 #include <record.h>
484 #include <rec_type.h>
485 #include <rec_streamlf.h>
486 #include <mail_conf.h>
487 #include <cleanup_user.h>
488 #include <mail_task.h>
489 #include <mail_run.h>
490 #include <debug_process.h>
491 #include <tok822.h>
492 #include <mail_flush.h>
493 #include <mail_stream.h>
494 #include <verp_sender.h>
495 #include <deliver_request.h>
496 #include <mime_state.h>
497 #include <header_opts.h>
498 #include <mail_dict.h>
499 #include <user_acl.h>
500 #include <dsn_mask.h>
501 #include <mail_parm_split.h>
502 
503 /* Application-specific. */
504 
505  /*
506  * Modes of operation.
507  */
508 #define SM_MODE_ENQUEUE 1 /* delivery mode */
509 #define SM_MODE_NEWALIAS 2 /* initialize alias database */
510 #define SM_MODE_MAILQ 3 /* list mail queue */
511 #define SM_MODE_DAEMON 4 /* daemon mode */
512 #define SM_MODE_USER 5 /* user (stand-alone) mode */
513 #define SM_MODE_FLUSHQ 6 /* user (stand-alone) mode */
514 #define SM_MODE_IGNORE 7 /* ignore this mode */
515 
516  /*
517  * Flag parade. Flags 8-15 are reserved for delivery request trace flags.
518  */
519 #define SM_FLAG_AEOF (1<<0) /* archaic EOF */
520 #define SM_FLAG_XRCPT (1<<1) /* extract recipients from headers */
521 
522 #define SM_FLAG_DEFAULT (SM_FLAG_AEOF)
523 
524  /*
525  * VERP support.
526  */
527 static char *verp_delims;
528 
529  /*
530  * Callback context for extracting recipients.
531  */
532 typedef struct SM_STATE {
533  VSTREAM *dst; /* output stream */
534  ARGV *recipients; /* recipients from regular headers */
535  ARGV *resent_recip; /* recipients from resent headers */
536  int resent; /* resent flag */
537  const char *saved_sender; /* for error messages */
538  uid_t uid; /* for error messages */
539  VSTRING *temp; /* scratch buffer */
540 } SM_STATE;
541 
542  /*
543  * Mail submission ACL, line-end fixing.
544  */
547 
548 static const CONFIG_STR_TABLE str_table[] = {
551  0,
552 };
553 
554  /*
555  * Silly little macros (SLMs).
556  */
557 #define STR vstring_str
558 
559 /* output_text - output partial or complete text line */
560 
561 static void output_text(void *context, int rec_type, const char *buf, ssize_t len,
562  off_t unused_offset)
563 {
564  SM_STATE *state = (SM_STATE *) context;
565 
566  if (rec_put(state->dst, rec_type, buf, len) < 0)
568  "%s(%ld): error writing queue file: %m",
569  state->saved_sender, (long) state->uid);
570 }
571 
572 /* output_header - output one message header */
573 
574 static void output_header(void *context, int header_class,
575  const HEADER_OPTS *header_info,
576  VSTRING *buf, off_t offset)
577 {
578  SM_STATE *state = (SM_STATE *) context;
579  TOK822 *tree;
580  TOK822 **addr_list;
581  TOK822 **tpp;
582  ARGV *rcpt;
583  char *start;
584  char *line;
585  char *next_line;
586  ssize_t len;
587 
588  /*
589  * Parse the header line, and save copies of recipient addresses in the
590  * appropriate place.
591  */
592  if (header_class == MIME_HDR_PRIMARY
593  && header_info
594  && (header_info->flags & HDR_OPT_RECIP)
595  && (header_info->flags & HDR_OPT_EXTRACT)
596  && (state->resent == 0 || (header_info->flags & HDR_OPT_RR))) {
597  if (header_info->flags & HDR_OPT_RR) {
598  rcpt = state->resent_recip;
599  if (state->resent == 0)
600  state->resent = 1;
601  } else
602  rcpt = state->recipients;
603  tree = tok822_parse(STR(buf) + strlen(header_info->name) + 1);
604  addr_list = tok822_grep(tree, TOK822_ADDR);
605  for (tpp = addr_list; *tpp; tpp++) {
606  tok822_internalize(state->temp, tpp[0]->head, TOK822_STR_DEFL);
607  argv_add(rcpt, STR(state->temp), (char *) 0);
608  }
609  myfree((void *) addr_list);
610  tok822_free_tree(tree);
611  }
612 
613  /*
614  * Pipe the unmodified message header through the header line folding
615  * routine, and ensure that long lines are chopped appropriately.
616  */
617  for (line = start = STR(buf); line; line = next_line) {
618  next_line = split_at(line, '\n');
619  len = next_line ? next_line - line - 1 : strlen(line);
620  do {
621  if (len > var_line_limit) {
622  output_text(context, REC_TYPE_CONT, line, var_line_limit, offset);
623  line += var_line_limit;
624  len -= var_line_limit;
625  offset += var_line_limit;
626  } else {
627  output_text(context, REC_TYPE_NORM, line, len, offset);
628  offset += len;
629  break;
630  }
631  } while (len > 0);
632  offset += 1;
633  }
634 }
635 
636 /* enqueue - post one message */
637 
638 static void enqueue(const int flags, const char *encoding,
639  const char *dsn_envid, int dsn_ret, int dsn_notify,
640  const char *rewrite_context, const char *sender,
641  const char *full_name, char **recipients)
642 {
643  VSTRING *buf;
644  VSTREAM *dst;
645  char *saved_sender;
646  char **cpp;
647  int type;
648  char *start;
649  int skip_from_;
650  TOK822 *tree;
651  TOK822 *tp;
652  int rcpt_count = 0;
653  enum {
654  STRIP_CR_DUNNO, STRIP_CR_DO, STRIP_CR_DONT, STRIP_CR_ERROR
655  } strip_cr;
656  MAIL_STREAM *handle;
657  VSTRING *postdrop_command;
658  uid_t uid = getuid();
659  int status;
660  int naddr;
661  int prev_type;
662  MIME_STATE *mime_state = 0;
663  SM_STATE state;
664  int mime_errs;
665  const char *errstr;
666  int addr_count;
667  int level;
668  static NAME_CODE sm_fix_eol_table[] = {
669  SM_FIX_EOL_ALWAYS, STRIP_CR_DO,
670  SM_FIX_EOL_STRICT, STRIP_CR_DUNNO,
671  SM_FIX_EOL_NEVER, STRIP_CR_DONT,
672  0, STRIP_CR_ERROR,
673  };
674 
675  /*
676  * Access control is enforced in the postdrop command. The code here
677  * merely produces a more user-friendly interface.
678  */
679  if ((errstr = check_user_acl_byuid(VAR_SUBMIT_ACL,
680  var_submit_acl, uid)) != 0)
682  "User %s(%ld) is not allowed to submit mail", errstr, (long) uid);
683 
684  /*
685  * Initialize.
686  */
687  buf = vstring_alloc(100);
688 
689  /*
690  * Stop run-away process accidents by limiting the queue file size. This
691  * is not a defense against DOS attack.
692  */
695 
696  /*
697  * The sender name is provided by the user. In principle, the mail pickup
698  * service could deduce the sender name from queue file ownership, but:
699  * pickup would not be able to run chrooted, and it may not be desirable
700  * to use login names at all.
701  */
702  if (sender != 0) {
703  VSTRING_RESET(buf);
704  VSTRING_TERMINATE(buf);
705  tree = tok822_parse(sender);
706  for (naddr = 0, tp = tree; tp != 0; tp = tp->next)
707  if (tp->type == TOK822_ADDR && naddr++ == 0)
709  tok822_free_tree(tree);
710  saved_sender = mystrdup(STR(buf));
711  if (naddr > 1)
712  msg_warn("-f option specified malformed sender: %s", sender);
713  } else {
714  if ((sender = username()) == 0)
715  msg_fatal_status(EX_OSERR, "no login name found for user ID %lu",
716  (unsigned long) uid);
717  saved_sender = mystrdup(sender);
718  }
719 
720  /*
721  * Let the postdrop command open the queue file for us, and sanity check
722  * the content. XXX Make postdrop a manifest constant.
723  */
724  errno = 0;
725  postdrop_command = vstring_alloc(1000);
726  vstring_sprintf(postdrop_command, "%s/postdrop -r", var_command_dir);
727  for (level = 0; level < msg_verbose; level++)
728  vstring_strcat(postdrop_command, " -v");
729  if ((handle = mail_stream_command(STR(postdrop_command))) == 0)
730  msg_fatal_status(EX_UNAVAILABLE, "%s(%ld): unable to execute %s: %m",
731  saved_sender, (long) uid, STR(postdrop_command));
732  vstring_free(postdrop_command);
733  dst = handle->stream;
734 
735  /*
736  * First, write envelope information to the output stream.
737  *
738  * For sendmail compatibility, parse each command-line recipient as if it
739  * were an RFC 822 message header; some MUAs specify comma-separated
740  * recipient lists; and some MUAs even specify "word word <address>".
741  *
742  * Sort-uniq-ing the recipient list is done after address canonicalization,
743  * before recipients are written to queue file. That's cleaner than
744  * having the queue manager nuke duplicate recipient status records.
745  *
746  * XXX Should limit the size of envelope records.
747  *
748  * With "sendmail -N", instead of a per-message NOTIFY record we store one
749  * per recipient so that we can simplify the implementation somewhat.
750  */
751  if (dsn_envid)
752  rec_fprintf(dst, REC_TYPE_ATTR, "%s=%s",
753  MAIL_ATTR_DSN_ENVID, dsn_envid);
754  if (dsn_ret)
755  rec_fprintf(dst, REC_TYPE_ATTR, "%s=%d",
756  MAIL_ATTR_DSN_RET, dsn_ret);
757  rec_fprintf(dst, REC_TYPE_ATTR, "%s=%s",
758  MAIL_ATTR_RWR_CONTEXT, rewrite_context);
759  if (full_name || (full_name = fullname()) != 0)
760  rec_fputs(dst, REC_TYPE_FULL, full_name);
761  rec_fputs(dst, REC_TYPE_FROM, saved_sender);
762  if (verp_delims && *saved_sender == 0)
764  "%s(%ld): -V option requires non-null sender address",
765  saved_sender, (long) uid);
766  if (encoding)
767  rec_fprintf(dst, REC_TYPE_ATTR, "%s=%s", MAIL_ATTR_ENCODING, encoding);
768  if (DEL_REQ_TRACE_FLAGS(flags))
770  DEL_REQ_TRACE_FLAGS(flags));
771  if (verp_delims)
772  rec_fputs(dst, REC_TYPE_VERP, verp_delims);
773  if (recipients) {
774  for (cpp = recipients; *cpp != 0; cpp++) {
775  tree = tok822_parse(*cpp);
776  for (addr_count = 0, tp = tree; tp != 0; tp = tp->next) {
777  if (tp->type == TOK822_ADDR) {
779  if (dsn_notify)
780  rec_fprintf(dst, REC_TYPE_ATTR, "%s=%d",
781  MAIL_ATTR_DSN_NOTIFY, dsn_notify);
782  if (REC_PUT_BUF(dst, REC_TYPE_RCPT, buf) < 0)
784  "%s(%ld): error writing queue file: %m",
785  saved_sender, (long) uid);
786  ++rcpt_count;
787  ++addr_count;
788  }
789  }
790  tok822_free_tree(tree);
791  if (addr_count == 0) {
792  if (rec_put(dst, REC_TYPE_RCPT, "", 0) < 0)
794  "%s(%ld): error writing queue file: %m",
795  saved_sender, (long) uid);
796  ++rcpt_count;
797  }
798  }
799  }
800 
801  /*
802  * Append the message contents to the queue file. Write chunks of at most
803  * 1kbyte. Internally, we use different record types for data ending in
804  * LF and for data that doesn't, so we can actually be binary transparent
805  * for local mail. Unfortunately, SMTP has no record continuation
806  * convention, so there is no guarantee that arbitrary data will be
807  * delivered intact via SMTP. Strip leading From_ lines. For the benefit
808  * of UUCP environments, also get rid of leading >>>From_ lines.
809  */
810  rec_fputs(dst, REC_TYPE_MESG, "");
811  if (DEL_REQ_TRACE_ONLY(flags) != 0) {
812  if (flags & SM_FLAG_XRCPT)
813  msg_fatal_status(EX_USAGE, "%s(%ld): -t option cannot be used with -bv",
814  saved_sender, (long) uid);
815  if (*saved_sender)
816  rec_fprintf(dst, REC_TYPE_NORM, "From: %s", saved_sender);
817  rec_fprintf(dst, REC_TYPE_NORM, "Subject: probe");
818  if (recipients) {
819  rec_fprintf(dst, REC_TYPE_CONT, "To:");
820  for (cpp = recipients; *cpp != 0; cpp++) {
821  rec_fprintf(dst, REC_TYPE_NORM, " %s%s",
822  *cpp, cpp[1] ? "," : "");
823  }
824  }
825  } else {
826 
827  /*
828  * Initialize the MIME processor and set up the callback context.
829  */
830  if (flags & SM_FLAG_XRCPT) {
831  state.dst = dst;
832  state.recipients = argv_alloc(2);
833  state.resent_recip = argv_alloc(2);
834  state.resent = 0;
835  state.saved_sender = saved_sender;
836  state.uid = uid;
837  state.temp = vstring_alloc(10);
840  output_header,
841  (MIME_STATE_ANY_END) 0,
842  output_text,
843  (MIME_STATE_ANY_END) 0,
845  (void *) &state);
846  }
847 
848  /*
849  * Process header/body lines.
850  */
851  skip_from_ = 1;
852  strip_cr = name_code(sm_fix_eol_table, NAME_CODE_FLAG_STRICT_CASE,
854  if (strip_cr == STRIP_CR_ERROR)
856  "invalid %s value: %s", VAR_SM_FIX_EOL, var_sm_fix_eol);
857  for (prev_type = 0; (type = rec_streamlf_get(VSTREAM_IN, buf, var_line_limit))
858  != REC_TYPE_EOF; prev_type = type) {
859  if (strip_cr == STRIP_CR_DUNNO && type == REC_TYPE_NORM) {
860  if (VSTRING_LEN(buf) > 0 && vstring_end(buf)[-1] == '\r')
861  strip_cr = STRIP_CR_DO;
862  else
863  strip_cr = STRIP_CR_DONT;
864  }
865  if (skip_from_) {
866  if (type == REC_TYPE_NORM) {
867  start = STR(buf);
868  if (strncmp(start + strspn(start, ">"), "From ", 5) == 0)
869  continue;
870  }
871  skip_from_ = 0;
872  }
873  if (strip_cr == STRIP_CR_DO && type == REC_TYPE_NORM)
874  while (VSTRING_LEN(buf) > 0 && vstring_end(buf)[-1] == '\r')
875  vstring_truncate(buf, VSTRING_LEN(buf) - 1);
876  if ((flags & SM_FLAG_AEOF) && prev_type != REC_TYPE_CONT
877  && VSTRING_LEN(buf) == 1 && *STR(buf) == '.')
878  break;
879  if (mime_state) {
880  mime_errs = mime_state_update(mime_state, type, STR(buf),
881  VSTRING_LEN(buf));
882  if (mime_errs)
884  "%s(%ld): unable to extract recipients: %s",
885  saved_sender, (long) uid,
886  mime_state_error(mime_errs));
887  } else {
888  if (REC_PUT_BUF(dst, type, buf) < 0)
890  "%s(%ld): error writing queue file: %m",
891  saved_sender, (long) uid);
892  }
893  }
894  }
895 
896  /*
897  * Finish MIME processing. We need a final mime_state_update() call in
898  * order to flush text that is still buffered. That can happen when the
899  * last line did not end in newline.
900  */
901  if (mime_state) {
902  mime_errs = mime_state_update(mime_state, REC_TYPE_EOF, "", 0);
903  if (mime_errs)
905  "%s(%ld): unable to extract recipients: %s",
906  saved_sender, (long) uid,
907  mime_state_error(mime_errs));
908  mime_state = mime_state_free(mime_state);
909  }
910 
911  /*
912  * Append recipient addresses that were extracted from message headers.
913  */
914  rec_fputs(dst, REC_TYPE_XTRA, "");
915  if (flags & SM_FLAG_XRCPT) {
916  for (cpp = state.resent ? state.resent_recip->argv :
917  state.recipients->argv; *cpp; cpp++) {
918  if (dsn_notify)
919  rec_fprintf(dst, REC_TYPE_ATTR, "%s=%d",
920  MAIL_ATTR_DSN_NOTIFY, dsn_notify);
921  if (rec_put(dst, REC_TYPE_RCPT, *cpp, strlen(*cpp)) < 0)
923  "%s(%ld): error writing queue file: %m",
924  saved_sender, (long) uid);
925  ++rcpt_count;
926  }
927  argv_free(state.recipients);
928  argv_free(state.resent_recip);
929  vstring_free(state.temp);
930  }
931  if (rcpt_count == 0)
932  msg_fatal_status(EX_USAGE, (flags & SM_FLAG_XRCPT) ?
933  "%s(%ld): No recipient addresses found in message header" :
934  "%s(%ld): Recipient addresses must be specified on"
935  " the command line or via the -t option",
936  saved_sender, (long) uid);
937 
938  /*
939  * Identify the end of the queue file.
940  */
941  rec_fputs(dst, REC_TYPE_END, "");
942 
943  /*
944  * Make sure that the message makes it to the file system. Once we have
945  * terminated with successful exit status we cannot lose the message due
946  * to "frivolous reasons". If all goes well, prevent the run-time error
947  * handler from removing the file.
948  */
950  msg_fatal_status(EX_DATAERR, "%s(%ld): error reading input: %m",
951  saved_sender, (long) uid);
952  if ((status = mail_stream_finish(handle, (VSTRING *) 0)) != 0)
954  (status & CLEANUP_STAT_WRITE) ? EX_TEMPFAIL :
955  EX_UNAVAILABLE, "%s(%ld): %s", saved_sender,
956  (long) uid, cleanup_strerror(status));
957 
958  /*
959  * Don't leave them in the dark.
960  */
961  if (DEL_REQ_TRACE_FLAGS(flags)) {
962  vstream_printf("Mail Delivery Status Report will be mailed to <%s>.\n",
963  saved_sender);
965  }
966 
967  /*
968  * Cleanup. Not really necessary as we're about to exit, but good for
969  * debugging purposes.
970  */
971  vstring_free(buf);
972  myfree(saved_sender);
973 }
974 
975 /* tempfail - sanitize exit status after library run-time error */
976 
977 static void tempfail(void)
978 {
979  exit(EX_TEMPFAIL);
980 }
981 
983 
984 /* main - the main program */
985 
986 int main(int argc, char **argv)
987 {
988  static char *full_name = 0; /* sendmail -F */
989  struct stat st;
990  char *slash;
991  char *sender = 0; /* sendmail -f */
992  int c;
993  int fd;
994  int mode;
995  ARGV *ext_argv;
996  int debug_me = 0;
997  int err;
998  int n;
999  int flags = SM_FLAG_DEFAULT;
1000  char *site_to_flush = 0;
1001  char *id_to_flush = 0;
1002  char *encoding = 0;
1003  char *qtime = 0;
1004  const char *errstr;
1005  uid_t uid;
1006  const char *rewrite_context = MAIL_ATTR_RWR_LOCAL;
1007  int dsn_notify = 0;
1008  int dsn_ret = 0;
1009  const char *dsn_envid = 0;
1010  int saved_optind;
1011  ARGV *import_env;
1012 
1013  /*
1014  * Fingerprint executables and core dumps.
1015  */
1017 
1018  /*
1019  * Be consistent with file permissions.
1020  */
1021  umask(022);
1022 
1023  /*
1024  * To minimize confusion, make sure that the standard file descriptors
1025  * are open before opening anything else. XXX Work around for 44BSD where
1026  * fstat can return EBADF on an open file descriptor.
1027  */
1028  for (fd = 0; fd < 3; fd++)
1029  if (fstat(fd, &st) == -1
1030  && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
1031  msg_fatal_status(EX_OSERR, "open /dev/null: %m");
1032 
1033  /*
1034  * The CDE desktop calendar manager leaks a parent file descriptor into
1035  * the child process. For the sake of sendmail compatibility we have to
1036  * close the file descriptor otherwise mail notification will hang.
1037  */
1038  for ( /* void */ ; fd < 100; fd++)
1039  (void) close(fd);
1040 
1041  /*
1042  * Process environment options as early as we can. We might be called
1043  * from a set-uid (set-gid) program, so be careful with importing
1044  * environment variables.
1045  */
1047  msg_verbose = 1;
1049  debug_me = 1;
1050 
1051  /*
1052  * Initialize. Set up logging, read the global configuration file and
1053  * extract configuration information. Set up signal handlers so that we
1054  * can clean up incomplete output.
1055  */
1056  if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
1057  argv[0] = slash + 1;
1058  msg_vstream_init(argv[0], VSTREAM_ERR);
1059  msg_cleanup(tempfail);
1060  msg_syslog_init(mail_task("sendmail"), LOG_PID, LOG_FACILITY);
1062 
1063  /*
1064  * Check the Postfix library version as soon as we enable logging.
1065  */
1067 
1068  /*
1069  * Some sites mistakenly install Postfix sendmail as set-uid root. Drop
1070  * set-uid privileges only when root, otherwise some systems will not
1071  * reset the saved set-userid, which would be a security vulnerability.
1072  */
1073  if (geteuid() == 0 && getuid() != 0) {
1074  msg_warn("the Postfix sendmail command has set-uid root file permissions");
1075  msg_warn("or the command is run from a set-uid root process");
1076  msg_warn("the Postfix sendmail command must be installed without set-uid root file permissions");
1077  set_ugid(getuid(), getgid());
1078  }
1079 
1080  /*
1081  * Further initialization. Load main.cf first, so that command-line
1082  * options can override main.cf settings. Pre-scan the argument list so
1083  * that we load the right main.cf file.
1084  */
1085 #define GETOPT_LIST "A:B:C:F:GIL:N:O:R:UV:X:b:ce:f:h:imno:p:r:q:tvx"
1086 
1087  saved_optind = optind;
1088  while (argv[OPTIND] != 0) {
1089  if (strcmp(argv[OPTIND], "-q") == 0) { /* not getopt compatible */
1090  optind++;
1091  continue;
1092  }
1093  if ((c = GETOPT(argc, argv, GETOPT_LIST)) <= 0)
1094  break;
1095  if (c == 'C') {
1096  VSTRING *buf = vstring_alloc(1);
1097  char *dir;
1098 
1099  dir = strcmp(sane_basename(buf, optarg), MAIN_CONF_FILE) == 0 ?
1100  sane_dirname(buf, optarg) : optarg;
1101  if (strcmp(dir, DEF_CONFIG_DIR) != 0 && geteuid() != 0)
1102  mail_conf_checkdir(dir);
1103  if (setenv(CONF_ENV_PATH, dir, 1) < 0)
1104  msg_fatal_status(EX_UNAVAILABLE, "out of memory");
1105  vstring_free(buf);
1106  }
1107  }
1108  optind = saved_optind;
1109  mail_conf_read();
1110  /* Enforce consistent operation of different Postfix parts. */
1112  update_env(import_env->argv);
1113  argv_free(import_env);
1114  /* Re-evaluate mail_task() after reading main.cf. */
1115  msg_syslog_init(mail_task("sendmail"), LOG_PID, LOG_FACILITY);
1116  get_mail_conf_str_table(str_table);
1117 
1118  mail_dict_init();
1119 
1120  if (chdir(var_queue_dir))
1121  msg_fatal_status(EX_UNAVAILABLE, "chdir %s: %m", var_queue_dir);
1122 
1123  signal(SIGPIPE, SIG_IGN);
1124 
1125  /*
1126  * Optionally start the debugger on ourself. This must be done after
1127  * reading the global configuration file, because that file specifies
1128  * what debugger command to execute.
1129  */
1130  if (debug_me)
1131  debug_process();
1132 
1133  /*
1134  * The default mode of operation is determined by the process name. It
1135  * can, however, be changed via command-line options (for example,
1136  * "newaliases -bp" will show the mail queue).
1137  */
1138  if (strcmp(argv[0], "mailq") == 0) {
1139  mode = SM_MODE_MAILQ;
1140  } else if (strcmp(argv[0], "newaliases") == 0) {
1141  mode = SM_MODE_NEWALIAS;
1142  } else if (strcmp(argv[0], "smtpd") == 0) {
1143  mode = SM_MODE_DAEMON;
1144  } else {
1145  mode = SM_MODE_ENQUEUE;
1146  }
1147 
1148  /*
1149  * Parse JCL. Sendmail has been around for a long time, and has acquired
1150  * a large number of options in the course of time. Some options such as
1151  * -q are not parsable with GETOPT() and get special treatment.
1152  */
1153 #define OPTIND (optind > 0 ? optind : 1)
1154 
1155  while (argv[OPTIND] != 0) {
1156  if (strcmp(argv[OPTIND], "-q") == 0) {
1157  if (mode == SM_MODE_DAEMON)
1158  msg_warn("ignoring -q option in daemon mode");
1159  else
1160  mode = SM_MODE_FLUSHQ;
1161  optind++;
1162  continue;
1163  }
1164  if (strcmp(argv[OPTIND], "-V") == 0
1165  && argv[OPTIND + 1] != 0 && strlen(argv[OPTIND + 1]) == 2) {
1166  msg_warn("option -V is deprecated with Postfix 2.3; "
1167  "specify -XV instead");
1168  argv[OPTIND] = "-XV";
1169  }
1170  if (strncmp(argv[OPTIND], "-V", 2) == 0 && strlen(argv[OPTIND]) == 4) {
1171  msg_warn("option %s is deprecated with Postfix 2.3; "
1172  "specify -X%s instead",
1173  argv[OPTIND], argv[OPTIND] + 1);
1174  argv[OPTIND] = concatenate("-X", argv[OPTIND] + 1, (char *) 0);
1175  }
1176  if (strcmp(argv[OPTIND], "-XV") == 0) {
1177  verp_delims = var_verp_delims;
1178  optind++;
1179  continue;
1180  }
1181  if ((c = GETOPT(argc, argv, GETOPT_LIST)) <= 0)
1182  break;
1183  switch (c) {
1184  default:
1185  if (msg_verbose)
1186  msg_info("-%c option ignored", c);
1187  break;
1188  case 'n':
1189  msg_fatal_status(EX_USAGE, "-%c option not supported", c);
1190  case 'B':
1191  if (strcmp(optarg, "8BITMIME") == 0)/* RFC 1652 */
1192  encoding = MAIL_ATTR_ENC_8BIT;
1193  else if (strcmp(optarg, "7BIT") == 0) /* RFC 1652 */
1194  encoding = MAIL_ATTR_ENC_7BIT;
1195  else
1196  msg_fatal_status(EX_USAGE, "-B option needs 8BITMIME or 7BIT");
1197  break;
1198  case 'F': /* full name */
1199  full_name = optarg;
1200  break;
1201  case 'G': /* gateway submission */
1202  rewrite_context = MAIL_ATTR_RWR_REMOTE;
1203  break;
1204  case 'I': /* newaliases */
1205  mode = SM_MODE_NEWALIAS;
1206  break;
1207  case 'N':
1208  if ((dsn_notify = dsn_notify_mask(optarg)) == 0)
1209  msg_warn("bad -N option value -- ignored");
1210  break;
1211  case 'R':
1212  if ((dsn_ret = dsn_ret_code(optarg)) == 0)
1213  msg_warn("bad -R option value -- ignored");
1214  break;
1215  case 'V': /* DSN, was: VERP */
1216  if (strlen(optarg) > 100)
1217  msg_warn("too long -V option value -- ignored");
1218  else if (!allprint(optarg))
1219  msg_warn("bad syntax in -V option value -- ignored");
1220  else
1221  dsn_envid = optarg;
1222  break;
1223  case 'X':
1224  switch (*optarg) {
1225  default:
1226  msg_fatal_status(EX_USAGE, "unsupported: -%c%c", c, *optarg);
1227  case 'V': /* VERP */
1228  if (verp_delims_verify(optarg + 1) != 0)
1229  msg_fatal_status(EX_USAGE, "-V requires two characters from %s",
1230  var_verp_filter);
1231  verp_delims = optarg + 1;
1232  break;
1233  }
1234  break;
1235  case 'b':
1236  switch (*optarg) {
1237  default:
1238  msg_fatal_status(EX_USAGE, "unsupported: -%c%c", c, *optarg);
1239  case 'd': /* daemon mode */
1240  case 'l': /* daemon mode */
1241  if (mode == SM_MODE_FLUSHQ)
1242  msg_warn("ignoring -q option in daemon mode");
1243  mode = SM_MODE_DAEMON;
1244  break;
1245  case 'h': /* print host status */
1246  case 'H': /* flush host status */
1247  mode = SM_MODE_IGNORE;
1248  break;
1249  case 'i': /* newaliases */
1250  mode = SM_MODE_NEWALIAS;
1251  break;
1252  case 'm': /* deliver mail */
1253  mode = SM_MODE_ENQUEUE;
1254  break;
1255  case 'p': /* mailq */
1256  mode = SM_MODE_MAILQ;
1257  break;
1258  case 's': /* stand-alone mode */
1259  mode = SM_MODE_USER;
1260  break;
1261  case 'v': /* expand recipients */
1262  flags |= DEL_REQ_FLAG_USR_VRFY;
1263  break;
1264  }
1265  break;
1266  case 'f':
1267  sender = optarg;
1268  break;
1269  case 'i':
1270  flags &= ~SM_FLAG_AEOF;
1271  break;
1272  case 'o':
1273  switch (*optarg) {
1274  default:
1275  if (msg_verbose)
1276  msg_info("-%c%c option ignored", c, *optarg);
1277  break;
1278  case 'A':
1279  if (optarg[1] == 0)
1280  msg_fatal_status(EX_USAGE, "-oA requires pathname");
1282  var_alias_db_map = mystrdup(optarg + 1);
1284  break;
1285  case '7':
1286  case '8':
1287  break;
1288  case 'i':
1289  flags &= ~SM_FLAG_AEOF;
1290  break;
1291  case 'm':
1292  break;
1293  }
1294  break;
1295  case 'r': /* obsoleted by -f */
1296  sender = optarg;
1297  break;
1298  case 'q':
1299  if (ISDIGIT(optarg[0])) {
1300  qtime = optarg;
1301  } else if (optarg[0] == 'R') {
1302  site_to_flush = optarg + 1;
1303  if (*site_to_flush == 0)
1304  msg_fatal_status(EX_USAGE, "specify: -qRsitename");
1305  } else if (optarg[0] == 'I') {
1306  id_to_flush = optarg + 1;
1307  if (*id_to_flush == 0)
1308  msg_fatal_status(EX_USAGE, "specify: -qIqueueid");
1309  } else {
1310  msg_fatal_status(EX_USAGE, "-q%c is not implemented",
1311  optarg[0]);
1312  }
1313  break;
1314  case 't':
1315  flags |= SM_FLAG_XRCPT;
1316  break;
1317  case 'v':
1318  msg_verbose++;
1319  break;
1320  case '?':
1321  msg_fatal_status(EX_USAGE, "usage: %s [options]", argv[0]);
1322  }
1323  }
1324 
1325  /*
1326  * Look for conflicting options and arguments.
1327  */
1328  if ((flags & SM_FLAG_XRCPT) && mode != SM_MODE_ENQUEUE)
1329  msg_fatal_status(EX_USAGE, "-t can be used only in delivery mode");
1330 
1331  if (site_to_flush && mode != SM_MODE_ENQUEUE)
1332  msg_fatal_status(EX_USAGE, "-qR can be used only in delivery mode");
1333 
1334  if (id_to_flush && mode != SM_MODE_ENQUEUE)
1335  msg_fatal_status(EX_USAGE, "-qI can be used only in delivery mode");
1336 
1337  if (flags & DEL_REQ_FLAG_USR_VRFY) {
1338  if (flags & SM_FLAG_XRCPT)
1339  msg_fatal_status(EX_USAGE, "-t option cannot be used with -bv");
1340  if (dsn_notify)
1341  msg_fatal_status(EX_USAGE, "-N option cannot be used with -bv");
1342  if (dsn_ret)
1343  msg_fatal_status(EX_USAGE, "-R option cannot be used with -bv");
1344  if (msg_verbose == 1)
1345  msg_fatal_status(EX_USAGE, "-v option cannot be used with -bv");
1346  }
1347 
1348  /*
1349  * The -v option plays double duty. One requests verbose delivery, more
1350  * than one requests verbose logging.
1351  */
1352  if (msg_verbose == 1 && mode == SM_MODE_ENQUEUE) {
1353  msg_verbose = 0;
1354  flags |= DEL_REQ_FLAG_RECORD;
1355  }
1356 
1357  /*
1358  * Start processing. Everything is delegated to external commands.
1359  */
1360  if (qtime && mode != SM_MODE_DAEMON)
1361  exit(0);
1362  switch (mode) {
1363  default:
1364  msg_panic("unknown operation mode: %d", mode);
1365  /* NOTREACHED */
1366  case SM_MODE_ENQUEUE:
1367  if (site_to_flush) {
1368  if (argv[OPTIND])
1369  msg_fatal_status(EX_USAGE, "flush site requires no recipient");
1370  ext_argv = argv_alloc(2);
1371  argv_add(ext_argv, "postqueue", "-s", site_to_flush, (char *) 0);
1372  for (n = 0; n < msg_verbose; n++)
1373  argv_add(ext_argv, "-v", (char *) 0);
1374  argv_terminate(ext_argv);
1376  /* NOTREACHED */
1377  } else if (id_to_flush) {
1378  if (argv[OPTIND])
1379  msg_fatal_status(EX_USAGE, "flush queue_id requires no recipient");
1380  ext_argv = argv_alloc(2);
1381  argv_add(ext_argv, "postqueue", "-i", id_to_flush, (char *) 0);
1382  for (n = 0; n < msg_verbose; n++)
1383  argv_add(ext_argv, "-v", (char *) 0);
1384  argv_terminate(ext_argv);
1386  /* NOTREACHED */
1387  } else {
1388  enqueue(flags, encoding, dsn_envid, dsn_ret, dsn_notify,
1389  rewrite_context, sender, full_name, argv + OPTIND);
1390  exit(0);
1391  /* NOTREACHED */
1392  }
1393  break;
1394  case SM_MODE_MAILQ:
1395  if (argv[OPTIND])
1397  "display queue mode requires no recipient");
1398  ext_argv = argv_alloc(2);
1399  argv_add(ext_argv, "postqueue", "-p", (char *) 0);
1400  for (n = 0; n < msg_verbose; n++)
1401  argv_add(ext_argv, "-v", (char *) 0);
1402  argv_terminate(ext_argv);
1404  /* NOTREACHED */
1405  case SM_MODE_FLUSHQ:
1406  if (argv[OPTIND])
1408  "flush queue mode requires no recipient");
1409  ext_argv = argv_alloc(2);
1410  argv_add(ext_argv, "postqueue", "-f", (char *) 0);
1411  for (n = 0; n < msg_verbose; n++)
1412  argv_add(ext_argv, "-v", (char *) 0);
1413  argv_terminate(ext_argv);
1415  /* NOTREACHED */
1416  case SM_MODE_DAEMON:
1417  if (argv[OPTIND])
1418  msg_fatal_status(EX_USAGE, "daemon mode requires no recipient");
1419  ext_argv = argv_alloc(2);
1420  argv_add(ext_argv, "postfix", (char *) 0);
1421  for (n = 0; n < msg_verbose; n++)
1422  argv_add(ext_argv, "-v", (char *) 0);
1423  argv_add(ext_argv, "start", (char *) 0);
1424  argv_terminate(ext_argv);
1425  err = (mail_run_background(var_command_dir, ext_argv->argv) < 0);
1426  argv_free(ext_argv);
1427  exit(err);
1428  break;
1429  case SM_MODE_NEWALIAS:
1430  if (argv[OPTIND])
1432  "alias initialization mode requires no recipient");
1433  if (*var_alias_db_map == 0)
1434  return (0);
1435  ext_argv = argv_alloc(2);
1436  argv_add(ext_argv, "postalias", (char *) 0);
1437  for (n = 0; n < msg_verbose; n++)
1438  argv_add(ext_argv, "-v", (char *) 0);
1440  argv_terminate(ext_argv);
1442  /* NOTREACHED */
1443  case SM_MODE_USER:
1444  if (argv[OPTIND])
1446  "stand-alone mode requires no recipient");
1447  /* The actual enforcement happens in the postdrop command. */
1449  uid = getuid())) != 0)
1451  "User %s(%ld) is not allowed to submit mail",
1452  errstr, (long) uid);
1453  ext_argv = argv_alloc(2);
1454  argv_add(ext_argv, "smtpd", "-S", (char *) 0);
1455  for (n = 0; n < msg_verbose; n++)
1456  argv_add(ext_argv, "-v", (char *) 0);
1457  argv_terminate(ext_argv);
1458  mail_run_replace(var_daemon_dir, ext_argv->argv);
1459  /* NOTREACHED */
1460  case SM_MODE_IGNORE:
1461  exit(0);
1462  /* NOTREACHED */
1463  }
1464 }
int mail_stream_finish(MAIL_STREAM *info, VSTRING *why)
Definition: mail_stream.c:398
int msg_verbose
Definition: msg.c:177
#define MAIL_ATTR_DSN_NOTIFY
Definition: mail_proto.h:275
VSTREAM * dst
Definition: sendmail.c:533
#define SM_MODE_FLUSHQ
Definition: sendmail.c:513
int resent
Definition: sendmail.c:536
#define SM_MODE_IGNORE
Definition: sendmail.c:514
void myfree(void *ptr)
Definition: mymalloc.c:207
#define SM_MODE_NEWALIAS
Definition: sendmail.c:509
#define MAIL_ATTR_TRACE_FLAGS
Definition: mail_proto.h:149
const char * mail_task(const char *argv0)
Definition: mail_task.c:49
char * var_procname
Definition: mail_params.c:252
#define SM_FLAG_DEFAULT
Definition: sendmail.c:522
VSTRING * temp
Definition: sendmail.c:539
char * mystrdup(const char *str)
Definition: mymalloc.c:225
#define MAIL_ATTR_ENCODING
Definition: mail_proto.h:202
#define TOK822_ADDR
Definition: tok822.h:46
#define DEF_SUBMIT_ACL
Definition: mail_params.h:118
char * var_import_environ
Definition: mail_params.c:296
const char * saved_sender
Definition: sendmail.c:537
char * var_command_dir
Definition: mail_params.c:244
MIME_STATE * mime_state_alloc(int flags, MIME_STATE_HEAD_OUT head_out, MIME_STATE_ANY_END head_end, MIME_STATE_BODY_OUT body_out, MIME_STATE_ANY_END body_end, MIME_STATE_ERR_PRINT err_print, void *context)
Definition: mime_state.c:493
void set_mail_conf_str(const char *, const char *)
ARGV * argv_free(ARGV *argvp)
Definition: argv.c:136
TOK822 ** tok822_grep(TOK822 *, int)
Definition: tok822_tree.c:292
Definition: argv.h:17
#define VAR_IMPORT_ENVIRON
Definition: mail_params.h:2506
char * var_verp_delims
Definition: mail_params.c:289
NORETURN msg_panic(const char *fmt,...)
Definition: msg.c:295
#define VSTREAM_OUT
Definition: vstream.h:67
#define stat(p, s)
Definition: warn_stat.h:18
#define VAR_PROCNAME
Definition: mail_params.h:2435
NORETURN mail_run_replace(const char *dir, char **argv)
Definition: mail_run.c:143
#define LOG_FACILITY
Definition: mail_params.h:357
#define REC_TYPE_VERP
Definition: rec_type.h:68
char * var_submit_acl
Definition: sendmail.c:545
Definition: tok822.h:27
#define tok822_parse(cp)
Definition: tok822.h:84
#define SM_MODE_ENQUEUE
Definition: sendmail.c:508
char ** argv
Definition: argv.h:20
#define MAIL_ATTR_RWR_CONTEXT
Definition: mail_proto.h:163
#define DEL_REQ_TRACE_ONLY(f)
VSTRING * vstring_truncate(VSTRING *vp, ssize_t len)
Definition: vstring.c:415
#define SM_FLAG_XRCPT
Definition: sendmail.c:520
#define MAIL_ATTR_RWR_LOCAL
Definition: mail_proto.h:166
void argv_add(ARGV *argvp,...)
Definition: argv.c:197
void set_file_limit(off_t limit)
Definition: file_limit.c:80
#define DEL_REQ_FLAG_USR_VRFY
#define REC_TYPE_FROM
Definition: rec_type.h:43
#define VSTREAM_IN
Definition: vstream.h:66
#define MAIL_ATTR_RWR_REMOTE
Definition: mail_proto.h:167
#define REC_TYPE_END
Definition: rec_type.h:77
ARGV * argv_alloc(ssize_t len)
Definition: argv.c:149
#define VSTRING_LEN(vp)
Definition: vstring.h:72
#define REC_TYPE_EOF
Definition: rec_type.h:23
char * sane_dirname(VSTRING *bp, const char *path)
char * var_sm_fix_eol
Definition: sendmail.c:546
#define CONF_ENV_VERB
Definition: mail_conf.h:23
int rec_streamlf_get(VSTREAM *stream, VSTRING *buf, int maxlen)
Definition: rec_streamlf.c:76
#define MIME_OPT_DISABLE_MIME
Definition: mime_state.h:47
#define SM_FLAG_AEOF
Definition: sendmail.c:519
MAIL_STREAM * mail_stream_command(const char *command)
Definition: mail_stream.c:462
#define DEL_REQ_TRACE_FLAGS(f)
#define MAIN_CONF_FILE
Definition: mail_params.h:334
TOK822 * tok822_free_tree(TOK822 *)
Definition: tok822_tree.c:262
void mail_conf_read(void)
Definition: mail_conf.c:178
off_t get_file_limit(void)
Definition: file_limit.c:58
#define VSTRING_TERMINATE(vp)
Definition: vstring.h:74
long var_message_limit
Definition: mail_params.c:265
#define vstring_end(vp)
Definition: vstring.h:73
const char * name
Definition: header_opts.h:17
#define EX_DATAERR
Definition: sys_exits.h:32
#define HDR_OPT_EXTRACT
Definition: header_opts.h:66
#define SM_FIX_EOL_NEVER
Definition: mail_params.h:3959
#define REC_PUT_BUF(v, t, b)
Definition: record.h:43
MAIL_VERSION_STAMP_DECLARE
Definition: sendmail.c:982
#define DEF_SM_FIX_EOL
Definition: mail_params.h:3963
#define NAME_CODE_FLAG_STRICT_CASE
Definition: name_code.h:23
#define CONF_ENV_DEBUG
Definition: mail_conf.h:24
#define EX_NOPERM
Definition: sys_exits.h:44
#define VAR_SM_FIX_EOL
Definition: mail_params.h:3962
#define HDR_OPT_RECIP
Definition: header_opts.h:64
#define CONF_ENV_PATH
Definition: mail_conf.h:22
int mime_state_update(MIME_STATE *state, int rec_type, const char *text, ssize_t len)
Definition: mime_state.c:755
#define EX_SOFTWARE
Definition: sys_exits.h:37
DEF_CONFIG_DIR
Definition: install_table.h:1
ARGV * mail_parm_split(const char *name, const char *value)
#define SM_MODE_MAILQ
Definition: sendmail.c:510
#define ISDIGIT(c)
Definition: sys_defs.h:1748
#define REC_TYPE_CONT
Definition: rec_type.h:58
struct TOK822 * head
Definition: tok822.h:32
int type
Definition: tok822.h:28
MIME_STATE * mime_state_free(MIME_STATE *state)
Definition: mime_state.c:530
int dsn_notify_mask(const char *str)
Definition: dsn_mask.c:108
#define SM_MODE_USER
Definition: sendmail.c:512
void mail_conf_checkdir(const char *config_dir)
Definition: mail_conf.c:134
#define REC_TYPE_RCPT
Definition: rec_type.h:45
void(* MIME_STATE_ANY_END)(void *)
Definition: mime_state.h:30
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
#define VSTRING_RESET(vp)
Definition: vstring.h:77
char * safe_getenv(const char *)
Definition: safe_getenv.c:38
int rec_fputs(VSTREAM *stream, int type, const char *str)
Definition: record.c:404
ARGV * recipients
Definition: sendmail.c:534
int mail_run_background(const char *dir, char **argv)
Definition: mail_run.c:113
#define REC_TYPE_MESG
Definition: rec_type.h:56
#define MIME_HDR_PRIMARY
Definition: mime_state.h:80
void msg_warn(const char *fmt,...)
Definition: msg.c:215
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
#define MAIL_ATTR_DSN_RET
Definition: mail_proto.h:274
void mail_dict_init(void)
Definition: mail_dict.c:81
const char * username(void)
Definition: username.c:38
#define MIME_OPT_REPORT_TRUNC_HEADER
Definition: mime_state.h:46
#define MAIL_VERSION_STAMP_ALLOCATE
Definition: mail_version.h:67
#define STR
Definition: sendmail.c:557
struct SM_STATE SM_STATE
VSTRING * vstring_sprintf(VSTRING *vp, const char *format,...)
Definition: vstring.c:602
#define MAIL_ATTR_DSN_ENVID
Definition: mail_proto.h:273
void debug_process(void)
Definition: debug_process.c:50
VSTRING * tok822_internalize(VSTRING *, TOK822 *, int)
Definition: tok822_parse.c:199
#define GETOPT_LIST
void get_mail_conf_str_table(const CONFIG_STR_TABLE *)
int rec_put(VSTREAM *stream, int type, const char *data, ssize_t len)
Definition: record.c:194
int name_code(const NAME_CODE *table, int flags, const char *name)
Definition: name_code.c:65
int var_line_limit
Definition: mail_params.c:263
const char * verp_delims_verify(const char *delims)
Definition: verp_sender.c:104
VSTREAM * stream
Definition: mail_stream.h:35
const char * mime_state_error(int error_code)
Definition: mime_state.c:1149
#define CHARS_COMMA_SP
Definition: sys_defs.h:1761
#define MAIL_VERSION_CHECK
Definition: mail_version.h:90
#define VAR_ALIAS_DB_MAP
Definition: mail_params.h:558
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
void update_env(char **preserve_list)
Definition: clean_env.c:102
ARGV * resent_recip
Definition: sendmail.c:535
#define CLEANUP_STAT_BAD
Definition: cleanup_user.h:57
char * concatenate(const char *arg0,...)
Definition: concatenate.c:42
#define GETOPT(argc, argv, str)
Definition: sys_defs.h:1313
void msg_syslog_init(const char *name, int logopt, int facility)
Definition: msg_syslog.c:173
int dsn_ret_code(const char *str)
Definition: dsn_mask.c:90
#define TOK822_STR_DEFL
Definition: tok822.h:91
#define OPTIND
void set_ugid(uid_t uid, gid_t gid)
Definition: set_ugid.c:45
#define SM_FIX_EOL_STRICT
Definition: mail_params.h:3958
char * sane_basename(VSTRING *bp, const char *path)
Definition: sane_basename.c:69
#define EX_UNAVAILABLE
Definition: sys_exits.h:36
int main(int argc, char **argv)
Definition: sendmail.c:986
#define REC_TYPE_XTRA
Definition: rec_type.h:62
void(* MIME_STATE_ERR_PRINT)(void *, int, const char *, ssize_t)
Definition: mime_state.h:31
#define SM_FIX_EOL_ALWAYS
Definition: mail_params.h:3960
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
char * var_daemon_dir
Definition: mail_params.c:242
const char * check_user_acl_byuid(const char *pname, const char *acl, uid_t uid)
Definition: user_acl.c:65
#define EX_USAGE
Definition: sys_exits.h:31
ARGV * argv_split_append(ARGV *, const char *, const char *)
Definition: argv_split.c:101
char * var_queue_dir
Definition: mail_params.c:246
int allprint(const char *string)
Definition: allprint.c:39
char * split_at(char *string, int delimiter)
Definition: split_at.c:53
#define REC_TYPE_ATTR
Definition: rec_type.h:49
void msg_vstream_init(const char *name, VSTREAM *vp)
Definition: msg_vstream.c:77
uid_t uid
Definition: sendmail.c:538
#define EX_TEMPFAIL
Definition: sys_exits.h:42
#define REC_TYPE_NORM
Definition: rec_type.h:59
#define CLEANUP_STAT_WRITE
Definition: cleanup_user.h:58
NORETURN msg_fatal_status(int status, const char *fmt,...)
Definition: msg.c:272
const char * fullname(void)
Definition: fullname.c:52
const char * cleanup_strerror(unsigned status)
#define HDR_OPT_RR
Definition: header_opts.h:65
#define MAIL_ATTR_ENC_8BIT
Definition: mail_proto.h:203
#define VAR_SUBMIT_ACL
Definition: mail_params.h:117
#define vstream_ferror(vp)
Definition: vstream.h:120
int rec_fprintf(VSTREAM *stream, int type, const char *format,...)
Definition: record.c:391
#define SM_MODE_DAEMON
Definition: sendmail.c:511
char * var_alias_db_map
Definition: mail_params.c:264
#define EX_OSERR
Definition: sys_exits.h:38
#define REC_TYPE_FULL
Definition: rec_type.h:40
MSG_CLEANUP_FN msg_cleanup(MSG_CLEANUP_FN cleanup_fn)
Definition: msg.c:317
VSTRING * vstring_strcat(VSTRING *vp, const char *src)
Definition: vstring.c:459
struct TOK822 * next
Definition: tok822.h:31
#define VSTREAM_ERR
Definition: vstream.h:68
#define MAIL_ATTR_ENC_7BIT
Definition: mail_proto.h:204
#define fstat(f, s)
Definition: warn_stat.h:20
char * var_verp_filter
Definition: mail_params.c:290
void argv_terminate(ARGV *argvp)
Definition: argv.c:242
#define DEL_REQ_FLAG_RECORD
void msg_info(const char *fmt,...)
Definition: msg.c:199