1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/*Some group management stuff*/
#include "libmsrpc.h"
#include "test_util.h"
int main(int argc, char **argv) {
CacServerHandle *hnd = NULL;
TALLOC_CTX *mem_ctx = NULL;
struct SamEnumGroups eg;
struct SamEnumUsers eu;
struct SamCreateGroup cg;
struct SamOpenGroup og;
struct SamGetGroupMembers ggm;
struct SamGetNamesFromRids gn;
struct SamAddGroupMember add;
struct SamRemoveGroupMember del;
struct SamSetGroupMembers set;
struct SamGetGroupsForUser gg;
struct SamOpenUser ou;
struct SamGetGroupInfo gi;
struct SamSetGroupInfo si;
struct SamRenameGroup rg;
struct SamGetSecurityObject gso;
POLICY_HND *group_hnd = NULL;
fstring tmp;
fstring input;
int i;
mem_ctx = talloc_init("cac_samgroup");
hnd = cac_NewServerHandle(True);
cac_parse_cmd_line(argc, argv, hnd);
if(!cac_Connect(hnd, NULL)) {
fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
exit(-1);
}
struct SamOpenDomain sod;
ZERO_STRUCT(sod);
sod.in.access = MAXIMUM_ALLOWED_ACCESS;
if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
goto done;
}
tmp[0] = 0x00;
while(tmp[0] != 'q') {
printf("\n");
printf("[l]ist groups\n");
printf("[c]reate group\n");
printf("[o]pen group\n");
printf("[d]elete group\n");
printf("list [m]embers\n");
printf("list [u]sers\n");
printf("list [g]roup for users\n");
printf("[a]dd member\n");
printf("[r]emove member\n");
printf("[x] clear members\n");
printf("get group [i]nfo\n");
printf("[e]dit group info\n");
printf("[s]et members\n");
printf("re[n]ame group\n");
printf("[z] close group\n");
printf("[t] get security info\n");
printf("[q]uit\n\n");
printf("Enter option: ");
cactest_readline(stdin, tmp);
printf("\n");
switch(tmp[0]) {
case 'c': /*create group*/
if(group_hnd != NULL) {
/*then we have an open handle.. close it*/
cac_SamClose(hnd, mem_ctx, group_hnd);
group_hnd = NULL;
}
printf("Enter group name: ");
cactest_readline(stdin, input);
ZERO_STRUCT(cg);
cg.in.name = talloc_strdup(mem_ctx, input);
cg.in.access = MAXIMUM_ALLOWED_ACCESS;
cg.in.dom_hnd = sod.out.dom_hnd;
if(!cac_SamCreateGroup(hnd, mem_ctx, &cg)) {
fprintf(stderr, "Could not create group. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Created group %s\n", cg.in.name);
group_hnd = cg.out.group_hnd;
}
break;
case 'o': /*open group*/
if(group_hnd != NULL) {
/*then we have an open handle.. close it*/
cac_SamClose(hnd, mem_ctx, group_hnd);
group_hnd = NULL;
}
ZERO_STRUCT(og);
og.in.dom_hnd = sod.out.dom_hnd;
og.in.access = MAXIMUM_ALLOWED_ACCESS;
printf("Enter RID: 0x");
scanf("%x", &og.in.rid);
if(!cac_SamOpenGroup(hnd, mem_ctx, &og)) {
fprintf(stderr, "Could not open group. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Opened group\n");
group_hnd = og.out.group_hnd;
}
break;
case 'l': /*list groups*/
ZERO_STRUCT(eg);
eg.in.dom_hnd = sod.out.dom_hnd;
while(cac_SamEnumGroups(hnd, mem_ctx, &eg)) {
for(i = 0; i < eg.out.num_groups; i++) {
printf("RID: 0x%x Name: %s\n", eg.out.rids[i], eg.out.names[i]);
}
}
if(CAC_OP_FAILED(hnd->status)) {
printf("Could not enumerate Groups. Error: %s\n", nt_errstr(hnd->status));
}
break;
case 'm': /*list group members*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(ggm);
ggm.in.group_hnd = group_hnd;
if(!cac_SamGetGroupMembers(hnd, mem_ctx, &ggm)) {
fprintf(stderr, "Could not get group members. Error: %s\n", nt_errstr(hnd->status));
break;
}
printf("Group has %d members:\n", ggm.out.num_members);
if(ggm.out.num_members == 0) /*just skip the rest of this case*/
break;
/**get the user names*/
gn.in.dom_hnd = sod.out.dom_hnd;
gn.in.num_rids = ggm.out.num_members;
gn.in.rids = ggm.out.rids;
if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &gn)) {
fprintf(stderr, "Could not lookup names. Error: %s\n", nt_errstr(hnd->status));
break;
}
for(i = 0; i < gn.out.num_names; i++) {
printf("RID: 0x%x Name: %s\n", gn.out.map[i].rid, gn.out.map[i].name);
}
break;
case 'd': /*delete group*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
if(!cac_SamDeleteGroup(hnd, mem_ctx, group_hnd)) {
fprintf(stderr, "Could not delete group. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Deleted group.\n");
group_hnd = NULL;
}
break;
case 'u': /*list users*/
ZERO_STRUCT(eu);
eu.in.dom_hnd = sod.out.dom_hnd;
while(cac_SamEnumUsers(hnd, mem_ctx, &eu)) {
for(i = 0; i < eu.out.num_users; i++) {
printf(" RID: 0x%x Name: %s\n", eu.out.rids[i], eu.out.names[i]);
}
}
if(CAC_OP_FAILED(hnd->status)) {
printf("Could not enumerate users. Error: %s\n", nt_errstr(hnd->status));
}
break;
case 'a': /*add member to group*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(add);
add.in.group_hnd = group_hnd;
printf("Enter user RID: 0x");
scanf("%x", &add.in.rid);
if(!cac_SamAddGroupMember(hnd, mem_ctx, &add)) {
fprintf(stderr, "Could not add user to group. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Successfully added user to group\n");
}
break;
case 'r': /*remove user from group*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(del);
del.in.group_hnd = group_hnd;
printf("Enter RID: 0x");
scanf("%x", &del.in.rid);
if(!cac_SamRemoveGroupMember(hnd, mem_ctx, &del)) {
fprintf(stderr, "Could not remove user from group. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Removed user from group.\n");
}
break;
case 'x': /*clear group members*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
if(!cac_SamClearGroupMembers(hnd, mem_ctx, group_hnd)) {
fprintf(stderr, "Could not clear group members. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Cleared group members\n");
}
break;
case 's': /*set members*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(set);
set.in.group_hnd = group_hnd;
printf("Enter the number of members: ");
scanf("%d", &set.in.num_members);
set.in.rids = TALLOC_ARRAY(mem_ctx, uint32, set.in.num_members);
for(i = 0; i < set.in.num_members; i++) {
printf("Enter RID #%d: 0x", (i+1));
scanf("%x", (set.in.rids + i));
}
if(!cac_SamSetGroupMembers(hnd, mem_ctx, &set)) {
printf("could not set members. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Set users\n");
}
break;
case 'g': /*list groups for user*/
ZERO_STRUCT(ou);
ZERO_STRUCT(gg);
printf("Enter username: ");
cactest_readline(stdin, input);
if(input[0] != '\0') {
ou.in.name = talloc_strdup(mem_ctx, input);
}
else {
printf("Enter RID: 0x");
scanf("%x", &ou.in.rid);
}
ou.in.access = MAXIMUM_ALLOWED_ACCESS;
ou.in.dom_hnd = sod.out.dom_hnd;
if(!cac_SamOpenUser(hnd, mem_ctx, &ou)) {
fprintf(stderr, "Could not open user %s. Error: %s\n", ou.in.name, nt_errstr(hnd->status));
break;
}
/*now find the groups*/
gg.in.user_hnd = ou.out.user_hnd;
if(!cac_SamGetGroupsForUser(hnd, mem_ctx, &gg)) {
fprintf(stderr, "Could not get groups for user. Error: %s\n", nt_errstr(hnd->status));
break;
}
cac_SamClose(hnd, mem_ctx, ou.out.user_hnd);
ZERO_STRUCT(gn);
gn.in.dom_hnd = sod.out.dom_hnd;
gn.in.num_rids = gg.out.num_groups;
gn.in.rids = gg.out.rids;
if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &gn)) {
fprintf(stderr, "Could not get names from RIDs. Error: %s\n", nt_errstr(hnd->status));
break;
}
printf("%d groups: \n", gn.out.num_names);
for(i = 0; i < gn.out.num_names; i++) {
printf("RID: 0x%x ", gn.out.map[i].rid);
if(gn.out.map[i].found)
printf("Name: %s\n", gn.out.map[i].name);
else
printf("Unknown RID\n");
}
break;
case 'z': /*close group*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
if(!cac_SamClose(hnd, mem_ctx, group_hnd)) {
printf("Could not close group\n");
break;
}
group_hnd = NULL;
break;
case 'i': /*get group info*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(gi);
gi.in.group_hnd = group_hnd;
if(!cac_SamGetGroupInfo(hnd, mem_ctx, &gi)) {
printf("Could not get group info. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Retrieved Group info\n");
print_cac_group_info(gi.out.info);
}
break;
case 'e': /*edit group info*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(gi);
ZERO_STRUCT(si);
gi.in.group_hnd = group_hnd;
if(!cac_SamGetGroupInfo(hnd, mem_ctx, &gi)) {
printf("Could not get group info. Error: %s\n", nt_errstr(hnd->status));
break;
}
edit_cac_group_info(mem_ctx, gi.out.info);
si.in.group_hnd = group_hnd;
si.in.info = gi.out.info;
if(!cac_SamSetGroupInfo(hnd, mem_ctx, &si)) {
printf("Could not set group info. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf(" Done.\n");
}
break;
case 'n': /*rename group*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(rg);
printf("Enter new group name: ");
cactest_readline(stdin, tmp);
rg.in.group_hnd = group_hnd;
rg.in.new_name = talloc_strdup(mem_ctx, tmp);
if(!cac_SamRenameGroup(hnd, mem_ctx, &rg))
printf("Could not rename group. Error: %s\n", nt_errstr(hnd->status));
else
printf("Done.\n");
break;
case 't': /*get security info*/
if(!group_hnd) {
printf("Must open group first!\n");
break;
}
ZERO_STRUCT(gso);
gso.in.pol = group_hnd;
if(!cac_SamGetSecurityObject(hnd, mem_ctx, &gso)) {
printf("Could not get security descriptor info. Error: %s\n", nt_errstr(hnd->status));
}
else {
printf("Got it.\n");
}
break;
case 'q':
break;
default:
printf("Invalid command\n");
}
}
cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
if(group_hnd)
cac_SamClose(hnd, mem_ctx, group_hnd);
done:
cac_FreeHandle(hnd);
talloc_destroy(mem_ctx);
return 0;
}
|