summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/test/test_util.c
blob: 81a9c9203d44e955442d59cc4e15557d241f74ee (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
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
/*some utility functions for the registry tests*/

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


void cactest_print_usage(char **argv) {
   printf("Usage:\n");
   printf("  %s server [-U username] [-W domain] [-P passwprd] [-N netbios_name]\n", argv[0]);
}

/*allocates memory for auth info and parses domain/user/server out of command line*/
void cac_parse_cmd_line(int argc, char **argv, CacServerHandle *hnd) {
   int i = 0;

   ZERO_STRUCTP(hnd->username);
   ZERO_STRUCTP(hnd->domain);
   ZERO_STRUCTP(hnd->netbios_name);
   ZERO_STRUCTP(hnd->password);

   for(i = 1; i < argc; i++) {
      if( strncmp(argv[i], "-U", sizeof(fstring)) == 0) {
         strncpy(hnd->username, argv[i+1], sizeof(fstring));
         i++;
      }

      else if(strncmp(argv[i], "-W", sizeof(fstring)) == 0) {
         strncpy(hnd->domain, argv[i+1], sizeof(fstring));
         i++;

      }

      else if(strncmp(argv[i], "-P", sizeof(fstring)) == 0) {
         strncpy(hnd->password, argv[i+1], sizeof(fstring));
         i++;

      }

      else if(strncmp(argv[i], "-N", sizeof(fstring)) == 0) {
         strncpy(hnd->netbios_name, argv[i+1], sizeof(fstring));
         i++;
      }

      else if(strncmp(argv[i], "-d", sizeof(fstring)) == 0) {
         sscanf(argv[i+1], "%d", &hnd->debug);
         i++;
      }

      else { /*assume this is the server name*/
         strncpy(hnd->server, argv[i], sizeof(fstring));
      }
   }

   if(!hnd->server) {
      cactest_print_usage(argv);
      cac_FreeHandle(hnd);
      exit(-1);
   }

}

void print_value(uint32 type, REG_VALUE_DATA *data) {
   int i = 0;

   switch(type) {
      case REG_SZ:
         printf(" Type: REG_SZ\n");
         printf(" Value: %s\n", data->reg_sz);
         break;
      case REG_EXPAND_SZ:
         printf(" Type: REG_EXPAND_SZ\n");
         printf(" Value: %s\n", data->reg_expand_sz);
         break;
      case REG_MULTI_SZ:
         printf(" Type: REG_MULTI_SZ\n");
         printf(" Values: ");

         for(i = 0; i < data->reg_multi_sz.num_strings; i++) {
            printf("     %d: %s\n", i, data->reg_multi_sz.strings[i]);
         }
         break;
      case REG_DWORD:
         printf(" Type: REG_DWORD\n");
         printf(" Value: %d\n", data->reg_dword);
         break;
      case REG_DWORD_BE:
         printf(" Type: REG_DWORD_BE\n");
         printf(" Value: 0x%x\n", data->reg_dword_be);
         break;
      case REG_BINARY:
         printf(" Type: REG_BINARY\n");
         break;
      default:
         printf(" Invalid type: %d\n", type);
         
   }

   printf("\n");
   
}

void cactest_readline(FILE *in, fstring line) {

   int c;
   
   c = fgetc(in);
   if(c != '\n')
      ungetc(c, in);

   fgets(line, sizeof(fstring), in);

   if(line[strlen(line) - 1] == '\n')
      line[strlen(line) - 1] = '\0';

}

void cactest_GetAuthDataFn(const char * pServer,
                 const char * pShare,
                 char * pWorkgroup,
                 int maxLenWorkgroup,
                 char * pUsername,
                 int maxLenUsername,
                 char * pPassword,
                 int maxLenPassword)
    
{
   char temp[sizeof(fstring)];

   static char authUsername[sizeof(fstring)];
   static char authWorkgroup[sizeof(fstring)];
   static char authPassword[sizeof(fstring)];
   static char authSet = 0;

   char *pass = NULL;

   if (authSet)
   {
      strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
      strncpy(pUsername, authUsername, maxLenUsername - 1);
      strncpy(pPassword, authPassword, maxLenPassword - 1);
   }
   else
   {
      if(pWorkgroup[0] != '\0') {
         strncpy(authWorkgroup, pWorkgroup, maxLenWorkgroup - 1);
      }
      else {
         d_printf("Domain: [%s] ", pWorkgroup);
         fscanf(stdin, "%s", temp);

         if (temp[0] != '\0')
         {
            strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
            strncpy(authWorkgroup, temp, maxLenWorkgroup - 1);
         }
      }


      if(pUsername[0] != '\0') {
         strncpy(authUsername, pUsername, maxLenUsername - 1);
      }
      else {
         d_printf("Username: [%s] ", pUsername);
         fscanf(stdin, "%s", temp);

         if (temp[strlen(temp) - 1] == '\n') /* A new line? */
         {
            temp[strlen(temp) - 1] = '\0';
         }

         if (temp[0] != '\0')
         {
            strncpy(pUsername, temp, maxLenUsername - 1);
            strncpy(authUsername, pUsername, maxLenUsername - 1);
         }
      }
      if(pPassword[0] != '\0') {
         strncpy(authPassword, pPassword, maxLenPassword - 1);
      }
      else {
         pass = getpass("Password: ");
         if (pass)
            fstrcpy(temp, pass);
         if (temp[strlen(temp) - 1] == '\n') /* A new line? */
         {
            temp[strlen(temp) - 1] = '\0';
         }        
         if (temp[0] != '\0')
         {
            strncpy(pPassword, temp, maxLenPassword - 1);
            strncpy(authPassword, pPassword, maxLenPassword - 1);
         }    
      }
      authSet = 1;
   }
}

void cactest_reg_input_val(TALLOC_CTX *mem_ctx, int *type, char **name, REG_VALUE_DATA *data) {
   fstring tmp;
   int i;

   printf("Enter value name: \n");
   cactest_readline(stdin, tmp);
   *name = talloc_strdup(mem_ctx, tmp);

   do {
      printf("Enter type. %d = REG_SZ, %d = REG_DWORD, %d = REG_MULTI_SZ: ", REG_SZ, REG_DWORD, REG_MULTI_SZ);
      scanf("%d", type);
   } while(*type != REG_SZ && *type != REG_DWORD && *type != REG_MULTI_SZ);

   switch(*type) {
      case REG_SZ:
         printf("Enter string:\n");
         cactest_readline(stdin, tmp);

         data->reg_sz = talloc_strdup(mem_ctx, tmp);
         break;

      case REG_DWORD:
         printf("Enter dword: ");
         scanf("%d", &data->reg_dword);
         break;

      case REG_MULTI_SZ:
         printf("Enter number of strings: ");
         scanf("%d", &data->reg_multi_sz.num_strings);
         
         data->reg_multi_sz.strings = talloc_array(mem_ctx, char *, data->reg_multi_sz.num_strings);

         for(i = 0; i < data->reg_multi_sz.num_strings; i++) {
            printf("String %d: ", i+1);
            cactest_readline(stdin, tmp);

            data->reg_multi_sz.strings[i] = talloc_strdup(mem_ctx, tmp);
         }
         break;
   }
}

void print_cac_user_info(CacUserInfo *info) {
   printf("	User Name    : %s\n", info->username);
   printf("	Full Name    : %s\n", info->full_name);
   printf("	Home Dir     : %s\n", info->home_dir);
   printf("	Home Drive   : %s\n", info->home_drive);
   printf("	Profile Path : %s\n", info->profile_path);
   printf("	Logon Script : %s\n", info->logon_script);
   printf("	Description  : %s\n", info->description);
   printf("	Workstations : %s\n", info->workstations);
   printf("	Remote Dial  : %s\n", info->dial);

   printf("	Logon Time   : %s\n", http_timestring(info->logon_time));
   printf("	Logoff Time  : %s\n", http_timestring(info->logoff_time));
   printf("	Kickoff Time : %s\n", http_timestring(info->kickoff_time));
   printf("	Pass last set: %s\n", http_timestring(info->pass_last_set_time));
   printf("	Pass can set : %s\n", http_timestring(info->pass_can_change_time));
   printf("	Pass must set: %s\n", http_timestring(info->pass_must_change_time));

   printf("	User RID     : 0x%x\n", info->rid);
   printf("	Group RID    : 0x%x\n", info->group_rid);
   printf("	ACB Mask     : 0x%x\n", info->acb_mask);

   printf("	Bad pwd count: %d\n", info->bad_passwd_count);
   printf("	Logon Cuont  : %d\n", info->logon_count);

   printf(" NT Password  : %s\n", info->nt_password);
   printf(" LM Password  : %s\n", info->lm_password);

}

void edit_readline(fstring line) {
   fgets(line, sizeof(fstring), stdin);

   if(line[strlen(line)-1] == '\n')
      line[strlen(line)-1] = '\0';
}
void edit_cac_user_info(TALLOC_CTX *mem_ctx, CacUserInfo *info) {
   fstring tmp;

   printf(" User Name [%s]: ", info->username);
   edit_readline(tmp);

   if(tmp[0] != '\0')
      info->username = talloc_strdup(mem_ctx, tmp);

   printf(" Full Name [%s]: ", info->full_name);

   edit_readline(tmp);
   if(tmp[0] != '\0')
      info->full_name = talloc_strdup(mem_ctx, tmp);

   printf(" Description [%s]: ", info->description);
   edit_readline(tmp);
   if(tmp[0] != '\0')
      info->description = talloc_strdup(mem_ctx, tmp);

   printf(" Remote Dial [%s]: ", info->dial);
   edit_readline(tmp);
   if(tmp[0] != '\0')
      info->dial = talloc_strdup(mem_ctx, tmp);

   printf(" ACB Mask [0x%x]: ", info->acb_mask);
   edit_readline(tmp);
   if(tmp[0] != '\0') 
      sscanf(tmp, "%x", &info->acb_mask);

   printf(" Must change pass at next logon? [y/N]: ");
   edit_readline(tmp);

   if(tmp[0] == 'y' || tmp[0] == 'Y')
      info->pass_must_change= True;

}
   
void print_cac_group_info(CacGroupInfo *info) {
   printf(" Group Name  : %s\n", info->name);
   printf(" Description : %s\n", info->description);
   printf(" Num Members : %d\n", info->num_members);
}

void edit_cac_group_info(TALLOC_CTX *mem_ctx, CacGroupInfo *info) {
   fstring tmp;

   printf("Group Name [%s]: ", info->name);
   edit_readline(tmp);
   if(tmp[0] != '\0')
      info->name = talloc_strdup(mem_ctx, tmp);

   printf("Description [%s]: ", info->description);
   edit_readline(tmp);
   if(tmp[0] != '\0')
      info->description = talloc_strdup(mem_ctx, tmp);
}

char *srv_role_str(uint32 role) {
   switch(role) {
      case ROLE_STANDALONE:
         return "STANDALONE";
         break;
      case ROLE_DOMAIN_MEMBER:
         return "DOMAIN_MEMBER";
         break;
      case ROLE_DOMAIN_BDC:
         return "DOMAIN_BDC";
         break;
      case ROLE_DOMAIN_PDC:
         return "DOMAIN_PDC";
         break;
   }

   return "Invalid role!\n";
}

char *cactime_str(CacTime ctime, fstring tmp) {

   snprintf(tmp, sizeof(fstring), "%u Days, %u Hours, %u Minutes, %u Seconds", ctime.days, ctime.hours, ctime.minutes, ctime.seconds);

   return tmp;
}

void print_cac_domain_info(CacDomainInfo *info) {
   fstring tmp;
   
   printf("  Server Role      : %s\n", srv_role_str(info->server_role));
   printf("  Num Users        : %d\n", info->num_users);
   printf("  Num Domain Groups: %d\n", info->num_domain_groups);
   printf("  Num Local Groups : %d\n", info->num_local_groups);
   printf("  Comment          : %s\n", info->comment);
   printf("  Domain Name      : %s\n", info->domain_name);
   printf("  Server Name      : %s\n", info->server_name);
   printf("  Min. Pass. Length: %d\n", info->min_pass_length);
   printf("  Password History : %d\n", info->pass_history);
   printf("\n");
   printf("  Passwords Expire In    : %s\n", cactime_str(info->expire, tmp));
   printf("  Passwords Can Change in: %s\n", cactime_str(info->min_pass_age, tmp));
   printf("  Lockouts last          : %s\n", cactime_str(info->lockout_duration, tmp));
   printf("  Allowed Bad Attempts   : %d\n", info->num_bad_attempts);
}

void print_cac_service(CacService svc) {
   printf("\tService Name: %s\n", svc.service_name);
   printf("\tDisplay Name: %s\n", svc.display_name);
   print_service_status(svc.status);
}

void print_service_status(SERVICE_STATUS status) {
   printf("\tStatus:\n");
   printf("\t Type:          0x%x\n", status.type); 
   printf("\t State:         0x%x\n", status.state);
   printf("\t Controls:      0x%x\n", status.controls_accepted);
   printf("\t W32 Exit Code: 0x%x\n", status.win32_exit_code);
   printf("\t SVC Exit Code: 0x%x\n", status.service_exit_code);
   printf("\t Checkpoint:    0x%x\n", status.check_point);
   printf("\t Wait Hint:     0x%x\n", status.wait_hint);
   printf("\n");
}

void print_service_config(CacServiceConfig *config) {
   printf("\tConfig:\n");
   printf("\tType:             0x%x\n", config->type);
   printf("\tStart Type:       0x%x\n", config->start_type);
   printf("\tError config:     0x%x\n", config->error_control);
   printf("\tExecutable Path:  %s\n", config->exe_path);
   printf("\tLoad Order Group: %s\n", config->load_order_group);
   printf("\tTag ID:           0x%x\n", config->tag_id);
   printf("\tDependencies:     %s\n", config->dependencies);
   printf("\tStart Name:       %s\n", config->start_name);
   printf("\tDisplay Name:     %s\n", config->display_name);
}