//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/a4ad0b48-dd95-46b6-8289-721e99c8dc76/mgmt_api/ldap/c/main.c

//Warning: This code has been marked up for HTML

class='cKeyword'>#if defined(WIN32)

   /*
   ** Windows specific includes.
   */
   class='cKeyword'>#include<windows.h>

class='cKeyword'>#elif defined(N_PLAT_NLM)

   /*
   ** NetWare NLM specific includes.
   */

   class='cKeyword'>#include <nwthread.h>
   class='cKeyword'>#include <string.h>
   
class='cKeyword'>#define printf ConsolePrintf

class='cKeyword'>#elif defined(UNIX)

   /*
   ** Unix specific includes.
   */
   
   class='cKeyword'>#include <ldap.h>
   class='cKeyword'>#include <lber.h>
class='cKeyword'>#endif

class='cKeyword'>#include <stdio.h>
class='cKeyword'>#include <ntypes.h>
class='cKeyword'>#include <nmaserr.h>
class='cKeyword'>#include <nmasext.h>
class='cKeyword'>#include <ldap_ssl.h>

class='cKeyword'>#define MAX_DN_CHARS         256
class='cKeyword'>#define MAX_TREE_NAME_CHARS    32

/* ******************************************************************* */
void findAnObject(LDAP *ld, class='cKeyword'>char *queryID)
{
   const class='cKeyword'>char *apszAttributes[] =
   {
         "mail",
         "cn",
         NULL
   };

   int err;
    class='cKeyword'>char searchBase[] = ""; // serach the entire tree
    struct timeval timeOut;
    LDAPMessage *searchResult;

    timeOut.tv_sec    = 10L;
    timeOut.tv_usec   = 0L;

    /* Search the directory */
    err = ldap_search_ext_s(  
                    ld,                    /* LDAP session handle */
                    searchBase,            /* container to search */
                    LDAP_SCOPE_SUBTREE,    /* search scope */
                    queryID,            /* search filter */
                    apszAttributes,        /* return specified attributes */
                    0,                     /* return attributes and values */
                    NULL,                  /* server controls */
                    NULL,                  /* client controls */
                    &timeOut,              /* time out */
                    LDAP_NO_LIMIT,         /* no size limit */
                    &searchResult );       /* returned results */

    ldap_msgfree( searchResult );
}

