File: ntpdll.c

package info (click to toggle)
xntp3 5.93-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,644 kB
  • ctags: 7,419
  • sloc: ansic: 59,474; perl: 3,633; sh: 2,623; awk: 417; makefile: 311; asm: 37
file content (365 lines) | stat: -rw-r--r-- 8,934 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * File: ntpdll.c
 * Purpose: DLL based helper functions for InstallShield SDK
 *
 */

/* includes */
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../libntp/log.h"


// ---------------------------------------------------------------------------------------
SC_HANDLE schService;
SC_HANDLE ntpService;

// ---------------------------------------------------------------------------------------

LONG APIENTRY CreateNTPService(HWND hwnd, LPLONG lpIValue, LPSTR serviceexe)
{
  LPCTSTR serviceName = "NetworkTimeProtocol";
  LPCTSTR lpszBinaryPathName = serviceexe;
  LPTSTR  lpszRootPathName="?:\\";
  HANDLE hSCManager = NULL;

  if ( (':' != *(lpszBinaryPathName+1)) || ('\\' != *(lpszBinaryPathName+2)) )
  {
    return 101;
  }

  #define DRIVE_TYPE_INDETERMINATE 0
  #define ROOT_DIR_DOESNT_EXIST    1

  *lpszRootPathName = *(lpszBinaryPathName+0) ;

  switch (  GetDriveType(lpszRootPathName)  )
  {
    case DRIVE_FIXED :
    { // OK
      break;
    }
    case  ROOT_DIR_DOESNT_EXIST :
    {
      return 101;
    }
    case  DRIVE_TYPE_INDETERMINATE :
    case  DRIVE_REMOVABLE          :
    case  DRIVE_REMOTE             :
    case  DRIVE_CDROM              :
    case  DRIVE_RAMDISK            :
    {
      return 101;
    }
    default :
    {
      return 101;
    }
  }

  if (INVALID_HANDLE_VALUE == CreateFile(lpszBinaryPathName,
                                         GENERIC_READ,
                                         FILE_SHARE_READ,
                                         NULL,
                                         OPEN_EXISTING,
                                         FILE_ATTRIBUTE_NORMAL,
                                         NULL))
  { 
    return 101;
  }
   
	if((hSCManager = OpenSCManager(
			NULL, 
     		NULL,
     		SC_MANAGER_ALL_ACCESS)) == NULL) {
		return 11;
	}

  schService = CreateService(
        hSCManager,                 // SCManager database
        serviceName,                // name of service
        serviceName,                // name to display (new parameter after october beta)
        SERVICE_ALL_ACCESS,         // desired access
        SERVICE_WIN32_OWN_PROCESS,  // service type
        SERVICE_AUTO_START,         // start type
        SERVICE_ERROR_NORMAL,       // error control type
        lpszBinaryPathName,         // service's binary
        NULL,                       // no load ordering group
        NULL,                       // no tag identifier
        NULL,                       // no dependencies
        NULL,                       // Local System account
        NULL);                      // null password

  if (NULL == schService)
  { switch (GetLastError())
    {
      case ERROR_ACCESS_DENIED :
      { 
        CloseServiceHandle(hSCManager);
        return 102;
        break;
      }
      case ERROR_SERVICE_EXISTS :
      {
        CloseServiceHandle(hSCManager);
        return 103;
        break;
      }
      default :
      {
      }
    }
    CloseServiceHandle(hSCManager);
    return 104;
  }
  else

  CloseServiceHandle(schService);
  CloseServiceHandle(hSCManager);

  return 0;
}

// ---------------------------------------------------------------------------------------


LONG APIENTRY RemoveNTPService(HWND hwnd, LPLONG lpIValue, LPSTR serviceName)
  {
    HANDLE hSCManager = NULL;

	if((hSCManager = OpenSCManager(
			NULL, 
     		NULL,
     		SC_MANAGER_ALL_ACCESS)) == NULL) {
		return 11;
	}

    
  ntpService = OpenService(hSCManager,serviceName,SERVICE_ALL_ACCESS);
  if (NULL == ntpService)
  { switch (GetLastError())
    {
      case ERROR_ACCESS_DENIED :
      { 
             
         CloseServiceHandle(hSCManager);
         return 102;
        break;
      }
      case ERROR_SERVICE_DOES_NOT_EXIST :
      { 
         CloseServiceHandle(hSCManager);
         return 106;
        break;
      }
      default :
       {
           CloseServiceHandle(hSCManager);
           return 105;
      }
    }
  
           CloseServiceHandle(hSCManager);
           return 105;
  }

  if (DeleteService(ntpService))
  {
   CloseServiceHandle(ntpService);
   CloseServiceHandle(hSCManager);
   return 0;
  }
  else
  { switch (GetLastError())
    {
      case ERROR_ACCESS_DENIED :
      { 
        CloseServiceHandle(ntpService);
        CloseServiceHandle(hSCManager);
        return 102;
        break;
      }
      default :
      { 
         CloseServiceHandle(ntpService);
         CloseServiceHandle(hSCManager);
         return 105;
      }
    }
   return 105;
  }
  CloseServiceHandle(ntpService);
  CloseServiceHandle(hSCManager);
 }


LONG APIENTRY addKeysToRegistry(HWND hwnd, LPLONG lpIValue, LPSTR bs)

