/****************************************************************************
** (c) Copyright IBM Corp. 2007 All rights reserved.
** 
** The following sample of source code ("Sample") is owned by International 
** Business Machines Corporation or one of its subsidiaries ("IBM") and is 
** copyrighted and licensed, not sold. You may use, copy, modify, and 
** distribute the Sample in any form without payment to IBM, for the purpose of 
** assisting you in the development of your applications.
** 
** The Sample code is provided to you on an "AS IS" basis, without warranty of 
** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR 
** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do 
** not allow for the exclusion or limitation of implied warranties, so the above 
** limitations or exclusions may not apply to you. IBM shall not be liable for 
** any damages you suffer as a result of using, copying, modifying or 
** distributing the Sample, even if IBM has been advised of the possibility of 
** such damages.
*****************************************************************************
**
** SOURCE FILE NAME: inauth.sqC 
**    
** SAMPLE: How to display authorities at instance level
**
**         This program ends in ".sqC" even though it does not contain 
**         embedded SQL statements. It links in  the embedded SQL utility
**         file for database connection and disconnection, so it needs the 
**         embedded SQL�extension for the precompiler.
**           
** DB2 APIs USED:
**         db2CfgGet -- GET DATABASE MANAGER CONFIGURATION
**         sqluadau -- GET AUTHORIZATIONS
**
**                           
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing embedded SQL applications see the Developing Embedded SQL Applications book.
**
** For information on DB2 APIs, see the Administrative API Reference.
**
** For the latest information on programming, compiling, and running DB2
** applications, visit the DB2 Information Center at
**     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/

#include <string.h>
#include <sqlutil.h>
#include <db2ApiDf.h>
#include <sqlenv.h>
#include "utilemb.h"
#if ((__cplusplus >= 199711L) && !defined DB2HP && !defined DB2AIX) || \
    (DB2LINUX && (__LP64__ || (__GNUC__ >= 3)) )
   #include <iostream>
   using namespace std; 
#else
   #include <iostream.h>
#endif

class InAuth
{
  public:
    int AuthorityGroupsAtInstanceLevelDisplay();
    int CurrentUserAuthoritiesAtInstanceLevelDisplay();
};

int InAuth::AuthorityGroupsAtInstanceLevelDisplay()
{
  struct sqlca sqlca;
  db2CfgParam cfgParameters[3];
  db2Cfg cfgStruct;

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE DB2 API:" << endl;
  cout << "  db2CfgGet -- GET CONFIGURATION" << endl;
  cout << "TO DISPLAY AUTHORITY GROUPS AT INSTANCE LEVEL:" << endl;

  // initialize cfgParameters
  cfgParameters[0].flags = 0;
  cfgParameters[0].token = SQLF_KTN_SYSADM_GROUP;
  cfgParameters[0].ptrvalue = new char[15 + 1];
  cfgParameters[1].flags = 0;
  cfgParameters[1].token = SQLF_KTN_SYSCTRL_GROUP;
  cfgParameters[1].ptrvalue = new char[15 + 1];
  cfgParameters[2].flags = 0;
  cfgParameters[2].token = SQLF_KTN_SYSMAINT_GROUP;
  cfgParameters[2].ptrvalue = new char[15 + 1];

  // get three DBM Config. Parameteres
  strcpy(cfgParameters[0].ptrvalue, "");
  strcpy(cfgParameters[1].ptrvalue, "");
  strcpy(cfgParameters[2].ptrvalue, "");

  // initialize cfgStruct
  cfgStruct.numItems = 3;
  cfgStruct.paramArray = cfgParameters;
  cfgStruct.flags = db2CfgDatabaseManager | db2CfgDelayed;
  cfgStruct.dbname = NULL;

  // get database manager configuration
  db2CfgGet(db2Version970, (void *)&cfgStruct, &sqlca);
  DB2_API_CHECK("DBM Config. -- Get");

  cout << endl;
  cout << "  SYSADM_GROUP   = " << cfgParameters[0].ptrvalue << endl;
  cout << "  SYSCTRL_GROUP  = " << cfgParameters[1].ptrvalue << endl;
  cout << "  SYSMAINT_GROUP = " << cfgParameters[2].ptrvalue << endl;

  // free the memory allocated
  delete [] cfgParameters[0].ptrvalue;
  delete [] cfgParameters[1].ptrvalue;
  delete [] cfgParameters[2].ptrvalue;

  return 0;
} //InAuth::AuthorityGroupsAtInstanceLevelDisplay

int InAuth::CurrentUserAuthoritiesAtInstanceLevelDisplay()
{
  struct sqlca sqlca;
  struct sql_authorizations currentUserAuthorities;

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE DB2 API:" << endl;
  cout << "  sqluadau -- Get Authorizations" << endl;
  cout << "TO DISPLAY CURRENT USER AUTHORITIES AT INSTANCE LEVEL:" << endl;

  currentUserAuthorities.sql_authorizations_len = SQL_AUTHORIZATION_SIZE;

  // get current user authorizations
  sqluadau(&currentUserAuthorities, &sqlca);
  DB2_API_CHECK("current user authorities -- get");

  cout << "\n  User SYSADM authority    = "
       << (currentUserAuthorities.sql_sysadm_auth == 1 ? "YES" : "NO");
  cout << "\n  User SYSCTRL authority   = "
       << (currentUserAuthorities.sql_sysctrl_auth == 1 ? "YES" : "NO");
  cout << "\n  User SYSMAINT authority  = "
       << (currentUserAuthorities.sql_sysmaint_auth == 1 ? "YES" : "NO");
  cout << endl;

  cout << "\n  Group SYSADM authority   = "
       << (currentUserAuthorities.sql_sysadm_grp_auth == 1 ? "YES" : "NO");
  cout << "\n  Group SYSCTRL authority  = "
       << (currentUserAuthorities.sql_sysctrl_grp_auth == 1 ? "YES" : "NO");
  cout << "\n  Group SYSMAINT authority = "
       << (currentUserAuthorities.sql_sysmaint_grp_auth == 1 ? "YES" : "NO")
       << endl;

  return 0;
} //InAuth::CurrentUserAuthoritiesAtInstanceLevelDisplay

int main(int argc, char *argv[])
{
  int rc = 0;
  CmdLineArgs check;
  InAuth auth;
  Instance inst;
  DbEmb db;

  // check the command line arguments
  rc = check.CmdLineArgsCheck3(argc, argv, db, inst);
  if (rc != 0)
  {
    return rc;
  }

  cout << "\nTHIS SAMPLE SHOWS HOW TO DISPLAY AUTHORITIES AT INSTANCE LEVEL"
       << endl;

  // attach to a local or remote instance
  rc = inst.Attach();
  if (rc != 0)
  {
    return rc;
  }

  rc = auth.AuthorityGroupsAtInstanceLevelDisplay();

  // detach from the local or remote instance
  rc = inst.Detach();
  if (rc != 0)
  {
    return rc;
  }

  // connect to database
  rc = db.Connect();
  if (rc != 0)
  {
    return rc;
  }

  rc = auth.CurrentUserAuthoritiesAtInstanceLevelDisplay();

  // disconnect from database
  rc = db.Disconnect();
  if (rc != 0)
  {
    return rc;
  }

  return 0;
} //main