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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2011 PADL Software Pty Ltd.
* 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>.
*/
#include <portable.h>
#include <ac/string.h>
#include <slap.h>
#include <lutil.h>
#include <sasl/sasl.h>
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_ext.h>
#define ACL_BUF_SIZE 1024
typedef struct gssattr_t {
slap_style_t gssattr_style;
struct berval gssattr_name; /* asserted name */
struct berval gssattr_value; /* asserted value */
} gssattr_t;
static int gssattr_dynacl_destroy( void *priv );
static int
regex_matches(
struct berval *pat, /* pattern to expand and match against */
char *str, /* string to match against pattern */
struct berval *dn_matches, /* buffer with $N expansion variables from DN */
struct berval *val_matches, /* buffer with $N expansion variables from val */
AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
);
static int
gssattr_dynacl_parse(
const char *fname,
int lineno,
const char *opts,
slap_style_t style,
const char *pattern,
void **privp )
{
gssattr_t *gssattr;
gssattr = (gssattr_t *)ch_calloc( 1, sizeof( gssattr_t ) );
if ( opts == NULL || opts[0] == '\0' ) {
fprintf( stderr, "%s line %d: GSS ACL: no attribute specified.\n",
fname, lineno );
goto cleanup;
}
if ( pattern == NULL || pattern[0] == '\0' ) {
fprintf( stderr, "%s line %d: GSS ACL: no attribute value specified.\n",
fname, lineno );
goto cleanup;
}
gssattr->gssattr_style = style;
switch ( gssattr->gssattr_style ) {
case ACL_STYLE_BASE:
case ACL_STYLE_REGEX:
case ACL_STYLE_EXPAND:
break;
default:
fprintf( stderr, "%s line %d: GSS ACL: unsupported style \"%s\".\n",
fname, lineno, style_strings[style] );
goto cleanup;
break;
}
ber_str2bv( opts, 0, 1, &gssattr->gssattr_name );
ber_str2bv( pattern, 0, 1, &gssattr->gssattr_value );
*privp = (void *)gssattr;
return 0;
cleanup:
(void)gssattr_dynacl_destroy( (void *)gssattr );
return 1;
}
static int
gssattr_dynacl_unparse(
void *priv,
struct berval *bv )
{
gssattr_t *gssattr = (gssattr_t *)priv;
char *ptr;
bv->bv_len = STRLENOF( " dynacl/gss/.expand=" ) +
gssattr->gssattr_name.bv_len +
gssattr->gssattr_value.bv_len;
bv->bv_val = ch_malloc( bv->bv_len + 1 );
ptr = lutil_strcopy( bv->bv_val, " dynacl/gss/" );
ptr = lutil_strncopy( ptr, gssattr->gssattr_name.bv_val,
gssattr->gssattr_name.bv_len );
switch ( gssattr->gssattr_style ) {
case ACL_STYLE_BASE:
ptr = lutil_strcopy( ptr, ".exact=" );
break;
case ACL_STYLE_REGEX:
ptr = lutil_strcopy( ptr, ".regex=" );
break;
case ACL_STYLE_EXPAND:
ptr = lutil_strcopy( ptr, ".expand=" );
break;
default:
assert( 0 );
break;
}
ptr = lutil_strncopy( ptr, gssattr->gssattr_value.bv_val,
gssattr->gssattr_value.bv_len );
ptr[ 0 ] = '\0';
bv->bv_len = ptr - bv->bv_val;
return 0;
}
static int
gssattr_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 )
{
gssattr_t *gssattr = (gssattr_t *)priv;
sasl_conn_t *sasl_ctx = op->o_conn->c_sasl_authctx;
gss_name_t gss_name = GSS_C_NO_NAME;
OM_uint32 major, minor;
int more = -1;
int authenticated, complete;
gss_buffer_desc attr = GSS_C_EMPTY_BUFFER;
int granted = 0;
ACL_INVALIDATE( *deny );
if ( sasl_ctx == NULL ||
sasl_getprop( sasl_ctx, SASL_GSS_PEER_NAME, (const void **)&gss_name) != 0 ||
gss_name == GSS_C_NO_NAME ) {
return 0;
}
attr.length = gssattr->gssattr_name.bv_len;
attr.value = gssattr->gssattr_name.bv_val;
while ( more != 0 ) {
AclRegexMatches amatches = { 0 };
gss_buffer_desc gss_value = GSS_C_EMPTY_BUFFER;
gss_buffer_desc gss_display_value = GSS_C_EMPTY_BUFFER;
struct berval bv_value;
major = gss_get_name_attribute( &minor, gss_name, &attr,
&authenticated, &complete,
&gss_value, &gss_display_value, &more );
if ( GSS_ERROR( major ) ) {
break;
} else if ( authenticated == 0 ) {
gss_release_buffer( &minor, &gss_value );
gss_release_buffer( &minor, &gss_display_value );
continue;
}
bv_value.bv_len = gss_value.length;
bv_value.bv_val = (char *)gss_value.value;
if ( !ber_bvccmp( &gssattr->gssattr_value, '*' ) ) {
if ( gssattr->gssattr_style != ACL_STYLE_BASE ) {
amatches.dn_count = nmatch;
AC_MEMCPY( amatches.dn_data, matches, sizeof( amatches.dn_data ) );
}
switch ( gssattr->gssattr_style ) {
case ACL_STYLE_REGEX:
/* XXX assumes value NUL terminated */
granted = regex_matches( &gssattr->gssattr_value, bv_value.bv_val,
&target->e_nname, val, &amatches );
break;
case ACL_STYLE_EXPAND: {
struct berval bv;
char buf[ACL_BUF_SIZE];
bv.bv_len = sizeof( buf ) - 1;
bv.bv_val = buf;
granted = ( acl_string_expand( &bv, &gssattr->gssattr_value,
&target->e_nname, val,
&amatches ) == 0 ) &&
( ber_bvstrcmp( &bv, &bv_value) == 0 );
break;
}
case ACL_STYLE_BASE:
granted = ( ber_bvstrcmp( &gssattr->gssattr_value, &bv_value ) == 0 );
break;
default:
assert(0);
break;
}
} else {
granted = 1;
}
gss_release_buffer( &minor, &gss_value );
gss_release_buffer( &minor, &gss_display_value );
if ( granted ) {
break;
}
}
if ( granted ) {
ACL_LVL_ASSIGN_WRITE( *grant );
}
return 0;
}
static int
gssattr_dynacl_destroy(
void *priv )
{
gssattr_t *gssattr = (gssattr_t *)priv;
if ( gssattr != NULL ) {
if ( !BER_BVISNULL( &gssattr->gssattr_name ) ) {
ber_memfree( gssattr->gssattr_name.bv_val );
}
if ( !BER_BVISNULL( &gssattr->gssattr_value ) ) {
ber_memfree( gssattr->gssattr_value.bv_val );
}
ch_free( gssattr );
}
return 0;
}
static struct slap_dynacl_t gssattr_dynacl = {
"gss",
gssattr_dynacl_parse,
gssattr_dynacl_unparse,
gssattr_dynacl_mask,
gssattr_dynacl_destroy
};
int
init_module( int argc, char *argv[] )
{
return slap_dynacl_register( &gssattr_dynacl );
}
static int
regex_matches(
struct berval *pat, /* pattern to expand and match against */
char *str, /* string to match against pattern */
struct berval *dn_matches, /* buffer with $N expansion variables from DN */
struct berval *val_matches, /* buffer with $N expansion variables from val */
AclRegexMatches *matches /* offsets in buffer for $N expansion variables */
)
{
regex_t re;
char newbuf[ACL_BUF_SIZE];
struct berval bv;
int rc;
bv.bv_len = sizeof( newbuf ) - 1;
bv.bv_val = newbuf;
if (str == NULL) {
str = "";
};
acl_string_expand( &bv, pat, dn_matches, val_matches, matches );
rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
if ( rc ) {
char error[ACL_BUF_SIZE];
regerror( rc, &re, error, sizeof( error ) );
Debug( LDAP_DEBUG_TRACE,
"compile( \"%s\", \"%s\") failed %s\n",
pat->bv_val, str, error );
return( 0 );
}
rc = regexec( &re, str, 0, NULL, 0 );
regfree( &re );
Debug( LDAP_DEBUG_TRACE,
"=> regex_matches: string: %s\n", str, 0, 0 );
Debug( LDAP_DEBUG_TRACE,
"=> regex_matches: rc: %d %s\n",
rc, !rc ? "matches" : "no matches", 0 );
return( !rc );
}
|