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

cacusermgr.c File Reference

#include "cacusermgr.h"

Go to the source code of this file.

Defines

#define DEFAULT_MENU_LINES   15

Functions

void create_menu (CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd)
void main_menu (CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd)
int main (int argc, char **argv)


Define Documentation

#define DEFAULT_MENU_LINES   15
 

Definition at line 23 of file cacusermgr.c.


Function Documentation

void create_menu CacServerHandle hnd,
TALLOC_CTX mem_ctx,
POLICY_HND dom_hnd
 

Definition at line 26 of file cacusermgr.c.

References ACB_NORMAL, ACB_WSTRUST, cac_SamCreateGroup(), cac_SamCreateUser(), group_menu(), SamCreateUser::in, SamCreateGroup::in, MAXIMUM_ALLOWED_ACCESS, mgr_getline(), SamCreateGroup::out, SamCreateUser::out, printerr(), printf(), _CACSERVERHANDLE::status, strlower_m(), talloc_asprintf(), talloc_strdup(), user_menu(), and ZERO_STRUCT.

Referenced by main_menu().

00026                                                                                  {
00027    struct SamCreateUser  cu;
00028    struct SamCreateGroup cg;
00029 
00030    fstring in;
00031    fstring tmp;
00032 
00033    if(!hnd || !mem_ctx || !dom_hnd) {
00034       printf("No Handle to SAM.\n");
00035       return;
00036    }
00037 
00038    /*the menu*/
00039    in[0] = '\0';
00040    while(in[0] != 'c' && in[0] != 'C' && in[0] != 'q' && in[0] != 'Q') {
00041       printf("\n");
00042       printf("[u] Create User\n");
00043       printf("[g] Create Group\n");
00044       printf("[m] Create Machine Account\n");
00045       printf("[c] Cancel\n\n");
00046 
00047       printf("Command: ");
00048       mgr_getline(in);
00049 
00050       printf("\n");
00051 
00052       switch(in[0]) {
00053          case 'u': /*create user*/
00054          case 'U':
00055             ZERO_STRUCT(cu);
00056             cu.in.dom_hnd = dom_hnd;
00057             cu.in.acb_mask = ACB_NORMAL;
00058 
00059             printf("Enter name: ");
00060             mgr_getline(tmp);
00061             cu.in.name = talloc_strdup(mem_ctx, tmp);
00062 
00063             if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
00064                printerr("Could not create user.", hnd->status);
00065             }
00066             else {
00067                user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
00068             }
00069 
00070             /*this will break the loop and send us back to the main menu*/
00071             in[0] = 'c';
00072             break;
00073 
00074          case 'g': /*create group*/
00075          case 'G':
00076             ZERO_STRUCT(cg);
00077             cg.in.dom_hnd = dom_hnd;
00078             cg.in.access  = MAXIMUM_ALLOWED_ACCESS;
00079 
00080             printf("Enter name: ");
00081             mgr_getline(tmp);
00082             cg.in.name = talloc_strdup(mem_ctx, tmp);
00083 
00084             if(!cac_SamCreateGroup(hnd, mem_ctx, &cg)) {
00085                printerr("Could not create group.", hnd->status);
00086             }
00087             else {
00088                group_menu(hnd, mem_ctx, dom_hnd, cg.out.group_hnd);
00089             }
00090 
00091             /*this will break the loop and send us back to the main menu*/
00092             in[0] = 'c';
00093             break;
00094 
00095          case 'm': /*create machine account*/
00096          case 'M':
00097             ZERO_STRUCT(cu);
00098             cu.in.dom_hnd  = dom_hnd;
00099             cu.in.acb_mask = ACB_WSTRUST;
00100 
00101             printf("Enter machine name: ");
00102             mgr_getline(tmp);
00103 
00104             /*make sure we have a $ on the end*/
00105             if(tmp[strlen(tmp) - 1] != '$')
00106                cu.in.name = talloc_asprintf(mem_ctx, "%s$", tmp);
00107             else
00108                cu.in.name = talloc_strdup(mem_ctx, tmp);
00109 
00110             strlower_m(cu.in.name);
00111 
00112             printf("Creating account: %s\n", cu.in.name);
00113 
00114             if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
00115                printerr("Could not create account.", hnd->status);
00116             }
00117             else {
00118                user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
00119             }
00120 
00121             /*this will break the loop and send us back to the main menu*/
00122             in[0] = 'c';
00123             break;
00124 
00125          case 'c': /*cancel*/
00126          case 'C':
00127          case 'q':
00128          case 'Q':
00129             /*do nothing*/
00130             break;
00131 
00132          default:
00133             printf("Invalid option\n");
00134       }
00135    }
00136 
00137    return;
00138 }

