aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/libldapschema/lldap.c
blob: daa219042a5bcc257f33556fb63f8de460fc469d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

/*
 *  LDAP Utilities
 *  Copyright (C) 2012, 2019 David M. Syzdek <david@syzdek.net>.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *     1. Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *
 *     2. Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *
 *     3. Neither the name of the copyright holder nor the names of its
 *        contributors may be used to endorse or promote products derived from
 *        this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 *  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 *   @file lib/libldapschema/lldap.c  contains LDAP functions
 */
#define _LIB_LIBLDAPSCHEMA_LLDAP_C 1
#include "lldap.h"

///////////////
//           //
//  Headers  //
//           //
///////////////
#pragma mark - Headers

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

#include "llexer.h"
#include "lquery.h"
#include "lerror.h"
#include "lmemory.h"
#include "lsort.h"


/////////////////
//             //
//  Functions  //
//             //
/////////////////
#pragma mark - Functions

int ldapschema_fetch(LDAPSchema * lsd, LDAP * ld)
{
   int                  err;
   int                  x;
   struct timeval       timeout;
   LDAPMessage        * res;
   LDAPMessage        * msg;
   char              ** dns;
   char              ** attrs;
   struct berval     ** vals;
   void               * ptr;
   size_t               idx;
   size_t               subidx;
   LDAPSchemaAlias    * alias;
   LDAPSchemaAttributeType     * attr;
   LDAPSchemaAttributeType     * attrsup;
   LDAPSchemaObjectclass       * objcls;
   LDAPSchemaObjectclass       * objclssup;
   LDAPSchemaMatchingRule *      mtchngrl;

   assert(lsd != NULL);
   assert(ld  != NULL);

   // reset errors
   if ((lsd->schema_errs))
      ldapschema_value_free(lsd->schema_errs);
   lsd->schema_errs     = NULL;

   attrs = NULL;
   if ((err = ldapschema_definition_split(lsd, NULL, "( + * )", 7, &attrs)) == -1)
      return(lsd->errcode);

   // searches for schema DN
   timeout.tv_sec    = 5;
   timeout.tv_usec   = 0;
   res               = NULL;
   if ((err = ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, NULL, NULL, &timeout, 0, &res)) != LDAP_SUCCESS)
   {
      ldapschema_value_free(attrs);
      return(err);
   };
   if ((msg = ldap_first_entry(ld, res)) == NULL)
   {
      ldap_msgfree(res);
      return(-1);
   };
   if ((dns = ldap_get_values(ld, msg, "subschemaSubentry")) == NULL)
   {
      ldap_msgfree(res);
      ldap_value_free(dns);
      return(-1);
   };
   ldap_msgfree(res);

   // searches for schema entry
   timeout.tv_sec    = 5;
   timeout.tv_usec   = 0;
   res               = NULL;
   if ((err = ldap_search_ext_s(ld, dns[0], LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, NULL, NULL, &timeout, 0, &res)) != LDAP_SUCCESS)
   {
      ldap_value_free(dns);
      ldapschema_value_free(attrs);
      return(-1);
   };
   ldap_value_free(dns);
   ldapschema_value_free(attrs);
   dns   = NULL;
   attrs = NULL;
   if ((msg = ldap_first_entry(ld, res)) == NULL)
   {
      ldap_msgfree(res);
      return(-1);
   };

   // process ldapSyntaxes
   if ((vals = ldap_get_values_len(ld, msg, "ldapSyntaxes")) != NULL)
   {
      for(x = 0; ((vals[x])); x++)
      {
         if ( ((ptr = ldapschema_parse_syntax(lsd, vals[x])) == NULL) &&
              (lsd->errcode != LDAPSCHEMA_SCHEMA_ERROR) )
         {
            ldap_value_free_len(vals);
            ldap_msgfree(res);
            return(-1);
         };
      };
      ldap_value_free_len(vals);
   };

   // process matchingRule
   if ((vals = ldap_get_values_len(ld, msg, "matchingRules")) != NULL)
   {
      for(x = 0; ((vals[x])); x++)
      {
         if ( ((ptr = ldapschema_parse_matchingrule(lsd, vals[x])) == NULL) &&
              (lsd->errcode != LDAPSCHEMA_SCHEMA_ERROR) )
         {
            ldap_value_free_len(vals);
            ldap_msgfree(res);
            return(-1);
         };
      };
      ldap_value_free_len(vals);

      // checks attribute
       for(idx = 0; (idx < lsd->oids_len); idx++)
       {
          mtchngrl = lsd->oids[idx].matchingrule;
          if (mtchngrl->model.type != LDAPSCHEMA_MATCHINGRULE)
             continue;

          if (!(mtchngrl->names))
             ldapschema_schema_err(lsd, (LDAPSchemaModel *)mtchngrl, "missing NAME");

          if (!(mtchngrl->syntax))
             ldapschema_schema_err(lsd, (LDAPSchemaModel *)mtchngrl, "missing or unknown SYNTAX");
       };
   };

   // process attributeTypes
   if ((vals = ldap_get_values_len(ld, msg, "attributeTypes")) != NULL)
   {
      // initial parsing of definition
      for(x = 0; ((vals[x])); x++)
      {
         if ( ((ptr = ldapschema_parse_attributetype(lsd, vals[x])) == NULL) &&
              (lsd->errcode != LDAPSCHEMA_SCHEMA_ERROR) )
         {
            ldap_value_free_len(vals);
            ldap_msgfree(res);
            return(-1);
         };
      };
      ldap_value_free_len(vals);

      // maps superior
      for(idx = 0; (idx < lsd->oids_len); idx++)
      {
         // checks for superior
         attr = lsd->oids[idx].attributetype;
         if (attr->model.type != LDAPSCHEMA_ATTRIBUTETYPE)
            continue;
         if (!(attr->sup_name))
            continue;
         if ((alias = ldapschema_find_alias(lsd, attr->sup_name, lsd->attrs, lsd->attrs_len)) == NULL)
         {
            ldapschema_schema_err(lsd, &attr->model, "specifies invalid superior '%s'", attr->sup_name);
            continue;
         };

         // saves superior
         attr->sup = alias->attributetype;

         // inherent specs from superior
         attrsup   = attr;
         while((attrsup = attrsup->sup) != NULL)
         {
            attr->model.flags |= attrsup->model.flags;
            if (!(attr->syntax))
               if ((attr->syntax = attrsup->syntax) != NULL)
                  ldapschema_insert(lsd, (void ***)&attr->syntax->attrs, &attr->syntax->attrs_len, attr, ldapschema_compar_models);
            if (!(attr->min_upper))
               attr->min_upper = attrsup->min_upper;
            if (!(attr->usage))
               attr->usage = attrsup->usage;
            if (!(attr->equality))
               if ((attr->equality = attrsup->equality) != NULL)
                  ldapschema_insert(lsd, (void ***)&attr->equality->used_by, &attr->equality->used_by_len, attr, ldapschema_compar_models);
            if (!(attr->ordering))
               if ((attr->ordering = attrsup->ordering) != NULL)
                  ldapschema_insert(lsd, (void ***)&attr->ordering->used_by, &attr->ordering->used_by_len, attr, ldapschema_compar_models);
            if (!(attr->substr))
               if ((attr->substr = attrsup->substr) != NULL)
                  ldapschema_insert(lsd, (void ***)&attr->substr->used_by, &attr->substr->used_by_len, attr, ldapschema_compar_models);
         };
      };

      // checks attribute
      for(idx = 0; (idx < lsd->oids_len); idx++)
      {
         attr = lsd->oids[idx].attributetype;
         if (attr->model.type != LDAPSCHEMA_ATTRIBUTETYPE)
            continue;

         if (!(attr->syntax))
            ldapschema_schema_err(lsd, (LDAPSchemaModel *)attr, "missing or unknown SYNTAX");
      };
   };

   // process objectClasses
   if ((vals = ldap_get_values_len(ld, msg, "objectClasses")) != NULL)
   {
      // initial parsing of definition
      for(x = 0; ((vals[x])); x++)
      {
         if ( ((ptr = ldapschema_parse_objectclass(lsd, vals[x])) == NULL) &&
              (lsd->errcode != LDAPSCHEMA_SCHEMA_ERROR) )
         {
            ldap_value_free_len(vals);
            ldap_msgfree(res);
            return(-1);
         };
      };
      ldap_value_free_len(vals);

      // maps superior
      for(idx = 0; (idx < lsd->oids_len); idx++)
      {
         // checks for superior
         objcls = lsd->oids[idx].objectclass;
         if (objcls->model.type != LDAPSCHEMA_OBJECTCLASS)
            continue;
         if (!(objcls->sup_name))
            continue;
         if ((alias = ldapschema_find_alias(lsd, objcls->sup_name, lsd->objclses, lsd->objclses_len)) == NULL)
         {
            ldapschema_schema_err(lsd, &objcls->model, "specifies invalid superior '%s'", objcls->sup_name);
            continue;
         };

         // saves superior
         objcls->sup = alias->objectclass;

         // inherent specs from superior
         objclssup   = objcls;
         while((objclssup = objclssup->sup) != NULL)
         {
            objcls->model.flags |= objclssup->model.flags;
            if (!(objcls->kind))
               objcls->kind = objclssup->kind;
            for(subidx = 0; (subidx < objclssup->may_len); subidx++)
            {
               if ((err = ldapschema_objectclass_attribute(lsd, objcls, objclssup->may[subidx], 0, 1)) > 0)
               {
                  ldap_value_free_len(vals);
                  ldap_msgfree(res);
                  return(lsd->errcode);
               };
            };
            for(subidx = 0; (subidx < objclssup->must_len); subidx++)
            {
               if ((err = ldapschema_objectclass_attribute(lsd, objcls, objclssup->must[subidx], 1, 1)) > 0)
               {
                  ldap_value_free_len(vals);
                  ldap_msgfree(res);
                  return(lsd->errcode);
               };
            };
         };
      };
   };

   ldap_msgfree(res);

   if ((lsd->schema_errs))
      return(lsd->errcode = LDAPSCHEMA_SCHEMA_ERROR);
   return(LDAP_SUCCESS);
}

/* end of source file */