2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <security/pam_ext.h>
19 #include <security/pam_modules.h>
25 #include "krate-guard.h"
26 #include "krate-builder.h"
28 #include <klay/exception.h>
29 #include <klay/filesystem.h>
30 #include <klay/xml/parser.h>
31 #include <klay/xml/document.h>
33 std::string buildKrateManifestPath(const std::string& name)
35 return CONF_PATH "/" + name + ".xml";
38 std::string getKrateName(pam_handle_t* handle)
41 int error = ::pam_get_item(handle, PAM_USER, &retItem);
42 if (error != PAM_SUCCESS) {
43 throw runtime::Exception("Failed to get user");
46 return static_cast<const char*>(retItem);
49 void openKrateSession(const std::string& name)
51 auto sessionBuilder = [](const runtime::User& user) {
52 KrateBuilder builder(user, buildKrateManifestPath(user.getName()));
53 builder.containerize();
56 createSession(runtime::User(name), sessionBuilder);
59 void closeKrateSession(const std::string& name)
61 destroySession(runtime::User(name));
65 PAM_EXTERN __attribute__((visibility("default")))
66 int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
69 std::string name = getKrateName(pamh);
70 KrateGuard krateGuard(name);
73 openKrateSession(name);
74 } catch (runtime::Exception& e) {
75 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
76 return PAM_SESSION_ERR;
82 PAM_EXTERN __attribute__((visibility("default")))
83 int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
86 std::string name = getKrateName(pamh);
87 KrateGuard krateGuard(name);
90 closeKrateSession(name);
91 } catch (runtime::Exception& e) {
92 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
93 return PAM_SESSION_ERR;
99 #ifdef PAM_MODULE_ENTRY
100 PAM_MODULE_ENTRY("pam_krate");