]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
c6783247a8b6b9fbd43369f7ccd09323917f552a
[openldap] / servers / slapd / back-meta / config.c
1 /*
2  * Copyright 1998-2001 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 "back-meta.h"
77
78 extern int
79 suffix_massage_config(
80                 struct rewrite_info *info,
81                 int argc,
82                 char **argv
83 );
84
85 static struct metatarget *
86 new_target( void )
87 {
88         struct metatarget *lt;
89         struct ldapmapping *mapping;
90
91         lt = ch_calloc( sizeof( struct metatarget ), 1 );
92         if ( lt == NULL ) {
93                 return NULL;
94         }
95
96         lt->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
97         if ( lt->rwinfo == NULL ) {
98                 free( lt );
99                 return NULL;
100         }
101
102         mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
103         if ( mapping == NULL ) {
104                 free( lt );
105                 return NULL;
106         }
107         mapping->src = ch_strdup( "objectClass" );
108         mapping->dst = ch_strdup( "objectClass" );
109         mapping[ 1 ].src = mapping->src;
110         mapping[ 1 ].dst = mapping->dst;
111
112         avl_insert( &lt->at_map.map, ( caddr_t )mapping, 
113                         mapping_cmp, mapping_dup );
114         avl_insert( &lt->at_map.remap, ( caddr_t )&mapping[ 1 ],
115                         mapping_cmp, mapping_dup );
116
117         return lt;
118 }
119
120 int
121 meta_back_db_config(
122                 BackendDB       *be,
123                 const char      *fname,
124                 int             lineno,
125                 int             argc,
126                 char            **argv
127 )
128 {
129         struct metainfo *li = ( struct metainfo * )be->be_private;
130
131         if ( li == NULL ) {
132                 fprintf( stderr, 
133         "%s: line %d: meta backend info is null!\n",
134                     fname, lineno );
135                 return 1;
136         }
137
138         /* URI of server to query */
139         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
140                 int j, i = li->ntargets;
141                 LDAPURLDesc *ludp;
142                 char *last;
143                 
144                 if ( argc != 2 ) {
145                         fprintf( stderr,
146         "%s: line %d: missing address"
147         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
148                                 fname, lineno );
149                         return 1;
150                 }
151                 
152                 ++li->ntargets;
153
154                 li->targets = ch_realloc( li->targets, 
155                         sizeof( struct metatarget *)*li->ntargets );
156                 if ( li->targets == NULL ) {
157                         fprintf( stderr,
158         "%s: line %d: out of memory while storing server name"
159         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
160                                 fname, lineno );
161                         return 1;
162                 }
163
164                 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
165                         fprintf( stderr,
166         "%s: line %d: unable to init server"
167         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
168                                 fname, lineno );
169                         return 1;
170                 }
171
172                 /*
173                  * uri MUST be legal!
174                  */
175                 if ( ldap_url_parse( argv[ 1 ], &ludp ) != LDAP_SUCCESS ) {
176                         fprintf( stderr,
177         "%s: line %d: unable to parse URI"
178         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
179                                 fname, lineno );
180                         return 1;
181                 }
182
183                 /*
184                  * uri MUST have the <dn> part!
185                  */
186                 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
187                         fprintf( stderr,
188         "%s: line %d: missing <naming context> "
189         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
190                                 fname, lineno );
191                         return 1;
192                 }
193
194                 /*
195                  * copies and stores uri and suffix
196                  */
197                 li->targets[ i ]->suffix = ch_strdup( ludp->lud_dn );
198                 li->targets[ i ]->uri = ch_strdup( argv[ 1 ] );
199                 last = strstr( li->targets[ i ]->uri,
200                                 li->targets[ i ]->suffix );
201                 assert( last != NULL );
202                 last[ 0 ] = '\0'; /* wasting memory ... */
203                 
204                 /*
205                  * Need to store the suffix in normalized form
206                  */
207                 (void) dn_normalize( li->targets[ i ]->suffix );
208                 
209                 /*
210                  * uri MUST be a branch of suffix!
211                  */
212 #if 0 /* too strict a constraint */
213                 if ( select_backend( li->targets[ i ]->suffix, 0 ) != be ) {
214                         fprintf( stderr,
215         "%s: line %d: <naming context> of URI does not refer to current backend"
216         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
217                                 fname, lineno );
218                         return 1;
219                 }
220 #else
221                 /*
222                  * uri MUST be a branch of a suffix!
223                  */
224                 if ( select_backend( li->targets[ i ]->suffix, 0 ) == NULL ) {
225                         fprintf( stderr,
226         "%s: line %d: <naming context> of URI does not resolve to a backend"
227         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
228                                 fname, lineno );
229                         return 1;
230                 }
231 #endif
232
233 #if 0
234                 /*
235                  * uri MUST not be used by other URIs!
236                  *
237                  * FIXME: this limitation may be removed,
238                  * or worked out, at least, in some manner
239                  */
240                 for ( j = 0; j < i-1; j++ ) {
241                         if ( strcmp( li->targets[ i ]->suffix,
242                                         li->targets[ j ]->suffix ) == 0 ) {
243                                 fprintf( stderr,
244         "%s: line %d: naming context \"%s\" already used"
245         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
246                                         fname, lineno, last+1 );
247                                 return 1;
248                         }
249                 }
250 #endif
251                 
252                 ldap_free_urldesc( ludp );
253
254 #ifdef NEW_LOGGING
255                 LDAP_LOG(( "config", LDAP_LEVEL_INFO,
256                                 "meta_back_db_config:"
257                                 " URI \"%s\", suffix \"%s\"\n",
258                                 li->targets[ i ]->uri,
259                                 li->targets[ i ]->suffix ));
260 #else /* !NEW_LOGGING */
261                 Debug( LDAP_DEBUG_CONFIG,
262         "==>meta_back_db_config: URI \"%s\", suffix \"%s\"\n%s",
263                         li->targets[ i ]->uri, li->targets[ i ]->suffix, "" );
264 #endif /* !NEW_LOGGING */
265                 
266         /* default target directive */
267         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
268                 int i = li->ntargets-1;
269                 
270                 if ( argc == 1 ) {
271                         if ( i < 0 ) {
272                                 fprintf( stderr,
273         "%s: line %d: \"default-target\" alone need be"
274         " inside a \"uri\" directive\n",
275                                         fname, lineno );
276                                 return 1;
277                         }
278                         li->defaulttarget = i;
279                 } else {
280                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
281                                 if ( i >= 0 ) {
282                                         fprintf( stderr,
283         "%s: line %d: \"default-target none\""
284         " should go before uri definitions\n",
285                                                 fname, lineno );
286                                 }
287                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
288                         } else {
289                                 int n = atoi( argv[ 1 ] );
290                                 if ( n < 1 || n >= i ) {
291                                         fprintf( stderr,
292         "%s: line %d: illegal target number %d\n",
293                                                 fname, lineno, n );
294                                         return 1;
295                                 }
296                                 li->defaulttarget = n-1;
297                         }
298                 }
299                 
300         /* ttl of dn cache */
301         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
302                 if ( argc != 2 ) {
303                         fprintf( stderr,
304         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
305                                 fname, lineno );
306                         return 1;
307                 }
308                 
309                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
310                         li->cache.ttl = META_DNCACHE_FOREVER;
311                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
312                         li->cache.ttl = META_DNCACHE_DISABLED;
313                 } else {
314                         li->cache.ttl = atol( argv[ 1 ] );
315                 }
316
317         /* name to use for meta_back_group */
318         } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
319                 int i = li->ntargets-1;
320
321                 if ( i < 0 ) {
322                         fprintf( stderr,
323         "%s: line %d: need \"uri\" directive first\n",
324                                 fname, lineno );
325                 }
326                 
327                 if ( argc != 2 ) {
328                         fprintf( stderr,
329         "%s: line %d: missing name in \"binddn <name>\" line\n",
330                                 fname, lineno );
331                         return 1;
332                 }
333                 li->targets[ i ]->binddn = ch_strdup( argv[ 1 ] );
334
335         /* password to use for meta_back_group */
336         } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
337                 int i = li->ntargets-1;
338
339                 if ( i < 0 ) {
340                         fprintf( stderr,
341         "%s: line %d: need \"uri\" directive first\n",
342                                 fname, lineno );
343                 }
344                 
345                 if ( argc != 2 ) {
346                         fprintf( stderr,
347         "%s: line %d: missing password in \"bindpw <password>\" line\n",
348                             fname, lineno );
349                         return 1;
350                 }
351                 li->targets[ i ]->bindpw = ch_strdup( argv[ 1 ] );
352         
353         /* dn massaging */
354         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
355                 BackendDB *tmp_be;
356                 int i = li->ntargets-1;
357
358                 if ( i < 0 ) {
359                         fprintf( stderr,
360         "%s: line %d: need \"uri\" directive first\n",
361                                 fname, lineno );
362                         return 1;
363                 }
364                 
365                 /*
366                  * syntax:
367                  * 
368                  *      suffixmassage <suffix> <massaged suffix>
369                  *
370                  * the <suffix> field must be defined as a valid suffix
371                  * (or suffixAlias?) for the current database;
372                  * the <massaged suffix> shouldn't have already been
373                  * defined as a valid suffix or suffixAlias for the 
374                  * current server
375                  */
376                 if ( argc != 3 ) {
377                         fprintf( stderr,
378         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
379                                 fname, lineno );
380                         return 1;
381                 }
382                 
383                 tmp_be = select_backend( argv[ 1 ], 0 );
384                 if ( tmp_be != NULL && tmp_be != be ) {
385                         fprintf( stderr, 
386         "%s: line %d: suffix already in use by another backend in"
387         " \"suffixMassage <suffix> <massaged suffix>\"\n",
388                                 fname, lineno );
389                         return 1;                                               
390                 }
391
392                 tmp_be = select_backend( argv[ 2 ], 0 );
393                 if ( tmp_be != NULL ) {
394                         fprintf( stderr,
395         "%s: line %d: massaged suffix already in use by another backend in" 
396         " \"suffixMassage <suffix> <massaged suffix>\"\n",
397                                 fname, lineno );
398                         return 1;
399                 }
400                 
401                 /*
402                  * The suffix massaging is emulated by means of the
403                  * rewrite capabilities
404                  * FIXME: no extra rewrite capabilities should be added
405                  * to the database
406                  */
407                 return suffix_massage_config( li->targets[ i ]->rwinfo,
408                                 argc, argv );
409                 
410         /* rewrite stuff ... */
411         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
412                 int i = li->ntargets-1;
413
414                 if ( i < 0 ) {
415                         fprintf( stderr,
416         "%s: line %d: need \"uri\" directive first\n",
417                                 fname, lineno );
418                 }
419                 
420                 return rewrite_parse( li->targets[ i ]->rwinfo, fname, lineno,
421                                 argc, argv );
422
423         /* objectclass/attribute mapping */
424         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
425                 struct ldapmap *map;
426                 struct ldapmapping *mapping;
427                 char *src, *dst;
428                 int i = li->ntargets-1;
429
430                 if ( i < 0 ) {
431                         fprintf( stderr,
432         "%s: line %d: need \"uri\" directive first\n",
433                                 fname, lineno );
434                 }
435                 
436
437                 if ( argc < 3 || argc > 4 ) {
438                         fprintf( stderr,
439         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
440                                 fname, lineno );
441                         return 1;
442                 }
443
444                 if ( strcasecmp( argv[ 1 ], "objectClass" ) == 0 ) {
445                         map = &li->targets[ i ]->oc_map;
446                 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
447                         map = &li->targets[ i ]->at_map;
448                 } else {
449                         fprintf( stderr,
450         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
451                                 fname, lineno );
452                         return 1;
453                 }
454
455                 if ( strcasecmp( argv[ 2 ], "*" ) != 0 ) {
456                         src = argv[ 2 ];
457                         if ( argc < 4 ) {
458                                 dst = "";
459                         } else if ( strcasecmp( argv[ 3 ], "*" ) == 0 ) {
460                                 dst = src;
461                         } else {
462                                 dst = argv[ 3 ];
463                         }
464                 } else {
465                         if ( argc < 4 ) {
466                                 map->drop_missing = 1;
467                                 return 0;
468                         }
469                         if ( strcasecmp( argv[ 3 ], "*" ) == 0 ) {
470                                 map->drop_missing = 0;
471                                 return 0;
472                         }
473
474                         src = argv[ 3 ];
475                         dst = src;
476                 }
477
478                 if ( ( map == &li->targets[ i ]->at_map )
479                         && ( strcasecmp( src, "objectclass" ) == 0
480                                 || strcasecmp( dst, "objectclass" ) == 0 ) ) {
481                         fprintf( stderr,
482         "%s: line %d: objectclass attribute cannot be mapped\n",
483                                 fname, lineno );
484                 }
485
486                 mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
487                 if ( mapping == NULL ) {
488                         fprintf( stderr,
489                                 "%s: line %d: out of memory\n",
490                                 fname, lineno );
491                         return 1;
492                 }
493                 mapping->src = ch_strdup( src );
494                 mapping->dst = ch_strdup( dst );
495                 if ( *dst != 0 ) {
496                         mapping[ 1 ].src = mapping->dst;
497                         mapping[ 1 ].dst = mapping->src;
498                 } else {
499                         mapping[ 1 ].src = mapping->src;
500                         mapping[ 1 ].dst = mapping->dst;
501                 }
502
503                 if ( avl_find( map->map, ( caddr_t )mapping,
504                                 mapping_cmp ) != NULL
505                         || avl_find( map->remap, ( caddr_t )&mapping[ 1 ],
506                                 mapping_cmp ) != NULL) {
507                         fprintf( stderr,
508         "%s: line %d: duplicate mapping found (ignored)\n",
509                                 fname, lineno );
510                         return 0;
511                 }
512
513                 avl_insert( &map->map, ( caddr_t )mapping,
514                                         mapping_cmp, mapping_dup );
515                 avl_insert( &map->remap, ( caddr_t )&mapping[ 1 ],
516                                         mapping_cmp, mapping_dup );
517
518         /* anything else */
519         } else {
520                 fprintf( stderr,
521         "%s: line %d: unknown directive \"%s\" in meta database definition"
522         " (ignored)\n",
523                     fname, lineno, argv[0] );
524         }
525         return 0;
526 }
527