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