d79cbcbdca707482271e30b6f626ed938362a1df
[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 /**
48  * Allocate a new MONS_INSTANCE
49  *
50  * @param mem_ctx talloc context for allocation
51  * @return new MONS_INSTANCE or null on failure
52  */
53 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
54 {
55   MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
56
57   if (mons) {
58     mons->hostname = NULL;
59     mons->port = 0;
60     mons->tids = NULL;
61     mons->trps = NULL;
62     mons->req_handler = NULL;
63     mons->auth_handler = NULL;
64     mons->cookie = NULL;
65     mons->authorized_gss_names = tr_gss_names_new(mons);
66     if (mons->authorized_gss_names == NULL) {
67       talloc_free(mons);
68       mons = NULL;
69     }
70   }
71   return mons;
72 }
73
74 /**
75  * Callback to process a request and produce a response
76  *
77  * @param req_str JSON-encoded request
78  * @param data pointer to a MONS_INSTANCE
79  * @return pointer to the response string or null to send no response
80  */
81 static char *mons_req_cb(TALLOC_CTX *mem_ctx, const char *req_str, void *data)
82 {
83   return "This is a response.";
84 }
85
86 /**
87  * Create a listener for monitoring requests
88  *
89  * Accept connections with mons_accept()
90  *
91  * @param mons monitoring server instance
92  * @param req_handler
93  * @param auth_handler
94  * @param hostname
95  * @param port
96  * @param cookie
97  * @param fd_out
98  * @param max_fd
99  * @return
100  */
101 int mons_get_listener(MONS_INSTANCE *mons, MONS_REQ_FUNC *req_handler, MONS_AUTH_FUNC *auth_handler, const char *hostname,
102                       unsigned int port, void *cookie, int *fd_out, size_t max_fd)
103 {
104   size_t n_fd=0;
105   size_t ii=0;
106
107   mons->port = port;
108   n_fd = tr_sock_listen_all(port, fd_out, max_fd);
109   if (n_fd<=0)
110     tr_err("mons_get_listener: Error opening port %d");
111   else {
112     /* opening port succeeded */
113     tr_info("mons_get_listener: Opened port %d.", port);
114
115     /* make this socket non-blocking */
116     for (ii=0; ii<n_fd; ii++) {
117       if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
118         tr_err("mons_get_listener: Error setting O_NONBLOCK.");
119         for (ii=0; ii<n_fd; ii++) {
120           close(fd_out[ii]);
121           fd_out[ii]=-1;
122         }
123         n_fd=0;
124         break;
125       }
126     }
127   }
128
129   if (n_fd>0) {
130     /* store the caller's request handler & cookie */
131     mons->req_handler = req_handler;
132     mons->auth_handler = auth_handler;
133     mons->hostname = hostname;
134     mons->cookie = cookie;
135   }
136
137   return (int) n_fd;
138 }
139
140 /**
141  * Accept and process a connection on a port opened with mons_get_listener()
142  *
143  * @param mons monitoring interface instance
144  * @param listen FD of the connection socket
145  * @return 0 on success
146  */
147 int mons_accept(MONS_INSTANCE *mons, int listen)
148 {
149   int conn=-1;
150   int pid=-1;
151
152   if (0 > (conn = accept(listen, NULL, NULL))) {
153     perror("Error from monitoring interface accept()");
154     return 1;
155   }
156
157   if (0 > (pid = fork())) {
158     perror("Error on fork()");
159     return 1;
160   }
161
162   if (pid == 0) {
163     close(listen);
164     tr_gss_handle_connection(conn,
165                              "trustmonitor", mons->hostname, /* acceptor name */
166                              mons->auth_handler, mons->cookie, /* auth callback and cookie */
167                              mons_req_cb, mons /* req callback and cookie */
168     );
169     close(conn);
170     exit(0); /* exit to kill forked child process */
171   } else {
172     close(conn);
173   }
174
175   /* clean up any processes that have completed */
176   while (waitpid(-1, 0, WNOHANG) > 0);
177
178   return 0;
179 }