int main int  argc,
char **  argv
 

first initialize the server handle with what we have

Definition at line 292 of file cacusermgr.c.

References cac_Connect(), cac_FreeHandle(), cac_NewServerHandle(), cac_SamClose(), cac_SamOpenDomain(), cleanup(), SamOpenDomain::in, main_menu(), MAXIMUM_ALLOWED_ACCESS, nt_errstr(), SamOpenDomain::out, printf(), process_cmd_line(), _CACSERVERHANDLE::server, _CACSERVERHANDLE::status, talloc_destroy, talloc_init(), True, usage(), and ZERO_STRUCT.

00292                                 {
00293    CacServerHandle *hnd = NULL;
00294    TALLOC_CTX *mem_ctx  = NULL;
00295 
00296    struct SamOpenDomain sod;
00297 
00298    mem_ctx = talloc_init("cacusermgr");
00299    if(!mem_ctx) {
00300       printf("Could not initialize Talloc Context\n");
00301       exit(-1);
00302    }
00303 
00305    hnd = cac_NewServerHandle(True);
00306    if(!hnd) {
00307       printf("Could not create server handle\n");
00308       exit(-1);
00309    }
00310 
00311    /*fill in the blanks*/
00312    if(!process_cmd_line(hnd, mem_ctx, argc, argv))
00313       usage();
00314 
00315    if(!cac_Connect(hnd, NULL)) {
00316       printf("Could not connect to server %s. %s\n", hnd->server, nt_errstr(hnd->status));
00317       exit(-1);
00318    }
00319 
00320    /*open the domain sam*/
00321    ZERO_STRUCT(sod);
00322    sod.in.access = MAXIMUM_ALLOWED_ACCESS;
00323 
00324    if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
00325       printf("Could not open handle to domain SAM. %s\n", nt_errstr(hnd->status));
00326       goto cleanup;
00327    }
00328 
00329    main_menu(hnd, mem_ctx, sod.out.dom_hnd);
00330 
00331 cleanup:
00332 
00333    if(sod.out.dom_hnd)
00334       cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
00335 
00336    if(sod.out.sam)
00337       cac_SamClose(hnd, mem_ctx, sod.out.sam);
00338 
00339    cac_FreeHandle(hnd);
00340 
00341    talloc_destroy(mem_ctx);
00342 
00343    return 0;
00344 }

void main_menu CacServerHandle hnd,
TALLOC_CTX mem_ctx,
POLICY_HND dom_hnd
 

Definition at line 140 of file cacusermgr.c.

References ACB_NORMAL, ACB_WSTRUST, CAC_GROUP_RID, CAC_OP_FAILED, cac_SamEnumGroups(), cac_SamEnumUsers(), cac_SamFlush(), cac_SamOpenGroup(), cac_SamOpenUser(), CAC_USER_RID, create_menu(), group_menu(), SamEnumGroups::in, SamEnumUsers::in, SamOpenGroup::in, SamOpenUser::in, SamFlush::in, SamCreateGroup::in, MAXIMUM_ALLOWED_ACCESS, mgr_getline(), name, SamEnumGroups::out, SamEnumUsers::out, SamOpenGroup::out, SamOpenUser::out, print_rid_list(), printerr(), printf(), rid_or_name(), _CACSERVERHANDLE::status, uint32, user_menu(), and ZERO_STRUCT.

Referenced by main().

