aboutsummaryrefslogtreecommitdiff
path: root/src/extensions/age_restriction/extension_age_restriction.c
blob: 697d066f2e85ec962580a5e0c63e32a8eba7ce74 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
   This file is part of TALER
   Copyright (C) 2021-2022 Taler Systems SA

   TALER 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.

   TALER 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
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file extension_age_restriction.c
 * @brief Utility functions regarding age restriction
 * @author Özgür Kesim
 */
#include "platform.h"
#include "taler_util.h"
#include "taler_extensions.h"
#include "stdint.h"

/* ==================================================
 *
 * Age Restriction  TALER_Extension implementation
 *
 * ==================================================
 */

/**
 * @brief local configuration
 */

static struct TALER_AgeRestrictionConfig AR_config = {0};

/**
 * @brief implements the TALER_Extension.disable interface.
 *
 * @param ext Pointer to the current extension
 */
static void
age_restriction_disable (
  struct TALER_Extension *ext)
{
  if (NULL == ext)
    return;

  ext->enabled = false;
  ext->config = NULL;

  if (NULL != ext->config_json)
  {
    json_decref (ext->config_json);
    ext->config_json = NULL;
  }

  AR_config.enabled = false;
  AR_config.mask.bits = 0;
  AR_config.num_groups = 0;
}


/**
 * @brief implements the TALER_Extension.load_json_config interface.
 *
 * @param ext if NULL, only tests the configuration
 * @param jconfig the configuration as json
 */
static enum GNUNET_GenericReturnValue
age_restriction_load_json_config (
  struct TALER_Extension *ext,
  json_t *jconfig)
{
  struct TALER_AgeMask mask = {0};
  enum GNUNET_GenericReturnValue ret;

  ret = TALER_JSON_parse_age_groups (jconfig, &mask);
  if (GNUNET_OK != ret)
    return ret;

  /* only testing the parser */
  if (ext == NULL)
    return GNUNET_OK;

  if (TALER_Extension_AgeRestriction != ext->type)
    return GNUNET_SYSERR;

  if (mask.bits > 0)
  {
    /* if the mask is not zero, the first bit MUST be set */
    if (0 == (mask.bits & 1))
      return GNUNET_SYSERR;

    AR_config.mask.bits = mask.bits;
    AR_config.num_groups = __builtin_popcount (mask.bits) - 1;
  }

  AR_config.enabled = true;
  ext->config = &AR_config;

  if (NULL != ext->config_json)
    json_decref (ext->config_json);

  ext->enabled = true;
  ext->config_json = json_copy (jconfig);
  json_decref (jconfig);

  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "loaded new age restriction config with age groups: %s\n",
              TALER_age_mask_to_string (&mask));

  return GNUNET_OK;
}


/**
 * @brief implements the TALER_Extension.config_to_json interface.
 *
 * @param ext if NULL, only tests the configuration
 * @return configuration as json_t* object, maybe NULL
 */
static json_t *
age_restriction_config_to_json (
  const struct TALER_Extension *ext)
{
  char *mask_str;
  json_t *conf;

  GNUNET_assert (NULL != ext);

  if (! ext->enabled)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "age restriction not enabled");
    return json_null ();
  }

  if (NULL == ext->config)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "age restriction not configured");
    return json_null ();
  }

  if (NULL != ext->config_json)
  {
    return json_copy (ext->config_json);
  }

  mask_str = TALER_age_mask_to_string (&AR_config.mask);
  conf = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("age_groups", mask_str)
    );

  free (mask_str);

  return GNUNET_JSON_PACK (
    GNUNET_JSON_pack_bool ("critical", ext->critical),
    GNUNET_JSON_pack_string ("version", ext->version),
    GNUNET_JSON_pack_object_steal ("config", conf)
    );
}


/**
 * @brief implements the TALER_Extension.test_json_config interface.
 *
 * @param config configuration as json_t* to test
 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise.
 */
