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