This commit is a preparation for new API with many privileges as input.
Change-Id: I611348de78fa6231eab52c9f8069c957c4aa14a3
Signed-off-by: Ernest Borowski <e.borowski@partner.samsung.com>
/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* @file privacy_privilege_manager.c
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Tomasz Swierczek <t.swierczek@samsung.com>
* @brief This file contains test of CAPI for Privacy Privilege Manager
*/
}
}
- virtual void popup(ConnectionFd fd, RequestId id, Privilege &&privilege) {
- printf("\npopup request fd: %d requestId: %d privilege: \"%s\"\n", fd, id, privilege.c_str());
- addRequest(fd, id, privilege);
+ virtual void popup(ConnectionFd fd, RequestId id, PrivilegeVector &&privileges) {
+ printf("\npopup request fd: %d requestId: %d privilege: \"%s\"\n", fd, id, privileges[0].c_str());
+ addRequest(fd, id, privileges[0]);
printPrompt(Mode::SERVER);
}
}
if (m_channel) {
- m_channel->popupResponse(fd, id, response);
+ m_channel->popupResponses(fd, id, {response});
// delete request
s.erase(reqIt);
void respondToAllRequests() {
for (const auto &reqSet : m_requests) {
for (const auto &req : reqSet.second) {
- m_channel->popupResponse(reqSet.first, req.m_id, PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_ONCE);
+ m_channel->popupResponses(reqSet.first, req.m_id, {PRIVACY_PRIVILEGE_MANAGER_REQUEST_RESULT_DENY_ONCE});
}
}
* @file ApiInterfaceImpl.cpp
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
* @author Zofia Grzelewska <z.abramowska@samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The definition of ApiInterfaceImpl.
*/
const askuser_popup_response_callback callback,
void *userData)
{
- Protocol::ConnectionContext conCtx = m_channel->popupRequest(privilege);
+ std::vector<std::string> privileges({privilege});
+ Protocol::ConnectionContext conCtx = m_channel->popupRequest(std::move(privileges));
auto sameRequest = [&] (const Request &req) {
return req.m_requestId == conCtx.m_requestId;
/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* @file ClientCallbacks.cpp
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The definition of ClientCallbacks.
*/
m_api->updateConnection(fd, mask);
}
-void ClientCallbacks::popupResponse(Protocol::RequestId id, int response)
+void ClientCallbacks::popupResponses(Protocol::RequestId id, const std::vector<int> &responses)
{
- m_api->popupResponse(id, response);
+ m_api->popupResponse(id, responses[0]);
}
} // namespace Client
/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* @file ClientCallbacks.h
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The declaration of ClientCallbacks.
*/
#pragma once
#include <client-channel.h>
+#include <vector>
#include "ApiInterfaceImpl.h"
{}
virtual void updateConnection(Protocol::ConnectionFd fd, int mask);
- virtual void popupResponse(Protocol::RequestId id, int response);
+ virtual void popupResponses(Protocol::RequestId id, const std::vector<int> &responses);
private:
ApiInterfaceImpl *m_api;
* @file client-channel.cpp
* @author Bartlomiej Grzelewski <b.grzelewski@samsung.com>
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The implementation of ClientChannel.
*/
return requestId++;
}
-Protocol::ConnectionContext ClientChannel::popupRequest(const std::string &privilege) {
+Protocol::ConnectionContext ClientChannel::popupRequest(const std::vector<std::string> &privileges) {
int fd = connect(); // clients use only one connection per request
std::stringstream ss;
RequestId requestId = generateRequestId();
- ss << MSGID_POPUP << " " << base64Encode(privilege) << " " << requestId << '\n';
+ ss << MSGID_POPUP << " " << requestId << " " << privileges.size() << " ";
+ for (auto &privilege : privileges)
+ ss << base64Encode(privilege) << " ";
+
+ ss << '\n';
std::string str = ss.str();
+
+ ALOGD("popupRequests: sending message: " << str);
+
std::copy(str.begin(), str.end(), std::back_inserter(m_sockets[fd].output));
m_callbacks->updateConnection(fd, FdMask::READ | FdMask::WRITE);
return -EINVAL;
}
- int command = std::stoi(message[0]);
+ int command = std::stoi(message[ASKUSER_MESSAGE_CMD_POS]);
switch (command) {
case MSGID_POPUP_RESPONSE:
{
- if (message.size() != 3) {
+ unsigned int responsesCount;
+ if (message.size() < ASKUSER_MESSAGE_MIN_MSG_PARAM_COUNT ||
+ message.size() != (responsesCount = std::stoul(message[ASKUSER_MESSAGE_PRIVILEGES_COUNT_POS])) +
+ ASKUSER_MESSAGE_MIN_MSG_PARAM_COUNT - 1) {
ALOGE("Inappropriate message size for MSGID_POPUP_RESPONSE command, size: " << message.size());
return -EINVAL;
}
+ RequestId id = std::stoi(message[ASKUSER_MESSAGE_REQUESTID_POS]);
+ std::vector<int> responses;
+ responses.reserve(responsesCount);
- RequestId id = std::stoi(message[1]);
- int response = std::stoi(message[2]);
+ std::transform(std::next(message.begin(), ASKUSER_MESSAGE_FIRST_PRIVILEGE_POS), message.end(),
+ std::back_inserter(responses),
+ [](const std::string &response) -> int { return std::stoi(response); });
- m_callbacks->popupResponse(id, response);
+ m_callbacks->popupResponses(id, responses);
// after receiving the response just close the connection
closeConnection(fd);
* @file client-channel.h
* @author Bartlomiej Grzelewski <b.grzelewski@samsung.com>
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The declaration of IClientCallbacks and ClientChannel.
*/
#pragma once
virtual void updateConnection(ConnectionFd fd, int mask) = 0;
/**
- * This function is called when popup response is received.
+ * This function is called when popup responses are received.
*
* \param[in] id Request identifier
- * \param[in] response Response from popup
+ * \param[in] responses Response from popup
*/
- virtual void popupResponse(RequestId id, int response) = 0;
+ virtual void popupResponses(RequestId id, const std::vector<int> &responses) = 0;
};
typedef std::unique_ptr<IClientCallbacks> ClientCallbacksPtr;
ClientChannel(ClientCallbacksPtr ptr);
virtual ~ClientChannel();
- virtual ConnectionContext popupRequest(const std::string &privilege);
+ virtual ConnectionContext popupRequest(const std::vector<std::string> &privileges);
private:
virtual void onAccept(int fd);
/*
- * Copyright (c) 2017 Samsung Electronics Co.
+ * Copyright (c) 2017 - 2018 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.
/**
* @file message-utils.h
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Tomasz Swierczek <t.swierczek@samsung.com>
* @brief The declaration of message handling helpers.
*/
const std::size_t MIN_COMMAND_LENGTH = 1;
const std::size_t MAX_COMMAND_LENGTH = maxNumberOfChars<int>();
+const std::size_t MIN_PRIVS_NUMBER_LENGTH = 1;
+const std::size_t MAX_PRIVS_NUMBER_LENGTH = maxNumberOfChars<int>();
+const std::size_t MIN_PRIVS_NUMBER = 1;
+const std::size_t MAX_PRIVS_NUMBER = 100;
const std::size_t MIN_REQUEST_ID_LENGTH = 1;
const std::size_t MAX_REQUEST_ID_LENGTH = maxNumberOfChars<RequestId>();
const std::size_t MIN_PRIVILEGE_LENGTH = 1;
SEP_LEN + // space
MIN_REQUEST_ID_LENGTH +
SEP_LEN + // space
- MIN_PRIVILEGE_ENC_LENGTH +
- SEP_LEN; // new line
+ MIN_PRIVS_NUMBER_LENGTH +
+ MIN_PRIVS_NUMBER * (
+ SEP_LEN + // space
+ MIN_PRIVILEGE_ENC_LENGTH) +
+ SEP_LEN; // new line
const std::size_t MAX_REQUEST_MESSAGE_LENGTH = MAX_COMMAND_LENGTH +
SEP_LEN + // space
MAX_REQUEST_ID_LENGTH +
SEP_LEN + // space
- MAX_PRIVILEGE_ENC_LENGTH +
+ MAX_PRIVS_NUMBER_LENGTH +
+ MAX_PRIVS_NUMBER * (
+ SEP_LEN + // space
+ MAX_PRIVILEGE_ENC_LENGTH) +
SEP_LEN; // new line
const std::size_t MIN_RESPONSE_MESSAGE_LENGTH = MIN_COMMAND_LENGTH +
SEP_LEN + // space
MIN_REQUEST_ID_LENGTH +
SEP_LEN + // space
- MIN_RESPONSE_STATUS_LEN +
+ MIN_PRIVS_NUMBER_LENGTH +
+ MIN_PRIVS_NUMBER * (
+ SEP_LEN + // space
+ MIN_RESPONSE_STATUS_LEN) +
SEP_LEN; // new line
const std::size_t MAX_RESPONSE_MESSAGE_LENGTH = MAX_COMMAND_LENGTH +
SEP_LEN + // space
MAX_REQUEST_ID_LENGTH +
SEP_LEN + // space
- MAX_RESPONSE_STATUS_LEN +
+ MAX_PRIVS_NUMBER_LENGTH +
+ MAX_PRIVS_NUMBER * (
+ SEP_LEN + // space
+ MAX_RESPONSE_STATUS_LEN) +
SEP_LEN; // new line
const std::size_t MIN_MESSAGE_LENGTH = std::min(MIN_REQUEST_MESSAGE_LENGTH, MIN_RESPONSE_MESSAGE_LENGTH);
const std::size_t MAX_MESSAGE_LENGTH = std::max(MAX_REQUEST_MESSAGE_LENGTH, MAX_RESPONSE_MESSAGE_LENGTH);
+const unsigned int ASKUSER_MESSAGE_CMD_POS = 0;
+const unsigned int ASKUSER_MESSAGE_REQUESTID_POS = 1;
+const unsigned int ASKUSER_MESSAGE_PRIVILEGES_COUNT_POS = 2;
+const unsigned int ASKUSER_MESSAGE_FIRST_PRIVILEGE_POS = 3;
+const unsigned int ASKUSER_MESSAGE_MIN_MSG_PARAM_COUNT = 4; //cmd, requestId, privilegeCount, privilege
+
std::string base64Encode(std::string input);
std::string base64Decode(std::string input);
/*
- * Copyright (c) 2017 Samsung Electronics Co.
+ * Copyright (c) 2017 - 2018 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.
* @file server-channel.cpp
* @author Bartlomiej Grzelewski <b.grzelewski@samsung.com>
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The implementation of ServerChannel.
*/
m_callbacks->updateConnection(fd, FdMask::READ);
}
-void ServerChannel::popupResponse(ConnectionFd fd, RequestId id, int response) {
+void ServerChannel::popupResponses(ConnectionFd fd, RequestId id, std::vector<int> &&responses) {
try {
auto it = m_sockets.find(fd);
if (it == m_sockets.end()) {
auto &desc = it->second;
std::stringstream ss;
- ss << MSGID_POPUP_RESPONSE << " " << id << " " << response << '\n';
+ ss << MSGID_POPUP_RESPONSE << " " << id << " " << responses.size();
+ for (const auto &response: responses) {
+ ss << " " << response;
+ }
+ ss << '\n';
std::string o = ss.str();
+
+ ALOGD("popupResponse: sending message: " << o);
+
std::copy(o.begin(), o.end(), std::back_inserter(desc.output));
m_callbacks->updateConnection(fd, FdMask::READ | FdMask::WRITE);
return -EINVAL;
}
- int command = std::stoi(message[0]);
+ int command = std::stoi(message[ASKUSER_MESSAGE_CMD_POS]);
switch (command) {
case MSGID_POPUP:
{
- if (message.size() != 3) {
+ unsigned int privilegesCount;
+ if (message.size() < ASKUSER_MESSAGE_MIN_MSG_PARAM_COUNT ||
+ message.size() != (privilegesCount = std::stoul(message[ASKUSER_MESSAGE_PRIVILEGES_COUNT_POS])) +
+ ASKUSER_MESSAGE_MIN_MSG_PARAM_COUNT - 1) {
ALOGE("Inappropriate message size for MSGID_POPUP command, size: " << message.size());
return -EINVAL;
}
+ ALOGD("privilegesCount:" << privilegesCount);
+ std::vector<std::string> privileges;
+ privileges.reserve(privilegesCount);
+
+ std::transform(std::next(message.begin(), ASKUSER_MESSAGE_FIRST_PRIVILEGE_POS), message.end(),
+ std::back_inserter(privileges),
+ [](const std::string &priv) -> std::string {return base64Decode(priv); });
+
+ RequestId requestId = std::stoi(message[ASKUSER_MESSAGE_REQUESTID_POS]);
- std::string privilege = base64Decode(message[1]);
- RequestId requestId = std::stoi(message[2]);
+ m_callbacks->popup(fd, requestId, std::move(privileges));
- m_callbacks->popup(fd, requestId, std::move(privilege));
break;
}
default :
/*
- * Copyright (c) 2017 Samsung Electronics Co.
+ * Copyright (c) 2017 - 2018 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.
* @file server-channel.h
* @author Bartlomiej Grzelewski <b.grzelewski@samsung.com>
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief The declaration of IServerCallbacks and ServerChannel.
*/
#pragma once
*
* \param[in] fd Connection file descriptor
* \param[in] id Request identifier
- * \param[in] privilege Privilege for which permission is asked for
+ * \param[in] privileges Privileges for which permissions are asked for
*/
- virtual void popup(ConnectionFd fd, RequestId id, std::string &&privilege) = 0;
+ virtual void popup(ConnectionFd fd, RequestId id, std::vector<std::string> &&privileges) = 0;
};
typedef std::unique_ptr<IServerCallbacks> ServerCallbacksPtr;
ServerChannel(ServerCallbacksPtr ptr);
virtual ~ServerChannel();
- virtual void popupResponse(ConnectionFd fd, RequestId id, int response);
+ virtual void popupResponses(ConnectionFd fd, RequestId id, std::vector<int> &&responses);
private:
virtual void onAccept(int fd);
* @file main.cpp
* @author Bartlomiej Grzelewski <b.grzelewski@samsung.com>
* @author Piotr Sawicki <p.sawicki2@partner.samsung.com>
+ * @author Tomasz Swierczek <t.swierczek@samsung.com>
* @brief
*/
#include <exception>
m_sockets[fd] = mask;
}
- virtual void popup(ConnectionFd fd, RequestId id, Privilege &&priv) {
- printf("call popup %s \n", priv.c_str());
- if (m_channel)
- m_channel->popupResponse(fd, id, responseCounter++ % (ASKUSER_ALLOW_FOREVER + 1));
+ virtual void popup(ConnectionFd fd, RequestId id, PrivilegeVector &&priv) {
+ printf("call popup %s \n", priv[0].c_str());
+ if (m_channel) {
+ m_channel->popupResponses(fd, id, { responseCounter++ % (ASKUSER_ALLOW_FOREVER + 1) });
+ }
}
void setChannel(ServerChannel *ptr) {
m_sockets[fd] = mask;
}
- virtual void popupResponse(RequestId id, int response) {
- printf("response from popup id: %d response: %d\n", id, response);
+ virtual void popupResponses(RequestId id, const std::vector<int> &responses) {
+ if (responses.empty()) {
+ printf("Response from popup id: %d is empty\n", id);
+ return;
+ }
+ printf("response from popup id: %d response: %d\n", id, responses[0]);
}
void setChannel(ClientChannel *ptr) {
printf("Invalid argument\n");
break;
}
- chan->popupRequest(privilege);
+ chan->popupRequest({privilege});
}
break;
default:
/**
* @file Logic.cpp
* @author Zofia Grzelewska <z.abramowska@samsung.com>
+ * @author Tomasz Swierczek <t.swierczek@samsung.com>
* @brief Declaration of Logic class
*/
ALOGD("Privilege policy level calculated to : " << policyLevel);
if (policyLevel == "Allow") {
- m_serverChannel->popupResponse(fd, id, ASKUSER_ALLOW_FOREVER);
+ m_serverChannel->popupResponses(fd, id, {ASKUSER_ALLOW_FOREVER});
return;
}
if (policyLevel == "Deny") {
- m_serverChannel->popupResponse(fd, id, ASKUSER_DENY_FOREVER);
+ m_serverChannel->popupResponses(fd, id, {ASKUSER_DENY_FOREVER});
return;
}
if (policyLevel != "Ask user") {
ALOGE("Unknown policy set : " << policyLevel << " for (" << conn.appId << ", " << conn.user
<< ", " << privilege << ")");
- m_serverChannel->popupResponse(fd, id, ASKUSER_DENY_ONCE);
+ m_serverChannel->popupResponses(fd, id, {ASKUSER_DENY_ONCE});
return;
}
if (privacies.empty()) {
ALOGE("All privacies for privilege " << privilege
<< " are already allowed");
- m_serverChannel->popupResponse(fd, id, ASKUSER_ALLOW_FOREVER);
+ m_serverChannel->popupResponses(fd, id, {ASKUSER_ALLOW_FOREVER});
return;
}
processEvents();
} catch (const std::exception &e) {
ALOGE("Failed to handle popup request : " << e.what());
- m_serverChannel->popupResponse(fd, id, ASKUSER_DENY_ONCE);
+ m_serverChannel->popupResponses(fd, id, {ASKUSER_DENY_ONCE});
}
}
void Logic::processResponse(const ConnectionInfo &conn, int clientResponse, const Policy &level) {
if (!setPolicy(conn, level)) {
- m_serverChannel->popupResponse(m_currentEvent.fd, m_currentEvent.id, ASKUSER_UNKNOWN_ERROR);
+ m_serverChannel->popupResponses(m_currentEvent.fd, m_currentEvent.id, {ASKUSER_UNKNOWN_ERROR});
} else {
- m_serverChannel->popupResponse(m_currentEvent.fd, m_currentEvent.id, clientResponse);
+ m_serverChannel->popupResponses(m_currentEvent.fd, m_currentEvent.id, {clientResponse});
}
}
/*
- * Copyright (c) 2017 Samsung Electronics Co.
+ * Copyright (c) 2017 - 2018 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.
/**
* @file src/agent/notification-daemon/ServerCallbacks.cpp
* @author Zofia Abramowska <z.abramowska@samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief Implementation ServerCallbacks classes
*/
#include <unistd.h>
m_service->updateChannelFd(fd, maskToFlags(mask));
}
-void ServerCallbacks::popup(ConnectionFd fd, RequestId id, std::string &&privilege) {
- m_service->popup(fd, id, privilege);
+void ServerCallbacks::popup(ConnectionFd fd, RequestId id, std::vector<std::string> &&privileges) {
+ m_service->popup(fd, id, privileges[0]);
}
} /* namespace Notification */
/*
- * Copyright (c) 2017 Samsung Electronics Co.
+ * Copyright (c) 2017 - 2018 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.
/**
* @file src/agent/notification-daemon/ServerCallbacks.h
* @author Zofia Abramowska <z.abramowska@samsung.com>
+ * @author Ernest Borowski <e.borowski@partner.samsung.com>
* @brief Declaration of ServerCallbacks classes
*/
#pragma once
#include <memory>
+#include <vector>
#include <server-channel.h>
{}
virtual void newConnection(ConnectionFd fd, const Credentials &creds);
virtual void updateConnection(ConnectionFd fd, int mask);
- virtual void popup(ConnectionFd fd, RequestId id, std::string &&privilege);
+ virtual void popup(ConnectionFd fd, RequestId id, std::vector<std::string> &&privileges);
virtual ~ServerCallbacks() {}
private: