Make winbindd_cli_state->request a pointer instead of a struct member
[ira/wip.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_remove_mapping_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_remove_mapping\n"));
122                 cont(private_data, False);
123                 return;
124         }
125
126         if (response->result != WINBINDD_OK) {
127                 DEBUG(5, ("idmap_remove_mapping returned an error\n"));
128                 cont(private_data, False);
129                 return;
130         }
131
132         cont(private_data, True);
133 }
134
135 void winbindd_remove_mapping_async(TALLOC_CTX *mem_ctx,
136                              const struct id_map *map,
137                              void (*cont)(void *private_data, bool success),
138                              void *private_data)
139 {
140         struct winbindd_request request;
141         ZERO_STRUCT(request);
142         request.cmd = WINBINDD_DUAL_REMOVE_MAPPING;
143         request.data.dual_idmapset.id = map->xid.id;
144         request.data.dual_idmapset.type = map->xid.type;
145         sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
146
147         do_async(mem_ctx, idmap_child(), &request, winbindd_remove_mapping_recv,
148                  (void *)cont, private_data);
149 }
150
151 enum winbindd_result winbindd_dual_remove_mapping(
152                                             struct winbindd_domain *domain,
153                                             struct winbindd_cli_state *state)
154 {
155         struct id_map map;
156         DOM_SID sid;
157         NTSTATUS result;
158
159         DEBUG(3, ("[%5lu]: dual_idmapremove\n", (unsigned long)state->pid));
160
161         if (!string_to_sid(&sid, state->request->data.dual_idmapset.sid))
162                 return WINBINDD_ERROR;
163
164         map.sid = &sid;
165         map.xid.id = state->request->data.dual_idmapset.id;
166         map.xid.type = state->request->data.dual_idmapset.type;
167         map.status = ID_MAPPED;
168
169         result = idmap_remove_mapping(&map);
170         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
171 }
172
173 static void winbindd_set_hwm_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) = (void (*)(void *, bool))c;
178
179         if (!success) {
180                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
181                 cont(private_data, False);
182                 return;
183         }
184
185         if (response->result != WINBINDD_OK) {
186                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
187                 cont(private_data, False);
188                 return;
189         }
190
191         cont(private_data, True);
192 }
193
194 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
195                              void (*cont)(void *private_data, bool success),
196                              void *private_data)
197 {
198         struct winbindd_request request;
199         ZERO_STRUCT(request);
200         request.cmd = WINBINDD_DUAL_SET_HWM;
201         request.data.dual_idmapset.id = xid->id;
202         request.data.dual_idmapset.type = xid->type;
203
204         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
205                  (void *)cont, private_data);
206 }
207
208 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
209                                             struct winbindd_cli_state *state)
210 {
211         struct unixid xid;
212         NTSTATUS result;
213
214         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
215
216         xid.id = state->request->data.dual_idmapset.id;
217         xid.type = state->request->data.dual_idmapset.type;
218
219         switch (xid.type) {
220         case ID_TYPE_UID:
221                 result = idmap_set_uid_hwm(&xid);
222                 break;
223         case ID_TYPE_GID:
224                 result = idmap_set_gid_hwm(&xid);
225                 break;
226         default:
227                 return WINBINDD_ERROR;
228         }
229         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
230 }
231
232 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
233                                struct winbindd_response *response,
234                                void *c, void *private_data)
235 {
236         void (*cont)(void *priv, bool succ, uid_t uid) =
237                 (void (*)(void *, bool, uid_t))c;
238
239         if (!success) {
240                 DEBUG(5, ("Could not trigger sid2uid\n"));
241                 cont(private_data, False, 0);
242                 return;
243         }
244
245         if (response->result != WINBINDD_OK) {
246                 DEBUG(5, ("sid2uid returned an error\n"));
247                 cont(private_data, False, 0);
248                 return;
249         }
250
251         cont(private_data, True, response->data.uid);
252 }
253
254 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
255                          void (*cont)(void *private_data, bool success, uid_t uid),
256                          void *private_data)
257 {
258         struct winbindd_request request;
259         struct winbindd_domain *domain;
260
261         ZERO_STRUCT(request);
262         request.cmd = WINBINDD_DUAL_SID2UID;
263
264         domain = find_domain_from_sid(sid);
265
266         if (domain != NULL) {
267                 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
268                            "have_idmap_config = %d\n", domain->name,
269                            (int)domain->have_idmap_config));
270
271         }
272         else {
273                 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
274                            "%s\n", sid_string_dbg(sid)));
275         }
276
277         if ((domain != NULL) && (domain->have_idmap_config)) {
278                 fstrcpy(request.domain_name, domain->name);
279         }
280
281         sid_to_fstring(request.data.dual_sid2id.sid, sid);
282         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
283                  (void *)cont, private_data);
284 }
285
286 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
287                                            struct winbindd_cli_state *state)
288 {
289         DOM_SID sid;
290         NTSTATUS result;
291
292         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
293                   state->request->data.dual_sid2id.sid));
294
295         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
296                 DEBUG(1, ("Could not get convert sid %s from string\n",
297                           state->request->data.dual_sid2id.sid));
298                 return WINBINDD_ERROR;
299         }
300
301         result = idmap_sid_to_uid(state->request->domain_name, &sid,
302                                   &state->response.data.uid);
303
304         DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
305                    NT_STATUS_V(result), sid_string_dbg(&sid),
306                    (unsigned int)state->response.data.uid));
307
308         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
309 }
310
311 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
312                                struct winbindd_response *response,
313                                void *c, void *private_data)
314 {
315         void (*cont)(void *priv, bool succ, gid_t gid) =
316                 (void (*)(void *, bool, gid_t))c;
317
318         if (!success) {
319                 DEBUG(5, ("Could not trigger sid2gid\n"));
320                 cont(private_data, False, 0);
321                 return;
322         }
323
324         if (response->result != WINBINDD_OK) {
325                 DEBUG(5, ("sid2gid returned an error\n"));
326                 cont(private_data, False, 0);
327                 return;
328         }
329
330         cont(private_data, True, response->data.gid);
331 }
332
333 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
334                          void (*cont)(void *private_data, bool success, gid_t gid),
335                          void *private_data)
336 {
337         struct winbindd_request request;
338         struct winbindd_domain *domain;
339
340         ZERO_STRUCT(request);
341         request.cmd = WINBINDD_DUAL_SID2GID;
342
343         domain = find_domain_from_sid(sid);
344         if ((domain != NULL) && (domain->have_idmap_config)) {
345                 fstrcpy(request.domain_name, domain->name);
346         }
347
348         sid_to_fstring(request.data.dual_sid2id.sid, sid);
349
350         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
351                 request.data.dual_sid2id.sid));
352
353         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
354                  (void *)cont, private_data);
355 }
356
357 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
358                                            struct winbindd_cli_state *state)
359 {
360         DOM_SID sid;
361         NTSTATUS result;
362
363         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
364                   state->request->data.dual_sid2id.sid));
365
366         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
367                 DEBUG(1, ("Could not get convert sid %s from string\n",
368                           state->request->data.dual_sid2id.sid));
369                 return WINBINDD_ERROR;
370         }
371
372         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
373
374         result = idmap_sid_to_gid(state->request->domain_name, &sid,
375                                   &state->response.data.gid);
376
377         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
378                    NT_STATUS_V(result), sid_string_dbg(&sid),
379                    (unsigned int)state->response.data.gid));
380
381         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
382 }
383
384 /* The following uid2sid/gid2sid functions has been contributed by
385  * Keith Reynolds <Keith.Reynolds@centrify.com> */
386
387 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
388                                   struct winbindd_response *response,
389                                   void *c, void *private_data)
390 {
391         void (*cont)(void *priv, bool succ, const char *sid) =
392                 (void (*)(void *, bool, const char *))c;
393
394         if (!success) {
395                 DEBUG(5, ("Could not trigger uid2sid\n"));
396                 cont(private_data, False, NULL);
397                 return;
398         }
399
400         if (response->result != WINBINDD_OK) {
401                 DEBUG(5, ("uid2sid returned an error\n"));
402                 cont(private_data, False, NULL);
403                 return;
404         }
405
406         cont(private_data, True, response->data.sid.sid);
407 }
408
409 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
410                             void (*cont)(void *private_data, bool success, const char *sid),
411                             void *private_data)
412 {
413         struct winbindd_domain *domain;
414         struct winbindd_request request;
415
416         ZERO_STRUCT(request);
417         request.cmd = WINBINDD_DUAL_UID2SID;
418         request.data.uid = uid;
419
420         for (domain = domain_list(); domain != NULL; domain = domain->next) {
421                 if (domain->have_idmap_config
422                     && (uid >= domain->id_range_low)
423                     && (uid <= domain->id_range_high)) {
424                         fstrcpy(request.domain_name, domain->name);
425                 }
426         }
427
428         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
429                  (void *)cont, private_data);
430 }
431
432 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
433                                            struct winbindd_cli_state *state)
434 {
435         DOM_SID sid;
436         NTSTATUS result;
437
438         DEBUG(3,("[%5lu]: uid to sid %lu\n",
439                  (unsigned long)state->pid,
440                  (unsigned long) state->request->data.uid));
441
442         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
443         result = idmap_uid_to_sid(state->request->domain_name, &sid,
444                                   state->request->data.uid);
445
446         if (NT_STATUS_IS_OK(result)) {
447                 sid_to_fstring(state->response.data.sid.sid, &sid);
448                 state->response.data.sid.type = SID_NAME_USER;
449                 return WINBINDD_OK;
450         }
451
452         return WINBINDD_ERROR;
453 }
454
455 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
456                                   struct winbindd_response *response,
457                                   void *c, void *private_data)
458 {
459         void (*cont)(void *priv, bool succ, const char *sid) =
460                 (void (*)(void *, bool, const char *))c;
461
462         if (!success) {
463                 DEBUG(5, ("Could not trigger gid2sid\n"));
464                 cont(private_data, False, NULL);
465                 return;
466         }
467
468         if (response->result != WINBINDD_OK) {
469                 DEBUG(5, ("gid2sid returned an error\n"));
470                 cont(private_data, False, NULL);
471                 return;
472         }
473
474         cont(private_data, True, response->data.sid.sid);
475 }
476
477 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
478                             void (*cont)(void *private_data, bool success, const char *sid),
479                             void *private_data)
480 {
481         struct winbindd_domain *domain;
482         struct winbindd_request request;
483
484         ZERO_STRUCT(request);
485         request.cmd = WINBINDD_DUAL_GID2SID;
486         request.data.gid = gid;
487
488         for (domain = domain_list(); domain != NULL; domain = domain->next) {
489                 if (domain->have_idmap_config
490                     && (gid >= domain->id_range_low)
491                     && (gid <= domain->id_range_high)) {
492                         fstrcpy(request.domain_name, domain->name);
493                 }
494         }
495
496         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
497                  (void *)cont, private_data);
498 }
499
500 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
501                                            struct winbindd_cli_state *state)
502 {
503         DOM_SID sid;
504         NTSTATUS result;
505
506         DEBUG(3,("[%5lu]: gid %lu to sid\n",
507                 (unsigned long)state->pid,
508                 (unsigned long) state->request->data.gid));
509
510         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
511         result = idmap_gid_to_sid(state->request->domain_name, &sid,
512                                   state->request->data.gid);
513
514         if (NT_STATUS_IS_OK(result)) {
515                 sid_to_fstring(state->response.data.sid.sid, &sid);
516                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
517                            (unsigned long)state->pid,
518                            state->response.data.sid.sid));
519                 state->response.data.sid.type = SID_NAME_DOM_GRP;
520                 return WINBINDD_OK;
521         }
522
523         return WINBINDD_ERROR;
524 }
525
526 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
527         {
528                 .name           = "DUAL_SID2UID",
529                 .struct_cmd     = WINBINDD_DUAL_SID2UID,
530                 .struct_fn      = winbindd_dual_sid2uid,
531         },{
532                 .name           = "DUAL_SID2GID",
533                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
534                 .struct_fn      = winbindd_dual_sid2gid,
535         },{
536                 .name           = "DUAL_UID2SID",
537                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
538                 .struct_fn      = winbindd_dual_uid2sid,
539         },{
540                 .name           = "DUAL_GID2SID",
541                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
542                 .struct_fn      = winbindd_dual_gid2sid,
543         },{
544                 .name           = "DUAL_SET_MAPPING",
545                 .struct_cmd     = WINBINDD_DUAL_SET_MAPPING,
546                 .struct_fn      = winbindd_dual_set_mapping,
547         },{
548                 .name           = "DUAL_REMOVE_MAPPING",
549                 .struct_cmd     = WINBINDD_DUAL_REMOVE_MAPPING,
550                 .struct_fn      = winbindd_dual_remove_mapping,
551         },{
552                 .name           = "DUAL_SET_HWMS",
553                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
554                 .struct_fn      = winbindd_dual_set_hwm,
555         },{
556                 .name           = "ALLOCATE_UID",
557                 .struct_cmd     = WINBINDD_ALLOCATE_UID,
558                 .struct_fn      = winbindd_dual_allocate_uid,
559         },{
560                 .name           = "ALLOCATE_GID",
561                 .struct_cmd     = WINBINDD_ALLOCATE_GID,
562                 .struct_fn      = winbindd_dual_allocate_gid,
563         },{
564                 .name           = NULL,
565         }
566 };