1 /* $Id: filter_auth_simple.cpp,v 1.3 2006-01-16 16:32:33 mike Exp $
2 Copyright (c) 2005, Index Data.
10 #include "package.hpp"
12 #include <boost/thread/mutex.hpp>
15 #include "filter_auth_simple.hpp"
21 namespace yf = yp2::filter;
25 class AuthSimple::Rep {
26 friend class AuthSimple;
27 typedef std::map<std::string, std::string> userpass;
33 yf::AuthSimple::AuthSimple() : m_p(new Rep)
38 yf::AuthSimple::~AuthSimple()
39 { // must have a destructor because of boost::scoped_ptr
43 void reject(yp2::Package &package, const char *addinfo) {
44 // Make an Init rejection APDU
45 Z_GDU *gdu = package.request().get();
47 Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 1014, addinfo);
48 apdu->u.initResponse->implementationName = "YP2/YAZ";
49 *apdu->u.initResponse->result = 0; // reject
50 package.response() = apdu;
51 package.session().close();
55 void yf::AuthSimple::process(yp2::Package &package) const
57 Z_GDU *gdu = package.request().get();
59 if (!gdu || gdu->which != Z_GDU_Z3950 ||
60 gdu->u.z3950->which != Z_APDU_initRequest) {
61 // pass on package -- I think that means authentication is
62 // accepted which may not be the correct thing for non-Z APDUs
63 // as it means that SRW sessions don't demand authentication
64 return package.move();
67 Z_IdAuthentication *auth = gdu->u.z3950->u.initRequest->idAuthentication;
69 return reject(package, "no credentials supplied");
70 if (auth->which != Z_IdAuthentication_idPass)
71 return reject(package, "only idPass authentication is supported");
72 Z_IdPass *idPass = auth->u.idPass;
73 // groupId is ignored, in accordance with ancient tradition
74 if (m_p->reg[idPass->userId] == idPass->password) {
75 // Success! Should the authentication information now be
76 // altered or deleted? That could be configurable.
77 return package.move();
79 return reject(package, "username/password combination rejected");
83 // Read XML config.. Put config info in m_p.
84 void yp2::filter::AuthSimple::configure(const xmlNode * ptr)
87 bool got_filename = false;
89 for (ptr = ptr->children; ptr != 0; ptr = ptr->next) {
90 if (ptr->type != XML_ELEMENT_NODE)
92 if (!strcmp((const char *) ptr->name, "filename")) {
93 filename = yp2::xml::get_text(ptr);
96 throw yp2::filter::FilterException("Bad element in auth_simple: "
97 + std::string((const char *)
103 throw yp2::filter::FilterException("auth_simple: no user-register "
104 "filename specified");
106 FILE *fp = fopen(filename.c_str(), "r");
108 throw yp2::filter::FilterException("can't open auth_simple "
109 "user-register '" + filename +
110 "': " + strerror(errno));
113 while (fgets(buf, sizeof buf, fp)) {
114 if (*buf == '\n' || *buf == '#')
116 buf[strlen(buf)-1] = 0;
117 char *cp = strchr(buf, ':');
119 throw yp2::filter::FilterException("auth_simple user-register '" +
120 filename + "': bad line: " +
124 //printf("Added user '%s' -> password '%s'\n", buf, cp);
128 static yp2::filter::Base* filter_creator()
130 return new yp2::filter::AuthSimple;
134 struct yp2_filter_struct yp2_filter_auth_simple = {
145 * indent-tabs-mode: nil
146 * c-file-style: "stroustrup"
148 * vim: shiftwidth=4 tabstop=8 expandtab