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