This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[kai/samba.git] / source3 / passdb / pdb_xml.c
1
2 /*
3  * XML password backend for samba
4  * Copyright (C) Jelmer Vernooij 2002
5  * Some parts based on the libxml gjobread example by Daniel Veillard
6  * 
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)
10  * any later version.
11  * 
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
15  * more details.
16  * 
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.
20  */
21
22 /* FIXME: 
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
26  * - Do locking!
27  * - Better names!
28  */
29
30
31 #define XML_URL "http://www.samba.org/ns"
32
33 #include "includes.h"
34
35 #include <libxml/xmlmemory.h>
36 #include <libxml/parser.h>
37
38 static int xmlsam_debug_level = DBGC_ALL;
39
40 #undef DBGC_CLASS
41 #define DBGC_CLASS xmlsam_debug_level
42
43 PDB_MODULE_VERSIONING_MAGIC 
44
45 static char * iota(int a) {
46         static char tmp[10];
47
48         snprintf(tmp, 9, "%d", a);
49         return tmp;
50 }
51
52 BOOL parsePass(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur, SAM_ACCOUNT * u)
53 {
54         pstring temp;
55
56         cur = cur->xmlChildrenNode;
57         while (cur != NULL) {
58                 if (strcmp(cur->name, "crypt"))
59                         DEBUG(0, ("Unknown element %s\n", cur->name));
60                 else {
61                         if (!strcmp(xmlGetProp(cur, "type"), "nt")
62                                 &&
63                                 pdb_gethexpwd(xmlNodeListGetString
64                                                           (doc, cur->xmlChildrenNode, 1), temp))
65                                 pdb_set_nt_passwd(u, temp, PDB_SET);
66                         else if (!strcmp(xmlGetProp(cur, "type"), "lanman")
67                                          &&
68                                          pdb_gethexpwd(xmlNodeListGetString
69                                                                    (doc, cur->xmlChildrenNode, 1), temp))
70                                 pdb_set_lanman_passwd(u, temp, PDB_SET);
71                         else
72                                 DEBUG(0,
73                                           ("Unknown crypt type: %s\n",
74                                            xmlGetProp(cur, "type")));
75                 }
76                 cur = cur->next;
77         }
78         return True;
79 }
80
81 BOOL parseUser(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur, SAM_ACCOUNT * u)
82 {
83         char *tmp;
84         DOM_SID sid;
85
86         tmp = xmlGetProp(cur, "sid");
87         if (tmp){
88                 string_to_sid(&sid, tmp);
89                 pdb_set_user_sid(u, &sid, PDB_SET);
90         }
91         tmp = xmlGetProp(cur, "uid");
92         if (tmp)
93                 pdb_set_uid(u, atol(tmp), PDB_SET);
94         pdb_set_username(u, xmlGetProp(cur, "name"), PDB_SET);
95         /* We don't care what the top level element name is */
96         cur = cur->xmlChildrenNode;
97         while (cur != NULL) {
98                 if ((!strcmp(cur->name, "group")) && (cur->ns == ns)) {
99                         tmp = xmlGetProp(cur, "gid");
100                         if (tmp)
101                                 pdb_set_gid(u, atol(tmp), PDB_SET);
102                         tmp = xmlGetProp(cur, "sid");
103                         if (tmp){
104                                 string_to_sid(&sid, tmp);
105                                 pdb_set_group_sid(u, &sid, PDB_SET);
106                         }
107                 }
108
109                 else if ((!strcmp(cur->name, "domain")) && (cur->ns == ns))
110                         pdb_set_domain(u,
111                                                    xmlNodeListGetString(doc, cur->xmlChildrenNode,
112                                                                                                 1), PDB_SET);
113
114                 else if (!strcmp(cur->name, "fullname") && cur->ns == ns)
115                         pdb_set_fullname(u,
116                                                          xmlNodeListGetString(doc,
117                                                                                                   cur->xmlChildrenNode,
118                                                                                                   1), PDB_SET);
119
120                 else if (!strcmp(cur->name, "nt_username") && cur->ns == ns)
121                         pdb_set_nt_username(u,
122                                                                 xmlNodeListGetString(doc,
123                                                                                                          cur->xmlChildrenNode,
124                                                                                                          1), PDB_SET);
125
126                 else if (!strcmp(cur->name, "logon_script") && cur->ns == ns)
127                         pdb_set_logon_script(u,
128                                                                  xmlNodeListGetString(doc,
129                                                                                                           cur->xmlChildrenNode,
130                                                                                                           1), PDB_SET);
131
132                 else if (!strcmp(cur->name, "profile_path") && cur->ns == ns)
133                         pdb_set_profile_path(u,
134                                                                  xmlNodeListGetString(doc,
135                                                                                                           cur->xmlChildrenNode,
136                                                                                                           1), PDB_SET);
137
138                 else if (!strcmp(cur->name, "logon_time") && cur->ns == ns)
139                         pdb_set_logon_time(u,
140                                                            atol(xmlNodeListGetString
141                                                                         (doc, cur->xmlChildrenNode, 1)), PDB_SET);
142
143                 else if (!strcmp(cur->name, "logoff_time") && cur->ns == ns)
144                         pdb_set_logoff_time(u,
145                                                                 atol(xmlNodeListGetString
146                                                                          (doc, cur->xmlChildrenNode, 1)),
147                                                                 PDB_SET);
148
149                 else if (!strcmp(cur->name, "kickoff_time") && cur->ns == ns)
150                         pdb_set_kickoff_time(u,
151                                                                  atol(xmlNodeListGetString
152                                                                           (doc, cur->xmlChildrenNode, 1)),
153                                                                  PDB_SET);
154
155                 else if (!strcmp(cur->name, "logon_divs") && cur->ns == ns)
156                         pdb_set_logon_divs(u,
157                                                            atol(xmlNodeListGetString
158                                                                         (doc, cur->xmlChildrenNode, 1)), PDB_SET);
159
160                 else if (!strcmp(cur->name, "hours_len") && cur->ns == ns)
161                         pdb_set_hours_len(u,
162                                                           atol(xmlNodeListGetString
163                                                                    (doc, cur->xmlChildrenNode, 1)), PDB_SET);
164
165                 else if (!strcmp(cur->name, "unknown_3") && cur->ns == ns)
166                         pdb_set_unknown_3(u,
167                                                           atol(xmlNodeListGetString
168                                                                    (doc, cur->xmlChildrenNode, 1)), PDB_SET);
169
170                 else if (!strcmp(cur->name, "unknown_5") && cur->ns == ns)
171                         pdb_set_unknown_5(u,
172                                                           atol(xmlNodeListGetString
173                                                                    (doc, cur->xmlChildrenNode, 1)), PDB_SET);
174
175                 else if (!strcmp(cur->name, "unknown_6") && cur->ns == ns)
176                         pdb_set_unknown_6(u,
177                                                           atol(xmlNodeListGetString
178                                                                    (doc, cur->xmlChildrenNode, 1)), PDB_SET);
179
180                 else if (!strcmp(cur->name, "homedir") && cur->ns == ns)
181                         pdb_set_homedir(u,
182                                                         xmlNodeListGetString(doc, cur->xmlChildrenNode,
183                                                                                                  1), PDB_SET);
184
185                 else if (!strcmp(cur->name, "unknown_str") && cur->ns == ns)
186                         pdb_set_unknown_str(u,
187                                                                 xmlNodeListGetString(doc,
188                                                                                                          cur->xmlChildrenNode,
189                                                                                                          1), PDB_SET);
190
191                 else if (!strcmp(cur->name, "dir_drive") && cur->ns == ns)
192                         pdb_set_dir_drive(u,
193                                                           xmlNodeListGetString(doc,
194                                                                                                    cur->xmlChildrenNode,
195                                                                                                    1), PDB_SET);
196
197                 else if (!strcmp(cur->name, "munged_dial") && cur->ns == ns)
198                         pdb_set_munged_dial(u,
199                                                                 xmlNodeListGetString(doc,
200                                                                                                          cur->xmlChildrenNode,
201                                                                                                          1), PDB_SET);
202
203                 else if (!strcmp(cur->name, "acct_desc") && cur->ns == ns)
204                         pdb_set_acct_desc(u,
205                                                           xmlNodeListGetString(doc,
206                                                                                                    cur->xmlChildrenNode,
207                                                                                                    1), PDB_SET);
208
209                 else if (!strcmp(cur->name, "acct_ctrl") && cur->ns == ns)
210                         pdb_set_acct_ctrl(u,
211                                                           atol(xmlNodeListGetString
212                                                                    (doc, cur->xmlChildrenNode, 1)), PDB_SET);
213
214                 else if (!strcmp(cur->name, "workstations") && cur->ns == ns)
215                         pdb_set_workstations(u,
216                                                                  xmlNodeListGetString(doc,
217                                                                                                           cur->xmlChildrenNode,
218                                                                                                           1), PDB_SET);
219
220                 else if ((!strcmp(cur->name, "password")) && (cur->ns == ns)) {
221                         tmp = xmlGetProp(cur, "last_set");
222                         if (tmp)
223                                 pdb_set_pass_last_set_time(u, atol(tmp), PDB_SET);
224                         tmp = xmlGetProp(cur, "must_change");
225                         if (tmp)
226                                 pdb_set_pass_must_change_time(u, atol(tmp), PDB_SET);
227                         tmp = xmlGetProp(cur, "can_change");
228                         if (tmp)
229                                 pdb_set_pass_can_change_time(u, atol(tmp), PDB_SET);
230                         parsePass(doc, ns, cur, u);
231                 }
232
233                 else
234                         DEBUG(0, ("Unknown element %s\n", cur->name));
235                 cur = cur->next;
236         }
237
238         return True;
239 }
240
241 typedef struct pdb_xml {
242         char *location;
243         char written;
244         xmlDocPtr doc;
245         xmlNodePtr users;
246         xmlNodePtr pwent;
247         xmlNsPtr ns;
248 } pdb_xml;
249
250 xmlNodePtr parseSambaXMLFile(struct pdb_xml *data)
251 {
252         xmlNodePtr cur;
253
254         data->doc = xmlParseFile(data->location);
255         if (data->doc == NULL)
256                 return NULL;
257
258         cur = xmlDocGetRootElement(data->doc);
259         if (!cur) {
260                 DEBUG(0, ("empty document\n"));
261                 xmlFreeDoc(data->doc);
262                 return NULL;
263         }
264         data->ns = xmlSearchNsByHref(data->doc, cur, XML_URL);
265         if (!data->ns) {
266                 DEBUG(0,
267                           ("document of the wrong type, samba user namespace not found\n"));
268                 xmlFreeDoc(data->doc);
269                 return NULL;
270         }
271         if (strcmp(cur->name, "samba")) {
272                 DEBUG(0, ("document of the wrong type, root node != samba"));
273                 xmlFreeDoc(data->doc);
274                 return NULL;
275         }
276
277         cur = cur->xmlChildrenNode;
278         while (cur && xmlIsBlankNode(cur)) {
279                 cur = cur->next;
280         }
281         if (!cur)
282                 return NULL;
283         if ((strcmp(cur->name, "users")) || (cur->ns != data->ns)) {
284                 DEBUG(0, ("document of the wrong type, was '%s', users expected",
285                                   cur->name));
286                 DEBUG(0, ("xmlDocDump follows\n"));
287                 xmlDocDump(stderr, data->doc);
288                 DEBUG(0, ("xmlDocDump finished\n"));
289                 xmlFreeDoc(data->doc);
290                 return NULL;
291         }
292         data->users = cur;
293         cur = cur->xmlChildrenNode;
294         return cur;
295 }
296
297 static NTSTATUS xmlsam_setsampwent(struct pdb_methods *methods, BOOL update)
298 {
299         pdb_xml *data;
300
301         if (!methods) {
302                 DEBUG(0, ("Invalid methods\n"));
303                 return NT_STATUS_INVALID_PARAMETER;
304         }
305         data = (pdb_xml *) methods->private_data;
306         if (!data) {
307                 DEBUG(0, ("Invalid pdb_xml_data\n"));
308                 return NT_STATUS_INVALID_PARAMETER;
309         }
310         data->pwent = parseSambaXMLFile(data);
311         if (!data->pwent)
312                 return NT_STATUS_UNSUCCESSFUL;
313         
314         return NT_STATUS_OK;
315 }
316
317 /***************************************************************
318   End enumeration of the passwd list.
319  ****************************************************************/
320
321 static void xmlsam_endsampwent(struct pdb_methods *methods)
322 {
323         pdb_xml *data;
324
325         if (!methods) {
326                 DEBUG(0, ("Invalid methods\n"));
327                 return;
328         }
329
330         data = (pdb_xml *) methods->private_data;
331
332         if (!data) {
333                 DEBUG(0, ("Invalid pdb_xml_data\n"));
334                 return;
335         }
336
337         xmlFreeDoc(data->doc);
338         data->doc = NULL;
339         data->pwent = NULL;
340 }
341
342 /*****************************************************************
343   Get one SAM_ACCOUNT from the list (next in line)
344  *****************************************************************/
345
346 static NTSTATUS xmlsam_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT * user)
347 {
348         pdb_xml *data;
349
350         if (!methods) {
351                 DEBUG(0, ("Invalid methods\n"));
352                 return NT_STATUS_INVALID_PARAMETER;
353         }
354         data = (pdb_xml *) methods->private_data;
355
356         if (!data) {
357                 DEBUG(0, ("Invalid pdb_xml_data\n"));
358                 return NT_STATUS_INVALID_PARAMETER;
359         }
360
361         while (data->pwent) {
362                 if ((!strcmp(data->pwent->name, "user")) &&
363                         (data->pwent->ns == data->ns)) {
364
365                         parseUser(data->doc, data->ns, data->pwent, user);
366                         data->pwent = data->pwent->next;
367                         return NT_STATUS_OK;
368                 }
369                 data->pwent = data->pwent->next;
370         }
371         return NT_STATUS_UNSUCCESSFUL;
372 }
373
374 /***************************************************************************
375   Adds an existing SAM_ACCOUNT
376  ****************************************************************************/
377
378 static NTSTATUS xmlsam_add_sam_account(struct pdb_methods *methods, SAM_ACCOUNT * u)
379 {
380         pstring temp;
381         fstring sid_str;
382         xmlNodePtr cur, user, pass, root;
383         pdb_xml *data;
384
385         DEBUG(10, ("xmlsam_add_sam_account called!\n"));
386
387         if (!methods) {
388                 DEBUG(0, ("Invalid methods\n"));
389                 return NT_STATUS_INVALID_PARAMETER;
390         }
391
392         data = (pdb_xml *) methods->private_data;
393         if (!data) {
394                 DEBUG(0, ("Invalid pdb_xml_data\n"));
395                 return NT_STATUS_INVALID_PARAMETER;
396         }
397
398         /* Create a new document if we can't open the current one */
399         if (!parseSambaXMLFile(data)) {
400                 DEBUG(0, ("Can't load current XML file, creating a new one\n"));
401                 data->doc = xmlNewDoc(XML_DEFAULT_VERSION);
402                 root = xmlNewDocNode(data->doc, NULL, "samba", NULL);
403                 cur = xmlDocSetRootElement(data->doc, root);
404                 data->ns = xmlNewNs(root, XML_URL, "samba");
405                 data->users = xmlNewChild(root, data->ns, "users", NULL);
406         }
407
408         user = xmlNewChild(data->users, data->ns, "user", NULL);
409         xmlNewProp(user, "sid",
410                            sid_to_string(sid_str, pdb_get_user_sid(u)));
411         if (pdb_get_init_flags(u, PDB_UID) != PDB_DEFAULT)
412                 xmlNewProp(user, "uid", iota(pdb_get_uid(u)));
413
414         if (pdb_get_username(u) && strcmp(pdb_get_username(u), ""))
415                 xmlNewProp(user, "name", pdb_get_username(u));
416
417         cur = xmlNewChild(user, data->ns, "group", NULL);
418         
419         xmlNewProp(cur, "sid",
420                            sid_to_string(sid_str, pdb_get_group_sid(u)));
421         if (pdb_get_init_flags(u, PDB_GID) != PDB_DEFAULT)
422                 xmlNewProp(cur, "gid", iota(pdb_get_gid(u)));
423
424         if (pdb_get_init_flags(u, PDB_LOGONTIME) != PDB_DEFAULT)
425                 xmlNewChild(user, data->ns, "login_time",
426                                         iota(pdb_get_logon_time(u)));
427
428         if (pdb_get_init_flags(u, PDB_LOGOFFTIME) != PDB_DEFAULT)
429                 xmlNewChild(user, data->ns, "logoff_time",
430                                         iota(pdb_get_logoff_time(u)));
431
432         if (pdb_get_init_flags(u, PDB_KICKOFFTIME) != PDB_DEFAULT)
433                 xmlNewChild(user, data->ns, "kickoff_time",
434                                         iota(pdb_get_kickoff_time(u)));
435
436         if (pdb_get_domain(u) && strcmp(pdb_get_domain(u), ""))
437                 xmlNewChild(user, data->ns, "domain", pdb_get_domain(u));
438
439         if (pdb_get_nt_username(u) && strcmp(pdb_get_nt_username(u), ""))
440                 xmlNewChild(user, data->ns, "nt_username", pdb_get_nt_username(u));
441
442         if (pdb_get_fullname(u) && strcmp(pdb_get_fullname(u), ""))
443                 xmlNewChild(user, data->ns, "fullname", pdb_get_fullname(u));
444
445         if (pdb_get_homedir(u) && strcmp(pdb_get_homedir(u), ""))
446                 xmlNewChild(user, data->ns, "homedir", pdb_get_homedir(u));
447
448         if (pdb_get_dir_drive(u) && strcmp(pdb_get_dir_drive(u), ""))
449                 xmlNewChild(user, data->ns, "dir_drive", pdb_get_dir_drive(u));
450
451         if (pdb_get_logon_script(u) && strcmp(pdb_get_logon_script(u), ""))
452                 xmlNewChild(user, data->ns, "logon_script",
453                                         pdb_get_logon_script(u));
454
455         if (pdb_get_profile_path(u) && strcmp(pdb_get_profile_path(u), ""))
456                 xmlNewChild(user, data->ns, "profile_path",
457                                         pdb_get_profile_path(u));
458
459         if (pdb_get_acct_desc(u) && strcmp(pdb_get_acct_desc(u), ""))
460                 xmlNewChild(user, data->ns, "acct_desc", pdb_get_acct_desc(u));
461
462         if (pdb_get_workstations(u) && strcmp(pdb_get_workstations(u), ""))
463                 xmlNewChild(user, data->ns, "workstations",
464                                         pdb_get_workstations(u));
465
466         if (pdb_get_unknown_str(u) && strcmp(pdb_get_unknown_str(u), ""))
467                 xmlNewChild(user, data->ns, "unknown_str", pdb_get_unknown_str(u));
468
469         if (pdb_get_munged_dial(u) && strcmp(pdb_get_munged_dial(u), ""))
470                 xmlNewChild(user, data->ns, "munged_dial", pdb_get_munged_dial(u));
471
472
473         /* Password stuff */
474         pass = xmlNewChild(user, data->ns, "password", NULL);
475         if (pdb_get_pass_last_set_time(u))
476                 xmlNewProp(pass, "last_set", iota(pdb_get_pass_last_set_time(u)));
477         if (pdb_get_init_flags(u, PDB_CANCHANGETIME) != PDB_DEFAULT)
478                 xmlNewProp(pass, "can_change",
479                                    iota(pdb_get_pass_can_change_time(u)));
480
481         if (pdb_get_init_flags(u, PDB_MUSTCHANGETIME) != PDB_DEFAULT)
482                 xmlNewProp(pass, "must_change",
483                                    iota(pdb_get_pass_must_change_time(u)));
484
485
486         if (pdb_get_lanman_passwd(u)) {
487                 pdb_sethexpwd(temp, pdb_get_lanman_passwd(u),
488                                           pdb_get_acct_ctrl(u));
489                 cur = xmlNewChild(pass, data->ns, "crypt", temp);
490                 xmlNewProp(cur, "type", "lanman");
491         }
492
493         if (pdb_get_nt_passwd(u)) {
494                 pdb_sethexpwd(temp, pdb_get_nt_passwd(u), pdb_get_acct_ctrl(u));
495                 cur = xmlNewChild(pass, data->ns, "crypt", temp);
496                 xmlNewProp(cur, "type", "nt");
497         }
498
499         xmlNewChild(user, data->ns, "acct_ctrl", iota(pdb_get_acct_ctrl(u)));
500         xmlNewChild(user, data->ns, "unknown_3", iota(pdb_get_unknown_3(u)));
501
502         if (pdb_get_logon_divs(u))
503                 xmlNewChild(user, data->ns, "logon_divs",
504                                         iota(pdb_get_logon_divs(u)));
505
506         if (pdb_get_hours_len(u))
507                 xmlNewChild(user, data->ns, "hours_len",
508                                         iota(pdb_get_hours_len(u)));
509
510         xmlNewChild(user, data->ns, "unknown_5", iota(pdb_get_unknown_5(u)));
511         xmlNewChild(user, data->ns, "unknown_6", iota(pdb_get_unknown_6(u)));
512         xmlSaveFile(data->location, data->doc);
513
514         return NT_STATUS_OK;
515 }
516
517 NTSTATUS pdb_init(PDB_CONTEXT * pdb_context, PDB_METHODS ** pdb_method,
518                  const char *location)
519 {
520         NTSTATUS nt_status;
521         pdb_xml *data;
522
523         xmlsam_debug_level = debug_add_class("xmlsam");
524         if (xmlsam_debug_level == -1) {
525                 xmlsam_debug_level = DBGC_ALL;
526                 DEBUG(0, ("xmlsam: Couldn't register custom debugging class!\n"));
527         }
528
529         if (!pdb_context) {
530                 DEBUG(0, ("invalid pdb_methods specified\n"));
531                 return NT_STATUS_UNSUCCESSFUL;
532         }
533
534         if (!NT_STATUS_IS_OK
535                 (nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
536                 return nt_status;
537         }
538
539         (*pdb_method)->name = "xmlsam";
540
541         (*pdb_method)->setsampwent = xmlsam_setsampwent;
542         (*pdb_method)->endsampwent = xmlsam_endsampwent;
543         (*pdb_method)->getsampwent = xmlsam_getsampwent;
544         (*pdb_method)->add_sam_account = xmlsam_add_sam_account;
545         (*pdb_method)->getsampwnam = NULL;
546         (*pdb_method)->getsampwsid = NULL;
547         (*pdb_method)->update_sam_account = NULL;
548         (*pdb_method)->delete_sam_account = NULL;
549         (*pdb_method)->getgrsid = NULL;
550         (*pdb_method)->getgrgid = NULL;
551         (*pdb_method)->getgrnam = NULL;
552         (*pdb_method)->add_group_mapping_entry = NULL;
553         (*pdb_method)->update_group_mapping_entry = NULL;
554         (*pdb_method)->delete_group_mapping_entry = NULL;
555         (*pdb_method)->enum_group_mapping = NULL;
556
557         data = talloc(pdb_context->mem_ctx, sizeof(pdb_xml));
558         data->location =
559                 (location ? talloc_strdup(pdb_context->mem_ctx, location) : "-");
560         data->pwent = NULL;
561         data->written = 0;
562         (*pdb_method)->private_data = data;
563
564         LIBXML_TEST_VERSION xmlKeepBlanksDefault(0);
565
566         return NT_STATUS_OK;
567 }