r14720: Add torture_context argument to all torture tests
[samba.git] / source4 / torture / ldap / cldap.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    test CLDAP operations
5    
6    Copyright (C) Andrew Tridgell 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
24 #include "includes.h"
25 #include "libcli/cldap/cldap.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "torture/torture.h"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 printf("(%s) Incorrect status %s - should be %s\n", \
32                        __location__, nt_errstr(status), nt_errstr(correct)); \
33                 ret = False; \
34                 goto done; \
35         } \
36         if (DEBUGLVL(10)) { \
37                 NDR_PRINT_UNION_DEBUG(nbt_cldap_netlogon, \
38                                       search.in.version & 0xF, \
39                                       &search.out.netlogon); \
40         } \
41 } while (0)
42
43 /*
44   test netlogon operations
45 */
46 static BOOL test_cldap_netlogon(TALLOC_CTX *mem_ctx, const char *dest)
47 {
48         struct cldap_socket *cldap = cldap_socket_init(mem_ctx, NULL);
49         NTSTATUS status;
50         struct cldap_netlogon search, empty_search;
51         union nbt_cldap_netlogon n1;
52         struct GUID guid;
53         int i;
54         BOOL ret = True;
55
56         ZERO_STRUCT(search);
57         search.in.dest_address = dest;
58         search.in.acct_control = -1;
59         search.in.version = 6;
60
61         empty_search = search;
62
63         printf("Trying without any attributes\n");
64         search = empty_search;
65         status = cldap_netlogon(cldap, mem_ctx, &search);
66         CHECK_STATUS(status, NT_STATUS_OK);
67
68         n1 = search.out.netlogon;
69
70         search.in.user         = "Administrator";
71         search.in.realm        = n1.logon5.dns_domain;
72         search.in.host         = "__cldap_torture__";
73
74         printf("Scanning for netlogon levels\n");
75         for (i=0;i<256;i++) {
76                 search.in.version = i;
77                 printf("Trying netlogon level %d\n", i);
78                 status = cldap_netlogon(cldap, mem_ctx, &search);
79                 CHECK_STATUS(status, NT_STATUS_OK);
80         }
81
82         printf("Scanning for netlogon level bits\n");
83         for (i=0;i<31;i++) {
84                 search.in.version = (1<<i);
85                 printf("Trying netlogon level 0x%x\n", i);
86                 status = cldap_netlogon(cldap, mem_ctx, &search);
87                 CHECK_STATUS(status, NT_STATUS_OK);
88         }
89
90         search.in.version = 6;
91         status = cldap_netlogon(cldap, mem_ctx, &search);
92         CHECK_STATUS(status, NT_STATUS_OK);
93
94         printf("Trying with User=NULL\n");
95
96         search.in.user = NULL;
97         status = cldap_netlogon(cldap, mem_ctx, &search);
98         CHECK_STATUS(status, NT_STATUS_OK);
99
100         printf("Trying with User=Administrator\n");
101
102         search.in.user = "Administrator";
103         status = cldap_netlogon(cldap, mem_ctx, &search);
104         CHECK_STATUS(status, NT_STATUS_OK);
105
106         printf("Trying with a GUID\n");
107         search.in.realm       = NULL;
108         search.in.domain_guid = GUID_string(mem_ctx, &n1.logon5.domain_uuid);
109         status = cldap_netlogon(cldap, mem_ctx, &search);
110         CHECK_STATUS(status, NT_STATUS_OK);
111
112         printf("Trying with a incorrect GUID\n");
113         guid = GUID_random();
114         search.in.user        = NULL;
115         search.in.domain_guid = GUID_string(mem_ctx, &guid);
116         status = cldap_netlogon(cldap, mem_ctx, &search);
117         CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
118
119         printf("Trying with a AAC\n");
120         search.in.acct_control = 0x180;
121         search.in.realm = n1.logon5.dns_domain;
122         status = cldap_netlogon(cldap, mem_ctx, &search);
123         CHECK_STATUS(status, NT_STATUS_OK);
124
125         printf("Trying with a bad AAC\n");
126         search.in.acct_control = 0xFF00FF00;
127         search.in.realm = n1.logon5.dns_domain;
128         status = cldap_netlogon(cldap, mem_ctx, &search);
129         CHECK_STATUS(status, NT_STATUS_OK);
130
131         printf("Trying with a user only\n");
132         search = empty_search;
133         search.in.user = "Administrator";
134         status = cldap_netlogon(cldap, mem_ctx, &search);
135         CHECK_STATUS(status, NT_STATUS_OK);
136
137         printf("Trying with just a bad username\n");
138         search.in.user = "___no_such_user___";
139         status = cldap_netlogon(cldap, mem_ctx, &search);
140         CHECK_STATUS(status, NT_STATUS_OK);
141
142         printf("Trying with just a bad domain\n");
143         search = empty_search;
144         search.in.realm = "___no_such_domain___";
145         status = cldap_netlogon(cldap, mem_ctx, &search);
146         CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
147
148         printf("Trying with a incorrect domain and correct guid\n");
149         search.in.domain_guid = GUID_string(mem_ctx, &n1.logon5.domain_uuid);
150         status = cldap_netlogon(cldap, mem_ctx, &search);
151         CHECK_STATUS(status, NT_STATUS_OK);
152
153         printf("Trying with a incorrect domain and incorrect guid\n");
154         search.in.domain_guid = GUID_string(mem_ctx, &guid);
155         status = cldap_netlogon(cldap, mem_ctx, &search);
156         CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
157
158         printf("Trying with a incorrect GUID and correct domain\n");
159         search.in.domain_guid = GUID_string(mem_ctx, &guid);
160         search.in.realm = n1.logon5.dns_domain;
161         status = cldap_netlogon(cldap, mem_ctx, &search);
162         CHECK_STATUS(status, NT_STATUS_OK);
163
164 done:
165         return ret;     
166 }
167
168 BOOL torture_cldap(struct torture_context *torture)
169 {
170         TALLOC_CTX *mem_ctx;
171         BOOL ret = True;
172         const char *host = lp_parm_string(-1, "torture", "host");
173
174         mem_ctx = talloc_init("torture_cldap");
175
176         ret &= test_cldap_netlogon(mem_ctx, host);
177
178         talloc_free(mem_ctx);
179
180         return ret;
181 }
182