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