Version 1.3.8
[yazproxy-moved-to-github.git] / src / mod_sample.cpp
1 /* This file is part of YAZ proxy
2    Copyright (C) 1998-2011 Index Data
3
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include <yazproxy/module.h>
24
25 #if YAZ_HAVE_XSLT
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxml/xinclude.h>
29 #include <libxslt/xsltutils.h>
30 #include <libxslt/transform.h>
31 #endif
32
33 void *my_init(void)
34 {
35     return 0;  // no private data for handler
36 }
37
38 void my_destroy(void *p)
39 {
40     // private data destroy
41 }
42
43 int my_authenticate(void *user_handle,
44                     const char *target_name,
45                     void *element_ptr,
46                     const char *user, const char *group, const char *password,
47                     const char *peer_IP)
48 {
49     // see if we have an "args" attribute
50     const char *args = 0;
51 #if YAZ_HAVE_XSLT
52     xmlNodePtr ptr = (xmlNodePtr) element_ptr;
53     struct _xmlAttr *attr;
54
55     for (attr = ptr->properties; attr; attr = attr->next)
56     {
57         if (!strcmp((const char *) attr->name, "args") &&
58             attr->children && attr->children->type == XML_TEXT_NODE)
59             args = (const char *) attr->children->content;
60     }
61 #endif
62     // args holds args (or NULL if  none is provided)
63
64     sleep(2);
65     fprintf(stderr, "my_authenticate: target=%s user=%s group=%s args=%s IP=%s"
66             "\n",
67             target_name ? target_name : "none",
68             user ? user : "none", group ? group : "none",
69             args ? args : "none",
70             peer_IP);
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 };
92 /*
93  * Local variables:
94  * c-basic-offset: 4
95  * c-file-style: "Stroustrup"
96  * indent-tabs-mode: nil
97  * End:
98  * vim: shiftwidth=4 tabstop=8 expandtab
99  */
100