r12608: Remove some unused #include lines.
[samba.git] / source4 / winbind / wb_cmd_lookupname.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo -n
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27
28 struct cmd_lookupname_state {
29         struct composite_context *ctx;
30         const char *name;
31         struct wb_sid_object *result;
32 };
33
34 static void lookupname_recv_domain(struct composite_context *ctx);
35 static void lookupname_recv_sids(struct composite_context *ctx);
36
37 struct composite_context *wb_cmd_lookupname_send(TALLOC_CTX *mem_ctx,
38                                                  struct wbsrv_service *service,
39                                                  const char *dom_name,
40                                                  const char *name)
41 {
42         struct composite_context *result, *ctx;
43         struct cmd_lookupname_state *state;
44
45         result = talloc(mem_ctx, struct composite_context);
46         if (result == NULL) goto failed;
47         result->state = COMPOSITE_STATE_IN_PROGRESS;
48         result->async.fn = NULL;
49         result->event_ctx = service->task->event_ctx;
50
51         state = talloc(result, struct cmd_lookupname_state);
52         if (state == NULL) goto failed;
53         state->ctx = result;
54         result->private_data = state;
55
56         state->name = talloc_asprintf(state, "%s\\%s", dom_name, name);
57         if (state->name == NULL) goto failed;
58
59         ctx = wb_sid2domain_send(state, service, service->primary_sid);
60         if (ctx == NULL) goto failed;
61
62         ctx->async.fn = lookupname_recv_domain;
63         ctx->async.private_data = state;
64         return result;
65
66  failed:
67         talloc_free(result);
68         return NULL;
69 }
70
71 static void lookupname_recv_domain(struct composite_context *ctx)
72 {
73         struct cmd_lookupname_state *state =
74                 talloc_get_type(ctx->async.private_data,
75                                 struct cmd_lookupname_state);
76         struct wbsrv_domain *domain;
77
78         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
79         if (!composite_is_ok(state->ctx)) return;
80
81         ctx = wb_lsa_lookupnames_send(state, domain->lsa_pipe,
82                                       domain->lsa_policy, 1, &state->name);
83         composite_continue(state->ctx, ctx, lookupname_recv_sids, state);
84 }
85
86 static void lookupname_recv_sids(struct composite_context *ctx)
87 {
88         struct cmd_lookupname_state *state =
89                 talloc_get_type(ctx->async.private_data,
90                                 struct cmd_lookupname_state);
91         struct wb_sid_object **sids;
92
93         state->ctx->status = wb_lsa_lookupnames_recv(ctx, state, &sids);
94         if (!composite_is_ok(state->ctx)) return;
95
96         state->result = sids[0];
97         composite_done(state->ctx);
98 }
99
100 NTSTATUS wb_cmd_lookupname_recv(struct composite_context *c,
101                                 TALLOC_CTX *mem_ctx,
102                                 struct wb_sid_object **sid)
103 {
104         struct cmd_lookupname_state *state =
105                 talloc_get_type(c->private_data, struct cmd_lookupname_state);
106         NTSTATUS status = composite_wait(c);
107         if (NT_STATUS_IS_OK(status)) {
108                 *sid = talloc_steal(mem_ctx, state->result);
109         }
110         talloc_free(state);
111         return status;
112 }
113
114 NTSTATUS wb_cmd_lookupname(TALLOC_CTX *mem_ctx,
115                            struct wbsrv_service *service,
116                            const char *dom_name,
117                            const char *name,
118                            struct wb_sid_object **sid)
119 {
120         struct composite_context *c =
121                 wb_cmd_lookupname_send(mem_ctx, service, dom_name, name);
122         return wb_cmd_lookupname_recv(c, mem_ctx, sid);
123 }