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>
24 * Network configuration files may live in unusual places. Here are some
25 * guesses. Shorter names follow longer ones.
27 char *inet_files
[] = {
28 "/private/etc/inetd.conf", /* NEXT */
29 "/etc/inet/inetd.conf", /* SYSV4 */
30 "/usr/etc/inetd.conf", /* IRIX?? */
31 "/etc/inetd.conf", /* BSD */
32 "/etc/net/tlid.conf", /* SYSV4?? */
33 "/etc/saf/tlid.conf", /* SYSV4?? */
34 "/etc/tlid.conf", /* SYSV4?? */
38 static void inet_chk();
39 static char *base_name();
42 * Structure with everything we know about a service.
45 struct inet_ent
*next
;
50 static struct inet_ent
*inet_list
= 0;
52 static char whitespace
[] = " \t\r\n";
54 /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
67 struct tcpd_context saved_context
;
72 saved_context
= tcpd_context
;
75 * The inetd.conf (or tlid.conf) information is so useful that we insist
76 * on its availability. When no file is given run a series of educated
80 if ((fp
= fopen(conf
, "r")) == 0) {
81 fprintf(stderr
, percent_m(buf
, "open %s: %m\n"), conf
);
85 for (i
= 0; inet_files
[i
] && (fp
= fopen(inet_files
[i
], "r")) == 0; i
++)
88 fprintf(stderr
, "Cannot find your inetd.conf or tlid.conf file.\n");
89 fprintf(stderr
, "Please specify its location.\n");
93 check_path(conf
, &st
);
97 * Process the file. After the 7.0 wrapper release it became clear that
98 * there are many more inetd.conf formats than the 8 systems that I had
99 * studied. EP/IX uses a two-line specification for rpc services; HP-UX
100 * permits long lines to be broken with backslash-newline.
102 tcpd_context
.file
= conf
;
103 tcpd_context
.line
= 0;
104 while (xgets(buf
, sizeof(buf
), fp
)) {
105 service
= strtok(buf
, whitespace
); /* service */
106 if (service
== 0 || *service
== '#')
108 if (STR_NE(service
, "stream") && STR_NE(service
, "dgram"))
109 strtok((char *) 0, whitespace
); /* endpoint */
110 protocol
= strtok((char *) 0, whitespace
);
111 (void) strtok((char *) 0, whitespace
); /* wait */
112 if ((user
= strtok((char *) 0, whitespace
)) == 0)
114 if (user
[0] == '/') { /* user */
117 if ((path
= strtok((char *) 0, whitespace
)) == 0)
120 if (path
[0] == '?') /* IRIX optional service */
122 if (STR_EQ(path
, "internal"))
124 if (path
[strspn(path
, "-0123456789")] == 0) {
127 * ConvexOS puts RPC version numbers before path names. Jukka
128 * Ukkonen <ukkonen@csc.fi>.
130 if ((path
= strtok((char *) 0, whitespace
)) == 0)
133 if ((arg0
= strtok((char *) 0, whitespace
)) == 0) {
134 tcpd_warn("incomplete line");
137 if (arg0
[strspn(arg0
, "0123456789")] == 0) {
140 * We're reading a tlid.conf file, the format is:
142 * ...stuff... path arg_count arguments mod_count modules
144 if ((arg0
= strtok((char *) 0, whitespace
)) == 0) {
145 tcpd_warn("incomplete line");
149 if ((arg1
= strtok((char *) 0, whitespace
)) == 0)
152 inet_chk(protocol
, path
, arg0
, arg1
);
155 tcpd_context
= saved_context
;
159 /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
161 static void inet_chk(protocol
, path
, arg0
, arg1
)
169 int wrap_status
= WR_MAYBE
;
170 char *base_name_path
= base_name(path
);
171 char *tcpd_proc_name
= (arg0
[0] == '/' ? base_name(arg0
) : arg0
);
174 * Always warn when the executable does not exist or when it is not
177 if (check_path(path
, &st
) < 0) {
178 tcpd_warn("%s: not found: %m", path
);
179 } else if ((st
.st_mode
& 0100) == 0) {
180 tcpd_warn("%s: not executable", path
);
184 * Cheat on the miscd tests, nobody uses it anymore.
186 if (STR_EQ(base_name_path
, "miscd")) {
187 inet_set(arg0
, WR_YES
);
192 * While we are here...
194 if (STR_EQ(tcpd_proc_name
, "rexd") || STR_EQ(tcpd_proc_name
, "rpc.rexd"))
195 tcpd_warn("%s may be an insecure service", tcpd_proc_name
);
198 * The tcpd program gets most of the attention.
200 if (STR_EQ(base_name_path
, "tcpd")) {
202 if (STR_EQ(tcpd_proc_name
, "tcpd"))
203 tcpd_warn("%s is recursively calling itself", tcpd_proc_name
);
205 wrap_status
= WR_YES
;
208 * Check: some sites install the wrapper set-uid.
210 if ((st
.st_mode
& 06000) != 0)
211 tcpd_warn("%s: file is set-uid or set-gid", path
);
214 * Check: some sites insert tcpd in inetd.conf, instead of replacing
215 * the daemon pathname.
217 if (arg0
[0] == '/' && STR_EQ(tcpd_proc_name
, base_name(arg1
)))
218 tcpd_warn("%s inserted before %s", path
, arg0
);
221 * Check: make sure files exist and are executable. On some systems
222 * the network daemons are set-uid so we cannot complain. Note that
223 * tcpd takes the basename only in case of absolute pathnames.
225 if (arg0
[0] == '/') { /* absolute path */
226 if (check_path(arg0
, &st
) < 0) {
227 tcpd_warn("%s: not found: %m", arg0
);
228 } else if ((st
.st_mode
& 0100) == 0) {
229 tcpd_warn("%s: not executable", arg0
);
231 } else { /* look in REAL_DAEMON_DIR */
232 sprintf(daemon
, "%s/%s", REAL_DAEMON_DIR
, arg0
);
233 if (check_path(daemon
, &st
) < 0) {
234 tcpd_warn("%s: not found in %s: %m",
235 arg0
, REAL_DAEMON_DIR
);
236 } else if ((st
.st_mode
& 0100) == 0) {
237 tcpd_warn("%s: not executable", daemon
);
244 * No tcpd program found. Perhaps they used the "simple installation"
245 * recipe. Look for a file with the same basename in REAL_DAEMON_DIR.
246 * Draw some conservative conclusions when a distinct file is found.
248 sprintf(daemon
, "%s/%s", REAL_DAEMON_DIR
, arg0
);
249 if (STR_EQ(path
, daemon
)) {
251 wrap_status
= WR_MAYBE
;
253 wrap_status
= WR_NOT
;
255 } else if (check_path(daemon
, &st
) >= 0) {
256 wrap_status
= WR_MAYBE
;
257 } else if (errno
== ENOENT
) {
258 wrap_status
= WR_NOT
;
260 tcpd_warn("%s: file lookup: %m", daemon
);
261 wrap_status
= WR_MAYBE
;
266 * Alas, we cannot wrap rpc/tcp services.
268 if (wrap_status
== WR_YES
&& STR_EQ(protocol
, "rpc/tcp"))
269 tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name
);
271 inet_set(tcpd_proc_name
, wrap_status
);
274 /* inet_set - remember service status */
276 void inet_set(name
, type
)
280 struct inet_ent
*ip
=
281 (struct inet_ent
*) malloc(sizeof(struct inet_ent
) + strlen(name
));
284 fprintf(stderr
, "out of memory\n");
287 ip
->next
= inet_list
;
288 strcpy(ip
->name
, name
);
293 /* inet_get - look up service status */
303 for (ip
= inet_list
; ip
; ip
= ip
->next
)
304 if (STR_EQ(ip
->name
, name
))
310 /* base_name - compute last pathname component */
312 static char *base_name(path
)
317 if ((cp
= strrchr(path
, '/')) != 0)