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

ear.c File Reference

#include "libmsrpc.h"
#include "includes.h"

Go to the source code of this file.

Functions

void fill_conn_info (CacServerHandle *hnd)
void get_server_names (TALLOC_CTX *mem_ctx, int *num_names, char ***names)
int main (int argc, char **argv)


Function Documentation

void fill_conn_info CacServerHandle hnd  ) 
 

Definition at line 9 of file ear.c.

References _CACSERVERHANDLE::domain, fprintf(), _CACSERVERHANDLE::password, password, _CACSERVERHANDLE::server, server, SMB_STRDUP, _CACSERVERHANDLE::username, and username.

Referenced by main().

00009                                           {
00010    pstring domain;
00011    pstring username;
00012    pstring password;
00013    pstring server;
00014 
00015    fprintf(stdout, "Enter domain name: ");
00016    fscanf(stdin, "%s", domain);
00017 
00018    fprintf(stdout, "Enter username: ");
00019    fscanf(stdin, "%s", username);
00020 
00021    fprintf(stdout, "Enter password (no input masking): ");
00022    fscanf(stdin, "%s", password);
00023 
00024    fprintf(stdout, "Enter server (ip or name): ");
00025    fscanf(stdin, "%s", server);
00026 
00027    hnd->domain = SMB_STRDUP(domain);
00028    hnd->username = SMB_STRDUP(username);
00029    hnd->password = SMB_STRDUP(password);
00030    hnd->server = SMB_STRDUP(server);
00031 }

void get_server_names TALLOC_CTX mem_ctx,
int *  num_names,
char ***  names
 

Definition at line 33 of file ear.c.

References fprintf(), TALLOC_ARRAY, and talloc_strdup().

Referenced by main().

00033                                                                           {
00034    int i = 0;
00035    pstring tmp;
00036    
00037    fprintf(stdout, "How many names do you want to lookup?: ");
00038    fscanf(stdin, "%d", num_names);
00039 
00040    *names = TALLOC_ARRAY(mem_ctx, char *, *num_names);
00041    if(*names == NULL) {
00042       fprintf(stderr, "No memory for allocation\n");
00043       exit(-1);
00044    }
00045 
00046    for(i = 0; i < *num_names; i++) {
00047       fprintf(stdout, "Enter name: ");
00048       fscanf(stdin, "%s", tmp);
00049       (*names)[i] = talloc_strdup(mem_ctx, tmp);
00050    }
00051 }

int main int  argc,
char **  argv
 

Definition at line 53 of file ear.c.

References cac_Connect(), CAC_DOMAIN_INFO, cac_FreeHandle(), CAC_LOCAL_INFO, cac_LsaClosePolicy(), cac_LsaEnumAccountRights(), cac_LsaFetchSid(), cac_LsaGetNamesFromSids(), cac_LsaGetSidsFromNames(), cac_LsaOpenAccount(), cac_LsaOpenPolicy(), cac_NewServerHandle(), CAC_PARTIAL_SUCCESS, False, fill_conn_info(), fprintf(), get_server_names(), LsaGetNamesFromSids::in, LsaEnumAccountRights::in, LsaOpenAccount::in, LsaGetSidsFromNames::in, LsaFetchSid::in, LsaOpenPolicy::in, nt_errstr(), LsaGetNamesFromSids::out, LsaEnumAccountRights::out, LsaGetSidsFromNames::out, LsaFetchSid::out, LsaOpenPolicy::out, printf(), result, SEC_RIGHT_MAXIMUM_ALLOWED, _CACSERVERHANDLE::server, sid_string_static(), _CACSERVERHANDLE::status, TALLOC_ARRAY, talloc_destroy, talloc_init(), True, and ZERO_STRUCT.

