winbindd: Do not ignore domain in the LOOKUPNAME request
[metze/samba/wip.git] / source3 / winbindd / winbindd_lookupname.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_LOOKUPNAME
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
23 struct winbindd_lookupname_state {
24         struct tevent_context *ev;
25         struct dom_sid sid;
26         enum lsa_SidType type;
27 };
28
29 static void winbindd_lookupname_done(struct tevent_req *subreq);
30
31 struct tevent_req *winbindd_lookupname_send(TALLOC_CTX *mem_ctx,
32                                             struct tevent_context *ev,
33                                             struct winbindd_cli_state *cli,
34                                             struct winbindd_request *request)
35 {
36         struct tevent_req *req, *subreq;
37         struct winbindd_lookupname_state *state;
38         const char *domname = NULL, *name = NULL;
39         char *p = NULL;
40
41         req = tevent_req_create(mem_ctx, &state,
42                                 struct winbindd_lookupname_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46         state->ev = ev;
47
48         /* Ensure null termination */
49         request->data.name.dom_name[
50                 sizeof(request->data.name.dom_name)-1]='\0';
51         request->data.name.name[sizeof(request->data.name.name)-1]='\0';
52
53         if (strlen(request->data.name.dom_name) == 0) {
54                 /* cope with the name being a fully qualified name */
55                 p = strstr(request->data.name.name, lp_winbind_separator());
56                 if (p != NULL) {
57                         *p = '\0';
58                         domname = request->data.name.name;
59                         name = p + 1;
60                 } else {
61                         p = strchr(request->data.name.name, '@');
62                         if (p != NULL) {
63                                 /* upn */
64                                 domname = p + 1;
65                                 *p = '\0';
66                                 name = request->data.name.name;
67                         } else {
68                                 domname = "";
69                                 name = request->data.name.name;
70                         }
71                 }
72         } else {
73                 domname = request->data.name.dom_name;
74                 name = request->data.name.name;
75         }
76
77         DEBUG(3, ("lookupname %s%s%s\n", domname, lp_winbind_separator(),
78                   name));
79
80         subreq = wb_lookupname_send(state, ev, domname, name, 0);
81         if (tevent_req_nomem(subreq, req)) {
82                 return tevent_req_post(req, ev);
83         }
84         tevent_req_set_callback(subreq, winbindd_lookupname_done, req);
85         return req;
86 }
87
88 static void winbindd_lookupname_done(struct tevent_req *subreq)
89 {
90         struct tevent_req *req = tevent_req_callback_data(
91                 subreq, struct tevent_req);
92         struct winbindd_lookupname_state *state = tevent_req_data(
93                 req, struct winbindd_lookupname_state);
94         NTSTATUS status;
95
96         status = wb_lookupname_recv(subreq, &state->sid, &state->type);
97         TALLOC_FREE(subreq);
98         if (tevent_req_nterror(req, status)) {
99                 return;
100         }
101         tevent_req_done(req);
102 }
103
104 NTSTATUS winbindd_lookupname_recv(struct tevent_req *req,
105                                   struct winbindd_response *response)
106 {
107         struct winbindd_lookupname_state *state = tevent_req_data(
108                 req, struct winbindd_lookupname_state);
109         NTSTATUS status;
110
111         if (tevent_req_is_nterror(req, &status)) {
112                 DEBUG(5, ("Could not convert sid %s: %s\n",
113                           sid_string_dbg(&state->sid), nt_errstr(status)));
114                 return status;
115         }
116         sid_to_fstring(response->data.sid.sid, &state->sid);
117         response->data.sid.type = state->type;
118         return NT_STATUS_OK;
119 }