Merge branch 'master' of /home/tridge/samba/git/combined
[Samba/aatanasov.git] / source3 / winbindd / wb_fill_pwent.c
blob4f4819ca23026c870aeb3200ff7e7b52f9bdc831
1 /*
2 Unix SMB/CIFS implementation.
3 async fill_pwent
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/cli_wbint.h"
24 struct wb_fill_pwent_state {
25 struct tevent_context *ev;
26 struct wbint_userinfo *info;
27 struct winbindd_pw *pw;
30 static void wb_fill_pwent_sid2uid_done(struct tevent_req *subreq);
31 static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq);
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)
38 struct tevent_req *req, *subreq;
39 struct wb_fill_pwent_state *state;
41 req = tevent_req_create(mem_ctx, &state, struct wb_fill_pwent_state);
42 if (req == NULL) {
43 return NULL;
45 state->ev = ev;
46 state->info = info;
47 state->pw = pw;
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);
53 tevent_req_set_callback(subreq, wb_fill_pwent_sid2uid_done, req);
54 return req;
57 static void wb_fill_pwent_sid2uid_done(struct tevent_req *subreq)
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;
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;
72 subreq = wb_sid2gid_send(state, state->ev, &state->info->group_sid);
73 if (tevent_req_nomem(subreq, req)) {
74 return;
76 tevent_req_set_callback(subreq, wb_fill_pwent_sid2gid_done, req);
79 static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq)
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;
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;
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;
103 dom_name = domain->name;
105 /* Username */
107 fstrcpy(user_name, state->info->acct_name);
108 strlower_m(user_name);
109 status = normalize_name_map(state, domain, user_name, &mapped_name);
111 /* Basic removal of whitespace */
112 if (NT_STATUS_IS_OK(status)) {
113 fill_domain_username(output_username, dom_name, mapped_name,
114 true);
116 /* Complete name replacement */
117 else if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
118 fstrcpy(output_username, mapped_name);
120 /* No change at all */
121 else {
122 fill_domain_username(output_username, dom_name, user_name,
123 true);
126 fstrcpy(state->pw->pw_name, output_username);
127 fstrcpy(state->pw->pw_gecos, state->info->full_name);
129 /* Home directory and shell */
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;
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;
145 /* Password - set to "*" as we can't generate anything useful here.
146 Authentication can be done using the pam_winbind module. */
148 fstrcpy(state->pw->pw_passwd, "*");
149 tevent_req_done(req);
152 NTSTATUS wb_fill_pwent_recv(struct tevent_req *req)
154 return tevent_req_simple_recv_ntstatus(req);