2 Unix SMB/CIFS implementation.
3 Run some local tests for group mapping
4 Copyright (C) Volker Lendecke 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define CHECK_STATUS(_status, _expected) do { \
24 if (!NT_STATUS_EQUAL(_status, _expected)) { \
25 printf("(%d) Incorrect status %s - should be %s\n", \
26 __LINE__, nt_errstr(status), nt_errstr(_expected)); \
30 static NTSTATUS create_v2_mapping(struct tdb_context *tdb,
31 const char *sid, gid_t gid,
32 enum SID_NAME_USE type,
41 if (asprintf(&key.dptr, "UNIXGROUP/%s", sid) < 0) {
42 d_fprintf(stderr, "(%s) asprintf failed\n",
44 return NT_STATUS_NO_MEMORY;
46 key.dsize = strlen(key.dptr)+1;
48 if (!tdb_pack_append(NULL, &data.dptr, &data.dsize, "ddff",
49 (uint32)gid, (uint32)type, nt_name, comment)) {
50 d_fprintf(stderr, "(%s) tdb_pack_append failed\n",
53 return NT_STATUS_NO_MEMORY;
56 if (tdb_store(tdb, key, data, TDB_INSERT) < 0) {
57 status = map_ntstatus_from_tdb(tdb);
58 d_fprintf(stderr, "(%s) tdb_store failed: %s\n", __location__,
61 TALLOC_FREE(data.dptr);
66 TALLOC_FREE(data.dptr);
70 #define NUM_ENTRIES (50)
72 static NTSTATUS create_v2_db(BOOL invalid)
74 struct tdb_context *tdb;
78 tdb = tdb_open_log(lock_path("group_mapping.tdb"), 0, TDB_DEFAULT,
79 O_RDWR|O_CREAT, 0600);
81 d_fprintf(stderr, "(%s) tdb_open_log failed: %s\n",
82 __location__, strerror(errno));
83 status = map_nt_error_from_unix(errno);
87 /* Empty the database */
88 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
90 if (tdb_store_int32(tdb, "INFO/version", 2) < 0) {
91 status = map_ntstatus_from_tdb(tdb);
92 d_fprintf(stderr, "(%s) tdb_store_uint32 failed: %s\n",
93 __location__, nt_errstr(status));
97 for (i=1000; i<1000+NUM_ENTRIES; i++) {
99 if (asprintf(&sid, "S-1-5-21-744032650-3806004166-77016029-%d",
101 d_fprintf(stderr, "(%s) asprintf failed\n",
105 if (asprintf(&name, "Unix group %d", i) < 0) {
106 d_fprintf(stderr, "(%s) asprintf failed\n",
111 status = create_v2_mapping(tdb, sid, (gid_t)i,
112 SID_NAME_DOM_GRP, name, name);
115 CHECK_STATUS(status, NT_STATUS_OK);
117 status = create_v2_mapping(tdb, "S-1-5-32-544", 10000,
118 SID_NAME_ALIAS, "Administrators",
120 CHECK_STATUS(status, NT_STATUS_OK);
121 status = create_v2_mapping(tdb, "S-1-5-32-545", 10001,
122 SID_NAME_ALIAS, "Users", "Machine Users");
123 CHECK_STATUS(status, NT_STATUS_OK);
126 /* Map 10001 to two different SIDs */
127 status = create_v2_mapping(tdb, "S-1-5-32-999", 10001,
128 SID_NAME_ALIAS, "Overlapping",
130 CHECK_STATUS(status, NT_STATUS_OK);
133 status = create_v2_mapping(tdb, "S-1-5-32-546", -1,
134 SID_NAME_ALIAS, "notthere", "To remove");
135 CHECK_STATUS(status, NT_STATUS_OK);
137 status = NT_STATUS_OK;
145 static BOOL groupmap_diff(const GROUP_MAP *m1, const GROUP_MAP *m2)
147 return ((sid_compare(&m1->sid, &m2->sid) != 0) ||
148 (m1->gid != m2->gid) ||
149 (m1->sid_name_use != m2->sid_name_use) ||
150 (strcmp(m1->nt_name, m2->nt_name) != 0) ||
151 (strcmp(m1->comment, m2->comment) != 0));
156 BOOL run_local_groupmap(int dummy)
161 GROUP_MAP *maps = NULL;
164 mem_ctx = talloc_init("run_local_groupmap");
165 if (mem_ctx == NULL) {
166 d_fprintf(stderr, "(%s) talloc_init failed\n",
172 status = create_v2_db(True);
173 if (!NT_STATUS_IS_OK(status)) {
179 if (pdb_getgrgid(&map, 10001)) {
180 d_fprintf(stderr, "(%s) upgrading an invalid group db "
181 "worked\n", __location__);
187 status = create_v2_db(False);
188 if (!NT_STATUS_IS_OK(status)) {
192 /* This tests upgrading the database, as well as listing */
194 if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &maps, &num_maps,
196 d_fprintf(stderr, "(%s) pdb_enum_group_mapping failed\n",
201 if (num_maps != NUM_ENTRIES+2) {
202 d_fprintf(stderr, "(%s) expected %d entries, got %d\n",
203 __location__, NUM_ENTRIES+2, num_maps);
207 /* See if getgrsid, getgrgid and getgrnam find the same entry */
212 string_to_sid(&sid, "S-1-5-32-545");
215 status = pdb_getgrsid(&map, &sid);
216 if (!NT_STATUS_IS_OK(status)) {
217 d_fprintf(stderr, "(%s) pdb_getgrsid failed: %s\n",
218 __location__, nt_errstr(status));
223 status = pdb_getgrgid(&map1, map.gid);
224 if (!NT_STATUS_IS_OK(status)) {
225 d_fprintf(stderr, "(%s) pdb_getgrgid failed: %s\n",
226 __location__, nt_errstr(status));
230 if (groupmap_diff(&map1, &map)) {
231 d_fprintf(stderr, "(%s) getgrsid/getgrgid disagree\n",
237 status = pdb_getgrnam(&map1, map.nt_name);
238 if (!NT_STATUS_IS_OK(status)) {
239 d_fprintf(stderr, "(%s) pdb_getgrnam failed: %s\n",
240 __location__, nt_errstr(status));
244 if (groupmap_diff(&map1, &map)) {
245 d_fprintf(stderr, "(%s) getgrsid/getgrnam disagree\n",
251 /* See if pdb_delete_group_mapping_entry works */
256 string_to_sid(&sid, "S-1-5-32-545");
258 status = pdb_getgrsid(&map, &sid);
259 if (!NT_STATUS_IS_OK(status)) {
260 d_fprintf(stderr, "(%s) did not find S-1-5-32-545: "
261 "%s\n", __location__, nt_errstr(status));
265 status = pdb_delete_group_mapping_entry(sid);
266 CHECK_STATUS(status, NT_STATUS_OK);
267 status = pdb_delete_group_mapping_entry(sid);
269 CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
271 CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
274 if (NT_STATUS_IS_OK(pdb_getgrsid(&map1, &sid))) {
275 d_fprintf(stderr, "(%s) getgrsid found deleted "
276 "entry\n", __location__);
280 if (NT_STATUS_IS_OK(pdb_getgrgid(&map1, map.gid))) {
281 d_fprintf(stderr, "(%s) getgrgid found deleted "
282 "entry\n", __location__);
286 if (NT_STATUS_IS_OK(pdb_getgrnam(&map1, map.nt_name))) {
287 d_fprintf(stderr, "(%s) getgrnam found deleted "
288 "entry\n", __location__);
294 /* See if pdb_update_group_mapping_entry works */
300 string_to_sid(&sid, "S-1-5-32-544");
302 status = pdb_getgrsid(&map, &sid);
303 if (!NT_STATUS_IS_OK(status)) {
304 d_fprintf(stderr, "(%s) did not find S-1-5-32-544: "
305 "%s\n", __location__, nt_errstr(status));
312 status = pdb_update_group_mapping_entry(&map);
313 CHECK_STATUS(status, NT_STATUS_OK);
315 if (NT_STATUS_IS_OK(pdb_getgrgid(&map1, oldgid))) {
316 d_fprintf(stderr, "(%s) getgrgid found outdated "
317 "entry\n", __location__);
321 /* Change to an existing entry, see "create_db_v2" */
324 status = pdb_update_group_mapping_entry(&map);
326 CHECK_STATUS(status, NT_STATUS_OBJECTID_EXISTS);
327 if (!pdb_getgrgid(&map1, 4711)) {
328 d_fprintf(stderr, "(%s) update_group changed entry "
329 "upon failure\n", __location__);
333 CHECK_STATUS(status, NT_STATUS_OK);
342 TALLOC_FREE(mem_ctx);