s3:winbind: Add async fill_pwent
[kai/samba.git] / source3 / winbindd / wb_fill_pwent.c
1 /*
2    Unix SMB/CIFS implementation.
3    async fill_pwent
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_fill_pwent_state {
25         struct tevent_context *ev;
26         struct wbint_userinfo *info;
27         struct winbindd_pw *pw;
28 };
29
30 static void wb_fill_pwent_sid2uid_done(struct tevent_req *subreq);
31 static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq);
32
33 struct tevent_req *wb_fill_pwent_send(TALLOC_CTX *mem_ctx,
34                                       struct tevent_context *ev,
35                                       struct wbint_userinfo *info,
36                                       struct winbindd_pw *pw)
37 {
38         struct tevent_req *req, *subreq;
39         struct wb_fill_pwent_state *state;
40
41         req = tevent_req_create(mem_ctx, &state, struct wb_fill_pwent_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45         state->ev = ev;
46         state->info = info;
47         state->pw = pw;
48
49         subreq = wb_sid2uid_send(state, state->ev, &state->info->user_sid);
50         if (tevent_req_nomem(subreq, req)) {
51                 return tevent_req_post(req, ev);
52         }
53         tevent_req_set_callback(subreq, wb_fill_pwent_sid2uid_done, req);
54         return req;
55 }
56
57 static void wb_fill_pwent_sid2uid_done(struct tevent_req *subreq)
58 {
59         struct tevent_req *req = tevent_req_callback_data(
60                 subreq, struct tevent_req);
61         struct wb_fill_pwent_state *state = tevent_req_data(
62                 req, struct wb_fill_pwent_state);
63         NTSTATUS status;
64
65         status = wb_sid2uid_recv(subreq, &state->pw->pw_uid);
66         TALLOC_FREE(subreq);
67         if (!NT_STATUS_IS_OK(status)) {
68                 tevent_req_nterror(req, status);
69                 return;
70         }
71
72         subreq = wb_sid2gid_send(state, state->ev, &state->info->group_sid);
73         if (tevent_req_nomem(subreq, req)) {
74                 return;
75         }
76         tevent_req_set_callback(subreq, wb_fill_pwent_sid2gid_done, req);
77 }
78
79 static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq)
80 {
81         struct tevent_req *req = tevent_req_callback_data(
82                 subreq, struct tevent_req);
83         struct wb_fill_pwent_state *state = tevent_req_data(
84                 req, struct wb_fill_pwent_state);
85         struct winbindd_domain *domain;
86         char *dom_name;
87         fstring user_name, output_username;
88         char *mapped_name = NULL;
89         NTSTATUS status;
90
91         status = wb_sid2gid_recv(subreq, &state->pw->pw_gid);
92         TALLOC_FREE(subreq);
93         if (!NT_STATUS_IS_OK(status)) {
94                 tevent_req_nterror(req, status);
95                 return;
96         }
97
98         domain = find_domain_from_sid_noinit(&state->info->user_sid);
99         if (domain == NULL) {
100                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
101                 return;
102         }
103         dom_name = domain->name;
104
105         /* Username */
106
107         fstrcpy(user_name, state->info->acct_name);
108         strlower_m(user_name);
109         status = normalize_name_map(state, domain, user_name, &mapped_name);
110
111         /* Basic removal of whitespace */
112         if (NT_STATUS_IS_OK(status)) {
113                 fill_domain_username(output_username, dom_name, mapped_name,
114                                      true);
115         }
116         /* Complete name replacement */
117         else if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
118                 fstrcpy(output_username, mapped_name);
119         }
120         /* No change at all */
121         else {
122                 fill_domain_username(output_username, dom_name, user_name,
123                                      true);
124         }
125
126         fstrcpy(state->pw->pw_name, output_username);
127         fstrcpy(state->pw->pw_gecos, state->info->full_name);
128
129         /* Home directory and shell */
130
131         if (!fillup_pw_field(lp_template_homedir(), user_name, dom_name,
132                              state->pw->pw_uid, state->pw->pw_gid,
133                              state->info->homedir, state->pw->pw_dir)) {
134                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
135                 return;
136         }
137
138         if (!fillup_pw_field(lp_template_shell(), user_name, dom_name,
139                              state->pw->pw_uid, state->pw->pw_gid,
140                              state->info->shell, state->pw->pw_shell)) {
141                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
142                 return;
143         }
144
145         /* Password - set to "*" as we can't generate anything useful here.
146            Authentication can be done using the pam_winbind module. */
147
148         fstrcpy(state->pw->pw_passwd, "*");
149         tevent_req_done(req);
150 }
151
152 NTSTATUS wb_fill_pwent_recv(struct tevent_req *req)
153 {
154         return tevent_req_simple_recv_ntstatus(req);
155 }