kernel - Implement spectre mitigations part 1
[dragonfly.git] / contrib / tcp_wrappers / inetcf.c
blob272287ade2acaea43647d994db541b7b43639108
1 /*
2 * Routines to parse an inetd.conf or tlid.conf file. This would be a great
3 * job for a PERL script.
4 *
5 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
6 */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <stdio.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
15 extern void exit();
17 #include "tcpd.h"
18 #include "inetcf.h"
19 #include "scaffold.h"
22 * Network configuration files may live in unusual places. Here are some
23 * guesses. Shorter names follow longer ones.
25 char *inet_files[] = {
26 "/private/etc/inetd.conf", /* NEXT */
27 "/etc/inet/inetd.conf", /* SYSV4 */
28 "/usr/etc/inetd.conf", /* IRIX?? */
29 "/etc/inetd.conf", /* BSD */
30 "/etc/net/tlid.conf", /* SYSV4?? */
31 "/etc/saf/tlid.conf", /* SYSV4?? */
32 "/etc/tlid.conf", /* SYSV4?? */
36 static void inet_chk();
37 static char *base_name();
40 * Structure with everything we know about a service.
42 struct inet_ent {
43 struct inet_ent *next;
44 int type;
45 char name[1];
48 static struct inet_ent *inet_list = 0;
50 static char whitespace[] = " \t\r\n";
52 /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
54 char *inet_cfg(conf)
55 char *conf;
57 char buf[BUFSIZ];
58 FILE *fp;
59 char *service;
60 char *protocol;
61 char *user;
62 char *path;
63 char *arg0;
64 char *arg1;
65 struct tcpd_context saved_context;
66 char *percent_m();
67 int i;
68 struct stat st;
70 saved_context = tcpd_context;
73 * The inetd.conf (or tlid.conf) information is so useful that we insist
74 * on its availability. When no file is given run a series of educated
75 * guesses.
77 if (conf != 0) {
78 if ((fp = fopen(conf, "r")) == 0) {
79 fprintf(stderr, percent_m(buf, "open %s: %m\n"), conf);
80 exit(1);
82 } else {
83 for (i = 0; inet_files[i] && (fp = fopen(inet_files[i], "r")) == 0; i++)
84 /* void */ ;
85 if (fp == 0) {
86 fprintf(stderr, "Cannot find your inetd.conf or tlid.conf file.\n");
87 fprintf(stderr, "Please specify its location.\n");
88 exit(1);
90 conf = inet_files[i];
91 check_path(conf, &st);
95 * Process the file. After the 7.0 wrapper release it became clear that
96 * there are many more inetd.conf formats than the 8 systems that I had
97 * studied. EP/IX uses a two-line specification for rpc services; HP-UX
98 * permits long lines to be broken with backslash-newline.
100 tcpd_context.file = conf;
101 tcpd_context.line = 0;
102 while (xgets(buf, sizeof(buf), fp)) {
103 service = strtok(buf, whitespace); /* service */
104 if (service == 0 || *service == '#')
105 continue;
106 if (STR_NE(service, "stream") && STR_NE(service, "dgram"))
107 strtok((char *) 0, whitespace); /* endpoint */
108 protocol = strtok((char *) 0, whitespace);
109 (void) strtok((char *) 0, whitespace); /* wait */
110 if ((user = strtok((char *) 0, whitespace)) == 0)
111 continue;
112 if (user[0] == '/') { /* user */
113 path = user;
114 } else { /* path */
115 if ((path = strtok((char *) 0, whitespace)) == 0)
116 continue;
118 if (path[0] == '?') /* IRIX optional service */
119 path++;
120 if (STR_EQ(path, "internal"))
121 continue;
122 if (path[strspn(path, "-0123456789")] == 0) {
125 * ConvexOS puts RPC version numbers before path names. Jukka
126 * Ukkonen <ukkonen@csc.fi>.
128 if ((path = strtok((char *) 0, whitespace)) == 0)
129 continue;
131 if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
132 tcpd_warn("incomplete line");
133 continue;
135 if (arg0[strspn(arg0, "0123456789")] == 0) {
138 * We're reading a tlid.conf file, the format is:
140 * ...stuff... path arg_count arguments mod_count modules
142 if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
143 tcpd_warn("incomplete line");
144 continue;
147 if ((arg1 = strtok((char *) 0, whitespace)) == 0)
148 arg1 = "";
150 inet_chk(protocol, path, arg0, arg1);
152 fclose(fp);
153 tcpd_context = saved_context;
154 return (conf);
157 /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
159 static void inet_chk(protocol, path, arg0, arg1)
160 char *protocol;
161 char *path;
162 char *arg0;
163 char *arg1;
165 char daemon[BUFSIZ];
166 struct stat st;
167 int wrap_status = WR_MAYBE;
168 char *base_name_path = base_name(path);
169 char *tcpd_proc_name = (arg0[0] == '/' ? base_name(arg0) : arg0);
172 * Always warn when the executable does not exist or when it is not
173 * executable.
175 if (check_path(path, &st) < 0) {
176 tcpd_warn("%s: not found: %m", path);
177 } else if ((st.st_mode & 0100) == 0) {
178 tcpd_warn("%s: not executable", path);
182 * Cheat on the miscd tests, nobody uses it anymore.
184 if (STR_EQ(base_name_path, "miscd")) {
185 inet_set(arg0, WR_YES);
186 return;
190 * While we are here...
192 if (STR_EQ(tcpd_proc_name, "rexd") || STR_EQ(tcpd_proc_name, "rpc.rexd"))
193 tcpd_warn("%s may be an insecure service", tcpd_proc_name);
196 * The tcpd program gets most of the attention.
198 if (STR_EQ(base_name_path, "tcpd")) {
200 if (STR_EQ(tcpd_proc_name, "tcpd"))
201 tcpd_warn("%s is recursively calling itself", tcpd_proc_name);
203 wrap_status = WR_YES;
206 * Check: some sites install the wrapper set-uid.
208 if ((st.st_mode & 06000) != 0)
209 tcpd_warn("%s: file is set-uid or set-gid", path);
212 * Check: some sites insert tcpd in inetd.conf, instead of replacing
213 * the daemon pathname.
215 if (arg0[0] == '/' && STR_EQ(tcpd_proc_name, base_name(arg1)))
216 tcpd_warn("%s inserted before %s", path, arg0);
219 * Check: make sure files exist and are executable. On some systems
220 * the network daemons are set-uid so we cannot complain. Note that
221 * tcpd takes the basename only in case of absolute pathnames.
223 if (arg0[0] == '/') { /* absolute path */
224 if (check_path(arg0, &st) < 0) {
225 tcpd_warn("%s: not found: %m", arg0);
226 } else if ((st.st_mode & 0100) == 0) {
227 tcpd_warn("%s: not executable", arg0);
229 } else { /* look in REAL_DAEMON_DIR */
230 sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
231 if (check_path(daemon, &st) < 0) {
232 tcpd_warn("%s: not found in %s: %m",
233 arg0, REAL_DAEMON_DIR);
234 } else if ((st.st_mode & 0100) == 0) {
235 tcpd_warn("%s: not executable", daemon);
239 } else {
242 * No tcpd program found. Perhaps they used the "simple installation"
243 * recipe. Look for a file with the same basename in REAL_DAEMON_DIR.
244 * Draw some conservative conclusions when a distinct file is found.
246 sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
247 if (STR_EQ(path, daemon)) {
248 #ifdef __FreeBSD__
249 wrap_status = WR_MAYBE;
250 #else
251 wrap_status = WR_NOT;
252 #endif
253 } else if (check_path(daemon, &st) >= 0) {
254 wrap_status = WR_MAYBE;
255 } else if (errno == ENOENT) {
256 wrap_status = WR_NOT;
257 } else {
258 tcpd_warn("%s: file lookup: %m", daemon);
259 wrap_status = WR_MAYBE;
264 * Alas, we cannot wrap rpc/tcp services.
266 if (wrap_status == WR_YES && STR_EQ(protocol, "rpc/tcp"))
267 tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name);
269 inet_set(tcpd_proc_name, wrap_status);
272 /* inet_set - remember service status */
274 void inet_set(name, type)
275 char *name;
276 int type;
278 struct inet_ent *ip =
279 (struct inet_ent *) malloc(sizeof(struct inet_ent) + strlen(name));
281 if (ip == 0) {
282 fprintf(stderr, "out of memory\n");
283 exit(1);
285 ip->next = inet_list;
286 strcpy(ip->name, name);
287 ip->type = type;
288 inet_list = ip;
291 /* inet_get - look up service status */
293 int inet_get(name)
294 char *name;
296 struct inet_ent *ip;
298 if (inet_list == 0)
299 return (WR_MAYBE);
301 for (ip = inet_list; ip; ip = ip->next)
302 if (STR_EQ(ip->name, name))
303 return (ip->type);
305 return (-1);
308 /* base_name - compute last pathname component */
310 static char *base_name(path)
311 char *path;
313 char *cp;
315 if ((cp = strrchr(path, '/')) != 0)
316 path = cp + 1;
317 return (path);