service.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. // Created: 2-8-2005
  38. // Author(s): Scott Bridges
  39. #include <windows.h>
  40. #include <iostream>
  41. #include "syncserv.h"
  42. #include "dssynchmsg.h"
  43. // syncserv.h
  44. // ntservice.h (modified)
  45. // ntservice.cpp (modified)
  46. // sysplat
  47. // uniutil
  48. // dssynchmsg
  49. // Copied: 2-10-2005
  50. // From: ntsynch.cpp
  51. //#include "sysplat.h"
  52. //#include "NTSynch.h"
  53. //#include "ldapconn.h"
  54. //#include "dsperson.h"
  55. #include "synchcmds.h"
  56. #include "dssynch.h"
  57. #ifdef _DEBUG
  58. void doDebug( PassSyncService *pSynch );
  59. #endif // _DEBUG
  60. //#include <nspr.h>
  61. /////////////////////////////////////////////////////////////////
  62. static void usage()
  63. {
  64. printf( "DS Synchronization Service version %s\n",
  65. SYNCH_VERSION );
  66. printf( " -%c NUMBER NT polling interval (minutes)\n",
  67. SYNCH_CMD_NT_POLL_INTERVAL );
  68. // printf( " -%c NUMBER DS polling interval (minutes)\n",
  69. // SYNCH_CMD_DS_POLL_INTERVAL );
  70. // printf( " -%c CALENDAR NT update schedule\n",
  71. // SYNCH_CMD_NT_CALENDAR );
  72. // printf( " -%c CALENDAR DS update schedule\n",
  73. // SYNCH_CMD_DS_CALENDAR );
  74. printf( " -%c NUMBER NT synchronization start time (minutes)\n",
  75. SYNCH_CMD_NT_CALENDAR );
  76. // printf( " -%c NUMBER DS synchronization start time (minutes)\n",
  77. // SYNCH_CMD_DS_CALENDAR );
  78. printf( " -%c NAME DS distinguished name\n",
  79. SYNCH_CMD_ADMIN_DN );
  80. printf( " -%c USERS_BASE DS users base\n",
  81. SYNCH_CMD_DIRECTORY_USERS_BASE );
  82. printf( " -%c GROUPS_BASE DS groups base\n",
  83. SYNCH_CMD_DIRECTORY_GROUPS_BASE );
  84. printf( " -%c HOST DS host\n",
  85. SYNCH_CMD_DS_HOST );
  86. printf( " -%c NUMBER DS port\n",
  87. SYNCH_CMD_DS_PORT );
  88. printf( " -%c PASSWORD DS password\n",
  89. SYNCH_CMD_ADMIN_PASSWORD );
  90. printf( " -%c NUMBER Command port\n",
  91. SYNCH_CMD_NT_PORT );
  92. printf( " -v Display this message\n",
  93. SYNCH_CMD_NT_POLL_INTERVAL );
  94. printf( " -i Install the service\n" );
  95. printf( " -u Uninstall the service\n" );
  96. printf( " -%c Synchronize all NT users to DS now\n",
  97. SYNCH_CMD_SYNCH_FROM_NT );
  98. // printf( " -%c Synchronize DS users to NT now\n",
  99. // SYNCH_CMD_SYNCH_FROM_DS );
  100. printf( " -%c Resynchronize changes to NT users now\n",
  101. SYNCH_CMD_SYNCH_CHANGES );
  102. printf( " -%c Load settings from Registry\n",
  103. SYNCH_CMD_RELOAD_SETTINGS );
  104. // printf( "Options -t and -k are contradictory, as are -m and -y\n" );
  105. }
  106. #define OPT_NONE 0
  107. #define OPT_START 1
  108. #define OPT_APP 2
  109. #define OPT_TERMINATE 3
  110. #define OPT_START_DIRECT 4
  111. /////////////////////////////////////////////////////////////////
  112. static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] )
  113. {
  114. int result = OPT_START; // Default is to start the service
  115. // Check first for uninstall, since we shouldn't do anything else if set
  116. int i;
  117. for( i = 1; i < argc; i++ )
  118. {
  119. if ( !strncmp( argv[i], "-u", 2 ) )
  120. {
  121. // Uninstall
  122. if ( !pSynch->IsInstalled() )
  123. wprintf( L"%s is not installed\n", pSynch->m_szServiceName );
  124. else
  125. {
  126. // Try and remove the copy that's installed
  127. if ( pSynch->Uninstall() )
  128. wprintf( L"%s removed\n", pSynch->m_szServiceName );
  129. else
  130. wprintf( L"Could not remove %s. Error %d\n",
  131. pSynch->m_szServiceName, GetLastError() );
  132. // pSynch->ClearRegistry();
  133. }
  134. // Terminate after completion
  135. result = OPT_TERMINATE;
  136. argc = 1;
  137. return result;
  138. }
  139. }
  140. // Check command-line arguments
  141. for( i = 1; i < argc; )
  142. {
  143. if ( '-' != argv[i][0] )
  144. {
  145. i++;
  146. continue;
  147. }
  148. char opt = argv[i][1];
  149. BOOL bLocal = FALSE;
  150. // Usage
  151. if ( 'v' == opt )
  152. {
  153. result = OPT_NONE;
  154. usage();
  155. bLocal = TRUE;
  156. }
  157. // Secret option to start as app, not service
  158. else if ( 'a' == opt )
  159. {
  160. result = OPT_APP;
  161. bLocal = TRUE;
  162. }
  163. // Start service
  164. else if ( 'x' == opt )
  165. {
  166. result = OPT_START_DIRECT;
  167. bLocal = TRUE;
  168. }
  169. /*
  170. // Command port
  171. else if ( 'c' == opt )
  172. {
  173. result = OPT_NONE;
  174. if ( i < (argc-1) )
  175. {
  176. i++;
  177. pSynch->SetCommandPort( atoi( argv[i] ) );
  178. bLocal = TRUE;
  179. }
  180. }
  181. */
  182. // Install
  183. else if ( 'i' == opt )
  184. {
  185. result = OPT_NONE;
  186. if ( pSynch->IsInstalled() )
  187. printf( "%S is already installed\n", pSynch->m_szServiceName );
  188. else
  189. {
  190. // Try and install the copy that's running
  191. if ( pSynch->Install() )
  192. {
  193. printf( "%S installed\n", pSynch->m_szServiceName );
  194. }
  195. else
  196. {
  197. printf( "%S failed to install. Error %d\n",
  198. pSynch->m_szServiceName, GetLastError() );
  199. }
  200. }
  201. bLocal = TRUE;
  202. }
  203. // Synchronize from NT to DS
  204. // Terminate after completion
  205. else if ( 'n' == opt )
  206. {
  207. result = OPT_NONE;
  208. }
  209. // Synchronize from DS to NT
  210. // Terminate after completion
  211. else if ( 's' == opt )
  212. {
  213. result = OPT_NONE;
  214. }
  215. // Synchronize both ways
  216. // Terminate after completion
  217. else if ( 'r' == opt )
  218. {
  219. result = OPT_NONE;
  220. }
  221. if ( bLocal )
  222. {
  223. for( int j = i; j < (argc-1); j++ )
  224. {
  225. argv[j] = argv[j+1];
  226. }
  227. argc--;
  228. }
  229. else
  230. {
  231. i++;
  232. if ( i >= argc )
  233. break;
  234. }
  235. }
  236. return result;
  237. }
  238. static int initialize( PassSyncService *pSynch, int argc, char *argv[] )
  239. {
  240. // Check command-line arguments
  241. for( int i = 1; i < argc; i++ )
  242. {
  243. if ( '-' != argv[i][0] )
  244. continue;
  245. char opt = argv[i][1];
  246. // pSynch->argToSynch( opt, argv[i] );
  247. if ( i >= argc )
  248. break;
  249. }
  250. if ( argc > 1 )
  251. {
  252. // Save settings to Registry
  253. // pSynch->SaveConfig();
  254. }
  255. return 0;
  256. }
  257. /////////////////////////////////////////////////////////////////
  258. int
  259. main( int argc, char *argv[] )
  260. {
  261. // Global single instance
  262. PassSyncService theSynch("Password Synchronization Service");
  263. // Process special install/uninstall switches; this does install/uninstall
  264. // It returns non-zero to actually start the service
  265. int nStart = checkOptions( &theSynch, argc, argv );
  266. // Set up configuration
  267. if ( OPT_TERMINATE != nStart )
  268. initialize( &theSynch, argc, argv );
  269. // Started by Service Control Manager
  270. if ( OPT_START == nStart )
  271. {
  272. // Start the service; doesn't return until the service is started
  273. BOOL bStarted = theSynch.StartService();
  274. if ( !bStarted )
  275. {
  276. printf( "Service could not be started\n" );
  277. return(1);
  278. }
  279. return 0;
  280. }
  281. #if 0
  282. // Started from command line
  283. else if ( OPT_START_DIRECT == nStart )
  284. {
  285. // This may fail, but the rest still succeeds
  286. BOOL bStarted = theSynch.StartService();
  287. bStarted = theSynch.StartServiceDirect();
  288. if ( !bStarted )
  289. {
  290. printf( "Service could not be started\n" );
  291. return(1);
  292. }
  293. return 0;
  294. }
  295. #endif
  296. // Secret debugging option - run as app instead of as service
  297. else if ( OPT_APP == nStart )
  298. {
  299. if ( theSynch.OnInit() )
  300. theSynch.Run();
  301. }
  302. exit(theSynch.m_Status.dwWin32ExitCode);
  303. ////////// That's it - the rest is debugging stuff //////
  304. #ifdef _DEBUG
  305. doDebug( &theSynch );
  306. #endif
  307. return 0;
  308. }