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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
/*
* Unix SMB/CIFS implementation.
* MS-RPC client library implementation (SVCCTL pipe)
* Copyright (C) Chris Nicholls 2005.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "libmsrpc.h"
#include "libsmb_internal.h"
#define WAIT_SLEEP_TIME 300
int cac_SvcOpenScm(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcOpenScm *op) {
SMBCSRV *srv = NULL;
WERROR err;
POLICY_HND *scm_out = NULL;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || op->in.access == 0 || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
/*initialize for samr pipe if we have to*/
if(!hnd->_internal.pipes[PI_SVCCTL]) {
if(!cli_nt_session_open(&srv->cli, PI_SVCCTL)) {
hnd->status = NT_STATUS_UNSUCCESSFUL;
return CAC_FAILURE;
}
hnd->_internal.pipes[PI_SVCCTL] = True;
}
srv->cli.pipe_idx = PI_SVCCTL;
scm_out = talloc(mem_ctx, POLICY_HND);
if(!scm_out) {
hnd->status = NT_STATUS_NO_MEMORY;
return CAC_FAILURE;
}
err = cli_svcctl_open_scm( &(srv->cli), mem_ctx, scm_out, op->in.access);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.scm_hnd = scm_out;
return CAC_SUCCESS;
}
int cac_SvcClose(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *scm_hnd) {
SMBCSRV *srv = NULL;
WERROR err;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!scm_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_close_service( &(srv->cli), mem_ctx, scm_hnd);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
return CAC_SUCCESS;
}
int cac_SvcEnumServices(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcEnumServices *op) {
SMBCSRV *srv = NULL;
WERROR err;
uint32 type_buf = 0;
uint32 state_buf = 0;
uint32 num_svc_out = 0;
ENUM_SERVICES_STATUS *svc_buf = NULL;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.scm_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
type_buf = (op->in.type != 0) ? op->in.type : (SVCCTL_TYPE_DRIVER | SVCCTL_TYPE_WIN32);
state_buf = (op->in.state != 0) ? op->in.state : SVCCTL_STATE_ALL;
err = cli_svcctl_enumerate_services( &(srv->cli), mem_ctx, op->in.scm_hnd, type_buf, state_buf, &num_svc_out, &svc_buf);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.services = cac_MakeServiceArray(mem_ctx, svc_buf, num_svc_out);
if(!op->out.services) {
hnd->status = NT_STATUS_NO_MEMORY;
return CAC_FAILURE;
}
talloc_free(svc_buf);
op->out.num_services = num_svc_out;
return CAC_SUCCESS;
}
int cac_SvcOpenService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcOpenService *op) {
SMBCSRV *srv = NULL;
WERROR err;
POLICY_HND *svc_hnd_out = NULL;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.scm_hnd || !op->in.name || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
svc_hnd_out = talloc(mem_ctx, POLICY_HND);
if(!svc_hnd_out) {
hnd->status = NT_STATUS_NO_MEMORY;
return CAC_FAILURE;
}
err = cli_svcctl_open_service( &(srv->cli), mem_ctx, op->in.scm_hnd, svc_hnd_out, op->in.name, op->in.access);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.svc_hnd = svc_hnd_out;
return CAC_SUCCESS;
}
int cac_SvcControlService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcControlService *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_STATUS status_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
if(op->in.control < SVCCTL_CONTROL_STOP || op->in.control > SVCCTL_CONTROL_SHUTDOWN) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_control_service( &(srv->cli), mem_ctx, op->in.svc_hnd, op->in.control, &status_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
return CAC_SUCCESS;
}
int cac_SvcGetStatus(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcGetStatus *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_STATUS status_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_query_status( &(srv->cli), mem_ctx, op->in.svc_hnd, &status_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.status = status_out;
return CAC_SUCCESS;
}
/*Internal function - similar to code found in utils/net_rpc_service.c
* Waits for a service to reach a specific state.
* svc_hnd - Handle to the service
* state - the state we are waiting for
* timeout - number of seconds to wait
* returns CAC_FAILURE if the state is never reached
* or CAC_SUCCESS if the state is reached
*/
int cac_WaitForService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *svc_hnd, uint32 state, uint32 timeout, SERVICE_STATUS *status) {
SMBCSRV *srv = NULL;
/*number of milliseconds we have spent*/
uint32 time_spent = 0;
WERROR err;
if(!hnd || !mem_ctx || !svc_hnd || !status)
return CAC_FAILURE;
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
while(status->state != state && time_spent < (timeout * 1000) && NT_STATUS_IS_OK(hnd->status)) {
/*if this is the first call, then we _just_ got the status.. sleep now*/
usleep(WAIT_SLEEP_TIME);
time_spent += WAIT_SLEEP_TIME;
err = cli_svcctl_query_status(&(srv->cli), mem_ctx, svc_hnd, status);
hnd->status = werror_to_ntstatus(err);
}
if(status->state == state)
return CAC_SUCCESS;
return CAC_FAILURE;
}
int cac_SvcStartService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcStartService *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_STATUS status_buf;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
if(op->in.num_parms != 0 && op->in.parms == NULL) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_start_service(&(srv->cli), mem_ctx, op->in.svc_hnd, (const char **)op->in.parms, op->in.num_parms);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
if(op->in.timeout == 0)
return CAC_SUCCESS;
return cac_WaitForService(hnd, mem_ctx, op->in.svc_hnd, SVCCTL_RUNNING, op->in.timeout, &status_buf);
}
int cac_SvcStopService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcStopService *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_STATUS status_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_control_service( &(srv->cli), mem_ctx, op->in.svc_hnd, SVCCTL_CONTROL_STOP, &status_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.status = status_out;
if(op->in.timeout == 0)
return CAC_SUCCESS;
return cac_WaitForService(hnd, mem_ctx, op->in.svc_hnd, SVCCTL_STOPPED, op->in.timeout, &op->out.status);
}
int cac_SvcPauseService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcPauseService *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_STATUS status_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_control_service( &(srv->cli), mem_ctx, op->in.svc_hnd, SVCCTL_CONTROL_PAUSE, &status_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.status = status_out;
if(op->in.timeout == 0)
return CAC_SUCCESS;
return cac_WaitForService(hnd, mem_ctx, op->in.svc_hnd, SVCCTL_PAUSED, op->in.timeout, &op->out.status);
}
int cac_SvcContinueService(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcContinueService *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_STATUS status_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_control_service( &(srv->cli), mem_ctx, op->in.svc_hnd, SVCCTL_CONTROL_CONTINUE, &status_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.status = status_out;
if(op->in.timeout == 0)
return CAC_SUCCESS;
return cac_WaitForService(hnd, mem_ctx, op->in.svc_hnd, SVCCTL_RUNNING, op->in.timeout, &op->out.status);
}
int cac_SvcGetDisplayName(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcGetDisplayName *op) {
SMBCSRV *srv = NULL;
WERROR err;
fstring disp_name_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_get_dispname( &(srv->cli), mem_ctx, op->in.svc_hnd, disp_name_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
op->out.display_name = talloc_strdup(mem_ctx, disp_name_out);
if(!op->out.display_name) {
hnd->status = NT_STATUS_NO_MEMORY;
return CAC_FAILURE;
}
return CAC_SUCCESS;
}
int cac_SvcGetServiceConfig(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, struct SvcGetServiceConfig *op) {
SMBCSRV *srv = NULL;
WERROR err;
SERVICE_CONFIG config_out;
if(!hnd)
return CAC_FAILURE;
if(!hnd->_internal.ctx) {
hnd->status = NT_STATUS_INVALID_HANDLE;
return CAC_FAILURE;
}
if(!op || !op->in.svc_hnd || !mem_ctx) {
hnd->status = NT_STATUS_INVALID_PARAMETER;
return CAC_FAILURE;
}
srv = cac_GetServer(hnd);
if(!srv) {
hnd->status = NT_STATUS_INVALID_CONNECTION;
return CAC_FAILURE;
}
srv->cli.pipe_idx = PI_SVCCTL;
err = cli_svcctl_query_config( &(srv->cli), mem_ctx, op->in.svc_hnd, &config_out);
hnd->status = werror_to_ntstatus(err);
if(!NT_STATUS_IS_OK(hnd->status))
return CAC_FAILURE;
if(!cac_InitCacServiceConfig(mem_ctx, &config_out, &op->out.config)) {
hnd->status = NT_STATUS_NO_MEMORY;
return CAC_FAILURE;
}
return CAC_SUCCESS;
}
|