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
, "bad_password_count") && cur
->ns
== ns
)
163 pdb_set_bad_password_count(u
,
164 atol(xmlNodeListGetString
165 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
167 else if (!strcmp(cur
->name
, "logon_count") && cur
->ns
== ns
)
168 pdb_set_logon_count(u
,
169 atol(xmlNodeListGetString
170 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
172 else if (!strcmp(cur
->name
, "unknown_6") && cur
->ns
== ns
)
174 atol(xmlNodeListGetString
175 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
177 else if (!strcmp(cur
->name
, "homedir") && cur
->ns
== ns
)
179 xmlNodeListGetString(doc
, cur
->xmlChildrenNode
,
182 else if (!strcmp(cur
->name
, "unknown_str") && cur
->ns
== ns
)
183 pdb_set_unknown_str(u
,
184 xmlNodeListGetString(doc
,
185 cur
->xmlChildrenNode
,
188 else if (!strcmp(cur
->name
, "dir_drive") && cur
->ns
== ns
)
190 xmlNodeListGetString(doc
,
191 cur
->xmlChildrenNode
,
194 else if (!strcmp(cur
->name
, "munged_dial") && cur
->ns
== ns
)
195 pdb_set_munged_dial(u
,
196 xmlNodeListGetString(doc
,
197 cur
->xmlChildrenNode
,
200 else if (!strcmp(cur
->name
, "acct_desc") && cur
->ns
== ns
)
202 xmlNodeListGetString(doc
,
203 cur
->xmlChildrenNode
,
206 else if (!strcmp(cur
->name
, "acct_ctrl") && cur
->ns
== ns
)
208 atol(xmlNodeListGetString
209 (doc
, cur
->xmlChildrenNode
, 1)), PDB_SET
);
211 else if (!strcmp(cur
->name
, "workstations") && cur
->ns
== ns
)
212 pdb_set_workstations(u
,
213 xmlNodeListGetString(doc
,
214 cur
->xmlChildrenNode
,
217 else if ((!strcmp(cur
->name
, "password")) && (cur
->ns
== ns
)) {
218 tmp
= xmlGetProp(cur
, "last_set");
220 pdb_set_pass_last_set_time(u
, atol(tmp
), PDB_SET
);
221 tmp
= xmlGetProp(cur
, "must_change");
223 pdb_set_pass_must_change_time(u
, atol(tmp
), PDB_SET
);
224 tmp
= xmlGetProp(cur
, "can_change");
226 pdb_set_pass_can_change_time(u
, atol(tmp
), PDB_SET
);
227 parsePass(doc
, ns
, cur
, u
);
231 DEBUG(0, ("Unknown element %s\n", cur
->name
));
238 typedef struct pdb_xml
{
247 static xmlNodePtr
parseSambaXMLFile(struct pdb_xml
*data
)
251 data
->doc
= xmlParseFile(data
->location
);
252 if (data
->doc
== NULL
)
255 cur
= xmlDocGetRootElement(data
->doc
);
257 DEBUG(0, ("empty document\n"));
258 xmlFreeDoc(data
->doc
);
261 data
->ns
= xmlSearchNsByHref(data
->doc
, cur
, XML_URL
);
264 ("document of the wrong type, samba user namespace not found\n"));
265 xmlFreeDoc(data
->doc
);
268 if (strcmp(cur
->name
, "samba")) {
269 DEBUG(0, ("document of the wrong type, root node != samba"));
270 xmlFreeDoc(data
->doc
);
274 cur
= cur
->xmlChildrenNode
;
275 while (cur
&& xmlIsBlankNode(cur
)) {
280 if ((strcmp(cur
->name
, "users")) || (cur
->ns
!= data
->ns
)) {
281 DEBUG(0, ("document of the wrong type, was '%s', users expected",
283 DEBUG(0, ("xmlDocDump follows\n"));
284 xmlDocDump(stderr
, data
->doc
);
285 DEBUG(0, ("xmlDocDump finished\n"));
286 xmlFreeDoc(data
->doc
);
290 cur
= cur
->xmlChildrenNode
;
294 static NTSTATUS
xmlsam_setsampwent(struct pdb_methods
*methods
, BOOL update
)
299 DEBUG(0, ("Invalid methods\n"));
300 return NT_STATUS_INVALID_PARAMETER
;
302 data
= (pdb_xml
*) methods
->private_data
;
304 DEBUG(0, ("Invalid pdb_xml_data\n"));
305 return NT_STATUS_INVALID_PARAMETER
;
307 data
->pwent
= parseSambaXMLFile(data
);
309 return NT_STATUS_UNSUCCESSFUL
;
314 /***************************************************************
315 End enumeration of the passwd list.
316 ****************************************************************/
318 static void xmlsam_endsampwent(struct pdb_methods
*methods
)
323 DEBUG(0, ("Invalid methods\n"));
327 data
= (pdb_xml
*) methods
->private_data
;
330 DEBUG(0, ("Invalid pdb_xml_data\n"));
334 xmlFreeDoc(data
->doc
);
339 /*****************************************************************
340 Get one SAM_ACCOUNT from the list (next in line)
341 *****************************************************************/
343 static NTSTATUS
xmlsam_getsampwent(struct pdb_methods
*methods
, SAM_ACCOUNT
* user
)
348 DEBUG(0, ("Invalid methods\n"));
349 return NT_STATUS_INVALID_PARAMETER
;
351 data
= (pdb_xml
*) methods
->private_data
;
354 DEBUG(0, ("Invalid pdb_xml_data\n"));
355 return NT_STATUS_INVALID_PARAMETER
;
358 while (data
->pwent
) {
359 if ((!strcmp(data
->pwent
->name
, "user")) &&
360 (data
->pwent
->ns
== data
->ns
)) {
362 parseUser(data
->doc
, data
->ns
, data
->pwent
, user
);
363 data
->pwent
= data
->pwent
->next
;
366 data
->pwent
= data
->pwent
->next
;
368 return NT_STATUS_UNSUCCESSFUL
;
371 /***************************************************************************
372 Adds an existing SAM_ACCOUNT
373 ****************************************************************************/
375 static NTSTATUS
xmlsam_add_sam_account(struct pdb_methods
*methods
, SAM_ACCOUNT
* u
)
379 xmlNodePtr cur
, user
, pass
, root
;
382 DEBUG(10, ("xmlsam_add_sam_account called!\n"));
385 DEBUG(0, ("Invalid methods\n"));
386 return NT_STATUS_INVALID_PARAMETER
;
389 data
= (pdb_xml
*) methods
->private_data
;
391 DEBUG(0, ("Invalid pdb_xml_data\n"));
392 return NT_STATUS_INVALID_PARAMETER
;
395 /* Create a new document if we can't open the current one */
396 if (!parseSambaXMLFile(data
)) {
397 DEBUG(0, ("Can't load current XML file, creating a new one\n"));
398 data
->doc
= xmlNewDoc(XML_DEFAULT_VERSION
);
399 root
= xmlNewDocNode(data
->doc
, NULL
, "samba", NULL
);
400 cur
= xmlDocSetRootElement(data
->doc
, root
);
401 data
->ns
= xmlNewNs(root
, XML_URL
, "samba");
402 data
->users
= xmlNewChild(root
, data
->ns
, "users", NULL
);
405 user
= xmlNewChild(data
->users
, data
->ns
, "user", NULL
);
406 xmlNewProp(user
, "sid",
407 sid_to_string(sid_str
, pdb_get_user_sid(u
)));
409 if (pdb_get_username(u
) && strcmp(pdb_get_username(u
), ""))
410 xmlNewProp(user
, "name", pdb_get_username(u
));
412 cur
= xmlNewChild(user
, data
->ns
, "group", NULL
);
414 xmlNewProp(cur
, "sid",
415 sid_to_string(sid_str
, pdb_get_group_sid(u
)));
417 if (pdb_get_init_flags(u
, PDB_LOGONTIME
) != PDB_DEFAULT
)
418 xmlNewChild(user
, data
->ns
, "logon_time",
419 iota(pdb_get_logon_time(u
)));
421 if (pdb_get_init_flags(u
, PDB_LOGOFFTIME
) != PDB_DEFAULT
)
422 xmlNewChild(user
, data
->ns
, "logoff_time",
423 iota(pdb_get_logoff_time(u
)));
425 if (pdb_get_init_flags(u
, PDB_KICKOFFTIME
) != PDB_DEFAULT
)
426 xmlNewChild(user
, data
->ns
, "kickoff_time",
427 iota(pdb_get_kickoff_time(u
)));
429 if (pdb_get_domain(u
) && strcmp(pdb_get_domain(u
), ""))
430 xmlNewChild(user
, data
->ns
, "domain", pdb_get_domain(u
));
432 if (pdb_get_nt_username(u
) && strcmp(pdb_get_nt_username(u
), ""))
433 xmlNewChild(user
, data
->ns
, "nt_username", pdb_get_nt_username(u
));
435 if (pdb_get_fullname(u
) && strcmp(pdb_get_fullname(u
), ""))
436 xmlNewChild(user
, data
->ns
, "fullname", pdb_get_fullname(u
));
438 if (pdb_get_homedir(u
) && strcmp(pdb_get_homedir(u
), ""))
439 xmlNewChild(user
, data
->ns
, "homedir", pdb_get_homedir(u
));
441 if (pdb_get_dir_drive(u
) && strcmp(pdb_get_dir_drive(u
), ""))
442 xmlNewChild(user
, data
->ns
, "dir_drive", pdb_get_dir_drive(u
));
444 if (pdb_get_logon_script(u
) && strcmp(pdb_get_logon_script(u
), ""))
445 xmlNewChild(user
, data
->ns
, "logon_script",
446 pdb_get_logon_script(u
));
448 if (pdb_get_profile_path(u
) && strcmp(pdb_get_profile_path(u
), ""))
449 xmlNewChild(user
, data
->ns
, "profile_path",
450 pdb_get_profile_path(u
));
452 if (pdb_get_acct_desc(u
) && strcmp(pdb_get_acct_desc(u
), ""))
453 xmlNewChild(user
, data
->ns
, "acct_desc", pdb_get_acct_desc(u
));
455 if (pdb_get_workstations(u
) && strcmp(pdb_get_workstations(u
), ""))
456 xmlNewChild(user
, data
->ns
, "workstations",
457 pdb_get_workstations(u
));
459 if (pdb_get_unknown_str(u
) && strcmp(pdb_get_unknown_str(u
), ""))
460 xmlNewChild(user
, data
->ns
, "unknown_str", pdb_get_unknown_str(u
));
462 if (pdb_get_munged_dial(u
) && strcmp(pdb_get_munged_dial(u
), ""))
463 xmlNewChild(user
, data
->ns
, "munged_dial", pdb_get_munged_dial(u
));
467 pass
= xmlNewChild(user
, data
->ns
, "password", NULL
);
468 if (pdb_get_pass_last_set_time(u
))
469 xmlNewProp(pass
, "last_set", iota(pdb_get_pass_last_set_time(u
)));
470 if (pdb_get_init_flags(u
, PDB_CANCHANGETIME
) != PDB_DEFAULT
)
471 xmlNewProp(pass
, "can_change",
472 iota(pdb_get_pass_can_change_time(u
)));
474 if (pdb_get_init_flags(u
, PDB_MUSTCHANGETIME
) != PDB_DEFAULT
)
475 xmlNewProp(pass
, "must_change",
476 iota(pdb_get_pass_must_change_time(u
)));
479 if (pdb_get_lanman_passwd(u
)) {
480 pdb_sethexpwd(temp
, pdb_get_lanman_passwd(u
),
481 pdb_get_acct_ctrl(u
));
482 cur
= xmlNewChild(pass
, data
->ns
, "crypt", temp
);
483 xmlNewProp(cur
, "type", "lanman");
486 if (pdb_get_nt_passwd(u
)) {
487 pdb_sethexpwd(temp
, pdb_get_nt_passwd(u
), pdb_get_acct_ctrl(u
));
488 cur
= xmlNewChild(pass
, data
->ns
, "crypt", temp
);
489 xmlNewProp(cur
, "type", "nt");
492 xmlNewChild(user
, data
->ns
, "acct_ctrl", iota(pdb_get_acct_ctrl(u
)));
493 xmlNewChild(user
, data
->ns
, "unknown_3", iota(pdb_get_unknown_3(u
)));
495 if (pdb_get_logon_divs(u
))
496 xmlNewChild(user
, data
->ns
, "logon_divs",
497 iota(pdb_get_logon_divs(u
)));
499 if (pdb_get_hours_len(u
))
500 xmlNewChild(user
, data
->ns
, "hours_len",
501 iota(pdb_get_hours_len(u
)));
503 xmlNewChild(user
, data
->ns
, "bad_password_count", iota(pdb_get_bad_password_count(u
)));
504 xmlNewChild(user
, data
->ns
, "logon_count", iota(pdb_get_logon_count(u
)));
505 xmlNewChild(user
, data
->ns
, "unknown_6", iota(pdb_get_unknown_6(u
)));
506 xmlSaveFile(data
->location
, data
->doc
);
511 static NTSTATUS
xmlsam_init(PDB_CONTEXT
* pdb_context
, PDB_METHODS
** pdb_method
,
512 const char *location
)
517 xmlsam_debug_level
= debug_add_class("xmlsam");
518 if (xmlsam_debug_level
== -1) {
519 xmlsam_debug_level
= DBGC_ALL
;
520 DEBUG(0, ("xmlsam: Couldn't register custom debugging class!\n"));
524 DEBUG(0, ("invalid pdb_methods specified\n"));
525 return NT_STATUS_UNSUCCESSFUL
;
529 (nt_status
= make_pdb_methods(pdb_context
->mem_ctx
, pdb_method
))) {
533 (*pdb_method
)->name
= "xmlsam";
535 (*pdb_method
)->setsampwent
= xmlsam_setsampwent
;
536 (*pdb_method
)->endsampwent
= xmlsam_endsampwent
;
537 (*pdb_method
)->getsampwent
= xmlsam_getsampwent
;
538 (*pdb_method
)->add_sam_account
= xmlsam_add_sam_account
;
539 (*pdb_method
)->getsampwnam
= NULL
;
540 (*pdb_method
)->getsampwsid
= NULL
;
541 (*pdb_method
)->update_sam_account
= NULL
;
542 (*pdb_method
)->delete_sam_account
= NULL
;
543 (*pdb_method
)->get_group_info_by_sid
= NULL
;
544 (*pdb_method
)->get_group_list
= NULL
;
545 (*pdb_method
)->get_group_sids
= NULL
;
546 (*pdb_method
)->add_group
= NULL
;
547 (*pdb_method
)->update_group
= NULL
;
548 (*pdb_method
)->delete_group
= NULL
;
549 (*pdb_method
)->add_sid_to_group
= NULL
;
550 (*pdb_method
)->remove_sid_from_group
= NULL
;
551 (*pdb_method
)->get_group_info_by_name
= NULL
;
552 (*pdb_method
)->get_group_info_by_nt_name
= NULL
;
553 (*pdb_method
)->get_group_uids
= NULL
;
555 data
= talloc(pdb_context
->mem_ctx
, sizeof(pdb_xml
));
556 data
->location
= talloc_strdup(pdb_context
->mem_ctx
, (location
? location
: "passdb.xml"));
559 (*pdb_method
)->private_data
= data
;
561 LIBXML_TEST_VERSION
xmlKeepBlanksDefault(0);
566 NTSTATUS
pdb_xml_init(void)
568 return smb_register_passdb(PASSDB_INTERFACE_VERSION
, "xml", xmlsam_init
);