xref: /titanic_41/usr/src/lib/gss_mechs/mech_krb5/krb5/os/dnssrv.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
1 /*
2  * lib/krb5/os/dnssrv.c
3  *
4  * Copyright 1990,2000,2001,2002,2003 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  *
27  * do DNS SRV RR queries
28  */
29 
30 #include "autoconf.h"
31 #ifdef KRB5_DNS_LOOKUP
32 
33 #include "dnsglue.h"
34 
35 /*
36  * Lookup a KDC via DNS SRV records
37  */
38 
krb5int_free_srv_dns_data(struct srv_dns_entry * p)39 void krb5int_free_srv_dns_data (struct srv_dns_entry *p)
40 {
41     struct srv_dns_entry *next;
42     while (p) {
43 	next = p->next;
44 	free(p->host);
45 	free(p);
46 	p = next;
47     }
48 }
49 
50 /* Do DNS SRV query, return results in *answers.
51 
52    Make best effort to return all the data we can.  On memory or
53    decoding errors, just return what we've got.  Always return 0,
54    currently.  */
55 
56 krb5_error_code
krb5int_make_srv_query_realm(const krb5_data * realm,const char * service,const char * protocol,struct srv_dns_entry ** answers)57 krb5int_make_srv_query_realm(const krb5_data *realm,
58 			     const char *service,
59 			     const char *protocol,
60 			     struct srv_dns_entry **answers)
61 {
62     const unsigned char *p = NULL, *base = NULL;
63     char host[MAXDNAME], *h;
64     int size, ret, rdlen, nlen;
65     unsigned short priority, weight, port;
66     struct krb5int_dns_state *ds = NULL;
67 
68     struct srv_dns_entry *head = NULL;
69     struct srv_dns_entry *srv = NULL, *entry = NULL;
70 
71     /*
72      * First off, build a query of the form:
73      *
74      * service.protocol.realm
75      *
76      * which will most likely be something like:
77      *
78      * _kerberos._udp.REALM
79      *
80      */
81 
82     if (memchr(realm->data, 0, realm->length))
83 	return 0;
84     if ( strlen(service) + strlen(protocol) + realm->length + 6
85          > MAXDNAME )
86 	return 0;
87     sprintf(host, "%s.%s.%.*s", service, protocol, (int) realm->length,
88 	    realm->data);
89 
90     /* Realm names don't (normally) end with ".", but if the query
91        doesn't end with "." and doesn't get an answer as is, the
92        resolv code will try appending the local domain.  Since the
93        realm names are absolutes, let's stop that.
94 
95        But only if a name has been specified.  If we are performing
96        a search on the prefix alone then the intention is to allow
97        the local domain or domain search lists to be expanded.  */
98 
99     h = host + strlen (host);
100     if ((h[-1] != '.') && ((h - host + 1) < sizeof(host)))
101         strcpy (h, ".");
102 
103 #ifdef TEST
104     fprintf (stderr, "sending DNS SRV query for %s\n", host);
105 #endif
106 
107     size = krb5int_dns_init(&ds, host, C_IN, T_SRV);
108     if (size < 0)
109 	goto out;
110 
111     for (;;) {
112 	ret = krb5int_dns_nextans(ds, &base, &rdlen);
113 	if (ret < 0 || base == NULL)
114 	    goto out;
115 
116 	p = base;
117 
118 	SAFE_GETUINT16(base, rdlen, p, 2, priority, out);
119 	SAFE_GETUINT16(base, rdlen, p, 2, weight, out);
120 	SAFE_GETUINT16(base, rdlen, p, 2, port, out);
121 
122 	/*
123 	 * RFC 2782 says the target is never compressed in the reply;
124 	 * do we believe that?  We need to flatten it anyway, though.
125 	 */
126 	nlen = krb5int_dns_expand(ds, p, host, sizeof(host));
127 	if (nlen < 0 || !INCR_OK(base, rdlen, p, nlen))
128 	    goto out;
129 
130 	/*
131 	 * We got everything!  Insert it into our list, but make sure
132 	 * it's in the right order.  Right now we don't do anything
133 	 * with the weight field
134 	 */
135 
136 	srv = (struct srv_dns_entry *) malloc(sizeof(struct srv_dns_entry));
137 	if (srv == NULL)
138 	    goto out;
139 
140 	srv->priority = priority;
141 	srv->weight = weight;
142 	srv->port = port;
143 	/* The returned names are fully qualified.  Don't let the
144 	   local resolver code do domain search path stuff.  */
145 	if (strlen(host) + 2 < sizeof(host))
146 	    strcat(host, ".");
147 	srv->host = strdup(host);
148 	if (srv->host == NULL) {
149 	    free(srv);
150 	    goto out;
151 	}
152 
153 	if (head == NULL || head->priority > srv->priority) {
154 	    srv->next = head;
155 	    head = srv;
156 	} else {
157 	    /*
158 	     * This is confusing.  Only insert an entry into this
159 	     * spot if:
160 	     * The next person has a higher priority (lower priorities
161 	     * are preferred).
162 	     * Or
163 	     * There is no next entry (we're at the end)
164 	     */
165 	    for (entry = head; entry != NULL; entry = entry->next) {
166 		if ((entry->next &&
167 		     entry->next->priority > srv->priority) ||
168 		    entry->next == NULL) {
169 		    srv->next = entry->next;
170 		    entry->next = srv;
171 		    break;
172 		}
173 	    }
174 	}
175     }
176 
177 out:
178     if (ds != NULL) {
179 	krb5int_dns_fini(ds);
180 	ds = NULL;
181     }
182     *answers = head;
183     return 0;
184 }
185 #endif
186