int writeState() { FILE *fp[2]; if ( ! _dir_exists(_key_file_dir()) ) return 0; if ( ! pppCheckFlags(PPP_FLAGS_PRESENT)) { /* Update the key file to include flags */ pppSetFlags(PPP_FLAGS_PRESENT); fp[0] = fopen(_key_file_name(), "w"); if (fp[0]) { _write_data(seqKey(), fp[0]); fclose(fp[0]); } } fp[0] = fopen(_cnt_file_name(), "w"); fp[1] = fopen(_gen_file_name(), "w"); if (fp[0] && fp[1]) { _write_data(currPasscodeNum(), fp[0]); fclose(fp[0]); _write_data(lastCardGenerated(), fp[1]); fclose(fp[1]); return 1; } else { if (fp[0]) fclose(fp[0]); if (fp[1]) fclose(fp[1]); } return 0; }
/** * Query Cache Pool (If the _WRITE_ Request is Matched) * If it's fully matched, update cache and return TRUE, * else return FALSE */ BOOLEAN QueryAndWriteToCachePool ( PCACHE_POOL CachePool, PUCHAR Buf, LONGLONG Offset, ULONG Length #ifdef READ_VERIFY ,PDEVICE_OBJECT LowerDeviceObject ,ULONG DiskNumber ,ULONG PartitionNumber #endif ) { PUCHAR origBuf; ULONG i, front_offset, front_skip, end_cut, origLen; BOOLEAN Ret = FALSE; BOOLEAN front_broken, end_broken; PCACHE_BLOCK *ppInternalBlocks = NULL; origBuf = Buf; origLen = Length; detect_broken(Offset, Length, front_broken, end_broken, front_offset, front_skip, end_cut); Offset /= BLOCK_SIZE; Length /= BLOCK_SIZE; ppInternalBlocks = (PCACHE_BLOCK*) ExAllocatePoolWithTag ( NonPagedPool, (SIZE_T)((Length+2) * sizeof(PCACHE_BLOCK)), CACHE_POOL_TAG ); if (ppInternalBlocks == NULL) goto l_error; // Query Pool to Check If Fully Matched if (front_broken == TRUE && _QueryPoolByIndex(CachePool, Offset-1, ppInternalBlocks+0) == FALSE) goto l_error; for (i = 0; i < Length; i++) if (_QueryPoolByIndex(CachePool, Offset+i, ppInternalBlocks+i+1) == FALSE) goto l_error; if (end_broken == TRUE && _QueryPoolByIndex(CachePool, Offset+Length, ppInternalBlocks+Length+1) == FALSE) goto l_error; if (front_broken == TRUE) { DO_READ_VERIFY(CachePool, &CachePool->Storage, ppInternalBlocks[0]); _write_data(ppInternalBlocks[0], front_offset, Buf, front_skip); Buf += front_skip; } for (i = 0; i < Length; i++) { DO_READ_VERIFY(CachePool, &CachePool->Storage, ppInternalBlocks[i+1]); _write_data(ppInternalBlocks[i+1], 0, Buf, BLOCK_SIZE); Buf += BLOCK_SIZE; } if (end_broken == TRUE) { DO_READ_VERIFY(CachePool, &CachePool->Storage, ppInternalBlocks[Length+1]); _write_data(ppInternalBlocks[Length+1], 0, Buf, end_cut); Buf += end_cut; } ASSERT(Buf - origBuf == origLen); Ret = TRUE; l_error: if (ppInternalBlocks != NULL) ExFreePoolWithTag(ppInternalBlocks, CACHE_POOL_TAG); return Ret; }
/** * Write Update Cache Pool with Buffer */ VOID WriteUpdateCachePool( PCACHE_POOL CachePool, PUCHAR Buf, LONGLONG Offset, ULONG Length #ifdef READ_VERIFY ,PDEVICE_OBJECT LowerDeviceObject ,ULONG DiskNumber ,ULONG PartitionNumber #endif ) { PUCHAR origBuf; ULONG i, front_offset, front_skip, end_cut, origLen; PCACHE_BLOCK pBlock; BOOLEAN front_broken, end_broken; origBuf = Buf; origLen = Length; detect_broken(Offset, Length, front_broken, end_broken, front_offset, front_skip, end_cut); Offset /= BLOCK_SIZE; Length /= BLOCK_SIZE; if (front_broken) { if (_QueryPoolByIndex(CachePool, Offset-1, &pBlock) == TRUE) { DO_READ_VERIFY(CachePool, &CachePool->Storage, pBlock); _write_data(pBlock, front_offset, Buf, front_skip); } Buf += front_skip; } for (i = 0; i < Length; i++) { LONGLONG Index = Offset + i; if(_QueryPoolByIndex(CachePool, Index, &pBlock) == TRUE) { DO_READ_VERIFY(CachePool, &CachePool->Storage, pBlock); _write_data(pBlock, 0, Buf, BLOCK_SIZE); } else if (_IsFull(CachePool) == FALSE) { pBlock = _AddNewBlockToPool(CachePool, Index, Buf, FALSE); ADD_TO_WBQUEUE_SAFE(pBlock); } else { pBlock = _FindBlockToReplace(CachePool, Index, Buf, FALSE); ADD_TO_WBQUEUE_SAFE(pBlock); } Buf += BLOCK_SIZE; } if (end_broken) { if (_QueryPoolByIndex(CachePool, Offset+Length, &pBlock) == TRUE) { DO_READ_VERIFY(CachePool, &CachePool->Storage, pBlock); _write_data(pBlock, 0, Buf, end_cut); } Buf += end_cut; } ASSERT (Buf - origBuf == origLen); }
int writeKeyFile() { FILE *fp[3]; char buf[128]; int proceed = 1; /* create ~/.pppauth if necessary */ if ( ! _dir_exists(_key_file_dir()) ) { mkdir(_key_file_dir(), S_IRWXU); } /* warn about overwriting an existing key */ if ( _file_exists(_key_file_name()) ) { proceed = 0; fprintf(stderr, "\n" "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" "@ WARNING: YOU ARE ABOUT TO OVERWRITE YOUR KEY FILE! @\n" "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" "\n" "IF YOU PROCEED, YOUR EXISTING PASSCARDS WILL BECOME USELESS!\n" "\n" "If this is not what you intended to do, type `NO' below.\n" "\n" "By typing `yes' below, a new sequence key will be generated\n" "and you will no longer be able to log in using your existing\n" "passcards. New passcards must be printed.\n" "\n" ); proceed = confirm("Are you sure you want to proceed (yes/no)? "); } if (proceed) { _enforce_permissions(); umask(S_IRWXG|S_IRWXO); fp[0] = fopen(_key_file_name(), "w"); fp[1] = fopen(_cnt_file_name(), "w"); fp[2] = fopen(_gen_file_name(), "w"); if (fp[0] && fp[1] && fp[2]) { _write_data(seqKey(), fp[0]); fclose(fp[0]); _write_data(currPasscodeNum(), fp[1]); fclose(fp[1]); _write_data(lastCardGenerated(), fp[2]); fclose(fp[2]); memset(buf, 0, 128); fprintf(stderr, "\n" "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" " A new sequence key has been generated and saved. It is\n" " HIGHLY RECOMMENDED that you IMMEDIATELY print new pass-\n" " cards in order to access your account in the future.\n" "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" "\n" ); return 1; } } else { fprintf(stderr, "\n" "===========================================================\n" " A new sequence key WAS NOT generated and your old key\n" " remains intact. As a result, you can continue to use\n" " your existing passcards to log into your account.\n" "===========================================================\n" "\n" ); } return 0; }