{
  HKEY hk;                      /* registry key handle */
  BOOL bSuccess;
  UCHAR   myarray[200];
  char *lpmyarray = myarray;
  int arsize = 0;

 /* now add the depends on service key */
 
  /* Create a new key for our application */
  bSuccess = RegCreateKey(HKEY_LOCAL_MACHINE,
      "SYSTEM\\CurrentControlSet\\Services\\NetworkTimeProtocol", &hk);
  if(bSuccess != ERROR_SUCCESS)
    {
      return 1;
    }

  strcpy(lpmyarray,"TcpIp");
  lpmyarray = lpmyarray + 6;
  arsize = arsize + 6;
  strcpy(lpmyarray,"Afd");
  lpmyarray = lpmyarray + 4;
  arsize = arsize + 4;
  strcpy(lpmyarray,"LanManWorkstation");
  lpmyarray = lpmyarray + 18;
  arsize = arsize + 19;
  strcpy(lpmyarray,"\0\0");
  
  bSuccess = RegSetValueEx(hk,  /* subkey handle         */
      "DependOnService",        /* value name            */
      0,                        /* must be zero          */
      REG_MULTI_SZ,             /* value type            */
      (LPBYTE) &myarray,        /* address of value data */
      arsize);                  /* length of value data  */
   if(bSuccess != ERROR_SUCCESS)
    {
      return 1;
    }

  RegCloseKey(hk);
  return 0;
}

LONG APIENTRY StartNTPService(HWND hwnd, LPLONG lpIValue, LPSTR bs)
  {

HANDLE hSCManager = NULL, hNetworkTimeProtocol = NULL;

	if((hSCManager = OpenSCManager(
			NULL, 
     		NULL,
     		SC_MANAGER_ALL_ACCESS)) == NULL) {
		return 11;
	}

  	if((hNetworkTimeProtocol = OpenService(
       		hSCManager,                              
       		TEXT("NetworkTimeProtocol"),               
       		SERVICE_ALL_ACCESS)) == NULL) {
                CloseServiceHandle(hSCManager);
		return 12;
	}

	if(!StartService(hNetworkTimeProtocol, 0, NULL))
                 {
		 if(GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
          { 
          }
		   else
          {
                   CloseServiceHandle(hNetworkTimeProtocol);
                   CloseServiceHandle(hSCManager);
		    return 50;
		  }
		 }
		 
       CloseServiceHandle(hNetworkTimeProtocol);
       CloseServiceHandle(hSCManager);
        return 0;

}
LONG APIENTRY StopNTPService(HWND hwnd, LPLONG lpIValue, LPSTR bs)
  {


DWORD ControlCode = SERVICE_CONTROL_STOP;
HANDLE hSCManager = NULL, hNetworkTimeProtocol = NULL;
SERVICE_STATUS ServiceStatus;

	if((hSCManager = OpenSCManager(
			NULL,
     		NULL,
     		SC_MANAGER_ALL_ACCESS)) == NULL) {
		return 11;
	}

  	if((hNetworkTimeProtocol = OpenService(
       		hSCManager,                              
       		TEXT("NetworkTimeProtocol"),               
       		SERVICE_ALL_ACCESS)) == NULL) {
                CloseServiceHandle(hSCManager);
		return 12;
	}

	if (!ControlService(hNetworkTimeProtocol, ControlCode, &ServiceStatus))
         {
                CloseServiceHandle(hNetworkTimeProtocol);
                CloseServiceHandle(hSCManager);
                //dont return an error here assume it is already stopped.
		return 0;
  	 }
	
  CloseServiceHandle(hNetworkTimeProtocol);
  CloseServiceHandle(hSCManager);
  return 0;

}




LONG APIENTRY LaunchApp(HWND hwnd, LPLONG lpIValue, LPSTR cmd)
{
   BOOLEAN dowait = (BOOLEAN)lpIValue;
   //char arglist[1000];
   STARTUPINFO StartInfo;
   BOOLEAN rvalue;
   PROCESS_INFORMATION phandle;
   DWORD waitval;

    /* Set up members of STARTUPINFO structure. */

    StartInfo.cb = sizeof(STARTUPINFO);
    StartInfo.lpReserved = NULL;
    StartInfo.lpReserved2 = NULL;
    StartInfo.cbReserved2 = 0;
    StartInfo.lpDesktop = NULL;

    // set noshow in case it is a win app
      StartInfo.dwFlags = 0;

	StartInfo.lpTitle = NULL;
    StartInfo.dwX = 1000;
    StartInfo.dwY = 1000;
    StartInfo.dwXSize = 500;
    StartInfo.dwYSize = 500;
    StartInfo.dwXCountChars= 0;
    StartInfo.dwYCountChars = 0;
    StartInfo.dwFillAttribute = 0;
    StartInfo.hStdInput = NULL;
    StartInfo.hStdOutput = NULL;
    StartInfo.hStdError = NULL;

     rvalue = CreateProcess(NULL,cmd,NULL,NULL,FALSE,CREATE_SEPARATE_WOW_VDM,NULL,NULL,&StartInfo,&phandle);
	 if (rvalue == FALSE)
	   {
        return 1;
       }	   
       
 	 if (dowait)
 	   waitval = WaitForSingleObject(phandle.hProcess,INFINITE);
  	 
  	 CloseHandle(phandle.hThread);
	 CloseHandle(phandle.hProcess);
     return 0;
}