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
9 void fill_conn_info(CacServerHandle *hnd) {
15 fprintf(stdout, "Enter domain name: ");
16 fscanf(stdin, "%s", domain);
18 fprintf(stdout, "Enter username: ");
19 fscanf(stdin, "%s", username);
21 fprintf(stdout, "Enter password (no input masking): ");
22 fscanf(stdin, "%s", password);
24 fprintf(stdout, "Enter server (ip or name): ");
25 fscanf(stdin, "%s", server);
27 hnd->domain = SMB_STRDUP(domain);
28 hnd->username = SMB_STRDUP(username);
29 hnd->password = SMB_STRDUP(password);
30 hnd->server = SMB_STRDUP(server);
33 void get_server_names(TALLOC_CTX *mem_ctx, int *num_names, char ***names) {
37 fprintf(stdout, "How many names do you want to lookup?: ");
38 fscanf(stdin, "%d", num_names);
40 *names = TALLOC_ARRAY(mem_ctx, char *, *num_names);
42 fprintf(stderr, "No memory for allocation\n");
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);
53 int main(int argc, char **argv) {
59 CacServerHandle *hnd = NULL;
60 POLICY_HND *lsa_pol = NULL;
61 TALLOC_CTX *mem_ctx = NULL;
63 DOM_SID *sid_buf = NULL;
65 BOOL sim_partial = False;
67 if(argc > 1 && strcmp(argv[1], "-p") == 0)
70 mem_ctx = talloc_init("lsaq");
72 hnd = cac_NewServerHandle(False);
76 get_server_names(mem_ctx, &num_names, &names);
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));
85 fprintf(stdout, "Connected to server: %s\n", hnd->server);
87 struct LsaOpenPolicy lop;
90 lop.in.access = SEC_RIGHT_MAXIMUM_ALLOWED;
91 lop.in.security_qos = True;
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));
99 fprintf(stdout, "Opened Policy Handle\n");
101 /*just to make things neater*/
102 lsa_pol = lop.out.pol;
104 /*fetch the local sid and domain sid for the pdc*/
106 struct LsaFetchSid fsop;
109 fsop.in.pol = lsa_pol;
110 fsop.in.info_class = (CAC_LOCAL_INFO|CAC_DOMAIN_INFO);
112 fprintf(stdout, "fetching SID info for %s\n", hnd->server);
114 result = cac_LsaFetchSid(hnd, mem_ctx, &fsop);
116 fprintf(stderr, "Could not get sid for server: %s\n. Error: %s\n", hnd->server, nt_errstr(hnd->status));
118 talloc_destroy(mem_ctx);
122 if(result == CAC_PARTIAL_SUCCESS) {
123 fprintf(stdout, "could not retrieve both domain and local information\n");
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));
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));
134 fprintf(stdout, "Looking up sids\n");
137 struct LsaGetSidsFromNames gsop;
140 gsop.in.pol = lsa_pol;
141 gsop.in.num_names = num_names;
142 gsop.in.names = names;
144 result = cac_LsaGetSidsFromNames(hnd, mem_ctx, &gsop);
147 fprintf(stderr, "Could not lookup any sids!\n Error: %s\n", nt_errstr(hnd->status));
151 if(result == CAC_PARTIAL_SUCCESS) {
152 fprintf(stdout, "Not all names could be looked up.\nThe following names were not found:\n");
154 for(i = 0; i < (num_names - gsop.out.num_found); i++) {
155 fprintf(stdout, " %s\n", gsop.out.unknown[i]);
158 fprintf(stdout, "\n");
161 /*buffer the sids so we can look them up back to names*/
162 num_sids = (sim_partial) ? gsop.out.num_found + 2: gsop.out.num_found;
163 sid_buf = TALLOC_ARRAY(mem_ctx, DOM_SID, num_sids);
165 fprintf(stdout, "%d names were resolved: \n", gsop.out.num_found);
169 while(i < gsop.out.num_found) {
170 fprintf(stdout, " Name: %s\n SID: %s\n\n", gsop.out.sids[i].name, sid_string_static(&gsop.out.sids[i].sid));
172 sid_buf[i] = gsop.out.sids[i].sid;
174 printf("Attempting to open account\n");
176 struct LsaOpenAccount loa;
179 loa.in.pol = lsa_pol;
180 loa.in.access = SEC_RIGHT_MAXIMUM_ALLOWED;
181 loa.in.sid = &gsop.out.sids[i].sid;
183 if(!cac_LsaOpenAccount(hnd, mem_ctx, &loa)) {
184 fprintf(stderr, "Could not open account.\n Error: %s\n", nt_errstr(hnd->status));
187 printf("\nEnumerating privs:");
188 struct LsaEnumAccountRights earop;
191 earop.in.pol = lsa_pol;
193 earop.in.sid = &gsop.out.sids[i].sid;
195 if(!cac_LsaEnumAccountRights(hnd, mem_ctx, &earop)) {
196 fprintf(stderr, "Could not enumerate account rights.\n Error: %s\n", nt_errstr(hnd->status));
201 for(j = 0; j < earop.out.num_privs; j++) {
202 printf(" %s\n", earop.out.priv_names[j]);
211 /*if we want a partial success to occur below, then add the server's SIDs to the end of the array*/
213 sid_buf[i] = fsop.out.local_sid->sid;
214 sid_buf[i+1] = fsop.out.domain_sid->sid;
217 fprintf(stdout, "Looking up Names from SIDs\n");
219 struct LsaGetNamesFromSids gnop;
222 gnop.in.pol = lsa_pol;
223 gnop.in.num_sids = num_sids;
224 gnop.in.sids = sid_buf;
226 result = cac_LsaGetNamesFromSids(hnd, mem_ctx, &gnop);
229 fprintf(stderr, "Could not lookup any names!.\n Error: %s\n", nt_errstr(hnd->status));
233 if(result == CAC_PARTIAL_SUCCESS) {
234 fprintf(stdout, "\nNot all SIDs could be looked up.\n. The following SIDs were not found:\n");
236 for(i = 0; i < (num_sids - gnop.out.num_found); i++) {
237 fprintf(stdout, "SID: %s\n", sid_string_static(&gnop.out.unknown[i]));
240 fprintf(stdout, "\n");
243 fprintf(stdout, "%d SIDs were resolved: \n", gnop.out.num_found);
244 for(i = 0; i < gnop.out.num_found; i++) {
245 fprintf(stdout, " SID: %s\n Name: %s\n", sid_string_static(&gnop.out.sids[i].sid), gsop.out.sids[i].name);
250 if(!cac_LsaClosePolicy(hnd, mem_ctx, lsa_pol)) {
251 fprintf(stderr, "Could not close LSA policy handle.\n Error: %s\n", nt_errstr(hnd->status));
254 fprintf(stdout, "Closed Policy handle.\n");
258 talloc_destroy(mem_ctx);