2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/amq_svc.c
42 #endif /* HAVE_CONFIG_H */
47 typedef char *(*amqsvcproc_t)(voidp, struct svc_req *);
49 #if defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP)
50 # ifdef NEED_LIBWRAP_SEVERITY_VARIABLES
52 * Some systems that define libwrap already define these two variables
53 * in libwrap, while others don't: so I need to know precisely iff
54 * to define these two severity variables.
56 int allow_severity=0, deny_severity=0, rfc931_timeout=0;
57 # endif /* NEED_LIBWRAP_SEVERITY_VARIABLES */
60 * check if remote amq is authorized to access this amd.
61 * Returns: 1=allowed, 0=denied.
64 amqsvc_is_client_allowed(const struct sockaddr_in *addr)
66 struct request_info req;
68 request_init(&req, RQ_DAEMON, AMD_SERVICE_NAME, RQ_CLIENT_SIN, addr, 0);
71 if (hosts_access(&req))
76 #endif /* defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) */
80 * Prepare the parent and child:
82 * 2) Set signal masks.
83 * 3) Fork by calling background() so that NumChildren is updated.
86 amq_fork(opaque_t argp)
90 #else /* not HAVE_SIGACTION */
92 #endif /* not HAVE_SIGACTION */
96 mp = find_ap(*(char **) argp);
102 if (pipe(mp->am_fd) == -1) {
108 #ifdef HAVE_SIGACTION
109 sigemptyset(&new); /* initialize signal set we wish to block */
110 sigaddset(&new, SIGHUP);
111 sigaddset(&new, SIGINT);
112 sigaddset(&new, SIGQUIT);
113 sigaddset(&new, SIGCHLD);
114 sigprocmask(SIG_BLOCK, &new, &mask);
115 #else /* not HAVE_SIGACTION */
121 mask = sigblock(mask);
122 #endif /* not HAVE_SIGACTION */
124 switch ((pid = background())) {
126 dlog("amq_fork failed");
130 close(mp->am_fd[1]); /* close output end of pipe */
134 default: /* parent */
135 close(mp->am_fd[0]); /* close input end of pipe */
138 #ifdef HAVE_SIGACTION
139 sigprocmask(SIG_SETMASK, &mask, NULL);
140 #else /* not HAVE_SIGACTION */
142 #endif /* not HAVE_SIGACTION */
149 amq_program_1(struct svc_req *rqstp, SVCXPRT *transp)
152 amq_string amqproc_mnttree_1_arg;
153 amq_string amqproc_umnt_1_arg;
154 amq_setopt amqproc_setopt_1_arg;
157 xdrproc_t xdr_argument, xdr_result;
163 #if defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP)
164 if (gopt.flags & CFM_USE_TCPWRAPPERS) {
165 struct sockaddr_in *remote_addr = svc_getcaller(rqstp->rq_xprt);
166 char *remote_hostname = inet_ntoa(remote_addr->sin_addr);
168 if (!amqsvc_is_client_allowed(remote_addr)) {
169 plog(XLOG_WARNING, "Amd denied remote amq service to %s", remote_hostname);
170 svcerr_auth(transp, AUTH_FAILED);
173 dlog("Amd allowed remote amq service to %s", remote_hostname);
176 #endif /* defined(HAVE_TCPD_H) && defined(HAVE_LIBWRAP) */
182 switch (rqstp->rq_proc) {
185 xdr_argument = (xdrproc_t) xdr_void;
186 xdr_result = (xdrproc_t) xdr_void;
187 local = (amqsvcproc_t) amqproc_null_1_svc;
190 case AMQPROC_MNTTREE:
191 xdr_argument = (xdrproc_t) xdr_amq_string;
192 xdr_result = (xdrproc_t) xdr_amq_mount_tree_p;
193 local = (amqsvcproc_t) amqproc_mnttree_1_svc;
197 xdr_argument = (xdrproc_t) xdr_amq_string;
198 xdr_result = (xdrproc_t) xdr_void;
199 local = (amqsvcproc_t) amqproc_umnt_1_svc;
203 xdr_argument = (xdrproc_t) xdr_void;
204 xdr_result = (xdrproc_t) xdr_amq_mount_stats;
205 local = (amqsvcproc_t) amqproc_stats_1_svc;
209 xdr_argument = (xdrproc_t) xdr_void;
210 xdr_result = (xdrproc_t) xdr_amq_mount_tree_list;
211 local = (amqsvcproc_t) amqproc_export_1_svc;
215 xdr_argument = (xdrproc_t) xdr_amq_setopt;
216 xdr_result = (xdrproc_t) xdr_int;
217 local = (amqsvcproc_t) amqproc_setopt_1_svc;
220 case AMQPROC_GETMNTFS:
221 xdr_argument = (xdrproc_t) xdr_void;
222 xdr_result = (xdrproc_t) xdr_amq_mount_info_qelem;
223 local = (amqsvcproc_t) amqproc_getmntfs_1_svc;
226 case AMQPROC_GETVERS:
227 xdr_argument = (xdrproc_t) xdr_void;
228 xdr_result = (xdrproc_t) xdr_amq_string;
229 local = (amqsvcproc_t) amqproc_getvers_1_svc;
233 xdr_argument = (xdrproc_t) xdr_void;
234 xdr_result = (xdrproc_t) xdr_int;
235 local = (amqsvcproc_t) amqproc_getpid_1_svc;
239 xdr_argument = (xdrproc_t) xdr_amq_string;
240 xdr_result = (xdrproc_t) xdr_amq_string;
241 local = (amqsvcproc_t) amqproc_pawd_1_svc;
244 case AMQPROC_SYNC_UMNT:
245 xdr_argument = (xdrproc_t) xdr_amq_string;
246 xdr_result = (xdrproc_t) xdr_amq_sync_umnt;
247 parent = (amqsvcproc_t) amqproc_sync_umnt_1_svc_parent;
248 child = (amqsvcproc_t) amqproc_sync_umnt_1_svc_child;
249 /* used if fork fails */
250 local = (amqsvcproc_t) amqproc_sync_umnt_1_svc_async;
253 case AMQPROC_GETMAPINFO:
254 xdr_argument = (xdrproc_t) xdr_void;
255 xdr_result = (xdrproc_t) xdr_amq_map_info_qelem;
256 local = (amqsvcproc_t) amqproc_getmapinfo_1_svc;
260 svcerr_noproc(transp);
264 memset((char *) &argument, 0, sizeof(argument));
265 if (!svc_getargs(transp,
266 (XDRPROC_T_TYPE) xdr_argument,
267 (SVC_IN_ARG_TYPE) & argument)) {
268 svcerr_decode(transp);
276 switch ((pid = amq_fork(&argument))) {
281 result = (*child) (&argument, rqstp);
285 default: /* parent */
286 result = (*parent) (&argument, rqstp);
293 result = (*local) (&argument, rqstp);
295 if (result != NULL && !svc_sendreply(transp,
296 (XDRPROC_T_TYPE) xdr_result,
298 svcerr_systemerr(transp);
301 if (!svc_freeargs(transp,
302 (XDRPROC_T_TYPE) xdr_argument,
303 (SVC_IN_ARG_TYPE) & argument)) {
304 plog(XLOG_FATAL, "unable to free rpc arguments in amqprog_1");
309 exit(0); /* the child is done! */