Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / libmsrpc / test / sam / samuser.c
blobdf56a2d991c7074e6ec9fc422c269ab0dab0f97e
1 /*Some user management stuff*/
3 #include "libmsrpc.h"
4 #include "test_util.h"
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;
21 fstring tmp;
22 fstring input;
24 char *pass1 = NULL;
25 char *pass2 = NULL;
27 int i;
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));
39 exit(-1);
42 struct SamOpenDomain sod;
43 ZERO_STRUCT(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));
49 goto done;
52 tmp[0] = 0x00;
53 while(tmp[0] != 'q') {
54 printf("\n");
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");
65 printf("[q]uit\n\n");
66 printf("Enter option: ");
67 cactest_readline(stdin, tmp);
69 printf("\n");
71 switch(tmp[0]) {
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);
76 user_hnd = NULL;
79 printf("Enter user name: ");
80 cactest_readline(stdin, input);
82 ZERO_STRUCT(cu);
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));
91 else {
92 printf("Created user %s with RID 0x%x\n", cu.in.name, cu.out.rid);
93 user_hnd = cu.out.user_hnd;
96 break;
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);
102 user_hnd = NULL;
105 ZERO_STRUCT(ou);
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));
116 else {
117 printf("Opened user\n");
118 user_hnd = ou.out.user_hnd;
121 break;
123 case 'l': /*list users*/
124 ZERO_STRUCT(eu);
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));
137 break;
139 break;
141 case 'd': /*delete group*/
142 if(!user_hnd) {
143 printf("Must open group first!\n");
144 break;
147 if(!cac_SamDeleteGroup(hnd, mem_ctx, user_hnd)) {
148 fprintf(stderr, "Could not delete group. Error: %s\n", nt_errstr(hnd->status));
150 else {
151 printf("Deleted group.\n");
152 user_hnd = NULL;
154 break;
157 case 'n':
158 if(!user_hnd) {
159 printf("Must open user first!\n");
160 break;
163 if(!cac_SamClose(hnd, mem_ctx, user_hnd)) {
164 printf("Could not user group\n");
165 break;
168 user_hnd = NULL;
169 break;
171 case 'g': /*get user info*/
172 if(!user_hnd) {
173 printf("Must open user first!\n");
174 break;
177 ZERO_STRUCT(gi);
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));
183 else {
184 printf("Retrieved User information:\n");
185 print_cac_user_info(gi.out.info);
188 break;
190 case 'e': /*edit user info*/
191 if(!user_hnd) {
192 printf("Must Open user first!\n");
193 break;
196 ZERO_STRUCT(gi);
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));
200 break;
203 edit_cac_user_info(mem_ctx, gi.out.info);
205 printf("setting following info:\n");
206 print_cac_user_info(gi.out.info);
208 ZERO_STRUCT(si);
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));
216 else {
217 printf("Done.\n");
220 break;
222 case 'r': /*rename user*/
223 if(!user_hnd) {
224 printf("Must open user first!\n");
225 break;
228 ZERO_STRUCT(ru);
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));
239 else {
240 printf("Renamed user\n");
243 break;
245 case 'p': /*reset password*/
247 if(!user_hnd) {
248 printf("Must open user first!\n");
249 break;
252 do {
253 if(pass1 && pass2) {
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));
261 ZERO_STRUCT(sp);
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));
268 else {
269 printf("Done.\n");
272 break;
274 case 'q':
275 break;
277 default:
278 printf("Invalid command\n");
282 cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
284 if(user_hnd)
285 cac_SamClose(hnd, mem_ctx, user_hnd);
287 done:
288 cac_FreeHandle(hnd);
290 talloc_destroy(mem_ctx);
292 return 0;