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

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    async implementation of WINBINDD_GETGRNAM
       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 "libcli/security/dom_sid.h"
      23             : #include "lib/util/string_wrappers.h"
      24             : 
      25             : struct winbindd_getgrnam_state {
      26             :         struct tevent_context *ev;
      27             :         fstring name_namespace, name_domain, name_group;
      28             :         struct dom_sid sid;
      29             :         const char *domname;
      30             :         const char *name;
      31             :         gid_t gid;
      32             :         struct db_context *members;
      33             : };
      34             : 
      35             : static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq);
      36             : static void winbindd_getgrnam_done(struct tevent_req *subreq);
      37             : 
      38         292 : struct tevent_req *winbindd_getgrnam_send(TALLOC_CTX *mem_ctx,
      39             :                                           struct tevent_context *ev,
      40             :                                           struct winbindd_cli_state *cli,
      41             :                                           struct winbindd_request *request)
      42             : {
      43             :         struct tevent_req *req, *subreq;
      44             :         struct winbindd_getgrnam_state *state;
      45             :         char *tmp;
      46             :         NTSTATUS nt_status;
      47             :         bool ok;
      48             : 
      49         292 :         req = tevent_req_create(mem_ctx, &state,
      50             :                                 struct winbindd_getgrnam_state);
      51         292 :         if (req == NULL) {
      52           0 :                 return NULL;
      53             :         }
      54         292 :         state->ev = ev;
      55             : 
      56             :         /* Ensure null termination */
      57         292 :         request->data.groupname[sizeof(request->data.groupname)-1]='\0';
      58             : 
      59         292 :         D_NOTICE("[%s (%u)] Winbind external command GETGRNAM start.\n"
      60             :                    "Searching group name '%s'.\n",
      61             :                    cli->client_name,
      62             :                    (unsigned int)cli->pid,
      63             :                    request->data.groupname);
      64             : 
      65         292 :         nt_status = normalize_name_unmap(state, request->data.groupname, &tmp);
      66             :         /* If we didn't map anything in the above call, just reset the
      67             :            tmp pointer to the original string */
      68         484 :         if (!NT_STATUS_IS_OK(nt_status) &&
      69         292 :             !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
      70             :         {
      71         292 :                 tmp = request->data.groupname;
      72             :         }
      73             : 
      74             :         /* Parse domain and groupname */
      75             : 
      76         292 :         ok = parse_domain_user(tmp,
      77         292 :                                state->name_namespace,
      78         292 :                                state->name_domain,
      79         292 :                                state->name_group);
      80         292 :         if (!ok) {
      81           0 :                 DBG_INFO("Could not parse domain user: %s\n", tmp);
      82           0 :                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
      83           0 :                 return tevent_req_post(req, ev);
      84             :         }
      85             : 
      86             :         /* if no domain or our local domain and no local tdb group, default to
      87             :          * our local domain for aliases */
      88             : 
      89         292 :         if ( !*(state->name_domain) || strequal(state->name_domain,
      90             :                                                 get_global_sam_name()) ) {
      91          98 :                 fstrcpy(state->name_domain, get_global_sam_name());
      92             :         }
      93             : 
      94         292 :         subreq = wb_lookupname_send(state, ev,
      95         292 :                                     state->name_namespace,
      96         292 :                                     state->name_domain,
      97         292 :                                     state->name_group,
      98             :                                     0);
      99         292 :         if (tevent_req_nomem(subreq, req)) {
     100           0 :                 return tevent_req_post(req, ev);
     101             :         }
     102         292 :         tevent_req_set_callback(subreq, winbindd_getgrnam_lookupname_done,
     103             :                                 req);
     104         292 :         return req;
     105             : }
     106             : 
     107         292 : static void winbindd_getgrnam_lookupname_done(struct tevent_req *subreq)
     108             : {
     109         292 :         struct tevent_req *req = tevent_req_callback_data(
     110             :                 subreq, struct tevent_req);
     111         292 :         struct winbindd_getgrnam_state *state = tevent_req_data(
     112             :                 req, struct winbindd_getgrnam_state);
     113             :         enum lsa_SidType type;
     114             :         NTSTATUS status;
     115             : 
     116         292 :         status = wb_lookupname_recv(subreq, &state->sid, &type);
     117         292 :         TALLOC_FREE(subreq);
     118         292 :         if (tevent_req_nterror(req, status)) {
     119         184 :                 return;
     120             :         }
     121             : 
     122         198 :         switch (type) {
     123         198 :         case SID_NAME_DOM_GRP:
     124             :         case SID_NAME_ALIAS:
     125             :         case SID_NAME_WKN_GRP:
     126             :         /*
     127             :          * Also give user types a chance:
     128             :          * These might be user sids mapped to the ID_TYPE_BOTH,
     129             :          * and in that case we should construct a group struct.
     130             :          */
     131             :         case SID_NAME_USER:
     132             :         case SID_NAME_COMPUTER:
     133         198 :                 break;
     134           0 :         default:
     135           0 :                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_GROUP);
     136           0 :                 return;
     137             :         }
     138             : 
     139         198 :         subreq = wb_getgrsid_send(state, state->ev, &state->sid,
     140             :                                   lp_winbind_expand_groups());
     141         198 :         if (tevent_req_nomem(subreq, req)) {
     142           0 :                 return;
     143             :         }
     144         198 :         tevent_req_set_callback(subreq, winbindd_getgrnam_done, req);
     145             : }
     146             : 
     147         198 : static void winbindd_getgrnam_done(struct tevent_req *subreq)
     148             : {
     149         198 :         struct tevent_req *req = tevent_req_callback_data(
     150             :                 subreq, struct tevent_req);
     151         198 :         struct winbindd_getgrnam_state *state = tevent_req_data(
     152             :                 req, struct winbindd_getgrnam_state);
     153             :         NTSTATUS status;
     154             : 
     155         198 :         status = wb_getgrsid_recv(subreq, state, &state->domname, &state->name,
     156             :                                   &state->gid, &state->members);
     157         198 :         TALLOC_FREE(subreq);
     158         198 :         if (tevent_req_nterror(req, status)) {
     159           2 :                 return;
     160             :         }
     161         196 :         tevent_req_done(req);
     162             : }
     163             : 
     164         292 : NTSTATUS winbindd_getgrnam_recv(struct tevent_req *req,
     165             :                                 struct winbindd_response *response)
     166             : {
     167         292 :         struct winbindd_getgrnam_state *state = tevent_req_data(
     168             :                 req, struct winbindd_getgrnam_state);
     169             :         NTSTATUS status;
     170             :         int num_members;
     171             :         char *buf;
     172             : 
     173         292 :         if (tevent_req_is_nterror(req, &status)) {
     174             :                 struct dom_sid_buf sidbuf;
     175          96 :                 D_WARNING("Could not convert sid %s: %s\n",
     176             :                           dom_sid_str_buf(&state->sid, &sidbuf),
     177             :                           nt_errstr(status));
     178          96 :                 return status;
     179             :         }
     180             : 
     181         196 :         if (!fill_grent(talloc_tos(), &response->data.gr, state->domname,
     182             :                         state->name, state->gid)) {
     183           0 :                 D_WARNING("fill_grent failed\n");
     184           0 :                 return NT_STATUS_NO_MEMORY;
     185             :         }
     186             : 
     187         196 :         status = winbindd_print_groupmembers(state->members, response,
     188             :                                              &num_members, &buf);
     189         196 :         if (!NT_STATUS_IS_OK(status)) {
     190           0 :                 return status;
     191             :         }
     192             : 
     193         196 :         response->data.gr.num_gr_mem = (uint32_t)num_members;
     194             : 
     195             :         /* Group membership lives at start of extra data */
     196             : 
     197         196 :         response->data.gr.gr_mem_ofs = 0;
     198         196 :         response->extra_data.data = buf;
     199         196 :         response->length += talloc_get_size(response->extra_data.data);
     200             : 
     201         196 :         D_NOTICE("Winbind external command GETGRNAM end.\n"
     202             :                  "Returning %"PRIu32" memmber(s).\n",
     203             :                  response->data.gr.num_gr_mem);
     204             : 
     205         196 :         return NT_STATUS_OK;
     206             : }

Generated by: LCOV version 1.13