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/>.
20 * @file gns/gnunet-bcd.c
21 * @author Christian Grothoff
22 * @brief HTTP server to create GNS business cards
26 #include <microhttpd.h>
27 #include "gnunet_util_lib.h"
30 * Error page to display if submitted GNS key is invalid.
32 #define INVALID_GNSKEY "<html><head><title>Error</title><body>Invalid GNS public key given.</body></html>"
35 * Error page to display on 404.
37 #define NOT_FOUND "<html><head><title>Error</title><body>404 not found</body></html>"
40 * Handle to the HTTP server as provided by libmicrohttpd
42 static struct MHD_Daemon *daemon_handle;
47 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 * Our primary task for the HTTPD.
52 static struct GNUNET_SCHEDULER_Task * http_task;
57 static struct MHD_Response *main_response;
60 * Error: invalid gns key.
62 static struct MHD_Response *invalid_gnskey_response;
67 static struct MHD_Response *not_found_response;
70 * Absolute name of the 'gns-bcd.tex' file.
77 static uint16_t port = 8888;
88 * Main request handler.
91 access_handler_callback (void *cls,
92 struct MHD_Connection *connection,
96 const char *upload_data,
97 size_t * upload_data_size,
101 static const struct Entry map[] = {
102 { "prefix", "prefix" },
104 { "suffix", "suffix" },
105 { "street", "street" },
107 { "phone", "phone" },
110 { "homepage", "homepage" },
112 { "departmenti18n", "departmentde"},
113 { "departmenten", "departmenten"},
114 { "subdepartmenti18n", "subdepartmentde"},
115 { "subdepartmenten", "subdepartmenten"},
116 { "jobtitlei18n", "jobtitlegerman"},
117 { "jobtitleen", "jobtitleenglish"},
118 { "subdepartmenten", "subdepartmenten"},
122 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
124 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
125 _("Refusing `%s' request to HTTP server\n"),
129 if (NULL == *con_cls)
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Sending 100 CONTINUE reply\n");
134 return MHD_YES; /* send 100 continue */
136 if (0 == strcasecmp (url, "/"))
137 return MHD_queue_response (connection,
140 if (0 == strcasecmp (url, "/submit.pdf"))
146 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
150 struct MHD_Response *response;
154 const char *gpg_fp = MHD_lookup_connection_value (connection,
155 MHD_GET_ARGUMENT_KIND,
157 const char *gns_nick = MHD_lookup_connection_value (connection,
158 MHD_GET_ARGUMENT_KIND,
160 const char *gnskey = MHD_lookup_connection_value (connection,
161 MHD_GET_ARGUMENT_KIND,
163 if ( (NULL == gnskey) ||
165 GNUNET_CRYPTO_ecdsa_public_key_from_string (gnskey,
169 return MHD_queue_response (connection,
171 invalid_gnskey_response);
173 tmp = GNUNET_DISK_mkdtemp (gnskey);
176 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mktemp", gnskey);
179 GNUNET_asprintf (&deffile,
181 tmp, DIR_SEPARATOR_STR, "def.tex");
182 f = FOPEN (deffile, "w");
185 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", deffile);
186 GNUNET_free (deffile);
187 GNUNET_DISK_directory_remove (tmp);
191 for (i=0; NULL != map[i].formname; i++)
193 const char *val = MHD_lookup_connection_value (connection,
194 MHD_GET_ARGUMENT_KIND,
199 map[i].texname, val);
210 slen = strlen (gpg_fp);
211 gpg1 = GNUNET_strndup (gpg_fp, slen / 2);
212 gpg2 = GNUNET_strdup (&gpg_fp[slen / 2]);
214 "\\def\\gpglineone{%s}\n\\def\\gpglinetwo{%s}\n",
220 "\\def\\gns{%s/%s}\n",
222 (NULL == gns_nick) ? "" : gns_nick);
225 "cd %s; cp %s gns-bcd.tex | pdflatex --enable-write18 gns-bcd.tex > /dev/null 2> /dev/null",
228 GNUNET_free (deffile);
230 if (WIFSIGNALED (ret) || (0 != WEXITSTATUS(ret)))
231 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
234 GNUNET_asprintf (&deffile,
236 tmp, DIR_SEPARATOR_STR, "gns-bcd.pdf");
237 fd = OPEN (deffile, O_RDONLY);
240 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
243 GNUNET_free (deffile);
245 GNUNET_DISK_directory_remove (tmp);
249 GNUNET_break (0 == STAT (deffile, &st));
250 if (NULL == (response = MHD_create_response_from_fd ((size_t) st.st_size, fd)))
253 GNUNET_break (0 == CLOSE (fd));
254 GNUNET_free (deffile);
256 GNUNET_DISK_directory_remove (tmp);
260 (void) MHD_add_response_header (response,
261 MHD_HTTP_HEADER_CONTENT_TYPE,
263 ret = MHD_queue_response (connection,
266 MHD_destroy_response (response);
267 GNUNET_free (deffile);
269 GNUNET_DISK_directory_remove (tmp);
273 return MHD_queue_response (connection,
280 * Function that queries MHD's select sets and
281 * starts the task waiting for them.
283 static struct GNUNET_SCHEDULER_Task *
284 prepare_daemon (struct MHD_Daemon *daemon_handle);
288 * Call MHD to process pending requests and then go back
289 * and schedule the next run.
292 run_daemon (void *cls)
294 struct MHD_Daemon *daemon_handle = cls;
297 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
298 http_task = prepare_daemon (daemon_handle);
303 * Function that queries MHD's select sets and
304 * starts the task waiting for them.
306 static struct GNUNET_SCHEDULER_Task *
307 prepare_daemon (struct MHD_Daemon *daemon_handle)
309 struct GNUNET_SCHEDULER_Task * ret;
313 struct GNUNET_NETWORK_FDSet *wrs;
314 struct GNUNET_NETWORK_FDSet *wws;
316 MHD_UNSIGNED_LONG_LONG timeout;
318 struct GNUNET_TIME_Relative tv;
323 wrs = GNUNET_NETWORK_fdset_create ();
324 wws = GNUNET_NETWORK_fdset_create ();
326 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
327 haveto = MHD_get_timeout (daemon_handle, &timeout);
328 if (haveto == MHD_YES)
329 tv.rel_value_us = (uint64_t) timeout * 1000LL;
331 tv = GNUNET_TIME_UNIT_FOREVER_REL;
332 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
333 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
335 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
337 &run_daemon, daemon_handle);
338 GNUNET_NETWORK_fdset_destroy (wrs);
339 GNUNET_NETWORK_fdset_destroy (wws);
345 * Start server offering our hostlist.
347 * @return #GNUNET_OK on success
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 _("Invalid port number %u. Exiting.\n"),
357 return GNUNET_SYSERR;
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
360 _("Businesscard HTTP server starts on %u\n"),
362 daemon_handle = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_DEBUG,
364 NULL /* accept_policy_callback */, NULL,
365 &access_handler_callback, NULL,
366 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 512,
367 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 2,
368 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 60,
369 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
371 if (NULL == daemon_handle)
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
374 _("Could not start businesscard HTTP server on port %u\n"),
375 (unsigned int) port);
376 return GNUNET_SYSERR;
378 http_task = prepare_daemon (daemon_handle);
387 server_stop (void *cls)
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "HTTP server shutdown\n");
391 if (NULL != http_task)
393 GNUNET_SCHEDULER_cancel (http_task);
396 if (NULL != daemon_handle)
398 MHD_stop_daemon (daemon_handle);
399 daemon_handle = NULL;
401 if (NULL != main_response)
403 MHD_destroy_response (main_response);
404 main_response = NULL;
406 if (NULL != invalid_gnskey_response)
408 MHD_destroy_response (invalid_gnskey_response);
409 invalid_gnskey_response = NULL;
411 if (NULL != not_found_response)
413 MHD_destroy_response (not_found_response);
414 not_found_response = NULL;
418 GNUNET_free (resfile);
425 * Main function that will be run.
428 * @param args remaining command-line arguments
429 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
430 * @param c configuration
436 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,
451 GNUNET_asprintf (&resfile,
457 fd = OPEN (fn, O_RDONLY);
460 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
466 if (0 != STAT (fn, &st))
468 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
476 if (NULL == (main_response = MHD_create_response_from_fd ((size_t) st.st_size, fd)))
479 GNUNET_break (0 == CLOSE (fd));
482 (void) MHD_add_response_header (main_response,
483 MHD_HTTP_HEADER_CONTENT_TYPE,
485 invalid_gnskey_response = MHD_create_response_from_buffer (strlen (INVALID_GNSKEY),
487 MHD_RESPMEM_PERSISTENT);
488 (void) MHD_add_response_header (invalid_gnskey_response,
489 MHD_HTTP_HEADER_CONTENT_TYPE,
491 not_found_response = MHD_create_response_from_buffer (strlen (NOT_FOUND),
493 MHD_RESPMEM_PERSISTENT);
494 (void) MHD_add_response_header (not_found_response,
495 MHD_HTTP_HEADER_CONTENT_TYPE,
500 GNUNET_SCHEDULER_add_shutdown (&server_stop,
506 * The main function for gnunet-gns.
508 * @param argc number of arguments from the command line
509 * @param argv command line arguments
510 * @return 0 ok, 1 on error
513 main (int argc, char *const *argv)
515 struct GNUNET_GETOPT_CommandLineOption options[] = {
517 GNUNET_GETOPT_option_uint16 ('p',
520 gettext_noop ("Run HTTP serve on port PORT (default is 8888)"),
522 GNUNET_GETOPT_OPTION_END
527 GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
529 GNUNET_log_setup ("gnunet-bcd", "WARNING", NULL);
532 GNUNET_PROGRAM_run (argc, argv, "gnunet-bcd",
533 _("GNUnet HTTP server to create business cards"),
535 &run, NULL)) ? 0 : 1;
536 GNUNET_free ((void*) argv);
541 /* end of gnunet-bcd.c */