summaryrefslogtreecommitdiff
path: root/source4/scripting/ejs/smbcalls_samba3.c
blob: 0d54d54dcc19547f06ae64a356887bd2c7cc4999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* 
   Unix SMB/CIFS implementation.

   provide hooks into smbd C calls from ejs scripts

   Copyright (C) Jelmer Vernooij 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "scripting/ejs/smbcalls.h"
#include "lib/appweb/ejs/ejs.h"
#include "lib/samba3/samba3.h"


static struct MprVar mprRegistry(struct samba3_regdb *reg)
{
	struct MprVar mpv = mprObject("registry"), ks, vs, k, v;
	int i, j;

	ks = mprObject("array");

	for (i = 0; i < reg->key_count; i++) {
		k = mprObject("regkey");

		mprSetVar(&k, "name", mprString(reg->keys[i].name));

		vs = mprObject("array");
		
		for (j = 0; j < reg->keys[i].value_count; j++) {
			v = mprObject("regval");

			mprSetVar(&v, "name", mprString(reg->keys[i].values[j].name));
			mprSetVar(&v, "type", mprCreateIntegerVar(reg->keys[i].values[j].type));
			mprSetVar(&v, "data", mprDataBlob(reg->keys[i].values[j].data));

			mprAddArray(&vs, j, v);
		}

		mprSetVar(&k, "values", vs);

		mprAddArray(&ks, i, k);
	}

	mprSetVar(&mpv, "keys", ks);

	return mpv;
}

static struct MprVar mprPolicy(struct samba3_policy *pol)
{
	struct MprVar mpv = mprObject("policy");

	mprSetVar(&mpv, "min_password_length", mprCreateIntegerVar(pol->min_password_length));
	mprSetVar(&mpv, "password_history", mprCreateIntegerVar(pol->password_history));
	mprSetVar(&mpv, "user_must_logon_to_change_password", mprCreateIntegerVar(pol->user_must_logon_to_change_password));
	mprSetVar(&mpv, "maximum_password_age", mprCreateIntegerVar(pol->maximum_password_age));
	mprSetVar(&mpv, "minimum_password_age", mprCreateIntegerVar(pol->minimum_password_age));
	mprSetVar(&mpv, "lockout_duration", mprCreateIntegerVar(pol->lockout_duration));
	mprSetVar(&mpv, "reset_count_minutes", mprCreateIntegerVar(pol->reset_count_minutes));
	mprSetVar(&mpv, "bad_lockout_minutes", mprCreateIntegerVar(pol->bad_lockout_minutes));
	mprSetVar(&mpv, "disconnect_time", mprCreateIntegerVar(pol->disconnect_time));
	mprSetVar(&mpv, "refuse_machine_password_change", mprCreateIntegerVar(pol->refuse_machine_password_change));

	return mpv;
}

static struct MprVar mprIdmapDb(struct samba3_idmapdb *db)
{
	struct MprVar mpv = mprObject("idmapdb"), mps, mp;
	int i;

	mprSetVar(&mpv, "user_hwm", mprCreateIntegerVar(db->user_hwm));
	mprSetVar(&mpv, "group_hwm", mprCreateIntegerVar(db->group_hwm));

	mps = mprObject("array");

	for (i = 0; i < db->mapping_count; i++) {
		char *tmp;
		mp = mprObject("idmap");

		mprSetVar(&mp, "IDMAP_GROUP", mprCreateIntegerVar(IDMAP_GROUP));
		mprSetVar(&mp, "IDMAP_USER", mprCreateIntegerVar(IDMAP_USER));
		mprSetVar(&mp, "type", mprCreateIntegerVar(db->mappings[i].type));
		mprSetVar(&mp, "unix_id", mprCreateIntegerVar(db->mappings[i].unix_id));

		tmp = dom_sid_string(NULL, db->mappings[i].sid);
		mprSetVar(&mp, "sid", mprString(tmp));
		talloc_free(tmp);

		mprAddArray(&mps, i, mp);
	}

	mprSetVar(&mpv, "mappings", mps);

	return mpv;
}

static struct MprVar mprGroupMappings(struct samba3_groupdb *db)
{
	struct MprVar mpv = mprObject("array"), g;
	int i;

