2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file gns/gnunet-bcd.c
23 * @author Christian Grothoff
24 * @brief HTTP server to create GNS business cards
28 #include <microhttpd.h>
29 #include "gnunet_util_lib.h"
30 #include "gnunet_mhd_compat.h"
33 * Error page to display if submitted GNS key is invalid.
35 #define INVALID_GNSKEY \
36 "<html><head><title>Error</title><body>Invalid GNS public key given.</body></html>"
39 * Error page to display on 404.
42 "<html><head><title>Error</title><body>404 not found</body></html>"
45 * Handle to the HTTP server as provided by libmicrohttpd
47 static struct MHD_Daemon *daemon_handle;
52 static const struct GNUNET_CONFIGURATION_Handle *cfg;
55 * Our primary task for the HTTPD.
57 static struct GNUNET_SCHEDULER_Task *http_task;
62 static struct MHD_Response *main_response;
65 * Error: invalid gns key.
67 static struct MHD_Response *invalid_gnskey_response;
72 static struct MHD_Response *not_found_response;
75 * Absolute name of the 'gns-bcd.tex' file.
82 static uint16_t port = 8888;
93 * Main request handler.
96 access_handler_callback (void *cls,
97 struct MHD_Connection *connection,
101 const char *upload_data,
102 size_t *upload_data_size,
106 static const struct Entry map[] = { { "prefix", "prefix" },
108 { "suffix", "suffix" },
109 { "street", "street" },
111 { "phone", "phone" },
113 { "email", "email" },
114 { "homepage", "homepage" },
116 { "departmenti18n", "departmentde" },
117 { "departmenten", "departmenten" },
118 { "subdepartmenti18n",
120 { "subdepartmenten", "subdepartmenten" },
121 { "jobtitlei18n", "jobtitlegerman" },
122 { "jobtitleen", "jobtitleenglish" },
123 { "subdepartmenten", "subdepartmenten" },
129 (void) upload_data_size;
130 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
133 _ ("Refusing `%s' request to HTTP server\n"),
137 if (NULL == *con_cls)
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending 100 CONTINUE reply\n");
141 return MHD_YES; /* send 100 continue */
143 if (0 == strcasecmp (url, "/"))
144 return MHD_queue_response (connection, MHD_HTTP_OK, main_response);
145 if (0 == strcasecmp (url, "/submit.pdf"))
151 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
155 struct MHD_Response *response;
159 const char *gpg_fp = MHD_lookup_connection_value (connection,
160 MHD_GET_ARGUMENT_KIND,
162 const char *gns_nick = MHD_lookup_connection_value (connection,
163 MHD_GET_ARGUMENT_KIND,
166 MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "gnskey");
167 if ((NULL == gnskey) ||
169 GNUNET_CRYPTO_ecdsa_public_key_from_string (gnskey,
173 return MHD_queue_response (connection,
175 invalid_gnskey_response);
177 tmp = GNUNET_DISK_mkdtemp (gnskey);
180 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mktemp", gnskey);
183 GNUNET_asprintf (&deffile, "%s%s%s", tmp, DIR_SEPARATOR_STR, "def.tex");
184 f = fopen (deffile, "w");
187 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", deffile);
188 GNUNET_free (deffile);
189 GNUNET_DISK_directory_remove (tmp);
193 for (i = 0; NULL != map[i].formname; i++)
195 const char *val = MHD_lookup_connection_value (connection,
196 MHD_GET_ARGUMENT_KIND,
199 fprintf (f, "\\def\\%s{%s}\n", map[i].texname, val);
201 fprintf (f, "\\def\\%s{}\n", map[i].texname);
208 slen = strlen (gpg_fp);
209 gpg1 = GNUNET_strndup (gpg_fp, slen / 2);
210 gpg2 = GNUNET_strdup (&gpg_fp[slen / 2]);
211 fprintf (f, "\\def\\gpglineone{%s}\n\\def\\gpglinetwo{%s}\n", gpg1, gpg2);
216 "\\def\\gns{%s/%s}\n",
218 (NULL == gns_nick) ? "" : gns_nick);
222 "cd %s; cp %s gns-bcd.tex | pdflatex --enable-write18 gns-bcd.tex > /dev/null 2> /dev/null",
225 GNUNET_free (deffile);
227 if (WIFSIGNALED (ret) || (0 != WEXITSTATUS (ret)))
228 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "system", p);
229 GNUNET_asprintf (&deffile, "%s%s%s", tmp, DIR_SEPARATOR_STR, "gns-bcd.pdf");
230 fd = open (deffile, O_RDONLY);
233 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", deffile);
234 GNUNET_free (deffile);
236 GNUNET_DISK_directory_remove (tmp);
240 GNUNET_break (0 == stat (deffile, &st));
242 (response = MHD_create_response_from_fd ((size_t) st.st_size, fd)))
245 GNUNET_break (0 == close (fd));
246 GNUNET_free (deffile);
248 GNUNET_DISK_directory_remove (tmp);
252 (void) MHD_add_response_header (response,
253 MHD_HTTP_HEADER_CONTENT_TYPE,
255 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
256 MHD_destroy_response (response);
257 GNUNET_free (deffile);
259 GNUNET_DISK_directory_remove (tmp);
263 return MHD_queue_response (connection,
270 * Function that queries MHD's select sets and
271 * starts the task waiting for them.
273 static struct GNUNET_SCHEDULER_Task *
274 prepare_daemon (struct MHD_Daemon *daemon_handle);
278 * Call MHD to process pending requests and then go back
279 * and schedule the next run.
282 run_daemon (void *cls)
284 struct MHD_Daemon *daemon_handle = cls;
287 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
288 http_task = prepare_daemon (daemon_handle);
293 * Function that queries MHD's select sets and
294 * starts the task waiting for them.
296 static struct GNUNET_SCHEDULER_Task *
297 prepare_daemon (struct MHD_Daemon *daemon_handle)
299 struct GNUNET_SCHEDULER_Task *ret;
303 struct GNUNET_NETWORK_FDSet *wrs;
304 struct GNUNET_NETWORK_FDSet *wws;
306 MHD_UNSIGNED_LONG_LONG timeout;
308 struct GNUNET_TIME_Relative tv;
313 wrs = GNUNET_NETWORK_fdset_create ();
314 wws = GNUNET_NETWORK_fdset_create ();
316 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
317 haveto = MHD_get_timeout (daemon_handle, &timeout);
318 if (haveto == MHD_YES)
319 tv.rel_value_us = (uint64_t) timeout * 1000LL;
321 tv = GNUNET_TIME_UNIT_FOREVER_REL;
322 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
323 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
324 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
330 GNUNET_NETWORK_fdset_destroy (wrs);
331 GNUNET_NETWORK_fdset_destroy (wws);
337 * Start server offering our hostlist.
339 * @return #GNUNET_OK on success
346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 _ ("Invalid port number %u. Exiting.\n"),
349 return GNUNET_SYSERR;
351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
352 _ ("Businesscard HTTP server starts on %u\n"),
354 daemon_handle = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_DEBUG,
356 NULL /* accept_policy_callback */,
358 &access_handler_callback,
360 MHD_OPTION_CONNECTION_LIMIT,
362 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
364 MHD_OPTION_CONNECTION_TIMEOUT,
366 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
367 (size_t) (16 * 1024),
369 if (NULL == daemon_handle)
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 _ ("Could not start businesscard HTTP server on port %u\n"),
373 (unsigned int) port);
374 return GNUNET_SYSERR;
376 http_task = prepare_daemon (daemon_handle);
385 server_stop (void *cls)
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "HTTP server shutdown\n");
389 if (NULL != http_task)
391 GNUNET_SCHEDULER_cancel (http_task);
394 if (NULL != daemon_handle)
396 MHD_stop_daemon (daemon_handle);
397 daemon_handle = NULL;
399 if (NULL != main_response)
401 MHD_destroy_response (main_response);
402 main_response = NULL;
404 if (NULL != invalid_gnskey_response)
406 MHD_destroy_response (invalid_gnskey_response);
407 invalid_gnskey_response = NULL;
409 if (NULL != not_found_response)
411 MHD_destroy_response (not_found_response);
412 not_found_response = NULL;
416 GNUNET_free (resfile);
423 * Main function that will be run.
426 * @param args remaining command-line arguments
427 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
428 * @param c configuration
434 const struct GNUNET_CONFIGURATION_Handle *c)
445 dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
446 GNUNET_assert (NULL != dir);
447 GNUNET_asprintf (&fn, "%s%s%s", dir, DIR_SEPARATOR_STR, "gns-bcd.html");
448 GNUNET_asprintf (&resfile, "%s%s%s", dir, DIR_SEPARATOR_STR, "gns-bcd.tex");
450 fd = open (fn, O_RDONLY);
453 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
457 if (0 != stat (fn, &st))
459 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
461 GNUNET_break (0 == close (fd));
466 (main_response = MHD_create_response_from_fd ((size_t) st.st_size, fd)))
469 GNUNET_break (0 == close (fd));
472 (void) MHD_add_response_header (main_response,
473 MHD_HTTP_HEADER_CONTENT_TYPE,
475 invalid_gnskey_response =
476 MHD_create_response_from_buffer (strlen (INVALID_GNSKEY),
478 MHD_RESPMEM_PERSISTENT);
479 (void) MHD_add_response_header (invalid_gnskey_response,
480 MHD_HTTP_HEADER_CONTENT_TYPE,
482 not_found_response = MHD_create_response_from_buffer (strlen (NOT_FOUND),
484 MHD_RESPMEM_PERSISTENT);
485 (void) MHD_add_response_header (not_found_response,
486 MHD_HTTP_HEADER_CONTENT_TYPE,
488 if (GNUNET_OK != server_start ())
490 GNUNET_SCHEDULER_add_shutdown (&server_stop, NULL);
495 * The main function for gnunet-gns.
497 * @param argc number of arguments from the command line
498 * @param argv command line arguments
499 * @return 0 ok, 1 on error
502 main (int argc, char *const *argv)
504 struct GNUNET_GETOPT_CommandLineOption options[] = {
505 GNUNET_GETOPT_option_uint16 ('p',
509 "Run HTTP serve on port PORT (default is 8888)"),
511 GNUNET_GETOPT_OPTION_END
515 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
517 GNUNET_log_setup ("gnunet-bcd", "WARNING", NULL);
519 GNUNET_PROGRAM_run (argc,
522 _ ("GNUnet HTTP server to create business cards"),
528 GNUNET_free ((void *) argv);
533 /* end of gnunet-bcd.c */