1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cfgadduser.cxx,v $
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"
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 ";
75 cout
<< "portal.dll\n";
77 cout
<< "libportal.so\n";
79 cout
<< " for this)\n";
80 cout
<< " In this case, <server> and <port> specify the location where\n";
81 cout
<< " StarPortal is running\n";
83 cout
<< "If no server is specified, the configuration proxy will try to bootstrap from\n";
84 cout
<< "the initialization file (";
86 cout
<< "sregistry.ini";
88 cout
<< "sregistryrc";
94 //=============================================================================
95 #if (defined UNX) || (defined OS2)
96 int main( int argc
, char * argv
[] )
98 int _cdecl
main( int argc
, char * argv
[] )
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
))
119 sal_Char
* pSwitch
= pCurArg
+ 1;
129 pHomeDirBase
= *++pArgs
;
131 bInvalidArg
= sal_False
;
134 pSysAccount
= *++pArgs
;
136 bInvalidArg
= sal_False
;
141 bInvalidArg
= sal_False
;
146 bInvalidArg
= sal_False
;
149 pRegistry
= *++pArgs
;
151 bInvalidArg
= sal_False
;
156 if (0 == strncmp(pSwitch
, "portal", 6))
158 bInvalidArg
= sal_False
;
166 if ((1 == nLen
) && ('?' == *pCurArg
))
173 bInvalidArg
= sal_False
;
174 aUsers
.push_back(pCurArg
);
184 if ((!pServer
&& pPort
) || (!pPort
&& pServer
))
190 if (0 == aUsers
.size())
196 // refine some params
197 ::rtl::OUString sHomeDirBase
, sSystemAccountName
;
200 sHomeDirBase
= ::rtl::OUString::createFromAscii(pHomeDirBase
);
201 if (!sHomeDirBase
.getLength() || ('/' != sHomeDirBase
.getStr()[sHomeDirBase
.getLength() - 1]))
202 sHomeDirBase
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
205 sSystemAccountName
= ::rtl::OUString::createFromAscii(pSysAccount
);
209 ::rtl::OUString
const sServiceRegistry
= ::rtl::OUString::createFromAscii( pRegistry
? pRegistry
: "applicat.rdb" );
210 Reference
< XMultiServiceFactory
> xORB
= ::cppu::createRegistryServiceFactory(
216 cerr
<< "Could not create the service factory !\n\n";
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
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")),
254 PropertyState_DIRECT_VALUE
258 Reference
< XMultiServiceFactory
> xCfgProvider(
259 xORB
->createInstanceWithArguments(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"),
262 if (!xCfgProvider
.is())
264 cerr
<< "Could not create the configuration provider !\n\n";
268 Reference
< XInterface
> xIFace
= xCfgProvider
->createInstance(
269 ::rtl::OUString::createFromAscii("com.sun.star.configuration.UserAdministration"));
272 cerr
<< "Could not create the configuration provider !\n\n";
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";
285 cout
<< "going to add the users ..." << endl
<< endl
;
286 for ( ::std::vector
< sal_Char
* >::const_iterator aUserLoop
= aUsers
.begin();
287 aUserLoop
!= aUsers
.end();
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
) << " ... ";
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
) << " ... ";
328 bHadLinebreak
= sal_True
;
331 xUserContainer
->insertByName(sUserName
, makeAny(xNewUser
));
332 xUserChanges
->commitChanges();
343 cerr
<< "unable to add the user named " << *aUserLoop
<< endl
;
346 cerr
<< "(exception message: " << ::rtl::OString(e
.Message
.getStr(), e
.Message
.getLength(), RTL_TEXTENCODING_ASCII_US
).getStr() << ")" << endl
;
353 cerr
<< "Caught exception: " << ASCII_STRING(e
.Message
) << endl
;