| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/config.h> |
| 21 |
|
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include "gio_mount.hxx" |
| 25 |
#include <ucbhelper/simpleauthenticationrequest.hxx> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#ifdef __GNUC__ |
| 29 |
#pragma GCC diagnostic push |
| 30 |
#pragma GCC diagnostic ignored "-Wunused-function" |
| 31 |
#if defined __clang__ |
| 32 |
#if __has_warning("-Wdeprecated-volatile") |
| 33 |
#pragma clang diagnostic ignored "-Wdeprecated-volatile" |
| 34 |
#endif |
| 35 |
#endif |
| 36 |
#endif |
| 37 |
G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION); |
| 38 |
#ifdef __GNUC__ |
| 39 |
#pragma GCC diagnostic pop |
| 40 |
#endif |
| 41 |
|
| 42 |
static void ooo_mount_operation_ask_password (GMountOperation *op, |
| 43 |
const char *message, const char *default_user, const char *default_domain, |
| 44 |
GAskPasswordFlags flags); |
| 45 |
|
| 46 |
static void ooo_mount_operation_init (OOoMountOperation *op) |
| 47 |
{ |
| 48 |
op->m_aPrevPassword.clear(); |
| 49 |
op->m_aPrevUsername.clear(); |
| 50 |
} |
| 51 |
|
| 52 |
void ooo_mount_operation_finalize (GObject *object) |
| 53 |
{ |
| 54 |
OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object); |
| 55 |
|
| 56 |
mount_op->~OOoMountOperation(); |
| 57 |
|
| 58 |
// coverity[pass_freed_arg : FALSE] - object is still valid for parent's purpose |
| 59 |
G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object); |
| 60 |
} |
| 61 |
|
| 62 |
static void ooo_mount_operation_class_init (OOoMountOperationClass *klass) |
| 63 |
{ |
| 64 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 65 |
object_class->finalize = ooo_mount_operation_finalize; |
| 66 |
|
| 67 |
GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass); |
| 68 |
mount_op_class->ask_password = ooo_mount_operation_ask_password; |
| 69 |
} |
| 70 |
|
| 71 |
namespace { |
| 72 |
|
| 73 |
// Temporarily undo the g_main_context_push_thread_default done in the surrounding MountOperation |
| 74 |
// ctor (in ucb/source/ucp/gio/gio_content.cxx): |
| 75 |
struct GlibThreadDefaultMainContextScope { |
| 76 |
public: |
| 77 |
GlibThreadDefaultMainContextScope(GMainContext * context): context_(context) |
| 78 |
{ g_main_context_push_thread_default(context_); } |
| 79 |
|
| 80 |
~GlibThreadDefaultMainContextScope() { g_main_context_pop_thread_default(context_); } |
| 81 |
|
| 82 |
private: |
| 83 |
GMainContext * context_; |
| 84 |
}; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
static void ooo_mount_operation_ask_password (GMountOperation *op, |
| 89 |
const char * /*message*/, const char *default_user, |
| 90 |
const char *default_domain, GAskPasswordFlags flags) |
| 91 |
{ |
| 92 |
css::uno::Reference< css::task::XInteractionHandler > xIH; |
| 93 |
|
| 94 |
OOoMountOperation *pThis = reinterpret_cast<OOoMountOperation*>(op); |
| 95 |
GlibThreadDefaultMainContextScope scope(pThis->context.get()); |
| 96 |
|
| 97 |
const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv = pThis->xEnv; |
| 98 |
|
| 99 |
if (xEnv.is()) |
| 100 |
xIH = xEnv->getInteractionHandler(); |
| 101 |
|
| 102 |
if (!xIH.is()) |
| 103 |
{ |
| 104 |
g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
OUString aDomain, aUserName, aPassword; |
| 109 |
|
| 110 |
if (default_user) |
| 111 |
aUserName = OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8); |
| 112 |
|
| 113 |
ucbhelper::SimpleAuthenticationRequest::EntityType eUserName = |
| 114 |
(flags & G_ASK_PASSWORD_NEED_USERNAME) |
| 115 |
? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY |
| 116 |
: aUserName.isEmpty() ? ucbhelper::SimpleAuthenticationRequest::ENTITY_NA |
| 117 |
: ucbhelper::SimpleAuthenticationRequest::ENTITY_FIXED; |
| 118 |
|
| 119 |
ucbhelper::SimpleAuthenticationRequest::EntityType ePassword = |
| 120 |
(flags & G_ASK_PASSWORD_NEED_PASSWORD) |
| 121 |
? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY |
| 122 |
: ucbhelper::SimpleAuthenticationRequest::ENTITY_NA; |
| 123 |
|
| 124 |
OUString aPrevPassword = pThis->m_aPrevUsername; |
| 125 |
OUString aPrevUsername = pThis->m_aPrevPassword; |
| 126 |
|
| 127 |
//The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#" |
| 128 |
if ( aUserName.isEmpty() ) |
| 129 |
aUserName = aPrevUsername; |
| 130 |
|
| 131 |
if ( aPassword.isEmpty() ) |
| 132 |
aPassword = aPrevPassword; |
| 133 |
|
| 134 |
ucbhelper::SimpleAuthenticationRequest::EntityType eDomain = |
| 135 |
(flags & G_ASK_PASSWORD_NEED_DOMAIN) |
| 136 |
? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY |
| 137 |
: ucbhelper::SimpleAuthenticationRequest::ENTITY_NA; |
| 138 |
|
| 139 |
if (default_domain) |
| 140 |
aDomain = OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8); |
| 141 |
|
| 142 |
rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest |
| 143 |
= new ucbhelper::SimpleAuthenticationRequest (OUString() /* FIXME: provide URL here */, OUString(), eDomain, aDomain, eUserName, aUserName, ePassword, aPassword); |
| 144 |
|
| 145 |
xIH->handle( xRequest ); |
| 146 |
|
| 147 |
rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection(); |
| 148 |
|
| 149 |
if ( !xSelection.is() ) |
| 150 |
{ |
| 151 |
g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
css::uno::Reference< css::task::XInteractionAbort > xAbort(xSelection->getXWeak(), css::uno::UNO_QUERY ); |
| 156 |
if ( xAbort.is() ) |
| 157 |
{ |
| 158 |
g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier(); |
| 163 |
aUserName = xSupp->getUserName(); |
| 164 |
aPassword = xSupp->getPassword(); |
| 165 |
|
| 166 |
if (flags & G_ASK_PASSWORD_NEED_USERNAME) |
| 167 |
g_mount_operation_set_username(op, OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr()); |
| 168 |
|
| 169 |
if (flags & G_ASK_PASSWORD_NEED_PASSWORD) |
| 170 |
g_mount_operation_set_password(op, OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr()); |
| 171 |
|
| 172 |
if (flags & G_ASK_PASSWORD_NEED_DOMAIN) |
| 173 |
g_mount_operation_set_domain(op, OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr()); |
| 174 |
|
| 175 |
switch (xSupp->getRememberPasswordMode()) |
| 176 |
{ |
| 177 |
default: |
| 178 |
case css::ucb::RememberAuthentication_NO: |
| 179 |
g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER); |
| 180 |
break; |
| 181 |
case css::ucb::RememberAuthentication_SESSION: |
| 182 |
g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION); |
| 183 |
break; |
| 184 |
case css::ucb::RememberAuthentication_PERSISTENT: |
| 185 |
g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY); |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
pThis->m_aPrevPassword = aPassword; |
| 190 |
pThis->m_aPrevUsername = aUserName; |
| 191 |
g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED); |
| 192 |
} |
| 193 |
|
| 194 |
GMountOperation *ooo_mount_operation_new(ucb::ucp::gio::glib::MainContextRef && context, const css::uno::Reference< css::ucb::XCommandEnvironment >& rEnv) |
| 195 |
{ |
| 196 |
void* pMem = g_object_new (OOO_TYPE_MOUNT_OPERATION, nullptr); |
| 197 |
OOoMountOperation *pRet = new (pMem) OOoMountOperation; |
| 198 |
pRet->context = std::move(context); |
| 199 |
pRet->xEnv = rEnv; |
| 200 |
return &pRet->parent_instance; |
| 201 |
} |
| 202 |
|
| 203 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |