]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/referral.c
Remove lint
[openldap] / servers / slapd / back-dnssrv / referral.c
1 /* referral.c - DNS SRV backend referral handler */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2000 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     const char *dn,
24     const char *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         struct berval **urls = NULL;
33
34         if( ndn == NULL || *ndn == '\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, &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 == NULL ? "" : dn,
56                 domain == NULL ? "" : domain,
57                 0 );
58
59         if( rc = ldap_domain2hostlist( domain, &hostlist ) ) {
60                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: domain2hostlist returned %d\n",
61                         rc, 0, 0 );
62                 *text = "no DNS SRV RR available for DN";
63                 rc = LDAP_NO_SUCH_OBJECT;
64                 goto done;
65         }
66
67         hosts = str2charray( hostlist, " " );
68
69         if( hosts == NULL ) {
70                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
71                 *text = "problem processing DNS SRV records for DN";
72                 goto done;
73         }
74
75         for( i=0; hosts[i] != NULL; i++) {
76                 struct berval *url = ch_malloc( sizeof( struct berval ) ); 
77
78                 url->bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
79                 url->bv_val = ch_malloc( url->bv_len + 1 );
80
81                 strcpy( url->bv_val, "ldap://" );
82                 strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] );
83
84                 if( ber_bvecadd( &urls, url ) < 0 ) {
85                         ber_bvfree( url );
86                         *text = "problem processing DNS SRV records for DN";
87                         goto done;
88                 }
89         }
90
91         Statslog( LDAP_DEBUG_STATS,
92             "conn=%ld op=%d DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
93             op->o_connid, op->o_opid, op->o_protocol, dn, urls[0]->bv_val );
94
95         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
96                 dn == NULL ? "" : dn,
97                 urls[0]->bv_val, 0 );
98
99         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
100                 NULL, "DNS SRV generated referrals", urls, NULL );
101
102 done:
103         if( domain != NULL ) ch_free( domain );
104         if( hostlist != NULL ) ch_free( hostlist );
105         if( hosts != NULL ) charray_free( hosts );
106         ber_bvecfree( urls );
107         return rc;
108 }