]> git.sur5r.net Git - openldap/blob - servers/slapd/oidm.c
Fix prev commit, reset DB pointer
[openldap] / servers / slapd / oidm.c
1 /* oidm.c - object identifier macro routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26 #include "lutil.h"
27 #include "config.h"
28
29 static LDAP_STAILQ_HEAD(OidMacroList, slap_oid_macro) om_list
30         = LDAP_STAILQ_HEAD_INITIALIZER(om_list);
31
32 /* Replace an OID Macro invocation with its full numeric OID.
33  * If the macro is used with "macroname:suffix" append ".suffix"
34  * to the expansion.
35  */
36 char *
37 oidm_find(char *oid)
38 {
39         OidMacro *om;
40
41         /* OID macros must start alpha */
42         if ( OID_LEADCHAR( *oid ) )     {
43                 return oid;
44         }
45
46         LDAP_STAILQ_FOREACH( om, &om_list, som_next ) {
47                 BerVarray names = om->som_names;
48
49                 if( names == NULL ) {
50                         continue;
51                 }
52
53                 for( ; !BER_BVISNULL( names ) ; names++ ) {
54                         int pos = dscompare(names->bv_val, oid, ':');
55
56                         if( pos ) {
57                                 int suflen = strlen(oid + pos);
58                                 char *tmp = SLAP_MALLOC( om->som_oid.bv_len
59                                         + suflen + 1);
60                                 if( tmp == NULL ) {
61                                         Debug( LDAP_DEBUG_ANY,
62                                                 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
63                                         return NULL;
64                                 }
65                                 strcpy(tmp, om->som_oid.bv_val);
66                                 if( suflen ) {
67                                         suflen = om->som_oid.bv_len;
68                                         tmp[suflen++] = '.';
69                                         strcpy(tmp+suflen, oid+pos+1);
70                                 }
71                                 return tmp;
72                         }
73                 }
74         }
75         return NULL;
76 }
77
78 void
79 oidm_destroy()
80 {
81         OidMacro *om;
82         while( !LDAP_STAILQ_EMPTY( &om_list )) {
83                 om = LDAP_STAILQ_FIRST( &om_list );
84                 LDAP_STAILQ_REMOVE_HEAD( &om_list, som_next );
85
86                 ber_bvarray_free(om->som_names);
87                 ber_bvarray_free(om->som_subs);
88                 free(om->som_oid.bv_val);
89                 free(om);
90                 
91         }
92 }
93
94 int
95 parse_oidm(
96         struct config_args_s *c,
97         int             user,
98         OidMacro **rom)
99 {
100         char *oid;
101         OidMacro *om = NULL;
102         struct berval bv;
103
104         oid = oidm_find( c->argv[1] );
105         if( oid != NULL ) {
106                 snprintf( c->msg, sizeof( c->msg ),
107                         "%s: \"%s\" previously defined \"%s\"",
108                         c->argv[0], c->argv[1], oid );
109                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
110                         "%s %s\n", c->log, c->msg, 0 );
111                 SLAP_FREE( oid );
112                 return 1;
113         }
114
115         om = (OidMacro *) SLAP_CALLOC( sizeof(OidMacro), 1 );
116         if( om == NULL ) {
117                 snprintf( c->msg, sizeof( c->msg ),
118                         "%s: SLAP_CALLOC failed", c->argv[0] );
119                 Debug( LDAP_DEBUG_ANY,
120                         "%s %s\n", c->log, c->msg, 0 );
121                 return 1;
122         }
123
124         om->som_names = NULL;
125         om->som_subs = NULL;
126         ber_str2bv( c->argv[1], 0, 1, &bv );
127         ber_bvarray_add( &om->som_names, &bv );
128         ber_str2bv( c->argv[2], 0, 1, &bv );
129         ber_bvarray_add( &om->som_subs, &bv );
130         om->som_oid.bv_val = oidm_find( c->argv[2] );
131
132         if (!om->som_oid.bv_val) {
133                 snprintf( c->msg, sizeof( c->msg ),
134                         "%s: OID %s not recognized",
135                         c->argv[0], c->argv[2] );
136                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
137                         "%s %s\n", c->log, c->msg, 0 );
138                 SLAP_FREE( om );
139                 return 1;
140         }
141
142         if (om->som_oid.bv_val == c->argv[2]) {
143                 om->som_oid.bv_val = ch_strdup( c->argv[2] );
144         }
145
146         om->som_oid.bv_len = strlen( om->som_oid.bv_val );
147         if ( !user )
148                 om->som_flags |= SLAP_OM_HARDCODE;
149
150         LDAP_STAILQ_INSERT_TAIL( &om_list, om, som_next );
151         if ( rom ) *rom = om;
152         return 0;
153 }
154
155 void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
156 {
157         OidMacro *om;
158         int i, j, num;
159         struct berval *bva = NULL, idx;
160         char ibuf[32], *ptr;
161
162         if ( !start )
163                 start = LDAP_STAILQ_FIRST( &om_list );
164
165         /* count the result size */
166         i = 0;
167         for ( om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
168                 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) continue;
169                 for ( j=0; !BER_BVISNULL(&om->som_names[j]); j++ );
170                 i += j;
171                 if ( om == end ) break;
172         }
173         num = i;
174         if (!i) return;
175
176         bva = ch_malloc( (num+1) * sizeof(struct berval) );
177         BER_BVZERO( bva+num );
178         idx.bv_val = ibuf;
179         if ( sys ) {
180                 idx.bv_len = 0;
181                 ibuf[0] = '\0';
182         }
183         for ( i=0,om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
184                 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) continue;
185                 for ( j=0; !BER_BVISNULL(&om->som_names[j]); i++,j++ ) {
186                         if ( !sys ) {
187                                 idx.bv_len = sprintf(idx.bv_val, "{%d}", i );
188                         }
189                         bva[i].bv_len = idx.bv_len + om->som_names[j].bv_len +
190                                 om->som_subs[j].bv_len + 1;
191                         bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
192                         ptr = lutil_strcopy( bva[i].bv_val, ibuf );
193                         ptr = lutil_strcopy( ptr, om->som_names[j].bv_val );
194                         *ptr++ = ' ';
195                         strcpy( ptr, om->som_subs[j].bv_val );
196                 }
197                 if ( i>=num ) break;
198                 if ( om == end ) break;
199         }
200         *res = bva;
201 }