1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "com/sun/star/task/DocumentPasswordRequest.hpp"
29 #include "com/sun/star/task/DocumentPasswordRequest2.hpp"
30 #include "com/sun/star/task/DocumentMSPasswordRequest.hpp"
31 #include "com/sun/star/task/DocumentMSPasswordRequest2.hpp"
32 #include "com/sun/star/task/MasterPasswordRequest.hpp"
33 #include "com/sun/star/task/XInteractionAbort.hpp"
34 #include "com/sun/star/task/XInteractionPassword.hpp"
35 #include "com/sun/star/task/XInteractionPassword2.hpp"
36 #include "com/sun/star/task/XInteractionRetry.hpp"
37 #include "com/sun/star/ucb/XInteractionSupplyAuthentication2.hpp"
38 #include "com/sun/star/ucb/URLAuthenticationRequest.hpp"
39 
40 #include "osl/diagnose.h"
41 #include "rtl/digest.h"
42 #include "vos/mutex.hxx"
43 #include "tools/errcode.hxx"
44 #include "vcl/msgbox.hxx"
45 #include "vcl/abstdlg.hxx"
46 #include "vcl/svapp.hxx"
47 
48 #include "ids.hrc"
49 #include "getcontinuations.hxx"
50 #include "passwordcontainer.hxx"
51 #include "loginerr.hxx"
52 #include "logindlg.hxx"
53 #include "masterpasscrtdlg.hxx"
54 #include "masterpassworddlg.hxx"
55 #include "passworddlg.hxx"
56 
57 #include "iahndl.hxx"
58 
59 using namespace com::sun::star;
60 
61 namespace {
62 
63 void
64 executeLoginDialog(
65     Window * pParent,
66     LoginErrorInfo & rInfo,
67     rtl::OUString const & rRealm)
68     SAL_THROW((uno::RuntimeException))
69 {
70     try
71     {
72         vos::OGuard aGuard(Application::GetSolarMutex());
73 
74         bool bAccount = (rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_ACCOUNT) != 0;
75         bool bSavePassword   = rInfo.GetCanRememberPassword();
76         bool bCanUseSysCreds = rInfo.GetCanUseSystemCredentials();
77 
78         sal_uInt16 nFlags = 0;
79         if (rInfo.GetPath().Len() == 0)
80             nFlags |= LF_NO_PATH;
81         if (rInfo.GetErrorText().Len() == 0)
82             nFlags |= LF_NO_ERRORTEXT;
83         if (!bAccount)
84             nFlags |= LF_NO_ACCOUNT;
85         if (!(rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_USER_NAME))
86             nFlags |= LF_USERNAME_READONLY;
87 
88         if (!bSavePassword)
89             nFlags |= LF_NO_SAVEPASSWORD;
90 
91         if (!bCanUseSysCreds)
92             nFlags |= LF_NO_USESYSCREDS;
93 
94         std::auto_ptr< ResMgr > xManager( ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
95         UniString aRealm(rRealm);
96         std::auto_ptr< LoginDialog > xDialog(
97                 new LoginDialog( pParent, nFlags, rInfo.GetServer(), &aRealm, xManager.get()));
98         if (rInfo.GetErrorText().Len() != 0)
99             xDialog->SetErrorText(rInfo.GetErrorText());
100         xDialog->SetName(rInfo.GetUserName());
101         if (bAccount)
102             xDialog->ClearAccount();
103         else
104             xDialog->ClearPassword();
105         xDialog->SetPassword(rInfo.GetPassword());
106 
107         if (bSavePassword)
108         {
109             xDialog->SetSavePasswordText(
110                 ResId(rInfo.GetIsRememberPersistent()
111                           ? RID_SAVE_PASSWORD
112                           : RID_KEEP_PASSWORD,
113                       *xManager.get()));
114 
115             xDialog->SetSavePassword(rInfo.GetIsRememberPassword());
116         }
117 
118         if ( bCanUseSysCreds )
119             xDialog->SetUseSystemCredentials( rInfo.GetIsUseSystemCredentials() );
120 
121         rInfo.SetResult(xDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK :
122                                                        ERRCODE_BUTTON_CANCEL);
123         rInfo.SetUserName(xDialog->GetName());
124         rInfo.SetPassword(xDialog->GetPassword());
125         rInfo.SetAccount(xDialog->GetAccount());
126         rInfo.SetIsRememberPassword(xDialog->IsSavePassword());
127 
128         if ( bCanUseSysCreds )
129           rInfo.SetIsUseSystemCredentials( xDialog->IsUseSystemCredentials() );
130     }
131     catch (std::bad_alloc const &)
132     {
133         throw uno::RuntimeException(
134                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
135                   uno::Reference< uno::XInterface >());
136     }
137 }
138 
139 void getRememberModes(
140     uno::Sequence< ucb::RememberAuthentication > const & rRememberModes,
141     ucb::RememberAuthentication & rPreferredMode,
142     ucb::RememberAuthentication & rAlternateMode )
143 {
144     sal_Int32 nCount = rRememberModes.getLength();
145     OSL_ENSURE( (nCount > 0) && (nCount < 4),
146                 "ucb::RememberAuthentication sequence size mismatch!" );
147     if ( nCount == 1 )
148     {
149         rPreferredMode = rAlternateMode = rRememberModes[ 0 ];
150         return;
151     }
152     else
153     {
154         //bool bHasRememberModeNo = false;
155         bool bHasRememberModeSession = false;
156         bool bHasRememberModePersistent = false;
157 
158         for (sal_Int32 i = 0; i < nCount; ++i)
159         {
160             switch ( rRememberModes[i] )
161             {
162             case ucb::RememberAuthentication_NO:
163                 //bHasRememberModeNo = true;
164                 break;
165             case ucb::RememberAuthentication_SESSION:
166                 bHasRememberModeSession = true;
167                 break;
168             case ucb::RememberAuthentication_PERSISTENT:
169                 bHasRememberModePersistent = true;
170                 break;
171             default:
172                 OSL_TRACE( "Unsupported RememberAuthentication value" );
173                 break;
174             }
175         }
176 
177         if (bHasRememberModePersistent)
178         {
179             rPreferredMode = ucb::RememberAuthentication_PERSISTENT;
180             if (bHasRememberModeSession)
181                 rAlternateMode = ucb::RememberAuthentication_SESSION;
182             else
183                 rAlternateMode = ucb::RememberAuthentication_NO;
184         }
185         else
186         {
187             rPreferredMode = ucb::RememberAuthentication_SESSION;
188             rAlternateMode = ucb::RememberAuthentication_NO;
189         }
190     }
191 }
192 
193 void
194 handleAuthenticationRequest_(
195     Window * pParent,
196     uno::Reference< task::XInteractionHandler > const & xIH,
197     uno::Reference< lang::XMultiServiceFactory > const & xServiceFactory,
198     ucb::AuthenticationRequest const & rRequest,
199     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
200         rContinuations,
201     const rtl::OUString & rURL)
202     SAL_THROW((uno::RuntimeException))
203 {
204     uno::Reference< task::XInteractionRetry > xRetry;
205     uno::Reference< task::XInteractionAbort > xAbort;
206     uno::Reference< ucb::XInteractionSupplyAuthentication >
207         xSupplyAuthentication;
208     uno::Reference< ucb::XInteractionSupplyAuthentication2 >
209         xSupplyAuthentication2;
210     getContinuations(rContinuations, &xRetry, &xAbort, &xSupplyAuthentication);
211     if (xSupplyAuthentication.is())
212         xSupplyAuthentication2.set(xSupplyAuthentication, uno::UNO_QUERY);
213 
214     //////////////////////////
215     // First, try to obtain credentials from password container service.
216     uui::PasswordContainerHelper aPwContainerHelper(xServiceFactory);
217     if (aPwContainerHelper.handleAuthenticationRequest(rRequest,
218                                                        xSupplyAuthentication,
219                                                        rURL,
220                                                        xIH))
221     {
222         xSupplyAuthentication->select();
223         return;
224     }
225 
226     //////////////////////////
227     // Second, try to obtain credentials from user via password dialog.
228     ucb::RememberAuthentication eDefaultRememberMode
229         = ucb::RememberAuthentication_SESSION;
230     ucb::RememberAuthentication ePreferredRememberMode
231         = eDefaultRememberMode;
232     ucb::RememberAuthentication eAlternateRememberMode
233         = ucb::RememberAuthentication_NO;
234 
235     if (xSupplyAuthentication.is())
236     {
237         getRememberModes(
238             xSupplyAuthentication->getRememberPasswordModes(
239                 eDefaultRememberMode),
240             ePreferredRememberMode,
241             eAlternateRememberMode);
242     }
243 
244     sal_Bool bCanUseSystemCredentials;
245     sal_Bool bDefaultUseSystemCredentials;
246     if (xSupplyAuthentication2.is())
247     {
248         bCanUseSystemCredentials
249             = xSupplyAuthentication2->canUseSystemCredentials(
250                 bDefaultUseSystemCredentials);
251     }
252     else
253     {
254         bCanUseSystemCredentials = sal_False;
255         bDefaultUseSystemCredentials = sal_False;
256     }
257 
258     LoginErrorInfo aInfo;
259     aInfo.SetTitle(rRequest.ServerName);
260     aInfo.SetServer(rRequest.ServerName);
261     if (rRequest.HasAccount)
262         aInfo.SetAccount(rRequest.Account);
263     if (rRequest.HasUserName)
264         aInfo.SetUserName(rRequest.UserName);
265     if (rRequest.HasPassword)
266         aInfo.SetPassword(rRequest.Password);
267     aInfo.SetErrorText(rRequest.Diagnostic);
268 
269     aInfo.SetCanRememberPassword(
270         ePreferredRememberMode != eAlternateRememberMode);
271     aInfo.SetIsRememberPassword(
272         ePreferredRememberMode == eDefaultRememberMode);
273     aInfo.SetIsRememberPersistent(
274         ePreferredRememberMode == ucb::RememberAuthentication_PERSISTENT);
275 
276     aInfo.SetCanUseSystemCredentials(bCanUseSystemCredentials);
277     aInfo.SetIsUseSystemCredentials( bDefaultUseSystemCredentials );
278     aInfo.SetModifyAccount(rRequest.HasAccount
279                            && xSupplyAuthentication.is()
280                            && xSupplyAuthentication->canSetAccount());
281     aInfo.SetModifyUserName(rRequest.HasUserName
282                             && xSupplyAuthentication.is()
283                             && xSupplyAuthentication->canSetUserName());
284     executeLoginDialog(pParent,
285                        aInfo,
286                        rRequest.HasRealm ? rRequest.Realm : rtl::OUString());
287     switch (aInfo.GetResult())
288     {
289     case ERRCODE_BUTTON_OK:
290         if (xSupplyAuthentication.is())
291         {
292             if (xSupplyAuthentication->canSetUserName())
293                 xSupplyAuthentication->setUserName(aInfo.GetUserName());
294             if (xSupplyAuthentication->canSetPassword())
295                 xSupplyAuthentication->setPassword(aInfo.GetPassword());
296 
297             if (ePreferredRememberMode != eAlternateRememberMode)
298             {
299                 // user had te choice.
300                 if (aInfo.GetIsRememberPassword())
301                     xSupplyAuthentication->setRememberPassword(
302                         ePreferredRememberMode);
303                 else
304                     xSupplyAuthentication->setRememberPassword(
305                         eAlternateRememberMode);
306             }
307             else
308             {
309                 // user had no choice.
310                 xSupplyAuthentication->setRememberPassword(
311                     ePreferredRememberMode);
312             }
313 
314             if (rRequest.HasRealm)
315             {
316                 if (xSupplyAuthentication->canSetRealm())
317                     xSupplyAuthentication->setRealm(aInfo.GetAccount());
318             }
319             else if (xSupplyAuthentication->canSetAccount())
320                 xSupplyAuthentication->setAccount(aInfo.GetAccount());
321 
322             if ( xSupplyAuthentication2.is() && bCanUseSystemCredentials )
323                 xSupplyAuthentication2->setUseSystemCredentials(
324                     aInfo.GetIsUseSystemCredentials() );
325 
326             xSupplyAuthentication->select();
327         }
328 
329         //////////////////////////
330         // Third, store credentials in password container.
331 
332           if ( aInfo.GetIsUseSystemCredentials() )
333           {
334               if (aInfo.GetIsRememberPassword())
335               {
336                   if (!aPwContainerHelper.addRecord(
337                           rURL.getLength() ? rURL : rRequest.ServerName,
338                           rtl::OUString(), // empty u/p -> sys creds
339                           uno::Sequence< rtl::OUString >(),
340                           xIH,
341                           ePreferredRememberMode
342                               == ucb::RememberAuthentication_PERSISTENT))
343                   {
344                       xSupplyAuthentication->setRememberPassword(
345                           ucb::RememberAuthentication_NO);
346                   }
347               }
348               else if (eAlternateRememberMode
349                            == ucb::RememberAuthentication_SESSION)
350               {
351                   if (!aPwContainerHelper.addRecord(
352                           rURL.getLength() ? rURL : rRequest.ServerName,
353                           rtl::OUString(), // empty u/p -> sys creds
354                           uno::Sequence< rtl::OUString >(),
355                           xIH,
356                           false /* SESSION */))
357                   {
358                       xSupplyAuthentication->setRememberPassword(
359                           ucb::RememberAuthentication_NO);
360                   }
361               }
362           }
363           // Empty user name can not be valid:
364           else if (aInfo.GetUserName().Len() != 0)
365           {
366               uno::Sequence< rtl::OUString >
367                   aPassList(aInfo.GetAccount().Len() == 0 ? 1 : 2);
368               aPassList[0] = aInfo.GetPassword();
369               if (aInfo.GetAccount().Len() != 0)
370                   aPassList[1] = aInfo.GetAccount();
371 
372               if (aInfo.GetIsRememberPassword())
373               {
374                   if (!aPwContainerHelper.addRecord(
375                           rURL.getLength() ? rURL : rRequest.ServerName,
376                           aInfo.GetUserName(),
377                           aPassList,
378                           xIH,
379                           ePreferredRememberMode
380                               == ucb::RememberAuthentication_PERSISTENT))
381                   {
382                       xSupplyAuthentication->setRememberPassword(
383                           ucb::RememberAuthentication_NO);
384                   }
385               }
386               else if (eAlternateRememberMode
387                            == ucb::RememberAuthentication_SESSION)
388               {
389                   if (!aPwContainerHelper.addRecord(
390                           rURL.getLength() ? rURL : rRequest.ServerName,
391                           aInfo.GetUserName(),
392                           aPassList,
393                           xIH,
394                           false /* SESSION */))
395                   {
396                       xSupplyAuthentication->setRememberPassword(
397                           ucb::RememberAuthentication_NO);
398                   }
399               }
400           }
401           break;
402 
403     case ERRCODE_BUTTON_RETRY:
404         if (xRetry.is())
405             xRetry->select();
406         break;
407 
408     default:
409         if (xAbort.is())
410             xAbort->select();
411         break;
412     }
413 }
414 
415 void
416 executeMasterPasswordDialog(
417     Window * pParent,
418     LoginErrorInfo & rInfo,
419     task::PasswordRequestMode nMode)
420         SAL_THROW((uno::RuntimeException))
421 {
422     rtl::OString aMaster;
423     try
424     {
425         vos::OGuard aGuard(Application::GetSolarMutex());
426 
427         std::auto_ptr< ResMgr > xManager(
428             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
429         if( nMode == task::PasswordRequestMode_PASSWORD_CREATE )
430         {
431             std::auto_ptr< MasterPasswordCreateDialog > xDialog(
432                 new MasterPasswordCreateDialog(pParent, xManager.get()));
433             rInfo.SetResult(xDialog->Execute()
434                 == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL);
435             aMaster = rtl::OUStringToOString(
436                 xDialog->GetMasterPassword(), RTL_TEXTENCODING_UTF8);
437         }
438         else
439         {
440             std::auto_ptr< MasterPasswordDialog > xDialog(
441                 new MasterPasswordDialog(pParent, nMode, xManager.get()));
442             rInfo.SetResult(xDialog->Execute()
443                 == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL);
444             aMaster = rtl::OUStringToOString(
445                 xDialog->GetMasterPassword(), RTL_TEXTENCODING_UTF8);
446         }
447     }
448     catch (std::bad_alloc const &)
449     {
450         throw uno::RuntimeException(
451                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
452                   uno::Reference< uno::XInterface >());
453     }
454 
455     sal_uInt8 aKey[RTL_DIGEST_LENGTH_MD5];
456     rtl_digest_PBKDF2(aKey,
457                       RTL_DIGEST_LENGTH_MD5,
458                       reinterpret_cast< sal_uInt8 const * >(aMaster.getStr()),
459                       aMaster.getLength(),
460                       reinterpret_cast< sal_uInt8 const * >(
461                           "3B5509ABA6BC42D9A3A1F3DAD49E56A51"),
462                       32,
463                       1000);
464 
465     rtl::OUStringBuffer aBuffer;
466     for (int i = 0; i < RTL_DIGEST_LENGTH_MD5; ++i)
467     {
468         aBuffer.append(static_cast< sal_Unicode >('a' + (aKey[i] >> 4)));
469         aBuffer.append(static_cast< sal_Unicode >('a' + (aKey[i] & 15)));
470     }
471     rInfo.SetPassword(aBuffer.makeStringAndClear());
472 }
473 
474 void
475 handleMasterPasswordRequest_(
476     Window * pParent,
477     task::PasswordRequestMode nMode,
478     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
479         rContinuations)
480     SAL_THROW((uno::RuntimeException))
481 {
482     uno::Reference< task::XInteractionRetry > xRetry;
483     uno::Reference< task::XInteractionAbort > xAbort;
484     uno::Reference< ucb::XInteractionSupplyAuthentication >
485         xSupplyAuthentication;
486     getContinuations(rContinuations, &xRetry, &xAbort, &xSupplyAuthentication);
487     LoginErrorInfo aInfo;
488 
489     // in case of master password a hash code is returned
490     executeMasterPasswordDialog(pParent, aInfo, nMode);
491 
492     switch (aInfo.GetResult())
493     {
494     case ERRCODE_BUTTON_OK:
495         if (xSupplyAuthentication.is())
496         {
497             if (xSupplyAuthentication->canSetPassword())
498                 xSupplyAuthentication->setPassword(aInfo.GetPassword());
499             xSupplyAuthentication->select();
500         }
501         break;
502 
503     case ERRCODE_BUTTON_RETRY:
504         if (xRetry.is())
505             xRetry->select();
506         break;
507 
508     default:
509         if (xAbort.is())
510             xAbort->select();
511         break;
512     }
513 }
514 
515 void
516 executePasswordDialog(
517     Window * pParent,
518     LoginErrorInfo & rInfo,
519     task::PasswordRequestMode nMode,
520     ::rtl::OUString aDocName,
521     bool bMSCryptoMode,
522     bool bIsPasswordToModify,
523     bool bIsSimplePasswordRequest )
524        SAL_THROW((uno::RuntimeException))
525 {
526     try
527     {
528         vos::OGuard aGuard(Application::GetSolarMutex());
529 
530         std::auto_ptr< ResMgr > xManager(
531             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
532         if( nMode == task::PasswordRequestMode_PASSWORD_CREATE )
533         {
534             if (bIsSimplePasswordRequest)
535             {
536                 std::auto_ptr< PasswordDialog > pDialog(
537                     new PasswordDialog( pParent, nMode, xManager.get(), aDocName,
538                     bIsPasswordToModify, bIsSimplePasswordRequest ) );
539                 pDialog->SetMinLen(0);
540 
541                 rInfo.SetResult( pDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL );
542                 rInfo.SetPassword( pDialog->GetPassword() );
543             }
544             else
545             {
546                 const sal_uInt16 nMaxPasswdLen = bMSCryptoMode ? 15 : 0;   // 0 -> allow any length
547 
548                 VclAbstractDialogFactory * pFact = VclAbstractDialogFactory::Create();
549                 AbstractPasswordToOpenModifyDialog *pTmp = pFact->CreatePasswordToOpenModifyDialog( pParent, 0, nMaxPasswdLen, bIsPasswordToModify );
550                 std::auto_ptr< AbstractPasswordToOpenModifyDialog > pDialog( pTmp );
551 
552                 rInfo.SetResult( pDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL );
553                 rInfo.SetPassword( pDialog->GetPasswordToOpen() );
554                 rInfo.SetPasswordToModify( pDialog->GetPasswordToModify() );
555                 rInfo.SetRecommendToOpenReadonly( pDialog->IsRecommendToOpenReadonly() );
556             }
557         }
558         else // enter password or reenter password
559         {
560             std::auto_ptr< PasswordDialog > pDialog(
561                 new PasswordDialog( pParent, nMode, xManager.get(), aDocName,
562                 bIsPasswordToModify, bIsSimplePasswordRequest ) );
563             pDialog->SetMinLen(0);
564 
565             rInfo.SetResult( pDialog->Execute() == RET_OK ? ERRCODE_BUTTON_OK : ERRCODE_BUTTON_CANCEL );
566             rInfo.SetPassword( bIsPasswordToModify ? String() : pDialog->GetPassword() );
567             rInfo.SetPasswordToModify( bIsPasswordToModify ? pDialog->GetPassword() : String() );
568         }
569     }
570     catch (std::bad_alloc const &)
571     {
572         throw uno::RuntimeException(
573             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
574             uno::Reference< uno::XInterface>());
575     }
576 }
577 
578 void
579 handlePasswordRequest_(
580     Window * pParent,
581     task::PasswordRequestMode nMode,
582     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
583         rContinuations,
584     ::rtl::OUString aDocumentName,
585     bool bMSCryptoMode,
586     bool bIsPasswordToModify,
587     bool bIsSimplePasswordRequest = false )
588     SAL_THROW((uno::RuntimeException))
589 {
590     uno::Reference< task::XInteractionRetry > xRetry;
591     uno::Reference< task::XInteractionAbort > xAbort;
592     uno::Reference< task::XInteractionPassword > xPassword;
593     uno::Reference< task::XInteractionPassword2 > xPassword2;
594     getContinuations(rContinuations, &xRetry, &xAbort, &xPassword2, &xPassword);
595 
596     if ( xPassword2.is() && !xPassword.is() )
597         xPassword.set( xPassword2, uno::UNO_QUERY_THROW );
598 
599     LoginErrorInfo aInfo;
600 
601     executePasswordDialog( pParent, aInfo, nMode,
602             aDocumentName, bMSCryptoMode, bIsPasswordToModify, bIsSimplePasswordRequest );
603 
604     switch (aInfo.GetResult())
605     {
606     case ERRCODE_BUTTON_OK:
607         OSL_ENSURE( !bIsPasswordToModify || xPassword2.is(), "PasswordToModify is requested, but there is no Interaction!" );
608         if (xPassword.is())
609         {
610             if (xPassword2.is())
611             {
612                 xPassword2->setPasswordToModify( aInfo.GetPasswordToModify() );
613                 xPassword2->setRecommendReadOnly( aInfo.IsRecommendToOpenReadonly() );
614             }
615 
616             xPassword->setPassword(aInfo.GetPassword());
617             xPassword->select();
618         }
619         break;
620 
621     case ERRCODE_BUTTON_RETRY:
622         if (xRetry.is())
623             xRetry->select();
624         break;
625 
626     default:
627         if (xAbort.is())
628             xAbort->select();
629         break;
630     }
631 }
632 
633 } // namespace
634 
635 bool
636 UUIInteractionHelper::handleAuthenticationRequest(
637     uno::Reference< task::XInteractionRequest > const & rRequest)
638     SAL_THROW((uno::RuntimeException))
639 {
640     uno::Any aAnyRequest(rRequest->getRequest());
641 
642     ucb::URLAuthenticationRequest aURLAuthenticationRequest;
643     if (aAnyRequest >>= aURLAuthenticationRequest)
644     {
645         handleAuthenticationRequest_(getParentProperty(),
646                                      getInteractionHandler(),
647                                      m_xServiceFactory,
648                                      aURLAuthenticationRequest,
649                                      rRequest->getContinuations(),
650                                      aURLAuthenticationRequest.URL);
651         return true;
652     }
653 
654     ucb::AuthenticationRequest aAuthenticationRequest;
655     if (aAnyRequest >>= aAuthenticationRequest)
656     {
657         handleAuthenticationRequest_(getParentProperty(),
658                                      getInteractionHandler(),
659                                      m_xServiceFactory,
660                                      aAuthenticationRequest,
661                                      rRequest->getContinuations(),
662                                      rtl::OUString());
663         return true;
664     }
665     return false;
666 }
667 
668 bool
669 UUIInteractionHelper::handleMasterPasswordRequest(
670     uno::Reference< task::XInteractionRequest > const & rRequest)
671     SAL_THROW((uno::RuntimeException))
672 {
673     uno::Any aAnyRequest(rRequest->getRequest());
674 
675     task::MasterPasswordRequest aMasterPasswordRequest;
676     if (aAnyRequest >>= aMasterPasswordRequest)
677     {
678         handleMasterPasswordRequest_(getParentProperty(),
679                                      aMasterPasswordRequest.Mode,
680                                      rRequest->getContinuations());
681         return true;
682     }
683     return false;
684 }
685 
686 bool
687 UUIInteractionHelper::handlePasswordRequest(
688     uno::Reference< task::XInteractionRequest > const & rRequest)
689     SAL_THROW((uno::RuntimeException))
690 {
691     // parameters to be filled for the call to handlePasswordRequest_
692     Window * pParent = getParentProperty();
693     task::PasswordRequestMode nMode = task::PasswordRequestMode_PASSWORD_ENTER;
694     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const & rContinuations = rRequest->getContinuations();
695     ::rtl::OUString aDocumentName;
696     bool bMSCryptoMode          = false;
697     bool bIsPasswordToModify    = false;
698 
699     bool bDoHandleRequest = false;
700 
701     uno::Any aAnyRequest(rRequest->getRequest());
702 
703     task::DocumentPasswordRequest2 aDocumentPasswordRequest2;
704     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentPasswordRequest2))
705     {
706         nMode               = aDocumentPasswordRequest2.Mode;
707         aDocumentName       = aDocumentPasswordRequest2.Name;
708         OSL_ENSURE( bMSCryptoMode == false, "bMSCryptoMode should be false" );
709         bIsPasswordToModify = aDocumentPasswordRequest2.IsRequestPasswordToModify;
710 
711         bDoHandleRequest = true;
712     }
713 
714     task::DocumentPasswordRequest aDocumentPasswordRequest;
715     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentPasswordRequest))
716     {
717         nMode               = aDocumentPasswordRequest.Mode;
718         aDocumentName       = aDocumentPasswordRequest.Name;
719         OSL_ENSURE( bMSCryptoMode == false, "bMSCryptoMode should be false" );
720         OSL_ENSURE( bIsPasswordToModify == false, "bIsPasswordToModify should be false" );
721 
722         bDoHandleRequest = true;
723     }
724 
725     task::DocumentMSPasswordRequest2 aDocumentMSPasswordRequest2;
726     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentMSPasswordRequest2))
727     {
728         nMode               = aDocumentMSPasswordRequest2.Mode;
729         aDocumentName       = aDocumentMSPasswordRequest2.Name;
730         bMSCryptoMode       = true;
731         bIsPasswordToModify = aDocumentMSPasswordRequest2.IsRequestPasswordToModify;
732 
733         bDoHandleRequest = true;
734     }
735 
736     task::DocumentMSPasswordRequest aDocumentMSPasswordRequest;
737     if (!bDoHandleRequest && (aAnyRequest >>= aDocumentMSPasswordRequest))
738     {
739         nMode               = aDocumentMSPasswordRequest.Mode;
740         aDocumentName       = aDocumentMSPasswordRequest.Name;
741         bMSCryptoMode       = true;
742         OSL_ENSURE( bIsPasswordToModify == false, "bIsPasswordToModify should be false" );
743 
744         bDoHandleRequest = true;
745     }
746 
747     if (bDoHandleRequest)
748     {
749         handlePasswordRequest_( pParent, nMode, rContinuations,
750                 aDocumentName, bMSCryptoMode, bIsPasswordToModify );
751         return true;
752     }
753 
754     task::PasswordRequest aPasswordRequest;
755     if( aAnyRequest >>= aPasswordRequest )
756     {
757         handlePasswordRequest_(getParentProperty(),
758                                aPasswordRequest.Mode,
759                                rRequest->getContinuations(),
760                                rtl::OUString(),
761                                false /* bool bMSCryptoMode */,
762                                false /* bool bIsPasswordToModify */,
763                                true  /* bool bIsSimplePasswordRequest */ );
764         return true;
765     }
766 
767     return false;
768 }
769 
770