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_framework.hxx"
26 
27 //_________________________________________________________________________________________________________________
28 //	my own includes
29 //_________________________________________________________________________________________________________________
30 
31 #ifndef __FRAMEWORK_SERVICES_LOGINDIALOG_HXX_
32 #include <services/logindialog.hxx>
33 #endif
34 #include <classes/servicemanager.hxx>
35 #include <macros/generic.hxx>
36 #include <macros/debug.hxx>
37 #include <services.h>
38 
39 //_________________________________________________________________________________________________________________
40 //	interface includes
41 //_________________________________________________________________________________________________________________
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/awt/XDialog.hpp>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 
46 //_________________________________________________________________________________________________________________
47 //	other includes
48 //_________________________________________________________________________________________________________________
49 #include <comphelper/processfactory.hxx>
50 #include <com/sun/star/uno/Reference.hxx>
51 #include <vos/process.hxx>
52 #include <rtl/ustring.hxx>
53 #include <rtl/ustrbuf.hxx>
54 #include <vcl/event.hxx>
55 #include <vcl/svapp.hxx>
56 #include <vcl/wrkwin.hxx>
57 #include <vcl/msgbox.hxx>
58 
59 #include <stdio.h>
60 
61 //_________________________________________________________________________________________________________________
62 //	const
63 //_________________________________________________________________________________________________________________
64 
65 #define	TEMPFILE_ENCODING			RTL_TEXTENCODING_UTF8			// encoding of written temp. ascii file
66 #define	LOGIN_RDB					DECLARE_ASCII("login.rdb")		// name of our own registry file - neccessary to create own servicemanager
67 #define SEPERATOR                   "\n"                            // used to seperate parts in temp. file
68 
69 #define MINARGUMENTCOUNT            1                               // count of min. required arguments
70 #define ARGUMENTFOUND               0                               // OUString::compareTo returns 0 if searched string match given one
71 #define ARGUMENTLENGTH              3                               // length of fixed part of any argument to detect it easier!
72 
73 #define ARGUMENT_TEMPFILE           DECLARE_ASCII("-f=")            // we support "-f=c:\temp\test.txt" to write dialog data in temp. file
74 #define ARGUMENT_DIALOGPARENT       DECLARE_ASCII("-p=")            // we support "-p=36748322" as window handle of parent for vcl dialog
75 
76 //_________________________________________________________________________________________________________________
77 //	namespace
78 //_________________________________________________________________________________________________________________
79 
80 using namespace ::rtl						;
81 using namespace ::vos						;
82 using namespace ::comphelper				;
83 using namespace ::framework					;
84 using namespace ::com::sun::star::uno		;
85 using namespace ::com::sun::star::lang		;
86 using namespace ::com::sun::star::awt		;
87 using namespace ::com::sun::star::beans		;
88 
89 //_________________________________________________________________________________________________________________
90 //	defines
91 //_________________________________________________________________________________________________________________
92 
93 //_________________________________________________________________________________________________________________
94 //	declarations
95 //_________________________________________________________________________________________________________________
96 
97 /*-************************************************************************************************************//**
98 	@short		implement command application to show login dialog and save his information in temp. file!
99 	@descr		We need this temp. file to share informations between our dialog and different processes, which
100 				can't use vcl directly. Caller of this executable give us the file name as an argument - we save
101 				all informations in it - caller can read it and MUST delete temp. file.
102 				This is neccessary for example; to hide the password!
103 
104 	@implements	-
105 
106 	@base		Application
107 *//*-*************************************************************************************************************/
108 class LoginApplication : public Application
109 {
110 	//*************************************************************************************************************
111 	//	public methods
112 	//*************************************************************************************************************
113 	public:
114 		void Main();
115 
116 	//*************************************************************************************************************
117 	//	private methods
118 	//*************************************************************************************************************
119 	private:
120 		void impl_parseCommandline();		// search supported arguments on command line
121 
122 	//*************************************************************************************************************
123 	//	private variables
124 	//*************************************************************************************************************
125 	private:
126         OString     m_sTempFile     ;   // name of temp. file in system notation
127         sal_Int32   m_nParentHandle ;   // a parent window handle for used vcl dialog
128 
129 };	//	class LoginApplication
130 
131 //_________________________________________________________________________________________________________________
132 //	global variables
133 //_________________________________________________________________________________________________________________
134 
135 LoginApplication	gLoginApplication;
136 
137 //_________________________________________________________________________________________________________________
138 //	main
139 //_________________________________________________________________________________________________________________
140 
Main()141 void LoginApplication::Main()
142 {
143 	// Init global uno servicemanager.
144 	ServiceManager aManager;
145 	Reference< XMultiServiceFactory > xServiceManager = aManager.getSharedUNOServiceManager( DECLARE_ASCII("login.rdb") );
146 	LOG_ASSERT( !(xServiceManager.is()==sal_False), "LoginApplication::Main()\nCould not create uno service manager!\n" )
147 
148 	// Parse command line and set found arguments on application member.
149 	impl_parseCommandline();
150 	LOG_ASSERT( !(m_sTempFile.getLength()<1), "LoginApplication::Main()\nWrong or missing argument for temp. file detected!\n" )
151 
152 	// Try to get neccessary dialog service.
153 	// By the way - cast it to interface XPropertySet too - we need it later.
154 	// (define SERVICENAME... comes from defines.hxx!)
155 	Reference< XDialog >		xLoginDialog( xServiceManager->createInstance( SERVICENAME_LOGINDIALOG ), UNO_QUERY );
156 	Reference< XPropertySet >	xPropertySet( xLoginDialog												, UNO_QUERY );
157 
158 	// Work with valid ressources only!
159 	// Otherwise do nothing ...
160 	if	(
161 			( xLoginDialog.is()			==	sal_True	)	&&
162 			( xPropertySet.is()			==	sal_True	)	&&
163             ( m_sTempFile.getLength()   >   0           )
164 		)
165 	{
166         // Exist a parent window? YES => set right property.
167         if( m_nParentHandle != 0 )
168         {
169             Any aParentWindow;
170             aParentWindow <<= m_nParentHandle;
171             xPropertySet->setPropertyValue( PROPERTYNAME_PARENTWINDOW, aParentWindow );
172         }
173 
174 		Any aConnectionType;
175 		aConnectionType <<= PROPERTYNAME_HTTPS;
176 		xPropertySet->setPropertyValue( PROPERTYNAME_CONNECTIONTYPE, aConnectionType );
177 
178 		// Show login dialog and get decision of user.
179 		sal_Bool bDecision = (sal_Bool)(xLoginDialog->execute());
180 
181 		OUString	sUserName		;
182 		OUString	sPassword		;
183 		OUString	sServer  		;
184 		OUString	sConnectionType	;
185 		sal_Int32	nPort=0			;	// We need this default if follow "if"-statement "failed"!
186 
187 		// If user say "OK" ... get values from dialog.
188 		// If user say "NO" ... leave it. Then we save empty informations later ...
189 		if( bDecision == sal_True )
190 		{
191 			// defines PROPERTYNAME... comes from logindialog.hxx!
192 			xPropertySet->getPropertyValue( PROPERTYNAME_USERNAME		) >>= sUserName 		;
193 			xPropertySet->getPropertyValue( PROPERTYNAME_PASSWORD		) >>= sPassword 		;
194 			xPropertySet->getPropertyValue( PROPERTYNAME_SERVER			) >>= sServer   		;
195 			xPropertySet->getPropertyValue( PROPERTYNAME_CONNECTIONTYPE	) >>= sConnectionType	;
196 			if( sConnectionType.getLength() > 0 )
197 			{
198 				xPropertySet->getPropertyValue( sConnectionType ) >>= nPort;
199 			}
200 		}
201 
202 		// Build string for output.
203 		// At this point it doesnt matter if information exist or not!
204         // Format of output: "<decision>    [0|1]       SEPERATOR
205         //                    <username>    [string]    SEPERATOR
206         //                    <password>    [string]    SEPERATOR
207         //                    <servername>  [string]    SEPERATOR
208         //                    <port>        [int]       SEPERATOR"
209 		OUStringBuffer sBuffer( 1000 );
210 
211 		if( bDecision == sal_True )
212 		{
213 			sBuffer.appendAscii( "1" );
214 		}
215 		else
216 		{
217 			sBuffer.appendAscii( "0" );
218 		}
219         sBuffer.appendAscii ( SEPERATOR         );
220 		sBuffer.append		( sUserName			);
221         sBuffer.appendAscii ( SEPERATOR         );
222 		sBuffer.append		( sPassword			);
223         sBuffer.appendAscii ( SEPERATOR         );
224 		sBuffer.append		( sServer			);
225         sBuffer.appendAscii ( SEPERATOR         );
226 		sBuffer.append		( sConnectionType	);
227         sBuffer.appendAscii ( SEPERATOR         );
228 		sBuffer.append		( nPort				);
229         sBuffer.appendAscii ( SEPERATOR         );
230 
231 		// Write informations in temp. file.
232 		// If given file name isnt valid ... caller will have a problem!!!
233 		// If fil already exist (That's out of specification!!!) we overwrite it everytime.
234 		FILE* pFile = fopen( m_sTempFile.getStr(), "w" );
235 		LOG_ASSERT( !(pFile==NULL), "LoginApplication::Main()\nCould not open file!\n" );
236 		if( pFile != NULL )
237 		{
238 			OString sEncodedOut = U2B_ENC( sBuffer.makeStringAndClear(), TEMPFILE_ENCODING );
239 			fprintf( pFile, sEncodedOut.getStr()	);
240 			fclose ( pFile							);
241 		}
242 	}
243 }
244 
245 //*****************************************************************************************************************
246 //	private method
247 //*****************************************************************************************************************
impl_parseCommandline()248 void LoginApplication::impl_parseCommandline()
249 {
250 	// Use vos::OStartupInfo for access to command line.
251 	// Step over all arguments, search for supported ones and try to get his values.
252 	// Set it on our member. Caller of this method must control setted values.
253 	OStartupInfo aInfo;
254 
255 	sal_uInt32	nCount		=	aInfo.getCommandArgCount()	;
256 	sal_uInt32	nArgument	=	0							;
257 	OUString	sArgument									;
258     OUString    sValue                                      ;
259 
260 	// Warn programmer if argument count isnt ok!
261     LOG_ASSERT( !(nCount!=MINARGUMENTCOUNT), "LoginApplication::impl_parseCommandline()\nWrong argument count detected!\n" )
262 
263     // Reset all possible argument variables to defaults if someone is missing.
264     m_sTempFile     = OString();
265     m_nParentHandle = 0        ;
266 
267 	// Step over all arguments ...
268 	for( nArgument=0; nArgument<nCount; ++nArgument )
269 	{
270 		// .. but work with valid ones only!
271 		// Don't check values here. Caller of this method must decide between wrong and allowed values!
272 		aInfo.getCommandArg( nArgument, sArgument );
273 
274 		//_____________________________________________________________________________________________________
275         // Look for "-f=<temp. file name>"
276         if( sArgument.compareTo( ARGUMENT_TEMPFILE, ARGUMENTLENGTH ) == ARGUMENTFOUND )
277 		{
278             sValue      = sArgument.copy( ARGUMENTLENGTH );
279             m_sTempFile = U2B(sValue);
280 		}
281         else
282 		//_____________________________________________________________________________________________________
283         // Look for "-p=<parent window handle>"
284         if( sArgument.compareTo( ARGUMENT_DIALOGPARENT, ARGUMENTLENGTH ) == ARGUMENTFOUND )
285 		{
286             sValue          = sArgument.copy( ARGUMENTLENGTH );
287             m_nParentHandle = sValue.toInt32();
288 		}
289 	}
290 
291     // Parent window handle is an optional argument ... but should be used mostly!
292     // Warn programmer.
293     LOG_ASSERT( !(m_nParentHandle==0), "Login.exe\nYou should give me a parent window handle!\n" )
294 }
295