-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnss-tlsd.c
1303 lines (1109 loc) · 33.5 KB
/
nss-tlsd.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
/*
* This file is part of nss-tls.
*
* Copyright (C) 2018, 2019, 2020 Dima Krasner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <pwd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grp.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <netinet/in.h>
#include <resolv.h>
#include <paths.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include <gmodule.h>
#include <libsoup/soup.h>
#include "nss-tls.h"
#define CACHE_CLEANUP_INTERVAL 5
#define FALLBACK_TTL (60 * 1000000)
#define MIN_TTL 10
#define CACHE_SIZE 1024
#define MAX_CONNS_PER_RESOLVER 10
#define MAX_RESOLVERS 16
#define MAX_REQ_SIZE 512
enum nss_tls_methods {
NSS_TLS_METHOD_POST,
NSS_TLS_METHOD_MIN = NSS_TLS_METHOD_POST,
NSS_TLS_METHOD_GET,
NSS_TLS_METHOD_MAX,
NSS_TLS_METHOD_RANDOM
};
struct nss_tls_session {
unsigned char dns[UINT16_MAX];
struct nss_tls_req request;
struct nss_tls_res response;
gint64 type;
GSocketConnection *connection;
SoupMessage *message;
gboolean canon;
};
static SoupSession *soup = NULL;
static struct {
SoupURI *uri;
gchar *url;
const gchar *domain;
enum nss_tls_methods method;
} resolvers[MAX_RESOLVERS];
static gint32 nresolvers = 0;
static gboolean cache = FALSE;
static gboolean randomize = FALSE;
static GHashTable *caches[2] = {NULL, NULL};
static GFile *cfg_file = NULL, *resolv_conf = NULL;
static GFileMonitor *cfg_monitor = NULL, *resolv_conf_monitor = NULL;
static
gboolean
check_ttl (gpointer key,
gpointer value,
gpointer user_data)
{
const gchar *name = (const gchar *)key;
struct nss_tls_res *res = (struct nss_tls_res *)value;
gint64 now = *(gint64 *)user_data;
if (now > res->expiry) {
g_debug ("Cache for %s has expired", name);
return TRUE;
}
return FALSE;
}
static
gboolean
on_cache_cleanup (gpointer user_data)
{
gint64 now;
gint i;
now = g_get_monotonic_time ();
for (i = 0; i < G_N_ELEMENTS (caches); ++i) {
g_hash_table_foreach_remove (caches[i], check_ttl, &now);
}
return TRUE;
}
static
GHashTable *
choose_cache (const int af)
{
if (af == AF_INET) {
return caches[0];
}
return caches[1];
}
static
void
add_to_cache (const struct nss_tls_req *req, struct nss_tls_res *res)
{
gchar *key;
struct nss_tls_res *val;
gint64 now;
GHashTable *cache;
cache = choose_cache (req->af);
if (!cache || (g_hash_table_size (cache) >= CACHE_SIZE)) {
return;
}
if (res->expiry == -1) {
now = g_get_monotonic_time ();
if (now > INT64_MAX - FALLBACK_TTL) {
return;
}
res->expiry = now + FALLBACK_TTL;
}
key = g_strdup (req->name);
val = g_memdup (res, sizeof (*res));
g_hash_table_insert (cache, key, val);
g_debug ("Caching %s until %"G_GINT64_FORMAT, req->name, res->expiry);
}
static
const struct nss_tls_res *
query_cache (const int af, const char *name)
{
gpointer res = NULL;
GHashTable *cache;
cache = choose_cache (af);
if (cache) {
res = g_hash_table_lookup (cache, name);
if (res) {
g_debug ("Found %s in the cache", name);
}
}
return (const struct nss_tls_res *)res;
}
static
gboolean
get_cached_response (struct nss_tls_session *session)
{
const struct nss_tls_res *res, *cres;
res = query_cache (session->request.af, session->request.name);
if (!res) {
return FALSE;
}
if (res->cname[0]) {
cres = query_cache (session->request.af, res->cname);
if (cres) {
memcpy (session->response.addrs,
cres->addrs,
cres->count * sizeof(cres->addrs[0]));
session->response.count = cres->count;
session->response.expiry = cres->expiry;
strcpy (session->response.cname, res->cname);
return TRUE;
}
}
memcpy (&session->response, res, sizeof (session->response));
return TRUE;
}
static
void
on_response (GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static
void
on_sent (GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static
gchar *
encode_dns_query (const unsigned char *buf, const gsize len)
{
gchar *b64;
size_t i;
b64 = g_base64_encode (buf, len);
/* https://tools.ietf.org/html/rfc4648#section-5 */
for (i = 0; i < strlen (b64); ++i) {
switch (b64[i]) {
case '+':
b64[i] = '-';
break;
case '/':
b64[i] = '_';
break;
case '=':
b64[i] = '\0';
return b64;
}
}
return b64;
}
static
gboolean
resolve_domain (struct nss_tls_session *session)
{
static unsigned char sbuf[MAX_REQ_SIZE];
unsigned char *buf = sbuf;
g_autofree gchar *url = NULL, *dns = NULL;
GOutputStream *out;
int type, len;
SoupMessageFlags flags;
guint id = 0;
gint method;
if (get_cached_response (session)) {
out = g_io_stream_get_output_stream (G_IO_STREAM (session->connection));
g_output_stream_write_all_async (out,
&session->response,
sizeof (session->response),
G_PRIORITY_DEFAULT,
NULL,
on_sent,
session);
return TRUE;
}
if (nresolvers == 0) {
return FALSE;
}
switch (session->request.af) {
case AF_INET:
type = ns_t_a;
break;
case AF_INET6:
type = ns_t_aaaa;
break;
default:
return FALSE;
}
if (randomize) {
id = g_random_int_range (0, nresolvers);
} else if (nresolvers > 1) {
id = g_str_hash (session->request.name) % nresolvers;
}
if (resolvers[id].method == NSS_TLS_METHOD_RANDOM) {
method = g_random_int_range (NSS_TLS_METHOD_MIN, NSS_TLS_METHOD_MAX);
} else {
method = (gint)resolvers[id].method;
}
if (method == NSS_TLS_METHOD_POST) {
buf = g_malloc (MAX_REQ_SIZE);
}
len = res_mkquery (QUERY,
session->request.name,
ns_c_in,
type,
NULL,
0,
NULL,
buf,
MAX_REQ_SIZE);
if (len <= 1) {
if (method == NSS_TLS_METHOD_POST) {
g_free (buf);
}
return FALSE;
}
/*
* always use 0 for the transaction ID, to improve the server's cache hit
* rate
*/
buf[0] = buf[1] = 0;
if (nresolvers > 1) {
g_debug ("Resolving %s (%s) using %s",
session->request.name,
(session->request.af == AF_INET) ? "IPv4" : "IPv6",
resolvers[id].url);
} else {
g_debug ("Resolving %s (%s)",
session->request.name,
(session->request.af == AF_INET) ? "IPv4" : "IPv6");
}
session->response.cname[0] = '\0';
session->type = (gint64)type;
if (method == NSS_TLS_METHOD_POST) {
session->message = soup_message_new ("POST", resolvers[id].url);
} else {
dns = encode_dns_query (buf, (gsize)len);
url = g_strdup_printf ("%s?dns=%s", resolvers[id].url, dns);
session->message = soup_message_new ("GET", url);
}
flags = soup_message_get_flags (session->message);
soup_message_set_flags (session->message, flags | SOUP_MESSAGE_IDEMPOTENT);
if (method == NSS_TLS_METHOD_POST) {
soup_message_set_request (session->message,
"application/dns-message",
SOUP_MEMORY_TAKE,
(const char *)buf,
(gsize)len);
}
soup_message_headers_append (session->message->request_headers,
"Accept",
"application/dns-message");
soup_session_send_async (soup,
session->message,
NULL,
on_response,
session);
return TRUE;
}
static
void
resolve_cname (struct nss_tls_session *session)
{
g_debug ("The canonical name of %s is %s",
session->request.name,
session->response.cname);
strcpy (session->request.name, session->response.cname);
/*
* ignore CNAME records for this name; we don't want to be stuck in an
* infinite loop if the canonical form of A is B, while B has a CNAME record
* that points back to A
*/
session->canon = TRUE;
resolve_domain (session);
}
static
void
on_close (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
struct nss_tls_session *session = (struct nss_tls_session *)user_data;
g_io_stream_close_finish (G_IO_STREAM (source_object), res, NULL);
g_object_unref (session->connection);
g_free (session);
}
static
void
stop_session (struct nss_tls_session *session)
{
g_io_stream_close_async (G_IO_STREAM (session->connection),
G_PRIORITY_DEFAULT,
NULL,
on_close,
session);
}
/* step 4: we're done sending the response to libnss_tls */
static
void
on_sent (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
struct nss_tls_session *session = (struct nss_tls_session *)user_data;
gsize out;
g_output_stream_write_all_finish (G_OUTPUT_STREAM (source_object),
res,
&out,
NULL);
stop_session (session);
}
static
void
on_answer (struct nss_tls_session *session,
const unsigned char *dns,
const gsize len,
ns_msg *msg,
const int rr_id,
const int a_type,
const size_t addrlen)
{
ns_rr rr;
gint64 ttl, now, expiry;
int type;
if (ns_parserr (msg, ns_s_an, rr_id, &rr) < 0) {
g_warning ("Failed to parse a result record for %s",
session->request.name);
return;
}
if (ns_rr_class (rr) != ns_c_in)
return;
type = ns_rr_type (rr);
if (type == a_type) {
if (ns_rr_rdlen (rr) == addrlen) {
memcpy (&session->response.addrs[session->response.count],
ns_rr_rdata (rr),
addrlen);
++session->response.count;
ttl = (gint64)ns_rr_ttl (rr);
if (ttl <= INT64_MAX / 1000000) {
if (ttl < MIN_TTL)
ttl = MIN_TTL;
ttl *= 1000000;
now = g_get_monotonic_time ();
/*
* after looking at all answer records, we use the shortest
* TTL for all answers
*/
if (INT64_MAX - ttl >= now) {
expiry = (int64_t)(now + ttl);
if ((session->response.expiry == -1) ||
(expiry < session->response.expiry)) {
session->response.expiry = expiry;
}
}
}
}
}
else if (!session->canon &&
(type == ns_t_cname) &&
!session->response.cname[0] &&
(ns_rr_rdlen (rr) > 0)) {
if (dn_expand (dns,
dns + len,
ns_rr_rdata (rr),
session->response.cname,
sizeof (session->response.cname)) <= 0) {
session->response.cname[0] = '\0';
}
}
}
static
void
on_body (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
ns_msg msg;
g_autoptr(GError) err = NULL;
struct nss_tls_session *session = (struct nss_tls_session *)user_data;
GOutputStream *out;
gsize len;
size_t addrlen;
int id, count, a_type;
if (!g_input_stream_read_all_finish (G_INPUT_STREAM (source_object),
res,
&len,
&err) ||
!len) {
goto cleanup;
}
switch (session->request.af) {
case AF_INET:
a_type = ns_t_a;
addrlen = sizeof (session->response.addrs[0].in);
break;
case AF_INET6:
a_type = ns_t_aaaa;
addrlen = sizeof (session->response.addrs[0].in6);
break;
default:
goto cleanup;
}
if (ns_initparse (session->dns, (int)len, &msg) < 0) {
g_warning ("Failed to parse the result for %s", session->request.name);
goto cleanup;
}
count = ns_msg_count(msg, ns_s_an);
for (id = 0;
((id < count) &&
((session->response.count < G_N_ELEMENTS (session->response.addrs)) ||
!session->response.cname[0]));
++id) {
on_answer (session, session->dns, len, &msg, id, a_type, addrlen);
}
/* we want to cache addresses or the lack of any addresses */
add_to_cache (&session->request, &session->response);
if (session->response.cname[0] && (session->response.count == 0)) {
resolve_cname (session);
return;
}
out = g_io_stream_get_output_stream (G_IO_STREAM (session->connection));
g_output_stream_write_all_async (out,
&session->response,
sizeof (session->response),
G_PRIORITY_DEFAULT,
NULL,
on_sent,
session);
g_debug ("Done resolving %s with %hhu %s result(s)",
session->request.name,
session->response.count,
(session->request.af == AF_INET) ? "IPv4" : "IPv6");
return;
cleanup:
if (session->response.count == 0) {
stop_session (session);
}
}
/* step 3: we received the HTTPS response, parse it to construct our response
* and send it to libnss_tls */
static
void
on_response (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
g_autoptr(GError) err = NULL;
struct nss_tls_session *session = (struct nss_tls_session *)user_data;
g_autoptr(GInputStream) in = NULL;
const char *type;
in = soup_session_send_finish (SOUP_SESSION (source_object),
res,
&err);
if (!in) {
if (err) {
g_warning ("Failed to query %s: %s",
session->request.name,
err->message);
}
else {
g_warning ("Failed to query %s", session->request.name);
}
goto cleanup;
}
if (!SOUP_STATUS_IS_SUCCESSFUL (session->message->status_code)) {
g_warning ("Failed to query %s: HTTP %d",
session->request.name,
session->message->status_code);
goto cleanup;
}
type = soup_message_headers_get_content_type (
session->message->response_headers,
NULL
);
if (type && strcmp (type, "application/dns-message")) {
g_warning ("Bad response type for %s: %s",
session->request.name,
type);
goto cleanup;
}
g_input_stream_read_all_async (in,
session->dns,
sizeof (session->dns),
G_PRIORITY_DEFAULT,
NULL,
on_body,
session);
g_object_unref (session->message);
return;
cleanup:
g_object_unref (session->message);
if (session->response.count == 0) {
stop_session (session);
}
}
/*
* we don't want to leak the local domain to the DoH server provider (for
* example, it may indicate a router model) and we don't want to waste time on
* this query if it's going to fail anyway
*/
static
gboolean
is_suffixed (const gchar *name)
{
struct __res_state res;
gchar *suffix;
gboolean ret;
guint i;
if (res_ninit (&res) < 0) {
return FALSE;
}
for (i = 0; (i < G_N_ELEMENTS (res.dnsrch)) && res.dnsrch[i]; ++i) {
suffix = g_strconcat (".", res.dnsrch[i], NULL);
ret = g_str_has_suffix (name, suffix);
g_free (suffix);
if (ret) {
g_debug ("%s is suffixed by a local domain", name);
return ret;
}
}
return FALSE;
}
/*
* the DoH server addresses must be resolved by other means, otherwise this
* results in infinite recursion
*/
static
gboolean
is_server_domain (const gchar *name)
{
gint i;
for (i = 0; i < nresolvers; ++i) {
if (strcmp (name, resolvers[i].domain) == 0) {
g_debug ("%s is a DoH server domain", name);
return TRUE;
}
}
return FALSE;
}
/* step 2: we received a request from libnss_tls and send a HTTPS request */
static
void
on_request (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
g_autoptr(GError) err = NULL;
struct nss_tls_session *session = user_data;
gsize len;
if (!g_input_stream_read_all_finish (G_INPUT_STREAM (source_object),
res,
&len,
&err)) {
if (err) {
g_warning ("Failed to receive a request: %s", err->message);
}
else {
g_warning ("Failed to receive a request");
}
goto fail;
}
if (len != sizeof (session->request)) {
g_debug ("Bad request");
goto fail;
}
session->request.name[sizeof (session->request.name) - 1] = '\0';
if (is_suffixed (session->request.name) ||
is_server_domain (session->request.name)) {
goto fail;
}
if (resolve_domain (session)) {
return;
}
fail:
stop_session (session);
}
/* step 1: we accept a new connection from libnss_tls and wait for it to send a
* request */
static
void
on_connection (GSocketService *service,
GSocketConnection *connection,
GObject *source_object,
gpointer user_data)
{
GSocket *s;
struct nss_tls_session *session;
GInputStream *in;
/* we disconnect the client after NSS_TLS_TIMEOUT seconds */
s = g_socket_connection_get_socket (connection);
g_socket_set_timeout (s, NSS_TLS_TIMEOUT);
session = g_new0 (struct nss_tls_session, 1);
session->connection = g_object_ref (connection);
session->response.count = 0;
session->response.expiry = -1;
/* we assume the domain is not canonical */
session->canon = FALSE;
in = g_io_stream_get_input_stream (G_IO_STREAM (connection));
/* read the incoming request */
g_input_stream_read_all_async (in,
&session->request,
sizeof (session->request),
G_PRIORITY_DEFAULT,
NULL,
on_request,
session);
}
static
gboolean
on_term (gpointer user_data)
{
g_main_loop_quit ((GMainLoop *)user_data);
return FALSE;
}
static
void
parse_cfg (const gboolean root);
static
void
update_cfg (const gboolean root)
{
gint pnresolvers = nresolvers, i;
if (cfg_monitor) {
g_object_unref (cfg_monitor);
cfg_monitor = NULL;
g_object_unref (cfg_file);
}
if (resolv_conf_monitor) {
g_object_unref (resolv_conf_monitor);
resolv_conf_monitor = NULL;
g_object_unref (resolv_conf);
}
parse_cfg (root);
for (i = 0; i < pnresolvers; ++i) {
soup_uri_free (resolvers[i].uri);
g_free (resolvers[i].url);
}
nresolvers -= pnresolvers;
memcpy (resolvers,
&resolvers[pnresolvers],
sizeof (resolvers[0]) * nresolvers);
}
static
void
on_cfg_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
gboolean root = (gboolean)(gintptr)user_data;
if ((event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) &&
(event_type != G_FILE_MONITOR_EVENT_DELETED)) {
return;
}
update_cfg (root);
}
static
void
watch_file (const gchar *path,
const gboolean root,
GFile **file,
GFileMonitor **mon)
{
*file = g_file_new_for_path (path);
*mon = g_file_monitor_file (*file,
G_FILE_MONITOR_NONE,
NULL,
NULL);
if (*mon) {
g_signal_connect (*mon,
"changed", G_CALLBACK (on_cfg_changed),
(gpointer)(gintptr)root);
} else {
g_object_unref (*file);
*file = NULL;
}
}
static
void
watch_cfg (const gchar *path,
const gboolean root)
{
return watch_file (path,
root,
&cfg_file,
&cfg_monitor);
}
static
void
watch_resolv_conf (const gchar *path,
const gboolean root)
{
return watch_file (path,
root,
&resolv_conf,
&resolv_conf_monitor);
}
static
void
add_resolver (const char *addr)
{
if (strchr (addr, ':')) {
resolvers[nresolvers].url = g_strconcat ("https://[", addr, "]/dns-query", NULL);
} else {
resolvers[nresolvers].url = g_strconcat ("https://", addr, "/dns-query", NULL);
}
resolvers[nresolvers].uri = soup_uri_new (resolvers[nresolvers].url);
resolvers[nresolvers].domain = soup_uri_get_host (resolvers[nresolvers].uri);
resolvers[nresolvers].method = NSS_TLS_METHOD_POST;
++nresolvers;
}
static
gchar *
find_resolv_conf (void)
{
g_autofree gchar *rpath = NULL;
#ifdef NSS_TLS_SYSTEMD
g_autofree gchar *base = NULL, *dir = NULL;
#endif
gchar *tmp;
rpath = g_file_read_link (_PATH_RESCONF, NULL);
if (!rpath) {
return g_strdup (_PATH_RESCONF);
}
#ifdef NSS_TLS_SYSTEMD
/*
* hack for systems with systemd: systemd-resolved hijacks
* /etc/resolv.conf by replacing it with a symlink to
* /run/systemd/resolve/stub-resolv.conf and the actual DNS servers
* are specified in /run/systemd/resolve/resolv.conf
*/
base = g_path_get_basename (rpath);
if (strcmp (base, "stub-resolv.conf") == 0) {
dir = g_path_get_dirname (rpath);
return g_build_filename (dir, "resolv.conf", NULL);
}
#endif
tmp = rpath;
rpath = NULL;
return tmp;
}
static
void
use_dns_servers (const gchar *path)
{
GFile *file;
GFileInputStream *fio;
GDataInputStream *in;
char *line;
g_debug ("Parsing %s", path);
if (nresolvers >= G_N_ELEMENTS (resolvers)) {
return;
}
file = g_file_new_for_path (path);
if (!file) {
return;
}
fio = g_file_read (file, NULL, NULL);
if (!fio) {
g_object_unref (file);
return;
}
in = g_data_input_stream_new (G_INPUT_STREAM (fio));
if (in) {
do {
line = g_data_input_stream_read_line (in, NULL, NULL, NULL);
if (!line) {
break;
}
if (g_str_has_prefix (line, "nameserver ")) {
add_resolver (&line[sizeof("nameserver ") - 1]);
if (nresolvers >= G_N_ELEMENTS (resolvers)) {
g_free (line);
break;
}
}
g_free (line);
} while (1);
g_object_unref (in);
}
g_object_unref (file);
g_object_unref (fio);
}
static
void
parse_cfg (const gboolean root)
{
GValue val = G_VALUE_INIT;
const gchar *dirs[3] = {NULL, NULL, NULL};
g_autofree gchar *user_dir = NULL;
gchar **list, **p;
g_autofree gchar *path = NULL, *resconf = NULL;
char *plus;