remove never working setuid helper code from the build-system.
[oweals/gnunet.git] / src / transport / transport_api_blacklist.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file transport/transport_api_blacklist.c
23  * @brief library to access the blacklisting functions of the transport service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
33
34 /**
35  * Handle for blacklisting requests.
36  */
37 struct GNUNET_TRANSPORT_Blacklist
38 {
39   /**
40    * Connection to transport service.
41    */
42   struct GNUNET_MQ_Handle *mq;
43
44   /**
45    * Configuration to use.
46    */
47   const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49   /**
50    * Function to call for determining if a peer is allowed
51    * to communicate with us.
52    */
53   GNUNET_TRANSPORT_BlacklistCallback cb;
54
55   /**
56    * Closure for @e cb.
57    */
58   void *cb_cls;
59 };
60
61
62 /**
63  * Establish blacklist connection to transport service.
64  *
65  * @param br overall handle
66  */
67 static void
68 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
69
70
71 /**
72  * Handle blacklist queries.
73  *
74  * @param cls our overall handle
75  * @param bm query
76  */
77 static void
78 handle_query (void *cls,
79               const struct BlacklistMessage *bm)
80 {
81   struct GNUNET_TRANSPORT_Blacklist *br = cls;
82   struct GNUNET_MQ_Envelope *env;
83   struct BlacklistMessage *res;
84
85   GNUNET_break (0 == ntohl (bm->is_allowed));
86   env = GNUNET_MQ_msg (res,
87                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
88   res->is_allowed = htonl (br->cb (br->cb_cls,
89                                    &bm->peer));
90   res->peer = bm->peer;
91   GNUNET_MQ_send (br->mq,
92                   env);
93 }
94
95 /**
96  * Generic error handler, called with the appropriate error code and
97  * the same closure specified at the creation of the message queue.
98  * Not every message queue implementation supports an error handler.
99  *
100  * @param cls closure with the `struct GNUNET_TRANSPORT_Blacklist *`
101  * @param error error code
102  */
103 static void
104 mq_error_handler (void *cls,
105                   enum GNUNET_MQ_Error error)
106 {
107   struct GNUNET_TRANSPORT_Blacklist *br = cls;
108
109   reconnect (br);
110 }
111
112
113 /**
114  * Establish blacklist connection to transport service.
115  *
116  * @param br overall handle
117  */
118 static void
119 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
120 {
121   struct GNUNET_MQ_MessageHandler handlers[] = {
122     GNUNET_MQ_hd_fixed_size (query,
123                              GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY,
124                              struct BlacklistMessage,
125                              br),
126     GNUNET_MQ_handler_end ()
127   };
128   struct GNUNET_MQ_Envelope *env;
129   struct GNUNET_MessageHeader *req;
130
131   if (NULL != br->mq)
132     GNUNET_MQ_destroy (br->mq);
133   br->mq = GNUNET_CLIENT_connect (br->cfg,
134                                   "transport",
135                                   handlers,
136                                   &mq_error_handler,
137                                   br);
138   if (NULL == br->mq)
139     return;
140   env = GNUNET_MQ_msg (req,
141                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
142   GNUNET_MQ_send (br->mq,
143                   env);
144 }
145
146
147 /**
148  * Install a blacklist callback.  The service will be queried for all
149  * existing connections as well as any fresh connections to check if
150  * they are permitted.  If the blacklisting callback is unregistered,
151  * all hosts that were denied in the past will automatically be
152  * whitelisted again.  Cancelling the blacklist handle is also the
153  * only way to re-enable connections from peers that were previously
154  * blacklisted.
155  *
156  * @param cfg configuration to use
157  * @param cb callback to invoke to check if connections are allowed
158  * @param cb_cls closure for @a cb
159  * @return NULL on error, otherwise handle for cancellation
160  */
161 struct GNUNET_TRANSPORT_Blacklist *
162 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
163                             GNUNET_TRANSPORT_BlacklistCallback cb,
164                             void *cb_cls)
165 {
166   struct GNUNET_TRANSPORT_Blacklist *br;
167
168   br = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
169   br->cfg = cfg;
170   br->cb = cb;
171   br->cb_cls = cb_cls;
172   reconnect (br);
173   if (NULL == br->mq)
174   {
175     GNUNET_free (br);
176     return NULL;
177   }
178   return br;
179 }
180
181
182 /**
183  * Abort the blacklist.  Note that this function is the only way for
184  * removing a peer from the blacklist.
185  *
186  * @param br handle of the request that is to be cancelled
187  */
188 void
189 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
190 {
191   GNUNET_MQ_destroy (br->mq);
192   GNUNET_free (br);
193 }
194
195
196 /* end of transport_api_blacklist.c */