backup
[platform/upstream/libzypp.git] / zypp / VendorAttr.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /*
10   File:       VendorAttr.cc
11
12   Author:     Michael Andres <ma@suse.de>
13   Maintainer: Michael Andres <ma@suse.de>
14
15   Purpose: Manage vendor attributes
16
17 /-*/
18
19 #include <iostream>
20 #include <fstream>
21 #include <set>
22 #include <map>
23
24 #include "zypp/base/LogTools.h"
25 #include "zypp/base/IOStream.h"
26 #include "zypp/base/String.h"
27
28 #include "zypp/PathInfo.h"
29 #include "zypp/VendorAttr.h"
30 #include "zypp/ZYppFactory.h"
31
32 #include "zypp/ZConfig.h"
33
34 using namespace std;
35
36 #undef  ZYPP_BASE_LOGGER_LOGGROUP
37 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::VendorAttr"
38
39 ///////////////////////////////////////////////////////////////////
40 namespace zypp
41 { /////////////////////////////////////////////////////////////////
42
43   ///////////////////////////////////////////////////////////////////
44   namespace
45   { /////////////////////////////////////////////////////////////////
46
47     typedef std::map<Vendor,bool> TrustMap;
48     TrustMap _trustMap;
49
50     typedef std::set<std::string> VendorList;
51     VendorList _trustedVendors;
52
53     bool addTrustedVendor( const std::string & str_r )
54     {
55       std::string line( str::trim( str_r ) );
56       if ( ! line.empty() && line[0] != '#')
57         {
58           _trustedVendors.insert( str::toLower( line ) );
59         }
60       return true;
61     }
62
63     bool trusted( const Vendor & vendor_r )
64     {
65       TrustMap::value_type val( vendor_r, false );
66       pair<TrustMap::iterator, bool> res = _trustMap.insert( val );
67
68       if ( res.second )
69         {
70           // check the new vendor in map
71           for ( VendorList::const_iterator it = _trustedVendors.begin();
72                 it != _trustedVendors.end(); ++it )
73             {
74               if ( str::toLower( res.first->first.substr( 0, it->size() ) )
75                    == str::toLower( *it ) )
76                 {
77                   // match
78                   res.first->second = true;
79                   break;
80                 }
81             }
82         }
83       return res.first->second;
84     }
85     /////////////////////////////////////////////////////////////////
86   } // namespace
87   ///////////////////////////////////////////////////////////////////
88
89   const VendorAttr & VendorAttr::instance()
90   {
91     static VendorAttr _val;
92     return _val;
93   }
94
95   VendorAttr::VendorAttr ()
96   {
97     const char * vendors[] = {
98       "jpackage project",
99       "novell",
100       "opensuse",
101       "sgi",
102       "silicon graphics",
103       "suse",
104       "ati technologies inc.",
105       "nvidia"
106     };
107     _trustedVendors.insert( vendors, vendors+(sizeof(vendors)/sizeof(const char *)) );
108
109     Pathname vendorrcPath( "/etc/zypp/trustedVendors" );
110     try
111       {
112         Target_Ptr trg( getZYpp()->target() );
113         if ( trg )
114           vendorrcPath = trg->root() / vendorrcPath;
115       }
116     catch ( ... )
117       {
118         // noop: Someone decided to let target() throw if the ptr is NULL ;(
119       }
120
121     PathInfo vendorrc( vendorrcPath );
122     if ( vendorrc.isFile() )
123       {
124         MIL << "Reading " << vendorrc << endl;
125         ifstream inp( vendorrc.asString().c_str() );
126         iostr::forEachLine( inp, addTrustedVendor );
127       }
128     MIL << "Trusted Vendors: " << _trustedVendors << endl;
129   }
130
131   void VendorAttr::enableAutoProtect()
132   {
133     MIL << "FIXME: Not implemented." << endl;
134     // FIXME use ZConfig
135   }
136
137   void VendorAttr::disableAutoProtect()
138   {
139     MIL << "FIXME: Not implemented." << endl;
140     // FIXME use ZConfig
141   }
142
143   bool VendorAttr::isKnown( const Vendor & vendor_r ) const
144   { return trusted( vendor_r ); }
145
146
147   bool VendorAttr::autoProtect( const Vendor & vendor_r ) const
148   { return( ZConfig::instance().autolock_untrustedvendor() && ! trusted( vendor_r ) ); }
149
150   /** Helper: Lowercase prefix */
151   inline bool hasLcPrefix( const std::string & str_r, const std::string & pref_r )
152   { return str::toLower( str_r.substr( 0, pref_r.size() ) ) == pref_r; }
153
154   /** Helper: SuSE and equivalent vendors */
155   inline bool isSUSE( const Vendor & vnd_r )
156   {
157     static const std::string defSUSE    ( "suse" );
158     static const std::string defopenSUSE( "opensuse" );
159
160     return(    hasLcPrefix( vnd_r, defSUSE )
161             || hasLcPrefix( vnd_r, defopenSUSE ) );
162   }
163
164   bool VendorAttr::equivalent( const Vendor & lhs, const Vendor & rhs ) const
165   {
166    if ( lhs == rhs )
167       return true;
168
169     // By now handcrafted equivalence definition:
170     return( isSUSE( lhs ) && isSUSE( rhs ) );
171   }
172
173   /////////////////////////////////////////////////////////////////
174 } // namespace zypp
175 ///////////////////////////////////////////////////////////////////
176