basicauth.[ch]: refactor to make basicauth_string() reusable
This commit is contained in:
parent
81ea4feb2e
commit
4d9891e59e
@ -26,35 +26,46 @@
|
|||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create basic-auth token in buf.
|
||||||
|
* Returns strlen of token on success,
|
||||||
|
* -1 if user/pass missing
|
||||||
|
* 0 if user/pass too long
|
||||||
|
*/
|
||||||
|
ssize_t basicauth_string(const char *user, const char *pass,
|
||||||
|
char *buf, size_t bufsize)
|
||||||
|
{
|
||||||
|
char tmp[256+2];
|
||||||
|
int l;
|
||||||
|
if (!user || !pass) return -1;
|
||||||
|
l = snprintf(tmp, sizeof tmp, "%s:%s", user, pass);
|
||||||
|
if (l < 0 || l >= (ssize_t) sizeof tmp) return 0;
|
||||||
|
if (bufsize < (BASE64ENC_BYTES((unsigned)l) + 1)) return 0;
|
||||||
|
base64enc(buf, tmp, l);
|
||||||
|
return BASE64ENC_BYTES(l);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Add entry to the basicauth list
|
* Add entry to the basicauth list
|
||||||
*/
|
*/
|
||||||
void basicauth_add (vector_t authlist,
|
void basicauth_add (vector_t authlist,
|
||||||
const char *user, const char *pass)
|
const char *user, const char *pass)
|
||||||
{
|
{
|
||||||
char tmp[256+2];
|
char b[BASE64ENC_BYTES((256+2)-1) + 1];
|
||||||
char b[BASE64ENC_BYTES((sizeof tmp)-1) + 1];
|
ssize_t ret;
|
||||||
int l;
|
|
||||||
size_t bl;
|
|
||||||
|
|
||||||
if (user == NULL || pass == NULL) {
|
ret = basicauth_string(user, pass, b, sizeof b);
|
||||||
|
if (ret == -1) {
|
||||||
log_message (LOG_WARNING,
|
log_message (LOG_WARNING,
|
||||||
"Illegal basicauth rule: missing user or pass");
|
"Illegal basicauth rule: missing user or pass");
|
||||||
return;
|
return;
|
||||||
}
|
} else if (ret == 0) {
|
||||||
|
|
||||||
l = snprintf(tmp, sizeof tmp, "%s:%s", user, pass);
|
|
||||||
|
|
||||||
if(l >= (ssize_t) sizeof tmp) {
|
|
||||||
log_message (LOG_WARNING,
|
log_message (LOG_WARNING,
|
||||||
"User / pass in basicauth rule too long");
|
"User / pass in basicauth rule too long");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
base64enc(b, tmp, l);
|
if (vector_append(authlist, b, ret + 1) == -ENOMEM) {
|
||||||
bl = BASE64ENC_BYTES(l) + 1;
|
|
||||||
|
|
||||||
if (vector_append(authlist, b, bl) == -ENOMEM) {
|
|
||||||
log_message (LOG_ERR,
|
log_message (LOG_ERR,
|
||||||
"Unable to allocate memory in basicauth_add()");
|
"Unable to allocate memory in basicauth_add()");
|
||||||
return;
|
return;
|
||||||
|
@ -21,8 +21,12 @@
|
|||||||
#ifndef TINYPROXY_BASICAUTH_H
|
#ifndef TINYPROXY_BASICAUTH_H
|
||||||
#define TINYPROXY_BASICAUTH_H
|
#define TINYPROXY_BASICAUTH_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
|
extern ssize_t basicauth_string(const char *user, const char *pass,
|
||||||
|
char *buf, size_t bufsize);
|
||||||
|
|
||||||
extern void basicauth_add (vector_t authlist,
|
extern void basicauth_add (vector_t authlist,
|
||||||
const char *user, const char *pass);
|
const char *user, const char *pass);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user