1 /*Some user management stuff*/
6 int main(int argc, char **argv) {
7 CacServerHandle *hnd = NULL;
8 TALLOC_CTX *mem_ctx = NULL;
11 struct SamOpenUser ou;
12 struct SamEnumUsers eu;
13 struct SamCreateUser cu;
14 struct SamGetUserInfo gi;
15 struct SamSetUserInfo si;
16 struct SamRenameUser ru;
17 struct SamSetPassword sp;
19 POLICY_HND *user_hnd = NULL;
29 mem_ctx = talloc_init("cac_samgroup");
31 hnd = cac_NewServerHandle(True);
33 cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
35 cac_parse_cmd_line(argc, argv, hnd);
37 if(!cac_Connect(hnd, NULL)) {
38 fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
42 struct SamOpenDomain sod;
45 sod.in.access = MAXIMUM_ALLOWED_ACCESS;
47 if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
48 fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
53 while(tmp[0] != 'q') {
55 printf("[l]ist users\n");
56 printf("[c]reate user\n");
57 printf("[o]pen user\n");
58 printf("[d]elete user\n");
59 printf("[g]et user info\n");
60 printf("[e]dit user info\n");
61 printf("[r]ename user\n");
62 printf("reset [p]assword\n");
63 printf("[n] close user\n");
66 printf("Enter option: ");
67 cactest_readline(stdin, tmp);
72 case 'c': /*create user*/
73 if(user_hnd != NULL) {
74 /*then we have an open handle.. close it*/
75 cac_SamClose(hnd, mem_ctx, user_hnd);
79 printf("Enter user name: ");
80 cactest_readline(stdin, input);
84 cu.in.name = talloc_strdup(mem_ctx, input);
85 cu.in.dom_hnd = sod.out.dom_hnd;
86 cu.in.acb_mask = ACB_NORMAL;
88 if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
89 printf("Could not create user. Error: %s\n", nt_errstr(hnd->status));
92 printf("Created user %s with RID 0x%x\n", cu.in.name, cu.out.rid);
93 user_hnd = cu.out.user_hnd;
98 case 'o': /*open group*/
99 if(user_hnd != NULL) {
100 /*then we have an open handle.. close it*/
101 cac_SamClose(hnd, mem_ctx, user_hnd);
107 ou.in.dom_hnd = sod.out.dom_hnd;
108 ou.in.access = MAXIMUM_ALLOWED_ACCESS;
110 printf("Enter RID: 0x");
111 scanf("%x", &ou.in.rid);
113 if(!cac_SamOpenUser(hnd, mem_ctx, &ou)) {
114 fprintf(stderr, "Could not open user. Error: %s\n", nt_errstr(hnd->status));
117 printf("Opened user\n");
118 user_hnd = ou.out.user_hnd;
123 case 'l': /*list users*/
125 eu.in.dom_hnd = sod.out.dom_hnd;
127 while(cac_SamEnumUsers(hnd, mem_ctx, &eu)) {
128 for(i = 0; i < eu.out.num_users; i++) {
129 printf("RID: 0x%x Name: %s\n", eu.out.rids[i], eu.out.names[i]);
133 if(CAC_OP_FAILED(hnd->status)) {
134 printf("Could not enumerate Users. Error: %s\n", nt_errstr(hnd->status));
141 case 'd': /*delete group*/
143 printf("Must open group first!\n");
147 if(!cac_SamDeleteGroup(hnd, mem_ctx, user_hnd)) {
148 fprintf(stderr, "Could not delete group. Error: %s\n", nt_errstr(hnd->status));
151 printf("Deleted group.\n");
159 printf("Must open user first!\n");
163 if(!cac_SamClose(hnd, mem_ctx, user_hnd)) {
164 printf("Could not user group\n");
171 case 'g': /*get user info*/
173 printf("Must open user first!\n");
178 gi.in.user_hnd = ou.out.user_hnd;
180 if(!cac_SamGetUserInfo(hnd, mem_ctx, &gi)) {
181 printf("Could not get user info. Error: %s\n", nt_errstr(hnd->status));
184 printf("Retrieved User information:\n");
185 print_cac_user_info(gi.out.info);
190 case 'e': /*edit user info*/
192 printf("Must Open user first!\n");
197 gi.in.user_hnd = ou.out.user_hnd;
198 if(!cac_SamGetUserInfo(hnd, mem_ctx, &gi)) {
199 printf("Could not get user info. Error: %s\n", nt_errstr(hnd->status));
203 edit_cac_user_info(mem_ctx, gi.out.info);
205 printf("setting following info:\n");
206 print_cac_user_info(gi.out.info);
210 si.in.user_hnd = user_hnd;
211 si.in.info = gi.out.info;
213 if(!cac_SamSetUserInfo(hnd, mem_ctx, &si)) {
214 printf("Could not set user info. Error: %s\n", nt_errstr(hnd->status));
222 case 'r': /*rename user*/
224 printf("Must open user first!\n");
230 printf("Enter new username: ");
231 cactest_readline(stdin, tmp);
233 ru.in.user_hnd = user_hnd;
234 ru.in.new_name = talloc_strdup(mem_ctx, tmp);
236 if(!cac_SamRenameUser(hnd, mem_ctx, &ru)) {
237 printf("Could not rename user. Error: %s\n", nt_errstr(hnd->status));
240 printf("Renamed user\n");
245 case 'p': /*reset password*/
248 printf("Must open user first!\n");
254 printf("Passwords do not match. Please try again\n");
257 pass1 = getpass("Enter new password: ");
258 pass2 = getpass("Re-enter new password: ");
259 } while(strncmp(pass1, pass2, MAX_PASS_LEN));
262 sp.in.user_hnd = user_hnd;
263 sp.in.password = talloc_strdup(mem_ctx, pass1);
265 if(!cac_SamSetPassword(hnd, mem_ctx, &sp)) {
266 printf("Could not set password. Error: %s\n", nt_errstr(hnd->status));
278 printf("Invalid command\n");
282 cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
285 cac_SamClose(hnd, mem_ctx, user_hnd);
290 talloc_destroy(mem_ctx);