2 Unix SMB/CIFS implementation.
4 provide hooks into C calls from esp scripts
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
25 #include "web_server/esp/esp.h"
26 #include "param/loadparm.h"
27 #include "lib/ldb/include/ldb.h"
30 add an indexed array element to a property
32 static void mprAddArray(struct MprVar *var, int i, struct MprVar v)
35 mprItoa(i, idx, sizeof(idx));
36 mprCreateProperty(var, idx, &v);
40 construct a MprVar from a list
42 static struct MprVar mprList(const char *name, const char **list)
47 var = mprCreateObjVar(name, ESP_HASH_SIZE);
48 for (i=0;list && list[i];i++) {
49 mprAddArray(&var, i, mprCreateStringVar(list[i], 1));
55 construct a string MprVar from a lump of data
57 static struct MprVar mprData(const uint8_t *p, size_t length)
60 char *s = talloc_strndup(NULL, p, length);
62 return mprCreateUndefinedVar();
64 var = mprCreateStringVar(s, 1);
70 turn a ldb_message into a ejs object variable
72 static struct MprVar mprLdbMessage(struct ldb_message *msg)
76 /* we force some attributes to always be an array in the
77 returned structure. This makes the scripting easier, as you don't
78 need a special case for the single value case */
79 const char *multivalued[] = { "objectClass", "memberOf", "privilege",
83 var = mprCreateObjVar(msg->dn, ESP_HASH_SIZE);
85 for (i=0;i<msg->num_elements;i++) {
86 struct ldb_message_element *el = &msg->elements[i];
87 if (el->num_values == 1 &&
88 !str_list_check_ci(multivalued, el->name)) {
89 val = mprData(el->values[0].data, el->values[0].length);
92 val = mprCreateObjVar(el->name, ESP_HASH_SIZE);
93 for (j=0;j<el->num_values;j++) {
95 mprData(el->values[j].data,
96 el->values[j].length));
99 mprCreateProperty(&var, el->name, &val);
102 /* add the dn if it is not already specified */
103 if (mprGetProperty(&var, "dn", 0) == 0) {
104 val = mprCreateStringVar(msg->dn, 1);
105 mprCreateProperty(&var, "dn", &val);
113 turn an array of ldb_messages into a ejs object variable
115 static struct MprVar mprLdbArray(struct ldb_message **msg, int count,
121 res = mprCreateObjVar(name?name:"(NULL)", ESP_HASH_SIZE);
122 for (i=0;i<count;i++) {
123 mprAddArray(&res, i, mprLdbMessage(msg[i]));
130 turn a MprVar string variable into a const char *
132 static const char *mprToString(const struct MprVar *v)
134 if (v->type != MPR_TYPE_STRING) return NULL;
139 turn a MprVar object variable into a string list
140 this assumes the object variable consists only of strings
142 static const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v)
144 const char **list = NULL;
147 if (v->type != MPR_TYPE_OBJECT ||
148 v->properties == NULL) {
151 for (el=mprGetFirstProperty(v, MPR_ENUM_DATA);
153 el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) {
154 const char *s = mprToString(el);
156 list = str_list_add(list, s);
159 talloc_steal(mem_ctx, list);
164 return the type of a variable
166 static int esp_typeof(struct EspRequest *ep, int argc, struct MprVar **argv)
172 { MPR_TYPE_UNDEFINED, "undefined" },
173 { MPR_TYPE_NULL, "null" },
174 { MPR_TYPE_BOOL, "boolean" },
175 { MPR_TYPE_CFUNCTION, "function" },
176 { MPR_TYPE_FLOAT, "float" },
177 { MPR_TYPE_INT, "int" },
178 { MPR_TYPE_INT64, "int64" },
179 { MPR_TYPE_OBJECT, "object" },
180 { MPR_TYPE_FUNCTION, "function" },
181 { MPR_TYPE_STRING, "string" },
182 { MPR_TYPE_STRING_CFUNCTION, "function" }
185 const char *type = "unknown";
187 if (argc != 1) return -1;
189 for (i=0;i<ARRAY_SIZE(types);i++) {
190 if (argv[0]->type == types[i].type) {
191 type = types[i].name;
196 espSetReturnString(ep, type);
201 setup a return of a string list
203 static void esp_returnlist(struct EspRequest *ep,
204 const char *name, const char **list)
206 espSetReturn(ep, mprList(name, list));
210 return a list of defined services
212 static int esp_lpServices(struct EspRequest *ep, int argc, char **argv)
215 const char **list = NULL;
216 if (argc != 0) return -1;
218 for (i=0;i<lp_numservices();i++) {
219 list = str_list_add(list, lp_servicename(i));
221 talloc_steal(ep, list);
222 esp_returnlist(ep, "services", list);
228 allow access to loadparm variables from inside esp scripts in swat
230 can be called in 4 ways:
232 v = lpGet("type:parm"); gets a parametric variable
233 v = lpGet("share", "type:parm"); gets a parametric variable on a share
234 v = lpGet("parm"); gets a global variable
235 v = lpGet("share", "parm"); gets a share variable
237 the returned variable is a ejs object. It is an array object for lists.
239 static int esp_lpGet(struct EspRequest *ep, int argc, char **argv)
241 struct parm_struct *parm = NULL;
242 void *parm_ptr = NULL;
245 if (argc < 1) return -1;
248 /* its a share parameter */
249 int snum = lp_servicenumber(argv[0]);
253 if (strchr(argv[1], ':')) {
254 /* its a parametric option on a share */
255 const char *type = talloc_strndup(ep, argv[1], strcspn(argv[1], ":"));
256 const char *option = strchr(argv[1], ':') + 1;
258 if (type == NULL || option == NULL) return -1;
259 value = lp_get_parametric(snum, type, option);
260 if (value == NULL) return -1;
261 espSetReturnString(ep, value);
265 parm = lp_parm_struct(argv[1]);
266 if (parm == NULL || parm->class == P_GLOBAL) {
269 parm_ptr = lp_parm_ptr(snum, parm);
270 } else if (strchr(argv[0], ':')) {
271 /* its a global parametric option */
272 const char *type = talloc_strndup(ep, argv[0], strcspn(argv[0], ":"));
273 const char *option = strchr(argv[0], ':') + 1;
275 if (type == NULL || option == NULL) return -1;
276 value = lp_get_parametric(-1, type, option);
277 if (value == NULL) return -1;
278 espSetReturnString(ep, value);
281 /* its a global parameter */
282 parm = lp_parm_struct(argv[0]);
283 if (parm == NULL) return -1;
284 parm_ptr = parm->ptr;
287 if (parm == NULL || parm_ptr == NULL) {
291 /* construct and return the right type of ejs object */
292 switch (parm->type) {
295 espSetReturnString(ep, *(char **)parm_ptr);
298 espSetReturn(ep, mprCreateBoolVar(*(BOOL *)parm_ptr));
301 espSetReturn(ep, mprCreateIntegerVar(*(int *)parm_ptr));
304 for (i=0; parm->enum_list[i].name; i++) {
305 if (*(int *)parm_ptr == parm->enum_list[i].value) {
306 espSetReturnString(ep, parm->enum_list[i].name);
312 esp_returnlist(ep, parm->label, *(const char ***)parm_ptr);
322 perform an ldb search, returning an array of results
325 ldbSearch("dbfile", "expression");
326 var attrs = new Array("attr1", "attr2", "attr3");
327 ldbSearch("dbfile", "expression", attrs);
329 static int esp_ldbSearch(struct EspRequest *ep, int argc, struct MprVar **argv)
331 const char **attrs = NULL;
332 const char *expression, *dbfile;
333 TALLOC_CTX *tmp_ctx = talloc_new(ep);
334 struct ldb_context *ldb;
336 struct ldb_message **res;
338 /* validate arguments */
339 if (argc < 2 || argc > 3 ||
340 argv[0]->type != MPR_TYPE_STRING) {
341 espError(ep, "ldbSearch invalid arguments");
344 if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
345 espError(ep, "ldbSearch attributes must be an object");
349 dbfile = mprToString(argv[0]);
350 expression = mprToString(argv[1]);
352 attrs = mprToList(tmp_ctx, argv[2]);
354 if (dbfile == NULL || expression == NULL) {
355 espError(ep, "ldbSearch invalid arguments");
359 ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
361 espError(ep, "ldbSearch failed to open %s", dbfile);
365 ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
367 espError(ep, "ldbSearch failed - %s", ldb_errstring(ldb));
371 espSetReturn(ep, mprLdbArray(res, ret, "ldb_message"));
373 talloc_free(tmp_ctx);
377 talloc_free(tmp_ctx);
381 /* try to authenticate the user/password pair against system auth mechanisms
385 fills in the session structure properly in case of success
386 NOTE: Currently only PAM Auth is supported
389 static int esp_unixAuth(struct EspRequest *ep, int argc, struct MprVar **argv)
391 TALLOC_CTX *tmp_ctx = talloc_new(ep);
392 const char *username;
393 const char *password;
397 if (argc != 2 || argv[0]->type != MPR_TYPE_STRING ||
398 argv[1]->type != MPR_TYPE_STRING) {
399 espError(ep, "unixAuth invalid arguments");
404 username = mprToString(argv[0]);
405 password = mprToString(argv[1]);
407 if (username == NULL || password == NULL) {
408 espError(ep, "unixAuth invalid arguments");
413 /* TODO: find out how to pass the real client name/address here */
414 if (NT_STATUS_IS_OK(unix_passcheck(tmp_ctx, "client", username, password))) {
416 pwd = getpwnam(username);
418 espSetReturn(ep, mprCreateIntegerVar(-1));
423 mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
424 "AUTHENTICATED", mprCreateStringVar("1", 0));
425 mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
426 "USERNAME", mprCreateStringVar(username, 0));
428 if (pwd->pw_uid == 0) { /* we are root */
430 mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
431 "PRIVILEGE", mprCreateStringVar("ADMIN", 0));
433 mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
434 "PRIVILEGE", mprCreateStringVar("USER", 0));
437 espSetReturn(ep, mprCreateIntegerVar(0));
439 if (mprGetProperty(&ep->variables[ESP_SESSION_OBJ], "AUTHENTICATED", 0) != 0) {
440 mprDeleteProperty(&ep->variables[ESP_SESSION_OBJ], "AUTHENTICATED");
442 espSetReturn(ep, mprCreateIntegerVar(-1));
446 talloc_free(tmp_ctx);
451 setup the C functions that be called from ejs
453 void http_setup_ejs_functions(void)
455 espDefineStringCFunction(NULL, "lpGet", esp_lpGet, NULL);
456 espDefineStringCFunction(NULL, "lpServices", esp_lpServices, NULL);
457 espDefineCFunction(NULL, "typeof", esp_typeof, NULL);
458 espDefineCFunction(NULL, "ldbSearch", esp_ldbSearch, NULL);
459 espDefineCFunction(NULL, "unixAuth", esp_unixAuth, NULL);