]> git.sur5r.net Git - openldap/blob - servers/slapd/oidm.c
Cleanup up LDAP_CLIENT_UPDATE code... including some bug fixing.
[openldap] / servers / slapd / oidm.c
1 /* schemaparse.c - routines to parse config file objectclass definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 = ch_malloc( om->som_oid.bv_len
47                                         + suflen + 1);
48                                 strcpy(tmp, om->som_oid.bv_val);
49                                 if( suflen ) {
50                                         suflen = om->som_oid.bv_len;
51                                         tmp[suflen++] = '.';
52                                         strcpy(tmp+suflen, oid+pos+1);
53                                 }
54                                 return tmp;
55                         }
56                 }
57         }
58         return NULL;
59 }
60
61 void
62 oidm_destroy()
63 {
64         OidMacro *om, *n;
65
66         for (om = om_list; om; om = n) {
67                 n = om->som_next;
68                 ldap_charray_free(om->som_names);
69                 free(om->som_oid.bv_val);
70                 free(om);
71         }
72 }
73
74 int
75 parse_oidm(
76     const char  *fname,
77     int         lineno,
78     int         argc,
79     char        **argv
80 )
81 {
82         char *oid;
83         OidMacro *om;
84
85         if (argc != 3) {
86                 fprintf( stderr, "%s: line %d: too many arguments\n",
87                         fname, lineno );
88 usage:  fprintf( stderr, "\tObjectIdentifier <name> <oid>\n");
89                 return 1;
90         }
91
92         oid = oidm_find( argv[1] );
93         if( oid != NULL ) {
94                 fprintf( stderr,
95                         "%s: line %d: "
96                         "ObjectIdentifier \"%s\" previously defined \"%s\"",
97                         fname, lineno, argv[1], oid );
98                 return 1;
99         }
100
101         om = (OidMacro *) ch_malloc( sizeof(OidMacro) );
102
103         om->som_names = NULL;
104         ldap_charray_add( &om->som_names, argv[1] );
105         om->som_oid.bv_val = oidm_find( argv[2] );
106
107         if (!om->som_oid.bv_val) {
108                 fprintf( stderr, "%s: line %d: OID %s not recognized\n",
109                         fname, lineno, argv[2] );
110                 goto usage;
111         }
112
113         if (om->som_oid.bv_val == argv[2]) {
114                 om->som_oid.bv_val = ch_strdup( argv[2] );
115         }
116
117         om->som_oid.bv_len = strlen( om->som_oid.bv_val );
118         om->som_next = om_list;
119         om_list = om;
120
121         return 0;
122 }