1 /* oidm.c - object identifier macro routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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, slap_oid_macro) 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 oid = oidm_find( c->argv[1] );
108 snprintf( c->msg, sizeof( c->msg ),
109 "%s: \"%s\" previously defined \"%s\"",
110 c->argv[0], c->argv[1], oid );
111 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
112 "%s %s\n", c->log, c->msg, 0 );
117 om = (OidMacro *) SLAP_CALLOC( sizeof(OidMacro), 1 );
119 snprintf( c->msg, sizeof( c->msg ),
120 "%s: SLAP_CALLOC failed", c->argv[0] );
121 Debug( LDAP_DEBUG_ANY,
122 "%s %s\n", c->log, c->msg, 0 );
126 om->som_names = NULL;
128 ber_str2bv( c->argv[1], 0, 1, &bv );
129 ber_bvarray_add( &om->som_names, &bv );
130 ber_str2bv( c->argv[2], 0, 1, &bv );
131 ber_bvarray_add( &om->som_subs, &bv );
132 om->som_oid.bv_val = oidm_find( c->argv[2] );
134 if (!om->som_oid.bv_val) {
135 snprintf( c->msg, sizeof( c->msg ),
136 "%s: OID %s not recognized",
137 c->argv[0], c->argv[2] );
138 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
139 "%s %s\n", c->log, c->msg, 0 );
144 if (om->som_oid.bv_val == c->argv[2]) {
145 om->som_oid.bv_val = ch_strdup( c->argv[2] );
148 om->som_oid.bv_len = strlen( om->som_oid.bv_val );
150 om->som_flags |= SLAP_OM_HARDCODE;
156 LDAP_STAILQ_INSERT_AFTER( &om_list, prev, om, som_next );
158 LDAP_STAILQ_INSERT_TAIL( &om_list, om, som_next );
160 if ( rom ) *rom = om;
164 void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
168 struct berval *bva = NULL, idx;
172 start = LDAP_STAILQ_FIRST( &om_list );
174 /* count the result size */
176 for ( om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
177 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) break;
178 for ( j=0; !BER_BVISNULL(&om->som_names[j]); j++ );
180 if ( om == end ) break;
185 bva = ch_malloc( (num+1) * sizeof(struct berval) );
186 BER_BVZERO( bva+num );
192 for ( i=0,om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
193 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) break;
194 for ( j=0; !BER_BVISNULL(&om->som_names[j]); i++,j++ ) {
196 idx.bv_len = sprintf(idx.bv_val, "{%d}", i );
198 bva[i].bv_len = idx.bv_len + om->som_names[j].bv_len +
199 om->som_subs[j].bv_len + 1;
200 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
201 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
202 ptr = lutil_strcopy( ptr, om->som_names[j].bv_val );
204 strcpy( ptr, om->som_subs[j].bv_val );
207 if ( om == end ) break;