LCOV - code coverage report
Current view: top level - source3/winbindd - idmap_util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 4 52 7.7 %
Date: 2024-04-21 15:09:00 Functions: 1 5 20.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    ID Mapping
       4             :    Copyright (C) Simo Sorce 2003
       5             :    Copyright (C) Jeremy Allison 2006
       6             :    Copyright (C) Michael Adam 2010
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
      20             : 
      21             : #include "includes.h"
      22             : #include "winbindd.h"
      23             : #include "winbindd_proto.h"
      24             : #include "idmap.h"
      25             : #include "idmap_cache.h"
      26             : #include "../libcli/security/security.h"
      27             : #include "secrets.h"
      28             : 
      29             : #undef DBGC_CLASS
      30             : #define DBGC_CLASS DBGC_IDMAP
      31             : 
      32             : /**
      33             :  * check whether a given unix id is inside the filter range of an idmap domain
      34             :  */
      35          25 : bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom)
      36             : {
      37          25 :         if ((dom->low_id && (id < dom->low_id)) ||
      38          25 :             (dom->high_id && (id > dom->high_id)))
      39             :         {
      40           1 :                 return false;
      41             :         }
      42             : 
      43           0 :         return true;
      44             : }
      45             : 
      46             : /**
      47             :  * Helper for unixids_to_sids: find entry by id in mapping array,
      48             :  * search up to IDMAP_AD_MAX_IDS entries
      49             :  */
      50           0 : struct id_map *idmap_find_map_by_id(struct id_map **maps, enum id_type type,
      51             :                                     uint32_t id)
      52             : {
      53           0 :         int i;
      54             : 
      55           0 :         for (i = 0; maps[i] != NULL; i++) {
      56           0 :                 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
      57           0 :                         return maps[i];
      58             :                 }
      59             :         }
      60             : 
      61           0 :         return NULL;
      62             : }
      63             : 
      64             : /**
      65             :  * Helper for sids_to_unix_ids: find entry by SID in mapping array,
      66             :  * search up to IDMAP_AD_MAX_IDS entries
      67             :  */
      68           0 : struct id_map *idmap_find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
      69             : {
      70           0 :         int i;
      71             : 
      72           0 :         for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
      73           0 :                 if (maps[i] == NULL) { /* end of the run */
      74           0 :                         return NULL;
      75             :                 }
      76           0 :                 if (dom_sid_equal(maps[i]->sid, sid)) {
      77           0 :                         return maps[i];
      78             :                 }
      79             :         }
      80             : 
      81           0 :         return NULL;
      82             : }
      83             : 
      84           0 : char *idmap_fetch_secret(const char *backend, const char *domain,
      85             :                          const char *identity)
      86             : {
      87           0 :         char *tmp, *ret;
      88           0 :         int r;
      89             : 
      90           0 :         r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
      91             : 
      92           0 :         if (r < 0)
      93           0 :                 return NULL;
      94             : 
      95             :         /* make sure the key is case insensitive */
      96           0 :         if (!strupper_m(tmp)) {
      97           0 :                 SAFE_FREE(tmp);
      98           0 :                 return NULL;
      99             :         }
     100             : 
     101           0 :         ret = secrets_fetch_generic(tmp, identity);
     102             : 
     103           0 :         SAFE_FREE(tmp);
     104             : 
     105           0 :         return ret;
     106             : }
     107             : 
     108           0 : struct id_map **id_map_ptrs_init(TALLOC_CTX *mem_ctx, size_t num_ids)
     109             : {
     110           0 :         struct id_map **ptrs;
     111           0 :         struct id_map *maps;
     112           0 :         struct dom_sid *sids;
     113           0 :         size_t i;
     114             : 
     115           0 :         ptrs = talloc_array(mem_ctx, struct id_map *, num_ids+1);
     116           0 :         if (ptrs == NULL) {
     117           0 :                 return NULL;
     118             :         }
     119           0 :         maps = talloc_array(ptrs, struct id_map, num_ids);
     120           0 :         if (maps == NULL) {
     121           0 :                 TALLOC_FREE(ptrs);
     122           0 :                 return NULL;
     123             :         }
     124           0 :         sids = talloc_zero_array(ptrs, struct dom_sid, num_ids);
     125           0 :         if (sids == NULL) {
     126           0 :                 TALLOC_FREE(ptrs);
     127           0 :                 return NULL;
     128             :         }
     129             : 
     130           0 :         for (i=0; i<num_ids; i++) {
     131           0 :                 maps[i] = (struct id_map) { .sid = &sids[i] };
     132           0 :                 ptrs[i] = &maps[i];
     133             :         }
     134           0 :         ptrs[num_ids] = NULL;
     135             : 
     136           0 :         return ptrs;
     137             : }

Generated by: LCOV version 1.14