db5fa27895395b8edd3fac5b0c8369890d68f347
[sfrench/samba-autobuild/.git] / examples / libmsrpc / test / svcctl / svc.c
1 /*Tests all of the svcctl calls (at least at time of writing)*/
2
3 #include "libmsrpc.h"
4 #include "test_util.h"
5
6 int main(int argc, char **argv) {
7    CacServerHandle *hnd = NULL;
8    TALLOC_CTX *mem_ctx = NULL;
9             
10
11    struct SvcOpenScm      sos;
12    struct SvcEnumServices es;
13    struct SvcOpenService  os;
14    struct SvcGetStatus    gs;
15    struct SvcStartService start;
16    struct SvcStopService  stop;
17    struct SvcPauseService pause;
18    struct SvcContinueService res;
19    struct SvcGetDisplayName gdn;
20    struct SvcGetServiceConfig sgc;
21   
22    POLICY_HND *svc_hnd = NULL;
23
24    fstring tmp;
25    fstring input;
26    
27    int i;
28
29    mem_ctx = talloc_init("cac_samgroup");
30
31    hnd = cac_NewServerHandle(True);
32
33    cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
34
35    cac_parse_cmd_line(argc, argv, hnd);
36
37    if(!cac_Connect(hnd, NULL)) {
38       fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
39       exit(-1);
40    }
41
42    /*open a handle to the scm*/
43    ZERO_STRUCT(sos);
44
45    sos.in.access = SC_MANAGER_ALL_ACCESS;
46
47    if(!cac_SvcOpenScm(hnd, mem_ctx, &sos)) {
48       fprintf(stderr, "Could not open SCM. Error: %s\n", nt_errstr(hnd->status));
49       goto done;
50    }
51
52    printf("Opened SCM\n");
53
54    tmp[0] = 0x00;
55    while(tmp[0] != 'q') {
56       printf("\n");
57       printf("[e] Enum Services\n");
58       printf("[o] Open Service\n");
59       printf("[x] Close Service\n");
60       printf("[g] Get service status\n");
61       printf("[s] Start service\n");
62       printf("[t] Stop service\n");
63       printf("[p] Pause service\n");
64       printf("[r] Resume service\n");
65       printf("[c] Get service config\n");
66
67       printf("[d] Get display name\n");
68
69       printf("[q]uit\n\n");
70       printf("Enter option: ");
71       cactest_readline(stdin, tmp);
72
73       printf("\n");
74
75       switch(tmp[0]) {
76          case 'e': /*enum services*/
77             ZERO_STRUCT(es);
78             es.in.scm_hnd = sos.out.scm_hnd;
79
80             if(!cac_SvcEnumServices(hnd, mem_ctx, &es)) {
81                printf("Could not enumerate services. Error: %s\n", nt_errstr(hnd->status));
82                break;
83             }
84
85             for(i = 0; i < es.out.num_services; i++) {
86                print_cac_service(es.out.services[i]);
87             }
88             printf("Enumerated %d services:\n", es.out.num_services);
89
90             break;
91
92          case 'o': /*Open service*/
93             ZERO_STRUCT(os);
94
95             printf("Enter service name: ");
96             cactest_readline(stdin, tmp);
97
98             os.in.name = talloc_strdup(mem_ctx, tmp);
99             os.in.scm_hnd = sos.out.scm_hnd;
100             os.in.access = SERVICE_ALL_ACCESS;
101
102             if(!cac_SvcOpenService(hnd, mem_ctx, &os)) {
103                printf("Could not open service. Error: %s\n", nt_errstr(hnd->status));
104                break;
105             }
106             
107             printf("Opened service.\n");
108             svc_hnd = os.out.svc_hnd;
109
110             break;
111          case 'x': /*close service*/
112             if(!svc_hnd) {
113                printf("Must open service first!\n");
114                break;
115             }
116
117             cac_SvcClose(hnd, mem_ctx, svc_hnd);
118             svc_hnd = NULL;
119             break;
120          case 'g': /*get svc status*/
121             
122             if(!svc_hnd) {
123                printf("Must open service first!\n");
124                break;
125             }
126
127             ZERO_STRUCT(gs);
128
129             gs.in.svc_hnd = svc_hnd;
130
131             if(!cac_SvcGetStatus(hnd, mem_ctx, &gs)) {
132                printf("Could not get status. Error: %s\n", nt_errstr(hnd->status));
133                break;
134             }
135
136             print_service_status(gs.out.status);
137             break;
138          case 's': /*start service*/
139             if(!svc_hnd) {
140                printf("Must open service first!\n");
141                break;
142             }
143
144             ZERO_STRUCT(start);
145
146             start.in.svc_hnd = svc_hnd;
147
148             printf("Enter number of parameters: ");
149             scanf("%d", &start.in.num_parms);
150
151             start.in.parms = talloc_array(mem_ctx, char *, start.in.num_parms);
152
153             for(i = 0; i < start.in.num_parms; i++) {
154                printf("Parm %d: ", i);
155                cactest_readline(stdin, tmp);
156                start.in.parms[i] = talloc_strdup(mem_ctx, tmp);
157             }
158
159             printf("Timeout (seconds): ");
160             scanf("%d", &start.in.timeout);
161
162             if(!cac_SvcStartService(hnd, mem_ctx, &start)) {
163                printf("Could not start service. Error: %s\n", nt_errstr(hnd->status));
164             }
165             else {
166                printf("Started service.\n");
167             }
168
169             break;
170          case 't': /*stop service*/
171             if(!svc_hnd) {
172                printf("Must open service first!\n");
173                break;
174             }
175
176             ZERO_STRUCT(stop);
177             stop.in.svc_hnd = svc_hnd;
178
179             printf("Timeout (seconds): ");
180             scanf("%d", &stop.in.timeout);
181
182             if(!cac_SvcStopService(hnd, mem_ctx, &stop)) {
183                if(CAC_OP_FAILED(hnd->status)) {
184                   printf("Error occured: %s\n", nt_errstr(hnd->status));
185                }
186                else {
187                   printf("Service was not stopped within %d seconds.\n", stop.in.timeout);
188                   print_service_status(stop.out.status);
189                }
190             }
191             else {
192                printf("Done.\n");
193                print_service_status(stop.out.status);
194             }
195             break;
196          case 'd': /*get display name*/
197             if(!svc_hnd) {
198                printf("Must open service first!\n");
199                break;
200             }
201
202             ZERO_STRUCT(gdn);
203             gdn.in.svc_hnd = svc_hnd;
204
205             if(!cac_SvcGetDisplayName(hnd, mem_ctx, &gdn)) {
206                printf("Could not get display name. Error: %s\n", nt_errstr(hnd->status));
207             }
208             else {
209                printf("\tDisplay Name: %s\n", gdn.out.display_name);
210             }
211             break;
212
213          case 'p': /*pause service*/
214             if(!svc_hnd) {
215                printf("Must open service first!\n");
216                break;
217             }
218
219             ZERO_STRUCT(pause);
220             pause.in.svc_hnd = svc_hnd;
221
222             printf("Timeout (seconds): ");
223             scanf("%d", &pause.in.timeout);
224
225             if(!cac_SvcPauseService(hnd, mem_ctx, &pause)) {
226                if(CAC_OP_FAILED(hnd->status)) {
227                   printf("Error occured: %s\n", nt_errstr(hnd->status));
228                }
229                else {
230                   printf("Service was not paused within %d seconds.\n", pause.in.timeout);
231                   print_service_status(pause.out.status);
232                }
233             }
234             else {
235                printf("Done.\n");
236                print_service_status(pause.out.status);
237             }
238
239             break;
240             
241          case 'r': /*resume service*/
242             if(!svc_hnd) {
243                printf("Must open service first!\n");
244                break;
245             }
246
247             ZERO_STRUCT(res);
248             res.in.svc_hnd = svc_hnd;
249
250             printf("Timeout (seconds): ");
251             scanf("%d", &res.in.timeout);
252
253             if(!cac_SvcContinueService(hnd, mem_ctx, &res)) {
254                if(CAC_OP_FAILED(hnd->status)) {
255                   printf("Error occured: %s\n", nt_errstr(hnd->status));
256                }
257                else {
258                   printf("Service was not resumed within %d seconds.\n", res.in.timeout);
259                   print_service_status(res.out.status);
260                }
261             }
262             else {
263                printf("Done.\n");
264                print_service_status(res.out.status);
265             }
266
267             break;
268
269          case 'c': /*get service config*/
270             if(!svc_hnd) {
271                printf("Must open service first!\n");
272                break;
273             }
274
275             ZERO_STRUCT(sgc);
276
277             sgc.in.svc_hnd = svc_hnd;
278
279             if(!cac_SvcGetServiceConfig(hnd, mem_ctx, &sgc)) {
280                printf("Could not get service config. Error: %s\n", nt_errstr(hnd->status));
281             }
282             else {
283                print_service_config(&sgc.out.config);
284             }
285             break;
286
287          case 'q': /*quit*/
288             break;
289          default:
290             printf("Invalid command\n");
291       }
292    }
293
294    cac_SvcClose(hnd, mem_ctx, sos.out.scm_hnd);
295
296    done:
297    cac_FreeHandle(hnd);
298
299    talloc_destroy(mem_ctx);
300
301    return 0;
302 }
303