home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dsfind.exe / FINDUSER.C < prev    next >
C/C++ Source or Header  |  1995-04-13  |  9KB  |  328 lines

  1. /****************************************************************************
  2. **      File:   FINDUSER.C
  3. **
  4. **      Desc:   Finds objects in the tree.
  5. **
  6. **                      This was designed only to find "User" Objects into the tree.
  7. **
  8. **
  9. **              DISCLAIMER
  10. **
  11. **      Novell, Inc. makes no representations or warranties with respect to
  12. **      any NetWare software, and specifically disclaims any express or
  13. **      implied warranties of merchantability, title, or fitness for a
  14. **      particular purpose.
  15. **
  16. **      Distribution of any NetWare software is forbidden without the
  17. **      express written consent of Novell, Inc.  Further, Novell reserves
  18. **      the right to discontinue distribution of any NetWare software.
  19. **
  20. **      Novell is not responsible for lost profits or revenue, loss of use
  21. **      of the software, loss of data, costs of re-creating lost data, the
  22. **      cost of any substitute equipment or program, or claims by any party
  23. **      other than you.  Novell strongly recommends a backup be made before
  24. **      any software is installed.   Technical support for this software
  25. **      may be provided at the discretion of Novell.
  26. **
  27. **      Programmers:
  28. **
  29. **              Ini     Who                                                             Firm
  30. **              --- ----------------- ------------------------------------------------
  31. **              CRG     Calvin Gaisford         Novell Developer Support.
  32. **
  33. **      History:
  34. **
  35. **              When             Who What
  36. **              -------- --- ---------------------------------------------------------
  37. **              10-21-94 CRG First code.
  38. */
  39.  
  40.  
  41.  
  42. /***************************************************************************
  43. **  Libraries Linked in for .exe
  44. **
  45. **  NWCALLS.LIB
  46. **  NWLOCALE.LIB
  47. **  NWNET.LIB
  48. **
  49. */
  50.  
  51.  
  52. /***************************************************************************
  53. **  #defines
  54. */
  55. #define FAILURE  0xFF
  56. #define SUCCESS  0x00
  57.  
  58.  
  59. /****************************************************************************
  60. **      Include headers, macros, etc.
  61. */
  62.  
  63.     /*-----------------------------------------------------------------------
  64.     **      ANSI
  65.     */
  66.     #include <stdio.h>
  67.     #include <stdlib.h>
  68.     #include <string.h>
  69.  
  70.  
  71.     /*-----------------------------------------------------------------------
  72.     **      NetWare
  73.     */
  74.     #include <nwnet.h>
  75.     #include <nwcalls.h>
  76.     #include <nwlocale.h>
  77.  
  78.     /*-----------------------------------------------------------------------
  79.     **      Program Global storage
  80.     */
  81.     struct
  82.     {
  83.         NWDSContextHandle context;
  84.         char                            ncontext[MAX_DN_CHARS];
  85.     }gs;
  86.  
  87.     extern unsigned _stklen=8000;
  88.  
  89.  
  90. /***************************************************************************
  91. **      Function Prototypes
  92. */
  93. void Proc(char *objectName);
  94. void FatalError(int errorCode);
  95. void InitDS(void);
  96. int  UninitDS(void);
  97.  
  98.  
  99. /***************************************************************************
  100. **      Main Program
  101. */
  102. void main(int argc, char *argv[])
  103. {
  104.     if(argc < 2)
  105.     {
  106.         printf("Usage:\n\tFINDUSER <USERNAME>\n");
  107.     }
  108.     else
  109.     {
  110.         InitDS();
  111.         Proc(argv[1]);
  112.         UninitDS();
  113.     }
  114. }
  115.  
  116.  
  117. /***************************************************************************
  118. **      This will search for the username that is passed
  119. **      in the tree.
  120. */
  121. void Proc(char *objectName)
  122. {
  123.     NWDS_ITERATION                            iterHandle=-1L;
  124.     NWOBJECT_INFO               objInfo;
  125.     NWDS_BUFFER                 *filter, *outBuf;
  126.     NWDS_FILTER_CURSOR NWFAR    *cur;
  127.     NWCOUNT                     totalObjs, totalAttrs, x;
  128.     NWDSCCODE                   ccode;
  129.     char                        targetName[MAX_DN_CHARS];
  130.  
  131.     ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &filter);
  132.     if(ccode < 0)
  133.         goto Exit0;
  134.     ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &outBuf);
  135.     if(ccode < 0)
  136.         goto Exit1;
  137.     ccode = NWDSInitBuf(gs.context, DSV_SEARCH_FILTER, filter);
  138.     if(ccode < 0)
  139.         goto Exit2;
  140.     ccode = NWDSAllocFilter(&cur);
  141.     if(ccode < 0)
  142.         goto Exit2;
  143.     ccode = NWDSAddFilterToken(cur, FTOK_ANAME,
  144.             "Object Class", SYN_CLASS_NAME);
  145.     if(ccode < 0)
  146.         goto Exit3;
  147.     ccode = NWDSAddFilterToken(cur, FTOK_EQ,
  148.             NULL, SYN_UNKNOWN);
  149.     if(ccode < 0)
  150.         goto Exit3;
  151.     ccode = NWDSAddFilterToken(cur, FTOK_AVAL,
  152.             "User", SYN_CLASS_NAME);
  153.     if(ccode < 0)
  154.         goto Exit3;
  155.     ccode = NWDSAddFilterToken(cur, FTOK_AND,
  156.             NULL, SYN_UNKNOWN);
  157.     if(ccode < 0)
  158.         goto Exit3;
  159.     ccode = NWDSAddFilterToken(cur, FTOK_ANAME,
  160.             "CN", SYN_CI_STRING);
  161.     if(ccode < 0)
  162.         goto Exit3;
  163.     ccode = NWDSAddFilterToken(cur, FTOK_EQ,
  164.             NULL, SYN_UNKNOWN);
  165.     if(ccode < 0)
  166.         goto Exit3;
  167.     ccode = NWDSAddFilterToken(cur, FTOK_AVAL,
  168.             objectName, SYN_CI_STRING);
  169.     if(ccode < 0)
  170.         goto Exit3;
  171.     ccode = NWDSAddFilterToken(cur, FTOK_END,
  172.             NULL, SYN_UNKNOWN);
  173.     if(ccode < 0)
  174.         goto Exit3;
  175.     ccode = NWDSPutFilter(gs.context, filter, cur, NULL);
  176.     if(ccode <0)
  177.         goto Exit3;
  178.  
  179.     do
  180.     {
  181.         /* if you want to search the entire tree, substiture "[Root]" in for the
  182.                 variable gs.ncontext and it will search everything but it could be
  183.                 time consuming!!! */
  184.  
  185.         ccode = NWDSSearch(gs.context,
  186.                                 gs.ncontext,                                    /* base object (current context)    (IN) */
  187.                                 DS_SEARCH_SUBTREE,            /* scope of the search        (IN) */
  188.                                 TRUE,                         /* derefence aliases    (IN) */
  189.                                 filter,                       /* filter for search          (IN) */
  190.                                 DS_ATTRIBUTE_VALUES,                /* get the names and values   (IN) */
  191.                                 FALSE,                        /* only specified attributes  (IN) */
  192.                                 NULL,                         /* All attributes             (IN) */
  193.                                 &iterHandle,                        /* flag for more to read      (OUT)*/
  194.                                 0,                            /*                            (IN) */
  195.                                 (NWDS_NUM_OBJ NWFAR *)NULL,
  196.                                 outBuf);                              /* information goes here      (OUT)*/
  197.         if(ccode < 0)
  198.         {
  199.             goto Exit2;
  200.         }
  201.  
  202.         ccode = NWDSGetObjectCount(gs.context, outBuf, &totalObjs);
  203.         if(ccode < 0)
  204.         {
  205.             goto Exit2;
  206.         }
  207.         if((NWCOUNT)0 == totalObjs)
  208.         {
  209.             goto Exit2;
  210.         }
  211.  
  212.         for(x=totalObjs;x;x--)
  213.         {
  214.             ccode = NWDSGetObjectName(gs.context, outBuf, targetName, &totalAttrs, &objInfo);
  215.             if(ccode < 0)
  216.             {
  217.                 goto Exit2;
  218.             }
  219.             printf("%s\n",targetName);
  220.         }
  221.     }
  222.     while(iterHandle != NO_MORE_ITERATIONS);
  223.  
  224.     ccode=SUCCESSFUL;
  225.  
  226.     goto Exit2;
  227. Exit3:
  228.     NWDSFreeFilter(cur, NULL);
  229. Exit2:
  230.     NWDSFreeBuf(outBuf);
  231. Exit1:
  232.     NWDSFreeBuf(filter);
  233. Exit0:
  234. }
  235.  
  236.  
  237.  
  238. /***************************************************************************
  239. **      FatalError is the common exit point for errors.
  240. */
  241. void FatalError(int errorCode)
  242. {
  243.         exit(errorCode);
  244. }
  245.  
  246.  
  247.  
  248. /***************************************************************************
  249. **      Establish context.  These are the standard initializations for DS calls
  250. */
  251. void InitDS(void)
  252. {
  253.     NWDSCCODE dsCcode;
  254.     LCONV                   lconvInfo;
  255.     DWORD                     flags;
  256.  
  257.  
  258.     dsCcode=NWCallsInit(NULL,NULL);
  259.     if(dsCcode)           /* initialize allowing to call nwcalls functions */
  260.     {
  261.         printf("FatalError during NWCallsInit %X\n",dsCcode);
  262.         FatalError(FAILURE);
  263.     }
  264.     NWLlocaleconv(&lconvInfo);
  265.     dsCcode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
  266.     if(dsCcode)
  267.     {
  268.         printf("NWInitUnicodeTables() returned: %04X\n", dsCcode);
  269.         FatalError(FAILURE);
  270.     }
  271.     gs.context=NWDSCreateContext();
  272.     if(gs.context)
  273.     {
  274.         printf("FatalError during NWDSCreateContext %X\n",gs.context);
  275.         FatalError(FAILURE);
  276.     }
  277.  
  278.     dsCcode = NWDSGetContext(gs.context, DCK_NAME_CONTEXT, gs.ncontext);
  279.     if(gs.context)
  280.     {
  281.         printf("FatalError during NWDSCreateContext %X\n",gs.context);
  282.         FatalError(FAILURE);
  283.     }
  284.  
  285.     dsCcode = NWDSGetContext(gs.context, DCK_FLAGS, &flags);
  286.     if(dsCcode < 0)
  287.     {
  288.         printf("NWDSGetContext() returned: %04X\n", dsCcode);
  289.         FatalError(FAILURE);
  290.     }
  291.  
  292.     flags |= DCV_TYPELESS_NAMES;
  293.     flags &= ~DCV_CANONICALIZE_NAMES;
  294.     dsCcode = NWDSSetContext(gs.context, DCK_FLAGS, &flags);
  295.     if(dsCcode < 0)
  296.     {
  297.         printf("NWDSSetContext() returned: %04X\n", dsCcode);
  298.         FatalError(FAILURE);
  299.     }
  300.  
  301.  
  302. }
  303.  
  304.  
  305.  
  306. /***************************************************************************
  307. **      Release context and clean up.
  308. */
  309. int UninitDS()
  310. {
  311.     NWDSCCODE dsCcode;
  312.  
  313.     dsCcode=NWDSFreeContext(gs.context);
  314.     if(dsCcode)
  315.     {
  316.         printf("FatalError during NWDSFreeContext %X\n",dsCcode);
  317.         FatalError (FAILURE);
  318.     }
  319.     dsCcode=NWFreeUnicodeTables();
  320.     if(dsCcode)
  321.     {
  322.         printf("Error during NWFreeUnicodeTables()\n");
  323.         FatalError (FAILURE);
  324.     }
  325.     return(0);
  326. }
  327.  
  328.