r23792: convert Samba4 to GPLv3
[samba.git] / source4 / scripting / ejs / smbcalls_nbt.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Tim Potter 2005
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
22 #include "includes.h"
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "librpc/gen_ndr/nbt.h"
26 #include "lib/events/events.h"
27 #include "libcli/resolve/resolve.h"
28
29 /*
30   look up a netbios name
31
32   syntax:
33     resolveName(result, "frogurt");
34     resolveName(result, "frogurt", 0x1c);
35 */
36
37 static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
38 {
39         int result = -1;
40         struct nbt_name name;
41         TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());  
42         NTSTATUS nt_status;
43         const char *reply_addr;
44
45         /* validate arguments */
46         if (argc < 2 || argc > 3) {
47                 ejsSetErrorMsg(eid, "resolveName invalid arguments");
48                 goto done;
49         }
50
51         if (argv[0]->type != MPR_TYPE_OBJECT) {
52                 ejsSetErrorMsg(eid, "resolvename invalid arguments");
53                 goto done;
54         }
55
56         if (argv[1]->type != MPR_TYPE_STRING) {
57                 ejsSetErrorMsg(eid, "resolveName invalid arguments");
58                 goto done;
59         }
60         
61         if (argc == 2) {
62                 make_nbt_name_client(&name, mprToString(argv[1]));
63         } else {
64                 if (!mprVarIsNumber(argv[1]->type)) {
65                         ejsSetErrorMsg(eid, "resolveName invalid arguments");
66                         goto done;
67                 }
68                 make_nbt_name(&name, mprToString(argv[1]), mprToInt(argv[2]));
69         }
70
71         result = 0;
72
73         nt_status = resolve_name(&name, tmp_ctx, &reply_addr, event_context_find(tmp_ctx));
74
75         if (NT_STATUS_IS_OK(nt_status)) {
76                 mprSetPropertyValue(argv[0], "value", mprString(reply_addr));
77         }
78
79         mpr_Return(eid, mprNTSTATUS(nt_status));
80
81  done:
82         talloc_free(tmp_ctx);
83         return result;
84 }
85
86 /*
87   setup C functions that be called from ejs
88 */
89 NTSTATUS smb_setup_ejs_nbt(void)
90 {
91         ejsDefineCFunction(-1, "resolveName", ejs_resolve_name, NULL, MPR_VAR_SCRIPT_HANDLE);
92         return NT_STATUS_OK;
93 }