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"
32 * Error page to display if submitted GNS key is invalid.
34 #define INVALID_GNSKEY \
35 "<html><head><title>Error</title><body>Invalid GNS public key given.</body></html>"
38 * Error page to display on 404.
41 "<html><head><title>Error</title><body>404 not found</body></html>"
44 * Handle to the HTTP server as provided by libmicrohttpd
46 static struct MHD_Daemon *daemon_handle;
51 static const struct GNUNET_CONFIGURATION_Handle *cfg;
54 * Our primary task for the HTTPD.
56 static struct GNUNET_SCHEDULER_Task *http_task;
61 static struct MHD_Response *main_response;
64 * Error: invalid gns key.
66 static struct MHD_Response *invalid_gnskey_response;
71 static struct MHD_Response *not_found_response;
74 * Absolute name of the 'gns-bcd.tex' file.
81 static uint16_t port = 8888;
92 * Main request handler.
95 access_handler_callback (void *cls,
96 struct MHD_Connection *connection,
100 const char *upload_data,
101 size_t *upload_data_size,
105 static const struct Entry map[] = { { "prefix", "prefix" },
107 { "suffix", "suffix" },
108 { "street", "street" },
110 { "phone", "phone" },
112 { "email", "email" },
113 { "homepage", "homepage" },
115 { "departmenti18n", "departmentde" },
116 { "departmenten", "departmenten" },
117 { "subdepartmenti18n",
119 { "subdepartmenten", "subdepartmenten" },
120 { "jobtitlei18n", "jobtitlegerman" },
121 { "jobtitleen", "jobtitleenglish" },
122 { "subdepartmenten", "subdepartmenten" },
128 (void) upload_data_size;
129 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
132 _ ("Refusing `%s' request to HTTP server\n"),
136 if (NULL == *con_cls)
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending 100 CONTINUE reply\n");
140 return MHD_YES; /* send 100 continue */
142 if (0 == strcasecmp (url, "/"))
143 return MHD_queue_response (connection, MHD_HTTP_OK, main_response);
144 if (0 == strcasecmp (url, "/submit.pdf"))
150 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
154 struct MHD_Response *response;
158 const char *gpg_fp = MHD_lookup_connection_value (connection,
159 MHD_GET_ARGUMENT_KIND,
161 const char *gns_nick = MHD_lookup_connection_value (connection,
162 MHD_GET_ARGUMENT_KIND,
165 MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "gnskey");
166 if ((NULL == gnskey) ||
168 GNUNET_CRYPTO_ecdsa_public_key_from_string (gnskey,
172 return MHD_queue_response (connection,
174 invalid_gnskey_response);
176 tmp = GNUNET_DISK_mkdtemp (gnskey);
179 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mktemp", gnskey);
182 GNUNET_asprintf (&deffile, "%s%s%s", tmp, DIR_SEPARATOR_STR, "def.tex");
183 f = fopen (deffile, "w");
186 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", deffile);
187 GNUNET_free (deffile);
188 GNUNET_DISK_directory_remove (tmp);
192 for (i = 0; NULL != map[i].formname; i++)
194 const char *val = MHD_lookup_connection_value (connection,
195 MHD_GET_ARGUMENT_KIND,
198 fprintf (f, "\\def\\%s{%s}\n", map[i].texname, val);
200 fprintf (f, "\\def\\%s{}\n", map[i].texname);
207 slen = strlen (gpg_fp);
208 gpg1 = GNUNET_strndup (gpg_fp, slen / 2);
209 gpg2 = GNUNET_strdup (&gpg_fp[slen / 2]);
210 fprintf (f, "\\def\\gpglineone{%s}\n\\def\\gpglinetwo{%s}\n", gpg1, gpg2);
215 "\\def\\gns{%s/%s}\n",
217 (NULL == gns_nick) ? "" : gns_nick);
221 "cd %s; cp %s gns-bcd.tex | pdflatex --enable-write18 gns-bcd.tex > /dev/null 2> /dev/null",
224 GNUNET_free (deffile);
226 if (WIFSIGNALED (ret) || (0 != WEXITSTATUS (ret)))
227 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "system", p);
228 GNUNET_asprintf (&deffile, "%s%s%s", tmp, DIR_SEPARATOR_STR, "gns-bcd.pdf");
229 fd = open (deffile, O_RDONLY);
232 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", deffile);
233 GNUNET_free (deffile);
235 GNUNET_DISK_directory_remove (tmp);
239 GNUNET_break (0 == stat (deffile, &st));
241 (response = MHD_create_response_from_fd ((size_t) st.st_size, fd)))
244 GNUNET_break (0 == close (fd));
245 GNUNET_free (deffile);
247 GNUNET_DISK_directory_remove (tmp);
251 (void) MHD_add_response_header (response,
252 MHD_HTTP_HEADER_CONTENT_TYPE,
254 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
255 MHD_destroy_response (response);
256 GNUNET_free (deffile);
258 GNUNET_DISK_directory_remove (tmp);
262 return MHD_queue_response (connection,
269 * Function that queries MHD's select sets and
270 * starts the task waiting for them.
272 static struct GNUNET_SCHEDULER_Task *
273 prepare_daemon (struct MHD_Daemon *daemon_handle);
277 * Call MHD to process pending requests and then go back
278 * and schedule the next run.
281 run_daemon (void *cls)
283 struct MHD_Daemon *daemon_handle = cls;
286 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
287 http_task = prepare_daemon (daemon_handle);
292 * Function that queries MHD's select sets and
293 * starts the task waiting for them.
295 static struct GNUNET_SCHEDULER_Task *
296 prepare_daemon (struct MHD_Daemon *daemon_handle)
298 struct GNUNET_SCHEDULER_Task *ret;
302 struct GNUNET_NETWORK_FDSet *wrs;
303 struct GNUNET_NETWORK_FDSet *wws;
305 MHD_UNSIGNED_LONG_LONG timeout;
307 struct GNUNET_TIME_Relative tv;
312 wrs = GNUNET_NETWORK_fdset_create ();
313 wws = GNUNET_NETWORK_fdset_create ();
315 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
316 haveto = MHD_get_timeout (daemon_handle, &timeout);
317 if (haveto == MHD_YES)
318 tv.rel_value_us = (uint64_t) timeout * 1000LL;
320 tv = GNUNET_TIME_UNIT_FOREVER_REL;
321 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
322 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
323 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
329 GNUNET_NETWORK_fdset_destroy (wrs);
330 GNUNET_NETWORK_fdset_destroy (wws);
336 * Start server offering our hostlist.
338 * @return #GNUNET_OK on success
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _ ("Invalid port number %u. Exiting.\n"),
348 return GNUNET_SYSERR;
350 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
351 _ ("Businesscard HTTP server starts on %u\n"),
353 daemon_handle = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_DEBUG,
355 NULL /* accept_policy_callback */,
357 &access_handler_callback,
359 MHD_OPTION_CONNECTION_LIMIT,
361 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
363 MHD_OPTION_CONNECTION_TIMEOUT,
365 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
366 (size_t) (16 * 1024),
368 if (NULL == daemon_handle)
370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 _ ("Could not start businesscard HTTP server on port %u\n"),
372 (unsigned int) port);
373 return GNUNET_SYSERR;
375 http_task = prepare_daemon (daemon_handle);
384 server_stop (void *cls)
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "HTTP server shutdown\n");
388 if (NULL != http_task)
390 GNUNET_SCHEDULER_cancel (http_task);
393 if (NULL != daemon_handle)
395 MHD_stop_daemon (daemon_handle);
396 daemon_handle = NULL;
398 if (NULL != main_response)
400 MHD_destroy_response (main_response);
401 main_response = NULL;
403 if (NULL != invalid_gnskey_response)
405 MHD_destroy_response (invalid_gnskey_response);
406 invalid_gnskey_response = NULL;
408 if (NULL != not_found_response)
410 MHD_destroy_response (not_found_response);
411 not_found_response = NULL;
415 GNUNET_free (resfile);
422 * Main function that will be run.
425 * @param args remaining command-line arguments
426 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
427 * @param c configuration
433 const struct GNUNET_CONFIGURATION_Handle *c)
444 dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
445 GNUNET_assert (NULL != dir);
446 GNUNET_asprintf (&fn, "%s%s%s", dir, DIR_SEPARATOR_STR, "gns-bcd.html");
447 GNUNET_asprintf (&resfile, "%s%s%s", dir, DIR_SEPARATOR_STR, "gns-bcd.tex");
449 fd = open (fn, O_RDONLY);
452 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
456 if (0 != stat (fn, &st))
458 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
460 GNUNET_break (0 == close (fd));
465 (main_response = MHD_create_response_from_fd ((size_t) st.st_size, fd)))
468 GNUNET_break (0 == close (fd));
471 (void) MHD_add_response_header (main_response,
472 MHD_HTTP_HEADER_CONTENT_TYPE,
474 invalid_gnskey_response =
475 MHD_create_response_from_buffer (strlen (INVALID_GNSKEY),
477 MHD_RESPMEM_PERSISTENT);
478 (void) MHD_add_response_header (invalid_gnskey_response,
479 MHD_HTTP_HEADER_CONTENT_TYPE,
481 not_found_response = MHD_create_response_from_buffer (strlen (NOT_FOUND),
483 MHD_RESPMEM_PERSISTENT);
484 (void) MHD_add_response_header (not_found_response,
485 MHD_HTTP_HEADER_CONTENT_TYPE,
487 if (GNUNET_OK != server_start ())
489 GNUNET_SCHEDULER_add_shutdown (&server_stop, NULL);
494 * The main function for gnunet-gns.
496 * @param argc number of arguments from the command line
497 * @param argv command line arguments
498 * @return 0 ok, 1 on error
501 main (int argc, char *const *argv)
503 struct GNUNET_GETOPT_CommandLineOption options[] = {
504 GNUNET_GETOPT_option_uint16 ('p',
508 "Run HTTP serve on port PORT (default is 8888)"),
510 GNUNET_GETOPT_OPTION_END
514 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
516 GNUNET_log_setup ("gnunet-bcd", "WARNING", NULL);
518 GNUNET_PROGRAM_run (argc,
521 _ ("GNUnet HTTP server to create business cards"),
527 GNUNET_free ((void *) argv);
532 /* end of gnunet-bcd.c */