1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * lib/krb5/os/dnsglue.h
4  *
5  * Copyright 2004 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  * Glue layer for DNS resolver, to make parsing of replies easier
28  * whether we are using BIND 4, 8, or 9.
29  */
30 
31 /*
32  * BIND 4 doesn't have the ns_initparse() API, so we need to do some
33  * manual parsing via the HEADER struct.  BIND 8 does have
34  * ns_initparse(), but has enums for the various protocol constants
35  * rather than the BIND 4 macros.  BIND 9 (at least on Mac OS X
36  * Panther) appears to disable res_nsearch() if BIND_8_COMPAT is
37  * defined (which is necessary to obtain the HEADER struct).
38  *
39  * We use ns_initparse() if available at all, and never define
40  * BIND_8_COMPAT.  If there is no ns_initparse(), we do manual parsing
41  * by using the HEADER struct.
42  */
43 
44 #ifndef KRB5_DNSGLUE_H
45 #define KRB5_DNSGLUE_H
46 
47 #ifdef KRB5_DNS_LOOKUP
48 
49 #define NEED_SOCKETS
50 #include "k5-int.h"
51 #include "os-proto.h"
52 #ifdef WSHELPER
53 #include <wshelper.h>
54 #else /* WSHELPER */
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <arpa/nameser.h>
58 #include <resolv.h>
59 #include <netdb.h>
60 #endif /* WSHELPER */
61 
62 #if HAVE_SYS_PARAM_H
63 #include <sys/param.h>		/* for MAXHOSTNAMELEN */
64 #endif
65 
66 #ifndef MAXHOSTNAMELEN
67 #define MAXHOSTNAMELEN 64	/* if we can't find it elswhere */
68 #endif
69 
70 #ifndef MAXDNAME
71 
72 #ifdef NS_MAXDNAME
73 #define MAXDNAME NS_MAXDNAME
74 #else
75 #ifdef MAXLABEL
76 #define MAXDNAME (16 * MAXLABEL)
77 #else
78 #define MAXDNAME (16 * MAXHOSTNAMELEN)
79 #endif
80 #endif
81 
82 #endif
83 
84 #if HAVE_RES_NSEARCH
85 /*
86  * Some BIND 8 / BIND 9 implementations disable the BIND 4 style
87  * constants.
88  */
89 #ifndef C_IN
90 #define C_IN ns_c_in
91 #endif
92 #ifndef T_SRV
93 #define T_SRV ns_t_srv
94 #endif
95 #ifndef T_TXT
96 #define T_TXT ns_t_txt
97 #endif
98 
99 #else  /* !HAVE_RES_NSEARCH */
100 
101 /*
102  * Some BIND implementations might be old enough to lack these.
103  */
104 #ifndef T_TXT
105 #define T_TXT 15
106 #endif
107 #ifndef T_SRV
108 #define T_SRV 33
109 #endif
110 
111 #endif /* HAVE_RES_NSEARCH */
112 
113 /*
114  * INCR_OK
115  *
116  * Given moving pointer PTR offset from BASE, return true if adding
117  * INCR to PTR doesn't move it PTR than MAX bytes from BASE.
118  */
119 #define INCR_OK(base, max, ptr, incr)				\
120     ((incr) <= (max) - ((const unsigned char *)(ptr)		\
121 			- (const unsigned char *)(base)))
122 
123 /*
124  * SAFE_GETUINT16
125  *
126  * Given PTR offset from BASE, if at least INCR bytes are safe to
127  * read, get network byte order uint16 into S, and increment PTR.  On
128  * failure, goto LABEL.
129  */
130 
131 #define SAFE_GETUINT16(base, max, ptr, incr, s, label)	\
132     do {						\
133 	if (!INCR_OK(base, max, ptr, incr)) goto label;	\
134 	(s) = (unsigned short)(p)[0] << 8		\
135 	    | (unsigned short)(p)[1];			\
136 	(p) += (incr);					\
137     } while (0)
138 
139 struct krb5int_dns_state;
140 
141 int krb5int_dns_init(struct krb5int_dns_state **, char *, int, int);
142 int krb5int_dns_nextans(struct krb5int_dns_state *,
143 			const unsigned char **, int *);
144 int krb5int_dns_expand(struct krb5int_dns_state *,
145 		       const unsigned char *, char *, int);
146 void krb5int_dns_fini(struct krb5int_dns_state *);
147 
148 #endif /* KRB5_DNS_LOOKUP */
149 #endif /* !defined(KRB5_DNSGLUE_H) */
150