merge the formfield patch from ooo-build
[ooovba.git] / configmgr / workben / apitest / cfgadduser.cxx
blob38539074d29a40137c060e10b67d2e92c55c2809
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cfgadduser.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include <stdio.h>
35 #include <string.h>
36 #include <comphelper/stl_types.hxx>
37 #include <cppuhelper/extract.hxx>
38 #include <com/sun/star/uno/Type.hxx>
39 #include <com/sun/star/uno/TypeClass.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/util/XChangesBatch.hpp>
44 #include <com/sun/star/container/XNameContainer.hpp>
45 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46 #include <cppuhelper/servicefactory.hxx>
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::util;
52 using namespace ::com::sun::star::container;
54 #define ASCII_STRING(rtlOUString) ::rtl::OString((rtlOUString).getStr(), (rtlOUString).getLength(), RTL_TEXTENCODING_ASCII_US).getStr()
56 //=============================================================================
57 void explain(sal_Bool _bVerbose)
59 cout << "cfgadduser - adding users to a registry server\n";
60 cout << "\nusage :\n";
61 cout << "cfgadduser [-s <server> -p <port>] [-portal] [-r <registry>] [-a <sysaccount>]";
62 cout << " [-h <homedirbase>] [-pwd] <user> [<user>]*\n";
63 cout << "\nparameters\n";
64 cout << " <server> - machine where the registry server is running\n";
65 cout << " <port> - port the registry server is listening at\n";
66 cout << " <registry> - registry file to use to instantiate services. Defaulted to\n";
67 cout << " applicat.rdb\n";
68 cout << " <sysaccount> - system account to use for the newly created user(s)\n";
69 cout << " <homedirbase> - home directory base. The concret home dir of a user will\n";
70 cout << " be built by appending the the user name to the base dir.\n";
71 cout << " <user> - user name to add\n";
72 cout << " -portal - specify that the program should connect to a running portal,\n";
73 cout << " not directly to the registry server (you need a ";
74 #ifdef WIN32
75 cout << "portal.dll\n";
76 #else
77 cout << "libportal.so\n";
78 #endif
79 cout << " for this)\n";
80 cout << " In this case, <server> and <port> specify the location where\n";
81 cout << " StarPortal is running\n";
82 cout << "\n";
83 cout << "If no server is specified, the configuration proxy will try to bootstrap from\n";
84 cout << "the initialization file (";
85 #ifdef WIN32
86 cout << "sregistry.ini";
87 #else
88 cout << "sregistryrc";
89 #endif
90 cout << ")\n\n";
91 cout.flush();
94 //=============================================================================
95 #if (defined UNX) || (defined OS2)
96 int main( int argc, char * argv[] )
97 #else
98 int _cdecl main( int argc, char * argv[] )
99 #endif
101 sal_Char* pPort = NULL;
102 sal_Char* pServer = NULL;
103 sal_Char* pRegistry = NULL;
104 sal_Char* pSysAccount = NULL;
105 sal_Char* pHomeDirBase = NULL;
106 sal_Bool bPortal = sal_False;
108 ::std::vector< sal_Char* > aUsers;
110 // collect some parameters
111 sal_Char** pArgs = argv + 1;
112 for (sal_Int32 i=1; i<argc; ++i, ++pArgs)
114 sal_Char* pCurArg = *pArgs;
115 sal_Int32 nLen = strlen(pCurArg);
116 sal_Bool bInvalidArg = sal_True;
117 if (nLen && ('-' == *pCurArg))
118 { // it's a switch
119 sal_Char* pSwitch = pCurArg + 1;
120 switch (nLen)
122 case 2:
123 switch (*pSwitch)
125 case '?':
126 explain(sal_True);
127 return 1;
128 case 'h':
129 pHomeDirBase = *++pArgs;
130 ++i;
131 bInvalidArg = sal_False;
132 break;
133 case 'a':
134 pSysAccount = *++pArgs;
135 ++i;
136 bInvalidArg = sal_False;
137 break;
138 case 'p':
139 pPort = *++pArgs;
140 ++i;
141 bInvalidArg = sal_False;
142 break;
143 case 's':
144 pServer = *++pArgs;
145 ++i;
146 bInvalidArg = sal_False;
147 break;
148 case 'r':
149 pRegistry = *++pArgs;
150 ++i;
151 bInvalidArg = sal_False;
152 break;
154 break;
155 case 7:
156 if (0 == strncmp(pSwitch, "portal", 6))
158 bInvalidArg = sal_False;
159 bPortal = sal_True;
161 break;
164 else
166 if ((1 == nLen) && ('?' == *pCurArg))
168 explain(sal_True);
169 return 1;
171 else
173 bInvalidArg = sal_False;
174 aUsers.push_back(pCurArg);
177 if (bInvalidArg)
179 explain(sal_False);
180 return 1;
184 if ((!pServer && pPort) || (!pPort && pServer))
186 explain(sal_False);
187 return 1;
190 if (0 == aUsers.size())
192 explain(sal_False);
193 return 1;
196 // refine some params
197 ::rtl::OUString sHomeDirBase, sSystemAccountName;
198 if (pHomeDirBase)
200 sHomeDirBase = ::rtl::OUString::createFromAscii(pHomeDirBase);
201 if (!sHomeDirBase.getLength() || ('/' != sHomeDirBase.getStr()[sHomeDirBase.getLength() - 1]))
202 sHomeDirBase += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
204 if (pSysAccount)
205 sSystemAccountName = ::rtl::OUString::createFromAscii(pSysAccount);
209 ::rtl::OUString const sServiceRegistry = ::rtl::OUString::createFromAscii( pRegistry ? pRegistry : "applicat.rdb" );
210 Reference< XMultiServiceFactory > xORB = ::cppu::createRegistryServiceFactory(
211 sServiceRegistry,
212 ::rtl::OUString()
214 if (!xORB.is())
216 cerr << "Could not create the service factory !\n\n";
217 return 1;
220 // collect the params for the config provider
221 Sequence< Any > aProviderArgs(3 + (pServer ? 2 : 0));
222 aProviderArgs[0] = makeAny(PropertyValue(
223 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("servertype")),
225 makeAny(::rtl::OUString::createFromAscii(bPortal ? "portal" : "remote")),
226 PropertyState_DIRECT_VALUE
228 aProviderArgs[1] = makeAny(PropertyValue(
229 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user")),
231 makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Administrator"))),
232 PropertyState_DIRECT_VALUE
234 aProviderArgs[2] = makeAny(PropertyValue(
235 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("password")),
237 makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("unused"))),
238 PropertyState_DIRECT_VALUE
240 if (pServer)
242 aProviderArgs[3] = makeAny(PropertyValue(
243 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("server")),
245 makeAny(::rtl::OUString::createFromAscii(pServer)),
246 PropertyState_DIRECT_VALUE
249 sal_Int32 nPort = ::rtl::OUString::createFromAscii(pPort).toInt32();
250 aProviderArgs[4] = makeAny(PropertyValue(
251 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port")),
253 makeAny(nPort),
254 PropertyState_DIRECT_VALUE
258 Reference< XMultiServiceFactory > xCfgProvider(
259 xORB->createInstanceWithArguments(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"),
260 aProviderArgs),
261 UNO_QUERY);
262 if (!xCfgProvider.is())
264 cerr << "Could not create the configuration provider !\n\n";
265 return 3;
268 Reference< XInterface > xIFace = xCfgProvider->createInstance(
269 ::rtl::OUString::createFromAscii("com.sun.star.configuration.UserAdministration"));
270 if (!xIFace.is())
272 cerr << "Could not create the configuration provider !\n\n";
273 return 4;
276 Reference< XChangesBatch > xUserChanges(xIFace, UNO_QUERY);
277 Reference< XNameContainer > xUserContainer(xIFace, UNO_QUERY);
278 Reference< XSingleServiceFactory> xUserFactory(xIFace, UNO_QUERY);
279 if (!xUserChanges.is() || !xUserContainer.is() || !xUserFactory.is())
281 cerr << "the user admin access does not provide the necessary interfaces !\n\n";
282 return 5;
285 cout << "going to add the users ..." << endl << endl;
286 for ( ::std::vector< sal_Char* >::const_iterator aUserLoop = aUsers.begin();
287 aUserLoop != aUsers.end();
288 ++aUserLoop
291 cout << *aUserLoop << " ... ";
292 sal_Bool bHadLinebreak = sal_False;
295 Reference< XInterface > xNewUser = xUserFactory->createInstance();
297 // the user name as unicode string use more than once)
298 ::rtl::OUString sUserName = ::rtl::OUString::createFromAscii(*aUserLoop);
300 // the XNameContainer access to the Security node in the user profile data
301 Reference< XNameReplace > xSecurityDataAccess;
302 Reference< XNameAccess > xUserDataAccess(xNewUser, UNO_QUERY);
303 if (xUserDataAccess.is())
305 Any aSecurity = xUserDataAccess->getByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Security")));
306 ::cppu::extractInterface(xSecurityDataAccess, aSecurity);
309 if (!xSecurityDataAccess.is())
310 throw Exception(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("The user administration service did not provide a valid user template.")), NULL);
312 // set the home directory
313 if (sHomeDirBase.getLength())
315 ::rtl::OUString sHomeDir(sHomeDirBase);
316 sHomeDir += sUserName;
317 xSecurityDataAccess->replaceByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HomeDirectory")), makeAny(sHomeDir));
318 cout << "\n\thome dir : " << ASCII_STRING(sHomeDir) << " ... ";
319 cout.flush();
320 bHadLinebreak = sal_True;
323 if (sSystemAccountName.getLength())
325 xSecurityDataAccess->replaceByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SystemAccount")), makeAny(sSystemAccountName));
326 cout << "\n\tsystem account: " << ASCII_STRING(sSystemAccountName) << " ... ";
327 cout.flush();
328 bHadLinebreak = sal_True;
331 xUserContainer->insertByName(sUserName, makeAny(xNewUser));
332 xUserChanges->commitChanges();
333 if (bHadLinebreak)
334 cout << "\n";
335 cout << "done.\n";
336 cout.flush();
338 catch(Exception& e)
340 cout << "\n";
341 if (!bHadLinebreak)
342 cout << "\t";
343 cerr << "unable to add the user named " << *aUserLoop << endl;
344 if (!bHadLinebreak)
345 cout << "\t";
346 cerr << "(exception message: " << ::rtl::OString(e.Message.getStr(), e.Message.getLength(), RTL_TEXTENCODING_ASCII_US).getStr() << ")" << endl;
348 cout << "\n";
351 catch(Exception& e)
353 cerr << "Caught exception: " << ASCII_STRING(e.Message) << endl;
354 return 2;
357 return 0;