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