/
netaddr.c
2301 lines (1852 loc) · 57.6 KB
/
netaddr.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
/*
* ProFTPD - FTP server daemon
* Copyright (c) 2003-2014 The ProFTPD Project team
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, The ProFTPD Project team and other respective
* copyright holders give permission to link this program with OpenSSL, and
* distribute the resulting executable, without including the source code for
* OpenSSL in the source distribution.
*/
/* Network address routines
* $Id: netaddr.c,v 1.98 2013-12-23 17:53:42 castaglia Exp $
*/
#include "conf.h"
#if HAVE_NET_IF_H
# include <net/if.h>
#endif
#if HAVE_IFADDRS_H
# include <ifaddrs.h>
#endif
/* Define an IPv4 equivalent of the IN6_IS_ADDR_LOOPBACK macro. */
#undef IN_IS_ADDR_LOOPBACK
#define IN_IS_ADDR_LOOPBACK(a) \
((((unsigned long int) ntohl((a)->s_addr)) & 0xff000000) == 0x7f000000)
static pr_netaddr_t sess_local_addr;
static int have_sess_local_addr = FALSE;
static pr_netaddr_t sess_remote_addr;
static char sess_remote_name[PR_TUNABLE_BUFFER_SIZE];
static int have_sess_remote_addr = FALSE;
/* Do reverse DNS lookups? */
static int reverse_dns = 1;
/* Use IPv6? */
#ifdef PR_USE_IPV6
static int use_ipv6 = TRUE;
#else
static int use_ipv6 = FALSE;
#endif /* PR_USE_IPV6 */
static char localaddr_str[PR_TUNABLE_BUFFER_SIZE];
static int have_localaddr_str = FALSE;
static pool *netaddr_pool = NULL;
static pr_table_t *netaddr_iptab = NULL;
static pr_table_t *netaddr_dnstab = NULL;
static const char *trace_channel = "dns";
/* Netaddr cache management */
static array_header *netaddr_dnscache_get(pool *p, const char *ip_str) {
array_header *res = NULL;
if (netaddr_dnstab) {
void *v = pr_table_get(netaddr_dnstab, ip_str, NULL);
if (v) {
res = v;
pr_trace_msg(trace_channel, 4,
"using %d DNS %s from netaddr DNS cache for IP address '%s'",
res->nelts, res->nelts != 1 ? "names" : "name", ip_str);
if (p) {
/* If the caller provided a pool, return a copy of the array. */
return copy_array_str(p, res);
}
return res;
}
}
pr_trace_msg(trace_channel, 12,
"no DNS names found in netaddr DNS cache for IP address '%s'", ip_str);
errno = ENOENT;
return NULL;
}
static void netaddr_dnscache_set(const char *ip_str, const char *dns_name) {
if (netaddr_dnstab) {
void *v = NULL;
array_header *res = NULL;
int add_list = FALSE;
res = netaddr_dnscache_get(NULL, ip_str);
if (res == NULL) {
/* No existing entries for this IP address yet. */
res = make_array(netaddr_pool, 1, sizeof(char *));
add_list = TRUE;
} else {
register unsigned int i;
char **names;
/* Check for duplicates. */
names = res->elts;
for (i = 0; i < res->nelts; i++) {
if (names[i] != NULL) {
if (strcmp(names[i], dns_name) == 0) {
pr_trace_msg(trace_channel, 5,
"DNS name '%s' for IP address '%s' already stashed in the "
"netaddr DNS cache", dns_name, ip_str);
return;
}
}
}
}
*((char **) push_array(res)) = pstrdup(netaddr_pool, dns_name);
v = res;
if (add_list) {
if (pr_table_add(netaddr_dnstab, pstrdup(netaddr_pool, ip_str), v,
sizeof(array_header *)) < 0) {
pr_trace_msg(trace_channel, 3,
"error adding DNS name '%s' for IP address '%s' to the netaddr "
"DNS cache: %s", dns_name, ip_str, strerror(errno));
} else {
pr_trace_msg(trace_channel, 5,
"stashed DNS name '%s' for IP address '%s' in the netaddr DNS cache",
dns_name, ip_str);
}
} else {
pr_trace_msg(trace_channel, 5,
"stashed DNS name '%s' for IP address '%s' in the netaddr DNS cache",
dns_name, ip_str);
}
}
return;
}
static pr_netaddr_t *netaddr_ipcache_get(pool *p, const char *name) {
pr_netaddr_t *res = NULL;
if (netaddr_iptab) {
void *v = pr_table_get(netaddr_iptab, name, NULL);
if (v) {
res = v;
pr_trace_msg(trace_channel, 4,
"using IP address '%s' from netaddr IP cache for name '%s'",
pr_netaddr_get_ipstr(res), name);
/* We return a copy of the cache's netaddr_t, if the caller provided
* a pool for duplication.
*/
if (p) {
pr_netaddr_t *dup_res = NULL;
dup_res = pr_netaddr_dup(p, res);
if (dup_res == NULL) {
pr_log_debug(DEBUG0, "error duplicating address for name '%s' "
"from cache: %s", name, strerror(errno));
}
return dup_res;
}
return res;
}
}
pr_trace_msg(trace_channel, 2,
"no IP address found in netaddr IP cache for name '%s'", name);
errno = ENOENT;
return NULL;
}
static int netaddr_ipcache_set(const char *name, pr_netaddr_t *na) {
if (netaddr_iptab) {
int count = 0;
void *v = NULL;
/* We store an internal copy of the netaddr_t in the cache. */
v = pr_netaddr_dup(netaddr_pool, na);
if (v == NULL) {
return -1;
}
count = pr_table_exists(netaddr_iptab, name);
if (count <= 0) {
if (pr_table_add(netaddr_iptab, pstrdup(netaddr_pool, name), v,
sizeof(pr_netaddr_t *)) < 0) {
pr_trace_msg(trace_channel, 3,
"error adding IP address '%s' for name '%s' to the netaddr "
"IP cache: %s", pr_netaddr_get_ipstr(na), name,
strerror(errno));
} else {
pr_trace_msg(trace_channel, 5,
"stashed IP address '%s' for name '%s' in the netaddr IP cache",
pr_netaddr_get_ipstr(v), name);
}
} else {
if (pr_table_set(netaddr_iptab, pstrdup(netaddr_pool, name), v,
sizeof(pr_netaddr_t *)) < 0) {
pr_trace_msg(trace_channel, 3,
"error setting IP address '%s' for name '%s' in the netaddr "
"IP cache: %s", pr_netaddr_get_ipstr(na), name, strerror(errno));
}
}
}
return 0;
}
/* Provide replacements for needed functions. */
#if !defined(HAVE_GETNAMEINFO) || defined(PR_USE_GETNAMEINFO)
int pr_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host,
size_t hostlen, char *serv, size_t servlen, int flags) {
struct sockaddr_in *sai = (struct sockaddr_in *) sa;
if (!sai || sai->sin_family != AF_INET)
return EAI_FAMILY;
if (serv != NULL && servlen > (size_t) 1)
snprintf(serv, servlen, "%lu", (unsigned long) ntohs(sai->sin_port));
if (host != NULL && hostlen > (size_t) 1) {
struct hostent *he = NULL;
if ((flags & NI_NUMERICHOST) == 0 &&
(he = gethostbyaddr((const char *) &(sai->sin_addr),
sizeof(sai->sin_addr), AF_INET)) != NULL &&
he->h_name != NULL &&
*he->h_name != 0) {
if (strlen(he->h_name) >= hostlen)
goto handle_numeric_ip;
sstrncpy(host, he->h_name, hostlen);
} else {
char *ipstr = NULL;
handle_numeric_ip:
ipstr = inet_ntoa(sai->sin_addr);
if (ipstr == NULL)
return EAI_SYSTEM;
if (strlen(ipstr) >= hostlen)
return EAI_FAIL;
sstrncpy(host, ipstr, hostlen);
}
}
return 0;
}
#endif /* HAVE_GETNAMEINFO or PR_USE_GETNAMEINFO */
#if !defined(HAVE_GETADDRINFO) || defined(PR_USE_GETADDRINFO)
int pr_getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res) {
struct addrinfo *ans = NULL;
struct sockaddr_in *saddr = NULL;
const char *proto_name = "tcp";
int socktype = SOCK_STREAM;
unsigned short port = 0;
if (!res)
return EAI_FAIL;
*res = NULL;
ans = malloc(sizeof(struct addrinfo));
if (ans == NULL)
return EAI_MEMORY;
saddr = malloc(sizeof(struct sockaddr_in));
if (saddr == NULL) {
free(ans);
return EAI_MEMORY;
}
ans->ai_family = AF_INET;
ans->ai_addrlen = sizeof *saddr;
ans->ai_addr = (struct sockaddr *) saddr;
ans->ai_next = NULL;
memset(saddr, 0, sizeof(*saddr));
saddr->sin_family = AF_INET;
if (hints != NULL) {
struct protoent *pe = NULL;
if ((pe = getprotobynumber(hints->ai_protocol)) != NULL &&
pe->p_name != NULL &&
*pe->p_name != 0)
proto_name = pe->p_name;
if (hints->ai_socktype != 0) {
socktype = hints->ai_socktype;
} else if (strncasecmp(proto_name, "udp", 4) == 0) {
socktype = SOCK_DGRAM;
}
}
if (service != NULL) {
struct servent *se = NULL;
if ((se = getservbyname(service, proto_name)) != NULL &&
se->s_port > 0)
port = se->s_port;
else if ((port = (unsigned short) strtoul(service, NULL, 0)) <= 0 ||
port > 65535)
port = 0;
}
if (hints != NULL &&
(hints->ai_flags & AI_PASSIVE) != 0)
saddr->sin_addr.s_addr = htonl(INADDR_ANY);
if (node != NULL) {
struct hostent *he = NULL;
if ((he = gethostbyname(node)) != NULL &&
he->h_addr_list != NULL &&
he->h_addr_list[0] != NULL &&
he->h_length > 0 &&
he->h_length <= (int) sizeof(saddr->sin_addr))
memcpy(&saddr->sin_addr, he->h_addr_list[0], he->h_length);
}
ans->ai_socktype = socktype;
saddr->sin_port = htons(port);
*res = ans;
return 0;
}
void pr_freeaddrinfo(struct addrinfo *ai) {
if (!ai)
return;
if (ai->ai_addr != NULL) {
free(ai->ai_addr);
ai->ai_addr = NULL;
}
free(ai);
}
#endif /* HAVE_GETADDRINFO or PR_USE_GETADDRINFO */
#if !defined(HAVE_INET_NTOP)
const char *pr_inet_ntop(int af, const void *src, char *dst, size_t len) {
char *res;
if (af != AF_INET) {
errno = EAFNOSUPPORT;
return NULL;
}
res = inet_ntoa(*((struct in_addr *) src));
if (res == NULL)
return NULL;
memcpy(dst, res, len);
return dst;
}
#endif /* !HAVE_INET_NTOP */
#if !defined(HAVE_INET_PTON)
int pr_inet_pton(int af, const char *src, void *dst) {
unsigned long res;
if (af != AF_INET) {
errno = EAFNOSUPPORT;
return -1;
}
/* inet_aton(3) would be better. However, it is not ubiquitous. */
res = inet_addr(src);
if (res == INADDR_NONE ||
res == 0)
return 0;
memcpy(dst, &res, sizeof(res));
return 1;
}
#endif /* !HAVE_INET_PTON */
static void *get_v4inaddr(const pr_netaddr_t *na) {
/* This function is specifically for IPv4 clients (when gethostbyname2(2) is
* present) that have an IPv4-mapped IPv6 address, when performing reverse
* DNS checks. This function is called iff the given netaddr object is
* indeed an IPv4-mapped IPv6 address. IPv6 address have 128 bits in their
* sin6_addr field. For IPv4-mapped IPv6 addresses, the relevant 32 bits
* are the last of those 128 bits (or, alternatively, the last 4 bytes of
* those 16 bytes); hence the read of 12 bytes after the start of the
* sin6_addr pointer.
*/
return (((char *) pr_netaddr_get_inaddr(na)) + 12);
}
/* Validate anything returned from the 'outside', since it's untrusted
* information.
*/
char *pr_netaddr_validate_dns_str(char *buf) {
char *p;
if (buf == NULL) {
errno = EINVAL;
return NULL;
}
/* Validate anything returned from a DNS. */
for (p = buf; p && *p; p++) {
/* Per RFC requirements, these are all that are valid from a DNS. */
if (!PR_ISALNUM(*p) &&
*p != '.' &&
*p != '-'
#ifdef PR_USE_IPV6
&& *p != ':'
#endif /* PR_USE_IPV6 */
) {
/* We set it to _ because we know that's an invalid, yet safe, option
* for a DNS entry.
*/
*p = '_';
}
}
return buf;
}
int pr_netaddr_set_reverse_dns(int enable) {
int old_enable = reverse_dns;
reverse_dns = enable;
return old_enable;
}
pr_netaddr_t *pr_netaddr_alloc(pool *p) {
if (!p) {
errno = EINVAL;
return NULL;
}
return pcalloc(p, sizeof(pr_netaddr_t));
}
void pr_netaddr_clear(pr_netaddr_t *na) {
if (!na)
return;
memset(na, 0, sizeof(pr_netaddr_t));
}
pr_netaddr_t *pr_netaddr_dup(pool *p, pr_netaddr_t *na) {
pr_netaddr_t *dup_na;
if (!p || !na) {
errno = EINVAL;
return NULL;
}
dup_na = pr_netaddr_alloc(p);
if (pr_netaddr_set_family(dup_na, pr_netaddr_get_family(na)) < 0) {
return NULL;
}
pr_netaddr_set_sockaddr(dup_na, pr_netaddr_get_sockaddr(na));
if (na->na_have_ipstr) {
sstrncpy(dup_na->na_ipstr, na->na_ipstr, sizeof(dup_na->na_ipstr));
dup_na->na_have_ipstr = 1;
}
if (na->na_have_dnsstr) {
sstrncpy(dup_na->na_dnsstr, na->na_dnsstr, sizeof(dup_na->na_dnsstr));
dup_na->na_have_dnsstr = 1;
}
return dup_na;
}
static pr_netaddr_t *get_addr_by_ip(pool *p, const char *name,
array_header **addrs) {
struct sockaddr_in v4;
pr_netaddr_t *na = NULL;
int res;
#ifdef PR_USE_IPV6
if (use_ipv6) {
struct sockaddr_in6 v6;
memset(&v6, 0, sizeof(v6));
v6.sin6_family = AF_INET6;
# ifdef SIN6_LEN
v6.sin6_len = sizeof(struct sockaddr_in6);
# endif /* SIN6_LEN */
res = pr_inet_pton(AF_INET6, name, &v6.sin6_addr);
if (res > 0) {
na = (pr_netaddr_t *) pcalloc(p, sizeof(pr_netaddr_t));
pr_netaddr_set_family(na, AF_INET6);
pr_netaddr_set_sockaddr(na, (struct sockaddr *) &v6);
if (addrs) {
*addrs = NULL;
}
pr_trace_msg(trace_channel, 7, "'%s' resolved to IPv6 address %s", name,
pr_netaddr_get_ipstr(na));
if (netaddr_ipcache_set(name, na) < 0) {
pr_trace_msg(trace_channel, 2, "error setting '%s' in cache: %s", name,
strerror(errno));
}
if (netaddr_ipcache_set(pr_netaddr_get_ipstr(na), na) < 0) {
pr_trace_msg(trace_channel, 2, "error setting '%s' in cache: %s",
pr_netaddr_get_ipstr(na), strerror(errno));
}
return na;
}
}
#endif /* PR_USE_IPV6 */
memset(&v4, 0, sizeof(v4));
v4.sin_family = AF_INET;
# ifdef SIN_LEN
v4.sin_len = sizeof(struct sockaddr_in);
# endif /* SIN_LEN */
res = pr_inet_pton(AF_INET, name, &v4.sin_addr);
if (res > 0) {
na = (pr_netaddr_t *) pcalloc(p, sizeof(pr_netaddr_t));
pr_netaddr_set_family(na, AF_INET);
pr_netaddr_set_sockaddr(na, (struct sockaddr *) &v4);
if (addrs) {
*addrs = NULL;
}
pr_trace_msg(trace_channel, 7, "'%s' resolved to IPv4 address %s", name,
pr_netaddr_get_ipstr(na));
if (netaddr_ipcache_set(name, na) < 0) {
pr_trace_msg(trace_channel, 2, "error setting '%s' in cache: %s", name,
strerror(errno));
}
if (netaddr_ipcache_set(pr_netaddr_get_ipstr(na), na) < 0) {
pr_trace_msg(trace_channel, 2, "error setting '%s' in cache: %s",
pr_netaddr_get_ipstr(na), strerror(errno));
}
return na;
}
return NULL;
}
static pr_netaddr_t *get_addr_by_name(pool *p, const char *name,
array_header **addrs) {
pr_netaddr_t *na = NULL;
int res;
struct addrinfo hints, *info = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
pr_trace_msg(trace_channel, 7,
"attempting to resolve '%s' to IPv4 address via DNS", name);
res = pr_getaddrinfo(name, NULL, &hints, &info);
if (res != 0) {
int xerrno = errno;
if (res != EAI_SYSTEM) {
#ifdef PR_USE_IPV6
if (use_ipv6) {
pr_trace_msg(trace_channel, 7,
"unable to resolve '%s' to an IPv4 address: %s", name,
pr_gai_strerror(res));
info = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
pr_trace_msg(trace_channel, 7,
"attempting to resolve '%s' to IPv6 address via DNS", name);
res = pr_getaddrinfo(name, NULL, &hints, &info);
if (res != 0) {
xerrno = errno;
if (res != EAI_SYSTEM) {
pr_trace_msg(trace_channel, 5,
"unable to resolve '%s' to an IPv6 address: %s", name,
pr_gai_strerror(res));
} else {
pr_trace_msg(trace_channel, 1,
"IPv6 getaddrinfo '%s' system error: [%d] %s", name,
xerrno, strerror(xerrno));
}
}
} else {
pr_trace_msg(trace_channel, 1, "IPv4 getaddrinfo '%s' error: %s",
name, pr_gai_strerror(res));
}
#else
pr_trace_msg(trace_channel, 1, "IPv4 getaddrinfo '%s' error: %s",
name, pr_gai_strerror(res));
#endif /* PR_USE_IPV6 */
} else {
pr_trace_msg(trace_channel, 1,
"IPv4 getaddrinfo '%s' system error: [%d] %s", name,
xerrno, strerror(xerrno));
}
if (res != 0) {
errno = xerrno;
return NULL;
}
}
if (info) {
na = (pr_netaddr_t *) pcalloc(p, sizeof(pr_netaddr_t));
/* Copy the first returned addr into na, as the return value. */
pr_netaddr_set_family(na, info->ai_family);
pr_netaddr_set_sockaddr(na, info->ai_addr);
pr_trace_msg(trace_channel, 7, "resolved '%s' to %s address %s", name,
info->ai_family == AF_INET ? "IPv4" : "IPv6",
pr_netaddr_get_ipstr(na));
if (netaddr_ipcache_set(name, na) < 0) {
pr_trace_msg(trace_channel, 2, "error setting '%s' in cache: %s", name,
strerror(errno));
}
if (netaddr_ipcache_set(pr_netaddr_get_ipstr(na), na) < 0) {
pr_trace_msg(trace_channel, 2, "error setting '%s' in cache: %s",
pr_netaddr_get_ipstr(na), strerror(errno));
}
pr_freeaddrinfo(info);
}
#ifdef PR_USE_IPV6
if (use_ipv6 && addrs) {
/* Do the call again, this time for IPv6 addresses.
*
* We make two separate getaddrinfo(3) calls, rather than one
* with a hint of AF_UNSPEC, because of certain bugs where the use
* of AF_UNSPEC does not function as advertised. (I suspect this
* bug was caused by proftpd's calling pattern, but as I could
* not track it down, and as there are reports of AF_UNSPEC not
* being as fast as AF_INET/AF_INET6, it just seemed easier to
* do it this way.)
*/
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
pr_trace_msg(trace_channel, 7,
"attempting to resolve '%s' to IPv6 address via DNS", name);
res = pr_getaddrinfo(name, NULL, &hints, &info);
if (res != 0) {
int xerrno = errno;
if (res != EAI_SYSTEM) {
pr_trace_msg(trace_channel, 1, "IPv6 getaddrinfo '%s' error: %s",
name, pr_gai_strerror(res));
} else {
pr_trace_msg(trace_channel, 1,
"IPv6 getaddrinfo '%s' system error: [%d] %s", name,
xerrno, strerror(xerrno));
}
} else {
/* We may have already looked up an IPv6 address as the first
* address; we don't want to have duplicate addresses in the
* returned list of additional addresses.
*/
if (info &&
info->ai_family != pr_netaddr_get_family(na)) {
pr_netaddr_t **elt;
*addrs = make_array(p, 0, sizeof(pr_netaddr_t *));
elt = push_array(*addrs);
*elt = pcalloc(p, sizeof(pr_netaddr_t));
pr_netaddr_set_family(*elt, info->ai_family);
pr_netaddr_set_sockaddr(*elt, info->ai_addr);
pr_trace_msg(trace_channel, 7, "resolved '%s' to %s address %s", name,
info->ai_family == AF_INET ? "IPv4" : "IPv6",
pr_netaddr_get_ipstr(*elt));
pr_freeaddrinfo(info);
}
}
}
#endif /* PR_USE_IPV6 */
return na;
}
static pr_netaddr_t *get_addr_by_device(pool *p, const char *name,
array_header **addrs) {
#ifdef HAVE_GETIFADDRS
struct ifaddrs *ifaddr;
pr_netaddr_t *na = NULL;
int res, xerrno;
/* Try to use the given name as a device/interface name, and see if we
* can suss out the IP address(es) to use based on that.
*/
res = getifaddrs(&ifaddr);
if (res < 0) {
xerrno = errno;
pr_trace_msg(trace_channel, 1,
"error retrieving interfaces via getifaddrs(3): %s", strerror(xerrno));
} else {
struct ifaddrs *ifa;
int found_device = FALSE;
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
pr_signals_handle();
/* Watch out for null ifa_addr, as when a device does not have
* an associated address (e.g. due to not be initialized).
*/
if (ifa->ifa_addr == NULL) {
continue;
}
/* We're only looking for addresses, not stats. */
if (ifa->ifa_addr->sa_family != AF_INET
#ifdef PR_USE_IPV6
&& ifa->ifa_addr->sa_family != AF_INET6
#endif /* PR_USE_IPV6 */
) {
continue;
}
if (strcmp(ifa->ifa_name, name) == 0) {
if (found_device == FALSE) {
na = (pr_netaddr_t *) pcalloc(p, sizeof(pr_netaddr_t));
pr_netaddr_set_family(na, ifa->ifa_addr->sa_family);
pr_netaddr_set_sockaddr(na, ifa->ifa_addr);
pr_trace_msg(trace_channel, 7,
"resolved '%s' to interface with %s address %s", name,
ifa->ifa_addr->sa_family == AF_INET ? "IPv4" : "IPv6",
pr_netaddr_get_ipstr(na));
found_device = TRUE;
/* If the caller did not request additional addresses, then
* return now. Otherwise, we keep looking for the other
* addresses bound to this interface.
*/
if (addrs == NULL) {
break;
}
} else {
pr_netaddr_t **elt;
/* We've already found the first match; this block happens
* if the caller wants all of the addresses for this interface.
*/
*addrs = make_array(p, 0, sizeof(pr_netaddr_t *));
elt = push_array(*addrs);
*elt = pcalloc(p, sizeof(pr_netaddr_t));
pr_netaddr_set_family(*elt, ifa->ifa_addr->sa_family);
pr_netaddr_set_sockaddr(*elt, ifa->ifa_addr);
pr_trace_msg(trace_channel, 7,
"resolved '%s' to interface with %s address %s", name,
ifa->ifa_addr->sa_family == AF_INET ? "IPv4" : "IPv6",
pr_netaddr_get_ipstr(*elt));
}
}
}
if (found_device) {
return na;
}
}
errno = ENOENT;
#else
errno = ENOSYS;
#endif /* HAVE_GETIFADDRS */
return NULL;
}
pr_netaddr_t *pr_netaddr_get_addr2(pool *p, const char *name,
array_header **addrs, unsigned int flags) {
pr_netaddr_t *na = NULL;
if (p == NULL ||
name == NULL) {
errno = EINVAL;
return NULL;
}
pr_trace_msg(trace_channel, 10, "resolving name '%s' to IP address",
name);
/* First, check our cache to see if this name has already been
* resolved. We only want to use the cache, though, if the caller did not
* provide the `addrs' pointer, indicating that the caller wants to know
* about any additional addresses for the given name. The netaddr cache
* is a simple cache, hidden from callers, and thus is unable to populate
* that `addrs' pointer if the name is in the cache.
*/
if (addrs == NULL) {
na = netaddr_ipcache_get(p, name);
if (na) {
return na;
}
}
/* Attempt to translate the given name into a pr_netaddr_t using
* pr_inet_pton() first.
*
* First, if IPv6 support is enabled, we try to translate the name using
* pr_inet_pton(AF_INET6) on the hopes that the given string is a valid
* representation of an IPv6 address. If that fails, or if IPv6 support
* is not enabled, we try with pr_inet_pton(AF_INET). If that fails, we
* assume that the given name is a DNS name, and we call pr_getaddrinfo().
*/
na = get_addr_by_ip(p, name, addrs);
if (na != NULL) {
return na;
}
/* If get_addr_by_ip() returns NULL, it means that name does not represent a
* valid network address in the specified address family. Usually,
* this means that name is actually a DNS name, not an IP address
* string. So we treat it as a DNS name, and use getaddrinfo(3) to
* resolve that name to its IP address(es) -- unless the EXCL_DNS flag
* has been used, indicating that the caller does not want us resolving
* DNS names.
*/
if (!(flags & PR_NETADDR_GET_ADDR_FL_EXCL_DNS)) {
na = get_addr_by_name(p, name, addrs);
if (na != NULL) {
return na;
}
}
if (flags & PR_NETADDR_GET_ADDR_FL_INCL_DEVICE) {
na = get_addr_by_device(p, name, addrs);
if (na != NULL) {
return na;
}
}
pr_trace_msg(trace_channel, 8, "failed to resolve '%s' to an IP address",
name);
errno = ENOENT;
return NULL;
}
pr_netaddr_t *pr_netaddr_get_addr(pool *p, const char *name,
array_header **addrs) {
return pr_netaddr_get_addr2(p, name, addrs, 0);
}
int pr_netaddr_get_family(const pr_netaddr_t *na) {
if (!na) {
errno = EINVAL;
return -1;
}
return na->na_family;
}
int pr_netaddr_set_family(pr_netaddr_t *na, int family) {
if (!na) {
errno = EINVAL;
return -1;
}
/* Set the family member of the appropriate sockaddr struct. */
switch (family) {
case AF_INET:
na->na_addr.v4.sin_family = AF_INET;
break;
#ifdef PR_USE_IPV6
case AF_INET6:
if (use_ipv6) {
na->na_addr.v6.sin6_family = AF_INET6;
break;
}
#endif /* PR_USE_IPV6 */
default:
#ifdef EAFNOSUPPORT
errno = EAFNOSUPPORT;
#else
errno = EINVAL;
#endif
return -1;
}
na->na_family = family;
return 0;
}
size_t pr_netaddr_get_sockaddr_len(const pr_netaddr_t *na) {
if (!na) {
errno = EINVAL;
return -1;
}
switch (pr_netaddr_get_family(na)) {
case AF_INET:
return sizeof(struct sockaddr_in);
#ifdef PR_USE_IPV6
case AF_INET6:
if (use_ipv6)
return sizeof(struct sockaddr_in6);
#endif /* PR_USE_IPV6 */
}
errno = EPERM;
return -1;
}
size_t pr_netaddr_get_inaddr_len(const pr_netaddr_t *na) {
if (!na) {
errno = EINVAL;
return -1;
}
switch (pr_netaddr_get_family(na)) {
case AF_INET:
return sizeof(struct in_addr);
#ifdef PR_USE_IPV6
case AF_INET6:
return sizeof(struct in6_addr);
#endif /* PR_USE_IPV6 */
}
errno = EPERM;
return -1;
}
struct sockaddr *pr_netaddr_get_sockaddr(const pr_netaddr_t *na) {
if (!na) {
errno = EINVAL;