r8296: - split out the ejs auth functions into a separate file
[jra/samba/.git] / source4 / scripting / ejs / smbcalls_config.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Andrew Tridgell 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/ejs/ejs.h"
26 #include "param/loadparm.h"
27
28 /*
29   return a list of defined services
30 */
31 static int ejs_lpServices(MprVarHandle eid, int argc, char **argv)
32 {
33         int i;
34         const char **list = NULL;
35         if (argc != 0) return -1;
36         
37         for (i=0;i<lp_numservices();i++) {
38                 list = str_list_add(list, lp_servicename(i));
39         }
40         talloc_steal(mprMemCtx(), list);
41         mpr_Return(eid, mprList("services", list));
42         return 0;
43 }
44
45
46 /*
47   allow access to loadparm variables from inside ejs scripts in swat
48   
49   can be called in 4 ways:
50
51     v = lpGet("type:parm");             gets a parametric variable
52     v = lpGet("share", "type:parm");    gets a parametric variable on a share
53     v = lpGet("parm");                  gets a global variable
54     v = lpGet("share", "parm");         gets a share variable
55
56   the returned variable is a ejs object. It is an array object for lists.  
57 */
58 static int ejs_lpGet(MprVarHandle eid, int argc, char **argv)
59 {
60         struct parm_struct *parm = NULL;
61         void *parm_ptr = NULL;
62         int i;
63
64         if (argc < 1) return -1;
65
66         if (argc == 2) {
67                 /* its a share parameter */
68                 int snum = lp_servicenumber(argv[0]);
69                 if (snum == -1) {
70                         return -1;
71                 }
72                 if (strchr(argv[1], ':')) {
73                         /* its a parametric option on a share */
74                         const char *type = talloc_strndup(mprMemCtx(), 
75                                                           argv[1], 
76                                                           strcspn(argv[1], ":"));
77                         const char *option = strchr(argv[1], ':') + 1;
78                         const char *value;
79                         if (type == NULL || option == NULL) return -1;
80                         value = lp_get_parametric(snum, type, option);
81                         if (value == NULL) return -1;
82                         mpr_ReturnString(eid, value);
83                         return 0;
84                 }
85
86                 parm = lp_parm_struct(argv[1]);
87                 if (parm == NULL || parm->class == P_GLOBAL) {
88                         return -1;
89                 }
90                 parm_ptr = lp_parm_ptr(snum, parm);
91         } else if (strchr(argv[0], ':')) {
92                 /* its a global parametric option */
93                 const char *type = talloc_strndup(mprMemCtx(), 
94                                                   argv[0], strcspn(argv[0], ":"));
95                 const char *option = strchr(argv[0], ':') + 1;
96                 const char *value;
97                 if (type == NULL || option == NULL) return -1;
98                 value = lp_get_parametric(-1, type, option);
99                 if (value == NULL) return -1;
100                 mpr_ReturnString(eid, value);
101                 return 0;
102         } else {
103                 /* its a global parameter */
104                 parm = lp_parm_struct(argv[0]);
105                 if (parm == NULL) return -1;
106                 parm_ptr = parm->ptr;
107         }
108
109         if (parm == NULL || parm_ptr == NULL) {
110                 return -1;
111         }
112
113         /* construct and return the right type of ejs object */
114         switch (parm->type) {
115         case P_STRING:
116         case P_USTRING:
117                 mpr_ReturnString(eid, *(char **)parm_ptr);
118                 break;
119         case P_BOOL:
120                 mpr_Return(eid, mprCreateBoolVar(*(BOOL *)parm_ptr));
121                 break;
122         case P_INTEGER:
123                 mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr));
124                 break;
125         case P_ENUM:
126                 for (i=0; parm->enum_list[i].name; i++) {
127                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
128                                 mpr_ReturnString(eid, parm->enum_list[i].name);
129                                 return 0;
130                         }
131                 }
132                 return -1;      
133         case P_LIST: 
134                 mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr));
135                 break;
136         case P_SEP:
137                 return -1;
138         }
139         return 0;
140 }
141
142 /*
143   setup C functions that be called from ejs
144 */
145 void smb_setup_ejs_config(void)
146 {
147         ejsDefineStringCFunction(-1, "lpGet", ejs_lpGet, NULL, MPR_VAR_SCRIPT_HANDLE);
148         ejsDefineStringCFunction(-1, "lpServices", ejs_lpServices, NULL, MPR_VAR_SCRIPT_HANDLE);
149 }