r26313: Fix more uses of static loadparm.
[mat/samba.git] / source4 / nbt_server / wins / wins_hook.c
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 #include "param/param.h"
28
29 static const char *wins_hook_action_string(enum wins_hook_action action)
30 {
31         switch (action) {
32         case WINS_HOOK_ADD:     return "add";
33         case WINS_HOOK_MODIFY:  return "refresh";
34         case WINS_HOOK_DELETE:  return "delete";
35         }
36
37         return "unknown";
38 }
39
40 void wins_hook(struct winsdb_handle *h, const struct winsdb_record *rec, 
41                enum wins_hook_action action, const char *wins_hook_script)
42 {
43         uint32_t i, length;
44         int child;
45         char *cmd = NULL;
46         TALLOC_CTX *tmp_mem = NULL;
47
48         if (!wins_hook_script || !wins_hook_script[0]) return;
49
50         tmp_mem = talloc_new(h);
51         if (!tmp_mem) goto failed;
52
53         length = winsdb_addr_list_length(rec->addresses);
54
55         if (action == WINS_HOOK_MODIFY && length < 1) {
56                 action = WINS_HOOK_DELETE;
57         }
58
59         cmd = talloc_asprintf(tmp_mem,
60                               "%s %s %s %02x %ld",
61                               wins_hook_script,
62                               wins_hook_action_string(action),
63                               rec->name->name,
64                               rec->name->type,
65                               rec->expire_time);
66         if (!cmd) goto failed;
67
68         for (i=0; rec->addresses[i]; i++) {
69                 cmd = talloc_asprintf_append_buffer(cmd, " %s", rec->addresses[i]->address);
70                 if (!cmd) goto failed;
71         }
72
73         DEBUG(10,("call wins hook '%s'\n", cmd));
74
75         /* signal handling in posix really sucks - doing this in a library
76            affects the whole app, but what else to do?? */
77         signal(SIGCHLD, SIG_IGN);
78
79         child = fork();
80         if (child == (pid_t)-1) {
81                 goto failed;
82         }
83
84         if (child == 0) {
85 /* TODO: close file handles */
86                 execl("/bin/sh", "sh", "-c", cmd, NULL);
87                 _exit(0);
88         }
89
90         talloc_free(tmp_mem);
91         return;
92 failed:
93         talloc_free(tmp_mem);
94         DEBUG(0,("FAILED: calling wins hook '%s'\n", wins_hook_script));
95 }