// // create a file and make a random token data entry to it. // send the file name back to caller // String LocalAuthFile::create() { PEG_METHOD_ENTER(TRC_AUTHENTICATION, "LocalAuthFile::create()"); Uint32 secs, milliSecs; Uint32 mySequenceNumber; System::getCurrentTime(secs, milliSecs); // // extension size is username plus the sequence count // { AutoMutex autoMut(sequenceCountLock); mySequenceNumber = sequenceCount++; } // mutex unlocks here char extension[2*INT_BUFFER_SIZE]; sprintf(extension,"_%u_%u", mySequenceNumber, milliSecs); extension[strlen(extension)] = 0; String filePath; // Check to see whether a domain was specified. If so, strip the domain // from the local auth file name, since the backslash and '@' are invalid // filename characters. Store this in another variable, since we need to // keep the domain around for the rest of the operations. String fileUserName = _userName; Uint32 index = _userName.find('\\'); if (index != PEG_NOT_FOUND) { fileUserName = _userName.subString(index + 1); } else { index = _userName.find('@'); if (index != PEG_NOT_FOUND) { fileUserName = _userName.subString(0, index); } } filePath.append(_authFilePath); filePath.append(fileUserName);//_userName); filePath.append(extension); CString filePathCString = filePath.getCString(); // // 1. Create a file name for authentication. // ofstream outfs(filePathCString); if (!outfs) { // unable to create file Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, MessageLoaderParms( "Security.Authentication.LocalAuthFile.NO_CREATE", "Creation of the local authentication security file" " $0 failed: $1", filePath, strerror(errno))); PEG_METHOD_EXIT(); throw CannotOpenFile (filePath); } outfs.clear(); // // 2. Set file permission to read/write by the owner only. // #if defined(PEGASUS_OS_TYPE_WINDOWS) Boolean success = FileSystem::changeFilePermissions(filePath, _S_IREAD | _S_IWRITE); #else Boolean success = FileSystem::changeFilePermissions(filePath, S_IRUSR | S_IWUSR); #endif if (!success) { // Unable to change the local auth file permissions, remove the file // and throw CannotOpenFile error. Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, MessageLoaderParms( "Security.Authentication.LocalAuthFile.NO_CHMOD", "Changing permissions of the local authentication security " "file $0 failed: $1", filePath, strerror(errno))); if (filePath.size()) { if (FileSystem::exists(filePath)) { FileSystem::removeFile(filePath); } } PEG_METHOD_EXIT(); throw CannotOpenFile (filePath); } // // 3. Generate random token and write the token to the file. // String randomToken = _generateRandomTokenString(); outfs << randomToken; // test if the write was successful if (outfs.fail()) { Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, MessageLoaderParms( "Security.Authentication.LocalAuthFile.NO_WRITE", "Cannot write security token to the local authentication " "security file $0.", filePath)); if (filePath.size()) { if (FileSystem::exists(filePath)) { FileSystem::removeFile(filePath); } } PEG_METHOD_EXIT(); throw CannotOpenFile (filePath); } outfs.close(); // // 4. Set file permission to read only by the owner. // #if defined(PEGASUS_OS_TYPE_WINDOWS) success = FileSystem::changeFilePermissions(filePath, _S_IREAD); #else success = FileSystem::changeFilePermissions(filePath, S_IRUSR); #endif if (!success) { // Unable to change the local auth file permissions, remove the file // and throw CannotOpenFile error. Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, MessageLoaderParms( "Security.Authentication.LocalAuthFile.NO_CHMOD", "Changing permissions of the local authentication security " "file $0 failed: $1", filePath, strerror(errno))); if (filePath.size()) { if (FileSystem::exists(filePath)) { FileSystem::removeFile(filePath); } } PEG_METHOD_EXIT(); throw CannotOpenFile (filePath); } // // 5. Change the file owner to the requesting user. // if (!FileSystem::changeFileOwner(filePath,_userName)) { // Unable to change owner on local auth file, remove the file // and throw CannotOpenFile error. Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, MessageLoaderParms( "Security.Authentication.LocalAuthFile.NO_CHOWN_REQUSER", "Changing ownership of the local authentication " "security file $0 to the requesting user failed: $1", filePath, strerror(errno))); if (filePath.size()) { if (FileSystem::exists(filePath)) { FileSystem::removeFile(filePath); } } PEG_METHOD_EXIT(); throw CannotOpenFile (filePath); } _secret = randomToken; _filePathName = filePath; PEG_METHOD_EXIT(); return _filePathName; }
int main( int nArgc, char **papszArgv ) { ErrorReport er; if(nArgc <= 1) er.checkErrors(er.NO_INPUT); std::deque<char *> pszFilename; std::deque<std::string> outfilename_str; for( int iArg = 1; iArg < nArgc; iArg++ ) { if(NULL == papszArgv[iArg] ) er.checkErrors(er.NO_INPUT); pszFilename.push_back(papszArgv[iArg]); std::stringstream outfilename; outfilename << pszFilename.back() << ".txt"; outfilename_str.push_back(outfilename.str()); } std::deque<char *>::iterator cb = pszFilename.begin(), ce = pszFilename.end(); std::deque<std::string>::iterator ob = outfilename_str.begin(), oe = outfilename_str.end(); for( unsigned iArg = 0; iArg < pszFilename.size(); iArg++, cb++, ob++) { std::ofstream outfs(ob->c_str(), std::ofstream::out); if(outfs) { outfs << "比如2302\n23号02:00-02:59区间的耗电量和电费\n02:59时刻的电表读数和总价格" << std::endl; outfs << "日时\t小时末电表读数\t小时末总价格\t该小时耗电量\t该小时的电费" << std::endl; std::ifstream infs(*cb, std::ifstream::in | std::ifstream::binary); if(infs) { //while(!infs.eof()) while(infs.good()) { infs.read(reinterpret_cast<char *>(&Everyhour), sizeof(struct EVERYHOUR)); outfs << std::setw(4) << std::setfill('0') << Everyhour.HourTime << "\t"; outfs << Everyhour.HourSumElect << "\t" << Everyhour.HourSumPrice << "\t" << Everyhour.HourConsumeElect << "\t" << Everyhour.HourConsumePrice << std::endl; } infs.close(); infs.clear(); outfs.close(); outfs.clear(); } else { outfs.close(); outfs.clear(); er.checkErrors(er.UNREAD_FILE); } } else er.checkErrors(er.UNWRITE_FILE); } }