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