home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / PASSWORD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  15.7 KB  |  431 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // password.c
  4. #include "snipit.h"
  5.  
  6. #define NAMELEN  10 // Length of the name fields
  7. #define PLACELEN 20 // Length of the POB field
  8. #define DATELEN  11 // Display length for a date field: mm\dd\yyyy
  9.  
  10. static const char szTblName[]               = "People";
  11. static char szProbTblName[DBIMAXPATHLEN]    = "ProbTbl";
  12. static const char szTblType[]               = szPARADOX;
  13.  
  14. static const char szPassword1[]     = "Password1";
  15. static const char szPassword2[]     = "Password2";
  16.  
  17. static SNIPFAR FLDDesc fldDesc[] = {
  18.               { // Field 1 - First Name
  19.                 1,            // Field number
  20.                 "First Name", // Field name
  21.                 fldZSTRING,   // Field type
  22.                 fldUNKNOWN,   // Field subtype
  23.                 NAMELEN,      // Field size ( 1 or 0, except
  24.                               //     BLOb or CHAR field )
  25.                 0,            // Decimal places ( 0 )
  26.                               //     computed
  27.                 0,            // Offset in record ( 0 )
  28.                 0,            // Length in bytes  ( 0 )
  29.                 0,            // For Null bits    ( 0 )
  30.                 fldvNOCHECKS, // Validity checks   ( 0 )
  31.                 fldrREADWRITE // Rights
  32.               },
  33.               { // Field 2 - Middle Name
  34.                 2, "Middle Name", fldZSTRING, fldUNKNOWN,
  35.                 NAMELEN, 0, 0, 0, 0,
  36.                 fldvNOCHECKS, fldrREADWRITE
  37.               },
  38.               { // Field 3 - Last Name
  39.                 3, "Last Name", fldZSTRING, fldUNKNOWN,
  40.                 NAMELEN, 0, 0, 0, 0,
  41.                 fldvNOCHECKS, fldrREADWRITE
  42.               },
  43.               { // Field 4 - Date of Birth
  44.                 4, "DOB", fldDATE, fldUNKNOWN,
  45.                 0, 0, 0, 0, 0,
  46.                 fldvNOCHECKS, fldrREADWRITE
  47.               },
  48.               { // Field 5 - Place of Birth
  49.                 5, "POB", fldZSTRING, fldUNKNOWN,
  50.                 PLACELEN, 0, 0, 0, 0,
  51.                 fldvNOCHECKS, fldrREADWRITE
  52.               }
  53.              };  // Array of field descriptors
  54.  
  55. // Index descriptor.
  56. static SNIPFAR IDXDesc idxDesc[] = {
  57.             { // Primary Index - Full Name
  58.                "Full Name",  // Name
  59.                1,            // Number
  60.                { NULL },     // Tag name ( for dBase )
  61.                { NULL },     // Optional Format ( BTREE,
  62.                              // HASH, etc )
  63.                TRUE,         // Primary?
  64.                TRUE,         // Unique?
  65.                FALSE,        // Descending?
  66.                TRUE,         // Maintained?
  67.                FALSE,        // SubSet?
  68.                FALSE,        // Expression index?
  69.                NULL,         // for QBE only
  70.                3,            // Fields in key
  71.                1,            // Length in bytes
  72.                FALSE,        // Index out of date?
  73.                0,            // Key type of expression
  74.                { 1,2,3 },    // Array of field numbers
  75.                { 0 },        // Key expression
  76.                { 0 },        // Key condition
  77.                FALSE,        // Case insensitive
  78.                0,            // Block size in bytes
  79.                0             // Restructure number
  80.             }
  81. };
  82.  
  83. // The number of fields in the table.
  84. static const UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  85.  
  86. // Number of indexes to be created when the table is created.
  87. static const UINT16 uNumIndexes = sizeof(idxDesc) / sizeof(idxDesc[0]);
  88.  
  89. static DBIResult ResetPassTable(hDBIDb hDb, pCHAR pszPass, pCHAR pszNewPass);
  90. static DBIResult CreatePassTable(phDBIDb phDb, pCHAR pszPass);
  91. static DBIResult AddRecord(phDBICur hCur, pCHAR pszFirst, pCHAR pszMiddle,
  92.                            pCHAR pszLast, UINT16 uMonth, UINT16 uDay,
  93.                            UINT16 uYear, pCHAR pszPOB);
  94.  
  95. //=====================================================================
  96. //  Function:
  97. //          Password();
  98. //
  99. //  Description:
  100. //          This example will create the table with the first password.  It 
  101. //          will then attempt to open the table and fail because the session 
  102. //          password is not set.  It will add the password to the session and 
  103. //          open the table, then restructure the table to use a different 
  104. //          password.
  105. //=====================================================================
  106. void
  107. Password (void)
  108. {
  109.     DBIResult   rslt;   // Value returned from IDAPI functions
  110.     hDBIDb      hDb;    // Database handle
  111.     hDBICur     hCur;   // Cursor handle
  112.  
  113.     Screen("*** Password Example ***\r\n");
  114.  
  115.     BREAK_IN_DEBUGGER();
  116.  
  117.     Screen("    Initializing IDAPI...");
  118.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  119.     {                                        
  120.         Screen("\r\n*** End of Example ***");
  121.         return;
  122.     }
  123.  
  124.     Screen("\r\n    Setting the database directory...");
  125.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  126.     ChkRslt(rslt, "SetDirectory");
  127.  
  128.     if (CreatePassTable(&hDb, (pCHAR) szPassword1)!= DBIERR_NONE)
  129.     {
  130.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  131.         ChkRslt(rslt, "DeleteTable");
  132.         CloseDbAndExit(&hDb);
  133.         Screen("\r\n*** End of Example ***");
  134.         return;
  135.     }
  136.  
  137.     Screen("    Password set to: %s...", (pCHAR) szPassword1);
  138.     Screen("    Open the table which we created...");
  139.     Screen("        Error expected as we have no passwords set for the "
  140.            "table...");
  141.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  142.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  143.                         xltFIELD, FALSE, NULL, &hCur);
  144.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  145.     {
  146.         // Abort the example if this is not the expected error.
  147.         if (rslt != DBIERR_NOTSUFFTABLERIGHTS)
  148.         {
  149.             rslt = DbiDeleteTable(hDb, (pCHAR) szTblName,(pCHAR) szTblType);
  150.             ChkRslt(rslt, "DeleteTable");
  151.             CloseDbAndExit(&hDb);
  152.             Screen("\r\n*** End of Example ***");
  153.             return;
  154.         }
  155.     }
  156.  
  157.     // Add a password to the session. IDAPI will check the password(s)
  158.     //   that are active in the session and check if they match the password
  159.     //   that is inside the table.
  160.     Screen("\r\n    Add the password, '%s', to the session..." ,
  161.                  (pCHAR) szPassword1);
  162.     rslt = DbiAddPassword((pCHAR) szPassword1);
  163.     ChkRslt(rslt, "AddPassword");
  164.  
  165.     Screen("    Try again to open the table we created...");
  166.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  167.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  168.                         xltFIELD, FALSE, NULL, &hCur);
  169.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  170.     {
  171.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  172.         ChkRslt(rslt, "DeleteTable");
  173.         CloseDbAndExit(&hDb);
  174.         Screen("\r\n*** End of Example ***");
  175.         return;
  176.     }
  177.  
  178.     Screen("\r\n    Table opened succesfully...");
  179.  
  180.     rslt = DbiCloseCursor(&hCur);
  181.     ChkRslt(rslt, "CloseCursor");
  182.  
  183.     Screen("\r\n    Restructure the table to have a new password...");
  184.     if (ResetPassTable(hDb, (pCHAR) szPassword1, (pCHAR) szPassword2)
  185.         != DBIERR_NONE)
  186.     {
  187.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  188.         ChkRslt(rslt, "DeleteTable");
  189.         CloseDbAndExit(&hDb);
  190.         Screen("\r\n*** End of Example ***");
  191.         return;
  192.     }
  193.  
  194.     Screen("    We have changed the password from '%s' to '%s'...",
  195.            (pCHAR) szPassword1, (pCHAR) szPassword2);
  196.     Screen("    Try to open the table which we created...");
  197.     Screen("        Error expected as we have no passwords set for the "
  198.            "table...");
  199.  
  200.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  201.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  202.                         xltFIELD, FALSE, NULL, &hCur);
  203.     ChkRslt(rslt, "OpenTable");
  204.  
  205.     Screen("\r\n    Add the correct password, '%s', to the "
  206.            " session...", (pCHAR) szPassword2);
  207.     rslt = DbiAddPassword((pCHAR) szPassword2);
  208.     ChkRslt(rslt, "AddPassword");
  209.  
  210.     Screen("    Try again to open the table we created...");
  211.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  212.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  213.                         xltFIELD, FALSE, NULL, &hCur);
  214.     ChkRslt(rslt, "OpenTable");
  215.  
  216.     Screen("\r\n    Table opened succesfully...");
  217.     Screen("\r\n    Close the table...");
  218.     rslt = DbiCloseCursor(&hCur);
  219.     ChkRslt(rslt, "CloseCursor");
  220.  
  221.     Screen("    Delete the table and remaining indexes...");
  222.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  223.     ChkRslt(rslt, "DeleteTable");
  224.  
  225.     Screen("    Close the database and exit IDAPI...");
  226.     CloseDbAndExit(&hDb);
  227.  
  228.     Screen("\r\n*** End of Example ***");
  229. }
  230.  
  231. //=====================================================================
  232. //  Function:
  233. //          ResetPassTable(hDb, pszPass, pszNewPass);
  234. //
  235. //  Input:  hb          - Database handle
  236. //          pszPass     - Session password
  237. //          pszNewPass  - New password for the table (pCHAR)
  238. //
  239. //  Return: result of the table initialization.
  240. //
  241. //  Description:
  242. //          This function will add a password to the session, close the
  243. //          table, and restructure the table to change the password
  244. //          that is presently set in the table.  Then it will drop the
  245. //          password from the session.
  246. //=====================================================================
  247. DBIResult
  248. ResetPassTable (hDBIDb hDb, pCHAR pszPass, pCHAR pszNewPass)
  249. {
  250.     DBIResult   rslt;      // Value returned from IDAPI functions
  251.     CRTblDesc   crTblDesc; // Table descriptor
  252.  
  253.     // Initialize the create table descriptor
  254.     memset(&crTblDesc, 0, sizeof(CRTblDesc)); 
  255.     strcpy(crTblDesc.szTblName, szTblName);
  256.     strcpy(crTblDesc.szTblType, szTblType);
  257.     crTblDesc.bProtected    = TRUE;
  258.     strcpy(crTblDesc.szPassword, pszNewPass);
  259.     crTblDesc.bPack         = TRUE;
  260.  
  261.     rslt = DbiAddPassword((pCHAR) pszPass);
  262.     ChkRslt(rslt, "AddPassword");
  263.  
  264.     // Restructure the table using information supplied in the table
  265.     // descriptor above.
  266.     rslt = DbiDoRestructure(hDb, 1, &crTblDesc, NULL,
  267.                             NULL, (pCHAR) szProbTblName, FALSE);
  268.     if (ChkRslt(rslt, "DoRestructure") != DBIERR_NONE)
  269.     {
  270.         return DBIERR_INVALIDRESTROP;
  271.     }
  272.  
  273.     // Release the password from the session so we can test the
  274.     // password functions.  We opened it in the function above so now
  275.     // we need to drop it to retest the newly added table.
  276.     rslt = DbiDropPassword((pCHAR) pszPass);
  277.     ChkRslt(rslt, "DropPassword");
  278.  
  279.     return rslt;
  280. }
  281.  
  282.  
  283. //=====================================================================
  284. //  Function:
  285. //          CreatePassTable();
  286. //
  287. //  Input:  pointer to the database handle (phDBIDb), password (pCHAR).
  288. //
  289. //  Return: result of the table initialization.
  290. //
  291. //  Description:
  292. //          This function will first create the table with a password.
  293. //          Then it will open the table, fill it with data, and close
  294. //          the table.
  295. //=====================================================================
  296. DBIResult
  297. CreatePassTable (phDBIDb phDb, pCHAR pszPass)
  298. {
  299.     DBIResult   rslt;              // Value returned from IDAPI functions
  300.     hDBICur     hCur;              // Cursor handle for the table that is
  301.                                    //   created
  302.     CRTblDesc   crTblDesc;         // Table descriptor
  303.     BOOL        bOverWrite = TRUE; // Overwrite, yes/no flag
  304.  
  305.  
  306.     // Register the password with the session.
  307.     rslt = DbiAddPassword((pCHAR) pszPass);
  308.     ChkRslt(rslt, "AddPassword");
  309.  
  310.     // Initialize the table create descriptor.
  311.     memset(&crTblDesc, 0, sizeof(CRTblDesc));
  312.     strcpy(crTblDesc.szTblName, szTblName);
  313.     strcpy(crTblDesc.szTblType, szTblType);
  314.     crTblDesc.bProtected    = TRUE;
  315.     strcpy(crTblDesc.szPassword, pszPass);
  316.     crTblDesc.bPack         = TRUE;
  317.     crTblDesc.iFldCount     = uNumFields;
  318.     crTblDesc.pfldDesc      = fldDesc;
  319.     crTblDesc.iIdxCount     = uNumIndexes;
  320.     crTblDesc.pidxDesc      = idxDesc;
  321.  
  322.     Screen("    Creating table and indexes...");
  323.     rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDesc);
  324.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  325.     {
  326.         return rslt;
  327.     }
  328.  
  329.     // Open the table.
  330.     rslt = DbiOpenTable(*phDb, (pCHAR) szTblName, (pCHAR) szTblType,
  331.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  332.                         xltFIELD, FALSE, NULL, &hCur);
  333.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  334.     {
  335.         return rslt;
  336.     }
  337.  
  338.     Screen("    Add records to the table...");
  339.     AddRecord(&hCur, "Klaus", "John", "Lockwood", 7, 28, 1968,
  340.               "Chicago");
  341.     AddRecord(&hCur, "Tracy", "Ann", "Browning", 12, 27, 1969,
  342.               "Hermosa Beach");
  343.     AddRecord(&hCur, "John", "Boy", "Krull", 2, 7, 1954,
  344.               "Ohmaha");
  345.     AddRecord(&hCur, "Goliath", "Joel", "Raccah", 4, 13, 1970,
  346.               "Tel Aviv");
  347.  
  348.     rslt = DbiCloseCursor(&hCur);
  349.     ChkRslt(rslt, "CloseCursor");
  350.  
  351.     // Release the password from the session so we can test the password
  352.     // functions.
  353.     rslt = DbiDropPassword((pCHAR) pszPass);
  354.     ChkRslt(rslt, "DropPassword");
  355.  
  356.     return rslt;
  357. }
  358.  
  359. //=====================================================================
  360. //  Function:
  361. //          AddRecord(phCur, pszFirst, pszMiddle, pszLast, uMonth,
  362. //                    uDay, uYear, pszPOB);
  363. //
  364. //  Input:  phCur       - Pointer to the cursor handle
  365. ///         pszFirst    - First name
  366. //          pszMiddle   - Middle name
  367. //          pszLast     - Last name
  368. //          uMonth      - Month of birth
  369. //          uDay        - Day of birth
  370. //          uYear       - Year of birth
  371. //          pszPOB      - Place of birth
  372. //
  373. //  Return: Result of adding the record to the table
  374. //
  375. //  Description:
  376. //          This function will add a record to the given table.
  377. //=====================================================================
  378. DBIResult
  379. AddRecord (phDBICur phCur, pCHAR pszFirst, pCHAR pszMiddle, pCHAR pszLast,
  380.            UINT16 uMonth, UINT16 uDay, UINT16 uYear, pCHAR pszPOB)
  381. {
  382.     DBIDATE     Date;           // Date structure
  383.     DBIResult   rslt;           // Value returned from IDAPI functions
  384.     CURProps    TblProps;       // Table properties
  385.     pBYTE       pRecBuf;        // Record buffer
  386.  
  387.     // Allocate a record buffer.
  388.     rslt = DbiGetCursorProps(*phCur, &TblProps);
  389.     ChkRslt(rslt, "GetCursorProps");
  390.  
  391.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  392.     if (pRecBuf == NULL)
  393.     {
  394.         return DBIERR_NOMEMORY;
  395.     }
  396.  
  397.     // Make sure we're starting with a clean record buffer.
  398.     rslt = DbiInitRecord(*phCur, pRecBuf);
  399.     ChkRslt(rslt, "InitRecord");
  400.  
  401.     // First Name.
  402.     rslt = DbiPutField(*phCur, 1, pRecBuf, (pBYTE) pszFirst);
  403.     ChkRslt(rslt, "PutField");
  404.  
  405.     // Middle Name.
  406.     rslt = DbiPutField(*phCur, 2, pRecBuf, (pBYTE) pszMiddle);
  407.     ChkRslt(rslt, "PutField");
  408.  
  409.     // Last Name.
  410.     rslt = DbiPutField(*phCur, 3, pRecBuf, (pBYTE) pszLast);
  411.     ChkRslt(rslt, "PutField");
  412.  
  413.     // DOB.
  414.     rslt = DbiDateEncode(uMonth, uDay, uYear, &Date);
  415.     ChkRslt(rslt, "DateEncode");
  416.  
  417.     rslt = DbiPutField(*phCur, 4,  pRecBuf, (pBYTE) &Date);
  418.     ChkRslt(rslt, "PutField");
  419.  
  420.     // Place of Birth.
  421.     rslt = DbiPutField(*phCur, 5,  pRecBuf, (pBYTE) pszPOB);
  422.     ChkRslt(rslt, "PutField");
  423.  
  424.     rslt = DbiInsertRecord(*phCur, dbiNOLOCK, pRecBuf),
  425.     ChkRslt(rslt, "InsertRecord");
  426.  
  427.     free(pRecBuf);
  428.  
  429.     return rslt;
  430. }
  431.