e24fc214a0619cca304294b834c2b025ba016668
[obnox/samba/samba-obnox.git] / source3 / torture / test_idmap_tdb_common.c
1 /*
2    Unix SMB/CIFS implementation.
3    IDMAP TDB common code tester
4
5    Copyright (C) Christian Ambach 2012
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 "system/filesys.h"
23 #include "torture/proto.h"
24 #include "idmap.h"
25 #include "winbindd/idmap_rw.h"
26 #include "winbindd/idmap_tdb_common.h"
27 #include "winbindd/winbindd.h"
28 #include "winbindd/winbindd_proto.h"
29 #include "dbwrap/dbwrap.h"
30 #include "dbwrap/dbwrap_open.h"
31 #include "../libcli/security/dom_sid.h"
32
33 #define HWM_GROUP  "GROUP HWM"
34 #define HWM_USER   "USER HWM"
35
36 #define LOW_ID 100
37 #define HIGH_ID 199
38
39 #define TESTDB "/tmp/idmap_test.tdb"
40
41 #define DOM_SID1 "S-1-5-21-1234-5678-9012"
42 #define DOM_SID2 "S-1-5-21-0123-5678-9012"
43 #define DOM_SID3 "S-1-5-21-0012-5678-9012"
44 #define DOM_SID4 "S-1-5-21-0001-5678-9012"
45 #define DOM_SID5 "S-1-5-21-2345-5678-9012"
46 #define DOM_SID6 "S-1-5-21-3456-5678-9012"
47
48 /* overwrite some winbind internal functions */
49 struct winbindd_domain *find_domain_from_name(const char *domain_name)
50 {
51         return NULL;
52 }
53
54 bool get_global_winbindd_state_offline(void) {
55         return false;
56 }
57
58 bool winbindd_use_idmap_cache(void) {
59         return false;
60 }
61
62 bool idmap_is_online(void)
63 {
64         return true;
65 }
66
67 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
68 {
69         return NT_STATUS_OK;
70 }
71
72 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
73 {
74         return NT_STATUS_OK;
75 }
76
77 static bool open_db(struct idmap_tdb_common_context *ctx)
78 {
79         NTSTATUS status;
80
81         if(ctx->db) {
82                 /* already open */
83                 return true;
84         }
85
86         unlink(TESTDB);
87
88         ctx->db = db_open(ctx, TESTDB, 0, TDB_DEFAULT,
89                           O_RDWR | O_CREAT | O_EXCL, 0600,
90                           DBWRAP_LOCK_ORDER_1);
91
92         if(dbwrap_transaction_start(ctx->db) != 0) {
93                 DEBUG(0, ("Failed to start transaction!\n"));
94                 return false;
95         }
96
97         status = dbwrap_store_uint32(ctx->db, ctx->hwmkey_uid, LOW_ID);
98         if(!NT_STATUS_IS_OK(status)) {
99                 dbwrap_transaction_cancel(ctx->db);
100                 return false;
101         }
102
103         status = dbwrap_store_uint32(ctx->db, ctx->hwmkey_gid, LOW_ID);
104         if(!NT_STATUS_IS_OK(status)) {
105                 dbwrap_transaction_cancel(ctx->db);
106                 return false;
107         }
108
109         if(dbwrap_transaction_commit(ctx->db) != 0) {
110                 DEBUG(0, ("Failed to commit transaction!\n"));
111                 return false;
112         }
113
114         return true;
115 }
116
117 static struct idmap_tdb_common_context *createcontext(TALLOC_CTX *memctx)
118 {
119         struct idmap_tdb_common_context *ret;
120
121         ret = talloc_zero(memctx, struct idmap_tdb_common_context);
122         ret->rw_ops = talloc_zero(ret, struct idmap_rw_ops);
123
124         ret->max_id = HIGH_ID;
125         ret->hwmkey_uid = HWM_USER;
126         ret->hwmkey_gid = HWM_GROUP;
127
128         ret->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
129         ret->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
130
131         open_db(ret);
132
133         return ret;
134 }
135
136 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
137 {
138         struct idmap_domain *dom;
139
140         dom = talloc_zero(memctx, struct idmap_domain);
141         dom->name = "*";
142         dom->low_id = LOW_ID;
143         dom->high_id = HIGH_ID;
144         dom->read_only = false;
145         dom->methods = talloc_zero(dom, struct idmap_methods);
146         dom->methods->sids_to_unixids = idmap_tdb_common_sids_to_unixids;
147         dom->methods->unixids_to_sids = idmap_tdb_common_unixids_to_sids;
148         dom->methods->allocate_id = idmap_tdb_common_get_new_id;
149
150         return dom;
151 }
152
153 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
154 {
155         NTSTATUS status;
156         struct unixid id;
157
158         id.type = ID_TYPE_UID;
159
160         status = idmap_tdb_common_get_new_id(dom, &id);
161
162         if(!NT_STATUS_IS_OK(status)) {
163                 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
164                 return false;
165         }
166
167         if(id.id == 0) {
168                 DEBUG(0, ("test_getnewid1: Allocate returned "
169                           "empty id!\n"));
170                 return false;
171         }
172
173         if(id.id > HIGH_ID || id.id < LOW_ID) {
174                 DEBUG(0, ("test_getnewid1: Allocate returned "
175                           "out of range id!\n"));
176                 return false;
177         }
178
179         DEBUG(0, ("test_getnewid1: PASSED!\n"));
180
181         return true;
182 }
183
184 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
185 {
186         NTSTATUS status;
187         struct unixid id;
188         int i, left;
189
190         id.type = ID_TYPE_UID;
191
192         status = idmap_tdb_common_get_new_id(dom, &id);
193
194         if(!NT_STATUS_IS_OK(status)) {
195                 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
196                 return false;
197         }
198
199         if(id.id == 0) {
200                 DEBUG(0, ("test_getnewid2: Allocate returned "
201                           "empty id!\n"));
202                 return false;
203         }
204
205         if(id.id > HIGH_ID || id.id < LOW_ID) {
206                 DEBUG(0, ("test_getnewid2: Allocate returned "
207                           "out of range id!\n"));
208                 return false;
209         }
210
211         /* how many ids are left? */
212
213         left = HIGH_ID - id.id;
214
215         /* consume them all */
216         for(i = 0; i<left; i++) {
217
218                 status = idmap_tdb_common_get_new_id(dom, &id);
219
220                 if(!NT_STATUS_IS_OK(status)) {
221                         DEBUG(0, ("test_getnewid2: Allocate returned "
222                                   "error %s\n", nt_errstr(status)));
223                         return false;
224                 }
225
226                 if(id.id > HIGH_ID) {
227                         DEBUG(0, ("test_getnewid2: Allocate returned "
228                                   "out of range id (%d)!\n", id.id));
229                         return false;
230                 }
231         }
232
233         /* one more must fail */
234         status = idmap_tdb_common_get_new_id(dom, &id);
235
236         if(NT_STATUS_IS_OK(status)) {
237                 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
238                           "depleted pool!\n", id.id));
239                 return false;
240         }
241
242         DEBUG(0, ("test_getnewid2: PASSED!\n"));
243
244         return true;
245 }
246
247 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
248 {
249         NTSTATUS status;
250         struct id_map map;
251
252         ZERO_STRUCT(map);
253
254         /* test for correct return code with invalid data */
255
256         status = idmap_tdb_common_set_mapping(dom, NULL);
257         if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
258                 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
259                 return false;
260         }
261
262         status = idmap_tdb_common_set_mapping(dom, &map);
263         if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
264                 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
265                 return false;
266         }
267
268         map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
269
270         map.xid.type = ID_TYPE_NOT_SPECIFIED;
271         map.xid.id = 4711;
272
273         status = idmap_tdb_common_set_mapping(dom, &map);
274         if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
275                 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
276                 return false;
277         }
278
279         /* now the good ones */
280         map.xid.type = ID_TYPE_UID;
281         map.xid.id = 0;
282
283         status = idmap_tdb_common_get_new_id(dom, &(map.xid));
284         if(!NT_STATUS_IS_OK(status)) {
285                 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
286                 return false;
287         }
288
289         status = idmap_tdb_common_set_mapping(dom, &map);
290         if(!NT_STATUS_IS_OK(status)) {
291                 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
292                 return false;
293         }
294
295         /* try to set the same mapping again as group (must fail) */
296
297         map.xid.type = ID_TYPE_GID;
298         status = idmap_tdb_common_set_mapping(dom, &map);
299         if(NT_STATUS_IS_OK(status)) {
300                 DEBUG(0, ("test_setmap1: could create map for "
301                           "group and user!\n"));
302                 return false;
303         }
304
305         /* now a group with a different SID*/
306         map.xid.id = 0;
307
308         map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-101");
309
310         status = idmap_tdb_common_get_new_id(dom, &(map.xid));
311         if(!NT_STATUS_IS_OK(status)) {
312                 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
313                 return false;
314         }
315
316         status = idmap_tdb_common_set_mapping(dom, &map);
317         if(!NT_STATUS_IS_OK(status)) {
318                 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
319                 return false;
320         }
321         DEBUG(0, ("test_setmap1: PASSED!\n"));
322
323         return true;
324 }
325
326 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
327 {
328         NTSTATUS status1, status2, status3;
329         struct id_map map;
330
331         /* check for correct dealing with bad parameters */
332         status1 = idmap_tdb_common_sid_to_unixid(NULL, &map);
333         status2 = idmap_tdb_common_sid_to_unixid(dom, NULL);
334         status3 = idmap_tdb_common_sid_to_unixid(NULL, NULL);
335
336         if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
337             !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
338             !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
339                 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
340                 return false;
341         }
342
343         DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
344
345         return true;
346 }
347
348 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
349 {
350         NTSTATUS status;
351         struct id_map uid_map, gid_map, test_map;
352         bool doagain = true;
353
354         ZERO_STRUCT(uid_map);
355         ZERO_STRUCT(gid_map);
356
357         /* create two mappings for a UID and GID */
358
359 again:
360
361         uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1000");
362         uid_map.xid.type = ID_TYPE_UID;
363
364         gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1001");
365         gid_map.xid.type = ID_TYPE_GID;
366
367         status = idmap_tdb_common_new_mapping(dom, &uid_map);
368         if(!NT_STATUS_IS_OK(status)) {
369                 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
370                 return false;
371         }
372
373         status = idmap_tdb_common_new_mapping(dom, &gid_map);
374         if(!NT_STATUS_IS_OK(status)) {
375                 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
376                 return false;
377         }
378
379         /* now read them back */
380         ZERO_STRUCT(test_map);
381         test_map.sid = uid_map.sid;
382
383         status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
384         if(!NT_STATUS_IS_OK(status)) {
385                 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
386                 return false;
387         }
388
389         if(test_map.xid.id!=uid_map.xid.id) {
390                 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
391                 return false;
392         }
393
394         test_map.sid = gid_map.sid;
395
396         status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
397         if(!NT_STATUS_IS_OK(status)) {
398                 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
399                 return false;
400         }
401
402         if(test_map.xid.id!=gid_map.xid.id) {
403                 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
404                 return false;
405         }
406
407         /*
408          * Go through the same tests again once to see if trying to recreate
409          * a mapping that was already created will work or not
410          */
411         if(doagain) {
412                 doagain = false;
413                 goto again;
414         }
415
416         DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
417
418         return true;
419 }
420
421 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
422 {
423         NTSTATUS status;
424         struct id_map uid_map, gid_map, **test_maps;
425
426         ZERO_STRUCT(uid_map);
427         ZERO_STRUCT(gid_map);
428
429         /* create two mappings for a UID and GID */
430
431         uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1000");
432         uid_map.xid.type = ID_TYPE_UID;
433
434         gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1001");
435         gid_map.xid.type = ID_TYPE_GID;
436
437         status = idmap_tdb_common_new_mapping(dom, &uid_map);
438         if(!NT_STATUS_IS_OK(status)) {
439                 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
440                 return false;
441         }
442
443         status = idmap_tdb_common_new_mapping(dom, &gid_map);
444         if(!NT_STATUS_IS_OK(status)) {
445                 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
446                 return false;
447         }
448
449         /* now read them back  */
450         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
451
452         test_maps[0] = talloc(test_maps, struct id_map);
453         test_maps[1] = talloc(test_maps, struct id_map);
454         test_maps[2] = NULL;
455
456         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
457         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
458         sid_copy(test_maps[0]->sid, uid_map.sid);
459         sid_copy(test_maps[1]->sid, gid_map.sid);
460
461         status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
462         if(!NT_STATUS_IS_OK(status)) {
463                 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
464                 talloc_free(test_maps);
465                 return false;
466         }
467
468         if(test_maps[0]->xid.id!=uid_map.xid.id ||
469             test_maps[1]->xid.id!=gid_map.xid.id ) {
470                 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
471                 talloc_free(test_maps);
472                 return false;
473         }
474
475         DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
476
477         talloc_free(test_maps);
478
479         return true;
480 }
481
482 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
483 {
484         NTSTATUS status;
485         struct id_map **test_maps;
486         struct unixid save;
487
488         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
489
490         test_maps[0] = talloc(test_maps, struct id_map);
491         test_maps[1] = talloc(test_maps, struct id_map);
492         test_maps[2] = NULL;
493
494         /* ask for two new mappings for a UID and GID */
495         test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1003");
496         test_maps[0]->xid.type = ID_TYPE_UID;
497         test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
498         test_maps[1]->xid.type = ID_TYPE_GID;
499
500         status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
501         if(!NT_STATUS_IS_OK(status)) {
502                 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
503                           "failed (%s)!\n", nt_errstr(status)));
504                 talloc_free(test_maps);
505                 return false;
506         }
507
508         if(test_maps[0]->xid.id == 0 || test_maps[1]->xid.id == 0) {
509                 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
510                           "returned zero ids!\n"));
511                 talloc_free(test_maps);
512                 return false;
513         }
514
515         save = test_maps[1]->xid;
516
517         /* ask for a known and a new mapping at the same time */
518         talloc_free(test_maps);
519         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
520         test_maps[0] = talloc(test_maps, struct id_map);
521         test_maps[1] = talloc(test_maps, struct id_map);
522         test_maps[2] = NULL;
523
524         test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
525         test_maps[0]->xid.type = ID_TYPE_GID;
526         test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1005");
527         test_maps[1]->xid.type = ID_TYPE_UID;
528
529         status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
530         if(!NT_STATUS_IS_OK(status)) {
531                 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
532                           "failed (%s)!\n", nt_errstr(status)));
533                 talloc_free(test_maps);
534                 return false;
535         }
536
537         if(test_maps[0]->xid.type != save.type ||
538             test_maps[0]->xid.id != save.id) {
539                 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
540                           "different value!\n"));
541                 talloc_free(test_maps);
542                 return false;
543         }
544
545         if(test_maps[1]->xid.id == 0) {
546                 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
547                           "returned zero id for mixed mapping request!\n"));
548                 talloc_free(test_maps);
549                 return false;
550         }
551
552         DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
553
554         talloc_free(test_maps);
555
556         return true;
557 }
558
559 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
560 {
561         NTSTATUS status;
562         struct id_map **test_maps;
563         bool retval = true;
564
565         /*
566          * check the mapping states:
567          * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
568          *
569          * use the ids created by test_sids2unixids1
570          * need to make dom read-only
571          */
572
573         dom->read_only = true;
574
575         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
576
577         test_maps[0] = talloc(test_maps, struct id_map);
578         test_maps[1] = talloc(test_maps, struct id_map);
579         test_maps[2] = NULL;
580
581         /* NONE_MAPPED first */
582         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
583         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
584         test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
585                                                  "S-1-5-21-1-2-3-4");
586         test_maps[0]->xid.type = ID_TYPE_UID;
587
588         test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
589                                                  "S-1-5-21-1-2-3-5");
590         test_maps[1]->xid.type = ID_TYPE_GID;
591
592         status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
593         if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
594                 DEBUG(0, ("test_sids2unixids3: incorrect status "
595                           "(%s), expected NT_STATUS_NONE_MAPPED!\n",
596                            nt_errstr(status)));
597                 retval = false;
598                 goto out;
599         }
600
601         /* SOME_UNMAPPED */
602         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
603         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
604         test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
605                                                  DOM_SID4 "-1000");
606         test_maps[0]->xid.type = ID_TYPE_UID;
607         test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
608                                                  "S-1-5-21-1-2-3-5");
609         test_maps[1]->xid.type = ID_TYPE_GID;
610
611         status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
612         if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
613                 DEBUG(0, ("test_sids2unixids3: incorrect status "
614                           "(%s), expected STATUS_SOME_UNMAPPED!\n",
615                            nt_errstr(status)));
616                 retval = false;
617                 goto out;
618         }
619
620         /* OK */
621         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
622         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
623         test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
624                                                  DOM_SID4 "-1001");
625         test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
626                                                  DOM_SID4 "-1000");
627
628         status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
629         if(!NT_STATUS_IS_OK(status)) {
630                 DEBUG(0, ("test_sids2unixids3: incorrect status "
631                           "(%s), expected NT_STATUS_OK!\n",
632                            nt_errstr(status)));
633                 retval = false;
634                 goto out;
635         }
636
637         DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
638
639 out:
640         talloc_free(test_maps);
641         dom->read_only = false;
642         return retval;
643 }
644
645 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
646 {
647         NTSTATUS status1, status2, status3;
648         struct id_map map;
649
650         /* check for correct dealing with bad parameters */
651         status1 = idmap_tdb_common_unixid_to_sid(NULL, &map);
652         status2 = idmap_tdb_common_unixid_to_sid(dom, NULL);
653         status3 = idmap_tdb_common_unixid_to_sid(NULL, NULL);
654
655         if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
656             !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
657             !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
658                 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
659                 return false;
660         }
661
662         DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
663
664         return true;
665 }
666
667 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
668 {
669         NTSTATUS status;
670         struct id_map *map;
671         bool retval = true;
672
673         /* ask for mapping that is outside of the range */
674         map = talloc(memctx, struct id_map);
675         map->sid = talloc(map, struct dom_sid);
676
677         map->xid.type = ID_TYPE_UID;
678         map->xid.id = HIGH_ID + 1;
679
680         status = idmap_tdb_common_unixid_to_sid(dom, map);
681         if(NT_STATUS_IS_OK(status)) {
682                 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
683                           "out-of-range result\n"));
684                 retval = false;
685                 goto out;
686         }
687
688         DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
689 out:
690         talloc_free(map);
691         return retval;
692
693 }
694
695 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
696 {
697         NTSTATUS status;
698         struct id_map uid_map, gid_map, test_map;
699         struct dom_sid testsid;
700
701         ZERO_STRUCT(uid_map);
702         ZERO_STRUCT(gid_map);
703
704         /* create two mappings for a UID and GID */
705         uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1000");
706         uid_map.xid.type = ID_TYPE_UID;
707
708         gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1001");
709         gid_map.xid.type = ID_TYPE_GID;
710
711         status = idmap_tdb_common_new_mapping(dom, &uid_map);
712         if(!NT_STATUS_IS_OK(status)) {
713                 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
714                 return false;
715         }
716
717         status = idmap_tdb_common_new_mapping(dom, &gid_map);
718         if(!NT_STATUS_IS_OK(status)) {
719                 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
720                 return false;
721         }
722
723         /* now read them back */
724         ZERO_STRUCT(test_map);
725         test_map.xid.id = uid_map.xid.id;
726         test_map.xid.type = ID_TYPE_UID;
727         test_map.sid = &testsid;
728
729         status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
730         if(!NT_STATUS_IS_OK(status)) {
731                 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
732                 return false;
733         }
734
735         if(test_map.xid.type!=uid_map.xid.type) {
736                 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
737                 return false;
738         }
739
740         if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
741                 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
742                 return false;
743         }
744
745         ZERO_STRUCT(test_map);
746         test_map.xid.id = gid_map.xid.id;
747         test_map.xid.type = ID_TYPE_GID;
748         test_map.sid = &testsid;
749
750         status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
751         if(!NT_STATUS_IS_OK(status)) {
752                 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
753                 return false;
754         }
755
756         if(test_map.xid.type!=gid_map.xid.type) {
757                 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
758                 return false;
759         }
760
761         if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
762                 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
763                 return false;
764         }
765
766         DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
767
768         return true;
769 }
770
771 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
772 {
773         NTSTATUS status;
774         struct id_map uid_map, gid_map, **test_maps;
775
776         ZERO_STRUCT(uid_map);
777         ZERO_STRUCT(gid_map);
778
779         /* create two mappings for a UID and GID */
780
781         uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1000");
782         uid_map.xid.type = ID_TYPE_UID;
783
784         gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1001");
785         gid_map.xid.type = ID_TYPE_GID;
786
787         status = idmap_tdb_common_new_mapping(dom, &uid_map);
788         if(!NT_STATUS_IS_OK(status)) {
789                 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
790                 return false;
791         }
792
793         status = idmap_tdb_common_new_mapping(dom, &gid_map);
794         if(!NT_STATUS_IS_OK(status)) {
795                 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
796                 return false;
797         }
798
799         /* now read them back  */
800         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
801
802         test_maps[0] = talloc(test_maps, struct id_map);
803         test_maps[1] = talloc(test_maps, struct id_map);
804         test_maps[2] = NULL;
805
806         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
807         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
808         test_maps[0]->xid.id = uid_map.xid.id;
809         test_maps[0]->xid.type = ID_TYPE_UID;
810         test_maps[1]->xid.id = gid_map.xid.id;
811         test_maps[1]->xid.type = ID_TYPE_GID;
812
813         status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
814         if(!NT_STATUS_IS_OK(status)) {
815                 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
816                 talloc_free(test_maps);
817                 return false;
818         }
819
820         if(!dom_sid_equal(test_maps[0]->sid, uid_map.sid) ||
821             !dom_sid_equal(test_maps[1]->sid, gid_map.sid) ) {
822                 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
823                 talloc_free(test_maps);
824                 return false;
825         }
826
827         DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
828
829         talloc_free(test_maps);
830
831         return true;
832 }
833
834 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
835 {
836         NTSTATUS status;
837         struct id_map **test_maps;
838         bool retval = true;
839
840         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
841
842         test_maps[0] = talloc(test_maps, struct id_map);
843         test_maps[1] = talloc(test_maps, struct id_map);
844         test_maps[2] = NULL;
845
846         /* ask for two unknown mappings for a UID and GID */
847         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
848         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
849         test_maps[0]->xid.id = HIGH_ID - 1;
850         test_maps[0]->xid.type = ID_TYPE_UID;
851         test_maps[1]->xid.id = HIGH_ID - 1;
852         test_maps[1]->xid.type = ID_TYPE_GID;
853
854         status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
855         if(NT_STATUS_IS_OK(status)) {
856                 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
857                           "unexpectedly!\n"));
858                 retval = false;
859                 goto out;
860         }
861
862         DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
863
864 out:
865         talloc_free(test_maps);
866
867         return retval;;
868 }
869
870 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
871 {
872         NTSTATUS status;
873         struct id_map uid_map, gid_map, **test_maps;
874         bool retval = true;
875
876         ZERO_STRUCT(uid_map);
877         ZERO_STRUCT(gid_map);
878
879         /* create two mappings for a UID and GID */
880         uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1000");
881         uid_map.xid.type = ID_TYPE_UID;
882
883         gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1001");
884         gid_map.xid.type = ID_TYPE_GID;
885
886         status = idmap_tdb_common_new_mapping(dom, &uid_map);
887         if(!NT_STATUS_IS_OK(status)) {
888                 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
889                 return false;
890         }
891
892         status = idmap_tdb_common_new_mapping(dom, &gid_map);
893         if(!NT_STATUS_IS_OK(status)) {
894                 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
895                 return false;
896         }
897
898         /*
899          * check the mapping states:
900          * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
901          */
902         test_maps = talloc_zero_array(memctx, struct id_map*, 3);
903
904         test_maps[0] = talloc(test_maps, struct id_map);
905         test_maps[1] = talloc(test_maps, struct id_map);
906         test_maps[2] = NULL;
907
908         /* NONE_MAPPED first */
909         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
910         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
911
912         test_maps[0]->xid.id = HIGH_ID - 1;
913         test_maps[0]->xid.type = ID_TYPE_UID;
914
915         test_maps[1]->xid.id = HIGH_ID - 1;
916         test_maps[1]->xid.type = ID_TYPE_GID;
917
918         status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
919         if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
920                 DEBUG(0, ("test_unixids2sids3: incorrect status "
921                           "(%s), expected NT_STATUS_NONE_MAPPED!\n",
922                            nt_errstr(status)));
923                 retval = false;
924                 goto out;
925         }
926
927         /* SOME_UNMAPPED */
928         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
929         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
930         test_maps[0]->xid = uid_map.xid;
931         test_maps[1]->xid.id = HIGH_ID - 1;
932         test_maps[1]->xid.type = ID_TYPE_GID;
933
934         status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
935         if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
936                 DEBUG(0, ("test_unixids2sids3: incorrect status "
937                           "(%s), expected STATUS_SOME_UNMAPPED!\n",
938                            nt_errstr(status)));
939                 retval = false;
940                 goto out;
941         }
942
943         /* OK */
944         test_maps[0]->sid = talloc(test_maps, struct dom_sid);
945         test_maps[1]->sid = talloc(test_maps, struct dom_sid);
946         test_maps[0]->xid = uid_map.xid;
947         test_maps[1]->xid = gid_map.xid;
948
949         status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
950         if(!NT_STATUS_IS_OK(status)) {
951                 DEBUG(0, ("test_unixids2sids3: incorrect status "
952                           "(%s), expected NT_STATUS_OK!\n",
953                            nt_errstr(status)));
954                 retval = false;
955                 goto out;
956         }
957
958         DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
959
960 out:
961         talloc_free(test_maps);
962         return retval;
963 }
964
965 #define CHECKRESULT(r) if(!r) {return r;}
966
967 bool run_idmap_tdb_common_test(int dummy)
968 {
969         bool result;
970         struct idmap_tdb_common_context *ctx;
971         struct idmap_domain *dom;
972
973         TALLOC_CTX *memctx = talloc_new(NULL);
974         TALLOC_CTX *stack = talloc_stackframe();
975
976         ctx = createcontext(memctx);
977
978         dom = createdomain(memctx);
979
980         dom->private_data = ctx;
981
982         /* test a single allocation from pool (no mapping) */
983         result = test_getnewid1(memctx, dom);
984         CHECKRESULT(result);
985
986         /* test idmap_tdb_common_set_mapping */
987         result = test_setmap1(memctx, dom);
988         CHECKRESULT(result);
989
990         /* test idmap_tdb_common_sid_to_unixid */
991         result = test_sid2unixid1(memctx, dom);
992         CHECKRESULT(result);
993         result = test_sid2unixid2(memctx, dom);
994         CHECKRESULT(result);
995
996         /* test idmap_tdb_common_sids_to_unixids */
997         result = test_sids2unixids1(memctx, dom);
998         CHECKRESULT(result);
999         result = test_sids2unixids2(memctx, dom);
1000         CHECKRESULT(result);
1001         result = test_sids2unixids3(memctx, dom);
1002         CHECKRESULT(result);
1003
1004         /* test idmap_tdb_common_unixid_to_sid */
1005         result = test_unixid2sid1(memctx, dom);
1006         CHECKRESULT(result);
1007         result = test_unixid2sid2(memctx, dom);
1008         CHECKRESULT(result);
1009         result = test_unixid2sid3(memctx, dom);
1010         CHECKRESULT(result);
1011
1012         /* test idmap_tdb_common_unixids_to_sids */
1013         result = test_unixids2sids1(memctx, dom);
1014         CHECKRESULT(result);
1015         result = test_unixids2sids2(memctx, dom);
1016         CHECKRESULT(result);
1017         result = test_unixids2sids3(memctx, dom);
1018         CHECKRESULT(result);
1019
1020         /* test filling up the range */
1021         result = test_getnewid2(memctx, dom);
1022         CHECKRESULT(result);
1023
1024         talloc_free(memctx);
1025         talloc_free(stack);
1026
1027         return true;
1028 }