wb-ndr: remove unused WINBINDD_DUAL_GID2SID support
[metze/samba/wb-ndr.git] / source / 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 ndr_child_get_idmap_sid2uid(struct winbindd_domain *domain,
58                                         struct winbindd_cli_state *state,
59                                         struct winbind_get_idmap *r)
60 {
61         uid_t uid;
62         NTSTATUS result;
63
64         DEBUG(3, ("sid to uid '%s'\n", sid_string_tos(r->in.req.sid)));
65
66         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
67
68         result = idmap_sid_to_uid(r->in.req.sid, &uid);
69         if (!NT_STATUS_IS_OK(result)) {
70                 DEBUG(1, ("Can't map '%s' to uid: %s\n",
71                         sid_string_tos(r->in.req.sid),
72                         nt_errstr(result)));
73                 r->out.result = WINBIND_STATUS_FOOBAR;
74                 return;
75         }
76
77         DEBUG(10, ("sid %s mapped to uid %u\n",
78                 sid_string_tos(r->in.req.sid), uid));
79
80         r->out.rep->uid = uid;
81         r->out.result = WINBIND_STATUS_OK;
82 }
83
84 static void ndr_child_get_idmap_sid2gid(struct winbindd_domain *domain,
85                                         struct winbindd_cli_state *state,
86                                         struct winbind_get_idmap *r)
87 {
88         gid_t gid;
89         NTSTATUS result;
90
91         DEBUG(3, ("sid to gid '%s'\n", sid_string_tos(r->in.req.sid)));
92
93         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
94
95         result = idmap_sid_to_gid(r->in.req.sid, &gid);
96         if (!NT_STATUS_IS_OK(result)) {
97                 DEBUG(1, ("Can't map '%s' to gid: %s\n",
98                         sid_string_tos(r->in.req.sid),
99                         nt_errstr(result)));
100                 r->out.result = WINBIND_STATUS_FOOBAR;
101                 return;
102         }
103
104         DEBUG(10, ("sid %s mapped to gid %u\n",
105                 sid_string_tos(r->in.req.sid), gid));
106
107         r->out.rep->gid = gid;
108         r->out.result = WINBIND_STATUS_OK;
109 }
110
111 static void ndr_child_get_idmap_uid2sid(struct winbindd_domain *domain,
112                                         struct winbindd_cli_state *state,
113                                         struct winbind_get_idmap *r)
114 {
115         DOM_SID sid;
116         uid_t uid;
117         NTSTATUS result;
118
119         DEBUG(3, ("uid to sid '%llu'\n",
120                 (unsigned long long)r->in.req.uid));
121
122         /* the IDMAP subsystem only knows about uint32_t id's yet */
123         if (r->in.req.uid > UINT32_MAX) {
124                 DEBUG(1, ("Can't map uid '%llu' to sid\n",
125                         (unsigned long long)r->in.req.uid));
126                 r->out.result = WINBIND_STATUS_FOOBAR;
127                 return;
128         }
129
130         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
131
132         uid = r->in.req.uid;
133
134         result = idmap_uid_to_sid(&sid, uid);
135         if (!NT_STATUS_IS_OK(result)) {
136                 DEBUG(1, ("Can't map uid '%u' to sid: %s\n",
137                         uid, nt_errstr(result)));
138                 r->out.result = WINBIND_STATUS_FOOBAR;
139                 return;
140         }
141
142         DEBUG(10, ("uid %u mapped to sid %s\n",
143                 uid, sid_string_tos(&sid)));
144
145         r->out.rep->sid = sid_dup_talloc(r, &sid);
146         if (!r->out.rep->sid) {
147                 r->out.result = WINBIND_STATUS_NO_MEMORY;
148                 return;
149         }
150
151         r->out.result = WINBIND_STATUS_OK;
152 }
153
154 static void ndr_child_get_idmap_gid2sid(struct winbindd_domain *domain,
155                                         struct winbindd_cli_state *state,
156                                         struct winbind_get_idmap *r)
157 {
158         DOM_SID sid;
159         gid_t gid;
160         NTSTATUS result;
161
162         DEBUG(3, ("gid to sid '%llu'\n",
163                 (unsigned long long)r->in.req.gid));
164
165         /* the IDMAP subsystem only knows about uint32_t id's yet */
166         if (r->in.req.gid > UINT32_MAX) {
167                 DEBUG(1, ("Can't map gid '%llu' to sid\n",
168                         (unsigned long long)r->in.req.gid));
169                 r->out.result = WINBIND_STATUS_FOOBAR;
170                 return;
171         }
172
173         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
174         gid = r->in.req.gid;
175
176         result = idmap_gid_to_sid(&sid, gid);
177         if (!NT_STATUS_IS_OK(result)) {
178                 DEBUG(1, ("Can't map gid '%u' to sid: %s\n",
179                         gid, nt_errstr(result)));
180                 r->out.result = WINBIND_STATUS_FOOBAR;
181                 return;
182         }
183
184         DEBUG(10, ("gid %u mapped to sid %s\n",
185                 gid, sid_string_tos(&sid)));
186
187         r->out.rep->sid = sid_dup_talloc(r, &sid);
188         if (!r->out.rep->sid) {
189                 r->out.result = WINBIND_STATUS_NO_MEMORY;
190                 return;
191         }
192
193         r->out.result = WINBIND_STATUS_OK;
194 }
195
196 void winbindd_ndr_child_get_idmap(struct winbindd_domain *domain,
197                                   struct winbindd_cli_state *state)
198 {
199         struct winbind_get_idmap *r;
200
201         r = talloc_get_type_abort(state->c.ndr.r,
202                                   struct winbind_get_idmap);
203
204         switch (*r->in.level) {
205         case WINBIND_IDMAP_LEVEL_SID_TO_UID:
206                 ndr_child_get_idmap_sid2uid(domain, state, r);
207                 return;
208
209         case WINBIND_IDMAP_LEVEL_SID_TO_GID:
210                 ndr_child_get_idmap_sid2gid(domain, state, r);
211                 return;
212
213         case WINBIND_IDMAP_LEVEL_UID_TO_SID:
214                 ndr_child_get_idmap_uid2sid(domain, state, r);
215                 return;
216
217         case WINBIND_IDMAP_LEVEL_GID_TO_SID:
218                 ndr_child_get_idmap_gid2sid(domain, state, r);
219                 return;
220         }
221
222         r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
223         return;
224 }
225
226 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
227                                    struct winbindd_response *response,
228                                    void *c, void *private_data)
229 {
230         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
231
232         if (!success) {
233                 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
234                 cont(private_data, False);
235                 return;
236         }
237
238         if (response->result != WINBINDD_OK) {
239                 DEBUG(5, ("idmap_set_mapping returned an error\n"));
240                 cont(private_data, False);
241                 return;
242         }
243
244         cont(private_data, True);
245 }
246
247 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
248                              void (*cont)(void *private_data, bool success),
249                              void *private_data)
250 {
251         struct winbindd_request request;
252         ZERO_STRUCT(request);
253         request.cmd = WINBINDD_DUAL_SET_MAPPING;
254         request.data.dual_idmapset.id = map->xid.id;
255         request.data.dual_idmapset.type = map->xid.type;
256         sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
257
258         do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
259                  (void *)cont, private_data);
260 }
261
262 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
263                                             struct winbindd_cli_state *state)
264 {
265         struct id_map map;
266         DOM_SID sid;
267         NTSTATUS result;
268
269         DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
270
271         if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
272                 return WINBINDD_ERROR;
273
274         map.sid = &sid;
275         map.xid.id = state->request.data.dual_idmapset.id;
276         map.xid.type = state->request.data.dual_idmapset.type;
277         map.status = ID_MAPPED;
278
279         result = idmap_set_mapping(&map);
280         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
281 }
282
283 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
284                                    struct winbindd_response *response,
285                                    void *c, void *private_data)
286 {
287         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
288
289         if (!success) {
290                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
291                 cont(private_data, False);
292                 return;
293         }
294
295         if (response->result != WINBINDD_OK) {
296                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
297                 cont(private_data, False);
298                 return;
299         }
300
301         cont(private_data, True);
302 }
303
304 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
305                              void (*cont)(void *private_data, bool success),
306                              void *private_data)
307 {
308         struct winbindd_request request;
309         ZERO_STRUCT(request);
310         request.cmd = WINBINDD_DUAL_SET_HWM;
311         request.data.dual_idmapset.id = xid->id;
312         request.data.dual_idmapset.type = xid->type;
313
314         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
315                  (void *)cont, private_data);
316 }
317
318 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
319                                             struct winbindd_cli_state *state)
320 {
321         struct unixid xid;
322         NTSTATUS result;
323
324         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
325
326         xid.id = state->request.data.dual_idmapset.id;
327         xid.type = state->request.data.dual_idmapset.type;
328
329         switch (xid.type) {
330         case ID_TYPE_UID:
331                 result = idmap_set_uid_hwm(&xid);
332                 break;
333         case ID_TYPE_GID:
334                 result = idmap_set_gid_hwm(&xid);
335                 break;
336         default:
337                 return WINBINDD_ERROR;
338         }
339         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
340 }
341
342 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
343                                     struct winbindd_ndr_call *c,
344                                     void *_r,
345                                     void *_cont,
346                                     void *private_data)
347 {
348         void (*cont)(void *priv, bool succ, uid_t) =
349                 (void (*)(void *, bool, uid_t))_cont;
350         struct winbind_get_idmap *r =
351                 talloc_get_type_abort(_r, struct winbind_get_idmap);
352         uid_t uid;
353
354         if (!success) {
355                 DEBUG(5, ("Could not get_idmap(sid2uid)\n"));
356                 TALLOC_FREE(r);
357                 cont(private_data, false, (uid_t)-1);
358                 return;
359         }
360
361         if (r->out.result != WINBIND_STATUS_OK) {
362                 DEBUG(5, ("get_idmap(sid2uid) returned an error:0x%08X\n",
363                         r->out.result));
364                 TALLOC_FREE(r);
365                 cont(private_data, false, (uid_t)-1);
366                 return;
367         }
368
369         if (r->out.rep->uid > UINT32_MAX) {
370                 DEBUG(1, ("get_idmap(sid2uid) returned a 64bit uid %llu\n",
371                         (unsigned long long)r->out.rep->uid));
372                 TALLOC_FREE(r);
373                 cont(private_data, false, (uid_t)-1);
374                 return;
375         }
376
377         uid = r->out.rep->uid;
378         TALLOC_FREE(r);
379         cont(private_data, true, uid);
380 }
381
382 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
383                          void (*cont)(void *private_data, bool success, uid_t uid),
384                          void *private_data)
385 {
386         struct winbind_get_idmap *r = NULL;
387
388         DEBUG(7,("winbindd_sid2uid_async: Resolving %s to a uid\n",
389                 sid_string_tos(sid)));
390
391         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
392         if (!r) goto nomem;
393         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
394         if (!r->in.level) goto nomem;
395
396         *r->in.level    = WINBIND_IDMAP_LEVEL_SID_TO_UID;
397         r->in.req.sid   = discard_const(sid);
398
399         do_async_ndr(mem_ctx, idmap_child(),
400                      NDR_WINBIND_GET_IDMAP, r,
401                      winbindd_sid2uid_recv, r,
402                      (void *)cont, private_data);
403         return;
404 nomem:
405         TALLOC_FREE(r);
406         cont(private_data, false, (uid_t)-1);
407         return;
408 }
409
410 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
411                                     struct winbindd_ndr_call *c,
412                                     void *_r,
413                                     void *_cont,
414                                     void *private_data)
415 {
416         void (*cont)(void *priv, bool succ, gid_t) =
417                 (void (*)(void *, bool, gid_t))_cont;
418         struct winbind_get_idmap *r =
419                 talloc_get_type_abort(_r, struct winbind_get_idmap);
420         gid_t gid;
421
422         if (!success) {
423                 DEBUG(5, ("Could not get_idmap(sid2gid)\n"));
424                 TALLOC_FREE(r);
425                 cont(private_data, false, (gid_t)-1);
426                 return;
427         }
428
429         if (r->out.result != WINBIND_STATUS_OK) {
430                 DEBUG(5, ("get_idmap(sid2gid) returned an error:0x%08X\n",
431                         r->out.result));
432                 TALLOC_FREE(r);
433                 cont(private_data, false, (gid_t)-1);
434                 return;
435         }
436
437         if (r->out.rep->gid > UINT32_MAX) {
438                 DEBUG(1, ("get_idmap(sid2gid) returned a 64bit gid %llu\n",
439                         (unsigned long long)r->out.rep->gid));
440                 TALLOC_FREE(r);
441                 cont(private_data, false, (gid_t)-1);
442                 return;
443         }
444
445         gid = r->out.rep->gid;
446         TALLOC_FREE(r);
447         cont(private_data, true, gid);
448 }
449
450 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
451                          void (*cont)(void *private_data, bool success, gid_t gid),
452                          void *private_data)
453 {
454         struct winbind_get_idmap *r = NULL;
455
456         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
457                 sid_string_tos(sid)));
458
459         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
460         if (!r) goto nomem;
461         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
462         if (!r->in.level) goto nomem;
463
464         *r->in.level    = WINBIND_IDMAP_LEVEL_SID_TO_GID;
465         r->in.req.sid   = discard_const(sid);
466
467         do_async_ndr(mem_ctx, idmap_child(),
468                      NDR_WINBIND_GET_IDMAP, r,
469                      winbindd_sid2gid_recv, r,
470                      (void *)cont, private_data);
471         return;
472 nomem:
473         TALLOC_FREE(r);
474         cont(private_data, false, (gid_t)-1);
475         return;
476 }
477
478 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
479                                     struct winbindd_ndr_call *c,
480                                     void *_r,
481                                     void *_cont,
482                                     void *private_data)
483 {
484         void (*cont)(void *priv, bool succ, const char *sid) =
485                 (void (*)(void *, bool, const char *))_cont;
486         struct winbind_get_idmap *r =
487                 talloc_get_type_abort(_r, struct winbind_get_idmap);
488         const char *sid;
489
490         if (!success) {
491                 DEBUG(5, ("Could not get_idmap(uid2sid)\n"));
492                 TALLOC_FREE(r);
493                 cont(private_data, false, NULL);
494                 return;
495         }
496
497         if (r->out.result != WINBIND_STATUS_OK) {
498                 DEBUG(5, ("get_idmap(uid2sid) returned an error:0x%08X\n",
499                         r->out.result));
500                 TALLOC_FREE(r);
501                 cont(private_data, false, NULL);
502                 return;
503         }
504
505         sid = dom_sid_string(mem_ctx, r->out.rep->sid);
506         if (!sid) {
507                 TALLOC_FREE(r);
508                 cont(private_data, false, NULL);
509                 return;
510         }
511
512         TALLOC_FREE(r);
513         cont(private_data, true, sid);
514 }
515
516 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
517                             void (*cont)(void *private_data, bool success, const char *sid),
518                             void *private_data)
519 {
520         struct winbind_get_idmap *r = NULL;
521
522         DEBUG(7,("winbindd_uid2sid_async: Resolving %u to a sid\n",
523                 uid));
524
525         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
526         if (!r) goto nomem;
527         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
528         if (!r->in.level) goto nomem;
529
530         *r->in.level    = WINBIND_IDMAP_LEVEL_UID_TO_SID;
531         r->in.req.uid   = uid;
532
533         do_async_ndr(mem_ctx, idmap_child(),
534                      NDR_WINBIND_GET_IDMAP, r,
535                      winbindd_uid2sid_recv, r,
536                      (void *)cont, private_data);
537         return;
538 nomem:
539         TALLOC_FREE(r);
540         cont(private_data, false, NULL);
541         return;
542 }
543
544 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
545                                   struct winbindd_ndr_call *c,
546                                   void *_r,
547                                   void *_cont,
548                                   void *private_data)
549 {
550         void (*cont)(void *priv, bool succ, const char *sid) =
551                 (void (*)(void *, bool, const char *))_cont;
552         struct winbind_get_idmap *r =
553                 talloc_get_type_abort(_r, struct winbind_get_idmap);
554         const char *sid;
555
556         if (!success) {
557                 DEBUG(5, ("Could not get_idmap(gid2sid)\n"));
558                 TALLOC_FREE(r);
559                 cont(private_data, false, NULL);
560                 return;
561         }
562
563         if (r->out.result != WINBIND_STATUS_OK) {
564                 DEBUG(5, ("get_idmap(gid2sid) returned an error:0x%08X\n",
565                         r->out.result));
566                 TALLOC_FREE(r);
567                 cont(private_data, false, NULL);
568                 return;
569         }
570
571         sid = dom_sid_string(mem_ctx, r->out.rep->sid);
572         if (!sid) {
573                 TALLOC_FREE(r);
574                 cont(private_data, false, NULL);
575                 return;
576         }
577
578         TALLOC_FREE(r);
579         cont(private_data, true, sid);
580 }
581
582 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
583                             void (*cont)(void *private_data, bool success, const char *sid),
584                             void *private_data)
585 {
586         struct winbind_get_idmap *r = NULL;
587
588         DEBUG(7,("winbindd_gid2sid_async: Resolving %u to a sid\n",
589                 gid));
590
591         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
592         if (!r) goto nomem;
593         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
594         if (!r->in.level) goto nomem;
595
596         *r->in.level    = WINBIND_IDMAP_LEVEL_GID_TO_SID;
597         r->in.req.gid   = gid;
598
599         do_async_ndr(mem_ctx, idmap_child(),
600                      NDR_WINBIND_GET_IDMAP, r,
601                      winbindd_gid2sid_recv, r,
602                      (void *)cont, private_data);
603         return;
604 nomem:
605         TALLOC_FREE(r);
606         cont(private_data, false, NULL);
607         return;
608 }
609
610 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
611         {
612                 .name           = "DUAL_SET_MAPPING",
613                 .struct_cmd     = WINBINDD_DUAL_SET_MAPPING,
614                 .struct_fn      = winbindd_dual_set_mapping,
615         },{
616                 .name           = "DUAL_SET_HWMS",
617                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
618                 .struct_fn      = winbindd_dual_set_hwm,
619         },{
620                 .name           = "ALLOCATE_UID",
621                 .struct_cmd     = WINBINDD_ALLOCATE_UID,
622                 .struct_fn      = winbindd_dual_allocate_uid,
623         },{
624                 .name           = "ALLOCATE_GID",
625                 .struct_cmd     = WINBINDD_ALLOCATE_GID,
626                 .struct_fn      = winbindd_dual_allocate_gid,
627         },{
628                 .name           = "NDR_WINBIND_GET_IDMAP",
629                 .ndr_opnum      = NDR_WINBIND_GET_IDMAP,
630                 .ndr_fn         = winbindd_ndr_child_get_idmap,
631         },{
632                 .name           = NULL,
633         }
634 };