2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 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 allocate a new RID; We don't care if is a user or group
50 **********************************************************************/
52 static NTSTATUS
db_allocate_rid(uint32
*rid
, int rid_type
)
54 uint32 lowrid
, highrid
;
57 /* can't handle group rids right now. This is such a mess.... */
59 if ( rid_type
== GROUP_RID_TYPE
)
60 return NT_STATUS_UNSUCCESSFUL
;
62 /* cannot fail since idmap is only called winbindd */
64 get_free_rid_range( &lowrid
, &highrid
);
68 if ( !tdb_change_uint32_atomic(idmap_tdb
, "RID_COUNTER", &tmp_rid
, RID_MULTIPLIER
) ) {
69 DEBUG(3,("db_allocate_rid: Failed to locate next rid record in idmap db\n"));
70 return NT_STATUS_UNSUCCESSFUL
;
73 if ( tmp_rid
> highrid
) {
74 DEBUG(0, ("db_allocate_rid: no RIDs available!\n"));
75 return NT_STATUS_UNSUCCESSFUL
;
83 /**********************************************************************
84 Allocate either a user or group id from the pool
85 **********************************************************************/
87 static NTSTATUS
db_allocate_id(unid_t
*id
, int id_type
)
93 return NT_STATUS_INVALID_PARAMETER
;
95 /* Get current high water mark */
96 switch (id_type
& ID_TYPEMASK
) {
99 if ((hwm
= tdb_fetch_int32(idmap_tdb
, HWM_USER
)) == -1) {
100 return NT_STATUS_INTERNAL_DB_ERROR
;
103 /* check it is in the range */
104 if (hwm
> idmap_state
.uid_high
) {
105 DEBUG(0, ("idmap Fatal Error: UID range full!! (max: %lu)\n",
106 (unsigned long)idmap_state
.uid_high
));
107 return NT_STATUS_UNSUCCESSFUL
;
110 /* fetch a new id and increment it */
111 ret
= tdb_change_uint32_atomic(idmap_tdb
, HWM_USER
, (unsigned int *)&hwm
, 1);
113 DEBUG(0, ("idmap_tdb: Fatal error while fetching a new id\n!"));
114 return NT_STATUS_UNSUCCESSFUL
;
117 /* recheck 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
;
125 DEBUG(10,("db_allocate_id: ID_USERID (*id).uid = %d\n", (unsigned int)hwm
));
129 if ((hwm
= tdb_fetch_int32(idmap_tdb
, HWM_GROUP
)) == -1) {
130 return NT_STATUS_INTERNAL_DB_ERROR
;
133 /* check it is in the range */
134 if (hwm
> idmap_state
.gid_high
) {
135 DEBUG(0, ("idmap Fatal Error: GID range full!! (max: %lu)\n",
136 (unsigned long)idmap_state
.gid_high
));
137 return NT_STATUS_UNSUCCESSFUL
;
140 /* fetch a new id and increment it */
141 ret
= tdb_change_uint32_atomic(idmap_tdb
, HWM_GROUP
, (unsigned int *)&hwm
, 1);
144 DEBUG(0, ("idmap_tdb: Fatal error while fetching a new id\n!"));
145 return NT_STATUS_UNSUCCESSFUL
;
148 /* recheck it is in the range */
149 if (hwm
> idmap_state
.gid_high
) {
150 DEBUG(0, ("idmap Fatal Error: GID range full!! (max: %lu)\n",
151 (unsigned long)idmap_state
.gid_high
));
152 return NT_STATUS_UNSUCCESSFUL
;
156 DEBUG(10,("db_allocate_id: ID_GROUPID (*id).gid = %d\n", (unsigned int)hwm
));
160 return NT_STATUS_INVALID_PARAMETER
;
166 /* Get a sid from an id */
167 static NTSTATUS
internal_get_sid_from_id(DOM_SID
*sid
, unid_t id
, int id_type
)
171 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
174 return NT_STATUS_INVALID_PARAMETER
;
176 switch (id_type
& ID_TYPEMASK
) {
178 slprintf(keystr
, sizeof(keystr
), "UID %lu", (unsigned long)id
.uid
);
181 slprintf(keystr
, sizeof(keystr
), "GID %lu", (unsigned long)id
.gid
);
184 return NT_STATUS_UNSUCCESSFUL
;
188 key
.dsize
= strlen(keystr
) + 1;
190 DEBUG(10,("internal_get_sid_from_id: fetching record %s\n", keystr
));
192 data
= tdb_fetch(idmap_tdb
, key
);
195 if (string_to_sid(sid
, data
.dptr
)) {
196 DEBUG(10,("internal_get_sid_from_id: fetching record %s -> %s\n", keystr
, data
.dptr
));
199 SAFE_FREE(data
.dptr
);
205 /* Error codes for get_id_from_sid */
206 enum getidfromsiderr
{ GET_ID_FROM_SID_OK
= 0, GET_ID_FROM_SID_NOTFOUND
, GET_ID_FROM_SID_WRONG_TYPE
, GET_ID_FROM_SID_ERR
};
208 static enum getidfromsiderr
internal_get_id_from_sid(unid_t
*id
, int *id_type
, const DOM_SID
*sid
)
210 enum getidfromsiderr ret
= GET_ID_FROM_SID_ERR
;
213 int type
= *id_type
& ID_TYPEMASK
;
215 /* Check if sid is present in database */
216 sid_to_string(keystr
, sid
);
219 key
.dsize
= strlen(keystr
) + 1;
221 DEBUG(10,("internal_get_id_from_sid: fetching record %s of type 0x%x\n", keystr
, type
));
223 data
= tdb_fetch(idmap_tdb
, key
);
225 DEBUG(10,("internal_get_id_from_sid: record %s not found\n", keystr
));
226 return GET_ID_FROM_SID_NOTFOUND
;
228 DEBUG(10,("internal_get_id_from_sid: record %s -> %s\n", keystr
, data
.dptr
));
231 if (type
== ID_EMPTY
|| type
== ID_USERID
) {
233 /* Parse and return existing uid */
234 fstrcpy(scanstr
, "UID %d");
236 if (sscanf(data
.dptr
, scanstr
, &((*id
).uid
)) == 1) {
238 if (type
== ID_EMPTY
) {
239 *id_type
= ID_USERID
;
241 DEBUG(10,("internal_get_id_from_sid: %s fetching record %s -> %s \n",
242 (type
== ID_EMPTY
) ? "ID_EMPTY" : "ID_USERID",
243 keystr
, data
.dptr
));
244 ret
= GET_ID_FROM_SID_OK
;
246 ret
= GET_ID_FROM_SID_WRONG_TYPE
;
250 if ((ret
!= GET_ID_FROM_SID_OK
) && (type
== ID_EMPTY
|| type
== ID_GROUPID
)) {
252 /* Parse and return existing gid */
253 fstrcpy(scanstr
, "GID %d");
255 if (sscanf(data
.dptr
, scanstr
, &((*id
).gid
)) == 1) {
257 if (type
== ID_EMPTY
) {
258 *id_type
= ID_GROUPID
;
260 DEBUG(10,("internal_get_id_from_sid: %s fetching record %s -> %s \n",
261 (type
== ID_EMPTY
) ? "ID_EMPTY" : "ID_GROUPID",
262 keystr
, data
.dptr
));
263 ret
= GET_ID_FROM_SID_OK
;
265 ret
= GET_ID_FROM_SID_WRONG_TYPE
;
269 SAFE_FREE(data
.dptr
);
274 /* Get a sid from an id */
275 static NTSTATUS
db_get_sid_from_id(DOM_SID
*sid
, unid_t id
, int id_type_in
)
277 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
278 enum getidfromsiderr iderr
;
279 int id_type
= id_type_in
& ID_TYPEMASK
;
281 int id_type_tmp
= id_type
;
283 DEBUG(10,("db_get_sid_from_id: id_type_in = 0x%x\n", id_type_in
));
285 ret
= internal_get_sid_from_id(sid
, id
, id_type
);
286 if (!NT_STATUS_IS_OK(ret
)) {
290 iderr
= internal_get_id_from_sid(&id_tmp
, &id_type_tmp
, sid
);
291 if (iderr
!= GET_ID_FROM_SID_OK
) {
292 return NT_STATUS_UNSUCCESSFUL
;
294 if (id_type_tmp
!= id_type
) {
295 return NT_STATUS_UNSUCCESSFUL
;
296 } else if (id_type
== ID_USERID
) {
297 if (id_tmp
.uid
!= id
.uid
) {
298 return NT_STATUS_UNSUCCESSFUL
;
300 } else if (id_type
== ID_GROUPID
) {
301 if (id_tmp
.gid
!= id
.gid
) {
302 return NT_STATUS_UNSUCCESSFUL
;
305 return NT_STATUS_UNSUCCESSFUL
;
309 /* Get an id from a sid */
310 static NTSTATUS
db_get_id_from_sid(unid_t
*id
, int *id_type
, const DOM_SID
*sid
)
312 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
313 enum getidfromsiderr iderr
;
315 DEBUG(10,("db_get_id_from_sid\n"));
317 if (!sid
|| !id
|| !id_type
)
318 return NT_STATUS_INVALID_PARAMETER
;
320 iderr
= internal_get_id_from_sid(id
, id_type
, sid
);
321 if (iderr
== GET_ID_FROM_SID_OK
) {
323 ret
= internal_get_sid_from_id(&sid_tmp
, *id
, *id_type
);
324 if (NT_STATUS_IS_OK(ret
)) {
325 if (!sid_equal(&sid_tmp
, sid
)) {
326 return NT_STATUS_UNSUCCESSFUL
;
329 } else if (iderr
== GET_ID_FROM_SID_WRONG_TYPE
) {
330 /* We found a record but not the type we wanted.
331 * This is an error, not an opportunity to overwrite...
334 return NT_STATUS_UNSUCCESSFUL
;
337 if (!(*id_type
& ID_QUERY_ONLY
) && (iderr
!= GET_ID_FROM_SID_OK
) &&
338 (((*id_type
& ID_TYPEMASK
) == ID_USERID
)
339 || (*id_type
& ID_TYPEMASK
) == ID_GROUPID
)) {
344 sid_to_string(sid_string
, sid
);
346 sid_data
.dptr
= sid_string
;
347 sid_data
.dsize
= strlen(sid_string
)+1;
349 /* Lock the record for this SID. */
350 if (tdb_chainlock(idmap_tdb
, sid_data
) != 0) {
351 DEBUG(10,("db_get_id_from_sid: failed to lock record %s. Error %s\n",
352 sid_string
, tdb_errorstr(idmap_tdb
) ));
353 return NT_STATUS_UNSUCCESSFUL
;
359 /* Allocate a new id for this sid */
360 ret
= db_allocate_id(id
, *id_type
);
361 if (!NT_STATUS_IS_OK(ret
))
364 /* Store the UID side */
366 if (*id_type
& ID_USERID
) {
367 slprintf(ugid_str
, sizeof(ugid_str
), "UID %lu",
368 (unsigned long)((*id
).uid
));
370 slprintf(ugid_str
, sizeof(ugid_str
), "GID %lu",
371 (unsigned long)((*id
).gid
));
374 ugid_data
.dptr
= ugid_str
;
375 ugid_data
.dsize
= strlen(ugid_str
) + 1;
377 DEBUG(10,("db_get_id_from_sid: storing %s -> %s\n",
378 ugid_data
.dptr
, sid_data
.dptr
));
380 if (tdb_store(idmap_tdb
, ugid_data
, sid_data
, TDB_INSERT
) != -1) {
384 if (tdb_error(idmap_tdb
) != TDB_ERR_EXISTS
)
385 DEBUG(10,("db_get_id_from_sid: error %s\n", tdb_errorstr(idmap_tdb
) ));
386 ret
= NT_STATUS_UNSUCCESSFUL
;
387 } while (tdb_error(idmap_tdb
) == TDB_ERR_EXISTS
);
389 if (NT_STATUS_IS_OK(ret
)) {
391 DEBUG(10,("db_get_id_from_sid: storing %s -> %s\n",
392 sid_data
.dptr
, ugid_data
.dptr
));
394 if (tdb_store(idmap_tdb
, sid_data
, ugid_data
, TDB_REPLACE
) == -1) {
395 DEBUG(10,("db_get_id_from_sid: error %s\n", tdb_errorstr(idmap_tdb
) ));
396 /* TODO: print tdb error !! */
397 tdb_chainunlock(idmap_tdb
, sid_data
);
398 return NT_STATUS_UNSUCCESSFUL
;
402 tdb_chainunlock(idmap_tdb
, sid_data
);
408 static NTSTATUS
db_set_mapping(const DOM_SID
*sid
, unid_t id
, int id_type
)
410 TDB_DATA ksid
, kid
, data
;
414 DEBUG(10,("db_set_mapping: id_type = 0x%x\n", id_type
));
417 return NT_STATUS_INVALID_PARAMETER
;
419 sid_to_string(ksidstr
, sid
);
422 ksid
.dsize
= strlen(ksidstr
) + 1;
424 if (id_type
& ID_USERID
) {
425 slprintf(kidstr
, sizeof(kidstr
), "UID %lu", (unsigned long)id
.uid
);
426 } else if (id_type
& ID_GROUPID
) {
427 slprintf(kidstr
, sizeof(kidstr
), "GID %lu", (unsigned long)id
.gid
);
429 return NT_STATUS_INVALID_PARAMETER
;
433 kid
.dsize
= strlen(kidstr
) + 1;
435 /* *DELETE* prevoius mappings if any.
436 * This is done both SID and [U|G]ID passed in */
438 /* Lock the record for this SID. */
439 if (tdb_chainlock(idmap_tdb
, ksid
) != 0) {
440 DEBUG(10,("db_set_mapping: failed to lock record %s. Error %s\n",
441 ksidstr
, tdb_errorstr(idmap_tdb
) ));
442 return NT_STATUS_UNSUCCESSFUL
;
445 DEBUG(10,("db_set_mapping: fetching %s\n", ksid
.dptr
));
447 data
= tdb_fetch(idmap_tdb
, ksid
);
449 DEBUG(10,("db_set_mapping: deleting %s and %s\n", data
.dptr
, ksid
.dptr
));
450 tdb_delete(idmap_tdb
, data
);
451 tdb_delete(idmap_tdb
, ksid
);
452 SAFE_FREE(data
.dptr
);
454 data
= tdb_fetch(idmap_tdb
, kid
);
456 DEBUG(10,("db_set_mapping: deleting %s and %s\n", data
.dptr
, kid
.dptr
));
457 tdb_delete(idmap_tdb
, data
);
458 tdb_delete(idmap_tdb
, kid
);
459 SAFE_FREE(data
.dptr
);
462 if (tdb_store(idmap_tdb
, ksid
, kid
, TDB_INSERT
) == -1) {
463 DEBUG(0, ("idb_set_mapping: tdb_store 1 error: %s\n", tdb_errorstr(idmap_tdb
)));
464 tdb_chainunlock(idmap_tdb
, ksid
);
465 return NT_STATUS_UNSUCCESSFUL
;
467 if (tdb_store(idmap_tdb
, kid
, ksid
, TDB_INSERT
) == -1) {
468 DEBUG(0, ("idb_set_mapping: tdb_store 2 error: %s\n", tdb_errorstr(idmap_tdb
)));
469 tdb_chainunlock(idmap_tdb
, ksid
);
470 return NT_STATUS_UNSUCCESSFUL
;
473 tdb_chainunlock(idmap_tdb
, ksid
);
474 DEBUG(10,("db_set_mapping: stored %s -> %s and %s -> %s\n", ksid
.dptr
, kid
.dptr
, kid
.dptr
, ksid
.dptr
));
478 /*****************************************************************************
479 Initialise idmap database.
480 *****************************************************************************/
482 static NTSTATUS
db_idmap_init( char *params
)
484 SMB_STRUCT_STAT stbuf
;
485 char *tdbfile
= NULL
;
487 BOOL tdb_is_new
= False
;
489 /* use the old database if present */
490 tdbfile
= SMB_STRDUP(lock_path("winbindd_idmap.tdb"));
492 DEBUG(0, ("idmap_init: out of memory!\n"));
493 return NT_STATUS_NO_MEMORY
;
496 if (!file_exist(tdbfile
, &stbuf
)) {
500 DEBUG(10,("db_idmap_init: Opening tdbfile %s\n", tdbfile
));
502 /* Open idmap repository */
503 if (!(idmap_tdb
= tdb_open_log(tdbfile
, 0,
504 TDB_DEFAULT
, O_RDWR
| O_CREAT
,
506 DEBUG(0, ("idmap_init: Unable to open idmap database\n"));
508 return NT_STATUS_UNSUCCESSFUL
;
514 /* the file didn't existed before opening it, let's
515 * store idmap version as nobody else yet opened and
516 * stored it. I do not like this method but didn't
517 * found a way to understand if an opened tdb have
518 * been just created or not --- SSS */
519 tdb_store_int32(idmap_tdb
, "IDMAP_VERSION", IDMAP_VERSION
);
522 /* check against earlier versions */
523 version
= tdb_fetch_int32(idmap_tdb
, "IDMAP_VERSION");
524 if (version
!= IDMAP_VERSION
) {
525 DEBUG(0, ("idmap_init: Unable to open idmap database, it's in an old format!\n"));
526 return NT_STATUS_INTERNAL_DB_ERROR
;
529 /* Create high water marks for group and user id */
530 if (!lp_idmap_uid(&idmap_state
.uid_low
, &idmap_state
.uid_high
)) {
531 DEBUG(1, ("idmap uid range missing or invalid\n"));
532 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
534 if (tdb_fetch_int32(idmap_tdb
, HWM_USER
) == -1) {
535 if (tdb_store_int32(idmap_tdb
, HWM_USER
, idmap_state
.uid_low
) == -1) {
536 DEBUG(0, ("idmap_init: Unable to initialise user hwm in idmap database\n"));
537 return NT_STATUS_INTERNAL_DB_ERROR
;
542 if (!lp_idmap_gid(&idmap_state
.gid_low
, &idmap_state
.gid_high
)) {
543 DEBUG(1, ("idmap gid range missing or invalid\n"));
544 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
546 if (tdb_fetch_int32(idmap_tdb
, HWM_GROUP
) == -1) {
547 if (tdb_store_int32(idmap_tdb
, HWM_GROUP
, idmap_state
.gid_low
) == -1) {
548 DEBUG(0, ("idmap_init: Unable to initialise group hwm in idmap database\n"));
549 return NT_STATUS_INTERNAL_DB_ERROR
;
558 static NTSTATUS
db_idmap_close(void)
561 if (tdb_close(idmap_tdb
) == 0) {
564 return NT_STATUS_UNSUCCESSFUL
;
571 /* Dump status information to log file. Display different stuff based on
574 Debug Level Information Displayed
575 =================================================================
576 0 Percentage of [ug]id range allocated
577 0 High water marks (next allocated ids)
582 static void db_idmap_status(void)
584 int user_hwm
, group_hwm
;
586 DEBUG(0, ("winbindd idmap status:\n"));
588 /* Get current high water marks */
590 if ((user_hwm
= tdb_fetch_int32(idmap_tdb
, HWM_USER
)) == -1) {
592 ("\tCould not get userid high water mark!\n"));
595 if ((group_hwm
= tdb_fetch_int32(idmap_tdb
, HWM_GROUP
)) == -1) {
597 ("\tCould not get groupid high water mark!\n"));
600 /* Display next ids to allocate */
602 if (user_hwm
!= -1) {
604 ("\tNext userid to allocate is %d\n", user_hwm
));
607 if (group_hwm
!= -1) {
609 ("\tNext groupid to allocate is %d\n", group_hwm
));
612 /* Display percentage of id range already allocated. */
614 if (user_hwm
!= -1) {
615 int num_users
= user_hwm
- idmap_state
.uid_low
;
617 idmap_state
.uid_high
- idmap_state
.uid_low
;
620 ("\tUser id range is %d%% full (%d of %d)\n",
621 num_users
* 100 / total_users
, num_users
,
625 if (group_hwm
!= -1) {
626 int num_groups
= group_hwm
- idmap_state
.gid_low
;
628 idmap_state
.gid_high
- idmap_state
.gid_low
;
631 ("\tGroup id range is %d%% full (%d of %d)\n",
632 num_groups
* 100 / total_groups
, num_groups
,
636 /* Display complete mapping of users and groups to rids */
639 /**********************************************************************
640 Return the TDB_CONTEXT* for winbindd_idmap. I **really** feel
641 dirty doing this, but not so dirty that I want to create another
643 ***********************************************************************/
645 TDB_CONTEXT
*idmap_tdb_handle( void )
650 /* go ahead an open it; db_idmap_init() doesn't use any params
653 db_idmap_init( NULL
);
660 static struct idmap_methods db_methods
= {
673 NTSTATUS
idmap_tdb_init(void)
675 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);