s3-auth Change type of num_sids to uint32_t
[samba.git] / source3 / winbindd / wb_gid2sid.c
1 /*
2    Unix SMB/CIFS implementation.
3    async gid2sid
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/cli_wbint.h"
23 #include "idmap_cache.h"
24 #include "idmap.h"
25
26 struct wb_gid2sid_state {
27         struct tevent_context *ev;
28         char *dom_name;
29         struct dom_sid sid;
30 };
31
32 static void wb_gid2sid_done(struct tevent_req *subreq);
33
34 struct tevent_req *wb_gid2sid_send(TALLOC_CTX *mem_ctx,
35                                    struct tevent_context *ev,
36                                    gid_t gid)
37 {
38         struct tevent_req *req, *subreq;
39         struct wb_gid2sid_state *state;
40         struct winbindd_domain *domain;
41         struct winbindd_child *child;
42         bool expired;
43
44         req = tevent_req_create(mem_ctx, &state, struct wb_gid2sid_state);
45         if (req == NULL) {
46                 return NULL;
47         }
48
49         if (winbindd_use_idmap_cache()
50             && idmap_cache_find_gid2sid(gid, &state->sid, &expired)) {
51
52                 DEBUG(10, ("idmap_cache_find_gid2sid found %d%s\n",
53                            (int)gid, expired ? " (expired)": ""));
54
55                 if (!expired || idmap_is_offline()) {
56                         if (is_null_sid(&state->sid)) {
57                                 tevent_req_nterror(req,
58                                                    NT_STATUS_NONE_MAPPED);
59                         } else {
60                                 tevent_req_done(req);
61                         }
62                         return tevent_req_post(req, ev);
63                 }
64         }
65
66         state->dom_name = NULL;
67
68         for (domain = domain_list(); domain != NULL; domain = domain->next) {
69                 if (domain->have_idmap_config
70                     && (gid >= domain->id_range_low)
71                     && (gid <= domain->id_range_high)) {
72                         state->dom_name = domain->name;
73                         break;
74                 }
75         }
76
77         child = idmap_child();
78
79         subreq = dcerpc_wbint_Gid2Sid_send(
80                 state, ev, child->binding_handle, state->dom_name,
81                 gid, &state->sid);
82         if (tevent_req_nomem(subreq, req)) {
83                 return tevent_req_post(req, ev);
84         }
85         tevent_req_set_callback(subreq, wb_gid2sid_done, req);
86         return req;
87 }
88
89 static void wb_gid2sid_done(struct tevent_req *subreq)
90 {
91         struct tevent_req *req = tevent_req_callback_data(
92                 subreq, struct tevent_req);
93         struct wb_gid2sid_state *state = tevent_req_data(
94                 req, struct wb_gid2sid_state);
95         NTSTATUS status, result;
96
97         status = dcerpc_wbint_Gid2Sid_recv(subreq, state, &result);
98         TALLOC_FREE(subreq);
99         if (!NT_STATUS_IS_OK(status)) {
100                 tevent_req_nterror(req, status);
101                 return;
102         }
103         if (!NT_STATUS_IS_OK(result)) {
104                 tevent_req_nterror(req, result);
105                 return;
106         }
107         tevent_req_done(req);
108 }
109
110 NTSTATUS wb_gid2sid_recv(struct tevent_req *req, struct dom_sid *sid)
111 {
112         struct wb_gid2sid_state *state = tevent_req_data(
113                 req, struct wb_gid2sid_state);
114         NTSTATUS status;
115
116         if (tevent_req_is_nterror(req, &status)) {
117                 return status;
118         }
119         sid_copy(sid, &state->sid);
120         return NT_STATUS_OK;
121 }