1835b7aa426d75235f0f87ea9a75cbb427ce5f59
[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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "librpc/gen_ndr/ndr_drsuapi.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "system/time.h"
28 #include "lib/charset/charset.h"
29 #include "librpc/ndr/libndr.h"
30
31 static char *dsdb_subSchema_list_append(char *v, const char *list_name)
32 {
33         bool first = true;
34         uint32_t i;
35         const char *attrs[] = {
36                 "attr1",
37                 "attr2",
38                 "attr3",
39                 NULL
40         };
41
42         if (!attrs) {
43                 return v;
44         }
45
46         v = talloc_asprintf_append(v, "%s ( ", list_name);
47         if (!v) return NULL;
48
49         for (i=0; attrs[i]; i++) {
50                 v = talloc_asprintf_append(v, "%s%s ",
51                                            (!first ? "$ " : ""),
52                                            attrs[i]);
53                 if (!v) return NULL;
54                 first = false;
55         }
56
57         v = talloc_asprintf_append(v, ") ");
58         if (!v) return NULL;
59
60         return v;
61 }
62
63 WERROR dsdb_subSchema_attributeTypes(const struct dsdb_schema *schema,
64                                      TALLOC_CTX *mem_ctx)
65 {
66         struct ldb_message_element *e;
67         struct dsdb_attribute *a;
68
69         e = talloc_zero(mem_ctx, struct ldb_message_element);
70         W_ERROR_HAVE_NO_MEMORY(e);
71
72         for (a = schema->attributes; a; a = a->next) {
73                 char *v;
74
75                 v = talloc_asprintf(e, "( %s NAME '%s' SYNTAX '%s' ",
76                                     a->attributeID_oid, a->lDAPDisplayName,
77                                     a->syntax->ldap_oid);
78                 W_ERROR_HAVE_NO_MEMORY(v);
79
80                 if (a->isSingleValued) {
81                         v = talloc_asprintf_append(v, "SINGLE-VALUE ");
82                         W_ERROR_HAVE_NO_MEMORY(v);
83                 }
84
85                 if (a->systemOnly) {
86                         v = talloc_asprintf_append(v, "NO-USER-MODIFICATION ");
87                         W_ERROR_HAVE_NO_MEMORY(v);
88                 }
89
90                 v = talloc_asprintf_append(v, ")");
91                 W_ERROR_HAVE_NO_MEMORY(v);
92
93                 DEBUG(0,("%s\n", v));
94         }
95
96         return WERR_FOOBAR;
97 }
98
99 WERROR dsdb_subSchema_objectClasses(const struct dsdb_schema *schema,
100                                     TALLOC_CTX *mem_ctx)
101 {
102         struct ldb_message_element *e;
103         struct dsdb_class *c;
104
105         e = talloc_zero(mem_ctx, struct ldb_message_element);
106         W_ERROR_HAVE_NO_MEMORY(e);
107
108         for (c = schema->classes; c; c = c->next) {
109                 const char *class_type;
110                 char *v;
111
112                 switch (c->objectClassCategory) {
113                 case 0:
114                         /*
115                          * NOTE: this is an type 88 class
116                          *       e.g. 2.5.6.6 NAME 'person'
117                          *       but w2k3 gives STRUCTURAL here!
118                          */
119                         class_type = "STRUCTURAL";
120                         break;
121                 case 1:
122                         class_type = "STRUCTURAL";
123                         break;
124                 case 2:
125                         class_type = "ABSTRACT";
126                         break;
127                 case 3:
128                         class_type = "AUXILIARY";
129                         break;
130                 default:
131                         class_type = "UNKNOWN";
132                         break;
133                 }
134
135                 v = talloc_asprintf(e, "( %s NAME '%s' SUB %s %s ",
136                                     c->governsID_oid, c->lDAPDisplayName,
137                                     c->subClassOf, class_type);
138                 W_ERROR_HAVE_NO_MEMORY(v);
139
140                 v = dsdb_subSchema_list_append(v, "MUST");
141                 W_ERROR_HAVE_NO_MEMORY(v);
142
143                 v = dsdb_subSchema_list_append(v, "MAY");
144                 W_ERROR_HAVE_NO_MEMORY(v);
145
146                 v = talloc_asprintf_append(v, ")");
147                 W_ERROR_HAVE_NO_MEMORY(v);
148
149                 DEBUG(0,("%s\n", v));
150         }
151
152         return WERR_FOOBAR;
153 }
154
155 WERROR dsdb_subSchema_dITContentRules(const struct dsdb_schema *schema,
156                                       TALLOC_CTX *mem_ctx)
157 {
158         struct ldb_message_element *e;
159         struct dsdb_class *c;
160
161         e = talloc_zero(mem_ctx, struct ldb_message_element);
162         W_ERROR_HAVE_NO_MEMORY(e);
163
164         for (c = schema->classes; c; c = c->next) {
165                 char *v;
166
167                 /*
168                  * TODO: filter out classes without auxiliary classes
169                  */
170
171                 v = talloc_asprintf(e, "( %s NAME '%s' ",
172                                     c->governsID_oid, c->lDAPDisplayName);
173                 W_ERROR_HAVE_NO_MEMORY(v);
174
175                 v = dsdb_subSchema_list_append(v, "AUX");
176                 W_ERROR_HAVE_NO_MEMORY(v);
177
178                 v = dsdb_subSchema_list_append(v, "MUST");
179                 W_ERROR_HAVE_NO_MEMORY(v);
180
181                 v = dsdb_subSchema_list_append(v, "MAY");
182                 W_ERROR_HAVE_NO_MEMORY(v);
183
184                 v = talloc_asprintf_append(v, ")");
185                 W_ERROR_HAVE_NO_MEMORY(v);
186
187                 DEBUG(0,("%s\n", v));
188         }
189
190         return WERR_FOOBAR;
191 }