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