WHATSNEW: Add release notes for Samba 4.15.0.
[Samba.git] / source3 / winbindd / winbindd_lookuprids.c
blobfc8fa46f4d3607fd6ada5ce42ad5810be0cf6298
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/ndr_winbind_c.h"
23 #include "../libcli/security/security.h"
24 #include "lib/util/smb_strtox.h"
25 #include "lib/util/string_wrappers.h"
27 struct winbindd_lookuprids_state {
28 struct tevent_context *ev;
29 struct dom_sid domain_sid;
30 const char *domain_name;
31 struct wbint_RidArray rids;
32 struct wbint_Principals names;
35 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
36 uint32_t **prids, uint32_t *pnum_rids);
38 static void winbindd_lookuprids_done(struct tevent_req *subreq);
40 struct tevent_req *winbindd_lookuprids_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 struct winbindd_cli_state *cli,
43 struct winbindd_request *request)
45 struct tevent_req *req, *subreq;
46 struct winbindd_lookuprids_state *state;
47 struct winbindd_domain *domain;
49 req = tevent_req_create(mem_ctx, &state,
50 struct winbindd_lookuprids_state);
51 if (req == NULL) {
52 return NULL;
54 state->ev = ev;
56 /* Ensure null termination */
57 request->data.sid[sizeof(request->data.sid)-1]='\0';
59 DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
61 if (!string_to_sid(&state->domain_sid, request->data.sid)) {
62 DEBUG(5, ("%s not a SID\n", request->data.sid));
63 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
64 return tevent_req_post(req, ev);
67 domain = find_lookup_domain_from_sid(&state->domain_sid);
68 if (domain == NULL) {
69 struct dom_sid_buf buf;
70 DEBUG(5, ("Domain for sid %s not found\n",
71 dom_sid_str_buf(&state->domain_sid, &buf)));
72 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
73 return tevent_req_post(req, ev);
76 if (request->extra_data.data[request->extra_len-1] != '\0') {
77 DEBUG(5, ("extra_data not 0-terminated\n"));
78 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
79 return tevent_req_post(req, ev);
82 if (!parse_ridlist(state, request->extra_data.data,
83 &state->rids.rids, &state->rids.num_rids)) {
84 DEBUG(5, ("parse_ridlist failed\n"));
85 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
86 return tevent_req_post(req, ev);
89 subreq = dcerpc_wbint_LookupRids_send(
90 state, ev, dom_child_handle(domain), &state->domain_sid,
91 &state->rids, &state->domain_name, &state->names);
92 if (tevent_req_nomem(subreq, req)) {
93 return tevent_req_post(req, ev);
95 tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
96 return req;
99 static void winbindd_lookuprids_done(struct tevent_req *subreq)
101 struct tevent_req *req = tevent_req_callback_data(
102 subreq, struct tevent_req);
103 struct winbindd_lookuprids_state *state = tevent_req_data(
104 req, struct winbindd_lookuprids_state);
105 NTSTATUS status, result;
107 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
108 TALLOC_FREE(subreq);
109 if (any_nt_status_not_ok(status, result, &status)) {
110 tevent_req_nterror(req, status);
111 return;
113 tevent_req_done(req);
116 NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
117 struct winbindd_response *response)
119 struct winbindd_lookuprids_state *state = tevent_req_data(
120 req, struct winbindd_lookuprids_state);
121 NTSTATUS status;
122 char *result;
123 uint32_t i;
125 if (tevent_req_is_nterror(req, &status)) {
126 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
127 return status;
130 result = talloc_strdup(response, "");
131 if (result == NULL) {
132 return NT_STATUS_NO_MEMORY;
135 for (i=0; i<state->names.num_principals; i++) {
136 struct wbint_Principal *p = &state->names.principals[i];
138 result = talloc_asprintf_append_buffer(
139 result, "%d %s\n", (int)p->type, p->name);
140 if (result == NULL) {
141 return NT_STATUS_NO_MEMORY;
145 fstrcpy(response->data.domain_name, state->domain_name);
146 response->extra_data.data = result;
147 response->length += talloc_get_size(result);
148 return NT_STATUS_OK;
151 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
152 uint32_t **prids, uint32_t *pnum_rids)
154 uint32_t i, num_rids;
155 uint32_t *rids;
156 char *p;
158 if (ridstr == NULL) {
159 return false;
162 p = ridstr;
163 num_rids = 0;
165 /* count rids */
167 while ((p = strchr(p, '\n')) != NULL) {
168 p += 1;
169 num_rids += 1;
172 if (num_rids == 0) {
173 *pnum_rids = 0;
174 *prids = NULL;
175 return true;
178 rids = talloc_array(mem_ctx, uint32_t, num_rids);
179 if (rids == NULL) {
180 return false;
183 p = ridstr;
185 for (i=0; i<num_rids; i++) {
186 char *q;
187 int error = 0;
189 rids[i] = smb_strtoul(p, &q, 10, &error, SMB_STR_STANDARD);
190 if (error != 0 || *q != '\n') {
191 DEBUG(0, ("Got invalid ridstr: %s\n", p));
192 return false;
194 p = q+1;
197 *pnum_rids = num_rids;
198 *prids = rids;
199 return true;