dnssrv.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /*
  16. * locate LDAP servers using DNS SRV records.
  17. * Location code based on MIT Kerberos KDC location code.
  18. */
  19. #include "portable.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/param.h>
  23. #include <ac/socket.h>
  24. #include <ac/string.h>
  25. #include <ac/time.h>
  26. #include "ldap-int.h"
  27. #ifdef HAVE_ARPA_NAMESER_H
  28. #include <arpa/nameser.h>
  29. #endif
  30. #ifdef HAVE_RESOLV_H
  31. #include <resolv.h>
  32. #endif
  33. int ldap_dn2domain(
  34. LDAP_CONST char *dn_in,
  35. char **domainp)
  36. {
  37. int i, j;
  38. char *ndomain;
  39. LDAPDN dn = NULL;
  40. LDAPRDN rdn = NULL;
  41. LDAPAVA *ava = NULL;
  42. struct berval domain = BER_BVNULL;
  43. static const struct berval DC = BER_BVC("DC");
  44. static const struct berval DCOID = BER_BVC("0.9.2342.19200300.100.1.25");
  45. assert( dn_in != NULL );
  46. assert( domainp != NULL );
  47. *domainp = NULL;
  48. if ( ldap_str2dn( dn_in, &dn, LDAP_DN_FORMAT_LDAP ) != LDAP_SUCCESS ) {
  49. return -2;
  50. }
  51. if( dn ) for( i=0; dn[i] != NULL; i++ ) {
  52. rdn = dn[i];
  53. for( j=0; rdn[j] != NULL; j++ ) {
  54. ava = rdn[j];
  55. if( rdn[j+1] == NULL &&
  56. (ava->la_flags & LDAP_AVA_STRING) &&
  57. ava->la_value.bv_len &&
  58. ( ber_bvstrcasecmp( &ava->la_attr, &DC ) == 0
  59. || ber_bvcmp( &ava->la_attr, &DCOID ) == 0 ) )
  60. {
  61. if( domain.bv_len == 0 ) {
  62. ndomain = LDAP_REALLOC( domain.bv_val,
  63. ava->la_value.bv_len + 1);
  64. if( ndomain == NULL ) {
  65. goto return_error;
  66. }
  67. domain.bv_val = ndomain;
  68. AC_MEMCPY( domain.bv_val, ava->la_value.bv_val,
  69. ava->la_value.bv_len );
  70. domain.bv_len = ava->la_value.bv_len;
  71. domain.bv_val[domain.bv_len] = '\0';
  72. } else {
  73. ndomain = LDAP_REALLOC( domain.bv_val,
  74. ava->la_value.bv_len + sizeof(".") + domain.bv_len );
  75. if( ndomain == NULL ) {
  76. goto return_error;
  77. }
  78. domain.bv_val = ndomain;
  79. domain.bv_val[domain.bv_len++] = '.';
  80. AC_MEMCPY( &domain.bv_val[domain.bv_len],
  81. ava->la_value.bv_val, ava->la_value.bv_len );
  82. domain.bv_len += ava->la_value.bv_len;
  83. domain.bv_val[domain.bv_len] = '\0';
  84. }
  85. } else {
  86. domain.bv_len = 0;
  87. }
  88. }
  89. }
  90. if( domain.bv_len == 0 && domain.bv_val != NULL ) {
  91. LDAP_FREE( domain.bv_val );
  92. domain.bv_val = NULL;
  93. }
  94. ldap_dnfree( dn );
  95. *domainp = domain.bv_val;
  96. return 0;
  97. return_error:
  98. ldap_dnfree( dn );
  99. LDAP_FREE( domain.bv_val );
  100. return -1;
  101. }
  102. int ldap_domain2dn(
  103. LDAP_CONST char *domain_in,
  104. char **dnp)
  105. {
  106. char *domain, *s, *tok_r, *dn, *dntmp;
  107. size_t loc;
  108. assert( domain_in != NULL );
  109. assert( dnp != NULL );
  110. domain = LDAP_STRDUP(domain_in);
  111. if (domain == NULL) {
  112. return LDAP_NO_MEMORY;
  113. }
  114. dn = NULL;
  115. loc = 0;
  116. for (s = ldap_pvt_strtok(domain, ".", &tok_r);
  117. s != NULL;
  118. s = ldap_pvt_strtok(NULL, ".", &tok_r))
  119. {
  120. size_t len = strlen(s);
  121. dntmp = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len );
  122. if (dntmp == NULL) {
  123. if (dn != NULL)
  124. LDAP_FREE(dn);
  125. LDAP_FREE(domain);
  126. return LDAP_NO_MEMORY;
  127. }
  128. dn = dntmp;
  129. if (loc > 0) {
  130. /* not first time. */
  131. strcpy(dn + loc, ",");
  132. loc++;
  133. }
  134. strcpy(dn + loc, "dc=");
  135. loc += sizeof("dc=")-1;
  136. strcpy(dn + loc, s);
  137. loc += len;
  138. }
  139. LDAP_FREE(domain);
  140. *dnp = dn;
  141. return LDAP_SUCCESS;
  142. }
  143. #ifdef HAVE_RES_QUERY
  144. #define DNSBUFSIZ (64*1024)
  145. #define MAXHOST 254 /* RFC 1034, max length is 253 chars */
  146. typedef struct srv_record {
  147. u_short priority;
  148. u_short weight;
  149. u_short port;
  150. char hostname[MAXHOST];
  151. } srv_record;
  152. /* Linear Congruential Generator - we don't need
  153. * high quality randomness, and we don't want to
  154. * interfere with anyone else's use of srand().
  155. *
  156. * The PRNG here cycles thru 941,955 numbers.
  157. */
  158. static float srv_seed;
  159. static void srv_srand(int seed) {
  160. srv_seed = (float)seed / (float)RAND_MAX;
  161. }
  162. static float srv_rand() {
  163. float val = 9821.0 * srv_seed + .211327;
  164. srv_seed = val - (int)val;
  165. return srv_seed;
  166. }
  167. static int srv_cmp(const void *aa, const void *bb){
  168. srv_record *a=(srv_record *)aa;
  169. srv_record *b=(srv_record *)bb;
  170. int i = a->priority - b->priority;
  171. if (i) return i;
  172. return b->weight - a->weight;
  173. }
  174. static void srv_shuffle(srv_record *a, int n) {
  175. int i, j, total = 0, r, p;
  176. for (i=0; i<n; i++)
  177. total += a[i].weight;
  178. /* Do a shuffle per RFC2782 Page 4 */
  179. for (p=n; p>1; a++, p--) {
  180. if (!total) {
  181. /* all remaining weights are zero,
  182. do a straight Fisher-Yates shuffle */
  183. j = srv_rand() * p;
  184. } else {
  185. r = srv_rand() * total;
  186. for (j=0; j<p; j++) {
  187. r -= a[j].weight;
  188. if (r < 0) {
  189. total -= a[j].weight;
  190. break;
  191. }
  192. }
  193. }
  194. if (j && j<p) {
  195. srv_record t = a[0];
  196. a[0] = a[j];
  197. a[j] = t;
  198. }
  199. }
  200. }
  201. #endif /* HAVE_RES_QUERY */
  202. /*
  203. * Lookup and return LDAP servers for domain (using the DNS
  204. * SRV record _ldap._tcp.domain).
  205. */
  206. int ldap_domain2hostlist(
  207. LDAP_CONST char *domain,
  208. char **list )
  209. {
  210. #ifdef HAVE_RES_QUERY
  211. char *request;
  212. char *hostlist = NULL;
  213. srv_record *hostent_head=NULL;
  214. int i, j;
  215. int rc, len, cur = 0;
  216. unsigned char reply[DNSBUFSIZ];
  217. int hostent_count=0;
  218. assert( domain != NULL );
  219. assert( list != NULL );
  220. if( *domain == '\0' ) {
  221. return LDAP_PARAM_ERROR;
  222. }
  223. request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
  224. if (request == NULL) {
  225. return LDAP_NO_MEMORY;
  226. }
  227. sprintf(request, "_ldap._tcp.%s", domain);
  228. LDAP_MUTEX_LOCK(&ldap_int_resolv_mutex);
  229. rc = LDAP_UNAVAILABLE;
  230. #ifdef NS_HFIXEDSZ
  231. /* Bind 8/9 interface */
  232. len = res_query(request, ns_c_in, ns_t_srv, reply, sizeof(reply));
  233. # ifndef T_SRV
  234. # define T_SRV ns_t_srv
  235. # endif
  236. #else
  237. /* Bind 4 interface */
  238. # ifndef T_SRV
  239. # define T_SRV 33
  240. # endif
  241. len = res_query(request, C_IN, T_SRV, reply, sizeof(reply));
  242. #endif
  243. if (len >= 0) {
  244. unsigned char *p;
  245. char host[DNSBUFSIZ];
  246. int status;
  247. u_short port, priority, weight;
  248. /* Parse out query */
  249. p = reply;
  250. #ifdef NS_HFIXEDSZ
  251. /* Bind 8/9 interface */
  252. p += NS_HFIXEDSZ;
  253. #elif defined(HFIXEDSZ)
  254. /* Bind 4 interface w/ HFIXEDSZ */
  255. p += HFIXEDSZ;
  256. #else
  257. /* Bind 4 interface w/o HFIXEDSZ */
  258. p += sizeof(HEADER);
  259. #endif
  260. status = dn_expand(reply, reply + len, p, host, sizeof(host));
  261. if (status < 0) {
  262. goto out;
  263. }
  264. p += status;
  265. p += 4;
  266. while (p < reply + len) {
  267. int type, class, ttl, size;
  268. status = dn_expand(reply, reply + len, p, host, sizeof(host));
  269. if (status < 0) {
  270. goto out;
  271. }
  272. p += status;
  273. type = (p[0] << 8) | p[1];
  274. p += 2;
  275. class = (p[0] << 8) | p[1];
  276. p += 2;
  277. ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  278. p += 4;
  279. size = (p[0] << 8) | p[1];
  280. p += 2;
  281. if (type == T_SRV) {
  282. status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
  283. if (status < 0) {
  284. goto out;
  285. }
  286. /* Get priority weight and port */
  287. priority = (p[0] << 8) | p[1];
  288. weight = (p[2] << 8) | p[3];
  289. port = (p[4] << 8) | p[5];
  290. if ( port == 0 || host[ 0 ] == '\0' ) {
  291. goto add_size;
  292. }
  293. hostent_head = (srv_record *) LDAP_REALLOC(hostent_head, (hostent_count+1)*(sizeof(srv_record)));
  294. if(hostent_head==NULL){
  295. rc=LDAP_NO_MEMORY;
  296. goto out;
  297. }
  298. hostent_head[hostent_count].priority=priority;
  299. hostent_head[hostent_count].weight=weight;
  300. hostent_head[hostent_count].port=port;
  301. strncpy(hostent_head[hostent_count].hostname, host, MAXHOST-1);
  302. hostent_head[hostent_count].hostname[MAXHOST-1] = '\0';
  303. hostent_count++;
  304. }
  305. add_size:;
  306. p += size;
  307. }
  308. if (!hostent_head) goto out;
  309. qsort(hostent_head, hostent_count, sizeof(srv_record), srv_cmp);
  310. if (!srv_seed)
  311. srv_srand(time(0L));
  312. /* shuffle records of same priority */
  313. j = 0;
  314. priority = hostent_head[0].priority;
  315. for (i=1; i<hostent_count; i++) {
  316. if (hostent_head[i].priority != priority) {
  317. priority = hostent_head[i].priority;
  318. if (i-j > 1)
  319. srv_shuffle(hostent_head+j, i-j);
  320. j = i;
  321. }
  322. }
  323. if (i-j > 1)
  324. srv_shuffle(hostent_head+j, i-j);
  325. for(i=0; i<hostent_count; i++){
  326. int buflen;
  327. buflen = strlen(hostent_head[i].hostname) + STRLENOF(":65535 ");
  328. hostlist = (char *) LDAP_REALLOC(hostlist, cur+buflen+1);
  329. if (hostlist == NULL) {
  330. rc = LDAP_NO_MEMORY;
  331. goto out;
  332. }
  333. if(cur>0){
  334. hostlist[cur++]=' ';
  335. }
  336. cur += sprintf(&hostlist[cur], "%s:%hu", hostent_head[i].hostname, hostent_head[i].port);
  337. }
  338. }
  339. if (hostlist == NULL) {
  340. /* No LDAP servers found in DNS. */
  341. rc = LDAP_UNAVAILABLE;
  342. goto out;
  343. }
  344. rc = LDAP_SUCCESS;
  345. *list = hostlist;
  346. out:
  347. LDAP_MUTEX_UNLOCK(&ldap_int_resolv_mutex);
  348. if (request != NULL) {
  349. LDAP_FREE(request);
  350. }
  351. if (hostent_head != NULL) {
  352. LDAP_FREE(hostent_head);
  353. }
  354. if (rc != LDAP_SUCCESS && hostlist != NULL) {
  355. LDAP_FREE(hostlist);
  356. }
  357. return rc;
  358. #else
  359. return LDAP_NOT_SUPPORTED;
  360. #endif /* HAVE_RES_QUERY */
  361. }