Imported Upstream version 17.10.2
[platform/upstream/libzypp.git] / zypp / RepoInfo.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoInfo.cc
10  *
11 */
12 #include <iostream>
13 #include <vector>
14 #include <fstream>
15
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/LogTools.h"
18 #include "zypp/base/DefaultIntegral.h"
19 #include "zypp/parser/xml/XmlEscape.h"
20
21 #include "zypp/ManagedFile.h"
22 #include "zypp/PublicKey.h"
23 #include "zypp/MediaSetAccess.h"
24 #include "zypp/RepoInfo.h"
25 #include "zypp/Glob.h"
26 #include "zypp/TriBool.h"
27 #include "zypp/Pathname.h"
28 #include "zypp/ZConfig.h"
29 #include "zypp/repo/RepoMirrorList.h"
30 #include "zypp/ExternalProgram.h"
31 #include "zypp/media/MediaAccess.h"
32
33 #include "zypp/base/IOStream.h"
34 #include "zypp/base/InputStream.h"
35 #include "zypp/parser/xml/Reader.h"
36
37
38 #include "zypp/base/StrMatcher.h"
39 #include "zypp/KeyRing.h"
40 #include "zypp/TmpPath.h"
41 #include "zypp/ZYppFactory.h"
42 #include "zypp/ZYppCallbacks.h"
43
44 using std::endl;
45 using zypp::xml::escape;
46
47 ///////////////////////////////////////////////////////////////////
48 namespace zypp
49 { /////////////////////////////////////////////////////////////////
50
51   ///////////////////////////////////////////////////////////////////
52   //
53   //    CLASS NAME : RepoInfo::Impl
54   //
55   /** RepoInfo implementation. */
56   struct RepoInfo::Impl
57   {
58     Impl()
59       : _rawGpgCheck( indeterminate )
60       , _rawRepoGpgCheck( indeterminate )
61       , _rawPkgGpgCheck( indeterminate )
62       , _validRepoSignature( indeterminate )
63       , keeppackages(indeterminate)
64       , _mirrorListForceMetalink(false)
65       , type(repo::RepoType::NONE_e)
66       , emptybaseurls(false)
67     {}
68
69     ~Impl()
70     {}
71
72   public:
73     static const unsigned defaultPriority = 99;
74     static const unsigned noPriority = unsigned(-1);
75
76     void setProbedType( const repo::RepoType & t ) const
77     {
78       if ( type == repo::RepoType::NONE
79            && t != repo::RepoType::NONE )
80       {
81         // lazy init!
82         const_cast<Impl*>(this)->type = t;
83       }
84     }
85
86   public:
87     /** Path to a license tarball in case it exists in the repo. */
88     Pathname licenseTgz( const std::string & name_r ) const
89     {
90       Pathname ret;
91       if ( !metadataPath().empty() )
92       {
93         std::string licenseStem( "license" );
94         if ( !name_r.empty() )
95         {
96           licenseStem += "-";
97           licenseStem += name_r;
98         }
99
100         filesystem::Glob g;
101         // TODO: REPOMD: this assumes we know the name of the tarball. In fact
102         // we'd need to get the file from repomd.xml (<data type="license[-name_r]">)
103         g.add( metadataPath() / path / ("repodata/*"+licenseStem+".tar.gz") );
104         if ( g.empty() )
105           g.add( metadataPath() / path / (licenseStem+".tar.gz") );
106
107         if ( !g.empty() )
108           ret = *g.begin();
109       }
110       return ret;
111     }
112
113     const RepoVariablesReplacedUrlList & baseUrls() const
114     {
115       const Url & mlurl( _mirrorListUrl.transformed() );        // Variables replaced!
116       if ( _baseUrls.empty() && ! mlurl.asString().empty() )
117       {
118         emptybaseurls = true;
119         DBG << "MetadataPath: " << metadataPath() << endl;
120         repo::RepoMirrorList rmurls( mlurl, metadataPath(), _mirrorListForceMetalink );
121         _baseUrls.raw().insert( _baseUrls.raw().end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
122       }
123       return _baseUrls;
124     }
125
126     RepoVariablesReplacedUrlList & baseUrls()
127     { return _baseUrls; }
128
129     bool baseurl2dump() const
130     { return !emptybaseurls && !_baseUrls.empty(); }
131
132
133     const RepoVariablesReplacedUrlList & gpgKeyUrls() const
134     { return _gpgKeyUrls; }
135
136     RepoVariablesReplacedUrlList & gpgKeyUrls()
137     { return _gpgKeyUrls; }
138
139
140     const std::set<std::string> & contentKeywords() const
141     { hasContent()/*init if not yet done*/; return _keywords.second; }
142
143     void addContent( const std::string & keyword_r )
144     { _keywords.second.insert( keyword_r ); if ( ! hasContent() ) _keywords.first = true; }
145
146     bool hasContent() const
147     {
148       if ( !_keywords.first && ! metadataPath().empty() )
149       {
150         // HACK directly check master index file until RepoManager offers
151         // some content probing and zypper uses it.
152         /////////////////////////////////////////////////////////////////
153         MIL << "Empty keywords...." << metadataPath() << endl;
154         Pathname master;
155         if ( PathInfo( (master=metadataPath()/"/repodata/repomd.xml") ).isFile() )
156         {
157           //MIL << "GO repomd.." << endl;
158           xml::Reader reader( master );
159           while ( reader.seekToNode( 2, "content" ) )
160           {
161             _keywords.second.insert( reader.nodeText().asString() );
162             reader.seekToEndNode( 2, "content" );
163           }
164           _keywords.first = true;       // valid content in _keywords even if empty
165         }
166         else if ( PathInfo( (master=metadataPath()/"/content") ).isFile() )
167         {
168           //MIL << "GO content.." << endl;
169           iostr::forEachLine( InputStream( master ),
170                             [this]( int num_r, std::string line_r )->bool
171                             {
172                               if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
173                               {
174                                 std::vector<std::string> words;
175                                 if ( str::split( line_r, std::back_inserter(words) ) > 1
176                                   && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
177                                 {
178                                   this->_keywords.second.insert( ++words.begin(), words.end() );
179                                 }
180                                 return true; // mult. occurrances are ok.
181                               }
182                               return( ! str::startsWith( line_r, "META " ) );   // no need to parse into META section.
183                             } );
184           _keywords.first = true;       // valid content in _keywords even if empty
185         }
186         /////////////////////////////////////////////////////////////////
187       }
188       return _keywords.first;
189     }
190
191     bool hasContent( const std::string & keyword_r ) const
192     { return( hasContent() && _keywords.second.find( keyword_r ) != _keywords.second.end() ); }
193
194     /** Signature check result needs to be stored/retrieved from _metadataPath.
195      * Don't call them from outside validRepoSignature/setValidRepoSignature
196      */
197     //@{
198     TriBool internalValidRepoSignature() const
199     {
200       if ( ! indeterminate(_validRepoSignature) )
201         return _validRepoSignature;
202       // check metadata:
203       if ( ! metadataPath().empty() )
204       {
205         // A missing ".repo_gpgcheck" might be plaindir(no Downloader) or not yet refreshed signed repo!
206         TriBool linkval = triBoolFromPath( metadataPath() / ".repo_gpgcheck" );
207         return linkval;
208       }
209       return indeterminate;
210     }
211
212     void internalSetValidRepoSignature( TriBool value_r )
213     {
214       if ( PathInfo(metadataPath()).isDir() )
215       {
216         Pathname gpgcheckFile( metadataPath() / ".repo_gpgcheck" );
217         if ( PathInfo(gpgcheckFile).isExist() )
218         {
219           TriBool linkval( indeterminate );
220           if ( triBoolFromPath( gpgcheckFile, linkval ) && linkval == value_r )
221             return;     // existing symlink fits value_r
222           else
223             filesystem::unlink( gpgcheckFile ); // will write a new one
224         }
225         filesystem::symlink( asString(value_r), gpgcheckFile );
226       }
227       _validRepoSignature = value_r;
228     }
229
230     /** We definitely have a symlink pointing to "indeterminate" (for repoGpgCheckIsMandatory)?
231      * I.e. user accepted the unsigned repo in Downloader. A test whether `internalValidRepoSignature`
232      * is indeterminate would include not yet checked repos, which is unwanted here.
233      */
234     bool internalUnsignedConfirmed() const
235     {
236       TriBool linkval( true );  // want to see it being switched to indeterminate
237       return triBoolFromPath( metadataPath() / ".repo_gpgcheck", linkval ) && indeterminate(linkval);
238     }
239
240     bool triBoolFromPath( const Pathname & path_r, TriBool & ret_r ) const
241     {
242       static const Pathname truePath( "true" );
243       static const Pathname falsePath( "false" );
244       static const Pathname indeterminatePath( "indeterminate" );
245
246       // Quiet readlink;
247       static const ssize_t bufsiz = 63;
248       static char buf[bufsiz+1];
249       ssize_t ret = ::readlink( path_r.c_str(), buf, bufsiz );
250       buf[ret == -1 ? 0 : ret] = '\0';
251
252       Pathname linkval( buf );
253
254       bool known = true;
255       if ( linkval == truePath )
256         ret_r = true;
257       else if ( linkval == falsePath )
258         ret_r = false;
259       else if ( linkval == indeterminatePath )
260         ret_r = indeterminate;
261       else
262         known = false;
263       return known;
264     }
265
266     TriBool triBoolFromPath( const Pathname & path_r ) const
267     { TriBool ret(indeterminate); triBoolFromPath( path_r, ret ); return ret; }
268
269     //@}
270
271   private:
272     TriBool _rawGpgCheck;       ///< default gpgcheck behavior: Y/N/ZConf
273     TriBool _rawRepoGpgCheck;   ///< need to check repo sign.: Y/N/(ZConf(Y/N/gpgCheck))
274     TriBool _rawPkgGpgCheck;    ///< need to check pkg sign.: Y/N/(ZConf(Y/N/gpgCheck))
275
276   public:
277     TriBool rawGpgCheck() const                 { return _rawGpgCheck; }
278     TriBool rawRepoGpgCheck() const             { return _rawRepoGpgCheck; }
279     TriBool rawPkgGpgCheck() const              { return _rawPkgGpgCheck; }
280
281     void rawGpgCheck( TriBool val_r )           { _rawGpgCheck = val_r; }
282     void rawRepoGpgCheck( TriBool val_r )       { _rawRepoGpgCheck = val_r; }
283     void rawPkgGpgCheck( TriBool val_r )        { _rawPkgGpgCheck = val_r; }
284
285     bool cfgGpgCheck() const
286     { return indeterminate(_rawGpgCheck) ? ZConfig::instance().gpgCheck() : (bool)_rawGpgCheck; }
287     TriBool cfgRepoGpgCheck() const
288     { return indeterminate(_rawGpgCheck) && indeterminate(_rawRepoGpgCheck) ? ZConfig::instance().repoGpgCheck() : _rawRepoGpgCheck; }
289     TriBool cfgPkgGpgCheck() const
290     { return indeterminate(_rawGpgCheck) && indeterminate(_rawPkgGpgCheck) ? ZConfig::instance().pkgGpgCheck() : _rawPkgGpgCheck; }
291
292   private:
293     TriBool _validRepoSignature;///< have  signed and valid repo metadata
294   public:
295     TriBool keeppackages;
296     RepoVariablesReplacedUrl _mirrorListUrl;
297     bool                     _mirrorListForceMetalink;
298     repo::RepoType type;
299     Pathname path;
300     std::string service;
301     std::string targetDistro;
302
303     void metadataPath( Pathname new_r )
304     { _metadataPath = std::move( new_r ); }
305
306     void packagesPath( Pathname new_r )
307     { _packagesPath = std::move( new_r ); }
308
309     bool usesAutoMethadataPaths() const
310     { return str::hasSuffix( _metadataPath.asString(), "/%AUTO%" ); }
311
312     Pathname metadataPath() const
313     {
314       if ( usesAutoMethadataPaths() )
315         return _metadataPath.dirname() / "%RAW%";
316       return _metadataPath;
317     }
318
319     Pathname packagesPath() const
320     {
321       if ( _packagesPath.empty() && usesAutoMethadataPaths() )
322         return _metadataPath.dirname() / "%PKG%";
323       return _packagesPath;
324     }
325
326     DefaultIntegral<unsigned,defaultPriority> priority;
327     mutable bool emptybaseurls;
328
329   private:
330     Pathname _metadataPath;
331     Pathname _packagesPath;
332
333     mutable RepoVariablesReplacedUrlList _baseUrls;
334     mutable std::pair<FalseBool, std::set<std::string> > _keywords;
335
336     RepoVariablesReplacedUrlList _gpgKeyUrls;
337
338     friend Impl * rwcowClone<Impl>( const Impl * rhs );
339     /** clone for RWCOW_pointer */
340     Impl * clone() const
341     { return new Impl( *this ); }
342   };
343   ///////////////////////////////////////////////////////////////////
344
345   /** \relates RepoInfo::Impl Stream output */
346   inline std::ostream & operator<<( std::ostream & str, const RepoInfo::Impl & obj )
347   {
348     return str << "RepoInfo::Impl";
349   }
350
351   ///////////////////////////////////////////////////////////////////
352   //
353   //    CLASS NAME : RepoInfo
354   //
355   ///////////////////////////////////////////////////////////////////
356
357   const RepoInfo RepoInfo::noRepo;
358
359   RepoInfo::RepoInfo()
360   : _pimpl( new Impl() )
361   {}
362
363   RepoInfo::~RepoInfo()
364   {}
365
366   unsigned RepoInfo::priority() const
367   { return _pimpl->priority; }
368
369   unsigned RepoInfo::defaultPriority()
370   { return Impl::defaultPriority; }
371
372   unsigned RepoInfo::noPriority()
373   { return Impl::noPriority; }
374
375   void RepoInfo::setPriority( unsigned newval_r )
376   { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
377
378
379   bool RepoInfo::gpgCheck() const
380   { return _pimpl->cfgGpgCheck(); }
381
382   void RepoInfo::setGpgCheck( TriBool value_r )
383   { _pimpl->rawGpgCheck( value_r ); }
384
385   void RepoInfo::setGpgCheck( bool value_r ) // deprecated legacy and for squid
386   { setGpgCheck( TriBool(value_r) ); }
387
388
389   bool RepoInfo::repoGpgCheck() const
390   { return gpgCheck() || bool(_pimpl->cfgRepoGpgCheck()); }
391
392   bool RepoInfo::repoGpgCheckIsMandatory() const
393   {
394     bool ret = ( gpgCheck() && indeterminate(_pimpl->cfgRepoGpgCheck()) ) || bool(_pimpl->cfgRepoGpgCheck());
395     if ( ret && _pimpl->internalUnsignedConfirmed() )   // relax if unsigned repo was confirmed in the past
396       ret = false;
397     return ret;
398   }
399
400   void RepoInfo::setRepoGpgCheck( TriBool value_r )
401   { _pimpl->rawRepoGpgCheck( value_r ); }
402
403
404   bool RepoInfo::pkgGpgCheck() const
405   { return bool(_pimpl->cfgPkgGpgCheck()) || ( gpgCheck() && !bool(validRepoSignature())/*enforced*/ ) ; }
406
407   bool RepoInfo::pkgGpgCheckIsMandatory() const
408   { return bool(_pimpl->cfgPkgGpgCheck()) || ( gpgCheck() && indeterminate(_pimpl->cfgPkgGpgCheck()) && !bool(validRepoSignature())/*enforced*/ ); }
409
410   void RepoInfo::setPkgGpgCheck( TriBool value_r )
411   { _pimpl->rawPkgGpgCheck( value_r ); }
412
413
414   void RepoInfo::getRawGpgChecks( TriBool & g_r, TriBool & r_r, TriBool & p_r ) const
415   {
416     g_r = _pimpl->rawGpgCheck();
417     r_r = _pimpl->rawRepoGpgCheck();
418     p_r = _pimpl->rawPkgGpgCheck();
419   }
420
421
422   TriBool RepoInfo::validRepoSignature() const
423   {
424     TriBool ret( _pimpl->internalValidRepoSignature() );
425     if ( ret && !repoGpgCheck() ) ret = false;  // invalidate any old signature if repoGpgCheck is off
426     return ret;
427   }
428
429   void RepoInfo::setValidRepoSignature( TriBool value_r )
430   { _pimpl->internalSetValidRepoSignature( value_r ); }
431
432   ///////////////////////////////////////////////////////////////////
433   namespace
434   {
435     inline bool changeGpgCheckTo( TriBool & lhs, TriBool rhs )
436     { if ( ! sameTriboolState( lhs, rhs ) ) { lhs = rhs; return true; } return false; }
437
438     inline bool changeGpgCheckTo( TriBool ogpg[3], TriBool g, TriBool r, TriBool p )
439     {
440       bool changed = false;
441       if ( changeGpgCheckTo( ogpg[0], g ) ) changed = true;
442       if ( changeGpgCheckTo( ogpg[1], r ) ) changed = true;
443       if ( changeGpgCheckTo( ogpg[2], p ) ) changed = true;
444       return changed;
445     }
446   } // namespace
447   ///////////////////////////////////////////////////////////////////
448   bool RepoInfo::setGpgCheck( GpgCheck mode_r )
449   {
450     TriBool ogpg[3];    // Gpg RepoGpg PkgGpg
451     getRawGpgChecks( ogpg[0], ogpg[1], ogpg[2] );
452
453     bool changed = false;
454     switch ( mode_r )
455     {
456       case GpgCheck::On:
457         changed = changeGpgCheckTo( ogpg, true,          indeterminate, indeterminate );
458         break;
459       case GpgCheck::Strict:
460         changed = changeGpgCheckTo( ogpg, true,          true,          true          );
461         break;
462       case GpgCheck::AllowUnsigned:
463         changed = changeGpgCheckTo( ogpg, true,          false,         false         );
464         break;
465       case GpgCheck::AllowUnsignedRepo:
466         changed = changeGpgCheckTo( ogpg, true,          false,         indeterminate );
467         break;
468       case GpgCheck::AllowUnsignedPackage:
469         changed = changeGpgCheckTo( ogpg, true,          indeterminate, false         );
470         break;
471       case GpgCheck::Default:
472         changed = changeGpgCheckTo( ogpg, indeterminate, indeterminate, indeterminate );
473         break;
474       case GpgCheck::Off:
475         changed = changeGpgCheckTo( ogpg, false,         indeterminate, indeterminate );
476         break;
477       case GpgCheck::indeterminate:     // no change
478         break;
479     }
480
481     if ( changed )
482     {
483       setGpgCheck    ( ogpg[0] );
484       setRepoGpgCheck( ogpg[1] );
485       setPkgGpgCheck ( ogpg[2] );
486     }
487     return changed;
488   }
489
490   void RepoInfo::setMirrorListUrl( const Url & url_r )  // Raw
491   { _pimpl->_mirrorListUrl.raw() = url_r; _pimpl->_mirrorListForceMetalink = false; }
492
493   void  RepoInfo::setMetalinkUrl( const Url & url_r )   // Raw
494   { _pimpl->_mirrorListUrl.raw() = url_r; _pimpl->_mirrorListForceMetalink = true; }
495
496   void RepoInfo::setGpgKeyUrls( url_set urls )
497   { _pimpl->gpgKeyUrls().raw().swap( urls ); }
498
499   void RepoInfo::setGpgKeyUrl( const Url & url_r )
500   {
501     _pimpl->gpgKeyUrls().raw().clear();
502     _pimpl->gpgKeyUrls().raw().push_back( url_r );
503   }
504
505   Pathname RepoInfo::provideKey(const std::string &keyID_r, const Pathname &targetDirectory_r) const
506   {
507     if ( keyID_r.empty() )
508       return Pathname();
509
510     MIL << "Check for " << keyID_r << " at " << targetDirectory_r << endl;
511     std::string keyIDStr( keyID_r.size() > 8 ? keyID_r.substr( keyID_r.size()-8 ) : keyID_r );  // print short ID in Jobreports
512     filesystem::TmpDir tmpKeyRingDir;
513     KeyRing tempKeyRing(tmpKeyRingDir.path());
514
515     // translator: %1% is a gpg key ID like 3DBDC284
516     //             %2% is a cache directories path
517     JobReport::info( str::Format(_("Looking for gpg key ID %1% in cache %2%.") ) % keyIDStr % targetDirectory_r );
518     filesystem::dirForEach(targetDirectory_r,
519                            StrMatcher(".key", Match::STRINGEND),
520                            [&tempKeyRing]( const Pathname & dir_r, const std::string & str_r ){
521       try {
522
523         // deprecate a month old keys
524         PathInfo fileInfo ( dir_r/str_r );
525         if ( Date::now() - fileInfo.mtime() > Date::month ) {
526           //if unlink fails, the file will be overriden in the next step, no need
527           //to show a error
528           filesystem::unlink( dir_r/str_r );
529         } else {
530           tempKeyRing.multiKeyImport(dir_r/str_r, true);
531         }
532       } catch (const KeyRingException& e) {
533         ZYPP_CAUGHT(e);
534         ERR << "Error importing cached key from file '"<<dir_r/str_r<<"'."<<endl;
535       }
536       return true;
537     });
538
539     // no key in the cache is what we are looking for, lets download
540     // all keys specified in gpgkey= entries
541     if ( !tempKeyRing.isKeyTrusted(keyID_r) ) {
542       if ( ! gpgKeyUrlsEmpty() ) {
543         // translator: %1% is a gpg key ID like 3DBDC284
544         //             %2% is a repositories name
545         JobReport::info( str::Format(_("Looking for gpg key ID %1% in repository %2%.") ) % keyIDStr % asUserString() );
546         for ( const Url &url : gpgKeyUrls() ) {
547           try {
548             JobReport::info( "  gpgkey=" + url.asString() );
549             ManagedFile f = MediaSetAccess::provideOptionalFileFromUrl( url );
550             if ( f->empty() )
551               continue;
552
553             PublicKey key(f);
554             if ( !key.isValid() )
555               continue;
556
557             // import all keys into our temporary keyring
558             tempKeyRing.multiKeyImport(f, true);
559
560           } catch ( const std::exception & e ) {
561             //ignore and continue to next url
562             ZYPP_CAUGHT(e);
563             MIL << "Key import from url:'"<<url<<"' failed." << endl;
564           }
565         }
566       }
567       else {
568         // translator: %1% is a repositories name
569         JobReport::info( str::Format(_("Repository %1% does not define additional 'gpgkey=' URLs.") ) % asUserString() );
570       }
571     }
572
573     filesystem::assert_dir( targetDirectory_r );
574
575     //now write all keys into their own files in cache, override existing ones to always have
576     //up to date key data
577     for ( const auto & key: tempKeyRing.trustedPublicKeyData()) {
578       MIL << "KEY ID in KEYRING: " << key.id() << endl;
579
580       Pathname keyFile = targetDirectory_r/(str::Format("%1%.key") % key.rpmName()).asString();
581
582       std::ofstream fout( keyFile.c_str(), std::ios_base::out | std::ios_base::trunc );
583
584       if (!fout)
585         ZYPP_THROW(Exception(str::form("Cannot open file %s",keyFile.c_str())));
586
587       tempKeyRing.dumpTrustedPublicKey( key.id(), fout );
588     }
589
590     // key is STILL not known, we give up
591     if ( !tempKeyRing.isKeyTrusted(keyID_r) ) {
592       return Pathname();
593     }
594
595     PublicKeyData keyData( tempKeyRing.trustedPublicKeyData( keyID_r ) );
596     if ( !keyData ) {
597       ERR << "Error when exporting key from temporary keychain." << endl;
598       return Pathname();
599     }
600
601     return targetDirectory_r/(str::Format("%1%.key") % keyData.rpmName()).asString();
602   }
603
604   void RepoInfo::addBaseUrl( const Url & url_r )
605   {
606     for ( const auto & url : _pimpl->baseUrls().raw() ) // Raw unique!
607       if ( url == url_r )
608         return;
609     _pimpl->baseUrls().raw().push_back( url_r );
610   }
611
612   void RepoInfo::setBaseUrl( const Url & url_r )
613   {
614     _pimpl->baseUrls().raw().clear();
615     _pimpl->baseUrls().raw().push_back( url_r );
616   }
617
618   void RepoInfo::setBaseUrls( url_set urls )
619   { _pimpl->baseUrls().raw().swap( urls ); }
620
621   void RepoInfo::setPath( const Pathname &path )
622   { _pimpl->path = path; }
623
624   void RepoInfo::setType( const repo::RepoType &t )
625   { _pimpl->type = t; }
626
627   void RepoInfo::setProbedType( const repo::RepoType &t ) const
628   { _pimpl->setProbedType( t ); }
629
630
631   void RepoInfo::setMetadataPath( const Pathname &path )
632   { _pimpl->metadataPath( path ); }
633
634   void RepoInfo::setPackagesPath( const Pathname &path )
635   { _pimpl->packagesPath( path ); }
636
637   void RepoInfo::setKeepPackages( bool keep )
638   { _pimpl->keeppackages = keep; }
639
640   void RepoInfo::setService( const std::string& name )
641   { _pimpl->service = name; }
642
643   void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
644   { _pimpl->targetDistro = targetDistribution; }
645
646   bool RepoInfo::keepPackages() const
647   { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
648
649   Pathname RepoInfo::metadataPath() const
650   { return _pimpl->metadataPath(); }
651
652   Pathname RepoInfo::packagesPath() const
653   { return _pimpl->packagesPath(); }
654
655   bool RepoInfo::usesAutoMethadataPaths() const
656   { return _pimpl->usesAutoMethadataPaths(); }
657
658   repo::RepoType RepoInfo::type() const
659   { return _pimpl->type; }
660
661   Url RepoInfo::mirrorListUrl() const                   // Variables replaced!
662   { return _pimpl->_mirrorListUrl.transformed(); }
663
664   Url RepoInfo::rawMirrorListUrl() const                // Raw
665   { return _pimpl->_mirrorListUrl.raw(); }
666
667   bool RepoInfo::gpgKeyUrlsEmpty() const
668   { return _pimpl->gpgKeyUrls().empty(); }
669
670   RepoInfo::urls_size_type RepoInfo::gpgKeyUrlsSize() const
671   { return _pimpl->gpgKeyUrls().size(); }
672
673   RepoInfo::url_set RepoInfo::gpgKeyUrls() const        // Variables replaced!
674   { return _pimpl->gpgKeyUrls().transformed(); }
675
676   RepoInfo::url_set RepoInfo::rawGpgKeyUrls() const     // Raw
677   { return _pimpl->gpgKeyUrls().raw(); }
678
679   Url RepoInfo::gpgKeyUrl() const                       // Variables replaced!
680   { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().transformedBegin() ); }
681
682   Url RepoInfo::rawGpgKeyUrl() const                    // Raw
683   { return( _pimpl->gpgKeyUrls().empty() ? Url() : *_pimpl->gpgKeyUrls().rawBegin() ) ; }
684
685   RepoInfo::url_set RepoInfo::baseUrls() const          // Variables replaced!
686   { return _pimpl->baseUrls().transformed(); }
687
688   RepoInfo::url_set RepoInfo::rawBaseUrls() const       // Raw
689   { return _pimpl->baseUrls().raw(); }
690
691   Pathname RepoInfo::path() const
692   { return _pimpl->path; }
693
694   std::string RepoInfo::service() const
695   { return _pimpl->service; }
696
697   std::string RepoInfo::targetDistribution() const
698   { return _pimpl->targetDistro; }
699
700   Url RepoInfo::rawUrl() const
701   { return( _pimpl->baseUrls().empty() ? Url() : *_pimpl->baseUrls().rawBegin() ); }
702
703   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
704   { return _pimpl->baseUrls().transformedBegin(); }
705
706   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
707   { return _pimpl->baseUrls().transformedEnd(); }
708
709   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
710   { return _pimpl->baseUrls().size(); }
711
712   bool RepoInfo::baseUrlsEmpty() const
713   { return _pimpl->baseUrls().empty(); }
714
715   bool RepoInfo::baseUrlSet() const
716   { return _pimpl->baseurl2dump(); }
717
718   const std::set<std::string> & RepoInfo::contentKeywords() const
719   { return _pimpl->contentKeywords(); }
720
721   void RepoInfo::addContent( const std::string & keyword_r )
722   { _pimpl->addContent( keyword_r ); }
723
724   bool RepoInfo::hasContent() const
725   { return _pimpl->hasContent(); }
726
727   bool RepoInfo::hasContent( const std::string & keyword_r ) const
728   { return _pimpl->hasContent( keyword_r ); }
729
730   ///////////////////////////////////////////////////////////////////
731
732   bool RepoInfo::hasLicense() const
733   { return hasLicense( std::string() ); }
734
735   bool RepoInfo::hasLicense( const std::string & name_r ) const
736   { return !_pimpl->licenseTgz( name_r ).empty(); }
737
738
739   bool RepoInfo::needToAcceptLicense() const
740   { return needToAcceptLicense( std::string() ); }
741
742   bool RepoInfo::needToAcceptLicense( const std::string & name_r ) const
743   {
744     const Pathname & licenseTgz( _pimpl->licenseTgz( name_r ) );
745     if ( licenseTgz.empty() )
746       return false;     // no licenses at all
747
748     ExternalProgram::Arguments cmd;
749     cmd.push_back( "tar" );
750     cmd.push_back( "-t" );
751     cmd.push_back( "-z" );
752     cmd.push_back( "-f" );
753     cmd.push_back( licenseTgz.asString() );
754     ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
755
756     bool accept = true;
757     static const std::string noAcceptanceFile = "no-acceptance-needed\n";
758     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
759     {
760       if ( output == noAcceptanceFile )
761       {
762         accept = false;
763       }
764     }
765     prog.close();
766     MIL << "License(" << name_r << ") in " << name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
767     return accept;
768   }
769
770
771   std::string RepoInfo::getLicense( const Locale & lang_r )
772   { return const_cast<const RepoInfo *>(this)->getLicense( std::string(), lang_r ); }
773
774   std::string RepoInfo::getLicense( const Locale & lang_r ) const
775   { return getLicense( std::string(), lang_r ); }
776
777   std::string RepoInfo::getLicense( const std::string & name_r, const Locale & lang_r ) const
778   {
779     LocaleSet avlocales( getLicenseLocales( name_r ) );
780     if ( avlocales.empty() )
781       return std::string();
782
783     Locale getLang( Locale::bestMatch( avlocales, lang_r ) );
784     if ( !getLang && avlocales.find( Locale::noCode ) == avlocales.end() )
785     {
786       WAR << "License(" << name_r << ") in " << name() << " contains no fallback text!" << endl;
787       // Using the fist locale instead of returning no text at all.
788       // So the user might recognize that there is a license, even if he
789       // can't read it.
790       getLang = *avlocales.begin();
791     }
792
793     // now extract the license file.
794     static const std::string licenseFileFallback( "license.txt" );
795     std::string licenseFile( !getLang ? licenseFileFallback
796                                       : str::form( "license.%s.txt", getLang.c_str() ) );
797
798     ExternalProgram::Arguments cmd;
799     cmd.push_back( "tar" );
800     cmd.push_back( "-x" );
801     cmd.push_back( "-z" );
802     cmd.push_back( "-O" );
803     cmd.push_back( "-f" );
804     cmd.push_back( _pimpl->licenseTgz( name_r ).asString() ); // if it not exists, avlocales was empty.
805     cmd.push_back( licenseFile );
806
807     std::string ret;
808     ExternalProgram prog( cmd, ExternalProgram::Discard_Stderr );
809     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
810     {
811       ret += output;
812     }
813     prog.close();
814     return ret;
815   }
816
817
818   LocaleSet RepoInfo::getLicenseLocales() const
819   { return getLicenseLocales( std::string() ); }
820
821   LocaleSet RepoInfo::getLicenseLocales( const std::string & name_r ) const
822   {
823     const Pathname & licenseTgz( _pimpl->licenseTgz( name_r ) );
824     if ( licenseTgz.empty() )
825       return LocaleSet();
826
827     ExternalProgram::Arguments cmd;
828     cmd.push_back( "tar" );
829     cmd.push_back( "-t" );
830     cmd.push_back( "-z" );
831     cmd.push_back( "-f" );
832     cmd.push_back( licenseTgz.asString() );
833
834     LocaleSet ret;
835     ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
836     for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
837     {
838       static const C_Str license( "license." );
839       static const C_Str dotTxt( ".txt\n" );
840       if ( str::hasPrefix( output, license ) && str::hasSuffix( output, dotTxt ) )
841       {
842         if ( output.size() <= license.size() +  dotTxt.size() ) // license.txt
843           ret.insert( Locale() );
844         else
845           ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
846       }
847     }
848     prog.close();
849     return ret;
850   }
851
852   ///////////////////////////////////////////////////////////////////
853
854   std::ostream & RepoInfo::dumpOn( std::ostream & str ) const
855   {
856     RepoInfoBase::dumpOn(str);
857     if ( _pimpl->baseurl2dump() )
858     {
859       for ( const auto & url : _pimpl->baseUrls().raw() )
860       {
861         str << "- url         : " << url << std::endl;
862       }
863     }
864
865     // print if non empty value
866     auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
867       if ( ! value_r.empty() )
868         str << tag_r << value_r << std::endl;
869     });
870
871     strif( (_pimpl->_mirrorListForceMetalink ? "- metalink    : " : "- mirrorlist  : "), rawMirrorListUrl().asString() );
872     strif( "- path        : ", path().asString() );
873     str << "- type        : " << type() << std::endl;
874     str << "- priority    : " << priority() << std::endl;
875
876     // Yes No Default(Y) Default(N)
877 #define OUTS(T,B) ( indeterminate(T) ? (std::string("D(")+(B?"Y":"N")+")") : ((bool)T?"Y":"N") )
878     str << "- gpgcheck    : " << OUTS(_pimpl->rawGpgCheck(),gpgCheck())
879                               << " repo" << OUTS(_pimpl->rawRepoGpgCheck(),repoGpgCheck()) << (repoGpgCheckIsMandatory() ? "* ": " " )
880                               << "sig" << asString( validRepoSignature(), "?", "Y", "N" )
881                               << " pkg" << OUTS(_pimpl->rawPkgGpgCheck(),pkgGpgCheck()) << (pkgGpgCheckIsMandatory() ? "* ": " " )
882                               << std::endl;
883 #undef OUTS
884
885     for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
886     {
887       str << "- gpgkey      : " << url << std::endl;
888     }
889
890     if ( ! indeterminate(_pimpl->keeppackages) )
891       str << "- keeppackages: " << keepPackages() << std::endl;
892
893     strif( "- service     : ", service() );
894     strif( "- targetdistro: ", targetDistribution() );
895     strif( "- filePath:     ", filepath().asString() );
896     strif( "- metadataPath: ", metadataPath().asString() );
897     strif( "- packagesPath: ", packagesPath().asString() );
898
899     return str;
900   }
901
902   std::ostream & RepoInfo::dumpAsIniOn( std::ostream & str ) const
903   {
904     RepoInfoBase::dumpAsIniOn(str);
905
906     if ( _pimpl->baseurl2dump() )
907     {
908       str << "baseurl=";
909       std::string indent;
910       for ( const auto & url : _pimpl->baseUrls().raw() )
911       {
912         str << indent << url << endl;
913         if ( indent.empty() ) indent = "        ";      // "baseurl="
914       }
915     }
916
917     if ( ! _pimpl->path.empty() )
918       str << "path="<< path() << endl;
919
920     if ( ! (rawMirrorListUrl().asString().empty()) )
921       str << (_pimpl->_mirrorListForceMetalink ? "metalink=" : "mirrorlist=") << rawMirrorListUrl() << endl;
922
923     str << "type=" << type().asString() << endl;
924
925     if ( priority() != defaultPriority() )
926       str << "priority=" << priority() << endl;
927
928     if ( ! indeterminate(_pimpl->rawGpgCheck()) )
929       str << "gpgcheck=" << (_pimpl->rawGpgCheck() ? "1" : "0") << endl;
930
931     if ( ! indeterminate(_pimpl->rawRepoGpgCheck()) )
932       str << "repo_gpgcheck=" << (_pimpl->rawRepoGpgCheck() ? "1" : "0") << endl;
933
934     if ( ! indeterminate(_pimpl->rawPkgGpgCheck()) )
935       str << "pkg_gpgcheck=" << (_pimpl->rawPkgGpgCheck() ? "1" : "0") << endl;
936
937     {
938       std::string indent( "gpgkey=");
939       for ( const auto & url : _pimpl->gpgKeyUrls().raw() )
940       {
941         str << indent << url << endl;
942         if ( indent[0] != ' ' )
943           indent = "       ";
944       }
945     }
946
947     if (!indeterminate(_pimpl->keeppackages))
948       str << "keeppackages=" << keepPackages() << endl;
949
950     if( ! service().empty() )
951       str << "service=" << service() << endl;
952
953     return str;
954   }
955
956   std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
957   {
958     std::string tmpstr;
959     str
960       << "<repo"
961       << " alias=\"" << escape(alias()) << "\""
962       << " name=\"" << escape(name()) << "\"";
963     if (type() != repo::RepoType::NONE)
964       str << " type=\"" << type().asString() << "\"";
965     str
966       << " priority=\"" << priority() << "\""
967       << " enabled=\"" << enabled() << "\""
968       << " autorefresh=\"" << autorefresh() << "\""
969       << " gpgcheck=\"" << gpgCheck() << "\""
970       << " repo_gpgcheck=\"" << repoGpgCheck() << "\""
971       << " pkg_gpgcheck=\"" << pkgGpgCheck() << "\"";
972     if (!(tmpstr = gpgKeyUrl().asString()).empty())
973       str << " gpgkey=\"" << escape(tmpstr) << "\"";
974     if (!(tmpstr = mirrorListUrl().asString()).empty())
975       str << (_pimpl->_mirrorListForceMetalink ? " metalink=\"" : " mirrorlist=\"") << escape(tmpstr) << "\"";
976     str << ">" << endl;
977
978     if ( _pimpl->baseurl2dump() )
979     {
980       for_( it, baseUrlsBegin(), baseUrlsEnd() )        // !transform iterator replaces variables
981         str << "<url>" << escape((*it).asString()) << "</url>" << endl;
982     }
983
984     str << "</repo>" << endl;
985     return str;
986   }
987
988
989   std::ostream & operator<<( std::ostream & str, const RepoInfo & obj )
990   {
991     return obj.dumpOn(str);
992   }
993
994   std::ostream & operator<<( std::ostream & str, const RepoInfo::GpgCheck & obj )
995   {
996     switch ( obj )
997     {
998 #define OUTS( V ) case RepoInfo::V: return str << #V; break
999       OUTS( GpgCheck::On );
1000       OUTS( GpgCheck::Strict );
1001       OUTS( GpgCheck::AllowUnsigned );
1002       OUTS( GpgCheck::AllowUnsignedRepo );
1003       OUTS( GpgCheck::AllowUnsignedPackage );
1004       OUTS( GpgCheck::Default );
1005       OUTS( GpgCheck::Off );
1006       OUTS( GpgCheck::indeterminate );
1007 #undef OUTS
1008     }
1009     return str << "GpgCheck::UNKNOWN";
1010   }
1011
1012   /////////////////////////////////////////////////////////////////
1013 } // namespace zypp
1014 ///////////////////////////////////////////////////////////////////