1ebd1706eSgtb /*
2159d09a2SMark Phalan  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3ebd1706eSgtb  * Use is subject to license terms.
4ebd1706eSgtb  */
5ebd1706eSgtb 
6505d05c7Sgtb /*
7505d05c7Sgtb  * lib/krb5/os/dnsglue.h
8505d05c7Sgtb  *
9505d05c7Sgtb  * Copyright 2004 by the Massachusetts Institute of Technology.
10505d05c7Sgtb  * All Rights Reserved.
11505d05c7Sgtb  *
12505d05c7Sgtb  * Export of this software from the United States of America may
13505d05c7Sgtb  *   require a specific license from the United States Government.
14505d05c7Sgtb  *   It is the responsibility of any person or organization contemplating
15505d05c7Sgtb  *   export to obtain such a license before exporting.
16*55fea89dSDan Cross  *
17505d05c7Sgtb  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18505d05c7Sgtb  * distribute this software and its documentation for any purpose and
19505d05c7Sgtb  * without fee is hereby granted, provided that the above copyright
20505d05c7Sgtb  * notice appear in all copies and that both that copyright notice and
21505d05c7Sgtb  * this permission notice appear in supporting documentation, and that
22505d05c7Sgtb  * the name of M.I.T. not be used in advertising or publicity pertaining
23505d05c7Sgtb  * to distribution of the software without specific, written prior
24505d05c7Sgtb  * permission.  Furthermore if you modify this software you must label
25505d05c7Sgtb  * your software as modified software and not distribute it in such a
26505d05c7Sgtb  * fashion that it might be confused with the original M.I.T. software.
27505d05c7Sgtb  * M.I.T. makes no representations about the suitability of
28505d05c7Sgtb  * this software for any purpose.  It is provided "as is" without express
29505d05c7Sgtb  * or implied warranty.
30505d05c7Sgtb  *
31505d05c7Sgtb  * Glue layer for DNS resolver, to make parsing of replies easier
32505d05c7Sgtb  * whether we are using BIND 4, 8, or 9.
33505d05c7Sgtb  */
34505d05c7Sgtb 
35505d05c7Sgtb /*
36505d05c7Sgtb  * BIND 4 doesn't have the ns_initparse() API, so we need to do some
37505d05c7Sgtb  * manual parsing via the HEADER struct.  BIND 8 does have
38505d05c7Sgtb  * ns_initparse(), but has enums for the various protocol constants
39505d05c7Sgtb  * rather than the BIND 4 macros.  BIND 9 (at least on Mac OS X
40505d05c7Sgtb  * Panther) appears to disable res_nsearch() if BIND_8_COMPAT is
41505d05c7Sgtb  * defined (which is necessary to obtain the HEADER struct).
42505d05c7Sgtb  *
43505d05c7Sgtb  * We use ns_initparse() if available at all, and never define
44505d05c7Sgtb  * BIND_8_COMPAT.  If there is no ns_initparse(), we do manual parsing
45505d05c7Sgtb  * by using the HEADER struct.
46505d05c7Sgtb  */
47505d05c7Sgtb 
48505d05c7Sgtb #ifndef KRB5_DNSGLUE_H
49505d05c7Sgtb #define KRB5_DNSGLUE_H
50505d05c7Sgtb 
51159d09a2SMark Phalan #include "autoconf.h"
52505d05c7Sgtb #ifdef KRB5_DNS_LOOKUP
53505d05c7Sgtb 
54505d05c7Sgtb #include "k5-int.h"
55505d05c7Sgtb #include "os-proto.h"
56505d05c7Sgtb #ifdef WSHELPER
57505d05c7Sgtb #include <wshelper.h>
58505d05c7Sgtb #else /* WSHELPER */
59505d05c7Sgtb #include <netinet/in.h>
60505d05c7Sgtb #include <arpa/inet.h>
61505d05c7Sgtb #include <arpa/nameser.h>
62505d05c7Sgtb #include <resolv.h>
63505d05c7Sgtb #include <netdb.h>
64505d05c7Sgtb #endif /* WSHELPER */
65505d05c7Sgtb 
66505d05c7Sgtb #if HAVE_SYS_PARAM_H
67505d05c7Sgtb #include <sys/param.h>		/* for MAXHOSTNAMELEN */
68505d05c7Sgtb #endif
69505d05c7Sgtb 
70505d05c7Sgtb #ifndef MAXHOSTNAMELEN
71505d05c7Sgtb #define MAXHOSTNAMELEN 64	/* if we can't find it elswhere */
72505d05c7Sgtb #endif
73505d05c7Sgtb 
74505d05c7Sgtb #ifndef MAXDNAME
75505d05c7Sgtb 
76505d05c7Sgtb #ifdef NS_MAXDNAME
77505d05c7Sgtb #define MAXDNAME NS_MAXDNAME
78505d05c7Sgtb #else
79505d05c7Sgtb #ifdef MAXLABEL
80505d05c7Sgtb #define MAXDNAME (16 * MAXLABEL)
81505d05c7Sgtb #else
82505d05c7Sgtb #define MAXDNAME (16 * MAXHOSTNAMELEN)
83505d05c7Sgtb #endif
84505d05c7Sgtb #endif
85505d05c7Sgtb 
86505d05c7Sgtb #endif
87505d05c7Sgtb 
88159d09a2SMark Phalan #if HAVE_NS_INITPARSE
89159d09a2SMark Phalan /*
90159d09a2SMark Phalan  * Solaris 7 has ns_rr_cl rather than ns_rr_class.
91159d09a2SMark Phalan  */
92159d09a2SMark Phalan #if !defined(ns_rr_class) && defined(ns_rr_cl)
93159d09a2SMark Phalan #define ns_rr_class ns_rr_cl
94159d09a2SMark Phalan #endif
95159d09a2SMark Phalan #endif
96159d09a2SMark Phalan 
97505d05c7Sgtb #if HAVE_RES_NSEARCH
98505d05c7Sgtb /*
99505d05c7Sgtb  * Some BIND 8 / BIND 9 implementations disable the BIND 4 style
100505d05c7Sgtb  * constants.
101505d05c7Sgtb  */
102505d05c7Sgtb #ifndef C_IN
103505d05c7Sgtb #define C_IN ns_c_in
104505d05c7Sgtb #endif
105505d05c7Sgtb #ifndef T_SRV
106505d05c7Sgtb #define T_SRV ns_t_srv
107505d05c7Sgtb #endif
108505d05c7Sgtb #ifndef T_TXT
109505d05c7Sgtb #define T_TXT ns_t_txt
110505d05c7Sgtb #endif
111505d05c7Sgtb 
112505d05c7Sgtb #else  /* !HAVE_RES_NSEARCH */
113505d05c7Sgtb 
114505d05c7Sgtb /*
115505d05c7Sgtb  * Some BIND implementations might be old enough to lack these.
116505d05c7Sgtb  */
117505d05c7Sgtb #ifndef T_TXT
118505d05c7Sgtb #define T_TXT 15
119505d05c7Sgtb #endif
120505d05c7Sgtb #ifndef T_SRV
121505d05c7Sgtb #define T_SRV 33
122505d05c7Sgtb #endif
123505d05c7Sgtb 
124505d05c7Sgtb #endif /* HAVE_RES_NSEARCH */
125505d05c7Sgtb 
126505d05c7Sgtb /*
127505d05c7Sgtb  * INCR_OK
128505d05c7Sgtb  *
129505d05c7Sgtb  * Given moving pointer PTR offset from BASE, return true if adding
130505d05c7Sgtb  * INCR to PTR doesn't move it PTR than MAX bytes from BASE.
131505d05c7Sgtb  */
132505d05c7Sgtb #define INCR_OK(base, max, ptr, incr)				\
133505d05c7Sgtb     ((incr) <= (max) - ((const unsigned char *)(ptr)		\
134505d05c7Sgtb 			- (const unsigned char *)(base)))
135505d05c7Sgtb 
136505d05c7Sgtb /*
137505d05c7Sgtb  * SAFE_GETUINT16
138505d05c7Sgtb  *
139505d05c7Sgtb  * Given PTR offset from BASE, if at least INCR bytes are safe to
140505d05c7Sgtb  * read, get network byte order uint16 into S, and increment PTR.  On
141505d05c7Sgtb  * failure, goto LABEL.
142505d05c7Sgtb  */
143505d05c7Sgtb 
144159d09a2SMark Phalan /* Solaris Kerberos */
145505d05c7Sgtb #define SAFE_GETUINT16(base, max, ptr, incr, s, label)	\
146505d05c7Sgtb     do {						\
147505d05c7Sgtb 	if (!INCR_OK(base, max, ptr, incr)) goto label;	\
148ebd1706eSgtb 	(s) = (unsigned short)(ptr)[0] << 8		\
149ebd1706eSgtb 	    | (unsigned short)(ptr)[1];			\
150ebd1706eSgtb 	(ptr) += (incr);					\
151505d05c7Sgtb     } while (0)
152505d05c7Sgtb 
153505d05c7Sgtb struct krb5int_dns_state;
154505d05c7Sgtb 
155505d05c7Sgtb int krb5int_dns_init(struct krb5int_dns_state **, char *, int, int);
156505d05c7Sgtb int krb5int_dns_nextans(struct krb5int_dns_state *,
157505d05c7Sgtb 			const unsigned char **, int *);
158505d05c7Sgtb int krb5int_dns_expand(struct krb5int_dns_state *,
159505d05c7Sgtb 		       const unsigned char *, char *, int);
160505d05c7Sgtb void krb5int_dns_fini(struct krb5int_dns_state *);
161505d05c7Sgtb 
162505d05c7Sgtb #endif /* KRB5_DNS_LOOKUP */
163505d05c7Sgtb #endif /* !defined(KRB5_DNSGLUE_H) */
164