r14363: Remove credentials.h from the global includes.
[kai/samba.git] / source4 / torture / rpc / session_key.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_lsa.h"
26
27 #include "auth/credentials/credentials.h"
28 #include "torture/rpc/proto.h"
29 #include "libcli/auth/proto.h"
30
31 static void init_lsa_String(struct lsa_String *name, const char *s)
32 {
33         name->string = s;
34 }
35
36 static BOOL test_CreateSecret_basic(struct dcerpc_pipe *p, 
37                                     TALLOC_CTX *mem_ctx, 
38                                     struct policy_handle *handle)
39 {
40         NTSTATUS status;
41         struct lsa_CreateSecret r;
42         struct lsa_SetSecret r3;
43         struct lsa_QuerySecret r4;
44         struct policy_handle sec_handle;
45         struct lsa_Delete d;
46         struct lsa_DATA_BUF buf1;
47         struct lsa_DATA_BUF_PTR bufp1;
48         DATA_BLOB enc_key;
49         BOOL ret = True;
50         DATA_BLOB session_key;
51         NTTIME old_mtime, new_mtime;
52         DATA_BLOB blob1, blob2;
53         const char *secret1 = "abcdef12345699qwerty";
54         char *secret2;
55         char *secname;
56
57         secname = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
58
59         printf("Testing CreateSecret of %s\n", secname);
60                 
61         init_lsa_String(&r.in.name, secname);
62         
63         r.in.handle = handle;
64         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65         r.out.sec_handle = &sec_handle;
66         
67         status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
68         if (!NT_STATUS_IS_OK(status)) {
69                 printf("CreateSecret failed - %s\n", nt_errstr(status));
70                 return False;
71         }
72         
73         status = dcerpc_fetch_session_key(p, &session_key);
74         if (!NT_STATUS_IS_OK(status)) {
75                 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
76                 ret = False;
77         }
78         
79         enc_key = sess_encrypt_string(secret1, &session_key);
80         
81         r3.in.sec_handle = &sec_handle;
82         r3.in.new_val = &buf1;
83         r3.in.old_val = NULL;
84         r3.in.new_val->data = enc_key.data;
85         r3.in.new_val->length = enc_key.length;
86         r3.in.new_val->size = enc_key.length;
87         
88         printf("Testing SetSecret\n");
89         
90         status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
91         if (!NT_STATUS_IS_OK(status)) {
92                 printf("SetSecret failed - %s\n", nt_errstr(status));
93                 ret = False;
94         }
95                 
96         r3.in.sec_handle = &sec_handle;
97         r3.in.new_val = &buf1;
98         r3.in.old_val = NULL;
99         r3.in.new_val->data = enc_key.data;
100         r3.in.new_val->length = enc_key.length;
101         r3.in.new_val->size = enc_key.length;
102         
103         /* break the encrypted data */
104         enc_key.data[0]++;
105         
106         printf("Testing SetSecret with broken key\n");
107         
108         status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
109         if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
110                 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
111                 ret = False;
112         }
113         
114         data_blob_free(&enc_key);
115         
116         ZERO_STRUCT(new_mtime);
117         ZERO_STRUCT(old_mtime);
118         
119         /* fetch the secret back again */
120         r4.in.sec_handle = &sec_handle;
121         r4.in.new_val = &bufp1;
122         r4.in.new_mtime = &new_mtime;
123         r4.in.old_val = NULL;
124         r4.in.old_mtime = NULL;
125         
126         bufp1.buf = NULL;
127         
128         printf("Testing QuerySecret\n");
129         status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
130         if (!NT_STATUS_IS_OK(status)) {
131                 printf("QuerySecret failed - %s\n", nt_errstr(status));
132                 ret = False;
133         } else {
134                 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
135                         printf("No secret buffer returned\n");
136                         ret = False;
137                 } else {
138                         blob1.data = r4.out.new_val->buf->data;
139                         blob1.length = r4.out.new_val->buf->size;
140                         
141                         blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
142                         
143                         secret2 = sess_decrypt_string(&blob1, &session_key);
144                         
145                         if (strcmp(secret1, secret2) != 0) {
146                                 printf("Returned secret '%s' doesn't match '%s'\n", 
147                                        secret2, secret1);
148                                 ret = False;
149                         }
150                 }
151         }
152
153         d.in.handle = &sec_handle;
154         status = dcerpc_lsa_Delete(p, mem_ctx, &d);
155         if (!NT_STATUS_IS_OK(status)) {
156                 printf("delete should have returned OKINVALID_HANDLE - %s\n", nt_errstr(status));
157                 ret = False;
158         }
159         return ret;
160 }
161
162
163 /* TEST session key correctness by pushing and pulling secrets */
164
165 BOOL torture_rpc_lsa_secrets(void) 
166 {
167         NTSTATUS status;
168         struct dcerpc_pipe *p;
169         TALLOC_CTX *mem_ctx;
170         BOOL ret = True;
171         struct policy_handle *handle;
172
173         mem_ctx = talloc_init("torture_rpc_lsa_secrets");
174
175         status = torture_rpc_connection(mem_ctx, 
176                                         &p, 
177                                         &dcerpc_table_lsarpc);
178         if (!NT_STATUS_IS_OK(status)) {
179                 talloc_free(mem_ctx);
180                 return False;
181         }
182
183         if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
184                 ret = False;
185         }
186
187         if (!test_CreateSecret_basic(p, mem_ctx, handle)) {
188                 ret = False;
189         }
190
191         talloc_free(mem_ctx);
192
193         return ret;
194 }