LCOV - code coverage report
Current view: top level - source4/nbt_server/wins - wins_hook.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-lts cc996e7c Lines: 36 41 87.8 %
Date: 2025-10-17 03:45:34 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    wins hook feature, we run a specified script
       5             :    which can then do some custom actions
       6             : 
       7             :    Copyright (C) Stefan Metzmacher      2005
       8             :       
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             :    
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             :    
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include "nbt_server/nbt_server.h"
      25             : #include "nbt_server/wins/winsdb.h"
      26             : #include "system/filesys.h"
      27             : 
      28        1043 : static const char *wins_hook_action_string(enum wins_hook_action action)
      29             : {
      30        1043 :         switch (action) {
      31         205 :         case WINS_HOOK_ADD:     return "add";
      32         711 :         case WINS_HOOK_MODIFY:  return "refresh";
      33         127 :         case WINS_HOOK_DELETE:  return "delete";
      34             :         }
      35             : 
      36           0 :         return "unknown";
      37             : }
      38             : 
      39        1077 : void wins_hook(struct winsdb_handle *h, const struct winsdb_record *rec, 
      40             :                enum wins_hook_action action, const char *wins_hook_script)
      41             : {
      42             :         uint32_t i, length;
      43             :         int child;
      44        1077 :         char *cmd = NULL;
      45        1077 :         TALLOC_CTX *tmp_mem = NULL;
      46        1077 :         const char *p = NULL;
      47             : 
      48        1077 :         if (!wins_hook_script || !wins_hook_script[0]) return;
      49             : 
      50       12636 :         for (p = rec->name->name; *p; p++) {
      51       11593 :                 if (!(isalnum((int)*p) || strchr_m("._-", *p))) {
      52          34 :                         DBG_ERR("not calling wins hook for invalid name %s\n",
      53             :                                 rec->name->name);
      54          34 :                         return;
      55             :                 }
      56             :         }
      57             : 
      58        1043 :         tmp_mem = talloc_new(h);
      59        1043 :         if (!tmp_mem) goto failed;
      60             : 
      61        1043 :         length = winsdb_addr_list_length(rec->addresses);
      62             : 
      63        1043 :         if (action == WINS_HOOK_MODIFY && length < 1) {
      64         102 :                 action = WINS_HOOK_DELETE;
      65             :         }
      66             : 
      67        1043 :         cmd = talloc_asprintf(tmp_mem,
      68             :                               "%s %s %s %02x %ld",
      69             :                               wins_hook_script,
      70             :                               wins_hook_action_string(action),
      71        1043 :                               rec->name->name,
      72        1043 :                               rec->name->type,
      73        1043 :                               (long int) rec->expire_time);
      74        1043 :         if (!cmd) goto failed;
      75             : 
      76        2156 :         for (i=0; rec->addresses[i]; i++) {
      77        1113 :                 cmd = talloc_asprintf_append_buffer(cmd, " %s", rec->addresses[i]->address);
      78        1113 :                 if (!cmd) goto failed;
      79             :         }
      80             : 
      81        1043 :         DEBUG(10,("call wins hook '%s'\n", cmd));
      82             : 
      83             :         /* signal handling in posix really sucks - doing this in a library
      84             :            affects the whole app, but what else to do?? */
      85        1043 :         signal(SIGCHLD, SIG_IGN);
      86             : 
      87        1043 :         child = fork();
      88        2086 :         if (child == (pid_t)-1) {
      89           0 :                 goto failed;
      90             :         }
      91             : 
      92        2086 :         if (child == 0) {
      93             : /* TODO: close file handles */
      94        1043 :                 execl("/bin/sh", "sh", "-c", cmd, NULL);
      95        1043 :                 _exit(0);
      96             :         }
      97             : 
      98        1043 :         talloc_free(tmp_mem);
      99        1043 :         return;
     100           0 : failed:
     101           0 :         talloc_free(tmp_mem);
     102           0 :         DEBUG(0,("FAILED: calling wins hook '%s'\n", wins_hook_script));
     103             : }

Generated by: LCOV version 1.13