LCOV - code coverage report
Current view: top level - source3/winbindd - winbindd_list_users.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 67 92 72.8 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    async implementation of WINBINDD_LIST_USERS
       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 "lib/util/strv.h"
      24             : 
      25             : struct winbindd_list_users_domstate {
      26             :         struct tevent_req *subreq;
      27             :         struct winbindd_domain *domain;
      28             :         char *users;
      29             : };
      30             : 
      31             : struct winbindd_list_users_state {
      32             :         size_t num_received;
      33             :         /* All domains */
      34             :         size_t num_domains;
      35             :         struct winbindd_list_users_domstate *domains;
      36             : };
      37             : 
      38             : static void winbindd_list_users_done(struct tevent_req *subreq);
      39             : 
      40          28 : struct tevent_req *winbindd_list_users_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;
      46           0 :         struct winbindd_list_users_state *state;
      47           0 :         struct winbindd_domain *domain;
      48           0 :         size_t i;
      49             : 
      50          28 :         req = tevent_req_create(mem_ctx, &state,
      51             :                                 struct winbindd_list_users_state);
      52          28 :         if (req == NULL) {
      53           0 :                 return NULL;
      54             :         }
      55          28 :         D_NOTICE("[%s (%u)] Winbind external command LIST_USERS start.\n"
      56             :                  "WBFLAG_FROM_NSS is %s, winbind enum users is %d.\n",
      57             :                  cli->client_name,
      58             :                  (unsigned int)cli->pid,
      59             :                  request->wb_flags & WBFLAG_FROM_NSS ? "Set" : "Unset",
      60             :                  lp_winbind_enum_users());
      61             : 
      62          28 :         if (request->wb_flags & WBFLAG_FROM_NSS && !lp_winbind_enum_users()) {
      63           0 :                 tevent_req_done(req);
      64           0 :                 return tevent_req_post(req, ev);
      65             :         }
      66             : 
      67             :         /* Ensure null termination */
      68          28 :         request->domain_name[sizeof(request->domain_name)-1]='\0';
      69             : 
      70          28 :         D_NOTICE("Listing users for domain %s\n", request->domain_name);
      71          28 :         if (request->domain_name[0] != '\0') {
      72          10 :                 state->num_domains = 1;
      73             :         } else {
      74          18 :                 state->num_domains = 0;
      75          70 :                 for (domain = domain_list(); domain; domain = domain->next) {
      76          52 :                         state->num_domains += 1;
      77             :                 }
      78             :         }
      79             : 
      80          28 :         state->domains = talloc_array(state,
      81             :                                       struct winbindd_list_users_domstate,
      82             :                                       state->num_domains);
      83          28 :         if (tevent_req_nomem(state->domains, req)) {
      84           0 :                 return tevent_req_post(req, ev);
      85             :         }
      86             : 
      87          28 :         if (request->domain_name[0] != '\0') {
      88          20 :                 state->domains[0].domain = find_domain_from_name_noinit(
      89          10 :                         request->domain_name);
      90          10 :                 if (state->domains[0].domain == NULL) {
      91           0 :                         tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
      92           0 :                         return tevent_req_post(req, ev);
      93             :                 }
      94             :         } else {
      95          18 :                 i = 0;
      96          70 :                 for (domain = domain_list(); domain; domain = domain->next) {
      97          52 :                         state->domains[i++].domain = domain;
      98             :                 }
      99             :         }
     100             : 
     101          90 :         for (i=0; i<state->num_domains; i++) {
     102          62 :                 struct winbindd_list_users_domstate *d = &state->domains[i];
     103             :                 /*
     104             :                  * Use "state" as a talloc memory context since it has type
     105             :                  * "struct tevent_req". This is needed to make tevent call depth
     106             :                  * tracking working as expected.
     107             :                  * After calling wb_query_user_list_send(), re-parent back to
     108             :                  * "state->domains" to make TALLOC_FREE(state->domains) working.
     109             :                  */
     110          62 :                 d->subreq = wb_query_user_list_send(state, ev, d->domain);
     111          62 :                 d->subreq = talloc_reparent(state, state->domains, d->subreq);
     112          62 :                 if (tevent_req_nomem(d->subreq, req)) {
     113           0 :                         TALLOC_FREE(state->domains);
     114           0 :                         return tevent_req_post(req, ev);
     115             :                 }
     116          62 :                 tevent_req_set_callback(d->subreq, winbindd_list_users_done,
     117             :                                         req);
     118             :         }
     119          28 :         state->num_received = 0;
     120          28 :         return req;
     121             : }
     122             : 
     123          62 : static void winbindd_list_users_done(struct tevent_req *subreq)
     124             : {
     125          62 :         struct tevent_req *req = tevent_req_callback_data(
     126             :                 subreq, struct tevent_req);
     127          62 :         struct winbindd_list_users_state *state = tevent_req_data(
     128             :                 req, struct winbindd_list_users_state);
     129           0 :         struct winbindd_list_users_domstate *d;
     130           0 :         NTSTATUS status;
     131           0 :         size_t i;
     132             : 
     133         124 :         for (i=0; i<state->num_domains; i++) {
     134         124 :                 if (subreq == state->domains[i].subreq) {
     135          62 :                         break;
     136             :                 }
     137             :         }
     138          62 :         if (i == state->num_domains) {
     139           0 :                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
     140           0 :                 return;
     141             :         }
     142             : 
     143          62 :         d = &state->domains[i];
     144             : 
     145          62 :         status = wb_query_user_list_recv(subreq, state->domains,
     146             :                                          &d->users);
     147          62 :         TALLOC_FREE(subreq);
     148          62 :         if (!NT_STATUS_IS_OK(status)) {
     149             :                 /*
     150             :                  * Just skip this domain
     151             :                  */
     152           0 :                 d->users = NULL;
     153             :         }
     154             : 
     155          62 :         state->num_received += 1;
     156             : 
     157          62 :         if (state->num_received >= state->num_domains) {
     158          28 :                 tevent_req_done(req);
     159             :         }
     160             : }
     161             : 
     162          28 : NTSTATUS winbindd_list_users_recv(struct tevent_req *req,
     163             :                                   struct winbindd_response *response)
     164             : {
     165          28 :         struct winbindd_list_users_state *state = tevent_req_data(
     166             :                 req, struct winbindd_list_users_state);
     167           0 :         NTSTATUS status;
     168           0 :         char *result;
     169           0 :         size_t i, len;
     170             : 
     171          28 :         D_NOTICE("Winbind external command LIST_USERS end.\n");
     172          28 :         if (tevent_req_is_nterror(req, &status)) {
     173           0 :                 D_WARNING("Failed with %s.\n", nt_errstr(status));
     174           0 :                 return status;
     175             :         }
     176             : 
     177          28 :         result = NULL;
     178             : 
     179          90 :         for (i=0; i<state->num_domains; i++) {
     180          62 :                 struct winbindd_list_users_domstate *d = &state->domains[i];
     181           0 :                 int ret;
     182             : 
     183          62 :                 if (d->users == NULL) {
     184          26 :                         continue;
     185             :                 }
     186             : 
     187          36 :                 ret = strv_append(state, &result, d->users);
     188          36 :                 if (ret != 0) {
     189           0 :                         return map_nt_error_from_unix(ret);
     190             :                 }
     191             :         }
     192             : 
     193          28 :         len = talloc_get_size(result);
     194             : 
     195          28 :         response->extra_data.data = talloc_steal(response, result);
     196          28 :         response->length += len;
     197          28 :         response->data.num_entries = 0;
     198             : 
     199          28 :         if (result != NULL && len >= 1) {
     200          28 :                 len -= 1;
     201          28 :                 response->data.num_entries = 1;
     202             : 
     203       76622 :                 for (i=0; i<len; i++) {
     204       76594 :                         if (result[i] == '\0') {
     205        3376 :                                 result[i] = ',';
     206        3376 :                                 response->data.num_entries += 1;
     207             :                         }
     208             :                 }
     209             :         }
     210             : 
     211          28 :         D_NOTICE("Got %"PRIu32" user(s):\n%s\n",
     212             :                  response->data.num_entries,
     213             :                  result);
     214             : 
     215          28 :         return NT_STATUS_OK;
     216             : }

Generated by: LCOV version 1.14