1/* 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 2 * 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 3 */ 4 5 6#include "libmsrpc.h" 7#include "includes.h" 8 9void fill_conn_info(CacServerHandle *hnd) { 10 pstring domain; 11 pstring username; 12 pstring password; 13 pstring server; 14 15 fprintf(stdout, "Enter domain name: "); 16 fscanf(stdin, "%s", domain); 17 18 fprintf(stdout, "Enter username: "); 19 fscanf(stdin, "%s", username); 20 21 fprintf(stdout, "Enter password (no input masking): "); 22 fscanf(stdin, "%s", password); 23 24 fprintf(stdout, "Enter server (ip or name): "); 25 fscanf(stdin, "%s", server); 26 27 hnd->domain = SMB_STRDUP(domain); 28 hnd->username = SMB_STRDUP(username); 29 hnd->password = SMB_STRDUP(password); 30 hnd->server = SMB_STRDUP(server); 31} 32 33void get_server_names(TALLOC_CTX *mem_ctx, int *num_names, char ***names) { 34 int i = 0; 35 pstring tmp; 36 37 fprintf(stdout, "How many names do you want to lookup?: "); 38 fscanf(stdin, "%d", num_names); 39 40 *names = TALLOC_ARRAY(mem_ctx, char *, *num_names); 41 if(*names == NULL) { 42 fprintf(stderr, "No memory for allocation\n"); 43 exit(-1); 44 } 45 46 for(i = 0; i < *num_names; i++) { 47 fprintf(stdout, "Enter name: "); 48 fscanf(stdin, "%s", tmp); 49 (*names)[i] = talloc_strdup(mem_ctx, tmp); 50 } 51} 52 53int main(int argc, char **argv) { 54 int i; 55 int result; 56 char **names; 57 int num_names; 58 int num_sids; 59 CacServerHandle *hnd = NULL; 60 POLICY_HND *lsa_pol = NULL; 61 TALLOC_CTX *mem_ctx = NULL; 62 63 DOM_SID *sid_buf = NULL; 64 65 BOOL sim_partial = False; 66 67 if(argc > 1 && strcmp(argv[1], "-p") == 0) 68 sim_partial = True; 69 70 mem_ctx = talloc_init("lsaq"); 71 72 hnd = cac_NewServerHandle(False); 73 74 fill_conn_info(hnd); 75 76 get_server_names(mem_ctx, &num_names, &names); 77 78 /*connect to the PDC and open a LSA handle*/ 79 if(!cac_Connect(hnd, NULL)) { 80 fprintf(stderr, "Could not connect to server.\n Error %s.\n", nt_errstr(hnd->status)); 81 cac_FreeHandle(hnd); 82 exit(-1); 83 } 84 85 fprintf(stdout, "Connected to server: %s\n", hnd->server); 86 87 struct LsaOpenPolicy lop; 88 ZERO_STRUCT(lop); 89 90 lop.in.access = SEC_RIGHT_MAXIMUM_ALLOWED; 91 lop.in.security_qos = True; 92 93 if(!cac_LsaOpenPolicy(hnd, mem_ctx, &lop)) { 94 fprintf(stderr, "Could not get lsa policy handle.\n Error: %s\n", nt_errstr(hnd->status)); 95 cac_FreeHandle(hnd); 96 exit(-1); 97 } 98 99 fprintf(stdout, "Opened Policy Handle\n"); 100 101 /*just to make things neater*/ 102 lsa_pol = lop.out.pol; 103 104 /*fetch the local sid and domain sid for the pdc*/ 105 106 struct LsaFetchSid fsop; 107 ZERO_STRUCT(fsop); 108 109 fsop.in.pol = lsa_pol; 110 fsop.in.info_class = (CAC_LOCAL_INFO|CAC_DOMAIN_INFO); 111 112 fprintf(stdout, "fetching SID info for %s\n", hnd->server); 113 114 result = cac_LsaFetchSid(hnd, mem_ctx, &fsop); 115 if(!result) { 116 fprintf(stderr, "Could not get sid for server: %s\n. Error: %s\n", hnd->server, nt_errstr(hnd->status)); 117 cac_FreeHandle(hnd); 118 talloc_destroy(mem_ctx); 119 exit(-1); 120 } 121 122 if(result == CAC_PARTIAL_SUCCESS) { 123 fprintf(stdout, "could not retrieve both domain and local information\n"); 124 } 125 126 127 fprintf(stdout, "Fetched SID info for %s\n", hnd->server); 128 if(fsop.out.local_sid != NULL) 129 fprintf(stdout, " domain: %s. Local SID: %s\n", fsop.out.local_sid->domain, sid_string_static(&fsop.out.local_sid->sid)); 130 131 if(fsop.out.domain_sid != NULL) 132 fprintf(stdout, " domain: %s, Domain SID: %s\n", fsop.out.domain_sid->domain, sid_string_static(&fsop.out.domain_sid->sid)); 133 134 fprintf(stdout, "\nAttempting to query info policy\n"); 135 136 struct LsaQueryInfoPolicy qop; 137 ZERO_STRUCT(qop); 138 139 qop.in.pol = lsa_pol; 140 141 if(!cac_LsaQueryInfoPolicy(hnd, mem_ctx, &qop)) { 142 fprintf(stderr, "Could not query information policy!.\n Error: %s\n", nt_errstr(hnd->status)); 143 goto done; 144 } 145 146 fprintf(stdout, "Query result: \n"); 147 fprintf(stdout, " domain name: %s\n", qop.out.domain_name); 148 fprintf(stdout, " dns name: %s\n", qop.out.dns_name); 149 fprintf(stdout, " forest name: %s\n", qop.out.forest_name); 150 fprintf(stdout, " domain guid: %s\n", smb_uuid_string_static(*qop.out.domain_guid)); 151 fprintf(stdout, " domain sid: %s\n", sid_string_static(qop.out.domain_sid)); 152 153 fprintf(stdout, "\nLooking up sids\n"); 154 155 struct LsaGetSidsFromNames gsop; 156 ZERO_STRUCT(gsop); 157 158 gsop.in.pol = lsa_pol; 159 gsop.in.num_names = num_names; 160 gsop.in.names = names; 161 162 result = cac_LsaGetSidsFromNames(hnd, mem_ctx, &gsop); 163 164 if(!result) { 165 fprintf(stderr, "Could not lookup any sids!\n Error: %s\n", nt_errstr(hnd->status)); 166 goto done; 167 } 168 169 if(result == CAC_PARTIAL_SUCCESS) { 170 fprintf(stdout, "Not all names could be looked up.\nThe following names were not found:\n"); 171 172 for(i = 0; i < (num_names - gsop.out.num_found); i++) { 173 fprintf(stdout, " %s\n", gsop.out.unknown[i]); 174 } 175 176 fprintf(stdout, "\n"); 177 } 178 179 /*buffer the sids so we can look them up back to names*/ 180 num_sids = (sim_partial) ? gsop.out.num_found + 2: gsop.out.num_found; 181 sid_buf = TALLOC_ARRAY(mem_ctx, DOM_SID, num_sids); 182 183 fprintf(stdout, "%d names were resolved: \n", gsop.out.num_found); 184 185 186 i = 0; 187 while(i < gsop.out.num_found) { 188 fprintf(stdout, " Name: %s\n SID: %s\n\n", gsop.out.sids[i].name, sid_string_static(&gsop.out.sids[i].sid)); 189 190 sid_buf[i] = gsop.out.sids[i].sid; 191 192 i++; 193 } 194 195 /*if we want a partial success to occur below, then add the server's SIDs to the end of the array*/ 196 if(sim_partial) { 197 sid_buf[i] = fsop.out.local_sid->sid; 198 sid_buf[i+1] = fsop.out.domain_sid->sid; 199 } 200 201 fprintf(stdout, "Looking up Names from SIDs\n"); 202 203 struct LsaGetNamesFromSids gnop; 204 ZERO_STRUCT(gnop); 205 206 gnop.in.pol = lsa_pol; 207 gnop.in.num_sids = num_sids; 208 gnop.in.sids = sid_buf; 209 210 result = cac_LsaGetNamesFromSids(hnd, mem_ctx, &gnop); 211 212 if(!result) { 213 fprintf(stderr, "Could not lookup any names!.\n Error: %s\n", nt_errstr(hnd->status)); 214 goto done; 215 } 216 217 if(result == CAC_PARTIAL_SUCCESS) { 218 fprintf(stdout, "\nNot all SIDs could be looked up.\n. The following SIDs were not found:\n"); 219 220 for(i = 0; i < (num_sids - gnop.out.num_found); i++) { 221 fprintf(stdout, "SID: %s\n", sid_string_static(&gnop.out.unknown[i])); 222 } 223 224 fprintf(stdout, "\n"); 225 } 226 227 fprintf(stdout, "%d SIDs were resolved: \n", gnop.out.num_found); 228 for(i = 0; i < gnop.out.num_found; i++) { 229 fprintf(stdout, " SID: %s\n Name: %s\n", sid_string_static(&gnop.out.sids[i].sid), gsop.out.sids[i].name); 230 } 231 232done: 233 234 if(!cac_LsaClosePolicy(hnd, mem_ctx, lsa_pol)) { 235 fprintf(stderr, "Could not close LSA policy handle.\n Error: %s\n", nt_errstr(hnd->status)); 236 } 237 else { 238 fprintf(stdout, "Closed Policy handle.\n"); 239 } 240 241 cac_FreeHandle(hnd); 242 talloc_destroy(mem_ctx); 243 244 return 0; 245} 246