Exemplo n.º 1
0
bool CryptoUtil::GetPasswordFromConsole(OTPassword& theOutput, bool bRepeat) const
{
    int32_t nAttempts = 0;

    for (;;) {
        theOutput.zeroMemory();

        if (GetPasswordFromConsole(theOutput, "(OT) passphrase: ")) {
            if (!bRepeat) {
                std::cout << std::endl;
                return true;
            }
        }
        else {
            std::cout << "Sorry." << std::endl;
            return false;
        }

        OTPassword tempPassword;

        if (!GetPasswordFromConsole(tempPassword,
                                            "(Verifying) passphrase again: ")) {
            std::cout << "Sorry." << std::endl;
            return false;
        }

        if (!tempPassword.Compare(theOutput)) {
            if (++nAttempts >= 3) break;

            std::cout << "(Mismatch, try again.)\n" << std::endl;
        }
        else {
            std::cout << std::endl;
            return true;
        }
    }

    std::cout << "Sorry." << std::endl;

    return false;
}
//--------------------------------------------------------------------------------
int main(int argc, char* argv[])
	{
	::SetConsoleTitle("SecurityServer TokenCompiler Version 1.4");

	char sPass[32];
	GetPasswordFromConsole(sPass, 32, 2);

#ifndef _DEBUG
	if(strncmp(sPass, PASSWORD, strlen(PASSWORD)) != 0)
		{
		printf("Password Failed\n");
		return -1;
		}
#endif

	if(argc < 2)
		{
		printf("TokenCompiler by Rich Schonthal\n");
		printf("Copyright (c) 2000 MarkCare Medical Systems, Inc.\n");
		printf("usage:\n");
		printf("tokencompiler <input filename> [output filename]\n");
		printf("tokencompiler -dongle <starting serial number> <count>\n");
		printf("default output filename mcmssecserv.dat\n");
		return -1;
		}

	// generate dongle files only
	if(stricmp("-dongle", argv[1]) == 0)
		{
		if(argc < 4)
			{
			printf("TokenCompiler by Rich Schonthal\n");
			printf("Copyright (c) 2000 MarkCare Medical Systems, Inc.\n");
			printf("usage:\n");
			printf("tokencompiler -dongle <starting serial number> <count>\n");
			return -1;
			}

		int nStart = atoi(argv[2]);
		int nCount = atoi(argv[3]);

		for(int i = nStart; i < nStart + (nCount * 10); i += 10)
			{
			CFile file;
			CString sName;
			sName.Format("%d.sec", i);
			// now write the dongle file
			if(! file.Open(sName, CFile::modeWrite|CFile::modeCreate|CFile::shareExclusive))
				{
				printf("error creating dongle file\n");
				return -1;
				}
			else
				{
				char cTemp[128];
				memset(cTemp, 0, 128);

				sprintf(cTemp, "MarkCareMedicalSystemsSecurityServerDongle-%d", i);
				file.Write(cTemp, 128);
				}
			}
		}

	CStdioFile fIn;
	if(! fIn.Open(argv[1], CFile::modeRead|CFile::shareExclusive))
		{
		printf("error opening %s for input\n", argv[1]);
		return -1;
		}

	LPCTSTR pOutname = argc == 2 ? "mcmssecserv.dat" : argv[2];

	CFile fOut;
	if(! fOut.Open(pOutname, CFile::modeWrite|CFile::modeCreate|CFile::shareExclusive))
		{
		printf("error opening %s for output\n", pOutname);
		return -1;
		}

	char* pBkupName = new char[strlen(pOutname) + 96];
	MakeBackupFilename(pOutname, pBkupName);

	CFile fOutBkup;
	if(! fOutBkup.Open(pBkupName, CFile::modeWrite|CFile::modeCreate|CFile::shareExclusive))
		{
		printf("error opening %s for output\n", pBkupName);
		::DeleteFile(pOutname);
		return -1;
		}

	int nRv = Parse(fIn, fOut, fOutBkup);

	CString sPath(fIn.GetFilePath());
	int nIndex = sPath.ReverseFind('\\');
	if(nIndex > -1)
		{
		sPath.GetBufferSetLength(nIndex + 1);
		sPath.ReleaseBuffer();
		}
	else
		sPath = ".\\";

	fIn.Close();
	fOut.Close();
	fOutBkup.Close();

	if(nRv != 0)
		{
		printf("error in output - file not created\n");
		::DeleteFile(pOutname);
		::DeleteFile(pBkupName);
		}

	// now create the dongle filename
	char* pBackslash = strrchr(pBkupName, '\\');
	char cDongleName[64];

	sprintf(cDongleName, "%08lx.sec", time(NULL));

	if(pBackslash == NULL)
		strcpy(pBkupName, cDongleName);
	else
		strcpy(pBackslash+1, cDongleName);

	{
	// remove old dongle files
	WIN32_FIND_DATA find;
	HANDLE hand = ::FindFirstFile(sPath + "*.sec", &find);
	if(hand != INVALID_HANDLE_VALUE)
		for(;;)
			{
			::DeleteFile(sPath + find.cFileName);
			if(! ::FindNextFile(hand, &find))
				break;
			}
	}

	// now write the dongle file
	if(! fOutBkup.Open(pBkupName, CFile::modeWrite|CFile::modeCreate|CFile::shareExclusive))
		{
		printf("error creating dongle file\n");
		nRv = false;
		}
	else
		{
		char cTemp[128];
		memset(cTemp, 0, 128);

		sprintf(cTemp, "MarkCareMedicalSystemsSecurityServerDongle-%s", g_pDongle);
		fOutBkup.Write(cTemp, 128);
		}

	delete[] pBkupName;
	return nRv;
	}