xref: /freebsd/crypto/krb5/src/tests/t_inetd.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/t_inetd.c */
3 /*
4  * Copyright 1991 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /*
28  * A simple program to simulate starting a process from inetd.
29  *
30  * Unlike a proper inetd situation, environment variables are passed
31  * to the client.
32  *
33  * usage: t_inetd port program argv0 ...
34  */
35 
36 #include "autoconf.h"
37 
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <netdb.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <signal.h>
52 
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 
57 #include "com_err.h"
58 
59 
60 char *progname;
61 
62 static void
usage(void)63 usage(void)
64 {
65     fprintf(stderr, "%s: port program argv0 argv1 ...\n", progname);
66     exit(1);
67 }
68 
69 int
main(int argc,char ** argv)70 main(int argc, char **argv)
71 {
72     unsigned short port;
73     char *path;
74     int sock, acc;
75     int one = 1;
76     struct sockaddr_in l_inaddr, f_inaddr;  /* local, foreign address */
77     socklen_t namelen = sizeof(f_inaddr);
78 
79     progname = argv[0];
80 
81     if(argc <= 3) usage();
82 
83     if(atoi(argv[1]) == 0) usage();
84 
85     port = htons(atoi(argv[1]));
86     path = argv[2];
87 
88     if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
89         com_err(progname, errno, "creating socket");
90         exit(3);
91     }
92 
93     (void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one,
94                       sizeof (one));
95 
96     memset(&l_inaddr, 0, sizeof(l_inaddr));
97     l_inaddr.sin_family = AF_INET;
98     l_inaddr.sin_addr.s_addr = 0;
99     l_inaddr.sin_port = port;
100 
101     if (bind(sock, (struct sockaddr *)&l_inaddr, sizeof(l_inaddr))) {
102         com_err(progname, errno, "binding socket");
103         exit(3);
104     }
105 
106     if (listen(sock, 1) == -1) {
107         com_err(progname, errno, "listening");
108         exit(3);
109     }
110 
111     printf("Ready!\n");
112     fflush(stdout);
113     if ((acc = accept(sock, (struct sockaddr *)&f_inaddr,
114                       &namelen)) == -1) {
115         com_err(progname, errno, "accepting");
116         exit(3);
117     }
118 
119     dup2(acc, 0);
120     dup2(acc, 1);
121     dup2(acc, 2);
122     close(sock);
123     sock = 0;
124 
125     if(execv(path, &argv[3]))
126         fprintf(stderr, "t_inetd: Could not exec %s\n", path);
127     exit(1);
128 }
129