winbindd: Do not use group_list->out.resume_index after free
[samba.git] / source4 / winbind / wb_cmd_lookupsid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo -s
5
6    Copyright (C) Volker Lendecke 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "winbind/wb_helper.h"
26 #include "smbd/service_task.h"
27 #include "libcli/security/security.h"
28
29 struct cmd_lookupsid_state {
30         struct composite_context *ctx;
31         const struct dom_sid *sid;
32         struct wb_sid_object *result;
33 };
34
35 static void lookupsid_recv_domain(struct composite_context *ctx);
36 static void lookupsid_recv_names(struct composite_context *ctx);
37
38 struct composite_context *wb_cmd_lookupsid_send(TALLOC_CTX *mem_ctx,
39                                                 struct wbsrv_service *service,
40                                                 const struct dom_sid *sid)
41 {
42         struct composite_context *result, *ctx;
43         struct cmd_lookupsid_state *state;
44
45         DEBUG(5, ("wb_cmd_lookupsid_send called\n"));
46         result = composite_create(mem_ctx, service->task->event_ctx);
47         if (result == NULL) goto failed;
48
49         state = talloc(result, struct cmd_lookupsid_state);
50         if (state == NULL) goto failed;
51         state->ctx = result;
52         result->private_data = state;
53
54         state->sid = dom_sid_dup(state, sid);
55         if (state->sid == NULL) goto failed;
56
57         ctx = wb_sid2domain_send(state, service, service->primary_sid);
58         if (ctx == NULL) goto failed;
59
60         ctx->async.fn = lookupsid_recv_domain;
61         ctx->async.private_data = state;
62         return result;
63
64  failed:
65         talloc_free(result);
66         return NULL;
67 }
68
69 static void lookupsid_recv_domain(struct composite_context *ctx)
70 {
71         struct cmd_lookupsid_state *state =
72                 talloc_get_type(ctx->async.private_data,
73                                 struct cmd_lookupsid_state);
74         struct wbsrv_domain *domain;
75
76         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
77         if (!composite_is_ok(state->ctx)) return;
78
79         ctx = wb_lsa_lookupsids_send(state, state->ctx->event_ctx,
80                                      domain->libnet_ctx->lsa.lsa_handle,
81                                      &domain->libnet_ctx->lsa.handle, 1, &state->sid);
82         composite_continue(state->ctx, ctx, lookupsid_recv_names, state);
83 }
84
85 static void lookupsid_recv_names(struct composite_context *ctx)
86 {
87         struct cmd_lookupsid_state *state =
88                 talloc_get_type(ctx->async.private_data,
89                                 struct cmd_lookupsid_state);
90         struct wb_sid_object **names;
91
92         state->ctx->status = wb_lsa_lookupsids_recv(ctx, state, &names);
93         if (!composite_is_ok(state->ctx)) return;
94
95         state->result = names[0];
96         composite_done(state->ctx);
97 }
98
99 NTSTATUS wb_cmd_lookupsid_recv(struct composite_context *c,
100                                TALLOC_CTX *mem_ctx,
101                                struct wb_sid_object **sid)
102 {
103         struct cmd_lookupsid_state *state =
104                 talloc_get_type(c->private_data, struct cmd_lookupsid_state);
105         NTSTATUS status = composite_wait(c);
106         if (NT_STATUS_IS_OK(status)) {
107                 *sid = talloc_steal(mem_ctx, state->result);
108         }
109         talloc_free(state);
110         return status;
111 }
112
113 NTSTATUS wb_cmd_lookupsid(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
114                           const struct dom_sid *sid,
115                           struct wb_sid_object **name)
116 {
117         struct composite_context *c =
118                 wb_cmd_lookupsid_send(mem_ctx, service, sid);
119         return wb_cmd_lookupsid_recv(c, mem_ctx, name);
120 }