gpo: Add CSE for applying smb.conf
[Samba.git] / source3 / winbindd / winbindd_getpwnam.c
blob6f49ea9b356f6726cf3477d4d3c24fca83d8aa03
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_GETPWNAM
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 "passdb/lookup_sid.h" /* only for LOOKUP_NAME_NO_NSS flag */
23 #include "libcli/security/dom_sid.h"
25 struct winbindd_getpwnam_state {
26 struct tevent_context *ev;
27 fstring namespace;
28 fstring domname;
29 fstring username;
30 struct dom_sid sid;
31 enum lsa_SidType type;
32 struct winbindd_pw pw;
35 static void winbindd_getpwnam_lookupname_done(struct tevent_req *subreq);
36 static void winbindd_getpwnam_done(struct tevent_req *subreq);
38 struct tevent_req *winbindd_getpwnam_send(TALLOC_CTX *mem_ctx,
39 struct tevent_context *ev,
40 struct winbindd_cli_state *cli,
41 struct winbindd_request *request)
43 struct tevent_req *req, *subreq;
44 struct winbindd_getpwnam_state *state;
45 char *domuser, *mapped_user;
46 NTSTATUS status;
47 bool ok;
49 req = tevent_req_create(mem_ctx, &state,
50 struct winbindd_getpwnam_state);
51 if (req == NULL) {
52 return NULL;
54 state->ev = ev;
56 /* Ensure null termination */
57 request->data.username[sizeof(request->data.username)-1]='\0';
59 DBG_NOTICE("[%s (%u)] getpwnam %s\n",
60 cli->client_name,
61 (unsigned int)cli->pid,
62 request->data.username);
64 domuser = request->data.username;
66 status = normalize_name_unmap(state, domuser, &mapped_user);
68 if (NT_STATUS_IS_OK(status)
69 || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
70 /* normalize_name_unmapped did something */
71 domuser = mapped_user;
74 ok = parse_domain_user(domuser,
75 state->namespace,
76 state->domname,
77 state->username);
78 if (!ok) {
79 DEBUG(5, ("Could not parse domain user: %s\n", domuser));
80 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
81 return tevent_req_post(req, ev);
84 subreq = wb_lookupname_send(state, ev,
85 state->namespace,
86 state->domname,
87 state->username,
88 LOOKUP_NAME_NO_NSS);
89 if (tevent_req_nomem(subreq, req)) {
90 return tevent_req_post(req, ev);
92 tevent_req_set_callback(subreq, winbindd_getpwnam_lookupname_done,
93 req);
94 return req;
97 static void winbindd_getpwnam_lookupname_done(struct tevent_req *subreq)
99 struct tevent_req *req = tevent_req_callback_data(
100 subreq, struct tevent_req);
101 struct winbindd_getpwnam_state *state = tevent_req_data(
102 req, struct winbindd_getpwnam_state);
103 NTSTATUS status;
105 status = wb_lookupname_recv(subreq, &state->sid, &state->type);
106 TALLOC_FREE(subreq);
107 if (tevent_req_nterror(req, status)) {
108 return;
111 subreq = wb_getpwsid_send(state, state->ev, &state->sid, &state->pw);
112 if (tevent_req_nomem(subreq, req)) {
113 return;
115 tevent_req_set_callback(subreq, winbindd_getpwnam_done, req);
118 static void winbindd_getpwnam_done(struct tevent_req *subreq)
120 struct tevent_req *req = tevent_req_callback_data(
121 subreq, struct tevent_req);
122 NTSTATUS status;
124 status = wb_getpwsid_recv(subreq);
125 TALLOC_FREE(subreq);
126 if (tevent_req_nterror(req, status)) {
127 return;
129 tevent_req_done(req);
132 NTSTATUS winbindd_getpwnam_recv(struct tevent_req *req,
133 struct winbindd_response *response)
135 struct winbindd_getpwnam_state *state = tevent_req_data(
136 req, struct winbindd_getpwnam_state);
137 NTSTATUS status;
139 if (tevent_req_is_nterror(req, &status)) {
140 struct dom_sid_buf buf;
141 DEBUG(5, ("Could not convert sid %s: %s\n",
142 dom_sid_str_buf(&state->sid, &buf),
143 nt_errstr(status)));
144 return status;
146 response->data.pw = state->pw;
147 return NT_STATUS_OK;