summaryrefslogtreecommitdiffstats
path: root/contrib/slapd-modules/acl/now.c
blob: 582c01fbc438b62ca2ff69620a433ce6cf0a4934 (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
/* $OpenLDAP$ */
/*
 * Copyright 1998-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.
 */
/*
 * This dynacl module compares the value of a given attribute type
 * with the current time.  The syntax is
 *
 *	dynacl/now=<=attr
 *
 * where attr is an attribute whose syntax is generalizedTime
 * with generalizedTimeOrderingMatch as ORDERING rule.
 */ 

#include <portable.h>

#include <ac/string.h>
#include <slap.h>
#include <lutil.h>

/* Need dynacl... */

#ifdef SLAP_DYNACL

typedef enum {
	NOW_GE,
	NOW_LE
} now_style_t;

typedef struct now_t {
	AttributeDescription	*now_ad;
	now_style_t		now_style;
} now_t;

static int now_dynacl_destroy( void *priv );

static int
now_dynacl_parse(
	const char	*fname,
	int 		lineno,
	const char	*opts,
	slap_style_t	style,
	const char	*pattern,
	void		**privp )
{
	now_t			*now;
	now_style_t		sty = NOW_GE;
	AttributeDescription	*ad = NULL;
	int			rc;
	const char		*text = NULL;
	Syntax			*syn;
	MatchingRule		*mr;

	syn = syn_find( "1.3.6.1.4.1.1466.115.121.1.24" );
	if ( syn == NULL ) {
		fprintf( stderr,
			"%s line %d: unable to find syntax 1.3.6.1.4.1.1466.115.121.1.24 (generalizedTime)\n",
			fname, lineno );
		return 1;
	}

	mr = mr_find( "generalizedTimeOrderingMatch" );
	if ( mr == NULL ) {
		fprintf( stderr,
			"%s line %d: unable to find generalizedTimeOrderingMatch rule\n",
			fname, lineno );
		return 1;
	}

	if ( strncmp( pattern, ">=", STRLENOF( ">=" ) ) == 0 ) {
		sty = NOW_GE;
		pattern += 2;

	} else if ( strncmp( pattern, "<=", STRLENOF( "<=" ) ) == 0 ) {
		sty = NOW_LE;
		pattern += 2;
	}

	rc = slap_str2ad( pattern, &ad, &text );
	if ( rc != LDAP_SUCCESS ) {
		fprintf( stderr, "%s line %d: now ACL: "
			"unable to lookup \"%s\" "
			"attributeDescription (%d: %s).\n",
			fname, lineno, pattern, rc, text );
		return 1;
	}

	if ( ad->ad_type->sat_syntax != syn ) {
		fprintf( stderr,
			"%s line %d: syntax of attribute \"%s\" is not 1.3.6.1.4.1.1466.115.121.1.24 (generalizedTime)\n",
			fname, lineno, ad->ad_cname.bv_val );
		return 1;
	}

	if ( ad->ad_type->sat_ordering != mr ) {
		fprintf( stderr,
			"%s line %d: ordering matching rule of attribute \"%s\" is not generalizedTimeOrderingMatch\n",
			fname, lineno, ad->ad_cname.bv_val );
		return 1;
	}

	now = ch_calloc( 1, sizeof( now_t ) );
	now->now_ad = ad;
	now->now_style = sty;

	*privp = (void *)now;

	return 0;
}

static int
now_dynacl_unparse(
	void		*priv,
	struct berval	*bv )
{
	now_t		*now = (now_t *)priv;
	char		*ptr;

	bv->bv_len = STRLENOF( " dynacl/now=" ) + 2 + now->now_ad->ad_cname.bv_len;
	bv->bv_val = ch_malloc( bv->bv_len + 1 );

	ptr = lutil_strcopy( bv->bv_val, " dynacl/now=" );
	ptr[ 0 ] = now->now_style == NOW_GE ? '>' : '<';
	ptr[ 1 ] = '=';
	ptr += 2;
	ptr = lutil_strncopy( ptr, now->now_ad->ad_cname.bv_val, now->now_ad->ad_cname.bv_len );
	ptr[ 0 ] = '\0';

	bv->bv_len = ptr - bv->bv_val;

	return 0;
}

static int
now_dynacl_mask(
	void			*priv,
	Operation		*op,
	Entry			*target,
	AttributeDescription	*desc,
	struct berval		*val,
	int			nmatch,
	regmatch_t		*matches,
	slap_access_t		*grant,
	slap_access_t		*deny )
{
	now_t		*now = (now_t *)priv;
	int		rc;
	Attribute	*a;

	ACL_INVALIDATE( *deny );

	assert( target != NULL );

	a = attr_find( target->e_attrs, now->now_ad );
	if ( !a ) {
		rc = LDAP_NO_SUCH_ATTRIBUTE;

	} else {
		char		timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
		struct berval	timestamp;
		time_t		t = slap_get_time();
		int		match;
		MatchingRule	*mr = now->now_ad->ad_type->sat_ordering;
		const char	*text = NULL;

		timestamp.bv_val = timebuf;
		timestamp.bv_len = sizeof( timebuf );

		slap_timestamp( &t, &timestamp );

		rc = value_match( &match, now->now_ad, mr, SLAP_MR_ORDERING,
			&timestamp, &a->a_vals[ 0 ], &text );
		if ( rc == LDAP_SUCCESS ) {
			if ( now->now_style == NOW_LE ) {
				match = -match;
			}

			if ( match >= 0 ) {
				rc = LDAP_COMPARE_TRUE;

			} else {
				rc = LDAP_COMPARE_FALSE;
			}
		}
	}

	if ( rc == LDAP_COMPARE_TRUE ) {
		ACL_LVL_ASSIGN_WRITE( *grant );
	}

	return 0;
}

static int
now_dynacl_destroy(
	void		*priv )
{
	now_t		*now = (now_t *)priv;

	if ( now != NULL ) {
		ch_free( now );
	}

	return 0;
}

static struct slap_dynacl_t now_dynacl = {
	"now",
	now_dynacl_parse,
	now_dynacl_unparse,
	now_dynacl_mask,
	now_dynacl_destroy
};

int
init_module( int argc, char *argv[] )
{
	return slap_dynacl_register( &now_dynacl );
}

#endif /* SLAP_DYNACL */