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