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

lsaq.c

Go to the documentation of this file.
00001 /* connects to an LSA, asks for a list of server names,  prints out their sids, then looks up their names from the sids and prints them out again
00002  *  if you run as lsaq -p, then it will simulate a partial success for cac_GetNamesFromSids. It will try to lookup the server's local and domain sids
00003  */
00004 
00005 
00006 #include "libmsrpc.h"
00007 #include "includes.h"
00008 
00009 void fill_conn_info(CacServerHandle *hnd) {
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 }
00032 
00033 void get_server_names(TALLOC_CTX *mem_ctx, int *num_names, char ***names) {
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 }
00052 
00053 int main(int argc, char **argv) {
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, "\nAttempting to query info policy\n");
00135 
00136    struct LsaQueryInfoPolicy qop;
00137    ZERO_STRUCT(qop);
00138 
00139    qop.in.pol = lsa_pol;
00140 
00141    if(!cac_LsaQueryInfoPolicy(hnd, mem_ctx, &qop)) {
00142       fprintf(stderr, "Could not query information policy!.\n Error: %s\n", nt_errstr(hnd->status));
00143       goto done;
00144    }
00145 
00146    fprintf(stdout, "Query result: \n");
00147    fprintf(stdout, " domain name: %s\n", qop.out.domain_name);
00148    fprintf(stdout, " dns name:    %s\n", qop.out.dns_name);
00149    fprintf(stdout, " forest name: %s\n", qop.out.forest_name);
00150    fprintf(stdout, " domain guid: %s\n", smb_uuid_string_static(*qop.out.domain_guid));
00151    fprintf(stdout, " domain sid:  %s\n", sid_string_static(qop.out.domain_sid));
00152 
00153    fprintf(stdout, "\nLooking up sids\n");
00154    
00155    struct LsaGetSidsFromNames gsop;
00156    ZERO_STRUCT(gsop);
00157    
00158    gsop.in.pol       = lsa_pol;
00159    gsop.in.num_names = num_names;
00160    gsop.in.names     = names;
00161 
00162    result = cac_LsaGetSidsFromNames(hnd, mem_ctx, &gsop);
00163 
00164    if(!result) {
00165       fprintf(stderr, "Could not lookup any sids!\n Error: %s\n", nt_errstr(hnd->status));
00166       goto done;
00167    }
00168 
00169    if(result == CAC_PARTIAL_SUCCESS) {
00170       fprintf(stdout, "Not all names could be looked up.\nThe following names were not found:\n");
00171       
00172       for(i = 0; i < (num_names - gsop.out.num_found); i++) {
00173          fprintf(stdout, " %s\n", gsop.out.unknown[i]);
00174       }
00175       
00176       fprintf(stdout, "\n");
00177    }
00178 
00179    /*buffer the sids so we can look them up back to names*/
00180    num_sids = (sim_partial) ? gsop.out.num_found + 2: gsop.out.num_found;
00181    sid_buf = TALLOC_ARRAY(mem_ctx, DOM_SID, num_sids);
00182 
00183    fprintf(stdout, "%d names were resolved: \n", gsop.out.num_found);
00184 
00185 
00186    i = 0;
00187    while(i < gsop.out.num_found) {
00188       fprintf(stdout, " Name: %s\n SID: %s\n\n", gsop.out.sids[i].name, sid_string_static(&gsop.out.sids[i].sid));
00189 
00190       sid_buf[i] = gsop.out.sids[i].sid;
00191 
00192       i++;
00193    }
00194    
00195    /*if we want a partial success to occur below, then add the server's SIDs to the end of the array*/
00196    if(sim_partial) {
00197       sid_buf[i] = fsop.out.local_sid->sid;
00198       sid_buf[i+1] = fsop.out.domain_sid->sid;
00199    }
00200 
00201    fprintf(stdout, "Looking up Names from SIDs\n");
00202 
00203    struct LsaGetNamesFromSids gnop;
00204    ZERO_STRUCT(gnop);
00205 
00206    gnop.in.pol       = lsa_pol;
00207    gnop.in.num_sids  = num_sids;
00208    gnop.in.sids      = sid_buf;
00209 
00210    result = cac_LsaGetNamesFromSids(hnd, mem_ctx, &gnop);
00211 
00212    if(!result) {
00213       fprintf(stderr, "Could not lookup any names!.\n Error: %s\n", nt_errstr(hnd->status));
00214       goto done;
00215    }
00216 
00217    if(result == CAC_PARTIAL_SUCCESS) {
00218       fprintf(stdout, "\nNot all SIDs could be looked up.\n. The following SIDs were not found:\n");
00219 
00220       for(i = 0; i < (num_sids - gnop.out.num_found); i++) {
00221          fprintf(stdout, "SID: %s\n", sid_string_static(&gnop.out.unknown[i]));
00222       }
00223 
00224       fprintf(stdout, "\n");
00225    }
00226 
00227    fprintf(stdout, "%d SIDs were resolved: \n", gnop.out.num_found);
00228    for(i = 0; i < gnop.out.num_found; i++) {
00229       fprintf(stdout, " SID: %s\n Name: %s\n", sid_string_static(&gnop.out.sids[i].sid), gsop.out.sids[i].name);
00230    }
00231    
00232 done:
00233 
00234    if(!cac_LsaClosePolicy(hnd, mem_ctx, lsa_pol)) {
00235       fprintf(stderr, "Could not close LSA policy handle.\n Error: %s\n", nt_errstr(hnd->status));
00236    }
00237    else {
00238       fprintf(stdout, "Closed Policy handle.\n");
00239    }
00240 
00241    cac_FreeHandle(hnd);
00242    talloc_destroy(mem_ctx);
00243 
00244    return 0;
00245 }

© sourcejam.com 2005-2008