Added proper memory scheme for authentication handler module.
[yazproxy-moved-to-github.git] / src / mod_sample.cpp
1 /* $Id: mod_sample.cpp,v 1.3 2005-06-10 22:54:22 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25
26 #include <yazproxy/module.h>
27
28 #if HAVE_XSLT
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xinclude.h>
32 #include <libxslt/xsltutils.h>
33 #include <libxslt/transform.h>
34 #endif
35
36 void *my_init(void)
37 {
38     return 0;  // no private data for handler
39 }
40
41 void my_destroy(void *p)
42 {
43     // private data destroy
44 }
45
46 int my_authenticate(void *user_handle,
47                     const char *target_name,
48                     void *element_ptr,
49                     const char *user, const char *group, const char *password)
50 {
51     // see if we have an "args" attribute
52     const char *args = 0;
53 #if HAVE_XSLT
54     xmlNodePtr ptr = (xmlNodePtr) element_ptr;
55     struct _xmlAttr *attr;
56     
57     for (attr = ptr->properties; attr; attr = attr->next)
58     {
59         if (!strcmp((const char *) attr->name, "args") &&
60             attr->children && attr->children->type == XML_TEXT_NODE)
61             args = (const char *) attr->children->content;
62     }
63 #endif
64     // args holds args (or NULL if  none is provided)
65
66     sleep(1);
67     fprintf(stderr, "my_authenticate: target=%s user=%s group=%s args=%s\n",
68             target_name ? target_name : "none", 
69             user ? user : "none", group ? group : "none",
70             args ? args : "none");
71     // authentication handler
72     if (!user && !group && !password)
73         return YAZPROXY_RET_OK;   // OK if anonymous
74     if (user && !strcmp(user, "guest")
75         && password && !strcmp(password, "guest"))  // or guest guest
76         return YAZPROXY_RET_OK;
77     return YAZPROXY_RET_PERM;  // fail otherwise
78 }
79
80 Yaz_ProxyModule_int0 interface0 = {
81     my_init,
82     my_destroy,
83     my_authenticate
84 };
85
86 Yaz_ProxyModule_entry yazproxy_module = {
87     0,                            // interface version
88     "sample",                     // name
89     "Sample Module for YAZ Proxy",// description
90     &interface0
91 };