r11094: Connect to SAM, implement getdcname
[jelmer/samba4-debian.git] / source / 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_stream.h"
27
28 struct cmd_lookupname_state {
29         struct composite_context *ctx;
30         struct wbsrv_call *call;
31         struct wbsrv_domain *domain;
32         const char *name;
33         struct wb_sid_object *result;
34 };
35
36 static struct composite_context *lookupname_send_req(void *p);
37 static NTSTATUS lookupname_recv_req(struct composite_context *ctx, void *p);
38
39 struct composite_context *wb_cmd_lookupname_send(struct wbsrv_call *call,
40                                                  const char *name)
41 {
42         struct cmd_lookupname_state *state;
43         struct wbsrv_service *service = call->wbconn->listen_socket->service;
44
45         state = talloc(NULL, struct cmd_lookupname_state);
46         state->domain = service->domains;
47         state->call = call;
48         state->name = talloc_strdup(state, name);
49         state->ctx = wb_queue_domain_send(state, state->domain,
50                                           call->event_ctx,
51                                           call->wbconn->conn->msg_ctx,
52                                           lookupname_send_req,
53                                           lookupname_recv_req,
54                                           state);
55         if (state->ctx == NULL) {
56                 talloc_free(state);
57                 return NULL;
58         }
59         state->ctx->private_data = state;
60         return state->ctx;
61 }
62
63 static struct composite_context *lookupname_send_req(void *p)
64 {
65         struct cmd_lookupname_state *state =
66                 talloc_get_type(p, struct cmd_lookupname_state);
67
68         return wb_lsa_lookupnames_send(state->domain->lsa_pipe,
69                                        state->domain->lsa_policy,
70                                        1, &state->name);
71 }
72
73 static NTSTATUS lookupname_recv_req(struct composite_context *ctx, void *p)
74 {
75         struct cmd_lookupname_state *state =
76                 talloc_get_type(p, struct cmd_lookupname_state);
77         struct wb_sid_object **sids;
78         NTSTATUS status;
79
80         status = wb_lsa_lookupnames_recv(ctx, state, &sids);
81         if (NT_STATUS_IS_OK(status)) {
82                 state->result = sids[0];
83         }
84         return status;
85 }
86
87 NTSTATUS wb_cmd_lookupname_recv(struct composite_context *c,
88                                 TALLOC_CTX *mem_ctx,
89                                 struct wb_sid_object **sid)
90 {
91         struct cmd_lookupname_state *state =
92                 talloc_get_type(c->private_data, struct cmd_lookupname_state);
93         NTSTATUS status = composite_wait(c);
94         if (NT_STATUS_IS_OK(status)) {
95                 *sid = talloc_steal(mem_ctx, state->result);
96         }
97         talloc_free(state);
98         return status;
99 }
100
101 NTSTATUS wb_cmd_lookupname(struct wbsrv_call *call, const char *name,
102                            TALLOC_CTX *mem_ctx, struct wb_sid_object **sid)
103 {
104         struct composite_context *c =
105                 wb_cmd_lookupname_send(call, name);
106         return wb_cmd_lookupname_recv(c, mem_ctx, sid);
107 }