Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / libmsrpc / test / sam / adduser.c
blob94482d07041cd6e8af0624b99a8ffa08f2a452a9
1 /*add's a user to a domain*/
2 #include "libmsrpc.h"
3 #include "test_util.h"
5 int main(int argc, char **argv) {
6 CacServerHandle *hnd = NULL;
7 TALLOC_CTX *mem_ctx = NULL;
9 fstring tmp;
11 struct SamOpenUser ou;
13 POLICY_HND *user_hnd = NULL;
15 mem_ctx = talloc_init("cac_adduser");
17 hnd = cac_NewServerHandle(True);
19 cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
21 cac_parse_cmd_line(argc, argv, hnd);
23 if(!cac_Connect(hnd, NULL)) {
24 fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
25 exit(-1);
28 struct SamOpenDomain sod;
29 ZERO_STRUCT(sod);
31 sod.in.access = MAXIMUM_ALLOWED_ACCESS;
33 if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
34 fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
35 goto done;
38 struct SamCreateUser cdu;
39 ZERO_STRUCT(cdu);
41 printf("Enter account name: ");
42 cactest_readline(stdin, tmp);
44 cdu.in.dom_hnd = sod.out.dom_hnd;
45 cdu.in.name = talloc_strdup(mem_ctx, tmp);
46 cdu.in.acb_mask = ACB_NORMAL;
48 if(!cac_SamCreateUser(hnd, mem_ctx, &cdu)) {
49 fprintf(stderr, "Could not create user %s. Error: %s\n", cdu.in.name, nt_errstr(hnd->status));
52 printf("would you like to delete this user? [y/n]: ");
53 cactest_readline(stdin, tmp);
55 if(tmp[0] == 'y') {
57 if(!cdu.out.user_hnd) {
58 ZERO_STRUCT(ou);
59 ou.in.dom_hnd = sod.out.dom_hnd;
60 ou.in.access = MAXIMUM_ALLOWED_ACCESS;
61 ou.in.name = talloc_strdup(mem_ctx, cdu.in.name);
63 if(!cac_SamOpenUser(hnd, mem_ctx, &ou)) {
64 fprintf(stderr, "Could not open user for deletion. Error: %s\n", nt_errstr(hnd->status));
67 user_hnd = ou.out.user_hnd;
70 else {
71 user_hnd = cdu.out.user_hnd;
74 if(!cac_SamDeleteUser(hnd, mem_ctx, user_hnd))
75 fprintf(stderr, "Could not delete user. Error: %s\n", nt_errstr(hnd->status));
77 else {
78 printf("Nope..ok\n");
81 cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
82 cac_SamClose(hnd, mem_ctx, sod.out.sam);
84 done:
85 talloc_destroy(mem_ctx);
87 cac_FreeHandle(hnd);
89 return 0;
92 /*TODO: add a function that will create a user and set userinfo and set the password*/