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

lsatrust.c

Go to the documentation of this file.
00001 /*queries trusted domain information*/
00002 
00003 #include "libmsrpc.h"
00004 #include "includes.h"
00005 
00006 #define MAX_STRING_LEN 50;
00007 
00008 void print_info(LSA_TRUSTED_DOMAIN_INFO *info) {
00009    switch(info->info_class) {
00010       case CAC_INFO_TRUSTED_DOMAIN_FULL_INFO:
00011       case CAC_INFO_TRUSTED_DOMAIN_INFO_ALL:
00012          printf("     Domain Name:     %s\n", unistr2_static(&info->info_ex.domain_name.unistring));
00013          printf("     Netbios Name:    %s\n", unistr2_static(&info->info_ex.netbios_name.unistring));
00014          printf("     Domain Sid:      %s\n", sid_string_static(&info->info_ex.sid.sid));
00015          printf("     Trust direction: %d\n", info->info_ex.trust_direction);
00016          printf("     Trust Type:      %d\n", info->info_ex.trust_type);
00017          printf("     Trust attr:      %d\n", info->info_ex.trust_attributes); 
00018          printf("     Posix Offset:    %d\n", info->posix_offset.posix_offset);
00019          break;
00020    }
00021 }
00022 
00023 int main() {
00024    CacServerHandle *hnd = NULL;
00025    TALLOC_CTX *mem_ctx  = NULL;
00026    POLICY_HND *lsa_pol  = NULL;
00027 
00028    int i;
00029 
00030    mem_ctx = talloc_init("lsatrust");
00031 
00032    hnd = cac_NewServerHandle(False);
00033 
00034    /*malloc some memory so get_auth_data_fn can work*/
00035    hnd->username     = SMB_MALLOC_ARRAY(char, sizeof(fstring));
00036    hnd->domain       = SMB_MALLOC_ARRAY(char, sizeof(fstring));
00037    hnd->netbios_name = SMB_MALLOC_ARRAY(char, sizeof(fstring));
00038    hnd->password     = SMB_MALLOC_ARRAY(char, sizeof(fstring));
00039 
00040    hnd->server       = SMB_MALLOC_ARRAY(char, sizeof(fstring));
00041 
00042 
00043    printf("Server: ");
00044    fscanf(stdin, "%s", hnd->server);
00045 
00046    printf("Connecting to server....\n");
00047 
00048    if(!cac_Connect(hnd, NULL)) {
00049       fprintf(stderr, "Could not connect to server.\n Error: %s\n errno %s\n", nt_errstr(hnd->status), strerror(errno));
00050       cac_FreeHandle(hnd);
00051       exit(-1);
00052    }
00053 
00054    printf("Connected to server\n");
00055 
00056    struct LsaOpenPolicy lop;
00057    ZERO_STRUCT(lop);
00058 
00059    lop.in.access = SEC_RIGHT_MAXIMUM_ALLOWED;
00060    lop.in.security_qos = True;
00061 
00062 
00063    if(!cac_LsaOpenPolicy(hnd, mem_ctx, &lop)) {
00064       fprintf(stderr, "Could not open policy handle.\n Error: %s\n", nt_errstr(hnd->status));
00065       cac_FreeHandle(hnd);
00066       exit(-1);
00067    }
00068 
00069    lsa_pol = lop.out.pol;
00070 
00071    printf("Enumerating Trusted Domains\n");
00072 
00073    struct LsaEnumTrustedDomains etd;
00074    ZERO_STRUCT(etd);
00075 
00076    etd.in.pol = lsa_pol;
00077 
00078    while(cac_LsaEnumTrustedDomains(hnd, mem_ctx, &etd)) {
00079       printf(" Enumerated %d domains\n", etd.out.num_domains);
00080 
00081       for(i = 0; i < etd.out.num_domains; i++) {
00082          printf("   Name: %s\n", etd.out.domain_names[i]);
00083          printf("   SID:  %s\n", sid_string_static(&etd.out.domain_sids[i]));
00084 
00085          printf("\n   Attempting to open domain...\n");
00086 
00087          struct LsaOpenTrustedDomain otd;
00088          ZERO_STRUCT(otd);
00089 
00090          otd.in.pol = lsa_pol;
00091          otd.in.domain_sid = &etd.out.domain_sids[i];
00092          otd.in.access = SEC_RIGHT_MAXIMUM_ALLOWED;
00093 
00094          /*try to query trusted domain info by name*/
00095          struct LsaQueryTrustedDomainInfo qtd;
00096          ZERO_STRUCT(qtd);
00097 
00098          qtd.in.pol = lsa_pol;
00099          qtd.in.domain_name = etd.out.domain_names[i];
00100 
00101          
00102          int j;
00103          for(j = 0; j < 100; j++ ) {
00104             qtd.in.info_class = j;
00105 
00106             printf("    Querying trustdom by name\n");
00107             if(!cac_LsaQueryTrustedDomainInfo(hnd, mem_ctx, &qtd)) {
00108                fprintf(stderr, "    could not query trusted domain info.\n    Error %s\n", nt_errstr(hnd->status));
00109                continue;
00110             }
00111             
00112             printf("    info_class %d succeeded\n", j); 
00113             printf("    Query result:\n");    
00114             printf("     size %d\n", sizeof(*qtd.out.info));
00115          }
00116 
00117          /*try to query trusted domain info by SID*/
00118          printf("    Querying trustdom by sid\n");
00119          qtd.in.domain_sid = &etd.out.domain_sids[i];
00120          if(!cac_LsaQueryTrustedDomainInfo(hnd, mem_ctx, &qtd)) {
00121             fprintf(stderr, "    could not query trusted domain info.\n    Error %s\n", nt_errstr(hnd->status));
00122             continue;
00123          }
00124 
00125          printf("    Query result:\n");    
00126 /*         print_info(qtd.out.info);*/
00127 
00128          if(CAC_OP_FAILED(hnd->status)) {
00129             fprintf(stderr, "    Could not enum sids.\n    Error: %s\n", nt_errstr(hnd->status));
00130             continue;
00131          }
00132       }
00133 
00134       printf("\n");
00135    }
00136 
00137    if(CAC_OP_FAILED(hnd->status)) {
00138       fprintf(stderr, "Error while enumerating trusted domains.\n Error: %s\n", nt_errstr(hnd->status));
00139       goto done;
00140    }
00141 
00142 done:
00143    if(!cac_LsaClosePolicy(hnd, mem_ctx, lsa_pol)) {
00144       fprintf(stderr, "Could not close policy handle.\n Error: %s\n", nt_errstr(hnd->status));
00145    }
00146 
00147    cac_FreeHandle(hnd);
00148    talloc_destroy(mem_ctx);
00149 
00150    return 0;
00151 }

© sourcejam.com 2005-2008