s3: Remove unused winbindd_lookupsid_async
[ira/wip.git] / source3 / winbindd / winbindd_async.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
9    The helpers always consist of three functions: 
10
11    * A request setup function that takes the necessary parameters together
12      with a continuation function that is to be called upon completion
13
14    * A private continuation function that is internal only. This is to be
15      called by the lower-level functions in do_async(). Its only task is to
16      properly call the continuation function named above.
17
18    * A worker function that is called inside the appropriate child process.
19
20    This program is free software; you can redistribute it and/or modify
21    it under the terms of the GNU General Public License as published by
22    the Free Software Foundation; either version 3 of the License, or
23    (at your option) any later version.
24
25    This program is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28    GNU General Public License for more details.
29
30    You should have received a copy of the GNU General Public License
31    along with this program.  If not, see <http://www.gnu.org/licenses/>.
32 */
33
34 #include "includes.h"
35 #include "winbindd.h"
36
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_WINBIND
39
40 struct do_async_state {
41         TALLOC_CTX *mem_ctx;
42         struct winbindd_request request;
43         struct winbindd_response response;
44         void (*cont)(TALLOC_CTX *mem_ctx,
45                      bool success,
46                      struct winbindd_response *response,
47                      void *c, void *private_data);
48         void *c, *private_data;
49 };
50
51 static void do_async_recv(void *private_data, bool success)
52 {
53         struct do_async_state *state =
54                 talloc_get_type_abort(private_data, struct do_async_state);
55
56         state->cont(state->mem_ctx, success, &state->response,
57                     state->c, state->private_data);
58 }
59
60 void do_async(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
61               const struct winbindd_request *request,
62               void (*cont)(TALLOC_CTX *mem_ctx, bool success,
63                            struct winbindd_response *response,
64                            void *c, void *private_data),
65               void *c, void *private_data)
66 {
67         struct do_async_state *state;
68
69         state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
70         if (state == NULL) {
71                 DEBUG(0, ("talloc failed\n"));
72                 cont(mem_ctx, False, NULL, c, private_data);
73                 return;
74         }
75
76         state->mem_ctx = mem_ctx;
77         state->request = *request;
78         state->request.length = sizeof(state->request);
79         state->cont = cont;
80         state->c = c;
81         state->private_data = private_data;
82
83         async_request(mem_ctx, child, &state->request,
84                       &state->response, do_async_recv, state);
85 }
86
87 static void do_async_domain(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
88                             const struct winbindd_request *request,
89                             void (*cont)(TALLOC_CTX *mem_ctx, bool success,
90                                          struct winbindd_response *response,
91                                          void *c, void *private_data),
92                             void *c, void *private_data)
93 {
94         struct do_async_state *state;
95
96         state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
97         if (state == NULL) {
98                 DEBUG(0, ("talloc failed\n"));
99                 cont(mem_ctx, False, NULL, c, private_data);
100                 return;
101         }
102
103         state->mem_ctx = mem_ctx;
104         state->request = *request;
105         state->request.length = sizeof(state->request);
106         state->cont = cont;
107         state->c = c;
108         state->private_data = private_data;
109
110         async_domain_request(mem_ctx, domain, &state->request,
111                              &state->response, do_async_recv, state);
112 }
113
114 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
115                                              struct winbindd_cli_state *state)
116 {
117         enum lsa_SidType type;
118         DOM_SID sid;
119         char *name;
120         char *dom_name;
121
122         /* Ensure null termination */
123         state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
124
125         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
126                   state->request->data.sid));
127
128         /* Lookup sid from PDC using lsa_lookup_sids() */
129
130         if (!string_to_sid(&sid, state->request->data.sid)) {
131                 DEBUG(5, ("%s not a SID\n", state->request->data.sid));
132                 return WINBINDD_ERROR;
133         }
134
135         /* Lookup the sid */
136
137         if (!winbindd_lookup_name_by_sid(state->mem_ctx, domain, &sid, 
138                                          &dom_name, &name, &type)) 
139         {
140                 TALLOC_FREE(dom_name);
141                 TALLOC_FREE(name);
142                 return WINBINDD_ERROR;
143         }
144
145         fstrcpy(state->response->data.name.dom_name, dom_name);
146         fstrcpy(state->response->data.name.name, name);
147         state->response->data.name.type = type;
148
149         TALLOC_FREE(dom_name);
150         TALLOC_FREE(name);
151         return WINBINDD_OK;
152 }
153
154 /********************************************************************
155  This is the second callback after contacting the forest root
156 ********************************************************************/
157
158 struct lookupname_state {
159         char *dom_name;
160         char *name;
161         void *caller_private_data;
162 };
163
164
165 static void lookupname_recv2(TALLOC_CTX *mem_ctx, bool success,
166                             struct winbindd_response *response,
167                             void *c, void *private_data)
168 {
169         void (*cont)(void *priv, bool succ, const DOM_SID *sid,
170                      enum lsa_SidType type) =
171                 (void (*)(void *, bool, const DOM_SID *, enum lsa_SidType))c;
172         DOM_SID sid;
173         struct lookupname_state *s = talloc_get_type_abort( private_data,
174                                                             struct lookupname_state );
175
176         if (!success) {
177                 DEBUG(5, ("Could not trigger lookup_name\n"));
178                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
179                 return;
180         }
181
182         if (response->result != WINBINDD_OK) {
183                 DEBUG(5, ("lookup_name returned an error\n"));
184                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
185                 return;
186         }
187
188         if (!string_to_sid(&sid, response->data.sid.sid)) {
189                 DEBUG(0, ("Could not convert string %s to sid\n",
190                           response->data.sid.sid));
191                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
192                 return;
193         }
194
195         cont(s->caller_private_data, True, &sid,
196              (enum lsa_SidType)response->data.sid.type);
197 }
198
199 /********************************************************************
200  This is the first callback after contacting our own domain
201 ********************************************************************/
202
203 static void lookupname_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 DOM_SID *sid,
208                      enum lsa_SidType type) =
209                 (void (*)(void *, bool, const DOM_SID *, enum lsa_SidType))c;
210         DOM_SID sid;
211         struct lookupname_state *s = talloc_get_type_abort( private_data,
212                                                             struct lookupname_state );  
213
214         if (!success) {
215                 DEBUG(5, ("lookupname_recv: lookup_name() failed!\n"));
216                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
217                 return;
218         }
219
220         if (response->result != WINBINDD_OK) {
221                 /* Try again using the forest root */
222                 struct winbindd_domain *root_domain = find_root_domain();
223                 struct winbindd_request request;
224
225                 if ( !root_domain ) {
226                         DEBUG(5,("lookupname_recv: unable to determine forest root\n"));
227                         cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
228                         return;
229                 }
230
231                 ZERO_STRUCT(request);
232                 request.cmd = WINBINDD_LOOKUPNAME;
233
234                 fstrcpy( request.data.name.dom_name, s->dom_name );
235                 fstrcpy( request.data.name.name, s->name );
236
237                 do_async_domain(mem_ctx, root_domain, &request, lookupname_recv2,
238                                 (void *)cont, s);
239
240                 return;
241         }
242
243         if (!string_to_sid(&sid, response->data.sid.sid)) {
244                 DEBUG(0, ("Could not convert string %s to sid\n",
245                           response->data.sid.sid));
246                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
247                 return;
248         }
249
250         cont(s->caller_private_data, True, &sid,
251              (enum lsa_SidType)response->data.sid.type);
252 }
253
254 /********************************************************************
255  The lookup name call first contacts a DC in its own domain
256  and fallbacks to contact a DC if the forest in our domain doesn't
257  know the name.
258 ********************************************************************/
259
260 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx,
261                                const char *dom_name, const char *name,
262                                void (*cont)(void *private_data, bool success,
263                                             const DOM_SID *sid,
264                                             enum lsa_SidType type),
265                                enum winbindd_cmd orig_cmd,
266                                void *private_data)
267 {
268         struct winbindd_request request;
269         struct winbindd_domain *domain;
270         struct lookupname_state *s;
271
272         domain = find_lookup_domain_from_name(dom_name);
273         if (domain == NULL) {
274                 DEBUG(5, ("Could not find domain for name '%s'\n", dom_name));
275                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
276                 return;
277         }
278
279         ZERO_STRUCT(request);
280         request.cmd = WINBINDD_LOOKUPNAME;
281         request.original_cmd = orig_cmd;
282         fstrcpy(request.data.name.dom_name, dom_name);
283         fstrcpy(request.data.name.name, name);
284
285         if ( (s = TALLOC_ZERO_P(mem_ctx, struct lookupname_state)) == NULL ) {
286                 DEBUG(0, ("winbindd_lookupname_async: talloc failed\n"));
287                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
288                 return;
289         }
290
291         s->dom_name = talloc_strdup( s, dom_name );
292         s->name     = talloc_strdup( s, name );
293         if (!s->dom_name || !s->name) {
294                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
295                 return;
296         }
297
298         s->caller_private_data = private_data;
299
300         do_async_domain(mem_ctx, domain, &request, lookupname_recv,
301                         (void *)cont, s);
302 }
303
304 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
305                                               struct winbindd_cli_state *state)
306 {
307         enum lsa_SidType type;
308         char *name_domain, *name_user;
309         DOM_SID sid;
310         char *p;
311
312         /* Ensure null termination */
313         state->request->data.name.dom_name[sizeof(state->request->data.name.dom_name)-1]='\0';
314
315         /* Ensure null termination */
316         state->request->data.name.name[sizeof(state->request->data.name.name)-1]='\0';
317
318         /* cope with the name being a fully qualified name */
319         p = strstr(state->request->data.name.name, lp_winbind_separator());
320         if (p) {
321                 *p = 0;
322                 name_domain = state->request->data.name.name;
323                 name_user = p+1;
324         } else {
325                 name_domain = state->request->data.name.dom_name;
326                 name_user = state->request->data.name.name;
327         }
328
329         DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
330                   name_domain, lp_winbind_separator(), name_user));
331
332         /* Lookup name from DC using lsa_lookup_names() */
333         if (!winbindd_lookup_sid_by_name(state->mem_ctx, state->request->original_cmd, domain, name_domain,
334                                          name_user, &sid, &type)) {
335                 return WINBINDD_ERROR;
336         }
337
338         sid_to_fstring(state->response->data.sid.sid, &sid);
339         state->response->data.sid.type = type;
340
341         return WINBINDD_OK;
342 }
343
344 bool print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
345                    size_t num_sids, char **result, ssize_t *len)
346 {
347         size_t i;
348         size_t buflen = 0;
349
350         *len = 0;
351         *result = NULL;
352         for (i=0; i<num_sids; i++) {
353                 fstring tmp;
354                 sprintf_append(mem_ctx, result, len, &buflen,
355                                "%s\n", sid_to_fstring(tmp, &sids[i]));
356         }
357
358         if ((num_sids != 0) && (*result == NULL)) {
359                 return False;
360         }
361
362         return True;
363 }
364
365 bool parse_sidlist(TALLOC_CTX *mem_ctx, const char *sidstr,
366                    DOM_SID **sids, size_t *num_sids)
367 {
368         const char *p, *q;
369
370         p = sidstr;
371         if (p == NULL)
372                 return False;
373
374         while (p[0] != '\0') {
375                 fstring tmp;
376                 size_t sidlen;
377                 DOM_SID sid;
378                 q = strchr(p, '\n');
379                 if (q == NULL) {
380                         DEBUG(0, ("Got invalid sidstr: %s\n", p));
381                         return False;
382                 }
383                 sidlen = PTR_DIFF(q, p);
384                 if (sidlen >= sizeof(tmp)-1) {
385                         return false;
386                 }
387                 memcpy(tmp, p, sidlen);
388                 tmp[sidlen] = '\0';
389                 q += 1;
390                 if (!string_to_sid(&sid, tmp)) {
391                         DEBUG(0, ("Could not parse sid %s\n", p));
392                         return False;
393                 }
394                 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &sid, sids,
395                                                       num_sids)))
396                 {
397                         return False;
398                 }
399                 p = q;
400         }
401         return True;
402 }
403
404 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, bool success,
405                                struct winbindd_response *response,
406                                void *c, void *private_data)
407 {
408         void (*cont)(void *priv, bool succ,
409                      DOM_SID *aliases, size_t num_aliases) =
410                 (void (*)(void *, bool, DOM_SID *, size_t))c;
411         char *aliases_str;
412         DOM_SID *sids = NULL;
413         size_t num_sids = 0;
414
415         if (!success) {
416                 DEBUG(5, ("Could not trigger getsidaliases\n"));
417                 cont(private_data, success, NULL, 0);
418                 return;
419         }
420
421         if (response->result != WINBINDD_OK) {
422                 DEBUG(5, ("getsidaliases returned an error\n"));
423                 cont(private_data, False, NULL, 0);
424                 return;
425         }
426
427         aliases_str = (char *)response->extra_data.data;
428
429         if (aliases_str == NULL) {
430                 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
431                 cont(private_data, True, NULL, 0);
432                 return;
433         }
434
435         if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
436                 DEBUG(0, ("Could not parse sids\n"));
437                 cont(private_data, False, NULL, 0);
438                 return;
439         }
440
441         cont(private_data, True, sids, num_sids);
442 }
443
444 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
445                                   TALLOC_CTX *mem_ctx,
446                                   const DOM_SID *sids, size_t num_sids,
447                                   void (*cont)(void *private_data,
448                                                bool success,
449                                                const DOM_SID *aliases,
450                                                size_t num_aliases),
451                                   void *private_data)
452 {
453         struct winbindd_request request;
454         char *sidstr = NULL;
455         ssize_t len;
456
457         if (num_sids == 0) {
458                 cont(private_data, True, NULL, 0);
459                 return;
460         }
461
462         if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
463                 cont(private_data, False, NULL, 0);
464                 return;
465         }
466
467         ZERO_STRUCT(request);
468         request.cmd = WINBINDD_DUAL_GETSIDALIASES;
469         request.extra_len = len;
470         request.extra_data.data = sidstr;
471
472         do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
473                         (void *)cont, private_data);
474 }
475
476 static void query_user_recv(TALLOC_CTX *mem_ctx, bool success,
477                             struct winbindd_response *response,
478                             void *c, void *private_data)
479 {
480         void (*cont)(void *priv, bool succ, const char *acct_name,
481                      const char *full_name, const char *homedir, 
482                      const char *shell, uint32 gid, uint32 group_rid) =
483                 (void (*)(void *, bool, const char *, const char *,
484                           const char *, const char *, uint32, uint32))c;
485
486         if (!success) {
487                 DEBUG(5, ("Could not trigger query_user\n"));
488                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
489                 return;
490         }
491
492         if (response->result != WINBINDD_OK) {
493                 DEBUG(5, ("query_user returned an error\n"));
494                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
495                 return;
496         }
497
498         cont(private_data, True, response->data.user_info.acct_name,
499              response->data.user_info.full_name,
500              response->data.user_info.homedir,
501              response->data.user_info.shell,
502              response->data.user_info.primary_gid,
503              response->data.user_info.group_rid);
504 }
505
506 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
507                       const DOM_SID *sid,
508                       void (*cont)(void *private_data, bool success,
509                                    const char *acct_name,
510                                    const char *full_name,
511                                    const char *homedir,
512                                    const char *shell,
513                                    gid_t gid,
514                                    uint32 group_rid),
515                       void *private_data)
516 {
517         struct winbindd_request request;
518         ZERO_STRUCT(request);
519         request.cmd = WINBINDD_DUAL_USERINFO;
520         sid_to_fstring(request.data.sid, sid);
521         do_async_domain(mem_ctx, domain, &request, query_user_recv,
522                         (void *)cont, private_data);
523 }
524
525 enum winbindd_result winbindd_dual_ping(struct winbindd_domain *domain,
526                                         struct winbindd_cli_state *state)
527 {
528         return WINBINDD_OK;
529 }