1 /* $NetBSD: inetcf.c,v 1.7 2002/06/06 21:27:49 itojun Exp $ */
4 * Routines to parse an inetd.conf or tlid.conf file. This would be a great
5 * job for a PERL script.
7 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
10 #include <sys/cdefs.h>
13 static char sccsid
[] = "@(#) inetcf.c 1.7 97/02/12 02:13:23";
15 __RCSID("$NetBSD: inetcf.c,v 1.7 2002/06/06 21:27:49 itojun Exp $");
19 #include <sys/types.h>
28 #include "percent_m.h"
31 static void inet_chk
__P((char *, char *, char *, char *));
32 static char *base_name
__P((char *));
35 * Programs that use libwrap directly are not in inetd.conf, and so must
36 * be added here in a similar format. (We pretend we found them in
37 * /etc/inetd.conf.) Each one is a set of three strings that correspond
38 * to fields in /etc/inetd.conf:
39 * protocol (field 3), path (field 6), arg0 (field 7)
40 * The last entry should be a NULL.
42 char *uses_libwrap
[] = {
43 "tcp", "/usr/sbin/sendmail", "sendmail",
44 "tcp", "/usr/sbin/sshd", "sshd",
45 "udp", "/usr/sbin/syslogd", "syslogd",
46 "udp", "/usr/sbin/rpcbind", "rpcbind",
51 * Network configuration files may live in unusual places. Here are some
52 * guesses. Shorter names follow longer ones.
54 char *inet_files
[] = {
55 "/private/etc/inetd.conf", /* NEXT */
56 "/etc/inet/inetd.conf", /* SYSV4 */
57 "/usr/etc/inetd.conf", /* IRIX?? */
58 "/etc/inetd.conf", /* BSD */
59 "/etc/net/tlid.conf", /* SYSV4?? */
60 "/etc/saf/tlid.conf", /* SYSV4?? */
61 "/etc/tlid.conf", /* SYSV4?? */
66 * Structure with everything we know about a service.
69 struct inet_ent
*next
;
74 static struct inet_ent
*inet_list
= 0;
76 static char whitespace
[] = " \t\r\n";
78 /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
92 struct tcpd_context saved_context
;
96 saved_context
= tcpd_context
;
99 * The inetd.conf (or tlid.conf) information is so useful that we insist
100 * on its availability. When no file is given run a series of educated
104 if ((fp
= fopen(conf
, "r")) == 0) {
105 fprintf(stderr
, percent_m(buf
, "open %s: %m\n"), conf
);
109 for (i
= 0; inet_files
[i
] && (fp
= fopen(inet_files
[i
], "r")) == 0; i
++)
112 fprintf(stderr
, "Cannot find your inetd.conf or tlid.conf file.\n");
113 fprintf(stderr
, "Please specify its location.\n");
116 conf
= inet_files
[i
];
117 check_path(conf
, &st
);
121 * Process the list of programs that use libwrap directly.
123 wrapped
= uses_libwrap
;
124 while (*wrapped
!= NULL
) {
125 inet_chk(wrapped
[0], wrapped
[1], wrapped
[2], "");
130 * Process the file. After the 7.0 wrapper release it became clear that
131 * there are many more inetd.conf formats than the 8 systems that I had
132 * studied. EP/IX uses a two-line specification for rpc services; HP-UX
133 * permits long lines to be broken with backslash-newline.
135 tcpd_context
.file
= conf
;
136 tcpd_context
.line
= 0;
137 while (xgets(buf
, sizeof(buf
), fp
)) {
138 service
= strtok(buf
, whitespace
); /* service */
139 if (service
== 0 || *service
== '#')
141 if (STR_NE(service
, "stream") && STR_NE(service
, "dgram"))
142 strtok((char *) 0, whitespace
); /* endpoint */
143 protocol
= strtok((char *) 0, whitespace
);
144 (void) strtok((char *) 0, whitespace
); /* wait */
145 if ((user
= strtok((char *) 0, whitespace
)) == 0)
147 if (user
[0] == '/') { /* user */
150 if ((path
= strtok((char *) 0, whitespace
)) == 0)
153 if (path
[0] == '?') /* IRIX optional service */
155 if (STR_EQ(path
, "internal"))
157 if (path
[strspn(path
, "-0123456789")] == 0) {
160 * ConvexOS puts RPC version numbers before path names. Jukka
161 * Ukkonen <ukkonen@csc.fi>.
163 if ((path
= strtok((char *) 0, whitespace
)) == 0)
166 if ((arg0
= strtok((char *) 0, whitespace
)) == 0) {
167 tcpd_warn("incomplete line");
170 if (arg0
[strspn(arg0
, "0123456789")] == 0) {
173 * We're reading a tlid.conf file, the format is:
175 * ...stuff... path arg_count arguments mod_count modules
177 if ((arg0
= strtok((char *) 0, whitespace
)) == 0) {
178 tcpd_warn("incomplete line");
182 if ((arg1
= strtok((char *) 0, whitespace
)) == 0)
185 inet_chk(protocol
, path
, arg0
, arg1
);
188 tcpd_context
= saved_context
;
192 /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
194 static void inet_chk(protocol
, path
, arg0
, arg1
)
202 int wrap_status
= WR_MAYBE
;
203 char *base_name_path
= base_name(path
);
204 char *tcpd_proc_name
= (arg0
[0] == '/' ? base_name(arg0
) : arg0
);
207 * Always warn when the executable does not exist or when it is not
210 if (check_path(path
, &st
) < 0) {
211 tcpd_warn("%s: not found: %m", path
);
212 } else if ((st
.st_mode
& 0100) == 0) {
213 tcpd_warn("%s: not executable", path
);
217 * Cheat on the miscd tests, nobody uses it anymore.
219 if (STR_EQ(base_name_path
, "miscd")) {
220 inet_set(arg0
, WR_YES
);
225 * While we are here...
227 if (STR_EQ(tcpd_proc_name
, "rexd") || STR_EQ(tcpd_proc_name
, "rpc.rexd"))
228 tcpd_warn("%s may be an insecure service", tcpd_proc_name
);
231 * The tcpd program gets most of the attention.
233 if (STR_EQ(base_name_path
, "tcpd")) {
235 if (STR_EQ(tcpd_proc_name
, "tcpd"))
236 tcpd_warn("%s is recursively calling itself", tcpd_proc_name
);
238 wrap_status
= WR_YES
;
241 * Check: some sites install the wrapper set-uid.
243 if ((st
.st_mode
& 06000) != 0)
244 tcpd_warn("%s: file is set-uid or set-gid", path
);
247 * Check: some sites insert tcpd in inetd.conf, instead of replacing
248 * the daemon pathname.
250 if (arg0
[0] == '/' && STR_EQ(tcpd_proc_name
, base_name(arg1
)))
251 tcpd_warn("%s inserted before %s", path
, arg0
);
254 * Check: make sure files exist and are executable. On some systems
255 * the network daemons are set-uid so we cannot complain. Note that
256 * tcpd takes the basename only in case of absolute pathnames.
258 if (arg0
[0] == '/') { /* absolute path */
259 if (check_path(arg0
, &st
) < 0) {
260 tcpd_warn("%s: not found: %m", arg0
);
261 } else if ((st
.st_mode
& 0100) == 0) {
262 tcpd_warn("%s: not executable", arg0
);
264 } else { /* look in REAL_DAEMON_DIR */
265 snprintf(daemon
, sizeof(daemon
), "%s/%s", REAL_DAEMON_DIR
, arg0
);
266 if (check_path(daemon
, &st
) < 0) {
267 tcpd_warn("%s: not found in %s: %m",
268 arg0
, REAL_DAEMON_DIR
);
269 } else if ((st
.st_mode
& 0100) == 0) {
270 tcpd_warn("%s: not executable", daemon
);
277 * No tcpd program found. Perhaps they used the "simple installation"
278 * recipe. Look for a file with the same basename in REAL_DAEMON_DIR.
279 * Draw some conservative conclusions when a distinct file is found.
281 snprintf(daemon
, sizeof(daemon
), "%s/%s", REAL_DAEMON_DIR
, arg0
);
282 if (STR_EQ(path
, daemon
)) {
283 wrap_status
= WR_NOT
;
284 } else if (check_path(daemon
, &st
) >= 0) {
285 wrap_status
= WR_MAYBE
;
286 } else if (errno
== ENOENT
) {
287 wrap_status
= WR_NOT
;
289 tcpd_warn("%s: file lookup: %m", daemon
);
290 wrap_status
= WR_MAYBE
;
295 * Alas, we cannot wrap rpc/tcp services.
297 if (wrap_status
== WR_YES
&& STR_EQ(protocol
, "rpc/tcp"))
298 tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name
);
300 /* NetBSD inetd wraps all programs */
301 if (! STR_EQ(protocol
, "rpc/tcp"))
302 wrap_status
= WR_YES
;
304 inet_set(tcpd_proc_name
, wrap_status
);
307 /* inet_set - remember service status */
309 void inet_set(name
, type
)
313 struct inet_ent
*ip
=
314 (struct inet_ent
*) malloc(sizeof(struct inet_ent
) + strlen(name
));
317 fprintf(stderr
, "out of memory\n");
320 ip
->next
= inet_list
;
321 strcpy(ip
->name
, name
);
326 /* inet_get - look up service status */
336 for (ip
= inet_list
; ip
; ip
= ip
->next
)
337 if (STR_EQ(ip
->name
, name
))
343 /* base_name - compute last pathname component */
345 static char *base_name(path
)
350 if ((cp
= strrchr(path
, '/')) != 0)