home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / snapx2.exe / SCHRDN.EXE / SCHRDN.C next >
C/C++ Source or Header  |  1995-07-13  |  12KB  |  465 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 *commonName);
  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:SCHRDN <Object Name>");
  104.         exit(1);
  105.         }
  106.  
  107.     cCode = NWCallsInit(NULL, NULL);
  108.     if (cCode)
  109.         {
  110.         printf("\nCall to NWCallsInit returned: %04X", cCode);
  111.         exit(1);
  112.         }
  113.  
  114.     NWLsetlocale(LC_ALL, "");
  115.  
  116.     NWLlocaleconv(&lconvInfo);
  117.  
  118.     cCode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
  119.  
  120.     if(cCode)
  121.         {
  122.         printf("NWInitUnicodeTables() returned: %04X\n", cCode);
  123.         goto _FreeUnicodeTables;
  124.         }
  125.  
  126.     dContext = NWDSCreateContext();
  127.  
  128.     if (dContext == ERR_CONTEXT_CREATION)
  129.         {
  130.         printf("NWDSCreateContext returned: %04X\n", cCode);
  131.         goto _FreeContext;
  132.         }
  133.  
  134.  
  135.     /*-------------------------------------------------------------------
  136.    ** Get the current directory context flags so we can modify them.
  137.     */
  138.  
  139.    cCode = NWDSGetContext(
  140.                 /* Contxt Handle    */ dContext,
  141.                 /* Key              */ DCK_FLAGS,
  142.                 /* Context Flags    */ &flags
  143.                 );
  144.  
  145.     if (cCode < 0)
  146.         {
  147.         printf("NWDSGetContext returned: %04X\n", cCode);
  148.         goto _FreeContext;
  149.         }
  150.  
  151.     /*-------------------------------------------------------------------
  152.    **   Turn typeless naming on.
  153.    **   Turn canonicalize names off.  This means we will get full names.
  154.    */
  155.  
  156.    flags |= DCV_TYPELESS_NAMES;
  157.    flags &= ~DCV_CANONICALIZE_NAMES;
  158.  
  159.     /*-------------------------------------------------------------------
  160.    ** Set the directory context flags so they take effect.
  161.     */
  162.  
  163.    cCode = NWDSSetContext(
  164.                 /* Context Handle */ dContext,
  165.                 /* Key            */ DCK_FLAGS,
  166.                 /* Set Flag Value */ &flags
  167.                 );
  168.  
  169.     if (cCode < 0)
  170.         {
  171.         printf("NWDSSetContext returned: %04X\n", cCode);
  172.         goto _FreeContext;
  173.         }
  174.  
  175.     SearchForObjects(argV[1]);
  176.  
  177.  
  178. _FreeContext:
  179.     NWDSFreeContext(dContext);
  180. _FreeUnicodeTables:
  181.     NWFreeUnicodeTables();
  182.  
  183.  
  184. }
  185.  
  186. int SearchForObjects(char *commonName)
  187. {
  188.  
  189.    NWFLAGS           searchAliases = FALSE;
  190.    NWDS_ITERATION    iterHandle = (NWDS_ITERATION)-1;
  191.    NWDS_BUFFER NWFAR *searchFilter = NULL,
  192.             NWFAR         *attrNames = NULL,
  193.            NWFAR         *retBuf = NULL;
  194.     NWDSCCODE            cCode;
  195.    NWDS_NUM_OBJ      cntObjectsToSearch,
  196.                         cntObjectsSearched;
  197.    int               i;
  198.    void        NWFAR *val;
  199.    NWSYNTAX_ID       syntaxID;
  200.    NWDS_FILTER_CURSOR NWFAR *cur = NULL;
  201.  
  202.  
  203.  
  204.     /*-------------------------------------------------------------------
  205.    ** Allocate a buffer to restrict our search to only certain attributes.
  206.     */
  207.  
  208.     cCode = NWDSAllocBuf(
  209.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  210.                 /* Buff. Point.*/ &attrNames
  211.                 );
  212.  
  213.     if (cCode < 0)
  214.             {
  215.             printf("NWDSAllocBuf returned: %04X\n", cCode);
  216.             goto Out;
  217.             }
  218.  
  219.  
  220.    /*------------------------------------------------------------
  221.    ** Allocate a buffer to contain the search expression
  222.     */
  223.     cCode = NWDSAllocBuf(
  224.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  225.                 /* Buff. Point.*/ &searchFilter
  226.                 );
  227.  
  228.     if (cCode < 0)
  229.             {
  230.             printf("NWDSAllocBuf returned: %04X\n", cCode);
  231.             goto Out;
  232.             }
  233.  
  234.    /*------------------------------------------------------------
  235.    ** Initialize the searchFilter buffer
  236.     */
  237.    cCode = NWDSInitBuf(
  238.                 /* Context  */ dContext,
  239.                 /* Operation*/ DSV_SEARCH_FILTER,
  240.                 /* buffer   */ searchFilter
  241.                 );
  242.  
  243.  
  244.     if (cCode < 0)
  245.             {
  246.             printf("NWDSInitBuf returned: %04X\n", cCode);
  247.             goto Out;
  248.             }
  249.  
  250.    /*------------------------------------------------------------
  251.    ** Allocate a filter cursor to put the search expression
  252.     */
  253.    cCode = NWDSAllocFilter(
  254.                 /* Cursor    */ &cur
  255.                 );
  256.  
  257.     if (cCode < 0)
  258.             {
  259.             printf("NWDSAllocFilter returned: %04X\n", cCode);
  260.             goto Out;
  261.             }
  262.  
  263.    /*------------------------------------------------------------ 
  264.    ** Now we can build the expression tree.
  265.     ** Filter on Object Class of "User".
  266.     */
  267.  
  268.  
  269.  
  270.  
  271.    cCode = NWDSAddFilterToken(
  272.                 /* Cursor (exp tree) */ cur,
  273.                 /* TOKEN             */ FTOK_RDN,
  274.                 /* Name or Value     */ NULL,
  275.                 /* Syntax ID         */ 0
  276.                 );
  277.  
  278.     if (cCode < 0)
  279.             {
  280.             printf("NWDSAddFilterToken1 returned: %04X\n", cCode);
  281.             goto Out;
  282.             }
  283.  
  284.  
  285.  
  286.    cCode = NWDSAddFilterToken(
  287.                 /* Cursor (exp tree) */ cur,
  288.                 /* TOKEN             */ FTOK_ANAME,
  289.                 /* Name or Value     */ commonName,
  290.                 /* Syntax ID         */ SYN_CI_STRING
  291.                 );
  292.  
  293.     if (cCode < 0)
  294.             {
  295.             printf("NWDSAddFilterToken returned: %04X\n", cCode);
  296.             goto Out;
  297.             }
  298.  
  299.  
  300.    cCode = NWDSAddFilterToken(
  301.                 /* Cursor (exp tree) */ cur,
  302.                 /* TOKEN             */ FTOK_END,
  303.                 /* Name or Value     */ NULL,
  304.                 /* Syntax ID         */ 0
  305.                 );
  306.  
  307.     if (cCode < 0)
  308.             {
  309.             printf("NWDSAddFilterToken returned: %04X\n", cCode);
  310.             goto Out;
  311.             }
  312.  
  313.     /*-------------------------------------------------------------
  314.    ** The expression is complete! Put the filter expression
  315.    ** into the filter buffer.  The freeVal function pointer
  316.    ** would free the attribute values, but we allocated the
  317.    ** values on the stack, so pass NULL.
  318.    */
  319.  
  320.    cCode = NWDSPutFilter(
  321.                 /* Context Handle */ dContext,
  322.                 /* Input Buffer   */ searchFilter,
  323.                 /* Cursor Pointer */ cur,
  324.                 /* Free val. func */ NULL
  325.                 );
  326.  
  327.     if (cCode < 0)
  328.             {
  329.               printf("NWDSAddFilterToken returned: %04X\n", cCode);
  330.             goto Out;
  331.             }
  332.  
  333.    else
  334.       cur = NULL; /* so we know at the bottom of this routine to free or not */
  335.  
  336.     /*-------------------------------------------------------------
  337.     ** Allocate a buffer to receive the results.
  338.     */
  339.     cCode = NWDSAllocBuf(
  340.                 /* Buffer Size */ DEFAULT_MESSAGE_LEN,
  341.                 /* Buff. Point.*/ &retBuf
  342.                 );
  343.  
  344.     if (cCode < 0)
  345.             {
  346.               printf("NWDSAllocBuf returned: %04X\n", cCode);
  347.             goto Out;
  348.             }
  349.  
  350.    do
  351.    {
  352.         /*---------------------------------------------------------
  353.       ** Finally! we have everything we need to search. Note that
  354.         ** the base object specified here is "[Root]". The base
  355.         ** object could be an OU. This will initiate the search from
  356.         ** that OU on down the tree.
  357.         */
  358.       cCode = NWDSSearch(
  359.                 /* Context handle */ dContext,
  360.                 /* Base Obj Name  */ "[Root]",
  361.             /* Scope          */ DS_SEARCH_SUBTREE,
  362.                 /* Search Alias?  */ searchAliases,
  363.                 /* Search Filter  */ searchFilter,
  364.                 /* Info Type      */ 0,
  365.                 /* All Attrib's ? */ FALSE,
  366.                 /* Attrib. names  */ NULL,
  367.                 /* Iter. Handle   */ &iterHandle,
  368.             /* cnt Obj's 2 sch*/ cntObjectsToSearch,
  369.                 /* cnt obj's schd */ &cntObjectsSearched,
  370.                 /* Object returned*/ retBuf
  371.                 );
  372.  
  373.         if (cCode < 0)
  374.                 {
  375.                   printf("NWDSSearch returned: %04X\n", cCode);
  376.                 goto Out;
  377.                 }
  378.  
  379.       ActOnData(retBuf);
  380.  
  381.    }
  382.    while (iterHandle != (NWDS_ITERATION)-1);
  383.  
  384. Out:
  385.    if (retBuf)
  386.       NWDSFreeBuf(retBuf);
  387.    if (cur)
  388.       NWDSFreeFilter(cur, NULL);
  389.    if (searchFilter)
  390.       NWDSFreeBuf(searchFilter);
  391.    if (attrNames)
  392.       NWDSFreeBuf(attrNames);
  393.  
  394.     return(0);
  395. }
  396.  
  397.  
  398. /****************************************************************************
  399. ** Function: int ActOnData(NWDS_BUFFER NWFAR *buffer)
  400. **    The buffer returned from NWDSSearch() is passed to this function. This
  401. ** buffer contains all of the object/attribute information that met the
  402. ** criteria of the search. This function reads this information from the
  403. ** buffer.
  404. */
  405.  
  406. int ActOnData(NWDS_BUFFER NWFAR *buffer)
  407. {
  408.     NWDSCCODE            dscCode;
  409.     NWCCODE                cCode;
  410.     NWCOUNT               attrCount, attrValCount, objectCount;
  411.    NWOBJECT_INFO      objectInfo;
  412.    static char            objectName[MAX_DN_CHARS + 1];
  413.     char                    shortName[48];
  414.     int                    err=1,
  415.                             i,
  416.                             j,
  417.                             k;
  418.     NWNUMBER                maxConnections;
  419.     NWSIZE                attrValSize;
  420.     void                    *attrVal;
  421.     char                    attrName[MAX_SCHEMA_NAME_CHARS + 1];
  422.    NWSYNTAX_ID       syntaxID;
  423.  
  424.  
  425.     dscCode = NWDSGetObjectCount(
  426.                 /* Req. Context   */ dContext,
  427.                 /* P. to read buf.*/ buffer,
  428.                 /* Num. of OBJs   */ &objectCount
  429.                 );
  430.  
  431.     if (dscCode < 0)
  432.         {
  433.         printf("\nNWDSGetObjectCount returned : %04X\n", dscCode);
  434.         return(err);
  435.         }
  436.  
  437.  
  438.     for (i = 0; i < objectCount; i++)
  439.         {
  440.         dscCode = NWDSGetObjectName(
  441.                     /* Req. Context     */ dContext,
  442.                     /* P. to result buf */ buffer,
  443.                     /* object name      */ objectName,
  444.                     /* Attribute count  */ &attrCount,
  445.                     /* object Info      */ &objectInfo
  446.                     );
  447.  
  448.         if (dscCode < 0)
  449.             {
  450.             printf("\nNWDSGetObjectName returned: %04X\n", dscCode);
  451.             return(dscCode);
  452.             }
  453.  
  454.     printf("\nObject Name: %s", objectName);
  455.  
  456.  
  457.  
  458.         } /* end of objects loop */
  459.     return(0);
  460. }
  461.  
  462.  
  463.  
  464.  
  465.