#include<string>
#include<iostream>
#include<stdexcept>
#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 strOwnerUser = "ls";
string strOwnerPswd = "password1A";
string strGranteeUser = "bob";
try
{
string strHL = generateHiddenLink(strServer, nEncPort, strOwnerUser, strOwnerPswd);
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection);
auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strOwnerUser);
ctx->setPasswd(strOwnerPswd);
conn->open(strServer, nAdminPort, *ctx.operator->());
cout << "Connected to " << strServer << " as " << strOwnerUser << 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;
long lSystemID;
int i=0;
TEObjectContainer cont;
acl.getEntries(cont);
for (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;
if (i==0)
lSystemID = ent->getSystemID();
}
TEUserAndPasswordInfo grantee;
grantee.setUserName(strGranteeUser);
if (!admin.getPrincipalByName(grantee))
throw runtime_error("grantee principal not found: " + strGranteeUser);
long lPrincipalID = (long)grantee.getPrincipalId();
TEACLEntry ent2add;
ent2add.setACLId( acl.getACLId() );
ent2add.setACLRight(RIGHT_READ | RIGHT_WRITE);
ent2add.setEndTimestamp(0);
ent2add.setMaxUsage(0);
ent2add.setPrincipalId(lPrincipalID);
ent2add.setStartTimestamp(0);
ent2add.setSystemID(lSystemID);
ent2add.setUserRight(1);
acl.clearEntry();
acl.addEntry( ent2add );
admin.updateACL(ACLENTRY_ADD, acl);
cout << endl;
cout << "A new entry was added to ACLInfo. " << endl;
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;
acl.getEntries(cont);
for (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;
if ( ent->getPrincipalId() == lPrincipalID )
{
if (ent->getUserRight() == 0)
{
ent->setUserRight( 1 );
}
else
{
ent->setUserRight( 0 );
}
}
}
admin.updateACL(ACLENTRY_UPDATE, acl);
cout << endl;
cout << "ACLInfo was updated. " << endl;
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;
acl.getEntries(cont);
for ( 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();
conn->close();
cout << "Disconnected from " << strServer << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages().c_str() << endl;
}
catch (exception &e)
{
cout << e.what() << 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);
conn->open(strServer, nEncPort, strUser, strPswd);
cout << "Connected to " << strServer << " at " << nEncPort
<< " as " << strUser << "\n" << endl;
TEAgentMatrix matrix;
matrix.setElement(1, 1, "dummy data for creating hiddenlink");
matrix.attachConnection(conn.operator->());
matrix.encrypt();
matrix.detachConnection();
matrix.setEncodeMode(true);
matrix.getHiddenLink(1, strHiddenLink);
cout << "HiddenLink: " << strHiddenLink << endl;
conn->close();
cout << "Disconnected from " << strServer << endl;
return strHiddenLink;
}