	for (i = 0; i < db->groupmap_count; i++) {
		char *tmp;
		g = mprObject("group");

		mprSetVar(&g, "gid", mprCreateIntegerVar(db->groupmappings[i].gid));

		tmp = dom_sid_string(NULL, db->groupmappings[i].sid);
		mprSetVar(&g, "sid", mprString(tmp));
		talloc_free(tmp);

		mprSetVar(&g, "sid_name_use", mprCreateIntegerVar(db->groupmappings[i].sid_name_use));
		mprSetVar(&g, "nt_name", mprString(db->groupmappings[i].nt_name));
		mprSetVar(&g, "comment", mprString(db->groupmappings[i].comment));

		mprAddArray(&mpv, i, g);
	}

	return mpv;
}

static struct MprVar mprAliases(struct samba3_groupdb *db)
{
	struct MprVar mpv = mprObject("array"), a, am;
	int i, j;

	for (i = 0; i < db->alias_count; i++) {
		char *tmp;
		a = mprObject("alias");

		tmp = dom_sid_string(NULL, db->aliases[i].sid);
		mprSetVar(&a, "sid", mprString(tmp));
		talloc_free(tmp);

		am = mprObject("array");

		for (j = 0; j < db->aliases[i].member_count; j++) {
			tmp = dom_sid_string(NULL, db->aliases[i].members[j]);
			mprAddArray(&am, j, mprString(tmp));
			talloc_free(tmp);
		}

		mprSetVar(&a, "members", am);
	}

	return mpv;
}

static struct MprVar mprDomainSecrets(struct samba3_domainsecrets *ds)
{
	struct MprVar v, e = mprObject("domainsecrets");
	char *tmp;
	DATA_BLOB blob;

	mprSetVar(&e, "name", mprString(ds->name));

	tmp = dom_sid_string(NULL, &ds->sid);
	mprSetVar(&e, "sid", mprString(tmp));
	talloc_free(tmp);

	tmp = GUID_string(NULL, &ds->guid);
	mprSetVar(&e, "guid", mprString(tmp));
	talloc_free(tmp);

	mprSetVar(&e, "plaintext_pw", mprString(ds->plaintext_pw));

	mprSetVar(&e, "last_change_time", mprCreateIntegerVar(ds->last_change_time));
	mprSetVar(&e, "sec_channel_type", mprCreateIntegerVar(ds->sec_channel_type));

	v = mprObject("hash_pw");

	blob.data = ds->hash_pw.hash;
	blob.length = 16;
	mprSetVar(&v, "hash", mprDataBlob(blob));

	mprSetVar(&v, "mod_time", mprCreateIntegerVar(ds->hash_pw.mod_time));

	mprSetVar(&e, "hash_pw", v);

	return e;
}

static struct MprVar mprSecrets(struct samba3_secrets *sec)
{
	struct MprVar mpv = mprObject("samba3_secrets"), es, e;
	int i;

	es = mprObject("array");

	for (i = 0; i < sec->ldappw_count; i++) {
		e = mprObject("ldappw");

		mprSetVar(&e, "dn", mprString(sec->ldappws[i].dn));
		mprSetVar(&e, "password", mprString(sec->ldappws[i].password));

		mprAddArray(&es, i, e);
	}

	mprSetVar(&mpv, "ldappws", es);

	es = mprObject("array");

	for (i = 0; i < sec->domain_count; i++) {
		mprAddArray(&es, i, mprDomainSecrets(&sec->domains[i]));
	}

	mprSetVar(&mpv, "domains", es);

	es = mprObject("trusted_domains");

	for (i = 0; i < sec->trusted_domain_count; i++) {
		struct MprVar ns;
		char *tmp;
		int j;
		e = mprObject("trusted_domain");

		ns = mprObject("array");

		for (j = 0; j < sec->trusted_domains[i].uni_name_len; j++) {
			mprAddArray(&ns, j, mprString(sec->trusted_domains[i].uni_name[j]));
		}

		mprSetVar(&e, "uni_name", ns);

		mprSetVar(&e, "pass", mprString(sec->trusted_domains[i].pass));
		mprSetVar(&e, "mod_time", mprCreateIntegerVar(sec->trusted_domains[i].mod_time));

		tmp = dom_sid_string(NULL, &sec->trusted_domains[i].domain_sid);
		mprSetVar(&e, "domains_sid", mprString(tmp));
		talloc_free(tmp);

		mprAddArray(&es, i, e);
	}

