libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / roken / test-mini_inetd.c
blobd6333e4b883502b3d8a4505d8d5316dd45459648
1 /***********************************************************************
2 * Copyright (c) 2009, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 **********************************************************************/
32 #include <config.h>
33 #include <roken.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
38 #define PORT 8013
39 #define PORT_S "8013"
41 char * prog = "Master";
42 int is_client = 0;
44 static int
45 get_address(int flags, struct addrinfo ** ret)
47 struct addrinfo ai;
48 int rv;
50 memset(&ai, 0, sizeof(ai));
52 ai.ai_flags = flags | AI_NUMERICHOST | AI_NUMERICSERV;
53 ai.ai_family = AF_INET;
54 ai.ai_socktype = SOCK_STREAM;
55 ai.ai_protocol = PF_UNSPEC;
57 rv = getaddrinfo("127.0.0.1", PORT_S, &ai, ret);
58 if (rv)
59 warnx("getaddrinfo: %s", gai_strerror(rv));
60 return rv;
63 static int
64 get_connected_socket(rk_socket_t * s_ret)
66 struct addrinfo * ai = NULL;
67 int rv = 0;
68 rk_socket_t s = rk_INVALID_SOCKET;
70 rv = get_address(0, &ai);
71 if (rv)
72 return rv;
74 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
75 if (rk_IS_BAD_SOCKET(s)) {
76 rv = 1;
77 goto done;
80 rv = connect(s, ai->ai_addr, ai->ai_addrlen);
81 if (rk_IS_SOCKET_ERROR(rv))
82 goto done;
84 *s_ret = s;
85 s = rk_INVALID_SOCKET;
86 rv = 0;
88 done:
89 if (!rk_IS_BAD_SOCKET(s))
90 rk_closesocket(s);
92 if (ai)
93 freeaddrinfo(ai);
95 return (rv) ? rk_SOCK_ERRNO : 0;
98 const char * test_strings[] = {
99 "Hello",
100 "01234566789012345689012345678901234567890123456789",
101 "Another test",
102 "exit"
105 static int
106 test_simple_echo_client(void)
108 rk_socket_t s = rk_INVALID_SOCKET;
109 int rv;
110 char buf[81];
111 int i;
113 fprintf(stderr, "[%s] Getting connected socket...", getprogname());
114 rv = get_connected_socket(&s);
115 if (rv) {
116 fprintf(stderr, "\n[%s] get_connected_socket() failed (%s)\n",
117 getprogname(), strerror(rk_SOCK_ERRNO));
118 return 1;
121 fprintf(stderr, "[%s] done\n", getprogname());
123 for (i=0; i < sizeof(test_strings)/sizeof(test_strings[0]); i++) {
124 rv = send(s, test_strings[i], strlen(test_strings[i]), 0);
125 if (rk_IS_SOCKET_ERROR(rv)) {
126 fprintf(stderr, "[%s] send() failure (%s)\n",
127 getprogname(), strerror(rk_SOCK_ERRNO));
128 rk_closesocket(s);
129 return 1;
132 rv = recv(s, buf, sizeof(buf), 0);
133 if (rk_IS_SOCKET_ERROR(rv)) {
134 fprintf (stderr, "[%s] recv() failure (%s)\n",
135 getprogname(), strerror(rk_SOCK_ERRNO));
136 rk_closesocket(s);
137 return 1;
140 if (rv == 0) {
141 fprintf (stderr, "[%s] No data received\n", prog);
142 rk_closesocket(s);
143 return 1;
146 if (rv != strlen(test_strings[i])) {
147 fprintf (stderr, "[%s] Data length mismatch %d != %zu\n", prog, rv, strlen(test_strings[i]));
148 rk_closesocket(s);
149 return 1;
153 fprintf (stderr, "[%s] Done\n", prog);
154 rk_closesocket(s);
155 return 0;
158 static int
159 test_simple_echo_socket(void)
161 fprintf (stderr, "[%s] Process ID %d\n", prog, GetCurrentProcessId());
162 fprintf (stderr, "[%s] Starting echo test with sockets\n", prog);
164 if (is_client) {
165 return test_simple_echo_client();
166 } else {
168 rk_socket_t s = rk_INVALID_SOCKET;
170 fprintf (stderr, "[%s] Listening for connections...\n", prog);
171 mini_inetd(htons(PORT), &s);
172 if (rk_IS_BAD_SOCKET(s)) {
173 fprintf (stderr, "[%s] Connect failed (%s)\n",
174 getprogname(), strerror(rk_SOCK_ERRNO));
175 } else {
176 fprintf (stderr, "[%s] Connected\n", prog);
180 char buf[81];
181 int rv, srv;
183 while ((rv = recv(s, buf, sizeof(buf), 0)) != 0 && !rk_IS_SOCKET_ERROR(rv)) {
184 buf[rv] = 0;
185 fprintf(stderr, "[%s] Received [%s]\n", prog, buf);
187 /* simple echo */
188 srv = send(s, buf, rv, 0);
189 if (srv != rv) {
190 if (rk_IS_SOCKET_ERROR(srv))
191 fprintf(stderr, "[%s] send() error [%s]\n",
192 getprogname(), strerror(rk_SOCK_ERRNO));
193 else
194 fprintf(stderr, "[%s] send() size mismatch %d != %d",
195 getprogname(), srv, rv);
198 if (strcmp(buf, "exit") == 0) {
199 fprintf(stderr, "[%s] Exiting...\n", prog);
200 shutdown(s, SD_SEND);
201 rk_closesocket(s);
202 return 0;
206 fprintf(stderr, "[%s] recv() failed (%s)\n",
207 getprogname(),
208 strerror(rk_SOCK_ERRNO));
211 rk_closesocket(s);
214 return 1;
217 static int
218 test_simple_echo(void)
220 fprintf (stderr, "[%s] Starting echo test\n", prog);
222 if (is_client) {
224 return test_simple_echo_client();
226 } else {
228 fprintf (stderr, "[%s] Listening for connections...\n", prog);
229 mini_inetd(htons(PORT), NULL);
230 fprintf (stderr, "[%s] Connected\n", prog);
233 char buf[81];
234 while (gets(buf)) {
235 fprintf(stderr, "[%s] Received [%s]\n", prog, buf);
237 if (strcmp(buf, "exit") == 0)
238 return 0;
240 /* simple echo */
241 puts(buf);
244 fprintf(stderr, "[%s] gets() failed (%s)\n", prog, _strerror("gets"));
248 return 1;
251 static int
252 do_client(void)
254 int rv = 0;
256 if (rk_SOCK_INIT())
257 errx(1, "Failed to initialize sockets (%s)", strerror(rk_SOCK_ERRNO));
259 prog = "Client";
260 is_client = 1;
262 fprintf(stderr, "Starting client...\n");
264 rv = test_simple_echo_socket();
266 rk_SOCK_EXIT();
268 return rv;
271 static int
272 do_server(void)
274 int rv = 0;
276 if (rk_SOCK_INIT())
277 errx(1, "Failed to initialize sockets (%s)", strerror(rk_SOCK_ERRNO));
279 prog = "Server";
281 fprintf(stderr, "Starting server...\n");
283 rv = test_simple_echo_socket();
285 rk_SOCK_EXIT();
287 return rv;
290 static time_t
291 wait_callback(void *p)
293 return (time_t)-1;
296 static int
297 do_test(char * path)
299 intptr_t p_server;
300 intptr_t p_client;
301 int client_rv;
302 int server_rv;
304 p_server = _spawnl(_P_NOWAIT, path, path, "--server", NULL);
305 if (p_server <= 0) {
306 fprintf(stderr, "%s: %s", path, _strerror("Can't start server process"));
307 return 1;
309 #ifdef _WIN32
310 /* On Windows, the _spawn*() functions return a process handle on
311 success. We need a process ID for use with
312 wait_for_process_timed(). */
314 p_server = GetProcessId((HANDLE) p_server);
315 #endif
316 fprintf(stderr, "Created server process ID %d\n", p_server);
318 p_client = _spawnl(_P_NOWAIT, path, path, "--client", NULL);
319 if (p_client <= 0) {
320 fprintf(stderr, "%s: %s", path, _strerror("Can't start client process"));
321 fprintf(stderr, "Waiting for server process to terminate ...");
322 wait_for_process_timed(p_server, wait_callback, NULL, 5);
323 fprintf(stderr, "DONE\n");
324 return 1;
326 #ifdef _WIN32
327 p_client = GetProcessId((HANDLE) p_client);
328 #endif
329 fprintf(stderr, "Created client process ID %d\n", p_client);
331 fprintf(stderr, "Waiting for client process to terminate ...");
332 client_rv = wait_for_process_timed(p_client, wait_callback, NULL, 5);
333 if (SE_IS_ERROR(client_rv)) {
334 fprintf(stderr, "\nwait_for_process_timed() failed for client. rv=%d\n", client_rv);
335 } else {
336 fprintf(stderr, "DONE\n");
339 fprintf(stderr, "Waiting for server process to terminate ...");
340 server_rv = wait_for_process_timed(p_server, wait_callback, NULL, 5);
341 if (SE_IS_ERROR(server_rv)) {
342 fprintf(stderr, "\nwait_for_process_timed() failed for server. rv=%d\n", server_rv);
343 } else {
344 fprintf(stderr, "DONE\n");
347 if (client_rv == 0 && server_rv == 0) {
348 fprintf(stderr, "PASS\n");
349 return 0;
350 } else {
351 fprintf(stderr, "FAIL: Client rv=%d, Server rv=%d\n", client_rv, server_rv);
352 return 1;
356 int main(int argc, char ** argv)
358 setprogname(argv[0]);
360 if (argc == 2 && strcmp(argv[1], "--client") == 0)
361 return do_client();
362 else if (argc == 2 && strcmp(argv[1], "--server") == 0)
363 return do_server();
364 else if (argc == 1)
365 return do_test(argv[0]);
366 else {
367 printf ("%s: Test mini_inetd() function. Run with no arguments to start test\n",
368 argv[0]);
369 return 1;