AST-2018-005: res_pjsip_transport_management: Move to core
[asterisk/asterisk.git] / res / res_pjsip / config_global.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24 #include "asterisk/res_pjsip.h"
25 #include "include/res_pjsip_private.h"
26 #include "asterisk/sorcery.h"
27 #include "asterisk/taskprocessor.h"
28 #include "asterisk/ast_version.h"
29 #include "asterisk/res_pjsip_cli.h"
30
31 #define DEFAULT_MAX_FORWARDS 70
32 #define DEFAULT_KEEPALIVE_INTERVAL 0
33 #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
34 #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
35 #define DEFAULT_DEBUG "no"
36 #define DEFAULT_ENDPOINT_IDENTIFIER_ORDER "ip,username,anonymous"
37 #define DEFAULT_MAX_INITIAL_QUALIFY_TIME 0
38 #define DEFAULT_FROM_USER "asterisk"
39 #define DEFAULT_REALM "asterisk"
40 #define DEFAULT_REGCONTEXT ""
41 #define DEFAULT_CONTACT_EXPIRATION_CHECK_INTERVAL 30
42 #define DEFAULT_DISABLE_MULTI_DOMAIN 0
43 #define DEFAULT_VOICEMAIL_EXTENSION ""
44 #define DEFAULT_UNIDENTIFIED_REQUEST_COUNT 5
45 #define DEFAULT_UNIDENTIFIED_REQUEST_PERIOD 5
46 #define DEFAULT_UNIDENTIFIED_REQUEST_PRUNE_INTERVAL 30
47 #define DEFAULT_MWI_TPS_QUEUE_HIGH AST_TASKPROCESSOR_HIGH_WATER_LEVEL
48 #define DEFAULT_MWI_TPS_QUEUE_LOW -1
49 #define DEFAULT_MWI_DISABLE_INITIAL_UNSOLICITED 0
50 #define DEFAULT_IGNORE_URI_USER_OPTIONS 0
51
52 /*!
53  * \brief Cached global config object
54  *
55  * \details
56  * Cached so we don't have to keep asking sorcery for the config.
57  * We could ask for it hundreds of times a second if not more.
58  */
59 static AO2_GLOBAL_OBJ_STATIC(global_cfg);
60
61 static char default_useragent[256];
62
63 struct global_config {
64         SORCERY_OBJECT(details);
65         AST_DECLARE_STRING_FIELDS(
66                 AST_STRING_FIELD(useragent);
67                 AST_STRING_FIELD(regcontext);
68                 AST_STRING_FIELD(default_outbound_endpoint);
69                 /*! Debug logging yes|no|host */
70                 AST_STRING_FIELD(debug);
71                 /*! Order by which endpoint identifiers are checked (comma separated list) */
72                 AST_STRING_FIELD(endpoint_identifier_order);
73                 /*! User name to place in From header if there is no better option */
74                 AST_STRING_FIELD(default_from_user);
75                 /*! Default voicemail extension */
76                 AST_STRING_FIELD(default_voicemail_extension);
77                 /*! Realm to use in challenges before an endpoint is identified */
78                 AST_STRING_FIELD(default_realm);
79         );
80         /*! Value to put in Max-Forwards header */
81         unsigned int max_forwards;
82         /*! The interval at which to send keep alive messages to active connection-oriented transports */
83         unsigned int keep_alive_interval;
84         /*! The maximum time for all contacts to be qualified at startup */
85         unsigned int max_initial_qualify_time;
86         /*! The interval at which to check for expired contacts */
87         unsigned int contact_expiration_check_interval;
88         /*! Nonzero to disable multi domain support */
89         unsigned int disable_multi_domain;
90         /*! The maximum number of unidentified requests per source IP address before a security event is logged */
91         unsigned int unidentified_request_count;
92         /*! The period during which unidentified requests are accumulated */
93         unsigned int unidentified_request_period;
94         /*! Interval at which expired unidentifed requests will be pruned */
95         unsigned int unidentified_request_prune_interval;
96         struct {
97                 /*! Taskprocessor high water alert trigger level */
98                 unsigned int tps_queue_high;
99                 /*! Taskprocessor low water clear alert level. */
100                 int tps_queue_low;
101                 /*! Nonzero to disable sending unsolicited mwi to all endpoints on startup */
102                 unsigned int disable_initial_unsolicited;
103         } mwi;
104         /*! Nonzero if URI user field options are ignored. */
105         unsigned int ignore_uri_user_options;
106 };
107
108 static void global_destructor(void *obj)
109 {
110         struct global_config *cfg = obj;
111
112         ast_string_field_free_memory(cfg);
113 }
114
115 static void *global_alloc(const char *name)
116 {
117         struct global_config *cfg;
118
119         cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
120         if (!cfg || ast_string_field_init(cfg, 100)) {
121                 ao2_cleanup(cfg);
122                 return NULL;
123         }
124
125         return cfg;
126 }
127
128 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
129 {
130         struct global_config *cfg = obj;
131         char max_forwards[10];
132
133         if (ast_strlen_zero(cfg->debug)) {
134                 ast_log(LOG_ERROR,
135                         "Global option 'debug' can't be empty.  Set it to a valid value or remove the entry to accept 'no' as the default\n");
136                 return -1;
137         }
138
139         if (ast_strlen_zero(cfg->default_from_user)) {
140                 ast_log(LOG_ERROR,
141                         "Global option 'default_from_user' can't be empty.  Set it to a valid value or remove the entry to accept 'asterisk' as the default\n");
142                 return -1;
143         }
144
145         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
146
147         ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
148         ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
149         ast_sip_add_global_response_header("Server", cfg->useragent, 1);
150
151         ao2_t_global_obj_replace_unref(global_cfg, cfg, "Applying global settings");
152         return 0;
153 }
154
155 static struct global_config *get_global_cfg(void)
156 {
157         return ao2_global_obj_ref(global_cfg);
158 }
159
160 char *ast_sip_global_default_outbound_endpoint(void)
161 {
162         char *str;
163         struct global_config *cfg;
164
165         cfg = get_global_cfg();
166         if (!cfg) {
167                 return ast_strdup(DEFAULT_OUTBOUND_ENDPOINT);
168         }
169
170         str = ast_strdup(cfg->default_outbound_endpoint);
171         ao2_ref(cfg, -1);
172         return str;
173 }
174
175 char *ast_sip_get_debug(void)
176 {
177         char *res;
178         struct global_config *cfg;
179
180         cfg = get_global_cfg();
181         if (!cfg) {
182                 return ast_strdup(DEFAULT_DEBUG);
183         }
184
185         res = ast_strdup(cfg->debug);
186         ao2_ref(cfg, -1);
187         return res;
188 }
189
190 char *ast_sip_get_regcontext(void)
191 {
192         char *res;
193         struct global_config *cfg;
194
195         cfg = get_global_cfg();
196         if (!cfg) {
197                 return ast_strdup(DEFAULT_REGCONTEXT);
198         }
199
200         res = ast_strdup(cfg->regcontext);
201         ao2_ref(cfg, -1);
202
203         return res;
204 }
205
206 char *ast_sip_get_default_voicemail_extension(void)
207 {
208         char *res;
209         struct global_config *cfg;
210
211         cfg = get_global_cfg();
212         if (!cfg) {
213                 return ast_strdup(DEFAULT_VOICEMAIL_EXTENSION);
214         }
215
216         res = ast_strdup(cfg->default_voicemail_extension);
217         ao2_ref(cfg, -1);
218
219         return res;
220 }
221
222 char *ast_sip_get_endpoint_identifier_order(void)
223 {
224         char *res;
225         struct global_config *cfg;
226
227         cfg = get_global_cfg();
228         if (!cfg) {
229                 return ast_strdup(DEFAULT_ENDPOINT_IDENTIFIER_ORDER);
230         }
231
232         res = ast_strdup(cfg->endpoint_identifier_order);
233         ao2_ref(cfg, -1);
234         return res;
235 }
236
237 unsigned int ast_sip_get_keep_alive_interval(void)
238 {
239         unsigned int interval;
240         struct global_config *cfg;
241
242         cfg = get_global_cfg();
243         if (!cfg) {
244                 return DEFAULT_KEEPALIVE_INTERVAL;
245         }
246
247         interval = cfg->keep_alive_interval;
248         ao2_ref(cfg, -1);
249         return interval;
250 }
251
252 unsigned int ast_sip_get_contact_expiration_check_interval(void)
253 {
254         unsigned int interval;
255         struct global_config *cfg;
256
257         cfg = get_global_cfg();
258         if (!cfg) {
259                 return DEFAULT_CONTACT_EXPIRATION_CHECK_INTERVAL;
260         }
261
262         interval = cfg->contact_expiration_check_interval;
263         ao2_ref(cfg, -1);
264         return interval;
265 }
266
267 unsigned int ast_sip_get_disable_multi_domain(void)
268 {
269         unsigned int disable_multi_domain;
270         struct global_config *cfg;
271
272         cfg = get_global_cfg();
273         if (!cfg) {
274                 return DEFAULT_DISABLE_MULTI_DOMAIN;
275         }
276
277         disable_multi_domain = cfg->disable_multi_domain;
278         ao2_ref(cfg, -1);
279         return disable_multi_domain;
280 }
281
282 unsigned int ast_sip_get_max_initial_qualify_time(void)
283 {
284         unsigned int time;
285         struct global_config *cfg;
286
287         cfg = get_global_cfg();
288         if (!cfg) {
289                 return DEFAULT_MAX_INITIAL_QUALIFY_TIME;
290         }
291
292         time = cfg->max_initial_qualify_time;
293         ao2_ref(cfg, -1);
294         return time;
295 }
296
297 void ast_sip_get_unidentified_request_thresholds(unsigned int *count, unsigned int *period,
298         unsigned int *prune_interval)
299 {
300         struct global_config *cfg;
301
302         cfg = get_global_cfg();
303         if (!cfg) {
304                 *count = DEFAULT_UNIDENTIFIED_REQUEST_COUNT;
305                 *period = DEFAULT_UNIDENTIFIED_REQUEST_PERIOD;
306                 *prune_interval = DEFAULT_UNIDENTIFIED_REQUEST_PRUNE_INTERVAL;
307                 return;
308         }
309
310         *count = cfg->unidentified_request_count;
311         *period = cfg->unidentified_request_period;
312         *prune_interval = cfg->unidentified_request_prune_interval;
313
314         ao2_ref(cfg, -1);
315         return;
316 }
317
318 void ast_sip_get_default_realm(char *realm, size_t size)
319 {
320         struct global_config *cfg;
321
322         cfg = get_global_cfg();
323         if (!cfg) {
324                 ast_copy_string(realm, DEFAULT_REALM, size);
325         } else {
326                 ast_copy_string(realm, cfg->default_realm, size);
327                 ao2_ref(cfg, -1);
328         }
329 }
330
331 void ast_sip_get_default_from_user(char *from_user, size_t size)
332 {
333         struct global_config *cfg;
334
335         cfg = get_global_cfg();
336         if (!cfg) {
337                 ast_copy_string(from_user, DEFAULT_FROM_USER, size);
338         } else {
339                 ast_copy_string(from_user, cfg->default_from_user, size);
340                 ao2_ref(cfg, -1);
341         }
342 }
343
344
345 unsigned int ast_sip_get_mwi_tps_queue_high(void)
346 {
347         unsigned int tps_queue_high;
348         struct global_config *cfg;
349
350         cfg = get_global_cfg();
351         if (!cfg) {
352                 return DEFAULT_MWI_TPS_QUEUE_HIGH;
353         }
354
355         tps_queue_high = cfg->mwi.tps_queue_high;
356         ao2_ref(cfg, -1);
357         return tps_queue_high;
358 }
359
360 int ast_sip_get_mwi_tps_queue_low(void)
361 {
362         int tps_queue_low;
363         struct global_config *cfg;
364
365         cfg = get_global_cfg();
366         if (!cfg) {
367                 return DEFAULT_MWI_TPS_QUEUE_LOW;
368         }
369
370         tps_queue_low = cfg->mwi.tps_queue_low;
371         ao2_ref(cfg, -1);
372         return tps_queue_low;
373 }
374
375 unsigned int ast_sip_get_mwi_disable_initial_unsolicited(void)
376 {
377         unsigned int disable_initial_unsolicited;
378         struct global_config *cfg;
379
380         cfg = get_global_cfg();
381         if (!cfg) {
382                 return DEFAULT_MWI_DISABLE_INITIAL_UNSOLICITED;
383         }
384
385         disable_initial_unsolicited = cfg->mwi.disable_initial_unsolicited;
386         ao2_ref(cfg, -1);
387         return disable_initial_unsolicited;
388 }
389
390 unsigned int ast_sip_get_ignore_uri_user_options(void)
391 {
392         unsigned int ignore_uri_user_options;
393         struct global_config *cfg;
394
395         cfg = get_global_cfg();
396         if (!cfg) {
397                 return DEFAULT_IGNORE_URI_USER_OPTIONS;
398         }
399
400         ignore_uri_user_options = cfg->ignore_uri_user_options;
401         ao2_ref(cfg, -1);
402         return ignore_uri_user_options;
403 }
404
405 /*!
406  * \internal
407  * \brief Observer to set default global object if none exist.
408  *
409  * \param name Module name owning the sorcery instance.
410  * \param sorcery Instance being observed.
411  * \param object_type Name of object being observed.
412  * \param reloaded Non-zero if the object is being reloaded.
413  *
414  * \return Nothing
415  */
416 static void global_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
417 {
418         struct ao2_container *globals;
419         struct global_config *cfg;
420
421         if (strcmp(object_type, "global")) {
422                 /* Not interested */
423                 return;
424         }
425
426         globals = ast_sorcery_retrieve_by_fields(sorcery, "global",
427                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
428         if (globals) {
429                 int count;
430
431                 count = ao2_container_count(globals);
432                 ao2_ref(globals, -1);
433
434                 if (1 < count) {
435                         ast_log(LOG_ERROR,
436                                 "At most one pjsip.conf type=global object can be defined.  You have %d defined.\n",
437                                 count);
438                         return;
439                 }
440                 if (count) {
441                         return;
442                 }
443         }
444
445         ast_debug(1, "No pjsip.conf type=global object exists so applying defaults.\n");
446         cfg = ast_sorcery_alloc(sorcery, "global", NULL);
447         if (!cfg) {
448                 return;
449         }
450         global_apply(sorcery, cfg);
451         ao2_ref(cfg, -1);
452 }
453
454 static const struct ast_sorcery_instance_observer observer_callbacks_global = {
455         .object_type_loaded = global_loaded_observer,
456 };
457
458 int sip_cli_print_global(struct ast_sip_cli_context *context)
459 {
460         struct global_config *cfg = get_global_cfg();
461
462         if (!cfg) {
463                 cfg = ast_sorcery_alloc(ast_sip_get_sorcery(), "global", NULL);
464                 if (!cfg) {
465                         return -1;
466                 }
467         }
468
469         ast_str_append(&context->output_buffer, 0, "\nGlobal Settings:\n\n");
470         ast_sip_cli_print_sorcery_objectset(cfg, context, 0);
471
472         ao2_ref(cfg, -1);
473         return 0;
474 }
475
476 int ast_sip_destroy_sorcery_global(void)
477 {
478         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
479
480         ast_sorcery_instance_observer_remove(sorcery, &observer_callbacks_global);
481
482         ao2_t_global_obj_release(global_cfg, "Module is unloading");
483
484         return 0;
485 }
486
487 int ast_sip_initialize_sorcery_global(void)
488 {
489         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
490
491         snprintf(default_useragent, sizeof(default_useragent), "%s %s",
492                 DEFAULT_USERAGENT_PREFIX, ast_get_version());
493
494         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
495
496         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
497                 return -1;
498         }
499
500         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
501         ast_sorcery_object_field_register(sorcery, "global", "max_forwards",
502                 __stringify(DEFAULT_MAX_FORWARDS),
503                 OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
504         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
505                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
506         ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint",
507                 DEFAULT_OUTBOUND_ENDPOINT,
508                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
509         ast_sorcery_object_field_register(sorcery, "global", "debug", DEFAULT_DEBUG,
510                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
511         ast_sorcery_object_field_register(sorcery, "global", "endpoint_identifier_order",
512                 DEFAULT_ENDPOINT_IDENTIFIER_ORDER,
513                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, endpoint_identifier_order));
514         ast_sorcery_object_field_register(sorcery, "global", "keep_alive_interval",
515                 __stringify(DEFAULT_KEEPALIVE_INTERVAL),
516                 OPT_UINT_T, 0, FLDSET(struct global_config, keep_alive_interval));
517         ast_sorcery_object_field_register(sorcery, "global", "max_initial_qualify_time",
518                 __stringify(DEFAULT_MAX_INITIAL_QUALIFY_TIME),
519                 OPT_UINT_T, 0, FLDSET(struct global_config, max_initial_qualify_time));
520         ast_sorcery_object_field_register(sorcery, "global", "default_from_user", DEFAULT_FROM_USER,
521                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_from_user));
522         ast_sorcery_object_field_register(sorcery, "global", "default_voicemail_extension",
523                 DEFAULT_VOICEMAIL_EXTENSION, OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config,
524                 default_voicemail_extension));
525         ast_sorcery_object_field_register(sorcery, "global", "regcontext", DEFAULT_REGCONTEXT,
526                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, regcontext));
527         ast_sorcery_object_field_register(sorcery, "global", "contact_expiration_check_interval",
528                 __stringify(DEFAULT_CONTACT_EXPIRATION_CHECK_INTERVAL),
529                 OPT_UINT_T, 0, FLDSET(struct global_config, contact_expiration_check_interval));
530         ast_sorcery_object_field_register(sorcery, "global", "disable_multi_domain",
531                 DEFAULT_DISABLE_MULTI_DOMAIN ? "yes" : "no",
532                 OPT_BOOL_T, 1, FLDSET(struct global_config, disable_multi_domain));
533         ast_sorcery_object_field_register(sorcery, "global", "unidentified_request_count",
534                 __stringify(DEFAULT_UNIDENTIFIED_REQUEST_COUNT),
535                 OPT_UINT_T, 0, FLDSET(struct global_config, unidentified_request_count));
536         ast_sorcery_object_field_register(sorcery, "global", "unidentified_request_period",
537                 __stringify(DEFAULT_UNIDENTIFIED_REQUEST_PERIOD),
538                 OPT_UINT_T, 0, FLDSET(struct global_config, unidentified_request_period));
539         ast_sorcery_object_field_register(sorcery, "global", "unidentified_request_prune_interval",
540                 __stringify(DEFAULT_UNIDENTIFIED_REQUEST_PRUNE_INTERVAL),
541                 OPT_UINT_T, 0, FLDSET(struct global_config, unidentified_request_prune_interval));
542         ast_sorcery_object_field_register(sorcery, "global", "default_realm", DEFAULT_REALM,
543                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_realm));
544         ast_sorcery_object_field_register(sorcery, "global", "mwi_tps_queue_high",
545                 __stringify(DEFAULT_MWI_TPS_QUEUE_HIGH),
546                 OPT_UINT_T, 0, FLDSET(struct global_config, mwi.tps_queue_high));
547         ast_sorcery_object_field_register(sorcery, "global", "mwi_tps_queue_low",
548                 __stringify(DEFAULT_MWI_TPS_QUEUE_LOW),
549                 OPT_INT_T, 0, FLDSET(struct global_config, mwi.tps_queue_low));
550         ast_sorcery_object_field_register(sorcery, "global", "mwi_disable_initial_unsolicited",
551                 DEFAULT_MWI_DISABLE_INITIAL_UNSOLICITED ? "yes" : "no",
552                 OPT_BOOL_T, 1, FLDSET(struct global_config, mwi.disable_initial_unsolicited));
553         ast_sorcery_object_field_register(sorcery, "global", "ignore_uri_user_options",
554                 DEFAULT_IGNORE_URI_USER_OPTIONS ? "yes" : "no",
555                 OPT_BOOL_T, 1, FLDSET(struct global_config, ignore_uri_user_options));
556
557         if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
558                 return -1;
559         }
560
561         return 0;
562 }