From: Pawel Wieczorek Date: Fri, 16 Jan 2015 14:38:05 +0000 (+0100) Subject: Introduce ChecksumGenerator (chsgen) X-Git-Tag: accepted/tizen/common/20150310.163254~24 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F07%2F34007%2F9;p=platform%2Fcore%2Fsecurity%2Fcynara.git Introduce ChecksumGenerator (chsgen) A commandline-tool for computing checksums for Cynara's database contents. Change-Id: Iaddf799b84b82562734275991561ea0091852f2d --- diff --git a/CMakeLists.txt b/CMakeLists.txt index b6db3e7..116c14f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,7 @@ SET(TARGET_LIB_CREDS_SOCKET "cynara-creds-socket") SET(TARGET_LIB_SESSION "cynara-session") SET(TARGET_LIB_CYNARA_STORAGE "cynara-storage") SET(TARGET_CYAD "cyad") +SET(TARGET_CHSGEN "cynara-db-chsgen") ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(pkgconfig) diff --git a/packaging/cynara-db-chsgen.manifest b/packaging/cynara-db-chsgen.manifest new file mode 100644 index 0000000..a76fdba --- /dev/null +++ b/packaging/cynara-db-chsgen.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/cynara.spec b/packaging/cynara.spec index eea1b48..48d939a 100644 --- a/packaging/cynara.spec +++ b/packaging/cynara.spec @@ -18,6 +18,7 @@ Source1009: libcynara-creds-socket.manifest Source1010: libcynara-session.manifest Source1011: cynara-db-migration.manifest Source1012: cyad.manifest +Source1013: cynara-db-chsgen.manifest Requires: default-ac-domains Requires(pre): pwdutils Requires(pre): cynara-db-migration >= %{version}-%{release} @@ -171,6 +172,7 @@ cp -a %{SOURCE1009} . cp -a %{SOURCE1010} . cp -a %{SOURCE1011} . cp -a %{SOURCE1012} . +cp -a %{SOURCE1013} . cp -a test/db/db* . %build @@ -385,7 +387,9 @@ fi %files -n cynara-db-migration %manifest cynara-db-migration.manifest +%manifest cynara-db-chsgen.manifest %attr(700,root,root) %{_sbindir}/cynara-db-migration +%attr(700,root,root) %{_sbindir}/cynara-db-chsgen %files -n cyad %manifest cyad.manifest diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4bcf18c..1018e51 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved +# Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ ADD_SUBDIRECTORY(client) ADD_SUBDIRECTORY(client-async) ADD_SUBDIRECTORY(client-common) ADD_SUBDIRECTORY(cyad) +ADD_SUBDIRECTORY(chsgen) ADD_SUBDIRECTORY(admin) ADD_SUBDIRECTORY(agent) ADD_SUBDIRECTORY(storage) diff --git a/src/chsgen/CMakeLists.txt b/src/chsgen/CMakeLists.txt new file mode 100644 index 0000000..748d950 --- /dev/null +++ b/src/chsgen/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# @file CMakeLists.txt +# @author Pawel Wieczorek +# + +SET(CHSGEN_PATH ${CYNARA_PATH}/chsgen) + +SET(CHSGEN_SOURCES + ${CHSGEN_PATH}/ChecksumGenerator.cpp + ${CHSGEN_PATH}/main.cpp + ) + +INCLUDE_DIRECTORIES( + ${CYNARA_PATH} + ${CYNARA_PATH}/include + ) + +ADD_EXECUTABLE(${TARGET_CHSGEN} ${CHSGEN_SOURCES}) + +TARGET_LINK_LIBRARIES(${TARGET_CHSGEN} + ${TARGET_CYNARA_COMMON} + crypt + ) + +INSTALL(TARGETS ${TARGET_CHSGEN} DESTINATION ${SBIN_INSTALL_DIR}) diff --git a/src/chsgen/ChecksumGenerator.cpp b/src/chsgen/ChecksumGenerator.cpp new file mode 100644 index 0000000..6255403 --- /dev/null +++ b/src/chsgen/ChecksumGenerator.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/chsgen/ChecksumGenerator.cpp + * @author Pawel Wieczorek + * @version 1.0 + * @brief A micro-tool for computing checksums for Cynara's database contents + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include "ChecksumGenerator.h" + +namespace Cynara { + +namespace StorageConfig = PathConfig::StoragePath; + +const char ChecksumGenerator::m_fieldSeparator(StorageConfig::fieldSeparator); +const char ChecksumGenerator::m_recordSeparator(StorageConfig::recordSeparator); +const std::string ChecksumGenerator::m_backupFilenameSuffix(StorageConfig::backupFilenameSuffix); + +ChecksumGenerator::ChecksumGenerator(int argc, char * const *argv) : m_copyStream(std::string()) { + m_filename = (1 < argc ? argv[1] : std::string()); +} + +int ChecksumGenerator::run(void) { + try { + openFileStream(); + copyFileStream(); + printRecord(); + return CYNARA_API_SUCCESS; + } catch (const FileNotFoundException &ex) { + std::cerr << ex.message() << std::endl; + return CYNARA_API_INVALID_COMMANDLINE_PARAM; + } catch (const UnexpectedErrorException &ex) { + std::cerr << ex.message() << std::endl; + return CYNARA_API_UNKNOWN_ERROR; + } catch (const std::ios_base::failure &ex) { + std::cerr << ex.what() << std::endl; + return CYNARA_API_UNKNOWN_ERROR; + } catch (const std::length_error &ex) { + std::cerr << ex.what() << std::endl; + return CYNARA_API_UNKNOWN_ERROR; + } +} + +const std::string ChecksumGenerator::generate(const std::string &data) { + auto checksum = crypt(data.c_str(), "$1$"); + + if (nullptr != checksum) { + return std::string(checksum); + } else { + int err = errno; + if (err == ENOSYS) { + LOGE("'crypt' function was not implemented; error [%d] : <%s>", err, strerror(err)); + } else { + LOGE("'crypt' function error [%d] : <%s>", err, strerror(err)); + } + throw UnexpectedErrorException(err, strerror(err)); + } +}; + +void ChecksumGenerator::openFileStream(void) { + m_inputStream.open(m_filename); + + if (!m_inputStream.is_open()) { + throw FileNotFoundException(m_filename); + } +} + +void ChecksumGenerator::copyFileStream(void) { + std::copy(std::istreambuf_iterator(m_inputStream), + std::istreambuf_iterator(), + std::ostreambuf_iterator(m_copyStream)); + m_inputStream.seekg(0); +} + +void ChecksumGenerator::printRecord(void) const { + std::string filename(basename(m_filename.c_str())); + getBasicFilename(filename); + + std::cout << filename << m_fieldSeparator << generate(m_copyStream.str()) + << m_recordSeparator; +} + +void ChecksumGenerator::getBasicFilename(std::string &filename) const { + auto backupSuffixPos = filename.rfind(m_backupFilenameSuffix); + + if (std::string::npos != backupSuffixPos && + filename.size() == backupSuffixPos + m_backupFilenameSuffix.size()) { + filename.erase(backupSuffixPos); + } +} + +} /* namespace Cynara */ diff --git a/src/chsgen/ChecksumGenerator.h b/src/chsgen/ChecksumGenerator.h new file mode 100644 index 0000000..a3e96e0 --- /dev/null +++ b/src/chsgen/ChecksumGenerator.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/chsgen/ChecksumGenerator.h + * @author Pawel Wieczorek + * @version 1.0 + * @brief A micro-tool for computing checksums for Cynara's database contents + */ + +#ifndef SRC_CHSGEN_CHECKSUMGENERATOR_H_ +#define SRC_CHSGEN_CHECKSUMGENERATOR_H_ + +#include +#include +#include + +namespace Cynara { + +class ChecksumGenerator { +public: + ChecksumGenerator(int argc, char * const *argv); + ~ChecksumGenerator() {} + + int run(void); + +private: + static const std::string generate(const std::string &data); + + void openFileStream(void); + void copyFileStream(void); + void printRecord(void) const; + void getBasicFilename(std::string &filename) const; + + std::ifstream m_inputStream; + std::stringstream m_copyStream; + std::string m_filename; + static const char m_fieldSeparator; + static const char m_recordSeparator; + static const std::string m_backupFilenameSuffix; +}; + +} /* namespace Cynara */ + +#endif /* SRC_CHSGEN_CHECKSUMGENERATOR_H_ */ diff --git a/src/chsgen/main.cpp b/src/chsgen/main.cpp new file mode 100644 index 0000000..d91e889 --- /dev/null +++ b/src/chsgen/main.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/chsgen/main.cpp + * @author Pawel Wieczorek + * @version 1.0 + * @brief A micro-tool for computing checksums for Cynara's database contents + */ + +#include +#include + +#include + +#include + +int main(int argc, char **argv) { + if (2 != argc) { + std::cerr << "Invalid commandline parameters for chsgen" << std::endl; + return CYNARA_API_INVALID_COMMANDLINE_PARAM; + } + + try { + Cynara::ChecksumGenerator chsgen(argc, argv); + return chsgen.run(); + } catch (const std::bad_alloc &) { + std::cerr << "Chsgen could not allocate memory" << std::endl; + return CYNARA_API_OUT_OF_MEMORY; + } +}