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>
47 #include "mons_handlers.h"
49 static void mons_sweep_procs(MONS_INSTANCE *mons);
51 static int mons_destructor(void *object)
53 MONS_INSTANCE *mons = talloc_get_type_abort(object, MONS_INSTANCE);
55 g_ptr_array_unref(mons->handlers);
58 g_array_unref(mons->pids);
64 * Allocate a new MONS_INSTANCE
66 * @param mem_ctx talloc context for allocation
67 * @return new MONS_INSTANCE or null on failure
69 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
71 MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
74 mons->hostname = NULL;
78 mons->req_handler = NULL;
79 mons->auth_handler = NULL;
82 /* Before any steps that may fail, install the destructor */
83 talloc_set_destructor((void *)mons, mons_destructor);
85 mons->authorized_gss_names = tr_gss_names_new(mons);
86 if (mons->authorized_gss_names == NULL) {
91 mons->handlers = g_ptr_array_new();
92 if (mons->handlers == NULL) {
97 mons->pids = g_array_new(FALSE, FALSE, sizeof(pid_t));
98 if (mons->pids == NULL) {
107 * Callback to process a request and produce a response
109 * @param req_str JSON-encoded request
110 * @param data pointer to a MONS_INSTANCE
111 * @return pointer to the response string or null to send no response
113 static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
115 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
116 MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
118 MON_RESP *resp = NULL;
119 TR_MSG *resp_msg = NULL; /* This is the response value */
121 /* Validate inputs */
125 req = tr_msg_get_mon_req(req_msg);
127 /* this is an internal error */
128 tr_err("mons_req_cb: Received incorrect message type (was %d, expected %d)",
129 tr_msg_get_msg_type(req_msg),
131 /* TODO send an error response */
135 /* Allocate a response message */
136 resp_msg = talloc(tmp_ctx, TR_MSG);
137 if (resp_msg == NULL) {
138 /* can't return a message, just emit an error */
139 tr_crit("mons_req_cb: Error allocating response message.");
143 /* Handle the request */
144 resp = mons_handle_request(resp_msg, mons, req);
146 /* error processing the request */
147 /* TODO send back an error */
151 /* Set the response message payload */
152 tr_msg_set_mon_resp(resp_msg, resp);
154 /* Put the response message in the caller's context so it does not get freed when we exit */
155 talloc_steal(mem_ctx, resp_msg);
158 talloc_free(tmp_ctx);
163 * Create a listener for monitoring requests
165 * Accept connections with mons_accept()
167 * @param mons monitoring server instance
169 * @param auth_handler
177 int mons_get_listener(MONS_INSTANCE *mons, MONS_REQ_FUNC *req_handler, MONS_AUTH_FUNC *auth_handler, const char *hostname,
178 unsigned int port, void *cookie, int *fd_out, size_t max_fd)
184 n_fd = tr_sock_listen_all(port, fd_out, max_fd);
186 tr_err("mons_get_listener: Error opening port %d");
188 /* opening port succeeded */
189 tr_info("mons_get_listener: Opened port %d.", port);
191 /* make this socket non-blocking */
192 for (ii=0; ii<n_fd; ii++) {
193 if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
194 tr_err("mons_get_listener: Error setting O_NONBLOCK.");
195 for (ii=0; ii<n_fd; ii++) {
206 /* store the caller's request handler & cookie */
207 mons->req_handler = req_handler;
208 mons->auth_handler = auth_handler;
209 mons->hostname = hostname;
210 mons->cookie = cookie;
217 * Accept and process a connection on a port opened with mons_get_listener()
219 * @param mons monitoring interface instance
220 * @param listen FD of the connection socket
221 * @return 0 on success
223 int mons_accept(MONS_INSTANCE *mons, int listen)
228 if (0 > (conn = tr_sock_accept(listen))) {
229 tr_err("mons_accept: Error accepting connection");
233 if (0 > (pid = fork())) {
234 perror("Error on fork()");
240 tr_gss_handle_connection(conn,
241 "trustmonitor", mons->hostname, /* acceptor name */
242 mons->auth_handler, mons->cookie, /* auth callback and cookie */
243 mons_req_cb, mons /* req callback and cookie */
246 exit(0); /* exit to kill forked child process */
249 /* Only the parent process gets here */
251 g_array_append_val(mons->pids, pid);
253 /* clean up any processes that have completed */
254 mons_sweep_procs(mons);
259 void mons_sweep_procs(MONS_INSTANCE *mons)
265 /* loop backwards over the array so we can remove elements as we go */
266 for (ii=mons->pids->len; ii > 0; ii--) {
267 /* ii-1 is the current index */
268 pid = g_array_index(mons->pids, pid_t, ii-1);
269 if (waitpid(pid, &status, WNOHANG) > 0) {
270 /* the process exited */
271 tr_debug("mons_sweep_procs: monitoring process %d terminated.", pid);
273 g_array_remove_index_fast(mons->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */
274 if (WIFEXITED(status)) {
275 if (WEXITSTATUS(status) == 0)
276 tr_debug("mons_sweep_procs: monitoring process %d succeeded.", pid);
278 tr_debug("mons_sweep_procs: monitoring process %d exited with status %d.", pid, WTERMSIG(status));
279 } else if (WIFSIGNALED(status)) {
280 tr_debug("mons_sweep_procs: monitoring process %d terminated by signal %d.", pid, WTERMSIG(status));