]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
SourceLayout: Basic auth: shuffle helper request functions
authorAmos Jeffries <squid3@treenet.co.nz>
Sat, 30 Jul 2011 01:34:25 +0000 (19:34 -0600)
committerAmos Jeffries <squid3@treenet.co.nz>
Sat, 30 Jul 2011 01:34:25 +0000 (19:34 -0600)
helper lookup functions are AuthBasicUserRequest members. Should be in
that classes .cc file.

Also merge submit and queue functions. no need to be separate.

src/auth/basic/User.h
src/auth/basic/UserRequest.cc
src/auth/basic/UserRequest.h
src/auth/basic/auth_basic.cc
src/auth/basic/auth_basic.h

index b2882e93764d443a5a655abb85343a518554ce5a..a2528bf1681f9ed02b03b4454dd4a253e057d937 100644 (file)
@@ -23,9 +23,6 @@ public:
     User(Auth::Config *);
     ~User();
     bool authenticated() const;
-    void queueRequest(AuthUserRequest::Pointer auth_user_request, RH * handler, void *data);
-    void submitRequest(AuthUserRequest::Pointer auth_user_request, RH * handler, void *data);
-
     bool valid() const;
 
     /** Update the cached password for a username. */
index bb2d5bedf70424a3466a66deab81c1f0c53084e9..1d2ceee1baa86fbc34aff2a565fad9c6aab7969c 100644 (file)
@@ -2,8 +2,15 @@
 #include "auth/basic/auth_basic.h"
 #include "auth/basic/User.h"
 #include "auth/basic/UserRequest.h"
+#include "auth/State.h"
+#include "charset.h"
+#include "rfc1738.h"
 #include "SquidTime.h"
 
+#if !defined(HELPER_INPUT_BUFFER)
+#define HELPER_INPUT_BUFFER  8192
+#endif
+
 int
 AuthBasicUserRequest::authenticated() const
 {
@@ -86,10 +93,98 @@ AuthBasicUserRequest::module_start(RH * handler, void *data)
     /* check to see if the auth_user already has a request outstanding */
     if (user()->credentials() == Auth::Pending) {
         /* there is a request with the same credentials already being verified */
-        basic_auth->queueRequest(this, handler, data);
+
+        BasicAuthQueueNode *node = static_cast<BasicAuthQueueNode *>(xcalloc(1, sizeof(BasicAuthQueueNode)));
+        assert(node);
+        node->auth_user_request = this;
+        node->handler = handler;
+        node->data = cbdataReference(data);
+
+        /* queue this validation request to be infored of the pending lookup results */
+        node->next = basic_auth->auth_queue;
+        basic_auth->auth_queue = node;
         return;
     }
-
-    basic_auth->submitRequest(this, handler, data);
+    // otherwise submit this request to the auth helper(s) for validation
+
+    /* mark this user as having verification in progress */
+    user()->credentials(Auth::Pending);
+    char buf[HELPER_INPUT_BUFFER];
+    static char username[HELPER_INPUT_BUFFER];
+    static char pass[HELPER_INPUT_BUFFER];
+    if (static_cast<Auth::Basic::Config*>(user()->config)->utf8) {
+        latin1_to_utf8(username, sizeof(username), user()->username());
+        latin1_to_utf8(pass, sizeof(pass), basic_auth->passwd);
+        xstrncpy(username, rfc1738_escape(username), sizeof(username));
+        xstrncpy(pass, rfc1738_escape(pass), sizeof(pass));
+    } else {
+        xstrncpy(username, rfc1738_escape(user()->username()), sizeof(username));
+        xstrncpy(pass, rfc1738_escape(basic_auth->passwd), sizeof(pass));
+    }
+    int sz = snprintf(buf, sizeof(buf), "%s %s\n", username, pass);
+    if (sz<=0) {
+        debugs(9, DBG_CRITICAL, "ERROR: Basic Authentication Failure. Can not build helper validation request.");
+        handler(data, NULL);
+    } else if (sz>=sizeof(buf)) {
+        debugs(9, DBG_CRITICAL, "ERROR: Basic Authentication Failure. user:password exceeds " << sizeof(buf) << " bytes.");
+        handler(data, NULL);
+    } else
+        helperSubmit(basicauthenticators, buf, AuthBasicUserRequest::HandleReply,
+                     new Auth::StateData(this, handler, data));
 }
 
