LCOV - code coverage report
Current view: top level - source3/winbindd - winbindd_lookuprids.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 65 97 67.0 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    async implementation of WINBINDD_LOOKUPRIDS
       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_winbind_c.h"
      23             : #include "../libcli/security/security.h"
      24             : #include "lib/util/smb_strtox.h"
      25             : #include "lib/util/string_wrappers.h"
      26             : 
      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;
      33             : };
      34             : 
      35             : static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
      36             :                           uint32_t **prids, uint32_t *pnum_rids);
      37             : 
      38             : static void winbindd_lookuprids_done(struct tevent_req *subreq);
      39             : 
      40         160 : 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)
      44             : {
      45           0 :         struct tevent_req *req, *subreq;
      46           0 :         struct winbindd_lookuprids_state *state;
      47           0 :         struct winbindd_domain *domain;
      48             : 
      49         160 :         req = tevent_req_create(mem_ctx, &state,
      50             :                                 struct winbindd_lookuprids_state);
      51         160 :         if (req == NULL) {
      52           0 :                 return NULL;
      53             :         }
      54         160 :         state->ev = ev;
      55             : 
      56             :         /* Ensure null termination */
      57         160 :         request->data.sid[sizeof(request->data.sid)-1]='\0';
      58             : 
      59         160 :         DEBUG(3, ("lookuprids (%s)\n", request->data.sid));
      60             : 
      61         160 :         if (!string_to_sid(&state->domain_sid, request->data.sid)) {
      62           0 :                 DEBUG(5, ("%s not a SID\n", request->data.sid));
      63           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
      64           0 :                 return tevent_req_post(req, ev);
      65             :         }
      66             : 
      67         160 :         domain = find_lookup_domain_from_sid(&state->domain_sid);
      68         160 :         if (domain == NULL) {
      69           0 :                 struct dom_sid_buf buf;
      70         124 :                 DEBUG(5, ("Domain for sid %s not found\n",
      71             :                           dom_sid_str_buf(&state->domain_sid, &buf)));
      72         124 :                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
      73         124 :                 return tevent_req_post(req, ev);
      74             :         }
      75             : 
      76          36 :         if (request->extra_data.data[request->extra_len-1] != '\0') {
      77           0 :                 DEBUG(5, ("extra_data not 0-terminated\n"));
      78           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
      79           0 :                 return tevent_req_post(req, ev);
      80             :         }
      81             : 
      82          36 :         if (!parse_ridlist(state, request->extra_data.data,
      83          36 :                            &state->rids.rids, &state->rids.num_rids)) {
      84           0 :                 DEBUG(5, ("parse_ridlist failed\n"));
      85           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
      86           0 :                 return tevent_req_post(req, ev);
      87             :         }
      88             : 
      89          36 :         subreq = dcerpc_wbint_LookupRids_send(
      90          36 :                 state, ev, dom_child_handle(domain), &state->domain_sid,
      91          36 :                 &state->rids, &state->domain_name, &state->names);
      92          36 :         if (tevent_req_nomem(subreq, req)) {
      93           0 :                 return tevent_req_post(req, ev);
      94             :         }
      95          36 :         tevent_req_set_callback(subreq, winbindd_lookuprids_done, req);
      96          36 :         return req;
      97             : }
      98             : 
      99          36 : static void winbindd_lookuprids_done(struct tevent_req *subreq)
     100             : {
     101          36 :         struct tevent_req *req = tevent_req_callback_data(
     102             :                 subreq, struct tevent_req);
     103          36 :         struct winbindd_lookuprids_state *state = tevent_req_data(
     104             :                 req, struct winbindd_lookuprids_state);
     105           0 :         NTSTATUS status, result;
     106             : 
     107          36 :         status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
     108          36 :         TALLOC_FREE(subreq);
     109          36 :         if (any_nt_status_not_ok(status, result, &status)) {
     110          30 :                 tevent_req_nterror(req, status);
     111          30 :                 return;
     112             :         }
     113           6 :         tevent_req_done(req);
     114             : }
     115             : 
     116         160 : NTSTATUS winbindd_lookuprids_recv(struct tevent_req *req,
     117             :                                   struct winbindd_response *response)
     118             : {
     119         160 :         struct winbindd_lookuprids_state *state = tevent_req_data(
     120             :                 req, struct winbindd_lookuprids_state);
     121           0 :         NTSTATUS status;
     122           0 :         char *result;
     123           0 :         uint32_t i;
     124             : 
     125         160 :         if (tevent_req_is_nterror(req, &status)) {
     126         154 :                 DEBUG(5, ("Lookuprids failed: %s\n",nt_errstr(status)));
     127         154 :                 return status;
     128             :         }
     129             : 
     130           6 :         result = talloc_strdup(response, "");
     131           6 :         if (result == NULL) {
     132           0 :                 return NT_STATUS_NO_MEMORY;
     133             :         }
     134             : 
     135          18 :         for (i=0; i<state->names.num_principals; i++) {
     136          12 :                 struct wbint_Principal *p = &state->names.principals[i];
     137             : 
     138          12 :                 result = talloc_asprintf_append_buffer(
     139          12 :                         result, "%d %s\n", (int)p->type, p->name);
     140          12 :                 if (result == NULL) {
     141           0 :                         return NT_STATUS_NO_MEMORY;
     142             :                 }
     143             :         }
     144             : 
     145           6 :         fstrcpy(response->data.domain_name, state->domain_name);
     146           6 :         response->extra_data.data = result;
     147           6 :         response->length += talloc_get_size(result);
     148           6 :         return NT_STATUS_OK;
     149             : }
     150             : 
     151          36 : static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
     152             :                           uint32_t **prids, uint32_t *pnum_rids)
     153             : {
     154           0 :         uint32_t i, num_rids;
     155           0 :         uint32_t *rids;
     156           0 :         char *p;
     157             : 
     158          36 :         if (ridstr == NULL) {
     159           0 :                 return false;
     160             :         }
     161             : 
     162          36 :         p = ridstr;
     163          36 :         num_rids = 0;
     164             : 
     165             :         /* count rids */
     166             : 
     167          78 :         while ((p = strchr(p, '\n')) != NULL) {
     168          42 :                 p += 1;
     169          42 :                 num_rids += 1;
     170             :         }
     171             : 
     172          36 :         if (num_rids == 0) {
     173           0 :                 *pnum_rids = 0;
     174           0 :                 *prids = NULL;
     175           0 :                 return true;
     176             :         }
     177             : 
     178          36 :         rids = talloc_array(mem_ctx, uint32_t, num_rids);
     179          36 :         if (rids == NULL) {
     180           0 :                 return false;
     181             :         }
     182             : 
     183          36 :         p = ridstr;
     184             : 
     185          78 :         for (i=0; i<num_rids; i++) {
     186           0 :                 char *q;
     187          42 :                 int error = 0;
     188             : 
     189          42 :                 rids[i] = smb_strtoul(p, &q, 10, &error, SMB_STR_STANDARD);
     190          42 :                 if (error != 0 || *q != '\n') {
     191           0 :                         DEBUG(0, ("Got invalid ridstr: %s\n", p));
     192           0 :                         return false;
     193             :                 }
     194          42 :                 p = q+1;
     195             :         }
     196             : 
     197          36 :         *pnum_rids = num_rids;
     198          36 :         *prids = rids;
     199          36 :         return true;
     200             : }

Generated by: LCOV version 1.14