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