Add fetching credentials from socket fd 31/136431/22
authorPiotr Sawicki <p.sawicki2@partner.samsung.com>
Thu, 29 Jun 2017 09:19:56 +0000 (11:19 +0200)
committerPiotr Sawicki <p.sawicki2@partner.samsung.com>
Thu, 6 Jul 2017 16:54:02 +0000 (18:54 +0200)
Change-Id: I5adc2b5e01b1747c86846eb3c1a565b7643892cc

packaging/askuser-notification.spec
src/ipc-lib/CMakeLists.txt
src/ipc-lib/ask-user-server-channel.cpp
src/ipc-lib/askuser-notification/ask-user-server-channel.h
src/ipc-lib/askuser-notification/ask-user-types.h
src/ipc-lib/askuser-notification/credentials-exception.h [new file with mode: 0644]
src/ipc-lib/askuser-notification/credentials.h [new file with mode: 0644]
src/ipc-lib/credentials.cpp [new file with mode: 0644]
src/ipc-lib/test/main.cpp
src/notification-daemon/Logic.cpp

index 99e71a68ffa941b961ba597f8d2c8512d350e843..f76aa0d4c5549508f2769c368f414236c90a53f2 100644 (file)
@@ -28,6 +28,7 @@ BuildRequires: pkgconfig(vconf)
 BuildRequires: pkgconfig(capi-base-common)
 BuildRequires: pkgconfig(capi-ui-efl-util)
 BuildRequires: pkgconfig(capi-system-info)
+BuildRequires: pkgconfig(libsmack)
 BuildRequires: coregl
 BuildRequires: edje-bin
 
index 78899a4a2198f785ab87c2702dc9ffb18784a8ae..e89543457992d5a14ee95f4e94d1e47091ce75f2 100644 (file)
@@ -21,6 +21,7 @@ SET(ASKUSER_NOTIFICATION_LIB_PATH ${ASKUSER_PATH}/ipc-lib)
 PKG_CHECK_MODULES(ASKUSER_NOTIFICATION_LIB_DEP
     REQUIRED
     libsystemd
+    libsmack
     )
 
 INCLUDE_DIRECTORIES(SYSTEM ${ASKUSER_NOTIFICATION_LIB_DEP_INCLUDE_DIRS})
@@ -33,6 +34,7 @@ SET(ASKUSER_NOTIFICATION_LIB_SOURCES
     ${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
    )
index ba72edbc40f27902d4116e2fe3d0a75de71f3a2c..e58d184572250bff15eb181c71e378f59a54a88f 100644 (file)
@@ -69,7 +69,7 @@ void ServerChannel::popupResponse(ConnectionFd fd, RequestId id, int response) {
 }
 
 void ServerChannel::onAccept(int fd) {
-    m_callbacks->newConnection(fd, Credentials());
+    m_callbacks->newConnection(fd, Credentials(fd));
     m_callbacks->updateConnection(fd, FdMask::READ);
 }
 
index c5987cef71ec91016377e6a9d2a001dcb49418ab..2d04c44a65903127692564959388589348a4e836 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <askuser-notification/ask-user-types.h>
 #include <askuser-notification/ask-user-channel.h>
+#include <askuser-notification/credentials.h>
 
 namespace AskUser {
 namespace Protocol {
index 71ab50caba710ed74823e78b21453d2d2e9a9d6d..a8af9b1c537e5a7582e0f1db2e28053bddb03fe8 100644 (file)
@@ -39,11 +39,6 @@ enum FdMask {
     WRITE = 2,
 };
 
-struct Credentials {
-    std::string label;
-    uid_t uid;
-};
-
 typedef int ConnectionFd;
 typedef int RequestId;
 typedef std::string Privilege;
diff --git a/src/ipc-lib/askuser-notification/credentials-exception.h b/src/ipc-lib/askuser-notification/credentials-exception.h
new file mode 100644 (file)
index 0000000..5c38c58
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  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
+
diff --git a/src/ipc-lib/askuser-notification/credentials.h b/src/ipc-lib/askuser-notification/credentials.h
new file mode 100644 (file)
index 0000000..6fc3307
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *  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
diff --git a/src/ipc-lib/credentials.cpp b/src/ipc-lib/credentials.cpp
new file mode 100644 (file)
index 0000000..9418f29
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ *  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
index 48f1457f25b09c4a3f75d3d40a281aaac5710546..644d6f67046ebb0ccfb68ca91dcb64cdfdadfd12 100644 (file)
@@ -39,8 +39,8 @@ struct ServerCallbacks : public IServerCallbacks {
     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) {
index ffd95a0487c8c582b29c11f498af72ed6133cc20..fa4478522103d11b588467d046d81a5485905b50 100644 (file)
@@ -48,7 +48,7 @@ void Logic::addChannelFd(Protocol::ConnectionFd fd, const Protocol::Credentials
 
     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));
 }