LCOV - code coverage report
Current view: top level - source3/winbindd - winbindd_group.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 47 66 71.2 %
Date: 2024-02-29 22:57:05 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Winbind daemon for ntdom nss module
       5             : 
       6             :    Copyright (C) Tim Potter 2000
       7             :    Copyright (C) Jeremy Allison 2001.
       8             :    Copyright (C) Gerald (Jerry) Carter 2003.
       9             :    Copyright (C) Volker Lendecke 2005
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : #include "winbindd.h"
      27             : #include "lib/dbwrap/dbwrap.h"
      28             : 
      29             : #undef DBGC_CLASS
      30             : #define DBGC_CLASS DBGC_WINBIND
      31             : 
      32             : /* Fill a grent structure from various other information */
      33             : 
      34        5718 : bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
      35             :                 const char *dom_name, const char *gr_name, gid_t unix_gid)
      36             : {
      37           0 :         const char *full_group_name;
      38        5718 :         char *mapped_name = NULL;
      39        5718 :         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
      40             : 
      41        5718 :         nt_status = normalize_name_map(mem_ctx, dom_name, gr_name,
      42             :                                        &mapped_name);
      43             : 
      44        5718 :         D_DEBUG("Filling domain '%s' and group '%s'.\n", dom_name, gr_name);
      45             :         /* Basic whitespace replacement */
      46        5718 :         if (NT_STATUS_IS_OK(nt_status)) {
      47           0 :                 full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
      48             :                                      mapped_name, true);
      49             :         }
      50             :         /* Mapped to an alias */
      51        5718 :         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED)) {
      52           0 :                 full_group_name = mapped_name;
      53             :         }
      54             :         /* no change */
      55             :         else {
      56        5718 :                 full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
      57             :                                       gr_name, True );
      58             :         }
      59             : 
      60        5718 :         if (full_group_name == NULL) {
      61           0 :                 D_DEBUG("Returning false, since there is no full group name.\n");
      62           0 :                 return false;
      63             :         }
      64             : 
      65        5718 :         gr->gr_gid = unix_gid;
      66             : 
      67             :         /* Group name and password */
      68             : 
      69        5718 :         strlcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name));
      70        5718 :         strlcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd));
      71             : 
      72        5718 :         D_DEBUG("Returning true. Full group name is '%s'.\n", gr_name);
      73        5718 :         return True;
      74             : }
      75             : 
      76             : struct getgr_countmem {
      77             :         int num;
      78             :         size_t len;
      79             : };
      80             : 
      81          74 : static int getgr_calc_memberlen(struct db_record *rec, void *private_data)
      82             : {
      83          74 :         struct getgr_countmem *buf = private_data;
      84          74 :         TDB_DATA data = dbwrap_record_get_value(rec);
      85           0 :         size_t len;
      86             : 
      87          74 :         buf->num += 1;
      88             : 
      89          74 :         len = buf->len + data.dsize;
      90          74 :         if (len < buf->len) {
      91           0 :                 return 0;
      92             :         }
      93          74 :         buf->len = len;
      94          74 :         return 0;
      95             : }
      96             : 
      97             : struct getgr_stringmem {
      98             :         size_t ofs;
      99             :         char *buf;
     100             : };
     101             : 
     102          74 : static int getgr_unparse_members(struct db_record *rec, void *private_data)
     103             : {
     104          74 :         struct getgr_stringmem *buf = private_data;
     105          74 :         TDB_DATA data = dbwrap_record_get_value(rec);
     106           0 :         int len;
     107             : 
     108          74 :         len = data.dsize-1;
     109             : 
     110          74 :         memcpy(buf->buf + buf->ofs, data.dptr, len);
     111          74 :         buf->ofs += len;
     112          74 :         buf->buf[buf->ofs] = ',';
     113          74 :         buf->ofs += 1;
     114          74 :         return 0;
     115             : }
     116             : 
     117        5718 : NTSTATUS winbindd_print_groupmembers(struct db_context *members,
     118             :                                      TALLOC_CTX *mem_ctx,
     119             :                                      int *num_members, char **result)
     120             : {
     121           0 :         struct getgr_countmem c;
     122           0 :         struct getgr_stringmem m;
     123           0 :         int count;
     124           0 :         NTSTATUS status;
     125             : 
     126        5718 :         c.num = 0;
     127        5718 :         c.len = 0;
     128             : 
     129        5718 :         status = dbwrap_traverse(members, getgr_calc_memberlen, &c, &count);
     130        5718 :         if (!NT_STATUS_IS_OK(status)) {
     131           0 :                 DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
     132           0 :                 return status;
     133             :         }
     134             : 
     135        5718 :         m.ofs = 0;
     136        5718 :         m.buf = talloc_array(mem_ctx, char, c.len);
     137        5718 :         if (m.buf == NULL) {
     138           0 :                 D_WARNING("talloc failed\n");
     139           0 :                 return NT_STATUS_NO_MEMORY;
     140             :         }
     141             : 
     142        5718 :         status = dbwrap_traverse(members, getgr_unparse_members, &m, &count);
     143        5718 :         if (!NT_STATUS_IS_OK(status)) {
     144           0 :                 TALLOC_FREE(m.buf);
     145           0 :                 DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
     146           0 :                 return status;
     147             :         }
     148        5718 :         if (c.len > 0) {
     149          14 :                 m.buf[c.len - 1] = '\0';
     150             :         }
     151             : 
     152        5718 :         *num_members = c.num;
     153        5718 :         *result = m.buf;
     154        5718 :         D_DEBUG("Returning %d member(s).\n", *num_members);
     155        5718 :         return NT_STATUS_OK;
     156             : }

Generated by: LCOV version 1.14