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