]> git.sur5r.net Git - openldap/blob - servers/slapd/oidm.c
Happy new year
[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 OidMacro *om_list = NULL;
19
20 /* Replace an OID Macro invocation with its full numeric OID.
21  * If the macro is used with "macroname:suffix" append ".suffix"
22  * to the expansion.
23  */
24 char *
25 oidm_find(char *oid)
26 {
27         OidMacro *om;
28
29         /* OID macros must start alpha */
30         if ( OID_LEADCHAR( *oid ) )     {
31                 return oid;
32         }
33
34     for (om = om_list; om; om=om->som_next) {
35                 char **names = om->som_names;
36
37                 if( names == NULL ) {
38                         continue;
39                 }
40
41                 for( ; *names != NULL ; names++ ) {
42                         int pos = dscompare(*names, oid, ':');
43
44                         if( pos ) {
45                                 int suflen = strlen(oid + pos);
46                                 char *tmp = SLAP_MALLOC( om->som_oid.bv_len
47                                         + suflen + 1);
48                                 if( tmp == NULL ) {
49 #ifdef NEW_LOGGING
50                                         LDAP_LOG( OPERATION, ERR,
51                                                 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
52 #else
53                                         Debug( LDAP_DEBUG_ANY,
54                                                 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
55 #endif
56                                         return NULL;
57                                 }
58                                 strcpy(tmp, om->som_oid.bv_val);
59                                 if( suflen ) {
60                                         suflen = om->som_oid.bv_len;
61                                         tmp[suflen++] = '.';
62                                         strcpy(tmp+suflen, oid+pos+1);
63                                 }
64                                 return tmp;
65                         }
66                 }
67         }
68         return NULL;
69 }
70
71 void
72 oidm_destroy()
73 {
74         OidMacro *om, *n;
75
76         for (om = om_list; om; om = n) {
77                 n = om->som_next;
78                 ldap_charray_free(om->som_names);
79                 free(om->som_oid.bv_val);
80                 free(om);
81         }
82 }
83
84 int
85 parse_oidm(
86     const char  *fname,
87     int         lineno,
88     int         argc,
89     char        **argv
90 )
91 {
92         char *oid;
93         OidMacro *om;
94
95         if (argc != 3) {
96                 fprintf( stderr, "%s: line %d: too many arguments\n",
97                         fname, lineno );
98 usage:  fprintf( stderr, "\tObjectIdentifier <name> <oid>\n");
99                 return 1;
100         }
101
102         oid = oidm_find( argv[1] );
103         if( oid != NULL ) {
104                 fprintf( stderr,
105                         "%s: line %d: "
106                         "ObjectIdentifier \"%s\" previously defined \"%s\"",
107                         fname, lineno, argv[1], oid );
108                 return 1;
109         }
110
111         om = (OidMacro *) SLAP_MALLOC( sizeof(OidMacro) );
112         if( om == NULL ) {
113 #ifdef NEW_LOGGING
114                 LDAP_LOG( OPERATION, ERR, "parse_oidm: SLAP_MALLOC failed", 0, 0, 0 );
115 #else
116                 Debug( LDAP_DEBUG_ANY, "parse_oidm: SLAP_MALLOC failed", 0, 0, 0 );
117 #endif
118                 return 1;
119         }
120
121         om->som_names = NULL;
122         ldap_charray_add( &om->som_names, argv[1] );
123         om->som_oid.bv_val = oidm_find( argv[2] );
124
125         if (!om->som_oid.bv_val) {
126                 fprintf( stderr, "%s: line %d: OID %s not recognized\n",
127                         fname, lineno, argv[2] );
128                 goto usage;
129         }
130
131         if (om->som_oid.bv_val == argv[2]) {
132                 om->som_oid.bv_val = ch_strdup( argv[2] );
133         }
134
135         om->som_oid.bv_len = strlen( om->som_oid.bv_val );
136         om->som_next = om_list;
137         om_list = om;
138
139         return 0;
140 }