s4:role transfer - use always type "enum drepl_role_master" for role specifications
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_lookuprids.c
blob6e3d5c5ece83f34e6318a60339b4b3aef54c2150
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_LOOKUPRIDS
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/cli_wbint.h"
23 #include "../libcli/security/security.h"
25 struct winbindd_lookuprids_state {
26 struct tevent_context *ev;
27 const char *domain_name;
28 struct wbint_RidArray rids;
29 struct wbint_Principals names;
32 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
33 uint32_t **prids, uint32_t *pnum_rids);
35 static void winbindd_lookuprids_done(struct tevent_req *subreq);
37 struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct winbindd_cli_state *cli,
40 struct winbindd_request *request)
42 struct tevent_req *req, *subreq;
43 struct winbindd_lookuprids_state *state;
44 struct winbindd_domain *domain;
45 struct dom_sid sid;
47 req = tevent_req_create(mem_ctx, &state,
48 struct winbindd_lookuprids_state);
49 if (req == NULL) {
50 return NULL;
52 state->ev = ev;
54 /* Ensure null termination */
55 request->data.sid[sizeof(request->data.sid)-1]='\0';
57 DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
59 if (!string_to_sid(&sid, request->data.sid)) {
60 DEBUG(5, ("%s not a SID\n", request->data.sid));
61 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
62 return tevent_req_post(req, ev);
65 domain = find_domain_from_sid_noinit(&sid);
66 if (domain == NULL) {
67 DEBUG(5, ("Domain for sid %s not found\n",
68 sid_string_dbg(&sid)));
69 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
70 return tevent_req_post(req, ev);
73 if (request->extra_data.data[request->extra_len-1] != '\0') {
74 DEBUG(5, ("extra_data not 0-terminated\n"));
75 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
76 return tevent_req_post(req, ev);
79 if (!parse_ridlist(state, request->extra_data.data,
80 &state->rids.rids, &state->rids.num_rids)) {
81 DEBUG(5, ("parse_ridlist failed\n"));
82 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
83 return tevent_req_post(req, ev);
86 subreq = dcerpc_wbint_LookupRids_send(
87 state, ev, domain->child.binding_handle, &state->rids, &state->names);
88 if (tevent_req_nomem(subreq, req)) {
89 return tevent_req_post(req, ev);
91 tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
92 return req;
95 static void winbindd_lookuprids_done(struct tevent_req *subreq)
97 struct tevent_req *req = tevent_req_callback_data(
98 subreq, struct tevent_req);
99 struct winbindd_lookuprids_state *state = tevent_req_data(
100 req, struct winbindd_lookuprids_state);
101 NTSTATUS status, result;
103 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
104 TALLOC_FREE(subreq);
105 if (any_nt_status_not_ok(status, result, &status)) {
106 tevent_req_nterror(req, status);
107 return;
109 tevent_req_done(req);
112 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
113 struct winbindd_response *response)
115 struct winbindd_lookuprids_state *state = tevent_req_data(
116 req, struct winbindd_lookuprids_state);
117 NTSTATUS status;
118 char *result;
119 int i;
121 if (tevent_req_is_nterror(req, &status)) {
122 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
123 return status;
126 result = talloc_strdup(response, "");
127 if (result == NULL) {
128 return NT_STATUS_NO_MEMORY;
131 for (i=0; i<state->names.num_principals; i++) {
132 struct wbint_Principal *p = &state->names.principals[i];
134 result = talloc_asprintf_append_buffer(
135 result, "%d %s\n", (int)p->type, p->name);
136 if (result == NULL) {
137 return NT_STATUS_NO_MEMORY;
141 fstrcpy(response->data.domain_name, state->domain_name);
142 response->extra_data.data = result;
143 response->length += talloc_get_size(result);
144 return NT_STATUS_OK;
147 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
148 uint32_t **prids, uint32_t *pnum_rids)
150 uint32_t i, num_rids;
151 uint32_t *rids;
152 char *p;
154 if (ridstr == NULL) {
155 return false;
158 p = ridstr;
159 num_rids = 0;
161 /* count rids */
163 while ((p = strchr(p, '\n')) != NULL) {
164 p += 1;
165 num_rids += 1;
168 if (num_rids == 0) {
169 *pnum_rids = 0;
170 *prids = NULL;
171 return true;
174 rids = talloc_array(mem_ctx, uint32_t, num_rids);
175 if (rids == NULL) {
176 return false;
179 p = ridstr;
181 for (i=0; i<num_rids; i++) {
182 char *q;
183 rids[i] = strtoul(p, &q, 10);
184 if (*q != '\n') {
185 DEBUG(0, ("Got invalid ridstr: %s\n", p));
186 return false;
188 p = q+1;
191 *pnum_rids = num_rids;
192 *prids = rids;
193 return true;