BuildRequires: pkgconfig(capi-base-common)
BuildRequires: pkgconfig(capi-ui-efl-util)
BuildRequires: pkgconfig(capi-system-info)
+BuildRequires: pkgconfig(libsmack)
BuildRequires: coregl
BuildRequires: edje-bin
PKG_CHECK_MODULES(ASKUSER_NOTIFICATION_LIB_DEP
REQUIRED
libsystemd
+ libsmack
)
INCLUDE_DIRECTORIES(SYSTEM ${ASKUSER_NOTIFICATION_LIB_DEP_INCLUDE_DIRS})
${ASKUSER_NOTIFICATION_LIB_PATH}/ask-user-channel.cpp
${ASKUSER_NOTIFICATION_LIB_PATH}/ask-user-client-channel.cpp
${ASKUSER_NOTIFICATION_LIB_PATH}/ask-user-server-channel.cpp
+ ${ASKUSER_NOTIFICATION_LIB_PATH}/credentials.cpp
${ASKUSER_NOTIFICATION_LIB_PATH}/sock.cpp
${ASKUSER_NOTIFICATION_LIB_PATH}/ask-user-config.cpp
)
}
void ServerChannel::onAccept(int fd) {
- m_callbacks->newConnection(fd, Credentials());
+ m_callbacks->newConnection(fd, Credentials(fd));
m_callbacks->updateConnection(fd, FdMask::READ);
}
#include <askuser-notification/ask-user-types.h>
#include <askuser-notification/ask-user-channel.h>
+#include <askuser-notification/credentials.h>
namespace AskUser {
namespace Protocol {
WRITE = 2,
};
-struct Credentials {
- std::string label;
- uid_t uid;
-};
-
typedef int ConnectionFd;
typedef int RequestId;
typedef std::string Privilege;
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/**
+ * @file CredentialsException.h
+ * @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @brief The declaration of CredentialsException.
+ */
+
+#pragma once
+
+#include <exception>
+#include <string>
+
+namespace AskUser {
+
+namespace Protocol {
+
+class CredentialsException : public std::exception
+{
+public:
+ CredentialsException(const std::string &msg) : m_msg(msg) {
+ }
+
+ virtual const char* what() const noexcept {
+ return m_msg.c_str();
+ }
+
+private:
+ std::string m_msg;
+};
+
+} // namespace Protocol
+
+} // namespace AskUser
+
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/**
+ * @file credentials.h
+ * @autor Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @brief The declaration of Credentials.
+ */
+
+#pragma once
+
+#include <string>
+
+namespace AskUser {
+
+namespace Protocol {
+
+struct Credentials {
+ Credentials(int sockFd);
+
+ std::string label;
+ std::string uid;
+};
+
+} // namespace Protocol
+
+} // namespace AskUser
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/**
+ * @file Credentials.cpp
+ * @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @brief The implementation of Credentials.
+ */
+
+#include <memory>
+#include <stdlib.h>
+#include <sys/smack.h>
+#include <sys/socket.h>
+
+#include <askuser-notification/credentials-exception.h>
+
+#include <askuser-notification/credentials.h>
+
+namespace {
+
+std::string getUIDFromSocket(int sockFd)
+{
+ struct ucred cr;
+ socklen_t len = sizeof(cr);
+
+ if (getsockopt(sockFd, SOL_SOCKET, SO_PEERCRED, &cr, &len) == -1) {
+ throw AskUser::Protocol::CredentialsException("Couldn't fetch credentials from a socket");
+ }
+
+ return std::to_string(cr.uid);
+}
+
+std::string getSmackLabelFromSocket(int sockFd)
+{
+ char *label;
+ ssize_t labelLen = smack_new_label_from_socket(sockFd, &label);
+ if (labelLen <= 0) {
+ throw AskUser::Protocol::CredentialsException("Couldn't fetch a smack label from a socket");
+ }
+ std::unique_ptr<char, decltype(free)*> labelPtr(label, free);
+ return std::string(labelPtr.get(), labelLen);
+}
+
+} // namespace
+
+namespace AskUser {
+
+namespace Protocol {
+
+Credentials::Credentials(int sockFd)
+: label(getSmackLabelFromSocket(sockFd))
+, uid(getUIDFromSocket(sockFd))
+{
+}
+
+} // namespace Protocol
+
+} // namespace AskUser
ServerCallbacks() : m_channel(nullptr) {}
virtual void newConnection(ConnectionFd fd, const Credentials &creds) {
- printf("call newConnection fd: %d credentials = { label: %s uid: %d }\n", fd,
- creds.label.c_str(), creds.uid);
+ printf("call newConnection fd: %d credentials = { label: %s uid: %s }\n", fd,
+ creds.label.c_str(), creds.uid.c_str());
}
virtual void updateConnection(ConnectionFd fd, int mask) {
std::string appId, pkgId;
identifyApp(creds.label, appId, pkgId);
- ConnectionInfo connInfo{appId, pkgId, std::to_string(creds.uid)};
+ ConnectionInfo connInfo{appId, pkgId, creds.uid};
m_connToInfo.insert(it, std::make_pair(fd, connInfo));
}