1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetShare testsuite
4  *  Copyright (C) Guenther Deschner 2008
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 <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <netapi.h>
27 
28 #include "common.h"
29 
test_netshareenum(const char * hostname,uint32_t level,const char * sharename)30 static NET_API_STATUS test_netshareenum(const char *hostname,
31 					uint32_t level,
32 					const char *sharename)
33 {
34 	NET_API_STATUS status;
35 	uint32_t entries_read = 0;
36 	uint32_t total_entries = 0;
37 	uint32_t resume_handle = 0;
38 	int found_share = 0;
39 	const char *current_name = NULL;
40 	uint8_t *buffer = NULL;
41 	int i;
42 
43 	struct SHARE_INFO_0 *i0 = NULL;
44 	struct SHARE_INFO_1 *i1 = NULL;
45 	struct SHARE_INFO_2 *i2 = NULL;
46 
47 	printf("testing NetShareEnum level %d\n", level);
48 
49 	do {
50 		status = NetShareEnum(hostname,
51 				      level,
52 				      &buffer,
53 				      (uint32_t)-1,
54 				      &entries_read,
55 				      &total_entries,
56 				      &resume_handle);
57 		if (status == 0 || status == ERROR_MORE_DATA) {
58 			switch (level) {
59 				case 0:
60 					i0 = (struct SHARE_INFO_0 *)buffer;
61 					break;
62 				case 1:
63 					i1 = (struct SHARE_INFO_1 *)buffer;
64 					break;
65 				case 2:
66 					i2 = (struct SHARE_INFO_2 *)buffer;
67 					break;
68 				default:
69 					return -1;
70 			}
71 
72 			for (i=0; i<entries_read; i++) {
73 
74 				switch (level) {
75 					case 0:
76 						current_name = i0->shi0_netname;
77 						break;
78 					case 1:
79 						current_name = i1->shi1_netname;
80 						break;
81 					case 2:
82 						current_name = i2->shi2_netname;
83 						break;
84 					default:
85 						break;
86 				}
87 
88 				if (strcasecmp(current_name, sharename) == 0) {
89 					found_share = 1;
90 				}
91 
92 				switch (level) {
93 					case 0:
94 						i0++;
95 						break;
96 					case 1:
97 						i1++;
98 						break;
99 					case 2:
100 						i2++;
101 						break;
102 				}
103 			}
104 			NetApiBufferFree(buffer);
105 		}
106 	} while (status == ERROR_MORE_DATA);
107 
108 	if (status) {
109 		return status;
110 	}
111 
112 	if (!found_share) {
113 		printf("failed to get share\n");
114 		return -1;
115 	}
116 
117 	return 0;
118 }
119 
netapitest_share(struct libnetapi_ctx * ctx,const char * hostname)120 NET_API_STATUS netapitest_share(struct libnetapi_ctx *ctx,
121 				const char *hostname)
122 {
123 	NET_API_STATUS status = 0;
124 	const char *sharename, *comment;
125 	uint8_t *buffer = NULL;
126 	struct SHARE_INFO_2 i2;
127 	struct SHARE_INFO_502 i502;
128 	struct SHARE_INFO_1004 i1004;
129 	struct SHARE_INFO_501 *i501 = NULL;
130 	uint32_t parm_err = 0;
131 	uint32_t levels[] = { 0, 1, 2, 501, 1005 };
132 	uint32_t enum_levels[] = { 0, 1, 2 };
133 	int i;
134 
135 	printf("NetShare tests\n");
136 
137 	sharename = "torture_test_share";
138 
139 	/* cleanup */
140 	NetShareDel(hostname, sharename, 0);
141 
142 	/* add a share */
143 
144 	printf("testing NetShareAdd\n");
145 
146 	ZERO_STRUCT(i502);
147 
148 	i502.shi502_netname = sharename;
149 	i502.shi502_path = "c:\\";
150 
151 	status = NetShareAdd(hostname, 502, (uint8_t *)&i502, &parm_err);
152 	if (status) {
153 		NETAPI_STATUS(ctx, status, "NetShareAdd");
154 		goto out;
155 	};
156 
157 	status = NetShareDel(hostname, sharename, 0);
158 	if (status) {
159 		NETAPI_STATUS(ctx, status, "NetShareDel");
160 		goto out;
161 	};
162 
163 	ZERO_STRUCT(i2);
164 
165 	i2.shi2_netname = sharename;
166 	i2.shi2_path = "c:\\";
167 
168 	status = NetShareAdd(hostname, 2, (uint8_t *)&i2, &parm_err);
169 	if (status) {
170 		NETAPI_STATUS(ctx, status, "NetShareAdd");
171 		goto out;
172 	};
173 
174 	/* test enum */
175 
176 	for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
177 
178 		status = test_netshareenum(hostname, enum_levels[i], sharename);
179 		if (status) {
180 			NETAPI_STATUS(ctx, status, "NetShareEnum");
181 			goto out;
182 		}
183 	}
184 
185 	/* basic queries */
186 
187 	for (i=0; i<ARRAY_SIZE(levels); i++) {
188 
189 		printf("testing NetShareGetInfo level %d\n", levels[i]);
190 
191 		status = NetShareGetInfo(hostname, sharename, levels[i], &buffer);
192 		if (status && status != 124) {
193 			NETAPI_STATUS(ctx, status, "NetShareGetInfo");
194 			goto out;
195 		}
196 	}
197 
198 
199 	comment = "NetApi generated comment";
200 
201 	i1004.shi1004_remark = comment;
202 
203 	printf("testing NetShareSetInfo level 1004\n");
204 
205 	status = NetShareSetInfo(hostname, sharename, 1004, (uint8_t *)&i1004, &parm_err);
206 	if (status) {
207 		NETAPI_STATUS(ctx, status, "NetShareSetInfo");
208 		goto out;
209 	}
210 
211 	status = NetShareGetInfo(hostname, sharename, 501, (uint8_t **)&i501);
212 	if (status) {
213 		NETAPI_STATUS(ctx, status, "NetShareGetInfo");
214 		goto out;
215 	}
216 
217 	if (strcasecmp(i501->shi501_remark, comment) != 0) {
218 		NETAPI_STATUS(ctx, status, "NetShareGetInfo");
219 		goto out;
220 	}
221 
222 	/* delete */
223 
224 	printf("testing NetShareDel\n");
225 
226 	status = NetShareDel(hostname, sharename, 0);
227 	if (status) {
228 		NETAPI_STATUS(ctx, status, "NetShareDel");
229 		goto out;
230 	};
231 
232 	/* should not exist anymore */
233 
234 	status = NetShareGetInfo(hostname, sharename, 0, &buffer);
235 	if (status == 0) {
236 		NETAPI_STATUS(ctx, status, "NetShareGetInfo");
237 		goto out;
238 	};
239 
240 	status = 0;
241 
242 	printf("NetShare tests succeeded\n");
243  out:
244 	if (status != 0) {
245 		printf("NetShare testsuite failed with: %s\n",
246 			libnetapi_get_error_string(ctx, status));
247 	}
248 
249 	return status;
250 }
251