r12637: test CLDAP with both NULL and non-NULL user
[kai/samba.git] / source / 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=NULL\n");
93
94         search.in.user = NULL;
95         status = cldap_netlogon(cldap, mem_ctx, &search);
96         CHECK_STATUS(status, NT_STATUS_OK);
97
98         printf("Trying with User=Administrator\n");
99
100         search.in.user = "Administrator";
101         status = cldap_netlogon(cldap, mem_ctx, &search);
102         CHECK_STATUS(status, NT_STATUS_OK);
103
104         printf("Trying with a GUID\n");
105         search.in.realm       = NULL;
106         search.in.domain_guid = GUID_string(mem_ctx, &n1.logon5.domain_uuid);
107         status = cldap_netlogon(cldap, mem_ctx, &search);
108         CHECK_STATUS(status, NT_STATUS_OK);
109
110         printf("Trying with a incorrect GUID\n");
111         guid = GUID_random();
112         search.in.user        = NULL;
113         search.in.domain_guid = GUID_string(mem_ctx, &guid);
114         status = cldap_netlogon(cldap, mem_ctx, &search);
115         CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
116
117         printf("Trying with a AAC\n");
118         search.in.acct_control = 0x180;
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 bad AAC\n");
124         search.in.acct_control = 0xFF00FF00;
125         search.in.realm = n1.logon5.dns_domain;
126         status = cldap_netlogon(cldap, mem_ctx, &search);
127         CHECK_STATUS(status, NT_STATUS_OK);
128
129         printf("Trying with a user only\n");
130         search = empty_search;
131         search.in.user = "Administrator";
132         status = cldap_netlogon(cldap, mem_ctx, &search);
133         CHECK_STATUS(status, NT_STATUS_OK);
134
135         printf("Trying with just a bad username\n");
136         search.in.user = "___no_such_user___";
137         status = cldap_netlogon(cldap, mem_ctx, &search);
138         CHECK_STATUS(status, NT_STATUS_OK);
139
140         printf("Trying with just a bad domain\n");
141         search = empty_search;
142         search.in.realm = "___no_such_domain___";
143         status = cldap_netlogon(cldap, mem_ctx, &search);
144         CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
145
146         printf("Trying with a incorrect domain and correct guid\n");
147         search.in.domain_guid = GUID_string(mem_ctx, &n1.logon5.domain_uuid);
148         status = cldap_netlogon(cldap, mem_ctx, &search);
149         CHECK_STATUS(status, NT_STATUS_OK);
150
151         printf("Trying with a incorrect domain and incorrect guid\n");
152         search.in.domain_guid = GUID_string(mem_ctx, &guid);
153         status = cldap_netlogon(cldap, mem_ctx, &search);
154         CHECK_STATUS(status, NT_STATUS_NOT_FOUND);
155
156         printf("Trying with a incorrect GUID and correct domain\n");
157         search.in.domain_guid = GUID_string(mem_ctx, &guid);
158         search.in.realm = n1.logon5.dns_domain;
159         status = cldap_netlogon(cldap, mem_ctx, &search);
160         CHECK_STATUS(status, NT_STATUS_OK);
161
162 done:
163         return ret;     
164 }
165
166 BOOL torture_cldap(void)
167 {
168         TALLOC_CTX *mem_ctx;
169         BOOL ret = True;
170         const char *host = lp_parm_string(-1, "torture", "host");
171
172         mem_ctx = talloc_init("torture_cldap");
173
174         ret &= test_cldap_netlogon(mem_ctx, host);
175
176         talloc_free(mem_ctx);
177
178         return ret;
179 }
180