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
20 #include <security/pam_ext.h>
21 #include <security/pam_modules.h>
27 #include "krate-builder.h"
29 #include <klay/exception.h>
30 #include <klay/filesystem.h>
32 #define KRATE_UID_MIN 6000
33 #define KRATE_UID_MAX 6999
37 static std::string getFlagFilePath(runtime::User &user) {
38 return "/run/user/" + std::to_string(user.getUid()) + "/.container";
43 std::string getKrateName(pam_handle_t* handle)
46 int error = ::pam_get_item(handle, PAM_USER, &retItem);
47 if (error != PAM_SUCCESS) {
48 throw runtime::Exception("Failed to get user");
51 return static_cast<const char*>(retItem);
55 PAM_EXTERN __attribute__((visibility("default")))
56 int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
59 runtime::User user(getKrateName(pamh));
61 KrateBuilder builder(user);
64 if (user.getUid() >= KRATE_UID_MIN && user.getUid() <= KRATE_UID_MAX ) {
65 runtime::File flag(getFlagFilePath(user));
69 } catch (runtime::Exception& e) {
70 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
71 return PAM_SESSION_ERR;
77 PAM_EXTERN __attribute__((visibility("default")))
78 int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
81 runtime::User user(getKrateName(pamh));
83 KrateBuilder builder(user);
86 runtime::File flag(getFlagFilePath(user));
89 } catch (runtime::Exception& e) {
90 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
91 return PAM_SESSION_ERR;
97 #ifdef PAM_MODULE_ENTRY
98 PAM_MODULE_ENTRY("pam_krate");