|
发表于 2011-2-11 19:54:06
|
显示全部楼层
#include <sys/heap.h>
#include <stdlib.h>
#include <string.h>
#include <memdebug.h>
#include <pro/httpd.h>
#include <gorp/base64.h>
/*!
* \addtogroup xgHTTPD
*/
/*@{*/
AUTHINFO *authList = 0;
/*!
* \brief Look up an authorization entry.
*/
static AUTHINFO *NutHttpAuthLookup(CONST char *dirname, CONST char *login)
{
AUTHINFO *auth;
for (auth = authList; auth; auth = auth->auth_next) {
if (dirname && (strstr(dirname, auth->auth_dirname) != dirname))
continue;
if (login && strcmp(login, auth->auth_login))
continue;
break;
}
return auth;
}
/*!
* \brief Register an authorization entry.
*
* Protect a specified directory from unauthorized access.
*
* \warning Directories not registered by this function are
* accessible by anyone.
*
* \param dirname Name of the directory to protect.
* \param login Required login to access this directory. This
* string must contain a user name, followed by
* a colon followed by an uncrypted password.
*
* \return 0 on success, -1 otherwise.
*/
int NutRegisterAuth(CONST char *dirname, CONST char *login)
{
AUTHINFO *auth;
/* Allocate a new list element. */
if ((auth = malloc(sizeof(AUTHINFO))) != NULL) {
auth->auth_next = authList;
/* Allocate the path component. */
if ((auth->auth_dirname = strdup(dirname)) != NULL) {
/* Allocate the login component. */
if ((auth->auth_login = strdup(login)) != NULL) {
/* Success. Add element to the list and return. */
authList = auth;
return 0;
}
/* Allocation failed. */
free(auth->auth_dirname);
}
free(auth);
}
return -1;
}
/*!
* \brief Clear all authorization entries.
*
* Clears all authorization entries and frees the used ressouces.
*
*/
void NutClearAuth(void)
{
AUTHINFO *auth;
while (authList) {
auth = authList;
authList = auth->auth_next;
free(auth->auth_dirname);
free(auth->auth_login);
free(auth);
}
}
/*!
* \brief Validate an authorization request.
*
* \note This function is automatically called by the HTTP
* library on incoming requests. Applications do not
* need to call this function.
*
* \param req Request to be checked.
*
* \return 0, if access granted, -1 otherwise.
*/
int NutHttpAuthValidate(REQUEST * req)
{
char *realm;
char *cp = 0;
int rc = -1;
/*
* Get directory by chopping off filename.
*/
realm = req->req_url;
if ((cp = strrchr(realm, '/')) != 0)
*cp = 0;
else
realm = ".";
/*
* Check if authorization required.
*/
if (NutHttpAuthLookup(realm, 0)) {
/*
* Check authorization.
*/
if (req->req_auth) {
/*
* Acceptint basic authorization only.
*/
if (strncmp(req->req_auth, "Basic ", 6) == 0) {
NutDecodeBase64(req->req_auth + 6);
if (NutHttpAuthLookup(realm, req->req_auth + 6))
rc = 0;
}
}
} else
rc = 0;
if (cp)
*cp = '/';
return rc;
}
/*@}*/ |
|