Rework generation of the objectClass and attributeType lines.
[tprouty/samba.git] / source4 / dsdb / schema / schema_constructed.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB schema constructed attributes
4    attributeTypes, objectClasses, dITContentRules...
5    
6    Copyright (C) Stefan Metzmacher 2006
7     
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20    
21 */
22 #include "includes.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "librpc/gen_ndr/ndr_drsuapi.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "system/time.h"
27 #include "lib/charset/charset.h"
28 #include "librpc/ndr/libndr.h"
29
30 static char *dsdb_subSchema_list_append(char *v, const char *list_name)
31 {
32         bool first = true;
33         uint32_t i;
34         const char *attrs[] = {
35                 "attr1",
36                 "attr2",
37                 "attr3",
38                 NULL
39         };
40
41         v = talloc_asprintf_append(v, "%s ( ", list_name);
42         if (!v) return NULL;
43
44         for (i=0; attrs[i]; i++) {
45                 v = talloc_asprintf_append(v, "%s%s ",
46                                            (!first ? "$ " : ""),
47                                            attrs[i]);
48                 if (!v) return NULL;
49                 first = false;
50         }
51
52         v = talloc_asprintf_append(v, ") ");
53         if (!v) return NULL;
54
55         return v;
56 }
57
58 WERROR dsdb_subSchema_attributeTypes(const struct dsdb_schema *schema,
59                                      TALLOC_CTX *mem_ctx)
60 {
61         struct ldb_message_element *e;
62         struct dsdb_attribute *a;
63
64         e = talloc_zero(mem_ctx, struct ldb_message_element);
65         W_ERROR_HAVE_NO_MEMORY(e);
66
67         for (a = schema->attributes; a; a = a->next) {
68                 char *v;
69
70                 v = talloc_asprintf(e, "( %s NAME '%s' SYNTAX '%s' ",
71                                     a->attributeID_oid, a->lDAPDisplayName,
72                                     a->syntax->ldap_oid);
73                 W_ERROR_HAVE_NO_MEMORY(v);
74
75                 if (a->isSingleValued) {
76                         v = talloc_asprintf_append(v, "SINGLE-VALUE ");
77                         W_ERROR_HAVE_NO_MEMORY(v);
78                 }
79
80                 if (a->systemOnly) {
81                         v = talloc_asprintf_append(v, "NO-USER-MODIFICATION ");
82                         W_ERROR_HAVE_NO_MEMORY(v);
83                 }
84
85                 v = talloc_asprintf_append(v, ")");
86                 W_ERROR_HAVE_NO_MEMORY(v);
87
88                 DEBUG(0,("%s\n", v));
89         }
90
91         return WERR_FOOBAR;
92 }
93
94 WERROR dsdb_subSchema_objectClasses(const struct dsdb_schema *schema,
95                                     TALLOC_CTX *mem_ctx)
96 {
97         struct ldb_message_element *e;
98         struct dsdb_class *c;
99
100         e = talloc_zero(mem_ctx, struct ldb_message_element);
101         W_ERROR_HAVE_NO_MEMORY(e);
102
103         for (c = schema->classes; c; c = c->next) {
104                 const char *class_type;
105                 char *v;
106
107                 switch (c->objectClassCategory) {
108                 case 0:
109                         /*
110                          * NOTE: this is an type 88 class
111                          *       e.g. 2.5.6.6 NAME 'person'
112                          *       but w2k3 gives STRUCTURAL here!
113                          */
114                         class_type = "STRUCTURAL";
115                         break;
116                 case 1:
117                         class_type = "STRUCTURAL";
118                         break;
119                 case 2:
120                         class_type = "ABSTRACT";
121                         break;
122                 case 3:
123                         class_type = "AUXILIARY";
124                         break;
125                 default:
126                         class_type = "UNKNOWN";
127                         break;
128                 }
129
130                 v = talloc_asprintf(e, "( %s NAME '%s' SUB %s %s ",
131                                     c->governsID_oid, c->lDAPDisplayName,
132                                     c->subClassOf, class_type);
133                 W_ERROR_HAVE_NO_MEMORY(v);
134
135                 v = dsdb_subSchema_list_append(v, "MUST");
136                 W_ERROR_HAVE_NO_MEMORY(v);
137
138                 v = dsdb_subSchema_list_append(v, "MAY");
139                 W_ERROR_HAVE_NO_MEMORY(v);
140
141                 v = talloc_asprintf_append(v, ")");
142                 W_ERROR_HAVE_NO_MEMORY(v);
143
144                 DEBUG(0,("%s\n", v));
145         }
146
147         return WERR_FOOBAR;
148 }
149
150 WERROR dsdb_subSchema_dITContentRules(const struct dsdb_schema *schema,
151                                       TALLOC_CTX *mem_ctx)
152 {
153         struct ldb_message_element *e;
154         struct dsdb_class *c;
155
156         e = talloc_zero(mem_ctx, struct ldb_message_element);
157         W_ERROR_HAVE_NO_MEMORY(e);
158
159         for (c = schema->classes; c; c = c->next) {
160                 char *v;
161
162                 /*
163                  * TODO: filter out classes without auxiliary classes
164                  */
165
166                 v = talloc_asprintf(e, "( %s NAME '%s' ",
167                                     c->governsID_oid, c->lDAPDisplayName);
168                 W_ERROR_HAVE_NO_MEMORY(v);
169
170                 v = dsdb_subSchema_list_append(v, "AUX");
171                 W_ERROR_HAVE_NO_MEMORY(v);
172
173                 v = dsdb_subSchema_list_append(v, "MUST");
174                 W_ERROR_HAVE_NO_MEMORY(v);
175
176                 v = dsdb_subSchema_list_append(v, "MAY");
177                 W_ERROR_HAVE_NO_MEMORY(v);
178
179                 v = talloc_asprintf_append(v, ")");
180                 W_ERROR_HAVE_NO_MEMORY(v);
181
182                 DEBUG(0,("%s\n", v));
183         }
184
185         return WERR_FOOBAR;
186 }