+void
+AuthBasicUserRequest::HandleReply(void *data, char *reply)
+{
+    Auth::StateData *r = static_cast<Auth::StateData *>(data);
+    BasicAuthQueueNode *tmpnode;
+    char *t = NULL;
+    void *cbdata;
+    debugs(29, 9, HERE << "{" << (reply ? reply : "<NULL>") << "}");
+
+    if (reply) {
+        if ((t = strchr(reply, ' ')))
+            *t++ = '\0';
+
+        if (*reply == '\0')
+            reply = NULL;
+    }
+
+    assert(r->auth_user_request != NULL);
+    assert(r->auth_user_request->user()->auth_type == Auth::AUTH_BASIC);
+
+    /* this is okay since we only play with the Auth::Basic::User child fields below
+     * and dont pass the pointer itself anywhere */
+    Auth::Basic::User *basic_auth = dynamic_cast<Auth::Basic::User *>(r->auth_user_request->user().getRaw());
+
+    assert(basic_auth != NULL);
+
+    if (reply && (strncasecmp(reply, "OK", 2) == 0))
+        basic_auth->credentials(Auth::Ok);
+    else {
+        basic_auth->credentials(Auth::Failed);
+
+        if (t && *t)
+            r->auth_user_request->setDenyMessage(t);
+    }
+
+    basic_auth->expiretime = squid_curtime;
+
+    if (cbdataReferenceValidDone(r->data, &cbdata))
+        r->handler(cbdata, NULL);
+
+    cbdataReferenceDone(r->data);
+
+    while (basic_auth->auth_queue) {
+        tmpnode = basic_auth->auth_queue->next;
+
+        if (cbdataReferenceValidDone(basic_auth->auth_queue->data, &cbdata))
+            basic_auth->auth_queue->handler(cbdata, NULL);
+
+        xfree(basic_auth->auth_queue);
+
+        basic_auth->auth_queue = tmpnode;
+    }
+
+    delete r;
+}
index ed9f797e0d495c9855f94bdd88585883c9df5620..a24cd8ae7406683cd219b474921c571d34f79218 100644 (file)
@@ -22,6 +22,9 @@ public:
     virtual void authenticate(HttpRequest * request, ConnStateData *conn, http_hdr_type type);
     virtual Auth::Direction module_direction();
     virtual void module_start(RH *, void *);
+
+private:
+    static HLPCB HandleReply;
 };
 
 MEMPROXY_CLASS_INLINE(AuthBasicUserRequest);
index 946ed93ebe24af90496419e77e1d6d67b26e2f52..85ae452b8ec51a593879a9fdcd6852eed4f45b5d 100644 (file)
 #include "SquidTime.h"
 
 /* Basic Scheme */
-static HLPCB authenticateBasicHandleReply;
 static AUTHSSTATS authenticateBasicStats;
 
-static helper *basicauthenticators = NULL;
+helper *basicauthenticators = NULL;
 
 static int authbasic_initialised = 0;
 
@@ -135,62 +134,6 @@ Auth::Basic::Config::done()
         safe_free(basicAuthRealm);
 }
 
