2 * Routines to parse an inetd.conf or tlid.conf file. This would be a great
3 * job for a PERL script.
5 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
9 static char sccsid[] = "@(#) inetcf.c 1.7 97/02/12 02:13:23";
12 #include <sys/types.h>
26 * Network configuration files may live in unusual places. Here are some
27 * guesses. Shorter names follow longer ones.
29 char *inet_files[] = {
30 "/private/etc/inetd.conf", /* NEXT */
31 "/etc/inet/inetd.conf", /* SYSV4 */
32 "/usr/etc/inetd.conf", /* IRIX?? */
33 "/etc/inetd.conf", /* BSD */
34 "/etc/net/tlid.conf", /* SYSV4?? */
35 "/etc/saf/tlid.conf", /* SYSV4?? */
36 "/etc/tlid.conf", /* SYSV4?? */
40 static void inet_chk();
41 static char *base_name();
44 * Structure with everything we know about a service.
47 struct inet_ent *next;
52 static struct inet_ent *inet_list = 0;
54 static char whitespace[] = " \t\r\n";
56 /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
69 struct tcpd_context saved_context;
74 saved_context = tcpd_context;
77 * The inetd.conf (or tlid.conf) information is so useful that we insist
78 * on its availability. When no file is given run a series of educated
82 if ((fp = fopen(conf, "r")) == 0) {
83 fprintf(stderr, percent_m(buf, "open %s: %m\n"), conf);
87 for (i = 0; inet_files[i] && (fp = fopen(inet_files[i], "r")) == 0; i++)
90 fprintf(stderr, "Cannot find your inetd.conf or tlid.conf file.\n");
91 fprintf(stderr, "Please specify its location.\n");
95 check_path(conf, &st);
99 * Process the file. After the 7.0 wrapper release it became clear that
100 * there are many more inetd.conf formats than the 8 systems that I had
101 * studied. EP/IX uses a two-line specification for rpc services; HP-UX
102 * permits long lines to be broken with backslash-newline.
104 tcpd_context.file = conf;
105 tcpd_context.line = 0;
106 while (xgets(buf, sizeof(buf), fp)) {
107 service = strtok(buf, whitespace); /* service */
108 if (service == 0 || *service == '#')
110 if (STR_NE(service, "stream") && STR_NE(service, "dgram"))
111 strtok((char *) 0, whitespace); /* endpoint */
112 protocol = strtok((char *) 0, whitespace);
113 (void) strtok((char *) 0, whitespace); /* wait */
114 if ((user = strtok((char *) 0, whitespace)) == 0)
116 if (user[0] == '/') { /* user */
119 if ((path = strtok((char *) 0, whitespace)) == 0)
122 if (path[0] == '?') /* IRIX optional service */
124 if (STR_EQ(path, "internal"))
126 if (path[strspn(path, "-0123456789")] == 0) {
129 * ConvexOS puts RPC version numbers before path names. Jukka
130 * Ukkonen <ukkonen@csc.fi>.
132 if ((path = strtok((char *) 0, whitespace)) == 0)
135 if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
136 tcpd_warn("incomplete line");
139 if (arg0[strspn(arg0, "0123456789")] == 0) {
142 * We're reading a tlid.conf file, the format is:
144 * ...stuff... path arg_count arguments mod_count modules
146 if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
147 tcpd_warn("incomplete line");
151 if ((arg1 = strtok((char *) 0, whitespace)) == 0)
154 inet_chk(protocol, path, arg0, arg1);
157 tcpd_context = saved_context;
161 /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
163 static void inet_chk(protocol, path, arg0, arg1)
171 int wrap_status = WR_MAYBE;
172 char *base_name_path = base_name(path);
173 char *tcpd_proc_name = (arg0[0] == '/' ? base_name(arg0) : arg0);
176 * Always warn when the executable does not exist or when it is not
179 if (check_path(path, &st) < 0) {
180 tcpd_warn("%s: not found: %m", path);
181 } else if ((st.st_mode & 0100) == 0) {
182 tcpd_warn("%s: not executable", path);
186 * Cheat on the miscd tests, nobody uses it anymore.
188 if (STR_EQ(base_name_path, "miscd")) {
189 inet_set(arg0, WR_YES);
194 * While we are here...
196 if (STR_EQ(tcpd_proc_name, "rexd") || STR_EQ(tcpd_proc_name, "rpc.rexd"))
197 tcpd_warn("%s may be an insecure service", tcpd_proc_name);
200 * The tcpd program gets most of the attention.
202 if (STR_EQ(base_name_path, "tcpd")) {
204 if (STR_EQ(tcpd_proc_name, "tcpd"))
205 tcpd_warn("%s is recursively calling itself", tcpd_proc_name);
207 wrap_status = WR_YES;
210 * Check: some sites install the wrapper set-uid.
212 if ((st.st_mode & 06000) != 0)
213 tcpd_warn("%s: file is set-uid or set-gid", path);
216 * Check: some sites insert tcpd in inetd.conf, instead of replacing
217 * the daemon pathname.
219 if (arg0[0] == '/' && STR_EQ(tcpd_proc_name, base_name(arg1)))
220 tcpd_warn("%s inserted before %s", path, arg0);
223 * Check: make sure files exist and are executable. On some systems
224 * the network daemons are set-uid so we cannot complain. Note that
225 * tcpd takes the basename only in case of absolute pathnames.
227 if (arg0[0] == '/') { /* absolute path */
228 if (check_path(arg0, &st) < 0) {
229 tcpd_warn("%s: not found: %m", arg0);
230 } else if ((st.st_mode & 0100) == 0) {
231 tcpd_warn("%s: not executable", arg0);
233 } else { /* look in REAL_DAEMON_DIR */
234 sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
235 if (check_path(daemon, &st) < 0) {
236 tcpd_warn("%s: not found in %s: %m",
237 arg0, REAL_DAEMON_DIR);
238 } else if ((st.st_mode & 0100) == 0) {
239 tcpd_warn("%s: not executable", daemon);
246 * No tcpd program found. Perhaps they used the "simple installation"
247 * recipe. Look for a file with the same basename in REAL_DAEMON_DIR.
248 * Draw some conservative conclusions when a distinct file is found.
250 sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
251 if (STR_EQ(path, daemon)) {
253 wrap_status = WR_MAYBE;
255 wrap_status = WR_NOT;
257 } else if (check_path(daemon, &st) >= 0) {
258 wrap_status = WR_MAYBE;
259 } else if (errno == ENOENT) {
260 wrap_status = WR_NOT;
262 tcpd_warn("%s: file lookup: %m", daemon);
263 wrap_status = WR_MAYBE;
268 * Alas, we cannot wrap rpc/tcp services.
270 if (wrap_status == WR_YES && STR_EQ(protocol, "rpc/tcp"))
271 tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name);
273 inet_set(tcpd_proc_name, wrap_status);
276 /* inet_set - remember service status */
278 void inet_set(name, type)
282 struct inet_ent *ip =
283 (struct inet_ent *) malloc(sizeof(struct inet_ent) + strlen(name));
286 fprintf(stderr, "out of memory\n");
289 ip->next = inet_list;
290 strcpy(ip->name, name);
295 /* inet_get - look up service status */
305 for (ip = inet_list; ip; ip = ip->next)
306 if (STR_EQ(ip->name, name))
312 /* base_name - compute last pathname component */
314 static char *base_name(path)
319 if ((cp = strrchr(path, '/')) != 0)