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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/*
* XML password backend for samba
* Copyright (C) Jelmer Vernooij 2002
* Some parts based on the libxml gjobread example by Daniel Veillard
*
* 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.
*/
/* FIXME:
* - Support stdin input by using '-'
* - Be faster. Don't rewrite the whole file when adding a user, but store it in the memory and save it when exiting. Requires changes to samba source.
* - Gives the ability to read/write to standard input/output
* - Do locking!
* - Better names!
*/
#define XML_URL "http://samba.org/~jelmer/sambapdb.dtd"
#include "includes.h"
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
static int xmlsam_debug_level = DBGC_ALL;
#undef DBGC_CLASS
#define DBGC_CLASS xmlsam_debug_level
static char * iota(int a) {
static char tmp[10];
snprintf(tmp, 9, "%d", a);
return tmp;
}
static BOOL parsePass(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur, SAM_ACCOUNT * u)
{
pstring temp;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if (strcmp(cur->name, "crypt"))
DEBUG(0, ("Unknown element %s\n", cur->name));
else {
if (!strcmp(xmlGetProp(cur, "type"), "nt")
&&
pdb_gethexpwd(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1), temp))
pdb_set_nt_passwd(u, temp, PDB_SET);
else if (!strcmp(xmlGetProp(cur, "type"), "lanman")
&&
pdb_gethexpwd(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1), temp))
pdb_set_lanman_passwd(u, temp, PDB_SET);
else
DEBUG(0,
("Unknown crypt type: %s\n",
xmlGetProp(cur, "type")));
}
cur = cur->next;
}
return True;
}
static BOOL parseUser(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur, SAM_ACCOUNT * u)
{
char *tmp;
DOM_SID sid;
tmp = xmlGetProp(cur, "sid");
if (tmp){
string_to_sid(&sid, tmp);
pdb_set_user_sid(u, &sid, PDB_SET);
}
pdb_set_username(u, xmlGetProp(cur, "name"), PDB_SET);
/* We don't care what the top level element name is */
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!strcmp(cur->name, "group")) && (cur->ns == ns)) {
tmp = xmlGetProp(cur, "sid");
if (tmp){
string_to_sid(&sid, tmp);
pdb_set_group_sid(u, &sid, PDB_SET);
}
}
else if ((!strcmp(cur->name, "domain")) && (cur->ns == ns))
pdb_set_domain(u,
xmlNodeListGetString(doc, cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "fullname") && cur->ns == ns)
pdb_set_fullname(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "nt_username") && cur->ns == ns)
pdb_set_nt_username(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "logon_script") && cur->ns == ns)
pdb_set_logon_script(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "profile_path") && cur->ns == ns)
pdb_set_profile_path(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "logon_time") && cur->ns == ns)
pdb_set_logon_time(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "logoff_time") && cur->ns == ns)
pdb_set_logoff_time(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)),
PDB_SET);
else if (!strcmp(cur->name, "kickoff_time") && cur->ns == ns)
pdb_set_kickoff_time(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)),
PDB_SET);
else if (!strcmp(cur->name, "logon_divs") && cur->ns == ns)
pdb_set_logon_divs(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "hours_len") && cur->ns == ns)
pdb_set_hours_len(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "bad_password_count") && cur->ns == ns)
pdb_set_bad_password_count(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "logon_count") && cur->ns == ns)
pdb_set_logon_count(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "unknown_6") && cur->ns == ns)
pdb_set_unknown_6(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "homedir") && cur->ns == ns)
pdb_set_homedir(u,
xmlNodeListGetString(doc, cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "unknown_str") && cur->ns == ns)
pdb_set_unknown_str(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "dir_drive") && cur->ns == ns)
pdb_set_dir_drive(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "munged_dial") && cur->ns == ns)
pdb_set_munged_dial(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "acct_desc") && cur->ns == ns)
pdb_set_acct_desc(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if (!strcmp(cur->name, "acct_ctrl") && cur->ns == ns)
pdb_set_acct_ctrl(u,
atol(xmlNodeListGetString
(doc, cur->xmlChildrenNode, 1)), PDB_SET);
else if (!strcmp(cur->name, "workstations") && cur->ns == ns)
pdb_set_workstations(u,
xmlNodeListGetString(doc,
cur->xmlChildrenNode,
1), PDB_SET);
else if ((!strcmp(cur->name, "password")) && (cur->ns == ns)) {
tmp = xmlGetProp(cur, "last_set");
if (tmp)
pdb_set_pass_last_set_time(u, atol(tmp), PDB_SET);
tmp = xmlGetProp(cur, "must_change");
if (tmp)
pdb_set_pass_must_change_time(u, atol(tmp), PDB_SET);
tmp = xmlGetProp(cur, "can_change");
if (tmp)
pdb_set_pass_can_change_time(u, atol(tmp), PDB_SET);
parsePass(doc, ns, cur, u);
}
else
DEBUG(0, ("Unknown element %s\n", cur->name));
cur = cur->next;
}
return True;
}
typedef struct pdb_xml {
char *location;
char written;
xmlDocPtr doc;
xmlNodePtr users;
xmlNodePtr pwent;
xmlNsPtr ns;
} pdb_xml;
static xmlNodePtr parseSambaXMLFile(struct pdb_xml *data)
{
xmlNodePtr cur;
data->doc = xmlParseFile(data->location);
if (data->doc == NULL)
return NULL;
cur = xmlDocGetRootElement(data->doc);
if (!cur) {
DEBUG(0, ("empty document\n"));
xmlFreeDoc(data->doc);
return NULL;
}
data->ns = xmlSearchNsByHref(data->doc, cur, XML_URL);
if (!data->ns) {
DEBUG(0,
("document of the wrong type, samba user namespace not found\n"));
xmlFreeDoc(data->doc);
return NULL;
}
if (strcmp(cur->name, "samba")) {
DEBUG(0, ("document of the wrong type, root node != samba"));
xmlFreeDoc(data->doc);
return NULL;
}
cur = cur->xmlChildrenNode;
while (cur && xmlIsBlankNode(cur)) {
cur = cur->next;
}
if (!cur)
return NULL;
if ((strcmp(cur->name, "users")) || (cur->ns != data->ns)) {
DEBUG(0, ("document of the wrong type, was '%s', users expected",
cur->name));
DEBUG(0, ("xmlDocDump follows\n"));
xmlDocDump(stderr, data->doc);
DEBUG(0, ("xmlDocDump finished\n"));
xmlFreeDoc(data->doc);
return NULL;
}
data->users = cur;
cur = cur->xmlChildrenNode;
return cur;
}
static NTSTATUS xmlsam_setsampwent(struct pdb_methods *methods, BOOL update)
{
pdb_xml *data;
if (!methods) {
DEBUG(0, ("Invalid methods\n"));
return NT_STATUS_INVALID_PARAMETER;
}
data = (pdb_xml *) methods->private_data;
if (!data) {
DEBUG(0, ("Invalid pdb_xml_data\n"));
return NT_STATUS_INVALID_PARAMETER;
}
data->pwent = parseSambaXMLFile(data);
if (!data->pwent)
return NT_STATUS_UNSUCCESSFUL;
return NT_STATUS_OK;
}
/***************************************************************
End enumeration of the passwd list.
****************************************************************/
static void xmlsam_endsampwent(struct pdb_methods *methods)
{
pdb_xml *data;
if (!methods) {
DEBUG(0, ("Invalid methods\n"));
return;
}
data = (pdb_xml *) methods->private_data;
if (!data) {
DEBUG(0, ("Invalid pdb_xml_data\n"));
return;
}
xmlFreeDoc(data->doc);
data->doc = NULL;
data->pwent = NULL;
}
/*****************************************************************
Get one SAM_ACCOUNT from the list (next in line)
*****************************************************************/
static NTSTATUS xmlsam_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT * user)
{
pdb_xml *data;
if (!methods) {
DEBUG(0, ("Invalid methods\n"));
return NT_STATUS_INVALID_PARAMETER;
}
data = (pdb_xml *) methods->private_data;
if (!data) {
DEBUG(0, ("Invalid pdb_xml_data\n"));
return NT_STATUS_INVALID_PARAMETER;
}
while (data->pwent) {
if ((!strcmp(data->pwent->name, "user")) &&
(data->pwent->ns == data->ns)) {
parseUser(data->doc, data->ns, data->pwent, user);
data->pwent = data->pwent->next;
return NT_STATUS_OK;
}
data->pwent = data->pwent->next;
}
return NT_STATUS_UNSUCCESSFUL;
}
/***************************************************************************
Adds an existing SAM_ACCOUNT
****************************************************************************/
static NTSTATUS xmlsam_add_sam_account(struct pdb_methods *methods, SAM_ACCOUNT * u)
{
pstring temp;
fstring sid_str;
xmlNodePtr cur, user, pass, root;
pdb_xml *data;
DEBUG(10, ("xmlsam_add_sam_account called!\n"));
if (!methods) {
DEBUG(0, ("Invalid methods\n"));
return NT_STATUS_INVALID_PARAMETER;
}
data = (pdb_xml *) methods->private_data;
if (!data) {
DEBUG(0, ("Invalid pdb_xml_data\n"));
return NT_STATUS_INVALID_PARAMETER;
}
/* Create a new document if we can't open the current one */
if (!parseSambaXMLFile(data)) {
DEBUG(0, ("Can't load current XML file, creating a new one\n"));
data->doc = xmlNewDoc(XML_DEFAULT_VERSION);
root = xmlNewDocNode(data->doc, NULL, "samba", NULL);
cur = xmlDocSetRootElement(data->doc, root);
data->ns = xmlNewNs(root, XML_URL, "samba");
data->users = xmlNewChild(root, data->ns, "users", NULL);
}
user = xmlNewChild(data->users, data->ns, "user", NULL);
xmlNewProp(user, "sid",
sid_to_string(sid_str, pdb_get_user_sid(u)));
if (pdb_get_username(u) && strcmp(pdb_get_username(u), ""))
xmlNewProp(user, "name", pdb_get_username(u));
cur = xmlNewChild(user, data->ns, "group", NULL);
xmlNewProp(cur, "sid",
sid_to_string(sid_str, pdb_get_group_sid(u)));
if (pdb_get_init_flags(u, PDB_LOGONTIME) != PDB_DEFAULT)
xmlNewChild(user, data->ns, "logon_time",
iota(pdb_get_logon_time(u)));
if (pdb_get_init_flags(u, PDB_LOGOFFTIME) != PDB_DEFAULT)
xmlNewChild(user, data->ns, "logoff_time",
iota(pdb_get_logoff_time(u)));
if (pdb_get_init_flags(u, PDB_KICKOFFTIME) != PDB_DEFAULT)
xmlNewChild(user, data->ns, "kickoff_time",
iota(pdb_get_kickoff_time(u)));
if (pdb_get_domain(u) && strcmp(pdb_get_domain(u), ""))
xmlNewChild(user, data->ns, "domain", pdb_get_domain(u));
if (pdb_get_nt_username(u) && strcmp(pdb_get_nt_username(u), ""))
xmlNewChild(user, data->ns, "nt_username", pdb_get_nt_username(u));
if (pdb_get_fullname(u) && strcmp(pdb_get_fullname(u), ""))
xmlNewChild(user, data->ns, "fullname", pdb_get_fullname(u));
if (pdb_get_homedir(u) && strcmp(pdb_get_homedir(u), ""))
xmlNewChild(user, data->ns, "homedir", pdb_get_homedir(u));
if (pdb_get_dir_drive(u) && strcmp(pdb_get_dir_drive(u), ""))
xmlNewChild(user, data->ns, "dir_drive", pdb_get_dir_drive(u));
if (pdb_get_logon_script(u) && strcmp(pdb_get_logon_script(u), ""))
xmlNewChild(user, data->ns, "logon_script",
pdb_get_logon_script(u));
if (pdb_get_profile_path(u) && strcmp(pdb_get_profile_path(u), ""))
xmlNewChild(user, data->ns, "profile_path",
pdb_get_profile_path(u));
if (pdb_get_acct_desc(u) && strcmp(pdb_get_acct_desc(u), ""))
xmlNewChild(user, data->ns, "acct_desc", pdb_get_acct_desc(u));
if (pdb_get_workstations(u) && strcmp(pdb_get_workstations(u), ""))
xmlNewChild(user, data->ns, "workstations",
pdb_get_workstations(u));
if (pdb_get_unknown_str(u) && strcmp(pdb_get_unknown_str(u), ""))
xmlNewChild(user, data->ns, "unknown_str", pdb_get_unknown_str(u));
if (pdb_get_munged_dial(u) && strcmp(pdb_get_munged_dial(u), ""))
xmlNewChild(user, data->ns, "munged_dial", pdb_get_munged_dial(u));
/* Password stuff */
pass = xmlNewChild(user, data->ns, "password", NULL);
if (pdb_get_pass_last_set_time(u))
xmlNewProp(pass, "last_set", iota(pdb_get_pass_last_set_time(u)));
if (pdb_get_init_flags(u, PDB_CANCHANGETIME) != PDB_DEFAULT)
xmlNewProp(pass, "can_change",
iota(pdb_get_pass_can_change_time(u)));
if (pdb_get_init_flags(u, PDB_MUSTCHANGETIME) != PDB_DEFAULT)
xmlNewProp(pass, "must_change",
iota(pdb_get_pass_must_change_time(u)));
if (pdb_get_lanman_passwd(u)) {
pdb_sethexpwd(temp, pdb_get_lanman_passwd(u),
pdb_get_acct_ctrl(u));
cur = xmlNewChild(pass, data->ns, "crypt", temp);
xmlNewProp(cur, "type", "lanman");
}
if (pdb_get_nt_passwd(u)) {
pdb_sethexpwd(temp, pdb_get_nt_passwd(u), pdb_get_acct_ctrl(u));
cur = xmlNewChild(pass, data->ns, "crypt", temp);
xmlNewProp(cur, "type", "nt");
}
xmlNewChild(user, data->ns, "acct_ctrl", iota(pdb_get_acct_ctrl(u)));
if (pdb_get_logon_divs(u))
xmlNewChild(user, data->ns, "logon_divs",
iota(pdb_get_logon_divs(u)));
if (pdb_get_hours_len(u))
xmlNewChild(user, data->ns, "hours_len",
iota(pdb_get_hours_len(u)));
xmlNewChild(user, data->ns, "bad_password_count", iota(pdb_get_bad_password_count(u)));
xmlNewChild(user, data->ns, "logon_count", iota(pdb_get_logon_count(u)));
xmlNewChild(user, data->ns, "unknown_6", iota(pdb_get_unknown_6(u)));
xmlSaveFile(data->location, data->doc);
return NT_STATUS_OK;
}
static NTSTATUS xmlsam_init(PDB_CONTEXT * pdb_context, PDB_METHODS ** pdb_method,
const char *location)
{
NTSTATUS nt_status;
pdb_xml *data;
xmlsam_debug_level = debug_add_class("xmlsam");
if (xmlsam_debug_level == -1) {
xmlsam_debug_level = DBGC_ALL;
DEBUG(0, ("xmlsam: Couldn't register custom debugging class!\n"));
}
if (!pdb_context) {
DEBUG(0, ("invalid pdb_methods specified\n"));
return NT_STATUS_UNSUCCESSFUL;
}
if (!NT_STATUS_IS_OK
(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
return nt_status;
}
(*pdb_method)->name = "xmlsam";
(*pdb_method)->setsampwent = xmlsam_setsampwent;
(*pdb_method)->endsampwent = xmlsam_endsampwent;
(*pdb_method)->getsampwent = xmlsam_getsampwent;
(*pdb_method)->add_sam_account = xmlsam_add_sam_account;
(*pdb_method)->getsampwnam = NULL;
(*pdb_method)->getsampwsid = NULL;
(*pdb_method)->update_sam_account = NULL;
(*pdb_method)->delete_sam_account = NULL;
(*pdb_method)->get_group_info_by_sid = NULL;
(*pdb_method)->get_group_list = NULL;
(*pdb_method)->get_group_sids = NULL;
(*pdb_method)->add_group = NULL;
(*pdb_method)->update_group = NULL;
(*pdb_method)->delete_group = NULL;
(*pdb_method)->add_sid_to_group = NULL;
(*pdb_method)->remove_sid_from_group = NULL;
(*pdb_method)->get_group_info_by_name = NULL;
(*pdb_method)->get_group_info_by_nt_name = NULL;
(*pdb_method)->get_group_uids = NULL;
data = talloc(pdb_context->mem_ctx, sizeof(pdb_xml));
data->location = talloc_strdup(pdb_context->mem_ctx, (location ? location : "passdb.xml"));
data->pwent = NULL;
data->written = 0;
(*pdb_method)->private_data = data;
LIBXML_TEST_VERSION xmlKeepBlanksDefault(0);
return NT_STATUS_OK;
}
NTSTATUS pdb_xml_init(void)
{
return smb_register_passdb(PASSDB_INTERFACE_VERSION, "xml", xmlsam_init);
}
|