]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/basic/UserRequest.cc
2 #include "auth/basic/auth_basic.h"
3 #include "auth/basic/User.h"
4 #include "auth/basic/UserRequest.h"
5 #include "auth/QueueNode.h"
6 #include "auth/State.h"
9 #include "format/Format.h"
10 #include "HelperReply.h"
12 #include "HttpRequest.h"
15 #include "SquidTime.h"
17 #if !defined(HELPER_INPUT_BUFFER)
18 #define HELPER_INPUT_BUFFER 8192
22 Auth::Basic::UserRequest::authenticated() const
24 Auth::Basic::User
const *basic_auth
= dynamic_cast<Auth::Basic::User
const *>(user().getRaw());
26 if (basic_auth
&& basic_auth
->authenticated())
33 Auth::Basic::UserRequest::credentialsStr()
35 Auth::Basic::User
const *basic_auth
= dynamic_cast<Auth::Basic::User
const *>(user().getRaw());
37 return basic_auth
->passwd
;
41 /* log a basic user in
44 Auth::Basic::UserRequest::authenticate(HttpRequest
* request
, ConnStateData
* conn
, http_hdr_type type
)
46 assert(user() != NULL
);
48 /* if the password is not ok, do an identity */
49 if (!user() || user()->credentials() != Auth::Ok
)
52 /* are we about to recheck the credentials externally? */
53 if ((user()->expiretime
+ static_cast<Auth::Basic::Config
*>(Auth::Config::Find("basic"))->credentialsTTL
) <= squid_curtime
) {
54 debugs(29, 4, HERE
<< "credentials expired - rechecking");
58 /* we have been through the external helper, and the credentials haven't expired */
59 debugs(29, 9, HERE
<< "user '" << user()->username() << "' authenticated");
61 /* Decode now takes care of finding the AuthUser struct in the cache */
62 /* after external auth occurs anyway */
63 user()->expiretime
= current_time
.tv_sec
;
69 Auth::Basic::UserRequest::module_direction()
71 /* null auth_user is checked for by Auth::UserRequest::direction() */
72 if (user()->auth_type
!= Auth::AUTH_BASIC
)
73 return Auth::CRED_ERROR
;
75 switch (user()->credentials()) {
79 return Auth::CRED_LOOKUP
;
82 if (user()->expiretime
+ static_cast<Auth::Basic::Config
*>(Auth::Config::Find("basic"))->credentialsTTL
<= squid_curtime
)
83 return Auth::CRED_LOOKUP
;
84 return Auth::CRED_VALID
;
87 return Auth::CRED_VALID
;
90 return Auth::CRED_ERROR
;
94 /* send the initial data to a basic authenticator module */
96 Auth::Basic::UserRequest::module_start(HttpRequest
*request
, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
98 assert(user()->auth_type
== Auth::AUTH_BASIC
);
99 Auth::Basic::User
*basic_auth
= dynamic_cast<Auth::Basic::User
*>(user().getRaw());
100 assert(basic_auth
!= NULL
);
101 debugs(29, 9, HERE
<< "'" << basic_auth
->username() << ":" << basic_auth
->passwd
<< "'");
103 if (static_cast<Auth::Basic::Config
*>(Auth::Config::Find("basic"))->authenticateProgram
== NULL
) {
104 debugs(29, DBG_CRITICAL
, "ERROR: No Basic authentication program configured.");
109 /* check to see if the auth_user already has a request outstanding */
110 if (user()->credentials() == Auth::Pending
) {
111 /* there is a request with the same credentials already being verified */
113 Auth::QueueNode
*node
= new Auth::QueueNode(this, handler
, data
);
115 /* queue this validation request to be infored of the pending lookup results */
116 node
->next
= basic_auth
->queue
;
117 basic_auth
->queue
= node
;
120 // otherwise submit this request to the auth helper(s) for validation
122 /* mark this user as having verification in progress */
123 user()->credentials(Auth::Pending
);
124 char buf
[HELPER_INPUT_BUFFER
];
125 static char usern
[HELPER_INPUT_BUFFER
];
126 static char pass
[HELPER_INPUT_BUFFER
];
127 if (static_cast<Auth::Basic::Config
*>(user()->config
)->utf8
) {
128 latin1_to_utf8(usern
, sizeof(usern
), user()->username());
129 latin1_to_utf8(pass
, sizeof(pass
), basic_auth
->passwd
);
130 xstrncpy(usern
, rfc1738_escape(usern
), sizeof(usern
));
131 xstrncpy(pass
, rfc1738_escape(pass
), sizeof(pass
));
133 xstrncpy(usern
, rfc1738_escape(user()->username()), sizeof(usern
));
134 xstrncpy(pass
, rfc1738_escape(basic_auth
->passwd
), sizeof(pass
));
137 if (const char *keyExtras
= helperRequestKeyExtras(request
, al
))
138 sz
= snprintf(buf
, sizeof(buf
), "%s %s %s\n", usern
, pass
, keyExtras
);
140 sz
= snprintf(buf
, sizeof(buf
), "%s %s\n", usern
, pass
);
143 debugs(9, DBG_CRITICAL
, "ERROR: Basic Authentication Failure. Can not build helper validation request.");
145 } else if (static_cast<size_t>(sz
) >= sizeof(buf
)) {
146 debugs(9, DBG_CRITICAL
, "ERROR: Basic Authentication Failure. user:password exceeds " << sizeof(buf
) << " bytes.");
149 helperSubmit(basicauthenticators
, buf
, Auth::Basic::UserRequest::HandleReply
,
150 new Auth::StateData(this, handler
, data
));
154 Auth::Basic::UserRequest::HandleReply(void *data
, const HelperReply
&reply
)
156 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
158 debugs(29, 5, HERE
<< "reply=" << reply
);
160 assert(r
->auth_user_request
!= NULL
);
161 assert(r
->auth_user_request
->user()->auth_type
== Auth::AUTH_BASIC
);
163 // add new helper kv-pair notes to the credentials object
164 // so that any transaction using those credentials can access them
165 r
->auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
167 /* this is okay since we only play with the Auth::Basic::User child fields below
168 * and dont pass the pointer itself anywhere */
169 Auth::Basic::User
*basic_auth
= dynamic_cast<Auth::Basic::User
*>(r
->auth_user_request
->user().getRaw());
171 assert(basic_auth
!= NULL
);
173 if (reply
.result
== HelperReply::Okay
)
174 basic_auth
->credentials(Auth::Ok
);
176 basic_auth
->credentials(Auth::Failed
);
178 if (reply
.other().hasContent())
179 r
->auth_user_request
->setDenyMessage(reply
.other().content());
182 basic_auth
->expiretime
= squid_curtime
;
184 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
187 cbdataReferenceDone(r
->data
);
189 while (basic_auth
->queue
) {
190 if (cbdataReferenceValidDone(basic_auth
->queue
->data
, &cbdata
))
191 basic_auth
->queue
->handler(cbdata
);
193 Auth::QueueNode
*tmpnode
= basic_auth
->queue
->next
;
194 basic_auth
->queue
->next
= NULL
;
195 delete basic_auth
->queue
;
197 basic_auth
->queue
= tmpnode
;