first public release of samba4 code
[samba.git] / source / torture / cmd_sam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SAM module functions
4
5    Copyright (C) Jelmer Vernooij 2002
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "samtest.h"
24
25 static void print_account(SAM_ACCOUNT_HANDLE *a)
26 {
27         /* FIXME */
28 }
29
30 static NTSTATUS cmd_context(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
31 {
32         NTSTATUS status;
33         char **plugins;
34         int i;
35
36         plugins = malloc(argc * sizeof(char *));
37
38         for(i = 1; i < argc; i++)
39                 plugins[i-1] = argv[i];
40
41         plugins[argc-1] = NULL;
42
43         if(!NT_STATUS_IS_OK(status = make_sam_context_list(&st->context, plugins))) {
44                 printf("make_sam_context_list failed: %s\n", nt_errstr(status));
45                 SAFE_FREE(plugins);
46                 return status;
47         }
48
49         SAFE_FREE(plugins);
50         
51         return NT_STATUS_OK;
52 }
53
54 static NTSTATUS cmd_load_module(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
55 {
56         char *plugin_arg[2];
57         NTSTATUS status;
58         if (argc != 2 && argc != 3) {
59                 printf("Usage: load <module path> [domain-name]\n");
60                 return NT_STATUS_OK;
61         }
62
63         if (argc == 3)
64                 asprintf(&plugin_arg[0], "plugin:%s|%s", argv[1], argv[2]);
65         else
66                 asprintf(&plugin_arg[0], "plugin:%s", argv[1]);
67
68         plugin_arg[1] = NULL;
69         
70         if(!NT_STATUS_IS_OK(status = make_sam_context_list(&st->context, plugin_arg))) {
71                 free(plugin_arg[0]);
72                 return status;
73         }
74         
75         free(plugin_arg[0]);
76
77         printf("load: ok\n");
78         return NT_STATUS_OK;
79 }
80
81 static NTSTATUS cmd_get_sec_desc(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
82 {
83         return NT_STATUS_NOT_IMPLEMENTED;
84 }
85
86 static NTSTATUS cmd_set_sec_desc(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
87 {
88         return NT_STATUS_NOT_IMPLEMENTED;
89 }
90
91 static NTSTATUS cmd_lookup_sid(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
92 {
93         char *name;
94         uint32 type;
95         NTSTATUS status;
96         DOM_SID sid;
97         if (argc != 2) {
98                 printf("Usage: lookup_sid <sid>\n");
99                 return NT_STATUS_INVALID_PARAMETER;
100         }
101
102         if (!string_to_sid(&sid, argv[1])){
103                 printf("Unparseable SID specified!\n");
104                 return NT_STATUS_INVALID_PARAMETER;
105         }
106
107         if (!NT_STATUS_IS_OK(status = sam_lookup_sid(st->context, st->token, mem_ctx, &sid, &name, &type))) {
108                 printf("sam_lookup_sid failed!\n");
109                 return status;
110         }
111
112         printf("Name: %s\n", name);
113         printf("Type: %d\n", type); /* FIXME: What kind of an integer is type ? */
114
115         return NT_STATUS_OK;
116 }
117
118 static NTSTATUS cmd_lookup_name(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
119 {
120         DOM_SID sid;
121         uint32 type;
122         NTSTATUS status;
123         if (argc != 3) {
124                 printf("Usage: lookup_name <domain> <name>\n");
125                 return NT_STATUS_INVALID_PARAMETER;
126         }
127
128         if (!NT_STATUS_IS_OK(status = sam_lookup_name(st->context, st->token, argv[1], argv[2], &sid, &type))) {
129                 printf("sam_lookup_name failed!\n");
130                 return status;
131         }
132
133         printf("SID: %s\n", sid_string_static(&sid));
134         printf("Type: %d\n", type);
135         
136         return NT_STATUS_OK;
137 }
138
139 static NTSTATUS cmd_lookup_account(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
140 {
141         return NT_STATUS_NOT_IMPLEMENTED;
142 }
143
144 static NTSTATUS cmd_lookup_group(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
145 {
146         return NT_STATUS_NOT_IMPLEMENTED;
147 }
148
149 static NTSTATUS cmd_lookup_domain(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
150 {
151         DOM_SID *sid;
152         NTSTATUS status;
153         if (argc != 2) {
154                 printf("Usage: lookup_domain <domain>\n");
155                 return NT_STATUS_INVALID_PARAMETER;
156         }
157
158         if (!NT_STATUS_IS_OK(status = sam_lookup_domain(st->context, st->token, argv[1], &sid))) {
159                 printf("sam_lookup_name failed!\n");
160                 return status;
161         }
162
163         printf("SID: %s\n", sid_string_static(sid));
164         
165         return NT_STATUS_OK;
166 }
167
168 static NTSTATUS cmd_enum_domains(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
169 {
170         int32 domain_count, i;
171         DOM_SID *domain_sids;
172         char **domain_names;
173         NTSTATUS status;
174
175         if (!NT_STATUS_IS_OK(status = sam_enum_domains(st->context, st->token, &domain_count, &domain_sids, &domain_names))) {
176                 printf("sam_enum_domains failed!\n");
177                 return status;
178         }
179
180         if (domain_count == 0) {
181                 printf("No domains found!\n");
182                 return NT_STATUS_OK;
183         }
184
185         for (i = 0; i < domain_count; i++) {
186                 printf("%s %s\n", domain_names[i], sid_string_static(&domain_sids[i]));
187         }
188
189         SAFE_FREE(domain_sids);
190         SAFE_FREE(domain_names);
191         
192         return NT_STATUS_OK;
193 }
194
195 static NTSTATUS cmd_update_domain(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
196 {
197         return NT_STATUS_NOT_IMPLEMENTED;
198 }
199
200 static NTSTATUS cmd_show_domain(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
201 {
202         NTSTATUS status;
203         DOM_SID sid;
204         SAM_DOMAIN_HANDLE *domain;
205         uint32 tmp_uint32;
206         uint16 tmp_uint16;
207         NTTIME tmp_nttime;
208         BOOL tmp_bool;
209         const char *tmp_string;
210
211         if (argc != 2) {
212                 printf("Usage: show_domain <sid>\n");
213                 return status;
214         }
215
216         if (!string_to_sid(&sid, argv[1])){
217                 printf("Unparseable SID specified!\n");
218                 return NT_STATUS_INVALID_PARAMETER;
219         }
220
221         if (!NT_STATUS_IS_OK(status = sam_get_domain_by_sid(st->context, st->token, GENERIC_RIGHTS_DOMAIN_ALL_ACCESS, &sid, &domain))) {
222                 printf("sam_get_domain_by_sid failed\n");
223                 return status;
224         }
225
226         if (!NT_STATUS_IS_OK(status = sam_get_domain_num_accounts(domain, &tmp_uint32))) {
227                 printf("sam_get_domain_num_accounts failed: %s\n", nt_errstr(status));
228         } else {
229                 printf("Number of accounts: %d\n", tmp_uint32);
230         }
231
232         if (!NT_STATUS_IS_OK(status = sam_get_domain_num_groups(domain, &tmp_uint32))) {
233                 printf("sam_get_domain_num_groups failed: %s\n", nt_errstr(status));
234         } else {
235                 printf("Number of groups: %u\n", tmp_uint32);
236         }
237         
238         if (!NT_STATUS_IS_OK(status = sam_get_domain_num_aliases(domain, &tmp_uint32))) {
239                 printf("sam_get_domain_num_aliases failed: %s\n", nt_errstr(status));
240         } else {
241                 printf("Number of aliases: %u\n", tmp_uint32);
242         }
243         
244         if (!NT_STATUS_IS_OK(status = sam_get_domain_name(domain, &tmp_string))) {
245                 printf("sam_get_domain_name failed: %s\n", nt_errstr(status));
246         } else {
247                 printf("Domain Name: %s\n", tmp_string);
248         }
249         
250         if (!NT_STATUS_IS_OK(status = sam_get_domain_lockout_count(domain, &tmp_uint16))) {
251                 printf("sam_get_domain_lockout_count failed: %s\n", nt_errstr(status));
252         } else {
253                 printf("Lockout Count: %u\n", tmp_uint16);
254         }
255
256         if (!NT_STATUS_IS_OK(status = sam_get_domain_force_logoff(domain, &tmp_bool))) {
257                 printf("sam_get_domain_force_logoff failed: %s\n", nt_errstr(status));
258         } else {
259                 printf("Force Logoff: %s\n", (tmp_bool?"Yes":"No"));
260         }
261         
262         if (!NT_STATUS_IS_OK(status = sam_get_domain_lockout_duration(domain, &tmp_nttime))) {
263                 printf("sam_get_domain_lockout_duration failed: %s\n", nt_errstr(status));
264         } else {
265                 printf("Lockout duration: %u\n", tmp_nttime.low);
266         }
267
268         if (!NT_STATUS_IS_OK(status = sam_get_domain_login_pwdchange(domain, &tmp_bool))) {
269                 printf("sam_get_domain_login_pwdchange failed: %s\n", nt_errstr(status));
270         } else {
271                 printf("Password changing allowed: %s\n", (tmp_bool?"Yes":"No"));
272         }
273         
274         if (!NT_STATUS_IS_OK(status = sam_get_domain_max_pwdage(domain, &tmp_nttime))) {
275                 printf("sam_get_domain_max_pwdage failed: %s\n", nt_errstr(status));
276         } else {
277                 printf("Maximum password age: %u\n", tmp_nttime.low);
278         }
279         
280         if (!NT_STATUS_IS_OK(status = sam_get_domain_min_pwdage(domain, &tmp_nttime))) {
281                 printf("sam_get_domain_min_pwdage failed: %s\n", nt_errstr(status));
282         } else {
283                 printf("Minimal password age: %u\n", tmp_nttime.low);
284         }
285         
286         if (!NT_STATUS_IS_OK(status = sam_get_domain_min_pwdlength(domain, &tmp_uint16))) {
287                 printf("sam_get_domain_min_pwdlength: %s\n", nt_errstr(status));
288         } else {
289                 printf("Minimal Password Length: %u\n", tmp_uint16);
290         }
291
292         if (!NT_STATUS_IS_OK(status = sam_get_domain_pwd_history(domain, &tmp_uint16))) {
293                 printf("sam_get_domain_pwd_history failed: %s\n", nt_errstr(status));
294         } else {
295                 printf("Password history: %u\n", tmp_uint16);
296         }
297
298         if (!NT_STATUS_IS_OK(status = sam_get_domain_reset_count(domain, &tmp_nttime))) {
299                 printf("sam_get_domain_reset_count failed: %s\n", nt_errstr(status));
300         } else {
301                 printf("Reset count: %u\n", tmp_nttime.low);
302         }
303
304         if (!NT_STATUS_IS_OK(status = sam_get_domain_server(domain, &tmp_string))) {
305                 printf("sam_get_domain_server failed: %s\n", nt_errstr(status));
306         } else {
307                 printf("Server: %s\n", tmp_string);
308         }
309         
310         return NT_STATUS_OK;
311 }
312
313 static NTSTATUS cmd_create_account(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
314 {
315         return NT_STATUS_NOT_IMPLEMENTED;
316 }
317
318 static NTSTATUS cmd_update_account(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
319 {
320         return NT_STATUS_NOT_IMPLEMENTED;
321 }
322
323 static NTSTATUS cmd_delete_account(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
324 {
325         return NT_STATUS_NOT_IMPLEMENTED;
326 }
327
328 static NTSTATUS cmd_enum_accounts(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
329 {
330         NTSTATUS status;
331         DOM_SID sid;
332         int32 account_count, i;
333         SAM_ACCOUNT_ENUM *accounts;
334
335         if (argc != 2) {
336                 printf("Usage: enum_accounts <domain-sid>\n");
337                 return NT_STATUS_INVALID_PARAMETER;
338         }
339
340         if (!string_to_sid(&sid, argv[1])){
341                 printf("Unparseable SID specified!\n");
342                 return NT_STATUS_INVALID_PARAMETER;
343         }
344
345         if (!NT_STATUS_IS_OK(status = sam_enum_accounts(st->context, st->token, &sid, 0, &account_count, &accounts))) {
346                 printf("sam_enum_accounts failed: %s\n", nt_errstr(status));
347                 return status;
348         }
349
350         if (account_count == 0) {
351                 printf("No accounts found!\n");
352                 return NT_STATUS_OK;
353         }
354
355         for (i = 0; i < account_count; i++)
356                 printf("SID: %s\nName: %s\nFullname: %s\nDescription: %s\nACB_BITS: %08X\n\n", 
357                            sid_string_static(&accounts[i].sid), accounts[i].account_name,
358                            accounts[i].full_name, accounts[i].account_desc, 
359                            accounts[i].acct_ctrl);
360
361         SAFE_FREE(accounts);
362         
363         return NT_STATUS_OK;
364 }
365
366 static NTSTATUS cmd_lookup_account_sid(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
367 {
368         NTSTATUS status;
369         DOM_SID sid;
370         SAM_ACCOUNT_HANDLE *account;
371
372         if (argc != 2) {
373                 printf("Usage: lookup_account_sid <account-sid>\n");
374                 return NT_STATUS_INVALID_PARAMETER;
375         }
376
377         if (!string_to_sid(&sid, argv[1])){
378                 printf("Unparseable SID specified!\n");
379                 return NT_STATUS_INVALID_PARAMETER;
380         }
381
382         if (!NT_STATUS_IS_OK(status = sam_get_account_by_sid(st->context, st->token, GENERIC_RIGHTS_USER_ALL_ACCESS, &sid, &account))) {
383                 printf("context_sam_get_account_by_sid failed: %s\n", nt_errstr(status));
384                 return status;
385         }
386
387         print_account(account);
388         
389         return NT_STATUS_OK;
390 }
391
392 static NTSTATUS cmd_lookup_account_name(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
393 {
394         NTSTATUS status;
395         SAM_ACCOUNT_HANDLE *account;
396
397         if (argc != 3) {
398                 printf("Usage: lookup_account_name <domain-name> <account-name>\n");
399                 return NT_STATUS_INVALID_PARAMETER;
400         }
401
402
403         if (!NT_STATUS_IS_OK(status = sam_get_account_by_name(st->context, st->token, GENERIC_RIGHTS_USER_ALL_ACCESS, argv[1], argv[2], &account))) {
404                 printf("context_sam_get_account_by_sid failed: %s\n", nt_errstr(status));
405                 return status;
406         }
407
408         print_account(account);
409         
410         return NT_STATUS_OK;
411 }
412
413 static NTSTATUS cmd_create_group(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
414 {
415         return NT_STATUS_NOT_IMPLEMENTED;
416 }
417
418 static NTSTATUS cmd_update_group(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
419 {
420         return NT_STATUS_NOT_IMPLEMENTED;
421 }
422
423 static NTSTATUS cmd_delete_group(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
424 {
425         return NT_STATUS_NOT_IMPLEMENTED;
426 }
427
428 static NTSTATUS cmd_enum_groups(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
429 {
430         return NT_STATUS_NOT_IMPLEMENTED;
431 }
432
433 static NTSTATUS cmd_lookup_group_sid(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
434 {
435         return NT_STATUS_NOT_IMPLEMENTED;
436 }
437
438 static NTSTATUS cmd_lookup_group_name(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
439 {
440         return NT_STATUS_NOT_IMPLEMENTED;
441 }
442
443 static NTSTATUS cmd_group_add_member(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
444 {
445         return NT_STATUS_NOT_IMPLEMENTED;
446 }
447
448 static NTSTATUS cmd_group_del_member(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
449 {
450         return NT_STATUS_NOT_IMPLEMENTED;
451 }
452
453
454 static NTSTATUS cmd_group_enum(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
455 {
456         return NT_STATUS_NOT_IMPLEMENTED;
457 }
458
459
460 static NTSTATUS cmd_get_sid_groups(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
461 {
462         return NT_STATUS_NOT_IMPLEMENTED;
463 }
464
465 struct cmd_set sam_general_commands[] = {
466
467         { "General SAM Commands" },
468
469         { "load", cmd_load_module, "Load a module", "load <module.so> [domain-sid]" },
470         { "context", cmd_context, "Load specified context", "context [DOMAIN|]backend1[:options] [DOMAIN|]backend2[:options]" },
471         { "get_sec_desc", cmd_get_sec_desc, "Get security descriptor info", "get_sec_desc <access-token> <sid>" },
472         { "set_sec_desc", cmd_set_sec_desc, "Set security descriptor info", "set_sec_desc <access-token> <sid>" },
473         { "lookup_sid", cmd_lookup_sid, "Lookup type of specified SID", "lookup_sid <sid>" },
474         { "lookup_name", cmd_lookup_name, "Lookup type of specified name", "lookup_name <sid>" },
475         { NULL }
476 };
477
478 struct cmd_set sam_domain_commands[] = {
479         { "Domain Commands" },
480         { "update_domain", cmd_update_domain, "Update domain information", "update_domain [domain-options] domain-name | domain-sid" },
481         { "show_domain", cmd_show_domain, "Show domain information", "show_domain domain-sid | domain-name" },
482         { "enum_domains", cmd_enum_domains, "Enumerate all domains", "enum_domains <token> <acct-ctrl>" },
483         { "lookup_domain", cmd_lookup_domain, "Lookup a domain by name", "lookup_domain domain-name" },
484         { NULL }
485 };
486
487 struct cmd_set sam_account_commands[] = {
488         { "Account Commands" },
489         { "create_account", cmd_create_account, "Create a new account with specified properties", "create_account [account-options]" },
490         { "update_account", cmd_update_account, "Update an existing account", "update_account [account-options] account-sid | account-name" },
491         { "delete_account", cmd_delete_account, "Delete an account", "delete_account account-sid | account-name" },
492         { "enum_accounts", cmd_enum_accounts, "Enumerate all accounts", "enum_accounts <token> <acct-ctrl>" },
493         { "lookup_account", cmd_lookup_account, "Lookup an account by either sid or name", "lookup_account account-sid | account-name" },
494         { "lookup_account_sid", cmd_lookup_account_sid, "Lookup an account by sid", "lookup_account_sid account-sid" },
495         { "lookup_account_name", cmd_lookup_account_name, "Lookup an account by name", "lookup_account_name account-name" },
496         { NULL }
497 };
498
499 struct cmd_set sam_group_commands[] = {
500         { "Group Commands" },
501         { "create_group", cmd_create_group, "Create a new group", "create_group [group-opts]" },
502         { "update_group", cmd_update_group, "Update an existing group", "update_group [group-opts] group-name | group-sid" },
503         { "delete_group", cmd_delete_group, "Delete an existing group", "delete_group group-name | group-sid" },
504         { "enum_groups", cmd_enum_groups, "Enumerate all groups", "enum_groups <token> <group-ctrl>" },
505         { "lookup_group", cmd_lookup_group, "Lookup a group by SID or name", "lookup_group group-sid | group-name" },
506         { "lookup_group_sid", cmd_lookup_group_sid, "Lookup a group by SID", "lookup_group_sid <sid>" },
507         { "lookup_group_name", cmd_lookup_group_name, "Lookup a group by name", "lookup_group_name <name>" },
508         { "group_add_member", cmd_group_add_member, "Add group member to group", "group_add_member <group-name | group-sid> <member-name | member-sid>" },
509         { "group_del_member", cmd_group_del_member, "Delete group member from group", "group_del_member <group-name | group-sid> <member-name | member-sid>" },
510         { "group_enum", cmd_group_enum, "Enumerate all members of specified group", "group_enum group-sid | group-name" },
511
512         { "get_sid_groups", cmd_get_sid_groups, "Get a list of groups specified sid is a member of", "group_enum <group-sid | group-name>" },
513         { NULL }
514 };