]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/basic/UserRequest.cc
2 * Copyright (C) 1996-2018 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"
20 #include "HttpRequest.h"
23 #include "SquidTime.h"
25 #if !defined(HELPER_INPUT_BUFFER)
26 #define HELPER_INPUT_BUFFER 8192
30 Auth::Basic::UserRequest::authenticated() const
32 Auth::Basic::User
const *basic_auth
= dynamic_cast<Auth::Basic::User
const *>(user().getRaw());
34 if (basic_auth
&& basic_auth
->authenticated())
41 Auth::Basic::UserRequest::credentialsStr()
43 Auth::Basic::User
const *basic_auth
= dynamic_cast<Auth::Basic::User
const *>(user().getRaw());
45 return basic_auth
->passwd
;
49 /* log a basic user in
52 Auth::Basic::UserRequest::authenticate(HttpRequest
*, ConnStateData
*, Http::HdrType
)
54 assert(user() != NULL
);
56 /* if the password is not ok, do an identity */
57 if (!user() || user()->credentials() != Auth::Ok
)
60 /* are we about to recheck the credentials externally? */
61 if ((user()->expiretime
+ static_cast<Auth::Basic::Config
*>(Auth::SchemeConfig::Find("basic"))->credentialsTTL
) <= squid_curtime
) {
62 debugs(29, 4, HERE
<< "credentials expired - rechecking");
66 /* we have been through the external helper, and the credentials haven't expired */
67 debugs(29, 9, HERE
<< "user '" << user()->username() << "' authenticated");
69 /* Decode now takes care of finding the AuthUser struct in the cache */
70 /* after external auth occurs anyway */
71 user()->expiretime
= current_time
.tv_sec
;
75 Auth::Basic::UserRequest::module_direction()
77 /* null auth_user is checked for by Auth::UserRequest::direction() */
78 if (user()->auth_type
!= Auth::AUTH_BASIC
)
79 return Auth::CRED_ERROR
;
81 switch (user()->credentials()) {
85 return Auth::CRED_LOOKUP
;
88 if (user()->expiretime
+ static_cast<Auth::Basic::Config
*>(Auth::SchemeConfig::Find("basic"))->credentialsTTL
<= squid_curtime
)
89 return Auth::CRED_LOOKUP
;
90 return Auth::CRED_VALID
;
93 return Auth::CRED_VALID
;
96 return Auth::CRED_ERROR
;
100 /* send the initial data to a basic authenticator module */
102 Auth::Basic::UserRequest::startHelperLookup(HttpRequest
*request
, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
104 assert(user()->auth_type
== Auth::AUTH_BASIC
);
105 Auth::Basic::User
*basic_auth
= dynamic_cast<Auth::Basic::User
*>(user().getRaw());
106 assert(basic_auth
!= NULL
);
107 debugs(29, 9, HERE
<< "'" << basic_auth
->username() << ":" << basic_auth
->passwd
<< "'");
109 if (static_cast<Auth::Basic::Config
*>(Auth::SchemeConfig::Find("basic"))->authenticateProgram
== NULL
) {
110 debugs(29, DBG_CRITICAL
, "ERROR: No Basic authentication program configured.");
115 /* check to see if the auth_user already has a request outstanding */
116 if (user()->credentials() == Auth::Pending
) {
117 /* there is a request with the same credentials already being verified */
119 Auth::QueueNode
*node
= new Auth::QueueNode(this, handler
, data
);
121 /* queue this validation request to be infored of the pending lookup results */
122 node
->next
= basic_auth
->queue
;
123 basic_auth
->queue
= node
;
126 // otherwise submit this request to the auth helper(s) for validation
128 /* mark this user as having verification in progress */
129 user()->credentials(Auth::Pending
);
130 char buf
[HELPER_INPUT_BUFFER
];
131 static char usern
[HELPER_INPUT_BUFFER
];
132 static char pass
[HELPER_INPUT_BUFFER
];
133 if (static_cast<Auth::Basic::Config
*>(user()->config
)->utf8
) {
134 latin1_to_utf8(usern
, sizeof(usern
), user()->username());
135 latin1_to_utf8(pass
, sizeof(pass
), basic_auth
->passwd
);
136 xstrncpy(usern
, rfc1738_escape(usern
), sizeof(usern
));
137 xstrncpy(pass
, rfc1738_escape(pass
), sizeof(pass
));
139 xstrncpy(usern
, rfc1738_escape(user()->username()), sizeof(usern
));
140 xstrncpy(pass
, rfc1738_escape(basic_auth
->passwd
), sizeof(pass
));
143 if (const char *keyExtras
= helperRequestKeyExtras(request
, al
))
144 sz
= snprintf(buf
, sizeof(buf
), "%s %s %s\n", usern
, pass
, keyExtras
);
146 sz
= snprintf(buf
, sizeof(buf
), "%s %s\n", usern
, pass
);
149 debugs(9, DBG_CRITICAL
, "ERROR: Basic Authentication Failure. Can not build helper validation request.");
151 } else if (static_cast<size_t>(sz
) >= sizeof(buf
)) {
152 debugs(9, DBG_CRITICAL
, "ERROR: Basic Authentication Failure. user:password exceeds " << sizeof(buf
) << " bytes.");
155 helperSubmit(basicauthenticators
, buf
, Auth::Basic::UserRequest::HandleReply
,
156 new Auth::StateData(this, handler
, data
));
160 Auth::Basic::UserRequest::HandleReply(void *data
, const Helper::Reply
&reply
)
162 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
164 debugs(29, 5, HERE
<< "reply=" << reply
);
166 assert(r
->auth_user_request
!= NULL
);
167 assert(r
->auth_user_request
->user()->auth_type
== Auth::AUTH_BASIC
);
169 // add new helper kv-pair notes to the credentials object
170 // so that any transaction using those credentials can access them
171 r
->auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
173 /* this is okay since we only play with the Auth::Basic::User child fields below
174 * and dont pass the pointer itself anywhere */
175 Auth::Basic::User
*basic_auth
= dynamic_cast<Auth::Basic::User
*>(r
->auth_user_request
->user().getRaw());
177 assert(basic_auth
!= NULL
);
179 if (reply
.result
== Helper::Okay
)
180 basic_auth
->credentials(Auth::Ok
);
182 basic_auth
->credentials(Auth::Failed
);
184 if (reply
.other().hasContent())
185 r
->auth_user_request
->setDenyMessage(reply
.other().content());
188 basic_auth
->expiretime
= squid_curtime
;
190 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
193 cbdataReferenceDone(r
->data
);
195 while (basic_auth
->queue
) {
196 if (cbdataReferenceValidDone(basic_auth
->queue
->data
, &cbdata
))
197 basic_auth
->queue
->handler(cbdata
);
199 Auth::QueueNode
*tmpnode
= basic_auth
->queue
->next
;
200 basic_auth
->queue
->next
= NULL
;
201 delete basic_auth
->queue
;
203 basic_auth
->queue
= tmpnode
;