From 2396d8555cb0aa80440274a5617c0ea6a293959b Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Wed, 30 Nov 2011 12:19:37 +0100 Subject: [PATCH] Fix bug #167 - Build errors on OpenBSD. - Don't link with -ldl, as it is not needed in most cases - Don't compile plugins if USE_PLUGINS=NO - Fix warning about missing newline at end of getopt.h - Removed the O_NOATIME open() flag from the logging plugin. - Removed the O_LARGEFILE open() flag. _FILE_OFFSET_BITS is 64. - Use fsync() if fdatasync() is not available for log file writing. - Replaced some sprintf() with snprintf() due to compiler warnings (though, they were length limited otherwise). - Replaced two occurences of strcpy() with memcpy(). --- GNUmakefile | 8 ++++---- src/core/commands.c | 24 ++++++++++++------------ src/core/hub.c | 10 ++++++---- src/plugins/mod_logging.c | 8 +++++++- src/util/getopt.h | 3 ++- src/util/misc.c | 2 +- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 4ac1d5d..46ce534 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -11,6 +11,7 @@ MV := mv RANLIB := ranlib CFLAGS += -pipe -Wall USE_SSL ?= YES +USE_PLUGINS ?= YES USE_BIGENDIAN ?= AUTO BITS ?= AUTO SILENT ?= YES @@ -119,8 +120,6 @@ CFLAGS += -DSSL_SUPPORT LDLIBS += -lssl -lcrypto endif -LDLIBS += -ldl - GIT_VERSION=$(shell git describe --tags 2>/dev/null || echo "") GIT_REVISION=$(shell git show --abbrev-commit 2>/dev/null | head -n 1 | cut -f 2 -d " " || echo "") OLD_REVISION=$(shell grep GIT_REVISION revision.h 2>/dev/null | cut -f 3 -d " " | tr -d "\"") @@ -221,7 +220,7 @@ adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o) admin_OBJECTS := $(admin_SOURCES:.c=.o) all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(libutils_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS) $(admin_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS) -all_plugins := $(plugin_example_TARGET) $(plugin_logging_TARGET) $(plugin_auth_TARGET) $(plugin_auth_sqlite_TARGET) +all_plugins := uhub_BINARY=uhub$(BIN_EXT) adcrush_BINARY=adcrush$(BIN_EXT) @@ -229,7 +228,8 @@ admin_BINARY=uhub-admin$(BIN_EXT) autotest_BINARY=autotest/test$(BIN_EXT) ifeq ($(USE_PLUGINS),YES) -all_OBJECTS += $(plugins) +all_plugins := $(plugin_example_TARGET) $(plugin_logging_TARGET) $(plugin_auth_TARGET) $(plugin_auth_sqlite_TARGET) +all_OBJECTS += $(all_plugins) endif .PHONY: revision.h.tmp all plugins diff --git a/src/core/commands.c b/src/core/commands.c index 678cb45..62b0f20 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -328,7 +328,7 @@ static int command_help(struct hub_info* hub, struct hub_user* user, struct hub_ if (!found) { - sprintf(msg, "Command \"%s\" not found.\n", command); + snprintf(msg, sizeof(msg), "Command \"%s\" not found.\n", command); } } return command_status(hub, user, cmd, msg); @@ -676,11 +676,11 @@ static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_c if (search_len) { - sprintf(tmp, "Logged entries: " PRINTF_SIZE_T ", searching for \"%s\"", list_size(messages), search); + snprintf(tmp, sizeof(tmp), "Logged entries: " PRINTF_SIZE_T ", searching for \"%s\"", list_size(messages), search); } else { - sprintf(tmp, "Logged entries: " PRINTF_SIZE_T, list_size(messages)); + snprintf(tmp, sizeof(tmp), "Logged entries: " PRINTF_SIZE_T, list_size(messages)); } command_status(hub, user, cmd, tmp); @@ -705,7 +705,7 @@ static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_c if (show) { - sprintf(tmp, "* %s %s, %s [%s] - %s", get_timestamp(log->time), log->cid, log->nick, ip_convert_to_string(&log->addr), user_get_quit_reason_string(log->reason)); + snprintf(tmp, sizeof(tmp), "* %s %s, %s [%s] - %s", get_timestamp(log->time), log->cid, log->nick, ip_convert_to_string(&log->addr), user_get_quit_reason_string(log->reason)); send_message(hub, user, tmp); } log = (struct hub_logout_info*) list_get_next(messages); @@ -713,7 +713,7 @@ static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_c if (search_len) { - sprintf(tmp, PRINTF_SIZE_T " entries shown.", search_hits); + snprintf(tmp, sizeof(tmp), PRINTF_SIZE_T " entries shown.", search_hits); command_status(hub, user, cmd, tmp); } @@ -736,12 +736,12 @@ static int command_register(struct hub_info* hub, struct hub_user* user, struct { if (acl_register_user(hub, &data)) { - sprintf(tmp, "User \"%s\" registered.", user->id.nick); + snprintf(tmp, sizeof(tmp), "User \"%s\" registered.", user->id.nick); return command_status(hub, user, cmd, tmp); } else { - sprintf(tmp, "Unable to register user \"%s\".", user->id.nick); + snprintf(tmp, sizeof(tmp), "Unable to register user \"%s\".", user->id.nick); return command_status(hub, user, cmd, tmp); } } @@ -769,7 +769,7 @@ static int command_password(struct hub_info* hub, struct hub_user* user, struct } else { - sprintf(tmp, "Unable to change password for user \"%s\".", user->id.nick); + snprintf(tmp, sizeof(tmp), "Unable to change password for user \"%s\".", user->id.nick); return command_status(hub, user, cmd, tmp); } } @@ -796,12 +796,12 @@ static int command_useradd(struct hub_info* hub, struct hub_user* user, struct h if (acl_register_user(hub, &data)) { - sprintf(tmp, "User \"%s\" registered.", nick); + snprintf(tmp, sizeof(tmp), "User \"%s\" registered.", nick); return command_status(hub, user, cmd, tmp); } else { - sprintf(tmp, "Unable to register user \"%s\".", nick); + snprintf(tmp, sizeof(tmp), "Unable to register user \"%s\".", nick); return command_status(hub, user, cmd, tmp); } } @@ -813,12 +813,12 @@ static int command_userdel(struct hub_info* hub, struct hub_user* user, struct h if (acl_delete_user(hub, nick)) { - sprintf(tmp, "User \"%s\" is deleted.", nick); + snprintf(tmp, sizeof(tmp), "User \"%s\" is deleted.", nick); return command_status(hub, user, cmd, tmp); } else { - sprintf(tmp, "Unable to delete user \"%s\".", nick); + snprintf(tmp, sizeof(tmp), "Unable to delete user \"%s\".", nick); return command_status(hub, user, cmd, tmp); } } diff --git a/src/core/hub.c b/src/core/hub.c index 0d34319..839f063 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -337,8 +337,10 @@ void hub_chat_history_add(struct hub_info* hub, struct hub_user* user, struct ad { char* msg_esc = adc_msg_get_argument(cmd, 0); char* message = adc_msg_unescape(msg_esc); - char* log = hub_malloc(strlen(message) + strlen(user->id.nick) + 14); - sprintf(log, "%s <%s> %s\n", get_timestamp(time(NULL)), user->id.nick, message); + size_t loglen = strlen(message) + strlen(user->id.nick) + 13; + char* log = hub_malloc(loglen + 1); + snprintf(log, loglen, "%s <%s> %s\n", get_timestamp(time(NULL)), user->id.nick, message); + log[loglen] = '\0'; list_append(hub->chat_history, log); while (list_size(hub->chat_history) > (size_t) hub->config->max_chat_history) { @@ -1331,8 +1333,8 @@ void hub_logout_log(struct hub_info* hub, struct hub_user* user) struct hub_logout_info* loginfo = hub_malloc_zero(sizeof(struct hub_logout_info)); if (!loginfo) return; loginfo->time = time(NULL); - strcpy(loginfo->cid, user->id.cid); - strcpy(loginfo->nick, user->id.nick); + memcpy(loginfo->cid, user->id.cid, sizeof(loginfo->cid)); + memcpy(loginfo->nick, user->id.nick, sizeof(loginfo->nick)); memcpy(&loginfo->addr, &user->id.addr, sizeof(struct ip_addr_encap)); loginfo->reason = user->quit_reason; diff --git a/src/plugins/mod_logging.c b/src/plugins/mod_logging.c index 2f0c1fd..2147c81 100644 --- a/src/plugins/mod_logging.c +++ b/src/plugins/mod_logging.c @@ -40,7 +40,8 @@ static void set_error_message(struct plugin_handle* plugin, const char* msg) static int log_open_file(struct plugin_handle* plugin, struct log_data* data) { - data->fd = open(data->logfile, O_CREAT | O_APPEND | O_NOATIME | O_LARGEFILE | O_WRONLY, 0664); + int flags = O_CREAT | O_APPEND | O_WRONLY; + data->fd = open(data->logfile, flags, 0664); return (data->fd != -1); } @@ -163,7 +164,12 @@ static void log_message(struct log_data* data, const char *format, ...) va_end(args); write(data->fd, logmsg, size + 20); + +#ifdef _POSIX_SYNCHRONIZED_IO fdatasync(data->fd); +#else + fsync(data->fd); +#endif } else { diff --git a/src/util/getopt.h b/src/util/getopt.h index 21c9304..37ca71d 100644 --- a/src/util/getopt.h +++ b/src/util/getopt.h @@ -26,4 +26,5 @@ extern int optind; extern int getopt(int argc, char* const argv[], const char *optstring); -#endif \ No newline at end of file +#endif /* NEED_GETOPT */ + diff --git a/src/util/misc.c b/src/util/misc.c index f21f7f8..81fc606 100644 --- a/src/util/misc.c +++ b/src/util/misc.c @@ -399,7 +399,7 @@ const char* get_timestamp(time_t now) { static char ts[32] = {0, }; struct tm* t = localtime(&now); - sprintf(ts, "[%02d:%02d]", t->tm_hour, t->tm_min); + snprintf(ts, sizeof(ts), "[%02d:%02d]", t->tm_hour, t->tm_min); return ts; }