openldap-dns-priority.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Implement priority/weight for DNS SRV records
  2. From RFC 2782:
  3. A client MUST attempt to contact the target host with the
  4. lowest-numbered priority it can reach.
  5. This patch sorts the DNS SRV records by their priority, and
  6. additionally gives records with a larger weight a higher probability
  7. of appearing earlier. This way, the DNS SRV records are tried in the
  8. order of their priority.
  9. Author: James M Leddy <james.leddy@redhat.com>
  10. Upstream ITS: #7027
  11. Resolves: #733078
  12. ---
  13. libraries/libldap/dnssrv.c | 106 ++++++++++++++++++++++++++++++++++----------
  14. 1 files changed, 83 insertions(+), 23 deletions(-)
  15. diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c
  16. index 16b1544..40f93b4 100644
  17. --- a/libraries/libldap/dnssrv.c
  18. +++ b/libraries/libldap/dnssrv.c
  19. @@ -174,6 +174,46 @@ int ldap_domain2dn(
  20. return LDAP_SUCCESS;
  21. }
  22. +#ifdef HAVE_RES_QUERY
  23. +#define DNSBUFSIZ (64*1024)
  24. +typedef struct srv_record {
  25. + u_short priority;
  26. + u_short weight;
  27. + u_short port;
  28. + char hostname[DNSBUFSIZ];
  29. +} srv_record;
  30. +
  31. +
  32. +static int srv_cmp(const void *aa, const void *bb){
  33. + srv_record *a=(srv_record *)aa;
  34. + srv_record *b=(srv_record *)bb;
  35. + u_long total;
  36. +
  37. + if(a->priority < b->priority) {
  38. + return -1;
  39. + }
  40. + if(a->priority > b->priority) {
  41. + return 1;
  42. + }
  43. + if(a->priority == b->priority){
  44. + /* targets with same priority are in psudeo random order */
  45. + if (a->weight == 0 && b->weight == 0) {
  46. + if (rand() % 2) {
  47. + return -1;
  48. + } else {
  49. + return 1;
  50. + }
  51. + }
  52. + total = a->weight + b->weight;
  53. + if (rand() % total < a->weight) {
  54. + return -1;
  55. + } else {
  56. + return 1;
  57. + }
  58. + }
  59. +}
  60. +#endif /* HAVE_RES_QUERY */
  61. +
  62. /*
  63. * Lookup and return LDAP servers for domain (using the DNS
  64. * SRV record _ldap._tcp.domain).
  65. @@ -183,15 +223,16 @@ int ldap_domain2hostlist(
  66. char **list )
  67. {
  68. #ifdef HAVE_RES_QUERY
  69. -#define DNSBUFSIZ (64*1024)
  70. - char *request;
  71. - char *hostlist = NULL;
  72. + char *request;
  73. + char *hostlist = NULL;
  74. + srv_record *hostent_head=NULL;
  75. + int i;
  76. int rc, len, cur = 0;
  77. unsigned char reply[DNSBUFSIZ];
  78. + int hostent_count=0;
  79. assert( domain != NULL );
  80. assert( list != NULL );
  81. -
  82. if( *domain == '\0' ) {
  83. return LDAP_PARAM_ERROR;
  84. }
  85. @@ -223,8 +264,7 @@ int ldap_domain2hostlist(
  86. unsigned char *p;
  87. char host[DNSBUFSIZ];
  88. int status;
  89. - u_short port;
  90. - /* int priority, weight; */
  91. + u_short port, priority, weight;
  92. /* Parse out query */
  93. p = reply;
  94. @@ -263,40 +303,56 @@ int ldap_domain2hostlist(
  95. size = (p[0] << 8) | p[1];
  96. p += 2;
  97. if (type == T_SRV) {
  98. - int buflen;
  99. status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
  100. if (status < 0) {
  101. goto out;
  102. }
  103. - /* ignore priority and weight for now */
  104. - /* priority = (p[0] << 8) | p[1]; */
  105. - /* weight = (p[2] << 8) | p[3]; */
  106. +
  107. + /* Get priority weight and port */
  108. + priority = (p[0] << 8) | p[1];
  109. + weight = (p[2] << 8) | p[3];
  110. port = (p[4] << 8) | p[5];
  111. if ( port == 0 || host[ 0 ] == '\0' ) {
  112. goto add_size;
  113. }
  114. - buflen = strlen(host) + STRLENOF(":65355 ");
  115. - hostlist = (char *) LDAP_REALLOC(hostlist, cur + buflen + 1);
  116. - if (hostlist == NULL) {
  117. - rc = LDAP_NO_MEMORY;
  118. - goto out;
  119. + hostent_head = (srv_record *) LDAP_REALLOC(hostent_head, (hostent_count+1)*(sizeof(srv_record)));
  120. + if(hostent_head==NULL){
  121. + rc=LDAP_NO_MEMORY;
  122. + goto out;
  123. +
  124. }
  125. - if (cur > 0) {
  126. - /* not first time around */
  127. - hostlist[cur++] = ' ';
  128. - }
  129. - cur += sprintf(&hostlist[cur], "%s:%hu", host, port);
  130. + hostent_head[hostent_count].priority=priority;
  131. + hostent_head[hostent_count].weight=weight;
  132. + hostent_head[hostent_count].port=port;
  133. + strncpy(hostent_head[hostent_count].hostname, host,255);
  134. + hostent_count=hostent_count+1;
  135. }
  136. add_size:;
  137. p += size;
  138. }
  139. }
  140. + qsort(hostent_head, hostent_count, sizeof(srv_record), srv_cmp);
  141. +
  142. + for(i=0; i<hostent_count; i++){
  143. + int buflen;
  144. + buflen = strlen(hostent_head[i].hostname) + STRLENOF(":65355" );
  145. + hostlist = (char *) LDAP_REALLOC(hostlist, cur+buflen+1);
  146. + if (hostlist == NULL) {
  147. + rc = LDAP_NO_MEMORY;
  148. + goto out;
  149. + }
  150. + if(cur>0){
  151. + hostlist[cur++]=' ';
  152. + }
  153. + cur += sprintf(&hostlist[cur], "%s:%hd", hostent_head[i].hostname, hostent_head[i].port);
  154. + }
  155. +
  156. if (hostlist == NULL) {
  157. - /* No LDAP servers found in DNS. */
  158. - rc = LDAP_UNAVAILABLE;
  159. - goto out;
  160. + /* No LDAP servers found in DNS. */
  161. + rc = LDAP_UNAVAILABLE;
  162. + goto out;
  163. }
  164. rc = LDAP_SUCCESS;
  165. @@ -308,8 +364,12 @@ add_size:;
  166. if (request != NULL) {
  167. LDAP_FREE(request);
  168. }
  169. + if (hostent_head != NULL) {
  170. + LDAP_FREE(hostent_head);
  171. + }
  172. if (rc != LDAP_SUCCESS && hostlist != NULL) {
  173. LDAP_FREE(hostlist);
  174. +
  175. }
  176. return rc;
  177. #else
  178. --
  179. 1.7.6