service.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2005 Red Hat, Inc.
  3. * All rights reserved.
  4. * --- END COPYRIGHT BLOCK --- */
  5. // Created: 2-8-2005
  6. // Author(s): Scott Bridges
  7. #include <windows.h>
  8. #include <iostream>
  9. #include "syncserv.h"
  10. #include "dssynchmsg.h"
  11. // syncserv.h
  12. // ntservice.h (modified)
  13. // ntservice.cpp (modified)
  14. // sysplat
  15. // uniutil
  16. // dssynchmsg
  17. // Copied: 2-10-2005
  18. // From: ntsynch.cpp
  19. //#include "sysplat.h"
  20. //#include "NTSynch.h"
  21. //#include "ldapconn.h"
  22. //#include "dsperson.h"
  23. #include "synchcmds.h"
  24. #include "dssynch.h"
  25. #ifdef _DEBUG
  26. void doDebug( PassSyncService *pSynch );
  27. #endif // _DEBUG
  28. //#include <nspr.h>
  29. /////////////////////////////////////////////////////////////////
  30. static void usage()
  31. {
  32. printf( "DS Synchronization Service version %s\n",
  33. SYNCH_VERSION );
  34. printf( " -%c NUMBER NT polling interval (minutes)\n",
  35. SYNCH_CMD_NT_POLL_INTERVAL );
  36. // printf( " -%c NUMBER DS polling interval (minutes)\n",
  37. // SYNCH_CMD_DS_POLL_INTERVAL );
  38. // printf( " -%c CALENDAR NT update schedule\n",
  39. // SYNCH_CMD_NT_CALENDAR );
  40. // printf( " -%c CALENDAR DS update schedule\n",
  41. // SYNCH_CMD_DS_CALENDAR );
  42. printf( " -%c NUMBER NT synchronization start time (minutes)\n",
  43. SYNCH_CMD_NT_CALENDAR );
  44. // printf( " -%c NUMBER DS synchronization start time (minutes)\n",
  45. // SYNCH_CMD_DS_CALENDAR );
  46. printf( " -%c NAME DS distinguished name\n",
  47. SYNCH_CMD_ADMIN_DN );
  48. printf( " -%c USERS_BASE DS users base\n",
  49. SYNCH_CMD_DIRECTORY_USERS_BASE );
  50. printf( " -%c GROUPS_BASE DS groups base\n",
  51. SYNCH_CMD_DIRECTORY_GROUPS_BASE );
  52. printf( " -%c HOST DS host\n",
  53. SYNCH_CMD_DS_HOST );
  54. printf( " -%c NUMBER DS port\n",
  55. SYNCH_CMD_DS_PORT );
  56. printf( " -%c PASSWORD DS password\n",
  57. SYNCH_CMD_ADMIN_PASSWORD );
  58. printf( " -%c NUMBER Command port\n",
  59. SYNCH_CMD_NT_PORT );
  60. printf( " -v Display this message\n",
  61. SYNCH_CMD_NT_POLL_INTERVAL );
  62. printf( " -i Install the service\n" );
  63. printf( " -u Uninstall the service\n" );
  64. printf( " -%c Synchronize all NT users to DS now\n",
  65. SYNCH_CMD_SYNCH_FROM_NT );
  66. // printf( " -%c Synchronize DS users to NT now\n",
  67. // SYNCH_CMD_SYNCH_FROM_DS );
  68. printf( " -%c Resynchronize changes to NT users now\n",
  69. SYNCH_CMD_SYNCH_CHANGES );
  70. printf( " -%c Load settings from Registry\n",
  71. SYNCH_CMD_RELOAD_SETTINGS );
  72. // printf( "Options -t and -k are contradictory, as are -m and -y\n" );
  73. }
  74. #define OPT_NONE 0
  75. #define OPT_START 1
  76. #define OPT_APP 2
  77. #define OPT_TERMINATE 3
  78. #define OPT_START_DIRECT 4
  79. /////////////////////////////////////////////////////////////////
  80. static int checkOptions( PassSyncService *pSynch, int& argc, char *argv[] )
  81. {
  82. int result = OPT_START; // Default is to start the service
  83. // Check first for uninstall, since we shouldn't do anything else if set
  84. int i;
  85. for( i = 1; i < argc; i++ )
  86. {
  87. if ( !strncmp( argv[i], "-u", 2 ) )
  88. {
  89. // Uninstall
  90. if ( !pSynch->IsInstalled() )
  91. wprintf( L"%s is not installed\n", pSynch->m_szServiceName );
  92. else
  93. {
  94. // Try and remove the copy that's installed
  95. if ( pSynch->Uninstall() )
  96. wprintf( L"%s removed\n", pSynch->m_szServiceName );
  97. else
  98. wprintf( L"Could not remove %s. Error %d\n",
  99. pSynch->m_szServiceName, GetLastError() );
  100. // pSynch->ClearRegistry();
  101. }
  102. // Terminate after completion
  103. result = OPT_TERMINATE;
  104. argc = 1;
  105. return result;
  106. }
  107. }
  108. // Check command-line arguments
  109. for( i = 1; i < argc; )
  110. {
  111. if ( '-' != argv[i][0] )
  112. {
  113. i++;
  114. continue;
  115. }
  116. char opt = argv[i][1];
  117. BOOL bLocal = FALSE;
  118. // Usage
  119. if ( 'v' == opt )
  120. {
  121. result = OPT_NONE;
  122. usage();
  123. bLocal = TRUE;
  124. }
  125. // Secret option to start as app, not service
  126. else if ( 'a' == opt )
  127. {
  128. result = OPT_APP;
  129. bLocal = TRUE;
  130. }
  131. // Start service
  132. else if ( 'x' == opt )
  133. {
  134. result = OPT_START_DIRECT;
  135. bLocal = TRUE;
  136. }
  137. /*
  138. // Command port
  139. else if ( 'c' == opt )
  140. {
  141. result = OPT_NONE;
  142. if ( i < (argc-1) )
  143. {
  144. i++;
  145. pSynch->SetCommandPort( atoi( argv[i] ) );
  146. bLocal = TRUE;
  147. }
  148. }
  149. */
  150. // Install
  151. else if ( 'i' == opt )
  152. {
  153. result = OPT_NONE;
  154. if ( pSynch->IsInstalled() )
  155. printf( "%S is already installed\n", pSynch->m_szServiceName );
  156. else
  157. {
  158. // Try and install the copy that's running
  159. if ( pSynch->Install() )
  160. {
  161. printf( "%S installed\n", pSynch->m_szServiceName );
  162. }
  163. else
  164. {
  165. printf( "%S failed to install. Error %d\n",
  166. pSynch->m_szServiceName, GetLastError() );
  167. }
  168. }
  169. bLocal = TRUE;
  170. }
  171. // Synchronize from NT to DS
  172. // Terminate after completion
  173. else if ( 'n' == opt )
  174. {
  175. result = OPT_NONE;
  176. }
  177. // Synchronize from DS to NT
  178. // Terminate after completion
  179. else if ( 's' == opt )
  180. {
  181. result = OPT_NONE;
  182. }
  183. // Synchronize both ways
  184. // Terminate after completion
  185. else if ( 'r' == opt )
  186. {
  187. result = OPT_NONE;
  188. }
  189. if ( bLocal )
  190. {
  191. for( int j = i; j < (argc-1); j++ )
  192. {
  193. argv[j] = argv[j+1];
  194. }
  195. argc--;
  196. }
  197. else
  198. {
  199. i++;
  200. if ( i >= argc )
  201. break;
  202. }
  203. }
  204. return result;
  205. }
  206. static int initialize( PassSyncService *pSynch, int argc, char *argv[] )
  207. {
  208. // Check command-line arguments
  209. for( int i = 1; i < argc; i++ )
  210. {
  211. if ( '-' != argv[i][0] )
  212. continue;
  213. char opt = argv[i][1];
  214. // pSynch->argToSynch( opt, argv[i] );
  215. if ( i >= argc )
  216. break;
  217. }
  218. if ( argc > 1 )
  219. {
  220. // Save settings to Registry
  221. // pSynch->SaveConfig();
  222. }
  223. return 0;
  224. }
  225. /////////////////////////////////////////////////////////////////
  226. int
  227. main( int argc, char *argv[] )
  228. {
  229. // Global single instance
  230. PassSyncService theSynch("Password Synchronization Service");
  231. // Process special install/uninstall switches; this does install/uninstall
  232. // It returns non-zero to actually start the service
  233. int nStart = checkOptions( &theSynch, argc, argv );
  234. // Set up configuration
  235. if ( OPT_TERMINATE != nStart )
  236. initialize( &theSynch, argc, argv );
  237. // Started by Service Control Manager
  238. if ( OPT_START == nStart )
  239. {
  240. // Start the service; doesn't return until the service is started
  241. BOOL bStarted = theSynch.StartService();
  242. if ( !bStarted )
  243. {
  244. printf( "Service could not be started\n" );
  245. return(1);
  246. }
  247. return 0;
  248. }
  249. #if 0
  250. // Started from command line
  251. else if ( OPT_START_DIRECT == nStart )
  252. {
  253. // This may fail, but the rest still succeeds
  254. BOOL bStarted = theSynch.StartService();
  255. bStarted = theSynch.StartServiceDirect();
  256. if ( !bStarted )
  257. {
  258. printf( "Service could not be started\n" );
  259. return(1);
  260. }
  261. return 0;
  262. }
  263. #endif
  264. // Secret debugging option - run as app instead of as service
  265. else if ( OPT_APP == nStart )
  266. {
  267. if ( theSynch.OnInit() )
  268. theSynch.Run();
  269. }
  270. exit(theSynch.m_Status.dwWin32ExitCode);
  271. ////////// That's it - the rest is debugging stuff //////
  272. #ifdef _DEBUG
  273. doDebug( &theSynch );
  274. #endif
  275. return 0;
  276. }