home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / schcls.exe / SCHBSCLS.C next >
C/C++ Source or Header  |  1995-07-13  |  12KB  |  467 lines

  1. /****************************************************************************
  2. **    File:    SCHRDN.C
  3. **
  4. **    Desc:    This sample code demonstrates how to set up an NWDSSearch() to 
  5. **            search for objects in the NDS tree. More specifically this code
  6. **            demonstrates how to use the FTOK_RDN token to allow searching 
  7. **            for a "common name" or a "relative distinquished name" without
  8. **            having an authenticated NDS connections. An attachment to an NDS
  9. **            server is all that is required.
  10. **
  11. **
  12. **    Copyright (c) 1994 Novell, Inc.  All Rights Reserved.
  13. **
  14. **    THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
  15. **    TREATIES.  USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE 
  16. **    LICENSE AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK)
  17. **    THAT CONTAINS THIS WORK.
  18. **
  19. **    Pursuant to the SDK License Agreement, Novell hereby grants to 
  20. **    Developer a royalty-free, non-exclusive license to include the
  21. **    sample code SNAPEX01.C and derivative binaries in its product.
  22. **    Novell grants to Developer worldwide distribution rights to market,
  23. **    distribute or sell the sample code SNAPEX01.C and derivative
  24. **    binaries as a component of Developer's product(s).  Novell shall 
  25. **    have no obligations to Developer or Developer's customers with 
  26. **    respect to this code.
  27. **
  28. **
  29. **        DISCLAIMER
  30. **
  31. **       Novell, Inc. makes no representations or warranties with respect
  32. **    to the contents or use of this code, and specifically disclaims any
  33. **    express or implied warranties of merchantability or fitness for any
  34. **    particular purpose.  Further, Novell, Inc. reserves the right to revise
  35. **    this publication and to make changes to its content, at any time,
  36. **    without obligation to notify any person or entity of such revisions or
  37. **    changes.
  38. **    
  39. **       Further, Novell, Inc. makes no representations or warranties with
  40. **    respect to any software, and specifically disclaims any express or
  41. **    implied warranties of merchantability or fitness for any particular
  42. **    purpose.  Further, Novell, Inc. reserves the right to make changes to
  43. **    any and all parts of the software, at any time, without obligation to
  44. **    notify any person or entity of such changes.
  45. **
  46. **
  47. **    Programmers:
  48. **
  49. **        Ini    Who                        Firm
  50. **        -----------------------------------------------------------------------
  51. **        KLB    Karl Bunnell                Novell Developer Support.
  52. **
  53. **    History:
  54. **
  55. **        When        Who    What
  56. **        -----------------------------------------------------------------------
  57. **        07-13-95    klb    First code.
  58. */
  59.  
  60. /****************************************************************************
  61. **    Include headers, macros, function prototypes, etc.
  62. */
  63.  
  64.     /*------------------------------------------------------------------------
  65.     **    MACROS
  66.     */
  67.     #define NWDOS
  68.  
  69.     /*------------------------------------------------------------------------
  70.     **    ANSI
  71.     */
  72.     #include <stdlib.h>
  73.     #include <stdio.h>
  74.  
  75.     /*------------------------------------------------------------------------
  76.     **    NetWare
  77.     */
  78.     #include <nwnet.h>
  79.     #include <nwcalls.h>
  80.     #include <nwlocale.h>
  81.  
  82.  
  83.     /*------------------------------------------------------------------------
  84.     **    Prototypes
  85.     */
  86.     int SearchForObjects(char *baseClass);
  87.     int ActOnData(NWDS_BUFFER NWFAR *buffer);
  88.  
  89.     /*------------------------------------------------------------------------
  90.     **    Globals
  91.     */
  92.     NWDSContextHandle    dContext;
  93.  
  94.  
  95. void main(int argC, char *argV[])
  96. {
  97.     NWDSCCODE            cCode;
  98.     LCONV                lconvInfo;
  99.     DWORD                    flags;
  100.  
  101.     if(argC < 2)
  102.         {
  103.         printf("\nUsage:SCHBSCLS <Object Base Class>");
  104.         printf("\ne.g. SCHBSCLS User");
  105.         exit(1);
  106.         }
  107.  
  108.  
  109.     cCode = NWCallsInit(NULL, NULL);
  110.     if (cCode)
  111.         {
  112.         printf("\nCall to NWCallsInit returned: %04X", cCode);
  113.         exit(1);
  114.         }
  115.  
  116.     NWLsetlocale(LC_ALL, "");
  117.  
  118.     NWLlocaleconv(&lconvInfo);
  119.  
  120.     cCode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
  121.  
  122.     if(cCode)
  123.         {
  124.         printf("NWInitUnicodeTables() returned: %04X\n", cCode);
  125.         goto _FreeUnicodeTables;
  126.         }
  127.  
  128.     dContext = NWDSCreateContext();
  129.  
  130.     if (dContext == ERR_CONTEXT_CREATION)
  131.         {
  132.         printf("NWDSCreateContext returned: %04X\n", cCode);
  133.         goto _FreeContext;
  134.         }
  135.  
  136.  
  137.     /*-------------------------------------------------------------------
  138.    ** Get the current directory context flags so we can modify them.
  139.     */
  140.  
  141.    cCode = NWDSGetContext(
  142.                 /* Contxt Handle    */ dContext,
  143.                 /* Key              */ DCK_FLAGS,
  144.                 /* Context Flags    */ &flags
  145.                 );
  146.  
  147.     if (cCode < 0)
  148.         {
  149.         printf("NWDSGetContext returned: %04X\n", cCode);
  150.         goto _FreeContext;
  151.         }
  152.  
  153.     /*-------------------------------------------------------------------
  154.    **   Turn typeless naming on.
  155.    **   Turn canonicalize names off.  This means we will get full names.
  156.    */
  157.  
  158.    flags |= DCV_TYPELESS_NAMES;
  159.    flags &= ~DCV_CANONICALIZE_NAMES;
  160.  
  161.     /*-------------------------------------------------------------------
  162.    ** Set the directory context flags so they take effect.
  163.     */
  164.  
  165.    cCode = NWDSSetContext(
  166.                 /* Context Handle */ dContext,
  167.                 /* Key            */ DCK_FLAGS,
  168.                 /* Set Flag Value */ &flags
  169.                 );
  170.  
  171.     if (cCode < 0)
  172.         {
  173.         printf("NWDSSetContext returned: %04X\n", cCode);
  174.         goto _FreeContext;
  175.         }
  176.  
  177.     SearchForObjects(argV[1]);
  178.  
  179.  
  180. _FreeContext:
  181.     NWDSFreeContext(dContext);
  182. _FreeUnicodeTables:
  183.     NWFreeUnicodeTables();
  184.  
  185.  
  186. }
  187.  
  188. int SearchForObjects(char *baseClass)
  189. {
  190.  
  191.    NWFLAGS           searchAliases = FALSE;
  192.    NWDS_ITERATION    iterHandle = (NWDS_ITERATION)-1;
  193.    NWDS_BUFFER NWFAR *searchFilter = NULL,
  194.             NWFAR         *attrNames = NULL,
  195.            NWFAR         *retBuf = NULL;
  196.     NWDSCCODE            cCode;
  197.    NWDS_NUM_OBJ      cntObjectsToSearch,
  198.                         cntObjectsSearched;
  199.    int               i;
  200.    void        NWFAR *val;
  201.    NWSYNTAX_ID       syntaxID;
  202.    NWDS_FILTER_CURSOR NWFAR *cur = NULL;
  203.  
  204.  
  205.  
  206.     /*-------------------------------------------------------------------
  207.    ** Allocate a buffer to restrict our search to only certain attributes.
  208.     */
  209.  
  210.     cCode = NWDSAllocBuf(
  211.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  212.                 /* Buff. Point.*/ &attrNames
  213.                 );
  214.  
  215.     if (cCode < 0)
  216.             {
  217.             printf("NWDSAllocBuf returned: %04X\n", cCode);
  218.             goto Out;
  219.             }
  220.  
  221.  
  222.    /*------------------------------------------------------------
  223.    ** Allocate a buffer to contain the search expression
  224.     */
  225.     cCode = NWDSAllocBuf(
  226.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  227.                 /* Buff. Point.*/ &searchFilter
  228.                 );
  229.  
  230.     if (cCode < 0)
  231.             {
  232.             printf("NWDSAllocBuf returned: %04X\n", cCode);
  233.             goto Out;
  234.             }
  235.  
  236.    /*------------------------------------------------------------
  237.    ** Initialize the searchFilter buffer
  238.     */
  239.    cCode = NWDSInitBuf(
  240.                 /* Context  */ dContext,
  241.                 /* Operation*/ DSV_SEARCH_FILTER,
  242.                 /* buffer   */ searchFilter
  243.                 );
  244.  
  245.  
  246.     if (cCode < 0)
  247.             {
  248.             printf("NWDSInitBuf returned: %04X\n", cCode);
  249.             goto Out;
  250.             }
  251.  
  252.    /*------------------------------------------------------------
  253.    ** Allocate a filter cursor to put the search expression
  254.     */
  255.    cCode = NWDSAllocFilter(
  256.                 /* Cursor    */ &cur
  257.                 );
  258.  
  259.     if (cCode < 0)
  260.             {
  261.             printf("NWDSAllocFilter returned: %04X\n", cCode);
  262.             goto Out;
  263.             }
  264.  
  265.    /*------------------------------------------------------------
  266.    ** Now we can build the expression tree.
  267.     ** Filter on Object Class of "User".
  268.     */
  269.  
  270.  
  271.  
  272.  
  273.    cCode = NWDSAddFilterToken(
  274.                 /* Cursor (exp tree) */ cur,
  275.                 /* TOKEN             */ FTOK_BASECLS,
  276.                 /* Name or Value     */ NULL,
  277.                 /* Syntax ID         */ 0
  278.                 );
  279.  
  280.     if (cCode < 0)
  281.             {
  282.             printf("NWDSAddFilterToken1 returned: %04X\n", cCode);
  283.             goto Out;
  284.             }
  285.  
  286.  
  287.  
  288.    cCode = NWDSAddFilterToken(
  289.                 /* Cursor (exp tree) */ cur,
  290.                 /* TOKEN             */ FTOK_ANAME,
  291.                 /* Name or Value     */ baseClass,
  292.                 /* Syntax ID         */ SYN_CI_STRING
  293.                 );
  294.  
  295.     if (cCode < 0)
  296.             {
  297.             printf("NWDSAddFilterToken returned: %04X\n", cCode);
  298.             goto Out;
  299.             }
  300.  
  301.  
  302.    cCode = NWDSAddFilterToken(
  303.                 /* Cursor (exp tree) */ cur,
  304.                 /* TOKEN             */ FTOK_END,
  305.                 /* Name or Value     */ NULL,
  306.                 /* Syntax ID         */ 0
  307.                 );
  308.  
  309.     if (cCode < 0)
  310.             {
  311.             printf("NWDSAddFilterToken returned: %04X\n", cCode);
  312.             goto Out;
  313.             }
  314.  
  315.     /*-------------------------------------------------------------
  316.    ** The expression is complete! Put the filter expression
  317.    ** into the filter buffer.  The freeVal function pointer
  318.    ** would free the attribute values, but we allocated the
  319.    ** values on the stack, so pass NULL.
  320.    */
  321.  
  322.    cCode = NWDSPutFilter(
  323.                 /* Context Handle */ dContext,
  324.                 /* Input Buffer   */ searchFilter,
  325.                 /* Cursor Pointer */ cur,
  326.                 /* Free val. func */ NULL
  327.                 );
  328.  
  329.     if (cCode < 0)
  330.             {
  331.               printf("NWDSAddFilterToken returned: %04X\n", cCode);
  332.             goto Out;
  333.             }
  334.  
  335.    else
  336.       cur = NULL; /* so we know at the bottom of this routine to free or not */
  337.  
  338.     /*-------------------------------------------------------------
  339.     ** Allocate a buffer to receive the results.
  340.     */
  341.     cCode = NWDSAllocBuf(
  342.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  343.                 /* Buff. Point.*/ &retBuf
  344.                 );
  345.  
  346.     if (cCode < 0)
  347.             {
  348.               printf("NWDSAllocBuf returned: %04X\n", cCode);
  349.             goto Out;
  350.             }
  351.  
  352.    do
  353.    {
  354.         /*---------------------------------------------------------
  355.       ** Finally! we have everything we need to search. Note that
  356.         ** the base object specified here is "[Root]". The base
  357.         ** object could be an OU. This will initiate the search from
  358.         ** that OU on down the tree.
  359.         */
  360.       cCode = NWDSSearch(
  361.                 /* Context handle */ dContext,
  362.                 /* Base Obj Name  */ "[Root]",
  363.             /* Scope          */ DS_SEARCH_SUBTREE,
  364.                 /* Search Alias?  */ searchAliases,
  365.                 /* Search Filter  */ searchFilter,
  366.                 /* Info Type      */ 0,
  367.                 /* All Attrib's ? */ FALSE,
  368.                 /* Attrib. names  */ NULL,
  369.                 /* Iter. Handle   */ &iterHandle,
  370.             /* cnt Obj's 2 sch*/ cntObjectsToSearch,
  371.                 /* cnt obj's schd */ &cntObjectsSearched,
  372.                 /* Object returned*/ retBuf
  373.                 );
  374.  
  375.         if (cCode < 0)
  376.                 {
  377.                   printf("NWDSSearch returned: %04X\n", cCode);
  378.                 goto Out;
  379.                 }
  380.  
  381.       ActOnData(retBuf);
  382.  
  383.    }
  384.    while (iterHandle != (NWDS_ITERATION)-1);
  385.  
  386. Out:
  387.    if (retBuf)
  388.       NWDSFreeBuf(retBuf);
  389.    if (cur)
  390.       NWDSFreeFilter(cur, NULL);
  391.    if (searchFilter)
  392.       NWDSFreeBuf(searchFilter);
  393.    if (attrNames)
  394.       NWDSFreeBuf(attrNames);
  395.  
  396.     return(0);
  397. }
  398.  
  399.  
  400. /****************************************************************************
  401. ** Function: int ActOnData(NWDS_BUFFER NWFAR *buffer)
  402. **    The buffer returned from NWDSSearch() is passed to this function. This
  403. ** buffer contains all of the object/attribute information that met the
  404. ** criteria of the search. This function reads this information from the
  405. ** buffer.
  406. */
  407.  
  408. int ActOnData(NWDS_BUFFER NWFAR *buffer)
  409. {
  410.     NWDSCCODE            dscCode;
  411.     NWCCODE                cCode;
  412.     NWCOUNT               attrCount, attrValCount, objectCount;
  413.    NWOBJECT_INFO      objectInfo;
  414.    static char            objectName[MAX_DN_CHARS + 1];
  415.     char                    shortName[48];
  416.     int                    err=1,
  417.                             i,
  418.                             j,
  419.                             k;
  420.     NWNUMBER                maxConnections;
  421.     NWSIZE                attrValSize;
  422.     void                    *attrVal;
  423.     char                    attrName[MAX_SCHEMA_NAME_CHARS + 1];
  424.    NWSYNTAX_ID       syntaxID;
  425.  
  426.  
  427.     dscCode = NWDSGetObjectCount(
  428.                 /* Req. Context   */ dContext,
  429.                 /* P. to read buf.*/ buffer,
  430.                 /* Num. of OBJs   */ &objectCount
  431.                 );
  432.  
  433.     if (dscCode < 0)
  434.         {
  435.         printf("\nNWDSGetObjectCount returned : %04X\n", dscCode);
  436.         return(err);
  437.         }
  438.  
  439.  
  440.     for (i = 0; i < objectCount; i++)
  441.         {
  442.         dscCode = NWDSGetObjectName(
  443.                     /* Req. Context     */ dContext,
  444.                     /* P. to result buf */ buffer,
  445.                     /* object name      */ objectName,
  446.                     /* Attribute count  */ &attrCount,
  447.                     /* object Info      */ &objectInfo
  448.                     );
  449.  
  450.         if (dscCode < 0)
  451.             {
  452.             printf("\nNWDSGetObjectName returned: %04X\n", dscCode);
  453.             return(dscCode);
  454.             }
  455.  
  456.     printf("\nObject Name: %s", objectName);
  457.  
  458.  
  459.  
  460.         } /* end of objects loop */
  461.     return(0);
  462. }
  463.  
  464.  
  465.  
  466.  
  467.