r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[Samba/gebeck_regimport.git] / examples / libmsrpc / test / test_util.c
blob81a9c9203d44e955442d59cc4e15557d241f74ee
1 /*some utility functions for the registry tests*/
3 #include "libmsrpc.h"
4 #include "test_util.h"
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]);
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;
16 ZERO_STRUCTP(hnd->username);
17 ZERO_STRUCTP(hnd->domain);
18 ZERO_STRUCTP(hnd->netbios_name);
19 ZERO_STRUCTP(hnd->password);
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++;
27 else if(strncmp(argv[i], "-W", sizeof(fstring)) == 0) {
28 strncpy(hnd->domain, argv[i+1], sizeof(fstring));
29 i++;
33 else if(strncmp(argv[i], "-P", sizeof(fstring)) == 0) {
34 strncpy(hnd->password, argv[i+1], sizeof(fstring));
35 i++;
39 else if(strncmp(argv[i], "-N", sizeof(fstring)) == 0) {
40 strncpy(hnd->netbios_name, argv[i+1], sizeof(fstring));
41 i++;
44 else if(strncmp(argv[i], "-d", sizeof(fstring)) == 0) {
45 sscanf(argv[i+1], "%d", &hnd->debug);
46 i++;
49 else { /*assume this is the server name*/
50 strncpy(hnd->server, argv[i], sizeof(fstring));
54 if(!hnd->server) {
55 cactest_print_usage(argv);
56 cac_FreeHandle(hnd);
57 exit(-1);
62 void print_value(uint32 type, REG_VALUE_DATA *data) {
63 int i = 0;
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: ");
78 for(i = 0; i < data->reg_multi_sz.num_strings; i++) {
79 printf(" %d: %s\n", i, data->reg_multi_sz.strings[i]);
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);
98 printf("\n");
102 void cactest_readline(FILE *in, fstring line) {
104 int c;
106 c = fgetc(in);
107 if(c != '\n')
108 ungetc(c, in);
110 fgets(line, sizeof(fstring), in);
112 if(line[strlen(line) - 1] == '\n')
113 line[strlen(line) - 1] = '\0';
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)
127 char temp[sizeof(fstring)];
129 static char authUsername[sizeof(fstring)];
130 static char authWorkgroup[sizeof(fstring)];
131 static char authPassword[sizeof(fstring)];
132 static char authSet = 0;
134 char *pass = NULL;
136 if (authSet)
138 strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
139 strncpy(pUsername, authUsername, maxLenUsername - 1);
140 strncpy(pPassword, authPassword, maxLenPassword - 1);
142 else
144 if(pWorkgroup[0] != '\0') {
145 strncpy(authWorkgroup, pWorkgroup, maxLenWorkgroup - 1);
147 else {
148 d_printf("Domain: [%s] ", pWorkgroup);
149 fscanf(stdin, "%s", temp);
151 if (temp[0] != '\0')
153 strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
154 strncpy(authWorkgroup, temp, maxLenWorkgroup - 1);
159 if(pUsername[0] != '\0') {
160 strncpy(authUsername, pUsername, maxLenUsername - 1);
162 else {
163 d_printf("Username: [%s] ", pUsername);
164 fscanf(stdin, "%s", temp);
166 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
168 temp[strlen(temp) - 1] = '\0';
171 if (temp[0] != '\0')
173 strncpy(pUsername, temp, maxLenUsername - 1);
174 strncpy(authUsername, pUsername, maxLenUsername - 1);
177 if(pPassword[0] != '\0') {
178 strncpy(authPassword, pPassword, maxLenPassword - 1);
180 else {
181 pass = getpass("Password: ");
182 if (pass)
183 fstrcpy(temp, pass);
184 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
186 temp[strlen(temp) - 1] = '\0';
188 if (temp[0] != '\0')
190 strncpy(pPassword, temp, maxLenPassword - 1);
191 strncpy(authPassword, pPassword, maxLenPassword - 1);
194 authSet = 1;
198 void cactest_reg_input_val(TALLOC_CTX *mem_ctx, int *type, char **name, REG_VALUE_DATA *data) {
199 fstring tmp;
200 int i;
202 printf("Enter value name: \n");
203 cactest_readline(stdin, tmp);
204 *name = talloc_strdup(mem_ctx, tmp);
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);
211 switch(*type) {
212 case REG_SZ:
213 printf("Enter string:\n");
214 cactest_readline(stdin, tmp);
216 data->reg_sz = talloc_strdup(mem_ctx, tmp);
217 break;
219 case REG_DWORD:
220 printf("Enter dword: ");
221 scanf("%d", &data->reg_dword);
222 break;
224 case REG_MULTI_SZ:
225 printf("Enter number of strings: ");
226 scanf("%d", &data->reg_multi_sz.num_strings);
228 data->reg_multi_sz.strings = talloc_array(mem_ctx, char *, data->reg_multi_sz.num_strings);
230 for(i = 0; i < data->reg_multi_sz.num_strings; i++) {
231 printf("String %d: ", i+1);
232 cactest_readline(stdin, tmp);
234 data->reg_multi_sz.strings[i] = talloc_strdup(mem_ctx, tmp);
236 break;
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);
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));
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);
262 printf(" Bad pwd count: %d\n", info->bad_passwd_count);
263 printf(" Logon Cuont : %d\n", info->logon_count);
265 printf(" NT Password : %s\n", info->nt_password);
266 printf(" LM Password : %s\n", info->lm_password);
270 void edit_readline(fstring line) {
271 fgets(line, sizeof(fstring), stdin);
273 if(line[strlen(line)-1] == '\n')
274 line[strlen(line)-1] = '\0';
276 void edit_cac_user_info(TALLOC_CTX *mem_ctx, CacUserInfo *info) {
277 fstring tmp;
279 printf(" User Name [%s]: ", info->username);
280 edit_readline(tmp);
282 if(tmp[0] != '\0')
283 info->username = talloc_strdup(mem_ctx, tmp);
285 printf(" Full Name [%s]: ", info->full_name);
287 edit_readline(tmp);
288 if(tmp[0] != '\0')
289 info->full_name = talloc_strdup(mem_ctx, tmp);
291 printf(" Description [%s]: ", info->description);
292 edit_readline(tmp);
293 if(tmp[0] != '\0')
294 info->description = talloc_strdup(mem_ctx, tmp);
296 printf(" Remote Dial [%s]: ", info->dial);
297 edit_readline(tmp);
298 if(tmp[0] != '\0')
299 info->dial = talloc_strdup(mem_ctx, tmp);
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);
306 printf(" Must change pass at next logon? [y/N]: ");
307 edit_readline(tmp);
309 if(tmp[0] == 'y' || tmp[0] == 'Y')
310 info->pass_must_change= True;
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);
320 void edit_cac_group_info(TALLOC_CTX *mem_ctx, CacGroupInfo *info) {
321 fstring tmp;
323 printf("Group Name [%s]: ", info->name);
324 edit_readline(tmp);
325 if(tmp[0] != '\0')
326 info->name = talloc_strdup(mem_ctx, tmp);
328 printf("Description [%s]: ", info->description);
329 edit_readline(tmp);
330 if(tmp[0] != '\0')
331 info->description = talloc_strdup(mem_ctx, tmp);
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;
350 return "Invalid role!\n";
353 char *cactime_str(CacTime ctime, fstring tmp) {
355 snprintf(tmp, sizeof(fstring), "%u Days, %u Hours, %u Minutes, %u Seconds", ctime.days, ctime.hours, ctime.minutes, ctime.seconds);
357 return tmp;
360 void print_cac_domain_info(CacDomainInfo *info) {
361 fstring tmp;
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);
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);
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");
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);