2 * Copyright (c) 2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
42 #include <mon_internal.h>
43 #include <tr_socket.h>
48 * Allocate a new MONS_INSTANCE
50 * @param mem_ctx talloc context for allocation
51 * @return new MONS_INSTANCE or null on failure
53 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
55 MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
59 mons->req_handler = NULL;
60 mons->auth_handler = NULL;
62 mons->authorized_gss_names = tr_gss_names_new(mons);
63 if (mons->authorized_gss_names == NULL) {
72 * Create a listener for monitoring requests
74 * Accept connections with mons_accept()
76 * @param mons monitoring server instance
86 int mons_get_listener(MONS_INSTANCE *mons,
87 MONS_REQ_FUNC *req_handler,
88 MONS_AUTH_FUNC *auth_handler,
98 n_fd = tr_sock_listen_all(port, fd_out, max_fd);
100 tr_err("mons_get_listener: Error opening port %d");
102 /* opening port succeeded */
103 tr_info("mons_get_listener: Opened port %d.", port);
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++) {
120 /* store the caller's request handler & cookie */
121 mons->req_handler = req_handler;
122 mons->auth_handler = auth_handler;
123 mons->cookie = cookie;
130 * Accept and process a connection on a port opened with mons_get_listener()
132 * @param mons monitoring interface instance
133 * @param listen FD of the connection socket
134 * @return 0 on success
136 int mons_accept(MONS_INSTANCE *mons, int listen)
141 if (0 > (conn = accept(listen, NULL, NULL))) {
142 perror("Error from monitoring interface accept()");
146 if (0 > (pid = fork())) {
147 perror("Error on fork()");
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 */
159 exit(0); /* exit to kill forked child process */
164 /* clean up any processes that have completed */
165 while (waitpid(-1, 0, WNOHANG) > 0);