	mprSetVar(&mpv, "trusted_domains", es);
	
	es = mprObject("array");

	for (i = 0; i < sec->afs_keyfile_count; i++) {
		struct MprVar ks;
		int j;
		e = mprObject("afs_keyfile");

		mprSetVar(&e, "cell", mprString(sec->afs_keyfiles[i].cell));

		ks = mprObject("array");
		
		for (j = 0; j < 8; j++) {
			struct MprVar k = mprObject("entry");
			DATA_BLOB blob;
			
			mprSetVar(&k, "kvno", mprCreateIntegerVar(sec->afs_keyfiles[i].entry[j].kvno));
			blob.data = (uint8_t*)sec->afs_keyfiles[i].entry[j].key;
			blob.length = 8;
			mprSetVar(&k, "key", mprDataBlob(blob));

			mprAddArray(&ks, j, k);
		}

		mprSetVar(&e, "entry", ks);

		mprSetVar(&e, "nkeys", mprCreateIntegerVar(sec->afs_keyfiles[i].nkeys));

		mprAddArray(&es, i, e);
	}

	mprSetVar(&mpv, "afs_keyfiles", es);

	mprSetVar(&mpv, "ipc_cred", mprCredentials(sec->ipc_cred));

	return mpv;
}

static struct MprVar mprShares(struct samba3 *samba3)
{
	struct MprVar mpv = mprObject("array"), s;
	int i;

	for (i = 0; i < samba3->share_count; i++) {
		s = mprObject("share");

		mprSetVar(&s, "name", mprString(samba3->shares[i].name));

		/* FIXME: secdesc */

		mprAddArray(&mpv, i, s);
	}

	return mpv;
}

static struct MprVar mprSamAccounts(struct samba3 *samba3)
{
	struct MprVar mpv = mprObject("array"), m;
	int i;

	for (i = 0; i < samba3->samaccount_count; i++) {
		struct samba3_samaccount *a = &samba3->samaccounts[i];
		DATA_BLOB blob;

		m = mprObject("samba3_samaccount");

		mprSetVar(&m, "logon_time", mprCreateIntegerVar(a->logon_time));
		mprSetVar(&m, "logoff_time", mprCreateIntegerVar(a->logoff_time));
		mprSetVar(&m, "kickoff_time", mprCreateIntegerVar(a->kickoff_time));
		mprSetVar(&m, "bad_password_time", mprCreateIntegerVar(a->bad_password_time));
		mprSetVar(&m, "pass_last_set_time", mprCreateIntegerVar(a->pass_last_set_time));
		mprSetVar(&m, "pass_can_change_time", mprCreateIntegerVar(a->pass_can_change_time));
		mprSetVar(&m, "pass_must_change_time", mprCreateIntegerVar(a->pass_must_change_time));
		mprSetVar(&m, "user_rid", mprCreateIntegerVar(a->user_rid));
		mprSetVar(&m, "group_rid", mprCreateIntegerVar(a->group_rid));
		mprSetVar(&m, "acct_ctrl", mprCreateIntegerVar(a->acct_ctrl));
		mprSetVar(&m, "logon_divs", mprCreateIntegerVar(a->logon_divs));
		mprSetVar(&m, "bad_password_count", mprCreateIntegerVar(a->bad_password_count));
		mprSetVar(&m, "logon_count", mprCreateIntegerVar(a->logon_count));
		mprSetVar(&m, "username", mprString(a->username));
		mprSetVar(&m, "domain", mprString(a->domain));
		mprSetVar(&m, "nt_username", mprString(a->nt_username));
		mprSetVar(&m, "dir_drive", mprString(a->dir_drive));
		mprSetVar(&m, "munged_dial", mprString(a->munged_dial));
		mprSetVar(&m, "fullname", mprString(a->fullname));
		mprSetVar(&m, "homedir", mprString(a->homedir));
		mprSetVar(&m, "logon_script", mprString(a->logon_script));
		mprSetVar(&m, "profile_path", mprString(a->profile_path));
		mprSetVar(&m, "acct_desc", mprString(a->acct_desc));
		mprSetVar(&m, "workstations", mprString(a->workstations));
		blob.length = 16;
		blob.data = a->lm_pw.hash;
		mprSetVar(&m, "lm_pw", mprDataBlob(blob));
		blob.data = a->nt_pw.hash;
		mprSetVar(&m, "nt_pw", mprDataBlob(blob));

		mprAddArray(&mpv, i, m);
	}

