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 /**********************************************************************
49 Return the TDB_CONTEXT* for winbindd_idmap. I **really** feel
50 dirty doing this, but not so dirty that I want to create another
52 ***********************************************************************/
54 TDB_CONTEXT *idmap_tdb_handle( void )
62 /**********************************************************************
63 allocate a new RID; We don't care if is a user or group
64 **********************************************************************/
66 static NTSTATUS db_allocate_rid(uint32 *rid, int rid_type)
68 uint32 lowrid, highrid;
71 /* can't handle group rids right now. This is such a mess.... */
73 if ( rid_type == GROUP_RID_TYPE )
74 return NT_STATUS_UNSUCCESSFUL;
76 /* cannot fail since idmap is only called winbindd */
78 idmap_get_free_rid_range( &lowrid, &highrid );
82 if ( !tdb_change_uint32_atomic(idmap_tdb, "RID_COUNTER", &tmp_rid, RID_MULTIPLIER) ) {
83 DEBUG(3,("db_allocate_rid: Failed to locate next rid record in idmap db\n"));
84 return NT_STATUS_UNSUCCESSFUL;
87 if ( tmp_rid > highrid ) {
88 DEBUG(0, ("db_allocate_rid: no RIDs available!\n"));
89 return NT_STATUS_UNSUCCESSFUL;
97 /**********************************************************************
98 Allocate either a user or group id from the pool
99 **********************************************************************/
101 static NTSTATUS db_allocate_id(unid_t *id, int id_type)
107 return NT_STATUS_INVALID_PARAMETER;
109 /* Get current high water mark */
110 switch (id_type & ID_TYPEMASK) {
113 if ((hwm = tdb_fetch_int32(idmap_tdb, HWM_USER)) == -1) {
114 return NT_STATUS_INTERNAL_DB_ERROR;
117 /* check it is in the range */
118 if (hwm > idmap_state.uid_high) {
119 DEBUG(0, ("idmap Fatal Error: UID range full!! (max: %lu)\n",
120 (unsigned long)idmap_state.uid_high));
121 return NT_STATUS_UNSUCCESSFUL;
124 /* fetch a new id and increment it */
125 ret = tdb_change_uint32_atomic(idmap_tdb, HWM_USER, &hwm, 1);
127 DEBUG(0, ("idmap_tdb: Fatal error while fetching a new id\n!"));
128 return NT_STATUS_UNSUCCESSFUL;
131 /* recheck it is in the range */
132 if (hwm > idmap_state.uid_high) {
133 DEBUG(0, ("idmap Fatal Error: UID range full!! (max: %lu)\n",
134 (unsigned long)idmap_state.uid_high));
135 return NT_STATUS_UNSUCCESSFUL;
139 DEBUG(10,("db_allocate_id: ID_USERID (*id).uid = %d\n", (unsigned int)hwm));
143 if ((hwm = tdb_fetch_int32(idmap_tdb, HWM_GROUP)) == -1) {
144 return NT_STATUS_INTERNAL_DB_ERROR;
147 /* check it is in the range */
148 if (hwm > idmap_state.gid_high) {
149 DEBUG(0, ("idmap Fatal Error: GID range full!! (max: %lu)\n",
150 (unsigned long)idmap_state.gid_high));
151 return NT_STATUS_UNSUCCESSFUL;
154 /* fetch a new id and increment it */
155 ret = tdb_change_uint32_atomic(idmap_tdb, HWM_GROUP, &hwm, 1);
158 DEBUG(0, ("idmap_tdb: Fatal error while fetching a new id\n!"));
159 return NT_STATUS_UNSUCCESSFUL;
162 /* recheck it is in the range */
163 if (hwm > idmap_state.gid_high) {
164 DEBUG(0, ("idmap Fatal Error: GID range full!! (max: %lu)\n",
165 (unsigned long)idmap_state.gid_high));
166 return NT_STATUS_UNSUCCESSFUL;
170 DEBUG(10,("db_allocate_id: ID_GROUPID (*id).gid = %d\n", (unsigned int)hwm));
174 return NT_STATUS_INVALID_PARAMETER;
180 /* Get a sid from an id */
181 static NTSTATUS internal_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
185 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
188 return NT_STATUS_INVALID_PARAMETER;
190 switch (id_type & ID_TYPEMASK) {
192 slprintf(keystr, sizeof(keystr), "UID %lu", (unsigned long)id.uid);
195 slprintf(keystr, sizeof(keystr), "GID %lu", (unsigned long)id.gid);
198 return NT_STATUS_UNSUCCESSFUL;
202 key.dsize = strlen(keystr) + 1;
204 DEBUG(10,("internal_get_sid_from_id: fetching record %s\n", keystr ));
206 data = tdb_fetch(idmap_tdb, key);
209 if (string_to_sid(sid, data.dptr)) {
210 DEBUG(10,("internal_get_sid_from_id: fetching record %s -> %s\n", keystr, data.dptr ));
213 SAFE_FREE(data.dptr);
219 /* Error codes for get_id_from_sid */
220 enum getidfromsiderr { GET_ID_FROM_SID_OK = 0, GET_ID_FROM_SID_NOTFOUND, GET_ID_FROM_SID_WRONG_TYPE, GET_ID_FROM_SID_ERR };
222 static enum getidfromsiderr internal_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
224 enum getidfromsiderr ret = GET_ID_FROM_SID_ERR;
227 int type = *id_type & ID_TYPEMASK;
229 /* Check if sid is present in database */
230 sid_to_string(keystr, sid);
233 key.dsize = strlen(keystr) + 1;
235 DEBUG(10,("internal_get_id_from_sid: fetching record %s of type 0x%x\n", keystr, type ));
237 data = tdb_fetch(idmap_tdb, key);
239 DEBUG(10,("internal_get_id_from_sid: record %s not found\n", keystr ));
240 return GET_ID_FROM_SID_NOTFOUND;
242 DEBUG(10,("internal_get_id_from_sid: record %s -> %s\n", keystr, data.dptr ));
245 if (type == ID_EMPTY || type == ID_USERID) {
247 /* Parse and return existing uid */
248 fstrcpy(scanstr, "UID %d");
250 if (sscanf(data.dptr, scanstr, &((*id).uid)) == 1) {
252 if (type == ID_EMPTY) {
253 *id_type = ID_USERID;
255 DEBUG(10,("internal_get_id_from_sid: %s fetching record %s -> %s \n",
256 (type == ID_EMPTY) ? "ID_EMPTY" : "ID_USERID",
257 keystr, data.dptr ));
258 ret = GET_ID_FROM_SID_OK;
260 ret = GET_ID_FROM_SID_WRONG_TYPE;
264 if ((ret != GET_ID_FROM_SID_OK) && (type == ID_EMPTY || type == ID_GROUPID)) {
266 /* Parse and return existing gid */
267 fstrcpy(scanstr, "GID %d");
269 if (sscanf(data.dptr, scanstr, &((*id).gid)) == 1) {
271 if (type == ID_EMPTY) {
272 *id_type = ID_GROUPID;
274 DEBUG(10,("internal_get_id_from_sid: %s fetching record %s -> %s \n",
275 (type == ID_EMPTY) ? "ID_EMPTY" : "ID_GROUPID",
276 keystr, data.dptr ));
277 ret = GET_ID_FROM_SID_OK;
279 ret = GET_ID_FROM_SID_WRONG_TYPE;
283 SAFE_FREE(data.dptr);
288 /* Get a sid from an id */
289 static NTSTATUS db_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type_in)
291 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
292 enum getidfromsiderr iderr;
293 int id_type = id_type_in & ID_TYPEMASK;
295 int id_type_tmp = id_type;
297 DEBUG(10,("db_get_sid_from_id: id_type_in = 0x%x\n", id_type_in));
299 ret = internal_get_sid_from_id(sid, id, id_type);
300 if (!NT_STATUS_IS_OK(ret)) {
304 iderr = internal_get_id_from_sid(&id_tmp, &id_type_tmp, sid);
305 if (iderr != GET_ID_FROM_SID_OK) {
306 return NT_STATUS_UNSUCCESSFUL;
308 if (id_type_tmp != id_type) {
309 return NT_STATUS_UNSUCCESSFUL;
310 } else if (id_type == ID_USERID) {
311 if (id_tmp.uid != id.uid) {
312 return NT_STATUS_UNSUCCESSFUL;
314 } else if (id_type == ID_GROUPID) {
315 if (id_tmp.gid != id.gid) {
316 return NT_STATUS_UNSUCCESSFUL;
319 return NT_STATUS_UNSUCCESSFUL;
323 /* Get an id from a sid */
324 static NTSTATUS db_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
326 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
327 enum getidfromsiderr iderr;
329 DEBUG(10,("db_get_id_from_sid\n"));
331 if (!sid || !id || !id_type)
332 return NT_STATUS_INVALID_PARAMETER;
334 iderr = internal_get_id_from_sid(id, id_type, sid);
335 if (iderr == GET_ID_FROM_SID_OK) {
337 ret = internal_get_sid_from_id(&sid_tmp, *id, *id_type);
338 if (NT_STATUS_IS_OK(ret)) {
339 if (!sid_equal(&sid_tmp, sid)) {
340 return NT_STATUS_UNSUCCESSFUL;
343 } else if (iderr == GET_ID_FROM_SID_WRONG_TYPE) {
344 /* We found a record but not the type we wanted.
345 * This is an error, not an opportunity to overwrite...
348 return NT_STATUS_UNSUCCESSFUL;
351 if (!(*id_type & ID_QUERY_ONLY) && (iderr != GET_ID_FROM_SID_OK) &&
352 (((*id_type & ID_TYPEMASK) == ID_USERID)
353 || (*id_type & ID_TYPEMASK) == ID_GROUPID)) {
358 sid_to_string(sid_string, sid);
360 sid_data.dptr = sid_string;
361 sid_data.dsize = strlen(sid_string)+1;
363 /* Lock the record for this SID. */
364 if (tdb_chainlock(idmap_tdb, sid_data) != 0) {
365 DEBUG(10,("db_get_id_from_sid: failed to lock record %s. Error %s\n",
366 sid_string, tdb_errorstr(idmap_tdb) ));
367 return NT_STATUS_UNSUCCESSFUL;
373 /* Allocate a new id for this sid */
374 ret = db_allocate_id(id, *id_type);
375 if (!NT_STATUS_IS_OK(ret))
378 /* Store the UID side */
380 if (*id_type & ID_USERID) {
381 slprintf(ugid_str, sizeof(ugid_str), "UID %lu",
382 (unsigned long)((*id).uid));
384 slprintf(ugid_str, sizeof(ugid_str), "GID %lu",
385 (unsigned long)((*id).gid));
388 ugid_data.dptr = ugid_str;
389 ugid_data.dsize = strlen(ugid_str) + 1;
391 DEBUG(10,("db_get_id_from_sid: storing %s -> %s\n",
392 ugid_data.dptr, sid_data.dptr ));
394 if (tdb_store(idmap_tdb, ugid_data, sid_data, TDB_INSERT) != -1) {
398 if (tdb_error(idmap_tdb) != TDB_ERR_EXISTS)
399 DEBUG(10,("db_get_id_from_sid: error %s\n", tdb_errorstr(idmap_tdb) ));
400 ret = NT_STATUS_UNSUCCESSFUL;
401 } while (tdb_error(idmap_tdb) == TDB_ERR_EXISTS);
403 if (NT_STATUS_IS_OK(ret)) {
405 DEBUG(10,("db_get_id_from_sid: storing %s -> %s\n",
406 sid_data.dptr, ugid_data.dptr ));
408 if (tdb_store(idmap_tdb, sid_data, ugid_data, TDB_REPLACE) == -1) {
409 DEBUG(10,("db_get_id_from_sid: error %s\n", tdb_errorstr(idmap_tdb) ));
410 /* TODO: print tdb error !! */
411 tdb_chainunlock(idmap_tdb, sid_data);
412 return NT_STATUS_UNSUCCESSFUL;
416 tdb_chainunlock(idmap_tdb, sid_data);
422 static NTSTATUS db_set_mapping(const DOM_SID *sid, unid_t id, int id_type)
424 TDB_DATA ksid, kid, data;
428 DEBUG(10,("db_set_mapping: id_type = 0x%x\n", id_type));
431 return NT_STATUS_INVALID_PARAMETER;
433 sid_to_string(ksidstr, sid);
436 ksid.dsize = strlen(ksidstr) + 1;
438 if (id_type & ID_USERID) {
439 slprintf(kidstr, sizeof(kidstr), "UID %lu", (unsigned long)id.uid);
440 } else if (id_type & ID_GROUPID) {
441 slprintf(kidstr, sizeof(kidstr), "GID %lu", (unsigned long)id.gid);
443 return NT_STATUS_INVALID_PARAMETER;
447 kid.dsize = strlen(kidstr) + 1;
449 /* *DELETE* prevoius mappings if any.
450 * This is done both SID and [U|G]ID passed in */
452 /* Lock the record for this SID. */
453 if (tdb_chainlock(idmap_tdb, ksid) != 0) {
454 DEBUG(10,("db_set_mapping: failed to lock record %s. Error %s\n",
455 ksidstr, tdb_errorstr(idmap_tdb) ));
456 return NT_STATUS_UNSUCCESSFUL;
459 DEBUG(10,("db_set_mapping: fetching %s\n", ksid.dptr));
461 data = tdb_fetch(idmap_tdb, ksid);
463 DEBUG(10,("db_set_mapping: deleting %s and %s\n", data.dptr, ksid.dptr ));
464 tdb_delete(idmap_tdb, data);
465 tdb_delete(idmap_tdb, ksid);
466 SAFE_FREE(data.dptr);
468 data = tdb_fetch(idmap_tdb, kid);
470 DEBUG(10,("db_set_mapping: deleting %s and %s\n", data.dptr, kid.dptr ));
471 tdb_delete(idmap_tdb, data);
472 tdb_delete(idmap_tdb, kid);
473 SAFE_FREE(data.dptr);
476 if (tdb_store(idmap_tdb, ksid, kid, TDB_INSERT) == -1) {
477 DEBUG(0, ("idb_set_mapping: tdb_store 1 error: %s\n", tdb_errorstr(idmap_tdb)));
478 tdb_chainunlock(idmap_tdb, ksid);
479 return NT_STATUS_UNSUCCESSFUL;
481 if (tdb_store(idmap_tdb, kid, ksid, TDB_INSERT) == -1) {
482 DEBUG(0, ("idb_set_mapping: tdb_store 2 error: %s\n", tdb_errorstr(idmap_tdb)));
483 tdb_chainunlock(idmap_tdb, ksid);
484 return NT_STATUS_UNSUCCESSFUL;
487 tdb_chainunlock(idmap_tdb, ksid);
488 DEBUG(10,("db_set_mapping: stored %s -> %s and %s -> %s\n", ksid.dptr, kid.dptr, kid.dptr, ksid.dptr ));
492 /*****************************************************************************
493 Initialise idmap database.
494 *****************************************************************************/
496 static NTSTATUS db_idmap_init( char *params )
498 SMB_STRUCT_STAT stbuf;
499 char *tdbfile = NULL;
501 BOOL tdb_is_new = False;
503 /* use the old database if present */
504 tdbfile = strdup(lock_path("winbindd_idmap.tdb"));
506 DEBUG(0, ("idmap_init: out of memory!\n"));
507 return NT_STATUS_NO_MEMORY;
510 if (!file_exist(tdbfile, &stbuf)) {
514 DEBUG(10,("db_idmap_init: Opening tdbfile %s\n", tdbfile ));
516 /* Open idmap repository */
517 if (!(idmap_tdb = tdb_open_log(tdbfile, 0,
518 TDB_DEFAULT, O_RDWR | O_CREAT,
520 DEBUG(0, ("idmap_init: Unable to open idmap database\n"));
522 return NT_STATUS_UNSUCCESSFUL;
528 /* the file didn't existed before opening it, let's
529 * store idmap version as nobody else yet opened and
530 * stored it. I do not like this method but didn't
531 * found a way to understand if an opened tdb have
532 * been just created or not --- SSS */
533 tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION);
536 /* check against earlier versions */
537 version = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
538 if (version != IDMAP_VERSION) {
539 DEBUG(0, ("idmap_init: Unable to open idmap database, it's in an old format!\n"));
540 return NT_STATUS_INTERNAL_DB_ERROR;
543 /* Create high water marks for group and user id */
544 if (!lp_idmap_uid(&idmap_state.uid_low, &idmap_state.uid_high)) {
545 DEBUG(1, ("idmap uid range missing or invalid\n"));
546 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
548 if (tdb_fetch_int32(idmap_tdb, HWM_USER) == -1) {
549 if (tdb_store_int32(idmap_tdb, HWM_USER, idmap_state.uid_low) == -1) {
550 DEBUG(0, ("idmap_init: Unable to initialise user hwm in idmap database\n"));
551 return NT_STATUS_INTERNAL_DB_ERROR;
556 if (!lp_idmap_gid(&idmap_state.gid_low, &idmap_state.gid_high)) {
557 DEBUG(1, ("idmap gid range missing or invalid\n"));
558 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
560 if (tdb_fetch_int32(idmap_tdb, HWM_GROUP) == -1) {
561 if (tdb_store_int32(idmap_tdb, HWM_GROUP, idmap_state.gid_low) == -1) {
562 DEBUG(0, ("idmap_init: Unable to initialise group hwm in idmap database\n"));
563 return NT_STATUS_INTERNAL_DB_ERROR;
572 static NTSTATUS db_idmap_close(void)
575 if (tdb_close(idmap_tdb) == 0) {
578 return NT_STATUS_UNSUCCESSFUL;
585 /* Dump status information to log file. Display different stuff based on
588 Debug Level Information Displayed
589 =================================================================
590 0 Percentage of [ug]id range allocated
591 0 High water marks (next allocated ids)
596 static void db_idmap_status(void)
598 int user_hwm, group_hwm;
600 DEBUG(0, ("winbindd idmap status:\n"));
602 /* Get current high water marks */
604 if ((user_hwm = tdb_fetch_int32(idmap_tdb, HWM_USER)) == -1) {
606 ("\tCould not get userid high water mark!\n"));
609 if ((group_hwm = tdb_fetch_int32(idmap_tdb, HWM_GROUP)) == -1) {
611 ("\tCould not get groupid high water mark!\n"));
614 /* Display next ids to allocate */
616 if (user_hwm != -1) {
618 ("\tNext userid to allocate is %d\n", user_hwm));
621 if (group_hwm != -1) {
623 ("\tNext groupid to allocate is %d\n", group_hwm));
626 /* Display percentage of id range already allocated. */
628 if (user_hwm != -1) {
629 int num_users = user_hwm - idmap_state.uid_low;
631 idmap_state.uid_high - idmap_state.uid_low;
634 ("\tUser id range is %d%% full (%d of %d)\n",
635 num_users * 100 / total_users, num_users,
639 if (group_hwm != -1) {
640 int num_groups = group_hwm - idmap_state.gid_low;
642 idmap_state.gid_high - idmap_state.gid_low;
645 ("\tGroup id range is %d%% full (%d of %d)\n",
646 num_groups * 100 / total_groups, num_groups,
650 /* Display complete mapping of users and groups to rids */
653 static struct idmap_methods db_methods = {
666 NTSTATUS idmap_tdb_init(void)
668 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);