winbindd: error handling in rpc_lookup_sids()
[amitay/samba.git] / source3 / winbindd / wb_getpwsid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async getpwsid
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/ndr_winbind_c.h"
23 #include "../libcli/security/security.h"
24
25 struct wb_getpwsid_state {
26         struct tevent_context *ev;
27         struct dom_sid sid;
28         struct wbint_userinfo *userinfo;
29         struct winbindd_pw *pw;
30 };
31
32 static void wb_getpwsid_queryuser_done(struct tevent_req *subreq);
33
34 struct tevent_req *wb_getpwsid_send(TALLOC_CTX *mem_ctx,
35                                     struct tevent_context *ev,
36                                     const struct dom_sid *user_sid,
37                                     struct winbindd_pw *pw)
38 {
39         struct tevent_req *req, *subreq;
40         struct wb_getpwsid_state *state;
41
42         req = tevent_req_create(mem_ctx, &state, struct wb_getpwsid_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46         sid_copy(&state->sid, user_sid);
47         state->ev = ev;
48         state->pw = pw;
49
50         subreq = wb_queryuser_send(state, ev, &state->sid);
51         if (tevent_req_nomem(subreq, req)) {
52                 return tevent_req_post(req, ev);
53         }
54         tevent_req_set_callback(subreq, wb_getpwsid_queryuser_done, req);
55         return req;
56 }
57
58 static void wb_getpwsid_queryuser_done(struct tevent_req *subreq)
59 {
60         struct tevent_req *req = tevent_req_callback_data(
61                 subreq, struct tevent_req);
62         struct wb_getpwsid_state *state = tevent_req_data(
63                 req, struct wb_getpwsid_state);
64         struct winbindd_pw *pw = state->pw;
65         struct wbint_userinfo *info;
66         fstring acct_name, output_username;
67         char *tmp;
68         NTSTATUS status;
69
70         status = wb_queryuser_recv(subreq, state, &state->userinfo);
71         TALLOC_FREE(subreq);
72         if (tevent_req_nterror(req, status)) {
73                 return;
74         }
75         info = state->userinfo;
76
77         pw->pw_uid = info->uid;
78         pw->pw_gid = info->primary_gid;
79
80         fstrcpy(acct_name, info->acct_name);
81         if (!strlower_m(acct_name)) {
82                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
83                 return;
84         }
85
86         fill_domain_username(output_username, info->domain_name,
87                              acct_name, true);
88         strlcpy(pw->pw_name, output_username, sizeof(pw->pw_name));
89
90         strlcpy(pw->pw_gecos, info->full_name ? info->full_name : "",
91                 sizeof(pw->pw_gecos));
92
93         tmp = talloc_sub_specified(
94                 state, info->homedir, acct_name,
95                 info->primary_group_name, info->domain_name,
96                 pw->pw_uid, pw->pw_gid);
97         if (tevent_req_nomem(tmp, req)) {
98                 return;
99         }
100         strlcpy(pw->pw_dir, tmp, sizeof(pw->pw_dir));
101         TALLOC_FREE(tmp);
102
103         tmp = talloc_sub_specified(
104                 state, info->shell, info->acct_name,
105                 info->primary_group_name, info->domain_name,
106                 pw->pw_uid, pw->pw_gid);
107         if (tevent_req_nomem(tmp, req)) {
108                 return;
109         }
110         strlcpy(pw->pw_shell, tmp, sizeof(pw->pw_shell));
111         TALLOC_FREE(tmp);
112
113         strlcpy(pw->pw_passwd, "*", sizeof(pw->pw_passwd));
114
115         tevent_req_done(req);
116 }
117
118 NTSTATUS wb_getpwsid_recv(struct tevent_req *req)
119 {
120         return tevent_req_simple_recv_ntstatus(req);
121 }