18238b63b2f7b80fab47e8dd69299a35d8ee4b40
[yazproxy-moved-to-github.git] / src / modules.cpp
1 /* $Id: modules.cpp,v 1.2 2005-06-10 22:53:43 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 #if HAVE_DLFCN_H
24 #include <dlfcn.h>
25 #endif
26
27 #include <yaz/nmem.h>
28 #include <yaz/log.h>
29 #include <yazproxy/module.h>
30
31 class Yaz_ProxyModule {
32     friend class Proxy_Msg;
33 private:
34     void *m_dl_handle;                /* dlopen/close handle */
35     Yaz_ProxyModule_entry *m_entry;
36     Yaz_ProxyModule *m_next; 
37     void *m_user_handle;              /* user handle */
38 public:
39     Yaz_ProxyModule(void *dl_handle, Yaz_ProxyModule_entry *ent,
40                     Yaz_ProxyModule *next);
41     ~Yaz_ProxyModule();
42     
43     Yaz_ProxyModule *get_next() { return m_next; };
44     int is_module(const char *name);
45     int authenticate(const char *target_name, void *element_ptr,
46                      const char *user, const char *group, const char *password);
47 };
48
49 int Yaz_ProxyModule::is_module(const char *name)
50 {
51     if (!name || !strcmp(m_entry->module_name, name))
52         return 1;
53     return 0;
54 }
55
56 Yaz_ProxyModule::Yaz_ProxyModule(void *dl_handle, Yaz_ProxyModule_entry *ent,
57                                  Yaz_ProxyModule *next)
58 {
59     m_dl_handle = dl_handle;
60     m_entry = ent;
61     m_next = next;
62     m_user_handle = 0;
63     if (m_entry->int_version == 0)
64     {
65         struct Yaz_ProxyModule_int0 *int0 =
66             reinterpret_cast<Yaz_ProxyModule_int0 *>(m_entry->fl);
67         if (int0->init)
68             m_user_handle = (*int0->init)();
69     }
70 }
71
72 Yaz_ProxyModule::~Yaz_ProxyModule()
73 {
74     if (m_entry->int_version == 0)
75     {
76         struct Yaz_ProxyModule_int0 *int0 =
77             reinterpret_cast<Yaz_ProxyModule_int0 *>(m_entry->fl);
78         if (int0->destroy)
79             (*int0->destroy)(m_user_handle);
80     }
81 #if HAVE_DLFCN_H
82     dlclose(m_dl_handle);
83 #endif
84 }
85
86 int Yaz_ProxyModule::authenticate(const char *name,
87                                   void *element_ptr,
88                                   const char *user, const char *group,
89                                   const char *password)
90 {
91     if (m_entry->int_version == 0)
92     {
93         struct Yaz_ProxyModule_int0 *int0 =
94             reinterpret_cast<Yaz_ProxyModule_int0 *>(m_entry->fl);
95         
96         if (!int0->authenticate)
97             return YAZPROXY_RET_NOT_ME;
98         return (*int0->authenticate)(m_user_handle, name, element_ptr,
99                                      user, group, password);
100     }
101     return YAZPROXY_RET_NOT_ME;
102 }
103
104 Yaz_ProxyModules::Yaz_ProxyModules()
105 {
106     m_list = 0;
107     m_no_open = 0;
108 }
109
110
111 Yaz_ProxyModules::~Yaz_ProxyModules()
112 {
113     unload_modules();
114 }
115
116 void Yaz_ProxyModules::unload_modules()
117 {
118     Yaz_ProxyModule *m = m_list;
119     while (m)
120     {
121         Yaz_ProxyModule *m_next = m->get_next();
122         delete m;
123         m_no_open--;
124         m = m_next;
125     }
126     m_list = 0;
127 }
128
129
130 int Yaz_ProxyModules::authenticate(const char *module_name,
131                                    const char *target_name, void *element_ptr,
132                                    const char *user,
133                                    const char *group,
134                                    const char *password)
135 {
136     int ret = YAZPROXY_RET_NOT_ME;
137     Yaz_ProxyModule *m = m_list;
138     for (; m; m = m->get_next())
139     {
140         if (m->is_module(module_name))
141         {
142             ret = m->authenticate(target_name, element_ptr,
143                                        user, group, password);
144             if (ret != YAZPROXY_RET_NOT_ME)
145                 break;
146         }
147     }
148     return ret;
149 }
150
151 int Yaz_ProxyModules::add_module(const char *fname)
152 {
153 #if HAVE_DLFCN_H
154     void *dl_handle = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
155     if (dl_handle)
156     {
157         Yaz_ProxyModule_entry *fl_ptr = 0;
158         fl_ptr = reinterpret_cast<Yaz_ProxyModule_entry *> 
159             (dlsym(dl_handle, "yazproxy_module"));
160         if (fl_ptr)
161         {
162             Yaz_ProxyModule *m = new Yaz_ProxyModule(dl_handle,
163                                                      fl_ptr,
164                                                      m_list);
165             m_list = m;
166
167             m_no_open++;
168             yaz_log(YLOG_LOG, "Loaded module no_open=%d", m_no_open);
169             return 0;
170         }
171         else
172         {
173             return -1;
174             dlclose(dl_handle);
175         }
176     }
177     else
178         return -1;
179 #else
180     return -1;
181 #endif
182 }
183