2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2024 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Ondřej Kuzník for inclusion in OpenLDAP
22 #define LDAP_DEPRECATED 1
23 #include "rewrite-int.h"
24 #include "rewrite-map.h"
28 typedef int (escape_fn
)( struct berval
*input
, struct berval
*output
);
31 * Map configuration, a NULL-terminated list of escape_fn pointers
33 struct escape_map_data
{
38 * (un)escape functions
42 map_escape_to_filter( struct berval
*input
, struct berval
*output
)
44 return ldap_bv2escaped_filter_value( input
, output
);
48 map_unescape_filter( struct berval
*input
, struct berval
*output
)
52 if ( ber_dupbv( output
, input
) == NULL
) {
56 len
= ldap_pvt_filter_value_unescape( output
->bv_val
);
58 ber_memfree( output
->bv_val
);
67 map_escape_to_dn( struct berval
*input
, struct berval
*output
)
69 LDAPAVA ava
= { .la_attr
= BER_BVC("uid"),
71 .la_flags
= LDAP_AVA_STRING
},
72 *ava_
[] = { &ava
, NULL
};
73 LDAPRDN rdn
[] = { ava_
, NULL
};
79 rc
= ldap_dn2bv( dn
, &dnstr
, LDAP_DN_FORMAT_LDAPV3
);
80 if ( rc
!= LDAP_SUCCESS
) {
84 p
= strchr( dnstr
.bv_val
, '=' );
87 output
->bv_len
= dnstr
.bv_len
- ( p
- dnstr
.bv_val
);
88 output
->bv_val
= malloc( output
->bv_len
+ 1 );
89 if ( output
->bv_val
== NULL
) {
93 memcpy( output
->bv_val
, p
, output
->bv_len
);
94 output
->bv_val
[output
->bv_len
] = '\0';
97 return REWRITE_SUCCESS
;
101 map_unescape_dn( struct berval
*input
, struct berval
*output
)
104 struct berval fake_dn
;
106 int rc
= REWRITE_SUCCESS
;
108 fake_dn
.bv_len
= STRLENOF("uid=") + input
->bv_len
;
109 fake_dn
.bv_val
= p
= malloc( fake_dn
.bv_len
+ 1 );
114 memcpy( p
, "uid=", STRLENOF("uid=") );
115 p
+= STRLENOF("uid=");
116 memcpy( p
, input
->bv_val
, input
->bv_len
);
117 fake_dn
.bv_val
[fake_dn
.bv_len
] = '\0';
119 if ( ldap_bv2dn( &fake_dn
, &dn
, LDAP_DN_FORMAT_LDAPV3
) != LDAP_SUCCESS
) {
120 free( fake_dn
.bv_val
);
123 if ( ber_dupbv( output
, &dn
[0][0]->la_value
) == NULL
) {
127 free( fake_dn
.bv_val
);
131 /* Registered callbacks */
144 assert( fname
!= NULL
);
145 assert( argv
!= NULL
);
148 Debug( LDAP_DEBUG_ANY
,
149 "[%s:%d] escape map needs at least one operation\n",
154 fns
= calloc( sizeof(escape_fn
*), argc
+ 1 );
159 for ( i
= 0; i
< argc
; i
++ ) {
160 if ( strcasecmp( argv
[i
], "escape2dn" ) == 0 ) {
161 fns
[i
] = map_escape_to_dn
;
162 } else if ( strcasecmp( argv
[i
], "escape2filter" ) == 0 ) {
163 fns
[i
] = map_escape_to_filter
;
164 } else if ( strcasecmp( argv
[i
], "unescapedn" ) == 0 ) {
165 fns
[i
] = map_unescape_dn
;
166 } else if ( strcasecmp( argv
[i
], "unescapefilter" ) == 0 ) {
167 fns
[i
] = map_unescape_filter
;
169 Debug( LDAP_DEBUG_ANY
,
170 "[%s:%d] unknown option %s (ignored)\n",
171 fname
, lineno
, argv
[i
] );
184 struct berval
*output
)
186 escape_fn
**fns
= private;
187 struct berval tmpin
, tmpout
= BER_BVNULL
;
190 assert( private != NULL
);
191 assert( input
!= NULL
);
192 assert( output
!= NULL
);
194 ber_str2bv( input
, 0, 1, &tmpin
);
196 for ( i
=0; fns
[i
]; i
++ ) {
197 int rc
= fns
[i
]( &tmpin
, &tmpout
);
198 free( tmpin
.bv_val
);
199 if ( rc
!= REWRITE_SUCCESS
) {
203 BER_BVZERO( &tmpout
);
207 return REWRITE_SUCCESS
;
215 struct ldap_map_data
*data
= private;
217 assert( private != NULL
);
223 const rewrite_mapper rewrite_escape_mapper
= {