+ 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();
+ }
+
+ 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;
+ }
+
+ // Operations other than those listed above do not require authorisation
+ return package.move();
+}
+
+
+static void reject_init(yp2::Package &package, const char *addinfo);
+
+
+void yf::AuthSimple::process_init(yp2::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, "no credentials supplied");
+ if (auth->which != Z_IdAuthentication_idPass)
+ return reject_init(package, "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, "username/password combination rejected");
+}