6f1649d131f184b8436d50a3321b5bdfaa82103a
[jra/samba/.git] / source4 / web_server / calls.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into C calls from esp 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 "web_server/esp/esp.h"
25 #include "param/loadparm.h"
26
27
28 /*
29   return the type of a variable
30 */
31 static int esp_typeof(struct EspRequest *ep, int argc, struct MprVar **argv)
32 {
33         const struct {
34                 MprType type;
35                 const char *name;
36         } types[] = {
37                 { MPR_TYPE_UNDEFINED, "undefined" },
38                 { MPR_TYPE_NULL, "null" },
39                 { MPR_TYPE_BOOL, "boolean" },
40                 { MPR_TYPE_CFUNCTION, "function" },
41                 { MPR_TYPE_FLOAT, "float" },
42                 { MPR_TYPE_INT, "int" },
43                 { MPR_TYPE_INT64, "int64" },
44                 { MPR_TYPE_OBJECT, "object" },
45                 { MPR_TYPE_FUNCTION, "function" },
46                 { MPR_TYPE_STRING, "string" },
47                 { MPR_TYPE_STRING_CFUNCTION, "function" }
48         };
49         int i;
50         const char *type = "unknown";
51
52         if (argc != 1) return -1;
53         
54         for (i=0;i<ARRAY_SIZE(types);i++) {
55                 if (argv[0]->type == types[i].type) {
56                         type = types[i].name;
57                         break;
58                 }
59         }
60
61         espSetReturnString(ep, type);
62         return 0;
63 }
64
65
66 /*
67   allow access to loadparm variables from inside esp scripts in swat
68   
69   can be called in 4 ways:
70
71     v = lpGet("type:parm");             gets a parametric variable
72     v = lpGet("share", "type:parm");    gets a parametric variable on a share
73     v = lpGet("parm");                  gets a global variable
74     v = lpGet("share", "parm");         gets a share variable
75
76   the returned variable is a ejs object. It is an array object for lists.  
77 */
78 static int esp_lpGet(struct EspRequest *ep, int argc, char **argv)
79 {
80         struct parm_struct *parm = NULL;
81         void *parm_ptr = NULL;
82         int i;
83
84         if (argc < 1) return -1;
85
86         if (argc == 2) {
87                 /* its a share parameter */
88                 int snum = lp_servicenumber(argv[0]);
89                 if (snum == -1) {
90                         return -1;
91                 }
92                 if (strchr(argv[1], ':')) {
93                         /* its a parametric option on a share */
94                         const char *type = talloc_strndup(ep, argv[1], strcspn(argv[1], ":"));
95                         const char *option = strchr(argv[1], ':') + 1;
96                         const char *value;
97                         if (type == NULL || option == NULL) return -1;
98                         value = lp_get_parametric(snum, type, option);
99                         if (value == NULL) return -1;
100                         espSetReturnString(ep, value);
101                         return 0;
102                 }
103
104                 parm = lp_parm_struct(argv[1]);
105                 if (parm == NULL || parm->class == P_GLOBAL) {
106                         return -1;
107                 }
108                 parm_ptr = lp_parm_ptr(snum, parm);
109         } else if (strchr(argv[0], ':')) {
110                 /* its a global parametric option */
111                 const char *type = talloc_strndup(ep, argv[0], strcspn(argv[0], ":"));
112                 const char *option = strchr(argv[0], ':') + 1;
113                 const char *value;
114                 if (type == NULL || option == NULL) return -1;
115                 value = lp_get_parametric(-1, type, option);
116                 if (value == NULL) return -1;
117                 espSetReturnString(ep, value);
118                 return 0;
119         } else {
120                 /* its a global parameter */
121                 parm = lp_parm_struct(argv[0]);
122                 if (parm == NULL) return -1;
123                 parm_ptr = parm->ptr;
124         }
125
126         if (parm == NULL || parm_ptr == NULL) {
127                 return -1;
128         }
129
130         /* construct and return the right type of ejs object */
131         switch (parm->type) {
132         case P_STRING:
133         case P_USTRING:
134                 espSetReturnString(ep, *(char **)parm_ptr);
135                 break;
136         case P_BOOL:
137                 espSetReturn(ep, mprCreateBoolVar(*(BOOL *)parm_ptr));
138                 break;
139         case P_INTEGER:
140                 espSetReturn(ep, mprCreateIntegerVar(*(int *)parm_ptr));
141                 break;
142         case P_ENUM:
143                 for (i=0; parm->enum_list[i].name; i++) {
144                         if (*(int *)parm_ptr == parm->enum_list[i].value) {
145                                 espSetReturnString(ep, parm->enum_list[i].name);
146                                 return 0;
147                         }
148                 }
149                 return -1;
150         
151         case P_LIST: {
152                 const char **list = *(const char ***)parm_ptr;
153                 struct MprVar var;
154                 var = mprCreateObjVar(parm->label, 10);
155                 for (i=0;list[i];i++) {
156                         char idx[16];
157                         struct MprVar val;
158                         mprItoa(i, idx, sizeof(idx));
159                         val = mprCreateStringVar(list[i], 1);
160                         mprCreateProperty(&var, idx, &val);
161                 }
162                 espSetReturn(ep, var);
163                 break;
164         case P_SEP:
165                 return -1;
166         }
167         }
168         return 0;
169 }
170
171 /*
172   setup the C functions that be called from ejs
173 */
174 void http_setup_ejs_functions(void)
175 {
176         espDefineStringCFunction(NULL, "lpGet", esp_lpGet, NULL);
177         espDefineCFunction(NULL, "typeof", esp_typeof, NULL);
178 }