Skip to main content

cpp_admin_acl_get.cpp

/*****************************************************************************************
*
* cpp_admin_acl_get.cpp
*
* Note: See Tricryption Engine SDK for details on how to set the project environment.
*
******************************************************************************************/

#include<string>
#include<iostream>
#include "teagent.h"
#include "teconnection.h"
#include "teexception.h"
#include "teauthuserctx.h"
#include "teobject.h"
#include "teadmin.h"

using namespace ERUCES;
using namespace std;

string generateHiddenLink(string strServer, long nEncPort, string strUser, string strPswd);


int main()
{
string strServer = "localhost";
long nAdminPort = 8888;
long nEncPort = 8888;
string strAdminUser = "ls";
string strAdminPswd = "password1A";

try
{
// Set the hidden link for which the ACLInfo will be got.
string strHL = generateHiddenLink(strServer, nEncPort, strAdminUser, strAdminPswd);

// Connect to server
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection);

auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strAdminUser);
ctx->setPasswd(strAdminPswd);

conn->open(strServer, nAdminPort, *ctx.operator->());
cout << "Connected to " << strServer << " as " << strAdminUser << endl;

TEAdmin admin;
admin.attachConnection(conn.operator->());

TEACLInfo acl;
admin.getACL(strHL, acl, true);

cout << "ACLInfo: " << endl;
cout << "\tACLID: " << acl.getACLId() << endl;
cout << "\tExpiration Flag: " << acl.getKeyExpirationFlag() << endl;
cout << "\tNumber of ACL entries: " << acl.EntryCount() << endl;

TEObjectContainer cont;
acl.getEntries(cont);
for (int i=0; i < cont.size(); ++i)
{
TEACLEntry *ent = (TEACLEntry*)cont[i];
cout << endl;
cout << "\tACLID: " << ent->getACLId() << endl;
cout << "\tACLRight: " << ent->getACLRight() << endl;
cout << "\tCreationTimestamp: " << ent->getCreationTimestamp() << endl;
cout << "\tEndTimestamp: " << ent->getEndTimestamp() << endl;
cout << "\tMaxUsage: " << ent->getMaxUsage() << endl;
cout << "\tPrincipalId: " << ent->getPrincipalId() << endl;
cout << "\tStartTimestamp: " << ent->getStartTimestamp() << endl;
cout << "\tSystemID: " << ent->getSystemID() << endl;
cout << "\tUserRight: " << ent->getUserRight() << endl;
}

admin.detachConnection();

// Disconnect from server
conn->close();
cout << "Disconnected from " << strServer << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages().c_str() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}

return 0;
}



string generateHiddenLink(string strServer, long nEncPort, string strUser, string strPswd)
{
string strHiddenLink = "";

auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection);

// Open connection to the server
conn->open(strServer, nEncPort, strUser, strPswd);

cout << "Connected to " << strServer << " at " << nEncPort
<< " as " << strUser << "\n" << endl;

// Create TEAgentMatrix object
TEAgentMatrix matrix;

// Set matrix for encryption
matrix.setElement(1, 1, "dummy data for creating hiddenlink"); // set element (1, 1)

// Encrypt matrix
matrix.attachConnection(conn.operator->()); // Attach connection
matrix.encrypt();
matrix.detachConnection(); // Detach connection

// Get hiddenlink
matrix.setEncodeMode(true);

matrix.getHiddenLink(1, strHiddenLink); // hiddenlink for first row

cout << "HiddenLink: " << strHiddenLink << endl;

// Close conneciton
conn->close();
cout << "Disconnected from " << strServer << endl;

return strHiddenLink;
}