xref: /aoo42x/main/ucb/source/ucp/gio/gio_mount.cxx (revision 2f86921c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "gio_mount.hxx"
25 #include <ucbhelper/simpleauthenticationrequest.hxx>
26 #include <stdio.h>
27 #include <string.h>
28 
29 G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
30 
31 static void ooo_mount_operation_ask_password (GMountOperation   *op,
32     const char *message, const char *default_user, const char *default_domain,
33     GAskPasswordFlags flags);
34 
ooo_mount_operation_init(OOoMountOperation * op)35 static void ooo_mount_operation_init (OOoMountOperation *op)
36 {
37     op->m_pPrevPassword = NULL;
38     op->m_pPrevUsername = NULL;
39 }
40 
ooo_mount_operation_finalize(GObject * object)41 static void ooo_mount_operation_finalize (GObject *object)
42 {
43     OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
44     if (mount_op->m_pPrevUsername)
45         free(mount_op->m_pPrevUsername);
46     if (mount_op->m_pPrevPassword)
47         free(mount_op->m_pPrevPassword);
48 
49     G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
50 }
51 
ooo_mount_operation_class_init(OOoMountOperationClass * klass)52 static void ooo_mount_operation_class_init (OOoMountOperationClass *klass)
53 {
54     GObjectClass *object_class = G_OBJECT_CLASS (klass);
55     object_class->finalize = ooo_mount_operation_finalize;
56 
57     GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
58     mount_op_class->ask_password = ooo_mount_operation_ask_password;
59 }
60 
61 using namespace com::sun::star;
62 
ooo_mount_operation_ask_password(GMountOperation * op,const char *,const char * default_user,const char * default_domain,GAskPasswordFlags flags)63 static void ooo_mount_operation_ask_password (GMountOperation *op,
64     const char * /*message*/, const char *default_user,
65     const char *default_domain, GAskPasswordFlags flags)
66 {
67     uno::Reference< task::XInteractionHandler > xIH;
68 
69     OOoMountOperation *pThis = (OOoMountOperation*)op;
70 
71     const com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
72 
73     if (xEnv.is())
74       xIH = xEnv->getInteractionHandler();
75 
76     if (!xIH.is())
77     {
78         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
79         return;
80     }
81 
82     ::rtl::OUString aHostName, aDomain, aUserName, aPassword;
83 
84     ucbhelper::SimpleAuthenticationRequest::EntityType eUserName =
85         (flags & G_ASK_PASSWORD_NEED_USERNAME)
86           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
87           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
88 
89     if (default_user)
90         aUserName = rtl::OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
91 
92     ucbhelper::SimpleAuthenticationRequest::EntityType ePassword =
93         (flags & G_ASK_PASSWORD_NEED_PASSWORD)
94           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
95           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
96 
97     rtl::OUString aPrevPassword, aPrevUsername;
98     if (pThis->m_pPrevUsername)
99         aPrevUsername = rtl::OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
100     if (pThis->m_pPrevPassword)
101         aPrevPassword = rtl::OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
102 
103     //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
104     if ( aUserName.getLength() == 0 )
105         aUserName = aPrevUsername;
106 
107     if ( aPassword.getLength() == 0 )
108         aPassword = aPrevPassword;
109 
110     ucbhelper::SimpleAuthenticationRequest::EntityType eDomain =
111         (flags & G_ASK_PASSWORD_NEED_DOMAIN)
112           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
113           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
114 
115     if (default_domain)
116         aDomain = rtl::OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
117 
118     uno::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
119         = new ucbhelper::SimpleAuthenticationRequest (rtl::OUString() /* FIXME: provide URL here */, aHostName, eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
120 
121     xIH->handle( xRequest.get() );
122 
123     rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
124 
125     if ( !xSelection.is() )
126     {
127         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
128         return;
129     }
130 
131     uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
132     if ( xAbort.is() )
133     {
134         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
135         return;
136     }
137 
138     const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
139     aUserName = xSupp->getUserName();
140     aPassword = xSupp->getPassword();
141 
142     if (flags & G_ASK_PASSWORD_NEED_USERNAME)
143         g_mount_operation_set_username(op, rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
144 
145     if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
146         g_mount_operation_set_password(op, rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
147 
148     if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
149         g_mount_operation_set_domain(op, rtl::OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
150 
151     switch (xSupp->getRememberPasswordMode())
152     {
153 	default:
154         case ucb::RememberAuthentication_NO:
155             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
156             break;
157         case ucb::RememberAuthentication_SESSION:
158             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
159             break;
160         case ucb::RememberAuthentication_PERSISTENT:
161             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
162             break;
163     }
164 
165     if (pThis->m_pPrevPassword)
166         free(pThis->m_pPrevPassword);
167     pThis->m_pPrevPassword = strdup(rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
168     if (pThis->m_pPrevUsername)
169         free(pThis->m_pPrevUsername);
170     pThis->m_pPrevUsername = strdup(rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
171     g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
172 }
173 
ooo_mount_operation_new(const uno::Reference<ucb::XCommandEnvironment> & rEnv)174 GMountOperation *ooo_mount_operation_new(const uno::Reference< ucb::XCommandEnvironment >& rEnv)
175 {
176     OOoMountOperation *pRet = (OOoMountOperation*)g_object_new (OOO_TYPE_MOUNT_OPERATION, NULL);
177     pRet->pEnv = &rEnv;
178     return (GMountOperation*)pRet;
179 }
180