s3:winbind: Convert WINBINDD_SET_HWM 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_sid2uid_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, uid_t uid) =
53                 (void (*)(void *, bool, uid_t))c;
54
55         if (!success) {
56                 DEBUG(5, ("Could not trigger sid2uid\n"));
57                 cont(private_data, False, 0);
58                 return;
59         }
60
61         if (response->result != WINBINDD_OK) {
62                 DEBUG(5, ("sid2uid returned an error\n"));
63                 cont(private_data, False, 0);
64                 return;
65         }
66
67         cont(private_data, True, response->data.uid);
68 }
69
70 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
71                          void (*cont)(void *private_data, bool success, uid_t uid),
72                          void *private_data)
73 {
74         struct winbindd_request request;
75         struct winbindd_domain *domain;
76
77         ZERO_STRUCT(request);
78         request.cmd = WINBINDD_DUAL_SID2UID;
79
80         domain = find_domain_from_sid(sid);
81
82         if (domain != NULL) {
83                 DEBUG(10, ("winbindd_sid2uid_async found domain %s, "
84                            "have_idmap_config = %d\n", domain->name,
85                            (int)domain->have_idmap_config));
86
87         }
88         else {
89                 DEBUG(10, ("winbindd_sid2uid_async did not find a domain for "
90                            "%s\n", sid_string_dbg(sid)));
91         }
92
93         if ((domain != NULL) && (domain->have_idmap_config)) {
94                 fstrcpy(request.domain_name, domain->name);
95         }
96
97         sid_to_fstring(request.data.dual_sid2id.sid, sid);
98         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
99                  (void *)cont, private_data);
100 }
101
102 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
103                                            struct winbindd_cli_state *state)
104 {
105         DOM_SID sid;
106         NTSTATUS result;
107
108         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
109                   state->request->data.dual_sid2id.sid));
110
111         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
112                 DEBUG(1, ("Could not get convert sid %s from string\n",
113                           state->request->data.dual_sid2id.sid));
114                 return WINBINDD_ERROR;
115         }
116
117         result = idmap_sid_to_uid(state->request->domain_name, &sid,
118                                   &state->response->data.uid);
119
120         DEBUG(10, ("winbindd_dual_sid2uid: 0x%08x - %s - %u\n",
121                    NT_STATUS_V(result), sid_string_dbg(&sid),
122                    (unsigned int)state->response->data.uid));
123
124         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
125 }
126
127 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
128                                struct winbindd_response *response,
129                                void *c, void *private_data)
130 {
131         void (*cont)(void *priv, bool succ, gid_t gid) =
132                 (void (*)(void *, bool, gid_t))c;
133
134         if (!success) {
135                 DEBUG(5, ("Could not trigger sid2gid\n"));
136                 cont(private_data, False, 0);
137                 return;
138         }
139
140         if (response->result != WINBINDD_OK) {
141                 DEBUG(5, ("sid2gid returned an error\n"));
142                 cont(private_data, False, 0);
143                 return;
144         }
145
146         cont(private_data, True, response->data.gid);
147 }
148
149 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
150                          void (*cont)(void *private_data, bool success, gid_t gid),
151                          void *private_data)
152 {
153         struct winbindd_request request;
154         struct winbindd_domain *domain;
155
156         ZERO_STRUCT(request);
157         request.cmd = WINBINDD_DUAL_SID2GID;
158
159         domain = find_domain_from_sid(sid);
160         if ((domain != NULL) && (domain->have_idmap_config)) {
161                 fstrcpy(request.domain_name, domain->name);
162         }
163
164         sid_to_fstring(request.data.dual_sid2id.sid, sid);
165
166         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
167                 request.data.dual_sid2id.sid));
168
169         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
170                  (void *)cont, private_data);
171 }
172
173 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
174                                            struct winbindd_cli_state *state)
175 {
176         DOM_SID sid;
177         NTSTATUS result;
178
179         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
180                   state->request->data.dual_sid2id.sid));
181
182         if (!string_to_sid(&sid, state->request->data.dual_sid2id.sid)) {
183                 DEBUG(1, ("Could not get convert sid %s from string\n",
184                           state->request->data.dual_sid2id.sid));
185                 return WINBINDD_ERROR;
186         }
187
188         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
189
190         result = idmap_sid_to_gid(state->request->domain_name, &sid,
191                                   &state->response->data.gid);
192
193         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
194                    NT_STATUS_V(result), sid_string_dbg(&sid),
195                    (unsigned int)state->response->data.gid));
196
197         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
198 }
199
200 /* The following uid2sid/gid2sid functions has been contributed by
201  * Keith Reynolds <Keith.Reynolds@centrify.com> */
202
203 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
204                                   struct winbindd_response *response,
205                                   void *c, void *private_data)
206 {
207         void (*cont)(void *priv, bool succ, const char *sid) =
208                 (void (*)(void *, bool, const char *))c;
209
210         if (!success) {
211                 DEBUG(5, ("Could not trigger uid2sid\n"));
212                 cont(private_data, False, NULL);
213                 return;
214         }
215
216         if (response->result != WINBINDD_OK) {
217                 DEBUG(5, ("uid2sid returned an error\n"));
218                 cont(private_data, False, NULL);
219                 return;
220         }
221
222         cont(private_data, True, response->data.sid.sid);
223 }
224
225 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
226                             void (*cont)(void *private_data, bool success, const char *sid),
227                             void *private_data)
228 {
229         struct winbindd_domain *domain;
230         struct winbindd_request request;
231
232         ZERO_STRUCT(request);
233         request.cmd = WINBINDD_DUAL_UID2SID;
234         request.data.uid = uid;
235
236         for (domain = domain_list(); domain != NULL; domain = domain->next) {
237                 if (domain->have_idmap_config
238                     && (uid >= domain->id_range_low)
239                     && (uid <= domain->id_range_high)) {
240                         fstrcpy(request.domain_name, domain->name);
241                 }
242         }
243
244         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
245                  (void *)cont, private_data);
246 }
247
248 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
249                                            struct winbindd_cli_state *state)
250 {
251         DOM_SID sid;
252         NTSTATUS result;
253
254         DEBUG(3,("[%5lu]: uid to sid %lu\n",
255                  (unsigned long)state->pid,
256                  (unsigned long) state->request->data.uid));
257
258         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
259         result = idmap_uid_to_sid(state->request->domain_name, &sid,
260                                   state->request->data.uid);
261
262         if (NT_STATUS_IS_OK(result)) {
263                 sid_to_fstring(state->response->data.sid.sid, &sid);
264                 state->response->data.sid.type = SID_NAME_USER;
265                 return WINBINDD_OK;
266         }
267
268         return WINBINDD_ERROR;
269 }
270
271 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
272                                   struct winbindd_response *response,
273                                   void *c, void *private_data)
274 {
275         void (*cont)(void *priv, bool succ, const char *sid) =
276                 (void (*)(void *, bool, const char *))c;
277
278         if (!success) {
279                 DEBUG(5, ("Could not trigger gid2sid\n"));
280                 cont(private_data, False, NULL);
281                 return;
282         }
283
284         if (response->result != WINBINDD_OK) {
285                 DEBUG(5, ("gid2sid returned an error\n"));
286                 cont(private_data, False, NULL);
287                 return;
288         }
289
290         cont(private_data, True, response->data.sid.sid);
291 }
292
293 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
294                             void (*cont)(void *private_data, bool success, const char *sid),
295                             void *private_data)
296 {
297         struct winbindd_domain *domain;
298         struct winbindd_request request;
299
300         ZERO_STRUCT(request);
301         request.cmd = WINBINDD_DUAL_GID2SID;
302         request.data.gid = gid;
303
304         for (domain = domain_list(); domain != NULL; domain = domain->next) {
305                 if (domain->have_idmap_config
306                     && (gid >= domain->id_range_low)
307                     && (gid <= domain->id_range_high)) {
308                         fstrcpy(request.domain_name, domain->name);
309                 }
310         }
311
312         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
313                  (void *)cont, private_data);
314 }
315
316 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
317                                            struct winbindd_cli_state *state)
318 {
319         DOM_SID sid;
320         NTSTATUS result;
321
322         DEBUG(3,("[%5lu]: gid %lu to sid\n",
323                 (unsigned long)state->pid,
324                 (unsigned long) state->request->data.gid));
325
326         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
327         result = idmap_gid_to_sid(state->request->domain_name, &sid,
328                                   state->request->data.gid);
329
330         if (NT_STATUS_IS_OK(result)) {
331                 sid_to_fstring(state->response->data.sid.sid, &sid);
332                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
333                            (unsigned long)state->pid,
334                            state->response->data.sid.sid));
335                 state->response->data.sid.type = SID_NAME_DOM_GRP;
336                 return WINBINDD_OK;
337         }
338
339         return WINBINDD_ERROR;
340 }
341
342 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
343         {
344                 .name           = "PING",
345                 .struct_cmd     = WINBINDD_PING,
346                 .struct_fn      = winbindd_dual_ping,
347         },{
348                 .name           = "DUAL_SID2UID",
349                 .struct_cmd     = WINBINDD_DUAL_SID2UID,
350                 .struct_fn      = winbindd_dual_sid2uid,
351         },{
352                 .name           = "DUAL_SID2GID",
353                 .struct_cmd     = WINBINDD_DUAL_SID2GID,
354                 .struct_fn      = winbindd_dual_sid2gid,
355         },{
356                 .name           = "DUAL_UID2SID",
357                 .struct_cmd     = WINBINDD_DUAL_UID2SID,
358                 .struct_fn      = winbindd_dual_uid2sid,
359         },{
360                 .name           = "DUAL_GID2SID",
361                 .struct_cmd     = WINBINDD_DUAL_GID2SID,
362                 .struct_fn      = winbindd_dual_gid2sid,
363         },{
364                 .name           = "NDRCMD",
365                 .struct_cmd     = WINBINDD_DUAL_NDRCMD,
366                 .struct_fn      = winbindd_dual_ndrcmd,
367         },{
368                 .name           = NULL,
369         }
370 };
371
372 void init_idmap_child(void)
373 {
374         setup_child(NULL, &static_idmap_child,
375                     idmap_dispatch_table,
376                     "log.winbindd", "idmap");
377 }