fc696cb9a1884069547ab1e526f4116fa26e6484
[ira/wip.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/cli_wbint.h"
23
24 struct wb_getpwsid_state {
25         struct winbindd_domain *user_domain;
26         struct tevent_context *ev;
27         struct dom_sid sid;
28         struct winbind_userinfo *userinfo;
29         struct winbindd_pw *pw;
30 };
31
32 static void wb_getpwsid_queryuser_done(struct tevent_req *subreq);
33 static void wb_getpwsid_lookupsid_done(struct tevent_req *subreq);
34 static void wb_getpwsid_sid2uid_done(struct tevent_req *subreq);
35 static void wb_getpwsid_sid2gid_done(struct tevent_req *subreq);
36
37 struct tevent_req *wb_getpwsid_send(TALLOC_CTX *mem_ctx,
38                                     struct tevent_context *ev,
39                                     const struct dom_sid *user_sid,
40                                     struct winbindd_pw *pw)
41 {
42         struct tevent_req *req, *subreq;
43         struct wb_getpwsid_state *state;
44
45         req = tevent_req_create(mem_ctx, &state, struct wb_getpwsid_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49         sid_copy(&state->sid, user_sid);
50         state->ev = ev;
51         state->pw = pw;
52
53         state->user_domain = find_domain_from_sid_noinit(user_sid);
54         if (state->user_domain == NULL) {
55                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
56                 return tevent_req_post(req, ev);
57         }
58
59         subreq = wb_queryuser_send(state, ev, &state->sid);
60         if (tevent_req_nomem(subreq, req)) {
61                 return tevent_req_post(req, ev);
62         }
63         tevent_req_set_callback(subreq, wb_getpwsid_queryuser_done, req);
64         return req;
65 }
66
67 static void wb_getpwsid_queryuser_done(struct tevent_req *subreq)
68 {
69         struct tevent_req *req = tevent_req_callback_data(
70                 subreq, struct tevent_req);
71         struct wb_getpwsid_state *state = tevent_req_data(
72                 req, struct wb_getpwsid_state);
73         NTSTATUS status;
74
75         status = wb_queryuser_recv(subreq, state, &state->userinfo);
76         TALLOC_FREE(subreq);
77         if (!NT_STATUS_IS_OK(status)) {
78                 tevent_req_nterror(req, status);
79                 return;
80         }
81
82         if ((state->userinfo->acct_name != NULL)
83             && (state->userinfo->acct_name[0] != '\0')) {
84                 /*
85                  * QueryUser got us a name, let's got directly to the
86                  * sid2uid step
87                  */
88                 subreq = wb_sid2uid_send(state, state->ev,
89                                          &state->userinfo->user_sid);
90                 if (tevent_req_nomem(subreq, req)) {
91                         return;
92                 }
93                 tevent_req_set_callback(subreq, wb_getpwsid_sid2uid_done, req);
94                 return;
95         }
96
97         /*
98          * QueryUser didn't get us a name, do it via LSA.
99          */
100         subreq = wb_lookupsid_send(state, state->ev,
101                                    &state->userinfo->user_sid);
102         if (tevent_req_nomem(subreq, req)) {
103                 return;
104         }
105         tevent_req_set_callback(subreq, wb_getpwsid_lookupsid_done, req);
106 }
107
108 static void wb_getpwsid_lookupsid_done(struct tevent_req *subreq)
109 {
110         struct tevent_req *req = tevent_req_callback_data(
111                 subreq, struct tevent_req);
112         struct wb_getpwsid_state *state = tevent_req_data(
113                 req, struct wb_getpwsid_state);
114         NTSTATUS status;
115         enum lsa_SidType type;
116         const char *domain;
117
118         status = wb_lookupsid_recv(subreq, state->userinfo, &type, &domain,
119                                    &state->userinfo->acct_name);
120         TALLOC_FREE(subreq);
121         if (!NT_STATUS_IS_OK(status)) {
122                 tevent_req_nterror(req, status);
123                 return;
124         }
125         subreq = wb_sid2uid_send(state, state->ev, &state->userinfo->user_sid);
126         if (tevent_req_nomem(subreq, req)) {
127                 return;
128         }
129         tevent_req_set_callback(subreq, wb_getpwsid_sid2uid_done, req);
130 }
131
132 static void wb_getpwsid_sid2uid_done(struct tevent_req *subreq)
133 {
134         struct tevent_req *req = tevent_req_callback_data(
135                 subreq, struct tevent_req);
136         struct wb_getpwsid_state *state = tevent_req_data(
137                 req, struct wb_getpwsid_state);
138         NTSTATUS status;
139
140         status = wb_sid2uid_recv(subreq, &state->pw->pw_uid);
141         TALLOC_FREE(subreq);
142         if (!NT_STATUS_IS_OK(status)) {
143                 tevent_req_nterror(req, status);
144                 return;
145         }
146         subreq = wb_sid2gid_send(state, state->ev,
147                                  &state->userinfo->group_sid);
148         if (tevent_req_nomem(subreq, req)) {
149                 return;
150         }
151         tevent_req_set_callback(subreq, wb_getpwsid_sid2gid_done, req);
152 }
153
154 static void wb_getpwsid_sid2gid_done(struct tevent_req *subreq)
155 {
156         struct tevent_req *req = tevent_req_callback_data(
157                 subreq, struct tevent_req);
158         struct wb_getpwsid_state *state = tevent_req_data(
159                 req, struct wb_getpwsid_state);
160         NTSTATUS status;
161         char *username;
162         char *mapped_name;
163
164         status = wb_sid2gid_recv(subreq, &state->pw->pw_gid);
165         TALLOC_FREE(subreq);
166         if (!NT_STATUS_IS_OK(status)) {
167                 tevent_req_nterror(req, status);
168                 return;
169         }
170
171         username = talloc_strdup_lower(state, state->userinfo->acct_name);
172         if (tevent_req_nomem(username, req)) {
173                 return;
174         }
175
176         status = normalize_name_map(state, state->user_domain, username,
177                                     &mapped_name);
178
179         if (NT_STATUS_IS_OK(status)
180             || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
181                 /*
182                  * normalize_name_map did something
183                  */
184                 fstrcpy(state->pw->pw_name, mapped_name);
185                 TALLOC_FREE(mapped_name);
186         } else {
187                 fill_domain_username(state->pw->pw_name,
188                                      state->user_domain->name,
189                                      username, True);
190         }
191         fstrcpy(state->pw->pw_passwd, "*");
192         fstrcpy(state->pw->pw_gecos, state->userinfo->full_name);
193
194         if (!fillup_pw_field(lp_template_homedir(), username,
195                              state->user_domain->name, state->pw->pw_uid,
196                              state->pw->pw_gid, state->userinfo->homedir,
197                              state->pw->pw_dir)) {
198                 DEBUG(5, ("Could not compose homedir\n"));
199                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
200                 return;
201         }
202
203         if (!fillup_pw_field(lp_template_shell(), state->pw->pw_name,
204                              state->user_domain->name, state->pw->pw_uid,
205                              state->pw->pw_gid, state->userinfo->shell,
206                              state->pw->pw_shell)) {
207                 DEBUG(5, ("Could not compose shell\n"));
208                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
209                 return;
210         }
211
212         tevent_req_done(req);
213 }
214
215 NTSTATUS wb_getpwsid_recv(struct tevent_req *req)
216 {
217         NTSTATUS status;
218
219         if (tevent_req_is_nterror(req, &status)) {
220                 return status;
221         }
222         return NT_STATUS_OK;
223 }