~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ports/winnt/ntpd/ntservice.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2009-01-05 21:10:03 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090105211003-mh6zc3um4k1uhsj7
Tags: 1:4.2.4p4+dfsg-8
It did not properly check the return value of EVP_VerifyFinal
which results in an malformed DSA signature being treated as
a good signature rather than as an error.  (CVE-2009-0021)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
 
3
 * Copyright (C) 1999-2002  Internet Software Consortium.
 
4
 *
 
5
 * Permission to use, copy, modify, and distribute this software for any
 
6
 * purpose with or without fee is hereby granted, provided that the above
 
7
 * copyright notice and this permission notice appear in all copies.
 
8
 *
 
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 
10
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 
11
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 
12
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 
13
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 
14
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
15
 * PERFORMANCE OF THIS SOFTWARE.
 
16
 */
 
17
 
 
18
/* $Id: ntservice.c,v 1.3.2.1.10.3 2004/03/08 04:04:22 marka Exp $ */
 
19
 
 
20
#include <config.h>
 
21
#include <stdio.h>
 
22
 
 
23
#include <ntp_cmdargs.h>
 
24
#include <ntp_stdlib.h>
 
25
#include "syslog.h"
 
26
#include "ntservice.h"
 
27
#include "clockstuff.h"
 
28
#include "ntp_iocompletionport.h"
 
29
#ifdef DEBUG
 
30
#include <crtdbg.h>
 
31
#endif
 
32
 
 
33
/* Handle to SCM for updating service status */
 
34
static SERVICE_STATUS_HANDLE hServiceStatus = 0;
 
35
static BOOL foreground = FALSE;
 
36
static char ConsoleTitle[128];
 
37
static int glb_argc;
 
38
static char **glb_argv;
 
39
HANDLE hServDoneEvent = NULL;
 
40
extern volatile int debug;
 
41
extern char *progname;
 
42
 
 
43
void uninit_io_completion_port();
 
44
int ntpdmain(int argc, char *argv[]);
 
45
/*
 
46
 * Forward declarations
 
47
 */
 
48
void ServiceControl(DWORD dwCtrlCode);
 
49
void ntservice_exit(void);
 
50
 
 
51
void WINAPI service_main( DWORD argc, LPTSTR *argv )
 
52
{
 
53
  /* pass the global command line options on to the service */
 
54
  ntpdmain( glb_argc, glb_argv );
 
55
}
 
56
 
 
57
 
 
58
/*
 
59
 * This is the entry point for the executable 
 
60
 * We can call ntpdmain() explicitly or via StartServiceCtrlDispatcher()
 
61
 * as we need to.
 
62
 */
 
63
int main( int argc, char *argv[] )
 
64
{
 
65
        int rc;
 
66
        int i = 1;
 
67
 
 
68
        /* Save the command line parameters */
 
69
        glb_argc = argc;
 
70
        glb_argv = argv;
 
71
 
 
72
        /* Command line users should put -f in the options */
 
73
        while (argv[i]) {
 
74
                if (!_strnicmp(argv[i], "-d", 2) ||
 
75
                        !strcmp(argv[i], "-q") ||
 
76
                        !strcmp(argv[i], "--help") ||
 
77
                        !strcmp(argv[i], "-n")) {
 
78
                        foreground = TRUE;
 
79
                        break;
 
80
                }
 
81
                i++;
 
82
        }
 
83
 
 
84
        if (foreground) {
 
85
                /* run in console window */
 
86
                exit(ntpdmain(argc, argv));
 
87
        } else {
 
88
                /* Start up as service */
 
89
 
 
90
                SERVICE_TABLE_ENTRY dispatchTable[] = {
 
91
                        { TEXT(NTP_DISPLAY_NAME), (LPSERVICE_MAIN_FUNCTION) service_main },
 
92
                        { NULL, NULL }
 
93
                };
 
94
 
 
95
                rc = StartServiceCtrlDispatcher(dispatchTable);
 
96
                if (!rc) {
 
97
                        progname = argv[0];
 
98
                        rc = GetLastError();
 
99
#ifdef DEBUG
 
100
                        fprintf(stderr, "%s: unable to start as service, rc: %i\n\n", progname, rc);
 
101
#endif
 
102
                        fprintf(stderr, "\nUse -d, -q, --help or -n to run from the command line.\n");
 
103
                        exit(rc);
 
104
                }
 
105
        }
 
106
        exit(0);
 
107
}
 
108
 
 
109
/*
 
110
 * Initialize the Service by registering it.
 
111
 */
 
112
void
 
113
ntservice_init() {
 
114
        if (!foreground) {
 
115
                /* Register handler with the SCM */
 
116
                hServiceStatus = RegisterServiceCtrlHandler(NTP_DISPLAY_NAME,
 
117
                                        (LPHANDLER_FUNCTION)ServiceControl);
 
118
                if (!hServiceStatus) {
 
119
                        NTReportError(NTP_SERVICE_NAME,
 
120
                                "could not register service control handler");
 
121
                        UpdateSCM(SERVICE_STOPPED);
 
122
                        exit(1);
 
123
                }
 
124
                UpdateSCM(SERVICE_RUNNING);
 
125
        } else {
 
126
                strcpy(ConsoleTitle, "NTP Version ");
 
127
                strcat(ConsoleTitle, Version);
 
128
                SetConsoleTitle(ConsoleTitle);
 
129
        }
 
130
 
 
131
        atexit( ntservice_exit );
 
132
}
 
