home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / xdsa1.exe / DSACDEMO.C < prev    next >
Text File  |  1994-10-21  |  12KB  |  412 lines

  1. /*****************************************************************************
  2. ** File: DSACDEMO.C     (shows usage of search.h streams type DS calls)
  3. **
  4. **    Description:
  5. **        Provides an example of using DSSearchOpen, DSSearchSeek, DSRead, and
  6. **        DSClose. These functions provide a streams like interface to the NDS
  7. **        x/open style x.500 interface NDS is pattered after.  It simplifying
  8. **        access to NDS by allocating all resources for the working of the NWDS
  9. **        calls.
  10. **
  11. **    Disclaimer:
  12. **
  13. **        Novell, Inc. makes no representations or warranties with respect to
  14. **        any NetWare software, and specifically disclaims any express or
  15. **        implied warranties of merchantability, title, or fitness for a
  16. **        particular purpose.  
  17. **
  18. **        Distribution of any NetWare software is forbidden without the
  19. **        express written consent of Novell, Inc.  Further, Novell reserves
  20. **        the right to discontinue distribution of any NetWare software.
  21. **
  22. **        Novell is not responsible for lost profits or revenue, loss of use
  23. **        of the software, loss of data, costs of re-creating lost data, the
  24. **        cost of any substitute equipment or program, or claims by any party
  25. **        other than you.  Novell strongly recommends a backup be made before
  26. **        any software is installed.   Technical support for this software
  27. **        may be provided at the discretion of Novell.
  28. **
  29. ** Syntax: load DSACDEMO <base object to find telephone numbers from>
  30. **
  31. ** Output: Search results-showing telephone numbers of all user objects
  32. **            found below the base object;  read results-showing the telephone
  33. **            numbers of the first object found in the search results; and list
  34. **            results-showing all objects below the base object specified.
  35. **
  36. **    Special Considerations:
  37. **        When running on a pre 4.1 version of netware, it is required that you
  38. **        use a full typed distinguished name for the user.  4.1 does not have
  39. **        this constraint.
  40. **
  41. **    QMK386 options used:
  42. **
  43. **        /csDSACDEMO    Include source DSACDEMO.C
  44. **        /csDSACCESS    Include source DSACCESS.C
  45. **        /id               Include import statement for Directory Services symbols.
  46. **
  47. ** Programmers:
  48. **
  49. **    Ini   Who                  Firm
  50. **    ------------------------------------------------------------------------
  51. **    MDO   Mark D. Oberg            Novell Developer Support
  52. **
  53. **
  54. ** History:
  55. **
  56. **    When            Who   What
  57. **    ------------------------------------------------------------------------
  58. **     09-22-94     MDO    Begin
  59. **        10-03-94        MDO    Tested
  60. **
  61. */
  62.  
  63. /*****************************************************************************
  64. ** Include files and macro definitions
  65. */
  66.    /*-------------------------------------------------------------------------
  67.    ** ANSI Includes
  68.    */
  69.     #include <stdlib.h>
  70.     #include <stdio.h>
  71.     #include <string.h>
  72.     #include <conio.h>
  73.  
  74.    /*-------------------------------------------------------------------------
  75.    ** NetWare Includes
  76.    */
  77.     #include <nwdsapi.h>
  78.     #include <nwdsmisc.h>
  79.     #include <nwdsdc.h>
  80.     #include <nwconn.h>
  81.     #include <nwenvrn.h>
  82.     #include <nwcntask.h>
  83.  
  84.    /*-------------------------------------------------------------------------
  85.    ** DSACDEMO
  86.    */
  87.    #include "dsaccess.h"
  88.  
  89. /*****************************************************************************
  90. ** DisplayUsage
  91. */
  92. void DisplayUsage(void)
  93.     {
  94.     printf("Syntax:  load dsacdemo <ds-context to list tel#'s from>\n");
  95.     printf("Example: load dsacdemo ou=services.o=novell\n");
  96.     exit(0);
  97.     }
  98.  
  99. /*****************************************************************************
  100. ** GetPassword prompts the user with "Password:" and accepts a string marking
  101. **     each entered position with an asterisk.
  102. */
  103. void GetPassword(char *password, int size)
  104.     {
  105.     int    index=0;
  106.     WORD    x,y;
  107.  
  108.     printf("Password: ");
  109.     y=wherey();                    /* find current row */
  110.  
  111.     do
  112.         {
  113.         x=wherex();                /* find current column */
  114.         password[index]=getch();
  115.         if(0x0D==password[index])
  116.             password[index]='\0';
  117.  
  118.         else if(0x08==password[index] && 0<=index-1)    /* Backspace */
  119.             {
  120.             index-=2;
  121.             gotoxy(x-1,y);            /* backup */
  122.             putch(' ');                /* clear output */
  123.             gotoxy(x-1,y);            /* backup */
  124.             }
  125.         else
  126.             putch('*');
  127.  
  128.         index++;
  129.         x++;            /* follow cursor position */
  130.         }while (password[index-1] && index<size);
  131.  
  132.     if(password[index-1])
  133.         password=NULL;
  134.     putch('\n');
  135.     }
  136.  
  137. /*****************************************************************************
  138. ** GetText prompts the user with promptMsg and takes input from
  139. **        user placing it in rtext.  Size specifies the maximum string length
  140. **        allowed by the buffer passed in.
  141. **/
  142. void GetText(char *promptMsg, char *rtext, int size)
  143.     {
  144.     int    index=0;
  145.     WORD    x,y;
  146.  
  147.     printf("%s",promptMsg);
  148.     y=wherey();
  149.     do
  150.         {
  151.         x=wherex();
  152.         rtext[index]=getch();
  153.         if(0x0D==rtext[index])
  154.             rtext[index]='\0';
  155.  
  156.         else if(0x08==rtext[index] && 0<=index-1)    /* Backspace */
  157.             {
  158.             index-=2;
  159.             gotoxy(x-1,y);            /* backup */
  160.             putch(' ');                /* clear output */
  161.             gotoxy(x-1,y);            /* backup */
  162.             }
  163.         else putch(rtext[index]);
  164.  
  165.         index++;
  166.         }while (rtext[index-1] && index<size);
  167.  
  168.     if(rtext[index-1]) rtext=NULL;
  169.  
  170.     putch('\n');
  171.     }
  172.  
  173. /*****************************************************************************
  174. ** main
  175. */
  176. void main(int argc, char *argv[])
  177.     {
  178.     NWDSContextHandle    cx;
  179.     DS_HANDLE    *dsHandle;
  180.     READ_DATA    *readData;
  181.     NWDSCCODE    ccode;
  182.     char    password[255+1];
  183.     char    administrator[255+1];
  184.     char    objectName[MAX_DN_CHARS]="";
  185.     /*-------------------------------------------------------------------------
  186.     **    Specify to return Telephone Number's and only User's and if telephone
  187.     **    number exists.
  188.     */
  189.     char    *attrNames[]={"Telephone Number",NULL};
  190.     /*                                TOKEN,        VALUE.             SYNTAX,                */
  191.     /*                                ----------    --------------    ----------------    */
  192.     TOKEN_ENTRY tokens[]={ {FTOK_ANAME, "Object Class",        SYN_CLASS_NAME    },
  193.                                   {FTOK_EQ,     NULL,                     SYN_UNKNOWN        },
  194.                                   {FTOK_AVAL,    "User",                     SYN_CLASS_NAME    },
  195.                                   {FTOK_AND,    NULL,                        SYN_UNKNOWN        },
  196.                                   {FTOK_PRESENT,NULL,                    SYN_UNKNOWN        },
  197.                                   {FTOK_ANAME, "Telephone Number",    SYN_TEL_NUMBER },
  198.                                   {FTOK_END,    NULL,                     SYN_UNKNOWN        }    };
  199.     if(argc!=2)
  200.         DisplayUsage();        /*display usage and exit nlm*/
  201.     
  202.     /*-------------------------------------------------------------------------
  203.     ** pre 4.1 requires using typed names with default context flags.
  204.     */
  205.     GetText("Enter user's typed distinguished name for login: ",administrator,255);    /*get admin name*/
  206.     GetPassword(password, 255);    /*get password for user*/
  207.   
  208.     clrscr();        /* clear off password prompt */
  209.     
  210.     cx=NWDSCreateContext();
  211.     if(ERR_CONTEXT_CREATION==cx)
  212.         {
  213.         printf("Failed NWDSCreateContext.\n");
  214.         goto Exit0;
  215.         }
  216.     
  217.     ccode=NWDSLogin(
  218.         /* NWDSContextHandle    context, */    cx,
  219.         /* uint32    optionsFlag,         */    0,
  220.         /* char NWFAR *objectName,     */    administrator,
  221.         /* char NWFAR *password,        */    password,
  222.         /* uint32 validityPeriod        */    60
  223.         );
  224.  
  225.     memset(password,0x00,255);        /* clear password from memory */
  226.  
  227.     if(ccode)
  228.         {
  229.         printf("Login failed. Check administrator name and password.\n");
  230.         goto Exit1;
  231.         }
  232.  
  233.     /*-------------------------------------------------------------------------
  234.     ** search section.
  235.     */
  236.     printf("Results of search.\n");
  237.     
  238.     /*-------------------------------------------------------------------------
  239.     **    Opens a dsHandle to use for search 
  240.     */
  241.     dsHandle=DSSearchOpen(cx, attrNames, tokens);
  242.     if(NULL==dsHandle)
  243.         {
  244.         printf("Failed DSSearchOpen.\n");
  245.         goto Exit2;
  246.         }
  247.  
  248.     /*-------------------------------------------------------------------------
  249.     ** Initiates the search  
  250.     */
  251.     ccode=DSSearchSeek(
  252.                 /*    DS_HANDLE *searchHandle,     */ dsHandle, 
  253.                 /*    char *baseObjectName,         */ argv[1],    /* start from context specified */
  254.                 /*    NWDS_SEARCH_SCOPE scope,     */ DS_SEARCH_SUBTREE,
  255.                 /*    NWFLAGS    searchAliases,         */ FALSE,
  256.                 /*    NWDS_TYPE    infoType    )         */ 1    );            /* attribute names and values */
  257.     if(ccode)
  258.         {
  259.         printf("Failed DSSearchSeek: %d\n",ccode);
  260.         goto Exit3;
  261.         }
  262.  
  263.     /*-------------------------------------------------------------------------
  264.     ** DSRead returns data until EOF is returned.
  265.     */
  266.     for(readData=DSRead(dsHandle);NULL !=readData && EOF != readData;readData=DSRead(dsHandle))
  267.         {
  268.         switch (readData->readType)
  269.             {
  270.             case OBJECT:
  271.                 if(0x00 == strlen(objectName))
  272.                     strcpy(objectName,readData->read.object.name);    /* used in read example */
  273.                 printf("\n\n%s\n",readData->read.object.name); /* print user name */
  274.                 break;
  275.  
  276.             case ATTR:
  277.                 printf("%s\n",readData->read.attr.name);    /* print attribute name */
  278.                 break;
  279.  
  280.             case ATTR_VALUE:
  281.                 printf("%s\n",(char *)readData->read.attrVal.value);
  282.             }
  283.         free(readData);
  284.         }
  285.  
  286.     if(NULL==readData)
  287.         {
  288.         printf("Failed DSRead.\n");
  289.         }
  290.  
  291.     /*-------------------------------------------------------------------------
  292.     ** close the handle.
  293.     */
  294.     DSClose(dsHandle);        /* deallocate resources from DSSearchOpen */
  295.  
  296.     /*-------------------------------------------------------------------------
  297.     ** read section.
  298.     */
  299.     printf("Results of read on object %s.\n",objectName);
  300.  
  301.     /*-------------------------------------------------------------------------
  302.     **    Opens a dsHandle to use for read
  303.     */
  304.     dsHandle=DSReadOpen(cx, attrNames);
  305.     if(NULL==dsHandle)
  306.         {
  307.         printf("Failed DSReadOpen.\n");
  308.         goto Exit2;
  309.         }
  310.  
  311.     /*-------------------------------------------------------------------------
  312.     ** Initiates the read
  313.     */
  314.     ccode=DSReadSeek(
  315.                 /*    DS_HANDLE *searchHandle,*/ dsHandle, 
  316.                 /*    char *baseObjectName,         */ objectName,        /*read first object found on search */
  317.                 /*    NWDS_TYPE    infoType    )         */ 1    );        /* attribute names and values */
  318.     if(ccode)
  319.         {
  320.         printf("Failed DSReadSeek: %d\n",ccode);
  321.         goto Exit3;
  322.         }
  323.  
  324.     /*-------------------------------------------------------------------------
  325.     ** DSRead returns data until EOF is returned.
  326.     */
  327.     for(readData=DSRead(dsHandle);NULL !=readData && EOF != readData;readData=DSRead(dsHandle))
  328.         {
  329.         switch (readData->readType)
  330.             {
  331.             case ATTR:
  332.                 printf("%s\n",readData->read.attr.name);    /* print attribute name */
  333.                 break;
  334.  
  335.             case ATTR_VALUE:
  336.                 printf("%s\n",(char *)readData->read.attrVal.value);
  337.             }
  338.         free(readData);
  339.         }
  340.  
  341.     if(NULL==readData)
  342.         {
  343.         printf("Failed DSRead.\n");
  344.         }
  345.  
  346.     /*-------------------------------------------------------------------------
  347.     ** close the handle.
  348.     */
  349.     DSClose(dsHandle);        /* deallocate resources from DSSearchOpen */
  350.  
  351.     /*-------------------------------------------------------------------------
  352.     ** list section.
  353.     */
  354.     printf("Results of list.\n");
  355.  
  356.     /*-------------------------------------------------------------------------
  357.     **    Opens a dsHandle to use for list
  358.     */
  359.     dsHandle=DSListOpen(cx);
  360.     if(NULL==dsHandle)
  361.         {
  362.         printf("Failed DSListOpen.\n");
  363.         goto Exit2;
  364.         }
  365.  
  366.     /*-------------------------------------------------------------------------
  367.     ** Initiates the list
  368.     */
  369.     ccode=DSListSeek(
  370.                 /*    DS_HANDLE *searchHandle,     */ dsHandle, 
  371.                 /*    char *baseObjectName,         */ argv[1] );        /* start from context specified */
  372.     if(ccode)
  373.         {
  374.         printf("Failed DSListSeek: %d\n",ccode);
  375.         goto Exit3;
  376.         }
  377.  
  378.     /*-------------------------------------------------------------------------
  379.     ** DSRead returns data until EOF is returned.
  380.     */
  381.     for(readData=DSRead(dsHandle);NULL !=readData && EOF != readData;readData=DSRead(dsHandle))
  382.         {
  383.         switch (readData->readType)
  384.             {
  385.             case OBJECT        :
  386.                 printf("%s\n",readData->read.object.name); /* print user name */
  387.                 break;
  388.             default:
  389.                 printf("Invalid type received from DSRead by List.\n");
  390.  
  391.             }
  392.         free(readData);
  393.         }
  394.  
  395.     if(NULL==readData)
  396.         {
  397.         printf("Failed DSList.\n");
  398.         }
  399.  
  400. Exit3:
  401.     DSClose(dsHandle);        /* deallocate resources from DSSearchOpen */
  402. Exit2:
  403.     NWDSLogout(cx);
  404. Exit1:
  405.     NWDSFreeContext(cx);
  406. Exit0:
  407.     exit(0);
  408.     }
  409.  
  410.  
  411.  
  412.