1 /* oidm.c - object identifier macro routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
29 static LDAP_STAILQ_HEAD(OidMacroList, OidMacro) om_list
30 = LDAP_STAILQ_HEAD_INITIALIZER(om_list);
32 OidMacro *om_sys_tail;
34 /* Replace an OID Macro invocation with its full numeric OID.
35 * If the macro is used with "macroname:suffix" append ".suffix"
43 /* OID macros must start alpha */
44 if ( OID_LEADCHAR( *oid ) ) {
48 LDAP_STAILQ_FOREACH( om, &om_list, som_next ) {
49 BerVarray names = om->som_names;
55 for( ; !BER_BVISNULL( names ) ; names++ ) {
56 int pos = dscompare(names->bv_val, oid, ':');
59 int suflen = strlen(oid + pos);
60 char *tmp = SLAP_MALLOC( om->som_oid.bv_len
63 Debug( LDAP_DEBUG_ANY,
64 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
67 strcpy(tmp, om->som_oid.bv_val);
69 suflen = om->som_oid.bv_len;
71 strcpy(tmp+suflen, oid+pos+1);
84 while( !LDAP_STAILQ_EMPTY( &om_list )) {
85 om = LDAP_STAILQ_FIRST( &om_list );
86 LDAP_STAILQ_REMOVE_HEAD( &om_list, som_next );
88 ber_bvarray_free(om->som_names);
89 ber_bvarray_free(om->som_subs);
90 free(om->som_oid.bv_val);
98 struct config_args_s *c,
103 OidMacro *om = NULL, *prev = NULL;
106 oidv = oidm_find( c->argv[2] );
108 snprintf( c->cr_msg, sizeof( c->cr_msg ),
109 "%s: OID %s not recognized",
110 c->argv[0], c->argv[2] );
111 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
112 "%s %s\n", c->log, c->cr_msg, 0 );
116 oid = oidm_find( c->argv[1] );
119 snprintf( c->cr_msg, sizeof( c->cr_msg ),
120 "%s: \"%s\" previously defined \"%s\"",
121 c->argv[0], c->argv[1], oid );
122 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
123 "%s %s\n", c->log, c->cr_msg, 0 );
124 /* Allow duplicate if the definition is identical */
125 rc = strcmp( oid, oidv ) != 0;
127 if ( oidv != c->argv[2] )
132 om = (OidMacro *) SLAP_CALLOC( sizeof(OidMacro), 1 );
134 snprintf( c->cr_msg, sizeof( c->cr_msg ),
135 "%s: SLAP_CALLOC failed", c->argv[0] );
136 Debug( LDAP_DEBUG_ANY,
137 "%s %s\n", c->log, c->cr_msg, 0 );
138 if ( oidv != c->argv[2] )
143 om->som_names = NULL;
145 ber_str2bv( c->argv[1], 0, 1, &bv );
146 ber_bvarray_add( &om->som_names, &bv );
147 ber_str2bv( c->argv[2], 0, 1, &bv );
148 ber_bvarray_add( &om->som_subs, &bv );
149 om->som_oid.bv_val = oidv;
151 if (om->som_oid.bv_val == c->argv[2]) {
152 om->som_oid.bv_val = ch_strdup( c->argv[2] );
155 om->som_oid.bv_len = strlen( om->som_oid.bv_val );
157 om->som_flags |= SLAP_OM_HARDCODE;
163 LDAP_STAILQ_INSERT_AFTER( &om_list, prev, om, som_next );
165 LDAP_STAILQ_INSERT_TAIL( &om_list, om, som_next );
167 if ( rom ) *rom = om;
171 void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
175 struct berval *bva = NULL, idx;
179 start = LDAP_STAILQ_FIRST( &om_list );
181 /* count the result size */
183 for ( om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
184 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) break;
185 for ( j=0; !BER_BVISNULL(&om->som_names[j]); j++ );
187 if ( om == end ) break;
192 bva = ch_malloc( (num+1) * sizeof(struct berval) );
193 BER_BVZERO( bva+num );
199 for ( i=0,om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
200 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) break;
201 for ( j=0; !BER_BVISNULL(&om->som_names[j]); i++,j++ ) {
203 idx.bv_len = sprintf(idx.bv_val, "{%d}", i );
205 bva[i].bv_len = idx.bv_len + om->som_names[j].bv_len +
206 om->som_subs[j].bv_len + 1;
207 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
208 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
209 ptr = lutil_strcopy( ptr, om->som_names[j].bv_val );
211 strcpy( ptr, om->som_subs[j].bv_val );
214 if ( om == end ) break;