home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dbsamp.exe / DBINST / DBINST.C next >
Text File  |  1995-01-12  |  16KB  |  550 lines

  1. /****************************************************************************
  2. **    File:    DBINST.C
  3. **
  4. **    Desc:    Defines the following new classes in the tree:
  5. **                    DBServer -     a class that is used for a Database Server
  6. **                                            to authenticate to the tree.
  7. **                    Database -     a class that is used as an object to represent
  8. **                                            a database in a Database Server.
  9. **        Defines the following new Attributes in the tree:
  10. **                    DBAdmin -        an attribute that is used to hold the name of
  11. **                                            a NDS group.  This will be used to hold the
  12. **                                            administrators of the Database.
  13. **                    DBOperator- an attribute that is used to hold the name of
  14. **                                            a NDS group.  This will be used to hold the
  15. **                                            operators of the Database.
  16. **                  DBName-            an attribute that is added to the class "User"
  17. **                                            that will be used to hold the name of the default
  18. **                                            database the user wants to use.
  19. **
  20. **                    NOTE -  Administrators and Operators of a database are not
  21. **                  the same as end users.  There is an attribute already
  22. **                                    defined calles "User" that will be added to the Database
  23. **                                    class to hold the end users.  Administrators and Operators
  24. **                                    were both added to allow for different types of
  25. **                                    administration.
  26. **                                    THE ATTRBUTE DBNAME WILL NOT BE ABLE TO BE REMOVED BECAUSE
  27. **                                    IT IS BEING ADDED TO A CLASS IN THE BASE SCHEMA.  THE ONLY
  28. **                                    WAY TO GET RID OF IT IS TO UNINSTALL DIRECTORY SERVICES AND
  29. **                                    REINSTALL.
  30. **
  31. **        DISCLAIMER
  32. **
  33. **    Novell, Inc. makes no representations or warranties with respect to
  34. **    any NetWare software, and specifically disclaims any express or
  35. **    implied warranties of merchantability, title, or fitness for a
  36. **    particular purpose.
  37. **
  38. **    Distribution of any NetWare software is forbidden without the
  39. **    express written consent of Novell, Inc.  Further, Novell reserves
  40. **    the right to discontinue distribution of any NetWare software.
  41. **
  42. **    Novell is not responsible for lost profits or revenue, loss of use
  43. **    of the software, loss of data, costs of re-creating lost data, the
  44. **    cost of any substitute equipment or program, or claims by any party
  45. **    other than you.  Novell strongly recommends a backup be made before
  46. **    any software is installed.   Technical support for this software
  47. **    may be provided at the discretion of Novell.
  48. **
  49. **    Programmers:
  50. **
  51. **        Ini    Who                                Firm
  52. **        --- ----------------- ------------------------------------------------
  53. **        CRG    Calvin Gaisford        Novell Developer Support.
  54. **
  55. **    History:
  56. **
  57. **        When         Who What
  58. **        -------- --- ---------------------------------------------------------
  59. **        10-21-94 CRG First code.
  60. **         1-04-95 CRG Modified to be generic as an example.
  61. */
  62.  
  63.  
  64.  
  65. /***************************************************************************
  66. **  Libraries Linked in for .exe
  67. **
  68. **  NWCALLS.LIB
  69. **  NWLOCALE.LIB
  70. **  NWNET.LIB
  71. **
  72. */
  73.  
  74.  
  75. /***************************************************************************
  76. **  #defines
  77. */
  78. #define FAILURE  0xFF
  79. #define SUCCESS  0x00
  80.  
  81.  
  82. /****************************************************************************
  83. **    Include headers, macros, etc.
  84. */
  85.  
  86.     /*-----------------------------------------------------------------------
  87.     **    ANSI
  88.     */
  89.     #include <stdio.h>
  90.     #include <stdlib.h>
  91.     #include <string.h>
  92.     #include <conio.h>
  93.  
  94.     /*-----------------------------------------------------------------------
  95.     **    NetWare
  96.     */
  97.     #include <nwnet.h>
  98.     #include <nwcalls.h>
  99.     #include <nwlocale.h>
  100.  
  101.     /*-----------------------------------------------------------------------
  102.     **    Program Global storage
  103.     */
  104.     struct
  105.     {
  106.         NWDSContextHandle context;
  107.     }gs;
  108.     extern unsigned _stklen=8000;
  109.  
  110.  
  111. /***************************************************************************
  112. **    Function Prototypes
  113. */
  114. void processCode(NWDSCCODE ccode);
  115. void addAttr(void);
  116. void defineDBS(void);
  117. void defineDB(void);
  118. void modifyUser(void);
  119. void FatalError(int errorCode);
  120. void InitDS(void);
  121. int  UninitDS(void);
  122.  
  123. /***************************************************************************
  124. **    Main Program
  125. */
  126. //void main(int argc, char *argv[])
  127.  
  128. void main(void)
  129. {
  130.     InitDS();
  131.     addAttr();
  132.     defineDBS();
  133.     defineDB();
  134.     modifyUser();
  135.     UninitDS();
  136. }
  137.  
  138.  
  139.  
  140. /***************************************************************************
  141. **    Processes the Codes returned after extending the Schema.  The most common
  142. ** errors were inlcuded here.  The error codes are at the end of the
  143. ** second programming manual.
  144. */
  145. void processCode(NWDSCCODE ccode)
  146. {
  147.     switch(ccode)
  148.     {
  149.         case    0        :    printf("Successfully Added\n");
  150.                             break;
  151.         case    -615    :
  152.         case    -645    :  printf("Failed -- Already Exists\n");
  153.                             break;
  154.         default        :  printf("Failed -- Returned Error: %d\n", ccode);
  155.                             break;
  156.     }
  157. }
  158.  
  159.  
  160. /***************************************************************************
  161. **    Adds the following Attributes to the Schema: "DBS", "DB", "DBT", "DBV".
  162. */
  163. void addAttr(void)
  164. {
  165.     Attr_Info_T       attrInfo;
  166.     char              attrName[MAX_SCHEMA_NAME_CHARS+2];
  167.     NWDSCCODE            ccode;
  168.  
  169.  
  170.     /*---------------------------------------------------------------
  171.     ** Creating two new attributes: DBAdmin and DBOperator.  These two
  172.     ** attributes will hold the name of NDS groups that allow for
  173.     ** multiple Administrators and Operators.  These are different from
  174.     ** Users of the database.
  175.     */
  176.     memset(&attrInfo, (int) '\0', sizeof(Attr_Info_T));
  177.     attrInfo.asn1ID.length = 0;
  178.     memset(attrInfo.asn1ID.data, 0, MAX_ASN1_NAME);
  179.     attrInfo.attrFlags = DS_SINGLE_VALUED_ATTR;
  180.     attrInfo.attrSyntaxID = SYN_CI_STRING;
  181.     strcpy(attrName, "DBAdmin");
  182.     printf("Adding attribute \"%s\" to Schema...",attrName);
  183.     ccode = NWDSDefineAttr(gs.context, attrName, &attrInfo);
  184.     processCode(ccode);
  185.  
  186.     memset(&attrInfo, (int) '\0', sizeof(Attr_Info_T));
  187.     attrInfo.asn1ID.length = 0;
  188.     memset(attrInfo.asn1ID.data, 0, MAX_ASN1_NAME);
  189.     attrInfo.attrFlags = DS_SINGLE_VALUED_ATTR;
  190.     attrInfo.attrSyntaxID = SYN_CI_STRING;
  191.     strcpy(attrName, "DBOperator");
  192.     printf("Adding attribute \"%s\" to Schema...",attrName);
  193.     ccode = NWDSDefineAttr(gs.context, attrName, &attrInfo);
  194.     processCode(ccode);
  195.  
  196.     memset(&attrInfo, (int) '\0', sizeof(Attr_Info_T));
  197.     attrInfo.asn1ID.length = 0;
  198.     memset(attrInfo.asn1ID.data, 0, MAX_ASN1_NAME);
  199.     attrInfo.attrFlags = DS_SINGLE_VALUED_ATTR;
  200.     attrInfo.attrSyntaxID = SYN_DIST_NAME;
  201.     strcpy(attrName, "DBName");
  202.     printf("Adding attribute \"%s\" to Schema...",attrName);
  203.     ccode = NWDSDefineAttr(gs.context, attrName, &attrInfo);
  204.     processCode(ccode);
  205. }
  206.  
  207.  
  208. /***************************************************************************
  209. **    Defines the new DBServer Class
  210. **        This class is based on the "Top" class.  It is defined so that it
  211. **        can be contained by Organization or Organizational Unit.  It is
  212. **        defined as an object for a Database
  213. **        Server to authenticate to the Tree. (therefore the public and private
  214. **        keys)  Also, the Network Address Attribute was added so clients could
  215. **        find the server through DS and no have to look for SAPs.
  216. */
  217. void defineDBS(void)
  218. {
  219.     NWCLASS_INFO        classInfo;
  220.     NWDS_BUFFER            *classBuf;
  221.     NWDSCCODE            ccode;
  222.     char                    *className="DBServer";
  223.  
  224.     ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &classBuf);
  225.     if(ccode)
  226.         goto _EndProc;
  227.  
  228.     ccode = NWDSInitBuf(gs.context, DSV_DEFINE_CLASS, classBuf);
  229.     if(ccode)
  230.         goto _FreeBuf;
  231.  
  232.     classInfo.classFlags    = (DS_EFFECTIVE_CLASS);
  233.     classInfo.asn1ID.length = 8;
  234.     memset(classInfo.asn1ID.data, 0x00, 8);
  235.  
  236.  
  237.     /*-----------------------------------------------------------------------
  238.     **    Define super class
  239.     */
  240.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  241.     if(ccode)
  242.         goto _FreeBuf;
  243.     ccode = NWDSPutClassItem(gs.context, classBuf, "Top");
  244.     if(ccode)
  245.         goto _FreeBuf;
  246.  
  247.     /*-----------------------------------------------------------------------
  248.     **    Define new containment classes
  249.     */
  250.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  251.     if(ccode)
  252.         goto _FreeBuf;
  253.     ccode = NWDSPutClassItem(gs.context, classBuf, "Organization");
  254.     if(ccode)
  255.         goto _FreeBuf;
  256.     ccode = NWDSPutClassItem(gs.context, classBuf, "Organizational Unit");
  257.     if(ccode)
  258.         goto _FreeBuf;
  259.  
  260.     /*-----------------------------------------------------------------------
  261.     **    Define new naming attributes.
  262.     */
  263.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  264.     if(ccode)
  265.         goto _FreeBuf;
  266.     ccode = NWDSPutClassItem(gs.context, classBuf, "CN");
  267.     if(ccode)
  268.         goto _FreeBuf;
  269.  
  270.     /*-----------------------------------------------------------------------
  271.     **    Define new manditory attributes
  272.     */
  273.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  274.     if(ccode)
  275.         goto _FreeBuf;
  276.     ccode = NWDSPutClassItem(gs.context, classBuf, "CN");
  277.     if(ccode)
  278.         goto _FreeBuf;
  279.  
  280.  
  281.     /*-----------------------------------------------------------------------
  282.     **    Define optional attributes.  These attributes can be changed to fit
  283.     ** any need but to maintain login capabilities, the Private and Public
  284.     ** keys but remain.  The Network Address should also remain to allow
  285.     ** clients to locate the actual server.
  286.     */
  287.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  288.     if(ccode)
  289.         goto _FreeBuf;
  290.     ccode = NWDSPutClassItem(gs.context, classBuf, "Description");
  291.     if(ccode)
  292.         goto _FreeBuf;
  293.     ccode = NWDSPutClassItem(gs.context, classBuf, "Full Name");
  294.     if(ccode)
  295.         goto _FreeBuf;
  296.     ccode = NWDSPutClassItem(gs.context, classBuf, "Private Key");
  297.     if(ccode)
  298.         goto _FreeBuf;
  299.     ccode = NWDSPutClassItem(gs.context, classBuf, "Public Key");
  300.     if(ccode)
  301.         goto _FreeBuf;
  302.     ccode = NWDSPutClassItem(gs.context, classBuf, "See Also");
  303.     if(ccode)
  304.         goto _FreeBuf;
  305.     ccode = NWDSPutClassItem(gs.context, classBuf, "Serial Number");
  306.     if(ccode)
  307.         goto _FreeBuf;
  308.     ccode = NWDSPutClassItem(gs.context, classBuf, "Host Server");
  309.     if(ccode)
  310.         goto _FreeBuf;
  311.     ccode = NWDSPutClassItem(gs.context, classBuf, "Version");
  312.     if(ccode)
  313.         goto _FreeBuf;
  314.     ccode = NWDSPutClassItem(gs.context, classBuf, "User");
  315.     if(ccode)
  316.         goto _FreeBuf;
  317.  
  318.     printf("Adding Class \"%s\" to Schema...",className);
  319.     ccode = NWDSDefineClass(gs.context,
  320.                                                 className,
  321.                                                 &classInfo,
  322.                                                 classBuf);
  323.     processCode(ccode);
  324.  
  325.     _FreeBuf:
  326.         NWDSFreeBuf(classBuf);
  327.     _EndProc:
  328. }
  329.  
  330.  
  331.  
  332. /***************************************************************************
  333. **    Defines the new Database Class.  This class is defined to represent
  334. ** an an object that represents a database.  It can only
  335. ** be contained by a Organization or Organizational Unit.
  336. */
  337. void defineDB(void)
  338. {
  339.     NWCLASS_INFO        classInfo;
  340.     NWDS_BUFFER            *classBuf;
  341.     NWDSCCODE                ccode;
  342.     char                        *className="Database";
  343.  
  344.     ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &classBuf);
  345.     if(ccode)
  346.         goto _EndProc;
  347.  
  348.     ccode = NWDSInitBuf(gs.context, DSV_DEFINE_CLASS, classBuf);
  349.     if(ccode)
  350.         goto _FreeBuf;
  351.  
  352.     classInfo.classFlags    = (DS_EFFECTIVE_CLASS);
  353.     classInfo.asn1ID.length = 8;
  354.     memset(classInfo.asn1ID.data, 0x00, 8);
  355.  
  356.  
  357.     /*-----------------------------------------------------------------------
  358.     **    Define super class
  359.     */
  360.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  361.     if(ccode)
  362.         goto _FreeBuf;
  363.     ccode = NWDSPutClassItem(gs.context, classBuf, "Top");
  364.     if(ccode)
  365.         goto _FreeBuf;
  366.  
  367.     /*-----------------------------------------------------------------------
  368.     **    Define new containment classes
  369.     */
  370.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  371.     if(ccode)
  372.         goto _FreeBuf;
  373.     ccode = NWDSPutClassItem(gs.context, classBuf, "Organizational Unit");
  374.     if(ccode)
  375.         goto _FreeBuf;
  376.     ccode = NWDSPutClassItem(gs.context, classBuf, "Organization");
  377.     if(ccode)
  378.         goto _FreeBuf;
  379.  
  380.     /*-----------------------------------------------------------------------
  381.     **    Define new naming attributes.
  382.     */
  383.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  384.     if(ccode)
  385.         goto _FreeBuf;
  386.     ccode = NWDSPutClassItem(gs.context, classBuf, "CN");
  387.     if(ccode)
  388.         goto _FreeBuf;
  389.  
  390.     /*-----------------------------------------------------------------------
  391.     **    Define new manditory attributes
  392.     */
  393.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  394.     if(ccode)
  395.         goto _FreeBuf;
  396.     ccode = NWDSPutClassItem(gs.context, classBuf, "CN");
  397.     if(ccode)
  398.         goto _FreeBuf;
  399.     ccode = NWDSPutClassItem(gs.context, classBuf, "Host Server");
  400.     if(ccode)
  401.         goto _FreeBuf;
  402.  
  403.     /*-----------------------------------------------------------------------
  404.     **    Define optional attributes
  405.     */
  406.     ccode = NWDSBeginClassItem(gs.context, classBuf);
  407.     if(ccode)
  408.         goto _FreeBuf;
  409.     ccode = NWDSPutClassItem(gs.context, classBuf, "Description");
  410.     if(ccode)
  411.         goto _FreeBuf;
  412.     ccode = NWDSPutClassItem(gs.context, classBuf, "Full Name");
  413.     if(ccode)
  414.         goto _FreeBuf;
  415.     ccode = NWDSPutClassItem(gs.context, classBuf, "See Also");
  416.     if(ccode)
  417.         goto _FreeBuf;
  418.     ccode = NWDSPutClassItem(gs.context, classBuf, "User");
  419.     if(ccode)
  420.         goto _FreeBuf;
  421.     ccode = NWDSPutClassItem(gs.context, classBuf, "DBAdmin");
  422.     if(ccode)
  423.         goto _FreeBuf;
  424.     ccode = NWDSPutClassItem(gs.context, classBuf, "DBOperator");
  425.     if(ccode)
  426.         goto _FreeBuf;
  427.  
  428.     printf("Adding Class \"%s\" to Schema...",className);
  429.     ccode = NWDSDefineClass(gs.context,
  430.                                                 className,
  431.                                                 &classInfo,
  432.                                                 classBuf);
  433.     processCode(ccode);
  434.  
  435.     _FreeBuf:
  436.         NWDSFreeBuf(classBuf);
  437.     _EndProc:
  438. }
  439.  
  440.  
  441.  
  442. /***************************************************************************
  443. **    Modifies the class User
  444. **        This adds a new attribute to the User class called DBName.  This is
  445. **        so that users can store their default database in their user object
  446. **        and avoid having to look up the database every time they run the
  447. **        application.
  448. */
  449. void modifyUser(void)
  450. {
  451.     NWDS_BUFFER            *classBuf;
  452.     NWDSCCODE                ccode;
  453.     char                        *className="User";
  454.  
  455.     ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &classBuf);
  456.     if(ccode)
  457.         goto _EndProc;
  458.  
  459.     ccode = NWDSInitBuf(gs.context, DSV_MODIFY_CLASS_DEF, classBuf);
  460.     if(ccode)
  461.         goto _FreeBuf;
  462.  
  463.  
  464.     /*-----------------------------------------------------------------------
  465.     **    Define new attribute to add
  466.     **        This will be an optional attribute because the class is already
  467.     **        defined.
  468.     */
  469.     ccode = NWDSPutAttrName(gs.context, classBuf, "DBName");
  470.     if(ccode)
  471.         goto _FreeBuf;
  472.  
  473.     printf("Adding attribute \"DBName\" to Class \"%s\"...",className);
  474.     ccode = NWDSModifyClassDef(gs.context,
  475.                                                 className,
  476.                                                 classBuf);
  477.     processCode(ccode);
  478.  
  479.     _FreeBuf:
  480.         NWDSFreeBuf(classBuf);
  481.     _EndProc:
  482. }
  483.  
  484.  
  485.  
  486. /***************************************************************************
  487. **    FatalError is the common exit point for errors.
  488. */
  489. void FatalError(int errorCode)
  490. {
  491.         exit(errorCode);
  492. }
  493.  
  494.  
  495.  
  496. /***************************************************************************
  497. **    Establish context.  These are the standard initializations for DS calls
  498. */
  499. void InitDS(void)
  500. {
  501.     NWDSCCODE dsCcode;
  502.     LCONV            lconvInfo;
  503.  
  504.     dsCcode=NWCallsInit(NULL,NULL);
  505.     if(dsCcode)          /* initialize allowing to call nwcalls functions */
  506.     {
  507.         printf("FatalError during NWCallsInit %X\n",dsCcode);
  508.         FatalError(FAILURE);
  509.     }
  510.     NWLlocaleconv(&lconvInfo);
  511.     dsCcode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
  512.     if(dsCcode)
  513.     {
  514.         printf("NWInitUnicodeTables() returned: %04X\n", dsCcode);
  515.         FatalError(FAILURE);
  516.     }
  517.     gs.context=NWDSCreateContext();
  518.     if(gs.context)
  519.     {
  520.         printf("FatalError during NWDSCreateContext %X\n",gs.context);
  521.         FatalError(FAILURE);
  522.     }
  523. }
  524.  
  525.  
  526.  
  527. /***************************************************************************
  528. **    Release context and clean up.
  529. */
  530. int UninitDS()
  531. {
  532.     NWDSCCODE dsCcode;
  533.  
  534.     dsCcode=NWDSFreeContext(gs.context);
  535.     if(dsCcode)
  536.     {
  537.         printf("FatalError during NWDSFreeContext %X\n",dsCcode);
  538.         FatalError (FAILURE);
  539.     }
  540.     dsCcode=NWFreeUnicodeTables();
  541.     if(dsCcode)
  542.     {
  543.         printf("Error during NWFreeUnicodeTables()\n");
  544.         FatalError (FAILURE);
  545.     }
  546.     return(0);
  547. }
  548.  
  549.  
  550.