41#ifndef HAVE_STRCASECMP
43# define strcasecmp _stricmp
47#ifndef HAVE_STRNCASECMP
49# define strncasecmp _strnicmp
56#define STRINGP(s) ((s != NULL) && (*(s) != '\0'))
58static const char *srs_separators =
"=-+";
72#define X(e,s) if (code == e) return s;
104 return "Unknown SRS error.";
116 memset(srs, 0,
sizeof(
srs_t));
139 int newlen = (srs->
numsecrets + 1) *
sizeof(
char *);
147 if (idx < srs->numsecrets)
152#define SRS_PARAM_DEFINE(n, t) \
153 int srs_set_ ## n (srs_t *srs, t value) { \
155 return SRS_SUCCESS; \
157 t srs_get_ ## n (srs_t *srs) { \
163 if (strchr(srs_separators, value) == NULL)
186#define SRS_TIME_PRECISION (60 * 60 * 24)
187#define SRS_TIME_BASEBITS 5
191#define SRS_TIME_SIZE 2
192#define SRS_TIME_SLOTS (1<<(SRS_TIME_BASEBITS<<(SRS_TIME_SIZE-1)))
214 for (sp =
stamp; *sp; sp++) {
227 if (now <= then + srs->maxage)
233 "abcdefghijklmnopqrstuvwxyz"
236static void srs_hash_create_v(
srs_t *srs,
int idx,
char *
buf,
int nargs, va_list ap)
253 for (i = 0; i < nargs; i++) {
254 data = va_arg(ap,
char *);
256 lcdata = alloca(len + 1);
257 for (
j = 0;
j < len;
j++) {
258 if (isupper(
data[
j]))
259 lcdata[
j] = tolower(
data[
j]);
270 hp = (
unsigned char *)srshash;
272 for (i = 0; i < srs->
hashlen; i++) {
279 j = ((*hp & 0x03) << 4) |
280 ((*(hp + 1) & 0xF0) >> 4);
284 j = ((*hp & 0x0F) << 2) |
285 ((*(hp + 1) & 0xC0) >> 6);
311 srs_hash_create_v(srs, 0,
buf, nargs, ap);
326 if (len < srs->hashmin)
328 if (len < srs->hashlen) {
338 srshash = alloca(srs->
hashlen + 1);
339 srs_hash_create_v(srs, i, srshash, nargs, ap);
341 if (strncasecmp(
hash, srshash, len) == 0)
349 char *
buf,
int buflen,
350 char *sendhost,
char *senduser,
351 const char *aliashost) {
358 if ((strncasecmp(senduser,
SRS0TAG, 4) == 0) &&
359 (strchr(srs_separators, senduser[4]) != NULL)) {
360 sendhost = senduser + 5;
361 if (*sendhost ==
'\0')
363 senduser = strchr(sendhost,
SRSSEP);
364 if ((senduser == NULL) || (*senduser ==
'\0'))
371 strlen(sendhost) + 1 + strlen(senduser)
372 + 1 + strlen(aliashost);
379 srshash = alloca(srs->
hashlen + 1);
386 sendhost,
SRSSEP, senduser,
393 char *
buf,
int buflen,
394 char *sendhost,
char *senduser,
395 const char *aliashost) {
402 if ((strncasecmp(senduser,
SRS1TAG, 4) == 0) &&
403 (strchr(srs_separators, senduser[4]) != NULL)) {
405 srshash = senduser + 5;
406 if (*srshash ==
'\0')
417 srshash = alloca(srs->
hashlen + 1);
423 strlen(
srshost) + 1 + strlen(srsuser)
424 + 1 + strlen(aliashost);
433 else if ((strncasecmp(senduser,
SRS0TAG, 4) == 0) &&
434 (strchr(srs_separators, senduser[4]) != NULL)) {
435 srsuser = senduser + 4;
437 srshash = alloca(srs->
hashlen + 1);
443 strlen(
srshost) + 1 + strlen(srsuser)
444 + 1 + strlen(aliashost);
454 sendhost, senduser, aliashost);
468 if (strncasecmp(senduser,
SRS0TAG, 4) == 0) {
469 srshash = senduser + 5;
472 srsstamp = strchr(srshash,
SRSSEP);
505 if (strncasecmp(senduser,
SRS1TAG, 4) == 0) {
506 srshash = senduser + 5;
529 const char *
sender,
const char *alias)
540 while ((
tmp = strchr(alias,
'@')) != NULL)
551 if (strcasecmp(sendhost, alias) == 0) {
552 if (strlen(
sender) >= buflen)
560 senduser = alloca(len + 1);
567 sendhost, senduser, alias);
571 const char *
sender,
const char *alias)
583 alen = strlen(alias);
587 buf = (
char *)srs_f_malloc(len);
614 senduser = alloca(len + 1);
618 tmp = strchr(senduser,
'@');
639 buf = (
char *)srs_f_malloc(len);
void sha1_final(uint8_t hash[SHA1_DIGESTSIZE], sha1_ctx *context)
void sha1_init(sha1_ctx *context)
void sha1_update(sha1_ctx *context, const uint8_t *data, uint32_t len)
char stamp[FMT_ULONG+FMT_ULONG+3]
const char * srs_strerror(int code)
void srs_init(srs_t *srs)
int srs_set_separator(srs_t *srs, char value)
#define SRS_TIME_PRECISION
int srs_hash_create(srs_t *srs, char *buf, int nargs,...)
int srs_hash_check(srs_t *srs, char *hash, int nargs,...)
int srs_timestamp_check(srs_t *srs, const char *stamp)
int srs_reverse(srs_t *srs, char *buf, int buflen, const char *sender)
const char * srs_get_secret(srs_t *srs, int idx)
char srs_get_separator(srs_t *srs)
int srs_parse_guarded(srs_t *srs, char *buf, int buflen, char *senduser)
const char * SRS_TIME_BASECHARS
void srs_free(srs_t *srs)
int srs_reverse_alloc(srs_t *srs, char **sptr, const char *sender)
int srs_forward_alloc(srs_t *srs, char **sptr, const char *sender, const char *alias)
int srs_add_secret(srs_t *srs, const char *secret)
int srs_compile_guarded(srs_t *srs, char *buf, int buflen, char *sendhost, char *senduser, const char *aliashost)
int srs_set_malloc(srs_malloc_t m, srs_realloc_t r, srs_free_t f)
int srs_forward(srs_t *srs, char *buf, int buflen, const char *sender, const char *alias)
#define SRS_PARAM_DEFINE(n, t)
int srs_parse_shortcut(srs_t *srs, char *buf, int buflen, char *senduser)
int srs_timestamp_create(srs_t *srs, char *buf, time_t now)
int srs_compile_shortcut(srs_t *srs, char *buf, int buflen, char *sendhost, char *senduser, const char *aliashost)
#define SRS_TIME_BASEBITS
const char * SRS_HASH_BASECHARS
#define SRS_ETIMESTAMPOUTOFDATE
void *(* srs_malloc_t)(size_t)
#define SRS_ENOSENDERATSIGN
void(* srs_free_t)(void *)
void *(* srs_realloc_t)(void *, size_t)
#define SRS_EHASHTOOSHORT
#define SRS_ESEPARATORINVALID
#define SRS_ENOTREWRITTEN
#define SRS_IS_SRS_ADDRESS(x)
#define SRS_EBADTIMESTAMPCHAR
#define SRS_ENOTSRSADDRESS