service.cpp 6.7 KB

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