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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include "helper.hxx"
32 #include "osl/diagnose.h"
33 #include "rtl/ustring.h"
34 /*#include "libxml/xmlstring.h"
35 */
36 
37 /*-
38  * Helper : create a input stream from a file
39  */
40 Reference< XInputStream > createStreamFromFile( const OUString sFile )
41 {
42 	const sal_Char* pcFile ;
43 	OString aString ;
44 
45 	aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ;
46 	pcFile = aString.getStr() ;
47 	if( pcFile != NULL ) {
48 		FILE *f = fopen( pcFile , "rb" );
49 		Reference<  XInputStream >  r;
50 
51 		if( f ) {
52 			fseek( f , 0 , SEEK_END );
53 			int nLength = ftell( f );
54 			fseek( f , 0 , SEEK_SET );
55 
56 			Sequence<sal_Int8> seqIn(nLength);
57 			fread( seqIn.getArray() , nLength , 1 , f );
58 
59 			r = Reference< XInputStream > ( new OInputStream( seqIn ) );
60 			fclose( f );
61 		}
62 		return r;
63 	} else {
64 		return NULL ;
65 	}
66 
67 	return NULL ;
68 }
69 
70 /*-
71  * Helper : set a output stream to a file
72  */
73 Reference< XOutputStream > createStreamToFile( const OUString sFile )
74 {
75 	const sal_Char* pcFile ;
76 	OString aString ;
77 
78 	aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ;
79 	pcFile = aString.getStr() ;
80 	if( pcFile != NULL )
81 		return Reference< XOutputStream >( new OOutputStream( pcFile ) ) ;
82 	else
83 		return NULL ;
84 }
85 
86 /*-
87  * Helper : get service manager and context
88  */
89 Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl ) throw( RuntimeException , Exception )
90 {
91 	Reference< XMultiComponentFactory > xLocalServiceManager = NULL ;
92 	Reference< XComponentContext > xLocalComponentContext = NULL ;
93 	Reference< XMultiComponentFactory > xUsedServiceManager = NULL ;
94 	Reference< XComponentContext > xUsedComponentContext = NULL ;
95 
96 	OSL_ENSURE( !sUnoUrl.equalsAscii( "" ) ,
97 		"serviceManager - "
98 		"No uno URI specified" ) ;
99 
100 	OSL_ENSURE( !sRdbUrl.equalsAscii( "" ) ,
101 		"serviceManager - "
102 		"No rdb URI specified" ) ;
103 
104 	if( sUnoUrl.equalsAscii( "local" ) ) {
105 		Reference< XSimpleRegistry > xSimpleRegistry = createSimpleRegistry();
106 		OSL_ENSURE( xSimpleRegistry.is() ,
107 			"serviceManager - "
108 			"Cannot create simple registry" ) ;
109 
110 		//xSimpleRegistry->open(OUString::createFromAscii("xmlsecurity.rdb"), sal_False, sal_False);
111 		xSimpleRegistry->open(sRdbUrl, sal_True, sal_False);
112 		OSL_ENSURE( xSimpleRegistry->isValid() ,
113 			"serviceManager - "
114 			"Cannot open xml security registry rdb" ) ;
115 
116 		xLocalComponentContext = bootstrap_InitialComponentContext( xSimpleRegistry ) ;
117 		OSL_ENSURE( xLocalComponentContext.is() ,
118 			"serviceManager - "
119 			"Cannot create intial component context" ) ;
120 
121 		xLocalServiceManager = xLocalComponentContext->getServiceManager() ;
122 		OSL_ENSURE( xLocalServiceManager.is() ,
123 			"serviceManager - "
124 			"Cannot create intial service manager" ) ;
125 
126 		/*-
127 		 * Because of the exception rasied from
128 		 * ucbhelper/source/provider/provconf.cxx, lin 323
129 		 * I do not use the content broker at present
130 		 ********************************************************************
131 		//init ucb
132 		if( ::ucb::ContentBroker::get() == NULL ) {
133 			Reference< lang::XMultiServiceFactory > xSvmg( xLocalServiceManager , UNO_QUERY ) ;
134 			OSL_ENSURE( xLocalServiceManager.is() ,
135 				"serviceManager - "
136 				"Cannot get multi-service factory" ) ;
137 
138 			Sequence< Any > args( 2 ) ;
139 			args[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL ) ;
140 			args[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE ) ;
141 			if( ! ::ucb::ContentBroker::initialize( xSvmg , args ) ) {
142 				throw RuntimeException( OUString::createFromAscii( "Cannot inlitialize ContentBroker" ) , Reference< XInterface >() , Any() ) ;
143 			}
144 		}
145 		********************************************************************/
146 
147 		xUsedComponentContext = xLocalComponentContext ;
148 		xUsedServiceManager = xLocalServiceManager ;
149 	} else {
150 		Reference< XComponentContext > xLocalComponentContext = defaultBootstrap_InitialComponentContext() ;
151 		OSL_ENSURE( xLocalComponentContext.is() ,
152 			"serviceManager - "
153 			"Cannot create intial component context" ) ;
154 
155 		Reference< XMultiComponentFactory > xLocalServiceManager = xLocalComponentContext->getServiceManager();
156 		OSL_ENSURE( xLocalServiceManager.is() ,
157 			"serviceManager - "
158 			"Cannot create intial service manager" ) ;
159 
160 		Reference< XInterface > urlResolver =
161 			xLocalServiceManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver") , xLocalComponentContext ) ;
162 		OSL_ENSURE( urlResolver.is() ,
163 			"serviceManager - "
164 			"Cannot get service instance of \"bridge.UnoUrlResolver\"" ) ;
165 
166 		Reference< XUnoUrlResolver > xUnoUrlResolver( urlResolver , UNO_QUERY ) ;
167 		OSL_ENSURE( xUnoUrlResolver.is() ,
168 			"serviceManager - "
169 			"Cannot get interface of \"XUnoUrlResolver\" from service \"bridge.UnoUrlResolver\"" ) ;
170 
171 		Reference< XInterface > initialObject = xUnoUrlResolver->resolve( sUnoUrl ) ;
172 		OSL_ENSURE( initialObject.is() ,
173 			"serviceManager - "
174 			"Cannot resolve uno url" ) ;
175 
176 		/*-
177 		 * Method 1: with Naming Service
178 		 ********************************************************************
179 		Reference< XNamingService > xNamingService( initialObject , UNO_QUERY ) ;
180 		OSL_ENSURE( xNamingService.is() ,
181 			"serviceManager - "
182 			"Cannot get interface of \"XNamingService\" from URL resolver" ) ;
183 
184 		Reference< XInterface > serviceManager =
185 			xNamingService->getRegisteredObject( OUString::createFromAscii( "StarOffice.ServiceManager" ) ) ;
186 		OSL_ENSURE( serviceManager.is() ,
187 			"serviceManager - "
188 			"Cannot get service instance of \"StarOffice.ServiceManager\"" ) ;
189 
190 		xUsedServiceManager = Reference< XMultiComponentFactory >( serviceManager , UNO_QUERY );
191 		OSL_ENSURE( xUsedServiceManager.is() ,
192 			"serviceManager - "
193 			"Cannot get interface of \"XMultiComponentFactory\" from service \"StarOffice.ServiceManager\"" ) ;
194 
195 		Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ;
196 		OSL_ENSURE( xPropSet.is() ,
197 			"serviceManager - "
198 			"Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ;
199 
200 		xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ;
201 		OSL_ENSURE( xUsedComponentContext.is() ,
202 			"serviceManager - "
203 			"Cannot create remote component context" ) ;
204 
205 		********************************************************************/
206 
207 		/*-
208 		 * Method 2: with Componnent context
209 		 ********************************************************************
210 		Reference< XPropertySet > xPropSet( initialObject , UNO_QUERY ) ;
211 		OSL_ENSURE( xPropSet.is() ,
212 			"serviceManager - "
213 			"Cannot get interface of \"XPropertySet\" from URL resolver" ) ;
214 
215 		xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ;
216 		OSL_ENSURE( xUsedComponentContext.is() ,
217 			"serviceManager - "
218 			"Cannot create remote component context" ) ;
219 
220 		xUsedServiceManager = xUsedComponentContext->getServiceManager();
221 		OSL_ENSURE( xUsedServiceManager.is() ,
222 			"serviceManager - "
223 			"Cannot create remote service manager" ) ;
224 		********************************************************************/
225 
226 		/*-
227 		 * Method 3: with Service Manager
228 		 ********************************************************************/
229 		xUsedServiceManager = Reference< XMultiComponentFactory >( initialObject , UNO_QUERY );
230 		OSL_ENSURE( xUsedServiceManager.is() ,
231 			"serviceManager - "
232 			"Cannot create remote service manager" ) ;
233 
234 		Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ;
235 		OSL_ENSURE( xPropSet.is() ,
236 			"serviceManager - "
237 			"Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ;
238 
239 		xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ;
240 		OSL_ENSURE( xUsedComponentContext.is() ,
241 			"serviceManager - "
242 			"Cannot create remote component context" ) ;
243 		/********************************************************************/
244 	}
245 
246 	xContext = xUsedComponentContext ;
247 	return xUsedServiceManager ;
248 }
249 
250 char* PriPK11PasswordFunc(
251 	PK11SlotInfo *slot ,
252 	PRBool retry ,
253 	void* arg
254 ) {
255 	char* passwd = NULL ;
256 
257 	if( retry != PR_TRUE ) {
258 		passwd = ( char* )PORT_Alloc( 20 ) ;
259 		printf( "Input Password:\n" ) ;
260 		scanf( "%s" , passwd ) ;
261 		printf( "The passwod is [%s]\n" , passwd ) ;
262 	}
263 
264 	return passwd ;
265 }
266 
267