📄 passwdfile.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const PasswdFile_cxx_Version = "$Id: PasswdFile.cxx,v 1.2 2002/09/26 01:14:03 bko Exp $";#define _XOPEN_SOURCE#define __EXTENSIONS__#include <unistd.h>#undef _XOPEN_SOURCE#undef __EXTENSIONS__#include "PasswdFile.hxx"#include <fstream>#include "cpLog.h"#include "Lock.hxx"#include "FileStat.hxx"using Vocal::Threads::Lock;using Vocal::IO::FileStat;PasswdFile::PasswdFile(const char* filename) : myMutex(), myFilename(), myLastMod(0){ myFilename = filename;}PasswdFile::PasswdFile() : myLastMod(0){}voidPasswdFile::update(){ Lock x(myMutex); std::ifstream file; FileStat s(myFilename); if(s.statOK()) { // it's OK, so use the stat structure if(s.lastModified() > myLastMod) { cpLog(LOG_DEBUG, "reading password file %s", myFilename.c_str()); file.open(myFilename.c_str()); if(!file) { cpLog(LOG_ERR, "unable to read password file %s", myFilename.c_str()); } else { while(!file.eof()) { std::string buf, array[3]; int i = 0; getline(file, buf); // now, split buf into 3 unsigned int c; while(i < 3 && (c = buf.find(":")) != string::npos) { cpLog(LOG_DEBUG_STACK, "line: %s", buf.c_str()); if(c != 0) { array[i] = buf.substr(0, c); buf.erase(0, c + 1); cpLog(LOG_DEBUG_STACK, "entry: %s", array[i].c_str()); } buf.erase(static_cast<string::size_type>(0), static_cast<string::size_type>(0)); // needs to be here -- what if there is nothing before // the : ? i++; cpLog(LOG_DEBUG_STACK, "line: %s", buf.c_str()); } if(i < 3) { array[i] = buf; i++; } if(i == 2 || i == 3) { // this is an ok entry PasswordData data; data.pass = array[1]; if(array[2] == "1") { data.level = PasswdFile::PasswdReadWrite; } else { data.level = PasswdFile::PasswdReadOnly; } if(array[0] != "") { cpLog(LOG_DEBUG, "adding user %s:%s to passwords", array[0].c_str(), array[1].c_str()); myPasswords[array[0]] = data; } } else { cpLog(LOG_ERR, "bad entry in password database"); } } cpLog(LOG_DEBUG, "successfully read password file"); myLastMod = s.lastModified(); file.close(); } } }}PasswdFile::PasswdFileLevelPasswdFile::verify(std::string username, std::string password){ cpLog(LOG_DEBUG, "verifying password"); update(); std::map<std::string, PasswdFile::PasswordData>::const_iterator i; i = myPasswords.find(username); if(i != myPasswords.end() && i->second.pass == crypt(password.c_str(), i->second.pass.c_str())) { // good password cpLog(LOG_DEBUG, "password correct"); return i->second.level; } else { // bad password if(i == myPasswords.end()) { cpLog(LOG_ERR, "login failed: user does not exist"); } else { cpLog(LOG_ERR, "login failed: password incorrect: %s != %s", i->second.pass.c_str(), crypt(password.c_str(), i->second.pass.c_str())); } return PasswdFile::PasswdNoAuth; }}/* Local Variables: *//* c-file-style: "stroustrup" *//* indent-tabs-mode: nil *//* c-file-offsets: ((access-label . -) (inclass . ++)) *//* c-basic-offset: 4 *//* End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -