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
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
/*
* snmpctrl.c - start/stop/restart LDAP-based SNMP subagent
*
* Steve Ross -- 08/12/97
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libadminutil/admutil.h"
#include "dsalib.h"
#include "init_ds_env.h"
#if !defined(_WIN32)
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#else
#include <windows.h>
#endif
#define SUBAGT_PATH "bin/slapd/server"
#define SUBAGT_NAME "ns-ldapagt"
#define START 1
#define STOP 2
#define RESTART 3
#define NSLDAPAGT_PID "NSLDAPAGT.LK"
#ifdef __cplusplus
extern "C" {
#endif
int nsldapagt_is_running(void);
int nsldapagt_shutdown(void);
int nsldapagt_start(void);
int nsldapagt_restart(void);
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[])
{
char *action_type = NULL;
int haderror=0;
int status = 1;
fprintf(stdout, "Content-type: text/html\n\n");
if ( init_ds_env() )
return 1;
action_type = ds_a_get_cgi_var("ACTION", "Missing Command",
"Need to specify Start, Stop, or Restart");
if (!action_type)
return 1;
if (!strcmp(action_type, "START")) {
status = nsldapagt_start();
} else if (!strcmp(action_type, "STOP")) {
status = nsldapagt_shutdown();
} else if (!strcmp(action_type, "RESTART")) {
status = nsldapagt_restart();
} else {
status = DS_UNKNOWN_SNMP_COMMAND;
}
if ( !status ) {
rpt_success("Success!");
return 0;
} else {
rpt_err( status, action_type, NULL, NULL );
return 1;
}
}
#if !defined(_WIN32)
int
get_nsldapagt_pid(pid_t *pid)
{
char *SLAPD_ROOT;
char path[PATH_MAX];
FILE *fp;
*pid = -1;
SLAPD_ROOT = ds_get_install_root();
sprintf(path, "%s/logs/%s", SLAPD_ROOT, NSLDAPAGT_PID);
if (!ds_file_exists(path)) {
return(-1);
}
if ((fp = fopen(path, "r")) != (FILE *) NULL) {
if ((fscanf(fp, "%d\n", (int *) pid)) != -1) {
(void) fclose(fp);
return(0);
}
}
(void) fclose(fp);
return(-1);
}
#endif
#if defined(_WIN32)
BOOL isServiceRunning(LPCTSTR szServiceId)
{
BOOL bReturn = FALSE;
DWORD dwError = 0;
SC_HANDLE schService = NULL;
SC_HANDLE schSCManager = NULL;
SERVICE_STATUS lpss;
if((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)))
{
if((schService = OpenService(schSCManager,
szServiceId,
SERVICE_ALL_ACCESS)))
{
bReturn = ControlService(schService, SERVICE_CONTROL_INTERROGATE , &lpss);
if(SERVICE_RUNNING == lpss.dwCurrentState)
{
bReturn = TRUE;
}
CloseServiceHandle(schService);
}
dwError = GetLastError();
CloseServiceHandle(schSCManager);
}
return(bReturn);
}
#endif
/*
* This routine returns:
* 0 if nsldapagt is NOT running
* 1 if nsldapagt is actually running
*/
int
nsldapagt_is_running()
{
#if defined(_WIN32)
if (FALSE == isServiceRunning("SNMP") )
{
return(0);
}
#else
pid_t pid;
if (get_nsldapagt_pid(&pid) != 0) {
return(0);
}
if (kill(pid, 0) == -1) {
return(0);
}
#endif
return(1);
}
#if !defined(_WIN32)
/*
* This routine returns:
* 0 if magt is NOT running
* 1 if magt is actually running
*
* The run state is determined whether one can successfully bind to the
* smux port.
*
* this is for UNIX only
*/
int
smux_master_is_running()
{
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if (pse = getservbyname("smux", "tcp")) {
sin.sin_port = ntohs(pse->s_port);
} else {
sin.sin_port = 199;
}
if ((ppe = getprotobyname("tcp")) == 0) {
return(0);
}
if ((s = socket(AF_INET, SOCK_STREAM, ppe->p_proto)) < 0) {
return(0);
}
/* bind expects port number to be in network order
we should do this for all platforms, not just OSF. */
sin.sin_port = htons(sin.sin_port);
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
close(s);
return(1);
} else {
}
close(s);
return(0);
}
#endif
int
nsldapagt_start()
{
if (nsldapagt_is_running()) {
return(0);
}
#if defined(_WIN32)
/* NT version -- just try to start the SNMP service */
/* Bug 612322: redirecting the output to null device */
system("net start SNMP > nul");
#else
/*
* Check if smux master agent is running before firing off the subagent!
*/
if (!smux_master_is_running()) {
return(-1);
} else {
char *NETSITE_ROOT = getenv("NETSITE_ROOT");
char *SLAPD_ROOT = ds_get_install_root();
char command[1024];
sprintf(command, "cd %s/%s; ./%s -d %s", NETSITE_ROOT, SUBAGT_PATH,
SUBAGT_NAME, SLAPD_ROOT);
(void) system(command);
sleep(2);
}
#endif
if (!nsldapagt_is_running()) {
return(-1);
}
return(0);
}
int
nsldapagt_shutdown()
{
if (!nsldapagt_is_running()) {
rpt_success("NOT_RUNNING");
exit(0);
} else {
int status = -1;
#if defined(_WIN32)
/* NT version -- just try to stop the SNMP service */
/* Bug 612322: redirecting the output to null device */
status = system("net stop SNMP > nul");
#else
/* UNIX version */
pid_t pid;
if (get_nsldapagt_pid(&pid) == 0)
{
if (kill(pid, SIGTERM) == 0)
{
sleep(2);
if (!nsldapagt_is_running())
{
status = 0;
}
}
}
#endif
return(status);
}
return(0);
}
int
nsldapagt_restart()
{
int status;
if ( (status = nsldapagt_shutdown()) != 0 )
return status;
else
return nsldapagt_start();
}
|