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