]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/referral.c
Update copyright statements
[openldap] / servers / slapd / back-dnssrv / referral.c
1 /* referral.c - DNS SRV backend referral handler */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2000-2002 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     Backend     *be,
21     Connection  *conn,
22     Operation   *op,
23     struct berval *dn,
24     struct berval *ndn,
25         const char **text )
26 {
27         int i;
28         int rc = LDAP_OTHER;
29         char *domain = NULL;
30         char *hostlist = NULL;
31         char **hosts = NULL;
32         BVarray urls = NULL;
33
34         if( ndn->bv_len == 0 ) {
35                 *text = "DNS SRV operation upon null (empty) DN disallowed";
36                 return LDAP_UNWILLING_TO_PERFORM;
37         }
38
39         if( get_manageDSAit( op ) ) {
40                 if( op->o_tag == LDAP_REQ_SEARCH ) {
41                         return LDAP_SUCCESS;
42                 }
43
44                 *text = "DNS SRV problem processing manageDSAit control";
45                 return LDAP_OTHER;
46         } 
47
48         if( ldap_dn2domain( dn->bv_val, &domain ) ) {
49                 send_ldap_result( conn, op, LDAP_REFERRAL,
50                         NULL, NULL, default_referral, NULL );
51                 return LDAP_REFERRAL;
52         }
53
54         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
55                 dn->bv_val,
56                 domain == NULL ? "" : domain,
57                 0 );
58
59         if( rc = ldap_domain2hostlist( domain, &hostlist ) ) {
60                 Debug( LDAP_DEBUG_TRACE,
61                         "DNSSRV: domain2hostlist(%s) returned %d\n",
62                         domain, rc, 0 );
63                 *text = "no DNS SRV RR available for DN";
64                 rc = LDAP_NO_SUCH_OBJECT;
65                 goto done;
66         }
67
68         hosts = str2charray( hostlist, " " );
69
70         if( hosts == NULL ) {
71                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
72                 *text = "problem processing DNS SRV records for DN";
73                 goto done;
74         }
75
76         for( i=0; hosts[i] != NULL; i++) {
77                 struct berval url;
78
79                 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
80                 url.bv_val = ch_malloc( url.bv_len + 1 );
81
82                 strcpy( url.bv_val, "ldap://" );
83                 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
84
85                 if ( bvarray_add( &urls, &url ) < 0 ) {
86                         free( url.bv_val );
87                         *text = "problem processing DNS SRV records for DN";
88                         goto done;
89                 }
90         }
91
92         Statslog( LDAP_DEBUG_STATS,
93             "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
94             op->o_connid, op->o_opid, op->o_protocol,
95                 dn->bv_val, urls[0].bv_val );
96
97         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
98                 dn->bv_val, urls[0].bv_val, 0 );
99
100         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
101                 NULL, "DNS SRV generated referrals", urls, NULL );
102
103 done:
104         if( domain != NULL ) ch_free( domain );
105         if( hostlist != NULL ) ch_free( hostlist );
106         if( hosts != NULL ) charray_free( hosts );
107         bvarray_free( urls );
108         return rc;
109 }