58407e4343b38fd5e2b425de08758bdf58bf5b12
[samba.git] / examples / libmsrpc / test / lsa / lsapol.c
1 /* simple test code, opens and closes an LSA policy handle using libmsrpc, careful.. there's no password input masking*/
2
3 #include "includes.h"
4 #include "libmsrpc.h"
5
6 void fill_conn_info(CacServerHandle *hnd) {
7    pstring domain;
8    pstring username;
9    pstring password;
10    pstring server;
11
12    fprintf(stdout, "Enter domain name: ");
13    fscanf(stdin, "%s", domain);
14
15    fprintf(stdout, "Enter username: ");
16    fscanf(stdin, "%s", username);
17
18    fprintf(stdout, "Enter password (no input masking): ");
19    fscanf(stdin, "%s", password);
20
21    fprintf(stdout, "Enter server (ip or name): ");
22    fscanf(stdin, "%s", server);
23
24    hnd->domain = SMB_STRDUP(domain);
25    hnd->username = SMB_STRDUP(username);
26    hnd->password = SMB_STRDUP(password);
27    hnd->server = SMB_STRDUP(server);
28 }
29
30 int main() {
31    CacServerHandle *hnd = NULL;
32    TALLOC_CTX *mem_ctx;
33    struct LsaOpenPolicy op;
34
35    mem_ctx = talloc_init("lsapol");
36
37    
38    hnd = cac_NewServerHandle(False);
39
40    /*this line is unnecesary*/
41    cac_SetAuthDataFn(hnd, cac_GetAuthDataFn);
42
43    hnd->debug = 0;
44
45    fill_conn_info(hnd);
46
47    /*connect to the server, its name/ip is already in the handle so just pass NULL*/
48    if(!cac_Connect(hnd, NULL)) {
49       fprintf(stderr, "Could not connect to server. \n Error %s\n errno(%d): %s\n", nt_errstr(hnd->status), errno, strerror(errno));
50       cac_FreeHandle(hnd);
51       exit(-1);
52    }
53    else {
54       fprintf(stdout, "Connected to server\n");
55    }
56
57    op.in.access = GENERIC_EXECUTE_ACCESS;
58    op.in.security_qos = True;
59
60    /*open the handle*/
61    if(!cac_LsaOpenPolicy(hnd, mem_ctx, &op)) {
62       fprintf(stderr, "Could not open policy.\n Error: %s.errno: %d.\n", nt_errstr(hnd->status), errno);
63       cac_FreeHandle(hnd);
64       exit(-1);
65    }
66    else {
67       fprintf(stdout, "Opened Policy handle\n");
68    }
69
70    /*close the handle*/
71    if(!cac_LsaClosePolicy(hnd, mem_ctx, op.out.pol)) {
72       fprintf(stderr, "Could not close policy. Error: %s\n", nt_errstr(hnd->status));
73    }
74    else {
75       fprintf(stdout, "Closed Policy handle\n");
76    }
77
78    /*cleanup*/
79    cac_FreeHandle(hnd);
80
81    talloc_destroy(mem_ctx);
82
83    fprintf(stdout, "Free'd server handle\n");
84
85    return 0;
86 }
87