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