]> git.sur5r.net Git - openldap/blob - servers/slapd/back-dnssrv/referral.c
c22282eb3ae871dd3bd1e214a28b8b82247a2b5a
[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 ( BER_BVISEMPTY( &op->o_req_dn ) ) {
45 #ifdef LDAP_DEVEL
46                 /* FIXME: need some means to determine whether the database
47                  * is a glue instance */
48                 if ( SLAP_GLUE_INSTANCE( op->o_bd ) ) {
49                         return LDAP_SUCCESS;
50                 }
51 #endif /* LDAP_DEVEL */
52
53                 rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
54                 return LDAP_UNWILLING_TO_PERFORM;
55         }
56
57         if( get_manageDSAit( op ) ) {
58                 if( op->o_tag == LDAP_REQ_SEARCH ) {
59                         return LDAP_SUCCESS;
60                 }
61
62                 rs->sr_text = "DNS SRV problem processing manageDSAit control";
63                 return LDAP_OTHER;
64         } 
65
66         if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
67                 rs->sr_err = LDAP_REFERRAL;
68                 rs->sr_ref = default_referral;
69                 send_ldap_result( op, rs );
70                 rs->sr_ref = NULL;
71                 return LDAP_REFERRAL;
72         }
73
74         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
75                 op->o_req_dn.bv_val, domain, 0 );
76
77         if( ( rc = ldap_domain2hostlist( domain, &hostlist ) ) ) {
78                 Debug( LDAP_DEBUG_TRACE,
79                         "DNSSRV: domain2hostlist(%s) returned %d\n",
80                         domain, rc, 0 );
81                 rs->sr_text = "no DNS SRV RR available for DN";
82                 rc = LDAP_NO_SUCH_OBJECT;
83                 goto done;
84         }
85
86         hosts = ldap_str2charray( hostlist, " " );
87
88         if( hosts == NULL ) {
89                 Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
90                 rs->sr_text = "problem processing DNS SRV records for DN";
91                 goto done;
92         }
93
94         for( i=0; hosts[i] != NULL; i++) {
95                 struct berval url;
96
97                 url.bv_len = sizeof("ldap://")-1 + strlen(hosts[i]);
98                 url.bv_val = ch_malloc( url.bv_len + 1 );
99
100                 strcpy( url.bv_val, "ldap://" );
101                 strcpy( &url.bv_val[sizeof("ldap://")-1], hosts[i] );
102
103                 if ( ber_bvarray_add( &urls, &url ) < 0 ) {
104                         free( url.bv_val );
105                         rs->sr_text = "problem processing DNS SRV records for DN";
106                         goto done;
107                 }
108         }
109
110         Statslog( LDAP_DEBUG_STATS,
111             "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
112             op->o_log_prefix, op->o_protocol,
113                 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
114
115         Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
116                 op->o_req_dn.bv_val, urls[0].bv_val, 0 );
117
118         rs->sr_ref = urls;
119         send_ldap_error( op, rs, LDAP_REFERRAL,
120                 "DNS SRV generated referrals" );
121         rs->sr_ref = NULL;
122
123 done:
124         if( domain != NULL ) ch_free( domain );
125         if( hostlist != NULL ) ch_free( hostlist );
126         if( hosts != NULL ) ldap_charray_free( hosts );
127         ber_bvarray_free( urls );
128         return rs->sr_err;
129 }