LCOV - code coverage report
Current view: top level - source3/winbindd - winbindd_getgrent.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 75 99 75.8 %
Date: 2024-06-13 04:01:37 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    async implementation of WINBINDD_GETGRENT
       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             : 
      23             : struct winbindd_getgrent_state {
      24             :         struct tevent_context *ev;
      25             :         struct winbindd_cli_state *cli;
      26             :         uint32_t max_groups;
      27             :         uint32_t num_groups;
      28             :         struct winbindd_gr *groups;
      29             :         struct db_context **members;
      30             : };
      31             : 
      32             : static void winbindd_getgrent_done(struct tevent_req *subreq);
      33             : 
      34         274 : struct tevent_req *winbindd_getgrent_send(TALLOC_CTX *mem_ctx,
      35             :                                           struct tevent_context *ev,
      36             :                                           struct winbindd_cli_state *cli,
      37             :                                           struct winbindd_request *request)
      38             : {
      39             :         struct tevent_req *req, *subreq;
      40             :         struct winbindd_getgrent_state *state;
      41             : 
      42         274 :         req = tevent_req_create(mem_ctx, &state,
      43             :                                 struct winbindd_getgrent_state);
      44         274 :         if (req == NULL) {
      45           0 :                 return NULL;
      46             :         }
      47         274 :         state->ev = ev;
      48         274 :         state->num_groups = 0;
      49         274 :         state->cli = cli;
      50             : 
      51         274 :         D_NOTICE("[%s (%u)] Winbind external command GETGRENT start.\n"
      52             :                  "The caller (%s) provided room for %"PRIu32" entries.\n",
      53             :                  cli->client_name,
      54             :                  (unsigned int)cli->pid,
      55             :                  cli->client_name,
      56             :                  request->data.num_entries);
      57             : 
      58         274 :         if (cli->grent_state == NULL) {
      59         136 :                 D_NOTICE("The grent state from winbindd client state is NULL.\n");
      60         136 :                 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
      61         136 :                 return tevent_req_post(req, ev);
      62             :         }
      63             : 
      64         138 :         state->max_groups = MIN(500, request->data.num_entries);
      65         138 :         if (state->max_groups == 0) {
      66           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
      67           0 :                 return tevent_req_post(req, ev);
      68             :         }
      69             : 
      70         138 :         state->groups = talloc_zero_array(state, struct winbindd_gr,
      71             :                                           state->max_groups);
      72         138 :         if (tevent_req_nomem(state->groups, req)) {
      73           0 :                 return tevent_req_post(req, ev);
      74             :         }
      75             : 
      76         138 :         state->members = talloc_array(state, struct db_context *,
      77             :                                       state->max_groups);
      78         138 :         if (tevent_req_nomem(state->members, req)) {
      79           0 :                 TALLOC_FREE(state->groups);
      80           0 :                 return tevent_req_post(req, ev);
      81             :         }
      82             : 
      83         138 :         subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
      84             :                                     cli->grent_state,
      85         138 :                                     &state->groups[state->num_groups]);
      86         138 :         if (tevent_req_nomem(subreq, req)) {
      87           0 :                 return tevent_req_post(req, ev);
      88             :         }
      89         138 :         tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
      90         138 :         return req;
      91             : }
      92             : 
      93        2584 : static void winbindd_getgrent_done(struct tevent_req *subreq)
      94             : {
      95        2584 :         struct tevent_req *req = tevent_req_callback_data(
      96             :                 subreq, struct tevent_req);
      97        2584 :         struct winbindd_getgrent_state *state = tevent_req_data(
      98             :                 req, struct winbindd_getgrent_state);
      99             :         NTSTATUS status;
     100             : 
     101        2584 :         status = wb_next_grent_recv(subreq, state,
     102        2584 :                                     &state->members[state->num_groups]);
     103        2584 :         TALLOC_FREE(subreq);
     104        2584 :         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
     105         136 :                 D_WARNING("winbindd_getgrent_done: done with %"PRIu32" groups\n",
     106             :                           state->num_groups);
     107         136 :                 TALLOC_FREE(state->cli->grent_state);
     108         136 :                 tevent_req_done(req);
     109         136 :                 return;
     110             :         }
     111        2448 :         if (!NT_STATUS_IS_OK(status)) {
     112           0 :                 tevent_req_nterror(req, status);
     113           0 :                 return;
     114             :         }
     115        2448 :         state->num_groups += 1;
     116        2448 :         if (state->num_groups >= state->max_groups) {
     117           0 :                 D_DEBUG("winbindd_getgrent_done: Got enough groups: %"PRIu32"\n",
     118             :                         state->num_groups);
     119           0 :                 tevent_req_done(req);
     120           0 :                 return;
     121             :         }
     122        2448 :         if (state->cli->grent_state == NULL) {
     123           0 :                 D_DEBUG("winbindd_getgrent_done: endgrent called in between\n");
     124           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
     125           0 :                 return;
     126             :         }
     127        3672 :         subreq = wb_next_grent_send(state, state->ev,
     128             :                                     lp_winbind_expand_groups(),
     129        2448 :                                     state->cli->grent_state,
     130        2448 :                                     &state->groups[state->num_groups]);
     131        2448 :         if (tevent_req_nomem(subreq, req)) {
     132           0 :                 return;
     133             :         }
     134        2448 :         tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
     135             : }
     136             : 
     137         272 : NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
     138             :                                 struct winbindd_response *response)
     139             : {
     140         272 :         struct winbindd_getgrent_state *state = tevent_req_data(
     141             :                 req, struct winbindd_getgrent_state);
     142             :         NTSTATUS status;
     143             :         char **memberstrings;
     144             :         char *result;
     145             :         size_t base_memberofs, total_memberlen;
     146             :         uint32_t i;
     147             : 
     148         272 :         if (tevent_req_is_nterror(req, &status)) {
     149         136 :                 TALLOC_FREE(state->cli->grent_state);
     150         136 :                 D_WARNING("getgrent failed: %s\n", nt_errstr(status));
     151         136 :                 return status;
     152             :         }
     153             : 
     154         136 :         if (state->num_groups == 0) {
     155           0 :                 return NT_STATUS_NO_MORE_ENTRIES;
     156             :         }
     157             : 
     158         136 :         memberstrings = talloc_array(talloc_tos(), char *, state->num_groups);
     159         136 :         if (memberstrings == NULL) {
     160           0 :                 TALLOC_FREE(state->cli->grent_state);
     161           0 :                 return NT_STATUS_NO_MEMORY;
     162             :         }
     163             : 
     164         136 :         total_memberlen = 0;
     165             : 
     166        2584 :         for (i=0; i<state->num_groups; i++) {
     167             :                 int num_members;
     168             : 
     169        3672 :                 status = winbindd_print_groupmembers(
     170        2448 :                         state->members[i], memberstrings, &num_members,
     171        2448 :                         &memberstrings[i]);
     172             : 
     173        2448 :                 if (!NT_STATUS_IS_OK(status)) {
     174           0 :                         TALLOC_FREE(memberstrings);
     175           0 :                         TALLOC_FREE(state->cli->grent_state);
     176           0 :                         return status;
     177             :                 }
     178        2448 :                 TALLOC_FREE(state->members[i]);
     179             : 
     180        2448 :                 state->groups[i].num_gr_mem = num_members;
     181        2448 :                 state->groups[i].gr_mem_ofs = total_memberlen;
     182             : 
     183        2448 :                 total_memberlen += talloc_get_size(memberstrings[i]);
     184             :         }
     185             : 
     186         136 :         base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
     187             : 
     188         136 :         result = talloc_realloc(state, state->groups, char,
     189             :                                 base_memberofs + total_memberlen);
     190         136 :         if (result == NULL) {
     191           0 :                 TALLOC_FREE(state->cli->grent_state);
     192           0 :                 return NT_STATUS_NO_MEMORY;
     193             :         }
     194         136 :         state->groups = (struct winbindd_gr *)result;
     195             : 
     196        2584 :         for (i=0; i<state->num_groups; i++) {
     197        4896 :                 memcpy(result + base_memberofs + state->groups[i].gr_mem_ofs,
     198        3672 :                        memberstrings[i], talloc_get_size(memberstrings[i]));
     199        2448 :                 TALLOC_FREE(memberstrings[i]);
     200             :         }
     201             : 
     202         136 :         TALLOC_FREE(memberstrings);
     203             : 
     204         136 :         response->data.num_entries = state->num_groups;
     205         136 :         response->length += talloc_get_size(result);
     206         136 :         response->extra_data.data = talloc_move(response, &result);
     207             : 
     208         136 :         D_NOTICE("Winbind external command GETGRENT end.\n"
     209             :                  "Received %"PRIu32" entries.\n",
     210             :                  response->data.num_entries);
     211             : 
     212         136 :         return NT_STATUS_OK;
     213             : }

Generated by: LCOV version 1.13