-static void
-authenticateBasicHandleReply(void *data, char *reply)
-{
-    Auth::StateData *r = static_cast<Auth::StateData *>(data);
-    BasicAuthQueueNode *tmpnode;
-    char *t = NULL;
-    void *cbdata;
-    debugs(29, 9, HERE << "{" << (reply ? reply : "<NULL>") << "}");
-
-    if (reply) {
-        if ((t = strchr(reply, ' ')))
-            *t++ = '\0';
-
-        if (*reply == '\0')
-            reply = NULL;
-    }
-
-    assert(r->auth_user_request != NULL);
-    assert(r->auth_user_request->user()->auth_type == Auth::AUTH_BASIC);
-
-    /* this is okay since we only play with the Auth::Basic::User child fields below
-     * and dont pass the pointer itself anywhere */
-    Auth::Basic::User *basic_auth = dynamic_cast<Auth::Basic::User *>(r->auth_user_request->user().getRaw());
-
-    assert(basic_auth != NULL);
-
-    if (reply && (strncasecmp(reply, "OK", 2) == 0))
-        basic_auth->credentials(Auth::Ok);
-    else {
-        basic_auth->credentials(Auth::Failed);
-
-        if (t && *t)
-            r->auth_user_request->setDenyMessage(t);
-    }
-
-    basic_auth->expiretime = squid_curtime;
-
-    if (cbdataReferenceValidDone(r->data, &cbdata))
-        r->handler(cbdata, NULL);
-
-    cbdataReferenceDone(r->data);
-
-    while (basic_auth->auth_queue) {
-        tmpnode = basic_auth->auth_queue->next;
-
-        if (cbdataReferenceValidDone(basic_auth->auth_queue->data, &cbdata))
-            basic_auth->auth_queue->handler(cbdata, NULL);
-
-        xfree(basic_auth->auth_queue);
-
-        basic_auth->auth_queue = tmpnode;
-    }
-
-    delete r;
-}
-
 void
 Auth::Basic::Config::dump(StoreEntry * entry, const char *name, Auth::Config * scheme)
 {
@@ -426,40 +369,3 @@ Auth::Basic::Config::registerWithCacheManager(void)
                         "Basic User Authenticator Stats",
                         authenticateBasicStats, 0, 1);
 }
-
-// XXX: this is a auth management function. Surely not in scope for the credentials storage object
-void
-Auth::Basic::User::queueRequest(AuthUserRequest::Pointer auth_user_request, RH * handler, void *data)
-{
-    BasicAuthQueueNode *node;
-    node = static_cast<BasicAuthQueueNode *>(xcalloc(1, sizeof(BasicAuthQueueNode)));
-    assert(node);
-    /* save the details */
-    node->next = auth_queue;
-    auth_queue = node;
-    node->auth_user_request = auth_user_request;
-    node->handler = handler;
-    node->data = cbdataReference(data);
-}
-
-// XXX: this is a auth management function. Surely not in scope for the credentials storage object
-void
-Auth::Basic::User::submitRequest(AuthUserRequest::Pointer auth_user_request, RH * handler, void *data)
-{
-    /* mark the user as having verification in progress */
-    credentials(Auth::Pending);
-    char buf[8192];
-    char user[1024], pass[1024];
-    if (static_cast<Auth::Basic::Config*>(config)->utf8) {
-        latin1_to_utf8(user, sizeof(user), username());
-        latin1_to_utf8(pass, sizeof(pass), passwd);
-        xstrncpy(user, rfc1738_escape(user), sizeof(user));
-        xstrncpy(pass, rfc1738_escape(pass), sizeof(pass));
-    } else {
-        xstrncpy(user, rfc1738_escape(username()), sizeof(user));
-        xstrncpy(pass, rfc1738_escape(passwd), sizeof(pass));
-    }
-    snprintf(buf, sizeof(buf), "%s %s\n", user, pass);
-    helperSubmit(basicauthenticators, buf, authenticateBasicHandleReply,
-                 new Auth::StateData(auth_user_request, handler, data));
-}
index 01d9e4e467f6c3756871486f72ab682f494dc074..475b547fc8627d42e87913399af0c0f91e9d4cbb 100644 (file)
@@ -61,4 +61,6 @@ private:
 } // namespace Basic
 } // namespace Auth
 
+extern helper *basicauthenticators;
+
 #endif /* __AUTH_BASIC_H__ */