e89d89058c41533d9028fbeddf854bc37756104b
[dragonfly.git] / contrib / tcp_wrappers / inetcf.c
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   */
7
8 #ifndef lint
9 static char sccsid[] = "@(#) inetcf.c 1.7 97/02/12 02:13:23";
10 #endif
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <stdlib.h>
18
19 extern void exit();
20
21 #include "tcpd.h"
22 #include "inetcf.h"
23 #include "scaffold.h"
24
25  /*
26   * Network configuration files may live in unusual places. Here are some
27   * guesses. Shorter names follow longer ones.
28   */
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?? */
37     0,
38 };
39
40 static void inet_chk();
41 static char *base_name();
42
43  /*
44   * Structure with everything we know about a service.
45   */
46 struct inet_ent {
47     struct inet_ent *next;
48     int     type;
49     char    name[1];
50 };
51
52 static struct inet_ent *inet_list = 0;
53
54 static char whitespace[] = " \t\r\n";
55
56 /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
57
58 char   *inet_cfg(conf)
59 char   *conf;
60 {
61     char    buf[BUFSIZ];
62     FILE   *fp;
63     char   *service;
64     char   *protocol;
65     char   *user;
66     char   *path;
67     char   *arg0;
68     char   *arg1;
69     struct tcpd_context saved_context;
70     char   *percent_m();
71     int     i;
72     struct stat st;
73
74     saved_context = tcpd_context;
75
76     /*
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
79      * guesses.
80      */
81     if (conf != 0) {
82         if ((fp = fopen(conf, "r")) == 0) {
83             fprintf(stderr, percent_m(buf, "open %s: %m\n"), conf);
84             exit(1);
85         }
86     } else {
87         for (i = 0; inet_files[i] && (fp = fopen(inet_files[i], "r")) == 0; i++)
88              /* void */ ;
89         if (fp == 0) {
90             fprintf(stderr, "Cannot find your inetd.conf or tlid.conf file.\n");
91             fprintf(stderr, "Please specify its location.\n");
92             exit(1);
93         }
94         conf = inet_files[i];
95         check_path(conf, &st);
96     }
97
98     /*
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.
103      */
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 == '#')
109             continue;
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)
115             continue;
116         if (user[0] == '/') {                   /* user */
117             path = user;
118         } else {                                /* path */
119             if ((path = strtok((char *) 0, whitespace)) == 0)
120                 continue;
121         }
122         if (path[0] == '?')                     /* IRIX optional service */
123             path++;
124         if (STR_EQ(path, "internal"))
125             continue;
126         if (path[strspn(path, "-0123456789")] == 0) {
127
128             /*
129              * ConvexOS puts RPC version numbers before path names. Jukka
130              * Ukkonen <ukkonen@csc.fi>.
131              */
132             if ((path = strtok((char *) 0, whitespace)) == 0)
133                 continue;
134         }
135         if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
136             tcpd_warn("incomplete line");
137             continue;
138         }
139         if (arg0[strspn(arg0, "0123456789")] == 0) {
140
141             /*
142              * We're reading a tlid.conf file, the format is:
143              * 
144              * ...stuff... path arg_count arguments mod_count modules
145              */
146             if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
147                 tcpd_warn("incomplete line");
148                 continue;
149             }
150         }
151         if ((arg1 = strtok((char *) 0, whitespace)) == 0)
152             arg1 = "";
153
154         inet_chk(protocol, path, arg0, arg1);
155     }
156     fclose(fp);
157     tcpd_context = saved_context;
158     return (conf);
159 }
160
161 /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
162
163 static void inet_chk(protocol, path, arg0, arg1)
164 char   *protocol;
165 char   *path;
166 char   *arg0;
167 char   *arg1;
168 {
169     char    daemon[BUFSIZ];
170     struct stat st;
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);
174
175     /*
176      * Always warn when the executable does not exist or when it is not
177      * executable.
178      */
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);
183     }
184
185     /*
186      * Cheat on the miscd tests, nobody uses it anymore.
187      */
188     if (STR_EQ(base_name_path, "miscd")) {
189         inet_set(arg0, WR_YES);
190         return;
191     }
192
193     /*
194      * While we are here...
195      */
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);
198
199     /*
200      * The tcpd program gets most of the attention.
201      */
202     if (STR_EQ(base_name_path, "tcpd")) {
203
204         if (STR_EQ(tcpd_proc_name, "tcpd"))
205             tcpd_warn("%s is recursively calling itself", tcpd_proc_name);
206
207         wrap_status = WR_YES;
208
209         /*
210          * Check: some sites install the wrapper set-uid.
211          */
212         if ((st.st_mode & 06000) != 0)
213             tcpd_warn("%s: file is set-uid or set-gid", path);
214
215         /*
216          * Check: some sites insert tcpd in inetd.conf, instead of replacing
217          * the daemon pathname.
218          */
219         if (arg0[0] == '/' && STR_EQ(tcpd_proc_name, base_name(arg1)))
220             tcpd_warn("%s inserted before %s", path, arg0);
221
222         /*
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.
226          */
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);
232             }
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);
240             }
241         }
242
243     } else {
244
245         /*
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.
249          */
250         sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
251         if (STR_EQ(path, daemon)) {
252 #ifdef __FreeBSD__
253             wrap_status = WR_MAYBE;
254 #else
255             wrap_status = WR_NOT;
256 #endif
257         } else if (check_path(daemon, &st) >= 0) {
258             wrap_status = WR_MAYBE;
259         } else if (errno == ENOENT) {
260             wrap_status = WR_NOT;
261         } else {
262             tcpd_warn("%s: file lookup: %m", daemon);
263             wrap_status = WR_MAYBE;
264         }
265     }
266
267     /*
268      * Alas, we cannot wrap rpc/tcp services.
269      */
270     if (wrap_status == WR_YES && STR_EQ(protocol, "rpc/tcp"))
271         tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name);
272
273     inet_set(tcpd_proc_name, wrap_status);
274 }
275
276 /* inet_set - remember service status */
277
278 void    inet_set(name, type)
279 char   *name;
280 int     type;
281 {
282     struct inet_ent *ip =
283     (struct inet_ent *) malloc(sizeof(struct inet_ent) + strlen(name));
284
285     if (ip == 0) {
286         fprintf(stderr, "out of memory\n");
287         exit(1);
288     }
289     ip->next = inet_list;
290     strcpy(ip->name, name);
291     ip->type = type;
292     inet_list = ip;
293 }
294
295 /* inet_get - look up service status */
296
297 int     inet_get(name)
298 char   *name;
299 {
300     struct inet_ent *ip;
301
302     if (inet_list == 0)
303         return (WR_MAYBE);
304
305     for (ip = inet_list; ip; ip = ip->next)
306         if (STR_EQ(ip->name, name))
307             return (ip->type);
308
309     return (-1);
310 }
311
312 /* base_name - compute last pathname component */
313
314 static char *base_name(path)
315 char   *path;
316 {
317     char   *cp;
318
319     if ((cp = strrchr(path, '/')) != 0)
320         path = cp + 1;
321     return (path);
322 }