]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/referral.c
Sync with HEAD
[openldap] / servers / slapd / back-dnssrv / referral.c
1 /* referral.c - DNS SRV backend referral handler */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * Portions Copyright 2000-2003 Kurt D. Zeilenga.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was originally developed by Kurt D. Zeilenga for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/string.h>
27 #include <ac/socket.h>
28
29 #include "slap.h"
30 #include "proto-dnssrv.h"
31
32 int
33 dnssrv_back_referrals(
34     Operation   *op,
35     SlapReply   *rs )
36 {
37         int i;
38         int rc = LDAP_OTHER;
39         char *domain = NULL;
40         char *hostlist = NULL;
41         char **hosts = NULL;
42         BerVarray urls = NULL;
43
44         if( op->o_req_dn.bv_len == 0 ) {
45                 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
46                 return LDAP_UNWILLING_TO_PERFORM;
47         }
48
49         if( get_manageDSAit( op ) ) {
50                 if( op->o_tag == LDAP_REQ_SEARCH ) {
51                         return LDAP_SUCCESS;
52                 }
53
54                 rs->sr_text = "DNS SRV problem processing manageDSAit control";
55                 return LDAP_OTHER;
56         } 
57
58         if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
59                 rs->sr_err = LDAP_REFERRAL;
60                 rs->sr_ref = default_referral;
61                 send_ldap_result( op, rs );
62                 return LDAP_REFERRAL;
63         }
64
65         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
66                 op->o_req_dn.bv_val, domain, 0 );
67
68         if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
69                 Debug( LDAP_DEBUG_TRACE,
70                         "DNSSRV: domain2hostlist(%s) returned %d\n",
71                         domain, rc, 0 );
72                 rs->sr_text = "no DNS SRV RR available for DN";
73                 rc = LDAP_NO_SUCH_OBJECT;
74                 goto done;
75         }
76
77         hosts = ldap_str2charray( hostlist, " " );
78
79         if( hosts == NULL ) {
80                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
81                 rs->sr_text = "problem processing DNS SRV records for DN";
82                 goto done;
83         }
84
85         for( i=0; hosts[i] != NULL; i++) {
86                 struct berval url;
87
88                 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
89                 url.bv_val = ch_malloc( url.bv_len + 1 );
90
91                 strcpy( url.bv_val, "ldap://" );
92                 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
93
94                 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
95                         free( url.bv_val );
96                         rs->sr_text = "problem processing DNS SRV records for DN";
97                         goto done;
98                 }
99         }
100
101         Statslog( LDAP_DEBUG_STATS,
102             "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
103             op->o_log_prefix, op->o_protocol,
104                 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
105
106         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
107                 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
108
109         rs->sr_ref = urls;
110         send_ldap_error( op, rs, LDAP_REFERRAL,
111                 "DNS SRV generated referrals" );
112
113 done:
114         if( domain != NULL ) ch_free( domain );
115         if( hostlist != NULL ) ch_free( hostlist );
116         if( hosts != NULL ) ldap_charray_free( hosts );
117         ber_bvarray_free( urls );
118         return rc;
119 }