xref: /illumos-gate/usr/src/cmd/tcpd/inetcf.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate  /*
27c478bd9Sstevel@tonic-gate   * Routines to parse an inetd.conf or tlid.conf file. This would be a great
37c478bd9Sstevel@tonic-gate   * job for a PERL script.
4*55fea89dSDan Cross   *
57c478bd9Sstevel@tonic-gate   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
67c478bd9Sstevel@tonic-gate   */
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate #ifndef lint
97c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#) inetcf.c 1.7 97/02/12 02:13:23";
107c478bd9Sstevel@tonic-gate #endif
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include <sys/types.h>
137c478bd9Sstevel@tonic-gate #include <sys/stat.h>
147c478bd9Sstevel@tonic-gate #include <stdio.h>
157c478bd9Sstevel@tonic-gate #include <errno.h>
167c478bd9Sstevel@tonic-gate #include <string.h>
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate extern int errno;
197c478bd9Sstevel@tonic-gate extern void exit();
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #include "tcpd.h"
227c478bd9Sstevel@tonic-gate #include "inetcf.h"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate  /*
257c478bd9Sstevel@tonic-gate   * Network configuration files may live in unusual places. Here are some
267c478bd9Sstevel@tonic-gate   * guesses. Shorter names follow longer ones.
277c478bd9Sstevel@tonic-gate   */
287c478bd9Sstevel@tonic-gate char   *inet_files[] = {
297c478bd9Sstevel@tonic-gate     "/private/etc/inetd.conf",		/* NEXT */
307c478bd9Sstevel@tonic-gate     "/etc/inet/inetd.conf",		/* SYSV4 */
317c478bd9Sstevel@tonic-gate     "/usr/etc/inetd.conf",		/* IRIX?? */
327c478bd9Sstevel@tonic-gate     "/etc/inetd.conf",			/* BSD */
337c478bd9Sstevel@tonic-gate     "/etc/net/tlid.conf",		/* SYSV4?? */
347c478bd9Sstevel@tonic-gate     "/etc/saf/tlid.conf",		/* SYSV4?? */
357c478bd9Sstevel@tonic-gate     "/etc/tlid.conf",			/* SYSV4?? */
367c478bd9Sstevel@tonic-gate     0,
377c478bd9Sstevel@tonic-gate };
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void inet_chk();
407c478bd9Sstevel@tonic-gate static char *base_name();
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate  /*
437c478bd9Sstevel@tonic-gate   * Structure with everything we know about a service.
447c478bd9Sstevel@tonic-gate   */
457c478bd9Sstevel@tonic-gate struct inet_ent {
467c478bd9Sstevel@tonic-gate     struct inet_ent *next;
477c478bd9Sstevel@tonic-gate     int     type;
487c478bd9Sstevel@tonic-gate     char    name[1];
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static struct inet_ent *inet_list = 0;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static char whitespace[] = " \t\r\n";
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* inet_conf - read in and examine inetd.conf (or tlid.conf) entries */
567c478bd9Sstevel@tonic-gate 
inet_cfg(conf)577c478bd9Sstevel@tonic-gate char   *inet_cfg(conf)
587c478bd9Sstevel@tonic-gate char   *conf;
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate     char    buf[BUFSIZ];
617c478bd9Sstevel@tonic-gate     FILE   *fp;
627c478bd9Sstevel@tonic-gate     char   *service;
637c478bd9Sstevel@tonic-gate     char   *protocol;
647c478bd9Sstevel@tonic-gate     char   *user;
657c478bd9Sstevel@tonic-gate     char   *path;
667c478bd9Sstevel@tonic-gate     char   *arg0;
677c478bd9Sstevel@tonic-gate     char   *arg1;
687c478bd9Sstevel@tonic-gate     struct tcpd_context saved_context;
697c478bd9Sstevel@tonic-gate     char   *percent_m();
707c478bd9Sstevel@tonic-gate     int     i;
717c478bd9Sstevel@tonic-gate     struct stat st;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate     saved_context = tcpd_context;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate     /*
767c478bd9Sstevel@tonic-gate      * The inetd.conf (or tlid.conf) information is so useful that we insist
777c478bd9Sstevel@tonic-gate      * on its availability. When no file is given run a series of educated
787c478bd9Sstevel@tonic-gate      * guesses.
797c478bd9Sstevel@tonic-gate      */
807c478bd9Sstevel@tonic-gate     if (conf != 0) {
817c478bd9Sstevel@tonic-gate 	if ((fp = fopen(conf, "r")) == 0) {
827c478bd9Sstevel@tonic-gate 	    fprintf(stderr, percent_m(buf, "open %s: %m\n"), conf);
837c478bd9Sstevel@tonic-gate 	    exit(1);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate     } else {
867c478bd9Sstevel@tonic-gate 	for (i = 0; inet_files[i] && (fp = fopen(inet_files[i], "r")) == 0; i++)
877c478bd9Sstevel@tonic-gate 	     /* void */ ;
887c478bd9Sstevel@tonic-gate 	if (fp == 0) {
897c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Cannot find your inetd.conf or tlid.conf file.\n");
907c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Please specify its location.\n");
917c478bd9Sstevel@tonic-gate 	    exit(1);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	conf = inet_files[i];
947c478bd9Sstevel@tonic-gate 	check_path(conf, &st);
957c478bd9Sstevel@tonic-gate     }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate     /*
987c478bd9Sstevel@tonic-gate      * Process the file. After the 7.0 wrapper release it became clear that
997c478bd9Sstevel@tonic-gate      * there are many more inetd.conf formats than the 8 systems that I had
1007c478bd9Sstevel@tonic-gate      * studied. EP/IX uses a two-line specification for rpc services; HP-UX
1017c478bd9Sstevel@tonic-gate      * permits long lines to be broken with backslash-newline.
1027c478bd9Sstevel@tonic-gate      */
1037c478bd9Sstevel@tonic-gate     tcpd_context.file = conf;
1047c478bd9Sstevel@tonic-gate     tcpd_context.line = 0;
1057c478bd9Sstevel@tonic-gate     while (xgets(buf, sizeof(buf), fp)) {
1067c478bd9Sstevel@tonic-gate 	service = strtok(buf, whitespace);	/* service */
1077c478bd9Sstevel@tonic-gate 	if (service == 0 || *service == '#')
1087c478bd9Sstevel@tonic-gate 	    continue;
1097c478bd9Sstevel@tonic-gate 	if (STR_NE(service, "stream") && STR_NE(service, "dgram"))
1107c478bd9Sstevel@tonic-gate 	    strtok((char *) 0, whitespace);	/* endpoint */
1117c478bd9Sstevel@tonic-gate 	protocol = strtok((char *) 0, whitespace);
1127c478bd9Sstevel@tonic-gate 	(void) strtok((char *) 0, whitespace);	/* wait */
1137c478bd9Sstevel@tonic-gate 	if ((user = strtok((char *) 0, whitespace)) == 0)
1147c478bd9Sstevel@tonic-gate 	    continue;
1157c478bd9Sstevel@tonic-gate 	if (user[0] == '/') {			/* user */
1167c478bd9Sstevel@tonic-gate 	    path = user;
1177c478bd9Sstevel@tonic-gate 	} else {				/* path */
1187c478bd9Sstevel@tonic-gate 	    if ((path = strtok((char *) 0, whitespace)) == 0)
1197c478bd9Sstevel@tonic-gate 		continue;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	if (path[0] == '?')			/* IRIX optional service */
1227c478bd9Sstevel@tonic-gate 	    path++;
1237c478bd9Sstevel@tonic-gate 	if (STR_EQ(path, "internal"))
1247c478bd9Sstevel@tonic-gate 	    continue;
1257c478bd9Sstevel@tonic-gate 	if (path[strspn(path, "-0123456789")] == 0) {
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	    /*
1287c478bd9Sstevel@tonic-gate 	     * ConvexOS puts RPC version numbers before path names. Jukka
1297c478bd9Sstevel@tonic-gate 	     * Ukkonen <ukkonen@csc.fi>.
1307c478bd9Sstevel@tonic-gate 	     */
1317c478bd9Sstevel@tonic-gate 	    if ((path = strtok((char *) 0, whitespace)) == 0)
1327c478bd9Sstevel@tonic-gate 		continue;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
1357c478bd9Sstevel@tonic-gate 	    tcpd_warn("incomplete line");
1367c478bd9Sstevel@tonic-gate 	    continue;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	if (arg0[strspn(arg0, "0123456789")] == 0) {
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	    /*
1417c478bd9Sstevel@tonic-gate 	     * We're reading a tlid.conf file, the format is:
142*55fea89dSDan Cross 	     *
1437c478bd9Sstevel@tonic-gate 	     * ...stuff... path arg_count arguments mod_count modules
1447c478bd9Sstevel@tonic-gate 	     */
1457c478bd9Sstevel@tonic-gate 	    if ((arg0 = strtok((char *) 0, whitespace)) == 0) {
1467c478bd9Sstevel@tonic-gate 		tcpd_warn("incomplete line");
1477c478bd9Sstevel@tonic-gate 		continue;
1487c478bd9Sstevel@tonic-gate 	    }
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	if ((arg1 = strtok((char *) 0, whitespace)) == 0)
1517c478bd9Sstevel@tonic-gate 	    arg1 = "";
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	inet_chk(protocol, path, arg0, arg1);
1547c478bd9Sstevel@tonic-gate     }
1557c478bd9Sstevel@tonic-gate     fclose(fp);
1567c478bd9Sstevel@tonic-gate     tcpd_context = saved_context;
1577c478bd9Sstevel@tonic-gate     return (conf);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* inet_chk - examine one inetd.conf (tlid.conf?) entry */
1617c478bd9Sstevel@tonic-gate 
inet_chk(protocol,path,arg0,arg1)1627c478bd9Sstevel@tonic-gate static void inet_chk(protocol, path, arg0, arg1)
1637c478bd9Sstevel@tonic-gate char   *protocol;
1647c478bd9Sstevel@tonic-gate char   *path;
1657c478bd9Sstevel@tonic-gate char   *arg0;
1667c478bd9Sstevel@tonic-gate char   *arg1;
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate     char    daemon[BUFSIZ];
1697c478bd9Sstevel@tonic-gate     struct stat st;
1707c478bd9Sstevel@tonic-gate     int     wrap_status = WR_MAYBE;
1717c478bd9Sstevel@tonic-gate     char   *base_name_path = base_name(path);
1727c478bd9Sstevel@tonic-gate     char   *tcpd_proc_name = (arg0[0] == '/' ? base_name(arg0) : arg0);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate     /*
1757c478bd9Sstevel@tonic-gate      * Always warn when the executable does not exist or when it is not
1767c478bd9Sstevel@tonic-gate      * executable.
1777c478bd9Sstevel@tonic-gate      */
1787c478bd9Sstevel@tonic-gate     if (check_path(path, &st) < 0) {
1797c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: not found: %m", path);
1807c478bd9Sstevel@tonic-gate     } else if ((st.st_mode & 0100) == 0) {
1817c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: not executable", path);
1827c478bd9Sstevel@tonic-gate     }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate     /*
1857c478bd9Sstevel@tonic-gate      * Cheat on the miscd tests, nobody uses it anymore.
1867c478bd9Sstevel@tonic-gate      */
1877c478bd9Sstevel@tonic-gate     if (STR_EQ(base_name_path, "miscd")) {
1887c478bd9Sstevel@tonic-gate 	inet_set(arg0, WR_YES);
1897c478bd9Sstevel@tonic-gate 	return;
1907c478bd9Sstevel@tonic-gate     }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate     /*
1937c478bd9Sstevel@tonic-gate      * While we are here...
1947c478bd9Sstevel@tonic-gate      */
1957c478bd9Sstevel@tonic-gate     if (STR_EQ(tcpd_proc_name, "rexd") || STR_EQ(tcpd_proc_name, "rpc.rexd"))
1967c478bd9Sstevel@tonic-gate 	tcpd_warn("%s may be an insecure service", tcpd_proc_name);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate     /*
1997c478bd9Sstevel@tonic-gate      * The tcpd program gets most of the attention.
2007c478bd9Sstevel@tonic-gate      */
2017c478bd9Sstevel@tonic-gate     if (STR_EQ(base_name_path, "tcpd")) {
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (STR_EQ(tcpd_proc_name, "tcpd"))
2047c478bd9Sstevel@tonic-gate 	    tcpd_warn("%s is recursively calling itself", tcpd_proc_name);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	wrap_status = WR_YES;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Check: some sites install the wrapper set-uid.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if ((st.st_mode & 06000) != 0)
2127c478bd9Sstevel@tonic-gate 	    tcpd_warn("%s: file is set-uid or set-gid", path);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Check: some sites insert tcpd in inetd.conf, instead of replacing
2167c478bd9Sstevel@tonic-gate 	 * the daemon pathname.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	if (arg0[0] == '/' && STR_EQ(tcpd_proc_name, base_name(arg1)))
2197c478bd9Sstevel@tonic-gate 	    tcpd_warn("%s inserted before %s", path, arg0);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/*
2227c478bd9Sstevel@tonic-gate 	 * Check: make sure files exist and are executable. On some systems
2237c478bd9Sstevel@tonic-gate 	 * the network daemons are set-uid so we cannot complain. Note that
2247c478bd9Sstevel@tonic-gate 	 * tcpd takes the basename only in case of absolute pathnames.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	if (arg0[0] == '/') {			/* absolute path */
2277c478bd9Sstevel@tonic-gate 	    if (check_path(arg0, &st) < 0) {
2287c478bd9Sstevel@tonic-gate 		tcpd_warn("%s: not found: %m", arg0);
2297c478bd9Sstevel@tonic-gate 	    } else if ((st.st_mode & 0100) == 0) {
2307c478bd9Sstevel@tonic-gate 		tcpd_warn("%s: not executable", arg0);
2317c478bd9Sstevel@tonic-gate 	    }
2327c478bd9Sstevel@tonic-gate 	} else {				/* look in REAL_DAEMON_DIR */
2337c478bd9Sstevel@tonic-gate 	    sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
2347c478bd9Sstevel@tonic-gate 	    if (check_path(daemon, &st) < 0) {
2357c478bd9Sstevel@tonic-gate 		tcpd_warn("%s: not found in %s: %m",
2367c478bd9Sstevel@tonic-gate 			  arg0, REAL_DAEMON_DIR);
2377c478bd9Sstevel@tonic-gate 	    } else if ((st.st_mode & 0100) == 0) {
2387c478bd9Sstevel@tonic-gate 		tcpd_warn("%s: not executable", daemon);
2397c478bd9Sstevel@tonic-gate 	    }
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate     } else {
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * No tcpd program found. Perhaps they used the "simple installation"
2467c478bd9Sstevel@tonic-gate 	 * recipe. Look for a file with the same basename in REAL_DAEMON_DIR.
2477c478bd9Sstevel@tonic-gate 	 * Draw some conservative conclusions when a distinct file is found.
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	sprintf(daemon, "%s/%s", REAL_DAEMON_DIR, arg0);
2507c478bd9Sstevel@tonic-gate 	if (STR_EQ(path, daemon)) {
2517c478bd9Sstevel@tonic-gate 	    wrap_status = WR_NOT;
2527c478bd9Sstevel@tonic-gate 	} else if (check_path(daemon, &st) >= 0) {
2537c478bd9Sstevel@tonic-gate 	    wrap_status = WR_MAYBE;
2547c478bd9Sstevel@tonic-gate 	} else if (errno == ENOENT) {
2557c478bd9Sstevel@tonic-gate 	    wrap_status = WR_NOT;
2567c478bd9Sstevel@tonic-gate 	} else {
2577c478bd9Sstevel@tonic-gate 	    tcpd_warn("%s: file lookup: %m", daemon);
2587c478bd9Sstevel@tonic-gate 	    wrap_status = WR_MAYBE;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate     }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate     /*
2637c478bd9Sstevel@tonic-gate      * Alas, we cannot wrap rpc/tcp services.
2647c478bd9Sstevel@tonic-gate      */
2657c478bd9Sstevel@tonic-gate     if (wrap_status == WR_YES && STR_EQ(protocol, "rpc/tcp"))
2667c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: cannot wrap rpc/tcp services", tcpd_proc_name);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate     inet_set(tcpd_proc_name, wrap_status);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* inet_set - remember service status */
2727c478bd9Sstevel@tonic-gate 
inet_set(name,type)2737c478bd9Sstevel@tonic-gate void    inet_set(name, type)
2747c478bd9Sstevel@tonic-gate char   *name;
2757c478bd9Sstevel@tonic-gate int     type;
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate     struct inet_ent *ip =
2787c478bd9Sstevel@tonic-gate     (struct inet_ent *) malloc(sizeof(struct inet_ent) + strlen(name));
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate     if (ip == 0) {
2817c478bd9Sstevel@tonic-gate 	fprintf(stderr, "out of memory\n");
2827c478bd9Sstevel@tonic-gate 	exit(1);
2837c478bd9Sstevel@tonic-gate     }
2847c478bd9Sstevel@tonic-gate     ip->next = inet_list;
2857c478bd9Sstevel@tonic-gate     strcpy(ip->name, name);
2867c478bd9Sstevel@tonic-gate     ip->type = type;
2877c478bd9Sstevel@tonic-gate     inet_list = ip;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /* inet_get - look up service status */
2917c478bd9Sstevel@tonic-gate 
inet_get(name)2927c478bd9Sstevel@tonic-gate int     inet_get(name)
2937c478bd9Sstevel@tonic-gate char   *name;
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate     struct inet_ent *ip;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate     if (inet_list == 0)
2987c478bd9Sstevel@tonic-gate 	return (WR_MAYBE);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate     for (ip = inet_list; ip; ip = ip->next)
3017c478bd9Sstevel@tonic-gate 	if (STR_EQ(ip->name, name))
3027c478bd9Sstevel@tonic-gate 	    return (ip->type);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate     return (-1);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /* base_name - compute last pathname component */
3087c478bd9Sstevel@tonic-gate 
base_name(path)3097c478bd9Sstevel@tonic-gate static char *base_name(path)
3107c478bd9Sstevel@tonic-gate char   *path;
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate     char   *cp;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate     if ((cp = strrchr(path, '/')) != 0)
3157c478bd9Sstevel@tonic-gate 	path = cp + 1;
3167c478bd9Sstevel@tonic-gate     return (path);
3177c478bd9Sstevel@tonic-gate }
318