]> git.sur5r.net Git - openldap/blob - servers/slapd/oidm.c
e553b67227a394d89628d56298bf8a0891bd3676
[openldap] / servers / slapd / oidm.c
1 /* schemaparse.c - routines to parse config file objectclass definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17
18 static LDAP_SLIST_HEAD(OidMacroList, slap_oid_macro) om_list
19         = LDAP_SLIST_HEAD_INITIALIZER(om_list);
20
21 /* Replace an OID Macro invocation with its full numeric OID.
22  * If the macro is used with "macroname:suffix" append ".suffix"
23  * to the expansion.
24  */
25 char *
26 oidm_find(char *oid)
27 {
28         OidMacro *om;
29
30         /* OID macros must start alpha */
31         if ( OID_LEADCHAR( *oid ) )     {
32                 return oid;
33         }
34
35         LDAP_SLIST_FOREACH( om, &om_list, som_next ) {
36                 char **names = om->som_names;
37
38                 if( names == NULL ) {
39                         continue;
40                 }
41
42                 for( ; *names != NULL ; names++ ) {
43                         int pos = dscompare(*names, oid, ':');
44
45                         if( pos ) {
46                                 int suflen = strlen(oid + pos);
47                                 char *tmp = SLAP_MALLOC( om->som_oid.bv_len
48                                         + suflen + 1);
49                                 if( tmp == NULL ) {
50 #ifdef NEW_LOGGING
51                                         LDAP_LOG( OPERATION, ERR,
52                                                 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
53 #else
54                                         Debug( LDAP_DEBUG_ANY,
55                                                 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
56 #endif
57                                         return NULL;
58                                 }
59                                 strcpy(tmp, om->som_oid.bv_val);
60                                 if( suflen ) {
61                                         suflen = om->som_oid.bv_len;
62                                         tmp[suflen++] = '.';
63                                         strcpy(tmp+suflen, oid+pos+1);
64                                 }
65                                 return tmp;
66                         }
67                 }
68         }
69         return NULL;
70 }
71
72 void
73 oidm_destroy()
74 {
75         OidMacro *om;
76         while( !LDAP_SLIST_EMPTY( &om_list )) {
77                 om = LDAP_SLIST_FIRST( &om_list );
78                 LDAP_SLIST_REMOVE_HEAD( &om_list, som_next );
79
80                 ldap_charray_free(om->som_names);
81                 free(om->som_oid.bv_val);
82                 free(om);
83                 
84         }
85 }
86
87 int
88 parse_oidm(
89     const char  *fname,
90     int         lineno,
91     int         argc,
92     char        **argv )
93 {
94         char *oid;
95         OidMacro *om;
96
97         if (argc != 3) {
98                 fprintf( stderr, "%s: line %d: too many arguments\n",
99                         fname, lineno );
100 usage:  fprintf( stderr, "\tObjectIdentifier <name> <oid>\n");
101                 return 1;
102         }
103
104         oid = oidm_find( argv[1] );
105         if( oid != NULL ) {
106                 fprintf( stderr,
107                         "%s: line %d: "
108                         "ObjectIdentifier \"%s\" previously defined \"%s\"",
109                         fname, lineno, argv[1], oid );
110                 return 1;
111         }
112
113         om = (OidMacro *) SLAP_MALLOC( sizeof(OidMacro) );
114         if( om == NULL ) {
115 #ifdef NEW_LOGGING
116                 LDAP_LOG( OPERATION, ERR, "parse_oidm: SLAP_MALLOC failed", 0, 0, 0 );
117 #else
118                 Debug( LDAP_DEBUG_ANY, "parse_oidm: SLAP_MALLOC failed", 0, 0, 0 );
119 #endif
120                 return 1;
121         }
122
123         LDAP_SLIST_NEXT( om, som_next ) = NULL;
124         om->som_names = NULL;
125         ldap_charray_add( &om->som_names, argv[1] );
126         om->som_oid.bv_val = oidm_find( argv[2] );
127
128         if (!om->som_oid.bv_val) {
129                 fprintf( stderr, "%s: line %d: OID %s not recognized\n",
130                         fname, lineno, argv[2] );
131                 goto usage;
132         }
133
134         if (om->som_oid.bv_val == argv[2]) {
135                 om->som_oid.bv_val = ch_strdup( argv[2] );
136         }
137
138         om->som_oid.bv_len = strlen( om->som_oid.bv_val );
139
140         LDAP_SLIST_INSERT_HEAD( &om_list, om, som_next );
141         return 0;
142 }