00140                                                                                {
00141    fstring in;
00142    
00143    uint32 rid_type = 0;
00144 
00145    struct SamOpenUser   openu;
00146    struct SamOpenGroup  openg;
00147    struct SamEnumUsers  enumu;
00148    struct SamEnumGroups enumg;
00149    struct SamFlush      flush;
00150 
00151    char *name = NULL;
00152    uint32 rid = 0;
00153 
00154    if(!hnd || !mem_ctx || !dom_hnd) {
00155       printf("No handle to SAM.\n");
00156       return;
00157    }
00158 
00159    /*initialize this here and don't worry about it later*/
00160    ZERO_STRUCT(flush);
00161    flush.in.dom_hnd = dom_hnd;
00162 
00163    in[0] = '\0';
00164 
00165    /*handle the menu and commands*/
00166    while(in[0] != 'q' && in[0] != 'Q') {
00167       printf("\n");
00168 
00169       printf("[o] Open User or Group\n");
00170       printf("[c] Create Account or Group\n");
00171       printf("[u] List Users\n");
00172       printf("[g] List Groups\n");
00173       printf("[m] List Machine Accounts\n");
00174       printf("[q] Quit\n\n");
00175 
00176       printf("Command: ");
00177 
00178       mgr_getline(in);
00179 
00180       printf("\n");
00181 
00182       switch(in[0]) {
00183          case 'o': /*open user or group*/
00184          case 'O':
00185             printf("Enter RID or Name: ");
00186             rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &rid, &name);
00187 
00188             if(rid_type == CAC_USER_RID) {
00189                ZERO_STRUCT(openu);
00190                openu.in.dom_hnd = dom_hnd;
00191                openu.in.rid     = rid;
00192                openu.in.access  = MAXIMUM_ALLOWED_ACCESS;
00193 
00194                if(!cac_SamOpenUser(hnd, mem_ctx, &openu))
00195                   printerr("Could not open user.", hnd->status);
00196                else {
00197                   user_menu(hnd, mem_ctx, dom_hnd, openu.out.user_hnd);
00198 
00199                   if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
00200                      printerr("Lost handle while flushing SAM.", hnd->status);
00201                      /*we want to quit*/
00202                      in[0] = 'q';
00203                   }
00204                }
00205             }
00206             else if(rid_type == CAC_GROUP_RID) {
00207                ZERO_STRUCT(openg);
00208                openg.in.dom_hnd = dom_hnd;
00209                openg.in.rid     = rid;
00210                openg.in.access  = MAXIMUM_ALLOWED_ACCESS;
00211 
00212                if(!cac_SamOpenGroup(hnd, mem_ctx, &openg))
00213                   printerr("Could not open group.", hnd->status);
00214                else {
00215                   group_menu(hnd, mem_ctx, dom_hnd, openg.out.group_hnd);
00216 
00217                   if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
00218                      printerr("Lost handle while flushing SAM.", hnd->status);
00219                      /*we want to quit*/
00220                      in[0] = 'q';
00221                   }
00222                }
00223             }
00224             else {
00225                printf("Unknown RID/Name.\n");
00226             }
00227                
00228             break;
00229 
00230          case 'c': /*create account/group*/
00231          case 'C':
00232             create_menu(hnd, mem_ctx, dom_hnd);
00233             if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
00234                printerr("Lost handle while flushing SAM.", hnd->status);
00235                /*we want to quit*/
00236                in[0] = 'q';
00237             }
00238             break;
00239 
00240          case 'u': /*list users*/
00241          case 'U':
00242             ZERO_STRUCT(enumu);
00243             enumu.in.dom_hnd = dom_hnd;
00244             enumu.in.acb_mask = ACB_NORMAL;
00245 
00246             printf("Users:\n");
00247             while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
00248                print_rid_list(enumu.out.rids, enumu.out.names, enumu.out.num_users);
00249             }
00250             if(CAC_OP_FAILED(hnd->status))
00251                printerr("Error occured while enumerating users.", hnd->status);
00252             break;
00253 
00254          case 'g': /*list groups*/
00255          case 'G':
00256             ZERO_STRUCT(enumg);
00257             enumg.in.dom_hnd = dom_hnd;
00258 
00259             while(cac_SamEnumGroups(hnd, mem_ctx, &enumg)) {
00260                print_rid_list( enumg.out.rids, enumg.out.names, enumg.out.num_groups);
00261             }
00262 
00263             if(CAC_OP_FAILED(hnd->status))
00264                printerr("Error occured while enumerating groups.", hnd->status);
00265             break;
00266 
00267          case 'm': /*list machine accounts*/
00268          case 'M':
00269             ZERO_STRUCT(enumu);
00270             enumu.in.dom_hnd = dom_hnd;
00271             enumu.in.acb_mask = ACB_WSTRUST;
00272 
00273             printf("Users:\n");
00274             while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
00275                print_rid_list( enumu.out.rids, enumu.out.names, enumu.out.num_users);
00276             }
00277             if(CAC_OP_FAILED(hnd->status))
00278                printerr("Error occured while enumerating accounts.", hnd->status);
00279             break;
00280 
00281          case 'q': /*quit*/
00282          case 'Q':
00283             /*just do nothing*/
00284             break;
00285 
00286          default:
00287             printf("Invalid Command.\n");
00288       }
00289    }
00290 }


© sourcejam.com 2005-2008