LCOV - code coverage report
Current view: top level - mhd - mhd_config.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 48 137 35.0 %
Date: 2021-04-12 06:08:44 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014--2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file mhd_config.c
      18             :  * @brief functions to configure and setup MHD
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include "taler_mhd_lib.h"
      26             : 
      27             : 
      28             : /**
      29             :  * Backlog for listen operation on UNIX domain sockets.
      30             :  */
      31             : #define UNIX_BACKLOG 500
      32             : 
      33             : 
      34             : /**
      35             :  * Parse the configuration to determine on which port
      36             :  * or UNIX domain path we should run an HTTP service.
      37             :  *
      38             :  * @param cfg configuration to parse
      39             :  * @param section section of the configuration to parse (usually "exchange")
      40             :  * @param[out] rport set to the port number, or 0 for none
      41             :  * @param[out] unix_path set to the UNIX path, or NULL for none
      42             :  * @param[out] unix_mode set to the mode to be used for @a unix_path
      43             :  * @return #GNUNET_OK on success
      44             :  */
      45             : enum GNUNET_GenericReturnValue
      46          13 : TALER_MHD_parse_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
      47             :                         const char *section,
      48             :                         uint16_t *rport,
      49             :                         char **unix_path,
      50             :                         mode_t *unix_mode)
      51             : {
      52          13 :   const char *choices[] = {
      53             :     "tcp",
      54             :     "unix",
      55             :     NULL
      56             :   };
      57             :   const char *serve_type;
      58             : 
      59          13 :   if (GNUNET_OK !=
      60          13 :       GNUNET_CONFIGURATION_get_value_choice (cfg,
      61             :                                              section,
      62             :                                              "SERVE",
      63             :                                              choices,
      64             :                                              &serve_type))
      65             :   {
      66           0 :     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
      67             :                                section,
      68             :                                "SERVE",
      69             :                                "serve type (tcp or unix) required");
      70           0 :     return GNUNET_SYSERR;
      71             :   }
      72             : 
      73          13 :   if (0 == strcasecmp (serve_type,
      74             :                        "tcp"))
      75             :   {
      76             :     unsigned long long port;
      77             : 
      78          12 :     if (GNUNET_OK !=
      79          12 :         GNUNET_CONFIGURATION_get_value_number (cfg,
      80             :                                                section,
      81             :                                                "port",
      82             :                                                &port))
      83             :     {
      84           0 :       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
      85             :                                  section,
      86             :                                  "PORT",
      87             :                                  "port number required");
      88           0 :       return GNUNET_SYSERR;
      89             :     }
      90             : 
      91          12 :     if ( (0 == port) ||
      92          12 :          (port > UINT16_MAX) )
      93             :     {
      94           0 :       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
      95             :                                  section,
      96             :                                  "PORT",
      97             :                                  "port number not in [1,65535]");
      98           0 :       return GNUNET_SYSERR;
      99             :     }
     100          12 :     *rport = (uint16_t) port;
     101          12 :     *unix_path = NULL;
     102          12 :     return GNUNET_OK;
     103             :   }
     104           1 :   if (0 == strcmp (serve_type,
     105             :                    "unix"))
     106             :   {
     107             :     struct sockaddr_un s_un;
     108             :     char *modestring;
     109             : 
     110           1 :     if (GNUNET_OK !=
     111           1 :         GNUNET_CONFIGURATION_get_value_filename (cfg,
     112             :                                                  section,
     113             :                                                  "UNIXPATH",
     114             :                                                  unix_path))
     115             :     {
     116           0 :       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
     117             :                                  section,
     118             :                                  "UNIXPATH",
     119             :                                  "UNIXPATH value required");
     120           0 :       return GNUNET_SYSERR;
     121             :     }
     122           1 :     if (strlen (*unix_path) >= sizeof (s_un.sun_path))
     123             :     {
     124           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     125             :                   "unixpath `%s' is too long\n",
     126             :                   *unix_path);
     127           0 :       GNUNET_free (*unix_path);
     128           0 :       return GNUNET_SYSERR;
     129             :     }
     130             : 
     131           1 :     if (GNUNET_OK !=
     132           1 :         GNUNET_CONFIGURATION_get_value_string (cfg,
     133             :                                                section,
     134             :                                                "UNIXPATH_MODE",
     135             :                                                &modestring))
     136             :     {
     137           0 :       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     138             :                                  section,
     139             :                                  "UNIXPATH_MODE");
     140           0 :       GNUNET_free (*unix_path);
     141           0 :       return GNUNET_SYSERR;
     142             :     }
     143           1 :     errno = 0;
     144           1 :     *unix_mode = (mode_t) strtoul (modestring, NULL, 8);
     145           1 :     if (0 != errno)
     146             :     {
     147           0 :       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
     148             :                                  section,
     149             :                                  "UNIXPATH_MODE",
     150             :                                  "must be octal number");
     151           0 :       GNUNET_free (modestring);
     152           0 :       GNUNET_free (*unix_path);
     153           0 :       return GNUNET_SYSERR;
     154             :     }
     155           1 :     GNUNET_free (modestring);
     156           1 :     return GNUNET_OK;
     157             :   }
     158             :   /* not reached */
     159           0 :   GNUNET_assert (0);
     160             :   return GNUNET_SYSERR;
     161             : }
     162             : 
     163             : 
     164             : /**
     165             :  * Function called for logging by MHD.
     166             :  *
     167             :  * @param cls closure, NULL
     168             :  * @param fm format string (`printf()`-style)
     169             :  * @param ap arguments to @a fm
     170             :  */
     171             : void
     172          12 : TALER_MHD_handle_logs (void *cls,
     173             :                        const char *fm,
     174             :                        va_list ap)
     175             : {
     176             :   static int cache;
     177             :   char buf[2048];
     178             : 
     179             :   (void) cls;
     180          12 :   if (-1 == cache)
     181           0 :     return;
     182          12 :   if (0 == cache)
     183             :   {
     184          12 :     if (0 ==
     185          12 :         GNUNET_get_log_call_status (GNUNET_ERROR_TYPE_INFO,
     186             :                                     "libmicrohttpd",
     187             :                                     __FILE__,
     188             :                                     __FUNCTION__,
     189             :                                     __LINE__))
     190             :     {
     191           0 :       cache = -1;
     192           0 :       return;
     193             :     }
     194             :   }
     195          12 :   cache = 1;
     196          12 :   vsnprintf (buf,
     197             :              sizeof (buf),
     198             :              fm,
     199             :              ap);
     200          12 :   GNUNET_log_from_nocheck (GNUNET_ERROR_TYPE_INFO,
     201             :                            "libmicrohttpd",
     202             :                            "%s",
     203             :                            buf);
     204             : }
     205             : 
     206             : 
     207             : /**
     208             :  * Open UNIX domain socket for listining at @a unix_path with
     209             :  * permissions @a unix_mode.
     210             :  *
     211             :  * @param unix_path where to listen
     212             :  * @param unix_mode access permissions to set
     213             :  * @return -1 on error, otherwise the listen socket
     214             :  */
     215             : int
     216           1 : TALER_MHD_open_unix_path (const char *unix_path,
     217             :                           mode_t unix_mode)
     218             : {
     219             :   struct GNUNET_NETWORK_Handle *nh;
     220             :   struct sockaddr_un *un;
     221             : 
     222           1 :   if (sizeof (un->sun_path) <= strlen (unix_path))
     223             :   {
     224           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     225             :                 "unixpath `%s' is too long\n",
     226             :                 unix_path);
     227           0 :     return -1;
     228             :   }
     229           1 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     230             :               "Creating listen socket '%s' with mode %o\n",
     231             :               unix_path,
     232             :               unix_mode);
     233             : 
     234           1 :   if (GNUNET_OK !=
     235           1 :       GNUNET_DISK_directory_create_for_file (unix_path))
     236             :   {
     237           1 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     238             :                               "mkdir",
     239             :                               unix_path);
     240             :   }
     241             : 
     242           1 :   un = GNUNET_new (struct sockaddr_un);
     243           1 :   un->sun_family = AF_UNIX;
     244           1 :   strncpy (un->sun_path,
     245             :            unix_path,
     246             :            sizeof (un->sun_path) - 1);
     247           1 :   GNUNET_NETWORK_unix_precheck (un);
     248             : 
     249           1 :   if (NULL == (nh = GNUNET_NETWORK_socket_create (AF_UNIX,
     250             :                                                   SOCK_STREAM,
     251             :                                                   0)))
     252             :   {
     253           0 :     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     254             :                          "socket");
     255           0 :     GNUNET_free (un);
     256           0 :     return -1;
     257             :   }
     258           1 :   if (GNUNET_OK !=
     259           1 :       GNUNET_NETWORK_socket_bind (nh,
     260             :                                   (void *) un,
     261             :                                   sizeof (struct sockaddr_un)))
     262             :   {
     263           1 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     264             :                               "bind",
     265             :                               unix_path);
     266           1 :     GNUNET_free (un);
     267           1 :     GNUNET_NETWORK_socket_close (nh);
     268           1 :     return -1;
     269             :   }
     270           0 :   GNUNET_free (un);
     271           0 :   if (GNUNET_OK !=
     272           0 :       GNUNET_NETWORK_socket_listen (nh,
     273             :                                     UNIX_BACKLOG))
     274             :   {
     275           0 :     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     276             :                          "listen");
     277           0 :     GNUNET_NETWORK_socket_close (nh);
     278           0 :     return -1;
     279             :   }
     280             : 
     281           0 :   if (0 != chmod (unix_path,
     282             :                   unix_mode))
     283             :   {
     284           0 :     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     285             :                          "chmod");
     286           0 :     GNUNET_NETWORK_socket_close (nh);
     287           0 :     return -1;
     288             :   }
     289           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     290             :               "set socket '%s' to mode %o\n",
     291             :               unix_path,
     292             :               unix_mode);
     293             : 
     294             :   /* extract and return actual socket handle from 'nh' */
     295             :   {
     296             :     int fd;
     297             : 
     298           0 :     fd = GNUNET_NETWORK_get_fd (nh);
     299           0 :     GNUNET_NETWORK_socket_free_memory_only_ (nh);
     300           0 :     return fd;
     301             :   }
     302             : }
     303             : 
     304             : 
     305             : /**
     306             :  * Bind a listen socket to the UNIX domain path or the TCP port and IP address
     307             :  * as specified in @a cfg in section @a section.  IF only a port was
     308             :  * specified, set @a port and return -1.  Otherwise, return the bound file
     309             :  * descriptor.
     310             :  *
     311             :  * @param cfg configuration to parse
     312             :  * @param section configuration section to use
     313             :  * @param[out] port port to set, if TCP without BINDTO
     314             :  * @return -1 and a port of zero on error, otherwise
     315             :  *    either -1 and a port, or a bound stream socket
     316             :  */
     317             : int
     318           0 : TALER_MHD_bind (const struct GNUNET_CONFIGURATION_Handle *cfg,
     319             :                 const char *section,
     320             :                 uint16_t *port)
     321             : {
     322             :   char *bind_to;
     323             :   struct GNUNET_NETWORK_Handle *nh;
     324             : 
     325           0 :   *port = 0;
     326             :   {
     327             :     char *serve_unixpath;
     328             :     mode_t unixpath_mode;
     329             : 
     330           0 :     if (GNUNET_OK !=
     331           0 :         TALER_MHD_parse_config (cfg,
     332             :                                 section,
     333             :                                 port,
     334             :                                 &serve_unixpath,
     335             :                                 &unixpath_mode))
     336           0 :       return -1;
     337           0 :     if (NULL != serve_unixpath)
     338             :     {
     339             :       int ret;
     340             : 
     341           0 :       ret = TALER_MHD_open_unix_path (serve_unixpath,
     342             :                                       unixpath_mode);
     343           0 :       GNUNET_free (serve_unixpath);
     344           0 :       return ret;
     345             :     }
     346             :   }
     347           0 :   if (GNUNET_OK !=
     348           0 :       GNUNET_CONFIGURATION_get_value_string (cfg,
     349             :                                              section,
     350             :                                              "BIND_TO",
     351             :                                              &bind_to))
     352           0 :     return -1; /* only set port */
     353             : 
     354             :   /* let's have fun binding... */
     355             :   {
     356             :     char port_str[6];
     357             :     struct addrinfo hints;
     358             :     struct addrinfo *res;
     359             :     int ec;
     360             : 
     361           0 :     GNUNET_snprintf (port_str,
     362             :                      sizeof (port_str),
     363             :                      "%u",
     364           0 :                      (unsigned int) *port);
     365           0 :     *port = 0; /* do NOT return port in case of errors */
     366           0 :     memset (&hints,
     367             :             0,
     368             :             sizeof (hints));
     369           0 :     hints.ai_family = AF_UNSPEC;
     370           0 :     hints.ai_socktype = SOCK_STREAM;
     371           0 :     hints.ai_protocol = IPPROTO_TCP;
     372           0 :     hints.ai_flags = AI_PASSIVE
     373             : #ifdef AI_IDN
     374             :                      | AI_IDN
     375             : #endif
     376             :     ;
     377             : 
     378           0 :     if (0 !=
     379           0 :         (ec = getaddrinfo (bind_to,
     380             :                            port_str,
     381             :                            &hints,
     382             :                            &res)))
     383             :     {
     384           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     385             :                   "Failed to resolve BIND_TO address `%s': %s\n",
     386             :                   bind_to,
     387             :                   gai_strerror (ec));
     388           0 :       GNUNET_free (bind_to);
     389           0 :       return -1;
     390             :     }
     391           0 :     GNUNET_free (bind_to);
     392             : 
     393           0 :     if (NULL == (nh = GNUNET_NETWORK_socket_create (res->ai_family,
     394           0 :                                                     res->ai_socktype,
     395           0 :                                                     res->ai_protocol)))
     396             :     {
     397           0 :       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     398             :                            "socket");
     399           0 :       freeaddrinfo (res);
     400           0 :       return -1;
     401             :     }
     402           0 :     if (GNUNET_OK !=
     403           0 :         GNUNET_NETWORK_socket_bind (nh,
     404           0 :                                     res->ai_addr,
     405           0 :                                     res->ai_addrlen))
     406             :     {
     407           0 :       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     408             :                            "bind");
     409           0 :       freeaddrinfo (res);
     410           0 :       return -1;
     411             :     }
     412           0 :     freeaddrinfo (res);
     413             :   }
     414             : 
     415           0 :   if (GNUNET_OK !=
     416           0 :       GNUNET_NETWORK_socket_listen (nh,
     417             :                                     UNIX_BACKLOG))
     418             :   {
     419           0 :     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
     420             :                          "listen");
     421           0 :     GNUNET_SCHEDULER_shutdown ();
     422           0 :     return -1;
     423             :   }
     424             : 
     425             :   /* extract and return actual socket handle from 'nh' */
     426             :   {
     427             :     int fh;
     428             : 
     429           0 :     fh = GNUNET_NETWORK_get_fd (nh);
     430           0 :     GNUNET_NETWORK_socket_free_memory_only_ (nh);
     431           0 :     return fh;
     432             :   }
     433             : }

Generated by: LCOV version 1.14