3 * Routines to parse an inetd.conf or tlid.conf file. This would be a great
4 * job for a PERL script.
6 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
10 static char sccsid[] = "@(#) inetcf.c 1.7 97/02/12 02:13:23";
13 #include <sys/types.h>
28 * Network configuration files may live in unusual places. Here are some
29 * guesses. Shorter names follow longer ones.
31 char *inet_files[] = {
32 "/private/etc/inetd.conf", /* NEXT */
33 "/etc/inet/inetd.conf", /* SYSV4 */
34 "/usr/etc/inetd.conf", /* IRIX?? */
35 "/etc/inetd.conf", /* BSD */
36 "/etc/net/tlid.conf", /* SYSV4?? */
37 "/etc/saf/tlid.conf", /* SYSV4?? */
38 "/etc/tlid.conf", /* SYSV4?? */
42 static void inet_chk();
43 static char *base_name();
46 * Structure with everything we know about a service.
49 struct inet_ent *next;
54 static struct inet_ent *inet_list = 0;
56 static char whitespace[] = " \t\r\n";
58 /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
71 struct tcpd_context saved_context;
76 saved_context = tcpd_context;
79 * The inetd.conf (or tlid.conf) information is so useful that we insist
80 * on its availability. When no file is given run a series of educated
84 if ((fp = fopen(conf, "r")) == 0) {
85 fprintf(stderr, percent_m(buf, "open %s: %m\n"), conf);
89 for (i = 0; inet_files[i] && (fp = fopen(inet_files[i], "r")) == 0; i++)
92 fprintf(stderr, "Cannot find your inetd.conf or tlid.conf file.\n");
93 fprintf(stderr, "Please specify its location.\n");
97 check_path(conf, &st);
101 * Process the file. After the 7.0 wrapper release it became clear that
102 * there are many more inetd.conf formats than the 8 systems that I had
103 * studied. EP/IX uses a two-line specification for rpc services; HP-UX
104 * permits long lines to be broken with backslash-newline.
106 tcpd_context.file = conf;
107 tcpd_context.line = 0;
108 while (xgets(buf, sizeof(buf), fp)) {
109 service = strtok(buf, whitespace); /* service */
110 if (service == 0 || *service == '#')
112 if (STR_NE(service, "stream") && STR_NE(service, "dgram"))
113 strtok((char *) 0, whitespace); /* endpoint */
114 protocol = strtok((char *) 0, whitespace);
115 (void) strtok((char *) 0, whitespace); /* wait */
116 if ((user = strtok((char *) 0, whitespace)) == 0)
118 if (user[0] == '/') { /* user */
121 if ((path = strtok((char *) 0, whitespace)) == 0)
124 if (path[0] == '?') /* IRIX optional service */
126 if (STR_EQ(path, "internal"))
128 if (path[strspn(path, "-0123456789")] == 0) {
131 * ConvexOS puts RPC version numbers before path names. Jukka
132 * Ukkonen <ukkonen@csc.fi>.
134 if ((path = strtok((char *) 0, whitespace)) == 0)
137 if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
138 tcpd_warn("incomplete line");
141 if (arg0[strspn(arg0, "0123456789")] == 0) {
144 * We're reading a tlid.conf file, the format is:
146 * ...stuff... path arg_count arguments mod_count modules
148 if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
149 tcpd_warn("incomplete line");
153 if ((arg1 = strtok((char *) 0, whitespace)) == 0)
156 inet_chk(protocol, path, arg0, arg1);
159 tcpd_context = saved_context;
163 /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
165 static void inet_chk(protocol, path, arg0, arg1)
173 int wrap_status = WR_MAYBE;
174 char *base_name_path = base_name(path);
175 char *tcpd_proc_name = (arg0[0] == '/' ? base_name(arg0) : arg0);
178 * Always warn when the executable does not exist or when it is not
181 if (check_path(path, &st) < 0) {
182 tcpd_warn("%s: not found: %m", path);
183 } else if ((st.st_mode & 0100) == 0) {
184 tcpd_warn("%s: not executable", path);
188 * Cheat on the miscd tests, nobody uses it anymore.
190 if (STR_EQ(base_name_path, "miscd")) {
191 inet_set(arg0, WR_YES);
196 * While we are here...
198 if (STR_EQ(tcpd_proc_name, "rexd") || STR_EQ(tcpd_proc_name, "rpc.rexd"))
199 tcpd_warn("%s may be an insecure service", tcpd_proc_name);
202 * The tcpd program gets most of the attention.
204 if (STR_EQ(base_name_path, "tcpd")) {
206 if (STR_EQ(tcpd_proc_name, "tcpd"))
207 tcpd_warn("%s is recursively calling itself", tcpd_proc_name);
209 wrap_status = WR_YES;
212 * Check: some sites install the wrapper set-uid.
214 if ((st.st_mode & 06000) != 0)
215 tcpd_warn("%s: file is set-uid or set-gid", path);
218 * Check: some sites insert tcpd in inetd.conf, instead of replacing
219 * the daemon pathname.
221 if (arg0[0] == '/' && STR_EQ(tcpd_proc_name, base_name(arg1)))
222 tcpd_warn("%s inserted before %s", path, arg0);
225 * Check: make sure files exist and are executable. On some systems
226 * the network daemons are set-uid so we cannot complain. Note that
227 * tcpd takes the basename only in case of absolute pathnames.
229 if (arg0[0] == '/') { /* absolute path */
230 if (check_path(arg0, &st) < 0) {
231 tcpd_warn("%s: not found: %m", arg0);
232 } else if ((st.st_mode & 0100) == 0) {
233 tcpd_warn("%s: not executable", arg0);
235 } else { /* look in REAL_DAEMON_DIR */
236 sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
237 if (check_path(daemon, &st) < 0) {
238 tcpd_warn("%s: not found in %s: %m",
239 arg0, REAL_DAEMON_DIR);
240 } else if ((st.st_mode & 0100) == 0) {
241 tcpd_warn("%s: not executable", daemon);
248 * No tcpd program found. Perhaps they used the "simple installation"
249 * recipe. Look for a file with the same basename in REAL_DAEMON_DIR.
250 * Draw some conservative conclusions when a distinct file is found.
252 sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
253 if (STR_EQ(path, daemon)) {
255 wrap_status = WR_MAYBE;
257 wrap_status = WR_NOT;
259 } else if (check_path(daemon, &st) >= 0) {
260 wrap_status = WR_MAYBE;
261 } else if (errno == ENOENT) {
262 wrap_status = WR_NOT;
264 tcpd_warn("%s: file lookup: %m", daemon);
265 wrap_status = WR_MAYBE;
270 * Alas, we cannot wrap rpc/tcp services.
272 if (wrap_status == WR_YES && STR_EQ(protocol, "rpc/tcp"))
273 tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name);
275 inet_set(tcpd_proc_name, wrap_status);
278 /* inet_set - remember service status */
280 void inet_set(name, type)
284 struct inet_ent *ip =
285 (struct inet_ent *) malloc(sizeof(struct inet_ent) + strlen(name));
288 fprintf(stderr, "out of memory\n");
291 ip->next = inet_list;
292 strcpy(ip->name, name);
297 /* inet_get - look up service status */
307 for (ip = inet_list; ip; ip = ip->next)
308 if (STR_EQ(ip->name, name))
314 /* base_name - compute last pathname component */
316 static char *base_name(path)
321 if ((cp = strrchr(path, '/')) != 0)