2 Unix SMB/Netbios implementation.
5 Winbind daemon - user related function
7 Copyright (C) Tim Potter 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* High water mark keys */
28 #define HWM_GROUP "GROUP HWM"
29 #define HWM_USER "USER HWM"
33 static TDB_CONTEXT *idmap_tdb;
35 /* Allocate either a user or group id from the pool */
37 static BOOL allocate_id(int *id, BOOL isgroup)
41 /* Get current high water mark */
43 if ((hwm = tdb_get_int(idmap_tdb, isgroup ? HWM_GROUP : HWM_USER)) == -1) {
47 /* Return next available uid in list */
49 if ((isgroup && (hwm > server_state.gid_high)) ||
50 (!isgroup && (hwm > server_state.uid_high))) {
51 DEBUG(0, ("winbind %sid range full!\n", isgroup ? "g" : "u"));
61 /* Store new high water mark */
63 tdb_store_int(idmap_tdb, isgroup ? HWM_GROUP : HWM_USER, hwm);
68 /* Get an id from a rid */
70 static BOOL get_id_from_rid(char *domain_name, uint32 rid, int *id,
77 /* Check if rid is present in database */
79 slprintf(keystr, sizeof(keystr), "%s/%d", domain_name, rid);
82 key.dsize = strlen(keystr) + 1;
84 data = tdb_fetch(idmap_tdb, key);
90 /* Parse and return existing uid */
92 fstrcpy(scanstr, isgroup ? "GID" : "UID");
93 fstrcat(scanstr, " %d");
95 if (sscanf(data.dptr, scanstr, &the_id) == 1) {
110 /* Allocate a new id for this rid */
112 if (id && allocate_id(id, isgroup)) {
117 slprintf(keystr2, sizeof(keystr2), "%s %d", isgroup ? "GID" :
121 data.dsize = strlen(keystr2) + 1;
123 tdb_store(idmap_tdb, key, data, TDB_REPLACE);
124 tdb_store(idmap_tdb, data, key, TDB_REPLACE);
133 /* Get a uid from a user rid */
135 BOOL winbindd_idmap_get_uid_from_rid(char *domain_name, uint32 user_rid,
138 return get_id_from_rid(domain_name, user_rid, uid, False);
141 /* Get a gid from a group rid */
143 BOOL winbindd_idmap_get_gid_from_rid(char *domain_name, uint32 group_rid,
146 return get_id_from_rid(domain_name, group_rid, gid, True);
149 BOOL get_rid_from_id(int id, uint32 *rid, struct winbindd_domain **domain,
156 slprintf(keystr, sizeof(keystr), "%s %d", isgroup ? "GID" : "UID", id);
159 key.dsize = strlen(keystr) + 1;
161 data = tdb_fetch(idmap_tdb, key);
168 if (next_token(&p, domain_name, "/", sizeof(fstring))) {
177 *domain = find_domain_from_name(domain_name);
189 /* Get a user rid from a uid */
191 BOOL winbindd_idmap_get_rid_from_uid(uid_t uid, uint32 *user_rid,
192 struct winbindd_domain **domain)
194 return get_rid_from_id((int)uid, user_rid, domain, False);
197 /* Get a group rid from a gid */
199 BOOL winbindd_idmap_get_rid_from_gid(gid_t gid, uint32 *group_rid,
200 struct winbindd_domain **domain)
202 return get_rid_from_id((int)gid, group_rid, domain, True);
205 /* Initialise idmap database */
207 BOOL winbindd_idmap_init(void)
211 if (!(idmap_tdb = tdb_open(lock_path("winbindd_idmap.tdb"), 0,
212 TDB_NOLOCK | TDB_NOMMAP,
213 O_RDWR | O_CREAT, 0600))) {
214 DEBUG(0, ("Unable to open idmap database\n"));
218 /* Create high water marks for group and user id */
220 if (tdb_get_int(idmap_tdb, HWM_USER) == -1) {
221 if (tdb_store_int(idmap_tdb, HWM_USER, server_state.uid_low) == -1) {
222 DEBUG(0, ("Unable to initialise user hwm in idmap database\n"));
227 if (tdb_get_int(idmap_tdb, HWM_GROUP) == -1) {
228 if (tdb_store_int(idmap_tdb, HWM_GROUP, server_state.gid_low) == -1) {
229 DEBUG(0, ("Unable to initialise group hwm in idmap database\n"));