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