summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/test/lsa/lsapol.c
blob: 58407e4343b38fd5e2b425de08758bdf58bf5b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* simple test code, opens and closes an LSA policy handle using libmsrpc, careful.. there's no password input masking*/

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

void fill_conn_info(CacServerHandle *hnd) {
   pstring domain;
   pstring username;
   pstring password;
   pstring server;

   fprintf(stdout, "Enter domain name: ");
   fscanf(stdin, "%s", domain);

   fprintf(stdout, "Enter username: ");
   fscanf(stdin, "%s", username);

   fprintf(stdout, "Enter password (no input masking): ");
   fscanf(stdin, "%s", password);

   fprintf(stdout, "Enter server (ip or name): ");
   fscanf(stdin, "%s", server);

   hnd->domain = SMB_STRDUP(domain);
   hnd->username = SMB_STRDUP(username);
   hnd->password = SMB_STRDUP(password);
   hnd->server = SMB_STRDUP(server);
}

int main() {
   CacServerHandle *hnd = NULL;
   TALLOC_CTX *mem_ctx;
   struct LsaOpenPolicy op;

   mem_ctx = talloc_init("lsapol");

   
   hnd = cac_NewServerHandle(False);

   /*this line is unnecesary*/
   cac_SetAuthDataFn(hnd, cac_GetAuthDataFn);

   hnd->debug = 0;

   fill_conn_info(hnd);

   /*connect to the server, its name/ip is already in the handle so just pass NULL*/
   if(!cac_Connect(hnd, NULL)) {
      fprintf(stderr, "Could not connect to server. \n Error %s\n errno(%d): %s\n", nt_errstr(hnd->status), errno, strerror(errno));
      cac_FreeHandle(hnd);
      exit(-1);
   }
   else {
      fprintf(stdout, "Connected to server\n");
   }

   op.in.access = GENERIC_EXECUTE_ACCESS;
   op.in.security_qos = True;

   /*open the handle*/
   if(!cac_LsaOpenPolicy(hnd, mem_ctx, &op)) {
      fprintf(stderr, "Could not open policy.\n Error: %s.errno: %d.\n", nt_errstr(hnd->status), errno);
      cac_FreeHandle(hnd);
      exit(-1);
   }
   else {
      fprintf(stdout, "Opened Policy handle\n");
   }

   /*close the handle*/
   if(!cac_LsaClosePolicy(hnd, mem_ctx, op.out.pol)) {
      fprintf(stderr, "Could not close policy. Error: %s\n", nt_errstr(hnd->status));
   }
   else {
      fprintf(stdout, "Closed Policy handle\n");
   }

   /*cleanup*/
   cac_FreeHandle(hnd);

   talloc_destroy(mem_ctx);

   fprintf(stdout, "Free'd server handle\n");

   return 0;
}