Track TID processes and add TID req counts for success/error/pending
[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 int mons_destructor(void *object)
50 {
51   MONS_INSTANCE *mons = talloc_get_type_abort(object, MONS_INSTANCE);
52   if (mons->handlers) {
53     g_ptr_array_unref(mons->handlers);
54   }
55   return 0;
56 }
57
58 /**
59  * Allocate a new MONS_INSTANCE
60  *
61  * @param mem_ctx talloc context for allocation
62  * @return new MONS_INSTANCE or null on failure
63  */
64 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
65 {
66   MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
67
68   if (mons) {
69     mons->hostname = NULL;
70     mons->port = 0;
71     mons->tids = NULL;
72     mons->trps = NULL;
73     mons->req_handler = NULL;
74     mons->auth_handler = NULL;
75     mons->cookie = NULL;
76
77     /* Before any steps that may fail, install the destructor */
78     talloc_set_destructor((void *)mons, mons_destructor);
79
80     mons->authorized_gss_names = tr_gss_names_new(mons);
81     if (mons->authorized_gss_names == NULL) {
82       talloc_free(mons);
83       return NULL;
84     }
85
86     mons->handlers = g_ptr_array_new();
87     if (mons->handlers == NULL) {
88       talloc_free(mons);
89       return NULL;
90     }
91   }
92   return mons;
93 }
94
95 /**
96  * Callback to process a request and produce a response
97  *
98  * @param req_str JSON-encoded request
99  * @param data pointer to a MONS_INSTANCE
100  * @return pointer to the response string or null to send no response
101  */
102 static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
103 {
104   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
105   MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
106   MON_REQ *req = NULL;
107   MON_RESP *resp = NULL;
108   TR_MSG *resp_msg = NULL; /* This is the response value */
109
110   /* Validate inputs */
111   if (req_msg == NULL)
112     goto cleanup;
113
114   req = tr_msg_get_mon_req(req_msg);
115   if (req == NULL) {
116     /* this is an internal error */
117     tr_err("mons_req_cb: Received incorrect message type (was %d, expected %d)",
118            tr_msg_get_msg_type(req_msg),
119            MON_REQUEST);
120     /* TODO send an error response */
121     goto cleanup;
122   }
123
124   /* Allocate a response message */
125   resp_msg = talloc(tmp_ctx, TR_MSG);
126   if (resp_msg == NULL) {
127     /* can't return a message, just emit an error */
128     tr_crit("mons_req_cb: Error allocating response message.");
129     goto cleanup;
130   }
131
132   /* Handle the request */
133   resp = mons_handle_request(resp_msg, mons, req);
134   if (resp == NULL) {
135     /* error processing the request */
136     /* TODO send back an error */
137     goto cleanup;
138   }
139
140   /* Set the response message payload */
141   tr_msg_set_mon_resp(resp_msg, resp);
142
143   /* Put the response message in the caller's context so it does not get freed when we exit */
144   talloc_steal(mem_ctx, resp_msg);
145
146 cleanup:
147   talloc_free(tmp_ctx);
148   return resp_msg;
149 }
150
151 /**
152  * Create a listener for monitoring requests
153  *
154  * Accept connections with mons_accept()
155  *
156  * @param mons monitoring server instance
157  * @param req_handler
158  * @param auth_handler
159  * @param hostname
160  * @param port
161  * @param cookie
162  * @param fd_out
163  * @param max_fd
164  * @return
165  */
166 int mons_get_listener(MONS_INSTANCE *mons, MONS_REQ_FUNC *req_handler, MONS_AUTH_FUNC *auth_handler, const char *hostname,
167                       unsigned int port, void *cookie, int *fd_out, size_t max_fd)
168 {
169   size_t n_fd=0;
170   size_t ii=0;
171
172   mons->port = port;
173   n_fd = tr_sock_listen_all(port, fd_out, max_fd);
174   if (n_fd<=0)
175     tr_err("mons_get_listener: Error opening port %d");
176   else {
177     /* opening port succeeded */
178     tr_info("mons_get_listener: Opened port %d.", port);
179
180     /* make this socket non-blocking */
181     for (ii=0; ii<n_fd; ii++) {
182       if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
183         tr_err("mons_get_listener: Error setting O_NONBLOCK.");
184         for (ii=0; ii<n_fd; ii++) {
185           close(fd_out[ii]);
186           fd_out[ii]=-1;
187         }
188         n_fd=0;
189         break;
190       }
191     }
192   }
193
194   if (n_fd>0) {
195     /* store the caller's request handler & cookie */
196     mons->req_handler = req_handler;
197     mons->auth_handler = auth_handler;
198     mons->hostname = hostname;
199     mons->cookie = cookie;
200   }
201
202   return (int) n_fd;
203 }
204
205 /**
206  * Accept and process a connection on a port opened with mons_get_listener()
207  *
208  * @param mons monitoring interface instance
209  * @param listen FD of the connection socket
210  * @return 0 on success
211  */
212 int mons_accept(MONS_INSTANCE *mons, int listen)
213 {
214   int conn=-1;
215   int pid=-1;
216
217   if (0 > (conn = accept(listen, NULL, NULL))) {
218     perror("Error from monitoring interface accept()");
219     return 1;
220   }
221
222   if (0 > (pid = fork())) {
223     perror("Error on fork()");
224     return 1;
225   }
226
227   if (pid == 0) {
228     close(listen);
229     tr_gss_handle_connection(conn,
230                              "trustmonitor", mons->hostname, /* acceptor name */
231                              mons->auth_handler, mons->cookie, /* auth callback and cookie */
232                              mons_req_cb, mons /* req callback and cookie */
233     );
234     close(conn);
235     exit(0); /* exit to kill forked child process */
236   } else {
237     close(conn);
238   }
239
240   /* clean up any processes that have completed */
241   //while (waitpid(-1, 0, WNOHANG) > 0); TODO: only clean up our own pids
242
243   return 0;
244 }