]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/referral.c
b8453f37e8170ce19e7098a3c14643bcc9ab4edf
[openldap] / servers / slapd / back-dnssrv / referral.c
1 /* referral.c - DNS SRV backend referral handler */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2000-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "external.h"
17
18 int
19 dnssrv_back_referrals(
20     Operation   *op,
21     SlapReply   *rs )
22 {
23         int i;
24         int rc = LDAP_OTHER;
25         char *domain = NULL;
26         char *hostlist = NULL;
27         char **hosts = NULL;
28         BerVarray urls = NULL;
29
30         if( op->o_req_dn.bv_len == 0 ) {
31                 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
32                 return LDAP_UNWILLING_TO_PERFORM;
33         }
34
35         if( get_manageDSAit( op ) ) {
36                 if( op->o_tag == LDAP_REQ_SEARCH ) {
37                         return LDAP_SUCCESS;
38                 }
39
40                 rs->sr_text = "DNS SRV problem processing manageDSAit control";
41                 return LDAP_OTHER;
42         } 
43
44         if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
45                 rs->sr_err = LDAP_REFERRAL;
46                 rs->sr_ref = default_referral;
47                 send_ldap_result( op, rs );
48                 return LDAP_REFERRAL;
49         }
50
51         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
52                 op->o_req_dn.bv_val, domain, 0 );
53
54         if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
55                 Debug( LDAP_DEBUG_TRACE,
56                         "DNSSRV: domain2hostlist(%s) returned %d\n",
57                         domain, rc, 0 );
58                 rs->sr_text = "no DNS SRV RR available for DN";
59                 rc = LDAP_NO_SUCH_OBJECT;
60                 goto done;
61         }
62
63         hosts = ldap_str2charray( hostlist, " " );
64
65         if( hosts == NULL ) {
66                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
67                 rs->sr_text = "problem processing DNS SRV records for DN";
68                 goto done;
69         }
70
71         for( i=0; hosts[i] != NULL; i++) {
72                 struct berval url;
73
74                 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
75                 url.bv_val = ch_malloc( url.bv_len + 1 );
76
77                 strcpy( url.bv_val, "ldap://" );
78                 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
79
80                 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
81                         free( url.bv_val );
82                         rs->sr_text = "problem processing DNS SRV records for DN";
83                         goto done;
84                 }
85         }
86
87         Statslog( LDAP_DEBUG_STATS,
88             "conn=%lu op=%lu DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
89             op->o_connid, op->o_opid, op->o_protocol,
90                 op->o_req_dn.bv_val, urls[0].bv_val );
91
92         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
93                 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
94
95         rs->sr_ref = urls;
96         send_ldap_error( op, rs, LDAP_REFERRAL,
97                 "DNS SRV generated referrals" );
98
99 done:
100         if( domain != NULL ) ch_free( domain );
101         if( hostlist != NULL ) ch_free( hostlist );
102         if( hosts != NULL ) ldap_charray_free( hosts );
103         ber_bvarray_free( urls );
104         return rc;
105 }