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