Postfix3.3.1
postkick.c
[詳解]
1 /*++
2 /* NAME
3 /* postkick 1
4 /* SUMMARY
5 /* kick a Postfix service
6 /* SYNOPSIS
7 /* .fi
8 /* \fBpostkick\fR [\fB-c \fIconfig_dir\fR] [\fB-v\fR]
9 /* \fIclass service request\fR
10 /* DESCRIPTION
11 /* The \fBpostkick\fR(1) command sends \fIrequest\fR to the
12 /* specified \fIservice\fR over a local transport channel.
13 /* This command makes Postfix private IPC accessible
14 /* for use in, for example, shell scripts.
15 /*
16 /* Options:
17 /* .IP "\fB-c\fR \fIconfig_dir\fR"
18 /* Read the \fBmain.cf\fR configuration file in the named directory
19 /* instead of the default configuration directory.
20 /* .IP \fB-v\fR
21 /* Enable verbose logging for debugging purposes. Multiple \fB-v\fR
22 /* options make the software increasingly verbose.
23 /* .PP
24 /* Arguments:
25 /* .IP \fIclass\fR
26 /* Name of a class of local transport channel endpoints,
27 /* either \fBpublic\fR (accessible by any local user) or
28 /* \fBprivate\fR (administrative access only).
29 /* .IP \fIservice\fR
30 /* The name of a local transport endpoint within the named class.
31 /* .IP \fIrequest\fR
32 /* A string. The list of valid requests is service-specific.
33 /* DIAGNOSTICS
34 /* Problems and transactions are logged to the standard error
35 /* stream.
36 /* ENVIRONMENT
37 /* .ad
38 /* .fi
39 /* .IP \fBMAIL_CONFIG\fR
40 /* Directory with Postfix configuration files.
41 /* .IP \fBMAIL_VERBOSE\fR
42 /* Enable verbose logging for debugging purposes.
43 /* CONFIGURATION PARAMETERS
44 /* .ad
45 /* .fi
46 /* The following \fBmain.cf\fR parameters are especially relevant to
47 /* this program.
48 /* The text below provides only a parameter summary. See
49 /* \fBpostconf\fR(5) for more details including examples.
50 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
51 /* The default location of the Postfix main.cf and master.cf
52 /* configuration files.
53 /* .IP "\fBapplication_event_drain_time (100s)\fR"
54 /* How long the \fBpostkick\fR(1) command waits for a request to enter the
55 /* Postfix daemon process input buffer before giving up.
56 /* .IP "\fBimport_environment (see 'postconf -d' output)\fR"
57 /* The list of environment parameters that a privileged Postfix
58 /* process will import from a non-Postfix parent process, or name=value
59 /* environment overrides.
60 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
61 /* The location of the Postfix top-level queue directory.
62 /* FILES
63 /* /var/spool/postfix/private, private class endpoints
64 /* /var/spool/postfix/public, public class endpoints
65 /* SEE ALSO
66 /* qmgr(8), queue manager trigger protocol
67 /* pickup(8), local pickup daemon
68 /* postconf(5), configuration parameters
69 /* LICENSE
70 /* .ad
71 /* .fi
72 /* The Secure Mailer license must be distributed with this software.
73 /* AUTHOR(S)
74 /* Wietse Venema
75 /* IBM T.J. Watson Research
76 /* P.O. Box 704
77 /* Yorktown Heights, NY 10598, USA
78 /*
79 /* Wietse Venema
80 /* Google, Inc.
81 /* 111 8th Avenue
82 /* New York, NY 10011, USA
83 /*--*/
84 
85 /* System library. */
86 
87 #include <sys_defs.h>
88 #include <sys/stat.h>
89 #include <fcntl.h>
90 #include <unistd.h>
91 #include <syslog.h>
92 #include <string.h>
93 #include <stdlib.h>
94 
95 /* Utility library. */
96 
97 #include <msg.h>
98 #include <mymalloc.h>
99 #include <vstream.h>
100 #include <msg_vstream.h>
101 #include <safe.h>
102 #include <events.h>
103 #include <warn_stat.h>
104 #include <clean_env.h>
105 
106 /* Global library. */
107 
108 #include <mail_proto.h>
109 #include <mail_params.h>
110 #include <mail_version.h>
111 #include <mail_conf.h>
112 #include <mail_parm_split.h>
113 
114 static NORETURN usage(char *myname)
115 {
116  msg_fatal("usage: %s [-c config_dir] [-v] class service request", myname);
117 }
118 
120 
121 int main(int argc, char **argv)
122 {
123  char *class;
124  char *service;
125  char *request;
126  int fd;
127  struct stat st;
128  char *slash;
129  int c;
130  ARGV *import_env;
131 
132  /*
133  * Fingerprint executables and core dumps.
134  */
136 
137  /*
138  * To minimize confusion, make sure that the standard file descriptors
139  * are open before opening anything else. XXX Work around for 44BSD where
140  * fstat can return EBADF on an open file descriptor.
141  */
142  for (fd = 0; fd < 3; fd++)
143  if (fstat(fd, &st) == -1
144  && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
145  msg_fatal("open /dev/null: %m");
146 
147  /*
148  * Process environment options as early as we can.
149  */
151  msg_verbose = 1;
152 
153  /*
154  * Initialize. Set up logging, read the global configuration file and
155  * extract configuration information.
156  */
157  if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
158  argv[0] = slash + 1;
159  msg_vstream_init(argv[0], VSTREAM_ERR);
161 
162  /*
163  * Parse JCL.
164  */
165  while ((c = GETOPT(argc, argv, "c:v")) > 0) {
166  switch (c) {
167  default:
168  usage(argv[0]);
169  case 'c':
170  if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
171  msg_fatal("out of memory");
172  break;
173  case 'v':
174  msg_verbose++;
175  break;
176  }
177  }
178  if (argc != optind + 3)
179  usage(argv[0]);
180  class = argv[optind];
181  service = argv[optind + 1];
182  request = argv[optind + 2];
183 
184  /*
185  * Finish initializations.
186  */
187  mail_conf_read();
188  /* Enforce consistent operation of different Postfix parts. */
190  update_env(import_env->argv);
191  argv_free(import_env);
192  if (chdir(var_queue_dir))
193  msg_fatal("chdir %s: %m", var_queue_dir);
194 
195  /*
196  * Kick the service.
197  */
198  if (mail_trigger(class, service, request, strlen(request)) < 0) {
199  msg_warn("Cannot contact class %s service %s - perhaps the mail system is down",
200  class, service);
201  exit(1);
202  }
203 
204  /*
205  * Problem: With triggers over full duplex (i.e. non-FIFO) channels, we
206  * must avoid closing the channel before the server has received the
207  * request. Otherwise some hostile kernel may throw away the request.
208  *
209  * Solution: The trigger routine registers a read event handler that runs
210  * when the server closes the channel. The event_drain() routine waits
211  * for the event handler to run, but gives up when it takes too long.
212  */
213  else {
215  exit(0);
216  }
217 }
int msg_verbose
Definition: msg.c:177
char * var_procname
Definition: mail_params.c:252
char * mystrdup(const char *str)
Definition: mymalloc.c:225
char * var_import_environ
Definition: mail_params.c:296
void set_mail_conf_str(const char *, const char *)
ARGV * argv_free(ARGV *argvp)
Definition: argv.c:136
#define NORETURN
Definition: sys_defs.h:1583
Definition: argv.h:17
#define VAR_IMPORT_ENVIRON
Definition: mail_params.h:2506
#define stat(p, s)
Definition: warn_stat.h:18
#define VAR_PROCNAME
Definition: mail_params.h:2435
char ** argv
Definition: argv.h:20
int mail_trigger(const char *, const char *, const char *, ssize_t)
Definition: mail_trigger.c:72
#define CONF_ENV_VERB
Definition: mail_conf.h:23
void mail_conf_read(void)
Definition: mail_conf.c:178
#define CONF_ENV_PATH
Definition: mail_conf.h:22
ARGV * mail_parm_split(const char *name, const char *value)
int var_event_drain
Definition: mail_params.c:249
char * safe_getenv(const char *)
Definition: safe_getenv.c:38
void msg_warn(const char *fmt,...)
Definition: msg.c:215
#define MAIL_VERSION_STAMP_ALLOCATE
Definition: mail_version.h:67
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
void update_env(char **preserve_list)
Definition: clean_env.c:102
#define GETOPT(argc, argv, str)
Definition: sys_defs.h:1313
MAIL_VERSION_STAMP_DECLARE
Definition: postkick.c:119
char * var_queue_dir
Definition: mail_params.c:246
void msg_vstream_init(const char *name, VSTREAM *vp)
Definition: msg_vstream.c:77
void event_drain(int time_limit)
Definition: events.c:657
int main(int argc, char **argv)
Definition: postkick.c:121
#define VSTREAM_ERR
Definition: vstream.h:68
#define fstat(f, s)
Definition: warn_stat.h:20