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