From f2fd1824caad008ad529a2860d8568208a0955e7 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Bonicoli Date: Thu, 10 Mar 2022 13:52:03 +0100 Subject: [PATCH] Format the whole code using clang-format --- src/bip.c | 836 ++++++++++++++++++++++++++------------------- src/bip_main.c | 69 ++-- src/bipmkpw.c | 15 +- src/connection.c | 482 ++++++++++++++------------ src/connection.h | 5 +- src/irc.c | 635 ++++++++++++++++++---------------- src/irc.h | 18 +- src/line.c | 5 +- src/line.h | 110 +++--- src/log.c | 275 +++++++-------- src/log.h | 31 +- src/md5.c | 480 +++++++++++++------------- src/md5.h | 17 +- src/util.c | 87 ++--- src/util.h | 64 ++-- src/utils/base64.c | 17 +- src/utils/base64.h | 4 +- 17 files changed, 1683 insertions(+), 1467 deletions(-) diff --git a/src/bip.c b/src/bip.c index 34584d4..ff30180 100644 --- a/src/bip.c +++ b/src/bip.c @@ -79,7 +79,7 @@ static void hash_binary(char *hex, unsigned char **password, unsigned int *seed) if (strlen(hex) != 40) fatal("Incorrect password format %s\n", hex); - md5 = bip_malloc((size_t) 20); + md5 = bip_malloc((size_t)20); for (i = 0; i < 20; i++) { sscanf(hex + 2 * i, "%02x", &buf); // conversion from ‘unsigned int’ to ‘unsigned char’ may change value @@ -149,12 +149,12 @@ void conf_die(bip_t *bip, char *fmt, ...) va_start(ap, fmt); n = vsnprintf(error, size, fmt, ap); va_end(ap); - if (n > -1 && (unsigned int) n < size) { + if (n > -1 && (unsigned int)n < size) { list_add_last(&bip->errors, error); break; } if (n > -1) - size = (unsigned int) n + 1; + size = (unsigned int)n + 1; else size *= 2; error = bip_realloc(error, size); @@ -184,12 +184,12 @@ try_again: fatal("%s %s", "gethostname", strerror(errno)); hname[511] = 0; snprintf(longpath, longpath_max - 1, "%s.%s.%ld", conf_pid_file, hname, - (long)getpid()); + (long)getpid()); longpath[longpath_max] = 0; - if ((fd = open(longpath, O_CREAT|O_WRONLY, S_IWUSR|S_IRUSR)) == -1) + if ((fd = open(longpath, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR)) == -1) fatal("Cannot write to PID file (%s) %s", longpath, - strerror(errno)); - if (link(longpath, conf_pid_file) == -1) { + strerror(errno)); + if (link(longpath, conf_pid_file) == -1) { struct stat buf; if (stat(longpath, &buf) == -1) { if (buf.st_nlink != 2) { @@ -201,46 +201,47 @@ try_again: unlink(longpath); free(longpath); return fd; -pid_is_there: - { - pid_t pid; - size_t p; - if (fd != -1) - close(fd); - if (f) { - int c = fscanf(f, "%zu", (size_t *)&p); - fclose(f); - pid = (pid_t)p; - if (c != 1 || p == 0) { - mylog(LOG_INFO, "pid file found but invalid " - "data inside. Continuing...\n"); - if (unlink(conf_pid_file)) { - fatal("Cannot delete pid file '%s', " - "check permissions.\n", - conf_pid_file); - } - goto try_again; - } - } else - pid = 0; - int kr = kill(pid, 0); - if (kr == -1 && (errno == ESRCH || errno == EPERM)) { - /* that's not bip! */ +pid_is_there : { + pid_t pid; + size_t p; + if (fd != -1) + close(fd); + if (f) { + int c = fscanf(f, "%zu", (size_t *)&p); + fclose(f); + pid = (pid_t)p; + if (c != 1 || p == 0) { + mylog(LOG_INFO, + "pid file found but invalid " + "data inside. Continuing...\n"); if (unlink(conf_pid_file)) { - fatal("Cannot delete pid file '%s', check " - "permissions.\n", - conf_pid_file); + fatal("Cannot delete pid file '%s', " + "check permissions.\n", + conf_pid_file); } goto try_again; } - if (pid) - mylog(LOG_INFO, "pid file found (pid %ld).", pid); - mylog(LOG_FATAL, - "Another instance of bip is certainly running."); - mylog(LOG_FATAL, "If you are sure this is not the case remove" - " %s.", conf_pid_file); - exit(2); + } else + pid = 0; + int kr = kill(pid, 0); + if (kr == -1 && (errno == ESRCH || errno == EPERM)) { + /* that's not bip! */ + if (unlink(conf_pid_file)) { + fatal("Cannot delete pid file '%s', check " + "permissions.\n", + conf_pid_file); + } + goto try_again; } + if (pid) + mylog(LOG_INFO, "pid file found (pid %ld).", pid); + mylog(LOG_FATAL, "Another instance of bip is certainly running."); + mylog(LOG_FATAL, + "If you are sure this is not the case remove" + " %s.", + conf_pid_file); + exit(2); +} free(longpath); return 0; } @@ -255,27 +256,30 @@ void reload_config(int i) void rlimit_cpu_reached(int i) { (void)i; - mylog(LOG_WARN, "This process has reached the CPU time usage limit. " - "It means bip will be killed by the Operating System soon."); + mylog(LOG_WARN, + "This process has reached the CPU time usage limit. " + "It means bip will be killed by the Operating System soon."); } void rlimit_bigfile_reached(int i) { (void)i; - mylog(LOG_WARN, "A file has reached the max size this process is " - "allowed to create. The file will not be written correctly, " - "an error message should follow. This is not fatal."); + mylog(LOG_WARN, + "A file has reached the max size this process is " + "allowed to create. The file will not be written correctly, " + "an error message should follow. This is not fatal."); } void bad_quit(int i) { list_iterator_t it; for (list_it_init(&_bip->link_list, &it); list_it_item(&it); - list_it_next(&it)) { + list_it_next(&it)) { struct link *l = list_it_item(&it); struct link_server *ls = l->l_server; if (ls && l->s_state == IRCS_CONNECTED) { - write_line_fast(CONN(ls), "QUIT :Coyote finally " + write_line_fast(CONN(ls), + "QUIT :Coyote finally " "caught me\r\n"); } } @@ -323,17 +327,19 @@ static int add_network(bip_t *bip, list_t *data) #endif case LEX_SERVER: if (n->serverc < 0) { - conf_die(bip, "internal error in network statement"); + conf_die(bip, + "internal error in network statement"); return 0; } - n->serverv = bip_realloc(n->serverv, - (unsigned int)(n->serverc + 1) * sizeof(struct server)); + n->serverv = bip_realloc( + n->serverv, (unsigned int)(n->serverc + 1) + * sizeof(struct server)); n->serverc++; memset(&n->serverv[n->serverc - 1], 0, - sizeof(struct server)); + sizeof(struct server)); r = add_server(bip, &n->serverv[n->serverc - 1], - t->pdata); + t->pdata); if (!r) { n->serverc--; return 0; @@ -382,7 +388,7 @@ void adm_bip_delconn(bip_t *bip, struct link_client *ic, const char *conn_name) } void adm_bip_addconn(bip_t *bip, struct link_client *ic, const char *conn_name, - const char *network_name) + const char *network_name) { struct bipuser *user = LINK(ic)->user; struct network *network; @@ -414,7 +420,8 @@ void adm_bip_addconn(bip_t *bip, struct link_client *ic, const char *conn_name, #endif -#define SCOPY(member) l->member = (LINK(ic)->member ? bip_strdup(LINK(ic)->member) : NULL) +#define SCOPY(member) \ + l->member = (LINK(ic)->member ? bip_strdup(LINK(ic)->member) : NULL) #define ICOPY(member) l->member = LINK(ic)->member SCOPY(connect_nick); @@ -471,7 +478,7 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data) l->network = hash_get(&bip->networks, t->pdata); if (!l->network) { conf_die(bip, "Undefined network %s.\n", - t->pdata); + t->pdata); return 0; } break; @@ -503,7 +510,8 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data) } else if (strcmp(t->pdata, "EXTERNAL") == 0) { l->sasl_mechanism = SASL_AUTH_EXTERNAL; } else { - conf_die(bip, "Unsupported SASL mechanism %s.", t->pdata); + conf_die(bip, "Unsupported SASL mechanism %s.", + t->pdata); return 0; } break; @@ -538,8 +546,10 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data) ci->backlog = t2->ndata; break; default: - conf_die(bip, "Unknown keyword in channel block (%d)", - t2->type); + conf_die( + bip, + "Unknown keyword in channel block (%d)", + t2->type); return 0; } if (t2->tuple_type == TUPLE_STR && t2->pdata) @@ -584,13 +594,15 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data) break; #else case LEX_SSL_CHECK_MODE: - mylog(LOG_WARN, "Found SSL option whereas bip is " - "not built with SSL support."); + mylog(LOG_WARN, + "Found SSL option whereas bip is " + "not built with SSL support."); break; #endif default: - conf_die(bip, "Unknown keyword in connection " - "statement"); + conf_die(bip, + "Unknown keyword in connection " + "statement"); return 0; } if (t->tuple_type == TUPLE_STR && t->pdata) @@ -611,16 +623,18 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data) } if (!l->username) { if (!user->default_username) { - conf_die(bip, "No username set and no default " - "username."); + conf_die(bip, + "No username set and no default " + "username."); return 0; } l->username = bip_strdup(user->default_username); } if (!l->realname) { if (!user->default_realname) { - conf_die(bip, "No realname set and no default " - "realname."); + conf_die(bip, + "No realname set and no default " + "realname."); return 0; } l->realname = bip_strdup(user->default_realname); @@ -636,8 +650,11 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data) return 0; } - if (l->sasl_mechanism == SASL_AUTH_PLAIN && (!l->sasl_username || !l->sasl_password)) { - conf_die(bip, "SASL mechanism PLAIN requires username and password."); + if (l->sasl_mechanism == SASL_AUTH_PLAIN + && (!l->sasl_username || !l->sasl_password)) { + conf_die( + bip, + "SASL mechanism PLAIN requires username and password."); return 0; } if (l->sasl_username && !l->sasl_mechanism) @@ -653,7 +670,7 @@ static char *get_tuple_pvalue(list_t *tuple_l, int lex) list_iterator_t it; for (list_it_init(tuple_l, &it); (t = list_it_item(&it)); - list_it_next(&it)) { + list_it_next(&it)) { if (t->type == lex) return t->pdata; } @@ -666,14 +683,15 @@ static int get_tuple_nvalue(list_t *tuple_l, int lex) list_iterator_t it; for (list_it_init(tuple_l, &it); (t = list_it_item(&it)); - list_it_next(&it)) { + list_it_next(&it)) { if (t->type == lex) return t->ndata; } return -1; } -enum BLTimestamp lex_backlog_timestamp(char * tdata) { +enum BLTimestamp lex_backlog_timestamp(char *tdata) +{ if (strcmp(tdata, "time") == 0) { return BLTSTime; } else if (strcmp(tdata, "datetime") == 0) { @@ -708,7 +726,7 @@ static int add_user(bip_t *bip, list_t *data, struct historical_directives *hds) } u = hash_get(&bip->users, name); if (!u) { - u = bip_calloc(sizeof(struct bipuser), (size_t) 1); + u = bip_calloc(sizeof(struct bipuser), (size_t)1); hash_insert(&bip->users, name, u); hash_init(&u->connections, HASH_NOCASE); u->admin = 0; @@ -803,8 +821,9 @@ static int add_user(bip_t *bip, list_t *data, struct historical_directives *hds) case LEX_SSL_CLIENT_CERTFILE: case LEX_SSL_CHECK_MODE: case LEX_SSL_CHECK_STORE: - mylog(LOG_WARN, "Found SSL option whereas bip is " - "not built with SSL support."); + mylog(LOG_WARN, + "Found SSL option whereas bip is " + "not built with SSL support."); break; #endif default: @@ -841,22 +860,21 @@ static int validate_config(bip_t *bip) int r = 1; for (hash_it_init(&bip->users, &it); (user = hash_it_item(&it)); - hash_it_next(&it)) { + hash_it_next(&it)) { for (hash_it_init(&user->connections, &sit); - (link = hash_it_item(&sit)); - hash_it_next(&sit)) { - if (!user->default_nick || !user->default_username || - !user->default_realname) { - if ((!link->username && - !user->default_username) || - (!link->connect_nick && - !user->default_nick) || - (!link->realname && - !user->default_realname)) { - conf_die(bip, "user %s, " - "connection %s: you must defin" - "e nick, user and realname.", - user->name, link->name); + (link = hash_it_item(&sit)); hash_it_next(&sit)) { + if (!user->default_nick || !user->default_username + || !user->default_realname) { + if ((!link->username && !user->default_username) + || (!link->connect_nick + && !user->default_nick) + || (!link->realname + && !user->default_realname)) { + conf_die(bip, + "user %s, " + "connection %s: you must defin" + "e nick, user and realname.", + user->name, link->name); link_kill(bip, link); r = 0; continue; @@ -864,14 +882,14 @@ static int validate_config(bip_t *bip) } for (hash_it_init(&link->chan_infos, &cit); - (ci = hash_it_item(&cit)); - hash_it_next(&cit)) { + (ci = hash_it_item(&cit)); hash_it_next(&cit)) { if (!ci->name) { - conf_die(bip, "user %s, " - "connection " - "%s: channel must have" - "a name.", user->name, - link->name); + conf_die(bip, + "user %s, " + "connection " + "%s: channel must have" + "a name.", + user->name, link->name); r = 0; continue; } @@ -879,11 +897,12 @@ static int validate_config(bip_t *bip) } if (user->backlog && !conf_log && user->backlog_lines == 0) { - conf_die(bip, "If conf_log = false, you must set " - "backlog_" - "lines to a non-nul value for each user with" - "backlog = true. Faulty user is %s", - user->name); + conf_die(bip, + "If conf_log = false, you must set " + "backlog_" + "lines to a non-nul value for each user with" + "backlog = true. Faulty user is %s", + user->name); r = 0; continue; } @@ -896,8 +915,9 @@ static int validate_config(bip_t *bip) hash_it_next(&it); if (hash_it_item(&it)) { // hash contains at least two elements - mylog(LOG_WARN, "log_format does not contain %%u, all users'" - " logs will be mixed !"); + mylog(LOG_WARN, + "log_format does not contain %%u, all users'" + " logs will be mixed !"); } } } @@ -910,10 +930,10 @@ void clear_marks(bip_t *bip) hash_iterator_t hit; for (list_it_init(&bip->link_list, &lit); list_it_item(&lit); - list_it_next(&lit)) + list_it_next(&lit)) ((struct link *)list_it_item(&lit))->in_use = 0; for (hash_it_init(&bip->users, &hit); hash_it_item(&hit); - hash_it_next(&hit)) + hash_it_next(&hit)) ((struct bipuser *)hash_it_item(&hit))->in_use = 0; } @@ -941,18 +961,18 @@ void sweep(bip_t *bip) hash_iterator_t hit; for (list_it_init(&bip->link_list, &lit); list_it_item(&lit); - list_it_next(&lit)) { + list_it_next(&lit)) { struct link *l = ((struct link *)list_it_item(&lit)); if (!l->in_use) { mylog(LOG_INFO, "Administratively killing %s/%s", - l->user->name, l->name); + l->user->name, l->name); list_remove_if_exists(&bip->conn_list, l); link_kill(bip, l); list_it_remove(&lit); } } for (hash_it_init(&bip->users, &hit); hash_it_item(&hit); - hash_it_next(&hit)) { + hash_it_next(&hit)) { struct bipuser *u = (struct bipuser *)hash_it_item(&hit); if (!u->in_use) { hash_it_remove(&hit); @@ -979,13 +999,14 @@ int fireup(bip_t *bip, FILE *conf) return 0; } -#define SET_HV(d, n) do {\ - int __gtv = get_tuple_nvalue(root_list, LEX_##n);\ - if (__gtv != -1) \ - d = __gtv;\ - else\ - d = DEFAULT_##n;\ - } while(0); +#define SET_HV(d, n) \ + do { \ + int __gtv = get_tuple_nvalue(root_list, LEX_##n); \ + if (__gtv != -1) \ + d = __gtv; \ + else \ + d = DEFAULT_##n; \ + } while (0); SET_HV(hds.always_backlog, ALWAYS_BACKLOG); SET_HV(hds.backlog, BACKLOG); SET_HV(hds.bl_msg_only, BL_MSG_ONLY); @@ -1045,8 +1066,9 @@ int fireup(bip_t *bip, FILE *conf) case LEX_CSS_CIPHERS: case LEX_CSS_PEM: case LEX_DH_PARAM: - mylog(LOG_WARN, "Found SSL option whereas bip is " - "not built with SSL support."); + mylog(LOG_WARN, + "Found SSL option whereas bip is " + "not built with SSL support."); break; #endif case LEX_PID_FILE: @@ -1093,7 +1115,7 @@ int fireup(bip_t *bip, FILE *conf) break; default: conf_die(bip, "Config error in base config (%d)", - t->type); + t->type); goto out_conf_error; } if (t->tuple_type == TUPLE_STR && t->pdata) @@ -1126,11 +1148,12 @@ void check_rlimits(void) r = getrlimit(RLIMIT_AS, <); if (r) { mylog(LOG_ERROR, "getrlimit(): failed with %s", - strerror(errno)); + strerror(errno)); } else { if (lt.rlim_max != RLIM_INFINITY) { - mylog(LOG_WARN, "virtual memory rlimit active, " - "bip may be KILLED by the system"); + mylog(LOG_WARN, + "virtual memory rlimit active, " + "bip may be KILLED by the system"); cklim = 1; } } @@ -1139,11 +1162,12 @@ void check_rlimits(void) r = getrlimit(RLIMIT_CPU, <); if (r) { mylog(LOG_ERROR, "getrlimit(): failed with %s", - strerror(errno)); + strerror(errno)); } else { if (lt.rlim_max != RLIM_INFINITY) { - mylog(LOG_WARN, "CPU rlimit active, bip may " - "be OFTEN KILLED by the system"); + mylog(LOG_WARN, + "CPU rlimit active, bip may " + "be OFTEN KILLED by the system"); cklim = 1; } } @@ -1151,12 +1175,14 @@ void check_rlimits(void) r = getrlimit(RLIMIT_FSIZE, <); if (r) { mylog(LOG_ERROR, "getrlimit(): failed with %s", - strerror(errno)); + strerror(errno)); } else { if (lt.rlim_max != RLIM_INFINITY) { - mylog(LOG_WARN, "FSIZE rlimit active, bip will " - "fail to create files of size greater than " - "%d bytes.", (int)lt.rlim_max); + mylog(LOG_WARN, + "FSIZE rlimit active, bip will " + "fail to create files of size greater than " + "%d bytes.", + (int)lt.rlim_max); cklim = 1; } } @@ -1164,12 +1190,14 @@ void check_rlimits(void) r = getrlimit(RLIMIT_NOFILE, <); if (r) { mylog(LOG_ERROR, "getrlimit(): failed with %s", - strerror(errno)); + strerror(errno)); } else { if (lt.rlim_max != RLIM_INFINITY && lt.rlim_max < 256) { - mylog(LOG_WARN, "opened files count rlimit " - "active, bip will not be allowed to open more " - "than %d files at a time", (int)lt.rlim_max); + mylog(LOG_WARN, + "opened files count rlimit " + "active, bip will not be allowed to open more " + "than %d files at a time", + (int)lt.rlim_max); cklim = 1; } } @@ -1177,11 +1205,12 @@ void check_rlimits(void) r = getrlimit(RLIMIT_STACK, <); if (r) { mylog(LOG_ERROR, "getrlimit(): failed with %s", - strerror(errno)); + strerror(errno)); } else { if (lt.rlim_max != RLIM_INFINITY) { - mylog(LOG_WARN, "stack rlimit active, " - "bip may be KILLED by the system"); + mylog(LOG_WARN, + "stack rlimit active, " + "bip may be KILLED by the system"); cklim = 1; } } @@ -1194,7 +1223,7 @@ void check_rlimits(void) #define LINE_SIZE_LIM 70 void adm_print_connection(struct link_client *ic, struct link *lnk, - struct bipuser *bu) + struct bipuser *bu) { hash_iterator_t lit; char buf[LINE_SIZE_LIM + 1]; @@ -1205,10 +1234,10 @@ void adm_print_connection(struct link_client *ic, struct link *lnk, bu = lnk->user; bip_notify(ic, "* %s to %s as \"%s\" (%s!%s) :", lnk->name, - lnk->network->name, - (lnk->realname ? lnk->realname : bu->default_realname), - (lnk->connect_nick ? lnk->connect_nick : bu->default_nick), - (lnk->username ? lnk->username : bu->default_username)); + lnk->network->name, + (lnk->realname ? lnk->realname : bu->default_realname), + (lnk->connect_nick ? lnk->connect_nick : bu->default_nick), + (lnk->username ? lnk->username : bu->default_username)); bufpos = bip_strcat_fit(&remaining, bufpos, " Options:"); // This should not happen, unless LINE_SIZE_LIM is too low @@ -1221,62 +1250,77 @@ void adm_print_connection(struct link_client *ic, struct link *lnk, buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcat_fit(&remaining, bufpos, " follow_nick"); + bufpos = bip_strcat_fit(&remaining, bufpos, + " follow_nick"); if (!bufpos) goto limittoolow; } } if (lnk->ignore_first_nick) { - bufpos = bip_strcat_fit(&remaining, bufpos, " ignore_first_nick"); + bufpos = bip_strcat_fit(&remaining, bufpos, + " ignore_first_nick"); if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcat_fit(&remaining, bufpos, " ignore_first_nick"); + bufpos = bip_strcat_fit(&remaining, bufpos, + " ignore_first_nick"); if (!bufpos) goto limittoolow; } } if (lnk->away_nick) { - bufpos = bip_strcatf_fit(&remaining, bufpos, " away_nick=%s", lnk->away_nick); + bufpos = bip_strcatf_fit(&remaining, bufpos, " away_nick=%s", + lnk->away_nick); if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcatf_fit(&remaining, bufpos, " away_nick=%s", lnk->away_nick); + bufpos = bip_strcatf_fit(&remaining, bufpos, + " away_nick=%s", + lnk->away_nick); if (!bufpos) goto limittoolow; } } if (lnk->no_client_away_msg) { - bufpos = bip_strcatf_fit(&remaining, bufpos, " no_client_away_msg=%s", lnk->no_client_away_msg); + bufpos = bip_strcatf_fit(&remaining, bufpos, + " no_client_away_msg=%s", + lnk->no_client_away_msg); if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcatf_fit(&remaining, bufpos, " no_client_away_msg=%s", lnk->no_client_away_msg); + bufpos = bip_strcatf_fit(&remaining, bufpos, + " no_client_away_msg=%s", + lnk->no_client_away_msg); if (!bufpos) goto limittoolow; } } if (lnk->vhost) { - bufpos = bip_strcatf_fit(&remaining, bufpos, " vhost=%s", lnk->vhost); + bufpos = bip_strcatf_fit(&remaining, bufpos, " vhost=%s", + lnk->vhost); if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcatf_fit(&remaining, bufpos, " vhost=%s", lnk->vhost); + bufpos = bip_strcatf_fit(&remaining, bufpos, + " vhost=%s", lnk->vhost); if (!bufpos) goto limittoolow; } } if (lnk->bind_port) { - bufpos = bip_strcatf_fit(&remaining, bufpos, " bind_port=%s", lnk->bind_port); + bufpos = bip_strcatf_fit(&remaining, bufpos, " bind_port=%s", + lnk->bind_port); if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcatf_fit(&remaining, bufpos, " bind_port=%s", lnk->bind_port); + bufpos = bip_strcatf_fit(&remaining, bufpos, + " bind_port=%s", + lnk->bind_port); if (!bufpos) goto limittoolow; } @@ -1288,9 +1332,9 @@ void adm_print_connection(struct link_client *ic, struct link *lnk, list_iterator_t itocs; for (list_it_init(&lnk->on_connect_send, &itocs); - list_it_item(&itocs); ) { + list_it_item(&itocs);) { bufpos = bip_strcatf_fit(&remaining, bufpos, "%s", - (char *)list_it_item(&itocs)); + (char *)list_it_item(&itocs)); if (!bufpos) { // if oversized, print and reset buf[LINE_SIZE_LIM] = 0; @@ -1311,17 +1355,18 @@ void adm_print_connection(struct link_client *ic, struct link *lnk, // TODO : check channels struct - bufpos = bip_strcat_fit(&remaining, bufpos, " Channels (* with key, ` no backlog)"); + bufpos = bip_strcat_fit(&remaining, bufpos, + " Channels (* with key, ` no backlog)"); if (!bufpos) goto limittoolow; for (hash_it_init(&lnk->chan_infos, &lit); hash_it_item(&lit); - hash_it_next(&lit)) { + hash_it_next(&lit)) { struct chan_info *ch = hash_it_item(&lit); - bufpos = bip_strcatf_fit(&remaining, bufpos, "%s%s%s", - ch->name, (ch->key ? "*" : ""), - (ch->backlog ? "" : "`")); + bufpos = bip_strcatf_fit(&remaining, bufpos, "%s%s%s", ch->name, + (ch->key ? "*" : ""), + (ch->backlog ? "" : "`")); if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); @@ -1339,42 +1384,44 @@ void adm_print_connection(struct link_client *ic, struct link *lnk, if (!bufpos) goto limittoolow; switch (lnk->s_state) { - case IRCS_NONE: + case IRCS_NONE: bufpos = bip_strcat_fit(&remaining, bufpos, "not started"); if (!bufpos) goto limittoolow; break; - case IRCS_CONNECTING: + case IRCS_CONNECTING: bufpos = bip_strcatf_fit(&remaining, bufpos, - "connecting... attempts: %d, last: %s", - lnk->s_conn_attempt, - hrtime(lnk->last_connection_attempt)); + "connecting... attempts: %d, last: %s", + lnk->s_conn_attempt, + hrtime(lnk->last_connection_attempt)); if (!bufpos) goto noroomstatus; break; - case IRCS_CONNECTED: + case IRCS_CONNECTED: bufpos = bip_strcat_fit(&remaining, bufpos, "connected !"); if (!bufpos) goto limittoolow; break; - case IRCS_WAS_CONNECTED: + case IRCS_WAS_CONNECTED: bufpos = bip_strcatf_fit(&remaining, bufpos, - "disconnected, attempts: %d, last: %s", - lnk->s_conn_attempt, - hrtime(lnk->last_connection_attempt)); + "disconnected, attempts: %d, last: %s", + lnk->s_conn_attempt, + hrtime(lnk->last_connection_attempt)); if (!bufpos) goto noroomstatus; break; - case IRCS_RECONNECTING: - bufpos = bip_strcatf_fit(&remaining, bufpos, + case IRCS_RECONNECTING: + bufpos = bip_strcatf_fit( + &remaining, bufpos, "reconnecting... attempts: %d, last: %s", lnk->s_conn_attempt, hrtime(lnk->last_connection_attempt)); if (!bufpos) goto noroomstatus; break; - case IRCS_TIMER_WAIT: - bufpos = bip_strcatf_fit(&remaining, bufpos, + case IRCS_TIMER_WAIT: + bufpos = bip_strcatf_fit( + &remaining, bufpos, "waiting to reconnect, attempts: %d, last: %s", lnk->s_conn_attempt, hrtime(lnk->last_connection_attempt)); @@ -1396,8 +1443,10 @@ noroomstatus: bip_notify(ic, "%stoo long to print", buf); return; limittoolow: - bip_notify(ic, "cannot print connection, LINE_SIZE_LIM(%d) " - "is too low (please recompile)", LINE_SIZE_LIM); + bip_notify(ic, + "cannot print connection, LINE_SIZE_LIM(%d) " + "is too low (please recompile)", + LINE_SIZE_LIM); return; } @@ -1407,7 +1456,7 @@ void adm_list_all_links(struct link_client *ic) bip_notify(ic, "-- All links"); for (list_it_init(&_bip->link_list, &it); list_it_item(&it); - list_it_next(&it)) { + list_it_next(&it)) { struct link *l = list_it_item(&it); if (l) adm_print_connection(ic, l, NULL); @@ -1421,7 +1470,7 @@ void adm_list_all_connections(struct link_client *ic) bip_notify(ic, "-- All connections"); for (hash_it_init(&_bip->users, &it); hash_it_item(&it); - hash_it_next(&it)) { + hash_it_next(&it)) { struct bipuser *u = hash_it_item(&it); if (u) adm_list_connections(ic, u); @@ -1442,34 +1491,41 @@ void adm_info_user(struct link_client *ic, const char *name) return; } - bip_notify(ic, "user: %s%s", u->name, (u->admin ? ", is bip admin" : "")); + bip_notify(ic, "user: %s%s", u->name, + (u->admin ? ", is bip admin" : "")); #ifdef HAVE_LIBSSL if (u->ssl_check_store) { bip_notify(ic, "SSL check mode '%s', stored into '%s'", - checkmode2text(u->ssl_check_mode), - u->ssl_check_store); - } - else { - bip_notify(ic, "SSL check mode '%s', default or no certificate store", - checkmode2text(u->ssl_check_mode)); + checkmode2text(u->ssl_check_mode), + u->ssl_check_store); + } else { + bip_notify( + ic, + "SSL check mode '%s', default or no certificate store", + checkmode2text(u->ssl_check_mode)); } if (u->ssl_client_certfile) bip_notify(ic, "SSL client certificate stored into '%s'", - u->ssl_client_certfile); + u->ssl_client_certfile); #endif bip_notify(ic, "Defaults nick: %s, user: %s, realname: %s", STRORNULL(u->default_nick), STRORNULL(u->default_username), STRORNULL(u->default_realname)); if (u->backlog) { - bip_notify(ic, "Backlog enabled, lines: %d, timestamp: %s," - " messages only: %s", u->backlog_lines, - u->backlog_timestamp == BLTSNone ? "none" : - (u->backlog_timestamp == BLTSTime ? "time" : "datetime"), - bool2text(u->bl_msg_only)); + bip_notify(ic, + "Backlog enabled, lines: %d, timestamp: %s," + " messages only: %s", + u->backlog_lines, + u->backlog_timestamp == BLTSNone + ? "none" + : (u->backlog_timestamp == BLTSTime + ? "time" + : "datetime"), + bool2text(u->bl_msg_only)); bip_notify(ic, "always backlog: %s, reset on talk: %s", - bool2text(u->always_backlog), - bool2text(u->blreset_on_talk)); + bool2text(u->always_backlog), + bool2text(u->blreset_on_talk)); } else { bip_notify(ic, "Backlog disabled"); } @@ -1486,42 +1542,51 @@ void adm_list_users(struct link_client *ic) bip_notify(ic, "-- User list"); for (hash_it_init(&_bip->users, &it); hash_it_item(&it); - hash_it_next(&it)) { + hash_it_next(&it)) { struct bipuser *u = hash_it_item(&it); int first = 1; char *bufpos = buf; bufpos = bip_strcatf_fit(&remaining, bufpos, "* %s%s:", u->name, - (u->admin ? "(admin)": "")); + (u->admin ? "(admin)" : "")); // this should not happen or LINE_SIZE_LIM is really low... if (!bufpos) goto limittoolow; - for (hash_it_init(&u->connections, &lit); hash_it_item(&lit); ) { + for (hash_it_init(&u->connections, &lit); hash_it_item(&lit);) { struct link *lnk = hash_it_item(&lit); if (first) { first = 0; } else { - bufpos = bip_strcat_fit(&remaining, bufpos, ","); - // if this is too long for a comma, print and prefix with spaces + bufpos = + bip_strcat_fit(&remaining, bufpos, ","); + // if this is too long for a comma, print and + // prefix with spaces if (!bufpos) { buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcat_fit(&remaining, buf, " ");; - // this should not happen or LINE_SIZE_LIM is really low... + bufpos = bip_strcat_fit(&remaining, buf, + " "); + ; + // this should not happen or + // LINE_SIZE_LIM is really low... if (!bufpos) goto limittoolow; } } - bufpos = bip_strcatf_fit(&remaining, bufpos, " %s", lnk->name); + bufpos = bip_strcatf_fit(&remaining, bufpos, " %s", + lnk->name); if (!bufpos) { // if this is too long, print and reset buf[LINE_SIZE_LIM] = 0; bip_notify(ic, "%s", buf); remaining = LINE_SIZE_LIM; - bufpos = bip_strcat_fit(&remaining, buf, " ");; - // this should not happen or LINE_SIZE_LIM is really low... + bufpos = bip_strcat_fit(&remaining, buf, + " "); + ; + // this should not happen or LINE_SIZE_LIM is + // really low... if (!bufpos) goto limittoolow; } else { @@ -1537,8 +1602,10 @@ void adm_list_users(struct link_client *ic) bip_notify(ic, "-- End of User list"); return; limittoolow: - bip_notify(ic, "cannot print users, LINE_SIZE_LIM(%d) " - "is too low (please recompile)", LINE_SIZE_LIM); + bip_notify(ic, + "cannot print users, LINE_SIZE_LIM(%d) " + "is too low (please recompile)", + LINE_SIZE_LIM); } void adm_list_networks(struct link_client *ic) @@ -1548,7 +1615,7 @@ void adm_list_networks(struct link_client *ic) bip_notify(ic, "-- Network list (* means SSL):"); for (hash_it_init(&_bip->networks, &it); hash_it_item(&it); - hash_it_next(&it)) { + hash_it_next(&it)) { struct network *n = hash_it_item(&it); int i; char *bufpos = buf; @@ -1556,17 +1623,18 @@ void adm_list_networks(struct link_client *ic) #ifdef HAVE_LIBSSL if (n->ssl) { - bufpos = bip_strcatf_fit(&remaining, bufpos, "- %s*:", - n->name); + bufpos = bip_strcatf_fit(&remaining, bufpos, + "- %s*:", n->name); } else { #endif - bufpos = bip_strcatf_fit(&remaining, bufpos, "- %s:", - n->name); + bufpos = bip_strcatf_fit(&remaining, bufpos, + "- %s:", n->name); #ifdef HAVE_LIBSSL } #endif // if we've reached max length, print name and reset - // honestly, this should not happen, but for the sake of cleanliness... + // honestly, this should not happen, but for the sake of + // cleanliness... if (!bufpos) { #ifdef HAVE_LIBSSL if (n->ssl) { @@ -1581,10 +1649,10 @@ void adm_list_networks(struct link_client *ic) remaining = RET_STR_LEN; } - for (i = 0; i < n->serverc; ) { - struct server *serv = i+n->serverv; + for (i = 0; i < n->serverc;) { + struct server *serv = i + n->serverv; bufpos = bip_strcatf_fit(&remaining, bufpos, " %s:%d", - serv->host, serv->port); + serv->host, serv->port); if (!bufpos) { // if line is too long, print and reset buf[RET_STR_LEN] = 0; @@ -1615,8 +1683,8 @@ void adm_list_connections(struct link_client *ic, struct bipuser *bu) } for (hash_it_init(&bu->connections, &it); hash_it_item(&it); - hash_it_next(&it)) { - struct link *lnk= hash_it_item(&it); + hash_it_next(&it)) { + struct link *lnk = hash_it_item(&it); adm_print_connection(ic, lnk, bu); } bip_notify(ic, "-- End of Connection list"); @@ -1630,7 +1698,7 @@ int link_add_untrusted(struct link_server *ls, X509 *cert) /* Check whether the cert is already in the stack */ for (i = 0; i < sk_X509_num(LINK(ls)->untrusted_certs); i++) { if (!X509_cmp(cert, - sk_X509_value(LINK(ls)->untrusted_certs, i))) + sk_X509_value(LINK(ls)->untrusted_certs, i))) return 1; } @@ -1647,11 +1715,11 @@ int ssl_check_trust(struct link_client *ic) unsigned int fplen; int i; - if(!LINK(ic)->untrusted_certs || - sk_X509_num(LINK(ic)->untrusted_certs) <= 0) { + if (!LINK(ic)->untrusted_certs + || sk_X509_num(LINK(ic)->untrusted_certs) <= 0) { ic->allow_trust = 0; return 0; - } + } trustcert = sk_X509_value(LINK(ic)->untrusted_certs, 0); strcpy(subject, "Subject: "); @@ -1661,33 +1729,33 @@ int ssl_check_trust(struct link_client *ic) X509_NAME_oneline(X509_get_issuer_name(trustcert), issuer + 9, 256); X509_digest(trustcert, EVP_md5(), fp, &fplen); - for(i = 0; i < (int)fplen; i++) - sprintf(fpstr + 17 + (i * 3), "%02X%c", - fp[i], (i == (int)fplen - 1) ? '\0' : ':'); + for (i = 0; i < (int)fplen; i++) + sprintf(fpstr + 17 + (i * 3), "%02X%c", fp[i], + (i == (int)fplen - 1) ? '\0' : ':'); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", - "This server SSL certificate was not " - "accepted because it is not in your store " - "of trusted certificates:"); + "This server SSL certificate was not " + "accepted because it is not in your store " + "of trusted certificates:"); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", subject); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", issuer); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", fpstr); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", - "WARNING: if you've already trusted a " - "certificate for this server before, that " - "probably means it has changed."); + "WARNING: if you've already trusted a " + "certificate for this server before, that " + "probably means it has changed."); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", - "If so, YOU MAY BE SUBJECT OF A " - "MAN-IN-THE-MIDDLE ATTACK! PLEASE DON'T TRUST " - "THIS CERTIFICATE IF YOU'RE NOT SURE THIS IS " - "NOT THE CASE."); + "If so, YOU MAY BE SUBJECT OF A " + "MAN-IN-THE-MIDDLE ATTACK! PLEASE DON'T TRUST " + "THIS CERTIFICATE IF YOU'RE NOT SURE THIS IS " + "NOT THE CASE."); WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", - "Type /QUOTE BIP TRUST OK to trust this " - "certificate, /QUOTE BIP TRUST NO to discard it."); + "Type /QUOTE BIP TRUST OK to trust this " + "certificate, /QUOTE BIP TRUST NO to discard it."); TYPE(ic) = IRC_TYPE_TRUST_CLIENT; ic->allow_trust = 1; @@ -1713,7 +1781,7 @@ int adm_trust(struct link_client *ic, struct line *line) if (ic->allow_trust != 1) { /* shouldn't have been asked to /QUOTE BIP TRUST but well... */ WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", "TrustEm", - "No untrusted certificates."); + "No untrusted certificates."); return OK_FORGET; } @@ -1725,19 +1793,22 @@ int adm_trust(struct link_client *ic, struct line *line) BIO *bio = BIO_new_file(LINK(ic)->user->ssl_check_store, "a+"); X509 *trustcert = sk_X509_shift(LINK(ic)->untrusted_certs); - if(!bio || !trustcert || - PEM_write_bio_X509(bio, trustcert) <= 0) - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + if (!bio || !trustcert + || PEM_write_bio_X509(bio, trustcert) <= 0) + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":==== Error while trusting test!\r\n"); else - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":==== Certificate now trusted.\r\n"); BIO_free_all(bio); X509_free(trustcert); } else if (irc_line_elem_case_equals(line, 2, "NO")) { /* NO, discard the cert! */ - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":==== Certificate discarded.\r\n"); X509_free(sk_X509_shift(LINK(ic)->untrusted_certs)); @@ -1745,10 +1816,12 @@ int adm_trust(struct link_client *ic, struct line *line) return ERR_PROTOCOL; if (!ssl_check_trust(ic)) { - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":No more certificates waiting awaiting " "user trust, thanks!\r\n"); - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":If the certificate is trusted, bip should " "be able to connect to the server on the " "next retry. Please wait a while and try " @@ -1773,8 +1846,9 @@ void _bip_notify(struct link_client *ic, char *fmt, va_list ap) vsnprintf(str, (size_t)4095, fmt, ap); str[4095] = 0; - WRITE_LINE2(CONN(ic), P_IRCMASK, (LINK(ic)->user->bip_use_notice ? - "NOTICE" : "PRIVMSG"), nick, str); + WRITE_LINE2(CONN(ic), P_IRCMASK, + (LINK(ic)->user->bip_use_notice ? "NOTICE" : "PRIVMSG"), + nick, str); } void bip_notify(struct link_client *ic, char *fmt, ...) @@ -1839,8 +1913,7 @@ void set_on_connect_send(struct link_client *ic, char *val) } #define ON_CONNECT_MAX_STRSIZE 1024 -void adm_on_connect_send(struct link_client *ic, struct line *line, - int privmsg) +void adm_on_connect_send(struct link_client *ic, struct line *line, int privmsg) { size_t remaining = ON_CONNECT_MAX_STRSIZE; char buf[ON_CONNECT_MAX_STRSIZE]; @@ -1853,24 +1926,27 @@ void adm_on_connect_send(struct link_client *ic, struct line *line, } if (!irc_line_includes(line, 2)) { - mylog(LOG_DEBUG, "[%s] not enough parameters on /BIP on_connect_send", - LINK(ic)->user->name); + mylog(LOG_DEBUG, + "[%s] not enough parameters on /BIP on_connect_send", + LINK(ic)->user->name); return; } for (i = privmsg + 2; i < irc_line_count(line); i++) { mylog(LOG_DEBUG, "[%s] processing item %d, remaining %ld, %s", - LINK(ic)->user->name, i, remaining, buf); + LINK(ic)->user->name, i, remaining, buf); if (i > privmsg + 2) bufpos = bip_strcatf_fit(&remaining, bufpos, " %s", - irc_line_elem(line, i)); + irc_line_elem(line, i)); else bufpos = bip_strcat_fit(&remaining, bufpos, - irc_line_elem(line, i)); + irc_line_elem(line, i)); mylog(LOG_DEBUG, "[%s] processed item %d, remaining %ld, %s", - LINK(ic)->user->name, i, remaining, buf); + LINK(ic)->user->name, i, remaining, buf); if (!bufpos) { - bip_notify(ic, "on connect send string too big, not changing."); + bip_notify( + ic, + "on connect send string too big, not changing."); return; } } @@ -1899,59 +1975,76 @@ void adm_bip_help(struct link_client *ic, int admin, const char *subhelp) { if (subhelp == NULL) { if (admin) { - bip_notify(ic, "/BIP RELOAD # Re-read bip " - "configuration and apply changes."); - bip_notify(ic, "/BIP INFO user " - "# show a user's configuration"); - bip_notify(ic, "/BIP LIST networks|users|connections|" - "all_links|all_connections"); - bip_notify(ic, "/BIP ADD_CONN " - ""); + bip_notify(ic, + "/BIP RELOAD # Re-read bip " + "configuration and apply changes."); + bip_notify(ic, + "/BIP INFO user " + "# show a user's configuration"); + bip_notify(ic, + "/BIP LIST networks|users|connections|" + "all_links|all_connections"); + bip_notify(ic, + "/BIP ADD_CONN " + ""); bip_notify(ic, "/BIP DEL_CONN "); } else { bip_notify(ic, "/BIP LIST networks|connections"); } - bip_notify(ic, "/BIP JUMP # jump to next server (in same " - "network)"); - bip_notify(ic, "/BIP BLRESET [channel|query]# reset backlog " - "(this connection only). Add -q flag and the " - "operation is quiet. You can specify a channel " - "or a nick to reset only this channel/query."); + bip_notify(ic, + "/BIP JUMP # jump to next server (in same " + "network)"); + bip_notify(ic, + "/BIP BLRESET [channel|query]# reset backlog " + "(this connection only). Add -q flag and the " + "operation is quiet. You can specify a channel " + "or a nick to reset only this channel/query."); bip_notify(ic, "/BIP HELP [subhelp] # show this help..."); bip_notify(ic, "## Temporary changes for this connection:"); bip_notify(ic, "/BIP FOLLOW_NICK|IGNORE_FIRST_NICK TRUE|FALSE"); - bip_notify(ic, "/BIP ON_CONNECT_SEND # Adds a string to " - "send on connect"); + bip_notify(ic, + "/BIP ON_CONNECT_SEND # Adds a string to " + "send on connect"); bip_notify(ic, "/BIP ON_CONNECT_SEND # Clears on_connect_send"); bip_notify(ic, "/BIP AWAY_NICK # Set away nick"); bip_notify(ic, "/BIP AWAY_NICK # clear away nick"); - bip_notify(ic, "/BIP BACKLOG [n] # backlog text of the n last " - "hours"); + bip_notify(ic, + "/BIP BACKLOG [n] # backlog text of the n last " + "hours"); } else if (admin && strcasecmp(subhelp, "RELOAD") == 0) { bip_notify(ic, "/BIP RELOAD (admin only) :"); - bip_notify(ic, " Reloads bip configuration file and apply " - "changes."); - bip_notify(ic, " Please note that changes to 'user' or " - "'realname' will not be applied without a JUMP."); + bip_notify(ic, + " Reloads bip configuration file and apply " + "changes."); + bip_notify(ic, + " Please note that changes to 'user' or " + "'realname' will not be applied without a JUMP."); } else if (admin && strcasecmp(subhelp, "INFO") == 0) { bip_notify(ic, "/BIP INFO USER (admin only) :"); bip_notify(ic, " Show 's current configuration."); - bip_notify(ic, " That means it may be different from the " - "configuration stored in bip.conf"); + bip_notify(ic, + " That means it may be different from the " + "configuration stored in bip.conf"); } else if (admin && strcasecmp(subhelp, "ADD_CONN") == 0) { - bip_notify(ic, "/BIP ADD_CONN " - "(admin only) :"); - bip_notify(ic, " Add a connection named to " - "the network to your connection list"); - bip_notify(ic, " should already exist in bip's " - "configuration."); + bip_notify(ic, + "/BIP ADD_CONN " + "(admin only) :"); + bip_notify(ic, + " Add a connection named to " + "the network to your connection list"); + bip_notify(ic, + " should already exist in bip's " + "configuration."); } else if (admin && strcasecmp(subhelp, "DEL_CONN") == 0) { - bip_notify(ic, "/BIP DEL_CONN (admin only) " - ":"); - bip_notify(ic, " Remove the connection named from your connection list."); - bip_notify(ic, " Removing a connection will cause " - "its disconnection."); + bip_notify(ic, + "/BIP DEL_CONN (admin only) " + ":"); + bip_notify(ic, + " Remove the connection named from your connection list."); + bip_notify(ic, + " Removing a connection will cause " + "its disconnection."); } else if (strcasecmp(subhelp, "JUMP") == 0) { bip_notify(ic, "/BIP JUMP :"); bip_notify(ic, " Jump to next server in current network."); @@ -1960,47 +2053,62 @@ void adm_bip_help(struct link_client *ic, int admin, const char *subhelp) bip_notify(ic, " Reset backlog on this network."); } else if (strcasecmp(subhelp, "FOLLOW_NICK") == 0) { bip_notify(ic, "/BIP FOLLOW_NICK TRUE|FALSE :"); - bip_notify(ic, " Change the value of the follow_nick option " - "for this connection."); - bip_notify(ic, " If set to true, when you change nick, " - "BIP stores the new nickname as the new default " - "nickname value."); - bip_notify(ic, " Thus, if you are disconnected from the " - "server, BIP will restore the correct nickname."); + bip_notify(ic, + " Change the value of the follow_nick option " + "for this connection."); + bip_notify(ic, + " If set to true, when you change nick, " + "BIP stores the new nickname as the new default " + "nickname value."); + bip_notify(ic, + " Thus, if you are disconnected from the " + "server, BIP will restore the correct nickname."); } else if (strcasecmp(subhelp, "IGNORE_FIRST_NICK") == 0) { bip_notify(ic, "/BIP IGNORE_FIRST_NICK TRUE|FALSE :"); - bip_notify(ic, " Change the value of the ignore_first_nick " - "option for this connection."); - bip_notify(ic, " If set to TRUE, BIP will ignore the nickname" - "sent by the client upon connect."); - bip_notify(ic, " Further nickname changes will be processed " - "as usual."); + bip_notify(ic, + " Change the value of the ignore_first_nick " + "option for this connection."); + bip_notify(ic, + " If set to TRUE, BIP will ignore the nickname" + "sent by the client upon connect."); + bip_notify(ic, + " Further nickname changes will be processed " + "as usual."); } else if (strcasecmp(subhelp, "ON_CONNECT_SEND") == 0) { bip_notify(ic, "/BIP ON_CONNECT_SEND [some text] :"); - bip_notify(ic, " BIP will send the text as is to the server " - "upon connection."); + bip_notify(ic, + " BIP will send the text as is to the server " + "upon connection."); bip_notify(ic, " You can call this command more than once."); - bip_notify(ic, " If [some text] is empty, this command will " + bip_notify( + ic, + " If [some text] is empty, this command will " "remove any on_connect_send defined for this connection."); } else if (strcasecmp(subhelp, "AWAY_NICK") == 0) { bip_notify(ic, "/BIP AWAY_NICK [some_nick] :"); - bip_notify(ic, " If [some_nick] is set, BIP will change " + bip_notify( + ic, + " If [some_nick] is set, BIP will change " "nickname to [some_nick] if there are no more client " "attached"); - bip_notify(ic, " If [some_nick] is empty, this command will " - "unset current connection's away_nick."); + bip_notify(ic, + " If [some_nick] is empty, this command will " + "unset current connection's away_nick."); } else if (strcasecmp(subhelp, "LIST") == 0) { bip_notify(ic, "/BIP LIST
:"); bip_notify(ic, " List information from a these sections :"); bip_notify(ic, " - networks: list all available networks"); - bip_notify(ic, " - connections: list all your configured " - "connections and their state."); + bip_notify(ic, + " - connections: list all your configured " + "connections and their state."); if (admin) { bip_notify(ic, " - users: list all users (admin)"); - bip_notify(ic, " - all_links: list all connected " - "sockets from and to BIP (admin)"); - bip_notify(ic, " - all_connections: list all users' " - "configured connections (admin)"); + bip_notify(ic, + " - all_links: list all connected " + "sockets from and to BIP (admin)"); + bip_notify(ic, + " - all_connections: list all users' " + "configured connections (admin)"); } } else { bip_notify(ic, "-- No sub-help for '%s'", subhelp); @@ -2025,7 +2133,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) /* all elem size <= linestr size */ elemstr = bip_malloc(strlen(linestr) + 1); - while((eptr = strstr(ptr, " "))) { + while ((eptr = strstr(ptr, " "))) { // eptr is either >= ptr or NULL from strstr() // but it can't be NULL per while loop // we can then assume slen is unsigned @@ -2054,7 +2162,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) return OK_FORGET; mylog(LOG_INFO, "/BIP %s from %s", irc_line_elem(line, privmsg + 1), - LINK(ic)->user->name); + LINK(ic)->user->name); if (irc_line_elem_case_equals(line, privmsg + 1, "RELOAD")) { if (!admin) { bip_notify(ic, "-- You're not allowed to reload bip"); @@ -2069,38 +2177,48 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) return OK_FORGET; } - if (admin && strcasecmp(irc_line_elem(line, privmsg + 2), - "users") == 0) { + if (admin + && strcasecmp(irc_line_elem(line, privmsg + 2), "users") + == 0) { adm_list_users(ic); } else if (strcasecmp(irc_line_elem(line, privmsg + 2), - "networks") == 0) { + "networks") + == 0) { adm_list_networks(ic); } else if (strcasecmp(irc_line_elem(line, privmsg + 2), - "connections") == 0) { + "connections") + == 0) { adm_list_connections(ic, NULL); - } else if (admin && strcasecmp(irc_line_elem(line, privmsg + 2), - "all_connections") == 0) { + } else if (admin + && strcasecmp(irc_line_elem(line, privmsg + 2), + "all_connections") + == 0) { adm_list_all_connections(ic); - } else if (admin && strcasecmp(irc_line_elem(line, privmsg + 2), - "all_links") == 0) { + } else if (admin + && strcasecmp(irc_line_elem(line, privmsg + 2), + "all_links") + == 0) { adm_list_all_links(ic); } else { bip_notify(ic, "-- Invalid LIST request"); } } else if (irc_line_elem_case_equals(line, privmsg + 1, "INFO")) { if (!irc_line_includes(line, privmsg + 3)) { - bip_notify(ic, "-- INFO command needs at least two " - "arguments"); + bip_notify(ic, + "-- INFO command needs at least two " + "arguments"); return OK_FORGET; } - if (admin && irc_line_elem_case_equals(line, privmsg + 2, "user")) { + if (admin + && irc_line_elem_case_equals(line, privmsg + 2, "user")) { if (irc_line_count(line) == (privmsg + 4)) { adm_info_user(ic, - irc_line_elem(line, privmsg + 3)); + irc_line_elem(line, privmsg + 3)); } else { - bip_notify(ic, "-- INFO USER command needs one" - " argument"); + bip_notify(ic, + "-- INFO USER command needs one" + " argument"); } #if 0 TODO network info @@ -2111,7 +2229,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) } else if (irc_line_elem_case_equals(line, privmsg + 1, "JUMP")) { if (LINK(ic)->l_server) { WRITE_LINE1(CONN(LINK(ic)->l_server), NULL, "QUIT", - "jumpin' jumpin'"); + "jumpin' jumpin'"); connection_close(CONN(LINK(ic)->l_server)); } bip_notify(ic, "-- Jumping to next server"); @@ -2119,15 +2237,16 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) if (irc_line_includes(line, privmsg + 2)) { if (irc_line_elem_equals(line, privmsg + 2, "-q")) { if (irc_line_includes(line, privmsg + 3)) { - log_reset_store(LINK(ic)->log, + log_reset_store( + LINK(ic)->log, irc_line_elem(line, - privmsg + 3)); + privmsg + 3)); } else { log_reset_all(LINK(ic)->log); } } else { - adm_blreset_store(ic, irc_line_elem(line, - privmsg + 2)); + adm_blreset_store( + ic, irc_line_elem(line, privmsg + 2)); } } else { adm_blreset(ic); @@ -2137,34 +2256,38 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) adm_bip_help(ic, admin, NULL); else if (irc_line_count(line) == privmsg + 3) adm_bip_help(ic, admin, - irc_line_elem(line, privmsg + 2)); + irc_line_elem(line, privmsg + 2)); else - bip_notify(ic, + bip_notify( + ic, "-- HELP command needs at most one argument"); - } else if (irc_line_elem_case_equals(line, privmsg + 1, "FOLLOW_NICK")) { + } else if (irc_line_elem_case_equals(line, privmsg + 1, + "FOLLOW_NICK")) { if (irc_line_count(line) != privmsg + 3) { bip_notify(ic, - "-- FOLLOW_NICK command needs one argument"); + "-- FOLLOW_NICK command needs one argument"); return OK_FORGET; } adm_follow_nick(ic, irc_line_elem(line, privmsg + 2)); } else if (irc_line_elem_case_equals(line, privmsg + 1, - "IGNORE_FIRST_NICK")) { + "IGNORE_FIRST_NICK")) { if (irc_line_count(line) != privmsg + 3) { - bip_notify(ic, "-- IGNORE_FIRST_NICK " - "command needs one argument"); + bip_notify(ic, + "-- IGNORE_FIRST_NICK " + "command needs one argument"); return OK_FORGET; } adm_ignore_first_nick(ic, irc_line_elem(line, privmsg + 2)); } else if (irc_line_elem_case_equals(line, privmsg + 1, - "ON_CONNECT_SEND")) { + "ON_CONNECT_SEND")) { if (irc_line_count(line) == privmsg + 2) { adm_on_connect_send(ic, NULL, 0); } else if (irc_line_includes(line, privmsg + 2)) { adm_on_connect_send(ic, line, privmsg); } else { - bip_notify(ic, "-- ON_CONNECT_SEND command needs at " - "least one argument"); + bip_notify(ic, + "-- ON_CONNECT_SEND command needs at " + "least one argument"); } } else if (irc_line_elem_case_equals(line, privmsg + 1, "AWAY_NICK")) { if (irc_line_count(line) == privmsg + 2) { @@ -2172,8 +2295,9 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) } else if (irc_line_count(line) == privmsg + 3) { adm_away_nick(ic, irc_line_elem(line, privmsg + 2)); } else { - bip_notify(ic, "-- AWAY_NICK command needs zero or one" - " argument"); + bip_notify(ic, + "-- AWAY_NICK command needs zero or one" + " argument"); } } else if (irc_line_elem_case_equals(line, privmsg + 1, "BACKLOG")) { if (irc_line_count(line) == privmsg + 2) { @@ -2184,18 +2308,21 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg) } else { bip_notify(ic, "-- BACKLOG takes 0 or one argument"); } - } else if (admin && irc_line_elem_case_equals(line, privmsg + 1, - "ADD_CONN")) { + } else if (admin + && irc_line_elem_case_equals(line, privmsg + 1, + "ADD_CONN")) { if (irc_line_count(line) != privmsg + 4) { - bip_notify(ic, "/BIP ADD_CONN " - ""); + bip_notify(ic, + "/BIP ADD_CONN " + ""); } else { adm_bip_addconn(bip, ic, irc_line_elem(line, privmsg + 2), irc_line_elem(line, privmsg + 3)); } - } else if (admin && irc_line_elem_case_equals(line, privmsg + 1, - "DEL_CONN")) { + } else if (admin + && irc_line_elem_case_equals(line, privmsg + 1, + "DEL_CONN")) { if (irc_line_count(line) != privmsg + 3) { bip_notify(ic, "/BIP DEL_CONN "); } else { @@ -2235,4 +2362,3 @@ void free_conf(list_t *l) } free(l); } - diff --git a/src/bip_main.c b/src/bip_main.c index 8582d34..90545d6 100644 --- a/src/bip_main.c +++ b/src/bip_main.c @@ -72,24 +72,25 @@ int do_pid_stuff(void); static void usage(char *name) { - printf( -"Usage: %s [-f config_file] [-h] [-n]\n" -" -f config_file: Use config_file as the configuration file\n" -" If no config file is given %s will try to open ~/.bip/" S_CONF "\n" -" -n: Don't daemonize, log in stderr\n" -" -s: Bip HOME, default parent directory for client certificate,\n" -" configuration, logs, pid, oidentd\n" -" -v: Print version and exit\n" -" -h: This help\n", name, name); + printf("Usage: %s [-f config_file] [-h] [-n]\n" + " -f config_file: Use config_file as the configuration file\n" + " If no config file is given %s will try to open ~/.bip/" S_CONF + "\n" + " -n: Don't daemonize, log in stderr\n" + " -s: Bip HOME, default parent directory for client certificate,\n" + " configuration, logs, pid, oidentd\n" + " -v: Print version and exit\n" + " -h: This help\n", + name, name); exit(1); } static void version(void) { - printf( -"Bip IRC Proxy - " PACKAGE_VERSION "\n" -"Copyright © Arnaud Cornet and Loïc Gomez (2004 - 2008)\n" -"Distributed under the GNU General Public License Version 2\n"); + printf("Bip IRC Proxy - " PACKAGE_VERSION + "\n" + "Copyright © Arnaud Cornet and Loïc Gomez (2004 - 2008)\n" + "Distributed under the GNU General Public License Version 2\n"); } static void log_file_setup(void) @@ -99,7 +100,7 @@ static void log_file_setup(void) if (conf_log_system && conf_daemonize) { if (conf_global_log_file && conf_global_log_file != stderr) fclose(conf_global_log_file); - snprintf(buf, (size_t) 4095, "%s/bip.log", conf_log_root); + snprintf(buf, (size_t)4095, "%s/bip.log", conf_log_root); FILE *f = fopen(buf, "a"); if (!f) fatal("Can't open %s: %s", buf, strerror(errno)); @@ -208,8 +209,9 @@ int main(int argc, char **argv) char *home = NULL; /* oidentd path searching ignores conf_biphome */ home = getenv("HOME"); if (!home && !conf_biphome) { - conf_die(&bip, "no value for environment variable $HOME," - "use '-s' parameter"); + conf_die(&bip, + "no value for environment variable $HOME," + "use '-s' parameter"); return 0; } @@ -220,16 +222,16 @@ int main(int argc, char **argv) } if (!bip.oidentdpath) { - bip.oidentdpath = bip_malloc(strlen(conf_biphome) + 1 + - strlen(OIDENTD_FILENAME) + 1); + bip.oidentdpath = bip_malloc(strlen(conf_biphome) + 1 + + strlen(OIDENTD_FILENAME) + 1); strcpy(bip.oidentdpath, conf_biphome); strcat(bip.oidentdpath, "/"); strcat(bip.oidentdpath, OIDENTD_FILENAME); } if (!confpath) { - confpath = bip_malloc(strlen(conf_biphome) + 1 + - strlen(S_CONF) + 1); + confpath = bip_malloc(strlen(conf_biphome) + 1 + strlen(S_CONF) + + 1); strcpy(confpath, conf_biphome); strcat(confpath, "/"); strcat(confpath, S_CONF); @@ -245,16 +247,16 @@ int main(int argc, char **argv) if (!conf_log_root) { char *ap = "/logs"; - conf_log_root = bip_malloc(strlen(conf_biphome) + - strlen(ap) + 1); + conf_log_root = + bip_malloc(strlen(conf_biphome) + strlen(ap) + 1); strcpy(conf_log_root, conf_biphome); strcat(conf_log_root, ap); mylog(LOG_INFO, "Default log root: %s", conf_log_root); } if (!conf_pid_file) { char *pid = "/bip.pid"; - conf_pid_file = bip_malloc(strlen(conf_biphome) + - strlen(pid) + 1); + conf_pid_file = + bip_malloc(strlen(conf_biphome) + strlen(pid) + 1); strcpy(conf_pid_file, conf_biphome); strcat(conf_pid_file, pid); mylog(LOG_INFO, "Default pid file: %s", conf_pid_file); @@ -266,19 +268,22 @@ int main(int argc, char **argv) struct stat fs; if (!conf_ssl_certfile) { - conf_ssl_certfile = default_path(conf_biphome, "bip.pem", - "SSL certificate"); + conf_ssl_certfile = default_path( + conf_biphome, "bip.pem", "SSL certificate"); } assert_path_exists(conf_ssl_certfile); e = stat(conf_ssl_certfile, &fs); if (e) - mylog(LOG_WARN, "Unable to check PEM file, stat(%s): " - "%s", conf_ssl_certfile, strerror(errno)); + mylog(LOG_WARN, + "Unable to check PEM file, stat(%s): " + "%s", + conf_ssl_certfile, strerror(errno)); else if ((fs.st_mode & S_IROTH) | (fs.st_mode & S_IWOTH)) - mylog(LOG_ERROR, "PEM file %s should not be world " - "readable / writable. Please fix the modes.", - conf_ssl_certfile); + mylog(LOG_ERROR, + "PEM file %s should not be world " + "readable / writable. Please fix the modes.", + conf_ssl_certfile); if (conf_client_dh_file) { assert_path_exists(conf_client_dh_file); @@ -295,7 +300,7 @@ int main(int argc, char **argv) pid = daemonize(); else pid = getpid(); - snprintf(buf, (size_t) 29, "%lu\n", (unsigned long int)pid); + snprintf(buf, (size_t)29, "%lu\n", (unsigned long int)pid); ssize_t written; written = write(fd, buf, strlen(buf)); if (written <= 0) diff --git a/src/bipmkpw.c b/src/bipmkpw.c index 959d3f8..416a2e0 100644 --- a/src/bipmkpw.c +++ b/src/bipmkpw.c @@ -45,10 +45,11 @@ void readpass(char *buffer, int buflen) bipmkpw_fatal("tcgetattr failed", strerror(errno)); memcpy(&tt, &ttback, sizeof(ttback)); -// unsigned conversion from ‘int’ to ‘tcflag_t’ {aka ‘unsigned int’} changes value from ‘-11’ to ‘4294967285’ +// unsigned conversion from ‘int’ to ‘tcflag_t’ {aka ‘unsigned int’} changes +// value from ‘-11’ to ‘4294967285’ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wsign-conversion" - tt.c_lflag &= ~(ICANON|ECHO); + tt.c_lflag &= ~(ICANON | ECHO); #pragma GCC diagnostic pop if (tcsetattr(ttyfd, TCSANOW, &tt) < 0) bipmkpw_fatal("tcsetattr failed", strerror(errno)); @@ -59,7 +60,7 @@ void readpass(char *buffer, int buflen) int idx = 0; int valid = 1; while (idx < buflen) { - ssize_t rbytes = read(ttyfd, buffer+idx, (size_t)1); + ssize_t rbytes = read(ttyfd, buffer + idx, (size_t)1); if (rbytes <= 0) { break; } @@ -94,9 +95,9 @@ int main(void) readpass(str, 256); str[255] = 0; -// passing argument 1 of ‘srand’ with different width due to prototype [-Werror=traditional-conversion] -// conversion from ‘time_t’ {aka ‘long int’} to ‘unsigned int’ may change value [-Werror=conversion] -// We don't care. +// passing argument 1 of ‘srand’ with different width due to prototype +// [-Werror=traditional-conversion] conversion from ‘time_t’ {aka ‘long int’} to +// ‘unsigned int’ may change value [-Werror=conversion] We don't care. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtraditional-conversion" #pragma GCC diagnostic ignored "-Wconversion" @@ -106,7 +107,7 @@ int main(void) seed = (unsigned)rand(); // rand should be > 0 md5 = chash_double(str, seed); - for (i = 0; i < 20; i++) + for (i = 0; i < 20; i++) printf("%02x", md5[i]); printf("\n"); free(md5); diff --git a/src/connection.c b/src/connection.c index d5f4ebb..0a138eb 100644 --- a/src/connection.c +++ b/src/connection.c @@ -43,8 +43,7 @@ static int connection_timedout(connection_t *cn); static int socket_set_nonblock(int s); static void connection_connected(connection_t *c); -struct connecting_data -{ +struct connecting_data { struct addrinfo *dst; struct addrinfo *src; struct addrinfo *cur; @@ -62,12 +61,12 @@ static void connecting_data_free(struct connecting_data *t) void connection_close(connection_t *cn) { mylog(LOG_DEBUG, "Connection close asked. FD:%d (status: %d)", - (long)cn->handle, cn->connected); + (long)cn->handle, cn->connected); 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", - strerror(errno)); + strerror(errno)); } } @@ -87,7 +86,7 @@ void connection_free(connection_t *cn) free(cn->incoming); if (cn->connecting_data) connecting_data_free(cn->connecting_data); - /* conn->user_data */ + /* conn->user_data */ #ifdef HAVE_LIBSSL if (cn->ssl) { if (cn->cert) { @@ -123,24 +122,25 @@ static void connect_trynext(connection_t *cn) if (!cn->connecting_data) fatal("called connect_trynext with a connection not " - "connecting\n"); + "connecting\n"); cur = cn->connecting_data->cur; - for (cur = cn->connecting_data->cur ; cur ; cur = cur->ai_next) { + for (cur = cn->connecting_data->cur; cur; cur = cur->ai_next) { if ((cn->handle = socket(cur->ai_family, cur->ai_socktype, - cur->ai_protocol)) < 0) { + cur->ai_protocol)) + < 0) { mylog(LOG_WARN, "socket() : %s", strerror(errno)); continue; } if (cn->handle >= FD_SETSIZE) { mylog(LOG_WARN, "too many fd used, close socket %d", - cn->handle); + cn->handle); if (close(cn->handle) == -1) mylog(LOG_WARN, "Error on socket close: %s", - strerror(errno)); + strerror(errno)); cn->handle = -1; break; @@ -151,11 +151,11 @@ static void connect_trynext(connection_t *cn) if (cn->connecting_data->src) { /* local bind */ err = bind(cn->handle, - cn->connecting_data->src->ai_addr, - cn->connecting_data->src->ai_addrlen); + cn->connecting_data->src->ai_addr, + cn->connecting_data->src->ai_addrlen); if (err == -1) mylog(LOG_WARN, "bind() before connect: %s", - strerror(errno)); + strerror(errno)); } err = connect(cn->handle, cur->ai_addr, cur->ai_addrlen); @@ -180,8 +180,8 @@ static void connect_trynext(connection_t *cn) /* connect() failed */ char ip[256]; mylog(LOG_WARN, "connect(%s) : %s", - inet_ntop(cur->ai_family, cur->ai_addr, ip, 256), - strerror(errno)); + inet_ntop(cur->ai_family, cur->ai_addr, ip, 256), + strerror(errno)); close(cn->handle); cn->handle = -1; } @@ -203,17 +203,18 @@ static X509 *mySSL_get_cert(SSL *ssl) } cert = SSL_get_peer_certificate(ssl); if (cert == NULL) - mylog(LOG_WARN, "mySSL_get_cert() SSL server supplied no " - "certificate !"); + mylog(LOG_WARN, + "mySSL_get_cert() SSL server supplied no " + "certificate !"); return cert; } -static int _write_socket_SSL(connection_t *cn, char* message) +static int _write_socket_SSL(connection_t *cn, char *message) { int count; size_t size; - size = sizeof(char)*strlen(message); + size = sizeof(char) * strlen(message); // let's not ERR (SSL_write doesn't allow 0 len writes) if (size == 0) @@ -237,12 +238,11 @@ static int _write_socket_SSL(connection_t *cn, char* message) if (count <= 0) { int err = SSL_get_error(cn->ssl_h, count); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE - || err == SSL_ERROR_WANT_CONNECT - || err == SSL_ERROR_WANT_ACCEPT) + || err == SSL_ERROR_WANT_CONNECT + || err == SSL_ERROR_WANT_ACCEPT) return WRITE_KEEP; if (cn_is_connected(cn)) { - mylog(LOG_ERROR, "fd %d: Connection error", - cn->handle); + mylog(LOG_ERROR, "fd %d: Connection error", cn->handle); cn->connected = CONN_ERROR; } return WRITE_ERROR; @@ -250,8 +250,9 @@ static int _write_socket_SSL(connection_t *cn, char* message) if (count != (int)size) { /* abnormal : openssl keeps writing until message is not fully * sent */ - mylog(LOG_ERROR, "SSL_write wrote only %d while message length is %d", - count,size); + mylog(LOG_ERROR, + "SSL_write wrote only %d while message length is %d", + count, size); } mylog(LOG_DEBUGVERB, "%d/%d bytes sent", count, size); @@ -260,7 +261,8 @@ static int _write_socket_SSL(connection_t *cn, char* message) #if OPENSSL_VERSION_NUMBER < 0x10100000L #define X509_OBJECT_get0_X509(o) ((o)->data.x509) -#define X509_STORE_CTX_get_by_subject(vs, type, name, ret) X509_STORE_get_by_subject(vs, type, name, ret) +#define X509_STORE_CTX_get_by_subject(vs, type, name, ret) \ + X509_STORE_get_by_subject(vs, type, name, ret) int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) { @@ -317,7 +319,7 @@ static int _write_socket(connection_t *cn, char *message) * EINTR */ do { count = write(cn->handle, ((const char *)message) + tcount, - size - tcount); + size - tcount); if (count > 0) { tcount += (size_t)count; if (tcount == size) @@ -362,7 +364,6 @@ static int write_socket(connection_t *cn, char *line) else #endif return _write_socket(cn, line); - } /* returns 1 if connection must be notified */ @@ -498,7 +499,7 @@ static int read_socket_SSL(connection_t *cn) return -1; } - max = sizeof(char)*(CONN_BUFFER_SIZE - cn->incoming_end); + max = sizeof(char) * (CONN_BUFFER_SIZE - cn->incoming_end); if (max > INT_MAX) { mylog(LOG_ERROR, "read_socket_SSL: cannot read that much data"); return -1; @@ -512,29 +513,28 @@ static int read_socket_SSL(connection_t *cn) } } count = SSL_read(cn->ssl_h, (void *)(cn->incoming + cn->incoming_end), - (int)max); + (int)max); ERR_print_errors(errbio); if (count < 0) { int err = SSL_get_error(cn->ssl_h, count); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE - || err == SSL_ERROR_WANT_CONNECT - || err == SSL_ERROR_WANT_ACCEPT) + || err == SSL_ERROR_WANT_CONNECT + || err == SSL_ERROR_WANT_ACCEPT) return 0; if (cn_is_connected(cn)) { - mylog(LOG_ERROR, "fd %d: Connection error", - cn->handle); + mylog(LOG_ERROR, "fd %d: Connection error", cn->handle); cn->connected = CONN_ERROR; } return 1; } else if (count == 0) { -/* int err = SSL_get_error(cn->ssl_h,count); - if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE - || err == SSL_ERROR_WANT_CONNECT - || err == SSL_ERROR_WANT_ACCEPT) - return 0;*/ + /* int err = SSL_get_error(cn->ssl_h,count); + if (err == SSL_ERROR_WANT_READ || err == + SSL_ERROR_WANT_WRITE + || err == SSL_ERROR_WANT_CONNECT + || err == SSL_ERROR_WANT_ACCEPT) + return 0;*/ if (cn_is_connected(cn)) { - mylog(LOG_ERROR, "fd %d: Connection lost", - cn->handle); + mylog(LOG_ERROR, "fd %d: Connection lost", cn->handle); connection_close(cn); } return 1; @@ -559,21 +559,21 @@ static int read_socket(connection_t *cn) return -1; } - max = sizeof(char)*(CONN_BUFFER_SIZE - cn->incoming_end); - count = read(cn->handle, cn->incoming+cn->incoming_end, max); + max = sizeof(char) * (CONN_BUFFER_SIZE - cn->incoming_end); + count = read(cn->handle, cn->incoming + cn->incoming_end, max); if (count < 0) { if (errno == EAGAIN || errno == EINTR || errno == EINPROGRESS) return 0; if (cn_is_connected(cn)) { mylog(LOG_ERROR, "read(fd=%d): Connection error: %s", - cn->handle, strerror(errno)); + cn->handle, strerror(errno)); cn->connected = CONN_ERROR; } return 1; } else if (count == 0) { if (cn_is_connected(cn)) { mylog(LOG_ERROR, "read(fd=%d): Connection lost: %s", - cn->handle, strerror(errno)); + cn->handle, strerror(errno)); connection_close(cn); } return 1; @@ -671,14 +671,14 @@ static int check_event_except(fd_set *fds, connection_t *cn) if (cn_is_in_error(cn)) { mylog(LOG_ERROR, "Error on fd %d (except, state %d)", - cn->handle, cn->connected); + cn->handle, cn->connected); return 1; } if (!FD_ISSET(cn->handle, fds)) return 0; - mylog(LOG_DEBUGTOOMUCH,"fd %d is in exceptions list", cn->handle); + mylog(LOG_DEBUGTOOMUCH, "fd %d is in exceptions list", cn->handle); cn->connected = CONN_EXCEPT; return 1; } @@ -688,8 +688,8 @@ static int check_event_read(fd_set *fds, connection_t *cn) int ret; if (cn_is_in_error(cn)) { - mylog(LOG_ERROR, "Error on fd %d (read, state %d)", - cn->handle, cn->connected); + mylog(LOG_ERROR, "Error on fd %d (read, state %d)", cn->handle, + cn->connected); return 1; } @@ -697,7 +697,7 @@ static int check_event_read(fd_set *fds, connection_t *cn) return 0; mylog(LOG_DEBUGTOOMUCH, "Read positive on fd %d (state %d)", cn->handle, - cn->connected); + cn->connected); /* notify caller to make it check for a new client */ if (cn->listening) @@ -711,8 +711,7 @@ static int check_event_read(fd_set *fds, connection_t *cn) ret = read_socket(cn); if (ret) { - mylog(LOG_ERROR, "Error while reading on fd %d", - cn->handle); + mylog(LOG_ERROR, "Error while reading on fd %d", cn->handle); return 1; } @@ -723,7 +722,7 @@ static int check_event_read(fd_set *fds, connection_t *cn) return 0; mylog(LOG_DEBUGTOOMUCH, "newlines on fd %d (state %d)", cn->handle, - cn->connected); + cn->connected); return 1; } @@ -742,8 +741,8 @@ static void connection_connected(connection_t *c) static int check_event_write(fd_set *fds, connection_t *cn, int *nc) { if (cn_is_in_error(cn)) { - mylog(LOG_ERROR, "Error on fd %d (write, state %d)", - cn->handle, cn->connected); + mylog(LOG_ERROR, "Error on fd %d (write, state %d)", cn->handle, + cn->connected); return 1; } @@ -752,35 +751,37 @@ static int check_event_write(fd_set *fds, connection_t *cn, int *nc) return 0; mylog(LOG_DEBUG, "New socket still not connected (%d)", - cn->handle); + cn->handle); /* check timeout (handles connect_trynext) */ return connection_timedout(cn); } mylog(LOG_DEBUGTOOMUCH, "Write positive on fd %d (state %d)", - cn->handle, cn->connected); + cn->handle, cn->connected); if (cn_is_new(cn)) { int err, err2; socklen_t errSize = sizeof(err); err2 = getsockopt(cn->handle, SOL_SOCKET, SO_ERROR, - (void *)&err, &errSize); + (void *)&err, &errSize); if (err2 < 0) { mylog(LOG_ERROR, "fd:%d getsockopt error: %s", - cn->handle, strerror(errno)); + cn->handle, strerror(errno)); if (cn->connecting_data) { close(cn->handle); cn->handle = -1; connect_trynext(cn); } - return (cn_is_new(cn) || cn->connected == - CONN_NEED_SSLIZE) ? 0 : 1; + return (cn_is_new(cn) + || cn->connected == CONN_NEED_SSLIZE) + ? 0 + : 1; } else if (err == EINPROGRESS || err == EALREADY) { mylog(LOG_DEBUG, "fd:%d Connection in progress...", - cn->handle); + cn->handle); return connection_timedout(cn); } else if (err == EISCONN || err == 0) { #ifdef HAVE_LIBSSL @@ -793,18 +794,20 @@ static int check_event_write(fd_set *fds, connection_t *cn, int *nc) connection_connected(cn); *nc = 1; mylog(LOG_DEBUG, "fd:%d Connection established !", - cn->handle); + cn->handle); return 1; } else { mylog(LOG_WARN, "fd:%d Socket error: %s", cn->handle, - strerror(err)); + strerror(err)); if (cn->connecting_data) { close(cn->handle); cn->handle = -1; connect_trynext(cn); } - return (cn_is_new(cn) || cn->connected == - CONN_NEED_SSLIZE) ? 0 : 1; + return (cn_is_new(cn) + || cn->connected == CONN_NEED_SSLIZE) + ? 0 + : 1; } } @@ -842,7 +845,8 @@ static int cn_want_write(connection_t *cn) if (!clock_gettime(CLOCK_MONOTONIC, &tv)) { if (tv.tv_sec < 0 || tv.tv_nsec < 0) fatal("clock_gettime returned negative time"); - now = (unsigned long)(tv.tv_sec * 1000 + tv.tv_nsec / 1000000); + now = (unsigned long)(tv.tv_sec * 1000 + + tv.tv_nsec / 1000000); /* round now to TOKEN_INTERVAL multiple */ now -= now % TOKEN_INTERVAL; if (now < cn->lasttoken) { @@ -850,8 +854,8 @@ static int cn_want_write(connection_t *cn) cn->token = 1; cn->lasttoken = now; } else if (now > cn->lasttoken + TOKEN_INTERVAL) { - cn->token += (unsigned)((now - cn->lasttoken) / - TOKEN_INTERVAL); + cn->token += (unsigned)((now - cn->lasttoken) + / TOKEN_INTERVAL); if (cn->token > TOKEN_MAX) cn->token = TOKEN_MAX; if (!cn->token) @@ -917,8 +921,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) if (cn_is_connected(cn)) { FD_SET(cn->handle, &fds_read); mylog(LOG_DEBUGTOOMUCH, "Test read on fd %d %d:%d", - cn->handle, cn->connected, - cn_is_connected(cn)); + cn->handle, cn->connected, cn_is_connected(cn)); } /* we NEVER want to check write on a listening socket */ @@ -928,8 +931,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) if (!cn_is_connected(cn) || cn_want_write(cn)) { FD_SET(cn->handle, &fds_write); mylog(LOG_DEBUGTOOMUCH, "Test write on fd %d %d:%d", - cn->handle, cn->connected, - cn_is_connected(cn)); + cn->handle, cn->connected, cn_is_connected(cn)); } } @@ -946,7 +948,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) tv.tv_sec = *msec / 1000; tv.tv_usec = (*msec % 1000) * 1000; mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec, tv.tv_sec, - tv.tv_usec); + tv.tv_usec); bip_clock_gettime(CLOCK_MONOTONIC, &btv); err = select(maxfd + 1, &fds_read, &fds_write, &fds_except, &tv); @@ -958,7 +960,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) return cn_newdata; } else { mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec, - tv.tv_sec, tv.tv_usec); + tv.tv_sec, tv.tv_usec); } bip_clock_gettime(CLOCK_MONOTONIC, &etv); @@ -966,7 +968,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) mylog(LOG_ERROR, "Time rewinded ! not touching interval"); else { *msec -= (etv.tv_sec - btv.tv_sec) * 1000 - + (etv.tv_nsec - btv.tv_nsec) / 1000000; + + (etv.tv_nsec - btv.tv_nsec) / 1000000; /* in case we go forward in time */ if (*msec < 0) *msec = 0; @@ -984,7 +986,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) if (check_event_except(&fds_except, cn)) { mylog(LOG_DEBUGTOOMUCH, "Notify on FD %d (state %d)", - cn->handle, cn->connected); + cn->handle, cn->connected); list_add_first_uniq(cn_newdata, cn); continue; } @@ -995,7 +997,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) if (check_event_read(&fds_read, cn)) { mylog(LOG_DEBUGTOOMUCH, "Notify on FD %d (state %d)", - cn->handle, cn->connected); + cn->handle, cn->connected); toadd = 1; } if (toadd) @@ -1005,7 +1007,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) } static void create_socket(char *dsthostname, char *dstport, char *srchostname, - char *srcport, connection_t *cn) + char *srcport, connection_t *cn) { int err; struct connecting_data *cdata; @@ -1018,14 +1020,14 @@ static void create_socket(char *dsthostname, char *dstport, char *srchostname, hint.ai_protocol = 0; cn->connected = CONN_ERROR; - cdata = (struct connecting_data *) - bip_malloc(sizeof(struct connecting_data)); + cdata = (struct connecting_data *)bip_malloc( + sizeof(struct connecting_data)); cdata->dst = cdata->src = cdata->cur = NULL; err = getaddrinfo(dsthostname, dstport, &hint, &cdata->dst); if (err) { mylog(LOG_ERROR, "getaddrinfo(%s): %s", dsthostname, - gai_strerror(err)); + gai_strerror(err)); connecting_data_free(cdata); cdata = NULL; return; @@ -1033,10 +1035,10 @@ static void create_socket(char *dsthostname, char *dstport, char *srchostname, if (srchostname || srcport) { if ((err = getaddrinfo(srchostname, srcport, &hint, - &cdata->src))) { + &cdata->src))) { /* not fatal ? maybe a config option is needed */ mylog(LOG_ERROR, "getaddrinfo(src): %s", - gai_strerror(err)); + gai_strerror(err)); cdata->src = NULL; } } @@ -1049,22 +1051,20 @@ static void create_socket(char *dsthostname, char *dstport, char *srchostname, static void create_listening_socket(char *hostname, char *port, - connection_t *cn) + connection_t *cn) { int multi_client = 1; int err; struct addrinfo *res, *cur; - struct addrinfo hint = { - .ai_flags = AI_PASSIVE, - .ai_family = AF_UNSPEC, - .ai_socktype = SOCK_STREAM, - .ai_protocol = 0, + struct addrinfo hint = {.ai_flags = AI_PASSIVE, + .ai_family = AF_UNSPEC, + .ai_socktype = SOCK_STREAM, + .ai_protocol = 0, - .ai_addrlen = 0, - .ai_addr = 0, - .ai_canonname = 0, - .ai_next = 0 - }; + .ai_addrlen = 0, + .ai_addr = 0, + .ai_canonname = 0, + .ai_next = 0}; cn->connected = CONN_ERROR; @@ -1074,28 +1074,31 @@ static void create_listening_socket(char *hostname, char *port, return; } - for (cur = res ; cur ; cur = cur->ai_next) { + for (cur = res; cur; cur = cur->ai_next) { if ((cn->handle = socket(cur->ai_family, cur->ai_socktype, - cur->ai_protocol)) < 0) { + cur->ai_protocol)) + < 0) { mylog(LOG_WARN, "socket : %s", strerror(errno)); continue; } if (cn->handle >= FD_SETSIZE) { - mylog(LOG_WARN, "too many fd used, close listening socket %d", - cn->handle); + mylog(LOG_WARN, + "too many fd used, close listening socket %d", + cn->handle); if (close(cn->handle) == -1) mylog(LOG_WARN, "Error on socket close: %s", - strerror(errno)); + strerror(errno)); cn->handle = -1; break; } if (setsockopt(cn->handle, SOL_SOCKET, SO_REUSEADDR, - (char *)&multi_client, - (socklen_t)sizeof(multi_client)) < 0) { + (char *)&multi_client, + (socklen_t)sizeof(multi_client)) + < 0) { mylog(LOG_WARN, "setsockopt() : %s", strerror(errno)); close(cn->handle); cn->handle = -1; @@ -1129,7 +1132,7 @@ static void create_listening_socket(char *hostname, char *port, } static connection_t *connection_init(int anti_flood, int ssl, time_t timeout, - int listen) + int listen) { connection_t *conn; char *incoming; @@ -1174,7 +1177,7 @@ static int ctx_set_dh(SSL_CTX *ctx) if ((f = fopen(conf_client_dh_file, "r")) == NULL) { mylog(LOG_ERROR, "Unable to open DH parameters (%s): %s", - conf_client_dh_file, strerror(errno)); + conf_client_dh_file, strerror(errno)); return 0; } @@ -1183,11 +1186,12 @@ static int ctx_set_dh(SSL_CTX *ctx) if (dh == NULL) { mylog(LOG_ERROR, "Could not parse DH parameters from: %s", - conf_client_dh_file); + conf_client_dh_file); return 0; } -// SSL crap: passing argument 3 of ‘SSL_CTX_ctrl’ with different width due to prototype +// SSL crap: passing argument 3 of ‘SSL_CTX_ctrl’ with different width due to +// prototype #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtraditional-conversion" ret = SSL_CTX_set_tmp_dh(ctx, dh); @@ -1196,7 +1200,7 @@ static int ctx_set_dh(SSL_CTX *ctx) if (ret != 1) { mylog(LOG_ERROR, "Unable to set DH parameters: %s", - ERR_error_string(ERR_get_error(), NULL)); + ERR_error_string(ERR_get_error(), NULL)); return 0; } @@ -1208,7 +1212,7 @@ connection_t *accept_new(connection_t *cn) { connection_t *conn; int err; - socklen_t sa_len = sizeof (struct sockaddr); + socklen_t sa_len = sizeof(struct sockaddr); struct sockaddr sa; mylog(LOG_DEBUG, "Trying to accept new client on %d", cn->handle); @@ -1223,7 +1227,7 @@ connection_t *accept_new(connection_t *cn) if (close(err) == -1) mylog(LOG_WARN, "Error on socket close: %s", - strerror(errno)); + strerror(errno)); return NULL; } @@ -1238,20 +1242,24 @@ connection_t *accept_new(connection_t *cn) #ifdef HAVE_LIBSSL if (cn->ssl) { if (!sslctx) { - mylog(LOG_DEBUG, "No SSL context available for " - "accepted connections. " - "Initializing..."); + mylog(LOG_DEBUG, + "No SSL context available for " + "accepted connections. " + "Initializing..."); if (!(sslctx = SSL_init_context(conf_client_ciphers))) { - mylog(LOG_ERROR, "SSL context initialization " - "failed"); + mylog(LOG_ERROR, + "SSL context initialization " + "failed"); connection_free(conn); return NULL; } if (!conf_client_dh_file) { - // try with a default path but don't fail if it doesn't exist - conf_client_dh_file = default_path(conf_biphome, "dh.pem", - "DH parameters"); + // try with a default path but don't fail if it + // doesn't exist + conf_client_dh_file = + default_path(conf_biphome, "dh.pem", + "DH parameters"); struct stat st_buf; if (stat(conf_client_dh_file, &st_buf) != 0) { @@ -1262,20 +1270,22 @@ connection_t *accept_new(connection_t *cn) if (conf_client_dh_file) { if (!ctx_set_dh(sslctx)) { - mylog(LOG_ERROR, "SSL Unable to load DH " - "parameters"); + mylog(LOG_ERROR, + "SSL Unable to load DH " + "parameters"); connection_free(conn); return NULL; } } - if (!SSL_CTX_use_certificate_chain_file(sslctx, - conf_ssl_certfile)) - mylog(LOG_WARN, "SSL: Unable to load " - "certificate file"); + if (!SSL_CTX_use_certificate_chain_file( + sslctx, conf_ssl_certfile)) + mylog(LOG_WARN, + "SSL: Unable to load " + "certificate file"); if (!SSL_CTX_use_PrivateKey_file(sslctx, - conf_ssl_certfile, - SSL_FILETYPE_PEM)) + conf_ssl_certfile, + SSL_FILETYPE_PEM)) mylog(LOG_WARN, "SSL: Unable to load key file"); } @@ -1288,7 +1298,7 @@ connection_t *accept_new(connection_t *cn) SSL_set_accept_state(conn->ssl_h); } #endif - mylog(LOG_DEBUG, "New client on socket %d !",conn->handle); + mylog(LOG_DEBUG, "New client on socket %d !", conn->handle); return conn; } @@ -1312,7 +1322,8 @@ connection_t *listen_new(char *hostname, int port, int ssl) } static connection_t *_connection_new(char *dsthostname, char *dstport, - char *srchostname, char *srcport, time_t timeout) + char *srchostname, char *srcport, + time_t timeout) { connection_t *conn; @@ -1331,7 +1342,8 @@ static SSL_CTX *SSL_init_context(char *ciphers) SSL_CTX *ctx; if (!ssl_initialized) { -// SSL crap: passing argument 1 of ‘OPENSSL_init_ssl’ with different width due to prototype +// SSL crap: passing argument 1 of ‘OPENSSL_init_ssl’ with different width due +// to prototype #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtraditional-conversion" SSL_library_init(); @@ -1340,30 +1352,33 @@ static SSL_CTX *SSL_init_context(char *ciphers) errbio = BIO_new_fp(conf_global_log_file, BIO_NOCLOSE); ssl_cx_idx = SSL_get_ex_new_index((size_t)0, "bip connection_t", - NULL, NULL,NULL); + NULL, NULL, NULL); flags = O_RDONLY; flags |= O_NONBLOCK; fd = open("/dev/random", flags); if (fd < 0) { - mylog(LOG_WARN, "SSL: /dev/random not ready, unable " - "to manually seed PRNG."); + mylog(LOG_WARN, + "SSL: /dev/random not ready, unable " + "to manually seed PRNG."); goto prng_end; } do { ret = read(fd, buf, (size_t)1024); if (ret <= 0) { - mylog(LOG_ERROR,"/dev/random: %s", - strerror(errno)); + mylog(LOG_ERROR, "/dev/random: %s", + strerror(errno)); goto prng_end; } - mylog(LOG_DEBUG, "PRNG seeded with %d /dev/random " - "bytes", ret); + mylog(LOG_DEBUG, + "PRNG seeded with %d /dev/random " + "bytes", + ret); RAND_seed(buf, (int)ret); } while (!(rng = RAND_status())); -prng_end: + prng_end: do { ret = close(fd); } while (ret != 0 && errno == EINTR); @@ -1371,8 +1386,9 @@ prng_end: mylog(LOG_DEBUG, "SSL: PRNG is seeded !"); } else { mylog(LOG_WARN, "SSL: PRNG is not seeded enough"); - mylog(LOG_WARN, " OpenSSL will use /dev/urandom if " - "available."); + mylog(LOG_WARN, + " OpenSSL will use /dev/urandom if " + "available."); } ssl_initialized = 1; @@ -1385,10 +1401,12 @@ prng_end: } #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtraditional-conversion" -// SSL crap: passing argument 3 of ‘SSL_CTX_ctrl’ with different width due to prototype + // SSL crap: passing argument 3 of ‘SSL_CTX_ctrl’ with different width + // due to prototype SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_BOTH); SSL_CTX_set_timeout(ctx, (long)60); -// SSL crap: passing argument 2 of ‘SSL_CTX_set_options’ with different width due to prototype + // SSL crap: passing argument 2 of ‘SSL_CTX_set_options’ with different + // width due to prototype SSL_CTX_set_options(ctx, SSL_OP_ALL); #pragma GCC diagnostic pop if (ciphers && !SSL_CTX_set_cipher_list(ctx, ciphers)) { @@ -1416,7 +1434,7 @@ static int bip_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx) /* Retrieve the SSL and connection_t objects from the store */ ssl = X509_STORE_CTX_get_ex_data(ctx, - SSL_get_ex_data_X509_STORE_CTX_idx()); + SSL_get_ex_data_X509_STORE_CTX_idx()); c = SSL_get_ex_data(ssl, ssl_cx_idx); mylog(LOG_INFO, "SSL cert check: now at depth=%d", depth); @@ -1429,38 +1447,45 @@ static int bip_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx) /* in basic mode (mode 1), accept a leaf certificate if we can find it * in the store */ - if (c->ssl_check_mode == SSL_CHECK_BASIC && result == 0 && - (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY || - err == X509_V_ERR_CERT_UNTRUSTED || - err == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || - err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || - err == X509_V_ERR_CERT_HAS_EXPIRED || - err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)) { + if (c->ssl_check_mode == SSL_CHECK_BASIC && result == 0 + && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY + || err == X509_V_ERR_CERT_UNTRUSTED + || err == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE + || err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT + || err == X509_V_ERR_CERT_HAS_EXPIRED + || err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)) { if (!(xobj = X509_OBJECT_new())) { result = 0; } else { - if (X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, - X509_get_subject_name(err_cert), xobj) > 0 && - !X509_cmp(X509_OBJECT_get0_X509(xobj), err_cert)) { + if (X509_STORE_CTX_get_by_subject( + ctx, X509_LU_X509, + X509_get_subject_name(err_cert), xobj) + > 0 + && !X509_cmp(X509_OBJECT_get0_X509(xobj), + err_cert)) { if (err == X509_V_ERR_CERT_HAS_EXPIRED) - mylog(LOG_INFO, "Basic mode; Accepting " - "*expired* peer certificate " - "found in store."); + mylog(LOG_INFO, + "Basic mode; Accepting " + "*expired* peer certificate " + "found in store."); else - mylog(LOG_INFO, "Basic mode; Accepting peer " - "certificate found in store."); + mylog(LOG_INFO, + "Basic mode; Accepting peer " + "certificate found in store."); result = 1; err = X509_V_OK; X509_STORE_CTX_set_error(ctx, err); } else { - mylog(LOG_INFO, "Basic mode; peer certificate NOT " - "in store, rejecting it!"); + mylog(LOG_INFO, + "Basic mode; peer certificate NOT " + "in store, rejecting it!"); err = X509_V_ERR_CERT_REJECTED; X509_STORE_CTX_set_error(ctx, err); - link_add_untrusted(c->user_data, X509_dup(err_cert)); + link_add_untrusted(c->user_data, + X509_dup(err_cert)); } X509_OBJECT_free(xobj); } @@ -1469,7 +1494,7 @@ static int bip_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx) if (!result) { /* We have a verify error! Log it */ mylog(LOG_ERROR, "SSL cert check failed at depth=%d: %s (%d)", - depth, X509_verify_cert_error_string((long)err), err); + depth, X509_verify_cert_error_string((long)err), err); } return result; @@ -1524,23 +1549,24 @@ static int SSLize(connection_t *cn, int *nc) case SSL_CHECK_NONE: break; case SSL_CHECK_BASIC: - if((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) { + if ((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) { mylog(LOG_ERROR, "Certificate check failed: %s (%ld)!", - X509_verify_cert_error_string(errl), errl); + X509_verify_cert_error_string(errl), errl); cn->connected = CONN_UNTRUSTED; return 1; } break; case SSL_CHECK_CA: - if((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) { + if ((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) { mylog(LOG_ERROR, "Certificate check failed: %s (%ld)!", - X509_verify_cert_error_string(errl), errl); + X509_verify_cert_error_string(errl), errl); cn->connected = CONN_UNTRUSTED; return 1; } break; default: - mylog(LOG_ERROR, "Unknown ssl_check_mode (%d)!", cn->ssl_check_mode); + mylog(LOG_ERROR, "Unknown ssl_check_mode (%d)!", + cn->ssl_check_mode); return 1; } @@ -1563,8 +1589,11 @@ static int SSLize(connection_t *cn, int *nc) } static connection_t *_connection_new_SSL(char *dsthostname, char *dstport, - char *srchostname, char *srcport, char *ciphers, int check_mode, - char *check_store, char *ssl_client_certfile, time_t timeout) + char *srchostname, char *srcport, + char *ciphers, int check_mode, + char *check_store, + char *ssl_client_certfile, + time_t timeout) { connection_t *conn; @@ -1578,54 +1607,63 @@ static connection_t *_connection_new_SSL(char *dsthostname, char *dstport, conn->ssl_check_mode = check_mode; switch (conn->ssl_check_mode) { - struct stat st_buf; + struct stat st_buf; case SSL_CHECK_NONE: break; case SSL_CHECK_BASIC: if (!SSL_CTX_load_verify_locations(conn->ssl_ctx_h, check_store, - NULL)) { - mylog(LOG_ERROR, "Can't assign check store to " - "SSL connection! Proceeding without!"); + NULL)) { + mylog(LOG_ERROR, + "Can't assign check store to " + "SSL connection! Proceeding without!"); } break; case SSL_CHECK_CA: if (!check_store) { if (SSL_CTX_set_default_verify_paths(conn->ssl_ctx_h)) { - mylog(LOG_INFO, "No SSL certificate check store configured. " - "Default store will be used."); + mylog(LOG_INFO, + "No SSL certificate check store configured. " + "Default store will be used."); break; } else { - mylog(LOG_ERROR, "No SSL certificate check store configured " - "and cannot use default store!"); + mylog(LOG_ERROR, + "No SSL certificate check store configured " + "and cannot use default store!"); return conn; } } // Check if check_store is a file or directory if (stat(check_store, &st_buf) == 0) { if (st_buf.st_mode & S_IFDIR) { - if (!SSL_CTX_load_verify_locations(conn->ssl_ctx_h, NULL, - check_store)) { - mylog(LOG_ERROR, "Can't assign check store to " - "SSL connection!"); + if (!SSL_CTX_load_verify_locations( + conn->ssl_ctx_h, NULL, + check_store)) { + mylog(LOG_ERROR, + "Can't assign check store to " + "SSL connection!"); return conn; } break; } if (st_buf.st_mode & S_IFREG) { - if (!SSL_CTX_load_verify_locations(conn->ssl_ctx_h, check_store, - NULL)) { - mylog(LOG_ERROR, "Can't assign check store to " - "SSL connection!"); + if (!SSL_CTX_load_verify_locations( + conn->ssl_ctx_h, check_store, + NULL)) { + mylog(LOG_ERROR, + "Can't assign check store to " + "SSL connection!"); return conn; } break; } - mylog(LOG_ERROR, "Specified SSL certificate check store is neither " - "a file nor a directory."); + mylog(LOG_ERROR, + "Specified SSL certificate check store is neither " + "a file nor a directory."); return conn; } - mylog(LOG_ERROR, "Can't open SSL certificate check store! Check path " - "and permissions."); + mylog(LOG_ERROR, + "Can't open SSL certificate check store! Check path " + "and permissions."); return conn; default: fatal("Unknown SSL cert check mode."); @@ -1637,12 +1675,12 @@ static connection_t *_connection_new_SSL(char *dsthostname, char *dstport, break; case SSL_CHECK_BASIC: SSL_CTX_set_verify(conn->ssl_ctx_h, SSL_VERIFY_PEER, - bip_ssl_verify_callback); + bip_ssl_verify_callback); /* SSL_CTX_set_verify_depth(conn->ssl_ctx_h, 0); */ break; case SSL_CHECK_CA: SSL_CTX_set_verify(conn->ssl_ctx_h, SSL_VERIFY_PEER, - bip_ssl_verify_callback); + bip_ssl_verify_callback); break; default: fatal("Unknown SSL cert check mode."); @@ -1650,14 +1688,17 @@ static connection_t *_connection_new_SSL(char *dsthostname, char *dstport, if (ssl_client_certfile) { if (!SSL_CTX_use_certificate_chain_file(conn->ssl_ctx_h, - ssl_client_certfile)) + ssl_client_certfile)) mylog(LOG_WARN, "SSL: Unable to load certificate file"); else if (!SSL_CTX_use_PrivateKey_file(conn->ssl_ctx_h, - ssl_client_certfile, SSL_FILETYPE_PEM)) + ssl_client_certfile, + SSL_FILETYPE_PEM)) mylog(LOG_WARN, "SSL: Unable to load key file"); else - mylog(LOG_INFO, "SSL: using %s pem file as client SSL " - "certificate", ssl_client_certfile); + mylog(LOG_INFO, + "SSL: using %s pem file as client SSL " + "certificate", + ssl_client_certfile); } conn->ssl_h = SSL_new(conn->ssl_ctx_h); @@ -1683,8 +1724,9 @@ static connection_t *_connection_new_SSL(char *dsthostname, char *dstport, #endif connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, - int srcport, int ssl, char *ssl_ciphers, int ssl_check_mode, - char *ssl_check_store, char *ssl_client_certfile, time_t timeout) + int srcport, int ssl, char *ssl_ciphers, + int ssl_check_mode, char *ssl_check_store, + char *ssl_client_certfile, time_t timeout) { char dstportbuf[20], srcportbuf[20], *tmp; #ifndef HAVE_LIBSSL @@ -1706,12 +1748,13 @@ connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, #ifdef HAVE_LIBSSL if (ssl) return _connection_new_SSL(dsthostname, dstportbuf, srchostname, - tmp, ssl_ciphers, ssl_check_mode, ssl_check_store, - ssl_client_certfile, timeout); + tmp, ssl_ciphers, ssl_check_mode, + ssl_check_store, ssl_client_certfile, + timeout); else #endif return _connection_new(dsthostname, dstportbuf, srchostname, - tmp, timeout); + tmp, timeout); } int cn_is_listening(connection_t *cn) @@ -1747,37 +1790,37 @@ static int socket_set_nonblock(int s) int flags; if ((flags = fcntl(s, F_GETFL, 0)) < 0) { - mylog(LOG_ERROR, "Cannot set socket %d to non blocking : %s", - s, strerror(errno)); + mylog(LOG_ERROR, "Cannot set socket %d to non blocking : %s", s, + strerror(errno)); return 0; } if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) { - mylog(LOG_ERROR, "Cannot set socket %d to non blocking : %s", - s, strerror(errno)); + mylog(LOG_ERROR, "Cannot set socket %d to non blocking : %s", s, + strerror(errno)); return 0; } return 1; } #ifdef TEST -int main(int argc,char* argv[]) +int main(int argc, char *argv[]) { connection_t *conn, *conn2; int s, cont = 1; if (argc != 3) { - fprintf(stderr,"Usage: %s host port\n",argv[0]); + fprintf(stderr, "Usage: %s host port\n", argv[0]); exit(1); } conn = connection_init(0, 0, (time_t)0, 1); conn->connect_time = time(NULL); - create_listening_socket(argv[1],argv[2],&conn); + create_listening_socket(argv[1], argv[2], &conn); if (s == -1) { mylog(LOG_ERROR, "socket() : %s", strerror(errno)); exit(1); } - mylog(LOG_DEBUG, "Socket number %d",s); + mylog(LOG_DEBUG, "Socket number %d", s); while (cont) { conn2 = accept_new(conn); @@ -1789,8 +1832,8 @@ int main(int argc,char* argv[]) } while (1) { int ret = read_socket(conn2); - mylog(LOG_DEBUGTOOMUCH, "READ: %d %*s",ret, conn2->incoming, - conn2->incoming_end); + mylog(LOG_DEBUGTOOMUCH, "READ: %d %*s", ret, conn2->incoming, + conn2->incoming_end); conn2->incoming_end = 0; sleep(1); } @@ -1811,7 +1854,7 @@ uint16_t connection_localport(connection_t *cn) err = getsockname(cn->handle, (struct sockaddr *)&addr, &addrlen); if (err != 0) { mylog(LOG_ERROR, "in getsockname(%d): %s", cn->handle, - strerror(errno)); + strerror(errno)); return 0; } @@ -1835,7 +1878,7 @@ uint16_t connection_remoteport(connection_t *cn) err = getpeername(cn->handle, (struct sockaddr *)&addr, &addrlen); if (err != 0) { mylog(LOG_ERROR, "in getpeername(%d): %s", cn->handle, - strerror(errno)); + strerror(errno)); return 0; } @@ -1866,8 +1909,7 @@ static char *socket_ip(int fd, int remote) /* getsockname every time to get IP version */ err = getsockname(fd, (struct sockaddr *)&addr, &addrlen); if (err != 0) { - mylog(LOG_ERROR, "in getsockname(%d): %s", fd, - strerror(errno)); + mylog(LOG_ERROR, "in getsockname(%d): %s", fd, strerror(errno)); return NULL; } @@ -1879,19 +1921,19 @@ static char *socket_ip(int fd, int remote) if (remote) { err = getpeername(fd, (struct sockaddr *)&addr4, - &addrlen4); + &addrlen4); if (err != 0) { mylog(LOG_ERROR, "in getpeername(%d): %s", fd, - strerror(errno)); + strerror(errno)); free(ip); return NULL; } } else { err = getsockname(fd, (struct sockaddr *)&addr4, - &addrlen4); + &addrlen4); if (err != 0) { mylog(LOG_ERROR, "in getsockname(%d): %s", fd, - strerror(errno)); + strerror(errno)); free(ip); return NULL; } @@ -1908,19 +1950,19 @@ static char *socket_ip(int fd, int remote) if (remote) { err = getpeername(fd, (struct sockaddr *)&addr6, - &addrlen6); + &addrlen6); if (err != 0) { mylog(LOG_ERROR, "in getpeername(%d): %s", fd, - strerror(errno)); + strerror(errno)); free(ip); return NULL; } } else { err = getsockname(fd, (struct sockaddr *)&addr6, - &addrlen6); + &addrlen6); if (err != 0) { mylog(LOG_ERROR, "in getsockname(%d): %s", fd, - strerror(errno)); + strerror(errno)); free(ip); return NULL; } diff --git a/src/connection.h b/src/connection.h index b5f78a1..400b41e 100644 --- a/src/connection.h +++ b/src/connection.h @@ -92,8 +92,9 @@ typedef struct connection { } connection_t; connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, - int srcport, int ssl, char *ssl_ciphers, int ssl_check_mode, - char *ssl_check_store, char *ssl_client_certfile, time_t timeout); + int srcport, int ssl, char *ssl_ciphers, + int ssl_check_mode, char *ssl_check_store, + char *ssl_client_certfile, time_t timeout); connection_t *listen_new(char *hostname, int port, int ssl); connection_t *accept_new(connection_t *cn); void connection_free(connection_t *cn); diff --git a/src/irc.c b/src/irc.c index 2de88fd..dd226df 100644 --- a/src/irc.c +++ b/src/irc.c @@ -24,7 +24,8 @@ #include "md5.h" #include "utils/base64.h" -// TODO resolve assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 +// 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" #define S_CONN_DELAY (10) @@ -36,7 +37,8 @@ static int irc_join(struct link_server *server, struct line *line); static int irc_part(struct link_server *server, struct line *line); static int irc_mode(struct link_server *server, struct line *line); static int irc_mode_channel(struct link_server *s, struct channel *channel, - struct line *line, const char* mode, int add, int cur_arg); + struct line *line, const char *mode, int add, + int cur_arg); static int irc_kick(struct link_server *server, struct line *line); static int irc_privmsg(struct link_server *server, struct line *line); static int irc_notice(struct link_server *server, struct line *line); @@ -56,7 +58,7 @@ static void ls_set_nick(struct link_server *ircs, char *nick); static void server_set_chanmodes(struct link_server *l, const char *chanmodes); static void server_set_prefix(struct link_server *l, const char *prefix); static void server_init_modes(struct link_server *s); -static int bip_get_index(const char* str, char car); +static int bip_get_index(const char *str, char car); static int bip_fls(long v); void oidentd_dump(bip_t *bip); @@ -68,7 +70,7 @@ extern int conf_reconn_timer; void write_user_list(connection_t *c, char *dest); static void irc_copy_cli(struct link_client *src, struct link_client *dest, - struct line *line); + struct line *line); static void irc_cli_make_join(struct link_client *ic); static void server_setup_reconnect_timer(struct link *link); int irc_cli_bip(bip_t *bip, struct link_client *ic, struct line *line); @@ -123,7 +125,7 @@ list_t *channel_name_list(struct link_server *server, struct channel *c) ret = list_new(NULL); *str = 0; for (hash_it_init(&c->ovmasks, &hi); hash_it_key(&hi); - hash_it_next(&hi)){ + hash_it_next(&hi)) { const char *nick = hash_it_key(&hi); long int ovmask = (long int)hash_it_item(&hi); @@ -176,7 +178,7 @@ static int irc_001(struct link_server *server, struct line *line) for (i = 0; i < LINK(server)->l_clientc; i++) { struct link_client *c = LINK(server)->l_clientv[i]; WRITE_LINE1(CONN(c), LINK(server)->cli_nick, "NICK", - server->nick); + server->nick); } return OK_COPY; } @@ -195,8 +197,8 @@ void irc_compute_lag(struct link_server *is) time_t lag; assert(is->laginit_ts != -1); lag = time(NULL) - is->laginit_ts; - if (lag > LAGOUT_TIME*2) - is->lag = LAGOUT_TIME*2; + if (lag > LAGOUT_TIME * 2) + is->lag = LAGOUT_TIME * 2; else is->lag = (unsigned)lag; } @@ -222,7 +224,7 @@ static void irc_server_join(struct link_server *s) { list_iterator_t it; for (list_it_init(&LINK(s)->chan_infos_order, &it); list_it_item(&it); - list_it_next(&it)) { + list_it_next(&it)) { struct chan_info *ci = list_it_item(&it); if (!ci->key) WRITE_LINE1(CONN(s), NULL, "JOIN", ci->name); @@ -235,21 +237,21 @@ static void irc_server_connected(struct link_server *server) { unsigned int i; - LINK(server)->s_state = IRCS_CONNECTED; - LINK(server)->s_conn_attempt = 0; + LINK(server)->s_state = IRCS_CONNECTED; + LINK(server)->s_conn_attempt = 0; - mylog(LOG_INFO, "[%s] Connected for user %s", - LINK(server)->name, LINK(server)->user->name); + mylog(LOG_INFO, "[%s] Connected for user %s", LINK(server)->name, + LINK(server)->user->name); - irc_server_join(server); - log_connected(LINK(server)->log); + irc_server_join(server); + log_connected(LINK(server)->log); if (LINK(server)->cli_nick) { /* we change nick on client */ for (i = 0; i < LINK(server)->l_clientc; i++) { struct link_client *ic = LINK(server)->l_clientv[i]; WRITE_LINE1(CONN(ic), LINK(server)->cli_nick, "NICK", - server->nick); + server->nick); } free(LINK(server)->cli_nick); LINK(server)->cli_nick = NULL; @@ -258,21 +260,21 @@ static void irc_server_connected(struct link_server *server) /* basic helper for nickserv and co */ list_iterator_t itocs; for (list_it_init(&LINK(server)->on_connect_send, &itocs); - list_it_item(&itocs); list_it_next(&itocs)) { + list_it_item(&itocs); list_it_next(&itocs)) { size_t len = strlen(list_it_item(&itocs)) + 2; char *str = bip_malloc(len + 1); sprintf(str, "%s\r\n", (char *)list_it_item(&itocs)); write_line(CONN(server), str); free(str); - } + } if (LINK(server)->l_clientc == 0) { if (LINK(server)->away_nick) WRITE_LINE1(CONN(server), NULL, "NICK", - LINK(server)->away_nick); + LINK(server)->away_nick); if (LINK(server)->no_client_away_msg) WRITE_LINE1(CONN(server), NULL, "AWAY", - LINK(server)->no_client_away_msg); + LINK(server)->no_client_away_msg); } } @@ -337,9 +339,9 @@ static int irc_315(struct link_server *server, struct line *l) if (link->who_client->who_count > 0) { --link->who_client->who_count; mylog(LOG_DEBUG, - "RPL_ENDOFWHO: " - "Decrementing who count for %p: %d", - link->who_client, link->who_client->who_count); + "RPL_ENDOFWHO: " + "Decrementing who count for %p: %d", + link->who_client, link->who_client->who_count); } } @@ -367,7 +369,7 @@ void rotate_who_client(struct link *link) } int irc_dispatch_server(bip_t *bip, struct link_server *server, - struct line *line) + struct line *line) { int ret = OK_COPY; /* shut gcc up */ @@ -401,89 +403,115 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, } } else if (irc_line_elem_equals(line, 0, "CAP")) { if (LINK(server)->sasl_mechanism) { - if (irc_line_elem_equals(line, 2, "ACK") && irc_line_elem_equals(line, 3, "sasl")) { - // Server is answering our CAP REQ :sasl and is SASL capable - char *sasl_mech = sasl_mechanism_to_text(LINK(server)->sasl_mechanism); - mylog(LOG_INFO, "[%s] Server is SASL capable, starting %s authentication.", - LINK(server)->name, sasl_mech); - WRITE_LINE1(CONN(server), NULL, "AUTHENTICATE", sasl_mech); + if (irc_line_elem_equals(line, 2, "ACK") + && irc_line_elem_equals(line, 3, "sasl")) { + // Server is answering our CAP REQ :sasl and is + // SASL capable + char *sasl_mech = sasl_mechanism_to_text( + LINK(server)->sasl_mechanism); + mylog(LOG_INFO, + "[%s] Server is SASL capable, starting %s authentication.", + LINK(server)->name, sasl_mech); + WRITE_LINE1(CONN(server), NULL, "AUTHENTICATE", + sasl_mech); ret = OK_FORGET; - } else if (irc_line_elem_equals(line, 2, "NAK") && irc_line_elem_equals(line, 3, "sasl")) { - // Server is answering our CAP REQ :sasl and isn't SASL capable - mylog(LOG_INFO, "[%s] Server is not SASL capable.", LINK(server)->name); + } else if (irc_line_elem_equals(line, 2, "NAK") + && irc_line_elem_equals(line, 3, "sasl")) { + // Server is answering our CAP REQ :sasl and + // isn't SASL capable + mylog(LOG_INFO, + "[%s] Server is not SASL capable.", + LINK(server)->name); ret = ERR_PROTOCOL; } else { // Unhandled CAP message - mylog(LOG_ERROR, "[%s] Unhandled CAP message: %s", - LINK(server)->name, irc_line_to_string(line)); + mylog(LOG_ERROR, + "[%s] Unhandled CAP message: %s", + LINK(server)->name, + irc_line_to_string(line)); ret = OK_FORGET; } } else { // Unhandled CAP message - mylog(LOG_ERROR, "[%s] Unhandled CAP message: %s", LINK(server)->name, - irc_line_to_string(line)); + mylog(LOG_ERROR, "[%s] Unhandled CAP message: %s", + LINK(server)->name, irc_line_to_string(line)); ret = OK_FORGET; } } else if (irc_line_elem_equals(line, 0, "AUTHENTICATE")) { if (LINK(server)->sasl_mechanism) { - if (irc_line_count(line) == 2 && irc_line_elem_equals(line, 1, "+")) { - // Server is waiting for us to authenticate, let's do it - mylog(LOG_INFO, "[%s] Server accepted our authentication mechanism.", - LINK(server)->name); + if (irc_line_count(line) == 2 + && irc_line_elem_equals(line, 1, "+")) { + // Server is waiting for us to authenticate, + // let's do it + mylog(LOG_INFO, + "[%s] Server accepted our authentication mechanism.", + LINK(server)->name); ret = irc_server_sasl_authenticate(server); } else { - // Anything else than "AUTHENTICATE +" is unknown to us - mylog(LOG_ERROR, "[%s] Server sent gibberish: %s", - LINK(server)->name, irc_line_to_string(line)); + // Anything else than "AUTHENTICATE +" is + // unknown to us + mylog(LOG_ERROR, + "[%s] Server sent gibberish: %s", + LINK(server)->name, + irc_line_to_string(line)); ret = ERR_PROTOCOL; } } else { // Unhandled AUTHENTICATE message - mylog(LOG_ERROR, "[%s] Unhandled AUTHENTICATE message: %s", - LINK(server)->name, irc_line_to_string(line)); + mylog(LOG_ERROR, + "[%s] Unhandled AUTHENTICATE message: %s", + LINK(server)->name, irc_line_to_string(line)); ret = OK_FORGET; } } else if (irc_line_elem_equals(line, 0, "900")) { if (irc_line_count(line) >= 5) { - mylog(LOG_INFO, "[%s] Logged in as %s(%s): %s", LINK(server)->name, - irc_line_elem(line, 3), irc_line_elem(line, 2), irc_line_elem(line, 4)); + mylog(LOG_INFO, "[%s] Logged in as %s(%s): %s", + LINK(server)->name, irc_line_elem(line, 3), + irc_line_elem(line, 2), irc_line_elem(line, 4)); } else { - mylog(LOG_INFO, "[%s] Logged in: %s", LINK(server)->name, irc_line_to_string(line)); + mylog(LOG_INFO, "[%s] Logged in: %s", + LINK(server)->name, irc_line_to_string(line)); } ret = OK_FORGET; } else if (irc_line_elem_equals(line, 0, "901")) { if (irc_line_count(line) >= 4) { mylog(LOG_INFO, "[%s] Logged out: %s", - LINK(server)->name, irc_line_elem(line, 3)); + LINK(server)->name, irc_line_elem(line, 3)); } else { - mylog(LOG_INFO, "[%s] Logged out: %s", LINK(server)->name, irc_line_to_string(line)); + mylog(LOG_INFO, "[%s] Logged out: %s", + LINK(server)->name, irc_line_to_string(line)); } ret = OK_FORGET; } else if (irc_line_elem_equals(line, 0, "902")) { mylog(LOG_INFO, "[%s] Account unavailable: %s", - LINK(server)->name, irc_line_to_string(line)); + LINK(server)->name, irc_line_to_string(line)); ret = OK_FORGET; } else if (irc_line_elem_equals(line, 0, "903")) { - mylog(LOG_INFO, "[%s] SASL authentication successful", LINK(server)->name); + mylog(LOG_INFO, "[%s] SASL authentication successful", + LINK(server)->name); WRITE_LINE1(CONN(server), NULL, "CAP", "END"); ret = OK_FORGET; } else if (irc_line_elem_equals(line, 0, "904")) { - mylog(LOG_ERROR, "[%s] SASL authentication failed", LINK(server)->name); + mylog(LOG_ERROR, "[%s] SASL authentication failed", + LINK(server)->name); ret = ERR_AUTH; } else if (irc_line_elem_equals(line, 0, "905")) { - mylog(LOG_ERROR, "[%s] SASL message too long", LINK(server)->name); + mylog(LOG_ERROR, "[%s] SASL message too long", + LINK(server)->name); ret = ERR_AUTH; } else if (irc_line_elem_equals(line, 0, "906")) { mylog(LOG_ERROR, "[%s] SASL authentication aborted by client", - LINK(server)->name); + LINK(server)->name); ret = ERR_AUTH; } else if (irc_line_elem_equals(line, 0, "907")) { - mylog(LOG_ERROR, "[%s] SASL authentication has already been completed", - LINK(server)->name); + mylog(LOG_ERROR, + "[%s] SASL authentication has already been completed", + LINK(server)->name); ret = OK_FORGET; } else if (irc_line_elem_equals(line, 0, "908")) { - mylog(LOG_ERROR, "[%s] Server only accepts following authentication mechanisms: %s", - LINK(server)->name, irc_line_elem(line, 2)); + mylog(LOG_ERROR, + "[%s] Server only accepts following authentication mechanisms: %s", + LINK(server)->name, irc_line_elem(line, 2)); ret = ERR_AUTH; } else if (irc_line_elem_equals(line, 0, "433")) { if (LINK(server)->s_state != IRCS_CONNECTED) { @@ -501,8 +529,10 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, newnick[7] = '`'; } } else { - newnick[8] = (char)('a' + ('z' - 'a') * - rand() / RAND_MAX); + newnick[8] = + (char)('a' + + ('z' - 'a') * rand() + / RAND_MAX); } newnick[9] = 0; } @@ -516,35 +546,41 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, if (irc_line_elem_equals(line, 0, "376")) /* end of motd */ irc_server_connected(server); else if (irc_line_elem_equals(line, 0, "422")) /* no motd */ - irc_server_connected(server); + irc_server_connected(server); } else if (LINK(server)->s_state == IRCS_CONNECTING) { ret = OK_FORGET; - if (irc_line_elem_equals(line, 0, "005")) { + if (irc_line_elem_equals(line, 0, "005")) { int i; for (i = irc_line_count(line) - 1; i > 0; i--) { - if (LINK(server)->ignore_server_capab && - irc_line_elem_equals(line, i, "CAPAB")) + if (LINK(server)->ignore_server_capab + && irc_line_elem_equals(line, i, "CAPAB")) irc_line_drop(line, i); - else if (!strncmp(irc_line_elem(line, i), "CHANMODES=", (size_t)10)) - server_set_chanmodes(server, irc_line_elem(line, i) + 10); - else if (!strncmp(irc_line_elem(line, i), "PREFIX=(", (size_t)8)) - server_set_prefix(server, irc_line_elem(line, i) + 7); + else if (!strncmp(irc_line_elem(line, i), + "CHANMODES=", (size_t)10)) + server_set_chanmodes( + server, + irc_line_elem(line, i) + 10); + else if (!strncmp(irc_line_elem(line, i), + "PREFIX=(", (size_t)8)) + server_set_prefix(server, + irc_line_elem(line, i) + + 7); } } if (irc_line_elem_equals(line, 0, "NOTICE")) { } else if (irc_line_elem_equals(line, 0, "376")) { - /* end of motd */ + /* end of motd */ irc_server_connected(server); list_add_last(&LINK(server)->init_strings, - irc_line_dup(line)); + irc_line_dup(line)); } else if (irc_line_elem_equals(line, 0, "422")) { /* no motd */ irc_server_connected(server); list_add_last(&LINK(server)->init_strings, - irc_line_dup(line)); + irc_line_dup(line)); } else { list_add_last(&LINK(server)->init_strings, - irc_line_dup(line)); + irc_line_dup(line)); } } else if (irc_line_elem_equals(line, 0, "001")) { ret = irc_001(server, line); @@ -553,7 +589,7 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, return ERR_PROTOCOL; /* update the irc mask */ list_add_last(&LINK(server)->init_strings, - irc_line_dup(line)); + irc_line_dup(line)); } } else if (irc_line_elem_equals(line, 0, "JOIN")) { ret = irc_join(server, line); @@ -594,8 +630,8 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, if (ret == OK_COPY) { unsigned int i; for (i = 0; i < LINK(server)->l_clientc; i++) { - if (TYPE(LINK(server)->l_clientv[i]) == - IRC_TYPE_CLIENT) { + if (TYPE(LINK(server)->l_clientv[i]) + == IRC_TYPE_CLIENT) { char *s = irc_line_to_string(line); write_line(CONN(LINK(server)->l_clientv[i]), s); free(s); @@ -609,10 +645,10 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server, write_line(CONN(LINK(server)->who_client), s); free(s); } - if (LINK(server)->who_client && - LINK(server)->who_client->who_count == 0) { + if (LINK(server)->who_client + && LINK(server)->who_client->who_count == 0) { mylog(LOG_DEBUG, "OK_COPY_WHO: who_count for %p is nul", - LINK(server)->who_client); + LINK(server)->who_client); rotate_who_client(LINK(server)); } return ret; @@ -628,8 +664,8 @@ static void irc_send_join(struct link_client *ic, struct channel *chan) assert(user); /* user ircmask here for rbot */ - ircmask = bip_malloc(strlen(LINK(ic)->l_server->nick) + - strlen(BIP_FAKEMASK) + 1); + ircmask = bip_malloc(strlen(LINK(ic)->l_server->nick) + + strlen(BIP_FAKEMASK) + 1); strcpy(ircmask, LINK(ic)->l_server->nick); strcat(ircmask, BIP_FAKEMASK); WRITE_LINE1(CONN(ic), ircmask, "JOIN", chan->name); @@ -637,10 +673,10 @@ static void irc_send_join(struct link_client *ic, struct channel *chan) if (chan->topic) WRITE_LINE3(CONN(ic), P_SERV, "332", LINK(ic)->l_server->nick, - chan->name, chan->topic); + chan->name, chan->topic); if (chan->creator && chan->create_ts) WRITE_LINE4(CONN(ic), P_SERV, "333", LINK(ic)->l_server->nick, - chan->name, chan->creator, chan->create_ts); + chan->name, chan->creator, chan->create_ts); list_t *name_list = channel_name_list(LINK(ic)->l_server, chan); char *s; @@ -649,13 +685,13 @@ static void irc_send_join(struct link_client *ic, struct channel *chan) tmptype[0] = chan->type; tmptype[1] = 0; WRITE_LINE4(CONN(ic), P_SERV, "353", LINK(ic)->l_server->nick, - tmptype, chan->name, s); + tmptype, chan->name, s); free(s); } list_free(name_list); WRITE_LINE3(CONN(ic), P_SERV, "366", LINK(ic)->l_server->nick, - chan->name, "End of /NAMES list."); + chan->name, "End of /NAMES list."); } static void write_init_string(connection_t *c, struct line *line, char *nick) @@ -673,8 +709,8 @@ static void bind_to_link(struct link *l, struct link_client *ic) LINK(ic) = l; l->l_clientc++; - l->l_clientv = bip_realloc(l->l_clientv, l->l_clientc * - sizeof(struct link_client *)); + l->l_clientv = bip_realloc(l->l_clientv, + l->l_clientc * sizeof(struct link_client *)); l->l_clientv[i] = ic; } @@ -691,7 +727,7 @@ void unbind_from_link(struct link_client *ic) if (l->who_client == ic) { mylog(LOG_DEBUG, "unbind_from_link: %p: %d", l->who_client, - ic->who_count); + ic->who_count); l->who_client = NULL; } @@ -701,8 +737,8 @@ void unbind_from_link(struct link_client *ic) if (l->l_clientc == 0) fatal("unbind_from_link: negative client count"); l->l_clientc--; - l->l_clientv = bip_realloc(l->l_clientv, l->l_clientc * - sizeof(struct link_client *)); + l->l_clientv = bip_realloc(l->l_clientv, + l->l_clientc * sizeof(struct link_client *)); if (l->l_clientc == 0) { /* bip_realloc was equiv to free() */ l->l_clientv = NULL; return; @@ -758,21 +794,21 @@ static void irc_cli_make_join(struct link_client *ic) /* join channels, step one, those in conf, in order */ list_iterator_t li; for (list_it_init(&LINK(ic)->chan_infos_order, &li); - list_it_item(&li); list_it_next(&li)) { - struct chan_info *ci = (struct chan_info *) - list_it_item(&li); + list_it_item(&li); list_it_next(&li)) { + struct chan_info *ci = + (struct chan_info *)list_it_item(&li); struct channel *chan; if ((chan = hash_get(&LINK(ic)->l_server->channels, - ci->name))) + ci->name))) irc_send_join(ic, chan); } /* step two, those not in conf */ hash_iterator_t hi; for (hash_it_init(&LINK(ic)->l_server->channels, &hi); - hash_it_item(&hi); hash_it_next(&hi)) { - struct channel *chan = (struct channel *) - hash_it_item(&hi); + hash_it_item(&hi); hash_it_next(&hi)) { + struct channel *chan = + (struct channel *)hash_it_item(&hi); if (!hash_get(&LINK(ic)->chan_infos, chan->name)) irc_send_join(ic, chan); } @@ -789,7 +825,7 @@ void irc_cli_backlog(struct link_client *ic, int hours) if (!user->backlog) { mylog(LOG_DEBUG, "Backlog disabled for %s, not backlogging", - user->name); + user->name); return; } @@ -806,11 +842,11 @@ void irc_cli_backlog(struct link_client *ic, int hours) backlogl = log_backlogs(LINK(ic)->log); while ((bl = list_remove_first(backlogl))) { bllines = backlog_lines(LINK(ic)->log, bl, - LINK(ic)->l_server->nick, hours); + LINK(ic)->l_server->nick, hours); if (bllines) { if (!list_is_empty(bllines)) { mylog(LOG_INFO, "[%s] backlogging: %s", - LINK(ic)->name, bl); + LINK(ic)->name, bl); write_lines(CONN(ic), bllines); } list_free(bllines); @@ -821,7 +857,7 @@ void irc_cli_backlog(struct link_client *ic, int hours) } static int irc_cli_startup(bip_t *bip, struct link_client *ic, - struct line *line) + struct line *line) { char *init_nick; char *user, *pass, *connname; @@ -846,12 +882,12 @@ static int irc_cli_startup(bip_t *bip, struct link_client *ic, list_iterator_t it; for (list_it_init(&bip->link_list, &it); list_it_item(&it); - list_it_next(&it)) { + list_it_next(&it)) { struct link *l = list_it_item(&it); - if (strcmp(user, l->user->name) == 0 && - strcmp(connname, l->name) == 0) { - if (chash_cmp(pass, l->user->password, - l->user->seed) == 0) { + if (strcmp(user, l->user->name) == 0 + && strcmp(connname, l->name) == 0) { + if (chash_cmp(pass, l->user->password, l->user->seed) + == 0) { bind_to_link(l, ic); break; } @@ -860,7 +896,7 @@ static int irc_cli_startup(bip_t *bip, struct link_client *ic, if (!LINK(ic)) mylog(LOG_ERROR, "[%s] Invalid credentials (user: %s)", - connname, user); + connname, user); free(user); free(connname); free(pass); @@ -887,7 +923,8 @@ static int irc_cli_startup(bip_t *bip, struct link_client *ic, if (LINK(ic)->s_state == IRCS_NONE) { /* drop it if corresponding server hasn't connected at all. */ - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":ERROR Proxy not yet connected, try again " "later\r\n"); unbind_from_link(ic); @@ -898,8 +935,8 @@ static int irc_cli_startup(bip_t *bip, struct link_client *ic, list_remove(&bip->connecting_client_list, ic); TYPE(ic) = IRC_TYPE_CLIENT; - for (list_it_init(&LINK(ic)->init_strings, &it); - list_it_item(&it); list_it_next(&it)) + for (list_it_init(&LINK(ic)->init_strings, &it); list_it_item(&it); + list_it_next(&it)) write_init_string(CONN(ic), list_it_item(&it), init_nick); /* we change nick on server */ @@ -909,10 +946,10 @@ static int irc_cli_startup(bip_t *bip, struct link_client *ic, if (!LINK(ic)->ignore_first_nick) WRITE_LINE1(CONN(server), NULL, "NICK", init_nick); - else if (LINK(ic)->away_nick && - strcmp(LINK(ic)->away_nick, server->nick) == 0) + else if (LINK(ic)->away_nick + && strcmp(LINK(ic)->away_nick, server->nick) == 0) WRITE_LINE1(CONN(server), NULL, "NICK", - LINK(server)->connect_nick); + LINK(server)->connect_nick); /* change away status */ if (server && LINK(ic)->no_client_away_msg) @@ -951,8 +988,8 @@ static int irc_cli_nick(bip_t *bip, struct link_client *ic, struct line *line) if ((ic->state & IRCC_PASS) != IRCC_PASS) WRITE_LINE2(CONN(ic), P_SERV, "NOTICE", ic->init_nick, - "You should type /QUOTE PASS your_username:" - "your_password:your_connection_name"); + "You should type /QUOTE PASS your_username:" + "your_password:your_connection_name"); return OK_FORGET; } @@ -996,7 +1033,7 @@ static int irc_cli_quit(struct link_client *ic, struct line *line) } static int irc_cli_privmsg(bip_t *bip, struct link_client *ic, - struct line *line) + struct line *line) { if (!irc_line_includes(line, 2)) return OK_FORGET; @@ -1005,7 +1042,7 @@ static int irc_cli_privmsg(bip_t *bip, struct link_client *ic, return adm_bip(bip, ic, line, 1); else log_cli_privmsg(LINK(ic)->log, LINK(ic)->l_server->nick, - irc_line_elem(line, 1), irc_line_elem(line, 2)); + irc_line_elem(line, 1), irc_line_elem(line, 2)); if (LINK(ic)->user->blreset_on_talk) { if (LINK(ic)->user->blreset_connection) @@ -1021,7 +1058,7 @@ static int irc_cli_notice(struct link_client *ic, struct line *line) if (!irc_line_includes(line, 2)) return OK_FORGET; log_cli_notice(LINK(ic)->log, LINK(ic)->l_server->nick, - irc_line_elem(line, 1), irc_line_elem(line, 2)); + irc_line_elem(line, 1), irc_line_elem(line, 2)); if (LINK(ic)->user->blreset_on_talk) { if (LINK(ic)->user->blreset_connection) log_reset_all(LINK(ic)->log); @@ -1038,8 +1075,8 @@ static int irc_cli_who(struct link_client *ic, struct line *line) ++ic->who_count; if (ic->who_count == 1) ic->whoc_tstamp = time(NULL); - mylog(LOG_DEBUG, "cli_who: Incrementing who count for %p: %d", - ic, ic->who_count); + mylog(LOG_DEBUG, "cli_who: Incrementing who count for %p: %d", ic, + ic->who_count); if (l->who_client && l->who_client != ic) { list_add_first(&ic->who_queue, irc_line_to_string(line)); @@ -1060,15 +1097,15 @@ static int irc_cli_mode(struct link_client *ic, struct line *line) return OK_COPY; /* This is a wild guess and that sucks. */ - if (!irc_line_elem_equals(line, 0, "MODE") || - strchr(irc_line_elem(line, 2), 'b') == NULL) + if (!irc_line_elem_equals(line, 0, "MODE") + || strchr(irc_line_elem(line, 2), 'b') == NULL) return OK_COPY; ++ic->who_count; if (ic->who_count == 1) ic->whoc_tstamp = time(NULL); mylog(LOG_DEBUG, "cli_mode: Incrementing who count for %p: %d", - l->who_client, ic->who_count); + l->who_client, ic->who_count); if (l->who_client && l->who_client != ic) { list_add_first(&ic->who_queue, irc_line_to_string(line)); @@ -1091,18 +1128,18 @@ static void irc_notify_disconnection(struct link_server *is) struct link_client *ic = LINK(is)->l_clientv[i]; hash_iterator_t hi; for (hash_it_init(&is->channels, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { struct channel *c = (struct channel *)hash_it_item(&hi); - WRITE_LINE3(CONN(ic), P_IRCMASK, "KICK", - c->name, is->nick, - "Server disconnected, reconnecting"); + WRITE_LINE3(CONN(ic), P_IRCMASK, "KICK", c->name, + is->nick, + "Server disconnected, reconnecting"); } bip_notify(ic, "Server disconnected, reconnecting"); } } void irc_add_channel_info(struct link_server *ircs, const char *chan, - const char *key) + const char *key) { struct chan_info *ci; // TODO resolve this issue from array_get @@ -1191,8 +1228,8 @@ static int irc_cli_part(struct link_client *irc, struct line *line) cname = irc_line_elem(line, 1); - if ((ci = hash_remove_if_exists(&LINK(irc)->chan_infos, - cname)) != NULL) { + if ((ci = hash_remove_if_exists(&LINK(irc)->chan_infos, cname)) + != NULL) { list_remove(&LINK(irc)->chan_infos_order, ci); free(ci->name); if (ci->key) @@ -1209,8 +1246,8 @@ static int irc_dispatch_trust_client(struct link_client *ic, struct line *line) if (!irc_line_includes(line, 1)) return ERR_PROTOCOL; - if (strcasecmp(irc_line_elem(line, 0), "BIP") == 0 && - strcasecmp(irc_line_elem(line, 1), "TRUST") == 0) + if (strcasecmp(irc_line_elem(line, 0), "BIP") == 0 + && strcasecmp(irc_line_elem(line, 1), "TRUST") == 0) r = adm_trust(ic, line); return r; @@ -1218,7 +1255,7 @@ static int irc_dispatch_trust_client(struct link_client *ic, struct line *line) #endif static int irc_dispatch_client(bip_t *bip, struct link_client *ic, - struct line *line) + struct line *line) { int r = OK_COPY; if (irc_line_count(line) == 0) @@ -1228,10 +1265,11 @@ static int irc_dispatch_client(bip_t *bip, struct link_client *ic, if (!irc_line_includes(line, 1)) return ERR_PROTOCOL; WRITE_LINE1(CONN(ic), link_name((struct link_any *)ic), "PONG", - irc_line_elem(line, 1)); + irc_line_elem(line, 1)); r = OK_FORGET; } else if (LINK(ic)->s_state != IRCS_CONNECTED) { - write_line_fast(CONN(ic), ":irc.bip.net NOTICE pouet " + write_line_fast(CONN(ic), + ":irc.bip.net NOTICE pouet " ":ERROR Proxy not connected, please wait " "before sending commands\r\n"); r = OK_FORGET; @@ -1257,16 +1295,17 @@ static int irc_dispatch_client(bip_t *bip, struct link_client *ic, if (r == OK_COPY || r == OK_COPY_CLI) { char *str = irc_line_to_string(line); - if (LINK(ic)->s_state == IRCS_CONNECTED && - LINK(ic)->l_server->nick) + if (LINK(ic)->s_state == IRCS_CONNECTED + && LINK(ic)->l_server->nick) write_line(CONN(LINK(ic)->l_server), str); else if (LINK(ic)->l_server->nick) WRITE_LINE2(CONN(ic), P_IRCMASK, - (LINK(ic)->user->bip_use_notice ? - "NOTICE" : "PRIVMSG"), - LINK(ic)->l_server->nick, - ":Not connected please try again " - "later...\r\n"); + (LINK(ic)->user->bip_use_notice + ? "NOTICE" + : "PRIVMSG"), + LINK(ic)->l_server->nick, + ":Not connected please try again " + "later...\r\n"); free(str); if (r == OK_COPY_CLI) { @@ -1281,15 +1320,15 @@ static int irc_dispatch_client(bip_t *bip, struct link_client *ic, } static void irc_copy_cli(struct link_client *src, struct link_client *dest, - struct line *line) + struct line *line) { char *str; if (src == dest) return; - if (!irc_line_includes(line, 1) || - !irc_line_elem_equals(line, 0, "PRIVMSG")) { + if (!irc_line_includes(line, 1) + || !irc_line_elem_equals(line, 0, "PRIVMSG")) { str = irc_line_to_string(line); write_line(CONN(dest), str); free(str); @@ -1314,7 +1353,8 @@ static void irc_copy_cli(struct link_client *src, struct link_client *dest, /* LINK(src) == LINK(dest) */ size_t len = strlen(irc_line_elem(line, 2)) + 6; // snprintf fix ^ - // ‘__builtin___snprintf_chk’ output may be truncated before the last format character + // ‘__builtin___snprintf_chk’ output may be truncated before the last + // format character char *tmp; if (len == 0) @@ -1352,7 +1392,7 @@ static void irc_copy_cli(struct link_client *src, struct link_client *dest, } static int irc_dispatch_logging_client(bip_t *bip, struct link_client *ic, - struct line *line) + struct line *line) { if (irc_line_count(line) == 0) return ERR_PROTOCOL; @@ -1371,18 +1411,18 @@ int irc_dispatch(bip_t *bip, struct link_any *l, struct line *line) { switch (TYPE(l)) { case IRC_TYPE_SERVER: - return irc_dispatch_server(bip, (struct link_server*)l, line); + return irc_dispatch_server(bip, (struct link_server *)l, line); break; case IRC_TYPE_CLIENT: - return irc_dispatch_client(bip, (struct link_client*)l, line); + return irc_dispatch_client(bip, (struct link_client *)l, line); break; case IRC_TYPE_LOGGING_CLIENT: - return irc_dispatch_logging_client(bip, (struct link_client*)l, - line); + return irc_dispatch_logging_client(bip, (struct link_client *)l, + line); break; #ifdef HAVE_LIBSSL case IRC_TYPE_TRUST_CLIENT: - return irc_dispatch_trust_client((struct link_client*)l, line); + return irc_dispatch_trust_client((struct link_client *)l, line); break; #endif default: @@ -1485,7 +1525,7 @@ static int irc_333(struct link_server *server, struct line *line) channel->create_ts = bip_strdup("0"); } log_init_topic_time(LINK(server)->log, channel->name, channel->creator, - channel->create_ts); + channel->create_ts); return OK_COPY; } @@ -1518,7 +1558,7 @@ static int irc_353(struct link_server *server, struct line *line) while (*names) { long int ovmask = 0; /* some ircds (e.g. unreal) may display several flags for the - same nick */ + same nick */ // TODO resolve this issue from array_get // passing argument X of .... with different width due to prototype #pragma GCC diagnostic push @@ -1584,9 +1624,9 @@ static int irc_368(struct link_server *server, struct line *l) if (link->who_client->who_count > 0) { --link->who_client->who_count; mylog(LOG_DEBUG, - "RPL_ENDOFBANLIST: " - "Decrementing who count for %p: %d", - link->who_client, link->who_client->who_count); + "RPL_ENDOFBANLIST: " + "Decrementing who count for %p: %d", + link->who_client, link->who_client->who_count); } } @@ -1630,8 +1670,8 @@ static int irc_part(struct link_server *server, struct line *line) if (origin_is_me(line, server)) { log_part(LINK(server)->log, line->origin, s_chan, - irc_line_count(line) == 3 ? irc_line_elem(line, 2) : - NULL); + irc_line_count(line) == 3 ? irc_line_elem(line, 2) + : NULL); log_reset_store(LINK(server)->log, s_chan); log_drop(LINK(server)->log, s_chan); @@ -1654,8 +1694,7 @@ static int irc_part(struct link_server *server, struct line *line) free(s_nick); log_part(LINK(server)->log, line->origin, s_chan, - irc_line_count(line) == 3 ? - irc_line_elem(line, 2) : NULL); + irc_line_count(line) == 3 ? irc_line_elem(line, 2) : NULL); return OK_COPY; } @@ -1679,8 +1718,8 @@ static void mode_remove_letter(struct link_server *s, char c) for (; i < s->user_mode_len - 1; i++) s->user_mode[i] = s->user_mode[i + 1]; s->user_mode_len--; - s->user_mode = bip_realloc(s->user_mode, - s->user_mode_len); + s->user_mode = + bip_realloc(s->user_mode, s->user_mode_len); return; } } @@ -1728,8 +1767,8 @@ static int irc_mode(struct link_server *server, struct line *line) if (irc_line_includes(line, 3)) mode_args = array_extract(&line->words, 3, -1); log_mode(LINK(server)->log, line->origin, - irc_line_elem(line, 1), irc_line_elem(line, 2), - mode_args); + irc_line_elem(line, 1), irc_line_elem(line, 2), + mode_args); if (mode_args) array_free(mode_args); irc_user_mode(server, line); @@ -1754,7 +1793,7 @@ static int irc_mode(struct link_server *server, struct line *line) if (irc_line_includes(line, 3)) mode_args = array_extract(&line->words, 3, -1); log_mode(LINK(server)->log, line->origin, irc_line_elem(line, 1), - irc_line_elem(line, 2), mode_args); + irc_line_elem(line, 2), mode_args); if (mode_args) array_free(mode_args); @@ -1775,26 +1814,31 @@ static int irc_mode(struct link_server *server, struct line *line) // Check if mode is known: first user modes then // server modes if (!(str = strchr(server->usermodes, *mode))) { - array_each(&server->chanmodes, i, str) { + array_each(&server->chanmodes, i, str) + { if ((str = strchr(str, *mode))) break; } } if (str) { - // Usermodes, types A & B always take a parameter - // Type C take a parameter only when set + // Usermodes, types A & B always take a + // parameter Type C take a parameter only when + // set if (i <= 1 || (i == 2 && add)) { - if (!irc_line_includes(line, cur_arg + 3)) { + if (!irc_line_includes(line, + cur_arg + 3)) { return ERR_PROTOCOL; } else { - ret = irc_mode_channel(server, channel, line, mode, - add, cur_arg); + ret = irc_mode_channel( + server, channel, line, + mode, add, cur_arg); cur_arg++; } } else { - ret = irc_mode_channel(server, channel, line, mode, add, - cur_arg); + ret = irc_mode_channel(server, channel, + line, mode, add, + cur_arg); } } } @@ -1805,7 +1849,8 @@ static int irc_mode(struct link_server *server, struct line *line) } static int irc_mode_channel(struct link_server *s, struct channel *channel, - struct line *line, const char* mode, int add, int cur_arg) + struct line *line, const char *mode, int add, + int cur_arg) { const char *nick; long int ovmask; @@ -1817,8 +1862,8 @@ static int irc_mode_channel(struct link_server *s, struct channel *channel, #pragma GCC diagnostic ignored "-Wtraditional-conversion" if (*mode == 'k') { if (add) { - channel->key = bip_strdup( - irc_line_elem(line, cur_arg + 3)); + channel->key = + bip_strdup(irc_line_elem(line, cur_arg + 3)); } else { if (channel->key) { free(channel->key); @@ -1882,7 +1927,7 @@ static int irc_topic(struct link_server *server, struct line *line) channel->create_ts = irc_timestamp(); log_topic(LINK(server)->log, line->origin, irc_line_elem(line, 1), - topic); + topic); return OK_COPY; } @@ -1904,19 +1949,19 @@ static int irc_kick(struct link_server *server, struct line *line) if (strcasecmp(irc_line_elem(line, 2), server->nick) == 0) { /* we get kicked !! */ log_kick(LINK(server)->log, line->origin, channel->name, - irc_line_elem(line, 2), - irc_line_count(line) == 4 ? - irc_line_elem(line, 3) : NULL); + irc_line_elem(line, 2), + irc_line_count(line) == 4 ? irc_line_elem(line, 3) + : NULL); log_reset_store(LINK(server)->log, channel->name); log_drop(LINK(server)->log, channel->name); if (LINK(server)->autojoin_on_kick) { if (!channel->key) WRITE_LINE1(CONN(server), NULL, "JOIN", - channel->name); + channel->name); else WRITE_LINE2(CONN(server), NULL, "JOIN", - channel->name, channel->key); + channel->name, channel->key); } hash_remove(&server->channels, channel->name); @@ -1926,8 +1971,8 @@ static int irc_kick(struct link_server *server, struct line *line) hash_remove(&channel->ovmasks, irc_line_elem(line, 2)); log_kick(LINK(server)->log, line->origin, irc_line_elem(line, 1), - irc_line_elem(line, 2), - irc_line_count(line) == 4 ? irc_line_elem(line, 3) : NULL); + irc_line_elem(line, 2), + irc_line_count(line) == 4 ? irc_line_elem(line, 3) : NULL); return OK_COPY; @@ -1949,7 +1994,7 @@ static void irc_privmsg_check_ctcp(struct link_server *server, return; if (irc_line_elem_equals(line, 2, "\001VERSION\001")) { WRITE_LINE2(CONN(server), NULL, "NOTICE", nick, - "\001VERSION bip-" PACKAGE_VERSION "\001"); + "\001VERSION bip-" PACKAGE_VERSION "\001"); } free(nick); } @@ -1960,7 +2005,7 @@ static int irc_privmsg(struct link_server *server, struct line *line) return ERR_PROTOCOL; if (LINK(server)->s_state == IRCS_CONNECTED) log_privmsg(LINK(server)->log, line->origin, - irc_line_elem(line, 1), irc_line_elem(line, 2)); + irc_line_elem(line, 1), irc_line_elem(line, 2)); irc_privmsg_check_ctcp(server, line); return OK_COPY; } @@ -1971,7 +2016,7 @@ static int irc_notice(struct link_server *server, struct line *line) return ERR_PROTOCOL; if (LINK(server)->s_state == IRCS_CONNECTED) log_notice(LINK(server)->log, line->origin, - irc_line_elem(line, 1), irc_line_elem(line, 2)); + irc_line_elem(line, 1), irc_line_elem(line, 2)); return OK_COPY; } @@ -2000,7 +2045,7 @@ static int irc_nick(struct link_server *server, struct line *line) dst_nick = irc_line_elem(line, 1); for (hash_it_init(&server->channels, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { channel = hash_it_item(&hi); if (!hash_includes(&channel->ovmasks, org_nick)) continue; @@ -2011,10 +2056,10 @@ static int irc_nick(struct link_server *server, struct line *line) if (origin_is_me(line, server)) { free(server->nick); server->nick = bip_strdup(dst_nick); - if (LINK(server)->follow_nick && - (LINK(server)->away_nick == NULL || - strcmp(server->nick, LINK(server)->away_nick)) - != 0) { + if (LINK(server)->follow_nick + && (LINK(server)->away_nick == NULL + || strcmp(server->nick, LINK(server)->away_nick)) + != 0) { free(LINK(server)->connect_nick); LINK(server)->connect_nick = bip_strdup(server->nick); } @@ -2040,14 +2085,14 @@ static int irc_generic_quit(struct link_server *server, struct line *line) if (!s_nick) return ERR_PROTOCOL; for (hash_it_init(&server->channels, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { channel = hash_it_item(&hi); if (!hash_includes(&channel->ovmasks, s_nick)) continue; hash_remove(&channel->ovmasks, s_nick); log_quit(LINK(server)->log, line->origin, channel->name, - irc_line_includes(line, 1) ? - irc_line_elem(line, 1) : NULL); + irc_line_includes(line, 1) ? irc_line_elem(line, 1) + : NULL); } free(s_nick); return OK_COPY; @@ -2106,7 +2151,8 @@ static int irc_server_sasl_authenticate(struct link_server *ircs) // Should not happen, but we never know right ? if (!sasl_username || !sasl_password) { - mylog(LOG_ERROR, "[%s] Missing SASL username or password.", LINK(ircs)->name); + mylog(LOG_ERROR, "[%s] Missing SASL username or password.", + LINK(ircs)->name); return ERR_AUTH; } @@ -2118,7 +2164,7 @@ static int irc_server_sasl_authenticate(struct link_server *ircs) char chunk[SASL_AUTH_CHUNK_SZ + 1]; size_t u_len = strlen(sasl_username); size_t p_len = strlen(sasl_password); - size_t raw_len = u_len*2 + p_len + 2; + size_t raw_len = u_len * 2 + p_len + 2; size_t enc_len; unsigned char *raw_str = bip_malloc(raw_len + 1); unsigned char *enc_str; @@ -2126,28 +2172,31 @@ static int irc_server_sasl_authenticate(struct link_server *ircs) 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); + raw_str[u_len * 2 + 1] = '\0'; + memcpy(raw_str + u_len * 2 + 2, sasl_password, p_len); 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); + mylog(LOG_DEBUG, "[%s] Base64 encoded SASL auth token (len %d): %s", + LINK(ircs)->name, enc_len, enc_str); for (size_t i = 0; i < enc_len; i += chunk_chars) { size_t remaining = enc_len - i; if (remaining < chunk_chars) { memcpy(chunk, &enc_str[i], remaining); - chunk[remaining]= '\0'; + chunk[remaining] = '\0'; } else { memcpy(chunk, &enc_str[i], chunk_chars); - chunk[chunk_chars]= '\0'; + chunk[chunk_chars] = '\0'; } mylog(LOG_DEBUG, "[%s] SASL AUTHENTICATE chunk %d, len %d: %s", - LINK(ircs)->name, i/chunk_chars, strlen(chunk), chunk); + LINK(ircs)->name, i / chunk_chars, strlen(chunk), chunk); WRITE_LINE1(CONN(ircs), NULL, "AUTHENTICATE", chunk); - // Send a closing AUTHENTICATE line if last chunk size was exactly 400 + // Send a closing AUTHENTICATE line if last chunk size was + // exactly 400 if (remaining == chunk_chars) { - mylog(LOG_DEBUG, "[%s] Last SASL chunk was exactly 400, sending +", - LINK(ircs)->name); + mylog(LOG_DEBUG, + "[%s] Last SASL chunk was exactly 400, sending +", + LINK(ircs)->name); WRITE_LINE1(CONN(ircs), NULL, "AUTHENTICATE", "+"); break; } @@ -2163,7 +2212,7 @@ static void irc_server_startup(struct link_server *ircs) char *username, *realname; /* lower the token number as freenode hates fast login */ - CONN(ircs)->token = 1; + CONN(ircs)->token = 1; if (LINK(ircs)->s_password) WRITE_LINE1(CONN(ircs), NULL, "PASS", LINK(ircs)->s_password); @@ -2183,7 +2232,7 @@ static void irc_server_startup(struct link_server *ircs) nick = bip_strdup(LINK(ircs)->away_nick); } if ((!LINK(ircs)->follow_nick && !LINK(ircs)->away_nick) - || nick == NULL) { + || nick == NULL) { if (nick) free(nick); if (!LINK(ircs)->connect_nick) @@ -2236,7 +2285,7 @@ void server_cleanup(struct link_server *server) hash_iterator_t hi; for (hash_it_init(&server->channels, &hi); hash_it_item(&hi); - hash_it_next(&hi)) + hash_it_next(&hi)) channel_free(hash_it_item(&hi)); hash_clean(&server->channels); @@ -2256,10 +2305,10 @@ void irc_client_close(struct link_client *ic) if (LINK(ic)->l_clientc == 0) { if (is && LINK(ic)->away_nick) WRITE_LINE1(CONN(is), NULL, "NICK", - LINK(ic)->away_nick); + LINK(ic)->away_nick); if (is && LINK(ic)->no_client_away_msg) WRITE_LINE1(CONN(is), NULL, "AWAY", - LINK(ic)->no_client_away_msg); + LINK(ic)->no_client_away_msg); log_client_none_connected(LINK(ic)->log); } irc_client_free(ic); @@ -2275,15 +2324,13 @@ static void server_setup_reconnect_timer(struct link *link) { int timer = 0; - if (link->last_connection_attempt && - time(NULL) - link->last_connection_attempt - < CONN_INTERVAL) { + if (link->last_connection_attempt + && time(NULL) - link->last_connection_attempt < CONN_INTERVAL) { timer = conf_reconn_timer * (link->s_conn_attempt); if (timer > RECONN_TIMER_MAX) timer = RECONN_TIMER_MAX; } - mylog(LOG_ERROR, "[%s] reconnecting in %d seconds", link->name, - timer); + mylog(LOG_ERROR, "[%s] reconnecting in %d seconds", link->name, timer); link->recon_timer = timer; } @@ -2371,15 +2418,14 @@ void irc_server_free(struct link_server *s) int i; char *ptr; - array_each(&s->chanmodes, i, ptr) - free(ptr); + array_each(&s->chanmodes, i, ptr) free(ptr); MAYFREE(s->prefixes); MAYFREE(s->usermodes); hash_iterator_t hi; for (hash_it_init(&s->channels, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { struct channel *chan = hash_it_item(&hi); channel_free(chan); } @@ -2395,23 +2441,23 @@ connection_t *irc_server_connect(struct link *link) link->s_conn_attempt++; - mylog(LOG_INFO, "[%s] Connecting user '%s' using server " - "%s:%d", link->name, link->user->name, - link->network->serverv[link->cur_server].host, - link->network->serverv[link->cur_server].port); + mylog(LOG_INFO, + "[%s] Connecting user '%s' using server " + "%s:%d", + link->name, link->user->name, + link->network->serverv[link->cur_server].host, + link->network->serverv[link->cur_server].port); conn = connection_new(link->network->serverv[link->cur_server].host, - link->network->serverv[link->cur_server].port, - link->vhost, link->bind_port, + link->network->serverv[link->cur_server].port, + link->vhost, link->bind_port, #ifdef HAVE_LIBSSL - link->network->ssl, - link->network->ciphers, - link->ssl_check_mode, - link->user->ssl_check_store, - link->user->ssl_client_certfile, + link->network->ssl, link->network->ciphers, + link->ssl_check_mode, link->user->ssl_check_store, + link->user->ssl_client_certfile, #else - 0, NULL, 0, NULL, NULL, + 0, NULL, 0, NULL, NULL, #endif - (time_t)CONNECT_TIMEOUT); + (time_t)CONNECT_TIMEOUT); assert(conn); if (conn->handle == -1) { mylog(LOG_INFO, "[%s] Cannot connect.", link->name); @@ -2428,7 +2474,7 @@ connection_t *irc_server_connect(struct link *link) if (link->sasl_mechanism) { mylog(LOG_INFO, "[%s] SASL (%s) enabled, sending CAP REQ.", - link->name, sasl_mechanism_to_text(link->sasl_mechanism)); + link->name, sasl_mechanism_to_text(link->sasl_mechanism)); WRITE_LINE2(conn, NULL, "CAP", "REQ", ":sasl"); } irc_server_startup(ls); @@ -2458,8 +2504,7 @@ void irc_server_shutdown(struct link_server *s) { int i; char *cur; - array_each(&s->chanmodes, i, cur) - free(cur); + array_each(&s->chanmodes, i, cur) free(cur); array_deinit(&s->chanmodes); server_init_modes(s); @@ -2492,7 +2537,7 @@ void oidentd_dump(bip_t *bip) fchmod(fileno(f), 0644); } else { mylog(LOG_WARN, "Can't open/create %s", - bip->oidentdpath); + bip->oidentdpath); return; } } else { @@ -2502,7 +2547,7 @@ void oidentd_dump(bip_t *bip) if (!f) { mylog(LOG_WARN, "Can't open/create %s", - bip->oidentdpath); + bip->oidentdpath); return; } @@ -2510,9 +2555,10 @@ void oidentd_dump(bip_t *bip) content = (char *)bip_malloc((size_t)(stats.st_size + 1)); // validate that content is of stats.st_size size - if (fread(content, (size_t)1, (size_t)stats.st_size, f) != - (size_t)stats.st_size) { - mylog(LOG_WARN, "Can't read %s fully", bip->oidentdpath); + if (fread(content, (size_t)1, (size_t)stats.st_size, f) + != (size_t)stats.st_size) { + mylog(LOG_WARN, "Can't read %s fully", + bip->oidentdpath); free(content); goto clean_oidentd; } @@ -2526,51 +2572,56 @@ void oidentd_dump(bip_t *bip) fseek(f, (long)SEEK_SET, (int)0); if (ftruncate(fileno(f), (off_t)0) == -1) { mylog(LOG_DEBUG, "Can't reset %s size", - bip->oidentdpath); + bip->oidentdpath); free(content); goto clean_oidentd; } // data preceeding the tag, bipstart >= content (strstr) - fwrite(content, (size_t)1, (size_t)(bipstart - content), f); + fwrite(content, (size_t)1, (size_t)(bipstart - content), + f); bipend = strstr(bipstart, BIP_OIDENTD_END); if (bipend == NULL) { mylog(LOG_WARN, "No %s mark found in %s", - BIP_OIDENTD_END, - bip->oidentdpath); + BIP_OIDENTD_END, bip->oidentdpath); } else { /* data following the tag * ...........BIP_OIDENTD_START...BIP_OIDENTD_END.............. - * ^content...^bipstart...........^bipend........^remaining data + * ^content...^bipstart...........^bipend........^remaining + * data */ - char *remaining = bipend + BIP_OIDENTD_END_LENGTH; - off_t remaining_len = stats.st_size - - (bipend - content) - (off_t)BIP_OIDENTD_END_LENGTH; + char *remaining = + bipend + BIP_OIDENTD_END_LENGTH; + off_t remaining_len = + stats.st_size - (bipend - content) + - (off_t)BIP_OIDENTD_END_LENGTH; if (remaining_len < 0) { - mylog(LOG_ERROR, "oidentd_dump: error parsing %s", - bip->oidentdpath); + mylog(LOG_ERROR, + "oidentd_dump: error parsing %s", + bip->oidentdpath); goto clean_oidentd; - } - fwrite(remaining, (size_t)1, (size_t)remaining_len, f); + } + fwrite(remaining, (size_t)1, + (size_t)remaining_len, f); } } else { /* No previous conf */ - if (stats.st_size != 0 && - content[stats.st_size - 1] != '\n') + if (stats.st_size != 0 + && content[stats.st_size - 1] != '\n') fprintf(f, "\n"); } free(content); } for (list_it_init(&bip->conn_list, &it); list_it_item(&it); - list_it_next(&it)) { + list_it_next(&it)) { connection_t *c = list_it_item(&it); struct link_any *la = c->user_data; - if (la && TYPE(la) == IRC_TYPE_SERVER && ( - c->connected == CONN_OK || - c->connected == CONN_NEED_SSLIZE || - c->connected == CONN_UNTRUSTED)) { + if (la && TYPE(la) == IRC_TYPE_SERVER + && (c->connected == CONN_OK + || c->connected == CONN_NEED_SSLIZE + || c->connected == CONN_UNTRUSTED)) { struct link_server *ls; struct link *l; @@ -2579,12 +2630,12 @@ void oidentd_dump(bip_t *bip) tag_written = 1; } - ls = (struct link_server*)la; + ls = (struct link_server *)la; l = LINK(ls); fprintf(f, "to %s fport %d from %s lport %d {\n", - c->remoteip, c->remoteport, c->localip, - c->localport); + c->remoteip, c->remoteport, c->localip, + c->localport); fprintf(f, "\treply \"%s\"\n", l->username); fprintf(f, "}\n"); } @@ -2607,8 +2658,9 @@ void timeout_clean_who_counts(list_t *conns) time_t now; now = time(NULL); if (now - client->whoc_tstamp > 10) { - mylog(LOG_DEBUG, "Yawn, " - "forgetting one who reply"); + mylog(LOG_DEBUG, + "Yawn, " + "forgetting one who reply"); if (client->who_count > 0) --client->who_count; client->whoc_tstamp = time(NULL); @@ -2645,13 +2697,13 @@ void bip_tick(bip_t *bip) /* handle tick for links: detect lags or start a reconnection */ for (list_it_init(&bip->link_list, &li); (link = list_it_item(&li)); - list_it_next(&li)) { + list_it_next(&li)) { if (link->l_server) { if (irc_server_lag_compute(link)) { log_ping_timeout(link->log); list_remove(&bip->conn_list, - CONN(link->l_server)); - irc_close((struct link_any *) link->l_server); + CONN(link->l_server)); + irc_close((struct link_any *)link->l_server); } } else { if (link->recon_timer == 0) { @@ -2668,7 +2720,7 @@ void bip_tick(bip_t *bip) /* drop lagging connecting client */ for (list_it_init(&bip->connecting_client_list, &li); list_it_item(&li); - list_it_next(&li)) { + list_it_next(&li)) { struct link_client *ic = list_it_item(&li); ic->logging_timer++; if (ic->logging_timer > LOGGING_TIMEOUT) { @@ -2704,7 +2756,7 @@ void bip_on_event(bip_t *bip, connection_t *conn) if (err) { if (TYPE(lc) == IRC_TYPE_SERVER) { mylog(LOG_ERROR, "[%s] read_lines error, closing...", - link_name(lc)); + link_name(lc)); irc_server_shutdown(LINK(lc)->l_server); } else { mylog(LOG_ERROR, "client read_lines error, closing..."); @@ -2725,8 +2777,10 @@ void bip_on_event(bip_t *bip, connection_t *conn) line = irc_line_new_from_string(line_s); if (!line) { - mylog(LOG_ERROR, "[%s] Can not parse line. Link type: %d. " - "closing...", link_name(lc), TYPE(lc)); + mylog(LOG_ERROR, + "[%s] Can not parse line. Link type: %d. " + "closing...", + link_name(lc), TYPE(lc)); free(line_s); goto prot_err_lines; } @@ -2735,8 +2789,9 @@ void bip_on_event(bip_t *bip, connection_t *conn) irc_line_free(line); free(line_s); if (r == ERR_PROTOCOL) { - mylog(LOG_ERROR, "[%s] Error in protocol. Link type: %d closing...", - link_name(lc), TYPE(lc)); + mylog(LOG_ERROR, + "[%s] Error in protocol. Link type: %d closing...", + link_name(lc), TYPE(lc)); goto prot_err_lines; } if (r == ERR_AUTH) @@ -2744,7 +2799,6 @@ void bip_on_event(bip_t *bip, connection_t *conn) /* XXX: not real error */ if (r == OK_CLOSE) goto prot_err_lines; - } list_free(linel); return; @@ -2756,8 +2810,8 @@ prot_err: if (linel) list_free(linel); if (lc) { - if (TYPE(lc) == IRC_TYPE_LOGGING_CLIENT || TYPE(lc) == - IRC_TYPE_TRUST_CLIENT) + if (TYPE(lc) == IRC_TYPE_LOGGING_CLIENT + || TYPE(lc) == IRC_TYPE_TRUST_CLIENT) list_remove(&bip->connecting_client_list, lc); irc_close(lc); } @@ -2897,8 +2951,7 @@ static void server_set_chanmodes(struct link_server *l, const char *modes) mylog(LOG_DEBUG, "[%s] Set chanmodes", LINK(l)->name); - array_each(&l->chanmodes, i, cur) - free(cur); + array_each(&l->chanmodes, i, cur) free(cur); array_deinit(&l->chanmodes); // handle four categories, ignore all others @@ -2927,7 +2980,7 @@ static void server_set_chanmodes(struct link_server *l, const char *modes) static void server_set_prefix(struct link_server *s, const char *modes) { - char * end_mode; + char *end_mode; size_t len; mylog(LOG_DEBUG, "[%s] Set user modes", LINK(s)->name); @@ -2936,31 +2989,37 @@ static void server_set_prefix(struct link_server *s, const char *modes) end_mode = strchr(modes + 1, ')'); // skip '(' if (*modes != '(' || !end_mode) { - mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", LINK(s)->name); + mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", + LINK(s)->name); return; } // end_mode can't be lower than (modes + 1) len = (size_t)(end_mode - modes - 1); // len of mode without '(' if (len * 2 + 2 != strlen(modes)) { - mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", LINK(s)->name); + mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", + LINK(s)->name); return; } - s->prefixes = bip_realloc(s->prefixes, sizeof(*s->prefixes) * (len + 1)); - s->usermodes = bip_realloc(s->usermodes, sizeof(s->usermodes) * (len + 1)); + s->prefixes = + bip_realloc(s->prefixes, sizeof(*s->prefixes) * (len + 1)); + s->usermodes = + bip_realloc(s->usermodes, sizeof(s->usermodes) * (len + 1)); memcpy(s->usermodes, modes + 1, len); s->usermodes[len] = 0; memcpy(s->prefixes, end_mode + 1, len); s->prefixes[len] = 0; - mylog(LOG_DEBUGVERB, "[%s] user prefix: '%s'", LINK(s)->name, s->prefixes); - mylog(LOG_DEBUGVERB, "[%s] user modes: '%s'", LINK(s)->name, s->usermodes); + mylog(LOG_DEBUGVERB, "[%s] user prefix: '%s'", LINK(s)->name, + s->prefixes); + mylog(LOG_DEBUGVERB, "[%s] user modes: '%s'", LINK(s)->name, + s->usermodes); } // Return the position (*1 based*) of car in str, else -1 -static int bip_get_index(const char* str, char car) +static int bip_get_index(const char *str, char car) { char *cur; long diff; diff --git a/src/irc.h b/src/irc.h index e29ac77..07e937d 100644 --- a/src/irc.h +++ b/src/irc.h @@ -96,8 +96,7 @@ struct bipuser { char in_use; /* for mark and sweep on reload */ }; -struct network -{ +struct network { char *name; #ifdef HAVE_LIBSSL int ssl; @@ -111,7 +110,7 @@ struct network #define SASL_AUTH_PLAIN 2 struct link { - char *name; /* id */ + char *name; /* id */ /** link live data **/ struct link_server *l_server; @@ -144,8 +143,8 @@ struct link { list_t on_connect_send; char *no_client_away_msg; char *away_nick; - hash_t chan_infos; /* channels we want */ - list_t chan_infos_order; /* for order only */ + hash_t chan_infos; /* channels we want */ + list_t chan_infos_order; /* for order only */ struct bipuser *user; @@ -168,7 +167,7 @@ struct link { #ifdef HAVE_LIBSSL int ssl_check_mode; - STACK_OF(X509) *untrusted_certs; + STACK_OF(X509) * untrusted_certs; #endif int in_use; /* for mark and sweep on reload */ }; @@ -189,9 +188,9 @@ struct link_any { #define IRCC_NONE (0) #define IRCC_NICK (1) -#define IRCC_USER (1<<1) -#define IRCC_PASS (1<<2) -#define IRCC_READY (IRCC_NICK|IRCC_PASS|IRCC_USER) +#define IRCC_USER (1 << 1) +#define IRCC_PASS (1 << 2) +#define IRCC_READY (IRCC_NICK | IRCC_PASS | IRCC_USER) struct link_client { struct link_connection _link_c; @@ -284,4 +283,3 @@ void irc_cli_backlog(struct link_client *ic, int hours); #define BIP_FAKEMASK "!bip@bip.bip.bip" #endif - diff --git a/src/line.c b/src/line.c index 24936b3..34cc19d 100644 --- a/src/line.c +++ b/src/line.c @@ -16,7 +16,8 @@ #include "line.h" #include "util.h" -// TODO resolve assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 +// 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" void irc_line_init(struct line *l) @@ -85,7 +86,7 @@ char *irc_line_to_string(struct line *l) 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 ":" */ + len++; /* last args ":" */ ret = bip_malloc(len + 1); ret[0] = 0; diff --git a/src/line.h b/src/line.h index 729bc86..2965766 100644 --- a/src/line.h +++ b/src/line.h @@ -17,65 +17,65 @@ #include "connection.h" -#define WRITE_LINE0(con, org, com) \ - do { \ - struct line l; \ - irc_line_init(&l); \ - l.origin = org; \ - _irc_line_append(&l, com); \ - irc_line_write(&l, con); \ - _irc_line_deinit(&l); \ - } while(0) +#define WRITE_LINE0(con, org, com) \ + do { \ + struct line l; \ + irc_line_init(&l); \ + l.origin = org; \ + _irc_line_append(&l, com); \ + irc_line_write(&l, con); \ + _irc_line_deinit(&l); \ + } while (0) -#define WRITE_LINE1(con, org, com, a) \ - do { \ - struct line l; \ - irc_line_init(&l); \ - l.origin = org; \ - _irc_line_append(&l, com); \ - _irc_line_append(&l, a); \ - irc_line_write(&l, con); \ - _irc_line_deinit(&l); \ - } while(0) +#define WRITE_LINE1(con, org, com, a) \ + do { \ + struct line l; \ + irc_line_init(&l); \ + l.origin = org; \ + _irc_line_append(&l, com); \ + _irc_line_append(&l, a); \ + irc_line_write(&l, con); \ + _irc_line_deinit(&l); \ + } while (0) -#define WRITE_LINE2(con, org, com, a1, a2) \ - do { \ - struct line l; \ - irc_line_init(&l); \ - l.origin = org; \ - _irc_line_append(&l, com); \ - _irc_line_append(&l, a1); \ - _irc_line_append(&l, a2); \ - irc_line_write(&l, con); \ - _irc_line_deinit(&l); \ - } while(0) +#define WRITE_LINE2(con, org, com, a1, a2) \ + do { \ + struct line l; \ + irc_line_init(&l); \ + l.origin = org; \ + _irc_line_append(&l, com); \ + _irc_line_append(&l, a1); \ + _irc_line_append(&l, a2); \ + irc_line_write(&l, con); \ + _irc_line_deinit(&l); \ + } while (0) -#define WRITE_LINE3(con, org, com, a1, a2, a3) \ - do { \ - struct line l; \ - irc_line_init(&l); \ - l.origin = org; \ - _irc_line_append(&l, com); \ - _irc_line_append(&l, a1); \ - _irc_line_append(&l, a2); \ - _irc_line_append(&l, a3); \ - irc_line_write(&l, con); \ - _irc_line_deinit(&l); \ - } while(0) +#define WRITE_LINE3(con, org, com, a1, a2, a3) \ + do { \ + struct line l; \ + irc_line_init(&l); \ + l.origin = org; \ + _irc_line_append(&l, com); \ + _irc_line_append(&l, a1); \ + _irc_line_append(&l, a2); \ + _irc_line_append(&l, a3); \ + irc_line_write(&l, con); \ + _irc_line_deinit(&l); \ + } while (0) -#define WRITE_LINE4(con, org, com, a1, a2, a3, a4) \ - do { \ - struct line l; \ - irc_line_init(&l); \ - l.origin = org; \ - _irc_line_append(&l, com); \ - _irc_line_append(&l, a1); \ - _irc_line_append(&l, a2); \ - _irc_line_append(&l, a3); \ - _irc_line_append(&l, a4); \ - irc_line_write(&l, con); \ - _irc_line_deinit(&l); \ - } while(0) +#define WRITE_LINE4(con, org, com, a1, a2, a3, a4) \ + do { \ + struct line l; \ + irc_line_init(&l); \ + l.origin = org; \ + _irc_line_append(&l, com); \ + _irc_line_append(&l, a1); \ + _irc_line_append(&l, a2); \ + _irc_line_append(&l, a3); \ + _irc_line_append(&l, a4); \ + irc_line_write(&l, con); \ + _irc_line_deinit(&l); \ + } while (0) struct line { char *origin; diff --git a/src/log.c b/src/log.c index eb61341..40d681e 100644 --- a/src/log.c +++ b/src/log.c @@ -32,7 +32,7 @@ extern int conf_log; extern FILE *conf_global_log_file; static size_t _log_write(log_t *logdata, logstore_t *lf, const char *d, - const char *str); + const char *str); static char *_log_wrap(const char *dest, const char *line); void logfile_free(logfile_t *lf); void log_drop(log_t *log, const char *storename); @@ -53,16 +53,15 @@ int check_dir(char *filename, int is_fatal) if (err) { if (is_fatal) fatal("mkdir(%s) %s", filename, - strerror(errno)); + strerror(errno)); mylog(LOG_ERROR, "mkdir(%s) %s", filename, - strerror(errno)); + strerror(errno)); return 1; } } else if (err) { if (is_fatal) fatal("stat(%s) %s", filename, strerror(errno)); - mylog(LOG_ERROR, "stat(%s) %s", filename, - strerror(errno)); + mylog(LOG_ERROR, "stat(%s) %s", filename, strerror(errno)); return 1; } else if (!(statbuf.st_mode & S_IFDIR)) { if (is_fatal) @@ -99,7 +98,7 @@ int check_dir_r(char *dirname) tmp += pos; count += pos; *(dir + count) = '\0'; - mylog(LOG_DEBUGVERB,"check_dir_r: %s", dir); + mylog(LOG_DEBUGVERB, "check_dir_r: %s", dir); if (check_dir(dir, 0)) { free(dir); return 1; @@ -114,7 +113,8 @@ void strtolower(char *str) char *c; for (c = str; *c != '\0'; c++) - *c = (char)tolower(*c); // tolower()->int but should be safe to cast + *c = (char)tolower( + *c); // tolower()->int but should be safe to cast } /* @@ -128,7 +128,7 @@ void replace_var(char *str, char *var, char *value, unsigned int max) size_t lenvar = strlen(var); size_t lenval = strlen(value); - while((pos = strstr(str, var))) { + while ((pos = strstr(str, var))) { /* Make room */ if (strlen(str) + lenval - lenvar >= max) return; @@ -154,7 +154,7 @@ char *log_build_filename(log_t *logdata, const char *destination) strftime(month, (size_t)3, "%m", now); strftime(hour, (size_t)3, "%H", now); snprintf(logfile, (size_t)MAX_PATH_LEN, "%s/%s", conf_log_root, - conf_log_format); + conf_log_format); replace_var(logfile, "%u", logdata->user->name, MAX_PATH_LEN); replace_var(logfile, "%n", logdata->network, MAX_PATH_LEN); replace_var(logfile, "%c", dest, MAX_PATH_LEN); @@ -201,8 +201,8 @@ static void log_reset(logstore_t *store) return; } - while ((olf = list_get_first(&store->file_group)) && - olf != list_get_last(&store->file_group)) { + while ((olf = list_get_first(&store->file_group)) + && olf != list_get_last(&store->file_group)) { logfile_free(olf); list_remove_first(&store->file_group); } @@ -225,7 +225,7 @@ static void log_reset(logstore_t *store) void log_reinit(logstore_t *store) { mylog(LOG_ERROR, "%s is inconsistent, droping backlog info", - store->name); + store->name); log_reset(store); } @@ -253,10 +253,10 @@ static int log_has_file(log_t *logdata, const char *fname) logstore_t *store; for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { store = hash_it_item(&hi); for (list_it_init(&store->file_group, &li); list_it_item(&li); - list_it_next(&li)) { + list_it_next(&li)) { logfile_t *lf = list_it_item(&li); if (strcmp(fname, lf->filename) == 0) return 1; @@ -266,7 +266,7 @@ static int log_has_file(log_t *logdata, const char *fname) } static int log_add_file(log_t *logdata, const char *destination, - const char *filename) + const char *filename) { FILE *f; logstore_t *store; @@ -286,7 +286,7 @@ static int log_add_file(log_t *logdata, const char *destination, f = fopen(uniq_fname, "a+"); if (!f) { mylog(LOG_ERROR, "fopen(%s) %s", uniq_fname, - strerror(errno)); + strerror(errno)); free(uniq_fname); free(canonical_fname); return 0; @@ -294,7 +294,7 @@ static int log_add_file(log_t *logdata, const char *destination, if (fseek(f, (long)0, SEEK_END) == -1) { mylog(LOG_ERROR, "fseek(%s) %s", uniq_fname, - strerror(errno)); + strerror(errno)); free(uniq_fname); free(canonical_fname); fclose(f); @@ -307,7 +307,7 @@ static int log_add_file(log_t *logdata, const char *destination, lf->len = (size_t)ftell_r; if (ftell_r < 0) { mylog(LOG_ERROR, "log_add_file: ftell error %s", - strerror(errno)); + strerror(errno)); free(uniq_fname); free(canonical_fname); fclose(f); @@ -377,17 +377,17 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) if (!filename) return NULL; mylog(LOG_DEBUG, "Creating new logfile for %s: %s", - destination, filename); + destination, filename); } if (!log_add_file(logdata, destination, filename)) { - if (filename) - free(filename); + if (filename) + free(filename); return NULL; } store = hash_get(&logdata->logfgs, destination); assert(store); /* ok we are allocating a new store now, let's set it up for - * backlogging if applicable */ + * backlogging if applicable */ assert(logdata->user); assert(logdata->network); l = hash_get(&logdata->user->connections, logdata->network); @@ -413,8 +413,8 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) time(&t); ltime = localtime(&t); - if (ltime->tm_year != lf->last_log.tm_year || - ltime->tm_yday != lf->last_log.tm_yday) { + if (ltime->tm_year != lf->last_log.tm_year + || ltime->tm_yday != lf->last_log.tm_yday) { logfile_t *oldlf; /* day changed, we might want to rotate logfile */ @@ -430,7 +430,7 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) /* we do want to rotate logfiles */ mylog(LOG_DEBUG, "Rotating logfile for %s %s %s", destination, - lf->filename, filename); + lf->filename, filename); oldlf = list_get_last(&store->file_group); if (!log_add_file(logdata, destination, filename)) { @@ -443,8 +443,8 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) /* remove oldlf from file_group */ assert(list_remove_first(&store->file_group) == oldlf); logfile_free(oldlf); - assert(list_get_first(&store->file_group) == - list_get_last(&store->file_group)); + assert(list_get_first(&store->file_group) + == list_get_last(&store->file_group)); } else { fclose(oldlf->file); oldlf->file = NULL; @@ -456,53 +456,51 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) void log_join(log_t *logdata, const char *ircmask, const char *channel) { snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s has joined %s", timestamp(), ircmask, - channel); + "%s -!- %s has joined %s", timestamp(), ircmask, channel); log_write(logdata, channel, logdata->buffer); } void log_part(log_t *logdata, const char *ircmask, const char *channel, - const char *message) + const char *message) { if (message) snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s has left %s [%s]", timestamp(), ircmask, - channel, message); + "%s -!- %s has left %s [%s]", timestamp(), ircmask, + channel, message); else snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s has left %s", timestamp(), ircmask, - channel); + "%s -!- %s has left %s", timestamp(), ircmask, + channel); log_write(logdata, channel, logdata->buffer); } void log_kick(log_t *logdata, const char *ircmask, const char *channel, - const char *who, const char *message) + const char *who, const char *message) { snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s has been kicked by %s [%s]", timestamp(), - who, ircmask, message); + "%s -!- %s has been kicked by %s [%s]", timestamp(), who, + ircmask, message); log_write(logdata, channel, logdata->buffer); } void log_quit(log_t *logdata, const char *ircmask, const char *channel, - const char *message) + const char *message) { snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s has quit [%s]", timestamp(), ircmask, - message); + "%s -!- %s has quit [%s]", timestamp(), ircmask, message); log_write(logdata, channel, logdata->buffer); } void log_nick(log_t *logdata, const char *ircmask, const char *channel, - const char *newnick) + const char *newnick) { char *oldnick = nick_from_ircmask(ircmask); - logstore_t* oldstore; - logstore_t* newstore; + logstore_t *oldstore; + logstore_t *newstore; if ((oldstore = hash_get(&logdata->logfgs, oldnick))) { if ((newstore = hash_get(&logdata->logfgs, newnick)) - && oldstore != newstore) { + && oldstore != newstore) { log_drop(logdata, newnick); } hash_rename_key(&logdata->logfgs, oldnick, newnick); @@ -510,13 +508,12 @@ void log_nick(log_t *logdata, const char *ircmask, const char *channel, free(oldnick); snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s is now known as %s", - timestamp(), ircmask, newnick); + "%s -!- %s is now known as %s", timestamp(), ircmask, newnick); log_write(logdata, channel, logdata->buffer); } static void do_log_privmsg(log_t *logdata, const char *storage, int src, - const char *from, const char *message) + const char *from, const char *message) { char dir = '<'; @@ -525,10 +522,11 @@ static void do_log_privmsg(log_t *logdata, const char *storage, int src, if (src) dir = '>'; - if (strlen(message) > 8 && ((*message == '\001' || - ((*message == '+' || *message == '-') && - (*(message + 1) == '\001'))) && - (*(message + strlen(message) - 1) == '\001'))) { + if (strlen(message) > 8 + && ((*message == '\001' + || ((*message == '+' || *message == '-') + && (*(message + 1) == '\001'))) + && (*(message + strlen(message) - 1) == '\001'))) { char *msg; /* hack for freenode and the like */ const char *real_message = message; @@ -541,19 +539,17 @@ static void do_log_privmsg(log_t *logdata, const char *storage, int src, msg = bip_strdup(real_message); *(msg + strlen(msg) - 1) = '\0'; snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s %c * %s %s", timestamp(), dir, - from, msg + 8); + "%s %c * %s %s", timestamp(), dir, from, msg + 8); free(msg); } else { snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s %c %s: %s", timestamp(), dir, - from, message); + "%s %c %s: %s", timestamp(), dir, from, message); } log_write(logdata, storage, logdata->buffer); } void log_privmsg(log_t *logdata, const char *ircmask, const char *destination, - const char *message) + const char *message) { // TODO resolve this issue from array_get // passing argument X of .... with different width due to prototype @@ -570,7 +566,7 @@ void log_privmsg(log_t *logdata, const char *ircmask, const char *destination, } void log_cli_privmsg(log_t *logdata, const char *ircmask, - const char *destination, const char *message) + const char *destination, const char *message) { do_log_privmsg(logdata, destination, 1, ircmask, message); } @@ -595,30 +591,29 @@ void log_notice(log_t *logdata, const char *ircmask, const char *destination, } void log_cli_notice(log_t *logdata, const char *ircmask, - const char *destination, const char *message) + const char *destination, const char *message) { do_log_privmsg(logdata, destination, 1, ircmask, message); } void log_topic(log_t *logdata, const char *ircmask, const char *channel, - const char *message) + const char *message) { snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- %s changed topic of %s to: %s", timestamp(), - ircmask, channel, message); + "%s -!- %s changed topic of %s to: %s", timestamp(), ircmask, + channel, message); log_write(logdata, channel, logdata->buffer); } void log_init_topic(log_t *logdata, const char *channel, const char *message) { snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- Topic for %s: %s", timestamp(), channel, - message); + "%s -!- Topic for %s: %s", timestamp(), channel, message); log_write(logdata, channel, logdata->buffer); } void log_init_topic_time(log_t *logdata, const char *channel, const char *who, - const char *when) + const char *when) { struct tm *time; char *timestr; @@ -633,34 +628,33 @@ void log_init_topic_time(log_t *logdata, const char *channel, const char *who, timestr[50] = '\0'; snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- Topic set by %s [%s]", timestamp(), who, - timestr); + "%s -!- Topic set by %s [%s]", timestamp(), who, timestr); free(timestr); log_write(logdata, channel, logdata->buffer); } void log_mode(log_t *logdata, const char *ircmask, const char *channel, - const char *modes, array_t *mode_args) + const char *modes, array_t *mode_args) { int i; char *tmpbuf = bip_malloc((size_t)LOGLINE_MAXLEN + 1); char *tmpbuf2 = bip_malloc((size_t)LOGLINE_MAXLEN + 1); char *tmp; - snprintf(tmpbuf, (size_t)LOGLINE_MAXLEN, - "%s -!- mode/%s [%s", timestamp(), channel, modes); + snprintf(tmpbuf, (size_t)LOGLINE_MAXLEN, "%s -!- mode/%s [%s", + timestamp(), channel, modes); if (mode_args) { for (i = 0; i < array_count(mode_args); i++) { - snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", tmpbuf, - (const char*)array_get(mode_args, i)); + snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", + tmpbuf, (const char *)array_get(mode_args, i)); tmp = tmpbuf; tmpbuf = tmpbuf2; tmpbuf2 = tmp; } } - snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s] by %s", - tmpbuf, ircmask); + snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s] by %s", tmpbuf, + ircmask); log_write(logdata, channel, logdata->buffer); free(tmpbuf); @@ -670,10 +664,12 @@ void log_mode(log_t *logdata, const char *ircmask, const char *channel, void log_disconnected(log_t *logdata) { hash_iterator_t hi; - snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s -!- Disconnected" - " from server...", timestamp()); + snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, + "%s -!- Disconnected" + " from server...", + timestamp()); for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi); - hash_it_next(&hi)) + hash_it_next(&hi)) log_write(logdata, hash_it_key(&hi), logdata->buffer); } @@ -683,7 +679,7 @@ void log_ping_timeout(log_t *logdata) char *s; snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, - "%s -!- Ping timeout with server...", timestamp()); + "%s -!- Ping timeout with server...", timestamp()); while ((s = list_remove_first(l))) { log_write(logdata, s, logdata->buffer); free(s); @@ -696,10 +692,12 @@ void log_connected(log_t *logdata) { hash_iterator_t hi; - snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s -!- Connected to" - " server...", timestamp()); + snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, + "%s -!- Connected to" + " server...", + timestamp()); for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { log_write(logdata, hash_it_key(&hi), logdata->buffer); } } @@ -740,7 +738,7 @@ void log_reset_all(log_t *logdata) list_init(&drop, NULL); for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { store = hash_it_item(&hi); // TODO resolve this issue from array_get // passing argument X of .... with different width due to prototype @@ -793,11 +791,11 @@ void log_client_connected(log_t *logdata) logdata->connected = 1; } -void log_advance_backlogs(log_t* ld, logstore_t *store) +void log_advance_backlogs(log_t *ld, logstore_t *store) { int c; - //mylog(LOG_ERROR, "LOG: log_advance_backlogs %s", store->name); + // mylog(LOG_ERROR, "LOG: log_advance_backlogs %s", store->name); if (!store->track_backlog) return; @@ -811,18 +809,20 @@ void log_advance_backlogs(log_t* ld, logstore_t *store) logfile_t *lf; while ((lf = list_it_item(&store->file_it))) { - //mylog(LOG_ERROR, "LOG: %s %d", lf->filename, store->file_offset); + // mylog(LOG_ERROR, "LOG: %s %d", lf->filename, + // store->file_offset); if (!lf->file) { lf->file = fopen(lf->filename, "r"); if (!lf->file) { mylog(LOG_ERROR, "Can't open %s for reading", - lf->filename); + lf->filename); log_reinit(store); return; } } if (fseek(lf->file, store->file_offset, SEEK_SET)) { - //mylog(LOG_ERROR, "LOG: fseek FAIL %s %d", lf->filename, store->file_offset); + // mylog(LOG_ERROR, "LOG: fseek FAIL %s %d", + // lf->filename, store->file_offset); log_reinit(store); return; } @@ -830,16 +830,20 @@ void log_advance_backlogs(log_t* ld, logstore_t *store) while ((c = fgetc(lf->file)) != EOF) { store->file_offset++; if (c == '\n') { - //mylog(LOG_ERROR, "LOG: %s advanced at %d", lf->filename, store->file_offset); + // mylog(LOG_ERROR, "LOG: %s advanced at %d", + // lf->filename, store->file_offset); return; } } - //mylog(LOG_ERROR, "LOG: %s advanced at %d", lf->filename, store->file_offset); + // mylog(LOG_ERROR, "LOG: %s advanced at %d", lf->filename, + // store->file_offset); if (lf == list_get_last(&store->file_group)) { - //mylog(LOG_ERROR, "LOG: %s is last of file_group", lf->filename, store->file_offset); + // mylog(LOG_ERROR, "LOG: %s is last of file_group", + // lf->filename, store->file_offset); return; } - //mylog(LOG_ERROR, "LOG: next file %s %d", lf->filename, store->file_offset); + // mylog(LOG_ERROR, "LOG: next file %s %d", lf->filename, + // store->file_offset); fclose(lf->file); lf->file = NULL; list_it_next(&store->file_it); @@ -879,7 +883,7 @@ chan: 09-01-2009 14:16:21 < nohar!~nohar@haruka.t1r.net: chantestrepl */ char *log_beautify(log_t *logdata, const char *buf, const char *storename, - const char *dest) + const char *dest) { int action = 0; char *p; @@ -962,10 +966,10 @@ char *log_beautify(log_t *logdata, const char *buf, const char *storename, return _log_wrap(dest, buf); // action and out are 0 or 1, safe to cast - p = ret = (char *)bip_malloc( - 1 + lon + strlen(LAMESTRING) + strlen(dest) + 2 + lots + 2 + - lom + 3 + (size_t)action * (2 + strlen("ACTION ")) + - (size_t)out * strlen(PMSG_ARROW)); + p = ret = (char *)bip_malloc(1 + lon + strlen(LAMESTRING) + strlen(dest) + + 2 + lots + 2 + lom + 3 + + (size_t)action * (2 + strlen("ACTION ")) + + (size_t)out * strlen(PMSG_ARROW)); *p++ = ':'; @@ -977,8 +981,8 @@ char *log_beautify(log_t *logdata, const char *buf, const char *storename, memcpy(p, dest, strlen(dest)); p += strlen(dest); - //memcpy(p, sod, lod); - //p += lod; + // memcpy(p, sod, lod); + // p += lod; *p++ = ' '; *p++ = ':'; @@ -1032,20 +1036,20 @@ static time_t compute_time(const char *buf) } static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf, - list_t *res, const char *dest, time_t start) + list_t *res, const char *dest, time_t start) { char *buf, *logbr; int close = 0; - //mylog(LOG_ERROR, "log_backread_file store:", store->name); + // mylog(LOG_ERROR, "log_backread_file store:", store->name); if (!lf->file) { lf->file = fopen(lf->filename, "r"); if (!lf->file) { mylog(LOG_ERROR, "Can't open %s for reading", - lf->filename); + lf->filename); list_add_last(res, _log_wrap("Error opening logfile", - store->name)); + store->name)); return 0; } @@ -1059,26 +1063,29 @@ static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf, */ if (fseek(lf->file, store->file_offset, SEEK_SET)) { mylog(LOG_ERROR, "Can't seek in %s", lf->filename); - list_add_last(res, _log_wrap(store->name, + list_add_last(res, + _log_wrap(store->name, "Error seeking in logfile")); return 0; } } else { - //mylog(LOG_ERROR, "bread Seeking %s to %d", lf->filename, 0); + // mylog(LOG_ERROR, "bread Seeking %s to %d", lf->filename, 0); if (fseek(lf->file, (long)0, SEEK_SET)) { mylog(LOG_ERROR, "Can't seek in %s", lf->filename); - list_add_last(res, _log_wrap(store->name, + list_add_last(res, + _log_wrap(store->name, "Error seeking in logfile")); return 0; } } buf = bip_malloc((size_t)LOGLINE_MAXLEN + 1); - for(;;) { + for (;;) { if (!fgets(buf, LOGLINE_MAXLEN, lf->file)) { if (ferror(lf->file)) { list_add_last(res, _log_wrap("Error reading " - "logfile", store->name)); + "logfile", + store->name)); } /* error or oef */ break; @@ -1098,8 +1105,8 @@ static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf, /* parse error, don't backlog */ if (linetime == (time_t)-1) { list_add_last(res, _log_wrap("Error in " - "timestamp in %s", - store->name)); + "timestamp in %s", + store->name)); continue; } /* too old line, don't backlog */ @@ -1109,14 +1116,13 @@ static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf, logbr = log_beautify(log, buf, store->name, dest); if (logbr) list_add_last(res, logbr); - } if (close) { fclose(lf->file); lf->file = NULL; } free(buf); - //mylog(LOG_ERROR, "end of log_backread_file store: %s", store->name); + // mylog(LOG_ERROR, "end of log_backread_file store: %s", store->name); return 1; } @@ -1139,7 +1145,7 @@ static list_t *log_backread(log_t *log, const char *storename, const char *dest) ret = list_new(NULL); for (list_it_init(store->memlog, &li); list_it_item(&li); - list_it_next(&li)) + list_it_next(&li)) list_add_last(ret, bip_strdup(list_it_item(&li))); return ret; } @@ -1153,11 +1159,10 @@ static list_t *log_backread(log_t *log, const char *storename, const char *dest) logfile_t *logf; ret = list_new(NULL); - for (file_it = store->file_it; - (logf = list_it_item(&file_it)); - list_it_next(&file_it)) { + for (file_it = store->file_it; (logf = list_it_item(&file_it)); + list_it_next(&file_it)) { if (!log_backread_file(log, store, logf, ret, dest, - (time_t)0)) { + (time_t)0)) { log_reinit(store); return ret; } @@ -1172,10 +1177,10 @@ static char *_log_wrap(const char *dest, const char *line) buf = bip_malloc((size_t)LOGLINE_MAXLEN + 1); count = snprintf(buf, (size_t)LOGLINE_MAXLEN + 1, - ":" P_IRCMASK " PRIVMSG %s :%s\r\n", dest, line); + ":" P_IRCMASK " PRIVMSG %s :%s\r\n", dest, line); if (count < 0) { mylog(LOG_ERROR, "_log_wrap: error on snprintf: %s", - strerror(errno)); + strerror(errno)); buf[LOGLINE_MAXLEN - 2] = '\r'; buf[LOGLINE_MAXLEN - 1] = '\n'; buf[LOGLINE_MAXLEN] = 0; @@ -1191,7 +1196,7 @@ static char *_log_wrap(const char *dest, const char *line) } static size_t _log_write(log_t *logdata, logstore_t *store, - const char *destination, const char *str) + const char *destination, const char *str) { size_t nbwrite; size_t len; @@ -1201,8 +1206,8 @@ static size_t _log_write(log_t *logdata, logstore_t *store, tmpstr[LOGLINE_MAXLEN] = 0; if (store->memlog) { - char *r = log_beautify(logdata, tmpstr, store->name, - destination); + char *r = + log_beautify(logdata, tmpstr, store->name, destination); if (r != NULL) { list_add_last(store->memlog, r); if (store->memc == logdata->user->backlog_lines) @@ -1236,7 +1241,7 @@ void log_write(log_t *logdata, const char *destination, const char *str) if (!store) { mylog(LOG_ERROR, "Unable to find/create logfile for '%s'", - destination); + destination); return; } written = _log_write(logdata, store, destination, str); @@ -1254,15 +1259,15 @@ void log_flush_all(void) fflush(conf_global_log_file); for (list_it_init(log_all_logs, &li); list_it_item(&li); - list_it_next(&li)) { + list_it_next(&li)) { log_t *log = list_it_item(&li); hash_iterator_t hi; for (hash_it_init(&log->logfgs, &hi); hash_it_item(&hi); - hash_it_next(&hi)) { + hash_it_next(&hi)) { logstore_t *store = hash_it_item(&hi); list_iterator_t lj; for (list_it_init(&store->file_group, &lj); - list_it_item(&lj); list_it_next(&lj)) { + list_it_item(&lj); list_it_next(&lj)) { logfile_t *lf = list_it_item(&lj); if (lf->file) fflush(lf->file); @@ -1301,7 +1306,7 @@ void log_free(log_t *log) free(log->buffer); for (hash_it_init(&log->logfgs, &it); (store = hash_it_item(&it)); - hash_it_next(&it)) { + hash_it_next(&it)) { log_reset(store); log_store_free(store); } @@ -1320,7 +1325,8 @@ array_t *str_split(const char *str, const char *splt) const char *start = str; size_t len; char *extracted; - array_t *array = array_new();; + array_t *array = array_new(); + ; do { if (!*p || strchr(splt, *p)) { @@ -1341,7 +1347,7 @@ array_t *str_split(const char *str, const char *splt) /* 26-12-2008 08:54:39 */ int log_parse_date(char *strdate, int *year, int *month, int *mday, int *hour, - int *min, int *sec) + int *min, int *sec) { array_t *fields; char *sptr; @@ -1358,8 +1364,7 @@ int log_parse_date(char *strdate, int *year, int *month, int *mday, int *hour, ret = 1; } int i; - array_each(fields, i, sptr) - free(sptr); + array_each(fields, i, sptr) free(sptr); array_free(fields); return ret; @@ -1368,7 +1373,7 @@ int log_parse_date(char *strdate, int *year, int *month, int *mday, int *hour, void logstore_get_file_at(logstore_t *store, time_t at, list_iterator_t *li) { for (list_it_init(&store->file_group, li); list_it_item(li); - list_it_next(li)) { + list_it_next(li)) { logfile_t *lf = list_it_item(li); if (mktime(&lf->last_log) > at) @@ -1377,7 +1382,7 @@ void logstore_get_file_at(logstore_t *store, time_t at, list_iterator_t *li) } static list_t *log_backread_hours(log_t *log, const char *storename, - const char *dest, int hours) + const char *dest, int hours) { time_t blstarttime; struct timeval tv; @@ -1397,10 +1402,9 @@ static list_t *log_backread_hours(log_t *log, const char *storename, ret = list_new(NULL); for (logstore_get_file_at(store, blstarttime, &file_it); - (logf = list_it_item(&file_it)); - list_it_next(&file_it)) { + (logf = list_it_item(&file_it)); list_it_next(&file_it)) { if (!log_backread_file(log, store, logf, ret, dest, - blstarttime)) { + blstarttime)) { log_reinit(store); return ret; } @@ -1409,7 +1413,7 @@ static list_t *log_backread_hours(log_t *log, const char *storename, } list_t *backlog_lines(log_t *log, const char *bl, const char *cli_nick, - int hours) + int hours) { list_t *ret; struct line l; @@ -1454,4 +1458,3 @@ list_t *backlog_lines(log_t *log, const char *bl, const char *cli_nick, } return ret; } - diff --git a/src/log.h b/src/log.h index b77ad46..33bc3b5 100644 --- a/src/log.h +++ b/src/log.h @@ -29,8 +29,7 @@ struct list; -typedef struct logfile -{ +typedef struct logfile { FILE *file; char *filename; char *canonical_filename; @@ -38,8 +37,7 @@ typedef struct logfile size_t len; } logfile_t; -typedef struct logstore -{ +typedef struct logstore { char *name; list_t file_group; int skip_advance; @@ -51,8 +49,7 @@ typedef struct logstore long file_offset; } logstore_t; -typedef struct log -{ +typedef struct log { hash_t logfgs; char *network; char *buffer; @@ -69,29 +66,29 @@ void logdata_free(log_t *logdata); void log_join(log_t *logdata, const char *ircmask, const char *channel); void log_part(log_t *logdata, const char *ircmask, const char *channel, - const char *message); + const char *message); void log_kick(log_t *logdata, const char *ircmask, const char *channel, - const char *who, const char *message); + const char *who, const char *message); void log_quit(log_t *logdata, const char *ircmask, const char *channel, - const char *message); + const char *message); void log_nick(log_t *logdata, const char *ircmask, const char *channel, - const char *newnick); + const char *newnick); void log_privmsg(log_t *logdata, const char *ircmask, const char *destination, - const char *message); + const char *message); void log_notice(log_t *logdata, const char *ircmask, const char *channel, const char *message); void log_cli_privmsg(log_t *logdata, const char *ircmask, - const char *destination, const char *message); + const char *destination, const char *message); void log_cli_notice(log_t *logdata, const char *ircmask, const char *channel, - const char *message); + const char *message); void log_write(log_t *logdata, const char *str, const char *destination); void log_mode(log_t *logdata, const char *ircmask, const char *channel, - const char *modes, array_t *mode_args); + const char *modes, array_t *mode_args); void log_topic(log_t *logdata, const char *ircmask, const char *channel, - const char *message); + const char *message); void log_init_topic(log_t *logdata, const char *channel, const char *message); void log_init_topic_time(log_t *logdata, const char *channel, const char *who, - const char *when); + const char *when); void log_connected(log_t *logdata); void log_disconnected(log_t *logdata); void log_ping_timeout(log_t *logdata); @@ -108,5 +105,5 @@ void log_drop(log_t *log, const char *storename); list_t *log_backlogs(log_t *log); list_t *backlog_lines(log_t *log, const char *bl, const char *cli_nick, - int hours); + int hours); #endif diff --git a/src/md5.c b/src/md5.c index 03d0985..8048e48 100644 --- a/src/md5.c +++ b/src/md5.c @@ -25,226 +25,218 @@ #include "md5.h" #include "util.h" -#define GET_UINT32(n,b,i) \ -{ \ - (n) = ( (uint32) (b)[(i) ] ) \ - | ( (uint32) (b)[(i) + 1] << 8 ) \ - | ( (uint32) (b)[(i) + 2] << 16 ) \ - | ( (uint32) (b)[(i) + 3] << 24 ); \ -} +#define GET_UINT32(n, b, i) \ + { \ + (n) = ((uint32)(b)[(i)]) | ((uint32)(b)[(i) + 1] << 8) \ + | ((uint32)(b)[(i) + 2] << 16) \ + | ((uint32)(b)[(i) + 3] << 24); \ + } -#define PUT_UINT32(n,b,i) \ -{ \ - (b)[(i) ] = (uint8) ( (n) ); \ - (b)[(i) + 1] = (uint8) ( (n) >> 8 ); \ - (b)[(i) + 2] = (uint8) ( (n) >> 16 ); \ - (b)[(i) + 3] = (uint8) ( (n) >> 24 ); \ -} +#define PUT_UINT32(n, b, i) \ + { \ + (b)[(i)] = (uint8)((n)); \ + (b)[(i) + 1] = (uint8)((n) >> 8); \ + (b)[(i) + 2] = (uint8)((n) >> 16); \ + (b)[(i) + 3] = (uint8)((n) >> 24); \ + } -void md5_starts( md5_context *ctx ) +void md5_starts(md5_context *ctx) { - ctx->total[0] = 0; - ctx->total[1] = 0; + ctx->total[0] = 0; + ctx->total[1] = 0; - ctx->state[0] = 0x67452301; - ctx->state[1] = 0xEFCDAB89; - ctx->state[2] = 0x98BADCFE; - ctx->state[3] = 0x10325476; + ctx->state[0] = 0x67452301; + ctx->state[1] = 0xEFCDAB89; + ctx->state[2] = 0x98BADCFE; + ctx->state[3] = 0x10325476; } -void md5_process( md5_context *ctx, uint8 data[64] ) +void md5_process(md5_context *ctx, uint8 data[64]) { - uint32 X[16], A, B, C, D; + uint32 X[16], A, B, C, D; - GET_UINT32( X[0], data, 0 ); - GET_UINT32( X[1], data, 4 ); - GET_UINT32( X[2], data, 8 ); - GET_UINT32( X[3], data, 12 ); - GET_UINT32( X[4], data, 16 ); - GET_UINT32( X[5], data, 20 ); - GET_UINT32( X[6], data, 24 ); - GET_UINT32( X[7], data, 28 ); - GET_UINT32( X[8], data, 32 ); - GET_UINT32( X[9], data, 36 ); - GET_UINT32( X[10], data, 40 ); - GET_UINT32( X[11], data, 44 ); - GET_UINT32( X[12], data, 48 ); - GET_UINT32( X[13], data, 52 ); - GET_UINT32( X[14], data, 56 ); - GET_UINT32( X[15], data, 60 ); + GET_UINT32(X[0], data, 0); + GET_UINT32(X[1], data, 4); + GET_UINT32(X[2], data, 8); + GET_UINT32(X[3], data, 12); + GET_UINT32(X[4], data, 16); + GET_UINT32(X[5], data, 20); + GET_UINT32(X[6], data, 24); + GET_UINT32(X[7], data, 28); + GET_UINT32(X[8], data, 32); + GET_UINT32(X[9], data, 36); + GET_UINT32(X[10], data, 40); + GET_UINT32(X[11], data, 44); + GET_UINT32(X[12], data, 48); + GET_UINT32(X[13], data, 52); + GET_UINT32(X[14], data, 56); + GET_UINT32(X[15], data, 60); -#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) +#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) -#define P(a,b,c,d,k,s,t) \ -{ \ - a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \ -} +#define P(a, b, c, d, k, s, t) \ + { \ + a += F(b, c, d) + X[k] + t; \ + a = S(a, s) + b; \ + } - A = ctx->state[0]; - B = ctx->state[1]; - C = ctx->state[2]; - D = ctx->state[3]; + A = ctx->state[0]; + B = ctx->state[1]; + C = ctx->state[2]; + D = ctx->state[3]; -#define F(x,y,z) (z ^ (x & (y ^ z))) +#define F(x, y, z) (z ^ (x & (y ^ z))) - P( A, B, C, D, 0, 7, 0xD76AA478 ); - P( D, A, B, C, 1, 12, 0xE8C7B756 ); - P( C, D, A, B, 2, 17, 0x242070DB ); - P( B, C, D, A, 3, 22, 0xC1BDCEEE ); - P( A, B, C, D, 4, 7, 0xF57C0FAF ); - P( D, A, B, C, 5, 12, 0x4787C62A ); - P( C, D, A, B, 6, 17, 0xA8304613 ); - P( B, C, D, A, 7, 22, 0xFD469501 ); - P( A, B, C, D, 8, 7, 0x698098D8 ); - P( D, A, B, C, 9, 12, 0x8B44F7AF ); - P( C, D, A, B, 10, 17, 0xFFFF5BB1 ); - P( B, C, D, A, 11, 22, 0x895CD7BE ); - P( A, B, C, D, 12, 7, 0x6B901122 ); - P( D, A, B, C, 13, 12, 0xFD987193 ); - P( C, D, A, B, 14, 17, 0xA679438E ); - P( B, C, D, A, 15, 22, 0x49B40821 ); + P(A, B, C, D, 0, 7, 0xD76AA478); + P(D, A, B, C, 1, 12, 0xE8C7B756); + P(C, D, A, B, 2, 17, 0x242070DB); + P(B, C, D, A, 3, 22, 0xC1BDCEEE); + P(A, B, C, D, 4, 7, 0xF57C0FAF); + P(D, A, B, C, 5, 12, 0x4787C62A); + P(C, D, A, B, 6, 17, 0xA8304613); + P(B, C, D, A, 7, 22, 0xFD469501); + P(A, B, C, D, 8, 7, 0x698098D8); + P(D, A, B, C, 9, 12, 0x8B44F7AF); + P(C, D, A, B, 10, 17, 0xFFFF5BB1); + P(B, C, D, A, 11, 22, 0x895CD7BE); + P(A, B, C, D, 12, 7, 0x6B901122); + P(D, A, B, C, 13, 12, 0xFD987193); + P(C, D, A, B, 14, 17, 0xA679438E); + P(B, C, D, A, 15, 22, 0x49B40821); #undef F -#define F(x,y,z) (y ^ (z & (x ^ y))) +#define F(x, y, z) (y ^ (z & (x ^ y))) - P( A, B, C, D, 1, 5, 0xF61E2562 ); - P( D, A, B, C, 6, 9, 0xC040B340 ); - P( C, D, A, B, 11, 14, 0x265E5A51 ); - P( B, C, D, A, 0, 20, 0xE9B6C7AA ); - P( A, B, C, D, 5, 5, 0xD62F105D ); - P( D, A, B, C, 10, 9, 0x02441453 ); - P( C, D, A, B, 15, 14, 0xD8A1E681 ); - P( B, C, D, A, 4, 20, 0xE7D3FBC8 ); - P( A, B, C, D, 9, 5, 0x21E1CDE6 ); - P( D, A, B, C, 14, 9, 0xC33707D6 ); - P( C, D, A, B, 3, 14, 0xF4D50D87 ); - P( B, C, D, A, 8, 20, 0x455A14ED ); - P( A, B, C, D, 13, 5, 0xA9E3E905 ); - P( D, A, B, C, 2, 9, 0xFCEFA3F8 ); - P( C, D, A, B, 7, 14, 0x676F02D9 ); - P( B, C, D, A, 12, 20, 0x8D2A4C8A ); - -#undef F - -#define F(x,y,z) (x ^ y ^ z) - - P( A, B, C, D, 5, 4, 0xFFFA3942 ); - P( D, A, B, C, 8, 11, 0x8771F681 ); - P( C, D, A, B, 11, 16, 0x6D9D6122 ); - P( B, C, D, A, 14, 23, 0xFDE5380C ); - P( A, B, C, D, 1, 4, 0xA4BEEA44 ); - P( D, A, B, C, 4, 11, 0x4BDECFA9 ); - P( C, D, A, B, 7, 16, 0xF6BB4B60 ); - P( B, C, D, A, 10, 23, 0xBEBFBC70 ); - P( A, B, C, D, 13, 4, 0x289B7EC6 ); - P( D, A, B, C, 0, 11, 0xEAA127FA ); - P( C, D, A, B, 3, 16, 0xD4EF3085 ); - P( B, C, D, A, 6, 23, 0x04881D05 ); - P( A, B, C, D, 9, 4, 0xD9D4D039 ); - P( D, A, B, C, 12, 11, 0xE6DB99E5 ); - P( C, D, A, B, 15, 16, 0x1FA27CF8 ); - P( B, C, D, A, 2, 23, 0xC4AC5665 ); + P(A, B, C, D, 1, 5, 0xF61E2562); + P(D, A, B, C, 6, 9, 0xC040B340); + P(C, D, A, B, 11, 14, 0x265E5A51); + P(B, C, D, A, 0, 20, 0xE9B6C7AA); + P(A, B, C, D, 5, 5, 0xD62F105D); + P(D, A, B, C, 10, 9, 0x02441453); + P(C, D, A, B, 15, 14, 0xD8A1E681); + P(B, C, D, A, 4, 20, 0xE7D3FBC8); + P(A, B, C, D, 9, 5, 0x21E1CDE6); + P(D, A, B, C, 14, 9, 0xC33707D6); + P(C, D, A, B, 3, 14, 0xF4D50D87); + P(B, C, D, A, 8, 20, 0x455A14ED); + P(A, B, C, D, 13, 5, 0xA9E3E905); + P(D, A, B, C, 2, 9, 0xFCEFA3F8); + P(C, D, A, B, 7, 14, 0x676F02D9); + P(B, C, D, A, 12, 20, 0x8D2A4C8A); #undef F -#define F(x,y,z) (y ^ (x | ~z)) +#define F(x, y, z) (x ^ y ^ z) - P( A, B, C, D, 0, 6, 0xF4292244 ); - P( D, A, B, C, 7, 10, 0x432AFF97 ); - P( C, D, A, B, 14, 15, 0xAB9423A7 ); - P( B, C, D, A, 5, 21, 0xFC93A039 ); - P( A, B, C, D, 12, 6, 0x655B59C3 ); - P( D, A, B, C, 3, 10, 0x8F0CCC92 ); - P( C, D, A, B, 10, 15, 0xFFEFF47D ); - P( B, C, D, A, 1, 21, 0x85845DD1 ); - P( A, B, C, D, 8, 6, 0x6FA87E4F ); - P( D, A, B, C, 15, 10, 0xFE2CE6E0 ); - P( C, D, A, B, 6, 15, 0xA3014314 ); - P( B, C, D, A, 13, 21, 0x4E0811A1 ); - P( A, B, C, D, 4, 6, 0xF7537E82 ); - P( D, A, B, C, 11, 10, 0xBD3AF235 ); - P( C, D, A, B, 2, 15, 0x2AD7D2BB ); - P( B, C, D, A, 9, 21, 0xEB86D391 ); + P(A, B, C, D, 5, 4, 0xFFFA3942); + P(D, A, B, C, 8, 11, 0x8771F681); + P(C, D, A, B, 11, 16, 0x6D9D6122); + P(B, C, D, A, 14, 23, 0xFDE5380C); + P(A, B, C, D, 1, 4, 0xA4BEEA44); + P(D, A, B, C, 4, 11, 0x4BDECFA9); + P(C, D, A, B, 7, 16, 0xF6BB4B60); + P(B, C, D, A, 10, 23, 0xBEBFBC70); + P(A, B, C, D, 13, 4, 0x289B7EC6); + P(D, A, B, C, 0, 11, 0xEAA127FA); + P(C, D, A, B, 3, 16, 0xD4EF3085); + P(B, C, D, A, 6, 23, 0x04881D05); + P(A, B, C, D, 9, 4, 0xD9D4D039); + P(D, A, B, C, 12, 11, 0xE6DB99E5); + P(C, D, A, B, 15, 16, 0x1FA27CF8); + P(B, C, D, A, 2, 23, 0xC4AC5665); #undef F - ctx->state[0] += A; - ctx->state[1] += B; - ctx->state[2] += C; - ctx->state[3] += D; +#define F(x, y, z) (y ^ (x | ~z)) + + P(A, B, C, D, 0, 6, 0xF4292244); + P(D, A, B, C, 7, 10, 0x432AFF97); + P(C, D, A, B, 14, 15, 0xAB9423A7); + P(B, C, D, A, 5, 21, 0xFC93A039); + P(A, B, C, D, 12, 6, 0x655B59C3); + P(D, A, B, C, 3, 10, 0x8F0CCC92); + P(C, D, A, B, 10, 15, 0xFFEFF47D); + P(B, C, D, A, 1, 21, 0x85845DD1); + P(A, B, C, D, 8, 6, 0x6FA87E4F); + P(D, A, B, C, 15, 10, 0xFE2CE6E0); + P(C, D, A, B, 6, 15, 0xA3014314); + P(B, C, D, A, 13, 21, 0x4E0811A1); + P(A, B, C, D, 4, 6, 0xF7537E82); + P(D, A, B, C, 11, 10, 0xBD3AF235); + P(C, D, A, B, 2, 15, 0x2AD7D2BB); + P(B, C, D, A, 9, 21, 0xEB86D391); + +#undef F + + ctx->state[0] += A; + ctx->state[1] += B; + ctx->state[2] += C; + ctx->state[3] += D; } -void md5_update( md5_context *ctx, uint8 *input, uint32 length ) +void md5_update(md5_context *ctx, uint8 *input, uint32 length) { - uint32 left, fill; + uint32 left, fill; - if( ! length ) return; + if (!length) + return; - left = ctx->total[0] & 0x3F; - fill = 64 - left; + left = ctx->total[0] & 0x3F; + fill = 64 - left; - ctx->total[0] += length; - ctx->total[0] &= 0xFFFFFFFF; + ctx->total[0] += length; + ctx->total[0] &= 0xFFFFFFFF; - if( ctx->total[0] < length ) - ctx->total[1]++; + if (ctx->total[0] < length) + ctx->total[1]++; - if( left && length >= fill ) - { - memcpy( (void *) (ctx->buffer + left), - (void *) input, fill ); - md5_process( ctx, ctx->buffer ); - length -= fill; - input += fill; - left = 0; - } + if (left && length >= fill) { + memcpy((void *)(ctx->buffer + left), (void *)input, fill); + md5_process(ctx, ctx->buffer); + length -= fill; + input += fill; + left = 0; + } - while( length >= 64 ) - { - md5_process( ctx, input ); - length -= 64; - input += 64; - } + while (length >= 64) { + md5_process(ctx, input); + length -= 64; + input += 64; + } - if( length ) - { - memcpy( (void *) (ctx->buffer + left), - (void *) input, length ); - } + if (length) { + memcpy((void *)(ctx->buffer + left), (void *)input, length); + } } -static uint8 md5_padding[64] = +static uint8 md5_padding[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +void md5_finish(md5_context *ctx, uint8 digest[16]) { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; + uint32 last, padn; + uint32 high, low; + uint8 msglen[8]; -void md5_finish( md5_context *ctx, uint8 digest[16] ) -{ - uint32 last, padn; - uint32 high, low; - uint8 msglen[8]; + high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); + low = (ctx->total[0] << 3); - high = ( ctx->total[0] >> 29 ) - | ( ctx->total[1] << 3 ); - low = ( ctx->total[0] << 3 ); + PUT_UINT32(low, msglen, 0); + PUT_UINT32(high, msglen, 4); - PUT_UINT32( low, msglen, 0 ); - PUT_UINT32( high, msglen, 4 ); + last = ctx->total[0] & 0x3F; + padn = (last < 56) ? (56 - last) : (120 - last); - last = ctx->total[0] & 0x3F; - padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); + md5_update(ctx, md5_padding, padn); + md5_update(ctx, msglen, (unsigned long)8); - md5_update( ctx, md5_padding, padn ); - md5_update( ctx, msglen, (unsigned long)8 ); - - PUT_UINT32( ctx->state[0], digest, 0 ); - PUT_UINT32( ctx->state[1], digest, 4 ); - PUT_UINT32( ctx->state[2], digest, 8 ); - PUT_UINT32( ctx->state[3], digest, 12 ); + PUT_UINT32(ctx->state[0], digest, 0); + PUT_UINT32(ctx->state[1], digest, 4); + PUT_UINT32(ctx->state[2], digest, 8); + PUT_UINT32(ctx->state[3], digest, 12); } #ifdef TEST @@ -256,92 +248,76 @@ void md5_finish( md5_context *ctx, uint8 digest[16] ) * those are the standard RFC 1321 test vectors */ -static char *msg[] = +static char *msg[] = { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012" + "345678901234567890"}; + +static char *val[] = { + "d41d8cd98f00b204e9800998ecf8427e", "0cc175b9c0f1b6a831c399e269772661", + "900150983cd24fb0d6963f7d28e17f72", "f96b697d7cb7938d525a2f31aaf161d0", + "c3fcd3d76192e4007dfb496cca67e13b", "d174ab98d277d9f5a5611c2c9f419d9f", + "57edf4a22be3c955ac49da2e2107b67a"}; + +int main(int argc, char *argv[]) { - "", - "a", - "abc", - "message digest", - "abcdefghijklmnopqrstuvwxyz", - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - "12345678901234567890123456789012345678901234567890123456789012" \ - "345678901234567890" -}; + FILE *f; + int i, j; + char output[33]; + md5_context ctx; + unsigned char buf[1000]; + unsigned char md5sum[16]; -static char *val[] = -{ - "d41d8cd98f00b204e9800998ecf8427e", - "0cc175b9c0f1b6a831c399e269772661", - "900150983cd24fb0d6963f7d28e17f72", - "f96b697d7cb7938d525a2f31aaf161d0", - "c3fcd3d76192e4007dfb496cca67e13b", - "d174ab98d277d9f5a5611c2c9f419d9f", - "57edf4a22be3c955ac49da2e2107b67a" -}; + if (argc < 2) { + printf("\n MD5 Validation Tests:\n\n"); -int main( int argc, char *argv[] ) -{ - FILE *f; - int i, j; - char output[33]; - md5_context ctx; - unsigned char buf[1000]; - unsigned char md5sum[16]; + for (i = 0; i < 7; i++) { + printf(" Test %d ", i + 1); - if( argc < 2 ) - { - printf( "\n MD5 Validation Tests:\n\n" ); + md5_starts(&ctx); + md5_update(&ctx, (uint8 *)msg[i], strlen(msg[i])); + md5_finish(&ctx, md5sum); - for( i = 0; i < 7; i++ ) - { - printf( " Test %d ", i + 1 ); + for (j = 0; j < 16; j++) { + sprintf(output + j * 2, "%02x", md5sum[j]); + } - md5_starts( &ctx ); - md5_update( &ctx, (uint8 *) msg[i], strlen( msg[i] ) ); - md5_finish( &ctx, md5sum ); + if (memcmp(output, val[i], 32)) { + printf("failed!\n"); + return (1); + } - for( j = 0; j < 16; j++ ) - { - sprintf( output + j * 2, "%02x", md5sum[j] ); - } + printf("passed.\n"); + } - if( memcmp( output, val[i], 32 ) ) - { - printf( "failed!\n" ); - return( 1 ); - } + printf("\n"); + } else { + if (!(f = fopen(argv[1], "rb"))) { + perror("fopen"); + return (1); + } - printf( "passed.\n" ); - } + md5_starts(&ctx); - printf( "\n" ); - } - else - { - if( ! ( f = fopen( argv[1], "rb" ) ) ) - { - perror( "fopen" ); - return( 1 ); - } + while ((i = fread(buf, 1, sizeof(buf), f)) > 0) { + md5_update(&ctx, buf, i); + } - md5_starts( &ctx ); + md5_finish(&ctx, md5sum); - while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) - { - md5_update( &ctx, buf, i ); - } + for (j = 0; j < 16; j++) { + printf("%02x", md5sum[j]); + } - md5_finish( &ctx, md5sum ); + printf(" %s\n", argv[1]); + } - for( j = 0; j < 16; j++ ) - { - printf( "%02x", md5sum[j] ); - } - - printf( " %s\n", argv[1] ); - } - - return( 0 ); + return (0); } #endif @@ -360,7 +336,8 @@ unsigned char *chash_double(char *str, unsigned int seed) length = strlen(str); length += 4; ptr = bip_malloc(length); -// conversion from ‘unsigned int’ to ‘unsigned char’ may change value [-Werror=conversion] +// conversion from ‘unsigned int’ to ‘unsigned char’ may change value +// [-Werror=conversion] #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wconversion" ptr[0] = seed >> 24 & 0xff; @@ -399,4 +376,3 @@ int chash_cmp(char *try, unsigned char *pass, unsigned int seed) free(try_hash); return 0; } - diff --git a/src/md5.h b/src/md5.h index 6ca7c8a..8be17c0 100644 --- a/src/md5.h +++ b/src/md5.h @@ -2,22 +2,19 @@ #define _MD5_H #ifndef uint8 -#define uint8 unsigned char +#define uint8 unsigned char #endif #ifndef uint32 #define uint32 unsigned long int #endif -typedef struct -{ - uint32 total[2]; - uint32 state[4]; - uint8 buffer[64]; -} -md5_context; +typedef struct { + uint32 total[2]; + uint32 state[4]; + uint8 buffer[64]; +} md5_context; -int chash_cmp(char *try, unsigned char *pass, - unsigned int seed); +int chash_cmp(char *try, unsigned char *pass, unsigned int seed); unsigned char *chash_double(char *str, unsigned int seed); #endif /* md5.h */ diff --git a/src/util.c b/src/util.c index 85e16a2..fe64741 100644 --- a/src/util.c +++ b/src/util.c @@ -107,8 +107,9 @@ char *bip_strcat_fit(size_t *remaining, char *str, const char *str2) res = memccpy(str, str2, '\0', *remaining); if (!res) { - mylog(LOG_DEBUGTOOMUCH, "bip_strcat_fit: memccpy() failed, remaining %lu", - *remaining); + mylog(LOG_DEBUGTOOMUCH, + "bip_strcat_fit: memccpy() failed, remaining %lu", + *remaining); return NULL; } @@ -135,8 +136,9 @@ char *bip_strcatf_fit(size_t *remaining, char *str, const char *fmt, ...) } if (*remaining > STRCATF_BUF_MAXLEN) { - mylog(LOG_ERROR, "bip_strcatf_fit: remaining " - "is over STRCATF_BUF_MAXLEN"); + mylog(LOG_ERROR, + "bip_strcatf_fit: remaining " + "is over STRCATF_BUF_MAXLEN"); } va_start(ap, fmt); @@ -144,19 +146,22 @@ char *bip_strcatf_fit(size_t *remaining, char *str, const char *fmt, ...) written = vsnprintf(str2, *remaining, fmt, ap); if (written < 0) { mylog(LOG_ERROR, "bip_strcatf_fit: vsnprintf failed with: %s", - strerror(errno)); + strerror(errno)); return NULL; } if ((unsigned)written >= *remaining) { - mylog(LOG_DEBUGVERB, "bip_strcatf_fit,vsnprintf: no space left"); + mylog(LOG_DEBUGVERB, + "bip_strcatf_fit,vsnprintf: no space left"); goto end; } res = memccpy(str, str2, '\0', *remaining); if (!res) { - mylog(LOG_DEBUGTOOMUCH, "bip_strcatf_fit: memccpy() failed, " - "remaining %lu", *remaining); + mylog(LOG_DEBUGTOOMUCH, + "bip_strcatf_fit: memccpy() failed, " + "remaining %lu", + *remaining); goto end; } @@ -194,10 +199,10 @@ int is_valid_nick(char *str) return 0; tmp = str; - while (*tmp != '\0' && (isalnum(*tmp) || *tmp == '-' || *tmp == '[' || - *tmp == ']' || *tmp == '\\' || *tmp == '`' || - *tmp == '^' || *tmp == '{' || *tmp == '}' || - *tmp == '|' || *tmp == '_' )) + while (*tmp != '\0' + && (isalnum(*tmp) || *tmp == '-' || *tmp == '[' || *tmp == ']' + || *tmp == '\\' || *tmp == '`' || *tmp == '^' || *tmp == '{' + || *tmp == '}' || *tmp == '|' || *tmp == '_')) tmp++; return (*tmp == '\0'); } @@ -209,8 +214,8 @@ int is_valid_username(char *str) return 0; tmp = str; - while (*tmp != '\0' && *tmp != ' ' && *tmp != '\0' && *tmp != '\r' && - *tmp != '\n') + while (*tmp != '\0' && *tmp != ' ' && *tmp != '\0' && *tmp != '\r' + && *tmp != '\n') tmp++; return (*tmp == '\0'); } @@ -266,27 +271,27 @@ void _mylog(int level, char *fmt, va_list ap) return; switch (level) { - case LOG_FATAL: - prefix = "FATAL: "; - break; - case LOG_DEBUGVERB: - prefix = "DEBUGVERB: "; - break; - case LOG_DEBUG: - prefix = "DEBUG: "; - break; - case LOG_ERROR: - prefix = "ERROR: "; - break; - case LOG_WARN: - prefix = "WARNING: "; - break; - case LOG_INFO: - prefix = ""; - break; - default: - prefix = ""; - break; + case LOG_FATAL: + prefix = "FATAL: "; + break; + case LOG_DEBUGVERB: + prefix = "DEBUGVERB: "; + break; + case LOG_DEBUG: + prefix = "DEBUG: "; + break; + case LOG_ERROR: + prefix = "ERROR: "; + break; + case LOG_WARN: + prefix = "WARNING: "; + break; + case LOG_INFO: + prefix = ""; + break; + default: + prefix = ""; + break; } fprintf(conf_global_log_file, "%s %s", timestamp(), prefix); @@ -606,13 +611,13 @@ void hash_init(hash_t *h, int options) switch (options) { case HASH_NOCASE: list_init(&h->lists[i], - (int (*)(const void*, const void*)) - hash_item_nocase_cmp); + (int (*)(const void *, + const void *))hash_item_nocase_cmp); break; case HASH_DEFAULT: list_init(&h->lists[i], - (int (*)(const void*,const void*)) - hash_item_cmp); + (int (*)(const void *, + const void *))hash_item_cmp); break; default: fatal("wrong hash option %d", options); @@ -800,7 +805,7 @@ void hash_dump(hash_t *h) { hash_iterator_t it; assert(h); - for (hash_it_init(h, &it); hash_it_key(&it) ;hash_it_next(&it)) + for (hash_it_init(h, &it); hash_it_key(&it); hash_it_next(&it)) printf("%s => %p\n", hash_it_key(&it), hash_it_item(&it)); } @@ -869,7 +874,7 @@ void array_ensure(array_t *a, int index) a->elemv = bip_realloc(a->elemv, sizeof(void *) * (size_t)(index + 1)); // a->elemc should be lower than index + 1 memset(a->elemv + a->elemc, 0, - sizeof(void *) * (size_t)(index + 1 - a->elemc)); + sizeof(void *) * (size_t)(index + 1 - a->elemc)); a->elemc = index + 1; } diff --git a/src/util.h b/src/util.h index 3644ef0..8db79f4 100644 --- a/src/util.h +++ b/src/util.h @@ -74,37 +74,42 @@ typedef struct array { const void **elemv; } array_t; -#define MOVE_STRING(dest, src) do {\ - if (dest)\ - free(dest);\ - (dest) = (src);\ - (src) = NULL;\ -} while(0) +#define MOVE_STRING(dest, src) \ + do { \ + if (dest) \ + free(dest); \ + (dest) = (src); \ + (src) = NULL; \ + } while (0) -#define FREE(a) free(a); (a) = NULL; +#define FREE(a) \ + free(a); \ + (a) = NULL; -#define MAYFREE(a) do { \ - if (a) { \ - free(a); \ - (a) = NULL; \ - } \ - } while(0); +#define MAYFREE(a) \ + do { \ + if (a) { \ + free(a); \ + (a) = NULL; \ + } \ + } while (0); -#define assert(condition) \ - do { \ - if (!(condition)) \ - fatal("Failed assertion in " __FILE__ "(%d): " \ - #condition, __LINE__); \ - } while(0) +#define assert(condition) \ + do { \ + if (!(condition)) \ + fatal("Failed assertion in " __FILE__ \ + "(%d): " #condition, \ + __LINE__); \ + } while (0) -#define assert_msg(condition, msg) \ - do { \ - if (!(condition)) \ - fatal("%s in " __FILE__ "(%d): " \ - #condition, (msg), __LINE__); \ - } while(0) +#define assert_msg(condition, msg) \ + do { \ + if (!(condition)) \ + fatal("%s in " __FILE__ "(%d): " #condition, (msg), \ + __LINE__); \ + } while (0) -void list_init(list_t *list, int (*cmp)(const void*, const void*)); +void list_init(list_t *list, int (*cmp)(const void *, const void *)); int list_ptr_cmp(const void *a, const void *b); list_t *list_new(int (*cmp)(const void *, const void *)); void *list_remove(list_t *list, const void *ptr); @@ -183,9 +188,10 @@ char *bip_strdup(const char *str); char *bip_strcat_fit(size_t *remaining, char *str, const char *str2); char *bip_strcatf_fit(size_t *remaining, char *str, const char *str2, ...); void bip_clock_gettime(clockid_t clockid, struct timespec *tp); -#define array_each(a, idx, ptr) for ((idx) = 0; \ - (idx) < (a)->elemc && (((ptr) = bip_strdup(array_get((a), (idx)))) || 1); \ - (idx)++) +#define array_each(a, idx, ptr) \ + for ((idx) = 0; (idx) < (a)->elemc \ + && (((ptr) = bip_strdup(array_get((a), (idx)))) || 1); \ + (idx)++) void array_init(array_t *a); array_t *array_new(void); diff --git a/src/utils/base64.c b/src/utils/base64.c index 57488ba..f901294 100644 --- a/src/utils/base64.c +++ b/src/utils/base64.c @@ -29,8 +29,8 @@ static const unsigned char base64_table[65] = * * BIP change: remove line returns. */ -unsigned char * base64_encode(const unsigned char *src, size_t len, - size_t *out_len) +unsigned char *base64_encode(const unsigned char *src, size_t len, + size_t *out_len) { unsigned char *out, *pos; const unsigned char *end, *in; @@ -38,8 +38,8 @@ unsigned char * base64_encode(const unsigned char *src, size_t len, int line_len; olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ - olen += olen / 72; /* line feeds */ - olen++; /* nul termination */ + olen += olen / 72; /* line feeds */ + olen++; /* nul termination */ if (olen < len) return NULL; /* integer overflow */ out = malloc(olen); @@ -57,7 +57,7 @@ unsigned char * base64_encode(const unsigned char *src, size_t len, *pos++ = base64_table[in[2] & 0x3f]; in += 3; line_len += 4; - /* + /* * BIP change: remove line returns. if (line_len >= 72) { *pos++ = '\n'; @@ -72,15 +72,15 @@ unsigned char * base64_encode(const unsigned char *src, size_t len, *pos++ = base64_table[(in[0] & 0x03) << 4]; *pos++ = '='; } else { - *pos++ = base64_table[((in[0] & 0x03) << 4) | - (in[1] >> 4)]; + *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'; @@ -91,4 +91,3 @@ unsigned char * base64_encode(const unsigned char *src, size_t len, *out_len = (size_t)(pos - out); return out; } - diff --git a/src/utils/base64.h b/src/utils/base64.h index 666573f..fd2592f 100644 --- a/src/utils/base64.h +++ b/src/utils/base64.h @@ -10,7 +10,7 @@ #ifndef BASE64_H #define BASE64_H -unsigned char * base64_encode(const unsigned char *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 */