66e1299566eed030616294dd73ac8e0efc8ca928
[sfrench/samba-autobuild/.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         for (i = 0; i < sec->domain_count; i++) {
214                 mprAddArray(&es, i, mprDomainSecrets(&sec->domains[i]));
215         }
216
217         mprSetVar(&mpv, "domains", es);
218
219         es = mprObject("trusted_domains");
220
221         for (i = 0; i < sec->trusted_domain_count; i++) {
222                 struct MprVar ns;
223                 char *tmp;
224                 int j;
225                 e = mprObject("trusted_domain");
226
227                 ns = mprObject("array");
228
229                 for (j = 0; j < sec->trusted_domains[i].uni_name_len; j++) {
230                         mprAddArray(&ns, j, mprString(sec->trusted_domains[i].uni_name[j]));
231                 }
232
233                 mprSetVar(&e, "uni_name", ns);
234
235                 mprSetVar(&e, "pass", mprString(sec->trusted_domains[i].pass));
236                 mprSetVar(&e, "mod_time", mprCreateIntegerVar(sec->trusted_domains[i].mod_time));
237
238                 tmp = dom_sid_string(NULL, &sec->trusted_domains[i].domain_sid);
239                 mprSetVar(&e, "domains_sid", mprString(tmp));
240                 talloc_free(tmp);
241
242                 mprAddArray(&es, i, e);
243         }
244
245         mprSetVar(&mpv, "trusted_domains", es);
246         
247         es = mprObject("array");
248
249         for (i = 0; i < sec->afs_keyfile_count; i++) {
250                 struct MprVar ks;
251                 int j;
252                 e = mprObject("afs_keyfile");
253
254                 mprSetVar(&e, "cell", mprString(sec->afs_keyfiles[i].cell));
255
256                 ks = mprObject("array");
257                 
258                 for (j = 0; j < 8; j++) {
259                         struct MprVar k = mprObject("entry");
260                         
261                         mprSetVar(&k, "kvno", mprCreateIntegerVar(sec->afs_keyfiles[i].entry[j].kvno));
262                         mprSetVar(&k, "key", mprData((uint8_t*)sec->afs_keyfiles[i].entry[j].key, 8));
263
264                         mprAddArray(&ks, j, k);
265                 }
266
267                 mprSetVar(&e, "entry", ks);
268
269                 mprSetVar(&e, "nkeys", mprCreateIntegerVar(sec->afs_keyfiles[i].nkeys));
270
271                 mprAddArray(&es, i, e);
272         }
273
274         mprSetVar(&mpv, "afs_keyfiles", es);
275
276         mprSetVar(&mpv, "ipc_cred", mprCredentials(sec->ipc_cred));
277
278         return mpv;
279 }
280
281 static struct MprVar mprShares(struct samba3 *samba3)
282 {
283         struct MprVar mpv = mprObject("array"), s, ps, p;
284         int i, j;
285
286         for (i = 0; i < samba3->share_count; i++) {
287                 s = mprObject("share");
288
289                 mprSetVar(&s, "name", mprString(samba3->shares[i].name));
290
291                 /* FIXME: secdesc */
292
293                 ps = mprObject("array");
294
295                 for (j = 0; j < samba3->shares[i].parameter_count; j++) {
296                         p = mprObject("parameter");
297
298                         mprSetVar(&p, "name", mprString(samba3->shares[i].parameters[j].name));
299                         mprSetVar(&p, "value", mprString(samba3->shares[i].parameters[j].value));
300
301                         mprAddArray(&ps, j, p);
302                 }
303
304                 mprSetVar(&s, "parameters", ps);
305         }
306
307         return mpv;
308 }
309
310 static struct MprVar mprSamAccounts(struct samba3 *samba3)
311 {
312         struct MprVar mpv = mprObject("array"), m;
313         int i;
314
315         for (i = 0; i < samba3->samaccount_count; i++) {
316                 struct samba3_samaccount *a = &samba3->samaccounts[i];
317
318                 m = mprObject("samba3_samaccount");
319
320                 mprSetVar(&m, "logon_time", mprCreateIntegerVar(a->logon_time));
321                 mprSetVar(&m, "logoff_time", mprCreateIntegerVar(a->logoff_time));
322                 mprSetVar(&m, "kickoff_time", mprCreateIntegerVar(a->kickoff_time));
323                 mprSetVar(&m, "bad_password_time", mprCreateIntegerVar(a->bad_password_time));
324                 mprSetVar(&m, "pass_last_set_time", mprCreateIntegerVar(a->pass_last_set_time));
325                 mprSetVar(&m, "pass_can_change_time", mprCreateIntegerVar(a->pass_can_change_time));
326                 mprSetVar(&m, "pass_must_change_time", mprCreateIntegerVar(a->pass_must_change_time));
327                 mprSetVar(&m, "user_rid", mprCreateIntegerVar(a->user_rid));
328                 mprSetVar(&m, "group_rid", mprCreateIntegerVar(a->group_rid));
329                 mprSetVar(&m, "acct_ctrl", mprCreateIntegerVar(a->acct_ctrl));
330                 mprSetVar(&m, "logon_divs", mprCreateIntegerVar(a->logon_divs));
331                 mprSetVar(&m, "bad_password_count", mprCreateIntegerVar(a->bad_password_count));
332                 mprSetVar(&m, "logon_count", mprCreateIntegerVar(a->logon_count));
333                 mprSetVar(&m, "username", mprString(a->username));
334                 mprSetVar(&m, "domain", mprString(a->domain));
335                 mprSetVar(&m, "nt_username", mprString(a->nt_username));
336                 mprSetVar(&m, "dir_drive", mprString(a->dir_drive));
337                 mprSetVar(&m, "munged_dial", mprString(a->munged_dial));
338                 mprSetVar(&m, "fullname", mprString(a->fullname));
339                 mprSetVar(&m, "homedir", mprString(a->homedir));
340                 mprSetVar(&m, "logon_script", mprString(a->logon_script));
341                 mprSetVar(&m, "profile_path", mprString(a->profile_path));
342                 mprSetVar(&m, "acct_desc", mprString(a->acct_desc));
343                 mprSetVar(&m, "workstations", mprString(a->workstations));
344
345                 /* FIXME: lm_pw_ptr, nt_pw_ptr */
346
347                 mprAddArray(&mpv, i, m);
348         }
349
350         return mpv;
351 }
352
353 static struct MprVar mprWinsEntries(struct samba3 *samba3)
354 {
355         struct MprVar mpv = mprObject("array");
356         int i, j;
357
358         for (i = 0; i < samba3->winsdb_count; i++) {
359                 struct MprVar w = mprObject("wins_entry"), ips;
360
361                 mprSetVar(&w, "name", mprString(samba3->winsdb_entries[i].name));
362                 mprSetVar(&w, "nb_flags", mprCreateIntegerVar(samba3->winsdb_entries[i].nb_flags));
363                 mprSetVar(&w, "type", mprCreateIntegerVar(samba3->winsdb_entries[i].type));
364                 mprSetVar(&w, "ttl", mprCreateIntegerVar(samba3->winsdb_entries[i].ttl));
365
366                 ips = mprObject("array");
367
368                 for (j = 0; j < samba3->winsdb_entries[i].ip_count; j++) {
369                         mprAddArray(&ips, j, mprString(iface_n_ip(i)));
370                 }
371
372                 mprSetVar(&w, "ips", ips);
373                 
374                 mprAddArray(&mpv, i, w);
375         }
376
377         return mpv;
378 }
379
380 static int ejs_get_param(MprVarHandle eid, int argc, struct MprVar **argv)
381 {
382         struct samba3 *samba3;
383         const char *tmp;
384
385         if (argc < 2) {
386                 ejsSetErrorMsg(eid, "get_param invalid arguments");
387                 return -1;
388         }
389
390         samba3 = mprGetThisPtr(eid, "samba3");
391         mprAssert(samba3);
392         tmp = samba3_get_param(samba3, mprToString(argv[0]), mprToString(argv[1]));
393
394         if (tmp == NULL) {
395                 mpr_Return(eid, mprCreateUndefinedVar());
396         } else {
397                 mpr_Return(eid, mprString(tmp));
398         }
399
400         return 0;
401 }
402
403 static int ejs_find_domainsecrets(MprVarHandle eid, int argc, struct MprVar **argv)
404 {
405         struct samba3 *samba3 = NULL;
406         struct samba3_domainsecrets *sec;
407
408         if (argc < 1) {
409                 ejsSetErrorMsg(eid, "find_domainsecrets invalid arguments");
410                 return -1;
411         }
412
413         samba3 = mprGetThisPtr(eid, "samba3");
414         mprAssert(samba3);
415         sec = samba3_find_domainsecrets(samba3, mprToString(argv[0]));
416
417         if (sec == NULL) {
418                 mpr_Return(eid, mprCreateUndefinedVar());
419         } else {
420                 mpr_Return(eid, mprDomainSecrets(sec));
421         }
422
423         return 0;
424 }
425
426
427
428 /*
429   initialise samba3 ejs subsystem
430 */
431 static int ejs_samba3_read(MprVarHandle eid, int argc, struct MprVar **argv)
432 {
433         struct MprVar mpv = mprObject("samba3");
434         struct samba3 *samba3;
435         NTSTATUS status;
436
437         if (argc < 2) {
438                 ejsSetErrorMsg(eid, "samba3_read invalid arguments");
439                 return -1;
440         }
441
442         status = samba3_read(mprToString(argv[0]), mprToString(argv[0]), mprMemCtx(), &samba3);
443
444         if (NT_STATUS_IS_ERR(status)) {
445                 ejsSetErrorMsg(eid, "samba3_read: error");
446                 return -1;
447         }
448
449         mprAssert(samba3);
450         
451         mprSetThisPtr(eid, "samba3", samba3);
452         mprSetVar(&mpv, "winsentries", mprWinsEntries(samba3));
453         mprSetVar(&mpv, "samaccounts", mprSamAccounts(samba3));
454         mprSetVar(&mpv, "shares", mprShares(samba3));
455         mprSetVar(&mpv, "secrets", mprSecrets(&samba3->secrets));
456         mprSetVar(&mpv, "groupmappings", mprGroupMappings(&samba3->group));
457         mprSetVar(&mpv, "aliases", mprAliases(&samba3->group));
458         mprSetVar(&mpv, "idmapdb", mprIdmapDb(&samba3->idmap));
459         mprSetVar(&mpv, "policy", mprPolicy(&samba3->policy));
460         mprSetVar(&mpv, "registry", mprRegistry(&samba3->registry));
461         mprSetCFunction(&mpv, "get_param", ejs_get_param);
462         mprSetCFunction(&mpv, "find_domainsecrets", ejs_find_domainsecrets);
463
464         mpr_Return(eid, mpv);
465         
466         return 0;
467 }
468
469
470 /*
471   setup C functions that be called from ejs
472 */
473 void smb_setup_ejs_samba3(void)
474 {
475         ejsDefineCFunction(-1, "samba3_read", ejs_samba3_read, NULL, MPR_VAR_SCRIPT_HANDLE);
476 }