]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
04fc7d9facfa2f59e236807f5472ea7619600553
[openldap] / servers / slapd / back-meta / config.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
6  *
7  * This work has been developed to fulfill the requirements
8  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9  * to the OpenLDAP Foundation in the hope that it may be useful
10  * to the Open Source community, but WITHOUT ANY WARRANTY.
11  *
12  * Permission is granted to anyone to use this software for any purpose
13  * on any computer system, and to alter it and redistribute it, subject
14  * to the following restrictions:
15  *
16  * 1. The author and SysNet s.n.c. are not responsible for the consequences
17  *    of use of this software, no matter how awful, even if they arise from 
18  *    flaws in it.
19  *
20  * 2. The origin of this software must not be misrepresented, either by
21  *    explicit claim or by omission.  Since few users ever read sources,
22  *    credits should appear in the documentation.
23  *
24  * 3. Altered versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.  Since few users
26  *    ever read sources, credits should appear in the documentation.
27  *    SysNet s.n.c. cannot be responsible for the consequences of the
28  *    alterations.
29  *
30  * 4. This notice may not be removed or altered.
31  *
32  *
33  * This software is based on the backend back-ldap, implemented
34  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36  * contributors. The contribution of the original software to the present
37  * implementation is acknowledged in this copyright statement.
38  *
39  * A special acknowledgement goes to Howard for the overall architecture
40  * (and for borrowing large pieces of code), and to Mark, who implemented
41  * from scratch the attribute/objectclass mapping.
42  *
43  * The original copyright statement follows.
44  *
45  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
46  *
47  * Permission is granted to anyone to use this software for any purpose
48  * on any computer system, and to alter it and redistribute it, subject
49  * to the following restrictions:
50  *
51  * 1. The author is not responsible for the consequences of use of this
52  *    software, no matter how awful, even if they arise from flaws in it.
53  *
54  * 2. The origin of this software must not be misrepresented, either by
55  *    explicit claim or by omission.  Since few users ever read sources,
56  *    credits should appear in the documentation.
57  *
58  * 3. Altered versions must be plainly marked as such, and must not be
59  *    misrepresented as being the original software.  Since few users
60  *    ever read sources, credits should appear in the
61  *    documentation.
62  *
63  * 4. This notice may not be removed or altered.
64  *
65  */
66
67 #include "portable.h"
68
69 #include <stdio.h>
70
71 #include <ac/string.h>
72 #include <ac/socket.h>
73
74 #include "slap.h"
75 #include "../back-ldap/back-ldap.h"
76 #include "../../../libraries/libldap/ldap-int.h"
77 #include "back-meta.h"
78
79 static struct metatarget *
80 new_target( void )
81 {
82         struct metatarget *lt;
83         struct ldapmapping *mapping;
84
85         lt = ch_calloc( sizeof( struct metatarget ), 1 );
86         if ( lt == NULL ) {
87                 return NULL;
88         }
89
90         lt->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
91         if ( lt->rwinfo == NULL ) {
92                 free( lt );
93                 return NULL;
94         }
95
96         ldap_back_map_init( &lt->at_map, &mapping );
97
98         return lt;
99 }
100
101 int
102 meta_back_db_config(
103                 BackendDB       *be,
104                 const char      *fname,
105                 int             lineno,
106                 int             argc,
107                 char            **argv
108 )
109 {
110         struct metainfo *li = ( struct metainfo * )be->be_private;
111
112         if ( li == NULL ) {
113                 fprintf( stderr, 
114         "%s: line %d: meta backend info is null!\n",
115                     fname, lineno );
116                 return 1;
117         }
118
119         /* URI of server to query */
120         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
121                 int             i = li->ntargets;
122 #if 0
123                 int             j;
124 #endif /* uncomment if uri MUST be a branch of suffix */
125                 LDAPURLDesc     *ludp, *tmpludp;
126                 struct berval   dn;
127                 int             rc;
128                 
129                 if ( argc != 2 ) {
130                         fprintf( stderr,
131         "%s: line %d: missing address"
132         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
133                                 fname, lineno );
134                         return 1;
135                 }
136                 
137                 ++li->ntargets;
138
139                 li->targets = ch_realloc( li->targets, 
140                         sizeof( struct metatarget *)*li->ntargets );
141                 if ( li->targets == NULL ) {
142                         fprintf( stderr,
143         "%s: line %d: out of memory while storing server name"
144         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
145                                 fname, lineno );
146                         return 1;
147                 }
148
149                 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
150                         fprintf( stderr,
151         "%s: line %d: unable to init server"
152         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
153                                 fname, lineno );
154                         return 1;
155                 }
156
157                 /*
158                  * uri MUST be legal!
159                  */
160                 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
161                         fprintf( stderr,
162         "%s: line %d: unable to parse URI"
163         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
164                                 fname, lineno );
165                         return 1;
166                 }
167
168                 /*
169                  * uri MUST have the <dn> part!
170                  */
171                 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
172                         fprintf( stderr,
173         "%s: line %d: missing <naming context> "
174         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
175                                 fname, lineno );
176                         return 1;
177                 }
178
179                 /*
180                  * copies and stores uri and suffix
181                  */
182                 dn.bv_val = ludp->lud_dn;
183                 dn.bv_len = strlen( ludp->lud_dn );
184
185                 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
186                         &li->targets[ i ]->suffix );
187                 if( rc != LDAP_SUCCESS ) {
188                         fprintf( stderr, "%s: line %d: "
189                                         "target '%s' DN is invalid\n",
190                                         fname, lineno, argv[ 1 ] );
191                         return( 1 );
192                 }
193
194                 ludp->lud_dn[ 0 ] = '\0';
195
196                 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
197                         if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
198                                 fprintf( stderr, "%s: line %d: "
199                                                 "multiple URIs must have no DN part\n",
200                                         fname, lineno, argv[ 1 ] );
201                                 return( 1 );
202
203                         }
204                 }
205
206                 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
207                 ldap_free_urllist( ludp );
208                 if ( li->targets[ i ]->uri == NULL) {
209                         fprintf( stderr, "%s: line %d: no memory?\n",
210                                         fname, lineno );
211                         return( 1 );
212                 }
213                 
214                 /*
215                  * uri MUST be a branch of suffix!
216                  */
217 #if 0 /* too strict a constraint */
218                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
219                         fprintf( stderr,
220         "%s: line %d: <naming context> of URI does not refer to current backend"
221         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
222                                 fname, lineno );
223                         return 1;
224                 }
225 #else
226                 /*
227                  * uri MUST be a branch of a suffix!
228                  */
229                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
230                         fprintf( stderr,
231         "%s: line %d: <naming context> of URI does not resolve to a backend"
232         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
233                                 fname, lineno );
234                         return 1;
235                 }
236 #endif
237
238 #if 0
239                 /*
240                  * uri MUST not be used by other URIs!
241                  *
242                  * FIXME: this limitation may be removed,
243                  * or worked out, at least, in some manner
244                  */
245                 for ( j = 0; j < i-1; j++ ) {
246                         if ( dn_match( &li->targets[ i ]->suffix,
247                                         &li->targets[ j ]->suffix ) ) {
248                                 fprintf( stderr,
249         "%s: line %d: naming context \"%s\" already used"
250         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
251                                         fname, lineno, last+1 );
252                                 return 1;
253                         }
254                 }
255 #endif
256
257 #if 0
258                 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
259                         fname, lineno, li->targets[ i ]->uri, 
260                         li->targets[ i ]->psuffix.bv_val );
261 #endif
262                 
263         /* default target directive */
264         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
265                 int             i = li->ntargets-1;
266                 
267                 if ( argc == 1 ) {
268                         if ( i < 0 ) {
269                                 fprintf( stderr,
270         "%s: line %d: \"default-target\" alone need be"
271         " inside a \"uri\" directive\n",
272                                         fname, lineno );
273                                 return 1;
274                         }
275                         li->defaulttarget = i;
276                 } else {
277                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
278                                 if ( i >= 0 ) {
279                                         fprintf( stderr,
280         "%s: line %d: \"default-target none\""
281         " should go before uri definitions\n",
282                                                 fname, lineno );
283                                 }
284                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
285                         } else {
286                                 int n = atoi( argv[ 1 ] );
287                                 if ( n < 1 || n >= i ) {
288                                         fprintf( stderr,
289         "%s: line %d: illegal target number %d\n",
290                                                 fname, lineno, n );
291                                         return 1;
292                                 }
293                                 li->defaulttarget = n-1;
294                         }
295                 }
296                 
297         /* ttl of dn cache */
298         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
299                 if ( argc != 2 ) {
300                         fprintf( stderr,
301         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
302                                 fname, lineno );
303                         return 1;
304                 }
305                 
306                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
307                         li->cache.ttl = META_DNCACHE_FOREVER;
308                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
309                         li->cache.ttl = META_DNCACHE_DISABLED;
310                 } else {
311                         li->cache.ttl = atol( argv[ 1 ] );
312                 }
313
314         /* name to use for meta_back_group */
315         } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
316                 int             i = li->ntargets-1;
317                 struct berval   dn;
318
319                 if ( i < 0 ) {
320                         fprintf( stderr,
321         "%s: line %d: need \"uri\" directive first\n",
322                                 fname, lineno );
323                 }
324                 
325                 if ( argc != 2 ) {
326                         fprintf( stderr,
327         "%s: line %d: missing name in \"binddn <name>\" line\n",
328                                 fname, lineno );
329                         return 1;
330                 }
331
332                 dn.bv_val = argv[ 1 ];
333                 dn.bv_len = strlen( argv[ 1 ] );
334                 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->binddn ) != LDAP_SUCCESS ) {
335                         fprintf( stderr, "%s: line %d: "
336                                         "bind DN '%s' is invalid\n",
337                                         fname, lineno, argv[ 1 ] );
338                         return( 1 );
339                 }
340
341         /* password to use for meta_back_group */
342         } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
343                 int             i = li->ntargets-1;
344
345                 if ( i < 0 ) {
346                         fprintf( stderr,
347         "%s: line %d: need \"uri\" directive first\n",
348                                 fname, lineno );
349                 }
350                 
351                 if ( argc != 2 ) {
352                         fprintf( stderr,
353         "%s: line %d: missing password in \"bindpw <password>\" line\n",
354                             fname, lineno );
355                         return 1;
356                 }
357                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
358                 
359         /* save bind creds for referral rebinds? */
360         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
361                 if (argc != 1) {
362                         fprintf( stderr,
363         "%s: line %d: rebind-as-user takes no arguments\n",
364                             fname, lineno );
365                         return( 1 );
366                 }
367                 li->savecred = 1;
368         
369         /* name to use as pseudo-root dn */
370         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
371                 int             i = li->ntargets-1;
372                 struct berval   dn;
373
374                 if ( i < 0 ) {
375                         fprintf( stderr,
376         "%s: line %d: need \"uri\" directive first\n",
377                                 fname, lineno );
378                 }
379                 
380                 if ( argc != 2 ) {
381                         fprintf( stderr,
382         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
383                                 fname, lineno );
384                         return 1;
385                 }
386
387                 dn.bv_val = argv[ 1 ];
388                 dn.bv_len = strlen( argv[ 1 ] );
389                 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->pseudorootdn ) != LDAP_SUCCESS ) {
390                         fprintf( stderr, "%s: line %d: "
391                                         "pseudoroot DN '%s' is invalid\n",
392                                         fname, lineno, argv[ 1 ] );
393                         return( 1 );
394                 }
395
396         /* password to use as pseudo-root */
397         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
398                 int             i = li->ntargets-1;
399
400                 if ( i < 0 ) {
401                         fprintf( stderr,
402         "%s: line %d: need \"uri\" directive first\n",
403                                 fname, lineno );
404                 }
405                 
406                 if ( argc != 2 ) {
407                         fprintf( stderr,
408         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
409                             fname, lineno );
410                         return 1;
411                 }
412                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
413         
414         /* dn massaging */
415         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
416                 BackendDB       *tmp_be;
417                 int             i = li->ntargets-1;
418                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
419
420                 if ( i < 0 ) {
421                         fprintf( stderr,
422         "%s: line %d: need \"uri\" directive first\n",
423                                 fname, lineno );
424                         return 1;
425                 }
426                 
427                 /*
428                  * syntax:
429                  * 
430                  *      suffixmassage <suffix> <massaged suffix>
431                  *
432                  * the <suffix> field must be defined as a valid suffix
433                  * (or suffixAlias?) for the current database;
434                  * the <massaged suffix> shouldn't have already been
435                  * defined as a valid suffix or suffixAlias for the 
436                  * current server
437                  */
438                 if ( argc != 3 ) {
439                         fprintf( stderr,
440         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
441                                 fname, lineno );
442                         return 1;
443                 }
444
445                 dn.bv_val = argv[ 1 ];
446                 dn.bv_len = strlen( argv[ 1 ] );
447                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
448                         fprintf( stderr, "%s: line %d: "
449                                         "suffix '%s' is invalid\n",
450                                         fname, lineno, argv[ 1 ] );
451                         return 1;
452                 }
453                 
454                 tmp_be = select_backend( &nvnc, 0, 0 );
455                 if ( tmp_be != NULL && tmp_be != be ) {
456                         fprintf( stderr, 
457         "%s: line %d: suffix already in use by another backend in"
458         " \"suffixMassage <suffix> <massaged suffix>\"\n",
459                                 fname, lineno );
460                         free( pvnc.bv_val );
461                         free( nvnc.bv_val );
462                         return 1;                                               
463                 }
464
465                 dn.bv_val = argv[ 2 ];
466                 dn.bv_len = strlen( argv[ 2 ] );
467                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc ) != LDAP_SUCCESS ) {
468                         fprintf( stderr, "%s: line %d: "
469                                         "massaged suffix '%s' is invalid\n",
470                                         fname, lineno, argv[ 2 ] );
471                         free( pvnc.bv_val );
472                         free( nvnc.bv_val );
473                         return 1;
474                 }
475         
476 #if 0   
477                 tmp_be = select_backend( &nrnc, 0, 0 );
478                 if ( tmp_be != NULL ) {
479                         fprintf( stderr,
480         "%s: line %d: massaged suffix already in use by another backend in" 
481         " \"suffixMassage <suffix> <massaged suffix>\"\n",
482                                 fname, lineno );
483                         free( pvnc.bv_val );
484                         free( nvnc.bv_val );
485                         free( prnc.bv_val );
486                         free( nrnc.bv_val );
487                         return 1;
488                 }
489 #endif
490                 
491                 /*
492                  * The suffix massaging is emulated by means of the
493                  * rewrite capabilities
494                  * FIXME: no extra rewrite capabilities should be added
495                  * to the database
496                  */
497                 return suffix_massage_config( li->targets[ i ]->rwinfo,
498                                 &pvnc, &nvnc, &prnc, &nrnc );
499                 
500         /* rewrite stuff ... */
501         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
502                 int             i = li->ntargets-1;
503
504                 if ( i < 0 ) {
505                         fprintf( stderr,
506         "%s: line %d: need \"uri\" directive first\n",
507                                 fname, lineno );
508                 }
509                 
510                 return rewrite_parse( li->targets[ i ]->rwinfo, fname, lineno,
511                                 argc, argv );
512
513         /* objectclass/attribute mapping */
514         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
515                 int             i = li->ntargets-1;
516
517                 return ldap_back_map_config( &li->targets[ i ]->oc_map, 
518                                 &li->targets[ i ]->at_map,
519                                 fname, lineno, argc, argv );
520         /* anything else */
521         } else {
522                 fprintf( stderr,
523         "%s: line %d: unknown directive \"%s\" in meta database definition"
524         " (ignored)\n",
525                     fname, lineno, argv[0] );
526         }
527         return 0;
528 }
529