LCOV - code coverage report
Current view: top level - source4/dsdb/schema - schema_description.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 95 145 65.5 %
Date: 2024-01-11 09:59:51 Functions: 5 7 71.4 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS Implementation.
       3             :    Print schema info into string format
       4             :    
       5             :    Copyright (C) Andrew Bartlett 2006-2008
       6             :     
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             :    
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             :    
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             :    
      20             : */
      21             : #include "includes.h"
      22             : #include "dsdb/samdb/samdb.h"
      23             : #include "librpc/ndr/libndr.h"
      24             : 
      25             : #undef strcasecmp
      26             : 
      27       12169 : char *schema_attribute_description(TALLOC_CTX *mem_ctx, 
      28             :                                           enum dsdb_schema_convert_target target,
      29             :                                           const char *separator,
      30             :                                           const char *oid, 
      31             :                                           const char *name,
      32             :                                           const char *equality, 
      33             :                                           const char *substring, 
      34             :                                           const char *syntax,
      35             :                                           bool single_value, bool operational,
      36             :                                           uint32_t *range_lower,
      37             :                                           uint32_t *range_upper,
      38             :                                           const char *property_guid,
      39             :                                           const char *property_set_guid,
      40             :                                           bool indexed, bool system_only)
      41             : {
      42       12169 :         char *schema_entry = talloc_asprintf(mem_ctx, 
      43             :                                              "(%s%s%s", separator, oid, separator);
      44             : 
      45       12169 :         talloc_asprintf_addbuf(
      46             :                 &schema_entry, "NAME '%s'%s", name, separator);
      47             : 
      48       12169 :         if (equality) {
      49           0 :                 talloc_asprintf_addbuf(
      50             :                         &schema_entry, "EQUALITY %s%s", equality, separator);
      51             :         }
      52       12169 :         if (substring) {
      53           0 :                 talloc_asprintf_addbuf(
      54             :                         &schema_entry, "SUBSTR %s%s", substring, separator);
      55             :         }
      56             : 
      57       12169 :         if (syntax) {
      58       12169 :                 talloc_asprintf_addbuf(
      59             :                         &schema_entry, "SYNTAX %s%s", syntax, separator);
      60             :         }
      61             : 
      62       12169 :         if (single_value) {
      63        8502 :                 talloc_asprintf_addbuf(
      64             :                         &schema_entry, "SINGLE-VALUE%s", separator);
      65             :         }
      66             : 
      67       12169 :         if (operational) {
      68        1389 :                 talloc_asprintf_addbuf(
      69             :                         &schema_entry, "NO-USER-MODIFICATION%s", separator);
      70             :         }
      71             : 
      72       12169 :         if (range_lower) {
      73           0 :                 talloc_asprintf_addbuf(
      74             :                         &schema_entry,
      75             :                         "RANGE-LOWER '%u'%s",
      76             :                         *range_lower,
      77             :                         separator);
      78             :         }
      79             : 
      80       12169 :         if (range_upper) {
      81           0 :                 talloc_asprintf_addbuf(
      82             :                         &schema_entry,
      83             :                         "RANGE-UPPER '%u'%s",
      84             :                         *range_upper,
      85             :                         separator);
      86             :         }
      87             : 
      88       12169 :         if (property_guid) {
      89           0 :                 talloc_asprintf_addbuf(
      90             :                         &schema_entry,
      91             :                         "PROPERTY-GUID '%s'%s",
      92             :                         property_guid,
      93             :                         separator);
      94             :         }
      95             : 
      96       12169 :         if (property_set_guid) {
      97           0 :                 talloc_asprintf_addbuf(
      98             :                         &schema_entry,
      99             :                         "PROPERTY-SET-GUID '%s'%s",
     100             :                         property_set_guid,
     101             :                         separator);
     102             :         }
     103             : 
     104       12169 :         if (indexed) {
     105           0 :                 talloc_asprintf_addbuf(
     106             :                         &schema_entry, "INDEXED%s", separator);
     107             :         }
     108             : 
     109       12169 :         if (system_only) {
     110           0 :                 talloc_asprintf_addbuf(
     111             :                         &schema_entry, "SYSTEM-ONLY%s", separator);
     112             :         }
     113             : 
     114       12169 :         talloc_asprintf_addbuf(&schema_entry, ")");
     115             : 
     116       12169 :         return schema_entry;
     117             : }
     118             : 
     119       12169 : char *schema_attribute_to_description(TALLOC_CTX *mem_ctx, const struct dsdb_attribute *attribute) 
     120             : {
     121           0 :         char *schema_description;
     122       12169 :         const char *syntax = attribute->syntax->ldap_oid;
     123       12169 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     124       12169 :         if (!tmp_ctx) {
     125           0 :                 return NULL;
     126             :         }
     127             : 
     128           0 :         schema_description 
     129       24338 :                 = schema_attribute_description(mem_ctx, 
     130             :                                                TARGET_AD_SCHEMA_SUBENTRY,
     131             :                                                " ",
     132       12169 :                                                attribute->attributeID_oid,
     133       12169 :                                                attribute->lDAPDisplayName,
     134       12169 :                                                NULL, NULL, talloc_asprintf(tmp_ctx, "'%s'", syntax),
     135       12169 :                                                attribute->isSingleValued,
     136       12169 :                                                attribute->systemOnly,/* TODO: is this correct? */
     137             :                                                NULL, NULL, NULL, NULL,
     138             :                                                false, false);
     139       12169 :         talloc_free(tmp_ctx);
     140       12169 :         return schema_description;
     141             : }
     142             : 
     143           0 : char *schema_attribute_to_extendedInfo(TALLOC_CTX *mem_ctx, const struct dsdb_attribute *attribute)
     144             : {
     145           0 :         char *schema_description;
     146           0 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     147           0 :         if (!tmp_ctx) {
     148           0 :                 return NULL;
     149             :         }
     150             : 
     151           0 :         schema_description
     152           0 :                 = schema_attribute_description(mem_ctx,
     153             :                                                TARGET_AD_SCHEMA_SUBENTRY,
     154             :                                                " ",
     155           0 :                                                attribute->attributeID_oid,
     156           0 :                                                attribute->lDAPDisplayName,
     157             :                                                NULL, NULL, NULL,
     158             :                                                false, false,
     159           0 :                                                attribute->rangeLower,
     160           0 :                                                attribute->rangeUpper,
     161           0 :                                                GUID_hexstring(tmp_ctx, &attribute->schemaIDGUID),
     162           0 :                                                GUID_hexstring(tmp_ctx, &attribute->attributeSecurityGUID),
     163             :                                                /*
     164             :                                                 * We actually ignore the indexed
     165             :                                                 * flag for confidential
     166             :                                                 * attributes, but we'll include
     167             :                                                 * it for the purposes of
     168             :                                                 * description.
     169             :                                                 */
     170           0 :                                                (attribute->searchFlags & SEARCH_FLAG_ATTINDEX),
     171           0 :                                                attribute->systemOnly);
     172           0 :         talloc_free(tmp_ctx);
     173           0 :         return schema_description;
     174             : }
     175             : 
     176             : #define APPEND_ATTRS(attributes)                                \
     177             :         do {                                                            \
     178             :                 unsigned int k;                                         \
     179             :                 for (k=0; attributes && attributes[k]; k++) {           \
     180             :                         const char *attr_name = attributes[k];          \
     181             :                                                                         \
     182             :                         talloc_asprintf_addbuf(&schema_entry,           \
     183             :                                                               "%s ",  \
     184             :                                                               attr_name); \
     185             :                         if (attributes[k+1]) {                          \
     186             :                                 if (target == TARGET_OPENLDAP && ((k+1)%5 == 0)) { \
     187             :                                         talloc_asprintf_addbuf(&schema_entry, \
     188             :                                                                               "$%s ", separator); \
     189             :                                 } else {                                \
     190             :                                         talloc_asprintf_addbuf(&schema_entry, \
     191             :                                                                               "$ "); \
     192             :                                 }                                       \
     193             :                         }                                               \
     194             :                 }                                                       \
     195             :         } while (0)
     196             :         
     197             : 
     198             : /* Print a schema class or dITContentRule as a string.  
     199             :  *
     200             :  * To print a scheam class, specify objectClassCategory but not auxillary_classes
     201             :  * To print a dITContentRule, specify auxillary_classes but set objectClassCategory == -1
     202             :  *
     203             :  */
     204             : 
     205        2242 : char *schema_class_description(TALLOC_CTX *mem_ctx, 
     206             :                                enum dsdb_schema_convert_target target,
     207             :                                const char *separator,
     208             :                                const char *oid, 
     209             :                                const char *name,
     210             :                                const char **auxillary_classes,
     211             :                                const char *subClassOf,
     212             :                                int objectClassCategory,
     213             :                                const char **must,
     214             :                                const char **may,
     215             :                                const char *schemaHexGUID)
     216             : {
     217        2242 :         char *schema_entry = talloc_asprintf(mem_ctx, 
     218             :                                              "(%s%s%s", separator, oid, separator);
     219             : 
     220        2242 :         talloc_asprintf_addbuf(&schema_entry, "NAME '%s'%s", name, separator);
     221             : 
     222        2242 :         if (auxillary_classes) {
     223          81 :                 talloc_asprintf_addbuf(&schema_entry, "AUX ( ");
     224             : 
     225         226 :                 APPEND_ATTRS(auxillary_classes);
     226             : 
     227          81 :                 talloc_asprintf_addbuf(&schema_entry, ")%s", separator);
     228             :         }
     229             : 
     230        2242 :         if (subClassOf && strcasecmp(subClassOf, name) != 0) {
     231        2152 :                 talloc_asprintf_addbuf(
     232             :                         &schema_entry, "SUP %s%s", subClassOf, separator);
     233             :         }
     234             : 
     235        2242 :         switch (objectClassCategory) {
     236          81 :         case -1:
     237          81 :                 break;
     238             :                 /* Dummy case for when used for printing ditContentRules */
     239          54 :         case 0:
     240             :                 /*
     241             :                  * NOTE: this is an type 88 class
     242             :                  *       e.g. 2.5.6.6 NAME 'person'
     243             :                  *       but w2k3 gives STRUCTURAL here!
     244             :                  */
     245          54 :                 talloc_asprintf_addbuf(
     246             :                         &schema_entry, "STRUCTURAL%s", separator);
     247          54 :                 break;
     248        1907 :         case 1:
     249        1907 :                 talloc_asprintf_addbuf(
     250             :                         &schema_entry, "STRUCTURAL%s", separator);
     251        1907 :                 break;
     252          82 :         case 2:
     253          82 :                 talloc_asprintf_addbuf(
     254             :                         &schema_entry, "ABSTRACT%s", separator);
     255          82 :                 break;
     256         118 :         case 3:
     257         118 :                 talloc_asprintf_addbuf(
     258             :                         &schema_entry, "AUXILIARY%s", separator);
     259         118 :                 break;
     260             :         }
     261             : 
     262        2242 :         if (must) {
     263         960 :                 talloc_asprintf_addbuf(
     264             :                         &schema_entry,
     265             :                         "MUST (%s",
     266             :                         target == TARGET_AD_SCHEMA_SUBENTRY ? "" : " ");
     267             : 
     268        2974 :                 APPEND_ATTRS(must);
     269             : 
     270         960 :                 talloc_asprintf_addbuf(
     271             :                         &schema_entry, ")%s", separator);
     272             :         }
     273             : 
     274        2242 :         if (may) {
     275        1849 :                 talloc_asprintf_addbuf(
     276             :                         &schema_entry,
     277             :                         "MAY (%s",
     278             :                         target == TARGET_AD_SCHEMA_SUBENTRY ? "" : " ");
     279             : 
     280       20376 :                 APPEND_ATTRS(may);
     281             : 
     282        1849 :                 talloc_asprintf_addbuf(
     283             :                         &schema_entry, ")%s", separator);
     284             :         }
     285             : 
     286        2242 :         if (schemaHexGUID) {
     287           0 :                 talloc_asprintf_addbuf(
     288             :                         &schema_entry,
     289             :                         "CLASS-GUID '%s'%s",
     290             :                         schemaHexGUID,
     291             :                         separator);
     292             :         }
     293             : 
     294        2242 :         talloc_asprintf_addbuf(&schema_entry, ")");
     295             : 
     296        2242 :         return schema_entry;
     297             : }
     298             : 
     299        2161 : char *schema_class_to_description(TALLOC_CTX *mem_ctx, const struct dsdb_class *sclass)
     300             : {
     301           0 :         char *schema_description;
     302        2161 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     303        2161 :         if (!tmp_ctx) {
     304           0 :                 return NULL;
     305             :         }
     306             :         
     307           0 :         schema_description
     308        2161 :                 = schema_class_description(mem_ctx, 
     309             :                                            TARGET_AD_SCHEMA_SUBENTRY,
     310             :                                            " ",
     311        2161 :                                            sclass->governsID_oid,
     312        2161 :                                            sclass->lDAPDisplayName,
     313             :                                            NULL, 
     314        2161 :                                            sclass->subClassOf,
     315        2161 :                                            sclass->objectClassCategory,
     316             :                                            dsdb_attribute_list(tmp_ctx, 
     317             :                                                                sclass, DSDB_SCHEMA_ALL_MUST),
     318             :                                            dsdb_attribute_list(tmp_ctx, 
     319             :                                                                sclass, DSDB_SCHEMA_ALL_MAY),
     320             :                                            NULL);
     321        2161 :         talloc_free(tmp_ctx);
     322        2161 :         return schema_description;
     323             : }
     324             : 
     325          81 : char *schema_class_to_dITContentRule(TALLOC_CTX *mem_ctx, const struct dsdb_class *sclass,
     326             :                                      const struct dsdb_schema *schema)
     327             : {
     328           0 :         unsigned int i;
     329           0 :         char *schema_description;
     330          81 :         const char **aux_class_list = NULL;
     331           0 :         const char **attrs;
     332          81 :         const char **must_attr_list = NULL;
     333          81 :         const char **may_attr_list = NULL;
     334          81 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     335           0 :         const struct dsdb_class *aux_class;
     336          81 :         if (!tmp_ctx) {
     337           0 :                 return NULL;
     338             :         }
     339             : 
     340          81 :         aux_class_list = merge_attr_list(tmp_ctx, aux_class_list, sclass->systemAuxiliaryClass);
     341          81 :         aux_class_list = merge_attr_list(tmp_ctx, aux_class_list, sclass->auxiliaryClass);
     342             : 
     343         226 :         for (i=0; aux_class_list && aux_class_list[i]; i++) {
     344         145 :                 aux_class = dsdb_class_by_lDAPDisplayName(schema, aux_class_list[i]);
     345             :                 
     346         145 :                 attrs = dsdb_attribute_list(mem_ctx, aux_class, DSDB_SCHEMA_ALL_MUST);
     347         145 :                 must_attr_list = merge_attr_list(mem_ctx, must_attr_list, attrs);
     348             : 
     349         145 :                 attrs = dsdb_attribute_list(mem_ctx, aux_class, DSDB_SCHEMA_ALL_MAY);
     350         145 :                 may_attr_list = merge_attr_list(mem_ctx, may_attr_list, attrs);
     351             :         }
     352             : 
     353           0 :         schema_description
     354          81 :                 = schema_class_description(mem_ctx, 
     355             :                                            TARGET_AD_SCHEMA_SUBENTRY,
     356             :                                            " ",
     357          81 :                                            sclass->governsID_oid,
     358          81 :                                            sclass->lDAPDisplayName,
     359             :                                            (const char **)aux_class_list,
     360             :                                            NULL, /* Must not specify a
     361             :                                                   * SUP (subclass) in
     362             :                                                   * ditContentRules
     363             :                                                   * per MS-ADTS
     364             :                                                   * 3.1.1.3.1.1.1 */
     365             :                                            -1, must_attr_list, may_attr_list,
     366             :                                            NULL);
     367          81 :         talloc_free(tmp_ctx);
     368          81 :         return schema_description;
     369             : }
     370             : 
     371           0 : char *schema_class_to_extendedInfo(TALLOC_CTX *mem_ctx, const struct dsdb_class *sclass)
     372             : {
     373           0 :         char *schema_description = NULL;
     374           0 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     375           0 :         if (!tmp_ctx) {
     376           0 :                 return NULL;
     377             :         }
     378             : 
     379           0 :         schema_description
     380           0 :                 = schema_class_description(mem_ctx,
     381             :                                            TARGET_AD_SCHEMA_SUBENTRY,
     382             :                                            " ",
     383           0 :                                            sclass->governsID_oid,
     384           0 :                                            sclass->lDAPDisplayName,
     385             :                                            NULL,
     386             :                                            NULL, /* Must not specify a
     387             :                                                   * SUP (subclass) in
     388             :                                                   * ditContentRules
     389             :                                                   * per MS-ADTS
     390             :                                                   * 3.1.1.3.1.1.1 */
     391             :                                            -1, NULL, NULL,
     392           0 :                                            GUID_hexstring(tmp_ctx, &sclass->schemaIDGUID));
     393           0 :         talloc_free(tmp_ctx);
     394           0 :         return schema_description;
     395             : }
     396             : 
     397             : 

Generated by: LCOV version 1.14