1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "passwordcontainer.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
34*cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
35*cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/task/MasterPasswordRequest.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/task/NoMasterException.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <rtl/cipher.h>
41*cdf0e10cSrcweir #include <rtl/digest.h>
42*cdf0e10cSrcweir #include <rtl/byteseq.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #ifndef _TOOLS_INETSTRM_HXX
45*cdf0e10cSrcweir // @@@ #include <inetstrm.hxx>
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace std;
49*cdf0e10cSrcweir using namespace osl;
50*cdf0e10cSrcweir using namespace utl;
51*cdf0e10cSrcweir using namespace com::sun::star;
52*cdf0e10cSrcweir using namespace com::sun::star::uno;
53*cdf0e10cSrcweir using namespace com::sun::star::registry;
54*cdf0e10cSrcweir using namespace com::sun::star::lang;
55*cdf0e10cSrcweir using namespace com::sun::star::task;
56*cdf0e10cSrcweir using namespace com::sun::star::ucb;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir //-------------------------------------------------------------------------
59*cdf0e10cSrcweir //-------------------------------------------------------------------------
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir static ::rtl::OUString createIndex( vector< ::rtl::OUString > lines )
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     ::rtl::OString aResult;
64*cdf0e10cSrcweir     const sal_Char* pLine;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     for( unsigned int i = 0; i < lines.size(); i++ )
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         if( i )
69*cdf0e10cSrcweir             aResult += ::rtl::OString( "__" );
70*cdf0e10cSrcweir         ::rtl::OString line = ::rtl::OUStringToOString( lines[i], RTL_TEXTENCODING_UTF8 );
71*cdf0e10cSrcweir         pLine = line.getStr();
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         while( *pLine )
74*cdf0e10cSrcweir         {
75*cdf0e10cSrcweir             if( ( *pLine >= 'A' && *pLine <= 'Z' )
76*cdf0e10cSrcweir                 || ( *pLine >= 'a' && *pLine <= 'z' )
77*cdf0e10cSrcweir                 || ( *pLine >= '0' && *pLine <= '9' ) )
78*cdf0e10cSrcweir             {
79*cdf0e10cSrcweir                 aResult += ::rtl::OString::valueOf( *pLine );
80*cdf0e10cSrcweir             }
81*cdf0e10cSrcweir             else
82*cdf0e10cSrcweir             {
83*cdf0e10cSrcweir                 aResult += ::rtl::OString("_");
84*cdf0e10cSrcweir                 aResult += ::rtl::OString::valueOf( (sal_Int32) *pLine, 16 );
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             pLine++;
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii( aResult.getStr() );
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir //-------------------------------------------------------------------------
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir static vector< ::rtl::OUString > getInfoFromInd( ::rtl::OUString aInd )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     vector< ::rtl::OUString > aResult;
99*cdf0e10cSrcweir     sal_Bool aStart = sal_True;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     ::rtl::OString line = ::rtl::OUStringToOString( aInd, RTL_TEXTENCODING_ASCII_US );
102*cdf0e10cSrcweir     const sal_Char* pLine = line.getStr();
103*cdf0e10cSrcweir     do
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         ::rtl::OUString newItem;
106*cdf0e10cSrcweir         if( !aStart )
107*cdf0e10cSrcweir             pLine += 2;
108*cdf0e10cSrcweir         else
109*cdf0e10cSrcweir             aStart = sal_False;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir         while( *pLine && !( pLine[0] == '_' && pLine[1] == '_' ))
112*cdf0e10cSrcweir             if( *pLine != '_' )
113*cdf0e10cSrcweir             {
114*cdf0e10cSrcweir                 newItem += ::rtl::OUString::valueOf( (sal_Unicode) *pLine );
115*cdf0e10cSrcweir                 pLine++;
116*cdf0e10cSrcweir             }
117*cdf0e10cSrcweir             else
118*cdf0e10cSrcweir             {
119*cdf0e10cSrcweir                 ::rtl::OUString aNum;
120*cdf0e10cSrcweir                 for( int i = 1; i < 3; i++ )
121*cdf0e10cSrcweir                 {
122*cdf0e10cSrcweir                     if( !pLine[i]
123*cdf0e10cSrcweir                       ||  ( ( pLine[i] < '0' || pLine[i] > '9' )
124*cdf0e10cSrcweir                          && ( pLine[i] < 'a' || pLine[i] > 'f' )
125*cdf0e10cSrcweir                          && ( pLine[i] < 'A' || pLine[i] > 'F' ) ) )
126*cdf0e10cSrcweir                     {
127*cdf0e10cSrcweir                         OSL_ENSURE( sal_False, "Wrong index syntax!\n" );
128*cdf0e10cSrcweir                         return aResult;
129*cdf0e10cSrcweir                     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir                     aNum += ::rtl::OUString::valueOf( (sal_Unicode) pLine[i] );
132*cdf0e10cSrcweir                 }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                 newItem += ::rtl::OUString::valueOf( (sal_Unicode) aNum.toInt32( 16 ) );
135*cdf0e10cSrcweir                 pLine += 3;
136*cdf0e10cSrcweir             }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         aResult.push_back( newItem );
139*cdf0e10cSrcweir     } while( pLine[0] == '_' && pLine[1] == '_' );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     if( *pLine )
142*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "Wrong index syntax!\n" );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     return aResult;
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir //-------------------------------------------------------------------------
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir static sal_Bool shorterUrl( ::rtl::OUString& aURL )
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     sal_Int32 aInd = aURL.lastIndexOf( sal_Unicode( '/' ) );
152*cdf0e10cSrcweir     if( aInd > 0  && aURL.indexOf( ::rtl::OUString::createFromAscii( "://" ) ) != aInd-2 )
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         aURL = aURL.copy( 0, aInd );
155*cdf0e10cSrcweir         return sal_True;
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     return sal_False;
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir //-------------------------------------------------------------------------
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir static ::rtl::OUString getAsciiLine( const ::rtl::ByteSequence& buf )
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir     ::rtl::OUString aResult;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     ::rtl::ByteSequence outbuf( buf.getLength()*2+1 );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     for( int ind = 0; ind < buf.getLength(); ind++ )
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir         outbuf[ind*2]   = ( ((sal_uInt8)buf[ind]) >> 4 ) + 'a';
172*cdf0e10cSrcweir         outbuf[ind*2+1] = ( ((sal_uInt8)buf[ind]) & 0x0f ) + 'a';
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir     outbuf[buf.getLength()*2] = '\0';
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     aResult = ::rtl::OUString::createFromAscii( (sal_Char*)outbuf.getArray() );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     return aResult;
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir //-------------------------------------------------------------------------
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir static ::rtl::ByteSequence getBufFromAsciiLine( ::rtl::OUString line )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     OSL_ENSURE( line.getLength() % 2 == 0, "Wrong syntax!\n" );
186*cdf0e10cSrcweir     ::rtl::OString tmpLine = ::rtl::OUStringToOString( line, RTL_TEXTENCODING_ASCII_US );
187*cdf0e10cSrcweir     ::rtl::ByteSequence aResult(line.getLength()/2);
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     for( int ind = 0; ind < tmpLine.getLength()/2; ind++ )
190*cdf0e10cSrcweir     {
191*cdf0e10cSrcweir         aResult[ind] = ( (sal_uInt8)( tmpLine.getStr()[ind*2] - 'a' ) << 4 ) | (sal_uInt8)( tmpLine.getStr()[ind*2+1] - 'a' );
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     return aResult;
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir //-------------------------------------------------------------------------
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir static Sequence< ::rtl::OUString > copyVectorToSequence( const vector< ::rtl::OUString >& original )
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir     Sequence< ::rtl::OUString > newOne ( original.size() );
202*cdf0e10cSrcweir     for( unsigned int i = 0; i < original.size() ; i++ )
203*cdf0e10cSrcweir         newOne[i] = original[i];
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     return newOne;
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir static vector< ::rtl::OUString > copySequenceToVector( const Sequence< ::rtl::OUString >& original )
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir     vector< ::rtl::OUString > newOne ( original.getLength() );
211*cdf0e10cSrcweir     for( int i = 0; i < original.getLength() ; i++ )
212*cdf0e10cSrcweir         newOne[i] = original[i];
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     return newOne;
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir //-------------------------------------------------------------------------
218*cdf0e10cSrcweir //-------------------------------------------------------------------------
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir PassMap StorageItem::getInfo()
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir     PassMap aResult;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aNodeNames     = ConfigItem::GetNodeNames( ::rtl::OUString::createFromAscii("Store") );
225*cdf0e10cSrcweir     sal_Int32 aNodeCount = aNodeNames.getLength();
226*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aPropNames( aNodeCount );
227*cdf0e10cSrcweir     sal_Int32 aNodeInd;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     for( aNodeInd = 0; aNodeInd < aNodeCount; ++aNodeInd )
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         aPropNames[aNodeInd]  = ::rtl::OUString::createFromAscii( "Store/Passwordstorage['" );
232*cdf0e10cSrcweir         aPropNames[aNodeInd] += aNodeNames[aNodeInd];
233*cdf0e10cSrcweir         aPropNames[aNodeInd] += ::rtl::OUString::createFromAscii( "']/Password" );
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir     Sequence< Any > aPropertyValues = ConfigItem::GetProperties( aPropNames );
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir     if( aPropertyValues.getLength() != aNodeNames.getLength() )
239*cdf0e10cSrcweir     {
240*cdf0e10cSrcweir         OSL_ENSURE( aPropertyValues.getLength() == aNodeNames.getLength(), "Problems during reading\n" );
241*cdf0e10cSrcweir         return aResult;
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     for( aNodeInd = 0; aNodeInd < aNodeCount; ++aNodeInd )
245*cdf0e10cSrcweir     {
246*cdf0e10cSrcweir         vector< ::rtl::OUString > aUrlUsr = getInfoFromInd( aNodeNames[aNodeInd] );
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         if( aUrlUsr.size() == 2 )
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             ::rtl::OUString aUrl  = aUrlUsr[0];
251*cdf0e10cSrcweir             ::rtl::OUString aName = aUrlUsr[1];
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir             ::rtl::OUString aEPasswd;
254*cdf0e10cSrcweir             aPropertyValues[aNodeInd] >>= aEPasswd;
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir             PassMap::iterator aIter = aResult.find( aUrl );
257*cdf0e10cSrcweir             if( aIter != aResult.end() )
258*cdf0e10cSrcweir                 aIter->second.push_back( NamePassRecord( aName, aEPasswd ) );
259*cdf0e10cSrcweir             else
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 NamePassRecord aNewRecord( aName, aEPasswd );
262*cdf0e10cSrcweir                 list< NamePassRecord > listToAdd( 1, aNewRecord );
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir                 aResult.insert( PairUrlRecord( aUrl, listToAdd ) );
265*cdf0e10cSrcweir             }
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir         else
268*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "Wrong index sintax!\n" );
269*cdf0e10cSrcweir     }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     return aResult;
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir //-------------------------------------------------------------------------
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir void StorageItem::setUseStorage( sal_Bool bUse )
277*cdf0e10cSrcweir {
278*cdf0e10cSrcweir     Sequence< ::rtl::OUString > sendNames(1);
279*cdf0e10cSrcweir     Sequence< uno::Any > sendVals(1);
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     sendNames[0] = ::rtl::OUString::createFromAscii( "UseStorage" );
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     sendVals[0] <<= bUse;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     ConfigItem::SetModified();
286*cdf0e10cSrcweir     ConfigItem::PutProperties( sendNames, sendVals );
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir //-------------------------------------------------------------------------
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir sal_Bool StorageItem::useStorage()
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aNodeNames( 1 );
294*cdf0e10cSrcweir     aNodeNames[0] = ::rtl::OUString::createFromAscii( "UseStorage" );
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     Sequence< Any > aPropertyValues = ConfigItem::GetProperties( aNodeNames );
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir     if( aPropertyValues.getLength() != aNodeNames.getLength() )
299*cdf0e10cSrcweir     {
300*cdf0e10cSrcweir         OSL_ENSURE( aPropertyValues.getLength() == aNodeNames.getLength(), "Problems during reading\n" );
301*cdf0e10cSrcweir         return sal_False;
302*cdf0e10cSrcweir     }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     sal_Bool aResult = false;
305*cdf0e10cSrcweir     aPropertyValues[0] >>= aResult;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir     return aResult;
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir //-------------------------------------------------------------------------
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir sal_Bool StorageItem::getEncodedMP( ::rtl::OUString& aResult )
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir     if( hasEncoded )
315*cdf0e10cSrcweir     {
316*cdf0e10cSrcweir         aResult = mEncoded;
317*cdf0e10cSrcweir         return sal_True;
318*cdf0e10cSrcweir     }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aNodeNames( 2 );
321*cdf0e10cSrcweir     aNodeNames[0] = ::rtl::OUString::createFromAscii( "HasMaster" );
322*cdf0e10cSrcweir     aNodeNames[1] = ::rtl::OUString::createFromAscii( "Master" );
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir     Sequence< Any > aPropertyValues = ConfigItem::GetProperties( aNodeNames );
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir     if( aPropertyValues.getLength() != aNodeNames.getLength() )
327*cdf0e10cSrcweir     {
328*cdf0e10cSrcweir         OSL_ENSURE( aPropertyValues.getLength() == aNodeNames.getLength(), "Problems during reading\n" );
329*cdf0e10cSrcweir         return sal_False;
330*cdf0e10cSrcweir     }
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir     aPropertyValues[0] >>= hasEncoded;
333*cdf0e10cSrcweir     aPropertyValues[1] >>= mEncoded;
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir     aResult = mEncoded;
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir     return hasEncoded;
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir //-------------------------------------------------------------------------
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir void StorageItem::setEncodedMP( const ::rtl::OUString& aEncoded, sal_Bool bAcceptEmpty )
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     Sequence< ::rtl::OUString > sendNames(2);
345*cdf0e10cSrcweir     Sequence< uno::Any > sendVals(2);
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir     sendNames[0] = ::rtl::OUString::createFromAscii( "HasMaster" );
348*cdf0e10cSrcweir     sendNames[1] = ::rtl::OUString::createFromAscii( "Master" );
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir     sal_Bool bHasMaster = ( aEncoded.getLength() > 0 || bAcceptEmpty );
351*cdf0e10cSrcweir     sendVals[0] <<= bHasMaster;
352*cdf0e10cSrcweir     sendVals[1] <<= aEncoded;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir     ConfigItem::SetModified();
355*cdf0e10cSrcweir     ConfigItem::PutProperties( sendNames, sendVals );
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir     hasEncoded = bHasMaster;
358*cdf0e10cSrcweir     mEncoded = aEncoded;
359*cdf0e10cSrcweir }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir //-------------------------------------------------------------------------
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir void StorageItem::remove( const ::rtl::OUString& aURL, const ::rtl::OUString& aName )
364*cdf0e10cSrcweir {
365*cdf0e10cSrcweir     vector < ::rtl::OUString > forIndex;
366*cdf0e10cSrcweir     forIndex.push_back( aURL );
367*cdf0e10cSrcweir     forIndex.push_back( aName );
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir     Sequence< ::rtl::OUString > sendSeq(1);
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir     sendSeq[0] = createIndex( forIndex );
372*cdf0e10cSrcweir     // sendSeq[0]  = ::rtl::OUString::createFromAscii( "Store/Passwordstorage['" );
373*cdf0e10cSrcweir     // sendSeq[0] += createIndex( forIndex );
374*cdf0e10cSrcweir     // sendSeq[0] += ::rtl::OUString::createFromAscii( "']" );
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir     ConfigItem::ClearNodeElements( ::rtl::OUString::createFromAscii( "Store" ), sendSeq );
377*cdf0e10cSrcweir }
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir //-------------------------------------------------------------------------
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir void StorageItem::clear()
382*cdf0e10cSrcweir {
383*cdf0e10cSrcweir     Sequence< ::rtl::OUString > sendSeq(1);
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir     ConfigItem::ClearNodeSet( ::rtl::OUString::createFromAscii( "Store" ) );
386*cdf0e10cSrcweir }
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir //-------------------------------------------------------------------------
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir void StorageItem::update( const ::rtl::OUString& aURL, const NamePassRecord& aRecord )
391*cdf0e10cSrcweir {
392*cdf0e10cSrcweir     if ( !aRecord.HasPasswords( PERSISTENT_RECORD ) )
393*cdf0e10cSrcweir     {
394*cdf0e10cSrcweir         OSL_ASSERT( "Unexpected storing of a record!" );
395*cdf0e10cSrcweir         return;
396*cdf0e10cSrcweir     }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir     vector < ::rtl::OUString > forIndex;
399*cdf0e10cSrcweir     forIndex.push_back( aURL );
400*cdf0e10cSrcweir     forIndex.push_back( aRecord.GetUserName() );
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     Sequence< beans::PropertyValue > sendSeq(1);
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir     sendSeq[0].Name  = ::rtl::OUString::createFromAscii( "Store/Passwordstorage['" );
405*cdf0e10cSrcweir     sendSeq[0].Name += createIndex( forIndex );
406*cdf0e10cSrcweir     sendSeq[0].Name += ::rtl::OUString::createFromAscii( "']/Password" );
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     sendSeq[0].Value <<= aRecord.GetPersPasswords();
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir     ConfigItem::SetModified();
411*cdf0e10cSrcweir     ConfigItem::SetSetProperties( ::rtl::OUString::createFromAscii( "Store" ), sendSeq );
412*cdf0e10cSrcweir }
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir //-------------------------------------------------------------------------
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir void StorageItem::Notify( const Sequence< ::rtl::OUString >& )
417*cdf0e10cSrcweir {
418*cdf0e10cSrcweir     // this feature still should not be used
419*cdf0e10cSrcweir     if( mainCont )
420*cdf0e10cSrcweir         mainCont->Notify();
421*cdf0e10cSrcweir }
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir //-------------------------------------------------------------------------
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir void StorageItem::Commit()
426*cdf0e10cSrcweir {
427*cdf0e10cSrcweir     // Do nothing, we stored everything we want already
428*cdf0e10cSrcweir }
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir //-------------------------------------------------------------------------
431*cdf0e10cSrcweir //-------------------------------------------------------------------------
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir PasswordContainer::PasswordContainer( const Reference<XMultiServiceFactory>& xServiceFactory ):
434*cdf0e10cSrcweir     m_pStorageFile( NULL )
435*cdf0e10cSrcweir {
436*cdf0e10cSrcweir     // m_pStorageFile->Notify() can be called
437*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir     mComponent = Reference< XComponent >( xServiceFactory, UNO_QUERY );
440*cdf0e10cSrcweir     mComponent->addEventListener( this );
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir     m_pStorageFile = new StorageItem( this, ::rtl::OUString::createFromAscii( "Office.Common/Passwords" ) );
443*cdf0e10cSrcweir     if( m_pStorageFile )
444*cdf0e10cSrcweir         if( m_pStorageFile->useStorage() )
445*cdf0e10cSrcweir             m_aContainer = m_pStorageFile->getInfo();
446*cdf0e10cSrcweir }
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir //-------------------------------------------------------------------------
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir PasswordContainer::~PasswordContainer()
451*cdf0e10cSrcweir {
452*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir     if( m_pStorageFile )
455*cdf0e10cSrcweir     {
456*cdf0e10cSrcweir         delete m_pStorageFile;
457*cdf0e10cSrcweir         m_pStorageFile = NULL;
458*cdf0e10cSrcweir     }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir     if( mComponent.is() )
461*cdf0e10cSrcweir     {
462*cdf0e10cSrcweir         mComponent->removeEventListener(this);
463*cdf0e10cSrcweir         mComponent = Reference< XComponent >();
464*cdf0e10cSrcweir     }
465*cdf0e10cSrcweir }
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir //-------------------------------------------------------------------------
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir void SAL_CALL PasswordContainer::disposing( const EventObject& ) throw(RuntimeException)
470*cdf0e10cSrcweir {
471*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir     if( m_pStorageFile )
474*cdf0e10cSrcweir     {
475*cdf0e10cSrcweir         delete m_pStorageFile;
476*cdf0e10cSrcweir         m_pStorageFile = NULL;
477*cdf0e10cSrcweir     }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir     if( mComponent.is() )
480*cdf0e10cSrcweir     {
481*cdf0e10cSrcweir         //mComponent->removeEventListener(this);
482*cdf0e10cSrcweir         mComponent = Reference< XComponent >();
483*cdf0e10cSrcweir     }
484*cdf0e10cSrcweir }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir //-------------------------------------------------------------------------
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir vector< ::rtl::OUString > PasswordContainer::DecodePasswords( const ::rtl::OUString& aLine, const ::rtl::OUString& aMasterPasswd ) throw(RuntimeException)
489*cdf0e10cSrcweir {
490*cdf0e10cSrcweir     if( aMasterPasswd.getLength() )
491*cdf0e10cSrcweir     {
492*cdf0e10cSrcweir         rtlCipher aDecoder = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
493*cdf0e10cSrcweir         OSL_ENSURE( aDecoder, "Can't create decoder\n" );
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir         if( aDecoder )
496*cdf0e10cSrcweir         {
497*cdf0e10cSrcweir             OSL_ENSURE( aMasterPasswd.getLength() == RTL_DIGEST_LENGTH_MD5 * 2, "Wrong master password format!\n" );
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir             unsigned char code[RTL_DIGEST_LENGTH_MD5];
500*cdf0e10cSrcweir             for( int ind = 0; ind < RTL_DIGEST_LENGTH_MD5; ind++ )
501*cdf0e10cSrcweir                 code[ ind ] = (char)(aMasterPasswd.copy( ind*2, 2 ).toInt32(16));
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir             rtlCipherError result = rtl_cipher_init (
504*cdf0e10cSrcweir                     aDecoder, rtl_Cipher_DirectionDecode,
505*cdf0e10cSrcweir                     code, RTL_DIGEST_LENGTH_MD5, NULL, 0 );
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir             if( result == rtl_Cipher_E_None )
508*cdf0e10cSrcweir             {
509*cdf0e10cSrcweir                 ::rtl::ByteSequence aSeq = getBufFromAsciiLine( aLine );
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir                 ::rtl::ByteSequence resSeq( aSeq.getLength() );
512*cdf0e10cSrcweir 
513*cdf0e10cSrcweir                 result = rtl_cipher_decode ( aDecoder, (sal_uInt8*)aSeq.getArray(), aSeq.getLength(),
514*cdf0e10cSrcweir                                                         (sal_uInt8*)resSeq.getArray(), resSeq.getLength() );
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir                 ::rtl::OUString aPasswd( ( sal_Char* )resSeq.getArray(), resSeq.getLength(), RTL_TEXTENCODING_UTF8 );
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir                 rtl_cipher_destroy (aDecoder);
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir                 return getInfoFromInd( aPasswd );
521*cdf0e10cSrcweir             }
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir             rtl_cipher_destroy (aDecoder);
524*cdf0e10cSrcweir         }
525*cdf0e10cSrcweir     }
526*cdf0e10cSrcweir     else
527*cdf0e10cSrcweir     {
528*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "No master password provided!\n" );
529*cdf0e10cSrcweir         // throw special exception
530*cdf0e10cSrcweir     }
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir     // problems with decoding
533*cdf0e10cSrcweir     OSL_ENSURE( sal_False, "Problem with decoding\n" );
534*cdf0e10cSrcweir     throw RuntimeException( ::rtl::OUString::createFromAscii( "Can't decode!" ), Reference< XInterface >() );
535*cdf0e10cSrcweir }
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir //-------------------------------------------------------------------------
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir ::rtl::OUString PasswordContainer::EncodePasswords( vector< ::rtl::OUString > lines, const ::rtl::OUString& aMasterPasswd ) throw(RuntimeException)
541*cdf0e10cSrcweir {
542*cdf0e10cSrcweir     if( aMasterPasswd.getLength() )
543*cdf0e10cSrcweir     {
544*cdf0e10cSrcweir         ::rtl::OString aSeq = ::rtl::OUStringToOString( createIndex( lines ), RTL_TEXTENCODING_UTF8 );
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir         rtlCipher aEncoder = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
547*cdf0e10cSrcweir         OSL_ENSURE( aEncoder, "Can't create encoder\n" );
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir         if( aEncoder )
550*cdf0e10cSrcweir         {
551*cdf0e10cSrcweir             OSL_ENSURE( aMasterPasswd.getLength() == RTL_DIGEST_LENGTH_MD5 * 2, "Wrong master password format!\n" );
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir             unsigned char code[RTL_DIGEST_LENGTH_MD5];
554*cdf0e10cSrcweir             for( int ind = 0; ind < RTL_DIGEST_LENGTH_MD5; ind++ )
555*cdf0e10cSrcweir                 code[ ind ] = (char)(aMasterPasswd.copy( ind*2, 2 ).toInt32(16));
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir             rtlCipherError result = rtl_cipher_init (
558*cdf0e10cSrcweir                     aEncoder, rtl_Cipher_DirectionEncode,
559*cdf0e10cSrcweir                     code, RTL_DIGEST_LENGTH_MD5, NULL, 0 );
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir             if( result == rtl_Cipher_E_None )
562*cdf0e10cSrcweir             {
563*cdf0e10cSrcweir                 ::rtl::ByteSequence resSeq(aSeq.getLength()+1);
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir                 result = rtl_cipher_encode ( aEncoder, (sal_uInt8*)aSeq.getStr(), aSeq.getLength()+1,
566*cdf0e10cSrcweir                                                         (sal_uInt8*)resSeq.getArray(), resSeq.getLength() );
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir /*
569*cdf0e10cSrcweir                 //test
570*cdf0e10cSrcweir                 rtlCipherError result = rtl_cipher_init (
571*cdf0e10cSrcweir                     aEncoder, rtl_Cipher_DirectionDecode,
572*cdf0e10cSrcweir                     code, RTL_DIGEST_LENGTH_MD5, NULL, 0 );
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir                 if( result == rtl_Cipher_E_None )
576*cdf0e10cSrcweir                 {
577*cdf0e10cSrcweir                     ::rtl::OUString testOU = getAsciiLine( resSeq );
578*cdf0e10cSrcweir                     ::rtl::ByteSequence aSeq1 = getBufFromAsciiLine( testOU );
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir                     ::rtl::ByteSequence resSeq1( aSeq1.getLength() );
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir                     if( resSeq.getLength() == aSeq1.getLength() )
583*cdf0e10cSrcweir                     {
584*cdf0e10cSrcweir                         for( int ind = 0; ind < aSeq1.getLength(); ind++ )
585*cdf0e10cSrcweir                             if( resSeq[ind] != aSeq1[ind] )
586*cdf0e10cSrcweir                                 testOU = ::rtl::OUString();
587*cdf0e10cSrcweir                     }
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir                     result = rtl_cipher_decode ( aEncoder, (sal_uInt8*)aSeq1.getArray(), aSeq1.getLength(),
590*cdf0e10cSrcweir                                                         (sal_uInt8*)resSeq1.getArray(), resSeq1.getLength() );
591*cdf0e10cSrcweir 
592*cdf0e10cSrcweir                     ::rtl::OUString aPasswd( ( sal_Char* )resSeq1.getArray(), resSeq1.getLength(), RTL_TEXTENCODING_UTF8 );
593*cdf0e10cSrcweir                 }
594*cdf0e10cSrcweir */
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir                 rtl_cipher_destroy (aEncoder);
597*cdf0e10cSrcweir 
598*cdf0e10cSrcweir                 if( result == rtl_Cipher_E_None )
599*cdf0e10cSrcweir                     return getAsciiLine( resSeq );
600*cdf0e10cSrcweir 
601*cdf0e10cSrcweir             }
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir             rtl_cipher_destroy (aEncoder);
604*cdf0e10cSrcweir         }
605*cdf0e10cSrcweir     }
606*cdf0e10cSrcweir     else
607*cdf0e10cSrcweir     {
608*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "No master password provided!\n" );
609*cdf0e10cSrcweir         // throw special exception
610*cdf0e10cSrcweir     }
611*cdf0e10cSrcweir 
612*cdf0e10cSrcweir     // problems with encoding
613*cdf0e10cSrcweir     OSL_ENSURE( sal_False, "Problem with encoding\n" );
614*cdf0e10cSrcweir     throw RuntimeException( ::rtl::OUString::createFromAscii( "Can't encode!" ), Reference< XInterface >() );
615*cdf0e10cSrcweir }
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir //-------------------------------------------------------------------------
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir void PasswordContainer::UpdateVector( const ::rtl::OUString& aURL, list< NamePassRecord >& toUpdate, NamePassRecord& aRecord, sal_Bool writeFile ) throw(RuntimeException)
620*cdf0e10cSrcweir {
621*cdf0e10cSrcweir     for( list< NamePassRecord >::iterator aNPIter = toUpdate.begin(); aNPIter != toUpdate.end(); aNPIter++ )
622*cdf0e10cSrcweir         if( aNPIter->GetUserName().equals( aRecord.GetUserName() ) )
623*cdf0e10cSrcweir         {
624*cdf0e10cSrcweir             if( aRecord.HasPasswords( MEMORY_RECORD ) )
625*cdf0e10cSrcweir                 aNPIter->SetMemPasswords( aRecord.GetMemPasswords() );
626*cdf0e10cSrcweir 
627*cdf0e10cSrcweir             if( aRecord.HasPasswords( PERSISTENT_RECORD ) )
628*cdf0e10cSrcweir             {
629*cdf0e10cSrcweir                 aNPIter->SetPersPasswords( aRecord.GetPersPasswords() );
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir                 if( writeFile )
632*cdf0e10cSrcweir                 {
633*cdf0e10cSrcweir                     // the password must be already encoded
634*cdf0e10cSrcweir                     m_pStorageFile->update( aURL, aRecord ); // change existing ( aURL, aName ) record in the configfile
635*cdf0e10cSrcweir                 }
636*cdf0e10cSrcweir             }
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir             return;
639*cdf0e10cSrcweir         }
640*cdf0e10cSrcweir 
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir     if( aRecord.HasPasswords( PERSISTENT_RECORD ) && writeFile )
643*cdf0e10cSrcweir     {
644*cdf0e10cSrcweir         // the password must be already encoded
645*cdf0e10cSrcweir         m_pStorageFile->update( aURL, aRecord ); // add new aName to the existing url
646*cdf0e10cSrcweir     }
647*cdf0e10cSrcweir 
648*cdf0e10cSrcweir     toUpdate.insert( toUpdate.begin(), aRecord );
649*cdf0e10cSrcweir }
650*cdf0e10cSrcweir 
651*cdf0e10cSrcweir //-------------------------------------------------------------------------
652*cdf0e10cSrcweir 
653*cdf0e10cSrcweir UserRecord PasswordContainer::CopyToUserRecord( const NamePassRecord& aRecord, sal_Bool& io_bTryToDecode, const Reference< XInteractionHandler >& aHandler )
654*cdf0e10cSrcweir {
655*cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aPasswords;
656*cdf0e10cSrcweir     if( aRecord.HasPasswords( MEMORY_RECORD ) )
657*cdf0e10cSrcweir         aPasswords = aRecord.GetMemPasswords();
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir     if( io_bTryToDecode && aRecord.HasPasswords( PERSISTENT_RECORD ) )
660*cdf0e10cSrcweir     {
661*cdf0e10cSrcweir         try
662*cdf0e10cSrcweir         {
663*cdf0e10cSrcweir             ::std::vector< ::rtl::OUString > aDecodedPasswords = DecodePasswords( aRecord.GetPersPasswords(), GetMasterPassword( aHandler ) );
664*cdf0e10cSrcweir             aPasswords.insert( aPasswords.end(), aDecodedPasswords.begin(), aDecodedPasswords.end() );
665*cdf0e10cSrcweir         }
666*cdf0e10cSrcweir         catch( NoMasterException& )
667*cdf0e10cSrcweir         {
668*cdf0e10cSrcweir             // if master password could not be detected the entry will be just ignored
669*cdf0e10cSrcweir             io_bTryToDecode = sal_False;
670*cdf0e10cSrcweir         }
671*cdf0e10cSrcweir     }
672*cdf0e10cSrcweir 
673*cdf0e10cSrcweir     return UserRecord( aRecord.GetUserName(), copyVectorToSequence( aPasswords ) );
674*cdf0e10cSrcweir }
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir //-------------------------------------------------------------------------
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir Sequence< UserRecord > PasswordContainer::CopyToUserRecordSequence( const list< NamePassRecord >& original, const Reference< XInteractionHandler >& aHandler ) throw(RuntimeException)
679*cdf0e10cSrcweir {
680*cdf0e10cSrcweir     Sequence< UserRecord >     aResult( original.size() );
681*cdf0e10cSrcweir     sal_uInt32 nInd = 0;
682*cdf0e10cSrcweir     sal_Bool bTryToDecode = sal_True;
683*cdf0e10cSrcweir 
684*cdf0e10cSrcweir     for( list< NamePassRecord >::const_iterator aNPIter = original.begin();
685*cdf0e10cSrcweir          aNPIter != original.end();
686*cdf0e10cSrcweir          aNPIter++, nInd++ )
687*cdf0e10cSrcweir     {
688*cdf0e10cSrcweir         aResult[nInd] = CopyToUserRecord( *aNPIter, bTryToDecode, aHandler );
689*cdf0e10cSrcweir     }
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir     return aResult;
692*cdf0e10cSrcweir }
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir //-------------------------------------------------------------------------
695*cdf0e10cSrcweir 
696*cdf0e10cSrcweir void SAL_CALL PasswordContainer::add( const ::rtl::OUString& Url, const ::rtl::OUString& UserName, const Sequence< ::rtl::OUString >& Passwords, const Reference< XInteractionHandler >& aHandler ) throw(RuntimeException)
697*cdf0e10cSrcweir {
698*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
699*cdf0e10cSrcweir 
700*cdf0e10cSrcweir     PrivateAdd( Url, UserName, Passwords, MEMORY_RECORD, aHandler );
701*cdf0e10cSrcweir }
702*cdf0e10cSrcweir 
703*cdf0e10cSrcweir //-------------------------------------------------------------------------
704*cdf0e10cSrcweir 
705*cdf0e10cSrcweir void SAL_CALL PasswordContainer::addPersistent( const ::rtl::OUString& Url, const ::rtl::OUString& UserName, const Sequence< ::rtl::OUString >& Passwords, const Reference< XInteractionHandler >& aHandler  ) throw(RuntimeException)
706*cdf0e10cSrcweir {
707*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
708*cdf0e10cSrcweir 
709*cdf0e10cSrcweir     PrivateAdd( Url, UserName, Passwords, PERSISTENT_RECORD, aHandler );
710*cdf0e10cSrcweir }
711*cdf0e10cSrcweir 
712*cdf0e10cSrcweir //-------------------------------------------------------------------------
713*cdf0e10cSrcweir 
714*cdf0e10cSrcweir void PasswordContainer::PrivateAdd( const ::rtl::OUString& Url, const ::rtl::OUString& UserName, const Sequence< ::rtl::OUString >& Passwords, char Mode, const Reference< XInteractionHandler >& aHandler ) throw(RuntimeException)
715*cdf0e10cSrcweir {
716*cdf0e10cSrcweir     NamePassRecord aRecord( UserName );
717*cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aStorePass = copySequenceToVector( Passwords );
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir     if( Mode == PERSISTENT_RECORD )
720*cdf0e10cSrcweir         aRecord.SetPersPasswords( EncodePasswords( aStorePass, GetMasterPassword( aHandler ) ) );
721*cdf0e10cSrcweir     else if( Mode == MEMORY_RECORD )
722*cdf0e10cSrcweir         aRecord.SetMemPasswords( aStorePass );
723*cdf0e10cSrcweir     else
724*cdf0e10cSrcweir     {
725*cdf0e10cSrcweir         OSL_ASSERT( "Unexpected persistence status!" );
726*cdf0e10cSrcweir         return;
727*cdf0e10cSrcweir     }
728*cdf0e10cSrcweir 
729*cdf0e10cSrcweir     if( !m_aContainer.empty() )
730*cdf0e10cSrcweir     {
731*cdf0e10cSrcweir         PassMap::iterator aIter = m_aContainer.find( Url );
732*cdf0e10cSrcweir 
733*cdf0e10cSrcweir         if( aIter != m_aContainer.end() )
734*cdf0e10cSrcweir         {
735*cdf0e10cSrcweir             UpdateVector( aIter->first, aIter->second, aRecord, sal_True );
736*cdf0e10cSrcweir             return;
737*cdf0e10cSrcweir         }
738*cdf0e10cSrcweir     }
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir     list< NamePassRecord > listToAdd( 1, aRecord );
741*cdf0e10cSrcweir     m_aContainer.insert( PairUrlRecord( Url, listToAdd ) );
742*cdf0e10cSrcweir 
743*cdf0e10cSrcweir     if( Mode == PERSISTENT_RECORD && m_pStorageFile && m_pStorageFile->useStorage() )
744*cdf0e10cSrcweir         m_pStorageFile->update( Url, aRecord );
745*cdf0e10cSrcweir 
746*cdf0e10cSrcweir }
747*cdf0e10cSrcweir 
748*cdf0e10cSrcweir //-------------------------------------------------------------------------
749*cdf0e10cSrcweir 
750*cdf0e10cSrcweir 
751*cdf0e10cSrcweir UrlRecord SAL_CALL PasswordContainer::find( const ::rtl::OUString& aURL, const Reference< XInteractionHandler >& aHandler  ) throw(RuntimeException)
752*cdf0e10cSrcweir {
753*cdf0e10cSrcweir     return find( aURL, rtl::OUString(), false, aHandler );
754*cdf0e10cSrcweir }
755*cdf0e10cSrcweir 
756*cdf0e10cSrcweir //-------------------------------------------------------------------------
757*cdf0e10cSrcweir 
758*cdf0e10cSrcweir UrlRecord SAL_CALL PasswordContainer::findForName( const ::rtl::OUString& aURL, const ::rtl::OUString& aName, const Reference< XInteractionHandler >& aHandler  ) throw(RuntimeException)
759*cdf0e10cSrcweir {
760*cdf0e10cSrcweir     return find( aURL, aName, true, aHandler );
761*cdf0e10cSrcweir }
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir //-------------------------------------------------------------------------
764*cdf0e10cSrcweir 
765*cdf0e10cSrcweir Sequence< UserRecord > PasswordContainer::FindUsr( const list< NamePassRecord >& userlist, const ::rtl::OUString& aName, const Reference< XInteractionHandler >& aHandler ) throw(RuntimeException)
766*cdf0e10cSrcweir {
767*cdf0e10cSrcweir     sal_uInt32 nInd = 0;
768*cdf0e10cSrcweir     for( list< NamePassRecord >::const_iterator aNPIter = userlist.begin();
769*cdf0e10cSrcweir          aNPIter != userlist.end();
770*cdf0e10cSrcweir          aNPIter++, nInd++ )
771*cdf0e10cSrcweir     {
772*cdf0e10cSrcweir         if( aNPIter->GetUserName().equals( aName ) )
773*cdf0e10cSrcweir         {
774*cdf0e10cSrcweir             Sequence< UserRecord > aResult(1);
775*cdf0e10cSrcweir             sal_Bool bTryToDecode = sal_True;
776*cdf0e10cSrcweir             aResult[0] = CopyToUserRecord( *aNPIter, bTryToDecode, aHandler );
777*cdf0e10cSrcweir 
778*cdf0e10cSrcweir             return aResult;
779*cdf0e10cSrcweir         }
780*cdf0e10cSrcweir     }
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir     return Sequence< UserRecord >();
783*cdf0e10cSrcweir }
784*cdf0e10cSrcweir 
785*cdf0e10cSrcweir //-------------------------------------------------------------------------
786*cdf0e10cSrcweir 
787*cdf0e10cSrcweir bool PasswordContainer::createUrlRecord(
788*cdf0e10cSrcweir     const PassMap::iterator & rIter,
789*cdf0e10cSrcweir     bool bName,
790*cdf0e10cSrcweir     const ::rtl::OUString & aName,
791*cdf0e10cSrcweir     const Reference< XInteractionHandler >& aHandler,
792*cdf0e10cSrcweir     UrlRecord & rRec )
793*cdf0e10cSrcweir         throw( RuntimeException )
794*cdf0e10cSrcweir {
795*cdf0e10cSrcweir     if ( bName )
796*cdf0e10cSrcweir     {
797*cdf0e10cSrcweir         Sequence< UserRecord > aUsrRec
798*cdf0e10cSrcweir             = FindUsr( rIter->second, aName, aHandler );
799*cdf0e10cSrcweir         if( aUsrRec.getLength() )
800*cdf0e10cSrcweir         {
801*cdf0e10cSrcweir             rRec = UrlRecord( rIter->first, aUsrRec );
802*cdf0e10cSrcweir             return true;
803*cdf0e10cSrcweir         }
804*cdf0e10cSrcweir     }
805*cdf0e10cSrcweir     else
806*cdf0e10cSrcweir     {
807*cdf0e10cSrcweir         rRec = UrlRecord(
808*cdf0e10cSrcweir             rIter->first,
809*cdf0e10cSrcweir             CopyToUserRecordSequence( rIter->second, aHandler ) );
810*cdf0e10cSrcweir         return true;
811*cdf0e10cSrcweir     }
812*cdf0e10cSrcweir     return false;
813*cdf0e10cSrcweir }
814*cdf0e10cSrcweir 
815*cdf0e10cSrcweir //-------------------------------------------------------------------------
816*cdf0e10cSrcweir 
817*cdf0e10cSrcweir UrlRecord PasswordContainer::find(
818*cdf0e10cSrcweir     const ::rtl::OUString& aURL,
819*cdf0e10cSrcweir     const ::rtl::OUString& aName,
820*cdf0e10cSrcweir     bool bName, // only needed to support empty user names
821*cdf0e10cSrcweir     const Reference< XInteractionHandler >& aHandler  ) throw(RuntimeException)
822*cdf0e10cSrcweir {
823*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
824*cdf0e10cSrcweir 
825*cdf0e10cSrcweir     if( !m_aContainer.empty() && aURL.getLength() )
826*cdf0e10cSrcweir     {
827*cdf0e10cSrcweir         ::rtl::OUString aUrl( aURL );
828*cdf0e10cSrcweir 
829*cdf0e10cSrcweir         // each iteration remove last '/...' section from the aUrl
830*cdf0e10cSrcweir         // while it's possible, up to the most left '://'
831*cdf0e10cSrcweir         do
832*cdf0e10cSrcweir         {
833*cdf0e10cSrcweir             // first look for <url>/somename and then look for <url>/somename/...
834*cdf0e10cSrcweir             PassMap::iterator aIter = m_aContainer.find( aUrl );
835*cdf0e10cSrcweir             if( aIter != m_aContainer.end() )
836*cdf0e10cSrcweir             {
837*cdf0e10cSrcweir                 UrlRecord aRec;
838*cdf0e10cSrcweir                 if ( createUrlRecord( aIter, bName, aName, aHandler, aRec ) )
839*cdf0e10cSrcweir                   return aRec;
840*cdf0e10cSrcweir             }
841*cdf0e10cSrcweir             else
842*cdf0e10cSrcweir             {
843*cdf0e10cSrcweir                 ::rtl::OUString tmpUrl( aUrl );
844*cdf0e10cSrcweir                 if ( tmpUrl.getStr()[tmpUrl.getLength() - 1] != (sal_Unicode)'/' )
845*cdf0e10cSrcweir                     tmpUrl += ::rtl::OUString::createFromAscii( "/" );
846*cdf0e10cSrcweir 
847*cdf0e10cSrcweir                 aIter = m_aContainer.lower_bound( tmpUrl );
848*cdf0e10cSrcweir                 if( aIter != m_aContainer.end() && aIter->first.match( tmpUrl ) )
849*cdf0e10cSrcweir                 {
850*cdf0e10cSrcweir                     UrlRecord aRec;
851*cdf0e10cSrcweir                     if ( createUrlRecord( aIter, bName, aName, aHandler, aRec ) )
852*cdf0e10cSrcweir                       return aRec;
853*cdf0e10cSrcweir                 }
854*cdf0e10cSrcweir             }
855*cdf0e10cSrcweir         }
856*cdf0e10cSrcweir         while( shorterUrl( aUrl ) && aUrl.getLength() );
857*cdf0e10cSrcweir     }
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir     return UrlRecord();
860*cdf0e10cSrcweir }
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir //-------------------------------------------------------------------------
863*cdf0e10cSrcweir ::rtl::OUString PasswordContainer::GetDefaultMasterPassword()
864*cdf0e10cSrcweir {
865*cdf0e10cSrcweir     ::rtl::OUString aResult;
866*cdf0e10cSrcweir     for ( sal_Int32 nInd = 0; nInd < RTL_DIGEST_LENGTH_MD5; nInd++ )
867*cdf0e10cSrcweir         aResult += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "aa" ) );
868*cdf0e10cSrcweir 
869*cdf0e10cSrcweir     return aResult;
870*cdf0e10cSrcweir }
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir //-------------------------------------------------------------------------
873*cdf0e10cSrcweir ::rtl::OUString PasswordContainer::RequestPasswordFromUser( PasswordRequestMode aRMode, const uno::Reference< task::XInteractionHandler >& xHandler )
874*cdf0e10cSrcweir {
875*cdf0e10cSrcweir     // empty string means that the call was cancelled or just failed
876*cdf0e10cSrcweir     ::rtl::OUString aResult;
877*cdf0e10cSrcweir 
878*cdf0e10cSrcweir     if ( xHandler.is() )
879*cdf0e10cSrcweir     {
880*cdf0e10cSrcweir         ::rtl::Reference< MasterPasswordRequest_Impl > xRequest = new MasterPasswordRequest_Impl( aRMode );
881*cdf0e10cSrcweir 
882*cdf0e10cSrcweir         xHandler->handle( xRequest.get() );
883*cdf0e10cSrcweir 
884*cdf0e10cSrcweir         ::rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir         if ( xSelection.is() )
887*cdf0e10cSrcweir         {
888*cdf0e10cSrcweir             Reference< XInteractionAbort > xAbort( xSelection.get(), UNO_QUERY );
889*cdf0e10cSrcweir             if ( !xAbort.is() )
890*cdf0e10cSrcweir             {
891*cdf0e10cSrcweir                 const ::rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp
892*cdf0e10cSrcweir                             = xRequest->getAuthenticationSupplier();
893*cdf0e10cSrcweir 
894*cdf0e10cSrcweir                 aResult = xSupp->getPassword();
895*cdf0e10cSrcweir             }
896*cdf0e10cSrcweir         }
897*cdf0e10cSrcweir     }
898*cdf0e10cSrcweir 
899*cdf0e10cSrcweir     return aResult;
900*cdf0e10cSrcweir }
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir //-------------------------------------------------------------------------
903*cdf0e10cSrcweir 
904*cdf0e10cSrcweir ::rtl::OUString PasswordContainer::GetMasterPassword( const Reference< XInteractionHandler >& aHandler ) throw(RuntimeException)
905*cdf0e10cSrcweir {
906*cdf0e10cSrcweir     PasswordRequestMode aRMode = PasswordRequestMode_PASSWORD_ENTER;
907*cdf0e10cSrcweir     if( !m_pStorageFile || !m_pStorageFile->useStorage() )
908*cdf0e10cSrcweir         throw NoMasterException( ::rtl::OUString::createFromAscii( "Password storing is not active!" ), Reference< XInterface >(), aRMode );
909*cdf0e10cSrcweir 
910*cdf0e10cSrcweir     if( !m_aMasterPasswd.getLength() && aHandler.is() )
911*cdf0e10cSrcweir     {
912*cdf0e10cSrcweir         ::rtl::OUString aEncodedMP;
913*cdf0e10cSrcweir         sal_Bool bAskAgain = sal_False;
914*cdf0e10cSrcweir         sal_Bool bDefaultPassword = sal_False;
915*cdf0e10cSrcweir 
916*cdf0e10cSrcweir         if( !m_pStorageFile->getEncodedMP( aEncodedMP ) )
917*cdf0e10cSrcweir             aRMode = PasswordRequestMode_PASSWORD_CREATE;
918*cdf0e10cSrcweir         else if ( !aEncodedMP.getLength() )
919*cdf0e10cSrcweir         {
920*cdf0e10cSrcweir             m_aMasterPasswd = GetDefaultMasterPassword();
921*cdf0e10cSrcweir             bDefaultPassword = sal_True;
922*cdf0e10cSrcweir         }
923*cdf0e10cSrcweir 
924*cdf0e10cSrcweir         if ( !bDefaultPassword )
925*cdf0e10cSrcweir         {
926*cdf0e10cSrcweir             do {
927*cdf0e10cSrcweir                 bAskAgain = sal_False;
928*cdf0e10cSrcweir 
929*cdf0e10cSrcweir                 ::rtl::OUString aPass = RequestPasswordFromUser( aRMode, aHandler );
930*cdf0e10cSrcweir                 if ( aPass.getLength() )
931*cdf0e10cSrcweir                 {
932*cdf0e10cSrcweir                     if( aRMode == PasswordRequestMode_PASSWORD_CREATE )
933*cdf0e10cSrcweir                     {
934*cdf0e10cSrcweir                         m_aMasterPasswd = aPass;
935*cdf0e10cSrcweir                         vector< ::rtl::OUString > aMaster( 1, m_aMasterPasswd );
936*cdf0e10cSrcweir 
937*cdf0e10cSrcweir                         m_pStorageFile->setEncodedMP( EncodePasswords( aMaster, m_aMasterPasswd ) );
938*cdf0e10cSrcweir                     }
939*cdf0e10cSrcweir                     else
940*cdf0e10cSrcweir                     {
941*cdf0e10cSrcweir                         vector< ::rtl::OUString > aRM( DecodePasswords( aEncodedMP, aPass ) );
942*cdf0e10cSrcweir                         if( !aRM.size() || !aPass.equals( aRM[0] ) )
943*cdf0e10cSrcweir                         {
944*cdf0e10cSrcweir                             bAskAgain = sal_True;
945*cdf0e10cSrcweir                             aRMode = PasswordRequestMode_PASSWORD_REENTER;
946*cdf0e10cSrcweir                         }
947*cdf0e10cSrcweir                         else
948*cdf0e10cSrcweir                             m_aMasterPasswd = aPass;
949*cdf0e10cSrcweir                     }
950*cdf0e10cSrcweir                 }
951*cdf0e10cSrcweir 
952*cdf0e10cSrcweir             } while( bAskAgain );
953*cdf0e10cSrcweir         }
954*cdf0e10cSrcweir     }
955*cdf0e10cSrcweir 
956*cdf0e10cSrcweir     if ( !m_aMasterPasswd.getLength() )
957*cdf0e10cSrcweir         throw NoMasterException( ::rtl::OUString::createFromAscii( "No master password!" ), Reference< XInterface >(), aRMode );
958*cdf0e10cSrcweir 
959*cdf0e10cSrcweir     return m_aMasterPasswd;
960*cdf0e10cSrcweir }
961*cdf0e10cSrcweir 
962*cdf0e10cSrcweir //-------------------------------------------------------------------------
963*cdf0e10cSrcweir 
964*cdf0e10cSrcweir void SAL_CALL PasswordContainer::remove( const ::rtl::OUString& aURL, const ::rtl::OUString& aName ) throw(RuntimeException)
965*cdf0e10cSrcweir {
966*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
967*cdf0e10cSrcweir 
968*cdf0e10cSrcweir     ::rtl::OUString aUrl( aURL );
969*cdf0e10cSrcweir     if( !m_aContainer.empty() )
970*cdf0e10cSrcweir     {
971*cdf0e10cSrcweir         PassMap::iterator aIter = m_aContainer.find( aUrl );
972*cdf0e10cSrcweir 
973*cdf0e10cSrcweir         if( aIter == m_aContainer.end() )
974*cdf0e10cSrcweir         {
975*cdf0e10cSrcweir             sal_Int32 aInd = aUrl.lastIndexOf( sal_Unicode( '/' ) );
976*cdf0e10cSrcweir             if( aInd > 0 && aUrl.getLength()-1 == aInd )
977*cdf0e10cSrcweir                 aUrl = aUrl.copy( 0, aUrl.getLength() - 1 );
978*cdf0e10cSrcweir             else
979*cdf0e10cSrcweir                 aUrl += ::rtl::OUString::createFromAscii( "/" );
980*cdf0e10cSrcweir 
981*cdf0e10cSrcweir             aIter = m_aContainer.find( aUrl );
982*cdf0e10cSrcweir         }
983*cdf0e10cSrcweir 
984*cdf0e10cSrcweir         if( aIter != m_aContainer.end() )
985*cdf0e10cSrcweir         {
986*cdf0e10cSrcweir             for( list< NamePassRecord >::iterator aNPIter = aIter->second.begin(); aNPIter != aIter->second.end(); aNPIter++ )
987*cdf0e10cSrcweir                 if( aNPIter->GetUserName().equals( aName ) )
988*cdf0e10cSrcweir                 {
989*cdf0e10cSrcweir                     if( aNPIter->HasPasswords( PERSISTENT_RECORD ) && m_pStorageFile )
990*cdf0e10cSrcweir                         m_pStorageFile->remove( aURL, aName ); // remove record ( aURL, aName )
991*cdf0e10cSrcweir 
992*cdf0e10cSrcweir                     // the iterator will not be used any more so it can be removed directly
993*cdf0e10cSrcweir                     aIter->second.erase( aNPIter );
994*cdf0e10cSrcweir 
995*cdf0e10cSrcweir                     if( aIter->second.begin() == aIter->second.end() )
996*cdf0e10cSrcweir                         m_aContainer.erase( aIter );
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir                     return;
999*cdf0e10cSrcweir                 }
1000*cdf0e10cSrcweir         }
1001*cdf0e10cSrcweir     }
1002*cdf0e10cSrcweir }
1003*cdf0e10cSrcweir 
1004*cdf0e10cSrcweir //-------------------------------------------------------------------------
1005*cdf0e10cSrcweir 
1006*cdf0e10cSrcweir void SAL_CALL PasswordContainer::removePersistent( const ::rtl::OUString& aURL, const ::rtl::OUString& aName ) throw(RuntimeException)
1007*cdf0e10cSrcweir {
1008*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1009*cdf0e10cSrcweir 
1010*cdf0e10cSrcweir     ::rtl::OUString aUrl( aURL );
1011*cdf0e10cSrcweir     if( !m_aContainer.empty() )
1012*cdf0e10cSrcweir     {
1013*cdf0e10cSrcweir         PassMap::iterator aIter = m_aContainer.find( aUrl );
1014*cdf0e10cSrcweir 
1015*cdf0e10cSrcweir         if( aIter == m_aContainer.end() )
1016*cdf0e10cSrcweir         {
1017*cdf0e10cSrcweir             sal_Int32 aInd = aUrl.lastIndexOf( sal_Unicode( '/' ) );
1018*cdf0e10cSrcweir             if( aInd > 0 && aUrl.getLength()-1 == aInd )
1019*cdf0e10cSrcweir                 aUrl = aUrl.copy( 0, aUrl.getLength() - 1 );
1020*cdf0e10cSrcweir             else
1021*cdf0e10cSrcweir                 aUrl += ::rtl::OUString::createFromAscii( "/" );
1022*cdf0e10cSrcweir 
1023*cdf0e10cSrcweir             aIter = m_aContainer.find( aUrl );
1024*cdf0e10cSrcweir         }
1025*cdf0e10cSrcweir 
1026*cdf0e10cSrcweir         if( aIter != m_aContainer.end() )
1027*cdf0e10cSrcweir         {
1028*cdf0e10cSrcweir             for( list< NamePassRecord >::iterator aNPIter = aIter->second.begin(); aNPIter != aIter->second.end(); aNPIter++ )
1029*cdf0e10cSrcweir                 if( aNPIter->GetUserName().equals( aName ) )
1030*cdf0e10cSrcweir                 {
1031*cdf0e10cSrcweir                     if( aNPIter->HasPasswords( PERSISTENT_RECORD ) )
1032*cdf0e10cSrcweir                     {
1033*cdf0e10cSrcweir                         // TODO/LATER: should the password be converted to MemoryPassword?
1034*cdf0e10cSrcweir                         aNPIter->RemovePasswords( PERSISTENT_RECORD );
1035*cdf0e10cSrcweir 
1036*cdf0e10cSrcweir                         if ( m_pStorageFile )
1037*cdf0e10cSrcweir                             m_pStorageFile->remove( aURL, aName ); // remove record ( aURL, aName )
1038*cdf0e10cSrcweir                     }
1039*cdf0e10cSrcweir 
1040*cdf0e10cSrcweir                     if( !aNPIter->HasPasswords( MEMORY_RECORD ) )
1041*cdf0e10cSrcweir                         aIter->second.erase( aNPIter );
1042*cdf0e10cSrcweir 
1043*cdf0e10cSrcweir                     if( aIter->second.begin() == aIter->second.end() )
1044*cdf0e10cSrcweir                         m_aContainer.erase( aIter );
1045*cdf0e10cSrcweir 
1046*cdf0e10cSrcweir                     return;
1047*cdf0e10cSrcweir                 }
1048*cdf0e10cSrcweir         }
1049*cdf0e10cSrcweir     }
1050*cdf0e10cSrcweir }
1051*cdf0e10cSrcweir //-------------------------------------------------------------------------
1052*cdf0e10cSrcweir 
1053*cdf0e10cSrcweir void SAL_CALL PasswordContainer::removeAllPersistent() throw(RuntimeException)
1054*cdf0e10cSrcweir {
1055*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1056*cdf0e10cSrcweir 
1057*cdf0e10cSrcweir     if( m_pStorageFile )
1058*cdf0e10cSrcweir         m_pStorageFile->clear();
1059*cdf0e10cSrcweir 
1060*cdf0e10cSrcweir     for( PassMap::iterator aIter = m_aContainer.begin(); aIter != m_aContainer.end(); )
1061*cdf0e10cSrcweir     {
1062*cdf0e10cSrcweir         for( list< NamePassRecord >::iterator aNPIter = aIter->second.begin(); aNPIter != aIter->second.end(); )
1063*cdf0e10cSrcweir         {
1064*cdf0e10cSrcweir             if( aNPIter->HasPasswords( PERSISTENT_RECORD ) )
1065*cdf0e10cSrcweir             {
1066*cdf0e10cSrcweir                 // TODO/LATER: should the password be converted to MemoryPassword?
1067*cdf0e10cSrcweir                 aNPIter->RemovePasswords( PERSISTENT_RECORD );
1068*cdf0e10cSrcweir 
1069*cdf0e10cSrcweir                 if ( m_pStorageFile )
1070*cdf0e10cSrcweir                     m_pStorageFile->remove( aIter->first, aNPIter->GetUserName() ); // remove record ( aURL, aName )
1071*cdf0e10cSrcweir             }
1072*cdf0e10cSrcweir 
1073*cdf0e10cSrcweir             if( !aNPIter->HasPasswords( MEMORY_RECORD ) )
1074*cdf0e10cSrcweir             {
1075*cdf0e10cSrcweir                 list< NamePassRecord >::iterator aIterToDelete( aNPIter );
1076*cdf0e10cSrcweir                 aNPIter++;
1077*cdf0e10cSrcweir                 aIter->second.erase( aIterToDelete );
1078*cdf0e10cSrcweir             }
1079*cdf0e10cSrcweir             else
1080*cdf0e10cSrcweir                 aNPIter++;
1081*cdf0e10cSrcweir         }
1082*cdf0e10cSrcweir 
1083*cdf0e10cSrcweir         if( aIter->second.begin() == aIter->second.end() )
1084*cdf0e10cSrcweir         {
1085*cdf0e10cSrcweir             PassMap::iterator aIterToDelete( aIter );
1086*cdf0e10cSrcweir             aIter++;
1087*cdf0e10cSrcweir             m_aContainer.erase( aIterToDelete );
1088*cdf0e10cSrcweir         }
1089*cdf0e10cSrcweir         else
1090*cdf0e10cSrcweir             aIter++;
1091*cdf0e10cSrcweir     }
1092*cdf0e10cSrcweir }
1093*cdf0e10cSrcweir //-------------------------------------------------------------------------
1094*cdf0e10cSrcweir 
1095*cdf0e10cSrcweir Sequence< UrlRecord > SAL_CALL PasswordContainer::getAllPersistent( const Reference< XInteractionHandler >& xHandler ) throw(RuntimeException)
1096*cdf0e10cSrcweir {
1097*cdf0e10cSrcweir     Sequence< UrlRecord > aResult;
1098*cdf0e10cSrcweir 
1099*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1100*cdf0e10cSrcweir     for( PassMap::iterator aIter = m_aContainer.begin(); aIter != m_aContainer.end(); aIter++ )
1101*cdf0e10cSrcweir     {
1102*cdf0e10cSrcweir         Sequence< UserRecord > aUsers;
1103*cdf0e10cSrcweir         for( list< NamePassRecord >::iterator aNPIter = aIter->second.begin(); aNPIter != aIter->second.end(); aNPIter++ )
1104*cdf0e10cSrcweir             if( aNPIter->HasPasswords( PERSISTENT_RECORD ) )
1105*cdf0e10cSrcweir             {
1106*cdf0e10cSrcweir                 sal_Int32 oldLen = aUsers.getLength();
1107*cdf0e10cSrcweir                 aUsers.realloc( oldLen + 1 );
1108*cdf0e10cSrcweir                 aUsers[ oldLen ] = UserRecord( aNPIter->GetUserName(), copyVectorToSequence( DecodePasswords( aNPIter->GetPersPasswords(), GetMasterPassword( xHandler ) ) ) );
1109*cdf0e10cSrcweir             }
1110*cdf0e10cSrcweir 
1111*cdf0e10cSrcweir         if( aUsers.getLength() )
1112*cdf0e10cSrcweir         {
1113*cdf0e10cSrcweir             sal_Int32 oldLen = aResult.getLength();
1114*cdf0e10cSrcweir             aResult.realloc( oldLen + 1 );
1115*cdf0e10cSrcweir             aResult[ oldLen ] = UrlRecord( aIter->first, aUsers );
1116*cdf0e10cSrcweir         }
1117*cdf0e10cSrcweir     }
1118*cdf0e10cSrcweir 
1119*cdf0e10cSrcweir     return aResult;
1120*cdf0e10cSrcweir }
1121*cdf0e10cSrcweir 
1122*cdf0e10cSrcweir //-------------------------------------------------------------------------
1123*cdf0e10cSrcweir sal_Bool SAL_CALL PasswordContainer::authorizateWithMasterPassword( const uno::Reference< task::XInteractionHandler >& xHandler )
1124*cdf0e10cSrcweir     throw (uno::RuntimeException)
1125*cdf0e10cSrcweir {
1126*cdf0e10cSrcweir     sal_Bool bResult = sal_False;
1127*cdf0e10cSrcweir     ::rtl::OUString aEncodedMP;
1128*cdf0e10cSrcweir     uno::Reference< task::XInteractionHandler > xTmpHandler = xHandler;
1129*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1130*cdf0e10cSrcweir 
1131*cdf0e10cSrcweir     // the method should fail if there is no master password
1132*cdf0e10cSrcweir     if( m_pStorageFile && m_pStorageFile->useStorage() && m_pStorageFile->getEncodedMP( aEncodedMP ) )
1133*cdf0e10cSrcweir     {
1134*cdf0e10cSrcweir         if ( !aEncodedMP.getLength() )
1135*cdf0e10cSrcweir         {
1136*cdf0e10cSrcweir             // this is a default master password
1137*cdf0e10cSrcweir             // no UI is necessary
1138*cdf0e10cSrcweir             bResult = sal_True;
1139*cdf0e10cSrcweir         }
1140*cdf0e10cSrcweir         else
1141*cdf0e10cSrcweir         {
1142*cdf0e10cSrcweir             if ( !xTmpHandler.is() )
1143*cdf0e10cSrcweir             {
1144*cdf0e10cSrcweir                 uno::Reference< lang::XMultiServiceFactory > xFactory( mComponent, uno::UNO_QUERY_THROW );
1145*cdf0e10cSrcweir                 xTmpHandler.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.InteractionHandler" ) ) ), uno::UNO_QUERY_THROW );
1146*cdf0e10cSrcweir             }
1147*cdf0e10cSrcweir 
1148*cdf0e10cSrcweir             if ( m_aMasterPasswd.getLength() )
1149*cdf0e10cSrcweir             {
1150*cdf0e10cSrcweir                 // there is a password, it should be just rechecked
1151*cdf0e10cSrcweir                 PasswordRequestMode aRMode = PasswordRequestMode_PASSWORD_ENTER;
1152*cdf0e10cSrcweir                 ::rtl::OUString aPass;
1153*cdf0e10cSrcweir 
1154*cdf0e10cSrcweir                 do {
1155*cdf0e10cSrcweir                     aPass = RequestPasswordFromUser( aRMode, xTmpHandler );
1156*cdf0e10cSrcweir                     bResult = ( aPass.getLength() && aPass.equals( m_aMasterPasswd ) );
1157*cdf0e10cSrcweir                     aRMode = PasswordRequestMode_PASSWORD_REENTER; // further questions with error notification
1158*cdf0e10cSrcweir                 } while( !bResult && aPass.getLength() );
1159*cdf0e10cSrcweir             }
1160*cdf0e10cSrcweir             else
1161*cdf0e10cSrcweir             {
1162*cdf0e10cSrcweir                 try
1163*cdf0e10cSrcweir                 {
1164*cdf0e10cSrcweir                     // ask for the password, if user provide no correct password an exception will be thrown
1165*cdf0e10cSrcweir                     bResult = ( GetMasterPassword( xTmpHandler ).getLength() > 0 );
1166*cdf0e10cSrcweir                 }
1167*cdf0e10cSrcweir                 catch( uno::Exception& )
1168*cdf0e10cSrcweir                 {}
1169*cdf0e10cSrcweir             }
1170*cdf0e10cSrcweir         }
1171*cdf0e10cSrcweir     }
1172*cdf0e10cSrcweir 
1173*cdf0e10cSrcweir     return bResult;
1174*cdf0e10cSrcweir }
1175*cdf0e10cSrcweir 
1176*cdf0e10cSrcweir //-------------------------------------------------------------------------
1177*cdf0e10cSrcweir sal_Bool SAL_CALL PasswordContainer::changeMasterPassword( const uno::Reference< task::XInteractionHandler >& xHandler )
1178*cdf0e10cSrcweir     throw (uno::RuntimeException)
1179*cdf0e10cSrcweir {
1180*cdf0e10cSrcweir     sal_Bool bResult = sal_False;
1181*cdf0e10cSrcweir     uno::Reference< task::XInteractionHandler > xTmpHandler = xHandler;
1182*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1183*cdf0e10cSrcweir 
1184*cdf0e10cSrcweir     if ( m_pStorageFile && m_pStorageFile->useStorage() )
1185*cdf0e10cSrcweir     {
1186*cdf0e10cSrcweir         if ( !xTmpHandler.is() )
1187*cdf0e10cSrcweir         {
1188*cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xFactory( mComponent, uno::UNO_QUERY_THROW );
1189*cdf0e10cSrcweir             xTmpHandler.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.InteractionHandler" ) ) ), uno::UNO_QUERY_THROW );
1190*cdf0e10cSrcweir         }
1191*cdf0e10cSrcweir 
1192*cdf0e10cSrcweir         sal_Bool bCanChangePassword = sal_True;
1193*cdf0e10cSrcweir         // if there is already a stored master password it should be entered by the user before the change happen
1194*cdf0e10cSrcweir         ::rtl::OUString aEncodedMP;
1195*cdf0e10cSrcweir         if( m_aMasterPasswd.getLength() || m_pStorageFile->getEncodedMP( aEncodedMP ) )
1196*cdf0e10cSrcweir             bCanChangePassword = authorizateWithMasterPassword( xTmpHandler );
1197*cdf0e10cSrcweir 
1198*cdf0e10cSrcweir         if ( bCanChangePassword )
1199*cdf0e10cSrcweir         {
1200*cdf0e10cSrcweir             // ask for the new password, but do not set it
1201*cdf0e10cSrcweir             PasswordRequestMode aRMode = PasswordRequestMode_PASSWORD_CREATE;
1202*cdf0e10cSrcweir             ::rtl::OUString aPass = RequestPasswordFromUser( aRMode, xTmpHandler );
1203*cdf0e10cSrcweir 
1204*cdf0e10cSrcweir             if ( aPass.getLength() )
1205*cdf0e10cSrcweir             {
1206*cdf0e10cSrcweir                 // get all the persistent entries if it is possible
1207*cdf0e10cSrcweir                 Sequence< UrlRecord > aPersistent = getAllPersistent( uno::Reference< task::XInteractionHandler >() );
1208*cdf0e10cSrcweir 
1209*cdf0e10cSrcweir                 // remove the master password and the entries persistence
1210*cdf0e10cSrcweir                 removeMasterPassword();
1211*cdf0e10cSrcweir 
1212*cdf0e10cSrcweir                 // store the new master password
1213*cdf0e10cSrcweir                 m_aMasterPasswd = aPass;
1214*cdf0e10cSrcweir                 vector< ::rtl::OUString > aMaster( 1, m_aMasterPasswd );
1215*cdf0e10cSrcweir                 m_pStorageFile->setEncodedMP( EncodePasswords( aMaster, m_aMasterPasswd ) );
1216*cdf0e10cSrcweir 
1217*cdf0e10cSrcweir                 // store all the entries with the new password
1218*cdf0e10cSrcweir                 for ( int nURLInd = 0; nURLInd < aPersistent.getLength(); nURLInd++ )
1219*cdf0e10cSrcweir                     for ( int nNameInd = 0; nNameInd< aPersistent[nURLInd].UserList.getLength(); nNameInd++ )
1220*cdf0e10cSrcweir                         addPersistent( aPersistent[nURLInd].Url,
1221*cdf0e10cSrcweir                                        aPersistent[nURLInd].UserList[nNameInd].UserName,
1222*cdf0e10cSrcweir                                        aPersistent[nURLInd].UserList[nNameInd].Passwords,
1223*cdf0e10cSrcweir                                        uno::Reference< task::XInteractionHandler >() );
1224*cdf0e10cSrcweir 
1225*cdf0e10cSrcweir                 bResult = sal_True;
1226*cdf0e10cSrcweir             }
1227*cdf0e10cSrcweir         }
1228*cdf0e10cSrcweir     }
1229*cdf0e10cSrcweir 
1230*cdf0e10cSrcweir     return bResult;
1231*cdf0e10cSrcweir }
1232*cdf0e10cSrcweir 
1233*cdf0e10cSrcweir //-------------------------------------------------------------------------
1234*cdf0e10cSrcweir void SAL_CALL PasswordContainer::removeMasterPassword()
1235*cdf0e10cSrcweir     throw (uno::RuntimeException)
1236*cdf0e10cSrcweir {
1237*cdf0e10cSrcweir     // remove all the stored passwords and the master password
1238*cdf0e10cSrcweir     removeAllPersistent();
1239*cdf0e10cSrcweir 
1240*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1241*cdf0e10cSrcweir     if ( m_pStorageFile )
1242*cdf0e10cSrcweir     {
1243*cdf0e10cSrcweir         m_aMasterPasswd = ::rtl::OUString();
1244*cdf0e10cSrcweir         m_pStorageFile->setEncodedMP( ::rtl::OUString() ); // let the master password be removed from configuration
1245*cdf0e10cSrcweir     }
1246*cdf0e10cSrcweir }
1247*cdf0e10cSrcweir 
1248*cdf0e10cSrcweir //-------------------------------------------------------------------------
1249*cdf0e10cSrcweir ::sal_Bool SAL_CALL PasswordContainer::hasMasterPassword(  )
1250*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
1251*cdf0e10cSrcweir {
1252*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1253*cdf0e10cSrcweir 
1254*cdf0e10cSrcweir     if ( !m_pStorageFile )
1255*cdf0e10cSrcweir         throw uno::RuntimeException();
1256*cdf0e10cSrcweir 
1257*cdf0e10cSrcweir     ::rtl::OUString aEncodedMP;
1258*cdf0e10cSrcweir     return ( m_pStorageFile->useStorage() && m_pStorageFile->getEncodedMP( aEncodedMP ) );
1259*cdf0e10cSrcweir }
1260*cdf0e10cSrcweir 
1261*cdf0e10cSrcweir //-------------------------------------------------------------------------
1262*cdf0e10cSrcweir ::sal_Bool SAL_CALL PasswordContainer::allowPersistentStoring( ::sal_Bool bAllow )
1263*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
1264*cdf0e10cSrcweir {
1265*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1266*cdf0e10cSrcweir 
1267*cdf0e10cSrcweir     if ( !m_pStorageFile )
1268*cdf0e10cSrcweir         throw uno::RuntimeException();
1269*cdf0e10cSrcweir 
1270*cdf0e10cSrcweir     if ( !bAllow )
1271*cdf0e10cSrcweir         removeMasterPassword();
1272*cdf0e10cSrcweir 
1273*cdf0e10cSrcweir     if ( m_pStorageFile->useStorage() == bAllow )
1274*cdf0e10cSrcweir         return bAllow;
1275*cdf0e10cSrcweir 
1276*cdf0e10cSrcweir     m_pStorageFile->setUseStorage( bAllow );
1277*cdf0e10cSrcweir     return !bAllow;
1278*cdf0e10cSrcweir }
1279*cdf0e10cSrcweir 
1280*cdf0e10cSrcweir //-------------------------------------------------------------------------
1281*cdf0e10cSrcweir ::sal_Bool SAL_CALL PasswordContainer::isPersistentStoringAllowed()
1282*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
1283*cdf0e10cSrcweir {
1284*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1285*cdf0e10cSrcweir 
1286*cdf0e10cSrcweir     if ( !m_pStorageFile )
1287*cdf0e10cSrcweir         throw uno::RuntimeException();
1288*cdf0e10cSrcweir 
1289*cdf0e10cSrcweir     return m_pStorageFile->useStorage();
1290*cdf0e10cSrcweir }
1291*cdf0e10cSrcweir 
1292*cdf0e10cSrcweir //-------------------------------------------------------------------------
1293*cdf0e10cSrcweir ::sal_Bool SAL_CALL PasswordContainer::useDefaultMasterPassword( const uno::Reference< task::XInteractionHandler >& xHandler )
1294*cdf0e10cSrcweir     throw ( uno::RuntimeException )
1295*cdf0e10cSrcweir {
1296*cdf0e10cSrcweir     sal_Bool bResult = sal_False;
1297*cdf0e10cSrcweir     uno::Reference< task::XInteractionHandler > xTmpHandler = xHandler;
1298*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1299*cdf0e10cSrcweir 
1300*cdf0e10cSrcweir     if ( m_pStorageFile && m_pStorageFile->useStorage() )
1301*cdf0e10cSrcweir     {
1302*cdf0e10cSrcweir         if ( !xTmpHandler.is() )
1303*cdf0e10cSrcweir         {
1304*cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xFactory( mComponent, uno::UNO_QUERY_THROW );
1305*cdf0e10cSrcweir             xTmpHandler.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.InteractionHandler" ) ) ), uno::UNO_QUERY_THROW );
1306*cdf0e10cSrcweir         }
1307*cdf0e10cSrcweir 
1308*cdf0e10cSrcweir         sal_Bool bCanChangePassword = sal_True;
1309*cdf0e10cSrcweir         // if there is already a stored nondefault master password it should be entered by the user before the change happen
1310*cdf0e10cSrcweir         ::rtl::OUString aEncodedMP;
1311*cdf0e10cSrcweir         if( m_pStorageFile->getEncodedMP( aEncodedMP ) && aEncodedMP.getLength() )
1312*cdf0e10cSrcweir             bCanChangePassword = authorizateWithMasterPassword( xTmpHandler );
1313*cdf0e10cSrcweir 
1314*cdf0e10cSrcweir         if ( bCanChangePassword )
1315*cdf0e10cSrcweir         {
1316*cdf0e10cSrcweir             // generate the default password
1317*cdf0e10cSrcweir             ::rtl::OUString aPass = GetDefaultMasterPassword();
1318*cdf0e10cSrcweir             if ( aPass.getLength() )
1319*cdf0e10cSrcweir             {
1320*cdf0e10cSrcweir                 // get all the persistent entries if it is possible
1321*cdf0e10cSrcweir                 Sequence< UrlRecord > aPersistent = getAllPersistent( uno::Reference< task::XInteractionHandler >() );
1322*cdf0e10cSrcweir 
1323*cdf0e10cSrcweir                 // remove the master password and the entries persistence
1324*cdf0e10cSrcweir                 removeMasterPassword();
1325*cdf0e10cSrcweir 
1326*cdf0e10cSrcweir                 // store the empty string to flag the default master password
1327*cdf0e10cSrcweir                 m_aMasterPasswd = aPass;
1328*cdf0e10cSrcweir                 m_pStorageFile->setEncodedMP( ::rtl::OUString(), sal_True );
1329*cdf0e10cSrcweir 
1330*cdf0e10cSrcweir                 // store all the entries with the new password
1331*cdf0e10cSrcweir                 for ( int nURLInd = 0; nURLInd < aPersistent.getLength(); nURLInd++ )
1332*cdf0e10cSrcweir                     for ( int nNameInd = 0; nNameInd< aPersistent[nURLInd].UserList.getLength(); nNameInd++ )
1333*cdf0e10cSrcweir                         addPersistent( aPersistent[nURLInd].Url,
1334*cdf0e10cSrcweir                                        aPersistent[nURLInd].UserList[nNameInd].UserName,
1335*cdf0e10cSrcweir                                        aPersistent[nURLInd].UserList[nNameInd].Passwords,
1336*cdf0e10cSrcweir                                        uno::Reference< task::XInteractionHandler >() );
1337*cdf0e10cSrcweir 
1338*cdf0e10cSrcweir                 bResult = sal_True;
1339*cdf0e10cSrcweir             }
1340*cdf0e10cSrcweir         }
1341*cdf0e10cSrcweir     }
1342*cdf0e10cSrcweir 
1343*cdf0e10cSrcweir     return bResult;
1344*cdf0e10cSrcweir 
1345*cdf0e10cSrcweir }
1346*cdf0e10cSrcweir 
1347*cdf0e10cSrcweir //-------------------------------------------------------------------------
1348*cdf0e10cSrcweir ::sal_Bool SAL_CALL PasswordContainer::isDefaultMasterPasswordUsed()
1349*cdf0e10cSrcweir     throw ( uno::RuntimeException )
1350*cdf0e10cSrcweir {
1351*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1352*cdf0e10cSrcweir 
1353*cdf0e10cSrcweir     if ( !m_pStorageFile )
1354*cdf0e10cSrcweir         throw uno::RuntimeException();
1355*cdf0e10cSrcweir 
1356*cdf0e10cSrcweir     ::rtl::OUString aEncodedMP;
1357*cdf0e10cSrcweir     return ( m_pStorageFile->useStorage() && m_pStorageFile->getEncodedMP( aEncodedMP ) && !aEncodedMP.getLength() );
1358*cdf0e10cSrcweir }
1359*cdf0e10cSrcweir 
1360*cdf0e10cSrcweir 
1361*cdf0e10cSrcweir //-------------------------------------------------------------------------
1362*cdf0e10cSrcweir void SAL_CALL PasswordContainer::addUrl( const ::rtl::OUString& Url, ::sal_Bool MakePersistent )
1363*cdf0e10cSrcweir     throw (uno::RuntimeException)
1364*cdf0e10cSrcweir {
1365*cdf0e10cSrcweir     mUrlContainer.add( Url, MakePersistent );
1366*cdf0e10cSrcweir }
1367*cdf0e10cSrcweir 
1368*cdf0e10cSrcweir //-------------------------------------------------------------------------
1369*cdf0e10cSrcweir ::rtl::OUString SAL_CALL PasswordContainer::findUrl( const ::rtl::OUString& Url )
1370*cdf0e10cSrcweir     throw (uno::RuntimeException)
1371*cdf0e10cSrcweir {
1372*cdf0e10cSrcweir     return mUrlContainer.find( Url );
1373*cdf0e10cSrcweir }
1374*cdf0e10cSrcweir 
1375*cdf0e10cSrcweir //-------------------------------------------------------------------------
1376*cdf0e10cSrcweir void SAL_CALL PasswordContainer::removeUrl( const ::rtl::OUString& Url )
1377*cdf0e10cSrcweir     throw (uno::RuntimeException)
1378*cdf0e10cSrcweir {
1379*cdf0e10cSrcweir     mUrlContainer.remove( Url );
1380*cdf0e10cSrcweir }
1381*cdf0e10cSrcweir 
1382*cdf0e10cSrcweir //-------------------------------------------------------------------------
1383*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL PasswordContainer::getUrls( ::sal_Bool OnlyPersistent )
1384*cdf0e10cSrcweir     throw (uno::RuntimeException)
1385*cdf0e10cSrcweir {
1386*cdf0e10cSrcweir     return mUrlContainer.list( OnlyPersistent );
1387*cdf0e10cSrcweir }
1388*cdf0e10cSrcweir 
1389*cdf0e10cSrcweir //-------------------------------------------------------------------------
1390*cdf0e10cSrcweir 
1391*cdf0e10cSrcweir void PasswordContainer::Notify()
1392*cdf0e10cSrcweir {
1393*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( mMutex );
1394*cdf0e10cSrcweir 
1395*cdf0e10cSrcweir     PassMap::iterator aIter;
1396*cdf0e10cSrcweir 
1397*cdf0e10cSrcweir     // remove the cached persistent values in the memory
1398*cdf0e10cSrcweir     for( aIter = m_aContainer.begin(); aIter != m_aContainer.end(); aIter++ )
1399*cdf0e10cSrcweir     {
1400*cdf0e10cSrcweir         for( list< NamePassRecord >::iterator aNPIter = aIter->second.begin(); aNPIter != aIter->second.end(); )
1401*cdf0e10cSrcweir         {
1402*cdf0e10cSrcweir             if( aNPIter->HasPasswords( PERSISTENT_RECORD ) )
1403*cdf0e10cSrcweir             {
1404*cdf0e10cSrcweir                 aNPIter->RemovePasswords( PERSISTENT_RECORD );
1405*cdf0e10cSrcweir 
1406*cdf0e10cSrcweir                 if ( m_pStorageFile )
1407*cdf0e10cSrcweir                     m_pStorageFile->remove( aIter->first, aNPIter->GetUserName() ); // remove record ( aURL, aName )
1408*cdf0e10cSrcweir             }
1409*cdf0e10cSrcweir 
1410*cdf0e10cSrcweir             if( !aNPIter->HasPasswords( MEMORY_RECORD ) )
1411*cdf0e10cSrcweir             {
1412*cdf0e10cSrcweir                 list< NamePassRecord >::iterator aIterToDelete( aNPIter );
1413*cdf0e10cSrcweir                 aNPIter++;
1414*cdf0e10cSrcweir                 aIter->second.erase( aIterToDelete );
1415*cdf0e10cSrcweir             }
1416*cdf0e10cSrcweir             else
1417*cdf0e10cSrcweir                 aNPIter++;
1418*cdf0e10cSrcweir         }
1419*cdf0e10cSrcweir     }
1420*cdf0e10cSrcweir 
1421*cdf0e10cSrcweir     PassMap addon;
1422*cdf0e10cSrcweir     if( m_pStorageFile )
1423*cdf0e10cSrcweir         addon = m_pStorageFile->getInfo();
1424*cdf0e10cSrcweir 
1425*cdf0e10cSrcweir     for( aIter = addon.begin(); aIter != addon.end(); aIter++ )
1426*cdf0e10cSrcweir     {
1427*cdf0e10cSrcweir         PassMap::iterator aSearchIter = m_aContainer.find( aIter->first );
1428*cdf0e10cSrcweir         if( aSearchIter != m_aContainer.end() )
1429*cdf0e10cSrcweir             for( list< NamePassRecord >::iterator aNPIter = aIter->second.begin(); aNPIter != aIter->second.end(); aNPIter++ )
1430*cdf0e10cSrcweir                 UpdateVector( aSearchIter->first, aSearchIter->second, *aNPIter, sal_False );
1431*cdf0e10cSrcweir         else
1432*cdf0e10cSrcweir             m_aContainer.insert( PairUrlRecord( aIter->first, aIter->second ) );
1433*cdf0e10cSrcweir     }
1434*cdf0e10cSrcweir }
1435*cdf0e10cSrcweir 
1436*cdf0e10cSrcweir //-------------------------------------------------------------------------
1437*cdf0e10cSrcweir 
1438*cdf0e10cSrcweir ::rtl::OUString SAL_CALL PasswordContainer::getImplementationName(  ) throw(uno::RuntimeException)
1439*cdf0e10cSrcweir {
1440*cdf0e10cSrcweir     return impl_getStaticImplementationName();
1441*cdf0e10cSrcweir }
1442*cdf0e10cSrcweir 
1443*cdf0e10cSrcweir //-------------------------------------------------------------------------
1444*cdf0e10cSrcweir 
1445*cdf0e10cSrcweir sal_Bool SAL_CALL PasswordContainer::supportsService( const ::rtl::OUString& ServiceName ) throw(uno::RuntimeException)
1446*cdf0e10cSrcweir {
1447*cdf0e10cSrcweir     if ( ServiceName.compareToAscii("com.sun.star.task.PasswordContainer") == 0 )
1448*cdf0e10cSrcweir         return sal_True;
1449*cdf0e10cSrcweir     else
1450*cdf0e10cSrcweir         return sal_False;
1451*cdf0e10cSrcweir }
1452*cdf0e10cSrcweir 
1453*cdf0e10cSrcweir //-------------------------------------------------------------------------
1454*cdf0e10cSrcweir 
1455*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL PasswordContainer::getSupportedServiceNames(  ) throw(uno::RuntimeException)
1456*cdf0e10cSrcweir {
1457*cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
1458*cdf0e10cSrcweir }
1459*cdf0e10cSrcweir 
1460*cdf0e10cSrcweir //-------------------------------------------------------------------------
1461*cdf0e10cSrcweir 
1462*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL PasswordContainer::impl_getStaticSupportedServiceNames(  ) throw(uno::RuntimeException)
1463*cdf0e10cSrcweir {
1464*cdf0e10cSrcweir     Sequence< ::rtl::OUString > aRet(1);
1465*cdf0e10cSrcweir     *aRet.getArray() = ::rtl::OUString::createFromAscii("com.sun.star.task.PasswordContainer");
1466*cdf0e10cSrcweir     return aRet;
1467*cdf0e10cSrcweir }
1468*cdf0e10cSrcweir 
1469*cdf0e10cSrcweir //-------------------------------------------------------------------------
1470*cdf0e10cSrcweir 
1471*cdf0e10cSrcweir ::rtl::OUString SAL_CALL PasswordContainer::impl_getStaticImplementationName() throw(uno::RuntimeException)
1472*cdf0e10cSrcweir {
1473*cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii("stardiv.svl.PasswordContainer");
1474*cdf0e10cSrcweir }
1475*cdf0e10cSrcweir 
1476*cdf0e10cSrcweir //-------------------------------------------------------------------------
1477*cdf0e10cSrcweir 
1478*cdf0e10cSrcweir Reference< XInterface > SAL_CALL PasswordContainer::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( RuntimeException )
1479*cdf0e10cSrcweir {
1480*cdf0e10cSrcweir     return Reference< XInterface >( *new PasswordContainer( xServiceManager ) );
1481*cdf0e10cSrcweir }
1482*cdf0e10cSrcweir 
1483*cdf0e10cSrcweir //-------------------------------------------------------------------------
1484*cdf0e10cSrcweir 
1485*cdf0e10cSrcweir Reference< XSingleServiceFactory > SAL_CALL PasswordContainer::impl_createFactory( const Reference< XMultiServiceFactory >& ServiceManager ) throw(RuntimeException)
1486*cdf0e10cSrcweir {
1487*cdf0e10cSrcweir     Reference< XSingleServiceFactory > xReturn( ::cppu::createOneInstanceFactory( ServiceManager,
1488*cdf0e10cSrcweir                                                         PasswordContainer::impl_getStaticImplementationName(),
1489*cdf0e10cSrcweir                                                         PasswordContainer::impl_createInstance,
1490*cdf0e10cSrcweir                                                         PasswordContainer::impl_getStaticSupportedServiceNames()));
1491*cdf0e10cSrcweir     return xReturn ;
1492*cdf0e10cSrcweir 
1493*cdf0e10cSrcweir }
1494*cdf0e10cSrcweir 
1495*cdf0e10cSrcweir //-------------------------------------------------------------------------
1496*cdf0e10cSrcweir //-------------------------------------------------------------------------
1497*cdf0e10cSrcweir 
1498*cdf0e10cSrcweir MasterPasswordRequest_Impl::MasterPasswordRequest_Impl( PasswordRequestMode Mode )
1499*cdf0e10cSrcweir {
1500*cdf0e10cSrcweir     MasterPasswordRequest aRequest;
1501*cdf0e10cSrcweir 
1502*cdf0e10cSrcweir     aRequest.Classification = InteractionClassification_ERROR;
1503*cdf0e10cSrcweir     aRequest.Mode = Mode;
1504*cdf0e10cSrcweir 
1505*cdf0e10cSrcweir     setRequest( makeAny( aRequest ) );
1506*cdf0e10cSrcweir 
1507*cdf0e10cSrcweir     // Fill continuations...
1508*cdf0e10cSrcweir     Sequence< RememberAuthentication > aRememberModes( 1 );
1509*cdf0e10cSrcweir     aRememberModes[ 0 ] = RememberAuthentication_NO;
1510*cdf0e10cSrcweir 
1511*cdf0e10cSrcweir     m_xAuthSupplier
1512*cdf0e10cSrcweir         = new ::ucbhelper::InteractionSupplyAuthentication(
1513*cdf0e10cSrcweir                 this,
1514*cdf0e10cSrcweir                 sal_False, // bCanSetRealm
1515*cdf0e10cSrcweir                 sal_False,  // bCanSetUserName
1516*cdf0e10cSrcweir                 sal_True,  // bCanSetPassword
1517*cdf0e10cSrcweir                 sal_False, // bCanSetAccount
1518*cdf0e10cSrcweir                 aRememberModes, // rRememberPasswordModes
1519*cdf0e10cSrcweir                 RememberAuthentication_NO, // eDefaultRememberPasswordMode
1520*cdf0e10cSrcweir                 aRememberModes, // rRememberAccountModes
1521*cdf0e10cSrcweir                 RememberAuthentication_NO, // eDefaultRememberAccountMode
1522*cdf0e10cSrcweir                 sal_False, // bCanUseSystemCredentials
1523*cdf0e10cSrcweir                 sal_False  // bDefaultUseSystemCredentials
1524*cdf0e10cSrcweir             );
1525*cdf0e10cSrcweir 
1526*cdf0e10cSrcweir     Sequence<
1527*cdf0e10cSrcweir         Reference< XInteractionContinuation > > aContinuations( 3 );
1528*cdf0e10cSrcweir     aContinuations[ 0 ] = new ::ucbhelper::InteractionAbort( this );
1529*cdf0e10cSrcweir     aContinuations[ 1 ] = new ::ucbhelper::InteractionRetry( this );
1530*cdf0e10cSrcweir     aContinuations[ 2 ] = m_xAuthSupplier.get();
1531*cdf0e10cSrcweir 
1532*cdf0e10cSrcweir     setContinuations( aContinuations );
1533*cdf0e10cSrcweir }
1534*cdf0e10cSrcweir 
1535*cdf0e10cSrcweir //-------------------------------------------------------------------------
1536*cdf0e10cSrcweir //-------------------------------------------------------------------------
1537*cdf0e10cSrcweir 
1538*cdf0e10cSrcweir extern "C"
1539*cdf0e10cSrcweir {
1540*cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment (
1541*cdf0e10cSrcweir     const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */)
1542*cdf0e10cSrcweir {
1543*cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
1544*cdf0e10cSrcweir }
1545*cdf0e10cSrcweir 
1546*cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory (
1547*cdf0e10cSrcweir     const sal_Char * pImplementationName, void * pServiceManager, void * /* pRegistryKey */)
1548*cdf0e10cSrcweir {
1549*cdf0e10cSrcweir     void * pResult = 0;
1550*cdf0e10cSrcweir     if (pServiceManager)
1551*cdf0e10cSrcweir     {
1552*cdf0e10cSrcweir         Reference< XSingleServiceFactory > xFactory;
1553*cdf0e10cSrcweir         if (PasswordContainer::impl_getStaticImplementationName().compareToAscii (pImplementationName) == 0)
1554*cdf0e10cSrcweir         {
1555*cdf0e10cSrcweir             xFactory = PasswordContainer::impl_createFactory (
1556*cdf0e10cSrcweir                 reinterpret_cast< XMultiServiceFactory* >(pServiceManager));
1557*cdf0e10cSrcweir         }
1558*cdf0e10cSrcweir         if (xFactory.is())
1559*cdf0e10cSrcweir         {
1560*cdf0e10cSrcweir             xFactory->acquire();
1561*cdf0e10cSrcweir             pResult = xFactory.get();
1562*cdf0e10cSrcweir         }
1563*cdf0e10cSrcweir     }
1564*cdf0e10cSrcweir     return pResult;
1565*cdf0e10cSrcweir }
1566*cdf0e10cSrcweir 
1567*cdf0e10cSrcweir } // extern "C"
1568