iAdditional files for winbind merge.
[samba.git] / source3 / nsswitch / winbindd_idmap.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind daemon - user related function
6
7    Copyright (C) Tim Potter 2000
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "winbindd.h"
25
26 /* High water mark keys */
27
28 #define HWM_GROUP  "GROUP HWM"
29 #define HWM_USER   "USER HWM"
30
31 /* Globals */
32
33 static TDB_CONTEXT *idmap_tdb;
34
35 /* Allocate either a user or group id from the pool */
36
37 static BOOL allocate_id(int *id, BOOL isgroup)
38 {
39     int hwm;
40
41     /* Get current high water mark */
42
43     if ((hwm = tdb_fetch_int(idmap_tdb, 
44                              isgroup ? HWM_GROUP : HWM_USER)) == -1) {
45         return False;
46     }
47
48     /* Return next available uid in list */
49
50     if ((isgroup && (hwm > server_state.gid_high)) ||
51         (!isgroup && (hwm > server_state.uid_high))) {
52         DEBUG(0, ("winbind %sid range full!\n", isgroup ? "g" : "u"));
53         return False;
54     }
55
56     if (id) {
57         *id = hwm;
58     }
59
60     hwm++;
61
62     /* Store new high water mark */
63
64     tdb_store_int(idmap_tdb, isgroup ? HWM_GROUP : HWM_USER, hwm);
65
66     return True;
67 }
68
69 /* Get an id from a rid */
70
71 static BOOL get_id_from_rid(char *domain_name, uint32 rid, int *id,
72                             BOOL isgroup)
73 {
74     TDB_DATA data, key;
75     fstring keystr;
76     BOOL result = False;
77
78     /* Check if rid is present in database */
79
80     slprintf(keystr, sizeof(keystr), "%s/%d", domain_name, rid);
81     
82     key.dptr = keystr;
83     key.dsize = strlen(keystr) + 1;
84
85     data = tdb_fetch(idmap_tdb, key);
86
87     if (data.dptr) {
88         fstring scanstr;
89         int the_id;
90
91         /* Parse and return existing uid */
92
93         fstrcpy(scanstr, isgroup ? "GID" : "UID");
94         fstrcat(scanstr, " %d");
95
96         if (sscanf(data.dptr, scanstr, &the_id) == 1) {
97
98             /* Store uid */
99
100             if (id) {
101                 *id = the_id;
102             }
103
104             result = True;
105         }
106
107         free(data.dptr);
108
109     } else {
110
111         /* Allocate a new id for this rid */
112
113         if (id && allocate_id(id, isgroup)) {
114             fstring keystr2;
115
116             /* Store new id */
117             
118             slprintf(keystr2, sizeof(keystr2), "%s %d", isgroup ? "GID" :
119                      "UID", *id);
120
121             data.dptr = keystr2;
122             data.dsize = strlen(keystr2) + 1;
123
124             tdb_store(idmap_tdb, key, data, TDB_REPLACE);
125             tdb_store(idmap_tdb, data, key, TDB_REPLACE);
126
127             result = True;
128         }
129     }
130
131     return result;
132 }
133
134 /* Get a uid from a user rid */
135
136 BOOL winbindd_idmap_get_uid_from_rid(char *domain_name, uint32 user_rid, 
137                                      uid_t *uid)
138 {
139     return get_id_from_rid(domain_name, user_rid, uid, False);
140 }
141
142 /* Get a gid from a group rid */
143
144 BOOL winbindd_idmap_get_gid_from_rid(char *domain_name, uint32 group_rid, 
145                                      gid_t *gid)
146 {
147     return get_id_from_rid(domain_name, group_rid, gid, True);
148 }
149
150 BOOL get_rid_from_id(int id, uint32 *rid, struct winbindd_domain **domain,
151                      BOOL isgroup)
152 {
153     TDB_DATA key, data;
154     fstring keystr;
155     BOOL result = False;
156
157     slprintf(keystr, sizeof(keystr), "%s %d", isgroup ? "GID" : "UID", id);
158
159     key.dptr = keystr;
160     key.dsize = strlen(keystr) + 1;
161
162     data = tdb_fetch(idmap_tdb, key);
163
164     if (data.dptr) {
165         char *p = data.dptr;
166         fstring domain_name;
167         uint32 the_rid;
168
169         if (next_token(&p, domain_name, "/", sizeof(fstring))) {
170
171             the_rid = atoi(p);
172
173             if (rid) {
174                 *rid = the_rid;
175             }
176
177             if (domain) {
178                 *domain = find_domain_from_name(domain_name);
179                 if (*domain == NULL) {
180                         DEBUG(1, ("unknown domain %s for rid %d\n",
181                                   domain_name, the_rid));
182                         result = False;
183                         goto done;
184                 }
185             }
186
187             result = True;
188         }
189     done:            
190         free(data.dptr);
191     }
192
193     return result;
194 }
195
196 /* Get a user rid from a uid */
197
198 BOOL winbindd_idmap_get_rid_from_uid(uid_t uid, uint32 *user_rid,
199                                      struct winbindd_domain **domain)
200 {
201     return get_rid_from_id((int)uid, user_rid, domain, False);
202 }
203
204 /* Get a group rid from a gid */
205
206 BOOL winbindd_idmap_get_rid_from_gid(gid_t gid, uint32 *group_rid, 
207                                      struct winbindd_domain **domain)
208 {
209     return get_rid_from_id((int)gid, group_rid, domain, True);
210 }
211
212 /* Initialise idmap database */
213
214 BOOL winbindd_idmap_init(void)
215 {
216     /* Open tdb cache */
217
218     if (!(idmap_tdb = tdb_open(lock_path("winbindd_idmap.tdb"), 0,
219                                TDB_NOLOCK, O_RDWR | O_CREAT, 0600))) {
220         DEBUG(0, ("Unable to open idmap database\n"));
221         return False;
222     }
223
224      /* Create high water marks for group and user id */
225
226     if (tdb_fetch_int(idmap_tdb, HWM_USER) == -1) {
227         if (tdb_store_int(idmap_tdb, HWM_USER, server_state.uid_low) == -1) {
228             DEBUG(0, ("Unable to initialise user hwm in idmap database\n"));
229             return False;
230         }
231     }
232
233     if (tdb_fetch_int(idmap_tdb, HWM_GROUP) == -1) {
234         if (tdb_store_int(idmap_tdb, HWM_GROUP, server_state.gid_low) == -1) {
235             DEBUG(0, ("Unable to initialise group hwm in idmap database\n"));
236             return False;
237         }
238     }
239
240     return True;   
241 }
242
243 /* Dump status information to log file.  Display different stuff based on
244    the debug level:
245
246    Debug Level        Information Displayed
247    =================================================================
248    0                  Percentage of [ug]id range allocated
249    0                  High water marks (next allocated ids)
250 */
251
252 #define DUMP_INFO 0
253
254 void winbindd_idmap_dump_status(void)
255 {
256     int user_hwm, group_hwm;
257
258     DEBUG(0, ("Status for winbindd idmap:\n"));
259
260     /* Get current high water marks */
261
262     if ((user_hwm = tdb_fetch_int(idmap_tdb, HWM_USER)) == -1) {
263         DEBUG(DUMP_INFO, ("\tCould not get userid high water mark!\n"));
264     }
265
266     if ((group_hwm = tdb_fetch_int(idmap_tdb, HWM_GROUP)) == -1) {
267         DEBUG(DUMP_INFO, ("\tCould not get groupid high water mark!\n"));
268     }
269
270     /* Display next ids to allocate */
271
272     if (user_hwm != -1) {
273         DEBUG(DUMP_INFO, ("\tNext userid to allocate is %d\n", user_hwm));
274     }
275
276     if (group_hwm != -1) {
277         DEBUG(DUMP_INFO, ("\tNext groupid to allocate is %d\n", group_hwm));
278     }
279
280     /* Display percentage of id range already allocated. */
281
282     if (user_hwm != -1) {
283         int num_users = user_hwm - server_state.uid_low;
284         int total_users = server_state.uid_high - server_state.uid_low;
285
286         DEBUG(DUMP_INFO, ("\tUser id range is %d%% full (%d of %d)\n", 
287                           num_users * 100 / total_users, num_users,
288                           total_users));
289     }
290
291     if (group_hwm != -1) {
292         int num_groups = group_hwm - server_state.gid_low;
293         int total_groups = server_state.gid_high - server_state.gid_low;
294
295         DEBUG(DUMP_INFO, ("\tGroup id range is %d%% full (%d of %d)\n",
296                           num_groups * 100 / total_groups, num_groups,
297                           total_groups));
298     }
299
300     /* Display complete mapping of users and groups to rids */
301 }