static enum GNUNET_GenericReturnValue
age_restriction_test_json_config (
  const json_t *config)
{
  struct TALER_AgeMask mask = {0};

  return TALER_JSON_parse_age_groups (config, &mask);
}


/* The extension for age restriction */
struct TALER_Extension TE_extension_age_restriction = {
  .type = TALER_Extension_AgeRestriction,
  .name = "age_restriction",
  .critical = false,
  .version = "1",
  .enabled = false, /* disabled per default */
  .has_config = true, /* we need to store configuration */
  .config = NULL,
  .config_json = NULL,
  .disable = &age_restriction_disable,
  .test_json_config = &age_restriction_test_json_config,
  .load_json_config = &age_restriction_load_json_config,
  .config_to_json = &age_restriction_config_to_json,
  .http_post_handler = NULL,
};


/**
 * @brief implements the init() function for GNUNET_PLUGIN_load
 *
 * @param arg Pointer to the GNUNET_CONFIGURATION_Handle
 * @return pointer to TALER_Extension on success or NULL otherwise.
 */
void *
libtaler_extension_age_restriction_init (void *arg)
{
  const struct GNUNET_CONFIGURATION_Handle *cfg = arg;
  char *groups = NULL;
  struct TALER_AgeMask mask = {0};

  if ((GNUNET_YES !=
       GNUNET_CONFIGURATION_have_value (cfg,
                                        TALER_EXTENSION_SECTION_AGE_RESTRICTION,
                                        "ENABLED"))
      ||
      (GNUNET_YES !=
       GNUNET_CONFIGURATION_get_value_yesno (cfg,
                                             TALER_EXTENSION_SECTION_AGE_RESTRICTION,
                                             "ENABLED")))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "[age restriction] no section %s found in configuration\n",
                TALER_EXTENSION_SECTION_AGE_RESTRICTION);

    return NULL;
  }

  /* Age restriction is enabled, extract age groups */
  if ((GNUNET_YES ==
       GNUNET_CONFIGURATION_have_value (cfg,
                                        TALER_EXTENSION_SECTION_AGE_RESTRICTION,
                                        "AGE_GROUPS"))
      &&
      (GNUNET_YES !=
       GNUNET_CONFIGURATION_get_value_string (cfg,
                                              TALER_EXTENSION_SECTION_AGE_RESTRICTION,
                                              "AGE_GROUPS",
                                              &groups)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "[age restriction] AGE_GROUPS in %s is not a string\n",
                TALER_EXTENSION_SECTION_AGE_RESTRICTION);

    return NULL;
  }

  mask.bits = TALER_EXTENSION_AGE_RESTRICTION_DEFAULT_AGE_MASK;

  if ((groups != NULL) &&
      (GNUNET_OK != TALER_parse_age_group_string (groups, &mask)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "[age restriction] couldn't parse age groups: '%s'\n",
                groups);
    return NULL;
  }

  AR_config.mask = mask;
  AR_config.num_groups = __builtin_popcount (mask.bits) - 1;   /* no underflow, first bit always set */
  AR_config.enabled = true;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "[age restriction] setting age mask to %s with #groups: %d\n",
              TALER_age_mask_to_string (&AR_config.mask),
              __builtin_popcount (AR_config.mask.bits) - 1);

  TE_extension_age_restriction.config = &AR_config;
  TE_extension_age_restriction.enabled = true;

  /* Note: we do now have TE_age_restriction_config set, however
   * ext->config_json is NOT set, i.e. the extension is not yet active! For
   * age restriction to become active, load_json_config must have been
   * called. */

  GNUNET_free (groups);
  return &TE_extension_age_restriction;
}


/**
 * @brief implements the done() function for GNUNET_PLUGIN_load
 *
 * @param cfg unsued
 * @return pointer to TALER_Extension on success or NULL otherwise.
 */
void *
libtaler_extension_age_restriction_done (void *arg)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "[age restriction] disabling and unloading");
  AR_config.enabled = 0;
  AR_config.mask.bits = 0;
  AR_config.num_groups = 0;
  return NULL;
}


/* end of extension_age_restriction.c */