Added files and initial auth_simple filter
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 12 Jan 2006 10:04:34 +0000 (10:04 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 12 Jan 2006 10:04:34 +0000 (10:04 +0000)
etc/config2.xml
src/Makefile.am
src/factory_static.cpp
src/filter_auth_simple.cpp [new file with mode: 0644]
src/filter_auth_simple.hpp [new file with mode: 0644]
src/test_filter_auth_simple.cpp [new file with mode: 0644]

index d6f4338..39c7166 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<!-- $Id: config2.xml,v 1.1 2006-01-11 11:51:49 adam Exp $ -->
+<!-- $Id: config2.xml,v 1.2 2006-01-12 10:04:34 adam Exp $ -->
 <yp2 xmlns="http://indexdata.dk/yp2/config/1">
   <start route="start"/>
   <filters>
@@ -14,6 +14,7 @@
       <filter type="log">
         <message>F</message>
       </filter>
+      <filter type="auth_simple"/>
       <filter type="virt_db">
         <virtual>
           <database>loc</database>
index bc0262b..4d114b8 100644 (file)
@@ -1,4 +1,4 @@
-## $Id: Makefile.am,v 1.41 2006-01-09 13:43:59 adam Exp $
+## $Id: Makefile.am,v 1.42 2006-01-12 10:04:34 adam Exp $
 
 MAINTAINERCLEANFILES = Makefile.in config.in config.hpp
 
@@ -18,6 +18,7 @@ libyp2_la_SOURCES = \
         router_flexml.hpp router_flexml.cpp \
        thread_pool_observer.cpp thread_pool_observer.hpp \
        filter.hpp filter.cpp factory_filter.cpp factory_filter.hpp \
+       filter_auth_simple.cpp filter_auth_simple.hpp \
        filter_frontend_net.cpp filter_frontend_net.hpp \
        filter_log.cpp filter_log.hpp \
        filter_virt_db.cpp filter_virt_db.hpp \
@@ -57,6 +58,7 @@ check_PROGRAMS = \
        test_session1 test_session2 \
        test_thread_pool_observer \
        test_boost_threads test_boost_time \
+       test_filter_auth_simple \
        test_filter_factory \
        test_filter_frontend_net \
        test_filter_log \
@@ -77,8 +79,8 @@ test_session2_SOURCES=test_session2.cpp
 test_boost_threads_SOURCES=test_boost_threads.cpp
 test_boost_time_SOURCES=test_boost_time.cpp
 test_thread_pool_observer_SOURCES = test_thread_pool_observer.cpp
+test_filter_auth_simple_SOURCES = test_filter_auth_simple.cpp
 test_filter_factory_SOURCES = test_filter_factory.cpp
-test_filter_factory_LDFLAGS = -export-dynamic
 test_filter_frontend_net_SOURCES = test_filter_frontend_net.cpp
 test_filter_log_SOURCES = test_filter_log.cpp
 test_filter_z3950_client_SOURCES = test_filter_z3950_client.cpp
@@ -98,6 +100,7 @@ test_session2_LDADD = $(TESTLDADD)
 test_boost_threads_LDADD = $(TESTLDADD)
 test_boost_time_LDADD = $(TESTLDADD)
 test_thread_pool_observer_LDADD = $(TESTLDADD)
+test_filter_auth_simple_LDADD = $(TESTLDADD)
 test_filter_factory_LDADD = $(TESTLDADD)
 test_filter_frontend_net_LDADD = $(TESTLDADD)
 test_filter_log_LDADD = $(TESTLDADD)
index 0c01970..2b95aa9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: factory_static.cpp,v 1.4 2006-01-04 14:30:51 adam Exp $
+/* $Id: factory_static.cpp,v 1.5 2006-01-12 10:04:34 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -16,6 +16,7 @@
 
 #include "factory_filter.hpp"
 
+#include "filter_auth_simple.hpp"
 #include "filter_backend_test.hpp"
 #include "filter_frontend_net.hpp"
 #include "filter_log.hpp"
@@ -27,6 +28,7 @@
 yp2::FactoryStatic::FactoryStatic()
 {
     struct yp2_filter_struct *buildins[] = {
+        &yp2_filter_auth_simple,
         &yp2_filter_backend_test,
         &yp2_filter_frontend_net,        
         &yp2_filter_log,
diff --git a/src/filter_auth_simple.cpp b/src/filter_auth_simple.cpp
new file mode 100644 (file)
index 0000000..d5a4c82
--- /dev/null
@@ -0,0 +1,98 @@
+/* $Id: filter_auth_simple.cpp,v 1.1 2006-01-12 10:04:34 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#include "config.hpp"
+
+#include "filter.hpp"
+#include "package.hpp"
+
+#include <boost/thread/mutex.hpp>
+
+#include "util.hpp"
+#include "filter_auth_simple.hpp"
+
+#include <yaz/zgdu.h>
+
+namespace yf = yp2::filter;
+
+namespace yp2 {
+    namespace filter {
+        class AuthSimple::Rep {
+            friend class AuthSimple;
+            int dummy; // private data
+        };
+    }
+}
+
+yf::AuthSimple::AuthSimple() : m_p(new Rep)
+{
+    m_p->dummy = 1;
+}
+
+yf::AuthSimple::~AuthSimple()
+{  // must have a destructor because of boost::scoped_ptr
+}
+
+void yf::AuthSimple::process(yp2::Package &package) const
+{
+    Z_GDU *gdu = package.request().get();
+
+    if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
+        Z_APDU_initRequest)
+    {
+        // we have a Z39.50 init request
+        Z_InitRequest *init = gdu->u.z3950->u.initRequest;
+
+        // for now reject if we don't supply _some_ auth
+        if (!init->idAuthentication)
+        {
+            yp2::odr odr;
+            Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
+            
+            apdu->u.initResponse->implementationName = "YP2/YAZ";
+            *apdu->u.initResponse->result = 0; // reject
+            
+            package.response() = apdu;
+
+            package.session().close();
+            return;
+        }
+        // if we get here access is granted..
+
+        // should authentication be altered of deleted?
+        // that could be configurable..
+    }
+    package.move();  // pass on package
+}
+
+void yp2::filter::AuthSimple::configure(const xmlNode * ptr)
+{
+    // Read XML config.. Put config info in m_p..
+    m_p->dummy = 1;  
+}
+
+static yp2::filter::Base* filter_creator()
+{
+    return new yp2::filter::AuthSimple;
+}
+
+extern "C" {
+    struct yp2_filter_struct yp2_filter_auth_simple = {
+        0,
+        "auth_simple",
+        filter_creator
+    };
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
diff --git a/src/filter_auth_simple.hpp b/src/filter_auth_simple.hpp
new file mode 100644 (file)
index 0000000..efe20b8
--- /dev/null
@@ -0,0 +1,40 @@
+/* $Id: filter_auth_simple.hpp,v 1.1 2006-01-12 10:04:34 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#ifndef FILTER_AUTH_SIMPLE_HPP
+#define FILTER_AUTH_SIMPLE_HPP
+
+#include <boost/scoped_ptr.hpp>
+
+#include "filter.hpp"
+
+namespace yp2 {
+    namespace filter {
+        class AuthSimple : public Base {
+            class Rep;
+            boost::scoped_ptr<Rep> m_p;
+        public:
+            AuthSimple();
+            ~AuthSimple();
+            void process(yp2::Package & package) const;
+            void configure(const xmlNode * ptr);
+        };
+    }
+}
+
+extern "C" {
+    extern struct yp2_filter_struct yp2_filter_auth_simple;
+}
+
+#endif
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
diff --git a/src/test_filter_auth_simple.cpp b/src/test_filter_auth_simple.cpp
new file mode 100644 (file)
index 0000000..9517523
--- /dev/null
@@ -0,0 +1,104 @@
+/* $Id: test_filter_auth_simple.cpp,v 1.1 2006-01-12 10:04:34 adam Exp $
+   Copyright (c) 2005, Index Data.
+
+%LICENSE%
+ */
+
+#include "config.hpp"
+#include <iostream>
+#include <stdexcept>
+
+#include "filter_auth_simple.hpp"
+#include "util.hpp"
+#include "router_chain.hpp"
+#include "session.hpp"
+#include "package.hpp"
+
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+
+using namespace boost::unit_test;
+
+class FilterBounceInit: public yp2::filter::Base {
+public:
+    void process(yp2::Package & package) const {
+        
+        if (package.session().is_closed())
+        {
+            // std::cout << "Got Close.\n";
+        }
+       
+        Z_GDU *gdu = package.request().get();
+        if (gdu)
+        {
+            // std::cout << "Got PDU. Sending init response\n";
+            yp2::odr odr;
+            Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
+            
+            apdu->u.initResponse->implementationName = "YP2/YAZ";
+            
+            package.response() = apdu;
+        }
+        package.move();
+    };
+};
+
+
+BOOST_AUTO_UNIT_TEST( test_filter_auth_simple_1 )
+{
+    try 
+    {
+        yp2::filter::AuthSimple lf;
+    }
+    catch ( ... ) {
+        BOOST_CHECK (false);
+    }
+}
+
+BOOST_AUTO_UNIT_TEST( test_filter_auth_simple2 )
+{
+    try 
+    {
+        yp2::RouterChain router;
+        
+        yp2::filter::AuthSimple auth;
+        FilterBounceInit bounce;
+        
+        router.append(auth);
+        router.append(bounce);
+        
+        // Create package with Z39.50 init request in it
+        yp2::Package pack;
+        
+        yp2::odr odr;
+        Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
+        
+        pack.request() = apdu;
+        // Done creating query. 
+        
+        // Put it in router
+        pack.router(router).move(); 
+        
+        // Inspect that we got Z39.50 init response
+        yazpp_1::GDU *gdu = &pack.response();
+        
+        Z_GDU *z_gdu = gdu->get();
+        BOOST_CHECK(z_gdu);
+        if (z_gdu) {
+            BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
+            BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
+        }
+    }
+    catch ( ... ) {
+        BOOST_CHECK (false);
+    }
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * c-file-style: "stroustrup"
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */