Move krate service and library from device-policy-manager git
[platform/core/security/krate.git] / module / krate.cpp
1 /*
2  *  Copyright (c) 2015 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 #include <syslog.h>
18 #include <security/pam_ext.h>
19 #include <security/pam_modules.h>
20
21 #include <string>
22 #include <vector>
23
24 #include "session.h"
25 #include "krate-guard.h"
26 #include "krate-builder.h"
27
28 #include <klay/exception.h>
29 #include <klay/filesystem.h>
30 #include <klay/xml/parser.h>
31 #include <klay/xml/document.h>
32
33 std::string buildKrateManifestPath(const std::string& name)
34 {
35         return CONF_PATH "/" + name + ".xml";
36 }
37
38 std::string getKrateName(pam_handle_t* handle)
39 {
40         const void* retItem;
41         int error = ::pam_get_item(handle, PAM_USER, &retItem);
42         if (error != PAM_SUCCESS) {
43                 throw runtime::Exception("Failed to get user");
44         }
45
46         return static_cast<const char*>(retItem);
47 }
48
49 void openKrateSession(const std::string& name)
50 {
51         auto sessionBuilder = [](const runtime::User& user) {
52                 KrateBuilder builder(user, buildKrateManifestPath(user.getName()));
53                 builder.containerize();
54         };
55
56         createSession(runtime::User(name), sessionBuilder);
57 }
58
59 void closeKrateSession(const std::string& name)
60 {
61         destroySession(runtime::User(name));
62 }
63
64 extern "C" {
65 PAM_EXTERN  __attribute__((visibility("default")))
66 int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
67 {
68         try {
69                 std::string name = getKrateName(pamh);
70                 KrateGuard krateGuard(name);
71                 krateGuard.wait();
72
73                 openKrateSession(name);
74         } catch (runtime::Exception& e) {
75                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
76                 return PAM_SESSION_ERR;
77         }
78
79         return PAM_SUCCESS;
80 }
81
82 PAM_EXTERN  __attribute__((visibility("default")))
83 int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
84 {
85         try {
86                 std::string name = getKrateName(pamh);
87                 KrateGuard krateGuard(name);
88                 krateGuard.wait();
89
90                 closeKrateSession(name);
91         } catch (runtime::Exception& e) {
92                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
93                 return PAM_SESSION_ERR;
94         }
95
96         return PAM_SUCCESS;
97 }
98
99 #ifdef PAM_MODULE_ENTRY
100 PAM_MODULE_ENTRY("pam_krate");
101 #endif
102
103 }