Merge commit 'release-4-0-0alpha15' into master4-tmp
[kai/samba-autobuild/.git] / source3 / winbindd / wb_sid2uid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async sid2uid
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_wbint_c.h"
23 #include "idmap_cache.h"
24 #include "../libcli/security/security.h"
25
26 struct wb_sid2uid_state {
27         struct tevent_context *ev;
28         struct dom_sid sid;
29         char *dom_name;
30         uint64 uid64;
31         uid_t uid;
32 };
33
34 static void wb_sid2uid_lookup_done(struct tevent_req *subreq);
35 static void wb_sid2uid_done(struct tevent_req *subreq);
36
37 struct tevent_req *wb_sid2uid_send(TALLOC_CTX *mem_ctx,
38                                    struct tevent_context *ev,
39                                    const struct dom_sid *sid)
40 {
41         struct tevent_req *req, *subreq;
42         struct wb_sid2uid_state *state;
43         bool expired;
44
45         req = tevent_req_create(mem_ctx, &state, struct wb_sid2uid_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49         sid_copy(&state->sid, sid);
50         state->ev = ev;
51
52         if (winbindd_use_idmap_cache()
53             && idmap_cache_find_sid2uid(sid, &state->uid, &expired)) {
54
55                 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
56                            (int)state->uid, expired ? " (expired)": ""));
57
58                 if (!expired || is_domain_offline(find_our_domain())) {
59                         if (state->uid == -1) {
60                                 tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
61                         } else {
62                                 tevent_req_done(req);
63                         }
64                         return tevent_req_post(req, ev);
65                 }
66         }
67
68         /*
69          * We need to make sure the sid is of the right type to not flood
70          * idmap with wrong entries
71          */
72
73         subreq = wb_lookupsid_send(state, ev, &state->sid);
74         if (tevent_req_nomem(subreq, req)) {
75                 return tevent_req_post(req, ev);
76         }
77         tevent_req_set_callback(subreq, wb_sid2uid_lookup_done, req);
78         return req;
79 }
80
81 static void wb_sid2uid_lookup_done(struct tevent_req *subreq)
82 {
83         struct tevent_req *req = tevent_req_callback_data(
84                 subreq, struct tevent_req);
85         struct wb_sid2uid_state *state = tevent_req_data(
86                 req, struct wb_sid2uid_state);
87         struct winbindd_domain *domain;
88         const char *domname;
89         const char *name;
90         enum lsa_SidType type;
91         NTSTATUS status;
92         struct winbindd_child *child;
93
94         status = wb_lookupsid_recv(subreq, talloc_tos(), &type, &domname,
95                                    &name);
96         if (tevent_req_nterror(req, status)) {
97                 return;
98         }
99
100         if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
101                 DEBUG(5, ("Sid %s is not a user or a computer.\n",
102                           sid_string_dbg(&state->sid)));
103                 /*
104                  * We have to set the cache ourselves here, the child
105                  * which is normally responsible was not queried yet.
106                  */
107                 idmap_cache_set_sid2uid(&state->sid, -1);
108                 tevent_req_nterror(req, NT_STATUS_INVALID_SID);
109                 return;
110         }
111
112         domain = find_domain_from_sid_noinit(&state->sid);
113
114         /*
115          * TODO: Issue a gettrustinfo here in case we don't have "domain" yet?
116          */
117
118         if ((domain != NULL) && domain->have_idmap_config) {
119                 state->dom_name = domain->name;
120         } else {
121                 state->dom_name = NULL;
122         }
123
124         child = idmap_child();
125
126         subreq = dcerpc_wbint_Sid2Uid_send(state, state->ev, child->binding_handle,
127                                            state->dom_name, &state->sid,
128                                            &state->uid64);
129         if (tevent_req_nomem(subreq, req)) {
130                 return;
131         }
132         tevent_req_set_callback(subreq, wb_sid2uid_done, req);
133 }
134
135 static void wb_sid2uid_done(struct tevent_req *subreq)
136 {
137         struct tevent_req *req = tevent_req_callback_data(
138                 subreq, struct tevent_req);
139         struct wb_sid2uid_state *state = tevent_req_data(
140                 req, struct wb_sid2uid_state);
141         NTSTATUS status, result;
142
143         status = dcerpc_wbint_Sid2Uid_recv(subreq, state, &result);
144         TALLOC_FREE(subreq);
145         if (any_nt_status_not_ok(status, result, &status)) {
146                 tevent_req_nterror(req, status);
147                 return;
148         }
149
150         state->uid = state->uid64;
151         tevent_req_done(req);
152 }
153
154 NTSTATUS wb_sid2uid_recv(struct tevent_req *req, uid_t *uid)
155 {
156         struct wb_sid2uid_state *state = tevent_req_data(
157                 req, struct wb_sid2uid_state);
158         NTSTATUS status;
159
160         if (tevent_req_is_nterror(req, &status)) {
161                 return status;
162         }
163         *uid = state->uid;
164         return NT_STATUS_OK;
165 }