r7058: Add experimenthal pam authentication for web pages
[sfrench/samba-autobuild/.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 "pwd.h"
25 #include "web_server/esp/esp.h"
26 #include "param/loadparm.h"
27 #include "lib/ldb/include/ldb.h"
28
29 /*
30   add an indexed array element to a property
31 */
32 static void mprAddArray(struct MprVar *var, int i, struct MprVar v)
33 {
34         char idx[16];
35         mprItoa(i, idx, sizeof(idx));
36         mprCreateProperty(var, idx, &v);
37 }
38
39 /*
40   construct a MprVar from a list
41 */
42 static struct MprVar mprList(const char *name, const char **list)
43 {
44         struct MprVar var;
45         int i;
46
47         var = mprCreateObjVar(name, ESP_HASH_SIZE);
48         for (i=0;list && list[i];i++) {
49                 mprAddArray(&var, i, mprCreateStringVar(list[i], 1));
50         }
51         return var;
52 }
53
54 /*
55   construct a string MprVar from a lump of data
56 */
57 static struct MprVar mprData(const uint8_t *p, size_t length)
58 {
59         struct MprVar var;
60         char *s = talloc_strndup(NULL, p, length);
61         if (s == NULL) {
62                 return mprCreateUndefinedVar();
63         }
64         var = mprCreateStringVar(s, 1);
65         talloc_free(s);
66         return var;
67 }
68
69 /*
70   turn a ldb_message into a ejs object variable
71 */
72 static struct MprVar mprLdbMessage(struct ldb_message *msg)
73 {
74         struct MprVar var;
75         int i;
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", 
80                                             "member", NULL };
81         struct MprVar val;
82
83         var = mprCreateObjVar(msg->dn, ESP_HASH_SIZE);
84
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);
90                 } else {
91                         int j;
92                         val = mprCreateObjVar(el->name, ESP_HASH_SIZE);
93                         for (j=0;j<el->num_values;j++) {
94                                 mprAddArray(&val, j, 
95                                             mprData(el->values[j].data, 
96                                                     el->values[j].length));
97                         }
98                 }
99                 mprCreateProperty(&var, el->name, &val);
100         }
101
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);
106         }
107         
108         return var;             
109 }
110
111
112 /*
113   turn an array of ldb_messages into a ejs object variable
114 */
115 static struct MprVar mprLdbArray(struct ldb_message **msg, int count, 
116                                  const char *name)
117 {
118         struct MprVar res;
119         int i;
120
121         res = mprCreateObjVar(name?name:"(NULL)", ESP_HASH_SIZE);
122         for (i=0;i<count;i++) {
123                 mprAddArray(&res, i, mprLdbMessage(msg[i]));
124         }
125         return res;     
126 }
127
128
129 /*
130   turn a MprVar string variable into a const char *
131  */
132 static const char *mprToString(const struct MprVar *v)
133 {
134         if (v->type != MPR_TYPE_STRING) return NULL;
135         return v->string;
136 }
137
138 /*
139   turn a MprVar object variable into a string list
140   this assumes the object variable consists only of strings
141 */
142 static const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v)
143 {
144         const char **list = NULL;
145         struct MprVar *el;
146
147         if (v->type != MPR_TYPE_OBJECT ||
148             v->properties == NULL) {
149                 return NULL;
150         }
151         for (el=mprGetFirstProperty(v, MPR_ENUM_DATA);
152              el;
153              el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) {
154                 const char *s = mprToString(el);
155                 if (s) {
156                         list = str_list_add(list, s);
157                 }
158         }
159         talloc_steal(mem_ctx, list);
160         return list;
161 }
162
163 /*
164   return the type of a variable
165 */
166 static int esp_typeof(struct EspRequest *ep, int argc, struct MprVar **argv)
167 {
168         const struct {
169                 MprType type;
170                 const char *name;
171         } types[] = {
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" }
183         };
184         int i;
185         const char *type = "unknown";
186
187         if (argc != 1) return -1;
188         
189         for (i=0;i<ARRAY_SIZE(types);i++) {
190                 if (argv[0]->type == types[i].type) {
191                         type = types[i].name;
192                         break;
193                 }
194         }
195
196         espSetReturnString(ep, type);
197         return 0;
198 }
199
200 /*
201   setup a return of a string list
202 */
203 static void esp_returnlist(struct EspRequest *ep, 
204                            const char *name, const char **list)
205 {
206         espSetReturn(ep, mprList(name, list));
207 }
208
209 /*
210   return a list of defined services
211 */
212 static int esp_lpServices(struct EspRequest *ep, int argc, char **argv)
213 {
214         int i;
215         const char **list = NULL;
216         if (argc != 0) return -1;
217         
218         for (i=0;i<lp_numservices();i++) {
219                 list = str_list_add(list, lp_servicename(i));
220         }
221         talloc_steal(ep, list);
222         esp_returnlist(ep, "services", list);
223         return 0;
224 }
225
226
227 /*
228   allow access to loadparm variables from inside esp scripts in swat
229   
230   can be called in 4 ways:
231
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
236
237   the returned variable is a ejs object. It is an array object for lists.  
238 */
239 static int esp_lpGet(struct EspRequest *ep, int argc, char **argv)
240 {
241         struct parm_struct *parm = NULL;
242         void *parm_ptr = NULL;
243         int i;
244
245         if (argc < 1) return -1;
246
247         if (argc == 2) {
248                 /* its a share parameter */
249                 int snum = lp_servicenumber(argv[0]);
250                 if (snum == -1) {
251                         return -1;
252                 }
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;
257                         const char *value;
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);
262                         return 0;
263                 }
264
265                 parm = lp_parm_struct(argv[1]);
266                 if (parm == NULL || parm->class == P_GLOBAL) {
267                         return -1;
268                 }
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;
274                 const char *value;
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);
279                 return 0;
280         } else {
281                 /* its a global parameter */
282                 parm = lp_parm_struct(argv[0]);
283                 if (parm == NULL) return -1;
284                 parm_ptr = parm->ptr;
285         }
286
287         if (parm == NULL || parm_ptr == NULL) {
288                 return -1;
289         }
290
291         /* construct and return the right type of ejs object */
292         switch (parm->type) {
293         case P_STRING:
294         case P_USTRING:
295                 espSetReturnString(ep, *(char **)parm_ptr);
296                 break;
297         case P_BOOL:
298                 espSetReturn(ep, mprCreateBoolVar(*(BOOL *)parm_ptr));
299                 break;
300         case P_INTEGER:
301                 espSetReturn(ep, mprCreateIntegerVar(*(int *)parm_ptr));
302                 break;
303         case P_ENUM:
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);
307                                 return 0;
308                         }
309                 }
310                 return -1;      
311         case P_LIST: 
312                 esp_returnlist(ep, parm->label, *(const char ***)parm_ptr);
313                 break;
314         case P_SEP:
315                 return -1;
316         }
317         return 0;
318 }
319
320
321 /*
322   perform an ldb search, returning an array of results
323
324   syntax:
325      ldbSearch("dbfile", "expression");
326      var attrs = new Array("attr1", "attr2", "attr3");
327      ldbSearch("dbfile", "expression", attrs);
328 */
329 static int esp_ldbSearch(struct EspRequest *ep, int argc, struct MprVar **argv)
330 {
331         const char **attrs = NULL;
332         const char *expression, *dbfile;
333         TALLOC_CTX *tmp_ctx = talloc_new(ep);
334         struct ldb_context *ldb;
335         int ret;
336         struct ldb_message **res;
337
338         /* validate arguments */
339         if (argc < 2 || argc > 3 ||
340             argv[0]->type != MPR_TYPE_STRING) {
341                 espError(ep, "ldbSearch invalid arguments");
342                 goto failed;
343         }
344         if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) {
345                 espError(ep, "ldbSearch attributes must be an object");
346                 goto failed;
347         }
348
349         dbfile     = mprToString(argv[0]);
350         expression = mprToString(argv[1]);
351         if (argc > 2) {
352                 attrs = mprToList(tmp_ctx, argv[2]);
353         }
354         if (dbfile == NULL || expression == NULL) {
355                 espError(ep, "ldbSearch invalid arguments");
356                 goto failed;
357         }
358
359         ldb = ldb_wrap_connect(tmp_ctx, dbfile, 0, NULL);
360         if (ldb == NULL) {
361                 espError(ep, "ldbSearch failed to open %s", dbfile);
362                 goto failed;
363         }
364
365         ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res);
366         if (ret == -1) {
367                 espError(ep, "ldbSearch failed - %s", ldb_errstring(ldb));
368                 goto failed;
369         }
370
371         espSetReturn(ep, mprLdbArray(res, ret, "ldb_message"));
372
373         talloc_free(tmp_ctx);
374         return 0;
375
376 failed:
377         talloc_free(tmp_ctx);
378         return -1;
379 }
380
381 /* try to authenticate the user/password pair against system auth mechanisms
382    returns 0 on success
383    returns -1 on error
384
385    fills in the session structure properly in case of success
386    NOTE: Currently only PAM Auth is supported
387 */
388
389 static int esp_unixAuth(struct EspRequest *ep, int argc, struct MprVar **argv)
390 {
391         TALLOC_CTX *tmp_ctx = talloc_new(ep);
392         const char *username;
393         const char *password;
394         struct passwd *pwd;
395         int ret;
396
397         if (argc != 2 || argv[0]->type != MPR_TYPE_STRING ||
398                         argv[1]->type != MPR_TYPE_STRING) {
399                 espError(ep, "unixAuth invalid arguments");
400                 ret = -1;
401                 goto done;
402         }
403
404         username = mprToString(argv[0]);
405         password = mprToString(argv[1]);
406
407         if (username == NULL || password == NULL) {
408                 espError(ep, "unixAuth invalid arguments");
409                 ret = -1;
410                 goto done;
411         }
412
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))) {
415
416                 pwd = getpwnam(username);
417                 if (!pwd) {
418                         espSetReturn(ep, mprCreateIntegerVar(-1));
419                         ret = -1;
420                         goto done;
421                 }
422
423                 mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
424                                         "AUTHENTICATED", mprCreateStringVar("1", 0));
425                 mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
426                                         "USERNAME", mprCreateStringVar(username, 0));
427
428                 if (pwd->pw_uid == 0) { /* we are root */
429
430                         mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
431                                         "PRIVILEGE", mprCreateStringVar("ADMIN", 0));
432                 } else {
433                         mprSetPropertyValue(&ep->variables[ESP_SESSION_OBJ],
434                                         "PRIVILEGE", mprCreateStringVar("USER", 0));
435                 }
436
437                 espSetReturn(ep, mprCreateIntegerVar(0));
438         } else {
439                 if (mprGetProperty(&ep->variables[ESP_SESSION_OBJ], "AUTHENTICATED", 0) != 0) {
440                         mprDeleteProperty(&ep->variables[ESP_SESSION_OBJ], "AUTHENTICATED");
441                 }
442                 espSetReturn(ep, mprCreateIntegerVar(-1));
443         }
444
445 done:
446         talloc_free(tmp_ctx);
447         return ret;
448 }
449
450 /*
451   setup the C functions that be called from ejs
452 */
453 void http_setup_ejs_functions(void)
454 {
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);
460 }