LCOV - code coverage report
Current view: top level - source3/lib - id_cache.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 25 53 47.2 %
Date: 2021-09-23 10:06:22 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Samba Unix/Linux SMB client library
       3             :  *
       4             :  * Copyright (C) Gregor Beck 2011
       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             : /**
      21             :  * @brief  Notify smbd about idmap changes
      22             :  * @file   msg_idmap.c
      23             :  * @author Gregor Beck <gb@sernet.de>
      24             :  * @date   Feb 2011
      25             :  *
      26             :  */
      27             : 
      28             : #include "includes.h"
      29             : #include "messages.h"
      30             : #include "lib/id_cache.h"
      31             : #include "../lib/util/memcache.h"
      32             : #include "idmap_cache.h"
      33             : #include "../librpc/gen_ndr/ndr_security.h"
      34             : #include "../libcli/security/dom_sid.h"
      35             : 
      36        1622 : bool id_cache_ref_parse(const char* str, struct id_cache_ref* id)
      37             : {
      38             :         struct dom_sid sid;
      39             :         unsigned long ul;
      40             :         char c, trash;
      41             : 
      42        1622 :         if (sscanf(str, "%cID %lu%c", &c, &ul, &trash) == 2) {
      43           0 :                 switch(c) {
      44           0 :                 case 'G':
      45           0 :                         id->id.gid = ul;
      46           0 :                         id->type = GID;
      47           0 :                         return true;
      48           0 :                 case 'U':
      49           0 :                         id->id.uid = ul;
      50           0 :                         id->type = UID;
      51           0 :                         return true;
      52           0 :                 default:
      53           0 :                         break;
      54             :                 }
      55        1622 :         } else if (string_to_sid(&sid, str)) {
      56           0 :                 id->id.sid = sid;
      57           0 :                 id->type = SID;
      58           0 :                 return true;
      59        1622 :         } else if (strncmp(str, "USER ", 5) == 0) {
      60        1622 :                 id->id.name = str + 5;
      61        1622 :                 id->type = USERNAME;
      62        1622 :                 return true;
      63             :         }
      64           0 :         return false;
      65             : }
      66             : 
      67        1622 : static bool delete_getpwnam_cache(const char *username)
      68             : {
      69        1622 :         DATA_BLOB name = data_blob_string_const_null(username);
      70        1622 :         DEBUG(6, ("Delete passwd struct for %s from memcache\n",
      71             :                   username));
      72        1622 :         memcache_delete(NULL, GETPWNAM_CACHE, name);
      73        1622 :         return true;
      74             : }
      75             : 
      76        1622 : void id_cache_delete_from_cache(const struct id_cache_ref* id)
      77             : {
      78        1622 :         switch(id->type) {
      79           0 :         case UID:
      80           0 :                 idmap_cache_del_uid(id->id.uid);
      81           0 :                 break;
      82           0 :         case GID:
      83           0 :                 idmap_cache_del_gid(id->id.gid);
      84           0 :                 break;
      85           0 :         case SID:
      86           0 :                 idmap_cache_del_sid(&id->id.sid);
      87           0 :                 break;
      88        1622 :         case USERNAME:
      89        1622 :                 delete_getpwnam_cache(id->id.name);
      90        1622 :                 break;
      91           0 :         default:
      92           0 :                 break;
      93             :         }
      94        1622 : }
      95             : 
      96        1622 : void id_cache_delete_message(struct messaging_context *msg_ctx,
      97             :                              void *private_data,
      98             :                              uint32_t msg_type,
      99             :                              struct server_id server_id,
     100             :                              DATA_BLOB* data)
     101             : {
     102        1622 :         const char *msg = (data && data->data) ? (const char *)data->data : "<NULL>";
     103             :         struct id_cache_ref id;
     104             : 
     105        1622 :         if (!id_cache_ref_parse(msg, &id)) {
     106           0 :                 DEBUG(0, ("Invalid ?ID: %s\n", msg));
     107           0 :                 return;
     108             :         }
     109             : 
     110        1622 :         id_cache_delete_from_cache(&id);
     111             : }
     112             : 
     113       27307 : void id_cache_register_msgs(struct messaging_context *ctx)
     114             : {
     115       27307 :         messaging_register(ctx, NULL, ID_CACHE_DELETE, id_cache_delete_message);
     116       27307 : }

Generated by: LCOV version 1.13