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