r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[sfrench/samba-autobuild/.git] / examples / libmsrpc / test / test_util.c
1 /*some utility functions for the registry tests*/
2
3 #include "libmsrpc.h"
4 #include "test_util.h"
5
6
7 void cactest_print_usage(char **argv) {
8    printf("Usage:\n");
9    printf("  %s server [-U username] [-W domain] [-P passwprd] [-N netbios_name]\n", argv[0]);
10 }
11
12 /*allocates memory for auth info and parses domain/user/server out of command line*/
13 void cac_parse_cmd_line(int argc, char **argv, CacServerHandle *hnd) {
14    int i = 0;
15
16    ZERO_STRUCTP(hnd->username);
17    ZERO_STRUCTP(hnd->domain);
18    ZERO_STRUCTP(hnd->netbios_name);
19    ZERO_STRUCTP(hnd->password);
20
21    for(i = 1; i < argc; i++) {
22       if( strncmp(argv[i], "-U", sizeof(fstring)) == 0) {
23          strncpy(hnd->username, argv[i+1], sizeof(fstring));
24          i++;
25       }
26
27       else if(strncmp(argv[i], "-W", sizeof(fstring)) == 0) {
28          strncpy(hnd->domain, argv[i+1], sizeof(fstring));
29          i++;
30
31       }
32
33       else if(strncmp(argv[i], "-P", sizeof(fstring)) == 0) {
34          strncpy(hnd->password, argv[i+1], sizeof(fstring));
35          i++;
36
37       }
38
39       else if(strncmp(argv[i], "-N", sizeof(fstring)) == 0) {
40          strncpy(hnd->netbios_name, argv[i+1], sizeof(fstring));
41          i++;
42       }
43
44       else if(strncmp(argv[i], "-d", sizeof(fstring)) == 0) {
45          sscanf(argv[i+1], "%d", &hnd->debug);
46          i++;
47       }
48
49       else { /*assume this is the server name*/
50          strncpy(hnd->server, argv[i], sizeof(fstring));
51       }
52    }
53
54    if(!hnd->server) {
55       cactest_print_usage(argv);
56       cac_FreeHandle(hnd);
57       exit(-1);
58    }
59
60 }
61
62 void print_value(uint32 type, REG_VALUE_DATA *data) {
63    int i = 0;
64
65    switch(type) {
66       case REG_SZ:
67          printf(" Type: REG_SZ\n");
68          printf(" Value: %s\n", data->reg_sz);
69          break;
70       case REG_EXPAND_SZ:
71          printf(" Type: REG_EXPAND_SZ\n");
72          printf(" Value: %s\n", data->reg_expand_sz);
73          break;
74       case REG_MULTI_SZ:
75          printf(" Type: REG_MULTI_SZ\n");
76          printf(" Values: ");
77
78          for(i = 0; i < data->reg_multi_sz.num_strings; i++) {
79             printf("     %d: %s\n", i, data->reg_multi_sz.strings[i]);
80          }
81          break;
82       case REG_DWORD:
83          printf(" Type: REG_DWORD\n");
84          printf(" Value: %d\n", data->reg_dword);
85          break;
86       case REG_DWORD_BE:
87          printf(" Type: REG_DWORD_BE\n");
88          printf(" Value: 0x%x\n", data->reg_dword_be);
89          break;
90       case REG_BINARY:
91          printf(" Type: REG_BINARY\n");
92          break;
93       default:
94          printf(" Invalid type: %d\n", type);
95          
96    }
97
98    printf("\n");
99    
100 }
101
102 void cactest_readline(FILE *in, fstring line) {
103
104    int c;
105    
106    c = fgetc(in);
107    if(c != '\n')
108       ungetc(c, in);
109
110    fgets(line, sizeof(fstring), in);
111
112    if(line[strlen(line) - 1] == '\n')
113       line[strlen(line) - 1] = '\0';
114
115 }
116
117 void cactest_GetAuthDataFn(const char * pServer,
118                  const char * pShare,
119                  char * pWorkgroup,
120                  int maxLenWorkgroup,
121                  char * pUsername,
122                  int maxLenUsername,
123                  char * pPassword,
124                  int maxLenPassword)
125     
126 {
127    char temp[sizeof(fstring)];
128
129    static char authUsername[sizeof(fstring)];
130    static char authWorkgroup[sizeof(fstring)];
131    static char authPassword[sizeof(fstring)];
132    static char authSet = 0;
133
134    char *pass = NULL;
135
136    if (authSet)
137    {
138       strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
139       strncpy(pUsername, authUsername, maxLenUsername - 1);
140       strncpy(pPassword, authPassword, maxLenPassword - 1);
141    }
142    else
143    {
144       if(pWorkgroup[0] != '\0') {
145          strncpy(authWorkgroup, pWorkgroup, maxLenWorkgroup - 1);
146       }
147       else {
148          d_printf("Domain: [%s] ", pWorkgroup);
149          fscanf(stdin, "%s", temp);
150
151          if (temp[0] != '\0')
152          {
153             strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
154             strncpy(authWorkgroup, temp, maxLenWorkgroup - 1);
155          }
156       }
157
158
159       if(pUsername[0] != '\0') {
160          strncpy(authUsername, pUsername, maxLenUsername - 1);
161       }
162       else {
163          d_printf("Username: [%s] ", pUsername);
164          fscanf(stdin, "%s", temp);
165
166          if (temp[strlen(temp) - 1] == '\n') /* A new line? */
167          {
168             temp[strlen(temp) - 1] = '\0';
169          }
170
171          if (temp[0] != '\0')
172          {
173             strncpy(pUsername, temp, maxLenUsername - 1);
174             strncpy(authUsername, pUsername, maxLenUsername - 1);
175          }
176       }
177       if(pPassword[0] != '\0') {
178          strncpy(authPassword, pPassword, maxLenPassword - 1);
179       }
180       else {
181          pass = getpass("Password: ");
182          if (pass)
183             fstrcpy(temp, pass);
184          if (temp[strlen(temp) - 1] == '\n') /* A new line? */
185          {
186             temp[strlen(temp) - 1] = '\0';
187          }        
188          if (temp[0] != '\0')
189          {
190             strncpy(pPassword, temp, maxLenPassword - 1);
191             strncpy(authPassword, pPassword, maxLenPassword - 1);
192          }    
193       }
194       authSet = 1;
195    }
196 }
197
198 void cactest_reg_input_val(TALLOC_CTX *mem_ctx, int *type, char **name, REG_VALUE_DATA *data) {
199    fstring tmp;
200    int i;
201
202    printf("Enter value name: \n");
203    cactest_readline(stdin, tmp);
204    *name = talloc_strdup(mem_ctx, tmp);
205
206    do {
207       printf("Enter type. %d = REG_SZ, %d = REG_DWORD, %d = REG_MULTI_SZ: ", REG_SZ, REG_DWORD, REG_MULTI_SZ);
208       scanf("%d", type);
209    } while(*type != REG_SZ && *type != REG_DWORD && *type != REG_MULTI_SZ);
210
211    switch(*type) {
212       case REG_SZ:
213          printf("Enter string:\n");
214          cactest_readline(stdin, tmp);
215
216          data->reg_sz = talloc_strdup(mem_ctx, tmp);
217          break;
218
219       case REG_DWORD:
220          printf("Enter dword: ");
221          scanf("%d", &data->reg_dword);
222          break;
223
224       case REG_MULTI_SZ:
225          printf("Enter number of strings: ");
226          scanf("%d", &data->reg_multi_sz.num_strings);
227          
228          data->reg_multi_sz.strings = talloc_array(mem_ctx, char *, data->reg_multi_sz.num_strings);
229
230          for(i = 0; i < data->reg_multi_sz.num_strings; i++) {
231             printf("String %d: ", i+1);
232             cactest_readline(stdin, tmp);
233
234             data->reg_multi_sz.strings[i] = talloc_strdup(mem_ctx, tmp);
235          }
236          break;
237    }
238 }
239
240 void print_cac_user_info(CacUserInfo *info) {
241    printf("     User Name    : %s\n", info->username);
242    printf("     Full Name    : %s\n", info->full_name);
243    printf("     Home Dir     : %s\n", info->home_dir);
244    printf("     Home Drive   : %s\n", info->home_drive);
245    printf("     Profile Path : %s\n", info->profile_path);
246    printf("     Logon Script : %s\n", info->logon_script);
247    printf("     Description  : %s\n", info->description);
248    printf("     Workstations : %s\n", info->workstations);
249    printf("     Remote Dial  : %s\n", info->dial);
250
251    printf("     Logon Time   : %s\n", http_timestring(info->logon_time));
252    printf("     Logoff Time  : %s\n", http_timestring(info->logoff_time));
253    printf("     Kickoff Time : %s\n", http_timestring(info->kickoff_time));
254    printf("     Pass last set: %s\n", http_timestring(info->pass_last_set_time));
255    printf("     Pass can set : %s\n", http_timestring(info->pass_can_change_time));
256    printf("     Pass must set: %s\n", http_timestring(info->pass_must_change_time));
257
258    printf("     User RID     : 0x%x\n", info->rid);
259    printf("     Group RID    : 0x%x\n", info->group_rid);
260    printf("     ACB Mask     : 0x%x\n", info->acb_mask);
261
262    printf("     Bad pwd count: %d\n", info->bad_passwd_count);
263    printf("     Logon Cuont  : %d\n", info->logon_count);
264
265    printf(" NT Password  : %s\n", info->nt_password);
266    printf(" LM Password  : %s\n", info->lm_password);
267
268 }
269
270 void edit_readline(fstring line) {
271    fgets(line, sizeof(fstring), stdin);
272
273    if(line[strlen(line)-1] == '\n')
274       line[strlen(line)-1] = '\0';
275 }
276 void edit_cac_user_info(TALLOC_CTX *mem_ctx, CacUserInfo *info) {
277    fstring tmp;
278
279    printf(" User Name [%s]: ", info->username);
280    edit_readline(tmp);
281
282    if(tmp[0] != '\0')
283       info->username = talloc_strdup(mem_ctx, tmp);
284
285    printf(" Full Name [%s]: ", info->full_name);
286
287    edit_readline(tmp);
288    if(tmp[0] != '\0')
289       info->full_name = talloc_strdup(mem_ctx, tmp);
290
291    printf(" Description [%s]: ", info->description);
292    edit_readline(tmp);
293    if(tmp[0] != '\0')
294       info->description = talloc_strdup(mem_ctx, tmp);
295
296    printf(" Remote Dial [%s]: ", info->dial);
297    edit_readline(tmp);
298    if(tmp[0] != '\0')
299       info->dial = talloc_strdup(mem_ctx, tmp);
300
301    printf(" ACB Mask [0x%x]: ", info->acb_mask);
302    edit_readline(tmp);
303    if(tmp[0] != '\0') 
304       sscanf(tmp, "%x", &info->acb_mask);
305
306    printf(" Must change pass at next logon? [y/N]: ");
307    edit_readline(tmp);
308
309    if(tmp[0] == 'y' || tmp[0] == 'Y')
310       info->pass_must_change= True;
311
312 }
313    
314 void print_cac_group_info(CacGroupInfo *info) {
315    printf(" Group Name  : %s\n", info->name);
316    printf(" Description : %s\n", info->description);
317    printf(" Num Members : %d\n", info->num_members);
318 }
319
320 void edit_cac_group_info(TALLOC_CTX *mem_ctx, CacGroupInfo *info) {
321    fstring tmp;
322
323    printf("Group Name [%s]: ", info->name);
324    edit_readline(tmp);
325    if(tmp[0] != '\0')
326       info->name = talloc_strdup(mem_ctx, tmp);
327
328    printf("Description [%s]: ", info->description);
329    edit_readline(tmp);
330    if(tmp[0] != '\0')
331       info->description = talloc_strdup(mem_ctx, tmp);
332 }
333
334 char *srv_role_str(uint32 role) {
335    switch(role) {
336       case ROLE_STANDALONE:
337          return "STANDALONE";
338          break;
339       case ROLE_DOMAIN_MEMBER:
340          return "DOMAIN_MEMBER";
341          break;
342       case ROLE_DOMAIN_BDC:
343          return "DOMAIN_BDC";
344          break;
345       case ROLE_DOMAIN_PDC:
346          return "DOMAIN_PDC";
347          break;
348    }
349
350    return "Invalid role!\n";
351 }
352
353 char *cactime_str(CacTime ctime, fstring tmp) {
354
355    snprintf(tmp, sizeof(fstring), "%u Days, %u Hours, %u Minutes, %u Seconds", ctime.days, ctime.hours, ctime.minutes, ctime.seconds);
356
357    return tmp;
358 }
359
360 void print_cac_domain_info(CacDomainInfo *info) {
361    fstring tmp;
362    
363    printf("  Server Role      : %s\n", srv_role_str(info->server_role));
364    printf("  Num Users        : %d\n", info->num_users);
365    printf("  Num Domain Groups: %d\n", info->num_domain_groups);
366    printf("  Num Local Groups : %d\n", info->num_local_groups);
367    printf("  Comment          : %s\n", info->comment);
368    printf("  Domain Name      : %s\n", info->domain_name);
369    printf("  Server Name      : %s\n", info->server_name);
370    printf("  Min. Pass. Length: %d\n", info->min_pass_length);
371    printf("  Password History : %d\n", info->pass_history);
372    printf("\n");
373    printf("  Passwords Expire In    : %s\n", cactime_str(info->expire, tmp));
374    printf("  Passwords Can Change in: %s\n", cactime_str(info->min_pass_age, tmp));
375    printf("  Lockouts last          : %s\n", cactime_str(info->lockout_duration, tmp));
376    printf("  Allowed Bad Attempts   : %d\n", info->num_bad_attempts);
377 }
378
379 void print_cac_service(CacService svc) {
380    printf("\tService Name: %s\n", svc.service_name);
381    printf("\tDisplay Name: %s\n", svc.display_name);
382    print_service_status(svc.status);
383 }
384
385 void print_service_status(SERVICE_STATUS status) {
386    printf("\tStatus:\n");
387    printf("\t Type:          0x%x\n", status.type); 
388    printf("\t State:         0x%x\n", status.state);
389    printf("\t Controls:      0x%x\n", status.controls_accepted);
390    printf("\t W32 Exit Code: 0x%x\n", status.win32_exit_code);
391    printf("\t SVC Exit Code: 0x%x\n", status.service_exit_code);
392    printf("\t Checkpoint:    0x%x\n", status.check_point);
393    printf("\t Wait Hint:     0x%x\n", status.wait_hint);
394    printf("\n");
395 }
396
397 void print_service_config(CacServiceConfig *config) {
398    printf("\tConfig:\n");
399    printf("\tType:             0x%x\n", config->type);
400    printf("\tStart Type:       0x%x\n", config->start_type);
401    printf("\tError config:     0x%x\n", config->error_control);
402    printf("\tExecutable Path:  %s\n", config->exe_path);
403    printf("\tLoad Order Group: %s\n", config->load_order_group);
404    printf("\tTag ID:           0x%x\n", config->tag_id);
405    printf("\tDependencies:     %s\n", config->dependencies);
406    printf("\tStart Name:       %s\n", config->start_name);
407    printf("\tDisplay Name:     %s\n", config->display_name);
408 }