Merge "Match schema file version to db version" into tizen
[platform/core/security/key-manager.git] / src / manager / main / key-manager-main.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file        ckm-manager-main.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Implementation of central key manager
21  */
22 #include <stdlib.h>
23 #include <signal.h>
24
25 #include <openssl/ssl.h>
26 #include <openssl/conf.h>
27 #include <openssl/err.h>
28 #include <openssl/evp.h>
29
30 #include <dpl/log/log.h>
31 #include <dpl/singleton.h>
32
33 #include <socket-manager.h>
34
35 #include <ckm-service.h>
36 #include <ocsp-service.h>
37 #include <encryption-service.h>
38
39 #include <key-provider.h>
40 #include <file-system.h>
41
42 #define REGISTER_SOCKET_SERVICE(manager, service) \
43     registerSocketService<service>(manager, #service)
44
45 template<typename T>
46 void registerSocketService(CKM::SocketManager &manager, const std::string& serviceName)
47 {
48     T *service = NULL;
49     try {
50         service = new T();
51         service->Start();
52         manager.RegisterSocketService(service);
53         service = NULL;
54     } catch (const CKM::Exception &exception) {
55         LogError("Error in creating service " << serviceName <<
56                  ", details:\n" << exception.DumpToString());
57     } catch (const std::exception& e) {
58         LogError("Error in creating service " << serviceName <<
59                  ", details:\n" << e.what());
60     } catch (...) {
61         LogError("Error in creating service " << serviceName <<
62                  ", unknown exception occured");
63     }
64     if (service)
65         delete service;
66 }
67
68 int main(void) {
69     UNHANDLED_EXCEPTION_HANDLER_BEGIN
70     {
71         CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM");
72
73         int retCode = CKM::FileSystem::init();
74         if (retCode) {
75             LogError("Fatal error in FileSystem::init()");
76             return 1;
77         }
78
79         CKM::FileLock fl = CKM::FileSystem::lock();
80
81         sigset_t mask;
82         sigemptyset(&mask);
83         sigaddset(&mask, SIGTERM);
84         sigaddset(&mask, SIGPIPE);
85         if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
86             LogError("Error in pthread_sigmask");
87             return 1;
88         }
89         LogInfo("Init external libraries SKMM and openssl");
90
91         SSL_load_error_strings();
92         SSL_library_init();
93         OpenSSL_add_all_ciphers();
94         OPENSSL_config(NULL);
95
96         CKM::KeyProvider::initializeLibrary();
97
98         {
99             LogInfo("Start!");
100             CKM::SocketManager manager;
101
102             REGISTER_SOCKET_SERVICE(manager, CKM::CKMService);
103             REGISTER_SOCKET_SERVICE(manager, CKM::OCSPService);
104             REGISTER_SOCKET_SERVICE(manager, CKM::EncryptionService);
105
106             manager.MainLoop();
107         }
108         // Manager has been destroyed and we may close external libraries.
109         LogInfo("Deinit SKMM and openssl");
110         CKM::KeyProvider::closeLibrary();
111         // Deinit OPENSSL ?
112         EVP_cleanup();
113         ERR_free_strings();
114     }
115     catch (const std::runtime_error& e)
116     {
117         LogError(e.what());
118     }
119     UNHANDLED_EXCEPTION_HANDLER_END
120     return 0;
121 }
122