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