From: Martin Johansson Date: Wed, 12 Mar 2014 20:17:31 +0000 (+0100) Subject: Merge pull request #34 from fmorgner/master X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=a7ffadd41a1457e24ffde19a081c4b8bffe1b3b8;hp=673cad207cccf829038ab1a4399ca302bb0cf078;p=umurmur.git Merge pull request #34 from fmorgner/master CMake with Autotools (again) --- diff --git a/.gitignore b/.gitignore index 688938e..a3f4d87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,17 @@ +build +config.h + +# Autotools noise +Makefile.in +aclocal.m4 +autom4te.cache/ +config.guess +config.h.in +config.h.in~ +config.sub configure depcomp install-sh missing -stamp-h1 -Makefile.in -aclocal.m4 -config.h.in src/Makefile.in + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6e81c3c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 2.8) + +project(umurmurd C) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") +set(CMAKE_INSTALL_RPATH_USE_LINK_PATH true) + +option(USE_POLARSSL_TESTCERT "Link to the PolarSSL test certificate and key." OFF) +option(USE_POLARSSL_HAVEGE "Use the PolarSSL HAVEGE random generator key." OFF) + +if(USE_POLARSSL_TESTCERT OR USE_POLARSSL_HAVEGE) + if(SSL MATCHES "openssl") + message(FATAL_ERROR "Selecting USE_POLARSSL_TESTCERT or USE_POLARSSL_HAVEGE implies SSL=polarssl") + endif(SSL MATCHES "openssl") +endif(USE_POLARSSL_TESTCERT OR USE_POLARSSL_HAVEGE) + +find_package(Libconfig REQUIRED) +find_package(ProtobufC REQUIRED) +include(CheckFunctionExists) +include(CheckLibraryExists) + +if(SSL MATCHES "openssl") + find_package(OpenSSL REQUIRED) + if(OPENSSL_FOUND) + set(SSLIMP_LIBRARIES ${OPENSSL_LIBRARIES}) + set(SSLIMP_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) + set(SSLIMP_LIBRARY_DIR ${OPENSSL_LIB_DIR}) + endif(OPENSSL_FOUND) +else(SSL MATCHES "openssl") + find_package(PolarSSL REQUIRED) + if(POLARSSL_FOUND) + set(USE_POLARSSL ON) + set(SSLIMP_LIBRARIES ${POLARSSL_LIBRARIES}) + set(SSLIMP_INCLUDE_DIR ${POLARSSL_INCLUDE_DIR}) + set(SSLIMP_LIBRARY_DIR ${POLARSSL_LIB_DIR}) + endif(POLARSSL_FOUND) +endif(SSL MATCHES "openssl") + +check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME_RT) + +if(NOT HAVE_CLOCK_GETTIME_RT) + check_function_exists(clock_gettime HAVE_CLOCK_GETTIME) + if(NOT HAVE_CLOCK_GETTIME) + check_library_exists(c clock_get_time "mach/time.h" HAVE_CLOCK_GET_TIME) + if(NOT HAVE_CLOCK_GET_TIME) + message(FATAL_ERROR "Didn't find clock_gettime() or clock_get_time!") + endif() + endif() +else() + set(LIBRT rt) +endif() + +set(SOURCE "") +add_subdirectory(src) + +configure_file(src/config.h.in ${CMAKE_SOURCE_DIR}/src/config.h) + +include_directories(${LIBCONFIG_INCLUDE_DIR} ${PROTOBUFC_INCLUDE_DIR} ${SSLIMP_INCLUDE_DIR}) +link_directories(${LIBCONFIG_LIB_DIR} ${PROTOBUFC_LIB_DIR} ${SSLIMP_LIB_DIR}) + +add_executable(umurmurd ${SOURCE}) +install(TARGETS umurmurd RUNTIME DESTINATION "bin") + +find_path(OLD_CONFIG_FILE NAMES "umurmur.conf" PATHS ${CMAKE_INSTALL_PREFIX} PATH_SUFFIXES "etc") +if(NOT OLD_CONFIG_FILE) + install(FILES "umurmur.conf.example" DESTINATION "etc" RENAME "umurmur.conf") +endif(NOT OLD_CONFIG_FILE) + +add_definitions(${SSLIMP_CFLAGS}) +target_link_libraries(umurmurd ${LIBCONFIG_LIBRARIES} ${PROTOBUFC_LIBRARIES} ${SSLIMP_LIBRARIES} ${LIBRT}) diff --git a/README.md b/README.md index 6ca64e3..e451571 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,16 @@ Instructions for building from source * [OpenSSL](http://www.openssl.org/) or [PolarSSL](http://polarssl.org/) library. For PolarSSL version 1.0.0 and above is required. * [libconfig](http://www.hyperrealm.com/libconfig/) * [libprotoc-c](http://code.google.com/p/protobuf-c/) version 0.14 or 0.15 (use --disable-protoc option in its ./configure to build only the library). If you for some reason have to run an earlier version you need to recompile the protocol file `Mumble.proto` using the protobuf compiler for the corresponding version. + * [CMake](http://cmake.org) 2. Build - * `./autogen.sh` - Required only if building from a cloned Git tree. - * `./configure` - use `./configure --help` for switches. Defaults to build using PolarSSL, no test certificate, `/dev/urandom` as random source. + * Create a build folder and cd into it + * `cmake ../` to use default settings (polarssl, no test-cert and /dev/urandom as source for randomness). * `make` 3. Install * `make install` - * Edit umurmur.conf.example to your liking and put it in a suitable place. /etc/umurmur.conf is default. + * Edit the umurmur.conf found in the 'etc' folder in the prefix (eg. /usr/local) you installed to. 4. Run `umurmurd -c -p -r`. For other switches and their meaning run `umurmurd -h` diff --git a/cmake/Modules/FindLibconfig.cmake b/cmake/Modules/FindLibconfig.cmake new file mode 100644 index 0000000..98b03c2 --- /dev/null +++ b/cmake/Modules/FindLibconfig.cmake @@ -0,0 +1,10 @@ +include(FindPackageHandleStandardArgs) + +find_path(LIBCONFIG_INCLUDE_DIR NAMES "libconfig.h" PATHS /usr/pkg /usr/local /usr PATH_SUFFIXES "include") +find_path(LIBCONFIG_LIB_DIR NAMES "libconfig.so" "libconfig.dylib" PATHS /usr/pkg /usr/local /usr PATH_SUFFIXES "lib" "lib/${CMAKE_LIBRARY_ARCHITECTURE}") + +if(LIBCONFIG_INCLUDE_DIR AND LIBCONFIG_LIB_DIR) + set(LIBCONFIG_LIBRARIES config) +endif(LIBCONFIG_INCLUDE_DIR AND LIBCONFIG_LIB_DIR) + +find_package_handle_standard_args(Libconfig DEFAULT LIBCONFIG_INCLUDE_DIR LIBCONFIG_LIBRARIES LIBCONFIG_LIB_DIR) diff --git a/cmake/Modules/FindPolarSSL.cmake b/cmake/Modules/FindPolarSSL.cmake new file mode 100644 index 0000000..26ad909 --- /dev/null +++ b/cmake/Modules/FindPolarSSL.cmake @@ -0,0 +1,15 @@ +include(FindPackageHandleStandardArgs) +include(CheckSymbolExists) + +find_path(POLARSSL_INCLUDE_DIR NAMES "version.h" PATHS /usr/pkg /usr/local /usr PATH_SUFFIXES "include/polarssl") +find_path(POLARSSL_LIB_DIR NAMES "libpolarssl.so" "libpolarssl.dylib" "libpolarssl.a" PATHS /usr/pkg /usr/local /usr PATH_SUFFIXES "lib" "lib/${CMAKE_LIBRARY_ARCHITECTURE}") + +if(POLARSSL_INCLUDE_DIR AND POLARSSL_LIB_DIR) + set(POLARSSL_LIBRARIES polarssl) + check_symbol_exists(POLARSSL_ZLIB_SUPPORT "${POLARSSL_INCLUDE_DIR}/version.h" HAVE_ZLIB_SUPPORT) + if(HAVE_ZLIB_SUPPORT) + set(POLARSSL_LIBRARIES ${POLARSSL_LIBRARIES} z) + endif(HAVE_ZLIB_SUPPORT) +endif(POLARSSL_INCLUDE_DIR AND POLARSSL_LIB_DIR) + +find_package_handle_standard_args(PolarSSL REQUIRED_VARS POLARSSL_INCLUDE_DIR POLARSSL_LIBRARIES POLARSSL_LIB_DIR) diff --git a/cmake/Modules/FindProtobufC.cmake b/cmake/Modules/FindProtobufC.cmake new file mode 100644 index 0000000..50bef29 --- /dev/null +++ b/cmake/Modules/FindProtobufC.cmake @@ -0,0 +1,10 @@ +include(FindPackageHandleStandardArgs) + +find_path(PROTOBUFC_INCLUDE_DIR NAMES "protobuf-c.h" PATHS /usr/pkg /usr/local /usr PATH_SUFFIXES "include/google/protobuf-c") +find_path(PROTOBUFC_LIB_DIR NAMES "libprotobuf-c.so" "libprotobuf-c.dylib" PATHS /usr/pkg /usr/local /usr PATH_SUFFIXES "lib" "lib/${CMAKE_LIBRARY_ARCHITECTURE}") + +if(PROTOBUFC_INCLUDE_DIR AND PROTOBUFC_LIB_DIR) + set(PROTOBUFC_LIBRARIES protobuf-c) +endif(PROTOBUFC_INCLUDE_DIR AND PROTOBUFC_LIB_DIR) + +find_package_handle_standard_args(ProtobufC REQUIRED_VARS PROTOBUFC_INCLUDE_DIR PROTOBUFC_LIBRARIES PROTOBUFC_LIB_DIR) diff --git a/configure.ac b/configure.ac index a642207..ff2ddd5 100644 --- a/configure.ac +++ b/configure.ac @@ -86,6 +86,8 @@ AS_IF([test "x$with_ssl" = xopenssl], [ AC_CHECK_LIB([ssl], [SSL_library_init], [], [AC_MSG_ERROR([could not find libssl])]) ]) +AC_DEFINE([DEFAULT_CONFIG], ["/etc/umurmur.conf"], [Default config]) + # Checks for header files. AC_FUNC_ALLOCA AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h limits.h netinet/tcp.h stddef.h stdint.h stdlib.h string.h sys/socket.h sys/time.h syslog.h unistd.h sys/poll.h], [], [AC_MSG_ERROR([missing a required header])]) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..58288f5 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,20 @@ +set(SOURCE + ${SOURCE} + ${CMAKE_CURRENT_SOURCE_DIR}/Mumble.pb-c.c + ${CMAKE_CURRENT_SOURCE_DIR}/ban.c + ${CMAKE_CURRENT_SOURCE_DIR}/channel.c + ${CMAKE_CURRENT_SOURCE_DIR}/client.c + ${CMAKE_CURRENT_SOURCE_DIR}/conf.c + ${CMAKE_CURRENT_SOURCE_DIR}/crypt.c + ${CMAKE_CURRENT_SOURCE_DIR}/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/messagehandler.c + ${CMAKE_CURRENT_SOURCE_DIR}/messages.c + ${CMAKE_CURRENT_SOURCE_DIR}/pds.c + ${CMAKE_CURRENT_SOURCE_DIR}/server.c + ${CMAKE_CURRENT_SOURCE_DIR}/ssl.c + ${CMAKE_CURRENT_SOURCE_DIR}/timer.c + ${CMAKE_CURRENT_SOURCE_DIR}/voicetarget.c + + PARENT_SCOPE +) diff --git a/src/ban.c b/src/ban.c index b02d921..e1855de 100644 --- a/src/ban.c +++ b/src/ban.c @@ -56,10 +56,10 @@ void Ban_init(void) void Ban_deinit(void) { - /* Save banlist */ + /* Save banlist */ if (getStrConf(BANFILE) != NULL) Ban_saveBanFile(); - + Ban_clearBanList(); } @@ -72,7 +72,7 @@ void Ban_UserBan(client_t *client, char *reason) if (ban == NULL) Log_fatal("Out of memory"); memset(ban, 0, sizeof(ban_t)); - + memcpy(ban->hash, client->hash, 20); memcpy(&ban->address, &client->remote_tcp.sin_addr, sizeof(in_addr_t)); ban->mask = 128; @@ -86,7 +86,7 @@ void Ban_UserBan(client_t *client, char *reason) banlist_changed = true; if(getBoolConf(SYNC_BANFILE)) Ban_saveBanFile(); - + SSLi_hash2hex(ban->hash, hexhash); Log_info_client(client, "User kickbanned. Reason: '%s' Hash: %s IP: %s Banned for: %d seconds", ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)), ban->duration); @@ -99,7 +99,7 @@ void Ban_pruneBanned() ban_t *ban; char hexhash[41]; uint64_t bantime_long; - + list_iterate(itr, &banlist) { ban = list_get_entry(itr, ban_t, node); bantime_long = ban->duration * 1000000LL; @@ -129,11 +129,11 @@ bool_t Ban_isBanned(client_t *client) ban_t *ban; list_iterate(itr, &banlist) { ban = list_get_entry(itr, ban_t, node); - if (memcmp(ban->hash, client->hash, 20) == 0) + if (memcmp(ban->hash, client->hash, 20) == 0) return true; } return false; - + } bool_t Ban_isBannedAddr(in_addr_t *addr) @@ -142,7 +142,7 @@ bool_t Ban_isBannedAddr(in_addr_t *addr) ban_t *ban; int mask; in_addr_t tempaddr1, tempaddr2; - + list_iterate(itr, &banlist) { ban = list_get_entry(itr, ban_t, node); mask = ban->mask - 96; @@ -152,7 +152,7 @@ bool_t Ban_isBannedAddr(in_addr_t *addr) tempaddr1 &= (2 ^ mask) - 1; tempaddr2 &= (2 ^ mask) - 1; } - if (memcmp(&tempaddr1, &tempaddr2, sizeof(in_addr_t)) == 0) + if (memcmp(&tempaddr1, &tempaddr2, sizeof(in_addr_t)) == 0) return true; } return false; @@ -173,7 +173,7 @@ message_t *Ban_getBanList(void) char timestr[32]; char hexhash[41]; uint8_t address[16]; - + msg = Msg_banList_create(bancount); list_iterate(itr, &banlist) { ban = list_get_entry(itr, ban_t, node); @@ -212,7 +212,7 @@ void Ban_putBanList(message_t *msg, int n_bans) char *hexhash, *name, *reason, *start; uint32_t duration, mask; uint8_t *address; - + for (i = 0; i < n_bans; i++) { Msg_banList_getEntry(msg, i, &address, &mask, &name, &hexhash, &reason, &start, &duration); ban = malloc(sizeof(ban_t)); @@ -297,7 +297,7 @@ static void Ban_readBanFile(void) p = strtok(NULL, "\n"); if (p == NULL) break; reason = p; - + ban = malloc(sizeof(ban_t)); if (ban == NULL) Log_fatal("Out of memory"); diff --git a/src/channel.c b/src/channel.c index 5ac6924..54b57a2 100644 --- a/src/channel.c +++ b/src/channel.c @@ -93,7 +93,7 @@ static channel_t *next_channel(channel_t *ch) if (list_get_next(&ch->node) == &list_get_entry(&ch->node, channel_t, node)->parent->subs) return NULL; else - return list_get_entry(list_get_next(&ch->node), channel_t, node); + return list_get_entry(list_get_next(&ch->node), channel_t, node); } #endif @@ -134,7 +134,7 @@ channel_t *Chan_iterate_siblings(channel_t *parent, channel_t **channelpptr) *channelpptr = ch; return ch; } - + void Chan_init() { int i; @@ -143,7 +143,7 @@ void Chan_init() const char *defaultChannelName; defaultChannelName = getStrConf(DEFAULT_CHANNEL); - + for (i = 0; ; i++) { if (Conf_getNextChannel(&chdesc, i) < 0) { if (i == 0) @@ -165,18 +165,18 @@ void Chan_init() ch->position = chdesc.position; ch->silent = chdesc.silent; if (chdesc.password) { - Log_info("Setting password on channel '%s'", ch->name); + Log_info("Setting password on channel '%s'", ch->name); ch->password = strdup(chdesc.password); } if (strcmp(defaultChannelName, chdesc.name) == 0) { - Log_info("Setting default channel '%s'", ch->name); + Log_info("Setting default channel '%s'", ch->name); defaultChan = ch; } - + do { Chan_iterate(&ch_itr); } while (ch_itr != NULL && strcmp(ch_itr->name, chdesc.parent) != 0); - + if (ch_itr == NULL) Log_fatal("Error in channel configuration: parent '%s' not found", chdesc.parent); else { @@ -187,7 +187,7 @@ void Chan_init() } if (defaultChan == NULL) defaultChan = rootChan; - + if (defaultChan->noenter) Log_fatal("Error in channel configuration: default channel is marked as noenter"); if (defaultChan->password) @@ -210,8 +210,8 @@ void Chan_init() chlink.source); else ch_src = ch_itr; - - ch_itr = NULL; + + ch_itr = NULL; do { Chan_iterate(&ch_itr); } while (ch_itr != NULL && strcmp(ch_itr->name, chlink.destination) != 0); @@ -220,7 +220,7 @@ void Chan_init() chlink.destination); else ch_dst = ch_itr; - + list_add_tail(&ch_dst->link_node, &ch_src->channel_links); ch_src->linkcount++; Log_info("Adding channel link '%s' -> '%s'", ch_src->name, ch_dst->name); @@ -231,7 +231,7 @@ void Chan_free() { struct dlist *itr, *save; channel_t *ch; - + list_iterate_safe(itr, save, &channels) { ch = list_get_entry(itr, channel_t, flatlist_node); Log_debug("Free channel '%s'", ch->name); @@ -271,7 +271,7 @@ int Chan_userLeave(client_t *client) { channel_t *leaving = NULL; int leaving_id = -1; - + if (client->channel) { list_del(&client->chan_node); leaving = (channel_t *)client->channel; @@ -290,8 +290,8 @@ int Chan_userJoin(channel_t *ch, client_t *client) /* Do nothing if user already is in this channel */ if ((channel_t *)client->channel == ch) return 0; - - Log_debug("Add user %s to channel %s", client->username, ch->name); + + Log_debug("Add user %s to channel %s", client->username, ch->name); /* Only allowed in one channel at a time */ leaving_id = Chan_userLeave(client); list_add_tail(&client->chan_node, &ch->clients); @@ -310,7 +310,7 @@ int Chan_userJoin_id(int channelid, client_t *client) return -1; } else - return Chan_userJoin(ch_itr, client); + return Chan_userJoin(ch_itr, client); } channelJoinResult_t Chan_userJoin_id_test(int channelid, client_t *client) @@ -369,7 +369,7 @@ void Chan_buildTreeList(channel_t *ch, struct dlist *head) channellist_t *chl; struct dlist *itr; channel_t *sub; - + chl = malloc(sizeof(channellist_t)); chl->chan = ch; init_list_entry(&chl->node); diff --git a/src/client.c b/src/client.c index 37bdb37..f7d2b70 100644 --- a/src/client.c +++ b/src/client.c @@ -101,7 +101,7 @@ void Client_janitor() c->availableBandwidth += maxBandwidth; if (c->availableBandwidth > bwTop) c->availableBandwidth = bwTop; - + if (Timer_isElapsed(&c->lastActivity, 1000000LL * INACTIVITY_TIMEOUT)) { /* No activity from client - assume it is lost and close. */ Log_info_client(c, "Timeout, closing."); @@ -136,7 +136,7 @@ codec_t *Client_codec_iterate(client_t *client, codec_t **codec_itr) if (list_empty(&client->codecs)) return NULL; - + if (cd == NULL) { cd = list_get_entry(list_get_first(&client->codecs), codec_t, node); } else { @@ -170,7 +170,7 @@ bool_t Client_token_match(client_t *client, char *str) { token_t *token; struct dlist *itr; - + if (list_empty(&client->tokens)) return false; list_iterate(itr, &client->tokens) { @@ -185,7 +185,7 @@ void Client_token_free(client_t *client) { struct dlist *itr, *save; token_t *token; - + list_iterate_safe(itr, save, &client->tokens) { token = list_get_entry(itr, token_t, node); list_del(&token->node); @@ -210,13 +210,13 @@ void recheckCodecVersions(client_t *connectingClient) bool_t enableOpus; init_list_entry(&codec_list); - + while (Client_iterate(&client_itr) != NULL) { codec_itr = NULL; if (client_itr->codec_count == 0 && !client_itr->bOpus) continue; while (Client_codec_iterate(client_itr, &codec_itr) != NULL) { - found = false; + found = false; list_iterate(itr, &codec_list) { cd = list_get_entry(itr, codec_t, node); if (cd->codec == codec_itr->codec) { @@ -239,7 +239,7 @@ void recheckCodecVersions(client_t *connectingClient) if (client_itr->bOpus) opus++; } - if (users == 0) + if (users == 0) return; enableOpus = ((opus * 100 / users) >= getIntConf(OPUS_THRESHOLD)); @@ -266,7 +266,7 @@ void recheckCodecVersions(client_t *connectingClient) bPreferAlpha = true; else bPreferAlpha = !bPreferAlpha; - + if (bPreferAlpha) iCodecAlpha = version; else @@ -285,7 +285,7 @@ void recheckCodecVersions(client_t *connectingClient) sendmsg->payload.codecVersion->opus = enableOpus; Client_send_message_except(NULL, sendmsg); - + if (enableOpus && !bOpus) { client_itr = NULL; while (Client_iterate(&client_itr) != NULL) { @@ -294,9 +294,9 @@ void recheckCodecVersions(client_t *connectingClient) Client_textmessage(client_itr, OPUS_WARN_SWITCHING); } } - Log_info("OPUS codec %s", bOpus ? "enabled" : "disabled"); + Log_info("OPUS codec %s", bOpus ? "enabled" : "disabled"); } - + bOpus = enableOpus; } @@ -347,17 +347,17 @@ int Client_add(int fd, struct sockaddr_in *remote) newclient->sessionId = findFreeSessionId(); if (newclient->sessionId < 0) Log_fatal("Could not find a free session ID"); - + init_list_entry(&newclient->txMsgQueue); init_list_entry(&newclient->chan_node); init_list_entry(&newclient->node); init_list_entry(&newclient->voicetargets); init_list_entry(&newclient->codecs); init_list_entry(&newclient->tokens); - + list_add_tail(&newclient->node, &clients); clientcount++; - + /* Send version message to client */ sendmsg = Msg_create(Version); sendmsg->payload.version->has_version = true; @@ -395,7 +395,7 @@ void Client_free(client_t *client) Client_codec_free(client); Voicetarget_free_all(client); Client_token_free(client); - + list_del(&client->node); if (client->ssl) SSLi_free(client->ssl); @@ -404,9 +404,9 @@ void Client_free(client_t *client) if (client->release) free(client->release); if (client->os) - free(client->os); + free(client->os); if (client->os_version) - free(client->os_version); + free(client->os_version); if (client->username) free(client->username); if (client->context) @@ -426,7 +426,7 @@ void Client_close(client_t *client) void Client_disconnect_all() { struct dlist *itr, *save; - + list_iterate_safe(itr, save, &clients) { Client_free(list_get_entry(itr, client_t, node)); } @@ -436,7 +436,7 @@ int Client_read_fd(int fd) { struct dlist *itr; client_t *client = NULL; - + list_iterate(itr, &clients) { if (fd == list_get_entry(itr, client_t, node)->tcpfd) { client = list_get_entry(itr, client_t, node); @@ -454,13 +454,13 @@ int Client_read(client_t *client) int rc; Timer_restart(&client->lastActivity); - + if (client->writeBlockedOnRead) { client->writeBlockedOnRead = false; Log_debug("Client_read: writeBlockedOnRead == true"); return Client_write(client); } - + if (client->shutdown_wait) { Client_free(client); return 0; @@ -476,7 +476,7 @@ int Client_read(client_t *client) do { errno = 0; - if (!client->msgsize) + if (!client->msgsize) rc = SSLi_read(client->ssl, &client->rxbuf[client->rxcount], 6 - client->rxcount); else rc = SSLi_read(client->ssl, &client->rxbuf[client->rxcount], client->msgsize); @@ -514,7 +514,7 @@ int Client_read(client_t *client) client->readBlockedOnWrite = true; return 0; } - else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_ZERO_RETURN || + else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_ZERO_RETURN || SSLi_get_error(client->ssl, rc) == 0) { Log_info_client(client, "Connection closed by peer"); Client_close(client); @@ -524,7 +524,7 @@ int Client_read(client_t *client) if (errno == 0) Log_info_client(client, "Connection closed by peer"); else - Log_info_client(client,"Error: %s - Closing connection (code %d)", + Log_info_client(client,"Error: %s - Closing connection (code %d)", strerror(errno)); } else if (SSLi_get_error(client->ssl, rc) == SSLI_ERROR_CONNRESET) { @@ -538,7 +538,7 @@ int Client_read(client_t *client) } } } while (SSLi_data_pending(client->ssl)); - + return 0; } @@ -546,7 +546,7 @@ int Client_write_fd(int fd) { struct dlist *itr; client_t *client = NULL; - + list_iterate(itr, &clients) { if(fd == list_get_entry(itr, client_t, node)->tcpfd) { client = list_get_entry(itr, client_t, node); @@ -562,7 +562,7 @@ int Client_write_fd(int fd) int Client_write(client_t *client) { int rc; - + if (client->readBlockedOnWrite) { client->readBlockedOnWrite = false; Log_debug("Client_write: readBlockedOnWrite == true"); @@ -650,7 +650,7 @@ client_t *Client_iterate(client_t **client_itr) if (list_empty(&clients)) return NULL; - + if (c == NULL) { c = list_get_entry(list_get_first(&clients), client_t, node); } else { @@ -689,7 +689,7 @@ int Client_send_message_except(client_t *client, message_t *msg) { client_t *itr = NULL; int count = 0; - + Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */ while (Client_iterate(&itr) != NULL) { if (itr != client) { @@ -700,11 +700,11 @@ int Client_send_message_except(client_t *client, message_t *msg) } } Msg_free(msg); /* Free our reference to the message */ - + if (count == 0) Msg_free(msg); /* If only 1 client is connected then no message is passed * to Client_send_message(). Free it here. */ - + return 0; } @@ -712,7 +712,7 @@ int Client_send_message_except_ver(client_t *client, message_t *msg, uint32_t ve { client_t *itr = NULL; int count = 0; - + Msg_inc_ref(msg); /* Make sure a reference is held during the whole iteration. */ while (Client_iterate(&itr) != NULL) { if (itr != client) { @@ -723,11 +723,11 @@ int Client_send_message_except_ver(client_t *client, message_t *msg, uint32_t ve } } Msg_free(msg); /* Free our reference to the message */ - + if (count == 0) Msg_free(msg); /* If only 1 client is connected then no message is passed * to Client_send_message(). Free it here. */ - + return 0; } @@ -741,9 +741,9 @@ static bool_t checkDecrypt(client_t *client, const uint8_t *encrypted, uint8_t * if (Timer_elapsed(&client->cryptState.tLastRequest) > 5000000ULL) { message_t *sendmsg; Timer_restart(&client->cryptState.tLastRequest); - + sendmsg = Msg_create(CryptSetup); - Log_info_client(client, "Requesting voice channel crypt resync"); + Log_info_client(client, "Requesting voice channel crypt resync"); Client_send_message(client, sendmsg); } } @@ -759,7 +759,7 @@ int Client_read_udp() uint64_t key; client_t *itr; UDPMessageType_t msgType; - + #if defined(__LP64__) uint8_t encbuff[UDP_PACKET_SIZE + 8]; uint8_t *encrypted = encbuff + 4; @@ -767,7 +767,7 @@ int Client_read_udp() uint8_t encrypted[UDP_PACKET_SIZE]; #endif uint8_t buffer[UDP_PACKET_SIZE]; - + len = recvfrom(udpsock, encrypted, UDP_PACKET_SIZE, MSG_TRUNC, (struct sockaddr *)&from, &fromlen); if (len == 0) { return -1; @@ -788,21 +788,21 @@ int Client_read_udp() ping[3] = htonl((uint32_t)clientcount); ping[4] = htonl((uint32_t)getIntConf(MAX_CLIENTS)); ping[5] = htonl((uint32_t)getIntConf(MAX_BANDWIDTH)); - + sendto(udpsock, encrypted, 6 * sizeof(uint32_t), 0, (struct sockaddr *)&from, fromlen); return 0; } - + key = (((uint64_t)from.sin_addr.s_addr) << 16) ^ from.sin_port; itr = NULL; - + while (Client_iterate(&itr) != NULL) { if (itr->key == key) { if (!checkDecrypt(itr, encrypted, buffer, len)) goto out; break; } - } + } if (itr == NULL) { /* Unknown peer */ while (Client_iterate(&itr) != NULL) { if (itr->remote_tcp.sin_addr.s_addr == from.sin_addr.s_addr) { @@ -818,7 +818,7 @@ int Client_read_udp() if (itr == NULL) { /* Couldn't find this peer among connected clients */ goto out; } - + itr->bUDP = true; len -= 4; /* Adjust for crypt header */ msgType = (UDPMessageType_t)((buffer[0] >> 5) & 0x7); @@ -839,7 +839,7 @@ int Client_read_udp() Log_debug("Unknown UDP message type from %s port %d", inet_ntoa(from.sin_addr), ntohs(from.sin_port)); break; } - + out: return 0; } @@ -851,7 +851,7 @@ static inline void Client_send_voice(client_t *src, client_t *dst, uint8_t *data src->context != NULL && dst->context != NULL && /* ...both source and destination has context */ strcmp(src->context, dst->context) == 0) /* ...and the contexts match */ Client_send_udp(dst, data, len); - else + else Client_send_udp(dst, data, len - poslen); } } @@ -867,21 +867,21 @@ int Client_voiceMsg(client_t *client, uint8_t *data, int len) unsigned int poslen, counter, size; int offset, packetsize; voicetarget_t *vt; - + channel_t *ch = (channel_t *)client->channel; struct dlist *itr; - + if (!client->authenticated || client->mute || client->self_mute || ch->silent) goto out; - + packetsize = 20 + 8 + 4 + len; if (client->availableBandwidth - packetsize < 0) goto out; /* Discard */ client->availableBandwidth -= packetsize; - + Timer_restart(&client->idleTime); Timer_restart(&client->lastActivity); - + counter = Pds_get_numval(pdi); /* step past session id */ if ((type >> 5) != UDPVoiceOpus) { do { @@ -892,22 +892,22 @@ int Client_voiceMsg(client_t *client, uint8_t *data, int len) size = Pds_get_numval(pdi); Pds_skip(pdi, size & 0x1fff); } - + poslen = pdi->maxsize - pdi->offset; /* For stripping of positional info */ - + Pds_add_numval(pds, client->sessionId); Pds_append_data_nosize(pds, data + 1, len - 1); - + if (target == 0x1f) { /* Loopback */ buffer[0] = (uint8_t) type; Client_send_udp(client, buffer, pds->offset + 1); } else if (target == 0) { /* regular channel speech */ buffer[0] = (uint8_t) type; - + if (ch == NULL) goto out; - + list_iterate(itr, &ch->clients) { client_t *c; c = list_get_entry(itr, client_t, chan_node); @@ -959,7 +959,7 @@ int Client_voiceMsg(client_t *client, uint8_t *data, int len) } Chan_freeTreeList(&chanlist); } - } + } /* Sessions */ for (i = 0; i < TARGET_MAX_SESSIONS && vt->sessions[i] != -1; i++) { client_t *c; @@ -976,7 +976,7 @@ int Client_voiceMsg(client_t *client, uint8_t *data, int len) out: Pds_free(pds); Pds_free(pdi); - + return 0; } @@ -995,11 +995,11 @@ static int Client_send_udp(client_t *client, uint8_t *data, int len) #endif if (mbuf == NULL) Log_fatal("Out of memory"); - + CryptState_encrypt(&client->cryptState, data, buf, len); - + sendto(udpsock, buf, len + 4, 0, (struct sockaddr *)&client->remote_udp, sizeof(struct sockaddr_in)); - + free(mbuf); } else { message_t *msg; diff --git a/src/client.h b/src/client.h index 95f6172..c6085d2 100644 --- a/src/client.h +++ b/src/client.h @@ -31,7 +31,7 @@ #ifndef CLIENT_H_45786678 #define CLIENT_H_45786678 -#include +#include "config.h" #include #include /* close() */ #include @@ -65,9 +65,9 @@ typedef struct { bool_t shutdown_wait; cryptState_t cryptState; bool_t readBlockedOnWrite, writeBlockedOnRead; - + struct sockaddr_in remote_tcp; - struct sockaddr_in remote_udp; + struct sockaddr_in remote_udp; uint8_t rxbuf[BUFSIZE], txbuf[BUFSIZE]; uint32_t rxcount, msgsize, drainleft, txcount, txsize; int sessionId; diff --git a/src/conf.c b/src/conf.c index 4750452..2c6f56f 100644 --- a/src/conf.c +++ b/src/conf.c @@ -84,7 +84,7 @@ const char *getStrConf(param_t param) { config_setting_t *setting = NULL; const char *strsetting = NULL; - + switch (param) { case CERTIFICATE: setting = config_lookup(&configuration, "certificate"); @@ -228,7 +228,7 @@ const char *getStrConf(param_t param) int getIntConf(param_t param) { config_setting_t *setting = NULL; - + switch (param) { case BINDPORT: setting = config_lookup(&configuration, "bindport"); @@ -278,7 +278,7 @@ int getIntConf(param_t param) bool_t getBoolConf(param_t param) { config_setting_t *setting = NULL; - + switch (param) { case ALLOW_TEXTMESSAGE: setting = config_lookup(&configuration, "allow_textmessage"); @@ -311,47 +311,47 @@ int Conf_getNextChannel(conf_channel_t *chdesc, int index) config_setting_t *setting = NULL; int maxconfig = 64, ret = 0; char configstr[maxconfig]; - + ret = snprintf(configstr, maxconfig, "channels.[%d].name", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) return -1; /* Required */ chdesc->name = config_setting_get_string(setting); - + ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) return -1; /* Required */ chdesc->parent = config_setting_get_string(setting); - + ret = snprintf(configstr, maxconfig, "channels.[%d].description", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */ chdesc->description = NULL; else chdesc->description = config_setting_get_string(setting); - + ret = snprintf(configstr, maxconfig, "channels.[%d].password", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */ chdesc->password = NULL; else chdesc->password = config_setting_get_string(setting); - + ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */ chdesc->noenter = false; else chdesc->noenter = config_setting_get_bool(setting); - + ret = snprintf(configstr, maxconfig, "channels.[%d].silent", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */ chdesc->silent = false; else chdesc->silent = config_setting_get_bool(setting); - + ret = snprintf(configstr, maxconfig, "channels.[%d].position", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */ @@ -367,7 +367,7 @@ int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index) config_setting_t *setting = NULL; int maxconfig = 64, ret = 0; char configstr[maxconfig]; - + ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index); setting = config_lookup(&configuration, configstr); if (ret >= maxconfig || ret < 0 || setting == NULL) diff --git a/src/conf.h b/src/conf.h index 6a4ee69..8253fb3 100644 --- a/src/conf.h +++ b/src/conf.h @@ -32,8 +32,7 @@ #define CONF_H_24564356 #include "messages.h" - -#define DEFAULT_CONFIG "/etc/umurmur.conf" +#include "config.h" typedef enum param { CERTIFICATE, diff --git a/src/config.h.in b/src/config.h.in new file mode 100644 index 0000000..3169a19 --- /dev/null +++ b/src/config.h.in @@ -0,0 +1,9 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#cmakedefine USE_POLARSSL +#cmakedefine USE_POLARSSL_TESTCERT +#cmakedefine USE_POLARSSL_HAVEGE +#define DEFAULT_CONFIG "${CMAKE_INSTALL_PREFIX}/etc/umurmur.conf" + +#endif // CONFIG_H diff --git a/src/crypt.c b/src/crypt.c index 89fe19d..ddf1f74 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -199,7 +199,7 @@ bool_t CryptState_decrypt(cryptState_t *cs, const unsigned char *source, unsigne CryptState_ocb_decrypt(cs, source+4, dst, plain_length, cs->decrypt_iv, tag); if (memcmp(tag, source+1, 3) != 0) { - memcpy(cs->decrypt_iv, saveiv, AES_BLOCK_SIZE); + memcpy(cs->decrypt_iv, saveiv, AES_BLOCK_SIZE); return false; } cs->decrypt_history[cs->decrypt_iv[0]] = cs->decrypt_iv[1]; diff --git a/src/crypt.h b/src/crypt.h index e190093..292f196 100644 --- a/src/crypt.h +++ b/src/crypt.h @@ -31,9 +31,7 @@ #ifndef CRYPTSTATE_H_34564356 #define CRYPTSTATE_H_34564356 -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #ifdef USE_POLARSSL #include @@ -53,12 +51,12 @@ typedef struct CryptState { uint8_t encrypt_iv[AES_BLOCK_SIZE]; uint8_t decrypt_iv[AES_BLOCK_SIZE]; uint8_t decrypt_history[0x100]; - + unsigned int uiGood; unsigned int uiLate; unsigned int uiLost; unsigned int uiResync; - + unsigned int uiRemoteGood; unsigned int uiRemoteLate; unsigned int uiRemoteLost; @@ -72,7 +70,7 @@ typedef struct CryptState { #endif etimer_t tLastGood; etimer_t tLastRequest; - bool_t bInit; + bool_t bInit; } cryptState_t; void CryptState_init(cryptState_t *cs); diff --git a/src/log.c b/src/log.c index 539dee5..ebb5cd1 100644 --- a/src/log.c +++ b/src/log.c @@ -56,7 +56,7 @@ static void openlogfile(const char *logfilename) /* Set the stream as line buffered */ if (setvbuf(logfile, NULL, _IOLBF, 0) < 0) Log_fatal("setvbuf() failed: %s\n", strerror(errno)); - + /* XXX - Is it neccessary/appropriate that logging to file is non-blocking? * If not, there's a risk that execution blocks, meaning that voice blocks * as well since uMurmur is single threaded by design. OTOH, what could @@ -84,11 +84,11 @@ static char *timestring(void) void Log_init(bool_t terminal) { const char *logfilename; - - termprint = terminal; + + termprint = terminal; if (termprint) return; - + logfilename = getStrConf(LOGFILE); if (logfilename != NULL) { openlogfile(logfilename); @@ -103,14 +103,14 @@ void Log_free() return; else if (logfile) fclose(logfile); - else + else closelog(); } - + void Log_reset() { const char *logfilename; - + if (logfile) { logfilename = getStrConf(LOGFILE); fclose(logfile); @@ -122,11 +122,11 @@ void logthis(const char *logstring, ...) { va_list argp; char buf[STRSIZE + 1]; - + va_start(argp, logstring); vsnprintf(&buf[0], STRSIZE, logstring, argp); va_end(argp); - + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) @@ -140,14 +140,14 @@ void Log_warn(const char *logstring, ...) va_list argp; char buf[STRSIZE + 1]; int offset = 0; - + if (termprint || logfile) offset = sprintf(buf, "WARN: "); - + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) @@ -161,14 +161,14 @@ void Log_info(const char *logstring, ...) va_list argp; char buf[STRSIZE + 1]; int offset = 0; - + if (termprint || logfile) offset = sprintf(buf, "INFO: "); - - va_start(argp, logstring); + + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) @@ -182,14 +182,14 @@ void Log_info_client(client_t *client, const char *logstring, ...) va_list argp; char buf[STRSIZE + 1]; int offset = 0; - + if (termprint || logfile) offset = sprintf(buf, "INFO: "); va_start(argp, logstring); offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - + offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d", client->sessionId, client->username == NULL ? "" : client->username, @@ -209,11 +209,11 @@ void Log_debug(const char *logstring, ...) va_list argp; char buf[STRSIZE + 1]; int offset = 0; - + if (termprint || logfile) offset = sprintf(buf, "DEBUG: "); - - va_start(argp, logstring); + + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); if (termprint) @@ -230,20 +230,20 @@ void Log_fatal(const char *logstring, ...) va_list argp; char buf[STRSIZE + 1]; int offset = 0; - + if (termprint || logfile) offset = sprintf(buf, "FATAL: "); - - va_start(argp, logstring); + + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) fprintf(logfile, "%s %s\n", timestring(), buf); else { /* If logging subsystem is not initialized, fall back to stderr + - * syslog logging for fatal errors. + * syslog logging for fatal errors. */ if (!init) { openlog("uMurmurd", LOG_PID, LOG_DAEMON); @@ -251,6 +251,6 @@ void Log_fatal(const char *logstring, ...) } syslog(LOG_CRIT, "%s", buf); } - + exit(1); } diff --git a/src/main.c b/src/main.c index 234cc7f..bfff28a 100644 --- a/src/main.c +++ b/src/main.c @@ -54,6 +54,7 @@ #include "client.h" #include "conf.h" #include "version.h" +#include "config.h" char system_string[64], version_string[64]; int bindport; @@ -68,7 +69,7 @@ void lockfile(const char *pidfile) * unmodified if we cannot lock it. */ lfp = open(pidfile, O_WRONLY|O_CREAT, 0640); - + if (lfp < 0) Log_fatal("Cannot open PID-file %s for writing", pidfile); @@ -152,12 +153,12 @@ static void switch_user(void) if (setuid(pwd->pw_uid)) Log_fatal("setuid() failed: %s", strerror(errno)); - + if (!grp) grp = getgrgid(gid); if (!grp) Log_fatal("getgrgid() failed: %s", strerror(errno)); - + Log_info("Switch to user '%s' group '%s'", pwd->pw_name, grp->gr_name); } @@ -178,7 +179,7 @@ void signal_handler(int sig) void daemonize() { int i; - + if (getppid() == 1) return; /* already a daemon */ i = fork(); @@ -188,19 +189,19 @@ void daemonize() } if ( i > 0) exit(0); /* parent exits */ - + /* child (daemon) continues */ setsid(); /* obtain a new process group */ for (i = getdtablesize(); i >= 0; --i) close(i); /* close all descriptors */ - + i = open("/dev/null",O_RDWR); (void)dup(i); (void)dup(i); - + umask(027); /* set newly created file permissions */ (void)chdir("/"); - + } #ifdef POSIX_PRIORITY_SCHEDULING @@ -245,7 +246,7 @@ int main(int argc, char **argv) char *conffile = NULL, *pidfile = NULL; int c; struct utsname utsbuf; - + /* Arguments */ #ifdef POSIX_PRIORITY_SCHEDULING while ((c = getopt(argc, argv, "drp:c:a:b:ht")) != EOF) { @@ -292,12 +293,12 @@ int main(int argc, char **argv) else exit(0); } - + /* Initialize the config subsystem early; * switch_user() will need to read some config variables as well as logging. */ Conf_init(conffile); - + /* Logging to terminal if not daemonizing, otherwise to syslog or log file. */ if (!nodaemon) { @@ -311,10 +312,10 @@ int main(int argc, char **argv) /* Reopen log file. If user switch results in access denied, we catch * it early. */ - Log_reset(); + Log_reset(); } else Log_init(true); - + signal(SIGCHLD, SIG_IGN); /* ignore child */ signal(SIGTSTP, SIG_IGN); /* ignore tty signals */ signal(SIGTTOU, SIG_IGN); @@ -322,7 +323,7 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); signal(SIGHUP, signal_handler); /* catch hangup signal */ signal(SIGTERM, signal_handler); /* catch kill signal */ - + /* Build system string */ if (uname(&utsbuf) == 0) { snprintf(system_string, 64, "%s %s", utsbuf.sysname, utsbuf.machine); @@ -332,7 +333,7 @@ int main(int argc, char **argv) snprintf(system_string, 64, "unknown unknown"); snprintf(version_string, 64, "unknown"); } - + /* Initializing */ SSLi_init(); Chan_init(); @@ -343,17 +344,17 @@ int main(int argc, char **argv) if (realtime) setscheduler(); #endif - + Server_run(); - + Ban_deinit(); SSLi_deinit(); Chan_free(); Log_free(); Conf_deinit(); - + if (pidfile != NULL) unlink(pidfile); - + return 0; } diff --git a/src/messagehandler.c b/src/messagehandler.c index 849c559..f1a753f 100644 --- a/src/messagehandler.c +++ b/src/messagehandler.c @@ -61,7 +61,7 @@ static void sendServerReject(client_t *client, const char *reason, MumbleProto__ msg->payload.reject->type = type; msg->payload.reject->has_type = true; Client_send_message(client, msg); - + Log_info_client(client, "Server reject reason: %s", reason); } @@ -85,7 +85,7 @@ static void addTokens(client_t *client, message_t *msg) return; } } - + for (i = 0; i < msg->payload.authenticate->n_tokens; i++) { Log_debug("Adding token '%s' to client '%s'", msg->payload.authenticate->tokens[i], client->username); Client_token_add(client, msg->payload.authenticate->tokens[i]); @@ -105,7 +105,7 @@ void Mh_handle_message(client_t *client, message_t *msg) msg->messageType == Version)) { goto out; } - + switch (msg->messageType) { case UDPTunnel: case Ping: @@ -117,31 +117,31 @@ void Mh_handle_message(client_t *client, message_t *msg) default: Timer_restart(&client->idleTime); } - + switch (msg->messageType) { case Authenticate: Log_debug("Authenticate message received"); - + if (IS_AUTH(client) || !msg->payload.authenticate->username) { /* Authenticate message might be sent when a tokens are changed by the user.*/ Client_token_free(client); /* Clear the token list */ if (msg->payload.authenticate->n_tokens > 0) { Log_debug("Tokens in auth message from '%s'. n_tokens = %d", client->username, msg->payload.authenticate->n_tokens); - addTokens(client, msg); + addTokens(client, msg); } break; } - + if (SSLi_getSHA1Hash(client->ssl, client->hash) && Ban_isBanned(client)) { char hexhash[41]; SSLi_hash2hex(client->hash, hexhash); Log_info("Client with hash '%s' is banned. Disconnecting", hexhash); goto disconnect; } - + client->authenticated = true; - + client_itr = NULL; while (Client_iterate(&client_itr) != NULL) { if (!IS_AUTH(client_itr)) @@ -152,7 +152,7 @@ void Mh_handle_message(client_t *client, message_t *msg) Log_debug("Username already in use"); sendServerReject(client, buf, MUMBLE_PROTO__REJECT__REJECT_TYPE__UsernameInUse); goto disconnect; - } + } } if (strlen(getStrConf(PASSPHRASE)) > 0) { if (!msg->payload.authenticate->password || @@ -165,7 +165,7 @@ void Mh_handle_message(client_t *client, message_t *msg) msg->payload.authenticate->password : "(null)"); goto disconnect; } - } + } if (strlen(msg->payload.authenticate->username) == 0 || strlen(msg->payload.authenticate->username) >= MAX_USERNAME) { /* XXX - other invalid names? */ char buf[64]; @@ -173,7 +173,7 @@ void Mh_handle_message(client_t *client, message_t *msg) Log_debug("Invalid username"); sendServerReject(client, buf, MUMBLE_PROTO__REJECT__REJECT_TYPE__InvalidUsername); goto disconnect; - } + } if (Client_count() >= getIntConf(MAX_CLIENTS)) { char buf[64]; @@ -181,21 +181,21 @@ void Mh_handle_message(client_t *client, message_t *msg) sendServerReject(client, buf, MUMBLE_PROTO__REJECT__REJECT_TYPE__ServerFull); goto disconnect; } - + /* Name */ - client->username = strdup(msg->payload.authenticate->username); + client->username = strdup(msg->payload.authenticate->username); /* Tokens */ if (msg->payload.authenticate->n_tokens > 0) addTokens(client, msg); - + /* Check if admin PW among tokens */ if (strlen(getStrConf(ADMIN_PASSPHRASE)) > 0 && Client_token_match(client, getStrConf(ADMIN_PASSPHRASE))) { client->isAdmin = true; Log_info_client(client, "User provided admin password"); } - + /* Setup UDP encryption */ CryptState_init(&client->cryptState); CryptState_genKey(&client->cryptState); @@ -214,20 +214,20 @@ void Mh_handle_message(client_t *client, message_t *msg) /* Channel stuff */ Chan_userJoin(defaultChan, client); /* Join default channel */ - /* Codec version */ + /* Codec version */ Log_debug("Client %d has %d CELT codecs", client->sessionId, msg->payload.authenticate->n_celt_versions); if (msg->payload.authenticate->n_celt_versions > 0) { int i; codec_t *codec_itr; client->codec_count = msg->payload.authenticate->n_celt_versions; - + for (i = 0; i < client->codec_count; i++) Client_codec_add(client, msg->payload.authenticate->celt_versions[i]); codec_itr = NULL; while (Client_codec_iterate(client, &codec_itr) != NULL) Log_debug("Client %d CELT codec ver 0x%x", client->sessionId, codec_itr->codec); - + } else { Client_codec_add(client, (int32_t)0x8000000b); client->codec_count = 1; @@ -235,9 +235,9 @@ void Mh_handle_message(client_t *client, message_t *msg) } if (msg->payload.authenticate->opus) client->bOpus = true; - + recheckCodecVersions(client); - + sendmsg = Msg_create(CodecVersion); sendmsg->payload.codecVersion->alpha = iCodecAlpha; sendmsg->payload.codecVersion->beta = iCodecBeta; @@ -268,7 +268,7 @@ void Mh_handle_message(client_t *client, message_t *msg) sendmsg->payload.channelState->position = ch_itr->position; } Log_debug("Send channel info: %s", sendmsg->payload.channelState->name); - Client_send_message(client, sendmsg); + Client_send_message(client, sendmsg); } /* Iterate channels and send channel links info */ @@ -278,12 +278,12 @@ void Mh_handle_message(client_t *client, message_t *msg) uint32_t *links; int i = 0; struct dlist *itr; - + sendmsg = Msg_create(ChannelState); sendmsg->payload.channelState->has_channel_id = true; sendmsg->payload.channelState->channel_id = ch_itr->id; sendmsg->payload.channelState->n_links = ch_itr->linkcount; - + links = (uint32_t *)malloc(ch_itr->linkcount * sizeof(uint32_t)); list_iterate(itr, &ch_itr->channel_links) { /* Iterate links */ channel_t *ch; @@ -294,7 +294,7 @@ void Mh_handle_message(client_t *client, message_t *msg) Client_send_message(client, sendmsg); } } - + /* Send user state for connecting user to other users */ sendmsg = Msg_create(UserState); sendmsg->payload.userState->has_session = true; @@ -358,7 +358,7 @@ void Mh_handle_message(client_t *client, message_t *msg) Client_send_message(client, sendmsg); /* Server config message */ - sendmsg = Msg_create(ServerConfig); + sendmsg = Msg_create(ServerConfig); sendmsg->payload.serverConfig->has_allow_html = true; sendmsg->payload.serverConfig->allow_html = true; /* Support this? */ sendmsg->payload.serverConfig->has_message_length = true; @@ -369,7 +369,7 @@ void Mh_handle_message(client_t *client, message_t *msg) Log_info_client(client, "User %s authenticated", client->username); break; - + case Ping: if (msg->payload.ping->has_good) client->cryptState.uiRemoteGood = msg->payload.ping->good; @@ -384,14 +384,14 @@ void Mh_handle_message(client_t *client, message_t *msg) client->cryptState.uiRemoteGood, client->cryptState.uiRemoteLate, client->cryptState.uiRemoteLost, client->cryptState.uiRemoteResync ); - + client->UDPPingAvg = msg->payload.ping->udp_ping_avg; client->UDPPingVar = msg->payload.ping->udp_ping_var; client->TCPPingAvg = msg->payload.ping->tcp_ping_avg; client->TCPPingVar = msg->payload.ping->tcp_ping_var; client->UDPPackets = msg->payload.ping->udp_packets; client->TCPPackets = msg->payload.ping->tcp_packets; - + sendmsg = Msg_create(Ping); sendmsg->payload.ping->timestamp = msg->payload.ping->timestamp; @@ -451,7 +451,7 @@ void Mh_handle_message(client_t *client, message_t *msg) if (target == NULL) target = client; - + msg->payload.userState->has_session = true; msg->payload.userState->session = target->sessionId; msg->payload.userState->has_actor = true; @@ -492,7 +492,7 @@ void Mh_handle_message(client_t *client, message_t *msg) client->recording = msg->payload.userState->recording; char *message; uint32_t *tree_id; - + message = malloc(strlen(client->username) + 32); if (!message) Log_fatal("Out of memory"); @@ -560,19 +560,19 @@ void Mh_handle_message(client_t *client, message_t *msg) Log_fatal("Out of memory"); memcpy(client->context, msg->payload.userState->plugin_context.data, msg->payload.userState->plugin_context.len); - + break; /* Don't inform other users about this state */ } /* Re-use message */ Msg_inc_ref(msg); - + Client_send_message_except(NULL, msg); /* Need to send remove channel message _after_ UserState message */ if (sendmsg != NULL) Client_send_message_except(NULL, sendmsg); break; - + case TextMessage: if (!getBoolConf(ALLOW_TEXTMESSAGE)) break; @@ -584,7 +584,7 @@ void Mh_handle_message(client_t *client, message_t *msg) sendPermissionDenied(client, "Tree message not supported"); break; } - + if (msg->payload.textMessage->n_channel_id > 0) { /* To channel */ int i; channel_t *ch_itr; @@ -668,12 +668,12 @@ void Mh_handle_message(client_t *client, message_t *msg) Log_debug("Client release %s", client->release); } if (msg->payload.version->os) { - if (client->os) free(client->os); + if (client->os) free(client->os); client->os = strdup(msg->payload.version->os); Log_debug("Client OS %s", client->os); } if (msg->payload.version->os_version) { - if (client->os_version) free(client->os_version); + if (client->os_version) free(client->os_version); client->os_version = strdup(msg->payload.version->os_version); Log_debug("Client OS version %s", client->os_version); } @@ -681,17 +681,17 @@ void Mh_handle_message(client_t *client, message_t *msg) case PermissionQuery: Msg_inc_ref(msg); /* Re-use message */ msg->payload.permissionQuery->has_permissions = true; - + if (client->isAdmin) msg->payload.permissionQuery->permissions = PERM_ADMIN; else msg->payload.permissionQuery->permissions = PERM_DEFAULT; - + if (!getBoolConf(ALLOW_TEXTMESSAGE)) msg->payload.permissionQuery->permissions &= ~PERM_TEXTMESSAGE; if (!getBoolConf(ENABLE_BAN)) msg->payload.permissionQuery->permissions &= ~PERM_BAN; - + Client_send_message(client, msg); break; case UDPTunnel: @@ -701,7 +701,7 @@ void Mh_handle_message(client_t *client, message_t *msg) case ChannelState: { channel_t *ch_itr, *parent, *newchan; - int leave_id; + int leave_id; /* Don't allow any changes to existing channels */ if (msg->payload.channelState->has_channel_id) { sendPermissionDenied(client, "Not supported by uMurmur"); @@ -727,7 +727,7 @@ void Mh_handle_message(client_t *client, message_t *msg) sendPermissionDenied(client, "Channel name too long"); break; } - + parent = Chan_fromId(msg->payload.channelState->parent); if (parent == NULL) break; @@ -740,13 +740,13 @@ void Mh_handle_message(client_t *client, message_t *msg) } if (ch_itr != NULL) break; - + /* Disallow temporary channels as siblings to temporary channels */ if (parent->temporary) { sendPermissionDenied(client, "Parent channel is temporary channel"); break; } - + /* XXX - Murmur looks for "\\w" and sends perm denied if not found. * I don't know why so I don't do that here... */ @@ -785,7 +785,7 @@ void Mh_handle_message(client_t *client, message_t *msg) sendmsg->payload.channelRemove->channel_id = leave_id; Client_send_message_except(NULL, sendmsg); } - } + } break; case UserStats: @@ -794,10 +794,10 @@ void Mh_handle_message(client_t *client, message_t *msg) codec_t *codec_itr = NULL; int i; bool_t details = true; - + if (msg->payload.userStats->has_stats_only) details = !msg->payload.userStats->stats_only; - + if (!msg->payload.userStats->has_session) sendPermissionDenied(client, "Not supported by uMurmur"); while (Client_iterate(&target) != NULL) { @@ -808,13 +808,13 @@ void Mh_handle_message(client_t *client, message_t *msg) } if (!target) /* Not found */ break; - + /* * Differences from Murmur: * o Ignoring certificates intentionally * o Ignoring channel local determining */ - + sendmsg = Msg_create(UserStats); sendmsg->payload.userStats->session = msg->payload.userStats->session; sendmsg->payload.userStats->from_client->has_good = true; @@ -825,7 +825,7 @@ void Mh_handle_message(client_t *client, message_t *msg) sendmsg->payload.userStats->from_client->lost = target->cryptState.uiLost; sendmsg->payload.userStats->from_client->has_resync = true; sendmsg->payload.userStats->from_client->resync = target->cryptState.uiResync; - + sendmsg->payload.userStats->from_server->has_good = true; sendmsg->payload.userStats->from_server->good = target->cryptState.uiRemoteGood; sendmsg->payload.userStats->from_server->has_late = true; @@ -841,14 +841,14 @@ void Mh_handle_message(client_t *client, message_t *msg) sendmsg->payload.userStats->udp_ping_avg = target->UDPPingAvg; sendmsg->payload.userStats->has_udp_ping_var = true; sendmsg->payload.userStats->udp_ping_var = target->UDPPingVar; - + sendmsg->payload.userStats->has_tcp_ping_avg = true; sendmsg->payload.userStats->tcp_ping_avg = target->TCPPingAvg; sendmsg->payload.userStats->has_tcp_ping_var = true; sendmsg->payload.userStats->tcp_ping_var = target->TCPPingVar; sendmsg->payload.userStats->has_tcp_packets = true; sendmsg->payload.userStats->tcp_packets = target->TCPPackets; - + if (details) { sendmsg->payload.userStats->version->has_version = true; @@ -859,7 +859,7 @@ void Mh_handle_message(client_t *client, message_t *msg) sendmsg->payload.userStats->version->os = strdup(target->os); if (target->os_version) sendmsg->payload.userStats->version->os_version = strdup(target->os_version); - + sendmsg->payload.userStats->n_celt_versions = target->codec_count; sendmsg->payload.userStats->celt_versions = malloc(sizeof(int32_t) * target->codec_count); if (!sendmsg->payload.userStats->celt_versions) @@ -885,13 +885,13 @@ void Mh_handle_message(client_t *client, message_t *msg) /* BW */ sendmsg->payload.userStats->has_bandwidth = true; sendmsg->payload.userStats->bandwidth = target->availableBandwidth; - + /* Onlinesecs */ sendmsg->payload.userStats->has_onlinesecs = true; - sendmsg->payload.userStats->onlinesecs = Timer_elapsed(&target->connectTime) / 1000000LL; + sendmsg->payload.userStats->onlinesecs = Timer_elapsed(&target->connectTime) / 1000000LL; /* Idlesecs */ sendmsg->payload.userStats->has_idlesecs = true; - sendmsg->payload.userStats->idlesecs = Timer_elapsed(&target->idleTime) / 1000000LL; + sendmsg->payload.userStats->idlesecs = Timer_elapsed(&target->idleTime) / 1000000LL; Client_send_message(client, sendmsg); } break; @@ -925,7 +925,7 @@ void Mh_handle_message(client_t *client, message_t *msg) } /* Re-use message */ Msg_inc_ref(msg); - + Client_send_message_except(NULL, msg); Client_close(target); break; @@ -949,7 +949,7 @@ void Mh_handle_message(client_t *client, message_t *msg) Ban_putBanList(msg, msg->payload.banList->n_bans); } break; - + /* Permission denied for all these messages. Not implemented. */ case ChannelRemove: case ContextAction: @@ -959,7 +959,7 @@ void Mh_handle_message(client_t *client, message_t *msg) case QueryUsers: sendPermissionDenied(client, "Not supported by uMurmur"); break; - + default: Log_warn("Message %d not handled", msg->messageType); break; @@ -967,7 +967,7 @@ void Mh_handle_message(client_t *client, message_t *msg) out: Msg_free(msg); return; - + disconnect: Msg_free(msg); Client_close(client); diff --git a/src/messages.c b/src/messages.c index 944dcba..d621d4e 100644 --- a/src/messages.c +++ b/src/messages.c @@ -44,21 +44,21 @@ static message_t *Msg_create_nopayload(messageType_t messageType); static void Msg_addPreamble(uint8_t *buffer, uint16_t type, uint32_t len) -{ +{ buffer[1] = (type) & 0xff; buffer[0] = (type >> 8) & 0xff; - + buffer[5] = (len) & 0xff; buffer[4] = (len >> 8) & 0xff; buffer[3] = (len >> 16) & 0xff; - buffer[2] = (len >> 24) & 0xff; + buffer[2] = (len >> 24) & 0xff; } static void Msg_getPreamble(uint8_t *buffer, int *type, int *len) { uint16_t msgType; uint32_t msgLen; - + msgType = buffer[1] | (buffer[0] << 8); msgLen = buffer[5] | (buffer[4] << 8) | (buffer[3] << 16) | (buffer[2] << 24); *type = (int)msgType; @@ -70,7 +70,7 @@ int Msg_messageToNetwork(message_t *msg, uint8_t *buffer) { int len; uint8_t *bufptr = buffer + PREAMBLE_SIZE; - + Log_debug("To net: msg type %d", msg->messageType); switch (msg->messageType) { case Version: @@ -227,7 +227,7 @@ int Msg_messageToNetwork(message_t *msg, uint8_t *buffer) mumble_proto__channel_remove__pack(msg->payload.channelRemove, bufptr); break; case UserStats: - { + { len = mumble_proto__user_stats__get_packed_size(msg->payload.userStats); if (len > MAX_MSGSIZE) { Log_warn("Too big tx message. Discarding"); @@ -281,7 +281,7 @@ message_t *Msg_create(messageType_t messageType) { message_t *msg = Msg_create_nopayload(messageType); int i; - + switch (messageType) { case Version: msg->payload.version = malloc(sizeof(MumbleProto__Version)); @@ -354,7 +354,7 @@ message_t *Msg_create(messageType_t messageType) case UserStats: msg->payload.userStats = malloc(sizeof(MumbleProto__UserStats)); mumble_proto__user_stats__init(msg->payload.userStats); - + msg->payload.userStats->from_client = malloc(sizeof(MumbleProto__UserStats__Stats)); mumble_proto__user_stats__stats__init(msg->payload.userStats->from_client); @@ -363,7 +363,7 @@ message_t *Msg_create(messageType_t messageType) msg->payload.userStats->version = malloc(sizeof(MumbleProto__Version)); mumble_proto__version__init(msg->payload.userStats->version); - + if (!msg->payload.userStats || !msg->payload.userStats->from_client || !msg->payload.userStats->from_server || !msg->payload.userStats->version) Log_fatal("Out of memory"); @@ -385,7 +385,7 @@ message_t *Msg_banList_create(int n_bans) { message_t *msg = Msg_create_nopayload(BanList); int i; - + msg->payload.banList = malloc(sizeof(MumbleProto__BanList)); if (msg->payload.banList == NULL) Log_fatal("Out of memory"); @@ -409,10 +409,10 @@ void Msg_banList_addEntry(message_t *msg, int index, uint8_t *address, uint32_t char *name, char *hash, char *reason, char *start, uint32_t duration) { MumbleProto__BanList__BanEntry *entry = msg->payload.banList->bans[index]; - + entry->address.data = malloc(16); if (!entry->address.data) - Log_fatal("Out of memory"); + Log_fatal("Out of memory"); memcpy(entry->address.data, address, 16); entry->address.len = 16; entry->mask = mask; @@ -421,8 +421,8 @@ void Msg_banList_addEntry(message_t *msg, int index, uint8_t *address, uint32_t entry->reason = strdup(reason); entry->start = strdup(start); if (!entry->name || !entry->hash || !entry->reason || !entry->start) - Log_fatal("Out of memory"); - + Log_fatal("Out of memory"); + if (duration > 0) { entry->duration = duration; entry->has_duration = true; @@ -456,7 +456,7 @@ void Msg_inc_ref(message_t *msg) void Msg_free(message_t *msg) { int i; - + if (msg->refcount) msg->refcount--; if (msg->refcount > 0) return; @@ -670,7 +670,7 @@ void Msg_free(message_t *msg) message_t *Msg_CreateVoiceMsg(uint8_t *data, int size) { message_t *msg = NULL; - + msg = Msg_create_nopayload(UDPTunnel); msg->unpacked = false; msg->payload.UDPTunnel = malloc(sizeof(struct _MumbleProto__UDPTunnel)); @@ -694,7 +694,7 @@ message_t *Msg_networkToMessage(uint8_t *data, int size) Log_debug("Message type %d size %d", messageType, msgLen); //dumpmsg(data, size); - + switch (messageType) { case Version: { @@ -869,7 +869,7 @@ message_t *Msg_networkToMessage(uint8_t *data, int size) break; } return msg; - + err_out: free(msg); return NULL; diff --git a/src/pds.c b/src/pds.c index 60c17e4..066666f 100644 --- a/src/pds.c +++ b/src/pds.c @@ -102,7 +102,7 @@ int Pds_skip(pds_t *pds, int offset) pds->bOk = false; return 0; } - + } static inline uint64_t next(pds_t *pds) @@ -135,9 +135,9 @@ void Pds_free(pds_t *pds) void Pds_add_double(pds_t *pds, double value) { double64u_t u; - + u.dval = value; - + Pds_add_numval(pds, u.u64); } @@ -150,7 +150,7 @@ double Pds_get_double(pds_t *pds) void Pds_add_numval(pds_t *pds, const uint64_t value) { uint64_t i = value; - + if ((i & 0x8000000000000000LL) && (~i < 0x100000000LL)) { // Signed number. i = ~i; @@ -200,12 +200,12 @@ void Pds_add_numval(pds_t *pds, const uint64_t value) append_val(pds, i & 0xFF); } } - + uint64_t Pds_get_numval(pds_t *pds) { uint64_t i = 0; uint64_t v = next(pds); - + if ((v & 0x80) == 0x00) { i=(v & 0x7F); } else if ((v & 0xC0) == 0x80) { diff --git a/src/server.c b/src/server.c index 7c7675b..05f5213 100644 --- a/src/server.c +++ b/src/server.c @@ -52,7 +52,7 @@ #define UDP_SOCK 1 /* globals */ -int udpsock; +int udpsock; bool_t shutdown_server; extern char *bindaddr; extern int bindport; @@ -67,7 +67,7 @@ void Server_run() etimer_t janitorTimer; unsigned short port; in_addr_t inet_address; - + /* max clients + listen sock + udp sock + client connecting that will be disconnected */ pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd)); if (pollfds == NULL) @@ -78,7 +78,7 @@ void Server_run() port = htons(bindport); else port = htons(getIntConf(BINDPORT)); - + if (bindaddr != NULL && inet_addr(bindaddr) != -1) inet_address = inet_addr(bindaddr); else if (inet_addr(getStrConf(BINDADDR)) != -1) @@ -86,7 +86,7 @@ void Server_run() else inet_address = inet_addr("0.0.0.0"); Log_info("Bind to %s:%hu", inet_address == 0 ? "*" : inet_ntoa(*((struct in_addr *)&inet_address)), ntohs(port)); - + /* Prepare TCP socket */ memset(&sin, 0, sizeof(sin)); tcpsock = socket(PF_INET, SOCK_STREAM, 0); @@ -95,15 +95,15 @@ void Server_run() if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0) Log_fatal("setsockopt: %s", strerror(errno)); sin.sin_family = AF_INET; - sin.sin_port = port; + sin.sin_port = port; sin.sin_addr.s_addr = inet_address; - + rc = bind(tcpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in)); if (rc < 0) Log_fatal("bind: %s", strerror(errno)); rc = listen(tcpsock, 3); if (rc < 0) Log_fatal("listen"); fcntl(tcpsock, F_SETFL, O_NONBLOCK); - + pollfds[LISTEN_SOCK].fd = tcpsock; pollfds[LISTEN_SOCK].events = POLLIN; @@ -113,7 +113,7 @@ void Server_run() sin.sin_family = AF_INET; sin.sin_port = port; sin.sin_addr.s_addr = inet_address; - + rc = bind(udpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in)); if (rc < 0) Log_fatal("bind %d %s: %s", getIntConf(BINDPORT), getStrConf(BINDADDR), strerror(errno)); @@ -125,26 +125,26 @@ void Server_run() rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val)); if (rc < 0) Log_warn("Server: Failed to set TOS for UDP Socket"); - + fcntl(udpsock, F_SETFL, O_NONBLOCK); pollfds[UDP_SOCK].fd = udpsock; pollfds[UDP_SOCK].events = POLLIN | POLLHUP | POLLERR; - + Timer_init(&janitorTimer); - + Log_info("uMurmur version %s ('%s') protocol version %d.%d.%d", UMURMUR_VERSION, UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH); Log_info("Visit http://code.google.com/p/umurmur/"); - + /* Main server loop */ while (!shutdown_server) { struct sockaddr_in remote; int i; - + pollfds[UDP_SOCK].revents = 0; pollfds[TCP_SOCK].revents = 0; clientcount = Client_getfds(&pollfds[2]); - + timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL; if (timeout <= 0) { Client_janitor(); @@ -188,7 +188,7 @@ void Server_run() Client_write_fd(pollfds[i + 2].fd); } } - } + } /* Disconnect clients */ Client_disconnect_all(); diff --git a/src/ssl.c b/src/ssl.c index 42385ab..62257d0 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -73,7 +73,7 @@ static inline int x509parse_keyfile(rsa_context *rsa, const char *path, { int ret; pk_context pk; - + pk_init(&pk); ret = pk_parse_keyfile(&pk, path, pwd); if (ret == 0 && !pk_can_do( &pk, POLARSSL_PK_RSA)) @@ -128,7 +128,7 @@ static void initTestCert() static void initTestKey() { int rc; - + rc = x509parse_key_rsa(&key, (unsigned char *)test_srv_key, strlen(test_srv_key), NULL, 0); if (rc != 0) @@ -145,7 +145,7 @@ static void initCert() { int rc; char *crtfile = (char *)getStrConf(CERTIFICATE); - + if (crtfile == NULL) { #ifdef USE_POLARSSL_TESTCERT Log_warn("No certificate file specified. Falling back to test certificate."); @@ -177,7 +177,7 @@ static void initKey() char *keyfile = (char *)getStrConf(KEY); if (keyfile == NULL) - Log_fatal("No key file specified"); + Log_fatal("No key file specified"); rc = x509parse_keyfile(&key, keyfile, NULL); if (rc != 0) Log_fatal("Could not read RSA key file %s", keyfile); @@ -187,7 +187,7 @@ static void initKey() int urandom_bytes(void *ctx, unsigned char *dest, size_t len) { int cur; - + while (len) { cur = read(urandom_fd, dest, len); if (cur < 0) @@ -208,7 +208,7 @@ static void pssl_debug(void *ctx, int level, const char *str) void SSLi_init(void) { char verstring[12]; - + initCert(); #ifdef USE_POLARSSL_TESTCERT if (builtInTestCertificate) { @@ -230,7 +230,7 @@ void SSLi_init(void) if (urandom_fd < 0) Log_fatal("Cannot open /dev/urandom"); #endif - + version_get_string(verstring); Log_info("PolarSSL library version %s initialized", verstring); } @@ -239,7 +239,7 @@ void SSLi_deinit(void) { #ifdef POLARSSL_API_V1_3_ABOVE x509_crt_free(&certificate); -#else +#else x509_free(&certificate); #endif rsa_free(&key); @@ -260,37 +260,37 @@ bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash) #endif if (!cert) { return false; - } + } sha1(cert->raw.p, cert->raw.len, hash); return true; } - + SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) { ssl_context *ssl; ssl_session *ssn; int rc; - + ssl = malloc(sizeof(ssl_context)); ssn = malloc(sizeof(ssl_session)); if (!ssl || !ssn) Log_fatal("Out of memory"); memset(ssl, 0, sizeof(ssl_context)); memset(ssn, 0, sizeof(ssl_session)); - + rc = ssl_init(ssl); if (rc != 0 ) Log_fatal("Failed to initialize: %d", rc); - - ssl_set_endpoint(ssl, SSL_IS_SERVER); + + ssl_set_endpoint(ssl, SSL_IS_SERVER); ssl_set_authmode(ssl, SSL_VERIFY_OPTIONAL); - + #ifdef USE_POLARSSL_HAVEGE ssl_set_rng(ssl, HAVEGE_RAND, &hs); #else ssl_set_rng(ssl, urandom_bytes, NULL); #endif - + ssl_set_dbg(ssl, pssl_debug, NULL); ssl_set_bio(ssl, net_recv, fd, net_send, fd); @@ -301,7 +301,7 @@ SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) #else ssl_set_session(ssl, 0, 0, ssn); #endif - + ssl_set_ca_chain(ssl, &certificate, NULL, NULL); #ifdef POLARSSL_API_V1_3_ABOVE ssl_set_own_cert_rsa(ssl, &certificate, &key); @@ -316,13 +316,13 @@ SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready) { int rc; - + rc = ssl_handshake(ssl); if (rc != 0) { if (rc == POLARSSL_ERR_NET_WANT_READ || rc == POLARSSL_ERR_NET_WANT_WRITE) { return 0; } else if (rc == POLARSSL_ERR_X509_CERT_VERIFY_FAILED) { /* Allow this (selfsigned etc) */ - return 0; + return 0; } else { Log_warn("SSL handshake failed: %d", rc); return -1; @@ -345,7 +345,7 @@ int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len) int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len) { int rc; - + rc = ssl_write(ssl, buf, len); if (rc == POLARSSL_ERR_NET_WANT_WRITE) return SSLI_ERROR_WANT_WRITE; @@ -391,7 +391,7 @@ static X509 *x509; static RSA *rsa; static SSL_CTX *context; static EVP_PKEY *pkey; - + static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx); static int SSL_add_ext(X509 * crt, int nid, char *value) { @@ -412,17 +412,17 @@ static X509 *SSL_readcert(char *certfile) { FILE *fp; X509 *x509; - + /* open the certificate file */ fp = fopen(certfile, "r"); if (fp == NULL) { Log_warn("Unable to open the X509 file %s for reading.", certfile); return NULL; } - - /* allocate memory for the cert structure */ + + /* allocate memory for the cert structure */ x509 = X509_new(); - + if (PEM_read_X509(fp, &x509, NULL, NULL) == 0) { /* error reading the x509 information - check the error stack */ Log_warn("Error trying to read X509 info."); @@ -445,13 +445,13 @@ static RSA *SSL_readprivatekey(char *keyfile) Log_warn("Unable to open the private key file %s for reading.", keyfile); return NULL; } - + /* allocate memory for the RSA structure */ rsa = RSA_new(); - + /* assign a callback function for the password */ - - /* read a private key from file */ + + /* read a private key from file */ if (PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL) <= 0) { /* error reading the key - check the error stack */ Log_warn("Error trying to read private key."); @@ -466,13 +466,13 @@ static RSA *SSL_readprivatekey(char *keyfile) static void SSL_writecert(char *certfile, X509 *x509) { FILE *fp; - + /* open the private key file */ fp = fopen(certfile, "w"); if (fp == NULL) { Log_warn("Unable to open the X509 file %s for writing", certfile); return; - } + } if (PEM_write_X509(fp, x509) == 0) { Log_warn("Error trying to write X509 info."); } @@ -482,14 +482,14 @@ static void SSL_writecert(char *certfile, X509 *x509) static void SSL_writekey(char *keyfile, RSA *rsa) { FILE *fp; - + /* open the private key file for reading */ fp = fopen(keyfile, "w"); if (fp == NULL) { Log_warn("Unable to open the private key file %s for writing.", keyfile); return; } - + if (PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL) == 0) { Log_warn("Error trying to write private key"); } @@ -499,7 +499,7 @@ static void SSL_writekey(char *keyfile, RSA *rsa) static void SSL_initializeCert() { char *crt, *key, *pass; - + crt = (char *)getStrConf(CERTIFICATE); key = (char *)getStrConf(KEY); pass = (char *)getStrConf(PASSPHRASE); @@ -509,12 +509,12 @@ static void SSL_initializeCert() { if (rsa != NULL) { pkey = EVP_PKEY_new(); EVP_PKEY_assign_RSA(pkey, rsa); - } + } + - #if 0 /* Later ... */ - if (key && !x509) { + if (key && !x509) { qscCert = QSslCertificate(key); if (! qscCert.isNull()) { logthis("Using certificate from key."); @@ -542,35 +542,35 @@ static void SSL_initializeCert() { } #endif - + if (!rsa || !x509) { Log_info("Generating new server certificate."); - + CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); - + x509 = X509_new(); pkey = EVP_PKEY_new(); rsa = RSA_generate_key(1024,RSA_F4,NULL,NULL); EVP_PKEY_assign_RSA(pkey, rsa); - + X509_set_version(x509, 2); ASN1_INTEGER_set(X509_get_serialNumber(x509),1); X509_gmtime_adj(X509_get_notBefore(x509),0); X509_gmtime_adj(X509_get_notAfter(x509),60*60*24*365); X509_set_pubkey(x509, pkey); - + X509_NAME *name=X509_get_subject_name(x509); - + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const uint8_t *)"Murmur Autogenerated Certificate v2", -1, -1, 0); X509_set_issuer_name(x509, name); SSL_add_ext(x509, NID_basic_constraints, "critical,CA:FALSE"); SSL_add_ext(x509, NID_ext_key_usage, "serverAuth,clientAuth"); SSL_add_ext(x509, NID_subject_key_identifier, "hash"); SSL_add_ext(x509, NID_netscape_comment, "Generated from umurmur"); - + X509_sign(x509, pkey, EVP_md5()); - + SSL_writecert(crt, x509); SSL_writekey(key, rsa); } @@ -585,7 +585,7 @@ void SSLi_init(void) STACK_OF(SSL_CIPHER) *cipherlist = NULL, *cipherlist_new = NULL; SSL_CIPHER *cipher; char *cipherstring, tempstring[128]; - + SSL_library_init(); OpenSSL_add_all_algorithms(); /* load & register all cryptos, etc. */ SSL_load_error_strings(); /* load all error messages */ @@ -597,13 +597,13 @@ void SSLi_init(void) ERR_print_errors_fp(stderr); abort(); } - + char* sslCAPath = getStrConf(CAPATH); if(sslCAPath != NULL) { SSL_CTX_load_verify_locations(context, NULL, sslCAPath); } - + SSL_initializeCert(); if (SSL_CTX_use_certificate(context, x509) <= 0) Log_fatal("Failed to initialize cert"); @@ -611,12 +611,12 @@ void SSLi_init(void) ERR_print_errors_fp(stderr); Log_fatal("Failed to initialize private key"); } - + /* Set cipher list */ - ssl = SSL_new(context); + ssl = SSL_new(context); cipherlist = (STACK_OF(SSL_CIPHER) *) SSL_get_ciphers(ssl); cipherlist_new = (STACK_OF(SSL_CIPHER) *) sk_SSL_CIPHER_new_null(); - + for ( i = 0; (cipher = sk_SSL_CIPHER_value(cipherlist, i)) != NULL; i++) { if (SSL_CIPHER_get_bits(cipher, NULL) >= 128) { sk_SSL_CIPHER_push(cipherlist_new, cipher); @@ -635,21 +635,21 @@ void SSLi_init(void) offset += sprintf(cipherstring + offset, "%s:", SSL_CIPHER_get_name(cipher)); } } - + if (cipherlist_new) sk_SSL_CIPHER_free(cipherlist_new); - + if (strlen(cipherstring) == 0) Log_fatal("No suitable ciphers found!"); - + if (SSL_CTX_set_cipher_list(context, cipherstring) == 0) Log_fatal("Failed to set cipher list!"); free(cipherstring); - + SSL_CTX_set_verify(context, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, - verify_callback); - + verify_callback); + SSL_free(ssl); Log_info("OpenSSL library initialized"); @@ -682,7 +682,7 @@ int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready) SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) { SSL *ssl; - + *SSLready = false; ssl = SSL_new(context); SSL_set_fd(ssl, *fd); @@ -699,12 +699,12 @@ bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash) X509 *x509; uint8_t *buf, *p; int len; - + x509 = SSL_get_peer_certificate(ssl); if (!x509) { return false; - } - + } + len = i2d_X509(x509, NULL); buf = malloc(len); if (buf == NULL) { @@ -712,13 +712,13 @@ bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash) } p = buf; - i2d_X509(x509, &p); - + i2d_X509(x509, &p); + SHA1(buf, len, hash); free(buf); return true; } - + void SSLi_closeconnection(SSL_handle_t *ssl) { SSL_free(ssl); @@ -760,14 +760,14 @@ static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) X509 *err_cert; int err, depth; SSL *ssl; - + err_cert = X509_STORE_CTX_get_current_cert(ctx); err = X509_STORE_CTX_get_error(ctx); depth = X509_STORE_CTX_get_error_depth(ctx); - + ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256); - + if (depth > 5) { preverify_ok = 0; err = X509_V_ERR_CERT_CHAIN_TOO_LONG; @@ -787,5 +787,5 @@ static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) } return 1; } - + #endif diff --git a/src/ssl.h b/src/ssl.h index ce9e480..f88e301 100644 --- a/src/ssl.h +++ b/src/ssl.h @@ -32,9 +32,7 @@ #ifndef SSL_H_987698 #define SSL_H_987698 -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #ifdef USE_POLARSSL #include @@ -130,7 +128,7 @@ static inline void SSLi_hex2hash(char *in, uint8_t *hash) int i, offset = 0; char byte[3]; int scanned; - + byte[2] = '\0'; for (i = 0; i < 20; i++) { memcpy(byte, &in[i * 2], 2); diff --git a/src/voicetarget.c b/src/voicetarget.c index e69e68c..e2ed569 100644 --- a/src/voicetarget.c +++ b/src/voicetarget.c @@ -38,7 +38,7 @@ void Voicetarget_add_session(client_t *client, int targetId, int sessionId) { struct dlist *itr; voicetarget_t *vt; - + list_iterate(itr, &client->voicetargets) { if (targetId == list_get_entry(itr, voicetarget_t, node)->id) { int i; @@ -59,7 +59,7 @@ void Voicetarget_add_channel(client_t *client, int targetId, int channelId, { struct dlist *itr; voicetarget_t *vt; - + list_iterate(itr, &client->voicetargets) { if (targetId == list_get_entry(itr, voicetarget_t, node)->id) { int i; @@ -74,14 +74,14 @@ void Voicetarget_add_channel(client_t *client, int targetId, int channelId, } } } - } + } } void Voicetarget_add_id(client_t *client, int targetId) { voicetarget_t *newtarget; int i; - + Voicetarget_del_id(client, targetId); newtarget = malloc(sizeof(voicetarget_t)); if (!newtarget)