Add krate-mount service
[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 <dlfcn.h>
18 #include <syslog.h>
19 #include <unistd.h>
20 #include <security/pam_ext.h>
21 #include <security/pam_modules.h>
22
23 #include <string>
24 #include <vector>
25 #include <iostream>
26
27 #include "krate-builder.h"
28
29 #include <klay/exception.h>
30 #include <klay/filesystem.h>
31
32 #define KRATE_UID_MIN 6000
33 #define KRATE_UID_MAX 6999
34
35 namespace {
36
37 static std::string getFlagFilePath(runtime::User &user) {
38         return "/run/user/" + std::to_string(user.getUid()) + "/.container";
39 }
40
41 } // namespace
42
43 std::string getKrateName(pam_handle_t* handle)
44 {
45         const void* retItem;
46         int error = ::pam_get_item(handle, PAM_USER, &retItem);
47         if (error != PAM_SUCCESS) {
48                 throw runtime::Exception("Failed to get user");
49         }
50
51         return static_cast<const char*>(retItem);
52 }
53
54 extern "C" {
55 PAM_EXTERN  __attribute__((visibility("default")))
56 int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
57 {
58         try {
59                 runtime::User user(getKrateName(pamh));
60
61                 KrateBuilder builder(user);
62                 builder.enterKrate();
63
64                 if (user.getUid() >= KRATE_UID_MIN && user.getUid() <= KRATE_UID_MAX ) {
65                         runtime::File flag(getFlagFilePath(user));
66                         if (!flag.exists())
67                                 flag.create(0644);
68                 }
69         } catch (runtime::Exception& e) {
70                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
71                 return PAM_SESSION_ERR;
72         }
73
74         return PAM_SUCCESS;
75 }
76
77 PAM_EXTERN  __attribute__((visibility("default")))
78 int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
79 {
80         try {
81                 runtime::User user(getKrateName(pamh));
82
83                 KrateBuilder builder(user);
84                 builder.exitKrate();
85
86                 runtime::File flag(getFlagFilePath(user));
87                 if (flag.exists())
88                         flag.remove(false);
89         } catch (runtime::Exception& e) {
90                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
91                 return PAM_SESSION_ERR;
92         }
93
94         return PAM_SUCCESS;
95 }
96
97 #ifdef PAM_MODULE_ENTRY
98 PAM_MODULE_ENTRY("pam_krate");
99 #endif
100
101 }