summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/test/svcctl/svc.c
blob: db5fa27895395b8edd3fac5b0c8369890d68f347 (plain)
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
/*Tests all of the svcctl calls (at least at time of writing)*/

#include "libmsrpc.h"
#include "test_util.h"

int main(int argc, char **argv) {
   CacServerHandle *hnd = NULL;
   TALLOC_CTX *mem_ctx = NULL;
            

   struct SvcOpenScm      sos;
   struct SvcEnumServices es;
   struct SvcOpenService  os;
   struct SvcGetStatus    gs;
   struct SvcStartService start;
   struct SvcStopService  stop;
   struct SvcPauseService pause;
   struct SvcContinueService res;
   struct SvcGetDisplayName gdn;
   struct SvcGetServiceConfig sgc;
  
   POLICY_HND *svc_hnd = NULL;

   fstring tmp;
   fstring input;
   
   int i;

   mem_ctx = talloc_init("cac_samgroup");

   hnd = cac_NewServerHandle(True);

   cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);

   cac_parse_cmd_line(argc, argv, hnd);

   if(!cac_Connect(hnd, NULL)) {
      fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
      exit(-1);
   }

   /*open a handle to the scm*/
   ZERO_STRUCT(sos);

   sos.in.access = SC_MANAGER_ALL_ACCESS;

   if(!cac_SvcOpenScm(hnd, mem_ctx, &sos)) {
      fprintf(stderr, "Could not open SCM. Error: %s\n", nt_errstr(hnd->status));
      goto done;
   }

   printf("Opened SCM\n");

   tmp[0] = 0x00;
   while(tmp[0] != 'q') {
      printf("\n");
      printf("[e] Enum Services\n");
      printf("[o] Open Service\n");
      printf("[x] Close Service\n");
      printf("[g] Get service status\n");
      printf("[s] Start service\n");
      printf("[t] Stop service\n");
      printf("[p] Pause service\n");
      printf("[r] Resume service\n");
      printf("[c] Get service config\n");

      printf("[d] Get display name\n");

      printf("[q]uit\n\n");
      printf("Enter option: ");
      cactest_readline(stdin, tmp);

      printf("\n");

      switch(tmp[0]) {
         case 'e': /*enum services*/
            ZERO_STRUCT(es);
            es.in.scm_hnd = sos.out.scm_hnd;

            if(!cac_SvcEnumServices(hnd, mem_ctx, &es)) {
               printf("Could not enumerate services. Error: %s\n", nt_errstr(hnd->status));
               break;
            }

            for(i = 0; i < es.out.num_services; i++) {
               print_cac_service(es.out.services[i]);
            }
            printf("Enumerated %d services:\n", es.out.num_services);

            break;

         case 'o': /*Open service*/
            ZERO_STRUCT(os);

            printf("Enter service name: ");
            cactest_readline(stdin, tmp);

            os.in.name = talloc_strdup(mem_ctx, tmp);
            os.in.scm_hnd = sos.out.scm_hnd;
            os.in.access = SERVICE_ALL_ACCESS;

            if(!cac_SvcOpenService(hnd, mem_ctx, &os)) {
               printf("Could not open service. Error: %s\n", nt_errstr(hnd->status));
               break;
            }
            
            printf("Opened service.\n");
            svc_hnd = os.out.svc_hnd;

            break;
         case 'x': /*close service*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            cac_SvcClose(hnd, mem_ctx, svc_hnd);
            svc_hnd = NULL;
            break;
         case 'g': /*get svc status*/
            
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(gs);

            gs.in.svc_hnd = svc_hnd;

            if(!cac_SvcGetStatus(hnd, mem_ctx, &gs)) {
               printf("Could not get status. Error: %s\n", nt_errstr(hnd->status));
               break;
            }

            print_service_status(gs.out.status);
            break;
         case 's': /*start service*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(start);

            start.in.svc_hnd = svc_hnd;

            printf("Enter number of parameters: ");
            scanf("%d", &start.in.num_parms);

            start.in.parms = talloc_array(mem_ctx, char *, start.in.num_parms);

            for(i = 0; i < start.in.num_parms; i++) {
               printf("Parm %d: ", i);
               cactest_readline(stdin, tmp);
               start.in.parms[i] = talloc_strdup(mem_ctx, tmp);
            }

            printf("Timeout (seconds): ");
            scanf("%d", &start.in.timeout);

            if(!cac_SvcStartService(hnd, mem_ctx, &start)) {
               printf("Could not start service. Error: %s\n", nt_errstr(hnd->status));
            }
            else {
               printf("Started service.\n");
            }

            break;
         case 't': /*stop service*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(stop);
            stop.in.svc_hnd = svc_hnd;

            printf("Timeout (seconds): ");
            scanf("%d", &stop.in.timeout);

            if(!cac_SvcStopService(hnd, mem_ctx, &stop)) {
               if(CAC_OP_FAILED(hnd->status)) {
                  printf("Error occured: %s\n", nt_errstr(hnd->status));
               }
               else {
                  printf("Service was not stopped within %d seconds.\n", stop.in.timeout);
                  print_service_status(stop.out.status);
               }
            }
            else {
               printf("Done.\n");
               print_service_status(stop.out.status);
            }
            break;
         case 'd': /*get display name*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(gdn);
            gdn.in.svc_hnd = svc_hnd;

            if(!cac_SvcGetDisplayName(hnd, mem_ctx, &gdn)) {
               printf("Could not get display name. Error: %s\n", nt_errstr(hnd->status));
            }
            else {
               printf("\tDisplay Name: %s\n", gdn.out.display_name);
            }
            break;

         case 'p': /*pause service*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(pause);
            pause.in.svc_hnd = svc_hnd;

            printf("Timeout (seconds): ");
            scanf("%d", &pause.in.timeout);

            if(!cac_SvcPauseService(hnd, mem_ctx, &pause)) {
               if(CAC_OP_FAILED(hnd->status)) {
                  printf("Error occured: %s\n", nt_errstr(hnd->status));
               }
               else {
                  printf("Service was not paused within %d seconds.\n", pause.in.timeout);
                  print_service_status(pause.out.status);
               }
            }
            else {
               printf("Done.\n");
               print_service_status(pause.out.status);
            }

            break;
            
         case 'r': /*resume service*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(res);
            res.in.svc_hnd = svc_hnd;

            printf("Timeout (seconds): ");
            scanf("%d", &res.in.timeout);

            if(!cac_SvcContinueService(hnd, mem_ctx, &res)) {
               if(CAC_OP_FAILED(hnd->status)) {
                  printf("Error occured: %s\n", nt_errstr(hnd->status));
               }
               else {
                  printf("Service was not resumed within %d seconds.\n", res.in.timeout);
                  print_service_status(res.out.status);
               }
            }
            else {
               printf("Done.\n");
               print_service_status(res.out.status);
            }

            break;

         case 'c': /*get service config*/
            if(!svc_hnd) {
               printf("Must open service first!\n");
               break;
            }

            ZERO_STRUCT(sgc);

            sgc.in.svc_hnd = svc_hnd;

            if(!cac_SvcGetServiceConfig(hnd, mem_ctx, &sgc)) {
               printf("Could not get service config. Error: %s\n", nt_errstr(hnd->status));
            }
            else {
               print_service_config(&sgc.out.config);
            }
            break;

         case 'q': /*quit*/
            break;
         default:
            printf("Invalid command\n");
      }
   }

   cac_SvcClose(hnd, mem_ctx, sos.out.scm_hnd);

   done:
   cac_FreeHandle(hnd);

   talloc_destroy(mem_ctx);

   return 0;
}