// BDE - (C) Copyright 1995 by Borland International // password.c #include "snipit.h" #define NAMELEN 10 // Length of the name fields. #define PLACELEN 20 // Length of the Place of Birth field. #define DATELEN 11 // Display length for a date field: mm\dd\yyyy. static const char szTblName[] = "People"; static char szProbTblName[DBIMAXPATHLEN] = "ProbTbl"; static const char szTblType[] = szPARADOX; static const char szPassword1[] = "Password1"; static const char szPassword2[] = "Password2"; static SNIPFAR FLDDesc fldDesc[] = { { // Field 1 - First Name 1, // Field number "First Name", // Field name fldZSTRING, // Field type fldUNKNOWN, // Field subtype NAMELEN, // Field size (1 or 0, except // BLOB or CHAR field) 0, // Decimal places ( 0 ) // computed 0, // Offset in record ( 0 ) 0, // Length in bytes ( 0 ) 0, // For NULL bits ( 0 ) fldvNOCHECKS, // Validity checks ( 0 ) fldrREADWRITE // Rights }, { // Field 2 - Middle Name 2, "Middle Name", fldZSTRING, fldUNKNOWN, NAMELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 3 - Last Name 3, "Last Name", fldZSTRING, fldUNKNOWN, NAMELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 4 - Date of Birth 4, "DOB", fldDATE, fldUNKNOWN, 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 5 - Place of Birth 5, "POB", fldZSTRING, fldUNKNOWN, PLACELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE } }; // Array of field descriptors. // Index descriptor. static SNIPFAR IDXDesc idxDesc[] = { { // Primary Index - Full Name "Full Name", // Name 1, // Number { NULL }, // Tag name (for dBase) { NULL }, // Optional format (BTREE, // HASH, etc) TRUE, // Primary? TRUE, // Unique? FALSE, // Descending? TRUE, // Maintained? FALSE, // SubSet? FALSE, // Expression index? NULL, // For QBE only 3, // Fields in key 1, // Length in bytes FALSE, // Index out of date? 0, // Key type of expression { 1,2,3 }, // Array of field numbers { 0 }, // Key expression { 0 }, // Key condition FALSE, // Case insensitive 0, // Block size in bytes 0 // Restructure number } }; // The number of fields in the table. static const unsigned uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]); // Number of indexes to be created when the table is created. static const unsigned uNumIndexes = sizeof(idxDesc) / sizeof(idxDesc[0]); static DBIResult ResetPassTable(hDBIDb hDb, pCHAR pszPass, pCHAR pszNewPass); static DBIResult CreatePassTable(phDBIDb phDb, pCHAR pszPass); static DBIResult AddRecord(phDBICur hCur, pCHAR pszFirst, pCHAR pszMiddle, pCHAR pszLast, UINT16 uMonth, UINT16 uDay, UINT16 uYear, pCHAR pszPOB); //===================================================================== // Function: // Password(); // // Description: // This example will create the table with the first password. It // will then attempt to open the table and fail because the session // password is not set. It will add the password to the session and // open the table, then restructure the table to use a different // password. //===================================================================== void Password (void) { DBIResult rslt; // Value returned from IDAPI functions hDBIDb hDb; // Database handle hDBICur hCur; // Cursor handle Screen("*** Password Example ***\r\n"); BREAK_IN_DEBUGGER(); Screen(" Initializing IDAPI..."); if (InitAndConnect(&hDb) != DBIERR_NONE) { Screen("\r\n*** End of Example ***"); return; } Screen("\r\n Setting the database directory..."); rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory); ChkRslt(rslt, "SetDirectory"); if (CreatePassTable(&hDb, (pCHAR) szPassword1)!= DBIERR_NONE) { rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Password set to: %s...", (pCHAR) szPassword1); Screen(" Open the table which we created..."); Screen(" Error expected as we have no passwords set for the " "table..."); rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { // Cancel the example if this is not the expected error. if (rslt != DBIERR_NOTSUFFTABLERIGHTS) { rslt = DbiDeleteTable(hDb, (pCHAR) szTblName,(pCHAR) szTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } } // Add a password to the session. IDAPI will check the password(s) // that are active in the session and check if they match the password // that is inside the table. Screen("\r\n Add the password, '%s', to the session..." , (pCHAR) szPassword1); rslt = DbiAddPassword((pCHAR) szPassword1); ChkRslt(rslt, "AddPassword"); Screen(" Try again to open the table we created..."); rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen("\r\n Table opened succesfully..."); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); Screen("\r\n Restructure the table to have a new password..."); if (ResetPassTable(hDb, (pCHAR) szPassword1, (pCHAR) szPassword2) != DBIERR_NONE) { rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" We have changed the password from '%s' to '%s'...", (pCHAR) szPassword1, (pCHAR) szPassword2); Screen(" Try to open the table which we created..."); Screen(" Error expected as we have no passwords set for the " "table..."); rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); ChkRslt(rslt, "OpenTable"); Screen("\r\n Add the correct password, '%s', to the " " session...", (pCHAR) szPassword2); rslt = DbiAddPassword((pCHAR) szPassword2); ChkRslt(rslt, "AddPassword"); Screen(" Try again to open the table we created..."); rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); ChkRslt(rslt, "OpenTable"); Screen("\r\n Table opened succesfully..."); Screen("\r\n Close the table..."); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); Screen(" Delete the table and remaining indexes..."); rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType); ChkRslt(rslt, "DeleteTable"); Screen(" Close the database and exit IDAPI..."); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); } //===================================================================== // Function: // ResetPassTable(hDb, pszPass, pszNewPass); // // Input: hb - Database handle // pszPass - Session password // pszNewPass - New password for the table // // Return: Result of the table initialization // // Description: // This function will add a password to the session, close the // table, and restructure the table to change the password // that is presently set in the table, then it will drop the // password from the session. //===================================================================== DBIResult ResetPassTable (hDBIDb hDb, pCHAR pszPass, pCHAR pszNewPass) { DBIResult rslt = 0; // Value returned from IDAPI functions CRTblDesc crTblDesc; // Table descriptor // Initialize the table create descriptor. memset(&crTblDesc, 0, sizeof(CRTblDesc)); strcpy(crTblDesc.szTblName, szTblName); strcpy(crTblDesc.szTblType, szTblType); crTblDesc.bProtected = TRUE; strcpy(crTblDesc.szPassword, pszNewPass); crTblDesc.bPack = TRUE; rslt = DbiAddPassword((pCHAR) pszPass); ChkRslt(rslt, "AddPassword"); // Restructure the table using information supplied in the table // descriptor above. rslt = DbiDoRestructure(hDb, 1, &crTblDesc, NULL, NULL, (pCHAR) szProbTblName, FALSE); if (ChkRslt(rslt, "DoRestructure") != DBIERR_NONE) { return DBIERR_INVALIDRESTROP; } // Release the password from the session so we can test the // password functions. We opened it in the function above so now // we need to drop it to retest the newly added table. rslt = DbiDropPassword((pCHAR) pszPass); ChkRslt(rslt, "DropPassword"); return rslt; } //===================================================================== // Function: // CreatePassTable(phDb, pszPass); // // Input: phDb - Pointer to the database handle // pszPass - Password // // Return: Result of the table initialization // // Description: // This function will first create the table with a password. // Then it will open the table, fill it with data, and close // the table. //===================================================================== DBIResult CreatePassTable (phDBIDb phDb, pCHAR pszPass) { DBIResult rslt = DBIERR_NONE;// Value returned from IDAPI functions hDBICur hCur; // Cursor handle for the table that is // created CRTblDesc crTblDesc; // Table descriptor BOOL bOverWrite = TRUE; // Overwrite, yes/no flag // Register the password with the session. rslt = DbiAddPassword((pCHAR) pszPass); ChkRslt(rslt, "AddPassword"); // Initialize the table create descriptor. memset(&crTblDesc, 0, sizeof(CRTblDesc)); strcpy(crTblDesc.szTblName, szTblName); strcpy(crTblDesc.szTblType, szTblType); crTblDesc.bProtected = TRUE; strcpy(crTblDesc.szPassword, pszPass); crTblDesc.bPack = TRUE; crTblDesc.iFldCount = uNumFields; crTblDesc.pfldDesc = fldDesc; crTblDesc.iIdxCount = uNumIndexes; crTblDesc.pidxDesc = idxDesc; Screen(" Creating table and indexes..."); rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDesc); if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE) { return rslt; } // Open the table. rslt = DbiOpenTable(*phDb, (pCHAR) szTblName, (pCHAR) szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { return rslt; } Screen(" Add records to the table..."); rslt = AddRecord(&hCur, "Klaus", "John", "Lockwood", 7, 28, 1968, "Chicago"); rslt = AddRecord(&hCur, "Tracy", "Ann", "Browning", 12, 27, 1969, "Hermosa Beach"); rslt = AddRecord(&hCur, "John", "Boy", "Krull", 2, 7, 1954, "Ohmaha"); rslt = AddRecord(&hCur, "Goliath", "Joel", "Raccah", 4, 13, 1970, "Tel Aviv"); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); // Release the password from the session so we can test the password // functions. rslt = DbiDropPassword((pCHAR) pszPass); ChkRslt(rslt, "DropPassword"); return rslt; } //===================================================================== // Function: // AddRecord(phCur, pszFirst, pszMiddle, pszLast, uMonth, // uDay, uYear, pszPOB); // // Input: phCur - Pointer to the cursor handle /// pszFirst - First name // pszMiddle - Middle name // pszLast - Last name // uMonth - Month of birth // uDay - Day of birth // uYear - Year of birth // pszPOB - Place of birth // // Return: Result of adding the record to the table // // Description: // This function will add a record to the given table. //===================================================================== DBIResult AddRecord (phDBICur phCur, pCHAR pszFirst, pCHAR pszMiddle, pCHAR pszLast, UINT16 uMonth, UINT16 uDay, UINT16 uYear, pCHAR pszPOB) { DBIDATE Date; // Date structure DBIResult rslt; // Value returned from IDAPI functions CURProps TblProps; // Table properties pBYTE pRecBuf; // Record buffer // Allocate a record buffer. rslt = DbiGetCursorProps(*phCur, &TblProps); ChkRslt(rslt, "GetCursorProps"); pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE)); if (pRecBuf == NULL) { return DBIERR_NOMEMORY; } // Make sure we're starting with a clean record buffer. rslt = DbiInitRecord(*phCur, pRecBuf); ChkRslt(rslt, "InitRecord"); // First name. rslt = DbiPutField(*phCur, 1, pRecBuf, (pBYTE) pszFirst); ChkRslt(rslt, "PutField"); // Middle name. rslt = DbiPutField(*phCur, 2, pRecBuf, (pBYTE) pszMiddle); ChkRslt(rslt, "PutField"); // Last name. rslt = DbiPutField(*phCur, 3, pRecBuf, (pBYTE) pszLast); ChkRslt(rslt, "PutField"); // Date of birth. rslt = DbiDateEncode(uMonth, uDay, uYear, &Date); ChkRslt(rslt, "DateEncode"); rslt = DbiPutField(*phCur, 4, pRecBuf, (pBYTE) &Date); ChkRslt(rslt, "PutField"); // Place of birth. rslt = DbiPutField(*phCur, 5, pRecBuf, (pBYTE) pszPOB); ChkRslt(rslt, "PutField"); rslt = DbiInsertRecord(*phCur, dbiNOLOCK, pRecBuf), ChkRslt(rslt, "InsertRecord"); free(pRecBuf); return rslt; }