	return mpv;
}

static struct MprVar mprWinsEntries(struct samba3 *samba3)
{
	struct MprVar mpv = mprObject("array");
	int i, j;

	for (i = 0; i < samba3->winsdb_count; i++) {
		struct MprVar w = mprObject("wins_entry"), ips;

		mprSetVar(&w, "name", mprString(samba3->winsdb_entries[i].name));
		mprSetVar(&w, "nb_flags", mprCreateIntegerVar(samba3->winsdb_entries[i].nb_flags));
		mprSetVar(&w, "type", mprCreateIntegerVar(samba3->winsdb_entries[i].type));
		mprSetVar(&w, "ttl", mprCreateIntegerVar(samba3->winsdb_entries[i].ttl));

		ips = mprObject("array");

		for (j = 0; j < samba3->winsdb_entries[i].ip_count; j++) {
			mprAddArray(&ips, j, mprString(iface_n_ip(i)));
		}

		mprSetVar(&w, "ips", ips);
		
		mprAddArray(&mpv, i, w);
	}

	return mpv;
}

static int ejs_find_domainsecrets(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct samba3 *samba3 = NULL;
	struct samba3_domainsecrets *sec;

	if (argc < 1) {
		ejsSetErrorMsg(eid, "find_domainsecrets invalid arguments");
		return -1;
	}

	samba3 = mprGetThisPtr(eid, "samba3");
	mprAssert(samba3);
	sec = samba3_find_domainsecrets(samba3, mprToString(argv[0]));

	if (sec == NULL) {
		mpr_Return(eid, mprCreateUndefinedVar());
	} else {
		mpr_Return(eid, mprDomainSecrets(sec));
	}

	return 0;
}

/*
  initialise samba3 ejs subsystem

  samba3 = samba3_read(libdir,smbconf)
*/
static int ejs_samba3_read(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct MprVar mpv = mprObject("samba3");
	struct samba3 *samba3;
	NTSTATUS status;

	if (argc < 2) {
		ejsSetErrorMsg(eid, "samba3_read invalid arguments");
		return -1;
	}

	status = samba3_read(mprToString(argv[0]), mprToString(argv[1]), mprMemCtx(), &samba3);

	if (NT_STATUS_IS_ERR(status)) {
		ejsSetErrorMsg(eid, "samba3_read: error");
		return -1;
	}

	mprAssert(samba3);
	
	mprSetPtrChild(&mpv, "samba3", samba3);
	mprSetVar(&mpv, "winsentries", mprWinsEntries(samba3));
	mprSetVar(&mpv, "samaccounts", mprSamAccounts(samba3));
	mprSetVar(&mpv, "shares", mprShares(samba3));
	mprSetVar(&mpv, "secrets", mprSecrets(&samba3->secrets));
	mprSetVar(&mpv, "groupmappings", mprGroupMappings(&samba3->group));
	mprSetVar(&mpv, "aliases", mprAliases(&samba3->group));
	mprSetVar(&mpv, "idmapdb", mprIdmapDb(&samba3->idmap));
	mprSetVar(&mpv, "policy", mprPolicy(&samba3->policy));
	mprSetVar(&mpv, "registry", mprRegistry(&samba3->registry));
	mprSetVar(&mpv, "configuration", mprParam(samba3->configuration));
	mprSetCFunction(&mpv, "find_domainsecrets", ejs_find_domainsecrets);

	mpr_Return(eid, mpv);
	
	return 0;
}


/*
  setup C functions that be called from ejs
*/
void smb_setup_ejs_samba3(void)
{
	ejsDefineCFunction(-1, "samba3_read", ejs_samba3_read, NULL, MPR_VAR_SCRIPT_HANDLE);
}