1
0
forked from bip/bip

Compare commits

..

4 Commits

Author SHA1 Message Date
Loïc Gomez
d19099eb3c
Log unhandled IRC errors as LOG_INFO in bip.log
This will allow for user feedback in main bip.log when an IRC error
occurs, like:
- 401 ERR_NOSUCHNICK
- 404 ERR_CANNOTSENDTOCHAN
- 432 ERR_ERRONEUSNICKNAME

These should not be logged as LOG_ERROR as they are not bip errors but
usually on the end user instead.

Signed-off-by: Loïc Gomez <bip@animanova.fr>
2024-02-18 23:45:12 +01:00
Loïc Gomez
99a1244e46
Allow a user to /BIP JUMP [-f] [other_conn] (within their list).
This can be useful when a connection is very slow to reconnect and the
user wants to force an immediate reconnection.

Also:
- fix message when JUMPing on some already reconnecting link
- add find_link() method
- add reconnect timer info if any (else display 0s)
- add -f flag to reset reconnect timer

Signed-off-by: Loïc Gomez <bip@animanova.fr>
2024-02-18 23:39:27 +09:00
Loïc Gomez
edd460a8fa
Set default recon_timer (and step) to 30s
Waiting 2 minutes on the first disconnect is depressing.
With this the maximum of 10min wait time is reached after 20 attempts
instead of the current 5 attempts.

