r9690: Extend test + fix several bugs
[jra/samba/.git] / source4 / lib / ldb / samba / samba3sam.c
1 /* 
2    ldb database library - Samba3 SAM compatibility backend
3
4    Copyright (C) Jelmer Vernooij 2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "includes.h"
26 #include "ldb/ldb_map/ldb_map.h"
27 #include "ldb/include/ldb.h"
28 #include "ldb/include/ldb_private.h"
29
30 /* FIXME: 
31  * sambaSID -> member 
32  * sambaSIDList -> member (special!) 
33  * sambaDomainName -> name 
34  * sambaTrustPassword 
35  * sambaUnixIdPool 
36  * sambaIdmapEntry 
37  * sambaAccountPolicy 
38  * sambaSidEntry 
39  * sambaAcctFlags -> systemFlags ?
40  * sambaPasswordHistory  -> ntPwdHistory*/
41
42 /* Not necessary:
43  * sambaConfig
44  * sambaShare
45  * sambaConfigOption 
46  * sambaNextGroupRid
47  * sambaNextUserRid
48  * sambaAlgorithmicRidBase
49  */
50
51 /* Not in Samba4: 
52  * sambaKickoffTime
53  * sambaPwdCanChange
54  * sambaPwdMustChange
55  * sambaHomePath
56  * sambaHomeDrive
57  * sambaLogonScript
58  * sambaProfilePath
59  * sambaUserWorkstations
60  * sambaMungedDial
61  * sambaLogonHours */
62
63 static struct ldb_message_element *convert_sid_rid(TALLOC_CTX *ctx, const char *remote_attr, const struct ldb_message_element *el)
64 {
65         struct ldb_message_element *ret = talloc(ctx, struct ldb_message_element);
66         int i;
67
68         printf("Converting SID TO RID *\n");
69
70         ret->flags = el->flags;
71         ret->name = talloc_strdup(ret, remote_attr);
72         ret->num_values = el->num_values;
73         ret->values = talloc_array(ret, struct ldb_val, ret->num_values);
74
75         for (i = 0; i < ret->num_values; i++) {
76                 ret->values[i] = ldb_val_dup(ret->values, &el->values[i]);
77         }
78
79         return ret;
80 }
81
82 static struct ldb_message_element *convert_rid_sid(TALLOC_CTX *ctx, const char *remote_attr, const struct ldb_message_element *el)
83 {
84         struct ldb_message_element *ret = talloc(ctx, struct ldb_message_element);
85         int i;
86
87         printf("Converting RID TO SID *\n");
88
89         ret->flags = el->flags;
90         ret->name = talloc_strdup(ret, remote_attr);
91         ret->num_values = el->num_values;
92         ret->values = talloc_array(ret, struct ldb_val, ret->num_values);
93
94         for (i = 0; i < ret->num_values; i++) {
95                 ret->values[i] = ldb_val_dup(ret->values, &el->values[i]);
96         }
97
98         return ret;
99 }
100
101 static struct ldb_message_element *convert_unix_id2name(TALLOC_CTX *ctx, const char *remote_attr, const struct ldb_message_element *el)
102 {
103         int i;
104         struct ldb_message_element *ret = talloc(ctx, struct ldb_message_element);
105
106         printf("Converting UNIX ID to name\n");
107
108         ret->flags = el->flags;
109         ret->name = talloc_strdup(ret, remote_attr);
110         ret->num_values = el->num_values;
111         ret->values = talloc_array(ret, struct ldb_val, ret->num_values);
112
113         for (i = 0; i < ret->num_values; i++) {
114                 ret->values[i] = ldb_val_dup(ret->values, &el->values[i]);
115         }
116
117         return ret;
118 }
119
120 static struct ldb_message_element *convert_unix_name2id(TALLOC_CTX *ctx, const char *remote_attr, const struct ldb_message_element *el)
121 {
122         struct ldb_message_element *ret = talloc(ctx, struct ldb_message_element);
123         int i;
124
125         printf("Converting UNIX name to ID\n");
126
127         ret->flags = el->flags;
128         ret->name = talloc_strdup(ret, remote_attr);
129         ret->num_values = el->num_values;
130         ret->values = talloc_array(ret, struct ldb_val, ret->num_values);
131
132         for (i = 0; i < ret->num_values; i++) {
133                 ret->values[i] = ldb_val_dup(ret->values, &el->values[i]);
134         }
135
136         return ret;
137 }
138
139 const struct ldb_map_objectclass samba3_objectclasses[] = {
140         { "group", "sambaGroupMapping" },
141         { "user", "sambaSAMAccount" },
142         { "domain", "sambaDomain" },
143 };
144
145 const struct ldb_map_attribute samba3_attributes[] = 
146 {
147         /* sambaNextRid -> nextRid */
148         {
149                 .local_name = "nextRid",
150                 .type = MAP_RENAME,
151                 .u.rename.remote_name = "sambaNextRid",
152         },
153
154         /* sambaBadPasswordTime -> badPasswordtime*/
155         {
156                 .local_name = "badPasswordTime",
157                 .type = MAP_RENAME,
158                 .u.rename.remote_name = "sambaBadPasswordTime",
159         },
160
161         /* sambaLMPassword -> lmPwdHash*/
162         {
163                 .local_name = "lmPwdHash",
164                 .type = MAP_RENAME,
165                 .u.rename.remote_name = "sambaLMPassword",
166         },
167
168         /* sambaGroupType -> groupType */
169         {
170                 .local_name = "groupType",
171                 .type = MAP_RENAME,
172                 .u.rename.remote_name = "sambaGroupType",
173         },
174
175         /* sambaNTPassword -> ntPwdHash*/
176         {
177                 .local_name = "badPwdCount",
178                 .type = MAP_RENAME,
179                 .u.rename.remote_name = "sambaNTPassword",
180         },
181
182         /* sambaPrimaryGroupSID -> primaryGroupID */
183         {
184                 .local_name = "primaryGroupID",
185                 .type = MAP_CONVERT,
186                 .u.convert.remote_name = "sambaPrimaryGroupSID",
187                 .u.convert.convert_local = convert_rid_sid,
188                 .u.convert.convert_remote = convert_sid_rid, 
189         },
190
191         /* sambaBadPasswordCount -> badPwdCount */
192         {
193                 .local_name = "badPwdCount",
194                 .type = MAP_RENAME,
195                 .u.rename.remote_name = "sambaBadPasswordCount",
196         },
197
198         /* sambaLogonTime -> lastLogon*/
199         {
200                 .local_name = "lastLogon",
201                 .type = MAP_RENAME,
202                 .u.rename.remote_name = "sambaLogonTime",
203         },
204
205         /* sambaLogoffTime -> lastLogoff*/
206         {
207                 .local_name = "lastLogoff",
208                 .type = MAP_RENAME,
209                 .u.rename.remote_name = "sambaLogoffTime",
210         },
211
212         /* gidNumber -> unixName */
213         {
214                 .local_name = "unixName",
215                 .type = MAP_CONVERT,
216                 .u.convert.remote_name = "gidNumber",
217                 .u.convert.convert_local = convert_unix_name2id,
218                 .u.convert.convert_remote = convert_unix_id2name, 
219         },
220
221         /* uid -> unixName */
222         {
223                 .local_name = "unixName",
224                 .type = MAP_CONVERT,
225                 .u.convert.remote_name = "uid",
226                 .u.convert.convert_local = convert_unix_name2id,
227                 .u.convert.convert_remote = convert_unix_id2name,
228         },
229
230         /* displayName -> name */
231         {
232                 .local_name = "name",
233                 .type = MAP_RENAME,
234                 .u.rename.remote_name = "displayName",
235         },
236
237         /* cn */
238         {
239                 .local_name = "cn",
240                 .type = MAP_KEEP,
241         },
242
243         /* description */
244         {
245                 .local_name = "description",
246                 .type = MAP_KEEP,
247         },
248
249         /* sambaSID -> objectSid*/
250         {
251                 .local_name = "objectSid",
252                 .type = MAP_RENAME,
253                 .u.rename.remote_name = "sambaSID", 
254         },
255
256         /* sambaPwdLastSet -> pwdLastSet*/
257         {
258                 .local_name = "pwdLastSet",
259                 .type = MAP_RENAME,
260                 .u.rename.remote_name = "sambaPwdLastSet",
261         },      
262 };
263
264         /* the init function */
265 #ifdef HAVE_DLOPEN_DISABLED
266 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
267 #else
268 struct ldb_module *ldb_samba3sam_module_init(struct ldb_context *ldb, const char *options[])
269 #endif
270 {
271         return ldb_map_init(ldb, samba3_attributes, samba3_objectclasses, options);
272 }