27b8d299512fda88d5589b91500dfe0bd620beee
[metze/samba/wip.git] / source3 / winbindd / wb_queryuser.c
1 /*
2    Unix SMB/CIFS implementation.
3    async queryuser
4    Copyright (C) Volker Lendecke 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/cli_wbint.h"
23
24 struct wb_queryuser_state {
25         struct dom_sid sid;
26         struct wbint_userinfo info;
27 };
28
29 static void wb_queryuser_done(struct tevent_req *subreq);
30
31 struct tevent_req *wb_queryuser_send(TALLOC_CTX *mem_ctx,
32                                      struct tevent_context *ev,
33                                      const struct dom_sid *user_sid)
34 {
35         struct tevent_req *req, *subreq;
36         struct wb_queryuser_state *state;
37         struct winbindd_domain *domain;
38         struct winbind_userinfo info;
39         NTSTATUS status;
40
41         req = tevent_req_create(mem_ctx, &state, struct wb_queryuser_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45         sid_copy(&state->sid, user_sid);
46
47         domain = find_domain_from_sid_noinit(user_sid);
48         if (domain == NULL) {
49                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
50                 return tevent_req_post(req, ev);
51         }
52
53         status = wcache_query_user(domain, state, &state->sid, &info);
54         if (NT_STATUS_IS_OK(status)) {
55                 state->info.acct_name = info.acct_name;
56                 state->info.full_name = info.full_name;
57                 state->info.homedir = info.homedir;
58                 state->info.shell = info.shell;
59                 state->info.primary_gid = info.primary_gid;
60                 sid_copy(&state->info.user_sid, &info.user_sid);
61                 sid_copy(&state->info.group_sid, &info.group_sid);
62                 tevent_req_done(req);
63                 return tevent_req_post(req, ev);
64         }
65
66         subreq = rpccli_wbint_QueryUser_send(state, ev, domain->child.rpccli,
67                                              &state->sid, &state->info);
68         if (tevent_req_nomem(subreq, req)) {
69                 return tevent_req_post(req, ev);
70         }
71         tevent_req_set_callback(subreq, wb_queryuser_done, req);
72         return req;
73 }
74
75 static void wb_queryuser_done(struct tevent_req *subreq)
76 {
77         struct tevent_req *req = tevent_req_callback_data(
78                 subreq, struct tevent_req);
79         struct wb_queryuser_state *state = tevent_req_data(
80                 req, struct wb_queryuser_state);
81         NTSTATUS status, result;
82
83         status = rpccli_wbint_QueryUser_recv(subreq, state, &result);
84         TALLOC_FREE(subreq);
85         if (!NT_STATUS_IS_OK(status)) {
86                 tevent_req_nterror(req, status);
87                 return;
88         }
89         if (!NT_STATUS_IS_OK(result)) {
90                 tevent_req_nterror(req, result);
91                 return;
92         }
93         tevent_req_done(req);
94 }
95
96 NTSTATUS wb_queryuser_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
97                            struct winbind_userinfo **pinfo)
98 {
99         struct wb_queryuser_state *state = tevent_req_data(
100                 req, struct wb_queryuser_state);
101         struct winbind_userinfo *info;
102         NTSTATUS status;
103
104         if (tevent_req_is_nterror(req, &status)) {
105                 return status;
106         }
107
108         info = talloc(mem_ctx, struct winbind_userinfo);
109         if (info == NULL) {
110                 return NT_STATUS_NO_MEMORY;
111         }
112         info->acct_name = talloc_move(info, &state->info.acct_name);
113         info->full_name = talloc_move(info, &state->info.full_name);
114         info->homedir = talloc_move(info, &state->info.homedir);
115         info->shell = talloc_move(info, &state->info.shell);
116         info->primary_gid = state->info.primary_gid;
117         sid_copy(&info->user_sid, &state->info.user_sid);
118         sid_copy(&info->group_sid, &state->info.group_sid);
119         *pinfo = info;
120         return NT_STATUS_OK;
121 }