6f7b5624150e01e770ec46644a7e76706ea05422
[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_string(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_sids2xids_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 *, int) =
178                 (void (*)(void *, bool, void *, int))c;
179
180         if (!success) {
181                 DEBUG(5, ("Could not trigger sids2xids\n"));
182                 cont(private_data, False, NULL, 0);
183                 return;
184         }
185
186         if (response->result != WINBINDD_OK) {
187                 DEBUG(5, ("sids2xids returned an error\n"));
188                 cont(private_data, False, NULL, 0);
189                 return;
190         }
191
192         cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
193 }
194
195 void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
196                          void (*cont)(void *private_data, bool success, void *data, int len),
197                          void *private_data)
198 {
199         struct winbindd_request request;
200         ZERO_STRUCT(request);
201         request.cmd = WINBINDD_DUAL_SIDS2XIDS;
202         request.extra_data.data = (char *)sids;
203         request.extra_len = size;
204         do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
205                  (void *)cont, private_data);
206 }
207
208 enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
209                                            struct winbindd_cli_state *state)
210 {
211         DOM_SID *sids;
212         struct unixid *xids;
213         struct id_map **ids;
214         NTSTATUS result;
215         int num, i;
216
217         DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
218
219         if (state->request.extra_len == 0) {
220                 DEBUG(0, ("Invalid buffer size!\n"));
221                 return WINBINDD_ERROR;
222         }
223
224         sids = (DOM_SID *)state->request.extra_data.data;
225         num = state->request.extra_len / sizeof(DOM_SID);
226
227         ids = TALLOC_ZERO_ARRAY(state->mem_ctx, struct id_map *, num + 1);
228         if ( ! ids) {
229                 DEBUG(0, ("Out of memory!\n"));
230                 return WINBINDD_ERROR;
231         }
232         for (i = 0; i < num; i++) {
233                 ids[i] = TALLOC_P(ids, struct id_map);
234                 if ( ! ids[i]) {
235                         DEBUG(0, ("Out of memory!\n"));
236                         talloc_free(ids);
237                         return WINBINDD_ERROR;
238                 }
239                 ids[i]->sid = &sids[i];
240         }
241
242         result = idmap_sids_to_unixids(ids);
243
244         if (NT_STATUS_IS_OK(result)) {
245
246                 xids = SMB_MALLOC_ARRAY(struct unixid, num);
247                 if ( ! xids) {
248                         DEBUG(0, ("Out of memory!\n"));
249                         talloc_free(ids);
250                         return WINBINDD_ERROR;
251                 }
252
253                 for (i = 0; i < num; i++) {
254                         if (ids[i]->status == ID_MAPPED) {
255                                 xids[i].type = ids[i]->xid.type;
256                                 xids[i].id = ids[i]->xid.id;
257                         } else {
258                                 xids[i].type = -1;
259                         }
260                 }
261
262                 state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
263                 state->response.extra_data.data = xids;
264
265         } else {
266                 DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
267                 talloc_free(ids);
268                 return WINBINDD_ERROR;
269         }
270
271         talloc_free(ids);
272         return WINBINDD_OK;
273 }
274
275 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
276                                struct winbindd_response *response,
277                                void *c, void *private_data)
278 {
279         void (*cont)(void *priv, bool succ, uid_t uid) =
280                 (void (*)(void *, bool, uid_t))c;
281
282         if (!success) {
283                 DEBUG(5, ("Could not trigger sid2uid\n"));
284                 cont(private_data, False, 0);
285                 return;
286         }
287
288         if (response->result != WINBINDD_OK) {
289                 DEBUG(5, ("sid2uid returned an error\n"));
290                 cont(private_data, False, 0);
291                 return;
292         }
293
294         cont(private_data, True, response->data.uid);
295 }
296
297 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
298                          void (*cont)(void *private_data, bool success, uid_t uid),
299                          void *private_data)
300 {
301         struct winbindd_request request;
302         ZERO_STRUCT(request);
303         request.cmd = WINBINDD_DUAL_SID2UID;
304         sid_to_string(request.data.dual_sid2id.sid, sid);
305         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
306                  (void *)cont, private_data);
307 }
308
309 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
310                                            struct winbindd_cli_state *state)
311 {
312         DOM_SID sid;
313         NTSTATUS result;
314
315         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
316                   state->request.data.dual_sid2id.sid));
317
318         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
319                 DEBUG(1, ("Could not get convert sid %s from string\n",
320                           state->request.data.dual_sid2id.sid));
321                 return WINBINDD_ERROR;
322         }
323
324         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
325
326         result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
327
328         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
329 }
330
331 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
332                                struct winbindd_response *response,
333                                void *c, void *private_data)
334 {
335         void (*cont)(void *priv, bool succ, gid_t gid) =
336                 (void (*)(void *, bool, gid_t))c;
337
338         if (!success) {
339                 DEBUG(5, ("Could not trigger sid2gid\n"));
340                 cont(private_data, False, 0);
341                 return;
342         }
343
344         if (response->result != WINBINDD_OK) {
345                 DEBUG(5, ("sid2gid returned an error\n"));
346                 cont(private_data, False, 0);
347                 return;
348         }
349
350         cont(private_data, True, response->data.gid);
351 }
352
353 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
354                          void (*cont)(void *private_data, bool success, gid_t gid),
355                          void *private_data)
356 {
357         struct winbindd_request request;
358         ZERO_STRUCT(request);
359         request.cmd = WINBINDD_DUAL_SID2GID;
360         sid_to_string(request.data.dual_sid2id.sid, sid);
361
362         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
363                 request.data.dual_sid2id.sid));
364
365         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
366                  (void *)cont, private_data);
367 }
368
369 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
370                                            struct winbindd_cli_state *state)
371 {
372         DOM_SID sid;
373         NTSTATUS result;
374
375         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
376                   state->request.data.dual_sid2id.sid));
377
378         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
379                 DEBUG(1, ("Could not get convert sid %s from string\n",
380                           state->request.data.dual_sid2id.sid));
381                 return WINBINDD_ERROR;
382         }
383
384         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
385
386         result = idmap_sid_to_gid(&sid, &state->response.data.gid);
387
388         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n", NT_STATUS_V(result), sid_string_static(&sid), state->response.data.gid));
389
390         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
391 }
392
393 /* The following uid2sid/gid2sid functions has been contributed by
394  * Keith Reynolds <Keith.Reynolds@centrify.com> */
395
396 static void winbindd_uid2sid_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 uid2sid\n"));
405                 cont(private_data, False, NULL);
406                 return;
407         }
408
409         if (response->result != WINBINDD_OK) {
410                 DEBUG(5, ("uid2sid 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_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
419                             void (*cont)(void *private_data, bool success, const char *sid),
420                             void *private_data)
421 {
422         struct winbindd_request request;
423
424         ZERO_STRUCT(request);
425         request.cmd = WINBINDD_DUAL_UID2SID;
426         request.data.uid = uid;
427         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
428                  (void *)cont, private_data);
429 }
430
431 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
432                                            struct winbindd_cli_state *state)
433 {
434         DOM_SID sid;
435         NTSTATUS result;
436
437         DEBUG(3,("[%5lu]: uid to sid %lu\n",
438                  (unsigned long)state->pid,
439                  (unsigned long) state->request.data.uid));
440
441         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
442         result = idmap_uid_to_sid(&sid, state->request.data.uid);
443
444         if (NT_STATUS_IS_OK(result)) {
445                 sid_to_string(state->response.data.sid.sid, &sid);
446                 state->response.data.sid.type = SID_NAME_USER;
447                 return WINBINDD_OK;
448         }
449
450         return WINBINDD_ERROR;
451 }
452
453 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
454                                   struct winbindd_response *response,
455                                   void *c, void *private_data)
456 {
457         void (*cont)(void *priv, bool succ, const char *sid) =
458                 (void (*)(void *, bool, const char *))c;
459
460         if (!success) {
461                 DEBUG(5, ("Could not trigger gid2sid\n"));
462                 cont(private_data, False, NULL);
463                 return;
464         }
465
466         if (response->result != WINBINDD_OK) {
467                 DEBUG(5, ("gid2sid returned an error\n"));
468                 cont(private_data, False, NULL);
469                 return;
470         }
471
472         cont(private_data, True, response->data.sid.sid);
473 }
474
475 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
476                             void (*cont)(void *private_data, bool success, const char *sid),
477                             void *private_data)
478 {
479         struct winbindd_request request;
480
481         ZERO_STRUCT(request);
482         request.cmd = WINBINDD_DUAL_GID2SID;
483         request.data.gid = gid;
484         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
485                  (void *)cont, private_data);
486 }
487
488 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
489                                            struct winbindd_cli_state *state)
490 {
491         DOM_SID sid;
492         NTSTATUS result;
493
494         DEBUG(3,("[%5lu]: gid %lu to sid\n",
495                 (unsigned long)state->pid,
496                 (unsigned long) state->request.data.gid));
497
498         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
499         result = idmap_gid_to_sid(&sid, state->request.data.gid);
500
501         if (NT_STATUS_IS_OK(result)) {
502                 sid_to_string(state->response.data.sid.sid, &sid);
503                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
504                            (unsigned long)state->pid,
505                            state->response.data.sid.sid));
506                 state->response.data.sid.type = SID_NAME_DOM_GRP;
507                 return WINBINDD_OK;
508         }
509
510         return WINBINDD_ERROR;
511 }
512
513 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
514         {
515                 .name           = "DUAL_SID2UID",
516                 .struct_cmd     = WINBINDD_DUAL_SID2UID,
517                 .struct_fn      = winbindd_dual_sid2uid,
518         },{
519                 .name           = "DUAL_SID2GID",
520                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
521                 .struct_fn      = winbindd_dual_sid2gid,
522 #if 0   /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
523         },{
524                 .name           = "DUAL_SIDS2XIDS",
525                 .struct_cmd     = WINBINDD_DUAL_SIDS2XIDS,
526                 .struct_fn      = winbindd_dual_sids2xids,
527 #endif  /* end DISABLED */
528         },{
529                 .name           = "DUAL_UID2SID",
530                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
531                 .struct_fn      = winbindd_dual_uid2sid,
532         },{
533                 .name           = "DUAL_GID2SID",
534                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
535                 .struct_fn      = winbindd_dual_gid2sid,
536         },{
537                 .name           = "DUAL_SET_MAPPING",
538                 .struct_cmd     = WINBINDD_DUAL_SET_MAPPING,
539                 .struct_fn      = winbindd_dual_set_mapping,
540         },{
541                 .name           = "DUAL_SET_HWMS",
542                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
543                 .struct_fn      = winbindd_dual_set_hwm,
544         },{
545                 .name           = "ALLOCATE_UID",
546                 .struct_cmd     = WINBINDD_ALLOCATE_UID,
547                 .struct_fn      = winbindd_dual_allocate_uid,
548         },{
549                 .name           = "ALLOCATE_GID",
550                 .struct_cmd     = WINBINDD_ALLOCATE_GID,
551                 .struct_fn      = winbindd_dual_allocate_gid,
552         },{
553                 .name           = NULL,
554         }
555 };