a8f32f2fc94187b886bc306ea81c3b6088626a78
[gd/samba-autobuild/.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         if (dom_sid_in_domain(&global_sid_Unix_Users, user_sid)) {
51                 /* unmapped Unix users must be resolved locally */
52                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
53                 return tevent_req_post(req, ev);
54         }
55
56         subreq = wb_queryuser_send(state, ev, &state->sid);
57         if (tevent_req_nomem(subreq, req)) {
58                 return tevent_req_post(req, ev);
59         }
60         tevent_req_set_callback(subreq, wb_getpwsid_queryuser_done, req);
61         return req;
62 }
63
64 static void wb_getpwsid_queryuser_done(struct tevent_req *subreq)
65 {
66         struct tevent_req *req = tevent_req_callback_data(
67                 subreq, struct tevent_req);
68         struct wb_getpwsid_state *state = tevent_req_data(
69                 req, struct wb_getpwsid_state);
70         struct winbindd_pw *pw = state->pw;
71         struct wbint_userinfo *info;
72         fstring acct_name;
73         const char *output_username;
74         char *mapped_name = NULL;
75         char *tmp;
76         NTSTATUS status;
77
78         status = wb_queryuser_recv(subreq, state, &state->userinfo);
79         TALLOC_FREE(subreq);
80         if (tevent_req_nterror(req, status)) {
81                 return;
82         }
83         info = state->userinfo;
84
85         pw->pw_uid = info->uid;
86         pw->pw_gid = info->primary_gid;
87
88         fstrcpy(acct_name, info->acct_name);
89         if (!strlower_m(acct_name)) {
90                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
91                 return;
92         }
93
94         /*
95          * TODO:
96          * This function should be called in 'idmap winbind child'. It shouldn't
97          * be a blocking call, but for this we need to add a new function for
98          * winbind.idl. This is a fix which can be backported for now.
99          */
100         status = normalize_name_map(state,
101                                     info->domain_name,
102                                     acct_name,
103                                     &mapped_name);
104         if (NT_STATUS_IS_OK(status)) {
105                 output_username = fill_domain_username_talloc(state,
106                                      info->domain_name,
107                                      mapped_name, true);
108                 if (output_username == NULL) {
109                         tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
110                         return;
111                 }
112                 fstrcpy(acct_name, mapped_name);
113         } else if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
114                 fstrcpy(acct_name, mapped_name);
115         } else {
116                 output_username = fill_domain_username_talloc(state,
117                                      info->domain_name,
118                                      acct_name, true);
119                 if (output_username == NULL) {
120                         tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
121                         return;
122                 }
123         }
124
125         strlcpy(pw->pw_name, output_username, sizeof(pw->pw_name));
126
127         strlcpy(pw->pw_gecos, info->full_name ? info->full_name : "",
128                 sizeof(pw->pw_gecos));
129
130         tmp = talloc_sub_specified(
131                 state, info->homedir, acct_name,
132                 info->primary_group_name, info->domain_name,
133                 pw->pw_uid, pw->pw_gid);
134         if (tevent_req_nomem(tmp, req)) {
135                 return;
136         }
137         strlcpy(pw->pw_dir, tmp, sizeof(pw->pw_dir));
138         TALLOC_FREE(tmp);
139
140         tmp = talloc_sub_specified(
141                 state, info->shell, acct_name,
142                 info->primary_group_name, info->domain_name,
143                 pw->pw_uid, pw->pw_gid);
144         if (tevent_req_nomem(tmp, req)) {
145                 return;
146         }
147         strlcpy(pw->pw_shell, tmp, sizeof(pw->pw_shell));
148         TALLOC_FREE(tmp);
149
150         strlcpy(pw->pw_passwd, "*", sizeof(pw->pw_passwd));
151
152         tevent_req_done(req);
153 }
154
155 NTSTATUS wb_getpwsid_recv(struct tevent_req *req)
156 {
157         return tevent_req_simple_recv_ntstatus(req);
158 }