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