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