e6f6481060376b4166f2af42915883b616173ff3
[kai/samba.git] / source4 / scripting / ejs / smbcalls_samba3.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Jelmer Vernooij 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 "lib/samba3/samba3.h"
27
28
29 static struct MprVar mprRegistry(struct samba3_regdb *reg)
30 {
31         struct MprVar mpv = mprObject("registry"), ks, vs, k, v;
32         int i, j;
33
34         ks = mprObject("array");
35
36         for (i = 0; i < reg->key_count; i++) {
37                 k = mprObject("regkey");
38
39                 mprSetVar(&k, "name", mprString(reg->keys[i].name));
40
41                 vs = mprObject("array");
42                 
43                 for (j = 0; j < reg->keys[i].value_count; j++) {
44                         v = mprObject("regval");
45
46                         mprSetVar(&v, "name", mprString(reg->keys[i].values[j].name));
47                         mprSetVar(&v, "type", mprCreateIntegerVar(reg->keys[i].values[j].type));
48                         mprSetVar(&v, "data", mprDataBlob(reg->keys[i].values[j].data));
49
50                         mprAddArray(&vs, j, v);
51                 }
52
53                 mprSetVar(&k, "values", vs);
54
55                 mprAddArray(&ks, i, k);
56         }
57
58         mprSetVar(&mpv, "keys", ks);
59
60         return mpv;
61 }
62
63 static struct MprVar mprPolicy(struct samba3_policy *pol)
64 {
65         struct MprVar mpv = mprObject("policy");
66
67         mprSetVar(&mpv, "min_password_length", mprCreateIntegerVar(pol->min_password_length));
68         mprSetVar(&mpv, "password_history", mprCreateIntegerVar(pol->password_history));
69         mprSetVar(&mpv, "user_must_logon_to_change_password", mprCreateIntegerVar(pol->user_must_logon_to_change_password));
70         mprSetVar(&mpv, "maximum_password_age", mprCreateIntegerVar(pol->maximum_password_age));
71         mprSetVar(&mpv, "minimum_password_age", mprCreateIntegerVar(pol->minimum_password_age));
72         mprSetVar(&mpv, "lockout_duration", mprCreateIntegerVar(pol->lockout_duration));
73         mprSetVar(&mpv, "reset_count_minutes", mprCreateIntegerVar(pol->reset_count_minutes));
74         mprSetVar(&mpv, "bad_lockout_minutes", mprCreateIntegerVar(pol->bad_lockout_minutes));
75         mprSetVar(&mpv, "disconnect_time", mprCreateIntegerVar(pol->disconnect_time));
76         mprSetVar(&mpv, "refuse_machine_password_change", mprCreateIntegerVar(pol->refuse_machine_password_change));
77
78         return mpv;
79 }
80
81 static struct MprVar mprIdmapDb(struct samba3_idmapdb *db)
82 {
83         struct MprVar mpv = mprObject("idmapdb"), mps, mp;
84         int i;
85
86         mprSetVar(&mpv, "user_hwm", mprCreateIntegerVar(db->user_hwm));
87         mprSetVar(&mpv, "group_hwm", mprCreateIntegerVar(db->group_hwm));
88
89         mps = mprObject("array");
90
91         for (i = 0; i < db->mapping_count; i++) {
92                 char *tmp;
93                 mp = mprObject("idmap");
94
95                 mprSetVar(&mp, "IDMAP_GROUP", mprCreateIntegerVar(IDMAP_GROUP));
96                 mprSetVar(&mp, "IDMAP_USER", mprCreateIntegerVar(IDMAP_USER));
97                 mprSetVar(&mp, "type", mprCreateIntegerVar(db->mappings[i].type));
98                 mprSetVar(&mp, "unix_id", mprCreateIntegerVar(db->mappings[i].unix_id));
99
100                 tmp = dom_sid_string(NULL, db->mappings[i].sid);
101                 mprSetVar(&mp, "sid", mprString(tmp));
102                 talloc_free(tmp);
103
104                 mprAddArray(&mps, i, mp);
105         }
106
107         mprSetVar(&mpv, "mappings", mps);
108
109         return mpv;
110 }
111
112 static struct MprVar mprGroupMappings(struct samba3_groupdb *db)
113 {
114         struct MprVar mpv = mprObject("array"), g;
115         int i;
116
117         for (i = 0; i < db->groupmap_count; i++) {
118                 char *tmp;
119                 g = mprObject("group");
120
121                 mprSetVar(&g, "gid", mprCreateIntegerVar(db->groupmappings[i].gid));
122
123                 tmp = dom_sid_string(NULL, db->groupmappings[i].sid);
124                 mprSetVar(&g, "sid", mprString(tmp));
125                 talloc_free(tmp);
126
127                 mprSetVar(&g, "sid_name_use", mprCreateIntegerVar(db->groupmappings[i].sid_name_use));
128                 mprSetVar(&g, "nt_name", mprString(db->groupmappings[i].nt_name));
129                 mprSetVar(&g, "comment", mprString(db->groupmappings[i].comment));
130
131                 mprAddArray(&mpv, i, g);
132         }
133
134         return mpv;
135 }
136
137 static struct MprVar mprAliases(struct samba3_groupdb *db)
138 {
139         struct MprVar mpv = mprObject("array"), a, am;
140         int i, j;
141
142         for (i = 0; i < db->alias_count; i++) {
143                 char *tmp;
144                 a = mprObject("alias");
145
146                 tmp = dom_sid_string(NULL, db->aliases[i].sid);
147                 mprSetVar(&a, "sid", mprString(tmp));
148                 talloc_free(tmp);
149
150                 am = mprObject("array");
151
152                 for (j = 0; j < db->aliases[i].member_count; j++) {
153                         tmp = dom_sid_string(NULL, db->aliases[i].members[j]);
154                         mprAddArray(&am, j, mprString(tmp));
155                         talloc_free(tmp);
156                 }
157
158                 mprSetVar(&a, "members", am);
159         }
160
161         return mpv;
162 }
163
164 static struct MprVar mprDomainSecrets(struct samba3_domainsecrets *ds)
165 {
166         struct MprVar v, e = mprObject("domainsecrets");
167         char *tmp;
168
169         mprSetVar(&e, "name", mprString(ds->name));
170
171         tmp = dom_sid_string(NULL, &ds->sid);
172         mprSetVar(&e, "sid", mprString(tmp));
173         talloc_free(tmp);
174
175         tmp = GUID_string(NULL, &ds->guid);
176         mprSetVar(&e, "guid", mprString(tmp));
177         talloc_free(tmp);
178
179         mprSetVar(&e, "plaintext_pw", mprString(ds->plaintext_pw));
180
181         mprSetVar(&e, "last_change_time", mprCreateIntegerVar(ds->last_change_time));
182         mprSetVar(&e, "sec_channel_type", mprCreateIntegerVar(ds->sec_channel_type));
183
184         v = mprObject("hash_pw");
185
186         mprSetVar(&v, "hash", mprData(ds->hash_pw.hash, 16));
187
188         mprSetVar(&v, "mod_time", mprCreateIntegerVar(ds->hash_pw.mod_time));
189
190         mprSetVar(&e, "hash_pw", v);
191
192         return e;
193 }
194
195 static struct MprVar mprSecrets(struct samba3_secrets *sec)
196 {
197         struct MprVar mpv = mprObject("samba3_secrets"), es, e;
198         int i;
199
200         es = mprObject("array");
201
202         for (i = 0; i < sec->ldappw_count; i++) {
203                 e = mprObject("ldappw");
204
205                 mprSetVar(&e, "dn", mprString(sec->ldappws[i].dn));
206                 mprSetVar(&e, "password", mprString(sec->ldappws[i].password));
207
208                 mprAddArray(&es, i, e);
209         }
210
211         mprSetVar(&mpv, "ldappws", es);
212
213         es = mprObject("array");
214
215         for (i = 0; i < sec->domain_count; i++) {
216                 mprAddArray(&es, i, mprDomainSecrets(&sec->domains[i]));
217         }
218
219         mprSetVar(&mpv, "domains", es);
220
221         es = mprObject("trusted_domains");
222
223         for (i = 0; i < sec->trusted_domain_count; i++) {
224                 struct MprVar ns;
225                 char *tmp;
226                 int j;
227                 e = mprObject("trusted_domain");
228
229                 ns = mprObject("array");
230
231                 for (j = 0; j < sec->trusted_domains[i].uni_name_len; j++) {
232                         mprAddArray(&ns, j, mprString(sec->trusted_domains[i].uni_name[j]));
233                 }
234
235                 mprSetVar(&e, "uni_name", ns);
236
237                 mprSetVar(&e, "pass", mprString(sec->trusted_domains[i].pass));
238                 mprSetVar(&e, "mod_time", mprCreateIntegerVar(sec->trusted_domains[i].mod_time));
239
240                 tmp = dom_sid_string(NULL, &sec->trusted_domains[i].domain_sid);
241                 mprSetVar(&e, "domains_sid", mprString(tmp));
242                 talloc_free(tmp);
243
244                 mprAddArray(&es, i, e);
245         }
246
247         mprSetVar(&mpv, "trusted_domains", es);
248         
249         es = mprObject("array");
250
251         for (i = 0; i < sec->afs_keyfile_count; i++) {
252                 struct MprVar ks;
253                 int j;
254                 e = mprObject("afs_keyfile");
255
256                 mprSetVar(&e, "cell", mprString(sec->afs_keyfiles[i].cell));
257
258                 ks = mprObject("array");
259                 
260                 for (j = 0; j < 8; j++) {
261                         struct MprVar k = mprObject("entry");
262                         
263                         mprSetVar(&k, "kvno", mprCreateIntegerVar(sec->afs_keyfiles[i].entry[j].kvno));
264                         mprSetVar(&k, "key", mprData((uint8_t*)sec->afs_keyfiles[i].entry[j].key, 8));
265
266                         mprAddArray(&ks, j, k);
267                 }
268
269                 mprSetVar(&e, "entry", ks);
270
271                 mprSetVar(&e, "nkeys", mprCreateIntegerVar(sec->afs_keyfiles[i].nkeys));
272
273                 mprAddArray(&es, i, e);
274         }
275
276         mprSetVar(&mpv, "afs_keyfiles", es);
277
278         mprSetVar(&mpv, "ipc_cred", mprCredentials(sec->ipc_cred));
279
280         return mpv;
281 }
282
283 static struct MprVar mprShares(struct samba3 *samba3)
284 {
285         struct MprVar mpv = mprObject("array"), s, ps, p;
286         int i, j;
287
288         for (i = 0; i < samba3->share_count; i++) {
289                 s = mprObject("share");
290
291                 mprSetVar(&s, "name", mprString(samba3->shares[i].name));
292
293                 /* FIXME: secdesc */
294
295                 ps = mprObject("array");
296
297                 for (j = 0; j < samba3->shares[i].parameter_count; j++) {
298                         p = mprObject("parameter");
299
300                         mprSetVar(&p, "name", mprString(samba3->shares[i].parameters[j].name));
301                         mprSetVar(&p, "value", mprString(samba3->shares[i].parameters[j].value));
302
303                         mprAddArray(&ps, j, p);
304                 }
305
306                 mprSetVar(&s, "parameters", ps);
307         }
308
309         return mpv;
310 }
311
312 static struct MprVar mprSamAccounts(struct samba3 *samba3)
313 {
314         struct MprVar mpv = mprObject("array"), m;
315         int i;
316
317         for (i = 0; i < samba3->samaccount_count; i++) {
318                 struct samba3_samaccount *a = &samba3->samaccounts[i];
319
320                 m = mprObject("samba3_samaccount");
321
322                 mprSetVar(&m, "logon_time", mprCreateIntegerVar(a->logon_time));
323                 mprSetVar(&m, "logoff_time", mprCreateIntegerVar(a->logoff_time));
324                 mprSetVar(&m, "kickoff_time", mprCreateIntegerVar(a->kickoff_time));
325                 mprSetVar(&m, "bad_password_time", mprCreateIntegerVar(a->bad_password_time));
326                 mprSetVar(&m, "pass_last_set_time", mprCreateIntegerVar(a->pass_last_set_time));
327                 mprSetVar(&m, "pass_can_change_time", mprCreateIntegerVar(a->pass_can_change_time));
328                 mprSetVar(&m, "pass_must_change_time", mprCreateIntegerVar(a->pass_must_change_time));
329                 mprSetVar(&m, "user_rid", mprCreateIntegerVar(a->user_rid));
330                 mprSetVar(&m, "group_rid", mprCreateIntegerVar(a->group_rid));
331                 mprSetVar(&m, "acct_ctrl", mprCreateIntegerVar(a->acct_ctrl));
332                 mprSetVar(&m, "logon_divs", mprCreateIntegerVar(a->logon_divs));
333                 mprSetVar(&m, "bad_password_count", mprCreateIntegerVar(a->bad_password_count));
334                 mprSetVar(&m, "logon_count", mprCreateIntegerVar(a->logon_count));
335                 mprSetVar(&m, "username", mprString(a->username));
336                 mprSetVar(&m, "domain", mprString(a->domain));
337                 mprSetVar(&m, "nt_username", mprString(a->nt_username));
338                 mprSetVar(&m, "dir_drive", mprString(a->dir_drive));
339                 mprSetVar(&m, "munged_dial", mprString(a->munged_dial));
340                 mprSetVar(&m, "fullname", mprString(a->fullname));
341                 mprSetVar(&m, "homedir", mprString(a->homedir));
342                 mprSetVar(&m, "logon_script", mprString(a->logon_script));
343                 mprSetVar(&m, "profile_path", mprString(a->profile_path));
344                 mprSetVar(&m, "acct_desc", mprString(a->acct_desc));
345                 mprSetVar(&m, "workstations", mprString(a->workstations));
346
347                 /* FIXME: lm_pw_ptr, nt_pw_ptr */
348
349                 mprAddArray(&mpv, i, m);
350         }
351
352         return mpv;
353 }
354
355 static struct MprVar mprWinsEntries(struct samba3 *samba3)
356 {
357         struct MprVar mpv = mprObject("array");
358         int i, j;
359
360         for (i = 0; i < samba3->winsdb_count; i++) {
361                 struct MprVar w = mprObject("wins_entry"), ips;
362
363                 mprSetVar(&w, "name", mprString(samba3->winsdb_entries[i].name));
364                 mprSetVar(&w, "nb_flags", mprCreateIntegerVar(samba3->winsdb_entries[i].nb_flags));
365                 mprSetVar(&w, "type", mprCreateIntegerVar(samba3->winsdb_entries[i].type));
366                 mprSetVar(&w, "ttl", mprCreateIntegerVar(samba3->winsdb_entries[i].ttl));
367
368                 ips = mprObject("array");
369
370                 for (j = 0; j < samba3->winsdb_entries[i].ip_count; j++) {
371                         mprAddArray(&ips, j, mprString(iface_n_ip(i)));
372                 }
373
374                 mprSetVar(&w, "ips", ips);
375                 
376                 mprAddArray(&mpv, i, w);
377         }
378
379         return mpv;
380 }
381
382 static int ejs_get_param(MprVarHandle eid, int argc, struct MprVar **argv)
383 {
384         struct samba3 *samba3;
385         const char *tmp;
386
387         if (argc < 2) {
388                 ejsSetErrorMsg(eid, "get_param invalid arguments");
389                 return -1;
390         }
391
392         samba3 = mprGetThisPtr(eid, "samba3");
393         mprAssert(samba3);
394         tmp = samba3_get_param(samba3, mprToString(argv[0]), mprToString(argv[1]));
395
396         if (tmp == NULL) {
397                 mpr_Return(eid, mprCreateUndefinedVar());
398         } else {
399                 mpr_Return(eid, mprString(tmp));
400         }
401
402         return 0;
403 }
404
405 static int ejs_find_domainsecrets(MprVarHandle eid, int argc, struct MprVar **argv)
406 {
407         struct samba3 *samba3 = NULL;
408         struct samba3_domainsecrets *sec;
409
410         if (argc < 1) {
411                 ejsSetErrorMsg(eid, "find_domainsecrets invalid arguments");
412                 return -1;
413         }
414
415         samba3 = mprGetThisPtr(eid, "samba3");
416         mprAssert(samba3);
417         sec = samba3_find_domainsecrets(samba3, mprToString(argv[0]));
418
419         if (sec == NULL) {
420                 mpr_Return(eid, mprCreateUndefinedVar());
421         } else {
422                 mpr_Return(eid, mprDomainSecrets(sec));
423         }
424
425         return 0;
426 }
427
428
429
430 /*
431   initialise samba3 ejs subsystem
432 */
433 static int ejs_samba3_read(MprVarHandle eid, int argc, struct MprVar **argv)
434 {
435         struct MprVar mpv = mprObject("samba3");
436         struct samba3 *samba3;
437         NTSTATUS status;
438
439         if (argc < 2) {
440                 ejsSetErrorMsg(eid, "samba3_read invalid arguments");
441                 return -1;
442         }
443
444         status = samba3_read(mprToString(argv[0]), mprToString(argv[0]), mprMemCtx(), &samba3);
445
446         if (NT_STATUS_IS_ERR(status)) {
447                 ejsSetErrorMsg(eid, "samba3_read: error");
448                 return -1;
449         }
450
451         mprAssert(samba3);
452         
453         mprSetPtrChild(&mpv, "samba3", samba3);
454         mprSetVar(&mpv, "winsentries", mprWinsEntries(samba3));
455         mprSetVar(&mpv, "samaccounts", mprSamAccounts(samba3));
456         mprSetVar(&mpv, "shares", mprShares(samba3));
457         mprSetVar(&mpv, "secrets", mprSecrets(&samba3->secrets));
458         mprSetVar(&mpv, "groupmappings", mprGroupMappings(&samba3->group));
459         mprSetVar(&mpv, "aliases", mprAliases(&samba3->group));
460         mprSetVar(&mpv, "idmapdb", mprIdmapDb(&samba3->idmap));
461         mprSetVar(&mpv, "policy", mprPolicy(&samba3->policy));
462         mprSetVar(&mpv, "registry", mprRegistry(&samba3->registry));
463         mprSetCFunction(&mpv, "get_param", ejs_get_param);
464         mprSetCFunction(&mpv, "find_domainsecrets", ejs_find_domainsecrets);
465
466         mpr_Return(eid, mpv);
467         
468         return 0;
469 }
470
471
472 /*
473   setup C functions that be called from ejs
474 */
475 void smb_setup_ejs_samba3(void)
476 {
477         ejsDefineCFunction(-1, "samba3_read", ejs_samba3_read, NULL, MPR_VAR_SCRIPT_HANDLE);
478 }