9cc74328161bc4f71f9cdc55dbe54b597bc26636
[kai/samba.git] / source4 / torture / winbind / struct_based.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester - winbind struct based protocol
4    Copyright (C) Stefan Metzmacher 2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "pstring.h"
22 #include "torture/torture.h"
23 #include "torture/winbind/proto.h"
24 #include "nsswitch/winbind_client.h"
25 #include "libcli/security/security.h"
26 #include "param/param.h"
27
28 #define DO_STRUCT_REQ_REP_EXT(op,req,rep,expected,strict,warnaction,cmt) do { \
29         NSS_STATUS __got, __expected = (expected); \
30         __got = winbindd_request_response(op, req, rep); \
31         if (__got != __expected) { \
32                 const char *__cmt = (cmt); \
33                 if (strict) { \
34                         torture_result(torture, TORTURE_FAIL, \
35                                 __location__ ": " __STRING(op) \
36                                 " returned %d, expected %d%s%s", \
37                                 __got, __expected, \
38                                 (__cmt) ? ": " : "", \
39                                 (__cmt) ? (__cmt) : ""); \
40                         return false; \
41                 } else { \
42                         torture_warning(torture, \
43                                 __location__ ": " __STRING(op) \
44                                 " returned %d, expected %d%s%s", \
45                                 __got, __expected, \
46                                 (__cmt) ? ": " : "", \
47                                 (__cmt) ? (__cmt) : ""); \
48                         warnaction; \
49                 } \
50         } \
51 } while(0)
52
53 #define DO_STRUCT_REQ_REP(op,req,rep) do { \
54         bool __noop = false; \
55         DO_STRUCT_REQ_REP_EXT(op,req,rep,NSS_STATUS_SUCCESS,true,__noop=true,NULL); \
56 } while (0)
57
58 static bool torture_winbind_struct_ping(struct torture_context *torture)
59 {
60         struct timeval tv = timeval_current();
61         int timelimit = torture_setting_int(torture, "timelimit", 5);
62         uint32_t total = 0;
63
64         torture_comment(torture,
65                         "Running WINBINDD_PING (struct based) for %d seconds\n",
66                         timelimit);
67
68         while (timeval_elapsed(&tv) < timelimit) {
69                 DO_STRUCT_REQ_REP(WINBINDD_PING, NULL, NULL);
70                 total++;
71         }
72
73         torture_comment(torture,
74                         "%u (%.1f/s) WINBINDD_PING (struct based)\n",
75                         total, total / timeval_elapsed(&tv));
76
77         return true;
78 }
79
80 struct torture_trust_domain {
81         const char *netbios_name;
82         const char *dns_name;
83         struct dom_sid *sid;
84 };
85
86 static bool get_trusted_domains(struct torture_context *torture,
87                                 struct torture_trust_domain **_d)
88 {
89         struct winbindd_request req;
90         struct winbindd_response rep;
91         struct torture_trust_domain *d = NULL;
92         uint32_t dcount = 0;
93         fstring line;
94         const char *extra_data;
95
96         ZERO_STRUCT(req);
97         ZERO_STRUCT(rep);
98
99         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
100
101         extra_data = (char *)rep.extra_data.data;
102         torture_assert(torture, extra_data, "NULL trust list");
103
104         while (next_token(&extra_data, line, "\n", sizeof(fstring))) {
105                 char *p, *lp;
106
107                 d = talloc_realloc(torture, d,
108                                    struct torture_trust_domain,
109                                    dcount + 2);
110                 ZERO_STRUCT(d[dcount+1]);
111
112                 lp = line;
113                 p = strchr(lp, '\\');
114                 torture_assert(torture, p, "missing 1st '\\' in line");
115                 *p = 0;
116                 d[dcount].netbios_name = talloc_strdup(d, lp);
117                 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
118                                "empty netbios_name");
119
120                 lp = p+1;
121                 p = strchr(lp, '\\');
122                 torture_assert(torture, p, "missing 2nd '\\' in line");
123                 *p = 0;
124                 d[dcount].dns_name = talloc_strdup(d, lp);
125                 /* it's ok to have an empty dns_name */
126
127                 lp = p+1;
128                 d[dcount].sid = dom_sid_parse_talloc(d, lp);
129                 torture_assert(torture, d[dcount].sid,
130                                "failed to parse sid");
131
132                 dcount++;
133         }
134         SAFE_FREE(rep.extra_data.data);
135
136         torture_assert(torture, dcount >= 2,
137                        "The list of trusted domain should contain 2 entries");
138
139         *_d = d;
140         return true;
141 }
142
143 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
144 {
145         struct winbindd_request req;
146         struct winbindd_response rep;
147         char *list1;
148         char *list2;
149         bool ok;
150         struct torture_trust_domain *listd = NULL;
151         uint32_t i;
152
153         torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
154
155         ZERO_STRUCT(req);
156         ZERO_STRUCT(rep);
157
158         req.data.list_all_domains = false;
159
160         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
161
162         list1 = (char *)rep.extra_data.data;
163         torture_assert(torture, list1, "NULL trust list");
164
165         torture_comment(torture, "%s\n", list1);
166
167         ZERO_STRUCT(req);
168         ZERO_STRUCT(rep);
169
170         req.data.list_all_domains = true;
171
172         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
173
174         list2 = (char *)rep.extra_data.data;
175         torture_assert(torture, list2, "NULL trust list");
176
177         /*
178          * The list_all_domains parameter should be ignored
179          */
180         torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
181
182         SAFE_FREE(list1);
183         SAFE_FREE(list2);
184
185         ok = get_trusted_domains(torture, &listd);
186         torture_assert(torture, ok, "failed to get trust list");
187
188         for (i=0; listd[i].netbios_name; i++) {
189                 if (i == 0) {
190                         struct dom_sid *builtin_sid;
191
192                         builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
193
194                         torture_assert_str_equal(torture,
195                                                  listd[i].netbios_name,
196                                                  NAME_BUILTIN,
197                                                  "first domain should be 'BUILTIN'");
198
199                         torture_assert_str_equal(torture,
200                                                  listd[i].dns_name,
201                                                  "",
202                                                  "BUILTIN domain should not have a dns name");
203
204                         ok = dom_sid_equal(builtin_sid,
205                                            listd[i].sid);
206                         torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
207                                        
208                         continue;
209                 }
210
211                 /*
212                  * TODO: verify the content of the 2nd and 3rd (in member server mode)
213                  *       domain entries
214                  */
215         }
216
217         return true;
218 }
219
220 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
221 {
222         bool ok;
223         bool strict = torture_setting_bool(torture, "strict mode", false);
224         struct torture_trust_domain *listd = NULL;
225         uint32_t i;
226
227         torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
228
229         ok = get_trusted_domains(torture, &listd);
230         torture_assert(torture, ok, "failed to get trust list");
231
232         for (i=0; listd[i].netbios_name; i++) {
233                 struct winbindd_request req;
234                 struct winbindd_response rep;
235
236                 ZERO_STRUCT(req);
237                 ZERO_STRUCT(rep);
238
239                 fstrcpy(req.domain_name, listd[i].netbios_name);
240
241                 ok = true;
242                 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
243                                       NSS_STATUS_SUCCESS,
244                                       (i <2 || strict), ok = false,
245                                       talloc_asprintf(torture, "DOMAIN '%s'",
246                                                       req.domain_name));
247                 if (!ok) continue;
248
249                 /* TODO: check rep.data.dc_name; */
250                 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
251                                 req.domain_name, rep.data.dc_name);
252         }
253
254         return true;
255 }
256
257 struct torture_suite *torture_winbind_struct_init(void)
258 {
259         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "STRUCT");
260
261         torture_suite_add_simple_test(suite, "PING", torture_winbind_struct_ping);
262         torture_suite_add_simple_test(suite, "LIST_TRUSTDOM", torture_winbind_struct_list_trustdom);
263         torture_suite_add_simple_test(suite, "GETDCNAME", torture_winbind_struct_getdcname);
264
265         suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
266
267         return suite;
268 }