rwmdn.c revision 1.1.1.3.12.1 1 1.1.1.2 lukem /* $NetBSD: rwmdn.c,v 1.1.1.3.12.1 2014/08/19 23:52:03 tls Exp $ */
2 1.1.1.2 lukem
3 1.1 lukem /* rwmdn.c - massages dns */
4 1.1.1.3.12.1 tls /* $OpenLDAP$ */
5 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 1.1 lukem *
7 1.1.1.3.12.1 tls * Copyright 1999-2014 The OpenLDAP Foundation.
8 1.1 lukem * Portions Copyright 1999-2003 Howard Chu.
9 1.1 lukem * Portions Copyright 2000-2003 Pierangelo Masarati.
10 1.1 lukem * All rights reserved.
11 1.1 lukem *
12 1.1 lukem * Redistribution and use in source and binary forms, with or without
13 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
14 1.1 lukem * Public License.
15 1.1 lukem *
16 1.1 lukem * A copy of this license is available in the file LICENSE in the
17 1.1 lukem * top-level directory of the distribution or, alternatively, at
18 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
19 1.1 lukem */
20 1.1 lukem /* ACKNOWLEDGEMENTS:
21 1.1 lukem * This work was initially developed by Howard Chu for inclusion
22 1.1 lukem * in OpenLDAP Software and subsequently enhanced by Pierangelo
23 1.1 lukem * Masarati.
24 1.1 lukem */
25 1.1 lukem
26 1.1 lukem
27 1.1 lukem #include "portable.h"
28 1.1 lukem
29 1.1 lukem #ifdef SLAPD_OVER_RWM
30 1.1 lukem
31 1.1 lukem #include <stdio.h>
32 1.1 lukem
33 1.1 lukem #include <ac/string.h>
34 1.1 lukem #include <ac/socket.h>
35 1.1 lukem
36 1.1 lukem #include "slap.h"
37 1.1 lukem #include "rwm.h"
38 1.1 lukem
39 1.1 lukem /* FIXME: after rewriting, we should also remap attributes ... */
40 1.1 lukem
41 1.1 lukem /*
42 1.1 lukem * massages "in" and normalizes it into "ndn"
43 1.1 lukem *
44 1.1 lukem * "ndn" may be untouched if no massaging occurred and its value was not null
45 1.1 lukem */
46 1.1 lukem int
47 1.1 lukem rwm_dn_massage_normalize(
48 1.1 lukem dncookie *dc,
49 1.1 lukem struct berval *in,
50 1.1 lukem struct berval *ndn )
51 1.1 lukem {
52 1.1 lukem int rc;
53 1.1 lukem struct berval mdn = BER_BVNULL;
54 1.1 lukem
55 1.1 lukem /* massage and normalize a DN */
56 1.1 lukem rc = rwm_dn_massage( dc, in, &mdn );
57 1.1 lukem if ( rc != LDAP_SUCCESS ) {
58 1.1 lukem return rc;
59 1.1 lukem }
60 1.1 lukem
61 1.1 lukem if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( ndn ) ) {
62 1.1 lukem return rc;
63 1.1 lukem }
64 1.1 lukem
65 1.1 lukem rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
66 1.1 lukem
67 1.1 lukem if ( mdn.bv_val != in->bv_val ) {
68 1.1 lukem ch_free( mdn.bv_val );
69 1.1 lukem }
70 1.1 lukem
71 1.1 lukem return rc;
72 1.1 lukem }
73 1.1 lukem
74 1.1 lukem /*
75 1.1 lukem * massages "in" and prettifies it into "pdn"
76 1.1 lukem *
77 1.1 lukem * "pdn" may be untouched if no massaging occurred and its value was not null
78 1.1 lukem */
79 1.1 lukem int
80 1.1 lukem rwm_dn_massage_pretty(
81 1.1 lukem dncookie *dc,
82 1.1 lukem struct berval *in,
83 1.1 lukem struct berval *pdn )
84 1.1 lukem {
85 1.1 lukem int rc;
86 1.1 lukem struct berval mdn = BER_BVNULL;
87 1.1 lukem
88 1.1 lukem /* massage and pretty a DN */
89 1.1 lukem rc = rwm_dn_massage( dc, in, &mdn );
90 1.1 lukem if ( rc != LDAP_SUCCESS ) {
91 1.1 lukem return rc;
92 1.1 lukem }
93 1.1 lukem
94 1.1 lukem if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
95 1.1 lukem return rc;
96 1.1 lukem }
97 1.1 lukem
98 1.1 lukem rc = dnPretty( NULL, &mdn, pdn, NULL );
99 1.1 lukem
100 1.1 lukem if ( mdn.bv_val != in->bv_val ) {
101 1.1 lukem ch_free( mdn.bv_val );
102 1.1 lukem }
103 1.1 lukem
104 1.1 lukem return rc;
105 1.1 lukem }
106 1.1 lukem
107 1.1 lukem /*
108 1.1 lukem * massages "in" and prettifies and normalizes it into "pdn" and "ndn"
109 1.1 lukem *
110 1.1 lukem * "pdn" may be untouched if no massaging occurred and its value was not null;
111 1.1 lukem * "ndn" may be untouched if no massaging occurred and its value was not null;
112 1.1 lukem * if no massage occurred and "ndn" value was not null, it is filled
113 1.1 lukem * with the normaized value of "pdn", much like ndn = dnNormalize( pdn )
114 1.1 lukem */
115 1.1 lukem int
116 1.1 lukem rwm_dn_massage_pretty_normalize(
117 1.1 lukem dncookie *dc,
118 1.1 lukem struct berval *in,
119 1.1 lukem struct berval *pdn,
120 1.1 lukem struct berval *ndn )
121 1.1 lukem {
122 1.1 lukem int rc;
123 1.1 lukem struct berval mdn = BER_BVNULL;
124 1.1 lukem
125 1.1 lukem /* massage, pretty and normalize a DN */
126 1.1 lukem rc = rwm_dn_massage( dc, in, &mdn );
127 1.1 lukem if ( rc != LDAP_SUCCESS ) {
128 1.1 lukem return rc;
129 1.1 lukem }
130 1.1 lukem
131 1.1 lukem if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
132 1.1 lukem if ( BER_BVISNULL( ndn ) ) {
133 1.1 lukem rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
134 1.1 lukem }
135 1.1 lukem return rc;
136 1.1 lukem }
137 1.1 lukem
138 1.1 lukem rc = dnPrettyNormal( NULL, &mdn, pdn, ndn, NULL );
139 1.1 lukem
140 1.1 lukem if ( mdn.bv_val != in->bv_val ) {
141 1.1 lukem ch_free( mdn.bv_val );
142 1.1 lukem }
143 1.1 lukem
144 1.1 lukem return rc;
145 1.1 lukem }
146 1.1 lukem
147 1.1 lukem /*
148 1.1 lukem * massages "in" into "dn"
149 1.1 lukem *
150 1.1 lukem * "dn" may contain the value of "in" if no massage occurred
151 1.1 lukem */
152 1.1 lukem int
153 1.1 lukem rwm_dn_massage(
154 1.1 lukem dncookie *dc,
155 1.1 lukem struct berval *in,
156 1.1 lukem struct berval *dn
157 1.1 lukem )
158 1.1 lukem {
159 1.1 lukem int rc = 0;
160 1.1 lukem struct berval mdn;
161 1.1 lukem static char *dmy = "";
162 1.1 lukem char *in_val;
163 1.1 lukem
164 1.1 lukem assert( dc != NULL );
165 1.1 lukem assert( in != NULL );
166 1.1 lukem assert( dn != NULL );
167 1.1 lukem
168 1.1 lukem /* protect from NULL berval */
169 1.1 lukem in_val = in->bv_val ? in->bv_val : dmy;
170 1.1 lukem
171 1.1 lukem rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
172 1.1 lukem in_val, dc->conn, &mdn.bv_val );
173 1.1 lukem switch ( rc ) {
174 1.1 lukem case REWRITE_REGEXEC_OK:
175 1.1 lukem if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != in_val ) {
176 1.1 lukem mdn.bv_len = strlen( mdn.bv_val );
177 1.1 lukem *dn = mdn;
178 1.1 lukem } else {
179 1.1 lukem dn->bv_len = in->bv_len;
180 1.1 lukem dn->bv_val = in_val;
181 1.1 lukem }
182 1.1 lukem rc = LDAP_SUCCESS;
183 1.1 lukem
184 1.1 lukem Debug( LDAP_DEBUG_ARGS,
185 1.1 lukem "[rw] %s: \"%s\" -> \"%s\"\n",
186 1.1 lukem dc->ctx, in_val, dn->bv_val );
187 1.1 lukem break;
188 1.1 lukem
189 1.1 lukem case REWRITE_REGEXEC_UNWILLING:
190 1.1 lukem if ( dc->rs ) {
191 1.1 lukem dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
192 1.1 lukem dc->rs->sr_text = "Operation not allowed";
193 1.1 lukem }
194 1.1 lukem rc = LDAP_UNWILLING_TO_PERFORM;
195 1.1 lukem break;
196 1.1 lukem
197 1.1 lukem case REWRITE_REGEXEC_ERR:
198 1.1 lukem if ( dc->rs ) {
199 1.1 lukem dc->rs->sr_err = LDAP_OTHER;
200 1.1 lukem dc->rs->sr_text = "Rewrite error";
201 1.1 lukem }
202 1.1 lukem rc = LDAP_OTHER;
203 1.1 lukem break;
204 1.1 lukem }
205 1.1 lukem
206 1.1 lukem if ( mdn.bv_val == dmy ) {
207 1.1 lukem BER_BVZERO( &mdn );
208 1.1 lukem }
209 1.1 lukem
210 1.1 lukem if ( dn->bv_val == dmy ) {
211 1.1 lukem BER_BVZERO( dn );
212 1.1 lukem }
213 1.1 lukem
214 1.1 lukem return rc;
215 1.1 lukem }
216 1.1 lukem
217 1.1 lukem #endif /* SLAPD_OVER_RWM */
218