seem that someone changed the semanthic of unix_strlower without fixing all
[Samba.git] / source3 / sam / idmap_tdb.c
blobc494cf595faafef10d1b122875ad397b29302d5d
1 /*
2 Unix SMB/CIFS implementation.
4 idmap TDB backend
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.
25 #include "includes.h"
27 #undef DBGC_CLASS
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
37 /* Globals */
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 */
46 } idmap_state;
48 /* Allocate either a user or group id from the pool */
49 static NTSTATUS db_allocate_id(unid_t *id, int id_type)
51 int hwm;
53 if (!id) return NT_STATUS_INVALID_PARAMETER;
55 /* Get current high water mark */
56 switch (id_type & ID_TYPEMASK) {
57 case ID_USERID:
58 if ((hwm = tdb_fetch_int32(idmap_tdb, HWM_USER)) == -1) {
59 return NT_STATUS_INTERNAL_DB_ERROR;
62 if (hwm > idmap_state.uid_high) {
63 DEBUG(0, ("idmap Fatal Error: UID range full!!\n"));
64 return NT_STATUS_UNSUCCESSFUL;
67 (*id).uid = hwm++;
69 /* Store new high water mark */
70 tdb_store_int32(idmap_tdb, HWM_USER, hwm);
71 break;
72 case ID_GROUPID:
73 if ((hwm = tdb_fetch_int32(idmap_tdb, HWM_GROUP)) == -1) {
74 return NT_STATUS_INTERNAL_DB_ERROR;
77 if (hwm > idmap_state.gid_high) {
78 DEBUG(0, ("idmap Fatal Error: GID range full!!\n"));
79 return NT_STATUS_UNSUCCESSFUL;
82 (*id).gid = hwm++;
84 /* Store new high water mark */
85 tdb_store_int32(idmap_tdb, HWM_GROUP, hwm);
86 break;
87 default:
88 return NT_STATUS_INVALID_PARAMETER;
91 return NT_STATUS_OK;
94 /* Get a sid from an id */
95 static NTSTATUS db_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
97 TDB_DATA key, data;
98 fstring keystr;
99 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
101 if (!sid) return NT_STATUS_INVALID_PARAMETER;
103 switch (id_type & ID_TYPEMASK) {
104 case ID_USERID:
105 slprintf(keystr, sizeof(keystr), "UID %d", id.uid);
106 break;
107 case ID_GROUPID:
108 slprintf(keystr, sizeof(keystr), "GID %d", id.gid);
109 break;
110 default:
111 return NT_STATUS_UNSUCCESSFUL;
114 key.dptr = keystr;
115 key.dsize = strlen(keystr) + 1;
117 data = tdb_fetch(idmap_tdb, key);
119 if (data.dptr) {
120 if (string_to_sid(sid, data.dptr)) {
121 ret = NT_STATUS_OK;
123 SAFE_FREE(data.dptr);
126 return ret;
129 /* Get an id from a sid */
130 static NTSTATUS db_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
132 TDB_DATA data, key;
133 fstring keystr;
134 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
136 if (!sid || !id || !id_type) return NT_STATUS_INVALID_PARAMETER;
138 /* Check if sid is present in database */
139 sid_to_string(keystr, sid);
141 key.dptr = keystr;
142 key.dsize = strlen(keystr) + 1;
144 data = tdb_fetch(idmap_tdb, key);
146 if (data.dptr) {
147 int type = *id_type & ID_TYPEMASK;
148 fstring scanstr;
150 if (type == ID_EMPTY || type == ID_USERID) {
151 /* Parse and return existing uid */
152 fstrcpy(scanstr, "UID %d");
154 if (sscanf(data.dptr, scanstr, &((*id).uid)) == 1) {
155 /* uid ok? */
156 if (type == ID_EMPTY) {
157 *id_type = ID_USERID;
159 ret = NT_STATUS_OK;
160 goto idok;
164 if (type == ID_EMPTY || type == ID_GROUPID) {
165 /* Parse and return existing gid */
166 fstrcpy(scanstr, "GID %d");
168 if (sscanf(data.dptr, scanstr, &((*id).gid)) == 1) {
169 /* gid ok? */
170 if (type == ID_EMPTY) {
171 *id_type = ID_GROUPID;
173 ret = NT_STATUS_OK;
176 idok:
177 SAFE_FREE(data.dptr);
179 } else if (!(*id_type & ID_NOMAP) &&
180 (((*id_type & ID_TYPEMASK) == ID_USERID)
181 || (*id_type & ID_TYPEMASK) == ID_GROUPID)) {
183 /* Allocate a new id for this sid */
184 ret = db_allocate_id(id, *id_type);
185 if (NT_STATUS_IS_OK(ret)) {
186 fstring keystr2;
188 /* Store new id */
189 if (*id_type & ID_USERID) {
190 slprintf(keystr2, sizeof(keystr2), "UID %d", (*id).uid);
191 } else {
192 slprintf(keystr2, sizeof(keystr2), "GID %d", (*id).gid);
195 data.dptr = keystr2;
196 data.dsize = strlen(keystr2) + 1;
198 if (tdb_store(idmap_tdb, key, data, TDB_REPLACE) == -1) {
199 /* TODO: print tdb error !! */
200 return NT_STATUS_UNSUCCESSFUL;
202 if (tdb_store(idmap_tdb, data, key, TDB_REPLACE) == -1) {
203 /* TODO: print tdb error !! */
204 return NT_STATUS_UNSUCCESSFUL;
207 ret = NT_STATUS_OK;
211 return ret;
214 static NTSTATUS db_set_mapping(DOM_SID *sid, unid_t id, int id_type)
216 TDB_DATA ksid, kid;
217 fstring ksidstr;
218 fstring kidstr;
220 if (!sid) return NT_STATUS_INVALID_PARAMETER;
222 sid_to_string(ksidstr, sid);
224 ksid.dptr = ksidstr;
225 ksid.dsize = strlen(ksidstr) + 1;
227 if (id_type & ID_USERID) {
228 slprintf(kidstr, sizeof(kidstr), "UID %d", id.uid);
229 } else if (id_type & ID_GROUPID) {
230 slprintf(kidstr, sizeof(kidstr), "GID %d", id.gid);
231 } else {
232 return NT_STATUS_INVALID_PARAMETER;
235 kid.dptr = kidstr;
236 kid.dsize = strlen(kidstr) + 1;
238 if (tdb_store(idmap_tdb, ksid, kid, TDB_INSERT) == -1) {
239 DEBUG(0, ("idb_set_mapping: tdb_store 1 error: %s\n", tdb_errorstr(idmap_tdb)));
240 return NT_STATUS_UNSUCCESSFUL;
242 if (tdb_store(idmap_tdb, kid, ksid, TDB_INSERT) == -1) {
243 DEBUG(0, ("idb_set_mapping: tdb_store 2 error: %s\n", tdb_errorstr(idmap_tdb)));
244 return NT_STATUS_UNSUCCESSFUL;
246 return NT_STATUS_OK;
249 /*****************************************************************************
250 Initialise idmap database.
251 *****************************************************************************/
252 static NTSTATUS db_idmap_init(void)
254 SMB_STRUCT_STAT stbuf;
256 /* move to the new database on first startup */
257 if (!file_exist(lock_path("idmap.tdb"), &stbuf)) {
258 if (file_exist(lock_path("winbindd_idmap.tdb"), &stbuf)) {
259 DEBUG(0, ("idmap_init: winbindd_idmap.tdb is present and idmap.tdb is not!\nPlease RUN winbindd first to convert the db to the new format!\n"));
260 return NT_STATUS_UNSUCCESSFUL;
264 /* Open tdb cache */
265 if (!(idmap_tdb = tdb_open_log(lock_path("idmap.tdb"), 0,
266 TDB_DEFAULT, O_RDWR | O_CREAT,
267 0600))) {
268 DEBUG(0, ("idmap_init: Unable to open idmap database\n"));
269 return NT_STATUS_UNSUCCESSFUL;
272 /* Create high water marks for group and user id */
273 if (tdb_fetch_int32(idmap_tdb, HWM_USER) == -1) {
274 if (tdb_store_int32(idmap_tdb, HWM_USER, idmap_state.uid_low) == -1) {
275 DEBUG(0, ("idmap_init: Unable to initialise user hwm in idmap database\n"));
276 return NT_STATUS_INTERNAL_DB_ERROR;
280 if (tdb_fetch_int32(idmap_tdb, HWM_GROUP) == -1) {
281 if (tdb_store_int32(idmap_tdb, HWM_GROUP, idmap_state.gid_low) == -1) {
282 DEBUG(0, ("idmap_init: Unable to initialise group hwm in idmap database\n"));
283 return NT_STATUS_INTERNAL_DB_ERROR;
287 return NT_STATUS_OK;
290 /* Close the tdb */
291 static NTSTATUS db_idmap_close(void)
293 if (idmap_tdb) {
294 if (tdb_close(idmap_tdb) == 0) {
295 return NT_STATUS_OK;
296 } else {
297 return NT_STATUS_UNSUCCESSFUL;
300 return NT_STATUS_OK;
304 /* Dump status information to log file. Display different stuff based on
305 the debug level:
307 Debug Level Information Displayed
308 =================================================================
309 0 Percentage of [ug]id range allocated
310 0 High water marks (next allocated ids)
313 #define DUMP_INFO 0
315 static void db_idmap_status(void)
317 int user_hwm, group_hwm;
319 DEBUG(0, ("winbindd idmap status:\n"));
321 /* Get current high water marks */
323 if ((user_hwm = tdb_fetch_int32(idmap_tdb, HWM_USER)) == -1) {
324 DEBUG(DUMP_INFO,
325 ("\tCould not get userid high water mark!\n"));
328 if ((group_hwm = tdb_fetch_int32(idmap_tdb, HWM_GROUP)) == -1) {
329 DEBUG(DUMP_INFO,
330 ("\tCould not get groupid high water mark!\n"));
333 /* Display next ids to allocate */
335 if (user_hwm != -1) {
336 DEBUG(DUMP_INFO,
337 ("\tNext userid to allocate is %d\n", user_hwm));
340 if (group_hwm != -1) {
341 DEBUG(DUMP_INFO,
342 ("\tNext groupid to allocate is %d\n", group_hwm));
345 /* Display percentage of id range already allocated. */
347 if (user_hwm != -1) {
348 int num_users = user_hwm - idmap_state.uid_low;
349 int total_users =
350 idmap_state.uid_high - idmap_state.uid_low;
352 DEBUG(DUMP_INFO,
353 ("\tUser id range is %d%% full (%d of %d)\n",
354 num_users * 100 / total_users, num_users,
355 total_users));
358 if (group_hwm != -1) {
359 int num_groups = group_hwm - idmap_state.gid_low;
360 int total_groups =
361 idmap_state.gid_high - idmap_state.gid_low;
363 DEBUG(DUMP_INFO,
364 ("\tGroup id range is %d%% full (%d of %d)\n",
365 num_groups * 100 / total_groups, num_groups,
366 total_groups));
369 /* Display complete mapping of users and groups to rids */
372 struct idmap_methods db_methods = {
374 db_idmap_init,
375 db_get_sid_from_id,
376 db_get_id_from_sid,
377 db_set_mapping,
378 db_idmap_close,
379 db_idmap_status
383 NTSTATUS idmap_reg_tdb(struct idmap_methods **meth)
385 *meth = &db_methods;
387 return NT_STATUS_OK;