Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

samgroup.c

Go to the documentation of this file.
00001 /*Some group management stuff*/
00002 
00003 #include "libmsrpc.h"
00004 #include "test_util.h"
00005 
00006 int main(int argc, char **argv) {
00007    CacServerHandle *hnd = NULL;
00008    TALLOC_CTX *mem_ctx = NULL;
00009             
00010    
00011    struct SamEnumGroups eg;
00012    struct SamEnumUsers eu;
00013    struct SamCreateGroup cg;
00014    struct SamOpenGroup og;
00015    struct SamGetGroupMembers ggm;
00016    struct SamGetNamesFromRids gn;
00017    struct SamAddGroupMember add;
00018    struct SamRemoveGroupMember del;
00019    struct SamSetGroupMembers set;
00020    struct SamGetGroupsForUser gg;
00021    struct SamOpenUser         ou;
00022    struct SamGetGroupInfo     gi;
00023    struct SamSetGroupInfo     si;
00024    struct SamRenameGroup      rg;
00025    struct SamGetSecurityObject gso;
00026 
00027    POLICY_HND *group_hnd = NULL;
00028 
00029    fstring tmp;
00030    fstring input;
00031    
00032    int i;
00033 
00034    mem_ctx = talloc_init("cac_samgroup");
00035 
00036    hnd = cac_NewServerHandle(True);
00037 
00038    cac_parse_cmd_line(argc, argv, hnd);
00039 
00040    if(!cac_Connect(hnd, NULL)) {
00041       fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
00042       exit(-1);
00043    }
00044 
00045    struct SamOpenDomain sod;
00046    ZERO_STRUCT(sod);
00047 
00048    sod.in.access = MAXIMUM_ALLOWED_ACCESS; 
00049 
00050    if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
00051       fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
00052       goto done;
00053    }
00054 
00055    tmp[0] = 0x00;
00056    while(tmp[0] != 'q') {
00057       printf("\n");
00058       printf("[l]ist groups\n");
00059       printf("[c]reate group\n");
00060       printf("[o]pen group\n");
00061       printf("[d]elete group\n");
00062       printf("list [m]embers\n");
00063       printf("list [u]sers\n");
00064       printf("list [g]roup for users\n");
00065       printf("[a]dd member\n");
00066       printf("[r]emove member\n");
00067       printf("[x] clear members\n");
00068       printf("get group [i]nfo\n");
00069       printf("[e]dit group info\n");
00070       printf("[s]et members\n");
00071       printf("re[n]ame group\n");
00072       printf("[z] close group\n");
00073       printf("[t] get security info\n");
00074 
00075       printf("[q]uit\n\n");
00076       printf("Enter option: ");
00077       cactest_readline(stdin, tmp);
00078 
00079       printf("\n");
00080 
00081       switch(tmp[0]) {
00082          case 'c': /*create group*/
00083             if(group_hnd != NULL) {
00084                /*then we have an open handle.. close it*/
00085                cac_SamClose(hnd, mem_ctx, group_hnd);
00086                group_hnd = NULL;
00087             }
00088 
00089             printf("Enter group name: ");
00090             cactest_readline(stdin, input);
00091 
00092             ZERO_STRUCT(cg);
00093 
00094             cg.in.name      = talloc_strdup(mem_ctx, input);
00095             cg.in.access    = MAXIMUM_ALLOWED_ACCESS;
00096             cg.in.dom_hnd   = sod.out.dom_hnd;
00097 
00098             if(!cac_SamCreateGroup(hnd, mem_ctx, &cg)) {
00099                fprintf(stderr, "Could not create group. Error: %s\n", nt_errstr(hnd->status));
00100             }
00101             else {
00102                printf("Created group %s\n", cg.in.name);
00103 
00104                group_hnd = cg.out.group_hnd;
00105             }
00106             break;
00107 
00108          case 'o': /*open group*/
00109             if(group_hnd != NULL) {
00110                /*then we have an open handle.. close it*/
00111                cac_SamClose(hnd, mem_ctx, group_hnd);
00112                group_hnd = NULL;
00113             }
00114 
00115             ZERO_STRUCT(og);
00116 
00117             og.in.dom_hnd = sod.out.dom_hnd;
00118             og.in.access = MAXIMUM_ALLOWED_ACCESS;
00119 
00120             printf("Enter RID: 0x");
00121             scanf("%x", &og.in.rid);
00122 
00123             if(!cac_SamOpenGroup(hnd, mem_ctx, &og)) {
00124                fprintf(stderr, "Could not open group. Error: %s\n", nt_errstr(hnd->status));
00125             }
00126             else {
00127                printf("Opened group\n");
00128                group_hnd = og.out.group_hnd;
00129             }
00130 
00131             break;
00132 
00133          case 'l': /*list groups*/
00134             ZERO_STRUCT(eg);
00135             eg.in.dom_hnd = sod.out.dom_hnd;
00136 
00137             while(cac_SamEnumGroups(hnd, mem_ctx, &eg)) {
00138                for(i = 0; i < eg.out.num_groups; i++) {
00139                   printf("RID: 0x%x Name: %s\n", eg.out.rids[i], eg.out.names[i]);
00140                }
00141             }
00142 
00143             if(CAC_OP_FAILED(hnd->status)) {
00144                printf("Could not enumerate Groups. Error: %s\n", nt_errstr(hnd->status));
00145             }
00146 
00147             break;
00148             
00149          case 'm': /*list group members*/
00150             if(!group_hnd) {
00151                printf("Must open group first!\n");
00152                break;
00153             }
00154 
00155             ZERO_STRUCT(ggm);
00156             ggm.in.group_hnd = group_hnd;
00157 
00158             if(!cac_SamGetGroupMembers(hnd, mem_ctx, &ggm)) {
00159                fprintf(stderr, "Could not get group members. Error: %s\n", nt_errstr(hnd->status));
00160                break;
00161             }
00162 
00163             printf("Group has %d members:\n", ggm.out.num_members);
00164 
00165             if(ggm.out.num_members == 0) /*just skip the rest of this case*/
00166                break;
00167 
00169             gn.in.dom_hnd = sod.out.dom_hnd;
00170             gn.in.num_rids = ggm.out.num_members;
00171             gn.in.rids = ggm.out.rids;
00172 
00173             if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &gn)) {
00174                fprintf(stderr, "Could not lookup names. Error: %s\n", nt_errstr(hnd->status));
00175                break;
00176             }
00177 
00178             for(i = 0; i < gn.out.num_names; i++) {
00179                printf("RID: 0x%x Name: %s\n", gn.out.map[i].rid, gn.out.map[i].name);
00180             }
00181 
00182             break;
00183 
00184          case 'd': /*delete group*/
00185             if(!group_hnd) {
00186                printf("Must open group first!\n");
00187                break;
00188             }
00189 
00190             if(!cac_SamDeleteGroup(hnd, mem_ctx, group_hnd)) {
00191                fprintf(stderr, "Could not delete group. Error: %s\n", nt_errstr(hnd->status));
00192             }
00193             else {
00194                printf("Deleted group.\n");
00195                group_hnd = NULL;
00196             }
00197             break;
00198 
00199          case 'u': /*list users*/
00200             ZERO_STRUCT(eu);
00201 
00202             eu.in.dom_hnd = sod.out.dom_hnd;
00203             
00204             while(cac_SamEnumUsers(hnd, mem_ctx, &eu)) {
00205                for(i = 0; i < eu.out.num_users; i++) {
00206                   printf(" RID: 0x%x Name: %s\n", eu.out.rids[i], eu.out.names[i]);
00207                }
00208             }
00209 
00210             if(CAC_OP_FAILED(hnd->status)) {
00211                printf("Could not enumerate users. Error: %s\n", nt_errstr(hnd->status));
00212             }
00213 
00214             break;
00215 
00216          case 'a': /*add member to group*/
00217             if(!group_hnd) {
00218                printf("Must open group first!\n");
00219                break;
00220             }
00221 
00222             ZERO_STRUCT(add);
00223 
00224             add.in.group_hnd = group_hnd;
00225 
00226             printf("Enter user RID: 0x");
00227             scanf("%x", &add.in.rid);
00228 
00229             if(!cac_SamAddGroupMember(hnd, mem_ctx, &add)) {
00230                fprintf(stderr, "Could not add user to group. Error: %s\n", nt_errstr(hnd->status));
00231             }
00232             else {
00233                printf("Successfully added user to group\n");
00234             }
00235             break;
00236 
00237          case 'r': /*remove user from group*/
00238             if(!group_hnd) {
00239                printf("Must open group first!\n");
00240                break;
00241             }
00242 
00243             ZERO_STRUCT(del);
00244             del.in.group_hnd = group_hnd;
00245 
00246             printf("Enter RID: 0x");
00247             scanf("%x", &del.in.rid);
00248 
00249             if(!cac_SamRemoveGroupMember(hnd, mem_ctx, &del)) {
00250                fprintf(stderr, "Could not remove user from group. Error: %s\n", nt_errstr(hnd->status));
00251             }
00252             else {
00253                printf("Removed user from group.\n");
00254             }
00255 
00256             break;
00257 
00258          case 'x': /*clear group members*/
00259             if(!group_hnd) {
00260                printf("Must open group first!\n");
00261                break;
00262             }
00263 
00264             if(!cac_SamClearGroupMembers(hnd, mem_ctx, group_hnd)) {
00265                fprintf(stderr, "Could not clear group members. Error: %s\n", nt_errstr(hnd->status));
00266             }
00267             else {
00268                printf("Cleared group members\n");
00269             }
00270 
00271             break;
00272             
00273          case 's': /*set members*/
00274             if(!group_hnd) {
00275                printf("Must open group first!\n");
00276                break;
00277             }
00278 
00279             ZERO_STRUCT(set);
00280 
00281             set.in.group_hnd = group_hnd;
00282 
00283             printf("Enter the number of members: ");
00284             scanf("%d", &set.in.num_members);
00285 
00286             set.in.rids = TALLOC_ARRAY(mem_ctx, uint32, set.in.num_members);
00287 
00288             for(i = 0; i < set.in.num_members; i++) {
00289                printf("Enter RID #%d: 0x", (i+1));
00290                scanf("%x", (set.in.rids + i));
00291             }
00292 
00293             if(!cac_SamSetGroupMembers(hnd, mem_ctx, &set)) {
00294                printf("could not set members. Error: %s\n", nt_errstr(hnd->status));
00295             }
00296             else {
00297                printf("Set users\n");
00298             }
00299 
00300             break;
00301             
00302          case 'g': /*list groups for user*/
00303             ZERO_STRUCT(ou);
00304             ZERO_STRUCT(gg);
00305 
00306             printf("Enter username: ");
00307             cactest_readline(stdin, input);
00308 
00309             if(input[0] != '\0') {
00310                ou.in.name = talloc_strdup(mem_ctx, input);
00311             }
00312             else {
00313                printf("Enter RID: 0x");
00314                scanf("%x", &ou.in.rid);
00315             }
00316             
00317             ou.in.access   = MAXIMUM_ALLOWED_ACCESS;
00318             ou.in.dom_hnd  = sod.out.dom_hnd;
00319 
00320             if(!cac_SamOpenUser(hnd, mem_ctx, &ou)) {
00321                fprintf(stderr, "Could not open user %s. Error: %s\n", ou.in.name, nt_errstr(hnd->status));
00322                break;
00323             }
00324 
00325             /*now find the groups*/
00326             gg.in.user_hnd = ou.out.user_hnd;
00327 
00328             if(!cac_SamGetGroupsForUser(hnd, mem_ctx, &gg)) {
00329                fprintf(stderr, "Could not get groups for user. Error: %s\n", nt_errstr(hnd->status));
00330                break;
00331             }
00332 
00333             cac_SamClose(hnd, mem_ctx, ou.out.user_hnd);
00334 
00335             ZERO_STRUCT(gn);
00336 
00337             gn.in.dom_hnd = sod.out.dom_hnd;
00338             gn.in.num_rids = gg.out.num_groups;
00339             gn.in.rids  = gg.out.rids;
00340 
00341             if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &gn)) {
00342                fprintf(stderr, "Could not get names from RIDs. Error: %s\n", nt_errstr(hnd->status));
00343                break;
00344             }
00345 
00346             printf("%d groups: \n", gn.out.num_names);
00347 
00348             for(i = 0; i < gn.out.num_names; i++) {
00349                printf("RID: 0x%x ", gn.out.map[i].rid);
00350 
00351                if(gn.out.map[i].found)
00352                   printf("Name: %s\n", gn.out.map[i].name);
00353                else
00354                   printf("Unknown RID\n");
00355             }
00356 
00357             break;
00358 
00359          case 'z': /*close group*/
00360             if(!group_hnd) {
00361                printf("Must open group first!\n");
00362                break;
00363             }
00364 
00365             if(!cac_SamClose(hnd, mem_ctx, group_hnd)) {
00366                printf("Could not close group\n");
00367                break;
00368             }
00369 
00370             group_hnd = NULL;
00371             break;
00372 
00373          case 'i': /*get group info*/
00374             if(!group_hnd) {
00375                printf("Must open group first!\n");
00376                break;
00377             }
00378 
00379             ZERO_STRUCT(gi);
00380             gi.in.group_hnd = group_hnd;
00381 
00382             if(!cac_SamGetGroupInfo(hnd, mem_ctx, &gi)) {
00383                printf("Could not get group info. Error: %s\n", nt_errstr(hnd->status));
00384             }
00385             else {
00386                printf("Retrieved Group info\n");
00387                print_cac_group_info(gi.out.info);
00388             }
00389             
00390             break;
00391 
00392          case 'e': /*edit group info*/
00393             if(!group_hnd) {
00394                printf("Must open group first!\n");
00395                break;
00396             }
00397 
00398             ZERO_STRUCT(gi);
00399             ZERO_STRUCT(si);
00400 
00401             gi.in.group_hnd = group_hnd;
00402             
00403             if(!cac_SamGetGroupInfo(hnd, mem_ctx, &gi)) {
00404                printf("Could not get group info. Error: %s\n", nt_errstr(hnd->status));
00405                break;
00406             }
00407 
00408             edit_cac_group_info(mem_ctx, gi.out.info);
00409 
00410             si.in.group_hnd = group_hnd;
00411             si.in.info      = gi.out.info;
00412 
00413             if(!cac_SamSetGroupInfo(hnd, mem_ctx, &si)) {
00414                printf("Could not set group info. Error: %s\n", nt_errstr(hnd->status));
00415             }
00416             else {
00417                printf(" Done.\n");
00418             }
00419 
00420             break;
00421 
00422          case 'n': /*rename group*/
00423             if(!group_hnd) {
00424                printf("Must open group first!\n");
00425                break;
00426             }
00427 
00428             ZERO_STRUCT(rg);
00429 
00430             printf("Enter new group name: ");
00431             cactest_readline(stdin, tmp);
00432 
00433             rg.in.group_hnd = group_hnd;
00434             rg.in.new_name = talloc_strdup(mem_ctx, tmp);
00435 
00436             if(!cac_SamRenameGroup(hnd, mem_ctx, &rg)) 
00437                printf("Could not rename group. Error: %s\n", nt_errstr(hnd->status));
00438             else
00439                printf("Done.\n");
00440 
00441             break;
00442          case 't': /*get security info*/
00443             if(!group_hnd) {
00444                printf("Must open group first!\n");
00445                break;
00446             }
00447 
00448             ZERO_STRUCT(gso);
00449 
00450             gso.in.pol = group_hnd;
00451 
00452             if(!cac_SamGetSecurityObject(hnd, mem_ctx, &gso)) {
00453                printf("Could not get security descriptor info. Error: %s\n", nt_errstr(hnd->status));
00454             }
00455             else {
00456                printf("Got it.\n");
00457             }
00458             break;
00459             
00460          case 'q':
00461             break;
00462 
00463          default:
00464             printf("Invalid command\n");
00465       }
00466    }
00467 
00468    cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
00469 
00470    if(group_hnd)
00471       cac_SamClose(hnd, mem_ctx, group_hnd);
00472 
00473 done:
00474    cac_FreeHandle(hnd);
00475 
00476    talloc_destroy(mem_ctx);
00477 
00478    return 0;
00479 }
00480 

© sourcejam.com 2005-2008