s3/torture: PY3 port samba.ntlm_auth
[bbaumbach/samba-autobuild/.git] / source3 / torture / test_namemap_cache.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * namemap_cache.c
4  * Copyright (C) Volker Lendecke 2017
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 "torture/proto.h"
22 #include "lib/namemap_cache.h"
23 #include "libcli/security/dom_sid.h"
24 #include "lib/gencache.h"
25
26 static const struct dom_sid domsid = {
27         1, 4, {0,0,0,0,0,5}, {21, 123, 456, 789}
28 };
29
30 static void namemap_cache1_fn1(const char *domain,
31                                const char *name,
32                                enum lsa_SidType type,
33                                bool expired,
34                                void *private_data)
35 {
36         bool *p_ok = private_data;
37         bool ok;
38
39         ok = strequal(domain, "nt authority");
40         ok &= strequal(name, "network");
41         ok &= (type == SID_NAME_WKN_GRP);
42
43         *p_ok = ok;
44 }
45
46 static void namemap_cache1_fn2(const struct dom_sid *sid,
47                                enum lsa_SidType type,
48                                bool expired,
49                                void *private_data)
50 {
51         bool *p_ok = private_data;
52         bool ok;
53
54         ok = dom_sid_equal(sid, &global_sid_Network);
55         ok &= (type == SID_NAME_WKN_GRP);
56
57         *p_ok = ok;
58 }
59
60 static void namemap_cache1_fn3(const char *domain,
61                                const char *name,
62                                enum lsa_SidType type,
63                                bool expired,
64                                void *private_data)
65 {
66         bool *p_ok = private_data;
67         bool ok;
68
69         ok = strequal(domain, "");
70         ok &= strequal(name, "everyone");
71         ok &= (type == SID_NAME_WKN_GRP);
72
73         *p_ok = ok;
74 }
75
76 static void namemap_cache1_fn4(const struct dom_sid *sid,
77                                enum lsa_SidType type,
78                                bool expired,
79                                void *private_data)
80 {
81         bool *p_ok = private_data;
82         bool ok;
83
84         ok = dom_sid_equal(sid, &global_sid_World);
85         ok &= (type == SID_NAME_WKN_GRP);
86
87         *p_ok = ok;
88 }
89
90 static void namemap_cache1_fn5(const char *domain,
91                                const char *name,
92                                enum lsa_SidType type,
93                                bool expired,
94                                void *private_data)
95 {
96         bool *p_ok = private_data;
97         bool ok;
98
99         ok = strequal(domain, "samba-dom");
100         ok &= strequal(name, "");
101         ok &= (type == SID_NAME_DOMAIN);
102
103         *p_ok = ok;
104 }
105
106 static void namemap_cache1_fn6(const struct dom_sid *sid,
107                                enum lsa_SidType type,
108                                bool expired,
109                                void *private_data)
110 {
111         bool *p_ok = private_data;
112         bool ok;
113
114         ok = dom_sid_equal(sid, &domsid);
115         ok &= (type == SID_NAME_DOMAIN);
116
117         *p_ok = ok;
118 }
119
120 bool run_local_namemap_cache1(int dummy)
121 {
122         bool found;
123         bool ok;
124
125         ok = gencache_set("SID2NAME/S-1-5-2", "invalid", time(NULL)+60);
126         if (!ok) {
127                 fprintf(stderr, "gencache_set failed\n");
128                 return false;
129         }
130
131         ok = namemap_cache_find_sid(&global_sid_Network, namemap_cache1_fn1,
132                                     &found);
133         if (ok) {
134                 fprintf(stderr, "namemap_cache_find_sid parsed valid value\n");
135                 return false;
136         }
137
138         ok = namemap_cache_set_sid2name(&global_sid_Network,
139                                         "NT Authority", "Network",
140                                         SID_NAME_WKN_GRP,
141                                         time(NULL) + 60);
142         if (!ok) {
143                 fprintf(stderr, "namemap_cache_set_sid2name failed\n");
144                 return false;
145         }
146
147         ok = namemap_cache_find_sid(&global_sid_Network, namemap_cache1_fn1,
148                                     &found);
149         if (!ok) {
150                 fprintf(stderr, "namecache_find_sid failed\n");
151                 return false;
152         }
153         if (!found) {
154                 fprintf(stderr, "wrong values found\n");
155                 return false;
156         }
157
158         ok = namemap_cache_set_name2sid("NT Authority", "Network",
159                                         &global_sid_Network,
160                                         SID_NAME_WKN_GRP,
161                                         time(NULL) + 60);
162         if (!ok) {
163                 fprintf(stderr, "namemap_cache_set_name2sid failed\n");
164                 return false;
165         }
166
167         ok = namemap_cache_find_name("nt authority", "network",
168                                      namemap_cache1_fn2, &found);
169         if (!ok) {
170                 fprintf(stderr, "namecache_find_name failed\n");
171                 return false;
172         }
173         if (!found) {
174                 fprintf(stderr, "wrong values found\n");
175                 return false;
176         }
177
178         ok = namemap_cache_find_name("foo", "bar", namemap_cache1_fn2, &found);
179         if (ok) {
180                 fprintf(stderr,
181                         "namemap_cache_find_name succeeded unexpectedly\n");
182                 return false;
183         }
184
185         /*
186          * Test "" domain name
187          */
188
189         ok = namemap_cache_set_sid2name(&global_sid_World, "", "Everyone",
190                                         SID_NAME_WKN_GRP,
191                                         time(NULL) + 60);
192         if (!ok) {
193                 fprintf(stderr, "namemap_cache_set_sid2name failed\n");
194                 return false;
195         }
196
197         ok = namemap_cache_find_sid(&global_sid_World, namemap_cache1_fn3,
198                                     &found);
199         if (!ok) {
200                 fprintf(stderr, "namecache_find_sid failed\n");
201                 return false;
202         }
203         if (!found) {
204                 fprintf(stderr, "wrong values found\n");
205                 return false;
206         }
207
208         ok = namemap_cache_set_name2sid("", "Everyone",
209                                         &global_sid_World, SID_NAME_WKN_GRP,
210                                         time(NULL) + 60);
211         if (!ok) {
212                 fprintf(stderr, "namemap_cache_set failed\n");
213                 return false;
214         }
215
216         ok = namemap_cache_find_name("", "everyone",
217                                      namemap_cache1_fn4, &found);
218         if (!ok) {
219                 fprintf(stderr, "namecache_find_name failed\n");
220                 return false;
221         }
222         if (!found) {
223                 fprintf(stderr, "wrong values found\n");
224                 return false;
225         }
226
227         /*
228          * Test domain only
229          */
230
231         ok = namemap_cache_set_sid2name(&domsid, "SAMBA-DOM", "",
232                                         SID_NAME_DOMAIN,
233                                         time(NULL) + 60);
234         if (!ok) {
235                 fprintf(stderr, "namemap_cache_set failed\n");
236                 return false;
237         }
238
239         ok = namemap_cache_find_sid(&domsid, namemap_cache1_fn5,
240                                     &found);
241         if (!ok) {
242                 fprintf(stderr, "namecache_find_sid failed\n");
243                 return false;
244         }
245         if (!found) {
246                 fprintf(stderr, "wrong values found\n");
247                 return false;
248         }
249
250         ok = namemap_cache_set_name2sid("SAMBA-DOM", "",
251                                         &domsid, SID_NAME_DOMAIN,
252                                         time(NULL) + 60);
253         if (!ok) {
254                 fprintf(stderr, "namemap_cache_set failed\n");
255                 return false;
256         }
257
258         ok = namemap_cache_find_name("samba-dom", "",
259                                      namemap_cache1_fn6, &found);
260         if (!ok) {
261                 fprintf(stderr, "namecache_find_name failed\n");
262                 return false;
263         }
264         if (!found) {
265                 fprintf(stderr, "wrong values found\n");
266                 return false;
267         }
268
269         return true;
270 }