Move PAM module, volume manager 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 #define KRATE_MANIFEST_DIR CONF_PATH "/krate/"
34
35 std::string buildKrateManifestPath(const std::string& name)
36 {
37         return KRATE_MANIFEST_DIR + name + ".xml";
38 }
39
40 std::string getKrateName(pam_handle_t* handle)
41 {
42         const void* retItem;
43         int error = ::pam_get_item(handle, PAM_USER, &retItem);
44         if (error != PAM_SUCCESS) {
45                 throw runtime::Exception("Failed to get user");
46         }
47
48         return static_cast<const char*>(retItem);
49 }
50
51 void openKrateSession(const std::string& name)
52 {
53         auto sessionBuilder = [](const runtime::User& user) {
54                 KrateBuilder builder(user, buildKrateManifestPath(user.getName()));
55                 builder.containerize();
56         };
57
58         createSession(runtime::User(name), sessionBuilder);
59 }
60
61 void closeKrateSession(const std::string& name)
62 {
63         destroySession(runtime::User(name));
64 }
65
66 extern "C" {
67 PAM_EXTERN  __attribute__((visibility("default")))
68 int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
69 {
70         try {
71                 std::string name = getKrateName(pamh);
72                 KrateGuard krateGuard(name);
73                 krateGuard.wait();
74
75                 openKrateSession(name);
76         } catch (runtime::Exception& e) {
77                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
78                 return PAM_SESSION_ERR;
79         }
80
81         return PAM_SUCCESS;
82 }
83
84 PAM_EXTERN  __attribute__((visibility("default")))
85 int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
86 {
87         try {
88                 std::string name = getKrateName(pamh);
89                 KrateGuard krateGuard(name);
90                 krateGuard.wait();
91
92                 closeKrateSession(name);
93         } catch (runtime::Exception& e) {
94                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
95                 return PAM_SESSION_ERR;
96         }
97
98         return PAM_SUCCESS;
99 }
100
101 #ifdef PAM_MODULE_ENTRY
102 PAM_MODULE_ENTRY("pam_krate");
103 #endif
104
105 }