namespace capmgr {
-struct GDBusCredentials : public SecurityManager::Credentials {
- public:
- GDBusCredentials() { init_ = false; }
- GDBusCredentials(int pid, const char* user, const char* smack_label);
- ~GDBusCredentials();
- bool Init(const gchar* sender, GDBusConnection* conn);
- private:
- gchar* sender_;
- GDBusConnection* conn_;
-};
-
-GDBusCredentials::GDBusCredentials(int pid, const char* user,
- const char* smack_label) {
- pid_ = pid;
- init_ = false;
- smack_label_ = strdup(smack_label);
- if (!smack_label_) {
- return;
- }
-
- user_ = strdup(user);
- if (!user_) {
- free(smack_label_);
- smack_label_ = nullptr;
- return;
- }
-}
-
SecurityManager::Credentials GetCredentials(
GDBusConnection* conn, const gchar* sender) {
int ret = 0;
ret = cynara_creds_gdbus_get_user(conn, sender, USER_METHOD_DEFAULT, &user);
if (ret != CYNARA_API_SUCCESS) {
LOG(ERROR) << "cynara_creds_gdbus_get_user() failed: " << ret;
- return GDBusCredentials();
+ return SecurityManager::Credentials();
}
ret = cynara_creds_gdbus_get_client(conn, sender, CLIENT_METHOD_DEFAULT,
&client);
if (ret != CYNARA_API_SUCCESS) {
LOG(ERROR) << "cynara_creds_gdbus_get_client() failed: " << ret;
- return GDBusCredentials();
+ return SecurityManager::Credentials();
}
ret = cynara_creds_gdbus_get_pid(conn, sender, &pid);
if (ret != CYNARA_API_SUCCESS) {
LOG(ERROR) << "cynara_creds_gdbus_get_pid() failed: " << ret;
- return GDBusCredentials();
+ return SecurityManager::Credentials();
}
- GDBusCredentials c = GDBusCredentials(pid, client, user);
- if (!c.Init(sender, conn))
- return GDBusCredentials();
-
- return c;
-}
-
-GDBusCredentials::~GDBusCredentials() {
- if (smack_label_)
- free(smack_label_);
-
- if (user_)
- free(user_);
-
- if (sender_)
- g_free(sender_);
-
- if (conn_)
- g_object_unref(conn_);
-}
-
-bool GDBusCredentials::Init(const gchar* sender, GDBusConnection* conn) {
- sender_ = g_strdup(sender);
- if (!sender_) {
- init_ = false;
- return false;
- }
-
- conn_ = reinterpret_cast<GDBusConnection*>(g_object_ref(conn));
- init_ = true;
-
- return true;
+ return SecurityManager::Credentials(pid, client, user);
}
DBusService::DBusService() {
}
SecurityManager::Credentials c = GetCredentials(connection, sender);
+ if (!c.IsInit()) {
+ LOG(ERROR) << "Failed to create credentials";
+ return false;
+ }
+
if (!SecurityManager::CheckFilePermission(file_path_str, c)) {
LOG(ERROR) << "File permission error: " << file_path_str;
g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", -1));