01dab41e9b61e23e34e4e5d2b67ed9d9cc72faab
[trust_router.git] / tr / tr_mon.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
37 #include <tr_config.h>
38 #include <tr_debug.h>
39 #include <mon_internal.h>
40 #include <tr_mon.h>
41
42 /*
43  * Cookie for the event handling callback
44  */
45 struct tr_mons_event_cookie {
46   MONS_INSTANCE *mons;
47   TR_CFG_MGR *cfg_mgr;
48 };
49
50
51 /**
52  * Callback to handle a triggered event
53  *
54  * @param listener file descriptor of the socket that triggered the event
55  * @param event libevent2 event
56  * @param arg pointer to our MONS_INSTANCE
57  */
58 static void tr_mons_event_cb(int listener, short event, void *arg)
59 {
60   MONS_INSTANCE *mons = talloc_get_type_abort(arg, MONS_INSTANCE);
61
62   // check that we were not accidentally triggered
63   if (0==(event & EV_READ))
64     tr_debug("tr_mons_event_cb: unexpected event on monitoring interface socket (event=0x%X)", event);
65   else
66     mons_accept(mons, listener);
67 }
68
69
70 /**
71  * Callback to handle an incoming monitoring request
72  *
73  * @param mons monitoring interface instance
74  * @param orig_req incoming request
75  * @param resp destination for outgoing response
76  * @param cookie_in cookie from the event handling system
77  * @return 0 on success
78  */
79 static int tr_mons_req_handler(MONS_INSTANCE *mons,
80                                MON_REQ *orig_req,
81                                MON_RESP *resp,
82                                void *cookie_in)
83 {
84   return -1;
85 }
86
87 /**
88  * Callback to authorize a GSS client
89  *
90  * @param client_name ?
91  * @param gss_name GSS name of credential attempting to authorize
92  * @param cookie_in event cookie
93  * @return 0 if authorization is successful, -1 if not
94  */
95 static int tr_mons_auth_handler(gss_name_t client_name, TR_NAME *gss_name, void *cookie_in)
96 {
97   struct tr_mons_event_cookie *cookie=talloc_get_type_abort(cookie_in, struct tr_mons_event_cookie);
98   MONS_INSTANCE *mons = cookie->mons;
99   TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
100
101   if ((!client_name) || (!gss_name) || (!mons) || (!cfg_mgr)) {
102     tr_debug("tr_mons_gss_handler: Bad parameters.");
103     return -1;
104   }
105
106   /* Ensure at least one client exists using this GSS name */
107   if (! tr_gss_names_matches(mons->authorized_gss_names, gss_name)) {
108     tr_info("tr_mons_gss_handler: Unauthorized request from %.*s", gss_name->len, gss_name->buf);
109     return -1;
110   }
111
112   /* Credential was valid, authorize it */
113   tr_info("tr_mons_gss_handler: Authorized request from %.*s", gss_name->len, gss_name->buf);
114   return 0;
115 }
116
117
118 /*
119  *
120  * Get a listener for monitoring requests, returns its socket fd. Accept
121  * connections with tids_accept() */
122
123 /**
124  * Configure the monitoring service instance and set up its event handler
125  *
126  * @param base libevent2 event base
127  * @param mons MONS_INSTANCE for this monitoring interface
128  * @param cfg_mgr configuration manager instance
129  * @param mons_ev monitoring interface event instance
130  * @return 0 on success, nonzero on failure.
131  * */
132 int tr_mons_event_init(struct event_base *base,
133                        MONS_INSTANCE *mons,
134                        TR_CFG_MGR *cfg_mgr,
135                        struct tr_socket_event *mons_ev)
136 {
137   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
138   struct tr_mons_event_cookie *cookie=NULL;
139   int retval=0;
140   int ii=0;
141
142   if (mons_ev == NULL) {
143     tr_debug("tr_mon_event_init: Null mons_ev.");
144     retval=1;
145     goto cleanup;
146   }
147
148   /* Create the cookie for callbacks. We'll put it in the mons context, so it will
149    * be cleaned up when mons is freed by talloc_free. */
150   cookie=talloc(tmp_ctx, struct tr_mons_event_cookie);
151   if (cookie == NULL) {
152     tr_debug("tr_mons_event_init: Unable to allocate cookie.");
153     retval=1;
154     goto cleanup;
155   }
156   cookie->mons=mons;
157   cookie->cfg_mgr=cfg_mgr;
158   talloc_steal(mons, cookie);
159
160   /* get a monitoring interface listener */
161   mons_ev->n_sock_fd = mons_get_listener(mons,
162                                          tr_mons_req_handler,
163                                          tr_mons_auth_handler,
164                                          cfg_mgr->active->internal->monitoring_port,
165                                          (void *)cookie,
166                                          mons_ev->sock_fd,
167                                          TR_MAX_SOCKETS);
168   if (mons_ev->n_sock_fd==0) {
169     tr_crit("Error opening monitoring interface socket.");
170     retval=1;
171     goto cleanup;
172   }
173
174   /* Set up events */
175   for (ii=0; ii<mons_ev->n_sock_fd; ii++) {
176     mons_ev->ev[ii]=event_new(base,
177                               mons_ev->sock_fd[ii],
178                               EV_READ|EV_PERSIST,
179                               tr_mons_event_cb,
180                               (void *)mons);
181     event_add(mons_ev->ev[ii], NULL);
182   }
183
184 cleanup:
185   talloc_free(tmp_ctx);
186   return retval;
187 }