2 * Copyright (c) 2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <mon_internal.h>
43 #include <tr_socket.h>
45 #include <sys/resource.h>
48 #include "mons_handlers.h"
50 static void mons_sweep_procs(MONS_INSTANCE *mons);
52 static int mons_destructor(void *object)
54 MONS_INSTANCE *mons = talloc_get_type_abort(object, MONS_INSTANCE);
56 g_ptr_array_unref(mons->handlers);
59 g_array_unref(mons->pids);
65 * Allocate a new MONS_INSTANCE
67 * @param mem_ctx talloc context for allocation
68 * @return new MONS_INSTANCE or null on failure
70 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
72 MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
75 mons->hostname = NULL;
79 mons->req_handler = NULL;
80 mons->auth_handler = NULL;
83 /* Before any steps that may fail, install the destructor */
84 talloc_set_destructor((void *)mons, mons_destructor);
86 mons->authorized_gss_names = tr_gss_names_new(mons);
87 if (mons->authorized_gss_names == NULL) {
92 mons->handlers = g_ptr_array_new();
93 if (mons->handlers == NULL) {
98 mons->pids = g_array_new(FALSE, FALSE, sizeof(pid_t));
99 if (mons->pids == NULL) {
108 * Callback to process a request and produce a response
110 * @param req_str JSON-encoded request
111 * @param data pointer to a MONS_INSTANCE
112 * @return pointer to the response string or null to send no response
114 static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
116 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
117 MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
119 MON_RESP *resp = NULL;
120 TR_MSG *resp_msg = NULL; /* This is the response value */
122 /* Validate inputs */
126 req = tr_msg_get_mon_req(req_msg);
128 /* this is an internal error */
129 tr_err("mons_req_cb: Received incorrect message type (was %d, expected %d)",
130 tr_msg_get_msg_type(req_msg),
132 /* TODO send an error response */
136 /* Allocate a response message */
137 resp_msg = talloc(tmp_ctx, TR_MSG);
138 if (resp_msg == NULL) {
139 /* can't return a message, just emit an error */
140 tr_crit("mons_req_cb: Error allocating response message.");
144 /* Handle the request */
145 resp = mons_handle_request(resp_msg, mons, req);
147 /* error processing the request */
148 /* TODO send back an error */
152 /* Set the response message payload */
153 tr_msg_set_mon_resp(resp_msg, resp);
155 /* Put the response message in the caller's context so it does not get freed when we exit */
156 talloc_steal(mem_ctx, resp_msg);
159 talloc_free(tmp_ctx);
164 * Create a listener for monitoring requests
166 * Accept connections with mons_accept()
168 * @param mons monitoring server instance
170 * @param auth_handler
178 int mons_get_listener(MONS_INSTANCE *mons, MONS_REQ_FUNC *req_handler, MONS_AUTH_FUNC *auth_handler, const char *hostname,
179 unsigned int port, void *cookie, int *fd_out, size_t max_fd)
185 n_fd = tr_sock_listen_all(port, fd_out, max_fd);
187 tr_err("mons_get_listener: Error opening port %d");
189 /* opening port succeeded */
190 tr_info("mons_get_listener: Opened port %d.", port);
192 /* make this socket non-blocking */
193 for (ii=0; ii<n_fd; ii++) {
194 if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
195 tr_err("mons_get_listener: Error setting O_NONBLOCK.");
196 for (ii=0; ii<n_fd; ii++) {
207 /* store the caller's request handler & cookie */
208 mons->req_handler = req_handler;
209 mons->auth_handler = auth_handler;
210 mons->hostname = hostname;
211 mons->cookie = cookie;
218 * Process to handle an incoming monitoring request
220 * This should be run in a child process after fork(). Handles the request
221 * and terminates. Never returns to the caller.
223 * @param mons the monitoring server instance
224 * @param conn_fd file descriptor for the incoming connection
226 static void mons_handle_proc(MONS_INSTANCE *mons, int conn_fd)
228 struct rlimit rlim; /* for disabling core dump */
230 switch(tr_gss_handle_connection(conn_fd,
231 "trustmonitor", mons->hostname, /* acceptor name */
232 mons->auth_handler, mons->cookie, /* auth callback and cookie */
233 mons_req_cb, mons /* req callback and cookie */
240 tr_debug("mons_accept: Error returned by tr_gss_handle_connection()");
244 tr_err("mons_accept: Unexpected value returned by tr_gss_handle_connection()");
249 /* This ought to be an exit(0), but log4shib does not play well with fork() due to
250 * threading issues. To ensure we do not get stuck in the exit handler, we will
251 * abort. First disable core dump for this subprocess (the main process will still
252 * dump core if the environment allows). */
253 rlim.rlim_cur = 0; /* max core size of 0 */
254 rlim.rlim_max = 0; /* prevent the core size limit from being raised later */
255 setrlimit(RLIMIT_CORE, &rlim);
256 abort(); /* exit hard */
260 * Accept and process a connection on a port opened with mons_get_listener()
262 * @param mons monitoring interface instance
263 * @param listen FD of the connection socket
264 * @return 0 on success
266 int mons_accept(MONS_INSTANCE *mons, int listen)
271 if (0 > (conn = tr_sock_accept(listen))) {
272 tr_err("mons_accept: Error accepting connection");
276 if (0 > (pid = fork())) {
277 perror("Error on fork()");
282 /* Only the child process gets here */
283 close(listen); /* this belongs to the parent */
284 mons_handle_proc(mons, conn); /* never returns */
287 /* Only the parent process gets here */
288 close(conn); /* this belongs to the child */
289 g_array_append_val(mons->pids, pid);
291 /* clean up any processes that have completed */
292 mons_sweep_procs(mons);
297 void mons_sweep_procs(MONS_INSTANCE *mons)
303 /* loop backwards over the array so we can remove elements as we go */
304 for (ii=mons->pids->len; ii > 0; ii--) {
305 /* ii-1 is the current index */
306 pid = g_array_index(mons->pids, pid_t, ii-1);
307 if (waitpid(pid, &status, WNOHANG) > 0) {
308 /* the process exited */
309 tr_debug("mons_sweep_procs: monitoring process %d terminated.", pid);
311 g_array_remove_index_fast(mons->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */
312 if (WIFEXITED(status)) {
313 if (WEXITSTATUS(status) == 0)
314 tr_debug("mons_sweep_procs: monitoring process %d succeeded.", pid);
316 tr_debug("mons_sweep_procs: monitoring process %d exited with status %d.", pid, WTERMSIG(status));
317 } else if (WIFSIGNALED(status)) {
318 tr_debug("mons_sweep_procs: monitoring process %d terminated by signal %d.", pid, WTERMSIG(status));