source: branches/samba-3.0/examples/libmsrpc/test/sam/samuser.c

Last change on this file was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 7.6 KB
Line 
1/*Some user management stuff*/
2
3#include "libmsrpc.h"
4#include "test_util.h"
5
6int main(int argc, char **argv) {
7 CacServerHandle *hnd = NULL;
8 TALLOC_CTX *mem_ctx = NULL;
9
10
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;
18
19 POLICY_HND *user_hnd = NULL;
20
21 fstring tmp;
22 fstring input;
23
24 char *pass1 = NULL;
25 char *pass2 = NULL;
26
27 int i;
28
29 mem_ctx = talloc_init("cac_samgroup");
30
31 hnd = cac_NewServerHandle(True);
32
33 cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
34
35 cac_parse_cmd_line(argc, argv, hnd);
36
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);
40 }
41
42 struct SamOpenDomain sod;
43 ZERO_STRUCT(sod);
44
45 sod.in.access = MAXIMUM_ALLOWED_ACCESS;
46
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;
50 }
51
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");
64
65 printf("[q]uit\n\n");
66 printf("Enter option: ");
67 cactest_readline(stdin, tmp);
68
69 printf("\n");
70
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;
77 }
78
79 printf("Enter user name: ");
80 cactest_readline(stdin, input);
81
82 ZERO_STRUCT(cu);
83
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;
87
88 if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
89 printf("Could not create user. Error: %s\n", nt_errstr(hnd->status));
90 }
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;
94 }
95
96 break;
97
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;
103 }
104
105 ZERO_STRUCT(ou);
106
107 ou.in.dom_hnd = sod.out.dom_hnd;
108 ou.in.access = MAXIMUM_ALLOWED_ACCESS;
109
110 printf("Enter RID: 0x");
111 scanf("%x", &ou.in.rid);
112
113 if(!cac_SamOpenUser(hnd, mem_ctx, &ou)) {
114 fprintf(stderr, "Could not open user. Error: %s\n", nt_errstr(hnd->status));
115 }
116 else {
117 printf("Opened user\n");
118 user_hnd = ou.out.user_hnd;
119 }
120
121 break;
122
123 case 'l': /*list users*/
124 ZERO_STRUCT(eu);
125 eu.in.dom_hnd = sod.out.dom_hnd;
126
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]);
130 }
131 }
132
133 if(CAC_OP_FAILED(hnd->status)) {
134 printf("Could not enumerate Users. Error: %s\n", nt_errstr(hnd->status));
135 }
136
137 break;
138
139 break;
140
141 case 'd': /*delete group*/
142 if(!user_hnd) {
143 printf("Must open group first!\n");
144 break;
145 }
146
147 if(!cac_SamDeleteGroup(hnd, mem_ctx, user_hnd)) {
148 fprintf(stderr, "Could not delete group. Error: %s\n", nt_errstr(hnd->status));
149 }
150 else {
151 printf("Deleted group.\n");
152 user_hnd = NULL;
153 }
154 break;
155
156
157 case 'n':
158 if(!user_hnd) {
159 printf("Must open user first!\n");
160 break;
161 }
162
163 if(!cac_SamClose(hnd, mem_ctx, user_hnd)) {
164 printf("Could not user group\n");
165 break;
166 }
167
168 user_hnd = NULL;
169 break;
170
171 case 'g': /*get user info*/
172 if(!user_hnd) {
173 printf("Must open user first!\n");
174 break;
175 }
176
177 ZERO_STRUCT(gi);
178 gi.in.user_hnd = ou.out.user_hnd;
179
180 if(!cac_SamGetUserInfo(hnd, mem_ctx, &gi)) {
181 printf("Could not get user info. Error: %s\n", nt_errstr(hnd->status));
182 }
183 else {
184 printf("Retrieved User information:\n");
185 print_cac_user_info(gi.out.info);
186 }
187
188 break;
189
190 case 'e': /*edit user info*/
191 if(!user_hnd) {
192 printf("Must Open user first!\n");
193 break;
194 }
195
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;
201 }
202
203 edit_cac_user_info(mem_ctx, gi.out.info);
204
205 printf("setting following info:\n");
206 print_cac_user_info(gi.out.info);
207
208 ZERO_STRUCT(si);
209
210 si.in.user_hnd = user_hnd;
211 si.in.info = gi.out.info;
212
213 if(!cac_SamSetUserInfo(hnd, mem_ctx, &si)) {
214 printf("Could not set user info. Error: %s\n", nt_errstr(hnd->status));
215 }
216 else {
217 printf("Done.\n");
218 }
219
220 break;
221
222 case 'r': /*rename user*/
223 if(!user_hnd) {
224 printf("Must open user first!\n");
225 break;
226 }
227
228 ZERO_STRUCT(ru);
229
230 printf("Enter new username: ");
231 cactest_readline(stdin, tmp);
232
233 ru.in.user_hnd = user_hnd;
234 ru.in.new_name = talloc_strdup(mem_ctx, tmp);
235
236 if(!cac_SamRenameUser(hnd, mem_ctx, &ru)) {
237 printf("Could not rename user. Error: %s\n", nt_errstr(hnd->status));
238 }
239 else {
240 printf("Renamed user\n");
241 }
242
243 break;
244
245 case 'p': /*reset password*/
246
247 if(!user_hnd) {
248 printf("Must open user first!\n");
249 break;
250 }
251
252 do {
253 if(pass1 && pass2) {
254 printf("Passwords do not match. Please try again\n");
255 }
256
257 pass1 = getpass("Enter new password: ");
258 pass2 = getpass("Re-enter new password: ");
259 } while(strncmp(pass1, pass2, MAX_PASS_LEN));
260
261 ZERO_STRUCT(sp);
262 sp.in.user_hnd = user_hnd;
263 sp.in.password = talloc_strdup(mem_ctx, pass1);
264
265 if(!cac_SamSetPassword(hnd, mem_ctx, &sp)) {
266 printf("Could not set password. Error: %s\n", nt_errstr(hnd->status));
267 }
268 else {
269 printf("Done.\n");
270 }
271
272 break;
273
274 case 'q':
275 break;
276
277 default:
278 printf("Invalid command\n");
279 }
280 }
281
282 cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
283
284 if(user_hnd)
285 cac_SamClose(hnd, mem_ctx, user_hnd);
286
287done:
288 cac_FreeHandle(hnd);
289
290 talloc_destroy(mem_ctx);
291
292 return 0;
293}
294
Note: See TracBrowser for help on using the repository browser.