Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / overlays / rwmdn.c
blob6dc865f9db7344090f65636ab4da41833c4d56ec
1 /* rwmdn.c - massages dns */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/overlays/rwmdn.c,v 1.18.2.4 2008/02/11 23:26:49 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2008 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
18 /* ACKNOWLEDGEMENTS:
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 * Masarati.
25 #include "portable.h"
27 #ifdef SLAPD_OVER_RWM
29 #include <stdio.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
34 #include "slap.h"
35 #include "rwm.h"
37 /* FIXME: after rewriting, we should also remap attributes ... */
40 * massages "in" and normalizes it into "ndn"
42 * "ndn" may be untouched if no massaging occurred and its value was not null
44 int
45 rwm_dn_massage_normalize(
46 dncookie *dc,
47 struct berval *in,
48 struct berval *ndn )
50 int rc;
51 struct berval mdn = BER_BVNULL;
53 /* massage and normalize a DN */
54 rc = rwm_dn_massage( dc, in, &mdn );
55 if ( rc != LDAP_SUCCESS ) {
56 return rc;
59 if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( ndn ) ) {
60 return rc;
63 rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
65 if ( mdn.bv_val != in->bv_val ) {
66 ch_free( mdn.bv_val );
69 return rc;
73 * massages "in" and prettifies it into "pdn"
75 * "pdn" may be untouched if no massaging occurred and its value was not null
77 int
78 rwm_dn_massage_pretty(
79 dncookie *dc,
80 struct berval *in,
81 struct berval *pdn )
83 int rc;
84 struct berval mdn = BER_BVNULL;
86 /* massage and pretty a DN */
87 rc = rwm_dn_massage( dc, in, &mdn );
88 if ( rc != LDAP_SUCCESS ) {
89 return rc;
92 if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
93 return rc;
96 rc = dnPretty( NULL, &mdn, pdn, NULL );
98 if ( mdn.bv_val != in->bv_val ) {
99 ch_free( mdn.bv_val );
102 return rc;
106 * massages "in" and prettifies and normalizes it into "pdn" and "ndn"
108 * "pdn" may be untouched if no massaging occurred and its value was not null;
109 * "ndn" may be untouched if no massaging occurred and its value was not null;
110 * if no massage occurred and "ndn" value was not null, it is filled
111 * with the normaized value of "pdn", much like ndn = dnNormalize( pdn )
114 rwm_dn_massage_pretty_normalize(
115 dncookie *dc,
116 struct berval *in,
117 struct berval *pdn,
118 struct berval *ndn )
120 int rc;
121 struct berval mdn = BER_BVNULL;
123 /* massage, pretty and normalize a DN */
124 rc = rwm_dn_massage( dc, in, &mdn );
125 if ( rc != LDAP_SUCCESS ) {
126 return rc;
129 if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
130 if ( BER_BVISNULL( ndn ) ) {
131 rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
133 return rc;
136 rc = dnPrettyNormal( NULL, &mdn, pdn, ndn, NULL );
138 if ( mdn.bv_val != in->bv_val ) {
139 ch_free( mdn.bv_val );
142 return rc;
146 * massages "in" into "dn"
148 * "dn" may contain the value of "in" if no massage occurred
151 rwm_dn_massage(
152 dncookie *dc,
153 struct berval *in,
154 struct berval *dn
157 int rc = 0;
158 struct berval mdn;
159 static char *dmy = "";
160 char *in_val;
162 assert( dc != NULL );
163 assert( in != NULL );
164 assert( dn != NULL );
166 /* protect from NULL berval */
167 in_val = in->bv_val ? in->bv_val : dmy;
169 rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
170 in_val, dc->conn, &mdn.bv_val );
171 switch ( rc ) {
172 case REWRITE_REGEXEC_OK:
173 if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != in_val ) {
174 mdn.bv_len = strlen( mdn.bv_val );
175 *dn = mdn;
176 } else {
177 dn->bv_len = in->bv_len;
178 dn->bv_val = in_val;
180 rc = LDAP_SUCCESS;
182 Debug( LDAP_DEBUG_ARGS,
183 "[rw] %s: \"%s\" -> \"%s\"\n",
184 dc->ctx, in_val, dn->bv_val );
185 break;
187 case REWRITE_REGEXEC_UNWILLING:
188 if ( dc->rs ) {
189 dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
190 dc->rs->sr_text = "Operation not allowed";
192 rc = LDAP_UNWILLING_TO_PERFORM;
193 break;
195 case REWRITE_REGEXEC_ERR:
196 if ( dc->rs ) {
197 dc->rs->sr_err = LDAP_OTHER;
198 dc->rs->sr_text = "Rewrite error";
200 rc = LDAP_OTHER;
201 break;
204 if ( mdn.bv_val == dmy ) {
205 BER_BVZERO( &mdn );
208 if ( dn->bv_val == dmy ) {
209 BER_BVZERO( dn );
212 return rc;
215 #endif /* SLAPD_OVER_RWM */