idmap rewrite
[samba.git] / source3 / winbindd / winbindd_idmap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Async helpers for blocking functions
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Gerald Carter 2006
8    Copyright (C) Simo Sorce 2007
9
10    The helpers always consist of three functions:
11
12    * A request setup function that takes the necessary parameters together
13      with a continuation function that is to be called upon completion
14
15    * A private continuation function that is internal only. This is to be
16      called by the lower-level functions in do_async(). Its only task is to
17      properly call the continuation function named above.
18
19    * A worker function that is called inside the appropriate child process.
20
21    This program is free software; you can redistribute it and/or modify
22    it under the terms of the GNU General Public License as published by
23    the Free Software Foundation; either version 3 of the License, or
24    (at your option) any later version.
25
26    This program is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29    GNU General Public License for more details.
30
31    You should have received a copy of the GNU General Public License
32    along with this program.  If not, see <http://www.gnu.org/licenses/>.
33 */
34
35 #include "includes.h"
36 #include "winbindd.h"
37
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_WINBIND
40
41 static const struct winbindd_child_dispatch_table idmap_dispatch_table[];
42
43 static struct winbindd_child static_idmap_child;
44
45 void init_idmap_child(void)
46 {
47         setup_child(&static_idmap_child,
48                     idmap_dispatch_table,
49                     "log.winbindd", "idmap");
50 }
51
52 struct winbindd_child *idmap_child(void)
53 {
54         return &static_idmap_child;
55 }
56
57 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
58                                    struct winbindd_response *response,
59                                    void *c, void *private_data)
60 {
61         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
62
63         if (!success) {
64                 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
65                 cont(private_data, False);
66                 return;
67         }
68
69         if (response->result != WINBINDD_OK) {
70                 DEBUG(5, ("idmap_set_mapping returned an error\n"));
71                 cont(private_data, False);
72                 return;
73         }
74
75         cont(private_data, True);
76 }
77
78 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
79                              void (*cont)(void *private_data, bool success),
80                              void *private_data)
81 {
82         struct winbindd_request request;
83         ZERO_STRUCT(request);
84         request.cmd = WINBINDD_DUAL_SET_MAPPING;
85         request.data.dual_idmapset.id = map->xid.id;
86         request.data.dual_idmapset.type = map->xid.type;
87         sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
88
89         do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
90                  (void *)cont, private_data);
91 }
92
93 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
94                                             struct winbindd_cli_state *state)
95 {
96         struct id_map map;
97         DOM_SID sid;
98         NTSTATUS result;
99
100         DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
101
102         if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
103                 return WINBINDD_ERROR;
104
105         map.sid = &sid;
106         map.xid.id = state->request.data.dual_idmapset.id;
107         map.xid.type = state->request.data.dual_idmapset.type;
108         map.status = ID_MAPPED;
109
110         result = idmap_set_mapping(&map);
111         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
112 }
113
114 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
115                                    struct winbindd_response *response,
116                                    void *c, void *private_data)
117 {
118         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
119
120         if (!success) {
121                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
122                 cont(private_data, False);
123                 return;
124         }
125
126         if (response->result != WINBINDD_OK) {
127                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
128                 cont(private_data, False);
129                 return;
130         }
131
132         cont(private_data, True);
133 }
134
135 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
136                              void (*cont)(void *private_data, bool success),
137                              void *private_data)
138 {
139         struct winbindd_request request;
140         ZERO_STRUCT(request);
141         request.cmd = WINBINDD_DUAL_SET_HWM;
142         request.data.dual_idmapset.id = xid->id;
143         request.data.dual_idmapset.type = xid->type;
144
145         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
146                  (void *)cont, private_data);
147 }
148
149 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
150                                             struct winbindd_cli_state *state)
151 {
152         struct unixid xid;
153         NTSTATUS result;
154
155         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
156
157         xid.id = state->request.data.dual_idmapset.id;
158         xid.type = state->request.data.dual_idmapset.type;
159
160         switch (xid.type) {
161         case ID_TYPE_UID:
162                 result = idmap_set_uid_hwm(&xid);
163                 break;
164         case ID_TYPE_GID:
165                 result = idmap_set_gid_hwm(&xid);
166                 break;
167         default:
168                 return WINBINDD_ERROR;
169         }
170         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
171 }
172
173 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
174                                struct winbindd_response *response,
175                                void *c, void *private_data)
176 {
177         void (*cont)(void *priv, bool succ, uid_t uid) =
178                 (void (*)(void *, bool, uid_t))c;
179
180         if (!success) {
181                 DEBUG(5, ("Could not trigger sid2uid\n"));
182                 cont(private_data, False, 0);
183                 return;
184         }
185
186         if (response->result != WINBINDD_OK) {
187                 DEBUG(5, ("sid2uid returned an error\n"));
188                 cont(private_data, False, 0);
189                 return;
190         }
191
192         cont(private_data, True, response->data.uid);
193 }
194
195 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
196                          void (*cont)(void *private_data, bool success, uid_t uid),
197                          void *private_data)
198 {
199         struct winbindd_request request;
200         struct winbindd_domain *domain;
201
202         ZERO_STRUCT(request);
203         request.cmd = WINBINDD_DUAL_SID2UID;
204
205         domain = find_domain_from_sid(sid);
206
207         if (domain != NULL) {
208                 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
209                            "have_idmap_config = %d\n", domain->name,
210                            (int)domain->have_idmap_config));
211
212         }
213         else {
214                 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
215                            "%s\n", sid_string_dbg(sid)));
216         }
217
218         if ((domain != NULL) && (domain->have_idmap_config)) {
219                 fstrcpy(request.domain_name, domain->name);
220         }
221
222         sid_to_fstring(request.data.dual_sid2id.sid, sid);
223         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
224                  (void *)cont, private_data);
225 }
226
227 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
228                                            struct winbindd_cli_state *state)
229 {
230         DOM_SID sid;
231         NTSTATUS result;
232
233         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
234                   state->request.data.dual_sid2id.sid));
235
236         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
237                 DEBUG(1, ("Could not get convert sid %s from string\n",
238                           state->request.data.dual_sid2id.sid));
239                 return WINBINDD_ERROR;
240         }
241
242         result = idmap_sid_to_uid(state->request.domain_name, &sid,
243                                   &state->response.data.uid);
244
245         DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
246                    NT_STATUS_V(result), sid_string_dbg(&sid),
247                    state->response.data.uid));
248
249         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
250 }
251
252 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
253                                struct winbindd_response *response,
254                                void *c, void *private_data)
255 {
256         void (*cont)(void *priv, bool succ, gid_t gid) =
257                 (void (*)(void *, bool, gid_t))c;
258
259         if (!success) {
260                 DEBUG(5, ("Could not trigger sid2gid\n"));
261                 cont(private_data, False, 0);
262                 return;
263         }
264
265         if (response->result != WINBINDD_OK) {
266                 DEBUG(5, ("sid2gid returned an error\n"));
267                 cont(private_data, False, 0);
268                 return;
269         }
270
271         cont(private_data, True, response->data.gid);
272 }
273
274 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
275                          void (*cont)(void *private_data, bool success, gid_t gid),
276                          void *private_data)
277 {
278         struct winbindd_request request;
279         struct winbindd_domain *domain;
280
281         ZERO_STRUCT(request);
282         request.cmd = WINBINDD_DUAL_SID2GID;
283
284         domain = find_domain_from_sid(sid);
285         if ((domain != NULL) && (domain->have_idmap_config)) {
286                 fstrcpy(request.domain_name, domain->name);
287         }
288
289         sid_to_fstring(request.data.dual_sid2id.sid, sid);
290
291         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
292                 request.data.dual_sid2id.sid));
293
294         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
295                  (void *)cont, private_data);
296 }
297
298 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
299                                            struct winbindd_cli_state *state)
300 {
301         DOM_SID sid;
302         NTSTATUS result;
303
304         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
305                   state->request.data.dual_sid2id.sid));
306
307         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
308                 DEBUG(1, ("Could not get convert sid %s from string\n",
309                           state->request.data.dual_sid2id.sid));
310                 return WINBINDD_ERROR;
311         }
312
313         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
314
315         result = idmap_sid_to_gid(state->request.domain_name, &sid,
316                                   &state->response.data.gid);
317
318         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
319                    NT_STATUS_V(result), sid_string_dbg(&sid),
320                    state->response.data.gid));
321
322         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
323 }
324
325 /* The following uid2sid/gid2sid functions has been contributed by
326  * Keith Reynolds <Keith.Reynolds@centrify.com> */
327
328 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
329                                   struct winbindd_response *response,
330                                   void *c, void *private_data)
331 {
332         void (*cont)(void *priv, bool succ, const char *sid) =
333                 (void (*)(void *, bool, const char *))c;
334
335         if (!success) {
336                 DEBUG(5, ("Could not trigger uid2sid\n"));
337                 cont(private_data, False, NULL);
338                 return;
339         }
340
341         if (response->result != WINBINDD_OK) {
342                 DEBUG(5, ("uid2sid returned an error\n"));
343                 cont(private_data, False, NULL);
344                 return;
345         }
346
347         cont(private_data, True, response->data.sid.sid);
348 }
349
350 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
351                             void (*cont)(void *private_data, bool success, const char *sid),
352                             void *private_data)
353 {
354         struct winbindd_domain *domain;
355         struct winbindd_request request;
356
357         ZERO_STRUCT(request);
358         request.cmd = WINBINDD_DUAL_UID2SID;
359         request.data.uid = uid;
360
361         for (domain = domain_list(); domain != NULL; domain = domain->next) {
362                 if (domain->have_idmap_config
363                     && (uid >= domain->id_range_low)
364                     && (uid <= domain->id_range_high)) {
365                         fstrcpy(request.domain_name, domain->name);
366                 }
367         }
368
369         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
370                  (void *)cont, private_data);
371 }
372
373 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
374                                            struct winbindd_cli_state *state)
375 {
376         DOM_SID sid;
377         NTSTATUS result;
378
379         DEBUG(3,("[%5lu]: uid to sid %lu\n",
380                  (unsigned long)state->pid,
381                  (unsigned long) state->request.data.uid));
382
383         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
384         result = idmap_uid_to_sid(state->request.domain_name, &sid,
385                                   state->request.data.uid);
386
387         if (NT_STATUS_IS_OK(result)) {
388                 sid_to_fstring(state->response.data.sid.sid, &sid);
389                 state->response.data.sid.type = SID_NAME_USER;
390                 return WINBINDD_OK;
391         }
392
393         return WINBINDD_ERROR;
394 }
395
396 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
397                                   struct winbindd_response *response,
398                                   void *c, void *private_data)
399 {
400         void (*cont)(void *priv, bool succ, const char *sid) =
401                 (void (*)(void *, bool, const char *))c;
402
403         if (!success) {
404                 DEBUG(5, ("Could not trigger gid2sid\n"));
405                 cont(private_data, False, NULL);
406                 return;
407         }
408
409         if (response->result != WINBINDD_OK) {
410                 DEBUG(5, ("gid2sid returned an error\n"));
411                 cont(private_data, False, NULL);
412                 return;
413         }
414
415         cont(private_data, True, response->data.sid.sid);
416 }
417
418 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
419                             void (*cont)(void *private_data, bool success, const char *sid),
420                             void *private_data)
421 {
422         struct winbindd_domain *domain;
423         struct winbindd_request request;
424
425         ZERO_STRUCT(request);
426         request.cmd = WINBINDD_DUAL_GID2SID;
427         request.data.gid = gid;
428
429         for (domain = domain_list(); domain != NULL; domain = domain->next) {
430                 if (domain->have_idmap_config
431                     && (gid >= domain->id_range_low)
432                     && (gid <= domain->id_range_high)) {
433                         fstrcpy(request.domain_name, domain->name);
434                 }
435         }
436
437         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
438                  (void *)cont, private_data);
439 }
440
441 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
442                                            struct winbindd_cli_state *state)
443 {
444         DOM_SID sid;
445         NTSTATUS result;
446
447         DEBUG(3,("[%5lu]: gid %lu to sid\n",
448                 (unsigned long)state->pid,
449                 (unsigned long) state->request.data.gid));
450
451         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
452         result = idmap_gid_to_sid(state->request.domain_name, &sid,
453                                   state->request.data.gid);
454
455         if (NT_STATUS_IS_OK(result)) {
456                 sid_to_fstring(state->response.data.sid.sid, &sid);
457                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
458                            (unsigned long)state->pid,
459                            state->response.data.sid.sid));
460                 state->response.data.sid.type = SID_NAME_DOM_GRP;
461                 return WINBINDD_OK;
462         }
463
464         return WINBINDD_ERROR;
465 }
466
467 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
468         {
469                 .name           = "DUAL_SID2UID",
470                 .struct_cmd     = WINBINDD_DUAL_SID2UID,
471                 .struct_fn      = winbindd_dual_sid2uid,
472         },{
473                 .name           = "DUAL_SID2GID",
474                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
475                 .struct_fn      = winbindd_dual_sid2gid,
476         },{
477                 .name           = "DUAL_UID2SID",
478                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
479                 .struct_fn      = winbindd_dual_uid2sid,
480         },{
481                 .name           = "DUAL_GID2SID",
482                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
483                 .struct_fn      = winbindd_dual_gid2sid,
484         },{
485                 .name           = "DUAL_SET_MAPPING",
486                 .struct_cmd     = WINBINDD_DUAL_SET_MAPPING,
487                 .struct_fn      = winbindd_dual_set_mapping,
488         },{
489                 .name           = "DUAL_SET_HWMS",
490                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
491                 .struct_fn      = winbindd_dual_set_hwm,
492         },{
493                 .name           = "ALLOCATE_UID",
494                 .struct_cmd     = WINBINDD_ALLOCATE_UID,
495                 .struct_fn      = winbindd_dual_allocate_uid,
496         },{
497                 .name           = "ALLOCATE_GID",
498                 .struct_cmd     = WINBINDD_ALLOCATE_GID,
499                 .struct_fn      = winbindd_dual_allocate_gid,
500         },{
501                 .name           = NULL,
502         }
503 };