2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Anthony Liguori 2003
8 Copyright (C) Simo Sorce 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define DBGC_CLASS DBGC_IDMAP
30 /* High water mark keys */
31 #define HWM_GROUP "GROUP HWM"
32 #define HWM_USER "USER HWM"
34 /* idmap version determines auto-conversion */
35 #define IDMAP_VERSION 2
38 static TDB_CONTEXT *idmap_tdb;
40 static struct idmap_state {
42 /* User and group id pool */
44 uid_t uid_low, uid_high; /* Range of uids to allocate */
45 gid_t gid_low, gid_high; /* Range of gids to allocate */
48 /* Allocate either a user or group id from the pool */
49 static NTSTATUS db_allocate_id(unid_t *id, int id_type)
55 return NT_STATUS_INVALID_PARAMETER;
57 /* Get current high water mark */
58 switch (id_type & ID_TYPEMASK) {
61 if ((hwm = tdb_fetch_int32(idmap_tdb, HWM_USER)) == -1) {
62 return NT_STATUS_INTERNAL_DB_ERROR;
65 /* check it is in the range */
66 if (hwm > idmap_state.uid_high) {
67 DEBUG(0, ("idmap Fatal Error: UID range full!! (max: %u)\n", idmap_state.uid_high));
68 return NT_STATUS_UNSUCCESSFUL;
71 /* fetch a new id and increment it */
72 ret = tdb_change_uint32_atomic(idmap_tdb, HWM_USER, &hwm, 1);
74 DEBUG(0, ("idmap_tdb: Fatal error while fetching a new id\n!"));
75 return NT_STATUS_UNSUCCESSFUL;
78 /* recheck it is in the range */
79 if (hwm > idmap_state.uid_high) {
80 DEBUG(0, ("idmap Fatal Error: UID range full!! (max: %u)\n", idmap_state.uid_high));
81 return NT_STATUS_UNSUCCESSFUL;
85 DEBUG(10,("db_allocate_id: ID_USERID (*id).uid = %d\n", (unsigned int)hwm));
89 if ((hwm = tdb_fetch_int32(idmap_tdb, HWM_GROUP)) == -1) {
90 return NT_STATUS_INTERNAL_DB_ERROR;
93 /* check it is in the range */
94 if (hwm > idmap_state.gid_high) {
95 DEBUG(0, ("idmap Fatal Error: GID range full!! (max: %u)\n", idmap_state.gid_high));
96 return NT_STATUS_UNSUCCESSFUL;
99 /* fetch a new id and increment it */
100 ret = tdb_change_uint32_atomic(idmap_tdb, HWM_GROUP, &hwm, 1);
103 DEBUG(0, ("idmap_tdb: Fatal error while fetching a new id\n!"));
104 return NT_STATUS_UNSUCCESSFUL;
107 /* recheck it is in the range */
108 if (hwm > idmap_state.gid_high) {
109 DEBUG(0, ("idmap Fatal Error: GID range full!! (max: %u)\n", idmap_state.gid_high));
110 return NT_STATUS_UNSUCCESSFUL;
114 DEBUG(10,("db_allocate_id: ID_GROUPID (*id).uid = %d\n", (unsigned int)hwm));
118 return NT_STATUS_INVALID_PARAMETER;
124 /* Get a sid from an id */
125 static NTSTATUS internal_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
129 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
132 return NT_STATUS_INVALID_PARAMETER;
134 switch (id_type & ID_TYPEMASK) {
136 slprintf(keystr, sizeof(keystr), "UID %d", id.uid);
139 slprintf(keystr, sizeof(keystr), "GID %d", id.gid);
142 return NT_STATUS_UNSUCCESSFUL;
146 key.dsize = strlen(keystr) + 1;
148 DEBUG(10,("internal_get_sid_from_id: fetching record %s\n", keystr ));
150 data = tdb_fetch(idmap_tdb, key);
153 if (string_to_sid(sid, data.dptr)) {
154 DEBUG(10,("internal_get_sid_from_id: fetching record %s -> %s\n", keystr, data.dptr ));
157 SAFE_FREE(data.dptr);
163 /* Error codes for get_id_from_sid */
164 enum getidfromsiderr { GET_ID_FROM_SID_OK = 0, GET_ID_FROM_SID_NOTFOUND, GET_ID_FROM_SID_WRONG_TYPE, GET_ID_FROM_SID_ERR };
166 static enum getidfromsiderr internal_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
168 enum getidfromsiderr ret = GET_ID_FROM_SID_ERR;
171 int type = *id_type & ID_TYPEMASK;
173 /* Check if sid is present in database */
174 sid_to_string(keystr, sid);
177 key.dsize = strlen(keystr) + 1;
179 DEBUG(10,("internal_get_id_from_sid: fetching record %s of type 0x%x\n", keystr, type ));
181 data = tdb_fetch(idmap_tdb, key);
183 DEBUG(10,("internal_get_id_from_sid: record %s not found\n", keystr ));
184 return GET_ID_FROM_SID_NOTFOUND;
186 DEBUG(10,("internal_get_id_from_sid: record %s -> %s\n", keystr, data.dptr ));
189 if (type == ID_EMPTY || type == ID_USERID) {
191 /* Parse and return existing uid */
192 fstrcpy(scanstr, "UID %d");
194 if (sscanf(data.dptr, scanstr, &((*id).uid)) == 1) {
196 if (type == ID_EMPTY) {
197 *id_type = ID_USERID;
199 DEBUG(10,("internal_get_id_from_sid: %s fetching record %s -> %s \n",
200 (type == ID_EMPTY) ? "ID_EMPTY" : "ID_USERID",
201 keystr, data.dptr ));
202 ret = GET_ID_FROM_SID_OK;
204 ret = GET_ID_FROM_SID_WRONG_TYPE;
208 if ((ret != GET_ID_FROM_SID_OK) && (type == ID_EMPTY || type == ID_GROUPID)) {
210 /* Parse and return existing gid */
211 fstrcpy(scanstr, "GID %d");
213 if (sscanf(data.dptr, scanstr, &((*id).gid)) == 1) {
215 if (type == ID_EMPTY) {
216 *id_type = ID_GROUPID;
218 DEBUG(10,("internal_get_id_from_sid: %s fetching record %s -> %s \n",
219 (type == ID_EMPTY) ? "ID_EMPTY" : "ID_GROUPID",
220 keystr, data.dptr ));
221 ret = GET_ID_FROM_SID_OK;
223 ret = GET_ID_FROM_SID_WRONG_TYPE;
227 SAFE_FREE(data.dptr);
232 /* Get a sid from an id */
233 static NTSTATUS db_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type_in)
235 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
236 enum getidfromsiderr iderr;
237 int id_type = id_type_in & ID_TYPEMASK;
239 int id_type_tmp = id_type;
241 DEBUG(10,("db_get_sid_from_id: id_type_in = 0x%x\n", id_type_in));
243 ret = internal_get_sid_from_id(sid, id, id_type);
244 if (!NT_STATUS_IS_OK(ret)) {
248 iderr = internal_get_id_from_sid(&id_tmp, &id_type_tmp, sid);
249 if (iderr != GET_ID_FROM_SID_OK) {
250 return NT_STATUS_UNSUCCESSFUL;
252 if (id_type_tmp != id_type) {
253 return NT_STATUS_UNSUCCESSFUL;
254 } else if (id_type == ID_USERID) {
255 if (id_tmp.uid != id.uid) {
256 return NT_STATUS_UNSUCCESSFUL;
258 } else if (id_type == ID_GROUPID) {
259 if (id_tmp.gid != id.gid) {
260 return NT_STATUS_UNSUCCESSFUL;
263 return NT_STATUS_UNSUCCESSFUL;
267 /* Get an id from a sid */
268 static NTSTATUS db_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
270 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
271 enum getidfromsiderr iderr;
273 DEBUG(10,("db_get_id_from_sid\n"));
275 if (!sid || !id || !id_type)
276 return NT_STATUS_INVALID_PARAMETER;
278 iderr = internal_get_id_from_sid(id, id_type, sid);
279 if (iderr == GET_ID_FROM_SID_OK) {
281 ret = internal_get_sid_from_id(&sid_tmp, *id, *id_type);
282 if (NT_STATUS_IS_OK(ret)) {
283 if (!sid_equal(&sid_tmp, sid)) {
284 return NT_STATUS_UNSUCCESSFUL;
287 } else if (iderr == GET_ID_FROM_SID_WRONG_TYPE) {
288 /* We found a record but not the type we wanted.
289 * This is an error, not an opportunity to overwrite...
292 return NT_STATUS_UNSUCCESSFUL;
295 if (!(*id_type & ID_QUERY_ONLY) && (iderr != GET_ID_FROM_SID_OK) &&
296 (((*id_type & ID_TYPEMASK) == ID_USERID)
297 || (*id_type & ID_TYPEMASK) == ID_GROUPID)) {
302 sid_to_string(sid_string, sid);
304 sid_data.dptr = sid_string;
305 sid_data.dsize = strlen(sid_string)+1;
307 /* Lock the record for this SID. */
308 if (tdb_chainlock(idmap_tdb, sid_data) != 0) {
309 DEBUG(10,("db_get_id_from_sid: failed to lock record %s. Error %s\n",
310 sid_string, tdb_errorstr(idmap_tdb) ));
311 return NT_STATUS_UNSUCCESSFUL;
317 /* Allocate a new id for this sid */
318 ret = db_allocate_id(id, *id_type);
319 if (!NT_STATUS_IS_OK(ret))
322 /* Store the UID side */
324 if (*id_type & ID_USERID) {
325 slprintf(ugid_str, sizeof(ugid_str), "UID %d", (*id).uid);
327 slprintf(ugid_str, sizeof(ugid_str), "GID %d", (*id).gid);
330 ugid_data.dptr = ugid_str;
331 ugid_data.dsize = strlen(ugid_str) + 1;
333 DEBUG(10,("db_get_id_from_sid: storing %s -> %s\n",
334 ugid_data.dptr, sid_data.dptr ));
336 if (tdb_store(idmap_tdb, ugid_data, sid_data, TDB_INSERT) != -1) {
340 if (tdb_error(idmap_tdb) != TDB_ERR_EXISTS)
341 DEBUG(10,("db_get_id_from_sid: error %s\n", tdb_errorstr(idmap_tdb) ));
342 ret = NT_STATUS_UNSUCCESSFUL;
343 } while (tdb_error(idmap_tdb) == TDB_ERR_EXISTS);
345 if (NT_STATUS_IS_OK(ret)) {
347 DEBUG(10,("db_get_id_from_sid: storing %s -> %s\n",
348 sid_data.dptr, ugid_data.dptr ));
350 if (tdb_store(idmap_tdb, sid_data, ugid_data, TDB_REPLACE) == -1) {
351 DEBUG(10,("db_get_id_from_sid: error %s\n", tdb_errorstr(idmap_tdb) ));
352 /* TODO: print tdb error !! */
353 tdb_chainunlock(idmap_tdb, sid_data);
354 return NT_STATUS_UNSUCCESSFUL;
358 tdb_chainunlock(idmap_tdb, sid_data);
364 static NTSTATUS db_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
366 TDB_DATA ksid, kid, data;
370 DEBUG(10,("db_set_mapping: id_type = 0x%x\n", id_type));
373 return NT_STATUS_INVALID_PARAMETER;
375 sid_to_string(ksidstr, sid);
378 ksid.dsize = strlen(ksidstr) + 1;
380 if (id_type & ID_USERID) {
381 slprintf(kidstr, sizeof(kidstr), "UID %d", id.uid);
382 } else if (id_type & ID_GROUPID) {
383 slprintf(kidstr, sizeof(kidstr), "GID %d", id.gid);
385 return NT_STATUS_INVALID_PARAMETER;
389 kid.dsize = strlen(kidstr) + 1;
391 /* *DELETE* prevoius mappings if any.
392 * This is done both SID and [U|G]ID passed in */
394 /* Lock the record for this SID. */
395 if (tdb_chainlock(idmap_tdb, ksid) != 0) {
396 DEBUG(10,("db_get_id_from_sid: failed to lock record %s. Error %s\n",
397 ksidstr, tdb_errorstr(idmap_tdb) ));
398 return NT_STATUS_UNSUCCESSFUL;
401 DEBUG(10,("db_set_mapping: fetching %s\n", ksid.dptr));
403 data = tdb_fetch(idmap_tdb, ksid);
405 DEBUG(10,("db_set_mapping: deleting %s and %s\n", data.dptr, ksid.dptr ));
406 tdb_delete(idmap_tdb, data);
407 tdb_delete(idmap_tdb, ksid);
408 SAFE_FREE(data.dptr);
410 data = tdb_fetch(idmap_tdb, kid);
412 DEBUG(10,("db_set_mapping: deleting %s and %s\n", data.dptr, kid.dptr ));
413 tdb_delete(idmap_tdb, data);
414 tdb_delete(idmap_tdb, kid);
415 SAFE_FREE(data.dptr);
418 if (tdb_store(idmap_tdb, ksid, kid, TDB_INSERT) == -1) {
419 DEBUG(0, ("idb_set_mapping: tdb_store 1 error: %s\n", tdb_errorstr(idmap_tdb)));
420 tdb_chainunlock(idmap_tdb, ksid);
421 return NT_STATUS_UNSUCCESSFUL;
423 if (tdb_store(idmap_tdb, kid, ksid, TDB_INSERT) == -1) {
424 DEBUG(0, ("idb_set_mapping: tdb_store 2 error: %s\n", tdb_errorstr(idmap_tdb)));
425 tdb_chainunlock(idmap_tdb, ksid);
426 return NT_STATUS_UNSUCCESSFUL;
429 tdb_chainunlock(idmap_tdb, ksid);
430 DEBUG(10,("db_set_mapping: stored %s -> %s and %s -> %s\n", ksid.dptr, kid.dptr, kid.dptr, ksid.dptr ));
434 /*****************************************************************************
435 Initialise idmap database.
436 *****************************************************************************/
438 static NTSTATUS db_idmap_init( char *params )
440 SMB_STRUCT_STAT stbuf;
441 char *tdbfile = NULL;
443 BOOL tdb_is_new = False;
445 /* use the old database if present */
446 tdbfile = strdup(lock_path("winbindd_idmap.tdb"));
448 DEBUG(0, ("idmap_init: out of memory!\n"));
449 return NT_STATUS_NO_MEMORY;
452 if (!file_exist(tdbfile, &stbuf)) {
456 DEBUG(10,("db_idmap_init: Opening tdbfile %s\n", tdbfile ));
458 /* Open idmap repository */
459 if (!(idmap_tdb = tdb_open_log(tdbfile, 0,
460 TDB_DEFAULT, O_RDWR | O_CREAT,
462 DEBUG(0, ("idmap_init: Unable to open idmap database\n"));
464 return NT_STATUS_UNSUCCESSFUL;
470 /* the file didn't existed before opening it, let's
471 * store idmap version as nobody else yet opened and
472 * stored it. I do not like this method but didn't
473 * found a way to understand if an opened tdb have
474 * been just created or not --- SSS */
475 tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION);
478 /* check against earlier versions */
479 version = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
480 if (version != IDMAP_VERSION) {
481 DEBUG(0, ("idmap_init: Unable to open idmap database, it's in an old format!\n"));
482 return NT_STATUS_INTERNAL_DB_ERROR;
485 /* Create high water marks for group and user id */
486 if (!lp_idmap_uid(&idmap_state.uid_low, &idmap_state.uid_high)) {
487 DEBUG(1, ("idmap uid range missing or invalid\n"));
488 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
490 if (tdb_fetch_int32(idmap_tdb, HWM_USER) == -1) {
491 if (tdb_store_int32(idmap_tdb, HWM_USER, idmap_state.uid_low) == -1) {
492 DEBUG(0, ("idmap_init: Unable to initialise user hwm in idmap database\n"));
493 return NT_STATUS_INTERNAL_DB_ERROR;
498 if (!lp_idmap_gid(&idmap_state.gid_low, &idmap_state.gid_high)) {
499 DEBUG(1, ("idmap gid range missing or invalid\n"));
500 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
502 if (tdb_fetch_int32(idmap_tdb, HWM_GROUP) == -1) {
503 if (tdb_store_int32(idmap_tdb, HWM_GROUP, idmap_state.gid_low) == -1) {
504 DEBUG(0, ("idmap_init: Unable to initialise group hwm in idmap database\n"));
505 return NT_STATUS_INTERNAL_DB_ERROR;
514 static NTSTATUS db_idmap_close(void)
517 if (tdb_close(idmap_tdb) == 0) {
520 return NT_STATUS_UNSUCCESSFUL;
527 /* Dump status information to log file. Display different stuff based on
530 Debug Level Information Displayed
531 =================================================================
532 0 Percentage of [ug]id range allocated
533 0 High water marks (next allocated ids)
538 static void db_idmap_status(void)
540 int user_hwm, group_hwm;
542 DEBUG(0, ("winbindd idmap status:\n"));
544 /* Get current high water marks */
546 if ((user_hwm = tdb_fetch_int32(idmap_tdb, HWM_USER)) == -1) {
548 ("\tCould not get userid high water mark!\n"));
551 if ((group_hwm = tdb_fetch_int32(idmap_tdb, HWM_GROUP)) == -1) {
553 ("\tCould not get groupid high water mark!\n"));
556 /* Display next ids to allocate */
558 if (user_hwm != -1) {
560 ("\tNext userid to allocate is %d\n", user_hwm));
563 if (group_hwm != -1) {
565 ("\tNext groupid to allocate is %d\n", group_hwm));
568 /* Display percentage of id range already allocated. */
570 if (user_hwm != -1) {
571 int num_users = user_hwm - idmap_state.uid_low;
573 idmap_state.uid_high - idmap_state.uid_low;
576 ("\tUser id range is %d%% full (%d of %d)\n",
577 num_users * 100 / total_users, num_users,
581 if (group_hwm != -1) {
582 int num_groups = group_hwm - idmap_state.gid_low;
584 idmap_state.gid_high - idmap_state.gid_low;
587 ("\tGroup id range is %d%% full (%d of %d)\n",
588 num_groups * 100 / total_groups, num_groups,
592 /* Display complete mapping of users and groups to rids */
595 static struct idmap_methods db_methods = {
606 NTSTATUS idmap_tdb_init(void)
608 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);