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