-
Notifications
You must be signed in to change notification settings - Fork 453
/
mongoc-uri.c
2687 lines (2255 loc) · 73.4 KB
/
mongoc-uri.c
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>
/* strcasecmp on windows */
#include "mongoc/mongoc-util-private.h"
#include "mongoc/mongoc-config.h"
#include "mongoc/mongoc-host-list.h"
#include "mongoc/mongoc-host-list-private.h"
#include "mongoc/mongoc-log.h"
#include "mongoc/mongoc-handshake-private.h"
#include "mongoc/mongoc-socket.h"
#include "mongoc/mongoc-topology-private.h"
#include "mongoc/mongoc-uri-private.h"
#include "mongoc/mongoc-read-concern-private.h"
#include "mongoc/mongoc-write-concern-private.h"
#include "mongoc/mongoc-compression-private.h"
#include "mongoc/utlist.h"
struct _mongoc_uri_t {
char *str;
bool is_srv;
char srv[BSON_HOST_NAME_MAX + 1];
mongoc_host_list_t *hosts;
char *username;
char *password;
char *database;
bson_t raw; /* Unparsed options, see mongoc_uri_parse_options */
bson_t options; /* Type-coerced and canonicalized options */
bson_t credentials;
bson_t compressors;
mongoc_read_prefs_t *read_prefs;
mongoc_read_concern_t *read_concern;
mongoc_write_concern_t *write_concern;
};
#define MONGOC_URI_ERROR(error, format, ...) \
bson_set_error (error, \
MONGOC_ERROR_COMMAND, \
MONGOC_ERROR_COMMAND_INVALID_ARG, \
format, \
__VA_ARGS__);
static const char *escape_instructions = "Percent-encode username and password"
" according to RFC 3986";
static bool
_mongoc_uri_set_option_as_int32 (mongoc_uri_t *uri,
const char *option,
int32_t value);
static bool
_mongoc_uri_set_option_as_int32_with_error (mongoc_uri_t *uri,
const char *option,
int32_t value,
bson_error_t *error);
static bool
ends_with (const char *str, const char *suffix);
static void
mongoc_uri_do_unescape (char **str)
{
char *tmp;
if ((tmp = *str)) {
*str = mongoc_uri_unescape (tmp);
bson_free (tmp);
}
}
#define VALIDATE_SRV_ERR() \
do { \
bson_set_error (error, \
MONGOC_ERROR_STREAM, \
MONGOC_ERROR_STREAM_NAME_RESOLUTION, \
"Invalid host \"%s\" returned for service \"%s\": " \
"host must be subdomain of service name", \
host, \
service); \
return false; \
} while (0)
static int
count_dots (const char *s)
{
int n = 0;
const char *dot = s;
while ((dot = strchr (dot + 1, '.'))) {
n++;
}
return n;
}
/* at least one character, and does not start or end with dot */
static bool
valid_hostname (const char *s)
{
size_t len = strlen (s);
return len > 1 && s[0] != '.' && s[len - 1] != '.';
}
static bool
validate_srv_result (mongoc_uri_t *uri, const char *host, bson_error_t *error)
{
const char *service;
const char *service_root;
service = mongoc_uri_get_service (uri);
BSON_ASSERT (service);
if (!valid_hostname (host)) {
VALIDATE_SRV_ERR ();
}
service_root = strchr (service, '.');
BSON_ASSERT (service_root);
/* host must be descendent of service root: if service is
* "a.foo.co" host can be like "a.foo.co", "b.foo.co", "a.b.foo.co", etc.
*/
if (strlen (host) < strlen (service_root)) {
VALIDATE_SRV_ERR ();
}
if (!ends_with (host, service_root)) {
VALIDATE_SRV_ERR ();
}
return true;
}
static mongoc_host_list_t *
_mongoc_host_list_find_host_and_port (mongoc_host_list_t *hosts,
const char *host_and_port)
{
mongoc_host_list_t *iter;
LL_FOREACH (hosts, iter)
{
if (strcmp (iter->host_and_port, host_and_port) == 0) {
return iter;
}
}
return NULL;
}
/* upsert @host into @uri's host list. Side effect: modifies host->next when
* inserting. */
static bool
_upsert_into_host_list (mongoc_uri_t *uri,
mongoc_host_list_t *host,
bson_error_t *error)
{
mongoc_host_list_t *link;
if (uri->is_srv && !validate_srv_result (uri, host->host, error)) {
return false;
}
link =
_mongoc_host_list_find_host_and_port (uri->hosts, host->host_and_port);
if (!link) {
link = bson_malloc0 (sizeof (mongoc_host_list_t));
LL_APPEND (uri->hosts, link);
} else {
/* Make sure linking is preserved when copying data into final. */
host->next = link->next;
}
memcpy (link, host, sizeof (mongoc_host_list_t));
return true;
}
bool
mongoc_uri_upsert_host_and_port (mongoc_uri_t *uri,
const char *host_and_port,
bson_error_t *error)
{
mongoc_host_list_t temp;
memset (&temp, 0, sizeof (mongoc_host_list_t));
if (!_mongoc_host_list_from_string_with_err (&temp, host_and_port, error)) {
return false;
}
return _upsert_into_host_list (uri, &temp, error);
}
bool
mongoc_uri_upsert_host (mongoc_uri_t *uri,
const char *host,
uint16_t port,
bson_error_t *error)
{
mongoc_host_list_t temp;
memset (&temp, 0, sizeof (mongoc_host_list_t));
if (!_mongoc_host_list_from_hostport_with_err (&temp, host, port, error)) {
return false;
}
return _upsert_into_host_list (uri, &temp, error);
}
void
mongoc_uri_remove_host (mongoc_uri_t *uri, const char *host, uint16_t port)
{
_mongoc_host_list_remove_host (&(uri->hosts), host, port);
}
/*
*--------------------------------------------------------------------------
*
* scan_to_unichar --
*
* Scans 'str' until either a character matching 'match' is found,
* until one of the characters in 'terminators' is encountered, or
* until we reach the end of 'str'.
*
* NOTE: 'terminators' may not include multibyte UTF-8 characters.
*
* Returns:
* If 'match' is found, returns a copy of the section of 'str' before
* that character. Otherwise, returns NULL.
*
* Side Effects:
* If 'match' is found, sets 'end' to begin at the matching character
* in 'str'.
*
*--------------------------------------------------------------------------
*/
static char *
scan_to_unichar (const char *str,
bson_unichar_t match,
const char *terminators,
const char **end)
{
bson_unichar_t c;
const char *iter;
for (iter = str; iter && *iter && (c = bson_utf8_get_char (iter));
iter = bson_utf8_next_char (iter)) {
if (c == match) {
*end = iter;
return bson_strndup (str, iter - str);
} else if (c == '\\') {
iter = bson_utf8_next_char (iter);
if (!bson_utf8_get_char (iter)) {
break;
}
} else {
const char *term_iter;
for (term_iter = terminators; *term_iter; term_iter++) {
if (c == *term_iter) {
return NULL;
}
}
}
}
return NULL;
}
/*
*--------------------------------------------------------------------------
*
* ends_with --
*
* Return true if str ends with suffix.
*
*--------------------------------------------------------------------------
*/
static bool
ends_with (const char *str, const char *suffix)
{
size_t str_len = strlen (str);
size_t suffix_len = strlen (suffix);
const char *s1, *s2;
if (str_len < suffix_len) {
return false;
}
/* start at the ends of both strings */
s1 = str + str_len;
s2 = suffix + suffix_len;
/* until either pointer reaches start of its string, compare the pointers */
for (; s1 >= str && s2 >= suffix; s1--, s2--) {
if (*s1 != *s2) {
return false;
}
}
return true;
}
static bool
mongoc_uri_parse_scheme (mongoc_uri_t *uri, const char *str, const char **end)
{
if (!strncmp (str, "mongodb+srv://", 14)) {
uri->is_srv = true;
*end = str + 14;
return true;
}
if (!strncmp (str, "mongodb://", 10)) {
uri->is_srv = false;
*end = str + 10;
return true;
}
return false;
}
static bool
mongoc_uri_has_unescaped_chars (const char *str, const char *chars)
{
const char *c;
const char *tmp;
char *s;
for (c = chars; *c; c++) {
s = scan_to_unichar (str, (bson_unichar_t) *c, "", &tmp);
if (s) {
bson_free (s);
return true;
}
}
return false;
}
/* "str" is non-NULL, the part of URI between "mongodb://" and first "@" */
static bool
mongoc_uri_parse_userpass (mongoc_uri_t *uri,
const char *str,
bson_error_t *error)
{
const char *prohibited = "@:/";
const char *end_user;
BSON_ASSERT (str);
if ((uri->username = scan_to_unichar (str, ':', "", &end_user))) {
uri->password = bson_strdup (end_user + 1);
} else {
uri->username = bson_strdup (str);
uri->password = NULL;
}
if (mongoc_uri_has_unescaped_chars (uri->username, prohibited)) {
MONGOC_URI_ERROR (error,
"Username \"%s\" must not have unescaped chars. %s",
uri->username,
escape_instructions);
return false;
}
mongoc_uri_do_unescape (&uri->username);
if (!uri->username) {
MONGOC_URI_ERROR (
error, "Incorrect URI escapes in username. %s", escape_instructions);
return false;
}
/* Providing password at all is optional */
if (uri->password) {
if (mongoc_uri_has_unescaped_chars (uri->password, prohibited)) {
MONGOC_URI_ERROR (error,
"Password \"%s\" must not have unescaped chars. %s",
uri->password,
escape_instructions);
return false;
}
mongoc_uri_do_unescape (&uri->password);
if (!uri->password) {
MONGOC_URI_ERROR (error, "%s", "Incorrect URI escapes in password");
return false;
}
}
return true;
}
bool
mongoc_uri_parse_host (mongoc_uri_t *uri, const char *host_and_port_in)
{
char *host_and_port = bson_strdup (host_and_port_in);
bson_error_t err = {0};
bool r;
/* unescape host. It doesn't hurt including port. */
if (mongoc_uri_has_unescaped_chars (host_and_port, "/")) {
MONGOC_WARNING ("Unix Domain Sockets must be escaped (e.g. / = %%2F)");
bson_free (host_and_port);
return false;
}
mongoc_uri_do_unescape (&host_and_port);
if (!host_and_port) {
/* invalid */
bson_free (host_and_port);
return false;
}
r = mongoc_uri_upsert_host_and_port (uri, host_and_port, &err);
if (!r) {
MONGOC_ERROR ("%s", err.message);
bson_free (host_and_port);
return false;
}
bson_free (host_and_port);
return true;
}
bool
mongoc_uri_parse_srv (mongoc_uri_t *uri, const char *str)
{
char *service;
if (*str == '\0') {
return false;
}
service = bson_strdup (str);
mongoc_uri_do_unescape (&service);
if (!service) {
/* invalid */
return false;
}
if (!valid_hostname (service) || count_dots (service) < 2) {
bson_free (service);
return false;
}
bson_strncpy (uri->srv, service, sizeof uri->srv);
bson_free (service);
if (strchr (uri->srv, ',') || strchr (uri->srv, ':')) {
/* prohibit port number or multiple service names */
return false;
}
return true;
}
/* "hosts" is non-NULL, the part between "mongodb://" or "@" and last "/" */
static bool
mongoc_uri_parse_hosts (mongoc_uri_t *uri, const char *hosts)
{
const char *next;
const char *end_hostport;
char *s;
BSON_ASSERT (hosts);
/*
* Parsing the series of hosts is a lot more complicated than you might
* imagine. This is due to some characters being both separators as well as
* valid characters within the "hostname". In particularly, we can have file
* paths to specify paths to UNIX domain sockets. We impose the restriction
* that they must be suffixed with ".sock" to simplify the parsing.
*
* You can separate hosts and file system paths to UNIX domain sockets with
* ",".
*/
s = scan_to_unichar (hosts, '?', "", &end_hostport);
if (s) {
MONGOC_WARNING (
"%s", "A '/' is required between the host list and any options.");
goto error;
}
next = hosts;
do {
/* makes a copy of the section of the string */
s = scan_to_unichar (next, ',', "", &end_hostport);
if (s) {
next = (char *) end_hostport + 1;
} else {
s = bson_strdup (next);
next = NULL;
}
if (!mongoc_uri_parse_host (uri, s)) {
goto error;
}
bson_free (s);
} while (next);
return true;
error:
bson_free (s);
return false;
}
/* -----------------------------------------------------------------------------
*
* mongoc_uri_parse_database --
*
* Parse the database after @str. @str is expected to point after the
* host list to the character immediately after the / in the uri string.
* If no database is specified in the uri, e.g. the uri has a form like:
* mongodb://localhost/?option=X then uri->database remains NULL after
* parsing.
*
* Return:
* True if the parsed database is valid. An empty database is considered
* valid.
* -----------------------------------------------------------------------------
*/
static bool
mongoc_uri_parse_database (mongoc_uri_t *uri, const char *str, const char **end)
{
const char *end_database;
const char *c;
char *invalid_c;
const char *tmp;
if ((uri->database = scan_to_unichar (str, '?', "", &end_database))) {
if (strcmp (uri->database, "") == 0) {
/* no database is found, don't store the empty string. */
bson_free (uri->database);
uri->database = NULL;
/* but it is valid to have an empty database. */
return true;
}
*end = end_database;
} else if (*str) {
uri->database = bson_strdup (str);
*end = str + strlen (str);
}
mongoc_uri_do_unescape (&uri->database);
if (!uri->database) {
/* invalid */
return false;
}
/* invalid characters in database name */
for (c = "/\\. \"$"; *c; c++) {
invalid_c =
scan_to_unichar (uri->database, (bson_unichar_t) *c, "", &tmp);
if (invalid_c) {
bson_free (invalid_c);
return false;
}
}
return true;
}
static bool
mongoc_uri_parse_auth_mechanism_properties (mongoc_uri_t *uri, const char *str)
{
char *field;
char *value;
const char *end_scan;
bson_t properties;
bson_init (&properties);
/* build up the properties document */
while ((field = scan_to_unichar (str, ':', "&", &end_scan))) {
str = end_scan + 1;
if (!(value = scan_to_unichar (str, ',', ":&", &end_scan))) {
value = bson_strdup (str);
str = "";
} else {
str = end_scan + 1;
}
bson_append_utf8 (&properties, field, -1, value, -1);
bson_free (field);
bson_free (value);
}
/* append our auth properties to our credentials */
if (!mongoc_uri_set_mechanism_properties (uri, &properties)) {
bson_destroy (&properties);
return false;
}
bson_destroy (&properties);
return true;
}
static bool
mongoc_uri_parse_tags (mongoc_uri_t *uri, /* IN */
const char *str) /* IN */
{
const char *end_keyval;
const char *end_key;
bson_t b;
char *keyval;
char *key;
bson_init (&b);
again:
if ((keyval = scan_to_unichar (str, ',', "", &end_keyval))) {
if (!(key = scan_to_unichar (keyval, ':', "", &end_key))) {
bson_free (keyval);
goto fail;
}
bson_append_utf8 (&b, key, -1, end_key + 1, -1);
bson_free (key);
bson_free (keyval);
str = end_keyval + 1;
goto again;
} else if ((key = scan_to_unichar (str, ':', "", &end_key))) {
bson_append_utf8 (&b, key, -1, end_key + 1, -1);
bson_free (key);
} else if (strlen (str)) {
/* we're not finished but we couldn't parse the string */
goto fail;
}
mongoc_read_prefs_add_tag (uri->read_prefs, &b);
bson_destroy (&b);
return true;
fail:
MONGOC_WARNING ("Unsupported value for \"" MONGOC_URI_READPREFERENCETAGS
"\": \"%s\"",
str);
bson_destroy (&b);
return false;
}
/*
*--------------------------------------------------------------------------
*
* mongoc_uri_bson_append_or_replace_key --
*
*
* Appends 'option' to the end of 'options' if not already set.
*
* Since we cannot grow utf8 strings inline, we have to allocate a
* temporary bson variable and splice in the new value if the key
* is already set.
*
* NOTE: This function keeps the order of the BSON keys.
*
* NOTE: 'option' is case*in*sensitive.
*
*
*--------------------------------------------------------------------------
*/
static void
mongoc_uri_bson_append_or_replace_key (bson_t *options,
const char *option,
const char *value)
{
bson_iter_t iter;
bool found = false;
if (bson_iter_init (&iter, options)) {
bson_t tmp = BSON_INITIALIZER;
while (bson_iter_next (&iter)) {
const bson_value_t *bvalue;
if (!strcasecmp (bson_iter_key (&iter), option)) {
bson_append_utf8 (&tmp, option, -1, value, -1);
found = true;
continue;
}
bvalue = bson_iter_value (&iter);
BSON_APPEND_VALUE (&tmp, bson_iter_key (&iter), bvalue);
}
if (!found) {
bson_append_utf8 (&tmp, option, -1, value, -1);
}
bson_destroy (options);
bson_copy_to (&tmp, options);
bson_destroy (&tmp);
}
}
bool
mongoc_uri_option_is_int32 (const char *key)
{
return !strcasecmp (key, MONGOC_URI_CONNECTTIMEOUTMS) ||
!strcasecmp (key, MONGOC_URI_HEARTBEATFREQUENCYMS) ||
!strcasecmp (key, MONGOC_URI_SERVERSELECTIONTIMEOUTMS) ||
!strcasecmp (key, MONGOC_URI_SOCKETCHECKINTERVALMS) ||
!strcasecmp (key, MONGOC_URI_SOCKETTIMEOUTMS) ||
!strcasecmp (key, MONGOC_URI_LOCALTHRESHOLDMS) ||
!strcasecmp (key, MONGOC_URI_MAXPOOLSIZE) ||
!strcasecmp (key, MONGOC_URI_MAXSTALENESSSECONDS) ||
!strcasecmp (key, MONGOC_URI_MINPOOLSIZE) ||
!strcasecmp (key, MONGOC_URI_MAXIDLETIMEMS) ||
!strcasecmp (key, MONGOC_URI_WAITQUEUEMULTIPLE) ||
!strcasecmp (key, MONGOC_URI_WAITQUEUETIMEOUTMS) ||
!strcasecmp (key, MONGOC_URI_WTIMEOUTMS) ||
!strcasecmp (key, MONGOC_URI_ZLIBCOMPRESSIONLEVEL);
}
bool
mongoc_uri_option_is_bool (const char *key)
{
return !strcasecmp (key, MONGOC_URI_CANONICALIZEHOSTNAME) ||
!strcasecmp (key, MONGOC_URI_JOURNAL) ||
!strcasecmp (key, MONGOC_URI_RETRYREADS) ||
!strcasecmp (key, MONGOC_URI_RETRYWRITES) ||
!strcasecmp (key, MONGOC_URI_SAFE) ||
!strcasecmp (key, MONGOC_URI_SERVERSELECTIONTRYONCE) ||
!strcasecmp (key, MONGOC_URI_SLAVEOK) ||
!strcasecmp (key, MONGOC_URI_TLS) ||
!strcasecmp (key, MONGOC_URI_TLSINSECURE) ||
!strcasecmp (key, MONGOC_URI_TLSALLOWINVALIDCERTIFICATES) ||
!strcasecmp (key, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES) ||
/* deprecated options */
!strcasecmp (key, MONGOC_URI_SSL) ||
!strcasecmp (key, MONGOC_URI_SSLALLOWINVALIDCERTIFICATES) ||
!strcasecmp (key, MONGOC_URI_SSLALLOWINVALIDHOSTNAMES);
}
bool
mongoc_uri_option_is_utf8 (const char *key)
{
return !strcasecmp (key, MONGOC_URI_APPNAME) ||
!strcasecmp (key, MONGOC_URI_REPLICASET) ||
!strcasecmp (key, MONGOC_URI_READPREFERENCE) ||
!strcasecmp (key, MONGOC_URI_TLSCERTIFICATEKEYFILE) ||
!strcasecmp (key, MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD) ||
!strcasecmp (key, MONGOC_URI_TLSCAFILE) ||
/* deprecated options */
!strcasecmp (key, MONGOC_URI_SSLCLIENTCERTIFICATEKEYFILE) ||
!strcasecmp (key, MONGOC_URI_SSLCLIENTCERTIFICATEKEYPASSWORD) ||
!strcasecmp (key, MONGOC_URI_SSLCERTIFICATEAUTHORITYFILE);
}
const char *
mongoc_uri_canonicalize_option (const char *key)
{
if (!strcasecmp (key, MONGOC_URI_SSL)) {
return MONGOC_URI_TLS;
} else if (!strcasecmp (key, MONGOC_URI_SSLCLIENTCERTIFICATEKEYFILE)) {
return MONGOC_URI_TLSCERTIFICATEKEYFILE;
} else if (!strcasecmp (key, MONGOC_URI_SSLCLIENTCERTIFICATEKEYPASSWORD)) {
return MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD;
} else if (!strcasecmp (key, MONGOC_URI_SSLCERTIFICATEAUTHORITYFILE)) {
return MONGOC_URI_TLSCAFILE;
} else if (!strcasecmp (key, MONGOC_URI_SSLALLOWINVALIDCERTIFICATES)) {
return MONGOC_URI_TLSALLOWINVALIDCERTIFICATES;
} else if (!strcasecmp (key, MONGOC_URI_SSLALLOWINVALIDHOSTNAMES)) {
return MONGOC_URI_TLSALLOWINVALIDHOSTNAMES;
} else {
return key;
}
}
static bool
mongoc_uri_parse_int32 (const char *key, const char *value, int32_t *result)
{
char *endptr;
int64_t i;
errno = 0;
i = bson_ascii_strtoll (value, &endptr, 10);
if (errno || endptr < value + strlen (value)) {
MONGOC_WARNING ("Invalid %s: cannot parse integer\n", key);
return false;
}
if (i > INT32_MAX || i < INT32_MIN) {
MONGOC_WARNING ("Invalid %s: cannot fit in int32\n", key);
return false;
}
*result = (int32_t) i;
return true;
}
static bool
dns_option_allowed (const char *lkey)
{
/* Initial DNS Seedlist Discovery Spec: "A Client MUST only support the
* authSource and replicaSet options through a TXT record, and MUST raise an
* error if any other option is encountered."
*/
return !strcmp (lkey, MONGOC_URI_AUTHSOURCE) ||
!strcmp (lkey, MONGOC_URI_REPLICASET);
}
/* Decompose a key=val pair and place them into a document.
* Includes case-folding for key portion.
*/
static bool
mongoc_uri_split_option (mongoc_uri_t *uri,
bson_t *options,
const char *str,
bool from_dns,
bson_error_t *error)
{
bson_iter_t iter;
const char *end_key;
char *key = NULL;
char *lkey = NULL;
char *value = NULL;
const char *opt;
char *opt_end;
size_t opt_len;
bool ret = false;
if (!(key = scan_to_unichar (str, '=', "", &end_key))) {
MONGOC_URI_ERROR (error, "URI option \"%s\" contains no \"=\" sign", str);
goto CLEANUP;
}
value = bson_strdup (end_key + 1);
mongoc_uri_do_unescape (&value);
if (!value) {
/* do_unescape detected invalid UTF-8 and freed value */
MONGOC_URI_ERROR (
error, "Value for URI option \"%s\" contains invalid UTF-8", key);
goto CLEANUP;
}
lkey = bson_strdup (key);
mongoc_lowercase (key, lkey);
/* Initial DNS Seedlist Discovery Spec: "A Client MUST only support the
* authSource and replicaSet options through a TXT record, and MUST raise an
* error if any other option is encountered."*/
if (from_dns && !dns_option_allowed (lkey)) {
MONGOC_URI_ERROR (
error, "URI option \"%s\" prohibited in TXT record", key);
goto CLEANUP;
}
/* Special case: READPREFERENCETAGS is a composing option.
* Multiple instances should append, not overwrite.
* Encode them directly to the options field,
* bypassing canonicalization and duplicate checks.
*/
if (!strcmp (lkey, MONGOC_URI_READPREFERENCETAGS)) {
if (!mongoc_uri_parse_tags (uri, value)) {
MONGOC_URI_ERROR (
error, "Unsupported value for \"%s\": \"%s\"", key, value);
goto CLEANUP;
}
} else if (bson_iter_init_find (&iter, &uri->raw, lkey) ||
bson_iter_init_find (&iter, options, lkey)) {
/* Special case, MONGOC_URI_W == "any non-int" is not overridden
* by later values.
*/
if (!strcmp (lkey, MONGOC_URI_W) &&
(opt = bson_iter_utf8_unsafe (&iter, &opt_len))) {
strtol (opt, &opt_end, 10);
if (*opt_end != '\0') {
ret = true;
goto CLEANUP;
}
}
/* Initial DNS Seedlist Discovery Spec: "Client MUST use options
* specified in the Connection String to override options provided
* through TXT records." So, do NOT override existing options with TXT
* options. */
if (from_dns) {
MONGOC_WARNING (
"Cannot override URI option \"%s\" from TXT record \"%s\"",
key,
str);
ret = true;
goto CLEANUP;
}
MONGOC_WARNING ("Overwriting previously provided value for '%s'", key);
}
if (!(strcmp (lkey, MONGOC_URI_REPLICASET)) && *value == '\0') {
MONGOC_URI_ERROR (
error, "Value for URI option \"%s\" cannot be empty string", lkey);
goto CLEANUP;
}
mongoc_uri_bson_append_or_replace_key (options, lkey, value);
ret = true;
CLEANUP:
bson_free (key);
bson_free (lkey);
bson_free (value);
return ret;
}
/* Check for canonical/deprecated conflicts
* between the option list a, and b.
* If both names exist either way with differing values, error.
*/
static bool
mongoc_uri_options_validate_names (const bson_t *a,
const bson_t *b,
bson_error_t *error)
{
bson_iter_t key_iter, canon_iter;
const char *key = NULL;
const char *canon = NULL;
const char *value = NULL;
const char *cval = NULL;
size_t value_len = 0;
size_t cval_len = 0;
/* Scan `a` looking for deprecated names
* where the canonical name was also used in `a`,
* or was used in `b`. */
bson_iter_init (&key_iter, a);
while (bson_iter_next (&key_iter)) {
key = bson_iter_key (&key_iter);
value = bson_iter_utf8_unsafe (&key_iter, &value_len);
canon = mongoc_uri_canonicalize_option (key);
if (key == canon) {
/* Canonical form, no point checking `b`. */
continue;
}
/* Check for a conflict in `a`. */
if (bson_iter_init_find (&canon_iter, a, canon)) {
cval = bson_iter_utf8_unsafe (&canon_iter, &cval_len);
if ((value_len != cval_len) || strcmp (value, cval)) {
goto HANDLE_CONFLICT;
}
}
/* Check for a conflict in `b`. */
if (bson_iter_init_find (&canon_iter, b, canon)) {
cval = bson_iter_utf8_unsafe (&canon_iter, &cval_len);
if ((value_len != cval_len) || strcmp (value, cval)) {
goto HANDLE_CONFLICT;
}
}
}
return true;
HANDLE_CONFLICT:
MONGOC_URI_ERROR (error,
"Deprecated option '%s=%s' conflicts with "
"canonical name '%s=%s'",
key,
value,
canon,
cval);
return false;
}