wb-ndr: remove unused WINBINDD_DUAL_SID2UID 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_response *response,
412                                void *c, void *private_data)
413 {
414         void (*cont)(void *priv, bool succ, gid_t gid) =
415                 (void (*)(void *, bool, gid_t))c;
416
417         if (!success) {
418                 DEBUG(5, ("Could not trigger sid2gid\n"));
419                 cont(private_data, False, 0);
420                 return;
421         }
422
423         if (response->result != WINBINDD_OK) {
424                 DEBUG(5, ("sid2gid returned an error\n"));
425                 cont(private_data, False, 0);
426                 return;
427         }
428
429         cont(private_data, True, response->data.gid);
430 }
431
432 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
433                          void (*cont)(void *private_data, bool success, gid_t gid),
434                          void *private_data)
435 {
436         struct winbindd_request request;
437         ZERO_STRUCT(request);
438         request.cmd = WINBINDD_DUAL_SID2GID;
439         sid_to_fstring(request.data.dual_sid2id.sid, sid);
440
441         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
442                 request.data.dual_sid2id.sid));
443
444         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
445                  (void *)cont, private_data);
446 }
447
448 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
449                                            struct winbindd_cli_state *state)
450 {
451         DOM_SID sid;
452         NTSTATUS result;
453
454         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
455                   state->request.data.dual_sid2id.sid));
456
457         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
458                 DEBUG(1, ("Could not get convert sid %s from string\n",
459                           state->request.data.dual_sid2id.sid));
460                 return WINBINDD_ERROR;
461         }
462
463         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
464
465         result = idmap_sid_to_gid(&sid, &state->response.data.gid);
466
467         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
468                    NT_STATUS_V(result), sid_string_dbg(&sid),
469                    state->response.data.gid));
470
471         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
472 }
473
474 /* The following uid2sid/gid2sid functions has been contributed by
475  * Keith Reynolds <Keith.Reynolds@centrify.com> */
476
477 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
478                                   struct winbindd_response *response,
479                                   void *c, void *private_data)
480 {
481         void (*cont)(void *priv, bool succ, const char *sid) =
482                 (void (*)(void *, bool, const char *))c;
483
484         if (!success) {
485                 DEBUG(5, ("Could not trigger uid2sid\n"));
486                 cont(private_data, False, NULL);
487                 return;
488         }
489
490         if (response->result != WINBINDD_OK) {
491                 DEBUG(5, ("uid2sid returned an error\n"));
492                 cont(private_data, False, NULL);
493                 return;
494         }
495
496         cont(private_data, True, response->data.sid.sid);
497 }
498
499 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
500                             void (*cont)(void *private_data, bool success, const char *sid),
501                             void *private_data)
502 {
503         struct winbindd_request request;
504
505         ZERO_STRUCT(request);
506         request.cmd = WINBINDD_DUAL_UID2SID;
507         request.data.uid = uid;
508         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
509                  (void *)cont, private_data);
510 }
511
512 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
513                                            struct winbindd_cli_state *state)
514 {
515         DOM_SID sid;
516         NTSTATUS result;
517
518         DEBUG(3,("[%5lu]: uid to sid %lu\n",
519                  (unsigned long)state->pid,
520                  (unsigned long) state->request.data.uid));
521
522         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
523         result = idmap_uid_to_sid(&sid, state->request.data.uid);
524
525         if (NT_STATUS_IS_OK(result)) {
526                 sid_to_fstring(state->response.data.sid.sid, &sid);
527                 state->response.data.sid.type = SID_NAME_USER;
528                 return WINBINDD_OK;
529         }
530
531         return WINBINDD_ERROR;
532 }
533
534 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
535                                   struct winbindd_response *response,
536                                   void *c, void *private_data)
537 {
538         void (*cont)(void *priv, bool succ, const char *sid) =
539                 (void (*)(void *, bool, const char *))c;
540
541         if (!success) {
542                 DEBUG(5, ("Could not trigger gid2sid\n"));
543                 cont(private_data, False, NULL);
544                 return;
545         }
546
547         if (response->result != WINBINDD_OK) {
548                 DEBUG(5, ("gid2sid returned an error\n"));
549                 cont(private_data, False, NULL);
550                 return;
551         }
552
553         cont(private_data, True, response->data.sid.sid);
554 }
555
556 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
557                             void (*cont)(void *private_data, bool success, const char *sid),
558                             void *private_data)
559 {
560         struct winbindd_request request;
561
562         ZERO_STRUCT(request);
563         request.cmd = WINBINDD_DUAL_GID2SID;
564         request.data.gid = gid;
565         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
566                  (void *)cont, private_data);
567 }
568
569 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
570                                            struct winbindd_cli_state *state)
571 {
572         DOM_SID sid;
573         NTSTATUS result;
574
575         DEBUG(3,("[%5lu]: gid %lu to sid\n",
576                 (unsigned long)state->pid,
577                 (unsigned long) state->request.data.gid));
578
579         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
580         result = idmap_gid_to_sid(&sid, state->request.data.gid);
581
582         if (NT_STATUS_IS_OK(result)) {
583                 sid_to_fstring(state->response.data.sid.sid, &sid);
584                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
585                            (unsigned long)state->pid,
586                            state->response.data.sid.sid));
587                 state->response.data.sid.type = SID_NAME_DOM_GRP;
588                 return WINBINDD_OK;
589         }
590
591         return WINBINDD_ERROR;
592 }
593
594 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
595         {
596                 .name           = "DUAL_SID2GID",
597                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
598                 .struct_fn      = winbindd_dual_sid2gid,
599         },{
600                 .name           = "DUAL_UID2SID",
601                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
602                 .struct_fn      = winbindd_dual_uid2sid,
603         },{
604                 .name           = "DUAL_GID2SID",
605                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
606                 .struct_fn      = winbindd_dual_gid2sid,
607         },{
608                 .name           = "DUAL_SET_MAPPING",
609                 .struct_cmd     = WINBINDD_DUAL_SET_MAPPING,
610                 .struct_fn      = winbindd_dual_set_mapping,
611         },{
612                 .name           = "DUAL_SET_HWMS",
613                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
614                 .struct_fn      = winbindd_dual_set_hwm,
615         },{
616                 .name           = "ALLOCATE_UID",
617                 .struct_cmd     = WINBINDD_ALLOCATE_UID,
618                 .struct_fn      = winbindd_dual_allocate_uid,
619         },{
620                 .name           = "ALLOCATE_GID",
621                 .struct_cmd     = WINBINDD_ALLOCATE_GID,
622                 .struct_fn      = winbindd_dual_allocate_gid,
623         },{
624                 .name           = "NDR_WINBIND_GET_IDMAP",
625                 .ndr_opnum      = NDR_WINBIND_GET_IDMAP,
626                 .ndr_fn         = winbindd_ndr_child_get_idmap,
627         },{
628                 .name           = NULL,
629         }
630 };