+ if (0)
+ { // debugging
+ printf("Added user '%s' -> password '%s'\n", buf, passwdp);
+ std::list<std::string>::const_iterator i;
+ for (i = tmp.dbs.begin(); i != tmp.dbs.end(); i++)
+ printf("db '%s'\n", (*i).c_str());
+ }
+ }
+ fclose(fp);
+}
+
+
+// I feel a little bad about the duplication of code between this and
+// config_userRegister(). But not bad enough to refactor.
+//
+void mp::filter::AuthSimple::config_targetRegister(std::string filename)
+{
+ FILE *fp = fopen(filename.c_str(), "r");
+ if (fp == 0)
+ die("can't open auth_simple target-register '" + filename + "': " +
+ strerror(errno));
+
+ char buf[1000];
+ while (fgets(buf, sizeof buf, fp)) {
+ if (*buf == '\n' || *buf == '#')
+ continue;
+ buf[strlen(buf)-1] = 0;
+ char *targetsp = strchr(buf, ':');
+ if (targetsp == 0)
+ die("auth_simple target-register '" + filename + "': " +
+ "no targets on line: '" + buf + "'");
+ *targetsp++ = 0;
+ std::list<std::string> tmp;
+ boost::split(tmp, targetsp, boost::is_any_of(","));
+ m_p->targetsByUser[buf] = tmp;
+
+ if (0) { // debugging
+ printf("Added user '%s' with targets:\n", buf);
+ std::list<std::string>::const_iterator i;
+ for (i = tmp.begin(); i != tmp.end(); i++) {
+ printf("\t%s\n", (*i).c_str());
+ }
+ }
+ }
+}
+
+
+void yf::AuthSimple::process(mp::Package &package) const
+{
+ Z_GDU *gdu = package.request().get();
+
+ if (!gdu || gdu->which != Z_GDU_Z3950) {
+ // Pass on the package -- This means that authentication is
+ // waived, which may not be the correct thing for non-Z APDUs
+ // as it means that SRW sessions don't demand authentication
+ return package.move();
+ }
+
+ if (m_p->got_userRegister) {
+ switch (gdu->u.z3950->which) {
+ case Z_APDU_initRequest: return process_init(package);
+ case Z_APDU_searchRequest: return process_search(package);
+ case Z_APDU_scanRequest: return process_scan(package);
+ // In theory, we should check database authorisation for
+ // extended services, too (A) the proxy currently does not
+ // implement XS and turns off its negotiation bit; (B) it
+ // would be insanely complex to do as the top-level XS request
+ // structure does not carry a database name, but it is buried
+ // down in some of the possible EXTERNALs used as
+ // taskSpecificParameters; and (C) since many extended
+ // services modify the database, we'd need to more exotic
+ // authorisation database than we want to support.
+ default: break;
+ }
+ }
+
+ if (m_p->got_targetRegister && gdu->u.z3950->which == Z_APDU_initRequest)
+ return check_targets(package);
+
+ // Operations other than those listed above do not require authorisation
+ return package.move();
+}
+
+
+static void reject_init(mp::Package &package, int err, const char *addinfo);
+
+
+void yf::AuthSimple::process_init(mp::Package &package) const
+{
+ Z_IdAuthentication *auth =
+ package.request().get()->u.z3950->u.initRequest->idAuthentication;
+ // This is just plain perverted.
+
+ if (!auth)
+ return reject_init(package, 0, "no credentials supplied");
+ if (auth->which != Z_IdAuthentication_idPass)
+ return reject_init(package, 0,
+ "only idPass authentication is supported");
+ Z_IdPass *idPass = auth->u.idPass;
+
+ if (m_p->userRegister.count(idPass->userId)) {
+ // groupId is ignored, in accordance with ancient tradition.
+ yf::AuthSimple::Rep::PasswordAndDBs pdbs =
+ m_p->userRegister[idPass->userId];
+ if (pdbs.password == idPass->password) {
+ // Success! Remember who the user is for future reference
+ {
+ boost::mutex::scoped_lock lock(m_p->mutex);
+ m_p->userBySession[package.session()] = idPass->userId;
+ }
+ return package.move();
+ }
+ }
+
+ return reject_init(package, 0, "username/password combination rejected");
+}
+
+
+// I find it unutterable disappointing that I have to provide this
+static bool contains(std::list<std::string> list, std::string thing) {
+ std::list<std::string>::const_iterator i;
+ for (i = list.begin(); i != list.end(); i++)
+ if (mp::util::database_name_normalize(*i) ==
+ mp::util::database_name_normalize(thing))
+ return true;
+
+ return false;
+}
+
+
+void yf::AuthSimple::process_search(mp::Package &package) const
+{
+ Z_SearchRequest *req =
+ package.request().get()->u.z3950->u.searchRequest;
+
+ if (m_p->userBySession.count(package.session()) == 0) {
+ // It's a non-authenticated session, so just accept the operation
+ return package.move();
+ }
+
+ std::string user = m_p->userBySession[package.session()];
+ yf::AuthSimple::Rep::PasswordAndDBs pdb = m_p->userRegister[user];
+ for (int i = 0; i < req->num_databaseNames; i++) {
+ if (!contains(pdb.dbs, req->databaseNames[i]) &&
+ !contains(pdb.dbs, "*")) {
+ // Make an Search rejection APDU
+ mp::odr odr;
+ Z_APDU *apdu = odr.create_searchResponse(
+ package.request().get()->u.z3950,
+ YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED,
+ req->databaseNames[i]);
+ package.response() = apdu;