{ password.pas } program Password; {$IfDef VER80} uses SnipTool, SnipData, SysUtils, WinTypes, WinProcs, DbiProcs, DbiTypes, DbiErrs; {$Else} uses WinTypes, WinCrt, Strings, DbiProcs, DbiTypes, DbiErrs, SnipTool, SnipData; {$EndIf} const NAMELEN = 10; { Set the length of the name fields } PLACELEN = 20; { Set the length of the POB field } DATELEN = 11; { Length that a date will be displayed: mm\dd\yyyy } szTblName = 'People'; { Name of table to be created. } szTempTblName = 'TmpPeple'; { Temporary table name for the restructured file } szProbTblName = 'ProbTbl'; { Temporary table name for any problems } szTblType = szPARADOX; { Table type to use. } szPassword1 = 'Dino'; { First password that is used in this example. } szPassword2 = 'Fred'; { Second password that is used in this example. } fldDes: array[0..4] of FLDDesc = ( ( { Field 1 - First Name } iFldNum: 1; { Field Number } szName: 'First Name'; { Field Name } iFldType: fldZSTRING; { Field Type } iSubType: fldUNKNOWN; { Field Subtype } iUnits1: NAMELEN; { Field Size ( 1 or 0 ) } iUnits2: 0; { Decimal places ( 0 ) } iOffset: 0; { Offset in record ( 0 ) } iLen: 0; { Length in Bytes ( 0 ) } iNullOffset: 0; { For Null Bits ( 0 ) } efldvVchk: fldvNOCHECKS; { Validiy checks ( 0 ) } efldrRights: fldrREADWRITE { Rights } ), ( { Field 2 - Middle Name } iFldNum: 2; szName: 'Middle Name'; iFldType: fldZSTRING; iSubType: fldUNKNOWN; iUnits1: NAMELEN; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ), ( { Field 3 - Last Name } iFldNum: 3; szName: 'Last Name'; iFldType: fldZSTRING; iSubType: fldUNKNOWN; iUnits1: NAMELEN; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ), ( { Field 4 - Date of Birth } iFldNum: 4; szName: 'DOB'; iFldType: fldDATE; iSubType: fldUNKNOWN; iUnits1: 0; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ), ( { Field 5 - Place of Birth } iFldNum: 5; szName: 'POB'; iFldType: fldZSTRING; iSubType: fldUNKNOWN; iUnits1: PLACELEN; iUnits2: 0; iOffset: 0; iLen: 0; iNullOffset: 0; efldvVchk: fldvNOCHECKS; efldrRights: fldrREADWRITE ) ); { Array of field descriptors } { Index Descriptor } idxDes: IDXDesc = ( { Primary Index - Full Name } szName: 'Full Name'; { Name } iIndexId: 1; { Number } szTagName: ''; { Tag Name ( for dBase ) } szFormat: ''; { Optional Format } bPrimary: TRUE; { Primary? } bUnique: TRUE; { Unique? } bDescending: FALSE; { Descending? } bMaintained: TRUE; { Maintained? } bSubset: FALSE; { SubSet? } bExpIdx: FALSE; { Expression index? } iCost: 0; { for QBE only } iFldsInKey: 3; { Fields in key } iKeyLen: 1; { Length in bytes } bOutofDate: FALSE; { Index out of date? } iKeyExpType: 0; { Key Type of Expression } aiKeyFld: ( 1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0); { Array of field numbers } szKeyExp: ''; { Key expression } szKeyCond: ''; { Key Condition } bCaseInsensitive: FALSE; { Case insensitive } iBlockSize: 0; { Block size in bytes } iRestrNum: 0; { Restructure number } iUnUsed: (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) ); ecrFldOp: array[1..5] of CROpType = (crCOPY, crCOPY, crCOPY, crCOPY, crCOPY); ecrIdxOp: CROpType = (crADD); {===================================================================== Code: AddRecord(); Input: pointer to the cursor handle, first name (pCHAR), middle name (pCHAR), last name (pCHAR), Month of Birth (UINT16), Day of Birth (UINT16), Year of Birth (UINT16), Place of birth (pCHAR). Return: Result of adding the record to the table Description: This function will add a record to the given table. ===================================================================== } Function AddRecord (hCur: hDBICur; pszFirst: pCHAR; pszMiddle: pCHAR; pszLast: pCHAR; uMonth: UINT16; uDay: UINT16; uYear: UINT16; pszPOB: pCHAR): DBIResult; var dDate: DATE; { Date structure } rslt: DBIResult; { Value returned from IDAPI functions } TblProps: CURProps; { Table Properties } pRecBuf: pBYTE; { Record Buffer } begin { Allocate a record buffer } ChkRslt(DbiGetCursorProps(hCur, TblProps), DBIERR_NONE, ' Error - GetCursorProps.'); GetMem(pRecBuf, TblProps.iRecBufSize * sizeof(BYTE)); if not Assigned(pRecBuf) then begin AddRecord := DBIERR_NOMEMORY; exit; end; ChkRslt(DbiInitRecord(hCur, pRecBuf), DBIERR_NONE, ' Error - InitRecord.'); { First Name } ChkRslt(DbiPutField(hCur, 1, pRecBuf, pBYTE(pszFirst)), DBIERR_NONE, ' Error - PutField.'); { Middle Name } ChkRslt(DbiPutField(hCur, 2, pRecBuf, pBYTE(pszMiddle)), DBIERR_NONE, ' Error - PutField.'); { Last Name } ChkRslt(DbiPutField(hCur, 3, pRecBuf, pBYTE(pszLast)), DBIERR_NONE, ' Error - PutField.'); { DOB } ChkRslt(DbiDateEncode(uMonth, uDay, uYear, dDate), DBIERR_NONE, ' Error - DateEncode.'); ChkRslt(DbiPutField(hCur, 4, pRecBuf, @dDate), DBIERR_NONE, ' Error - PutField.'); { Place fo Birth } ChkRslt(DbiPutField(hCur, 5, pRecBuf, pBYTE(pszPOB)), DBIERR_NONE, ' Error - PutField.'); rslt := ChkRslt(DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf), DBIERR_NONE, ' Error - InsertRecord.'); FreeMem(pRecBuf, TblProps.iRecBufSize * sizeof(BYTE)); AddRecord := rslt; end; {===================================================================== Code: InitPassTable(); Input: pointer to the database handle, password, and restruction boolean. Return: result of the table initialization. Description: This function will create or restructure a table and fill the table with a number of records. The function uses another function called AddRecord which adds records to the table. This function also inserts a password into the table. ===================================================================== } function InitPassTable (phDb: hDBIDb; szPass: pCHAR; bRestructure: BOOL; szNewPass: pCHAR; phRestCur: hDBICur): DBIResult; var hCur: hDBICur; { Cursor handle for the table that is created } crTblDsc: CRTblDesc; { Table Descriptor } uNumFields, uNumIndexes: INT16; const bOverWrite: BOOL = TRUE; { Overwrite, yes/no flag } rslt: DBIResult = 0; { Value returned from IDAPI functions } begin { The number of fields in the table - note that fldDesc is defined globally } uNumFields := trunc(sizeof(fldDes) / sizeof (fldDes[0])); { Number of indexes to be created when the table is created - note that idxDesc is defined globally. } uNumIndexes := trunc(sizeof(idxDes) / sizeof(idxDes)); { Set the name and the type of the table } FillChar(crTblDsc, SizeOf(CRTblDesc), #0); { Clear the buffer. } StrCopy(crTblDsc.szTblName, szTblName); { name of the table } StrCopy(crTblDsc.szTblType, szTblType); { Type of table } { Set the Password information } crTblDsc.bProtected := TRUE; { Boolean for password enabled } StrCopy(crTblDsc.szPassword, szNewPass); { Password for table. } crTblDsc.bPack := TRUE; { pack the table } ChkRslt(DbiAddPassword(pCHAR(szPass)), DBIERR_NONE, ' Error - AddPassword.'); if (not bRestructure) then begin { Initialize the table create descriptor. } crTblDsc.iFldCount := uNumFields; { number of fields } crTblDsc.pfldDesc := @fldDes; { Field descriptor } crTblDsc.iIdxCount := uNumIndexes ; { Number of indexes } crTblDsc.pidxDesc := @idxDes; { Index descriptor } Screen(' Creating table and indexes...'); rslt := ChkRslt(DbiCreateTable(phDb, bOverWrite, crTblDsc), DBIERR_NONE, ' Error - CreateTable.'); if (rslt <> DBIERR_NONE) then begin InitPassTable := rslt; exit; end; rslt := ChkRslt(DbiOpenTable(phDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.'); if (rslt <> DBIERR_NONE) then begin InitPassTable := rslt; exit; end; 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'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); end else begin { Close the table so it can be Restructured. } ChkRslt(DbiCloseCursor(phRestCur), DBIERR_NONE, ' Error - CloseCursor.'); { Restructure the table using information supplied in the Table Descrpitor above. } Screen(' Restructure the '+szTempTblName+' table'+ ' to have a new password...'); if (ChkRslt(DbiDoRestructure(phDb, 1, @crTblDsc, pCHAR(szTempTblName), nil, pCHAR(szProbTblName), FALSE), DBIERR_NONE, ' Error - DoRestructure.') <> DBIERR_NONE) then begin Screen(' Delete the sorted table ('+szTempTblName+')...'); ChkRslt(DbiDeleteTable(phDb, pCHAR(szTempTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); InitPassTable := DBIERR_INVALIDRESTROP; end; end; { 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. } ChkRslt(DbiDropPassword(pCHAR(szPass)), DBIERR_NONE, ' Error - DropPassword.'); InitPassTable := rslt; end; {===================================================================== Code: Password(); Input: None. Return: None. 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. ===================================================================== } var rslt: DBIResult; { Value returned from IDAPI functions } hDb: hDBIDb; { Database handle } hCur: hDBICur; { Cursor handle } begin Screen('*** Password Example ***'); Screen(' Initializing IDAPI...'); if (InitAndConnect(hDb) <> DBIERR_NONE) then { Terminate example if } begin Screen(''); Screen('*** End of Example ***'); exit; end; Screen(''); Screen(' Setting the default Database directory...'); ChkRslt(DbiSetDirectory(hDb, pCHAR(szTblDirectory)), DBIERR_NONE, ' Error - SetDirectory.'); { Create the table and fill with data and the given password and pass a nil to function as we are not restructuring the table yet. } if (InitPassTable(hDb, pCHAR(szPassword1), FALSE, pCHAR(szPassword1), nil) <> DBIERR_NONE) then begin DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; Screen(' Password set to: '+szPassword1+'...'); Screen(' Open the table which we just created...'); Screen(''); Screen(' Error expected as we have no passwords setup yet to'+ ' open table...'); Screen(''); rslt := ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.'); if (rslt <> DBIERR_NONE) then begin { Abort the example if this is not the expected error. } if (rslt <> DBIERR_NOTSUFFTABLERIGHTS) then begin ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); Screen(''); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; end; { 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(' '); Screen(' Add the password: '+szPassword1+' to the session...'); ChkRslt(DbiAddPassword(pCHAR(szPassword1)), DBIERR_NONE, ' Error - AddPassword.'); Screen(' Try again to open the table we created...'); if (ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType) , nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.') <> DBIERR_NONE) then begin ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); Screen(''); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; Screen(''); Screen(' Table opened succesfully...'); { Restructure the table to change the password to the new one. } if(InitPassTable(hDb, pCHAR(szPassword1), TRUE, pCHAR(szPassword2), hCur) <> DBIERR_NONE) then begin ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); Screen(''); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; Screen(' We have changed the password from: '+szPassword1+ ' to: '+szPassword2+'...'); Screen(' Try to Open the table which we just created...'); Screen(''); Screen(' Error expected as we have no passwords set yet for'+ ' this table...'); Screen(''); if (ChkRslt(DbiOpenTable(hDb, pCHAR(szTempTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.') <> DBIERR_NONE) then Screen(' '); Screen(' Add the correct password: '+szPassword2+ ' to the session...'); ChkRslt(DbiAddPassword(pCHAR(szPassword2)), DBIERR_NONE, ' Error - AddPassword.'); Screen(' Try again to open the table we created...'); ChkRslt(DbiOpenTable(hDb, pCHAR(szTempTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.'); Screen(''); Screen(' Table opened succesfully...'); Screen(''); Screen(' Close the Table - table must be closed before'+ ' deleting it...'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); Screen(' Delete the table and remaining indexes...'); ChkRslt(DbiDeleteTable(hDb, pCHAR(szTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); { Delete the Restructured table and its related file from disk } ChkRslt(DbiDeleteTable(hDb, pCHAR(szTempTblName), pCHAR(szTblType)), DBIERR_NONE, ' Error - DeleteTable.'); Screen(''); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen('*** End of Example ***'); end.