3 * XML password backend for samba
4 * Copyright (C) Jelmer Vernooij 2002
5 * Some parts based on the libxml gjobread example by Daniel Veillard
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 675
19 * Mass Ave, Cambridge, MA 02139, USA.
23 * - Support stdin input by using '-'
24 * - 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.
25 * - Gives the ability to read/write to standard input/output
31 #define XML_URL "http://samba.org/~jelmer/sambapdb.dtd"
35 #include <libxml/xmlmemory.h>
36 #include <libxml/parser.h>
38 static int xmlsam_debug_level
= DBGC_ALL
;
41 #define DBGC_CLASS xmlsam_debug_level
43 static char * iota(int a
) {
46 snprintf(tmp
, 9, "%d", a
);
50 static BOOL
parsePass(xmlDocPtr doc
, xmlNsPtr ns
, xmlNodePtr cur
, SAM_ACCOUNT
* u
)
54 cur
= cur
->xmlChildrenNode
;
56 if (strcmp(cur
->name
, "crypt"))
57 DEBUG(0, ("Unknown element %s\n", cur
->name
));
59 if (!strcmp(xmlGetProp(cur
, "type"), "nt")
61 pdb_gethexpwd(xmlNodeListGetString
62 (doc
, cur
->xmlChildrenNode
, 1), temp
))
63 pdb_set_nt_passwd(u
, temp
, PDB_SET
);
64 else if (!strcmp(xmlGetProp(cur
, "type"), "lanman")
66 pdb_gethexpwd(xmlNodeListGetString
67 (doc
, cur
->xmlChildrenNode
, 1), temp
))
68 pdb_set_lanman_passwd(u
, temp
, PDB_SET
);
71 ("Unknown crypt type: %s\n",
72 xmlGetProp(cur
, "type")));
79 static BOOL
parseUser(xmlDocPtr doc
, xmlNsPtr ns
, xmlNodePtr cur
, SAM_ACCOUNT
* u
)
84 tmp
= xmlGetProp(cur
, "sid");
86 string_to_sid(&sid
, tmp
);
87 pdb_set_user_sid(u
, &sid
, PDB_SET
);
89 pdb_set_username(u
, xmlGetProp(cur
, "name"), PDB_SET
);
90 /* We don't care what the top level element name is */
91 cur
= cur
->xmlChildrenNode
;
93 if ((!strcmp(cur
->name
, "group")) && (cur
->ns
== ns
)) {
94 tmp
= xmlGetProp(cur
, "sid");
96 string_to_sid(&sid
, tmp
);
97 pdb_set_group_sid(u
, &sid
, PDB_SET
);
101 else if ((!strcmp(cur
->name
, "domain")) && (cur
->ns
== ns
))
103 xmlNodeListGetString(doc
, cur
->xmlChildrenNode
,
106 else if (!strcmp(cur
->name
, "fullname") && cur
->ns
== ns
)
108 xmlNodeListGetString(doc
,
109 cur
->xmlChildrenNode
,
112 else if (!strcmp(cur
->name
, "nt_username") && cur
->ns
== ns
)
113 pdb_set_nt_username(u
,
114 xmlNodeListGetString(doc
,
115 cur
->xmlChildrenNode
,
118 else if (!strcmp(cur
->name
, "logon_script") && cur
->ns
== ns
)
119 pdb_set_logon_script(u
,
120 xmlNodeListGetString(doc
,
121 cur
->xmlChildrenNode
,
124 else if (!strcmp(cur
->name
, "profile_path") && cur
->ns
== ns
)
125 pdb_set_profile_path(u
,
126 xmlNodeListGetString(doc
,
127 cur
->xmlChildrenNode
,
130 else if (!strcmp(cur
->name
, "logon_time") && cur
->ns
== ns
)
131 pdb_set_logon_time(u
,
132 atol(xmlNodeListGetString
133 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
135 else if (!strcmp(cur
->name
, "logoff_time") && cur
->ns
== ns
)
136 pdb_set_logoff_time(u
,
137 atol(xmlNodeListGetString
138 (doc
, cur
->xmlChildrenNode
, 1)),
141 else if (!strcmp(cur
->name
, "kickoff_time") && cur
->ns
== ns
)
142 pdb_set_kickoff_time(u
,
143 atol(xmlNodeListGetString
144 (doc
, cur
->xmlChildrenNode
, 1)),
147 else if (!strcmp(cur
->name
, "logon_divs") && cur
->ns
== ns
)
148 pdb_set_logon_divs(u
,
149 atol(xmlNodeListGetString
150 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
152 else if (!strcmp(cur
->name
, "hours_len") && cur
->ns
== ns
)
154 atol(xmlNodeListGetString
155 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
157 else if (!strcmp(cur
->name
, "unknown_3") && cur
->ns
== ns
)
159 atol(xmlNodeListGetString
160 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
162 else if (!strcmp(cur
->name
, "unknown_5") && cur
->ns
== ns
)
164 atol(xmlNodeListGetString
165 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
167 else if (!strcmp(cur
->name
, "unknown_6") && cur
->ns
== ns
)
169 atol(xmlNodeListGetString
170 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
172 else if (!strcmp(cur
->name
, "homedir") && cur
->ns
== ns
)
174 xmlNodeListGetString(doc
, cur
->xmlChildrenNode
,
177 else if (!strcmp(cur
->name
, "unknown_str") && cur
->ns
== ns
)
178 pdb_set_unknown_str(u
,
179 xmlNodeListGetString(doc
,
180 cur
->xmlChildrenNode
,
183 else if (!strcmp(cur
->name
, "dir_drive") && cur
->ns
== ns
)
185 xmlNodeListGetString(doc
,
186 cur
->xmlChildrenNode
,
189 else if (!strcmp(cur
->name
, "munged_dial") && cur
->ns
== ns
)
190 pdb_set_munged_dial(u
,
191 xmlNodeListGetString(doc
,
192 cur
->xmlChildrenNode
,
195 else if (!strcmp(cur
->name
, "acct_desc") && cur
->ns
== ns
)
197 xmlNodeListGetString(doc
,
198 cur
->xmlChildrenNode
,
201 else if (!strcmp(cur
->name
, "acct_ctrl") && cur
->ns
== ns
)
203 atol(xmlNodeListGetString
204 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
206 else if (!strcmp(cur
->name
, "workstations") && cur
->ns
== ns
)
207 pdb_set_workstations(u
,
208 xmlNodeListGetString(doc
,
209 cur
->xmlChildrenNode
,
212 else if ((!strcmp(cur
->name
, "password")) && (cur
->ns
== ns
)) {
213 tmp
= xmlGetProp(cur
, "last_set");
215 pdb_set_pass_last_set_time(u
, atol(tmp
), PDB_SET
);
216 tmp
= xmlGetProp(cur
, "must_change");
218 pdb_set_pass_must_change_time(u
, atol(tmp
), PDB_SET
);
219 tmp
= xmlGetProp(cur
, "can_change");
221 pdb_set_pass_can_change_time(u
, atol(tmp
), PDB_SET
);
222 parsePass(doc
, ns
, cur
, u
);
226 DEBUG(0, ("Unknown element %s\n", cur
->name
));
233 typedef struct pdb_xml
{
242 static xmlNodePtr
parseSambaXMLFile(struct pdb_xml
*data
)
246 data
->doc
= xmlParseFile(data
->location
);
247 if (data
->doc
== NULL
)
250 cur
= xmlDocGetRootElement(data
->doc
);
252 DEBUG(0, ("empty document\n"));
253 xmlFreeDoc(data
->doc
);
256 data
->ns
= xmlSearchNsByHref(data
->doc
, cur
, XML_URL
);
259 ("document of the wrong type, samba user namespace not found\n"));
260 xmlFreeDoc(data
->doc
);
263 if (strcmp(cur
->name
, "samba")) {
264 DEBUG(0, ("document of the wrong type, root node != samba"));
265 xmlFreeDoc(data
->doc
);
269 cur
= cur
->xmlChildrenNode
;
270 while (cur
&& xmlIsBlankNode(cur
)) {
275 if ((strcmp(cur
->name
, "users")) || (cur
->ns
!= data
->ns
)) {
276 DEBUG(0, ("document of the wrong type, was '%s', users expected",
278 DEBUG(0, ("xmlDocDump follows\n"));
279 xmlDocDump(stderr
, data
->doc
);
280 DEBUG(0, ("xmlDocDump finished\n"));
281 xmlFreeDoc(data
->doc
);
285 cur
= cur
->xmlChildrenNode
;
289 static NTSTATUS
xmlsam_setsampwent(struct pdb_methods
*methods
, BOOL update
)
294 DEBUG(0, ("Invalid methods\n"));
295 return NT_STATUS_INVALID_PARAMETER
;
297 data
= (pdb_xml
*) methods
->private_data
;
299 DEBUG(0, ("Invalid pdb_xml_data\n"));
300 return NT_STATUS_INVALID_PARAMETER
;
302 data
->pwent
= parseSambaXMLFile(data
);
304 return NT_STATUS_UNSUCCESSFUL
;
309 /***************************************************************
310 End enumeration of the passwd list.
311 ****************************************************************/
313 static void xmlsam_endsampwent(struct pdb_methods
*methods
)
318 DEBUG(0, ("Invalid methods\n"));
322 data
= (pdb_xml
*) methods
->private_data
;
325 DEBUG(0, ("Invalid pdb_xml_data\n"));
329 xmlFreeDoc(data
->doc
);
334 /*****************************************************************
335 Get one SAM_ACCOUNT from the list (next in line)
336 *****************************************************************/
338 static NTSTATUS
xmlsam_getsampwent(struct pdb_methods
*methods
, SAM_ACCOUNT
* user
)
343 DEBUG(0, ("Invalid methods\n"));
344 return NT_STATUS_INVALID_PARAMETER
;
346 data
= (pdb_xml
*) methods
->private_data
;
349 DEBUG(0, ("Invalid pdb_xml_data\n"));
350 return NT_STATUS_INVALID_PARAMETER
;
353 while (data
->pwent
) {
354 if ((!strcmp(data
->pwent
->name
, "user")) &&
355 (data
->pwent
->ns
== data
->ns
)) {
357 parseUser(data
->doc
, data
->ns
, data
->pwent
, user
);
358 data
->pwent
= data
->pwent
->next
;
361 data
->pwent
= data
->pwent
->next
;
363 return NT_STATUS_UNSUCCESSFUL
;
366 /***************************************************************************
367 Adds an existing SAM_ACCOUNT
368 ****************************************************************************/
370 static NTSTATUS
xmlsam_add_sam_account(struct pdb_methods
*methods
, SAM_ACCOUNT
* u
)
374 xmlNodePtr cur
, user
, pass
, root
;
377 DEBUG(10, ("xmlsam_add_sam_account called!\n"));
380 DEBUG(0, ("Invalid methods\n"));
381 return NT_STATUS_INVALID_PARAMETER
;
384 data
= (pdb_xml
*) methods
->private_data
;
386 DEBUG(0, ("Invalid pdb_xml_data\n"));
387 return NT_STATUS_INVALID_PARAMETER
;
390 /* Create a new document if we can't open the current one */
391 if (!parseSambaXMLFile(data
)) {
392 DEBUG(0, ("Can't load current XML file, creating a new one\n"));
393 data
->doc
= xmlNewDoc(XML_DEFAULT_VERSION
);
394 root
= xmlNewDocNode(data
->doc
, NULL
, "samba", NULL
);
395 cur
= xmlDocSetRootElement(data
->doc
, root
);
396 data
->ns
= xmlNewNs(root
, XML_URL
, "samba");
397 data
->users
= xmlNewChild(root
, data
->ns
, "users", NULL
);
400 user
= xmlNewChild(data
->users
, data
->ns
, "user", NULL
);
401 xmlNewProp(user
, "sid",
402 sid_to_string(sid_str
, pdb_get_user_sid(u
)));
404 if (pdb_get_username(u
) && strcmp(pdb_get_username(u
), ""))
405 xmlNewProp(user
, "name", pdb_get_username(u
));
407 cur
= xmlNewChild(user
, data
->ns
, "group", NULL
);
409 xmlNewProp(cur
, "sid",
410 sid_to_string(sid_str
, pdb_get_group_sid(u
)));
412 if (pdb_get_init_flags(u
, PDB_LOGONTIME
) != PDB_DEFAULT
)
413 xmlNewChild(user
, data
->ns
, "login_time",
414 iota(pdb_get_logon_time(u
)));
416 if (pdb_get_init_flags(u
, PDB_LOGOFFTIME
) != PDB_DEFAULT
)
417 xmlNewChild(user
, data
->ns
, "logoff_time",
418 iota(pdb_get_logoff_time(u
)));
420 if (pdb_get_init_flags(u
, PDB_KICKOFFTIME
) != PDB_DEFAULT
)
421 xmlNewChild(user
, data
->ns
, "kickoff_time",
422 iota(pdb_get_kickoff_time(u
)));
424 if (pdb_get_domain(u
) && strcmp(pdb_get_domain(u
), ""))
425 xmlNewChild(user
, data
->ns
, "domain", pdb_get_domain(u
));
427 if (pdb_get_nt_username(u
) && strcmp(pdb_get_nt_username(u
), ""))
428 xmlNewChild(user
, data
->ns
, "nt_username", pdb_get_nt_username(u
));
430 if (pdb_get_fullname(u
) && strcmp(pdb_get_fullname(u
), ""))
431 xmlNewChild(user
, data
->ns
, "fullname", pdb_get_fullname(u
));
433 if (pdb_get_homedir(u
) && strcmp(pdb_get_homedir(u
), ""))
434 xmlNewChild(user
, data
->ns
, "homedir", pdb_get_homedir(u
));
436 if (pdb_get_dir_drive(u
) && strcmp(pdb_get_dir_drive(u
), ""))
437 xmlNewChild(user
, data
->ns
, "dir_drive", pdb_get_dir_drive(u
));
439 if (pdb_get_logon_script(u
) && strcmp(pdb_get_logon_script(u
), ""))
440 xmlNewChild(user
, data
->ns
, "logon_script",
441 pdb_get_logon_script(u
));
443 if (pdb_get_profile_path(u
) && strcmp(pdb_get_profile_path(u
), ""))
444 xmlNewChild(user
, data
->ns
, "profile_path",
445 pdb_get_profile_path(u
));
447 if (pdb_get_acct_desc(u
) && strcmp(pdb_get_acct_desc(u
), ""))
448 xmlNewChild(user
, data
->ns
, "acct_desc", pdb_get_acct_desc(u
));
450 if (pdb_get_workstations(u
) && strcmp(pdb_get_workstations(u
), ""))
451 xmlNewChild(user
, data
->ns
, "workstations",
452 pdb_get_workstations(u
));
454 if (pdb_get_unknown_str(u
) && strcmp(pdb_get_unknown_str(u
), ""))
455 xmlNewChild(user
, data
->ns
, "unknown_str", pdb_get_unknown_str(u
));
457 if (pdb_get_munged_dial(u
) && strcmp(pdb_get_munged_dial(u
), ""))
458 xmlNewChild(user
, data
->ns
, "munged_dial", pdb_get_munged_dial(u
));
462 pass
= xmlNewChild(user
, data
->ns
, "password", NULL
);
463 if (pdb_get_pass_last_set_time(u
))
464 xmlNewProp(pass
, "last_set", iota(pdb_get_pass_last_set_time(u
)));
465 if (pdb_get_init_flags(u
, PDB_CANCHANGETIME
) != PDB_DEFAULT
)
466 xmlNewProp(pass
, "can_change",
467 iota(pdb_get_pass_can_change_time(u
)));
469 if (pdb_get_init_flags(u
, PDB_MUSTCHANGETIME
) != PDB_DEFAULT
)
470 xmlNewProp(pass
, "must_change",
471 iota(pdb_get_pass_must_change_time(u
)));
474 if (pdb_get_lanman_passwd(u
)) {
475 pdb_sethexpwd(temp
, pdb_get_lanman_passwd(u
),
476 pdb_get_acct_ctrl(u
));
477 cur
= xmlNewChild(pass
, data
->ns
, "crypt", temp
);
478 xmlNewProp(cur
, "type", "lanman");
481 if (pdb_get_nt_passwd(u
)) {
482 pdb_sethexpwd(temp
, pdb_get_nt_passwd(u
), pdb_get_acct_ctrl(u
));
483 cur
= xmlNewChild(pass
, data
->ns
, "crypt", temp
);
484 xmlNewProp(cur
, "type", "nt");
487 xmlNewChild(user
, data
->ns
, "acct_ctrl", iota(pdb_get_acct_ctrl(u
)));
488 xmlNewChild(user
, data
->ns
, "unknown_3", iota(pdb_get_unknown_3(u
)));
490 if (pdb_get_logon_divs(u
))
491 xmlNewChild(user
, data
->ns
, "logon_divs",
492 iota(pdb_get_logon_divs(u
)));
494 if (pdb_get_hours_len(u
))
495 xmlNewChild(user
, data
->ns
, "hours_len",
496 iota(pdb_get_hours_len(u
)));
498 xmlNewChild(user
, data
->ns
, "unknown_5", iota(pdb_get_unknown_5(u
)));
499 xmlNewChild(user
, data
->ns
, "unknown_6", iota(pdb_get_unknown_6(u
)));
500 xmlSaveFile(data
->location
, data
->doc
);
505 static NTSTATUS
xmlsam_init(PDB_CONTEXT
* pdb_context
, PDB_METHODS
** pdb_method
,
506 const char *location
)
511 xmlsam_debug_level
= debug_add_class("xmlsam");
512 if (xmlsam_debug_level
== -1) {
513 xmlsam_debug_level
= DBGC_ALL
;
514 DEBUG(0, ("xmlsam: Couldn't register custom debugging class!\n"));
518 DEBUG(0, ("invalid pdb_methods specified\n"));
519 return NT_STATUS_UNSUCCESSFUL
;
523 (nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
527 (*pdb_method
)->name
= "xmlsam";
529 (*pdb_method
)->setsampwent
= xmlsam_setsampwent
;
530 (*pdb_method
)->endsampwent
= xmlsam_endsampwent
;
531 (*pdb_method
)->getsampwent
= xmlsam_getsampwent
;
532 (*pdb_method
)->add_sam_account
= xmlsam_add_sam_account
;
533 (*pdb_method
)->getsampwnam
= NULL
;
534 (*pdb_method
)->getsampwsid
= NULL
;
535 (*pdb_method
)->update_sam_account
= NULL
;
536 (*pdb_method
)->delete_sam_account
= NULL
;
537 (*pdb_method
)->getgrsid
= NULL
;
538 (*pdb_method
)->getgrgid
= NULL
;
539 (*pdb_method
)->getgrnam
= NULL
;
540 (*pdb_method
)->add_group_mapping_entry
= NULL
;
541 (*pdb_method
)->update_group_mapping_entry
= NULL
;
542 (*pdb_method
)->delete_group_mapping_entry
= NULL
;
543 (*pdb_method
)->enum_group_mapping
= NULL
;
545 data
= talloc(pdb_context
->mem_ctx
, sizeof(pdb_xml
));
546 data
->location
= talloc_strdup(pdb_context
->mem_ctx
, (location
? location
: "passdb.xml"));
549 (*pdb_method
)->private_data
= data
;
551 LIBXML_TEST_VERSION
xmlKeepBlanksDefault(0);
556 NTSTATUS
pdb_xml_init(void)
558 return smb_register_passdb(PASSDB_INTERFACE_VERSION
, "xml", xmlsam_init
);