]> git.sur5r.net Git - openldap/blob - libraries/liblutil/ntservice.c
fix flag on formatMessage to not allocate buffer
[openldap] / libraries / liblutil / ntservice.c
1 // ntservice.c
2 #include "portable.h"
3 #include <stdio.h>
4 #include <windows.h>
5 #include "ldap.h"
6 #include "ldap_log.h"
7 #include "ldap_pvt_thread.h"
8 #include <winsvc.h>
9 #include <sys/types.h>
10 #include <ac/string.h>
11
12 #include "ldap_defaults.h"
13 //#include "slap.h"
14
15 #include "slapdmsg.h"
16
17 #define SCM_NOTIFICATION_INTERVAL       5000
18 #define THIRTY_SECONDS                          (30 * 1000)
19
20 int       is_NT_Service = 1;    // assume this is an NT service until determined that 
21                                                                         // startup was from the command line
22
23 SERVICE_STATUS                  SLAPDServiceStatus;
24 SERVICE_STATUS_HANDLE   hSLAPDServiceStatus;
25
26 ldap_pvt_thread_cond_t  started_event,          stopped_event;
27 ldap_pvt_thread_t               start_status_tid,       stop_status_tid;
28
29 void (*stopfunc)(int);
30
31 // in main.c
32 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
33
34
35 // in wsa_err.c
36 char *WSAGetLastErrorString( void );
37
38 // in nt_err.c
39 char *GetLastErrorString( void );
40
41 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
42 {
43         HKEY            hKey;
44         DWORD           dwValue, dwDisposition;
45         SC_HANDLE       schSCManager, schService;
46
47         fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
48         if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
49         {
50                 if ((schService = CreateService( 
51                                                         schSCManager, 
52                                                         lpszServiceName, 
53                                                         TEXT("OpenLDAP Directory Service"), 
54                                                         SC_MANAGER_CREATE_SERVICE, 
55                                                         SERVICE_WIN32_OWN_PROCESS, 
56                                                         SERVICE_DEMAND_START, 
57                                                         SERVICE_ERROR_NORMAL, 
58                                                         lpszBinaryPathName, 
59                                                         NULL, NULL, NULL, NULL, NULL)) != NULL)
60                 {
61                         char regpath[132];
62                         CloseServiceHandle(schService);
63                         CloseServiceHandle(schSCManager);
64
65                         sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
66                                 lpszServiceName );
67                         // Create the registry key for event logging to the Windows NT event log.
68                         if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
69                                 regpath, 0, 
70                                 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, 
71                                 &dwDisposition) != ERROR_SUCCESS)
72                         {
73                                 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
74                                 RegCloseKey(hKey);
75                                 return(0);
76                         }
77                         if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
78                         {
79                                 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
80                                 RegCloseKey(hKey);
81                                 return(0);
82                         }
83
84                         dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
85                         if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS) 
86                         {
87                                 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
88                                 RegCloseKey(hKey);
89                                 return(0);
90                         }
91                         RegCloseKey(hKey);
92                         return(1);
93                 }
94                 else
95                 {
96                         fprintf( stderr, "CreateService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
97                         CloseServiceHandle(schSCManager);
98                         return(0);
99                 }
100         }
101         else
102                 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
103         return(0);
104 }
105
106
107 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
108 {
109         SC_HANDLE schSCManager, schService;
110
111         fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
112         if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL ) 
113         {
114                 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL) 
115                 {
116                         if ( DeleteService(schService) == TRUE) 
117                         {
118                                 CloseServiceHandle(schService);
119                                 CloseServiceHandle(schSCManager);
120                                 return(1);
121                         } else {
122                                 fprintf( stderr, "DeleteService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
123                                 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
124                                 CloseServiceHandle(schService);
125                                 CloseServiceHandle(schSCManager);
126                                 return(0);
127                         }
128                 } else {
129                         fprintf( stderr, "OpenService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
130                         CloseServiceHandle(schSCManager);
131                         return(0);
132                 }
133         }
134         else
135                 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
136         return(0);
137 }
138
139
140
141 static void *start_status_routine( void *ptr )
142 {
143         DWORD   wait_result;
144         int             done = 0;
145
146         while ( !done )
147         {
148                 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
149                 switch ( wait_result )
150                 {
151                         case WAIT_ABANDONED:
152                         case WAIT_OBJECT_0:
153                                 // the object that we were waiting for has been destroyed (ABANDONED) or
154                                 // signalled (TIMEOUT_0). We can assume that the startup process is
155                                 // complete and tell the Service Control Manager that we are now runnng
156                                 SLAPDServiceStatus.dwCurrentState       = SERVICE_RUNNING;
157                                 SLAPDServiceStatus.dwWin32ExitCode      = NO_ERROR;
158                                 SLAPDServiceStatus.dwCheckPoint++;
159                                 SLAPDServiceStatus.dwWaitHint           = 1000;
160                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
161                                 done = 1;
162                                 break;
163                         case WAIT_TIMEOUT:
164                                 // We've waited for the required time, so send an update to the Service Control 
165                                 // Manager saying to wait again.
166                                 SLAPDServiceStatus.dwCheckPoint++;
167                                 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
168                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
169                                 break;
170                         case WAIT_FAILED:
171                                 // theres been some proble with WaitForSingleObject so tell the Service
172                                 // Control Manager to wait 30 seconds before deploying its assasin and 
173                                 // then leave the thread.
174                                 SLAPDServiceStatus.dwCheckPoint++;
175                                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
176                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
177                                 done = 1;
178                                 break;
179                 }
180         }
181         ldap_pvt_thread_exit(NULL);
182         return NULL;
183 }
184
185
186
187 static void *stop_status_routine( void *ptr )
188 {
189         DWORD   wait_result;
190         int             done = 0;
191
192         while ( !done )
193         {
194                 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
195                 switch ( wait_result )
196                 {
197                         case WAIT_ABANDONED:
198                         case WAIT_OBJECT_0:
199                                 // the object that we were waiting for has been destroyed (ABANDONED) or
200                                 // signalled (TIMEOUT_0). The shutting down process is therefore complete 
201                                 // and the final SERVICE_STOPPED message will be sent to the service control
202                                 // manager prior to the process terminating.
203                                 done = 1;
204                                 break;
205                         case WAIT_TIMEOUT:
206                                 // We've waited for the required time, so send an update to the Service Control 
207                                 // Manager saying to wait again.
208                                 SLAPDServiceStatus.dwCheckPoint++;
209                                 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
210                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
211                                 break;
212                         case WAIT_FAILED:
213                                 // theres been some proble with WaitForSingleObject so tell the Service
214                                 // Control Manager to wait 30 seconds before deploying its assasin and 
215                                 // then leave the thread.
216                                 SLAPDServiceStatus.dwCheckPoint++;
217                                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
218                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
219                                 done = 1;
220                                 break;
221                 }
222         }
223         ldap_pvt_thread_exit(NULL);
224         return NULL;
225 }
226
227
228
229 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
230 {
231         switch (Opcode)
232         {
233         case SERVICE_CONTROL_STOP:
234         case SERVICE_CONTROL_SHUTDOWN:
235
236                 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0 );
237                 SLAPDServiceStatus.dwCurrentState       = SERVICE_STOP_PENDING;
238                 SLAPDServiceStatus.dwCheckPoint++;
239                 SLAPDServiceStatus.dwWaitHint           = SCM_NOTIFICATION_INTERVAL * 2;
240                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
241
242                 ldap_pvt_thread_cond_init( &stopped_event );
243                 if ( stopped_event == NULL )
244                 {
245                         // the event was not created. We will ask the service control manager for 30
246                         // seconds to shutdown
247                         SLAPDServiceStatus.dwCheckPoint++;
248                         SLAPDServiceStatus.dwWaitHint           = THIRTY_SECONDS;
249                         SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
250                 }
251                 else
252                 {
253                         // start a thread to report the progress to the service control manager 
254                         // until the stopped_event is fired.
255                         if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
256                         {
257                                 
258                         }
259                         else {
260                                 // failed to create the thread that tells the Service Control Manager that the
261                                 // service stopping is proceeding. 
262                                 // tell the Service Control Manager to wait another 30 seconds before deploying its
263                                 // assasin.
264                                 SLAPDServiceStatus.dwCheckPoint++;
265                                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
266                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
267                         }
268                 }
269                 stopfunc( -1 );
270                 break;
271
272         case SERVICE_CONTROL_INTERROGATE:
273                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
274                 break;
275         }
276         return;
277 }
278
279
280 void *getRegParam( char *svc, char *value )
281 {
282         HANDLE hkey;
283         char path[255];
284         int i = 0, rc;
285         char vName[256];
286         DWORD vNameLen = 255;
287         DWORD vType;
288         static char vValue[1024];
289         DWORD valLen = 1024;
290
291         if ( svc != NULL )
292                 sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
293         else
294                 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
295
296         if( (rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
297                 path, 0, KEY_READ, &hkey )) != ERROR_SUCCESS )
298         {
299                 //Debug( LDAP_DEBUG_ANY, "%s\n", GetLastErrorString(), 0, 0 );
300                 return NULL;
301         }
302
303         while ( !RegEnumValue( hkey, i, vName, &vNameLen, NULL,
304                 &vType, vValue, &valLen ) )
305         {
306                 if ( !strcasecmp( value, vName ) )
307                 {
308                         switch ( vType )
309                         {
310                         case REG_BINARY:
311                         case REG_DWORD:
312                                 return (void*)&vValue;
313                         case REG_SZ:
314                                 return (void*)strdup( vValue );
315                         }
316                 }
317                 i++;
318                 vNameLen = 255;
319                 valLen = 1024;
320         }
321         return (void*)NULL;
322 }
323
324 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, short port, int udp )
325 {
326         char *Inserts[5];
327         WORD i = 0, j;
328         HANDLE hEventLog;
329         
330         hEventLog = RegisterEventSource( NULL, svc );
331
332         Inserts[i] = (char *)malloc( 20 );
333         itoa( slap_debug, Inserts[i++], 10 );
334         Inserts[i++] = ldap_pvt_strdup( configfile );
335         Inserts[i] = (char *)malloc( 20 );
336         itoa( port, Inserts[i++], 10 );
337         Inserts[i++] = ldap_pvt_strdup( udp ? "udp" : "tcp" );
338         Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
339
340         ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0, MSG_SLAPD_STARTED, NULL, i, 0, Inserts, NULL );
341
342         for ( j = 0; j < i; j++ )
343                 ldap_memfree( Inserts[j] );
344         DeregisterEventSource( hEventLog );
345 }
346
347
348
349 void LogSlapdStoppedEvent( char *svc )
350 {
351         HANDLE hEventLog;
352         
353         hEventLog = RegisterEventSource( NULL, svc );
354         ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0, MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
355         DeregisterEventSource( hEventLog );
356 }
357
358
359 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
360                                                            void (*stopper)(int) )
361 {
362         hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
363
364         stopfunc = stopper;
365
366         /* initialize the Service Status structure */
367         SLAPDServiceStatus.dwServiceType                                = SERVICE_WIN32_OWN_PROCESS;
368         SLAPDServiceStatus.dwCurrentState                               = SERVICE_START_PENDING;
369         SLAPDServiceStatus.dwControlsAccepted                   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
370         SLAPDServiceStatus.dwWin32ExitCode                              = NO_ERROR;
371         SLAPDServiceStatus.dwServiceSpecificExitCode    = 0;
372         SLAPDServiceStatus.dwCheckPoint                                 = 1;
373         SLAPDServiceStatus.dwWaitHint                                   = SCM_NOTIFICATION_INTERVAL * 2;
374
375         SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
376
377         // start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
378         // until the slapd listener is completed and listening. Only then should we send 
379         // SERVICE_RUNNING to the Service Control Manager.
380         ldap_pvt_thread_cond_init( &started_event );
381         if ( started_event == NULL)
382         {
383                 // failed to create the event to determine when the startup process is complete so
384                 // tell the Service Control Manager to wait another 30 seconds before deploying its
385                 // assasin
386                 SLAPDServiceStatus.dwCheckPoint++;
387                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
388                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
389         }
390         else
391         {
392                 // start a thread to report the progress to the service control manager 
393                 // until the started_event is fired.
394                 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
395                 {
396                         
397                 }
398                 else {
399                         // failed to create the thread that tells the Service Control Manager that the
400                         // service startup is proceeding. 
401                         // tell the Service Control Manager to wait another 30 seconds before deploying its
402                         // assasin.
403                         SLAPDServiceStatus.dwCheckPoint++;
404                         SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
405                         SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
406                 }
407         }
408 }
409
410 void ReportSlapdShutdownComplete(  )
411 {
412         if ( is_NT_Service )
413         {
414                 // stop sending SERVICE_STOP_PENDING messages to the Service Control Manager
415                 ldap_pvt_thread_cond_signal( &stopped_event );
416                 ldap_pvt_thread_cond_destroy( &stopped_event );
417
418                 // wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
419                 // if the wait fails then put ourselves to sleep for half the Service Control Manager update interval
420                 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
421                         ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
422
423                 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
424                 SLAPDServiceStatus.dwCheckPoint++;
425                 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
426                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
427         }
428 }