2013-04-17 22:34:27 +02:00
|
|
|
/*
|
2016-01-04 12:58:51 +01:00
|
|
|
* Copyright (c) 2013-2016 Joris Vink <joris@coders.se>
|
2013-04-17 22:34:27 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-08-01 16:10:37 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2016-02-01 21:33:51 +01:00
|
|
|
#include <ctype.h>
|
2016-01-22 12:08:13 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2016-01-07 09:24:45 +01:00
|
|
|
#include <time.h>
|
2014-07-21 01:16:03 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
2013-04-17 22:34:27 +02:00
|
|
|
#include "kore.h"
|
|
|
|
|
2013-05-01 20:10:45 +02:00
|
|
|
static struct {
|
|
|
|
char *name;
|
|
|
|
int value;
|
|
|
|
} month_names[] = {
|
|
|
|
{ "Jan", 0 },
|
|
|
|
{ "Feb", 1 },
|
|
|
|
{ "Mar", 2 },
|
|
|
|
{ "Apr", 3 },
|
|
|
|
{ "May", 4 },
|
|
|
|
{ "Jun", 5 },
|
|
|
|
{ "Jul", 6 },
|
|
|
|
{ "Aug", 7 },
|
|
|
|
{ "Sep", 8 },
|
|
|
|
{ "Oct", 9 },
|
|
|
|
{ "Nov", 10 },
|
|
|
|
{ "Dec", 11 },
|
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
2014-04-22 21:45:26 +02:00
|
|
|
static char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
2013-07-10 15:00:53 +02:00
|
|
|
|
2013-04-17 22:34:27 +02:00
|
|
|
void
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug_internal(char *file, int line, const char *fmt, ...)
|
2013-04-17 22:34:27 +02:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2014-04-23 16:29:58 +02:00
|
|
|
(void)vsnprintf(buf, sizeof(buf), fmt, args);
|
2013-04-17 22:34:27 +02:00
|
|
|
va_end(args);
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
printf("[%d] %s:%d - %s\n", kore_pid, file, line, buf);
|
2013-04-17 22:34:27 +02:00
|
|
|
}
|
|
|
|
|
2013-06-04 23:24:47 +02:00
|
|
|
void
|
|
|
|
kore_log_init(void)
|
|
|
|
{
|
2014-07-31 14:26:51 +02:00
|
|
|
if (!foreground)
|
|
|
|
openlog("kore", LOG_NDELAY | LOG_PID, LOG_DAEMON);
|
2013-06-04 23:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_log(int prio, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
2016-06-08 13:55:14 +02:00
|
|
|
char buf[2048], tmp[32];
|
2013-06-04 23:24:47 +02:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
2014-04-23 16:29:58 +02:00
|
|
|
(void)vsnprintf(buf, sizeof(buf), fmt, args);
|
2013-06-04 23:24:47 +02:00
|
|
|
va_end(args);
|
|
|
|
|
2014-07-31 14:26:51 +02:00
|
|
|
if (worker != NULL) {
|
2016-06-08 13:55:14 +02:00
|
|
|
(void)snprintf(tmp, sizeof(tmp), "wrk %d", worker->id);
|
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
if (worker->id == KORE_WORKER_KEYMGR)
|
2016-07-04 11:41:37 +02:00
|
|
|
(void)kore_strlcpy(tmp, "keymgr", sizeof(tmp));
|
2016-06-08 13:55:14 +02:00
|
|
|
#endif
|
2014-07-31 14:26:51 +02:00
|
|
|
if (foreground)
|
2016-06-08 13:55:14 +02:00
|
|
|
printf("[%s]: %s\n", tmp, buf);
|
2014-07-31 14:26:51 +02:00
|
|
|
else
|
2016-06-08 13:55:14 +02:00
|
|
|
syslog(prio, "[%s]: %s", tmp, buf);
|
2014-07-31 14:26:51 +02:00
|
|
|
} else {
|
|
|
|
if (foreground)
|
|
|
|
printf("[parent]: %s\n", buf);
|
|
|
|
else
|
|
|
|
syslog(prio, "[parent]: %s", buf);
|
|
|
|
}
|
2013-06-04 23:24:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 11:41:37 +02:00
|
|
|
size_t
|
|
|
|
kore_strlcpy(char *dst, const char *src, const size_t len)
|
2013-05-01 00:35:33 +02:00
|
|
|
{
|
|
|
|
char *d = dst;
|
|
|
|
const char *s = src;
|
2014-04-23 14:48:29 +02:00
|
|
|
const char *end = dst + len - 1;
|
2013-05-01 00:35:33 +02:00
|
|
|
|
2016-07-04 11:41:37 +02:00
|
|
|
if (len == 0)
|
|
|
|
fatal("kore_strlcpy: len == 0");
|
|
|
|
|
2014-04-23 14:48:29 +02:00
|
|
|
while ((*d = *s) != '\0') {
|
|
|
|
if (d == end) {
|
2013-05-01 00:35:33 +02:00
|
|
|
*d = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2014-04-23 14:48:29 +02:00
|
|
|
|
|
|
|
d++;
|
|
|
|
s++;
|
2013-05-01 00:35:33 +02:00
|
|
|
}
|
2016-07-04 11:41:37 +02:00
|
|
|
|
|
|
|
while (*s != '\0')
|
|
|
|
s++;
|
|
|
|
|
|
|
|
return (s - src);
|
2013-05-01 00:35:33 +02:00
|
|
|
}
|
|
|
|
|
2014-08-11 10:45:10 +02:00
|
|
|
int
|
|
|
|
kore_snprintf(char *str, size_t size, int *len, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
l = vsnprintf(str, size, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (l == -1 || (size_t)l >= size)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (len != NULL)
|
|
|
|
*len = l;
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-12-12 00:58:32 +01:00
|
|
|
long long
|
|
|
|
kore_strtonum(const char *str, int base, long long min, long long max, int *err)
|
2013-05-01 16:03:48 +02:00
|
|
|
{
|
2013-12-12 00:58:32 +01:00
|
|
|
long long l;
|
2013-05-01 16:03:48 +02:00
|
|
|
char *ep;
|
|
|
|
|
|
|
|
if (min > max) {
|
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
2013-08-13 16:13:43 +02:00
|
|
|
l = strtoll(str, &ep, base);
|
2013-05-01 16:03:48 +02:00
|
|
|
if (errno != 0 || str == ep || *ep != '\0') {
|
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l < min) {
|
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l > max) {
|
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
*err = KORE_RESULT_OK;
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
|
2014-07-21 01:16:03 +02:00
|
|
|
u_int64_t
|
|
|
|
kore_strtonum64(const char *str, int sign, int *err)
|
|
|
|
{
|
|
|
|
u_int64_t l;
|
2014-07-21 01:51:51 +02:00
|
|
|
long long ll;
|
2014-07-21 01:16:03 +02:00
|
|
|
char *ep;
|
2014-07-21 01:51:51 +02:00
|
|
|
int check;
|
2014-07-21 01:16:03 +02:00
|
|
|
|
2014-07-30 09:00:19 +02:00
|
|
|
l = 0;
|
2014-07-21 01:51:51 +02:00
|
|
|
check = 1;
|
|
|
|
|
|
|
|
ll = strtoll(str, &ep, 10);
|
|
|
|
if ((errno == EINVAL || errno == ERANGE) &&
|
|
|
|
(ll == LLONG_MIN || ll == LLONG_MAX)) {
|
|
|
|
if (sign) {
|
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
check = 0;
|
2014-07-21 01:16:03 +02:00
|
|
|
}
|
|
|
|
|
2014-07-21 01:51:51 +02:00
|
|
|
if (!sign) {
|
|
|
|
l = strtoull(str, &ep, 10);
|
|
|
|
if ((errno == EINVAL || errno == ERANGE) && l == ULONG_MAX) {
|
2014-07-21 01:16:03 +02:00
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
2014-07-21 01:51:51 +02:00
|
|
|
|
|
|
|
if (check && ll < 0) {
|
2014-07-21 01:16:03 +02:00
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 01:51:51 +02:00
|
|
|
if (str == ep || *ep != '\0') {
|
|
|
|
*err = KORE_RESULT_ERROR;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-07-21 01:16:03 +02:00
|
|
|
*err = KORE_RESULT_OK;
|
2014-07-21 01:51:51 +02:00
|
|
|
return ((sign) ? (u_int64_t)ll : l);
|
2014-07-21 01:16:03 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 20:10:45 +02:00
|
|
|
int
|
|
|
|
kore_split_string(char *input, char *delim, char **out, size_t ele)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
char **ap;
|
|
|
|
|
2016-02-13 14:35:11 +01:00
|
|
|
if (ele == 0)
|
2016-02-13 15:27:02 +01:00
|
|
|
return (0);
|
2016-02-13 14:35:11 +01:00
|
|
|
|
2013-05-01 20:10:45 +02:00
|
|
|
count = 0;
|
|
|
|
for (ap = out; ap < &out[ele - 1] &&
|
|
|
|
(*ap = strsep(&input, delim)) != NULL;) {
|
|
|
|
if (**ap != '\0') {
|
|
|
|
ap++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ap = NULL;
|
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:02:59 +02:00
|
|
|
void
|
2016-02-13 14:33:06 +01:00
|
|
|
kore_strip_chars(char *in, const char strip, char **out)
|
2013-09-10 11:02:59 +02:00
|
|
|
{
|
|
|
|
u_int32_t len;
|
|
|
|
char *s, *p;
|
|
|
|
|
|
|
|
len = strlen(in);
|
|
|
|
*out = kore_malloc(len + 1);
|
|
|
|
p = *out;
|
|
|
|
|
|
|
|
for (s = in; s < (in + len); s++) {
|
|
|
|
if (*s == strip)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
2013-05-01 20:10:45 +02:00
|
|
|
time_t
|
|
|
|
kore_date_to_time(char *http_date)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
int err, i;
|
2013-10-15 15:06:19 +02:00
|
|
|
struct tm tm, *ltm;
|
2013-05-01 20:10:45 +02:00
|
|
|
char *args[7], *tbuf[5], *sdup;
|
|
|
|
|
|
|
|
time(&t);
|
2013-10-15 15:06:19 +02:00
|
|
|
ltm = localtime(&t);
|
2013-05-01 20:10:45 +02:00
|
|
|
sdup = kore_strdup(http_date);
|
|
|
|
|
|
|
|
t = KORE_RESULT_ERROR;
|
|
|
|
|
|
|
|
if (kore_split_string(sdup, " ", args, 7) != 6) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-15 15:06:19 +02:00
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
|
|
|
|
tm.tm_year = kore_strtonum(args[3], 10, 1900, 2068, &err) - 1900;
|
2014-01-29 22:59:58 +01:00
|
|
|
if (err == KORE_RESULT_ERROR) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed year in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; month_names[i].name != NULL; i++) {
|
|
|
|
if (!strcmp(month_names[i].name, args[2])) {
|
|
|
|
tm.tm_mon = month_names[i].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (month_names[i].name == NULL) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed month in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-08-13 16:13:43 +02:00
|
|
|
tm.tm_mday = kore_strtonum(args[1], 10, 1, 31, &err);
|
2013-05-01 20:10:45 +02:00
|
|
|
if (err == KORE_RESULT_ERROR) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed mday in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kore_split_string(args[4], ":", tbuf, 5) != 3) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed HH:MM:SS in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-15 15:06:19 +02:00
|
|
|
tm.tm_hour = kore_strtonum(tbuf[0], 10, 0, 23, &err);
|
2013-05-01 20:10:45 +02:00
|
|
|
if (err == KORE_RESULT_ERROR) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed hour in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-15 15:06:19 +02:00
|
|
|
tm.tm_min = kore_strtonum(tbuf[1], 10, 0, 59, &err);
|
2013-05-01 20:10:45 +02:00
|
|
|
if (err == KORE_RESULT_ERROR) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed minutes in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-08-13 16:13:43 +02:00
|
|
|
tm.tm_sec = kore_strtonum(tbuf[2], 10, 0, 60, &err);
|
2013-05-01 20:10:45 +02:00
|
|
|
if (err == KORE_RESULT_ERROR) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("misformed seconds in http-date: '%s'", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-15 15:06:19 +02:00
|
|
|
tm.tm_isdst = ltm->tm_isdst;
|
|
|
|
t = mktime(&tm) + ltm->tm_gmtoff;
|
2013-05-01 20:10:45 +02:00
|
|
|
if (t == -1) {
|
|
|
|
t = 0;
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("mktime() on '%s' failed", http_date);
|
2013-05-01 20:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2013-06-27 08:43:07 +02:00
|
|
|
kore_mem_free(sdup);
|
2013-05-01 20:10:45 +02:00
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:16:09 +02:00
|
|
|
char *
|
|
|
|
kore_time_to_date(time_t now)
|
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
static time_t last = 0;
|
|
|
|
static char tbuf[32];
|
|
|
|
|
|
|
|
if (now != last) {
|
|
|
|
last = now;
|
|
|
|
|
|
|
|
tm = gmtime(&now);
|
|
|
|
if (!strftime(tbuf, sizeof(tbuf), "%a, %d %b %Y %T GMT", tm)) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("strftime() gave us NULL (%ld)", now);
|
2013-05-01 21:16:09 +02:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (tbuf);
|
|
|
|
}
|
|
|
|
|
2013-06-24 09:36:40 +02:00
|
|
|
u_int64_t
|
|
|
|
kore_time_ms(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) == -1)
|
|
|
|
return (0);
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
return (tv.tv_sec * 1000 + (tv.tv_usec / 1000));
|
2013-06-24 09:36:40 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 15:00:53 +02:00
|
|
|
int
|
|
|
|
kore_base64_encode(u_int8_t *data, u_int32_t len, char **out)
|
|
|
|
{
|
|
|
|
struct kore_buf *res;
|
|
|
|
u_int8_t n, *pdata;
|
|
|
|
int i, padding;
|
|
|
|
u_int32_t idx, b, plen;
|
|
|
|
|
|
|
|
if ((len % 3) != 0) {
|
|
|
|
padding = 3 - (len % 3);
|
|
|
|
plen = len + padding;
|
2013-07-13 21:08:55 +02:00
|
|
|
pdata = kore_malloc(plen);
|
2013-07-10 15:00:53 +02:00
|
|
|
|
|
|
|
memcpy(pdata, data, len);
|
|
|
|
memset(pdata + len, 0, padding);
|
|
|
|
} else {
|
|
|
|
plen = len;
|
|
|
|
padding = 0;
|
|
|
|
pdata = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = kore_buf_create(plen);
|
|
|
|
|
|
|
|
i = 2;
|
|
|
|
b = 0;
|
|
|
|
for (idx = 0; idx < plen; idx++) {
|
|
|
|
b |= (pdata[idx] << (i * 8));
|
|
|
|
if (i-- == 0) {
|
|
|
|
for (i = 3; i >= 0; i--) {
|
|
|
|
n = (b >> (6 * i)) & 0x3f;
|
|
|
|
if (n >= sizeof(b64table)) {
|
|
|
|
kore_debug("unable to encode %d", n);
|
|
|
|
kore_buf_free(res);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx >= len && i < padding)
|
|
|
|
break;
|
|
|
|
|
2013-08-26 08:52:56 +02:00
|
|
|
kore_buf_append(res, &(b64table[n]), 1);
|
2013-07-10 15:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b = 0;
|
|
|
|
i = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < padding; i++)
|
|
|
|
kore_buf_append(res, (u_int8_t *)"=", 1);
|
|
|
|
|
|
|
|
if (pdata != data)
|
|
|
|
kore_mem_free(pdata);
|
|
|
|
|
|
|
|
pdata = kore_buf_release(res, &plen);
|
|
|
|
*out = kore_malloc(plen + 1);
|
2016-07-04 11:41:37 +02:00
|
|
|
(void)kore_strlcpy(*out, (char *)pdata, plen + 1);
|
2013-07-10 15:00:53 +02:00
|
|
|
kore_mem_free(pdata);
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kore_base64_decode(char *in, u_int8_t **out, u_int32_t *olen)
|
|
|
|
{
|
2014-04-23 12:32:54 +02:00
|
|
|
int i, c;
|
2013-07-10 15:00:53 +02:00
|
|
|
struct kore_buf *res;
|
|
|
|
u_int8_t d, n, o;
|
|
|
|
u_int32_t b, len, idx;
|
|
|
|
|
2014-04-23 12:32:54 +02:00
|
|
|
i = 4;
|
2013-07-10 15:00:53 +02:00
|
|
|
b = 0;
|
2013-10-15 13:10:30 +02:00
|
|
|
d = 0;
|
2014-07-30 09:00:19 +02:00
|
|
|
c = 0;
|
2013-07-10 15:00:53 +02:00
|
|
|
len = strlen(in);
|
|
|
|
res = kore_buf_create(len);
|
|
|
|
|
|
|
|
for (idx = 0; idx < len; idx++) {
|
2014-04-23 12:32:54 +02:00
|
|
|
c = in[idx];
|
|
|
|
if (c == '=')
|
2014-04-22 21:45:26 +02:00
|
|
|
break;
|
|
|
|
|
2013-07-10 15:00:53 +02:00
|
|
|
for (o = 0; o < sizeof(b64table); o++) {
|
2014-04-23 12:32:54 +02:00
|
|
|
if (b64table[o] == c) {
|
2013-07-10 15:00:53 +02:00
|
|
|
d = o;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 12:32:54 +02:00
|
|
|
if (o == sizeof(b64table)) {
|
|
|
|
*out = NULL;
|
|
|
|
kore_buf_free(res);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2013-07-10 15:00:53 +02:00
|
|
|
|
2014-04-23 12:32:54 +02:00
|
|
|
b |= (d & 0x3f) << ((i - 1) * 6);
|
|
|
|
i--;
|
|
|
|
if (i == 0) {
|
2013-07-10 15:00:53 +02:00
|
|
|
for (i = 2; i >= 0; i--) {
|
|
|
|
n = (b >> (8 * i));
|
|
|
|
kore_buf_append(res, &n, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
b = 0;
|
2014-04-23 12:32:54 +02:00
|
|
|
i = 4;
|
2013-07-10 15:00:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 12:32:54 +02:00
|
|
|
if (c == '=') {
|
|
|
|
if (i > 2) {
|
|
|
|
*out = NULL;
|
|
|
|
kore_buf_free(res);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
o = i;
|
2014-04-22 21:45:26 +02:00
|
|
|
for (i = 2; i >= o; i--) {
|
|
|
|
n = (b >> (8 * i));
|
|
|
|
kore_buf_append(res, &n, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 15:00:53 +02:00
|
|
|
*out = kore_buf_release(res, olen);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:02:59 +02:00
|
|
|
void *
|
2016-04-01 19:54:10 +02:00
|
|
|
kore_mem_find(void *src, size_t slen, void *needle, size_t len)
|
2013-09-10 11:02:59 +02:00
|
|
|
{
|
2016-04-01 19:55:43 +02:00
|
|
|
size_t pos;
|
2013-09-10 11:02:59 +02:00
|
|
|
|
2016-05-05 15:29:43 +02:00
|
|
|
for (pos = 0; pos < slen; pos++) {
|
2016-03-31 19:55:23 +02:00
|
|
|
if ( *((u_int8_t *)src + pos) != *(u_int8_t *)needle)
|
2013-09-10 11:02:59 +02:00
|
|
|
continue;
|
|
|
|
|
2016-03-31 19:55:23 +02:00
|
|
|
if ((slen - pos) < len)
|
2013-09-10 11:02:59 +02:00
|
|
|
return (NULL);
|
|
|
|
|
2016-03-31 19:55:23 +02:00
|
|
|
if (!memcmp((u_int8_t *)src + pos, needle, len))
|
|
|
|
return ((u_int8_t *)src + pos);
|
2013-09-10 11:02:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:33:51 +01:00
|
|
|
char *
|
|
|
|
kore_text_trim(char *string, size_t len)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
2016-02-13 14:02:10 +01:00
|
|
|
if (len == 0)
|
|
|
|
return (string);
|
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
end = (string + len) - 1;
|
|
|
|
while (isspace(*string) && string < end)
|
2016-02-01 21:33:51 +01:00
|
|
|
string++;
|
|
|
|
|
|
|
|
while (isspace(*end) && end > string)
|
|
|
|
*(end)-- = '\0';
|
|
|
|
|
|
|
|
return (string);
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:10:10 +01:00
|
|
|
char *
|
|
|
|
kore_read_line(FILE *fp, char *in, size_t len)
|
|
|
|
{
|
|
|
|
char *p, *t;
|
|
|
|
|
|
|
|
if (fgets(in, len, fp) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
p = in;
|
|
|
|
in[strcspn(in, "\n")] = '\0';
|
|
|
|
|
|
|
|
while (isspace(*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (p[0] == '#' || p[0] == '\0') {
|
|
|
|
p[0] = '\0';
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (t = p; *t != '\0'; t++) {
|
|
|
|
if (*t == '\t')
|
|
|
|
*t = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2013-04-17 22:34:27 +02:00
|
|
|
void
|
|
|
|
fatal(const char *fmt, ...)
|
|
|
|
{
|
2016-07-06 16:16:15 +02:00
|
|
|
va_list args;
|
|
|
|
char buf[2048];
|
|
|
|
extern const char *__progname;
|
2013-04-17 22:34:27 +02:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
2014-04-23 16:29:58 +02:00
|
|
|
(void)vsnprintf(buf, sizeof(buf), fmt, args);
|
2013-04-17 22:34:27 +02:00
|
|
|
va_end(args);
|
|
|
|
|
2014-07-31 15:12:05 +02:00
|
|
|
if (!foreground)
|
|
|
|
kore_log(LOG_ERR, "%s", buf);
|
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
|
|
|
if (worker != NULL && worker->id == KORE_WORKER_KEYMGR)
|
|
|
|
kore_keymgr_cleanup();
|
|
|
|
#endif
|
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
printf("%s: %s\n", __progname, buf);
|
2013-04-17 22:34:27 +02:00
|
|
|
exit(1);
|
|
|
|
}
|