Treat TID req as error if a response is not sent
[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 <tr_gss.h>
46
47 #include "mons_handlers.h"
48
49 static void mons_sweep_procs(MONS_INSTANCE *mons);
50
51 static int mons_destructor(void *object)
52 {
53   MONS_INSTANCE *mons = talloc_get_type_abort(object, MONS_INSTANCE);
54   if (mons->handlers)
55     g_ptr_array_unref(mons->handlers);
56
57   if (mons->pids)
58     g_array_unref(mons->pids);
59
60   return 0;
61 }
62
63 /**
64  * Allocate a new MONS_INSTANCE
65  *
66  * @param mem_ctx talloc context for allocation
67  * @return new MONS_INSTANCE or null on failure
68  */
69 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
70 {
71   MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
72
73   if (mons) {
74     mons->hostname = NULL;
75     mons->port = 0;
76     mons->tids = NULL;
77     mons->trps = NULL;
78     mons->req_handler = NULL;
79     mons->auth_handler = NULL;
80     mons->cookie = NULL;
81
82     /* Before any steps that may fail, install the destructor */
83     talloc_set_destructor((void *)mons, mons_destructor);
84
85     mons->authorized_gss_names = tr_gss_names_new(mons);
86     if (mons->authorized_gss_names == NULL) {
87       talloc_free(mons);
88       return NULL;
89     }
90
91     mons->handlers = g_ptr_array_new();
92     if (mons->handlers == NULL) {
93       talloc_free(mons);
94       return NULL;
95     }
96
97     mons->pids = g_array_new(FALSE, FALSE, sizeof(pid_t));
98     if (mons->pids == NULL) {
99       talloc_free(mons);
100       return NULL;
101     }
102   }
103   return mons;
104 }
105
106 /**
107  * Callback to process a request and produce a response
108  *
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
112  */
113 static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
114 {
115   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
116   MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
117   MON_REQ *req = NULL;
118   MON_RESP *resp = NULL;
119   TR_MSG *resp_msg = NULL; /* This is the response value */
120
121   /* Validate inputs */
122   if (req_msg == NULL)
123     goto cleanup;
124
125   req = tr_msg_get_mon_req(req_msg);
126   if (req == NULL) {
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),
130            MON_REQUEST);
131     /* TODO send an error response */
132     goto cleanup;
133   }
134
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.");
140     goto cleanup;
141   }
142
143   /* Handle the request */
144   resp = mons_handle_request(resp_msg, mons, req);
145   if (resp == NULL) {
146     /* error processing the request */
147     /* TODO send back an error */
148     goto cleanup;
149   }
150
151   /* Set the response message payload */
152   tr_msg_set_mon_resp(resp_msg, resp);
153
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);
156
157 cleanup:
158   talloc_free(tmp_ctx);
159   return resp_msg;
160 }
161
162 /**
163  * Create a listener for monitoring requests
164  *
165  * Accept connections with mons_accept()
166  *
167  * @param mons monitoring server instance
168  * @param req_handler
169  * @param auth_handler
170  * @param hostname
171  * @param port
172  * @param cookie
173  * @param fd_out
174  * @param max_fd
175  * @return
176  */
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)
179 {
180   size_t n_fd=0;
181   size_t ii=0;
182
183   mons->port = port;
184   n_fd = tr_sock_listen_all(port, fd_out, max_fd);
185   if (n_fd<=0)
186     tr_err("mons_get_listener: Error opening port %d");
187   else {
188     /* opening port succeeded */
189     tr_info("mons_get_listener: Opened port %d.", port);
190
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++) {
196           close(fd_out[ii]);
197           fd_out[ii]=-1;
198         }
199         n_fd=0;
200         break;
201       }
202     }
203   }
204
205   if (n_fd>0) {
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;
211   }
212
213   return (int) n_fd;
214 }
215
216 /**
217  * Accept and process a connection on a port opened with mons_get_listener()
218  *
219  * @param mons monitoring interface instance
220  * @param listen FD of the connection socket
221  * @return 0 on success
222  */
223 int mons_accept(MONS_INSTANCE *mons, int listen)
224 {
225   int conn=-1;
226   int pid=-1;
227
228   if (0 > (conn = tr_sock_accept(listen))) {
229     tr_err("mons_accept: Error accepting connection");
230     return 1;
231   }
232
233   if (0 > (pid = fork())) {
234     perror("Error on fork()");
235     return 1;
236   }
237
238   if (pid == 0) {
239     close(listen);
240     switch(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 */
244     )) {
245       case TR_GSS_SUCCESS:
246         /* do nothing */
247         break;
248
249       case TR_GSS_ERROR:
250         tr_debug("mons_accept: Error returned by tr_gss_handle_connection()");
251         break;
252
253       default:
254         tr_err("mons_accept: Unexpected value returned by tr_gss_handle_connection()");
255         break;
256     }
257     close(conn);
258     exit(0); /* exit to kill forked child process */
259   }
260
261   /* Only the parent process gets here */
262   close(conn);
263   g_array_append_val(mons->pids, pid);
264
265   /* clean up any processes that have completed */
266   mons_sweep_procs(mons);
267
268   return 0;
269 }
270
271 void mons_sweep_procs(MONS_INSTANCE *mons)
272 {
273   guint ii;
274   pid_t pid;
275   int status;
276
277   /* loop backwards over the array so we can remove elements as we go */
278   for (ii=mons->pids->len; ii > 0; ii--) {
279     /* ii-1 is the current index */
280     pid = g_array_index(mons->pids, pid_t, ii-1);
281     if (waitpid(pid, &status, WNOHANG) > 0) {
282       /* the process exited */
283       tr_debug("mons_sweep_procs: monitoring process %d terminated.", pid);
284
285       g_array_remove_index_fast(mons->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */
286       if (WIFEXITED(status)) {
287         if (WEXITSTATUS(status) == 0)
288           tr_debug("mons_sweep_procs: monitoring process %d succeeded.", pid);
289         else
290           tr_debug("mons_sweep_procs: monitoring process %d exited with status %d.", pid, WTERMSIG(status));
291       } else if (WIFSIGNALED(status)) {
292         tr_debug("mons_sweep_procs: monitoring process %d terminated by signal %d.", pid, WTERMSIG(status));
293       }
294     }
295   }
296 }