Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / mon / mons.c
1 /*
2  * Copyright (c) 2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <talloc.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <netdb.h>
39
40 #include <tr.h>
41 #include <tr_debug.h>
42 #include <mon_internal.h>
43 #include <tr_socket.h>
44 #include <sys/wait.h>
45 #include <sys/resource.h>
46 #include <tr_gss.h>
47
48 #include "mons_handlers.h"
49
50 static void mons_sweep_procs(MONS_INSTANCE *mons);
51
52 static int mons_destructor(void *object)
53 {
54   MONS_INSTANCE *mons = talloc_get_type_abort(object, MONS_INSTANCE);
55   if (mons->handlers)
56     g_ptr_array_unref(mons->handlers);
57
58   if (mons->pids)
59     g_array_unref(mons->pids);
60
61   return 0;
62 }
63
64 /**
65  * Allocate a new MONS_INSTANCE
66  *
67  * @param mem_ctx talloc context for allocation
68  * @return new MONS_INSTANCE or null on failure
69  */
70 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
71 {
72   MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
73
74   if (mons) {
75     mons->hostname = NULL;
76     mons->mon_port = 0;
77     mons->tids = NULL;
78     mons->trps = NULL;
79     mons->req_handler = NULL;
80     mons->auth_handler = NULL;
81     mons->cookie = NULL;
82
83     /* Before any steps that may fail, install the destructor */
84     talloc_set_destructor((void *)mons, mons_destructor);
85
86     mons->authorized_gss_names = tr_gss_names_new(mons);
87     if (mons->authorized_gss_names == NULL) {
88       talloc_free(mons);
89       return NULL;
90     }
91
92     mons->handlers = g_ptr_array_new();
93     if (mons->handlers == NULL) {
94       talloc_free(mons);
95       return NULL;
96     }
97
98     mons->pids = g_array_new(FALSE, FALSE, sizeof(pid_t));
99     if (mons->pids == NULL) {
100       talloc_free(mons);
101       return NULL;
102     }
103   }
104   return mons;
105 }
106
107 /**
108  * Callback to process a request and produce a response
109  *
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
113  */
114 static TR_GSS_RC mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, TR_MSG **resp_msg, void *data)
115 {
116   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
117   MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
118   MON_REQ *req = NULL;
119   MON_RESP *resp = NULL;
120   TR_GSS_RC rc = TR_GSS_ERROR;
121
122   /* Validate inputs */
123   if (req_msg == NULL)
124     goto cleanup;
125
126   req = tr_msg_get_mon_req(req_msg);
127   if (req == NULL) {
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),
131            MON_REQUEST);
132     /* TODO send an error response */
133     goto cleanup;
134   }
135
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.");
141     goto cleanup;
142   }
143
144   /* Handle the request */
145   resp = mons_handle_request(*resp_msg, mons, req);
146   if (resp == NULL) {
147     /* error processing the request */
148     /* TODO send back an error */
149     *resp_msg = NULL; /* null this out so the caller doesn't mistake it for valid */
150     goto cleanup;
151   }
152
153   /* Set the response message payload */
154   tr_msg_set_mon_resp(*resp_msg, resp);
155
156   /* Put the response message in the caller's context so it does not get freed when we exit */
157   talloc_steal(mem_ctx, *resp_msg);
158   rc = TR_GSS_SUCCESS;
159
160 cleanup:
161   talloc_free(tmp_ctx);
162   return rc;
163 }
164
165 /**
166  * Create a listener for monitoring requests
167  *
168  * Accept connections with mons_accept()
169  *
170  * @param mons monitoring server instance
171  * @param req_handler
172  * @param auth_handler
173  * @param hostname
174  * @param port
175  * @param cookie
176  * @param fd_out
177  * @param max_fd
178  * @return
179  */
180 int mons_get_listener(MONS_INSTANCE *mons,
181                       MONS_REQ_FUNC *req_handler,
182                       MONS_AUTH_FUNC *auth_handler,
183                       const char *hostname,
184                       int port,
185                       void *cookie,
186                       int *fd_out,
187                       size_t max_fd)
188 {
189   size_t n_fd=0;
190   size_t ii=0;
191
192   mons->mon_port = port;
193   n_fd = tr_sock_listen_all(port, fd_out, max_fd);
194   if (n_fd<=0)
195     tr_err("mons_get_listener: Error opening port %d", port);
196   else {
197     /* opening port succeeded */
198     tr_info("mons_get_listener: Opened port %d.", port);
199
200     /* make this socket non-blocking */
201     for (ii=0; ii<n_fd; ii++) {
202       if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
203         tr_err("mons_get_listener: Error setting O_NONBLOCK.");
204         for (ii=0; ii<n_fd; ii++) {
205           close(fd_out[ii]);
206           fd_out[ii]=-1;
207         }
208         n_fd=0;
209         break;
210       }
211     }
212   }
213
214   if (n_fd>0) {
215     /* store the caller's request handler & cookie */
216     mons->req_handler = req_handler;
217     mons->auth_handler = auth_handler;
218     mons->hostname = hostname;
219     mons->cookie = cookie;
220   }
221
222   return (int) n_fd;
223 }
224
225 /**
226  * Process to handle an incoming monitoring request
227  *
228  * This should be run in a child process after fork(). Handles the request
229  * and terminates. Never returns to the caller.
230  *
231  * @param mons the monitoring server instance
232  * @param conn_fd file descriptor for the incoming connection
233  */
234 static void mons_handle_proc(MONS_INSTANCE *mons, int conn_fd)
235 {
236   struct rlimit rlim; /* for disabling core dump */
237
238   switch(tr_gss_handle_connection(conn_fd,
239                                   "trustmonitor", mons->hostname, /* acceptor name */
240                                   mons->auth_handler, mons->cookie, /* auth callback and cookie */
241                                   mons_req_cb, mons /* req callback and cookie */
242   )) {
243     case TR_GSS_SUCCESS:
244       /* do nothing */
245       break;
246
247     case TR_GSS_ERROR:
248       tr_debug("mons_accept: Error returned by tr_gss_handle_connection()");
249       break;
250
251     default:
252       tr_err("mons_accept: Unexpected value returned by tr_gss_handle_connection()");
253       break;
254   }
255   close(conn_fd);
256
257   /* This ought to be an exit(0), but log4shib does not play well with fork() due to
258    * threading issues. To ensure we do not get stuck in the exit handler, we will
259    * abort. First disable core dump for this subprocess (the main process will still
260    * dump core if the environment allows). */
261   rlim.rlim_cur = 0; /* max core size of 0 */
262   rlim.rlim_max = 0; /* prevent the core size limit from being raised later */
263   setrlimit(RLIMIT_CORE, &rlim);
264   abort(); /* exit hard */
265 }
266
267 /**
268  * Accept and process a connection on a port opened with mons_get_listener()
269  *
270  * @param mons monitoring interface instance
271  * @param listen FD of the connection socket
272  * @return 0 on success
273  */
274 int mons_accept(MONS_INSTANCE *mons, int listen)
275 {
276   int conn=-1;
277   int pid=-1;
278
279   if (0 > (conn = tr_sock_accept(listen))) {
280     tr_debug("mons_accept: Error accepting connection");
281     return 1;
282   }
283
284   if (0 > (pid = fork())) {
285     perror("Error on fork()");
286     return 1;
287   }
288
289   if (pid == 0) {
290     /* Only the child process gets here */
291     close(listen); /* this belongs to the parent */
292     mons_handle_proc(mons, conn); /* never returns */
293   }
294
295   /* Only the parent process gets here */
296   close(conn); /* this belongs to the child */
297   g_array_append_val(mons->pids, pid);
298
299   /* clean up any processes that have completed */
300   mons_sweep_procs(mons);
301
302   return 0;
303 }
304
305 void mons_sweep_procs(MONS_INSTANCE *mons)
306 {
307   guint ii;
308   pid_t pid;
309   int status;
310
311   /* loop backwards over the array so we can remove elements as we go */
312   for (ii=mons->pids->len; ii > 0; ii--) {
313     /* ii-1 is the current index */
314     pid = g_array_index(mons->pids, pid_t, ii-1);
315     if (waitpid(pid, &status, WNOHANG) > 0) {
316       /* the process exited */
317       tr_debug("mons_sweep_procs: monitoring process %d terminated.", pid);
318
319       g_array_remove_index_fast(mons->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */
320       if (WIFEXITED(status)) {
321         if (WEXITSTATUS(status) == 0)
322           tr_debug("mons_sweep_procs: monitoring process %d succeeded.", pid);
323         else
324           tr_debug("mons_sweep_procs: monitoring process %d exited with status %d.", pid, WTERMSIG(status));
325       } else if (WIFSIGNALED(status)) {
326         tr_debug("mons_sweep_procs: monitoring process %d terminated by signal %d.", pid, WTERMSIG(status));
327       }
328     }
329   }
330 }