1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "gio_mount.hxx" 29*cdf0e10cSrcweir #include <ucbhelper/simpleauthenticationrequest.hxx> 30*cdf0e10cSrcweir #include <stdio.h> 31*cdf0e10cSrcweir #include <string.h> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION); 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir static void ooo_mount_operation_ask_password (GMountOperation *op, 36*cdf0e10cSrcweir const char *message, const char *default_user, const char *default_domain, 37*cdf0e10cSrcweir GAskPasswordFlags flags); 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir static void ooo_mount_operation_init (OOoMountOperation *op) 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir op->m_pPrevPassword = NULL; 42*cdf0e10cSrcweir op->m_pPrevUsername = NULL; 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir static void ooo_mount_operation_finalize (GObject *object) 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object); 48*cdf0e10cSrcweir if (mount_op->m_pPrevUsername) 49*cdf0e10cSrcweir free(mount_op->m_pPrevUsername); 50*cdf0e10cSrcweir if (mount_op->m_pPrevPassword) 51*cdf0e10cSrcweir free(mount_op->m_pPrevPassword); 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object); 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir static void ooo_mount_operation_class_init (OOoMountOperationClass *klass) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir GObjectClass *object_class = G_OBJECT_CLASS (klass); 59*cdf0e10cSrcweir object_class->finalize = ooo_mount_operation_finalize; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass); 62*cdf0e10cSrcweir mount_op_class->ask_password = ooo_mount_operation_ask_password; 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir using namespace com::sun::star; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir static void ooo_mount_operation_ask_password (GMountOperation *op, 68*cdf0e10cSrcweir const char * /*message*/, const char *default_user, 69*cdf0e10cSrcweir const char *default_domain, GAskPasswordFlags flags) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir uno::Reference< task::XInteractionHandler > xIH; 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir OOoMountOperation *pThis = (OOoMountOperation*)op; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir const com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir if (xEnv.is()) 78*cdf0e10cSrcweir xIH = xEnv->getInteractionHandler(); 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir if (!xIH.is()) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); 83*cdf0e10cSrcweir return; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir ::rtl::OUString aHostName, aDomain, aUserName, aPassword; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir ucbhelper::SimpleAuthenticationRequest::EntityType eUserName = 89*cdf0e10cSrcweir (flags & G_ASK_PASSWORD_NEED_USERNAME) 90*cdf0e10cSrcweir ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY 91*cdf0e10cSrcweir : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir if (default_user) 94*cdf0e10cSrcweir aUserName = rtl::OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir ucbhelper::SimpleAuthenticationRequest::EntityType ePassword = 97*cdf0e10cSrcweir (flags & G_ASK_PASSWORD_NEED_PASSWORD) 98*cdf0e10cSrcweir ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY 99*cdf0e10cSrcweir : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir rtl::OUString aPrevPassword, aPrevUsername; 102*cdf0e10cSrcweir if (pThis->m_pPrevUsername) 103*cdf0e10cSrcweir aPrevUsername = rtl::OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8); 104*cdf0e10cSrcweir if (pThis->m_pPrevPassword) 105*cdf0e10cSrcweir aPrevPassword = rtl::OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#" 108*cdf0e10cSrcweir if ( aUserName.getLength() == 0 ) 109*cdf0e10cSrcweir aUserName = aPrevUsername; 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir if ( aPassword.getLength() == 0 ) 112*cdf0e10cSrcweir aPassword = aPrevPassword; 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir ucbhelper::SimpleAuthenticationRequest::EntityType eDomain = 115*cdf0e10cSrcweir (flags & G_ASK_PASSWORD_NEED_DOMAIN) 116*cdf0e10cSrcweir ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY 117*cdf0e10cSrcweir : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA; 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir if (default_domain) 120*cdf0e10cSrcweir aDomain = rtl::OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir uno::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest 123*cdf0e10cSrcweir = new ucbhelper::SimpleAuthenticationRequest (rtl::OUString() /* FIXME: provide URL here */, aHostName, eDomain, aDomain, eUserName, aUserName, ePassword, aPassword); 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir xIH->handle( xRequest.get() ); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection(); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir if ( !xSelection.is() ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); 132*cdf0e10cSrcweir return; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY ); 136*cdf0e10cSrcweir if ( xAbort.is() ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); 139*cdf0e10cSrcweir return; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier(); 143*cdf0e10cSrcweir aUserName = xSupp->getUserName(); 144*cdf0e10cSrcweir aPassword = xSupp->getPassword(); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir if (flags & G_ASK_PASSWORD_NEED_USERNAME) 147*cdf0e10cSrcweir g_mount_operation_set_username(op, rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr()); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir if (flags & G_ASK_PASSWORD_NEED_PASSWORD) 150*cdf0e10cSrcweir g_mount_operation_set_password(op, rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr()); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir if (flags & G_ASK_PASSWORD_NEED_DOMAIN) 153*cdf0e10cSrcweir g_mount_operation_set_domain(op, rtl::OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr()); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir switch (xSupp->getRememberPasswordMode()) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir default: 158*cdf0e10cSrcweir case ucb::RememberAuthentication_NO: 159*cdf0e10cSrcweir g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER); 160*cdf0e10cSrcweir break; 161*cdf0e10cSrcweir case ucb::RememberAuthentication_SESSION: 162*cdf0e10cSrcweir g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION); 163*cdf0e10cSrcweir break; 164*cdf0e10cSrcweir case ucb::RememberAuthentication_PERSISTENT: 165*cdf0e10cSrcweir g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY); 166*cdf0e10cSrcweir break; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir if (pThis->m_pPrevPassword) 170*cdf0e10cSrcweir free(pThis->m_pPrevPassword); 171*cdf0e10cSrcweir pThis->m_pPrevPassword = strdup(rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr()); 172*cdf0e10cSrcweir if (pThis->m_pPrevUsername) 173*cdf0e10cSrcweir free(pThis->m_pPrevUsername); 174*cdf0e10cSrcweir pThis->m_pPrevUsername = strdup(rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr()); 175*cdf0e10cSrcweir g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir GMountOperation *ooo_mount_operation_new(const uno::Reference< ucb::XCommandEnvironment >& rEnv) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir OOoMountOperation *pRet = (OOoMountOperation*)g_object_new (OOO_TYPE_MOUNT_OPERATION, NULL); 181*cdf0e10cSrcweir pRet->pEnv = &rEnv; 182*cdf0e10cSrcweir return (GMountOperation*)pRet; 183*cdf0e10cSrcweir } 184