s3:winbind: Convert WINBINDD_SET_MAPPING to the new API
[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 struct winbindd_child static_idmap_child;
42
43 struct winbindd_child *idmap_child(void)
44 {
45         return &static_idmap_child;
46 }
47
48 static void winbindd_remove_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
49                                    struct winbindd_response *response,
50                                    void *c, void *private_data)
51 {
52         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
53
54         if (!success) {
55                 DEBUG(5, ("Could not trigger idmap_remove_mapping\n"));
56                 cont(private_data, False);
57                 return;
58         }
59
60         if (response->result != WINBINDD_OK) {
61                 DEBUG(5, ("idmap_remove_mapping returned an error\n"));
62                 cont(private_data, False);
63                 return;
64         }
65
66         cont(private_data, True);
67 }
68
69 void winbindd_remove_mapping_async(TALLOC_CTX *mem_ctx,
70                              const struct id_map *map,
71                              void (*cont)(void *private_data, bool success),
72                              void *private_data)
73 {
74         struct winbindd_request request;
75         ZERO_STRUCT(request);
76         request.cmd = WINBINDD_DUAL_REMOVE_MAPPING;
77         request.data.dual_idmapset.id = map->xid.id;
78         request.data.dual_idmapset.type = map->xid.type;
79         sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
80
81         do_async(mem_ctx, idmap_child(), &request, winbindd_remove_mapping_recv,
82                  (void *)cont, private_data);
83 }
84
85 enum winbindd_result winbindd_dual_remove_mapping(
86                                             struct winbindd_domain *domain,
87                                             struct winbindd_cli_state *state)
88 {
89         struct id_map map;
90         DOM_SID sid;
91         NTSTATUS result;
92
93         DEBUG(3, ("[%5lu]: dual_idmapremove\n", (unsigned long)state->pid));
94
95         if (!string_to_sid(&sid, state->request->data.dual_idmapset.sid))
96                 return WINBINDD_ERROR;
97
98         map.sid = &sid;
99         map.xid.id = state->request->data.dual_idmapset.id;
100         map.xid.type = state->request->data.dual_idmapset.type;
101         map.status = ID_MAPPED;
102
103         result = idmap_remove_mapping(&map);
104         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
105 }
106
107 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
108                                    struct winbindd_response *response,
109                                    void *c, void *private_data)
110 {
111         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
112
113         if (!success) {
114                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
115                 cont(private_data, False);
116                 return;
117         }
118
119         if (response->result != WINBINDD_OK) {
120                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
121                 cont(private_data, False);
122                 return;
123         }
124
125         cont(private_data, True);
126 }
127
128 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
129                              void (*cont)(void *private_data, bool success),
130                              void *private_data)
131 {
132         struct winbindd_request request;
133         ZERO_STRUCT(request);
134         request.cmd = WINBINDD_DUAL_SET_HWM;
135         request.data.dual_idmapset.id = xid->id;
136         request.data.dual_idmapset.type = xid->type;
137
138         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
139                  (void *)cont, private_data);
140 }
141
142 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
143                                             struct winbindd_cli_state *state)
144 {
145         struct unixid xid;
146         NTSTATUS result;
147
148         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
149
150         xid.id = state->request->data.dual_idmapset.id;
151         xid.type = state->request->data.dual_idmapset.type;
152
153         switch (xid.type) {
154         case ID_TYPE_UID:
155                 result = idmap_set_uid_hwm(&xid);
156                 break;
157         case ID_TYPE_GID:
158                 result = idmap_set_gid_hwm(&xid);
159                 break;
160         default:
161                 return WINBINDD_ERROR;
162         }
163         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
164 }
165
166 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
167                                struct winbindd_response *response,
168                                void *c, void *private_data)
169 {
170         void (*cont)(void *priv, bool succ, uid_t uid) =
171                 (void (*)(void *, bool, uid_t))c;
172
173         if (!success) {
174                 DEBUG(5, ("Could not trigger sid2uid\n"));
175                 cont(private_data, False, 0);
176                 return;
177         }
178
179         if (response->result != WINBINDD_OK) {
180                 DEBUG(5, ("sid2uid returned an error\n"));
181                 cont(private_data, False, 0);
182                 return;
183         }
184
185         cont(private_data, True, response->data.uid);
186 }
187
188 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
189                          void (*cont)(void *private_data, bool success, uid_t uid),
190                          void *private_data)
191 {
192         struct winbindd_request request;
193         struct winbindd_domain *domain;
194
195         ZERO_STRUCT(request);
196         request.cmd = WINBINDD_DUAL_SID2UID;
197
198         domain = find_domain_from_sid(sid);
199
200         if (domain != NULL) {
201                 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
202                            "have_idmap_config = %d\n", domain->name,
203                            (int)domain->have_idmap_config));
204
205         }
206         else {
207                 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
208                            "%s\n", sid_string_dbg(sid)));
209         }
210
211         if ((domain != NULL) && (domain->have_idmap_config)) {
212                 fstrcpy(request.domain_name, domain->name);
213         }
214
215         sid_to_fstring(request.data.dual_sid2id.sid, sid);
216         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
217                  (void *)cont, private_data);
218 }
219
220 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
221                                            struct winbindd_cli_state *state)
222 {
223         DOM_SID sid;
224         NTSTATUS result;
225
226         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
227                   state->request->data.dual_sid2id.sid));
228
229         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
230                 DEBUG(1, ("Could not get convert sid %s from string\n",
231                           state->request->data.dual_sid2id.sid));
232                 return WINBINDD_ERROR;
233         }
234
235         result = idmap_sid_to_uid(state->request->domain_name, &sid,
236                                   &state->response->data.uid);
237
238         DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
239                    NT_STATUS_V(result), sid_string_dbg(&sid),
240                    (unsigned int)state->response->data.uid));
241
242         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
243 }
244
245 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
246                                struct winbindd_response *response,
247                                void *c, void *private_data)
248 {
249         void (*cont)(void *priv, bool succ, gid_t gid) =
250                 (void (*)(void *, bool, gid_t))c;
251
252         if (!success) {
253                 DEBUG(5, ("Could not trigger sid2gid\n"));
254                 cont(private_data, False, 0);
255                 return;
256         }
257
258         if (response->result != WINBINDD_OK) {
259                 DEBUG(5, ("sid2gid returned an error\n"));
260                 cont(private_data, False, 0);
261                 return;
262         }
263
264         cont(private_data, True, response->data.gid);
265 }
266
267 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
268                          void (*cont)(void *private_data, bool success, gid_t gid),
269                          void *private_data)
270 {
271         struct winbindd_request request;
272         struct winbindd_domain *domain;
273
274         ZERO_STRUCT(request);
275         request.cmd = WINBINDD_DUAL_SID2GID;
276
277         domain = find_domain_from_sid(sid);
278         if ((domain != NULL) && (domain->have_idmap_config)) {
279                 fstrcpy(request.domain_name, domain->name);
280         }
281
282         sid_to_fstring(request.data.dual_sid2id.sid, sid);
283
284         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
285                 request.data.dual_sid2id.sid));
286
287         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
288                  (void *)cont, private_data);
289 }
290
291 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
292                                            struct winbindd_cli_state *state)
293 {
294         DOM_SID sid;
295         NTSTATUS result;
296
297         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
298                   state->request->data.dual_sid2id.sid));
299
300         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
301                 DEBUG(1, ("Could not get convert sid %s from string\n",
302                           state->request->data.dual_sid2id.sid));
303                 return WINBINDD_ERROR;
304         }
305
306         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
307
308         result = idmap_sid_to_gid(state->request->domain_name, &sid,
309                                   &state->response->data.gid);
310
311         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
312                    NT_STATUS_V(result), sid_string_dbg(&sid),
313                    (unsigned int)state->response->data.gid));
314
315         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
316 }
317
318 /* The following uid2sid/gid2sid functions has been contributed by
319  * Keith Reynolds <Keith.Reynolds@centrify.com> */
320
321 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
322                                   struct winbindd_response *response,
323                                   void *c, void *private_data)
324 {
325         void (*cont)(void *priv, bool succ, const char *sid) =
326                 (void (*)(void *, bool, const char *))c;
327
328         if (!success) {
329                 DEBUG(5, ("Could not trigger uid2sid\n"));
330                 cont(private_data, False, NULL);
331                 return;
332         }
333
334         if (response->result != WINBINDD_OK) {
335                 DEBUG(5, ("uid2sid returned an error\n"));
336                 cont(private_data, False, NULL);
337                 return;
338         }
339
340         cont(private_data, True, response->data.sid.sid);
341 }
342
343 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
344                             void (*cont)(void *private_data, bool success, const char *sid),
345                             void *private_data)
346 {
347         struct winbindd_domain *domain;
348         struct winbindd_request request;
349
350         ZERO_STRUCT(request);
351         request.cmd = WINBINDD_DUAL_UID2SID;
352         request.data.uid = uid;
353
354         for (domain = domain_list(); domain != NULL; domain = domain->next) {
355                 if (domain->have_idmap_config
356                     && (uid >= domain->id_range_low)
357                     && (uid <= domain->id_range_high)) {
358                         fstrcpy(request.domain_name, domain->name);
359                 }
360         }
361
362         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
363                  (void *)cont, private_data);
364 }
365
366 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
367                                            struct winbindd_cli_state *state)
368 {
369         DOM_SID sid;
370         NTSTATUS result;
371
372         DEBUG(3,("[%5lu]: uid to sid %lu\n",
373                  (unsigned long)state->pid,
374                  (unsigned long) state->request->data.uid));
375
376         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
377         result = idmap_uid_to_sid(state->request->domain_name, &sid,
378                                   state->request->data.uid);
379
380         if (NT_STATUS_IS_OK(result)) {
381                 sid_to_fstring(state->response->data.sid.sid, &sid);
382                 state->response->data.sid.type = SID_NAME_USER;
383                 return WINBINDD_OK;
384         }
385
386         return WINBINDD_ERROR;
387 }
388
389 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
390                                   struct winbindd_response *response,
391                                   void *c, void *private_data)
392 {
393         void (*cont)(void *priv, bool succ, const char *sid) =
394                 (void (*)(void *, bool, const char *))c;
395
396         if (!success) {
397                 DEBUG(5, ("Could not trigger gid2sid\n"));
398                 cont(private_data, False, NULL);
399                 return;
400         }
401
402         if (response->result != WINBINDD_OK) {
403                 DEBUG(5, ("gid2sid returned an error\n"));
404                 cont(private_data, False, NULL);
405                 return;
406         }
407
408         cont(private_data, True, response->data.sid.sid);
409 }
410
411 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
412                             void (*cont)(void *private_data, bool success, const char *sid),
413                             void *private_data)
414 {
415         struct winbindd_domain *domain;
416         struct winbindd_request request;
417
418         ZERO_STRUCT(request);
419         request.cmd = WINBINDD_DUAL_GID2SID;
420         request.data.gid = gid;
421
422         for (domain = domain_list(); domain != NULL; domain = domain->next) {
423                 if (domain->have_idmap_config
424                     && (gid >= domain->id_range_low)
425                     && (gid <= domain->id_range_high)) {
426                         fstrcpy(request.domain_name, domain->name);
427                 }
428         }
429
430         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
431                  (void *)cont, private_data);
432 }
433
434 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
435                                            struct winbindd_cli_state *state)
436 {
437         DOM_SID sid;
438         NTSTATUS result;
439
440         DEBUG(3,("[%5lu]: gid %lu to sid\n",
441                 (unsigned long)state->pid,
442                 (unsigned long) state->request->data.gid));
443
444         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
445         result = idmap_gid_to_sid(state->request->domain_name, &sid,
446                                   state->request->data.gid);
447
448         if (NT_STATUS_IS_OK(result)) {
449                 sid_to_fstring(state->response->data.sid.sid, &sid);
450                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
451                            (unsigned long)state->pid,
452                            state->response->data.sid.sid));
453                 state->response->data.sid.type = SID_NAME_DOM_GRP;
454                 return WINBINDD_OK;
455         }
456
457         return WINBINDD_ERROR;
458 }
459
460 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
461         {
462                 .name           = "PING",
463                 .struct_cmd     = WINBINDD_PING,
464                 .struct_fn      = winbindd_dual_ping,
465         },{
466                 .name           = "DUAL_SID2UID",
467                 .struct_cmd     = WINBINDD_DUAL_SID2UID,
468                 .struct_fn      = winbindd_dual_sid2uid,
469         },{
470                 .name           = "DUAL_SID2GID",
471                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
472                 .struct_fn      = winbindd_dual_sid2gid,
473         },{
474                 .name           = "DUAL_UID2SID",
475                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
476                 .struct_fn      = winbindd_dual_uid2sid,
477         },{
478                 .name           = "DUAL_GID2SID",
479                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
480                 .struct_fn      = winbindd_dual_gid2sid,
481         },{
482                 .name           = "DUAL_REMOVE_MAPPING",
483                 .struct_cmd     = WINBINDD_DUAL_REMOVE_MAPPING,
484                 .struct_fn      = winbindd_dual_remove_mapping,
485         },{
486                 .name           = "DUAL_SET_HWMS",
487                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
488                 .struct_fn      = winbindd_dual_set_hwm,
489         },{
490                 .name           = "NDRCMD",
491                 .struct_cmd     = WINBINDD_DUAL_NDRCMD,
492                 .struct_fn      = winbindd_dual_ndrcmd,
493         },{
494                 .name           = NULL,
495         }
496 };
497
498 void init_idmap_child(void)
499 {
500         setup_child(NULL, &static_idmap_child,
501                     idmap_dispatch_table,
502                     "log.winbindd", "idmap");
503 }