summaryrefslogtreecommitdiffstats
path: root/contrib/slapd-modules/allop/allop.c
blob: 52fab3abe240cf72628b0996ed9158722bf649ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* allop.c - returns all operational attributes when appropriate */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2005-2022 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Pierangelo Masarati for inclusion in
 * OpenLDAP Software.
 */

/*
 * The intended usage is as a global overlay for use with those clients
 * that do not make use of the RFC3673 allOp ("+") in the requested 
 * attribute list, but expect all operational attributes to be returned.
 * Usage: add
 *

overlay		allop
allop-URI	<ldapURI>

 *
 * if the allop-URI is not given, the rootDSE, i.e. "ldap:///??base",
 * is assumed.
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>

#include "slap.h"
#include "slap-config.h"

#define	SLAP_OVER_VERSION_REQUIRE(major,minor,patch) \
	( \
		( LDAP_VENDOR_VERSION_MAJOR == X || LDAP_VENDOR_VERSION_MAJOR >= (major) ) \
		&& ( LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR >= (minor) ) \
		&& ( LDAP_VENDOR_VERSION_PATCH == X || LDAP_VENDOR_VERSION_PATCH >= (patch) ) \
	)

#if !SLAP_OVER_VERSION_REQUIRE(2,3,0)
#error "version mismatch"
#endif

typedef struct allop_t {
	struct berval	ao_ndn;
	int		ao_scope;
} allop_t;

static int
allop_db_config(
	BackendDB	*be,
	const char	*fname,
	int		lineno,
	int		argc,
	char		**argv )
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	allop_t		*ao = (allop_t *)on->on_bi.bi_private;

	if ( strcasecmp( argv[ 0 ], "allop-uri" ) == 0 ) {
		LDAPURLDesc	*lud;
		struct berval	dn,
				ndn;
		int		scope,
				rc = LDAP_SUCCESS;

		if ( argc != 2 ) {
			fprintf( stderr, "%s line %d: "
				"need exactly 1 arg "
				"in \"allop-uri <ldapURI>\" "
				"directive.\n",
				fname, lineno );
			return 1;
		}

		if ( ldap_url_parse( argv[ 1 ], &lud ) != LDAP_URL_SUCCESS ) {
			return -1;
		}

		scope = lud->lud_scope;
		if ( scope == LDAP_SCOPE_DEFAULT ) {
			scope = LDAP_SCOPE_BASE;
		}

		if ( lud->lud_dn == NULL || lud->lud_dn[ 0 ] == '\0' ) {
			if ( scope == LDAP_SCOPE_BASE ) {
				BER_BVZERO( &ndn );

			} else {
				ber_str2bv( "", 0, 1, &ndn );
			}

		} else {

			ber_str2bv( lud->lud_dn, 0, 0, &dn );
			rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
		}

		ldap_free_urldesc( lud );
		if ( rc != LDAP_SUCCESS ) {
			return -1;
		}

		if ( BER_BVISNULL( &ndn ) ) {
			/* rootDSE */
			if ( ao != NULL ) {
				ch_free( ao->ao_ndn.bv_val );
				ch_free( ao );
				on->on_bi.bi_private = NULL;
			}

		} else {
			if ( ao == NULL ) {
				ao = ch_calloc( 1, sizeof( allop_t ) );
				on->on_bi.bi_private = (void *)ao;

			} else {
				ch_free( ao->ao_ndn.bv_val );
			}

			ao->ao_ndn = ndn;
			ao->ao_scope = scope;
		}

	} else {
		return SLAP_CONF_UNKNOWN;
	}

	return 0;
}

static int
allop_db_destroy( BackendDB *be, ConfigReply *cr )
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	allop_t		*ao = (allop_t *)on->on_bi.bi_private;

	if ( ao != NULL ) {
		assert( !BER_BVISNULL( &ao->ao_ndn ) );

		ch_free( ao->ao_ndn.bv_val );
		ch_free( ao );
		on->on_bi.bi_private = NULL;
	}

	return 0;
}

static int
allop_op_search( Operation *op, SlapReply *rs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	allop_t		*ao = (allop_t *)on->on_bi.bi_private;

	slap_mask_t	mask;
	int		i,
			add_allUser = 0;

	if ( ao == NULL ) {
		if ( !BER_BVISEMPTY( &op->o_req_ndn )
			|| op->ors_scope != LDAP_SCOPE_BASE )
		{
			return SLAP_CB_CONTINUE;
		}

	} else {
		if ( !dnIsSuffix( &op->o_req_ndn, &ao->ao_ndn ) ) {
			return SLAP_CB_CONTINUE;
		}

		switch ( ao->ao_scope ) {
		case LDAP_SCOPE_BASE:
			if ( op->o_req_ndn.bv_len != ao->ao_ndn.bv_len ) {
				return SLAP_CB_CONTINUE;
			}
			break;

		case LDAP_SCOPE_ONELEVEL:
			if ( op->ors_scope == LDAP_SCOPE_BASE ) {
				struct berval	rdn = op->o_req_ndn;

				rdn.bv_len -= ao->ao_ndn.bv_len + STRLENOF( "," );
				if ( !dnIsOneLevelRDN( &rdn ) ) {
					return SLAP_CB_CONTINUE;
				}

				break;
			}
			return SLAP_CB_CONTINUE;

		case LDAP_SCOPE_SUBTREE:
			break;
		}
	}

	mask = slap_attr_flags( op->ors_attrs );
	if ( SLAP_OPATTRS( mask ) ) {
		return SLAP_CB_CONTINUE;
	}

	if ( !SLAP_USERATTRS( mask ) ) {
		return SLAP_CB_CONTINUE;
	}

	i = 0;
	if ( op->ors_attrs == NULL ) {
		add_allUser = 1;

	} else {
		for ( ; !BER_BVISNULL( &op->ors_attrs[ i ].an_name ); i++ )
			;
	}

	op->ors_attrs = op->o_tmprealloc( op->ors_attrs,
		sizeof( AttributeName ) * ( i + add_allUser + 2 ),
		op->o_tmpmemctx );

	if ( add_allUser ) {
		op->ors_attrs[ i ] = slap_anlist_all_user_attributes[ 0 ];
		i++;
	}

	op->ors_attrs[ i ] = slap_anlist_all_operational_attributes[ 0 ];

	BER_BVZERO( &op->ors_attrs[ i + 1 ].an_name );

	return SLAP_CB_CONTINUE;
}

static slap_overinst 		allop;

int
allop_init()
{
	allop.on_bi.bi_type = "allop";

	allop.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
	allop.on_bi.bi_db_config = allop_db_config;
	allop.on_bi.bi_db_destroy = allop_db_destroy;

	allop.on_bi.bi_op_search = allop_op_search;

	return overlay_register( &allop );
}

int
init_module( int argc, char *argv[] )
{
	return allop_init();
}