r13924: Split more prototypes out of include/proto.h + initial work on header
[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 "winbind/wb_async_helpers.h"
27 #include "winbind/wb_helper.h"
28 #include "smbd/service_task.h"
29
30 struct cmd_lookupname_state {
31         struct composite_context *ctx;
32         const char *name;
33         struct wb_sid_object *result;
34 };
35
36 static void lookupname_recv_domain(struct composite_context *ctx);
37 static void lookupname_recv_sids(struct composite_context *ctx);
38
39 struct composite_context *wb_cmd_lookupname_send(TALLOC_CTX *mem_ctx,
40                                                  struct wbsrv_service *service,
41                                                  const char *dom_name,
42                                                  const char *name)
43 {
44         struct composite_context *result, *ctx;
45         struct cmd_lookupname_state *state;
46
47         result = talloc(mem_ctx, struct composite_context);
48         if (result == NULL) goto failed;
49         result->state = COMPOSITE_STATE_IN_PROGRESS;
50         result->async.fn = NULL;
51         result->event_ctx = service->task->event_ctx;
52
53         state = talloc(result, struct cmd_lookupname_state);
54         if (state == NULL) goto failed;
55         state->ctx = result;
56         result->private_data = state;
57
58         state->name = talloc_asprintf(state, "%s\\%s", dom_name, name);
59         if (state->name == NULL) goto failed;
60
61         ctx = wb_sid2domain_send(state, service, service->primary_sid);
62         if (ctx == NULL) goto failed;
63
64         ctx->async.fn = lookupname_recv_domain;
65         ctx->async.private_data = state;
66         return result;
67
68  failed:
69         talloc_free(result);
70         return NULL;
71 }
72
73 static void lookupname_recv_domain(struct composite_context *ctx)
74 {
75         struct cmd_lookupname_state *state =
76                 talloc_get_type(ctx->async.private_data,
77                                 struct cmd_lookupname_state);
78         struct wbsrv_domain *domain;
79
80         state->ctx->status = wb_sid2domain_recv(ctx, &domain);
81         if (!composite_is_ok(state->ctx)) return;
82
83         ctx = wb_lsa_lookupnames_send(state, domain->lsa_pipe,
84                                       domain->lsa_policy, 1, &state->name);
85         composite_continue(state->ctx, ctx, lookupname_recv_sids, state);
86 }
87
88 static void lookupname_recv_sids(struct composite_context *ctx)
89 {
90         struct cmd_lookupname_state *state =
91                 talloc_get_type(ctx->async.private_data,
92                                 struct cmd_lookupname_state);
93         struct wb_sid_object **sids;
94
95         state->ctx->status = wb_lsa_lookupnames_recv(ctx, state, &sids);
96         if (!composite_is_ok(state->ctx)) return;
97
98         state->result = sids[0];
99         composite_done(state->ctx);
100 }
101
102 NTSTATUS wb_cmd_lookupname_recv(struct composite_context *c,
103                                 TALLOC_CTX *mem_ctx,
104                                 struct wb_sid_object **sid)
105 {
106         struct cmd_lookupname_state *state =
107                 talloc_get_type(c->private_data, struct cmd_lookupname_state);
108         NTSTATUS status = composite_wait(c);
109         if (NT_STATUS_IS_OK(status)) {
110                 *sid = talloc_steal(mem_ctx, state->result);
111         }
112         talloc_free(state);
113         return status;
114 }
115
116 NTSTATUS wb_cmd_lookupname(TALLOC_CTX *mem_ctx,
117                            struct wbsrv_service *service,
118                            const char *dom_name,
119                            const char *name,
120                            struct wb_sid_object **sid)
121 {
122         struct composite_context *c =
123                 wb_cmd_lookupname_send(mem_ctx, service, dom_name, name);
124         return wb_cmd_lookupname_recv(c, mem_ctx, sid);
125 }