133
/*
 
134
 * Routine to check if this is a service or a foreground program
 
135
 */
 
136
BOOL
 
137
ntservice_isservice() {
 
138
        return(!foreground);
 
139
}
 
140
/* service_ctrl - control handler for NTP service
 
141
 * signals the service_main routine of start/stop requests
 
142
 * from the control panel or other applications making
 
143
 * win32API calls
 
144
 */
 
145
void
 
146
ntservice_exit( void )
 
147
{
 
148
 
 
149
        if (!foreground) { /* did not become a service, simply exit */
 
150
                /* service mode, need to have the service_main routine
 
151
                 * register with the service control manager that the 
 
152
                 * service has stopped running, before exiting
 
153
                 */
 
154
                UpdateSCM(SERVICE_STOPPED);
 
155
        }
 
156
        uninit_io_completion_port();
 
157
        Sleep( 200 );   //##++ 
 
158
 
 
159
        reset_winnt_time();
 
160
 
 
161
        msyslog(LOG_INFO, "ntservice: The Network Time Protocol Service has stopped.");
 
162
 
 
163
# ifdef DEBUG
 
164
        _CrtDumpMemoryLeaks();
 
165
# endif 
 
166
}
 
167
 
 
168
/* 
 
169
 * ServiceControl(): Handles requests from the SCM and passes them on
 
170
 * to the service.
 
171
 */
 
172
void
 
173
ServiceControl(DWORD dwCtrlCode) {
 
174
        /* Handle the requested control code */
 
175
        HANDLE exitEvent = get_exit_event();
 
176
 
 
177
        switch(dwCtrlCode) {
 
178
 
 
179
        case SERVICE_CONTROL_SHUTDOWN:
 
180
        case SERVICE_CONTROL_STOP:
 
181
                UpdateSCM(SERVICE_STOP_PENDING);
 
182
                if (exitEvent != NULL) {
 
183
                        SetEvent(exitEvent);
 
184
                        Sleep( 100 );  //##++
 
185
                }
 
186
                return;
 
187
 
 
188
        case SERVICE_CONTROL_PAUSE:
 
189
        case SERVICE_CONTROL_CONTINUE:
 
190
        case SERVICE_CONTROL_INTERROGATE:
 
191
        default:
 
192
                break;
 
193
        }
 
194
        UpdateSCM(SERVICE_RUNNING);
 
195
}
 
196
 
 
197
/*
 
198
 * Tell the Service Control Manager the state of the service.
 
199
 */
 
200
void UpdateSCM(DWORD state) {
 
201
        SERVICE_STATUS ss;
 
202
        static DWORD dwState = SERVICE_STOPPED;
 
203
 
 
204
        if (hServiceStatus) {
 
205
                if (state)
 
206
                        dwState = state;
 
207
 
 
208
                memset(&ss, 0, sizeof(SERVICE_STATUS));
 
209
                ss.dwServiceType |= SERVICE_WIN32_OWN_PROCESS;
 
210
                ss.dwCurrentState = dwState;
 
211
                ss.dwControlsAccepted = SERVICE_ACCEPT_STOP |
 
212
                                        SERVICE_ACCEPT_SHUTDOWN;
 
213
                ss.dwCheckPoint = 0;
 
214
                ss.dwServiceSpecificExitCode = 0;
 
215
                ss.dwWin32ExitCode = NO_ERROR;
 
216
                ss.dwWaitHint = dwState == SERVICE_STOP_PENDING ? 5000 : 1000;
 
217
 
 
218
                if (!SetServiceStatus(hServiceStatus, &ss)) {
 
219
                        ss.dwCurrentState = SERVICE_STOPPED;
 
220
                        SetServiceStatus(hServiceStatus, &ss);
 
221
                }
 
222
        }
 
223
}
 
224
 
 
225
BOOL WINAPI 
 
226
OnConsoleEvent(  
 
227
        DWORD dwCtrlType
 
228
        )
 
229
{
 
230
        HANDLE exitEvent = get_exit_event();
 
231
 
 
232
        switch (dwCtrlType) {
 
233
#ifdef DEBUG
 
234
                case CTRL_BREAK_EVENT :
 
235
                        if (debug > 0) {
 
236
                                debug <<= 1;
 
237
                        }
 
238
                        else {
 
239
                                debug = 1;
 
240
                        }
 
241
                        if (debug > 8) {
 
242
                                debug = 0;
 
243
                        }
 
244
                        printf("debug level %d\n", debug);
 
245
                break ;
 
246
#endif
 
247
 
 
248
                case CTRL_C_EVENT  :
 
249
                case CTRL_CLOSE_EVENT :
 
250
                case CTRL_SHUTDOWN_EVENT :
 
251
                        if (exitEvent != NULL) {
 
252
                                SetEvent(exitEvent);
 
253
                                Sleep( 100 );  //##++
 
254
                        }
 
255
                break;
 
256
 
 
257
                default :
 
258
                        return FALSE;
 
259
 
 
260
 
 
261
        }
 
262
        return TRUE;;
 
263
}
 
264