26686bf9f0f6960e9739e5c199e371b7ba9346d5
[nivanova/samba-autobuild/.git] / source3 / winbindd / winbindd_getpwnam.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_GETPWNAM
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 "passdb/lookup_sid.h" /* only for LOOKUP_NAME_NO_NSS flag */
23
24 struct winbindd_getpwnam_state {
25         struct tevent_context *ev;
26         fstring domname;
27         fstring username;
28         struct dom_sid sid;
29         enum lsa_SidType type;
30         struct winbindd_pw pw;
31 };
32
33 static void winbindd_getpwnam_lookupname_done(struct tevent_req *subreq);
34 static void winbindd_getpwnam_done(struct tevent_req *subreq);
35
36 struct tevent_req *winbindd_getpwnam_send(TALLOC_CTX *mem_ctx,
37                                           struct tevent_context *ev,
38                                           struct winbindd_cli_state *cli,
39                                           struct winbindd_request *request)
40 {
41         struct tevent_req *req, *subreq;
42         struct winbindd_getpwnam_state *state;
43         char *domuser, *mapped_user;
44         NTSTATUS status;
45
46         req = tevent_req_create(mem_ctx, &state,
47                                 struct winbindd_getpwnam_state);
48         if (req == NULL) {
49                 return NULL;
50         }
51         state->ev = ev;
52
53         /* Ensure null termination */
54         request->data.username[sizeof(request->data.username)-1]='\0';
55
56         DEBUG(3, ("getpwnam %s\n", request->data.username));
57
58         domuser = request->data.username;
59
60         status = normalize_name_unmap(state, domuser, &mapped_user);
61
62         if (NT_STATUS_IS_OK(status)
63             || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
64                 /* normalize_name_unmapped did something */
65                 domuser = mapped_user;
66         }
67
68         if (!parse_domain_user(domuser, state->domname, state->username)) {
69                 DEBUG(5, ("Could not parse domain user: %s\n", domuser));
70                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
71                 return tevent_req_post(req, ev);
72         }
73
74         subreq = wb_lookupname_send(state, ev,
75                                     state->domname,
76                                     state->domname,
77                                     state->username,
78                                     LOOKUP_NAME_NO_NSS);
79         if (tevent_req_nomem(subreq, req)) {
80                 return tevent_req_post(req, ev);
81         }
82         tevent_req_set_callback(subreq, winbindd_getpwnam_lookupname_done,
83                                 req);
84         return req;
85 }
86
87 static void winbindd_getpwnam_lookupname_done(struct tevent_req *subreq)
88 {
89         struct tevent_req *req = tevent_req_callback_data(
90                 subreq, struct tevent_req);
91         struct winbindd_getpwnam_state *state = tevent_req_data(
92                 req, struct winbindd_getpwnam_state);
93         NTSTATUS status;
94
95         status = wb_lookupname_recv(subreq, &state->sid, &state->type);
96         TALLOC_FREE(subreq);
97         if (tevent_req_nterror(req, status)) {
98                 return;
99         }
100
101         subreq = wb_getpwsid_send(state, state->ev, &state->sid, &state->pw);
102         if (tevent_req_nomem(subreq, req)) {
103                 return;
104         }
105         tevent_req_set_callback(subreq, winbindd_getpwnam_done, req);
106 }
107
108 static void winbindd_getpwnam_done(struct tevent_req *subreq)
109 {
110         struct tevent_req *req = tevent_req_callback_data(
111                 subreq, struct tevent_req);
112         NTSTATUS status;
113
114         status = wb_getpwsid_recv(subreq);
115         TALLOC_FREE(subreq);
116         if (tevent_req_nterror(req, status)) {
117                 return;
118         }
119         tevent_req_done(req);
120 }
121
122 NTSTATUS winbindd_getpwnam_recv(struct tevent_req *req,
123                                 struct winbindd_response *response)
124 {
125         struct winbindd_getpwnam_state *state = tevent_req_data(
126                 req, struct winbindd_getpwnam_state);
127         NTSTATUS status;
128
129         if (tevent_req_is_nterror(req, &status)) {
130                 DEBUG(5, ("Could not convert sid %s: %s\n",
131                           sid_string_dbg(&state->sid), nt_errstr(status)));
132                 return status;
133         }
134         response->data.pw = state->pw;
135         return NT_STATUS_OK;
136 }