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