00053                                 {
00054    int i;
00055    int result;
00056    char **names;
00057    int num_names;
00058    int num_sids;
00059    CacServerHandle *hnd = NULL;
00060    POLICY_HND *lsa_pol  = NULL;
00061    TALLOC_CTX *mem_ctx  = NULL;
00062 
00063    DOM_SID *sid_buf     = NULL;
00064 
00065    BOOL sim_partial     = False;
00066 
00067    if(argc > 1 && strcmp(argv[1], "-p") == 0)
00068       sim_partial = True;
00069 
00070    mem_ctx = talloc_init("lsaq");
00071 
00072    hnd = cac_NewServerHandle(False);
00073 
00074    fill_conn_info(hnd);
00075 
00076    get_server_names(mem_ctx, &num_names, &names);
00077 
00078    /*connect to the PDC and open a LSA handle*/
00079    if(!cac_Connect(hnd, NULL)) {
00080       fprintf(stderr, "Could not connect to server.\n Error %s.\n", nt_errstr(hnd->status));
00081       cac_FreeHandle(hnd);
00082       exit(-1);
00083    }
00084 
00085    fprintf(stdout, "Connected to server: %s\n", hnd->server);
00086 
00087    struct LsaOpenPolicy lop;
00088    ZERO_STRUCT(lop);
00089 
00090    lop.in.access = SEC_RIGHT_MAXIMUM_ALLOWED;
00091    lop.in.security_qos = True;
00092 
00093    if(!cac_LsaOpenPolicy(hnd, mem_ctx, &lop)) {
00094       fprintf(stderr, "Could not get lsa policy handle.\n Error: %s\n", nt_errstr(hnd->status));
00095       cac_FreeHandle(hnd);
00096       exit(-1);
00097    }
00098 
00099    fprintf(stdout, "Opened Policy Handle\n");
00100 
00101    /*just to make things neater*/
00102    lsa_pol = lop.out.pol;
00103 
00104    /*fetch the local sid and domain sid for the pdc*/
00105 
00106    struct LsaFetchSid fsop;
00107    ZERO_STRUCT(fsop);
00108 
00109    fsop.in.pol = lsa_pol;
00110    fsop.in.info_class = (CAC_LOCAL_INFO|CAC_DOMAIN_INFO);
00111 
00112    fprintf(stdout, "fetching SID info for %s\n", hnd->server);
00113 
00114    result = cac_LsaFetchSid(hnd, mem_ctx, &fsop);
00115    if(!result) {
00116       fprintf(stderr, "Could not get sid for server: %s\n. Error: %s\n", hnd->server, nt_errstr(hnd->status));
00117       cac_FreeHandle(hnd);
00118       talloc_destroy(mem_ctx);
00119       exit(-1);
00120    }
00121 
00122    if(result == CAC_PARTIAL_SUCCESS) {
00123       fprintf(stdout, "could not retrieve both domain and local information\n");
00124    }
00125    
00126 
00127    fprintf(stdout, "Fetched SID info for %s\n", hnd->server);
00128    if(fsop.out.local_sid != NULL)
00129       fprintf(stdout, " domain: %s. Local SID: %s\n", fsop.out.local_sid->domain, sid_string_static(&fsop.out.local_sid->sid));
00130 
00131    if(fsop.out.domain_sid != NULL)
00132       fprintf(stdout, " domain: %s, Domain SID: %s\n", fsop.out.domain_sid->domain, sid_string_static(&fsop.out.domain_sid->sid));
00133 
00134    fprintf(stdout, "Looking up sids\n");
00135 
00136    
00137    struct LsaGetSidsFromNames gsop;
00138    ZERO_STRUCT(gsop);
00139    
00140    gsop.in.pol       = lsa_pol;
00141    gsop.in.num_names = num_names;
00142    gsop.in.names     = names;
00143 
00144    result = cac_LsaGetSidsFromNames(hnd, mem_ctx, &gsop);
00145 
00146    if(!result) {
00147       fprintf(stderr, "Could not lookup any sids!\n Error: %s\n", nt_errstr(hnd->status));
00148       goto done;
00149    }
00150 
00151    if(result == CAC_PARTIAL_SUCCESS) {
00152       fprintf(stdout, "Not all names could be looked up.\nThe following names were not found:\n");
00153       
00154       for(i = 0; i < (num_names - gsop.out.num_found); i++) {
00155          fprintf(stdout, " %s\n", gsop.out.unknown[i]);
00156       }
00157       
00158       fprintf(stdout, "\n");
00159    }
00160 
00161    /*buffer the sids so we can look them up back to names*/
00162    num_sids = (sim_partial) ? gsop.out.num_found + 2: gsop.out.num_found;
00163    sid_buf = TALLOC_ARRAY(mem_ctx, DOM_SID, num_sids);
00164 
00165    fprintf(stdout, "%d names were resolved: \n", gsop.out.num_found);
00166 
00167 
00168    i = 0;
00169    while(i < gsop.out.num_found) {
00170       fprintf(stdout, " Name: %s\n SID: %s\n\n", gsop.out.sids[i].name, sid_string_static(&gsop.out.sids[i].sid));
00171 
00172       sid_buf[i] = gsop.out.sids[i].sid;
00173 
00174       printf("Attempting to open account\n");
00175 
00176       struct LsaOpenAccount loa;
00177       ZERO_STRUCT(loa);
00178 
00179       loa.in.pol    = lsa_pol;
00180       loa.in.access = SEC_RIGHT_MAXIMUM_ALLOWED;
00181       loa.in.sid    = &gsop.out.sids[i].sid;
00182 
00183       if(!cac_LsaOpenAccount(hnd, mem_ctx, &loa)) {
00184          fprintf(stderr, "Could not open account.\n Error: %s\n", nt_errstr(hnd->status));
00185       }
00186 
00187       printf("\nEnumerating privs:");
00188       struct LsaEnumAccountRights earop;
00189       ZERO_STRUCT(earop);
00190 
00191       earop.in.pol = lsa_pol;
00192 
00193       earop.in.sid = &gsop.out.sids[i].sid;
00194 
00195       if(!cac_LsaEnumAccountRights(hnd, mem_ctx, &earop)) {
00196          fprintf(stderr, "Could not enumerate account rights.\n Error: %s\n", nt_errstr(hnd->status));
00197       }
00198 
00199       int j;
00200       printf( "Rights: ");
00201       for(j = 0; j < earop.out.num_privs; j++) {
00202          printf("  %s\n", earop.out.priv_names[j]);
00203       }
00204 
00205       printf("\n");
00206 
00207 
00208       i++;
00209    }
00210 
00211    /*if we want a partial success to occur below, then add the server's SIDs to the end of the array*/
00212    if(sim_partial) {
00213       sid_buf[i] = fsop.out.local_sid->sid;
00214       sid_buf[i+1] = fsop.out.domain_sid->sid;
00215    }
00216 
00217    fprintf(stdout, "Looking up Names from SIDs\n");
00218 
00219    struct LsaGetNamesFromSids gnop;
00220    ZERO_STRUCT(gnop);
00221 
00222    gnop.in.pol       = lsa_pol;
00223    gnop.in.num_sids  = num_sids;
00224    gnop.in.sids      = sid_buf;
00225 
00226    result = cac_LsaGetNamesFromSids(hnd, mem_ctx, &gnop);
00227 
00228    if(!result) {
00229       fprintf(stderr, "Could not lookup any names!.\n Error: %s\n", nt_errstr(hnd->status));
00230       goto done;
00231    }
00232 
00233    if(result == CAC_PARTIAL_SUCCESS) {
00234       fprintf(stdout, "\nNot all SIDs could be looked up.\n. The following SIDs were not found:\n");
00235 
00236       for(i = 0; i < (num_sids - gnop.out.num_found); i++) {
00237          fprintf(stdout, "SID: %s\n", sid_string_static(&gnop.out.unknown[i]));
00238       }
00239 
00240       fprintf(stdout, "\n");
00241    }
00242 
00243    fprintf(stdout, "%d SIDs were resolved: \n", gnop.out.num_found);
00244    for(i = 0; i < gnop.out.num_found; i++) {
00245       fprintf(stdout, " SID: %s\n Name: %s\n", sid_string_static(&gnop.out.sids[i].sid), gsop.out.sids[i].name);
00246    }
00247    
00248 done:
00249 
00250    if(!cac_LsaClosePolicy(hnd, mem_ctx, lsa_pol)) {
00251       fprintf(stderr, "Could not close LSA policy handle.\n Error: %s\n", nt_errstr(hnd->status));
00252    }
00253    else {
00254       fprintf(stdout, "Closed Policy handle.\n");
00255    }
00256 
00257    cac_FreeHandle(hnd);
00258    talloc_destroy(mem_ctx);
00259 
00260    return 0;
00261 }


© sourcejam.com 2005-2008