00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <iostream>
00020 #include <fstream>
00021 #include <set>
00022 #include <map>
00023
00024 #include "zypp/base/LogTools.h"
00025 #include "zypp/base/IOStream.h"
00026 #include "zypp/base/String.h"
00027
00028 #include "zypp/VendorAttr.h"
00029 #include "zypp/ZYppFactory.h"
00030
00031 using namespace std;
00032
00033 #undef ZYPP_BASE_LOGGER_LOGGROUP
00034 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::VendorAttr"
00035
00037 namespace zypp
00038 {
00039
00041 namespace
00042 {
00043
00044 typedef std::map<Vendor,bool> TrustMap;
00045 TrustMap _trustMap;
00046
00047 typedef std::set<std::string> VendorList;
00048 VendorList _trustedVendors;
00049
00050 bool addTrustedVendor( const std::string & str_r )
00051 {
00052 std::string line( str::trim( str_r ) );
00053 if ( ! line.empty() && line[0] != '#')
00054 {
00055 _trustedVendors.insert( str::toLower( line ) );
00056 }
00057 return true;
00058 }
00059
00060 bool trusted( const Vendor & vendor_r )
00061 {
00062 TrustMap::value_type val( vendor_r, false );
00063 pair<TrustMap::iterator, bool> res = _trustMap.insert( val );
00064
00065 if ( res.second )
00066 {
00067
00068 for ( VendorList::const_iterator it = _trustedVendors.begin();
00069 it != _trustedVendors.end(); ++it )
00070 {
00071 if ( str::toLower( res.first->first.substr( 0, it->size() ) )
00072 == str::toLower( *it ) )
00073 {
00074
00075 res.first->second = true;
00076 break;
00077 }
00078 }
00079 }
00080 return res.first->second;
00081 }
00082
00083 bool applyAutoProtection = true;
00084
00086 }
00088
00089 const VendorAttr & VendorAttr::instance()
00090 {
00091 static VendorAttr _val;
00092 return _val;
00093 }
00094
00095 VendorAttr::VendorAttr ()
00096 {
00097 char * vendors[] = {
00098 "jpackage project",
00099 "novell",
00100 "opensuse",
00101 "sgi",
00102 "silicon graphics",
00103 "suse",
00104 "ati technologies inc.",
00105 "nvidia"
00106 };
00107 _trustedVendors.insert( vendors, vendors+(sizeof(vendors)/sizeof(char *)) );
00108
00109 Pathname vendorrcPath( getZYpp()->homePath() / "db/trustedVendors" );
00110 try
00111 {
00112 Target_Ptr trg( getZYpp()->target() );
00113 if ( trg )
00114 vendorrcPath = trg->root() / vendorrcPath;
00115 }
00116 catch ( ... )
00117 {
00118
00119 }
00120
00121 PathInfo vendorrc( vendorrcPath );
00122 if ( vendorrc.isFile() )
00123 {
00124 MIL << "Reading " << vendorrc << endl;
00125 ifstream inp( vendorrc.asString().c_str() );
00126 iostr::forEachLine( inp, addTrustedVendor );
00127 }
00128 MIL << "Trusted Vendors: " << _trustedVendors << endl;
00129 }
00130
00131 void VendorAttr::enableAutoProtect()
00132 {
00133 MIL << "Foreign vendor auto protection enabled." << endl;
00134 applyAutoProtection = true;
00135 }
00136
00137 void VendorAttr::disableAutoProtect()
00138 {
00139 MIL << "Foreign vendor auto protection disabled." << endl;
00140 applyAutoProtection = false;
00141 }
00142
00143 bool VendorAttr::isKnown( const Vendor & vendor_r ) const
00144 { return trusted( vendor_r ); }
00145
00146
00147 bool VendorAttr::autoProtect( const Vendor & vendor_r ) const
00148 { return( applyAutoProtection && ! trusted( vendor_r ) ); }
00149
00151 }
00153