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