/* ******************************************************************* */
main(int argc, class='cKeyword'>char **argv)
{
   int err = 0;

   LDAP *ld = NULL;
   int LDAPVersion = LDAP_VERSION3;
   int resetCur = 0;
   class='cKeyword'>char polcDN[MAX_DN_CHARS +1];
   size_t polcDNLen = sizeof(polcDN);
   class='cKeyword'>char subjName[MAX_DN_CHARS +1];
   class='cKeyword'>char subjPwd[512];
   class='cKeyword'>char userName[MAX_DN_CHARS +1];
   class='cKeyword'>char userPwd[512];
   class='cKeyword'>char chgPwd[]="This is the changed password";
   class='cKeyword'>char newPwd[] ="THE NEW PASSWORD";
   class='cKeyword'>char curPwd[512];
   class='cKeyword'>char getPwd[512];
   size_t getPwdLen = sizeof(getPwd);
   nuint32 mid = 0;
   nuint32 midLen = sizeof(mid);
   class='cKeyword'>char tag[] = "BIG BUFFER";

   class='cKeyword'>char bigBuf[60000];
   class='cKeyword'>char getBigBuf[60000];
   size_t bigBufLen = sizeof(bigBuf);
   size_t getBigBufLen = sizeof(getBigBuf);
   
   class='cKeyword'>char *ipAddr, *derFile;


   if(argc < 7 )
   {
      printf("sample usage: <ldap ip addr> <der file> <subjDN> <subjPwd> <objDN> <objPwd>\n");
      return -1;      
   }
   ipAddr = argv[1];

   derFile = argv[2];

   strcpy( subjName, argv[3]);

   strcpy( subjPwd, argv[4]);
   
   strcpy( userName, argv[5]);

   strcpy( userPwd, argv[6]);

   // establish LDAP connection
    ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &LDAPVersion);

     // initialize the ssl library
    err = ldapssl_client_init(derFile, NULL );
    if (err != LDAP_SUCCESS)
    {
       printf ("ldapssl_client_init failed %d\n", err);
      return err;
    }

   // Initialize the ldap connection for ssl
   if (( ld = ldapssl_init( ipAddr, 636, 1 )) == NULL)
   {
      err = NMAS_E_SERVER_NOT_FOUND;
      printf ("(LSMLDAP)- ldapssl_init failed\n");
   }

   if (!err)
   {
      // change password -- anonymous connection
      err = nmasldap_change_password( ld, userName, userPwd, chgPwd);
      printf("ERROR %d nmasldap_change_password\n", err);

      // get password policy DN
      polcDNLen = sizeof(polcDN);
      err = nmasldap_get_password_policy_dn(ld, userName, &polcDNLen, polcDN);
      if (!err)
      {   
         printf("PUBLIC nmasldap_get_password_policy_dn %s\n", polcDN);
      }
      else
      {
         printf("ERROR %d PUBLIC nmasldap_get_password_policy_dn\n", err);
      }

      // Perform Simple BIND
      err = ldap_simple_bind_s( ld, subjName, subjPwd );
      if (err != LDAP_SUCCESS)
      {
         printf("ERROR %d ldap_simple_bind_s\n", err);
      }
      else
      {

           nuint32 pwdStatus=0xffffffff;
           nuint32 simplePwdStatus=0xffffffff;

         // get password status
            err = nmasldap_get_password_status(ld, userName, &pwdStatus, &simplePwdStatus);
         if (!err)
         {   
            printf("Universal Password status: 0x%x\n", pwdStatus);
            printf("Simple Password status: 0x%x\n", simplePwdStatus);
         }
         else
         {
            printf("ERROR %d nmasldap_get_password_status\n", err);
         }

         // get password policy DN
         polcDNLen = sizeof(polcDN);
         err = nmasldap_get_password_policy_dn(ld, userName, &polcDNLen, polcDN);
         if (!err)
         {   
            printf("nmasldap_get_password_policy_dn %s\n", polcDN);
         }
         else
         {
            printf("ERROR %d nmasldap_get_password_policy_dn\n", err);
         }

         /* **************************************************************** */
         // Universal Password
         /* **************************************************************** */

         // get current password
         getPwdLen = sizeof(getPwd);
         err = nmasldap_get_password( ld, userName, &getPwdLen, getPwd);
         printf("ERROR %d nmasldap_get_password\n", err);
         if (!err)
         {
            resetCur = 1;

            if (strcmp(chgPwd, getPwd) == 0)
            {
               printf("change and get passwords are the same\n");
            }
            else
            {
               printf("change and get passwords are NOT the same\n");
            }
         }


         // check current password
         err = nmasldap_policy_check_current_password(ld, userName);
         printf("ERROR %d nmasldap_policy_check_password\n", err);

         // check password
         err = nmasldap_policy_check_password(ld, userName, newPwd);
         printf("ERROR %d nmasldap_policy_check_password\n", err);

         // change password
         err = nmasldap_change_password( ld, userName, getPwd, chgPwd);
         printf("ERROR %d nmasldap_change_password\n", err);
         getPwdLen = sizeof(getPwd);
         err = nmasldap_get_password( ld, userName, &getPwdLen, getPwd);
         printf("ERROR %d nmasldap_get_password\n", err);

         if (!err)
         {
            if (strcmp(chgPwd, getPwd) == 0)
            {
               printf("change and get passwords are the same\n");
            }
            else
            {
               printf("change and get passwords are NOT the same\n");
            }
         }

         // set password
         err = nmasldap_set_password( ld, userName, newPwd);
         printf("ERROR %d nmasldap_set_password\n", err);
         getPwdLen = sizeof(getPwd);
         err = nmasldap_get_password( ld, userName, &getPwdLen, getPwd);
         printf("ERROR %d nmasldap_get_password\n", err);

         if (!err)
         {
            if (strcmp(newPwd, getPwd) == 0)
            {
               printf("set and get passwords are the same\n");
            }
            else
            {
               printf("set and get passwords are NOT the same\n");
            }
         }

         err = nmasldap_delete_password( ld, userName);
         printf("ERROR %d nmasldap_delete_password\n", err);

         err = nmasldap_set_password( ld, userName, "a"); // set to short password
         printf("ERROR %d nmasldap_set_password(a)\n", err);

         err = nmasldap_set_password( ld, userName, "novell"); // set to novell password
         printf("ERROR %d nmasldap_set_password(novell)\n", err);

         err = nmasldap_set_password( ld, userName, "5novell"); // set to first numeric character
         printf("ERROR %d nmasldap_set_password(5novell)\n", err);

         err = nmasldap_set_password( ld, userName, "novell5"); // set to last numeric character
         printf("ERROR %d nmasldap_set_password(novell5)\n", err);

          if (resetCur)
         {
            err = nmasldap_set_password( ld, userName, userPwd); // set password back to original
            printf("ERROR %d nmasldap_set_password\n", err);
         }
         else
         {
            err = nmasldap_set_password( ld, userName, newPwd); // set password back to new
            printf("ERROR %d nmasldap_set_password\n", err);
         }

         /* **************************************************************** */
         // Simple Password

         /* **************************************************************** */

         getPwdLen = sizeof(curPwd);
         err = nmasldap_get_simple_pwd( ld, userName, getPwdLen, curPwd);
         printf("ERROR %d nmasldap_get_simple_pwd\n", err);

         err = nmasldap_put_simple_pwd( ld, userName, newPwd);
         printf("ERROR %d nmasldap_put_simple_pwd\n", err);

         getPwdLen = sizeof(getPwd);
         err = nmasldap_get_simple_pwd( ld, userName, getPwdLen, getPwd);
         printf("ERROR %d nmasldap_get_simple_pwd\n", err);

         if (!err)
         {
            if (strcmp(newPwd, getPwd) == 0)
            {
               printf("set and get simple passwords are the same\n");
            }
            else
            {
               printf("set and get simple passwords are NOT the same\n");
            }
         }

         err = nmasldap_delete_simple_pwd( ld, userName);
         printf("ERROR %d nmasldap_delete_simple_pwd\n", err);


         /* **************************************************************** */
         // Login Configuration
         /* **************************************************************** */

         memset (bigBuf, '$', bigBufLen);
         err = nmasldap_put_login_config( ld, userName, midLen, &mid, tag, bigBufLen, bigBuf);
         printf("ERROR %d nmasldap_put_login_config (bigBuf)\n", err);

         err = nmasldap_get_login_config( ld, userName, midLen, &mid, tag, &getBigBufLen, getBigBuf);
         printf("ERROR %d nmasldap_get_login_config (getBigBuf)\n", err);
         if (!err)
         {
            if (bigBufLen != getBigBufLen)
            {
               printf("put %d and get %d length are different \n", bigBufLen, getBigBufLen);
            }
            else if (memcmp(bigBuf, getBigBuf, bigBufLen) != 0)
            {
               printf("put and get data are different \n");
            }
            else
            {
               printf("put and get data are the same \n");
            }
         }
         
         err = nmasldap_delete_login_config( ld, userName, midLen, &mid, tag);
         printf("ERROR %d nmasldap_delete_login_config\n", err);

         /* **************************************************************** */
         // Login Secret
         /* **************************************************************** */

         err = nmasldap_put_login_secret( ld, userName, midLen, &mid, tag, bigBufLen, bigBuf);
         printf("ERROR %d nmasldap_put_login_secret\n", err);

         err = nmasldap_delete_login_secret( ld, userName, midLen, &mid, tag);
         printf("ERROR %d nmasldap_delete_login_secret\n", err);
      }

      // unbind 
      ldap_unbind_s( ld );

      ldapssl_client_deinit();
   }

   return err;
}