]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/basic/UserRequest.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "auth/basic/Config.h"
11 #include "auth/basic/User.h"
12 #include "auth/basic/UserRequest.h"
13 #include "auth/QueueNode.h"
14 #include "auth/State.h"
17 #include "format/Format.h"
19 #include "helper/Reply.h"
21 #include "HttpRequest.h"
24 #include "SquidTime.h"
26 #if !defined(HELPER_INPUT_BUFFER)
27 #define HELPER_INPUT_BUFFER 8192
31 Auth::Basic::UserRequest::authenticated() const
33 Auth::Basic::User
const *basic_auth
= dynamic_cast<Auth::Basic::User
const *>(user().getRaw());
35 if (basic_auth
&& basic_auth
->authenticated())
42 Auth::Basic::UserRequest::credentialsStr()
44 Auth::Basic::User
const *basic_auth
= dynamic_cast<Auth::Basic::User
const *>(user().getRaw());
46 return basic_auth
->passwd
;
50 /* log a basic user in
53 Auth::Basic::UserRequest::authenticate(HttpRequest
* request
, ConnStateData
* conn
, http_hdr_type type
)
55 assert(user() != NULL
);
57 /* if the password is not ok, do an identity */
58 if (!user() || user()->credentials() != Auth::Ok
)
61 /* are we about to recheck the credentials externally? */
62 if ((user()->expiretime
+ static_cast<Auth::Basic::Config
*>(Auth::Config::Find("basic"))->credentialsTTL
) <= squid_curtime
) {
63 debugs(29, 4, HERE
<< "credentials expired - rechecking");
67 /* we have been through the external helper, and the credentials haven't expired */
68 debugs(29, 9, HERE
<< "user '" << user()->username() << "' authenticated");
70 /* Decode now takes care of finding the AuthUser struct in the cache */
71 /* after external auth occurs anyway */
72 user()->expiretime
= current_time
.tv_sec
;
78 Auth::Basic::UserRequest::module_direction()
80 /* null auth_user is checked for by Auth::UserRequest::direction() */
81 if (user()->auth_type
!= Auth::AUTH_BASIC
)
82 return Auth::CRED_ERROR
;
84 switch (user()->credentials()) {
88 return Auth::CRED_LOOKUP
;
91 if (user()->expiretime
+ static_cast<Auth::Basic::Config
*>(Auth::Config::Find("basic"))->credentialsTTL
<= squid_curtime
)
92 return Auth::CRED_LOOKUP
;
93 return Auth::CRED_VALID
;
96 return Auth::CRED_VALID
;
99 return Auth::CRED_ERROR
;
103 /* send the initial data to a basic authenticator module */
105 Auth::Basic::UserRequest::startHelperLookup(HttpRequest
*request
, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
107 assert(user()->auth_type
== Auth::AUTH_BASIC
);
108 Auth::Basic::User
*basic_auth
= dynamic_cast<Auth::Basic::User
*>(user().getRaw());
109 assert(basic_auth
!= NULL
);
110 debugs(29, 9, HERE
<< "'" << basic_auth
->username() << ":" << basic_auth
->passwd
<< "'");
112 if (static_cast<Auth::Basic::Config
*>(Auth::Config::Find("basic"))->authenticateProgram
== NULL
) {
113 debugs(29, DBG_CRITICAL
, "ERROR: No Basic authentication program configured.");
118 /* check to see if the auth_user already has a request outstanding */
119 if (user()->credentials() == Auth::Pending
) {
120 /* there is a request with the same credentials already being verified */
122 Auth::QueueNode
*node
= new Auth::QueueNode(this, handler
, data
);
124 /* queue this validation request to be infored of the pending lookup results */
125 node
->next
= basic_auth
->queue
;
126 basic_auth
->queue
= node
;
129 // otherwise submit this request to the auth helper(s) for validation
131 /* mark this user as having verification in progress */
132 user()->credentials(Auth::Pending
);
133 char buf
[HELPER_INPUT_BUFFER
];
134 static char usern
[HELPER_INPUT_BUFFER
];
135 static char pass
[HELPER_INPUT_BUFFER
];
136 if (static_cast<Auth::Basic::Config
*>(user()->config
)->utf8
) {
137 latin1_to_utf8(usern
, sizeof(usern
), user()->username());
138 latin1_to_utf8(pass
, sizeof(pass
), basic_auth
->passwd
);
139 xstrncpy(usern
, rfc1738_escape(usern
), sizeof(usern
));
140 xstrncpy(pass
, rfc1738_escape(pass
), sizeof(pass
));
142 xstrncpy(usern
, rfc1738_escape(user()->username()), sizeof(usern
));
143 xstrncpy(pass
, rfc1738_escape(basic_auth
->passwd
), sizeof(pass
));
146 if (const char *keyExtras
= helperRequestKeyExtras(request
, al
))
147 sz
= snprintf(buf
, sizeof(buf
), "%s %s %s\n", usern
, pass
, keyExtras
);
149 sz
= snprintf(buf
, sizeof(buf
), "%s %s\n", usern
, pass
);
152 debugs(9, DBG_CRITICAL
, "ERROR: Basic Authentication Failure. Can not build helper validation request.");
154 } else if (static_cast<size_t>(sz
) >= sizeof(buf
)) {
155 debugs(9, DBG_CRITICAL
, "ERROR: Basic Authentication Failure. user:password exceeds " << sizeof(buf
) << " bytes.");
158 helperSubmit(basicauthenticators
, buf
, Auth::Basic::UserRequest::HandleReply
,
159 new Auth::StateData(this, handler
, data
));
163 Auth::Basic::UserRequest::HandleReply(void *data
, const Helper::Reply
&reply
)
165 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
167 debugs(29, 5, HERE
<< "reply=" << reply
);
169 assert(r
->auth_user_request
!= NULL
);
170 assert(r
->auth_user_request
->user()->auth_type
== Auth::AUTH_BASIC
);
172 // add new helper kv-pair notes to the credentials object
173 // so that any transaction using those credentials can access them
174 r
->auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
176 /* this is okay since we only play with the Auth::Basic::User child fields below
177 * and dont pass the pointer itself anywhere */
178 Auth::Basic::User
*basic_auth
= dynamic_cast<Auth::Basic::User
*>(r
->auth_user_request
->user().getRaw());
180 assert(basic_auth
!= NULL
);
182 if (reply
.result
== Helper::Okay
)
183 basic_auth
->credentials(Auth::Ok
);
185 basic_auth
->credentials(Auth::Failed
);
187 if (reply
.other().hasContent())
188 r
->auth_user_request
->setDenyMessage(reply
.other().content());
191 basic_auth
->expiretime
= squid_curtime
;
193 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
196 cbdataReferenceDone(r
->data
);
198 while (basic_auth
->queue
) {
199 if (cbdataReferenceValidDone(basic_auth
->queue
->data
, &cbdata
))
200 basic_auth
->queue
->handler(cbdata
);
202 Auth::QueueNode
*tmpnode
= basic_auth
->queue
->next
;
203 basic_auth
->queue
->next
= NULL
;
204 delete basic_auth
->queue
;
206 basic_auth
->queue
= tmpnode
;