]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/nss_mdns.c
mDNSResponder-107.6.tar.gz
[apple/mdnsresponder.git] / mDNSPosix / nss_mdns.c
1 /*
2 NICTA Public Software Licence
3 Version 1.0
4
5 Copyright © 2004 National ICT Australia Ltd
6
7 All rights reserved.
8
9 By this licence, National ICT Australia Ltd (NICTA) grants permission,
10 free of charge, to any person who obtains a copy of this software
11 and any associated documentation files ("the Software") to use and
12 deal with the Software in source code and binary forms without
13 restriction, with or without modification, and to permit persons
14 to whom the Software is furnished to do so, provided that the
15 following conditions are met:
16
17 - Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimers.
19 - Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimers in
21 the documentation and/or other materials provided with the
22 distribution.
23 - The name of NICTA may not be used to endorse or promote products
24 derived from this Software without specific prior written permission.
25
26 EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
27 PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS" AND
28 NICTA MAKES NO REPRESENTATIONS, WARRANTIES OR CONDITIONS OF ANY
29 KIND, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY
30 REPRESENTATIONS, WARRANTIES OR CONDITIONS REGARDING THE CONTENTS
31 OR ACCURACY OF THE SOFTWARE, OR OF TITLE, MERCHANTABILITY, FITNESS
32 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, THE ABSENCE OF LATENT
33 OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR
34 NOT DISCOVERABLE.
35
36 TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT WILL
37 NICTA BE LIABLE ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
38 NEGLIGENCE) FOR ANY LOSS OR DAMAGE WHATSOEVER, INCLUDING (WITHOUT
39 LIMITATION) LOSS OF PRODUCTION OR OPERATION TIME, LOSS, DAMAGE OR
40 CORRUPTION OF DATA OR RECORDS; OR LOSS OF ANTICIPATED SAVINGS,
41 OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR OTHER ECONOMIC LOSS;
42 OR ANY SPECIAL, INCIDENTAL, INDIRECT, CONSEQUENTIAL, PUNITIVE OR
43 EXEMPLARY DAMAGES ARISING OUT OF OR IN CONNECTION WITH THIS LICENCE,
44 THE SOFTWARE OR THE USE OF THE SOFTWARE, EVEN IF NICTA HAS BEEN
45 ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46
47 If applicable legislation implies warranties or conditions, or
48 imposes obligations or liability on NICTA in respect of the Software
49 that cannot be wholly or partly excluded, restricted or modified,
50 NICTA's liability is limited, to the full extent permitted by the
51 applicable legislation, at its option, to:
52
53 a. in the case of goods, any one or more of the following:
54 i. the replacement of the goods or the supply of equivalent goods;
55 ii. the repair of the goods;
56 iii. the payment of the cost of replacing the goods or of acquiring
57 equivalent goods;
58 iv. the payment of the cost of having the goods repaired; or
59 b. in the case of services:
60 i. the supplying of the services again; or
61 ii. the payment of the cost of having the services supplied
62 again.
63 */
64
65 /*
66 NSSwitch Implementation of mDNS interface.
67
68 Andrew White (Andrew.White@nicta.com.au)
69 May 2004
70 */
71
72 #include <stdlib.h>
73 #include <stdio.h>
74 #include <string.h>
75 #include <errno.h>
76 #include <syslog.h>
77 #include <pthread.h>
78 #include <ctype.h>
79
80 #include <sys/types.h>
81 #include <sys/time.h>
82 #include <sys/socket.h>
83
84 #include <netinet/in.h>
85
86 #include <arpa/inet.h>
87 #define BIND_8_COMPAT 1
88 #include <arpa/nameser.h>
89
90 #include <dns_sd.h>
91
92
93 //----------
94 // Public functions
95
96 /*
97 Count the number of dots in a name string.
98 */
99 int
100 count_dots (const char * name);
101
102
103 /*
104 Test whether a domain name is local.
105
106 Returns
107 1 if name ends with ".local" or ".local."
108 0 otherwise
109 */
110 int
111 islocal (const char * name);
112
113
114 /*
115 Format an address structure as a string appropriate for DNS reverse (PTR)
116 lookup, based on address type.
117
118 Parameters
119 prefixlen
120 Prefix length, in bits. When formatting, this will be rounded up
121 to the nearest appropriate size. If -1, assume maximum.
122 buf
123 Output buffer. Must be long enough to hold largest possible
124 output.
125 Returns
126 Pointer to (first character of) output buffer,
127 or NULL on error.
128 */
129 char *
130 format_reverse_addr (int af, const void * addr, int prefixlen, char * buf);
131
132
133 /*
134 Format an address structure as a string appropriate for DNS reverse (PTR)
135 lookup for AF_INET. Output is in .in-addr.arpa domain.
136
137 Parameters
138 prefixlen
139 Prefix length, in bits. When formatting, this will be rounded up
140 to the nearest byte (8). If -1, assume 32.
141 buf
142 Output buffer. Must be long enough to hold largest possible
143 output. For AF_INET, this is 29 characters (including null).
144 Returns
145 Pointer to (first character of) output buffer,
146 or NULL on error.
147 */
148 char *
149 format_reverse_addr_in (
150 const struct in_addr * addr,
151 int prefixlen,
152 char * buf
153 );
154 #define DNS_PTR_AF_INET_SIZE 29
155
156 /*
157 Format an address structure as a string appropriate for DNS reverse (PTR)
158 lookup for AF_INET6. Output is in .ip6.arpa domain.
159
160 Parameters
161 prefixlen
162 Prefix length, in bits. When formatting, this will be rounded up
163 to the nearest nibble (4). If -1, assume 128.
164 buf
165 Output buffer. Must be long enough to hold largest possible
166 output. For AF_INET6, this is 72 characters (including null).
167 Returns
168 Pointer to (first character of) output buffer,
169 or NULL on error.
170 */
171 char *
172 format_reverse_addr_in6 (
173 const struct in6_addr * addr,
174 int prefixlen,
175 char * buf
176 );
177 #define DNS_PTR_AF_INET6_SIZE 72
178
179
180 /*
181 Compare whether the given dns name has the given domain suffix.
182 A single leading '.' on the name or leading or trailing '.' on the
183 domain is ignored for the purposes of the comparison.
184 Multiple leading or trailing '.'s are an error. Other DNS syntax
185 errors are not checked for. The comparison is case insensitive.
186
187 Returns
188 1 on success (match)
189 0 on failure (no match)
190 < 0 on error
191 */
192 int
193 cmp_dns_suffix (const char * name, const char * domain);
194 enum
195 {
196 CMP_DNS_SUFFIX_SUCCESS = 1,
197 CMP_DNS_SUFFIX_FAILURE = 0,
198 CMP_DNS_SUFFIX_BAD_NAME = 1,
199 CMP_DNS_SUFFIX_BAD_DOMAIN = -2
200 };
201
202 typedef int ns_type_t;
203 typedef int ns_class_t;
204
205 /*
206 Convert a DNS resource record (RR) code to an address family (AF) code.
207
208 Parameters
209 rrtype
210 resource record type (from nameser.h)
211
212 Returns
213 Appropriate AF code (from socket.h), or AF_UNSPEC if an appropriate
214 mapping couldn't be determined
215 */
216 int
217 rr_to_af (ns_type_t rrtype);
218
219
220 /*
221 Convert an address family (AF) code to a DNS resource record (RR) code.
222
223 Parameters
224 int
225 address family code (from socket.h)
226 Returns
227 Appropriate RR code (from nameser.h), or ns_t_invalid if an appropriate
228 mapping couldn't be determined
229 */
230 ns_type_t
231 af_to_rr (int af);
232
233
234 /*
235 Convert a string to an address family (case insensitive).
236
237 Returns
238 Matching AF code, or AF_UNSPEC if no match found.
239 */
240 int
241 str_to_af (const char * str);
242
243
244 /*
245 Convert a string to an ns_class_t (case insensitive).
246
247 Returns
248 Matching ns_class_t, or ns_c_invalid if no match found.
249 */
250 ns_class_t
251 str_to_ns_class (const char * str);
252
253
254 /*
255 Convert a string to an ns_type_t (case insensitive).
256
257 Returns
258 Matching ns_type_t, or ns_t_invalid if no match found.
259 */
260 ns_type_t
261 str_to_ns_type (const char * str);
262
263
264 /*
265 Convert an address family code to a string.
266
267 Returns
268 String representation of AF,
269 or NULL if address family unrecognised or invalid.
270 */
271 const char *
272 af_to_str (int in);
273
274
275 /*
276 Convert an ns_class_t code to a string.
277
278 Returns
279 String representation of ns_class_t,
280 or NULL if ns_class_t unrecognised or invalid.
281 */
282 const char *
283 ns_class_to_str (ns_class_t in);
284
285
286 /*
287 Convert an ns_type_t code to a string.
288
289 Returns
290 String representation of ns_type_t,
291 or NULL if ns_type_t unrecognised or invalid.
292 */
293 const char *
294 ns_type_to_str (ns_type_t in);
295
296
297 /*
298 Convert DNS rdata in label format (RFC1034, RFC1035) to a name.
299
300 On error, partial data is written to name (as much as was successfully
301 processed) and an error code is returned. Errors include a name too
302 long for the buffer and a pointer in the label (which cannot be
303 resolved).
304
305 Parameters
306 rdata
307 Rdata formatted as series of labels.
308 rdlen
309 Length of rdata buffer.
310 name
311 Buffer to store fully qualified result in.
312 By RFC1034 section 3.1, a 255 character buffer (256 characters
313 including null) is long enough for any legal name.
314 name_len
315 Number of characters available in name buffer, not including
316 trailing null.
317
318 Returns
319 Length of name buffer (not including trailing null).
320 < 0 on error.
321 A return of 0 implies the empty domain.
322 */
323 int
324 dns_rdata_to_name (const char * rdata, int rdlen, char * name, int name_len);
325 enum
326 {
327 DNS_RDATA_TO_NAME_BAD_FORMAT = -1,
328 // Format is broken. Usually because we ran out of data
329 // (according to rdata) before the labels said we should.
330 DNS_RDATA_TO_NAME_TOO_LONG = -2,
331 // The converted rdata is longer than the name buffer.
332 DNS_RDATA_TO_NAME_PTR = -3,
333 // The rdata contains a pointer.
334 };
335
336 #define DNS_LABEL_MAXLEN 63
337 // Maximum length of a single DNS label
338 #define DNS_NAME_MAXLEN 255
339 // Maximum length of a DNS name
340
341 //----------
342 // Public types
343
344 typedef int errcode_t;
345 // Used for 0 = success, non-zero = error code functions
346
347
348 //----------
349 // Public functions
350
351 /*
352 Test whether a domain name is in a domain covered by nss_mdns.
353 The name is assumed to be fully qualified (trailing dot optional);
354 unqualified names will be processed but may return unusual results
355 if the unqualified prefix happens to match a domain suffix.
356
357 Returns
358 1 success
359 0 failure
360 -1 error, check errno
361 */
362 int
363 config_is_mdns_suffix (const char * name);
364
365
366 /*
367 Loads all relevant data from configuration file. Other code should
368 rarely need to call this function, since all other public configuration
369 functions do so implicitly. Once loaded, configuration info doesn't
370 change.
371
372 Returns
373 0 configuration ready
374 non-zero configuration error code
375 */
376 errcode_t
377 init_config ();
378
379 #define ENTNAME hostent
380 #define DATABASE "hosts"
381
382 #include <nss.h>
383 // For nss_status
384 #include <netdb.h>
385 // For hostent
386 #include <sys/types.h>
387 // For size_t
388
389 typedef enum nss_status nss_status;
390 typedef struct hostent hostent;
391
392 /*
393 gethostbyname implementation
394
395 name:
396 name to look up
397 result_buf:
398 resulting entry
399 buf:
400 auxillary buffer
401 buflen:
402 length of auxillary buffer
403 errnop:
404 pointer to errno
405 h_errnop:
406 pointer to h_errno
407 */
408 nss_status
409 _nss_mdns_gethostbyname_r (
410 const char *name,
411 hostent * result_buf,
412 char *buf,
413 size_t buflen,
414 int *errnop,
415 int *h_errnop
416 );
417
418
419 /*
420 gethostbyname2 implementation
421
422 name:
423 name to look up
424 af:
425 address family
426 result_buf:
427 resulting entry
428 buf:
429 auxillary buffer
430 buflen:
431 length of auxillary buffer
432 errnop:
433 pointer to errno
434 h_errnop:
435 pointer to h_errno
436 */
437 nss_status
438 _nss_mdns_gethostbyname2_r (
439 const char *name,
440 int af,
441 hostent * result_buf,
442 char *buf,
443 size_t buflen,
444 int *errnop,
445 int *h_errnop
446 );
447
448
449 /*
450 gethostbyaddr implementation
451
452 addr:
453 address structure to look up
454 len:
455 length of address structure
456 af:
457 address family
458 result_buf:
459 resulting entry
460 buf:
461 auxillary buffer
462 buflen:
463 length of auxillary buffer
464 errnop:
465 pointer to errno
466 h_errnop:
467 pointer to h_errno
468 */
469 nss_status
470 _nss_mdns_gethostbyaddr_r (
471 const void *addr,
472 socklen_t len,
473 int af,
474 hostent * result_buf,
475 char *buf,
476 size_t buflen,
477 int *errnop,
478 int *h_errnop
479 );
480
481
482 //----------
483 // Types and Constants
484
485 const int MDNS_VERBOSE = 0;
486 // This enables verbose syslog messages
487 // If zero, only "imporant" messages will appear in syslog
488
489 #define k_hostname_maxlen 255
490 // As per RFC1034 and RFC1035
491 #define k_aliases_max 15
492 #define k_addrs_max 15
493
494 typedef struct buf_header
495 {
496 char hostname [k_hostname_maxlen + 1];
497 char * aliases [k_aliases_max + 1];
498 char * addrs [k_addrs_max + 1];
499 } buf_header_t;
500
501 typedef struct result_map
502 {
503 int done;
504 nss_status status;
505 hostent * hostent;
506 buf_header_t * header;
507 int aliases_count;
508 int addrs_count;
509 char * buffer;
510 int addr_idx;
511 // Index for addresses - grow from low end
512 // Index points to first empty space
513 int alias_idx;
514 // Index for aliases - grow from high end
515 // Index points to lowest entry
516 int r_errno;
517 int r_h_errno;
518 } result_map_t;
519
520 static const struct timeval
521 k_select_time = { 0, 500000 };
522 // 0 seconds, 500 milliseconds
523
524 //----------
525 // Local prototypes
526
527 static nss_status
528 mdns_gethostbyname2 (
529 const char *name,
530 int af,
531 hostent * result_buf,
532 char *buf,
533 size_t buflen,
534 int *errnop,
535 int *h_errnop
536 );
537
538
539 /*
540 Lookup name using mDNS server
541 */
542 static nss_status
543 mdns_lookup_name (
544 const char * fullname,
545 int af,
546 result_map_t * result
547 );
548
549 /*
550 Lookup address using mDNS server
551 */
552 static nss_status
553 mdns_lookup_addr (
554 const void * addr,
555 socklen_t len,
556 int af,
557 const char * addr_str,
558 result_map_t * result
559 );
560
561
562 /*
563 Handle incoming MDNS events
564 */
565 static nss_status
566 handle_events (DNSServiceRef sdref, result_map_t * result, const char * str);
567
568
569 // Callback for mdns_lookup operations
570 //DNSServiceQueryRecordReply mdns_lookup_callback;
571 typedef void
572 mdns_lookup_callback_t
573 (
574 DNSServiceRef sdref,
575 DNSServiceFlags flags,
576 uint32_t interface_index,
577 DNSServiceErrorType error_code,
578 const char *fullname,
579 uint16_t rrtype,
580 uint16_t rrclass,
581 uint16_t rdlen,
582 const void *rdata,
583 uint32_t ttl,
584 void *context
585 );
586
587 mdns_lookup_callback_t mdns_lookup_callback;
588
589
590 static int
591 init_result (
592 result_map_t * result,
593 hostent * result_buf,
594 char * buf,
595 size_t buflen
596 );
597
598 static int
599 callback_body_ptr (
600 const char * fullname,
601 result_map_t * result,
602 int rdlen,
603 const void * rdata
604 );
605
606 static void *
607 add_address_to_buffer (result_map_t * result, const void * data, int len);
608 static char *
609 add_alias_to_buffer (result_map_t * result, const char * data, int len);
610 static char *
611 add_hostname_len (result_map_t * result, const char * fullname, int len);
612 static char *
613 add_hostname_or_alias (result_map_t * result, const char * data, int len);
614
615 static void *
616 contains_address (result_map_t * result, const void * data, int len);
617 static char *
618 contains_alias (result_map_t * result, const char * data);
619
620
621 static const char *
622 is_applicable_name (
623 result_map_t * result,
624 const char * name,
625 char * lookup_name
626 );
627
628 static const char *
629 is_applicable_addr (
630 result_map_t * result,
631 const void * addr,
632 int af,
633 char * addr_str
634 );
635
636
637 // Error code functions
638
639 static nss_status
640 set_err (result_map_t * result, nss_status status, int err, int herr);
641
642 static nss_status set_err_notfound (result_map_t * result);
643 static nss_status set_err_bad_hostname (result_map_t * result);
644 static nss_status set_err_buf_too_small (result_map_t * result);
645 static nss_status set_err_internal_resource_full (result_map_t * result);
646 static nss_status set_err_system (result_map_t * result);
647 static nss_status set_err_mdns_failed (result_map_t * result);
648 static nss_status set_err_success (result_map_t * result);
649
650
651 //----------
652 // Global variables
653
654
655 //----------
656 // NSS functions
657
658 nss_status
659 _nss_mdns_gethostbyname_r (
660 const char *name,
661 hostent * result_buf,
662 char *buf,
663 size_t buflen,
664 int *errnop,
665 int *h_errnop
666 )
667 {
668 if (MDNS_VERBOSE)
669 syslog (LOG_DEBUG,
670 "mdns: Called nss_mdns_gethostbyname with %s",
671 name
672 );
673
674 return
675 mdns_gethostbyname2 (
676 name, AF_INET, result_buf, buf, buflen, errnop, h_errnop
677 );
678 }
679
680
681 nss_status
682 _nss_mdns_gethostbyname2_r (
683 const char *name,
684 int af,
685 hostent * result_buf,
686 char *buf,
687 size_t buflen,
688 int *errnop,
689 int *h_errnop
690 )
691 {
692 if (MDNS_VERBOSE)
693 syslog (LOG_DEBUG,
694 "mdns: Called nss_mdns_gethostbyname2 with %s",
695 name
696 );
697
698 return
699 mdns_gethostbyname2 (
700 name, af, result_buf, buf, buflen, errnop, h_errnop
701 );
702 }
703
704
705 nss_status
706 _nss_mdns_gethostbyaddr_r (
707 const void *addr,
708 socklen_t len,
709 int af,
710 hostent * result_buf,
711 char *buf,
712 size_t buflen,
713 int *errnop,
714 int *h_errnop
715 )
716 {
717 char addr_str [NI_MAXHOST + 1];
718 result_map_t result;
719 int err_status;
720
721 if (inet_ntop (af, addr, addr_str, NI_MAXHOST) == NULL)
722 {
723 const char * family = af_to_str (af);
724 if (family == NULL)
725 {
726 family = "Unknown";
727 }
728
729 syslog (LOG_WARNING,
730 "mdns: Couldn't covert address, family %d (%s) in nss_mdns_gethostbyaddr: %s",
731 af,
732 family,
733 strerror (errno)
734 );
735
736 // This address family never applicable to us, so return NOT_FOUND
737
738 *errnop = ENOENT;
739 *h_errnop = HOST_NOT_FOUND;
740 return NSS_STATUS_NOTFOUND;
741 }
742 if (MDNS_VERBOSE)
743 {
744 syslog (LOG_DEBUG,
745 "mdns: Called nss_mdns_gethostbyaddr with %s",
746 addr_str
747 );
748 }
749
750 // Initialise result
751 err_status = init_result (&result, result_buf, buf, buflen);
752 if (err_status)
753 {
754 *errnop = err_status;
755 *h_errnop = NETDB_INTERNAL;
756 return NSS_STATUS_TRYAGAIN;
757 }
758
759 if (is_applicable_addr (&result, addr, af, addr_str))
760 {
761 nss_status rv;
762
763 rv = mdns_lookup_addr (addr, len, af, addr_str, &result);
764 if (rv == NSS_STATUS_SUCCESS)
765 {
766 return rv;
767 }
768 }
769
770 // Return current error status (defaults to NOT_FOUND)
771
772 *errnop = result.r_errno;
773 *h_errnop = result.r_h_errno;
774 return result.status;
775 }
776
777
778 //----------
779 // Local functions
780
781 nss_status
782 mdns_gethostbyname2 (
783 const char *name,
784 int af,
785 hostent * result_buf,
786 char *buf,
787 size_t buflen,
788 int *errnop,
789 int *h_errnop
790 )
791 {
792 char lookup_name [k_hostname_maxlen + 1];
793 result_map_t result;
794 int err_status;
795
796 // Initialise result
797 err_status = init_result (&result, result_buf, buf, buflen);
798 if (err_status)
799 {
800 *errnop = err_status;
801 *h_errnop = NETDB_INTERNAL;
802 return NSS_STATUS_TRYAGAIN;
803 }
804
805 if (is_applicable_name (&result, name, lookup_name))
806 {
807 // Try using mdns
808 nss_status rv;
809
810 if (MDNS_VERBOSE)
811 syslog (LOG_DEBUG,
812 "mdns: Local name: %s",
813 name
814 );
815
816 rv = mdns_lookup_name (name, af, &result);
817 if (rv == NSS_STATUS_SUCCESS)
818 {
819 return rv;
820 }
821 }
822
823 // Return current error status (defaults to NOT_FOUND)
824
825 *errnop = result.r_errno;
826 *h_errnop = result.r_h_errno;
827 return result.status;
828 }
829
830
831 /*
832 Lookup a fully qualified hostname using the default record type
833 for the specified address family.
834
835 Parameters
836 fullname
837 Fully qualified hostname. If not fully qualified the code will
838 still 'work', but the lookup is unlikely to succeed.
839 af
840 Either AF_INET or AF_INET6. Other families are not supported.
841 result
842 Initialised 'result' data structure.
843 */
844 static nss_status
845 mdns_lookup_name (
846 const char * fullname,
847 int af,
848 result_map_t * result
849 )
850 {
851 // Lookup using mDNS.
852 DNSServiceErrorType errcode;
853 DNSServiceRef sdref;
854 ns_type_t rrtype;
855 nss_status status;
856
857 if (MDNS_VERBOSE)
858 syslog (LOG_DEBUG,
859 "mdns: Attempting lookup of %s",
860 fullname
861 );
862
863 switch (af)
864 {
865 case AF_INET:
866 rrtype = kDNSServiceType_A;
867 result->hostent->h_length = 4;
868 // Length of an A record
869 break;
870
871 case AF_INET6:
872 rrtype = kDNSServiceType_AAAA;
873 result->hostent->h_length = 16;
874 // Length of an AAAA record
875 break;
876
877 default:
878 syslog (LOG_WARNING,
879 "mdns: Unsupported address family %d",
880 af
881 );
882 return set_err_bad_hostname (result);
883 }
884 result->hostent->h_addrtype = af;
885
886 errcode =
887 DNSServiceQueryRecord (
888 &sdref,
889 kDNSServiceFlagsForceMulticast, // force multicast query
890 kDNSServiceInterfaceIndexAny, // all interfaces
891 fullname, // full name to query for
892 rrtype, // resource record type
893 kDNSServiceClass_IN, // internet class records
894 mdns_lookup_callback, // callback
895 result // Context - result buffer
896 );
897
898 if (errcode)
899 {
900 syslog (LOG_WARNING,
901 "mdns: Failed to initialise lookup, error %d",
902 errcode
903 );
904 return set_err_mdns_failed (result);
905 }
906
907 status = handle_events (sdref, result, fullname);
908 DNSServiceRefDeallocate (sdref);
909 return status;
910 }
911
912
913 /*
914 Reverse (PTR) lookup for the specified address.
915
916 Parameters
917 addr
918 Either a struct in_addr or a struct in6_addr
919 addr_len
920 size of the address
921 af
922 Either AF_INET or AF_INET6. Other families are not supported.
923 Must match addr
924 addr_str
925 Address in format suitable for PTR lookup.
926 AF_INET: a.b.c.d -> d.c.b.a.in-addr.arpa
927 AF_INET6: reverse nibble format, x.x.x...x.ip6.arpa
928 result
929 Initialised 'result' data structure.
930 */
931 static nss_status
932 mdns_lookup_addr (
933 const void * addr,
934 socklen_t addr_len,
935 int af,
936 const char * addr_str,
937 result_map_t * result
938 )
939 {
940 DNSServiceErrorType errcode;
941 DNSServiceRef sdref;
942 nss_status status;
943
944 if (MDNS_VERBOSE)
945 syslog (LOG_DEBUG,
946 "mdns: Attempting lookup of %s",
947 addr_str
948 );
949
950 result->hostent->h_addrtype = af;
951 result->hostent->h_length = addr_len;
952
953 // Query address becomes "address" in result.
954 if (! add_address_to_buffer (result, addr, addr_len))
955 {
956 return result->status;
957 }
958
959 result->hostent->h_name [0] = 0;
960
961 errcode =
962 DNSServiceQueryRecord (
963 &sdref,
964 kDNSServiceFlagsForceMulticast, // force multicast query
965 kDNSServiceInterfaceIndexAny, // all interfaces
966 addr_str, // address string to query for
967 kDNSServiceType_PTR, // pointer RRs
968 kDNSServiceClass_IN, // internet class records
969 mdns_lookup_callback, // callback
970 result // Context - result buffer
971 );
972
973 if (errcode)
974 {
975 syslog (LOG_WARNING,
976 "mdns: Failed to initialise mdns lookup, error %d",
977 errcode
978 );
979 return set_err_mdns_failed (result);
980 }
981
982 status = handle_events (sdref, result, addr_str);
983 DNSServiceRefDeallocate (sdref);
984 return status;
985 }
986
987
988 /*
989 Wait on result of callback, and process it when it arrives.
990
991 Parameters
992 sdref
993 dns-sd reference
994 result
995 Initialised 'result' data structure.
996 str
997 lookup string, used for status/error reporting.
998 */
999 static nss_status
1000 handle_events (DNSServiceRef sdref, result_map_t * result, const char * str)
1001 {
1002 int dns_sd_fd = DNSServiceRefSockFD(sdref);
1003 int nfds = dns_sd_fd + 1;
1004 fd_set readfds;
1005 struct timeval tv;
1006 int select_result;
1007
1008 while (! result->done)
1009 {
1010 FD_ZERO(&readfds);
1011 FD_SET(dns_sd_fd, &readfds);
1012
1013 tv = k_select_time;
1014
1015 select_result =
1016 select (nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
1017 if (select_result > 0)
1018 {
1019 if (FD_ISSET(dns_sd_fd, &readfds))
1020 {
1021 if (MDNS_VERBOSE)
1022 syslog (LOG_DEBUG,
1023 "mdns: Reply received for %s",
1024 str
1025 );
1026 DNSServiceProcessResult(sdref);
1027 }
1028 else
1029 {
1030 syslog (LOG_WARNING,
1031 "mdns: Unexpected return from select on lookup of %s",
1032 str
1033 );
1034 }
1035 }
1036 else
1037 {
1038 // Terminate loop due to timer expiry
1039 if (MDNS_VERBOSE)
1040 syslog (LOG_DEBUG,
1041 "mdns: %s not found - timer expired",
1042 str
1043 );
1044 set_err_notfound (result);
1045 break;
1046 }
1047 }
1048
1049 return result->status;
1050 }
1051
1052
1053 /*
1054 Examine incoming data and add to relevant fields in result structure.
1055 This routine is called from DNSServiceProcessResult where appropriate.
1056 */
1057 void
1058 mdns_lookup_callback
1059 (
1060 DNSServiceRef sdref,
1061 DNSServiceFlags flags,
1062 uint32_t interface_index,
1063 DNSServiceErrorType error_code,
1064 const char *fullname,
1065 uint16_t rrtype,
1066 uint16_t rrclass,
1067 uint16_t rdlen,
1068 const void *rdata,
1069 uint32_t ttl,
1070 void *context
1071 )
1072 {
1073 // A single record is received
1074
1075 result_map_t * result = (result_map_t *) context;
1076
1077 (void)sdref; // Unused
1078 (void)interface_index; // Unused
1079 (void)ttl; // Unused
1080
1081 if (! (flags & kDNSServiceFlagsMoreComing) )
1082 {
1083 result->done = 1;
1084 }
1085
1086 if (error_code == kDNSServiceErr_NoError)
1087 {
1088 ns_type_t expected_rr_type =
1089 af_to_rr (result->hostent->h_addrtype);
1090
1091 // Idiot check class
1092 if (rrclass != C_IN)
1093 {
1094 syslog (LOG_WARNING,
1095 "mdns: Received bad RR class: expected %d (%s),"
1096 " got %d (%s), RR type %d (%s)",
1097 C_IN,
1098 ns_class_to_str (C_IN),
1099 rrclass,
1100 ns_class_to_str (rrclass),
1101 rrtype,
1102 ns_type_to_str (rrtype)
1103 );
1104 return;
1105 }
1106
1107 // If a PTR
1108 if (rrtype == kDNSServiceType_PTR)
1109 {
1110 if (callback_body_ptr (fullname, result, rdlen, rdata) < 0)
1111 return;
1112 }
1113 else if (rrtype == expected_rr_type)
1114 {
1115 if (!
1116 add_hostname_or_alias (
1117 result,
1118 fullname,
1119 strlen (fullname)
1120 )
1121 )
1122 {
1123 result->done = 1;
1124 return;
1125 // Abort on error
1126 }
1127
1128 if (! add_address_to_buffer (result, rdata, rdlen) )
1129 {
1130 result->done = 1;
1131 return;
1132 // Abort on error
1133 }
1134 }
1135 else
1136 {
1137 syslog (LOG_WARNING,
1138 "mdns: Received bad RR type: expected %d (%s),"
1139 " got %d (%s)",
1140 expected_rr_type,
1141 ns_type_to_str (expected_rr_type),
1142 rrtype,
1143 ns_type_to_str (rrtype)
1144 );
1145 return;
1146 }
1147
1148 if (result->status != NSS_STATUS_SUCCESS)
1149 set_err_success (result);
1150 }
1151 else
1152 {
1153 // For now, dump message to syslog and continue
1154 syslog (LOG_WARNING,
1155 "mdns: callback returned error %d",
1156 error_code
1157 );
1158 }
1159 }
1160
1161 static int
1162 callback_body_ptr (
1163 const char * fullname,
1164 result_map_t * result,
1165 int rdlen,
1166 const void * rdata
1167 )
1168 {
1169 char result_name [k_hostname_maxlen + 1];
1170 int rv;
1171
1172 // Fullname should be .in-addr.arpa or equivalent, which we're
1173 // not interested in. Ignore it.
1174
1175 rv = dns_rdata_to_name (rdata, rdlen, result_name, k_hostname_maxlen);
1176 if (rv < 0)
1177 {
1178 const char * errmsg;
1179
1180 switch (rv)
1181 {
1182 case DNS_RDATA_TO_NAME_BAD_FORMAT:
1183 errmsg = "mdns: PTR '%s' result badly formatted ('%s...')";
1184 break;
1185
1186 case DNS_RDATA_TO_NAME_TOO_LONG:
1187 errmsg = "mdns: PTR '%s' result too long ('%s...')";
1188 break;
1189
1190 case DNS_RDATA_TO_NAME_PTR:
1191 errmsg = "mdns: PTR '%s' result contained pointer ('%s...')";
1192 break;
1193
1194 default:
1195 errmsg = "mdns: PTR '%s' result conversion failed ('%s...')";
1196 }
1197
1198 syslog (LOG_WARNING,
1199 errmsg,
1200 fullname,
1201 result_name
1202 );
1203
1204 return -1;
1205 }
1206
1207 if (MDNS_VERBOSE)
1208 {
1209 syslog (LOG_DEBUG,
1210 "mdns: PTR '%s' resolved to '%s'",
1211 fullname,
1212 result_name
1213 );
1214 }
1215
1216 // Data should be a hostname
1217 if (!
1218 add_hostname_or_alias (
1219 result,
1220 result_name,
1221 rv
1222 )
1223 )
1224 {
1225 result->done = 1;
1226 return -1;
1227 }
1228
1229 return 0;
1230 }
1231
1232
1233 /*
1234 Add an address to the buffer.
1235
1236 Parameter
1237 result
1238 Result structure to write to
1239 data
1240 Incoming address data buffer
1241 Must be 'int' aligned
1242 len
1243 Length of data buffer (in bytes)
1244 Must match data alignment
1245
1246 Result
1247 Pointer to start of newly written data,
1248 or NULL on error.
1249 If address already exists in buffer, returns pointer to that instead.
1250 */
1251 static void *
1252 add_address_to_buffer (result_map_t * result, const void * data, int len)
1253 {
1254 int new_addr;
1255 void * start;
1256 void * temp;
1257
1258 if ((temp = contains_address (result, data, len)))
1259 {
1260 return temp;
1261 }
1262
1263 if (result->addrs_count >= k_addrs_max)
1264 {
1265 // Not enough addr slots
1266 set_err_internal_resource_full (result);
1267 syslog (LOG_ERR,
1268 "mdns: Internal address buffer full; increase size"
1269 );
1270 return NULL;
1271 }
1272
1273 // Idiot check
1274 if (len != result->hostent->h_length)
1275 {
1276 syslog (LOG_WARNING,
1277 "mdns: Unexpected rdata length for address. Expected %d, got %d",
1278 result->hostent->h_length,
1279 len
1280 );
1281 // XXX And continue for now.
1282 }
1283
1284 new_addr = result->addr_idx + len;
1285
1286 if (new_addr > result->alias_idx)
1287 {
1288 // Not enough room
1289 set_err_buf_too_small (result);
1290 if (MDNS_VERBOSE)
1291 syslog (LOG_DEBUG,
1292 "mdns: Ran out of buffer when adding address %d",
1293 result->addrs_count + 1
1294 );
1295 return NULL;
1296 }
1297
1298 start = result->buffer + result->addr_idx;
1299 memcpy (start, data, len);
1300 result->addr_idx = new_addr;
1301 result->header->addrs [result->addrs_count] = start;
1302 result->addrs_count ++;
1303 result->header->addrs [result->addrs_count] = NULL;
1304
1305 return start;
1306 }
1307
1308
1309 static void *
1310 contains_address (result_map_t * result, const void * data, int len)
1311 {
1312 int i;
1313
1314 // Idiot check
1315 if (len != result->hostent->h_length)
1316 {
1317 syslog (LOG_WARNING,
1318 "mdns: Unexpected rdata length for address. Expected %d, got %d",
1319 result->hostent->h_length,
1320 len
1321 );
1322 // XXX And continue for now.
1323 }
1324
1325 for (i = 0; result->header->addrs [i]; i++)
1326 {
1327 if (memcmp (result->header->addrs [i], data, len) == 0)
1328 {
1329 return result->header->addrs [i];
1330 }
1331 }
1332
1333 return NULL;
1334 }
1335
1336
1337 /*
1338 Add an alias to the buffer.
1339
1340 Parameter
1341 result
1342 Result structure to write to
1343 data
1344 Incoming alias (null terminated)
1345 len
1346 Length of data buffer (in bytes), including trailing null
1347
1348 Result
1349 Pointer to start of newly written data,
1350 or NULL on error
1351 If alias already exists in buffer, returns pointer to that instead.
1352 */
1353 static char *
1354 add_alias_to_buffer (result_map_t * result, const char * data, int len)
1355 {
1356 int new_alias;
1357 char * start;
1358 char * temp;
1359
1360 if ((temp = contains_alias (result, data)))
1361 {
1362 return temp;
1363 }
1364
1365 if (result->aliases_count >= k_aliases_max)
1366 {
1367 // Not enough alias slots
1368 set_err_internal_resource_full (result);
1369 syslog (LOG_ERR,
1370 "mdns: Internal alias buffer full; increase size"
1371 );
1372 return NULL;
1373 }
1374
1375 new_alias = result->alias_idx - len;
1376
1377 if (new_alias < result->addr_idx)
1378 {
1379 // Not enough room
1380 set_err_buf_too_small (result);
1381 if (MDNS_VERBOSE)
1382 syslog (LOG_DEBUG,
1383 "mdns: Ran out of buffer when adding alias %d",
1384 result->aliases_count + 1
1385 );
1386 return NULL;
1387 }
1388
1389 start = result->buffer + new_alias;
1390 memcpy (start, data, len);
1391 result->alias_idx = new_alias;
1392 result->header->aliases [result->aliases_count] = start;
1393 result->aliases_count ++;
1394 result->header->aliases [result->aliases_count] = NULL;
1395
1396 return start;
1397 }
1398
1399
1400 static char *
1401 contains_alias (result_map_t * result, const char * alias)
1402 {
1403 int i;
1404
1405 for (i = 0; result->header->aliases [i]; i++)
1406 {
1407 if (strcmp (result->header->aliases [i], alias) == 0)
1408 {
1409 return result->header->aliases [i];
1410 }
1411 }
1412
1413 return NULL;
1414 }
1415
1416
1417 /*
1418 Add fully qualified hostname to result.
1419
1420 Parameter
1421 result
1422 Result structure to write to
1423 fullname
1424 Fully qualified hostname
1425
1426 Result
1427 Pointer to start of hostname buffer,
1428 or NULL on error (usually hostname too long)
1429 */
1430
1431 static char *
1432 add_hostname_len (result_map_t * result, const char * fullname, int len)
1433 {
1434 if (len >= k_hostname_maxlen)
1435 {
1436 set_err_bad_hostname (result);
1437 syslog (LOG_WARNING,
1438 "mdns: Hostname too long '%.*s': len %d, max %d",
1439 len,
1440 fullname,
1441 len,
1442 k_hostname_maxlen
1443 );
1444 return NULL;
1445 }
1446
1447 result->hostent->h_name =
1448 strcpy (result->header->hostname, fullname);
1449
1450 return result->header->hostname;
1451 }
1452
1453
1454 /*
1455 Add fully qualified name as hostname or alias.
1456
1457 If hostname is not fully qualified this is not an error, but the data
1458 returned may be not what the application wanted.
1459
1460 Parameter
1461 result
1462 Result structure to write to
1463 data
1464 Incoming alias (null terminated)
1465 len
1466 Length of data buffer (in bytes), including trailing null
1467
1468 Result
1469 Pointer to start of newly written data,
1470 or NULL on error
1471 If alias or hostname already exists, returns pointer to that instead.
1472 */
1473 static char *
1474 add_hostname_or_alias (result_map_t * result, const char * data, int len)
1475 {
1476 char * hostname = result->hostent->h_name;
1477
1478 if (*hostname)
1479 {
1480 if (strcmp (hostname, data) == 0)
1481 {
1482 return hostname;
1483 }
1484 else
1485 {
1486 return add_alias_to_buffer (result, data, len);
1487 }
1488 }
1489 else
1490 {
1491 return add_hostname_len (result, data, len);
1492 }
1493 }
1494
1495
1496 static int
1497 init_result (
1498 result_map_t * result,
1499 hostent * result_buf,
1500 char * buf,
1501 size_t buflen
1502 )
1503 {
1504 if (buflen < sizeof (buf_header_t))
1505 {
1506 return ERANGE;
1507 }
1508
1509 result->hostent = result_buf;
1510 result->header = (buf_header_t *) buf;
1511 result->header->hostname[0] = 0;
1512 result->aliases_count = 0;
1513 result->header->aliases[0] = NULL;
1514 result->addrs_count = 0;
1515 result->header->addrs[0] = NULL;
1516 result->buffer = buf + sizeof (buf_header_t);
1517 result->addr_idx = 0;
1518 result->alias_idx = buflen - sizeof (buf_header_t);
1519 result->done = 0;
1520 set_err_notfound (result);
1521
1522 // Point hostent to the right buffers
1523 result->hostent->h_name = result->header->hostname;
1524 result->hostent->h_aliases = result->header->aliases;
1525 result->hostent->h_addr_list = result->header->addrs;
1526
1527 return 0;
1528 }
1529
1530 /*
1531 Set the status in the result.
1532
1533 Parameters
1534 result
1535 Result structure to update
1536 status
1537 New nss_status value
1538 err
1539 New errno value
1540 herr
1541 New h_errno value
1542
1543 Returns
1544 New status value
1545 */
1546 static nss_status
1547 set_err (result_map_t * result, nss_status status, int err, int herr)
1548 {
1549 result->status = status;
1550 result->r_errno = err;
1551 result->r_h_errno = herr;
1552
1553 return status;
1554 }
1555
1556 static nss_status
1557 set_err_notfound (result_map_t * result)
1558 {
1559 return set_err (result, NSS_STATUS_NOTFOUND, ENOENT, HOST_NOT_FOUND);
1560 }
1561
1562 static nss_status
1563 set_err_bad_hostname (result_map_t * result)
1564 {
1565 return set_err (result, NSS_STATUS_TRYAGAIN, ENOENT, NO_RECOVERY);
1566 }
1567
1568 static nss_status
1569 set_err_buf_too_small (result_map_t * result)
1570 {
1571 return set_err (result, NSS_STATUS_TRYAGAIN, ERANGE, NETDB_INTERNAL);
1572 }
1573
1574 static nss_status
1575 set_err_internal_resource_full (result_map_t * result)
1576 {
1577 return set_err (result, NSS_STATUS_RETURN, ERANGE, NO_RECOVERY);
1578 }
1579
1580 static nss_status
1581 set_err_system (result_map_t * result)
1582 {
1583 return set_err (result, NSS_STATUS_UNAVAIL, errno, NETDB_INTERNAL);
1584 }
1585
1586 static nss_status
1587 set_err_mdns_failed (result_map_t * result)
1588 {
1589 return set_err (result, NSS_STATUS_TRYAGAIN, EAGAIN, TRY_AGAIN);
1590 }
1591
1592 static nss_status
1593 set_err_success (result_map_t * result)
1594 {
1595 result->status = NSS_STATUS_SUCCESS;
1596 return result->status;
1597 }
1598
1599
1600 /*
1601 Test whether name is applicable for mdns to process, and if so copy into
1602 lookup_name buffer (if non-NULL).
1603
1604 Returns
1605 Pointer to name to lookup up, if applicable, or NULL otherwise.
1606 */
1607 static const char *
1608 is_applicable_name (
1609 result_map_t * result,
1610 const char * name,
1611 char * lookup_name
1612 )
1613 {
1614 int match = config_is_mdns_suffix (name);
1615 if (match > 0)
1616 {
1617 if (lookup_name)
1618 {
1619 strncpy (lookup_name, name, k_hostname_maxlen + 1);
1620 return lookup_name;
1621 }
1622 else
1623 {
1624 return name;
1625 }
1626 }
1627 else
1628 {
1629 if (match < 0)
1630 {
1631 set_err_system (result);
1632 }
1633 return NULL;
1634 }
1635 }
1636
1637 /*
1638 Test whether address is applicable for mdns to process, and if so copy into
1639 addr_str buffer as an address suitable for ptr lookup.
1640
1641 Returns
1642 Pointer to name to lookup up, if applicable, or NULL otherwise.
1643 */
1644 static const char *
1645 is_applicable_addr (
1646 result_map_t * result,
1647 const void * addr,
1648 int af,
1649 char * addr_str
1650 )
1651 {
1652 int match;
1653
1654 if (! format_reverse_addr (af, addr, -1, addr_str))
1655 {
1656 if (MDNS_VERBOSE)
1657 syslog (LOG_DEBUG,
1658 "mdns: Failed to create reverse address"
1659 );
1660 return NULL;
1661 }
1662
1663 if (MDNS_VERBOSE)
1664 syslog (LOG_DEBUG,
1665 "mdns: Reverse address: %s",
1666 addr_str
1667 );
1668
1669 match = config_is_mdns_suffix (addr_str);
1670 if (match > 0)
1671 {
1672 return addr_str;
1673 }
1674 else
1675 {
1676 if (match < 0)
1677 {
1678 set_err_system (result);
1679 }
1680 return NULL;
1681 }
1682 }
1683
1684 //----------
1685 // Types and Constants
1686
1687 const char * k_conf_file = "/etc/nss_mdns.conf";
1688 #define CONF_LINE_SIZE 1024
1689
1690 const char k_comment_char = '#';
1691
1692 const char * k_keyword_domain = "domain";
1693
1694 const char * k_default_domains [] =
1695 {
1696 "local",
1697 "254.169.in-addr.arpa",
1698 "8.e.f.ip6.int",
1699 "8.e.f.ip6.arpa",
1700 "9.e.f.ip6.int",
1701 "9.e.f.ip6.arpa",
1702 "a.e.f.ip6.int",
1703 "a.e.f.ip6.arpa",
1704 "b.e.f.ip6.int",
1705 "b.e.f.ip6.arpa",
1706 NULL
1707 // Always null terminated
1708 };
1709
1710 // Linked list of domains
1711 typedef struct domain_entry
1712 {
1713 char * domain;
1714 struct domain_entry * next;
1715 } domain_entry_t;
1716
1717
1718 // Config
1719 typedef struct
1720 {
1721 domain_entry_t * domains;
1722 } config_t;
1723
1724 const config_t k_empty_config =
1725 {
1726 NULL
1727 };
1728
1729
1730 // Context - tracks position in config file, used for error reporting
1731 typedef struct
1732 {
1733 const char * filename;
1734 int linenum;
1735 } config_file_context_t;
1736
1737
1738 //----------
1739 // Local prototypes
1740
1741 static errcode_t
1742 load_config (config_t * conf);
1743
1744 static errcode_t
1745 process_config_line (
1746 config_t * conf,
1747 char * line,
1748 config_file_context_t * context
1749 );
1750
1751 static char *
1752 get_next_word (char * input, char **next);
1753
1754 static errcode_t
1755 default_config (config_t * conf);
1756
1757 static errcode_t
1758 add_domain (config_t * conf, const char * domain);
1759
1760 static int
1761 contains_domain (const config_t * conf, const char * domain);
1762
1763 static int
1764 contains_domain_suffix (const config_t * conf, const char * addr);
1765
1766
1767 //----------
1768 // Global variables
1769
1770 static config_t * g_config = NULL;
1771 // Configuration info
1772
1773 pthread_mutex_t g_config_mutex =
1774 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
1775 PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1776 #else
1777 PTHREAD_MUTEX_INITIALIZER;
1778 #endif
1779
1780
1781 //----------
1782 // Configuration functions
1783
1784
1785 /*
1786 Initialise the configuration from the config file.
1787
1788 Returns
1789 0 success
1790 non-zero error code on failure
1791 */
1792 errcode_t
1793 init_config ()
1794 {
1795 if (g_config)
1796 {
1797 /*
1798 Safe to test outside mutex.
1799 If non-zero, initialisation is complete and g_config can be
1800 safely used read-only. If zero, then we do proper mutex
1801 testing before initialisation.
1802 */
1803 return 0;
1804 }
1805 else
1806 {
1807 int errcode = -1;
1808 int presult;
1809 config_t * temp_config;
1810
1811 // Acquire mutex
1812 presult = pthread_mutex_lock (&g_config_mutex);
1813 if (presult)
1814 {
1815 syslog (LOG_ERR,
1816 "mdns: Fatal mutex lock error in nss_mdns:init_config, %s:%d: %d: %s",
1817 __FILE__, __LINE__, presult, strerror (presult)
1818 );
1819 return presult;
1820 }
1821
1822 // Test again now we have mutex, in case initialisation occurred while
1823 // we were waiting
1824 if (! g_config)
1825 {
1826 temp_config = (config_t *) malloc (sizeof (config_t));
1827 if (temp_config)
1828 {
1829 // NOTE: This code will leak memory if initialisation fails
1830 // repeatedly. This should only happen in the case of a memory
1831 // error, so I'm not sure if it's a meaningful problem. - AW
1832 *temp_config = k_empty_config;
1833 errcode = load_config (temp_config);
1834
1835 if (! errcode)
1836 {
1837 g_config = temp_config;
1838 }
1839 }
1840 else
1841 {
1842 syslog (LOG_ERR,
1843 "mdns: Can't allocate memory in nss_mdns:init_config, %s:%d",
1844 __FILE__, __LINE__
1845 );
1846 errcode = errno;
1847 }
1848 }
1849
1850 presult = pthread_mutex_unlock (&g_config_mutex);
1851 if (presult)
1852 {
1853 syslog (LOG_ERR,
1854 "mdns: Fatal mutex unlock error in nss_mdns:init_config, %s:%d: %d: %s",
1855 __FILE__, __LINE__, presult, strerror (presult)
1856 );
1857 errcode = presult;
1858 }
1859
1860 return errcode;
1861 }
1862 }
1863
1864
1865 int
1866 config_is_mdns_suffix (const char * name)
1867 {
1868 int errcode = init_config ();
1869 if (! errcode)
1870 {
1871 return contains_domain_suffix (g_config, name);
1872 }
1873 else
1874 {
1875 errno = errcode;
1876 return -1;
1877 }
1878 }
1879
1880
1881 //----------
1882 // Local functions
1883
1884 static errcode_t
1885 load_config (config_t * conf)
1886 {
1887 FILE * cf;
1888 char line [CONF_LINE_SIZE];
1889 config_file_context_t context;
1890
1891 context.filename = k_conf_file;
1892 context.linenum = 0;
1893
1894
1895 cf = fopen (context.filename, "r");
1896 if (! cf)
1897 {
1898 syslog (LOG_INFO,
1899 "mdns: Couldn't open nss_mdns configuration file %s, using default.",
1900 context.filename
1901 );
1902 return default_config (conf);
1903 }
1904
1905 while (fgets (line, CONF_LINE_SIZE, cf))
1906 {
1907 int errcode;
1908 context.linenum++;
1909 errcode = process_config_line (conf, line, &context);
1910 if (errcode)
1911 {
1912 // Critical error, give up
1913 return errcode;
1914 }
1915 }
1916
1917 return 0;
1918 }
1919
1920
1921 /*
1922 Parse a line of the configuration file.
1923 For each keyword recognised, perform appropriate handling.
1924 If the keyword is not recognised, print a message to syslog
1925 and continue.
1926
1927 Returns
1928 0 success, or recoverable config file error
1929 non-zero serious system error, processing aborted
1930 */
1931 static errcode_t
1932 process_config_line (
1933 config_t * conf,
1934 char * line,
1935 config_file_context_t * context
1936 )
1937 {
1938 char * curr = line;
1939 char * word;
1940
1941 word = get_next_word (curr, &curr);
1942 if (! word || word [0] == k_comment_char)
1943 {
1944 // Nothing interesting on this line
1945 return 0;
1946 }
1947
1948 if (strcmp (word, k_keyword_domain) == 0)
1949 {
1950 word = get_next_word (curr, &curr);
1951 if (word)
1952 {
1953 int errcode = add_domain (conf, word);
1954 if (errcode)
1955 {
1956 // something badly wrong, bail
1957 return errcode;
1958 }
1959
1960 if (get_next_word (curr, NULL))
1961 {
1962 syslog (LOG_WARNING,
1963 "%s, line %d: ignored extra text found after domain",
1964 context->filename,
1965 context->linenum
1966 );
1967 }
1968 }
1969 else
1970 {
1971 syslog (LOG_WARNING,
1972 "%s, line %d: no domain specified",
1973 context->filename,
1974 context->linenum
1975 );
1976 }
1977 }
1978 else
1979 {
1980 syslog (LOG_WARNING,
1981 "%s, line %d: unknown keyword %s - skipping",
1982 context->filename,
1983 context->linenum,
1984 word
1985 );
1986 }
1987
1988 return 0;
1989 }
1990
1991
1992 /*
1993 Get next word (whitespace separated) from input string.
1994 A null character is written into the first whitespace character following
1995 the word.
1996
1997 Parameters
1998 input
1999 Input string. This string is modified by get_next_word.
2000 next
2001 If non-NULL and the result is non-NULL, a pointer to the
2002 character following the end of the word (after the null)
2003 is written to 'next'.
2004 If no word is found, the original value is unchanged.
2005 If the word extended to the end of the string, 'next' points
2006 to the trailling NULL.
2007 It is safe to pass 'str' as 'input' and '&str' as 'next'.
2008 Returns
2009 Pointer to the first non-whitespace character (and thus word) found.
2010 if no word is found, returns NULL.
2011 */
2012 static char *
2013 get_next_word (char * input, char **next)
2014 {
2015 char * curr = input;
2016 char * result;
2017
2018 while (isspace (*curr))
2019 {
2020 curr ++;
2021 }
2022
2023 if (*curr == 0)
2024 {
2025 return NULL;
2026 }
2027
2028 result = curr;
2029 while (*curr && ! isspace (*curr))
2030 {
2031 curr++;
2032 }
2033 if (*curr)
2034 {
2035 *curr = 0;
2036 if (next)
2037 {
2038 *next = curr+1;
2039 }
2040 }
2041 else
2042 {
2043 if (next)
2044 {
2045 *next = curr;
2046 }
2047 }
2048
2049 return result;
2050 }
2051
2052
2053 static errcode_t
2054 default_config (config_t * conf)
2055 {
2056 int i;
2057 for (i = 0; k_default_domains [i]; i++)
2058 {
2059 int errcode =
2060 add_domain (conf, k_default_domains [i]);
2061 if (errcode)
2062 {
2063 // Something has gone (badly) wrong - let's bail
2064 return errcode;
2065 }
2066 }
2067
2068 return 0;
2069 }
2070
2071
2072 static errcode_t
2073 add_domain (config_t * conf, const char * domain)
2074 {
2075 if (! contains_domain (conf, domain))
2076 {
2077 domain_entry_t * d =
2078 (domain_entry_t *) malloc (sizeof (domain_entry_t));
2079 if (! d)
2080 {
2081 syslog (LOG_ERR,
2082 "mdns: Can't allocate memory in nss_mdns:init_config, %s:%d",
2083 __FILE__, __LINE__
2084 );
2085 return ENOMEM;
2086 }
2087
2088 d->domain = strdup (domain);
2089 if (! d->domain)
2090 {
2091 syslog (LOG_ERR,
2092 "mdns: Can't allocate memory in nss_mdns:init_config, %s:%d",
2093 __FILE__, __LINE__
2094 );
2095 free (d);
2096 return ENOMEM;
2097 }
2098 d->next = conf->domains;
2099 conf->domains = d;
2100 }
2101
2102 return 0;
2103 }
2104
2105
2106 static int
2107 contains_domain (const config_t * conf, const char * domain)
2108 {
2109 const domain_entry_t * curr = conf->domains;
2110
2111 while (curr != NULL)
2112 {
2113 if (strcasecmp (curr->domain, domain) == 0)
2114 {
2115 return 1;
2116 }
2117
2118 curr = curr->next;
2119 }
2120
2121 return 0;
2122 }
2123
2124
2125 static int
2126 contains_domain_suffix (const config_t * conf, const char * addr)
2127 {
2128 const domain_entry_t * curr = conf->domains;
2129
2130 while (curr != NULL)
2131 {
2132 if (cmp_dns_suffix (addr, curr->domain) > 0)
2133 {
2134 return 1;
2135 }
2136
2137 curr = curr->next;
2138 }
2139
2140 return 0;
2141 }
2142
2143 //----------
2144 // Types and Constants
2145
2146 static const char * k_local_suffix = "local";
2147 static const char k_dns_separator = '.';
2148
2149 static const int k_label_maxlen = DNS_LABEL_MAXLEN;
2150 // Label entries longer than this are actually pointers.
2151
2152 typedef struct
2153 {
2154 int value;
2155 const char * name;
2156 const char * comment;
2157 } table_entry_t;
2158
2159 static const table_entry_t k_table_af [] =
2160 {
2161 { AF_UNSPEC, NULL, NULL },
2162 { AF_LOCAL, "LOCAL", NULL },
2163 { AF_UNIX, "UNIX", NULL },
2164 { AF_INET, "INET", NULL },
2165 { AF_INET6, "INET6", NULL }
2166 };
2167 static const int k_table_af_size =
2168 sizeof (k_table_af) / sizeof (* k_table_af);
2169
2170 static const char * k_table_ns_class [] =
2171 {
2172 NULL,
2173 "IN"
2174 };
2175 static const int k_table_ns_class_size =
2176 sizeof (k_table_ns_class) / sizeof (* k_table_ns_class);
2177
2178 static const char * k_table_ns_type [] =
2179 {
2180 NULL,
2181 "A",
2182 "NS",
2183 "MD",
2184 "MF",
2185 "CNAME",
2186 "SOA",
2187 "MB",
2188 "MG",
2189 "MR",
2190 "NULL",
2191 "WKS",
2192 "PTR",
2193 "HINFO",
2194 "MINFO",
2195 "MX",
2196 "TXT",
2197 "RP",
2198 "AFSDB",
2199 "X25",
2200 "ISDN",
2201 "RT",
2202 "NSAP",
2203 NULL,
2204 "SIG",
2205 "KEY",
2206 "PX",
2207 "GPOS",
2208 "AAAA",
2209 "LOC",
2210 "NXT",
2211 "EID",
2212 "NIMLOC",
2213 "SRV",
2214 "ATMA",
2215 "NAPTR",
2216 "KX",
2217 "CERT",
2218 "A6",
2219 "DNAME",
2220 "SINK",
2221 "OPT"
2222 };
2223 static const int k_table_ns_type_size =
2224 sizeof (k_table_ns_type) / sizeof (* k_table_ns_type);
2225
2226
2227 //----------
2228 // Local prototypes
2229
2230 static int
2231 simple_table_index (const char * table [], int size, const char * str);
2232
2233 static int
2234 table_index_name (const table_entry_t table [], int size, const char * str);
2235
2236 static int
2237 table_index_value (const table_entry_t table [], int size, int n);
2238
2239
2240 //----------
2241 // Global variables
2242
2243
2244 //----------
2245 // Util functions
2246
2247 int
2248 count_dots (const char * name)
2249 {
2250 int count = 0;
2251 int i;
2252 for (i = 0; name[i]; i++)
2253 {
2254 if (name [i] == k_dns_separator)
2255 count++;
2256 }
2257
2258 return count;
2259 }
2260
2261
2262 int
2263 islocal (const char * name)
2264 {
2265 return cmp_dns_suffix (name, k_local_suffix) > 0;
2266 }
2267
2268
2269 int
2270 rr_to_af (ns_type_t rrtype)
2271 {
2272 switch (rrtype)
2273 {
2274 case kDNSServiceType_A:
2275 return AF_INET;
2276
2277 case kDNSServiceType_AAAA:
2278 return AF_INET6;
2279
2280 default:
2281 return AF_UNSPEC;
2282 }
2283 }
2284
2285
2286 ns_type_t
2287 af_to_rr (int af)
2288 {
2289 switch (af)
2290 {
2291 case AF_INET:
2292 return kDNSServiceType_A;
2293
2294 case AF_INET6:
2295 return kDNSServiceType_AAAA;
2296
2297 default:
2298 //return ns_t_invalid;
2299 return 0;
2300 }
2301 }
2302
2303
2304 int
2305 str_to_af (const char * str)
2306 {
2307 int result =
2308 table_index_name (k_table_af, k_table_af_size, str);
2309 if (result < 0)
2310 result = 0;
2311
2312 return k_table_af [result].value;
2313 }
2314
2315
2316 ns_class_t
2317 str_to_ns_class (const char * str)
2318 {
2319 return (ns_class_t)
2320 simple_table_index (k_table_ns_class, k_table_ns_class_size, str);
2321 }
2322
2323
2324 ns_type_t
2325 str_to_ns_type (const char * str)
2326 {
2327 return (ns_type_t)
2328 simple_table_index (k_table_ns_type, k_table_ns_type_size, str);
2329 }
2330
2331
2332 const char *
2333 af_to_str (int in)
2334 {
2335 int result =
2336 table_index_value (k_table_af, k_table_af_size, in);
2337 if (result < 0)
2338 result = 0;
2339
2340 return k_table_af [result].name;
2341 }
2342
2343
2344 const char *
2345 ns_class_to_str (ns_class_t in)
2346 {
2347 if (in < k_table_ns_class_size)
2348 return k_table_ns_class [in];
2349 else
2350 return NULL;
2351 }
2352
2353
2354 const char *
2355 ns_type_to_str (ns_type_t in)
2356 {
2357 if (in < k_table_ns_type_size)
2358 return k_table_ns_type [in];
2359 else
2360 return NULL;
2361 }
2362
2363
2364 char *
2365 format_reverse_addr_in (
2366 const struct in_addr * addr,
2367 int prefixlen,
2368 char * buf
2369 )
2370 {
2371 char * curr = buf;
2372 int i;
2373
2374 const uint8_t * in_addr_a = (uint8_t *) addr;
2375
2376 if (prefixlen > 32)
2377 return NULL;
2378 if (prefixlen < 0)
2379 prefixlen = 32;
2380
2381 i = (prefixlen + 7) / 8;
2382 // divide prefixlen into bytes, rounding up
2383
2384 while (i > 0)
2385 {
2386 i--;
2387 curr += sprintf (curr, "%d.", in_addr_a [i]);
2388 }
2389 sprintf (curr, "in-addr.arpa");
2390
2391 return buf;
2392 }
2393
2394
2395 char *
2396 format_reverse_addr_in6 (
2397 const struct in6_addr * addr,
2398 int prefixlen,
2399 char * buf
2400 )
2401 {
2402 char * curr = buf;
2403 int i;
2404
2405 const uint8_t * in_addr_a = (uint8_t *) addr;
2406
2407 if (prefixlen > 128)
2408 return NULL;
2409 if (prefixlen < 0)
2410 prefixlen = 128;
2411
2412 i = (prefixlen + 3) / 4;
2413 // divide prefixlen into nibbles, rounding up
2414
2415 // Special handling for first
2416 if (i % 2)
2417 {
2418 curr += sprintf (curr, "%d.", (in_addr_a [i/2] >> 4) & 0x0F);
2419 }
2420 i >>= 1;
2421 // Convert i to bytes (divide by 2)
2422
2423 while (i > 0)
2424 {
2425 uint8_t val;
2426
2427 i--;
2428 val = in_addr_a [i];
2429 curr += sprintf (curr, "%x.%x.", val & 0x0F, (val >> 4) & 0x0F);
2430 }
2431 sprintf (curr, "ip6.arpa");
2432
2433 return buf;
2434 }
2435
2436
2437 char *
2438 format_reverse_addr (
2439 int af,
2440 const void * addr,
2441 int prefixlen,
2442 char * buf
2443 )
2444 {
2445 switch (af)
2446 {
2447 case AF_INET:
2448 return
2449 format_reverse_addr_in (
2450 (struct in_addr *) addr, prefixlen, buf
2451 );
2452 break;
2453
2454 case AF_INET6:
2455 return
2456 format_reverse_addr_in6 (
2457 (struct in6_addr *) addr, prefixlen, buf
2458 );
2459 break;
2460
2461 default:
2462 return NULL;
2463 }
2464 }
2465
2466
2467 int
2468 cmp_dns_suffix (const char * name, const char * domain)
2469 {
2470 const char * nametail;
2471 const char * domaintail;
2472
2473 // Idiot checks
2474 if (*name == 0 || *name == k_dns_separator)
2475 {
2476 // Name can't be empty or start with separator
2477 return CMP_DNS_SUFFIX_BAD_NAME;
2478 }
2479
2480 if (*domain == 0)
2481 {
2482 return CMP_DNS_SUFFIX_SUCCESS;
2483 // trivially true
2484 }
2485
2486 if (*domain == k_dns_separator)
2487 {
2488 // drop leading separator from domain
2489 domain++;
2490 if (*domain == k_dns_separator)
2491 {
2492 return CMP_DNS_SUFFIX_BAD_DOMAIN;
2493 }
2494 }
2495
2496 // Find ends of strings
2497 for (nametail = name; *nametail; nametail++)
2498 ;
2499 for (domaintail = domain; *domaintail; domaintail++)
2500 ;
2501
2502 // Shuffle back to last real character, and drop any trailing '.'
2503 // while we're at it.
2504 nametail --;
2505 if (*nametail == k_dns_separator)
2506 {
2507 nametail --;
2508 if (*nametail == k_dns_separator)
2509 {
2510 return CMP_DNS_SUFFIX_BAD_NAME;
2511 }
2512 }
2513 domaintail --;
2514 if (*domaintail == k_dns_separator)
2515 {
2516 domaintail --;
2517 if (*domaintail == k_dns_separator)
2518 {
2519 return CMP_DNS_SUFFIX_BAD_DOMAIN;
2520 }
2521 }
2522
2523 // Compare.
2524 while (
2525 nametail >= name
2526 && domaintail >= domain
2527 && tolower(*nametail) == tolower(*domaintail))
2528 {
2529 nametail--;
2530 domaintail--;
2531 }
2532
2533 /* A successful finish will be one of the following:
2534 (leading and trailing . ignored)
2535
2536 name : domain2.domain1
2537 domain: domain2.domain1
2538 ^
2539
2540 name : domain3.domain2.domain1
2541 domain: domain2.domain1
2542 ^
2543 */
2544 if (
2545 domaintail < domain
2546 && (nametail < name || *nametail == k_dns_separator)
2547 )
2548 {
2549 return CMP_DNS_SUFFIX_SUCCESS;
2550 }
2551 else
2552 {
2553 return CMP_DNS_SUFFIX_FAILURE;
2554 }
2555 }
2556
2557
2558 int
2559 dns_rdata_to_name (const char * rdata, int rdlen, char * name, int name_len)
2560 {
2561 int i = 0;
2562 // Index into 'name'
2563 const char * rdata_curr = rdata;
2564
2565 // drop any leading whitespace rubbish
2566 while (isspace (*rdata_curr))
2567 {
2568 rdata_curr ++;
2569 if (rdata_curr > rdata + rdlen)
2570 {
2571 return DNS_RDATA_TO_NAME_BAD_FORMAT;
2572 }
2573 }
2574
2575 /*
2576 In RDATA, a DNS name is stored as a series of labels.
2577 Each label consists of a length octet (max value 63)
2578 followed by the data for that label.
2579 The series is terminated with a length 0 octet.
2580 A length octet beginning with bits 11 is a pointer to
2581 somewhere else in the payload, but we don't support these
2582 since we don't have access to the entire payload.
2583
2584 See RFC1034 section 3.1 and RFC1035 section 3.1.
2585 */
2586 while (1)
2587 {
2588 int term_len = *rdata_curr;
2589 rdata_curr++;
2590
2591 if (term_len == 0)
2592 {
2593 break;
2594 // 0 length record terminates label
2595 }
2596 else if (term_len > k_label_maxlen)
2597 {
2598 name [i] = 0;
2599 return DNS_RDATA_TO_NAME_PTR;
2600 }
2601 else if (rdata_curr + term_len > rdata + rdlen)
2602 {
2603 name [i] = 0;
2604 return DNS_RDATA_TO_NAME_BAD_FORMAT;
2605 }
2606
2607 if (name_len < i + term_len + 1)
2608 // +1 is separator
2609 {
2610 name [i] = 0;
2611 return DNS_RDATA_TO_NAME_TOO_LONG;
2612 }
2613
2614 memcpy (name + i, rdata_curr, term_len);
2615
2616 i += term_len;
2617 rdata_curr += term_len;
2618
2619 name [i] = k_dns_separator;
2620 i++;
2621 }
2622
2623 name [i] = 0;
2624 return i;
2625 }
2626
2627
2628 //----------
2629 // Local functions
2630
2631 /*
2632 Find the index of an string entry in a table. A case insenitive match
2633 is performed. If no entry is found, 0 is returned.
2634
2635 Parameters
2636 table
2637 Lookup table
2638 Table entries may be NULL. NULL entries will never match.
2639 size
2640 number of entries in table
2641 str
2642 lookup string
2643
2644 Result
2645 index of first matching entry, or 0 if no matches
2646 */
2647 static int
2648 simple_table_index (const char * table [], int size, const char * str)
2649 {
2650 int i;
2651 for (i = 0; i < size; i++)
2652 {
2653 if (
2654 table [i]
2655 && (strcasecmp (table [i], str) == 0)
2656 )
2657 {
2658 return i;
2659 }
2660 }
2661
2662 return 0;
2663 }
2664
2665
2666 /*
2667 Find the index of a name in a table.
2668
2669 Parameters
2670 table
2671 array of table_entry_t records. The name field is compared
2672 (ignoring case) to the input string.
2673 size
2674 number of entries in table
2675 str
2676 lookup string
2677
2678 Result
2679 index of first matching entry, or -1 if no matches
2680 */
2681 static int
2682 table_index_name (const table_entry_t table [], int size, const char * str)
2683 {
2684 int i;
2685 for (i = 0; i < size; i++)
2686 {
2687 if (
2688 table [i].name
2689 && (strcasecmp (table [i].name, str) == 0)
2690 )
2691 {
2692 return i;
2693 }
2694 }
2695
2696 return -1;
2697 }
2698
2699
2700 /*
2701 Find the index of a value a table.
2702
2703 Parameters
2704 table
2705 array of table_entry_t records. The value field is compared to
2706 the input value
2707 size
2708 number of entries in table
2709 n
2710 lookup value
2711
2712 Result
2713 index of first matching entry, or -1 if no matches
2714 */
2715 static int
2716 table_index_value (const table_entry_t table [], int size, int n)
2717 {
2718 int i;
2719 for (i = 0; i < size; i++)
2720 {
2721 if (table [i].value == n)
2722 {
2723 return i;
2724 }
2725 }
2726
2727 return -1;
2728 }