Signed-off-by: Loïc Gomez <bip@animanova.fr>
2024-02-18 00:07:41 +01:00
Loïc Gomez
4cd5bdb381
Set default backlog_lines = 0 (fixes Debian bug #818374)
This would have defaults move to backlog_always=false / log=true /
backlog_lines=0, which should not cause much trouble as backlog will
be reset after being displayed.

Also, it is doubtfuk anyone would be keeping the default of 10 for
backlog as it is pretty much an undesirable configuration.

We need to annouce this change as important though, so users having
log = false are aware memory usage could increase if they don't set
it manually to another value.

Signed-off-by: Loïc Gomez <bip@animanova.fr>
2024-02-17 03:29:45 +01:00
24 changed files with 135 additions and 296 deletions

2
.gitignore vendored
View File

@ -58,8 +58,6 @@ src/lex.c
# Binaries
src/*.o
src/utils/*.o
src/utils/*/*.o
src/*.a
src/bip
src/bipmkpw

View File

@ -10,6 +10,4 @@ Thanks to our marketting and management team:
ack|, ato, blackmore, lafouine, Gaston & gromit
Crypto shamelessly stolen from Christophe 'sexy' Devine.
Credits to Jouni Malinen for base64 library:
Source: http://w1.fi/cgit/hostap/commit/src/utils/base64.c
Credits to Jouni Malinen for base64 library (http://web.mit.edu/freebsd/head/contrib/wpa/src/utils/)

View File

@ -1,4 +1,4 @@
BIP copyright 2004-2022 Arnaud Cornet and Loïc Gomez, is released under the
bip copyright 2004 2005 Arnaud Cornet and Loïc Gomez, is released under the
terms of the GNU GPL, License that you can find below.
Specific permission is granted for the GPLed code in this distribution to

2
README
View File

@ -220,5 +220,5 @@ IV. USING BIP
Happy ircing!
-- Arnaud Cornet <nohar@t1r.net> and Loïc Gomez <bip@kyoshiro.org>
-- Arnaud Cornet <nohar@t1r.net> and Loïc Gomez <opensource@kyoshiro.org>

View File

@ -119,7 +119,7 @@ my %optdesc = (
'optional' => 1,
'depends' => 'log', 'depval' => 'true',
'desc' => 'Do you want to activate backlog {play back logs} system ?' },
'backlog_lines' => { 'type' => 'i', 'adv' => 0, 'default' => '10',
'backlog_lines' => { 'type' => 'i', 'adv' => 0, 'default' => '0',
'optional' => 1,
'depends' => 'backlog', 'depval' => 'true',
'desc' => 'How much line do you want bip to play back upon client connect' .

View File

@ -19,7 +19,7 @@ libbip_a_SOURCES = \
path_util.c path_util.h \
tuple.h \
util.c util.h \
utils/b64/base64.c utils/b64/base64.h
utils/base64.c utils/base64.h
libbip_a_CFLAGS = ${OPENSSL_CFLAGS} $(AM_CFLAGS)

View File

@ -39,7 +39,6 @@ int sighup = 0;
char *conf_log_root;
char *conf_log_format;
char *conf_timestamp_format;
int conf_log_level;
char *conf_ip;
unsigned short conf_port;
@ -162,8 +161,7 @@ void conf_die(bip_t *bip, char *fmt, ...)
error = bip_realloc(error, size);
}
va_start(ap, fmt);
/* log timestamp format is a conf option, we cannot log with time */
mylog_nots(LOG_ERROR, fmt, ap);
_mylog(LOG_ERROR, fmt, ap);
va_end(ap);
}
@ -710,13 +708,18 @@ struct link *find_link(const char *link_name, struct bipuser *bu)
{
list_iterator_t it;
// Immediately return NULL when bip user (bu) is NULL
if (!bu) {
return NULL;
}
for (list_it_init(&_bip->link_list, &it); list_it_item(&it);
list_it_next(&it)) {
struct link *lnk = list_it_item(&it);
if (!lnk)
continue;
// Only lookup in optionally specified user links
if (bu && bu != lnk->user)
if (bu != lnk->user)
continue;
if (strcmp(link_name, lnk->name) == 0)
@ -919,7 +922,6 @@ static int validate_config(bip_t *bip)
struct bipuser *user;
struct link *link;
struct chan_info *ci;
size_t len;
int r = 1;
for (hash_it_init(&bip->users, &it); (user = hash_it_item(&it));
@ -984,10 +986,6 @@ static int validate_config(bip_t *bip)
}
}
}
len = strlen(conf_timestamp_format);
if (len > TIMESTAMP_FORMAT_MAXLEN)
fatal("Timestamp format length exceeds maximum allowed length(%d)", TIMESTAMP_FORMAT_MAXLEN);
return r;
}
@ -1102,9 +1100,6 @@ int fireup(bip_t *bip, FILE *conf)
case LEX_LOG_LEVEL:
conf_log_level = t->ndata;
break;
case LEX_TIMESTAMP_FORMAT:
MOVE_STRING(conf_timestamp_format, t->pdata);
break;
case LEX_IP:
MOVE_STRING(conf_ip, t->pdata);
break;
@ -1401,10 +1396,9 @@ void adm_print_connection(struct link_client *ic, struct link *lnk,
bufpos = buf;
list_iterator_t itocs;
int i = 0;
for (list_it_init(&lnk->on_connect_send, &itocs);
list_it_item(&itocs) && i < 10; i++) {
bufpos = bip_strcatf_fit(&remaining, bufpos, " on_connect_send: %s",
list_it_item(&itocs);) {
bufpos = bip_strcatf_fit(&remaining, bufpos, "%s",
(char *)list_it_item(&itocs));
if (!bufpos) {
// if oversized, print and reset
@ -1412,7 +1406,6 @@ void adm_print_connection(struct link_client *ic, struct link *lnk,
bip_notify(ic, "%s", buf);
remaining = LINE_SIZE_LIM;
bufpos = buf;
list_it_next(&itocs);
continue;
} else {
// if ok, go to next item
@ -2121,10 +2114,10 @@ void adm_bip_help(struct link_client *ic, int admin, const char *subhelp)
bip_notify(ic, "/BIP JUMP [-f] [conn_name]:");
bip_notify(ic, " Jump to next server in current network.");
bip_notify(ic,
" If conn_name is set, jump to next server in "
"conn_name network instead.");
" If [conn_name] is set, jump to next server in "
"[conn_name] network instead.");
bip_notify(ic,
" If -f flag is used, also resets reconnect "
" If [-f] flag is used, also resets reconnect "
"timer.");
} else if (strcasecmp(subhelp, "BLRESET") == 0) {
bip_notify(ic, "/BIP BLRESET :");

View File

@ -39,7 +39,6 @@
extern int sighup;
extern char *conf_log_root;
extern char *conf_log_format;
extern char *conf_timestamp_format;
extern int conf_log_level;
extern char *conf_ip;
extern unsigned short conf_port;
@ -171,7 +170,6 @@ int main(int argc, char **argv)
conf_log_root = NULL;
conf_log_format = bip_strdup(DEFAULT_LOG_FORMAT);
conf_log_level = DEFAULT_LOG_LEVEL;
conf_timestamp_format = bip_strdup(DEFAULT_TIMESTAMP_FORMAT);
conf_reconn_timer = DEFAULT_RECONN_TIMER;
conf_daemonize = 1;
conf_global_log_file = stderr;

View File

@ -69,7 +69,7 @@ struct tuple *tuple_l_new(int type, void *p)
%}
%token LEX_IP LEX_EQ LEX_PORT LEX_CSS LEX_SEMICOLON LEX_CONNECTION LEX_NETWORK LEX_LBRA LEX_RBRA LEX_USER LEX_NAME LEX_NICK LEX_SERVER LEX_PASSWORD LEX_SRCIP LEX_HOST LEX_VHOST LEX_SOURCE_PORT LEX_NONE LEX_COMMENT LEX_BUNCH LEX_REALNAME LEX_SSL LEX_SSL_CHECK_MODE LEX_SSL_CHECK_STORE LEX_SSL_CLIENT_CERTFILE LEX_CIPHERS LEX_CSS_CIPHERS LEX_DEFAULT_CIPHERS LEX_DH_PARAM LEX_CHANNEL LEX_KEY LEX_LOG_ROOT LEX_LOG_FORMAT LEX_LOG_LEVEL LEX_BACKLOG_LINES LEX_BACKLOG_TIMESTAMP LEX_BACKLOG_NO_TIMESTAMP LEX_BACKLOG LEX_LOG LEX_LOG_SYSTEM LEX_LOG_SYNC_INTERVAL LEX_FOLLOW_NICK LEX_ON_CONNECT_SEND LEX_AWAY_NICK LEX_PID_FILE LEX_WRITE_OIDENTD LEX_OIDENTD_FILE LEX_IGN_FIRST_NICK LEX_ALWAYS_BACKLOG LEX_BLRESET_ON_TALK LEX_BLRESET_CONNECTION LEX_DEFAULT_USER LEX_DEFAULT_NICK LEX_DEFAULT_REALNAME LEX_NO_CLIENT_AWAY_MSG LEX_BL_MSG_ONLY LEX_ADMIN LEX_BIP_USE_NOTICE LEX_CSS_PEM LEX_AUTOJOIN_ON_KICK LEX_IGNORE_CAPAB LEX_RECONN_TIMER LEX_SASL_USERNAME LEX_SASL_PASSWORD LEX_SASL_MECHANISM LEX_TIMESTAMP_FORMAT
%token LEX_IP LEX_EQ LEX_PORT LEX_CSS LEX_SEMICOLON LEX_CONNECTION LEX_NETWORK LEX_LBRA LEX_RBRA LEX_USER LEX_NAME LEX_NICK LEX_SERVER LEX_PASSWORD LEX_SRCIP LEX_HOST LEX_VHOST LEX_SOURCE_PORT LEX_NONE LEX_COMMENT LEX_BUNCH LEX_REALNAME LEX_SSL LEX_SSL_CHECK_MODE LEX_SSL_CHECK_STORE LEX_SSL_CLIENT_CERTFILE LEX_CIPHERS LEX_CSS_CIPHERS LEX_DEFAULT_CIPHERS LEX_DH_PARAM LEX_CHANNEL LEX_KEY LEX_LOG_ROOT LEX_LOG_FORMAT LEX_LOG_LEVEL LEX_BACKLOG_LINES LEX_BACKLOG_TIMESTAMP LEX_BACKLOG_NO_TIMESTAMP LEX_BACKLOG LEX_LOG LEX_LOG_SYSTEM LEX_LOG_SYNC_INTERVAL LEX_FOLLOW_NICK LEX_ON_CONNECT_SEND LEX_AWAY_NICK LEX_PID_FILE LEX_WRITE_OIDENTD LEX_OIDENTD_FILE LEX_IGN_FIRST_NICK LEX_ALWAYS_BACKLOG LEX_BLRESET_ON_TALK LEX_BLRESET_CONNECTION LEX_DEFAULT_USER LEX_DEFAULT_NICK LEX_DEFAULT_REALNAME LEX_NO_CLIENT_AWAY_MSG LEX_BL_MSG_ONLY LEX_ADMIN LEX_BIP_USE_NOTICE LEX_CSS_PEM LEX_AUTOJOIN_ON_KICK LEX_IGNORE_CAPAB LEX_RECONN_TIMER LEX_SASL_USERNAME LEX_SASL_PASSWORD LEX_SASL_MECHANISM
%union {
int number;
@ -108,7 +108,6 @@ command:
| LEX_LOG_SYNC_INTERVAL LEX_EQ LEX_INT { $$ = tuple_i_new(
LEX_LOG_SYNC_INTERVAL, $3); }
| LEX_PID_FILE LEX_EQ LEX_STRING { $$ = tuple_s_new(LEX_PID_FILE, $3); }
| LEX_TIMESTAMP_FORMAT LEX_EQ LEX_STRING { $$ = tuple_s_new(LEX_TIMESTAMP_FORMAT, $3); }
| LEX_WRITE_OIDENTD LEX_EQ LEX_BOOL { $$ = tuple_i_new(LEX_WRITE_OIDENTD, $3); }
| LEX_OIDENTD_FILE LEX_EQ LEX_STRING { $$ = tuple_s_new(LEX_OIDENTD_FILE, $3); }
/* deprecated */

View File

@ -62,7 +62,7 @@ void connection_close(connection_t *cn)
{
mylog(LOG_DEBUG, "Connection close asked. FD:%d (status: %d)",
(long)cn->handle, cn->connected);
if (cn->connected != CONN_DISCONN) {
if (cn->connected != CONN_DISCONN && cn->connected != CONN_ERROR) {
cn->connected = CONN_DISCONN;
if (close(cn->handle) == -1)
mylog(LOG_WARN, "Error on socket close: %s",
@ -1171,7 +1171,6 @@ static connection_t *connection_init(int anti_flood, int ssl, time_t timeout,
static int ctx_set_dh(SSL_CTX *ctx)
{
/* Return ephemeral DH parameters. */
#if OPENSSL_VERSION_NUMBER < 0x30000000L /* 3.0.0 */
DH *dh = NULL;
FILE *f;
long ret;
@ -1204,31 +1203,7 @@ static int ctx_set_dh(SSL_CTX *ctx)
ERR_error_string(ERR_get_error(), NULL));
return 0;
}
#else
BIO *pbio = BIO_new_file(conf_client_dh_file, "r");
if (!pbio) {
mylog(LOG_ERROR, "Unable to open DH parameters, BIO_new_file(%s): %s",
conf_client_dh_file, ERR_error_string(ERR_get_error(), NULL));
return 0;
}
EVP_PKEY *param = PEM_read_bio_Parameters(pbio, NULL);
BIO_free(pbio);
if (!param) {
mylog(LOG_ERROR, "TLS DH Error: PEM_read_bio_Parameters(%s): %s",
conf_client_dh_file, ERR_error_string(ERR_get_error(), NULL));
return 0;
}
if (SSL_CTX_set0_tmp_dh_pkey(ctx, param) != 1) {
EVP_PKEY_free(param);
mylog(LOG_ERROR, "TLS DH Error: SSL_CTX_set0_tmp_dh_pkey(%s): %s",
conf_client_dh_file, ERR_error_string(ERR_get_error(), NULL));
return 0;
}
#endif
mylog(LOG_DEBUG, "TLS: succesfully set up DH params %s",
conf_client_dh_file);
return 1;
}
#endif

View File

@ -28,8 +28,6 @@
#define DEFAULT_LOG_SYNC_INTERVAL 5
#define DEFAULT_LOG_LEVEL LOG_INFO
#define DEFAULT_LOG_FORMAT "%u/%n/%Y-%m/%c.%d.log"
/* keep dd-mm-yyyy as default since we used that before adding the option */
#define DEFAULT_TIMESTAMP_FORMAT "%d-%m-%Y %H:%M:%S"
#define DEFAULT_BIP_USE_NOTICE 0
#define DEFAULT_RECONN_TIMER 30

View File

@ -22,7 +22,7 @@
#include "log.h"
#include "connection.h"
#include "md5.h"
#include "utils/b64/base64.h"
#include "utils/base64.h"
// TODO resolve assuming signed overflow does not occur when changing X +- C1
// cmp C2 to X cmp C2 -+ C1
@ -1030,31 +1030,6 @@ static int irc_cli_pass(bip_t *bip, struct link_client *ic, struct line *line)
return OK_FORGET;
}
static int irc_cli_cap(bip_t *bip, struct link_client *ic, struct line *line)
{
if (irc_line_count(line) < 2)
return ERR_PROTOCOL;
/* When we decide to enable actual capabilities, we'll have to handle CAP
* version, add a flag cap_started or similar, and handle CAP END.
*/
if (irc_line_elem_equals(line, 1, "LS")) {
WRITE_LINE2(CONN(ic), NULL, "CAP", "*", "LS");
} else if (irc_line_elem_equals(line, 1, "LIST")) {
WRITE_LINE3(CONN(ic), NULL, "CAP", "*", "LIST", "");
} else if (irc_line_elem_equals(line, 1, "REQ")) {
char *caps = irc_line_to_string_skip(line, 2);
if (caps) {
caps++;
WRITE_LINE3(CONN(ic), NULL, "CAP", "*", "NAK", caps);
}
}
if ((ic->state & IRCC_READY) == IRCC_READY)
return irc_cli_startup(bip, ic, line);
return OK_FORGET;
}
static int irc_cli_quit(struct link_client *ic, struct line *line)
{
(void)ic;
@ -1433,8 +1408,6 @@ static int irc_dispatch_logging_client(bip_t *bip, struct link_client *ic,
return irc_cli_user(bip, ic, line);
} else if (irc_line_elem_equals(line, 0, "PASS")) {
return irc_cli_pass(bip, ic, line);
} else if (irc_line_elem_equals(line, 0, "CAP")) {
return irc_cli_cap(bip, ic, line);
}
return OK_FORGET;
}
@ -2209,16 +2182,15 @@ static int irc_server_sasl_authenticate(struct link_server *ircs)
size_t p_len = strlen(sasl_password);
size_t raw_len = u_len * 2 + p_len + 2;
size_t enc_len;
char *raw_str = bip_malloc(raw_len + 1);
char *enc_str;
unsigned char *raw_str = bip_malloc(raw_len + 1);
unsigned char *enc_str;
memcpy(raw_str, sasl_username, u_len);
raw_str[u_len] = '\0';
memcpy(raw_str + u_len + 1, sasl_username, u_len);
raw_str[u_len * 2 + 1] = '\0';
memcpy(raw_str + u_len * 2 + 2, sasl_password, p_len);
enc_str = base64_encode_no_lf(raw_str, raw_len, &enc_len);
free(raw_str);
enc_str = base64_encode(raw_str, raw_len, &enc_len);
mylog(LOG_DEBUG, "[%s] Base64 encoded SASL auth token (len %d): %s",
LINK(ircs)->name, enc_len, enc_str);

View File

@ -90,7 +90,6 @@ list_t *parse_conf(FILE *file, int *err)
"log_level" { return LEX_LOG_LEVEL; }
"log_root" { return LEX_LOG_ROOT; }
"log_format" { return LEX_LOG_FORMAT; }
"timestamp_format" { return LEX_TIMESTAMP_FORMAT; }
"backlog_lines" { return LEX_BACKLOG_LINES; }
"backlog_timestamp" { return LEX_BACKLOG_TIMESTAMP; }
"backlog_no_timestamp" { return LEX_BACKLOG_NO_TIMESTAMP; }

View File

@ -16,8 +16,6 @@
#include "line.h"
#include "util.h"
char *_irc_line_to_string(struct line *l, int skip_first);
// TODO resolve assuming signed overflow does not occur when changing X +- C1
// cmp C2 to X cmp C2 -+ C1
#pragma GCC diagnostic ignored "-Wstrict-overflow"
@ -78,19 +76,6 @@ void irc_line_append(struct line *l, const char *s)
}
char *irc_line_to_string(struct line *l)
{
return _irc_line_to_string(l, 0);
}
char *irc_line_to_string_skip(struct line *l, int skip_first)
{
if (skip_first >= irc_line_count(l)) {
return NULL;
}
return _irc_line_to_string(l, skip_first);
}
char *_irc_line_to_string(struct line *l, int skip_first)
{
size_t len = 0;
int i;
@ -98,7 +83,7 @@ char *_irc_line_to_string(struct line *l, int skip_first)
if (l->origin)
len = strlen(l->origin) + 2;
for (i = skip_first; i < array_count(&l->words); i++)
for (i = 0; i < array_count(&l->words); i++)
len += strlen(array_get(&l->words, i)) + 1;
len += 1; /* remove one trailing space and add \r\n */
len++; /* last args ":" */
@ -110,7 +95,7 @@ char *_irc_line_to_string(struct line *l, int skip_first)
strcat(ret, l->origin);
strcat(ret, " ");
}
for (i = skip_first; i < array_count(&l->words) - 1; i++) {
for (i = 0; i < array_count(&l->words) - 1; i++) {
strcat(ret, array_get(&l->words, i));
strcat(ret, " ");
}
@ -158,11 +143,10 @@ void irc_line_drop(struct line *line, int elem)
bip_cfree(array_drop(&line->words, elem));
}
unsigned int irc_line_is_error(struct line *line)
int irc_line_is_error(struct line *line)
{
const char *irc_code = irc_line_elem(line, 0);
const char *error_code = "4";
return (irc_code[0] == error_code[0]);
return (irc_code[0] == '4');
}
int irc_line_elem_equals(struct line *line, int elem, const char *cmp)

View File

@ -90,7 +90,6 @@ void irc_line_write(struct line *l, connection_t *c);
void irc_line_append(struct line *l, const char *s);
struct line *irc_line_new_from_string(char *str);
char *irc_line_to_string(struct line *l);
char *irc_line_to_string_skip(struct line *l, int skip_first);
char *irc_line_to_string_to(struct line *line, char *nick);
void irc_line_free(struct line *l);
struct line *irc_line_dup(struct line *line);
@ -99,7 +98,7 @@ int irc_line_includes(struct line *line, int elem);
const char *irc_line_elem(struct line *line, int elem);
int irc_line_count(struct line *line);
char *irc_line_pop(struct line *l);
unsigned int irc_line_is_error(struct line *line);
int irc_line_is_error(struct line *line);
int irc_line_elem_equals(struct line *line, int elem, const char *cmp);
int irc_line_elem_case_equals(struct line *line, int elem, const char *cmp);
void irc_line_drop(struct line *line, int elem);

View File

@ -27,7 +27,6 @@ extern int errno;
extern int log_level;
extern char *conf_log_root;
extern char *conf_log_format;
extern char *conf_timestamp_format;
extern int conf_log;
extern FILE *conf_global_log_file;
@ -1031,7 +1030,7 @@ static time_t compute_time(const char *buf)
time(&tv);
tm = *localtime(&tv);
if (strptime(buf, conf_timestamp_format, &tm) == NULL)
if (strptime(buf, "%d-%m-%Y %H:%M:%S", &tm) == NULL)
return (time_t)-1;
return mktime(&tm);
}

View File

@ -29,13 +29,11 @@
extern int conf_log_level;
extern int conf_log_system;
extern char *conf_timestamp_format;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
extern int errno;
#pragma GCC diagnostic pop
extern FILE *conf_global_log_file;
void _mylog(int level, int with_timestamp, char *fmt, va_list ap);
void memory_fatal(void)
{
@ -224,46 +222,27 @@ int is_valid_username(char *str)
char *timestamp(void)
{
static char ts[20];
time_t tv;
struct tm *tm;
time(&tv);
tm = localtime(&tv);
return bip_strftime(tm);
strftime(ts, (size_t)20, "%d-%m-%Y %H:%M:%S", tm);
return ts;
}
char *hrtime(time_t s)
{
static char ts[20];
struct tm *tm;
if (s == 0)
return "never";
tm = localtime(&s);
return bip_strftime(tm);
}
char *bip_strftime(struct tm *tm)
{
static char ts[TIMESTAMP_FORMAT_MAXLEN];
size_t len, written;
ts[0] = '\0';
len = strlen(conf_timestamp_format);
if (len > TIMESTAMP_FORMAT_MAXLEN)
fatal_nots("Timestamp format itself exceeds "
"maximum allowed length(%d)", TIMESTAMP_FORMAT_MAXLEN);
/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=39438 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
written = strftime(ts, (size_t)TIMESTAMP_FORMAT_MAXLEN, conf_timestamp_format, tm);
#pragma GCC diagnostic pop
/* if format len is 0, format is empty and written will also return 0 */
if (!written && !errno && len != 0)
mylog_nots(LOG_WARN, "Truncated timestamp, result exceeds "
"maximum size of %d characters.", TIMESTAMP_FORMAT_MAXLEN);
strftime(ts, (size_t)20, "%d-%m-%Y %H:%M:%S", tm);
return ts;
}
@ -281,7 +260,7 @@ char *checkmode2text(int v)
}
#endif
void _mylog(int level, int with_timestamp, char *fmt, va_list ap)
void _mylog(int level, char *fmt, va_list ap)
{
char *prefix;
@ -315,11 +294,7 @@ void _mylog(int level, int with_timestamp, char *fmt, va_list ap)
break;
}
if (with_timestamp)
fprintf(conf_global_log_file, "%s %s", timestamp(), prefix);
else
fprintf(conf_global_log_file, "%s", prefix);
fprintf(conf_global_log_file, "%s %s", timestamp(), prefix);
vfprintf(conf_global_log_file, fmt, ap);
fprintf(conf_global_log_file, "\n");
#ifdef DEBUG
@ -332,16 +307,7 @@ void mylog(int level, char *fmt, ...)
va_list ap;
va_start(ap, fmt);
_mylog(level, 1, fmt, ap);
va_end(ap);
}
void mylog_nots(int level, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_mylog(level, 0, fmt, ap);
_mylog(level, fmt, ap);
va_end(ap);
}
@ -364,22 +330,7 @@ void fatal(char *fmt, ...)
va_list ap;
va_start(ap, fmt);
_mylog(LOG_FATAL, 1, fmt, ap);
va_end(ap);
#ifdef HAVE_BACKTRACE
dump_trace();
#endif
exit(200);
}
void fatal_nots(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_mylog(LOG_FATAL, 0, fmt, ap);
_mylog(LOG_FATAL, fmt, ap);
va_end(ap);
#ifdef HAVE_BACKTRACE

View File

@ -31,14 +31,10 @@
#define HASH_NOCASE 1
#define HASH_DEFAULT 0
#define TIMESTAMP_FORMAT_MAXLEN 50
void mylog(int level, char *fmt, ...);
void mylog_nots(int level, char *fmt, ...);
void _mylog(int level, char *fmt, va_list ap);
void fatal(char *fmt, ...);
void fatal_nots(char *fmt, ...);
char *timestamp(void);
char *bip_strftime(struct tm *tm);
struct hash_item;
struct list_item {

View File

@ -1,104 +0,0 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "includes.h"
#include <stdint.h>
#include "utils/common.h"
#include "os.h"
#include "base64.h"
static const char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define BASE64_PAD BIT(0)
#define BASE64_LF BIT(1)
static char * base64_gen_encode(const unsigned char *src, size_t len,
size_t *out_len, const char *table, int add_pad)
{
char *out, *pos;
const unsigned char *end, *in;
size_t olen;
int line_len;
if (len >= SIZE_MAX / 4)
return NULL;
olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
if (add_pad & BASE64_LF)
olen += olen / 72; /* line feeds */
olen++; /* nul termination */
if (olen < len)
return NULL; /* integer overflow */
out = os_malloc(olen);
if (out == NULL)
return NULL;
end = src + len;
in = src;
pos = out;
line_len = 0;
while (end - in >= 3) {
*pos++ = table[(in[0] >> 2) & 0x3f];
*pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
*pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
*pos++ = table[in[2] & 0x3f];
in += 3;
line_len += 4;
if ((add_pad & BASE64_LF) && line_len >= 72) {
*pos++ = '\n';
line_len = 0;
}
}
if (end - in) {
*pos++ = table[(in[0] >> 2) & 0x3f];
if (end - in == 1) {
*pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
if (add_pad & BASE64_PAD)
*pos++ = '=';
} else {
*pos++ = table[(((in[0] & 0x03) << 4) |
(in[1] >> 4)) & 0x3f];
*pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
}
if (add_pad & BASE64_PAD)
*pos++ = '=';
line_len += 4;
}
if ((add_pad & BASE64_LF) && line_len)
*pos++ = '\n';
*pos = '\0';
if (out_len)
*out_len = (size_t)(pos - out);
return out;
}
/**
* base64_encode - Base64 encode
* @src: Data to be encoded
* @len: Length of the data to be encoded
* @out_len: Pointer to output length variable, or %NULL if not used
* Returns: Allocated buffer of out_len bytes of encoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer. Returned buffer is
* nul terminated to make it easier to use as a C string. The nul terminator is
* not included in out_len.
*/
char * base64_encode_no_lf(const void *src, size_t len, size_t *out_len)
{
return base64_gen_encode(src, len, out_len, base64_table, BASE64_PAD);
}

View File

@ -1,4 +0,0 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

View File

@ -1,3 +0,0 @@
#ifndef os_malloc
#define os_malloc(s) malloc((s))
#endif

View File

@ -1,3 +0,0 @@
#ifndef BIT
#define BIT(x) (int)(1U << (x))
#endif

93
src/utils/base64.c Normal file
View File

@ -0,0 +1,93 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
* Copyright (c) 2022 Loïc Gomez
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* base64_encode - Base64 encode
* @src: Data to be encoded
* @len: Length of the data to be encoded
* @out_len: Pointer to output length variable, or %NULL if not used
* Returns: Allocated buffer of out_len bytes of encoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer. Returned buffer is
* nul terminated to make it easier to use as a C string. The nul terminator is
* not included in out_len.
*
* BIP change: remove line returns.
*/
unsigned char *base64_encode(const unsigned char *src, size_t len,
size_t *out_len)
{
unsigned char *out, *pos;
const unsigned char *end, *in;
size_t olen;
int line_len;
olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
olen += olen / 72; /* line feeds */
olen++; /* nul termination */
if (olen < len)
return NULL; /* integer overflow */
out = malloc(olen);
if (out == NULL)
return NULL;
end = src + len;
in = src;
pos = out;
line_len = 0;
while (end - in >= 3) {
*pos++ = base64_table[in[0] >> 2];
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = base64_table[in[2] & 0x3f];
in += 3;
line_len += 4;
/*
* BIP change: remove line returns.
if (line_len >= 72) {
*pos++ = '\n';
line_len = 0;
}
*/
}
if (end - in) {
*pos++ = base64_table[in[0] >> 2];
if (end - in == 1) {
*pos++ = base64_table[(in[0] & 0x03) << 4];
*pos++ = '=';
} else {
*pos++ = base64_table[((in[0] & 0x03) << 4)
| (in[1] >> 4)];
*pos++ = base64_table[(in[1] & 0x0f) << 2];
}
*pos++ = '=';
line_len += 4;
}
/*
* BIP change: remove line returns.
if (line_len)
*pos++ = '\n';
*/
*pos = '\0';
if (out_len)
*out_len = (size_t)(pos - out);
return out;
}

View File

@ -1,6 +1,7 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005, Jouni Malinen <j@w1.fi>
* Copyright (c) 2022 Loïc Gomez
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
@ -9,7 +10,7 @@
#ifndef BASE64_H
#define BASE64_H
char * base64_encode_no_lf(const void *src, size_t len, size_t *out_len);
unsigned char *base64_encode(const unsigned char *src, size_t len,
size_t *out_len);
#endif /* BASE64_H */