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