1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
This file is part of GNUnet
(C) 2008--2013 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file testbed/gnunet-daemon-testbed-blacklist.c
* @brief daemon to restrict incoming connections from other peers at the
* transport layer of a peer
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_transport_service.h"
/**
* Logging shorthand
*/
#define LOG(type,...) \
GNUNET_log (type, __VA_ARGS__)
/**
* Debug logging shorthand
*/
#define DEBUG(...) \
LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
/**
* Allow access from the peers read from the whitelist
*/
#define ACCESS_ALLOW 1
/**
* Deny access from the peers read from the blacklist
*/
#define ACCESS_DENY 0
/**
* The map to store the peer identities to allow/deny
*/
static struct GNUNET_CONTAINER_MultiPeerMap *map;
/**
* The array of peer identities we read from whitelist/blacklist
*/
static struct GNUNET_PeerIdentity *ilist;
/**
* The blacklist handle we obtain from transport when we register ourselves for
* access control
*/
struct GNUNET_TRANSPORT_Blacklist *bh;
/**
* Task for shutdown
*/
static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
/**
* Are we allowing or denying access from peers
*/
static int mode;
/**
* @ingroup hashmap
* Iterator over hash map entries.
*
* @param cls closure
* @param key current key code
* @param value value in the hash map
* @return #GNUNET_YES if we should continue to
* iterate,
* #GNUNET_NO if not.
*/
static int
iterator (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
{
GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (map, key,
value));
return GNUNET_YES;
}
/**
* Cleaup and destroy the map
*/
static void
cleanup_map ()
{
if (NULL != map)
{
GNUNET_assert (GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_iterate (map,
&iterator,
NULL));
GNUNET_CONTAINER_multipeermap_destroy (map);
map = NULL;
}
}
/**
* Shutdown task to cleanup our resources and exit.
*
* @param cls NULL
* @param tc scheduler task context
*/
static void
do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
cleanup_map ();
if (NULL != bh)
GNUNET_TRANSPORT_blacklist_cancel (bh);
}
/**
* Function that decides if a connection is acceptable or not.
*
* @param cls closure
* @param pid peer to approve or disapproave
* @return GNUNET_OK if the connection is allowed, GNUNET_SYSERR if not
*/
static int
check_access (void *cls, const struct GNUNET_PeerIdentity * pid)
{
int contains;
if (NULL != map)
contains = GNUNET_CONTAINER_multipeermap_contains (map, pid);
else
contains = GNUNET_NO;
if (ACCESS_DENY == mode)
return (contains) ? GNUNET_SYSERR : GNUNET_OK;
return (contains) ? GNUNET_OK : GNUNET_SYSERR;
}
/**
* Setup the access control by reading the given file containing peer identities
* and then establishing blacklist handler with the peer's transport service
*
* @param fname the filename to read the list of peer identities
* @param cfg the configuration for connecting to the peer's transport service
*/
static void
setup_ac (const char *fname, const struct GNUNET_CONFIGURATION_Handle *cfg)
{
uint64_t fsize;
unsigned int npeers;
unsigned int cnt;
GNUNET_assert (GNUNET_OK != GNUNET_DISK_file_size (fname, &fsize, GNUNET_NO,
GNUNET_YES));
if (0 != (fsize % sizeof (struct GNUNET_PeerIdentity)))
{
GNUNET_break (0);
return;
}
npeers = fsize / sizeof (struct GNUNET_PeerIdentity);
if (0 != npeers)
{
map = GNUNET_CONTAINER_multipeermap_create (npeers, GNUNET_YES);
ilist = GNUNET_malloc_large (fsize);
GNUNET_assert (fsize == GNUNET_DISK_fn_read (fname, ilist, fsize));
}
for (cnt = 0; cnt < npeers; cnt++)
{
if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_put (map, &ilist[cnt],
&ilist[cnt],
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
{
cleanup_map ();
GNUNET_free (ilist);
return;
}
}
shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
&do_shutdown, NULL);
bh = GNUNET_TRANSPORT_blacklist (cfg, &check_access, NULL);
}
/**
* Main function that will be run.
*
* @param cls closure
* @param args remaining command-line arguments
* @param cfgfile name of the configuration file used (for saving, can be NULL!)
* @param c configuration
*/
static void
run (void *cls, char *const *args, const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *c)
{
char *shome;
char fname[PATH_MAX];
if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "PATHS",
"GNUNET_HOME",
&shome))
{
GNUNET_break (0);
return;
}
GNUNET_assert (0 < GNUNET_snprintf (fname, PATH_MAX, "%s/whitelist", shome));
if (GNUNET_YES == GNUNET_DISK_file_test (fname))
{
mode = ACCESS_ALLOW;
setup_ac (fname, c);
GNUNET_free (shome);
return;
}
GNUNET_assert (0 < GNUNET_snprintf (fname, PATH_MAX, "%s/blacklist", shome));
if (GNUNET_YES == GNUNET_DISK_file_test (fname))
{
mode = ACCESS_DENY;
setup_ac (shome, c);
}
GNUNET_free (shome);
return;
}
/**
* The main function.
*
* @param argc number of arguments from the command line
* @param argv command line arguments
* @return 0 ok, 1 on error
*/
int
main (int argc, char *const *argv)
{
static const struct GNUNET_GETOPT_CommandLineOption options[] = {
GNUNET_GETOPT_OPTION_END
};
int ret;
if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
return 2;
ret =
(GNUNET_OK ==
GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-testbed-blacklist",
_
("Daemon to restrict incoming transport layer connections during testbed deployments"),
options, &run, NULL)) ? 0 : 1;
GNUNET_free ((void*) argv);
return ret;
}
|