yp_access.c revision 21366
112891Swpaul/*
212891Swpaul * Copyright (c) 1995
312891Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
412891Swpaul *
512891Swpaul * Redistribution and use in source and binary forms, with or without
612891Swpaul * modification, are permitted provided that the following conditions
712891Swpaul * are met:
812891Swpaul * 1. Redistributions of source code must retain the above copyright
912891Swpaul *    notice, this list of conditions and the following disclaimer.
1012891Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1112891Swpaul *    notice, this list of conditions and the following disclaimer in the
1212891Swpaul *    documentation and/or other materials provided with the distribution.
1312891Swpaul * 3. All advertising materials mentioning features or use of this software
1412891Swpaul *    must display the following acknowledgement:
1512891Swpaul *	This product includes software developed by Bill Paul.
1612891Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1712891Swpaul *    may be used to endorse or promote products derived from this software
1812891Swpaul *    without specific prior written permission.
1912891Swpaul *
2012891Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2112891Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2212891Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2312891Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2412891Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2512891Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2612891Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2712891Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2812891Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2912891Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3012891Swpaul * SUCH DAMAGE.
3112891Swpaul *
3212891Swpaul */
3312891Swpaul
3414248Swpaul#include <stdlib.h>
3512891Swpaul#include <rpc/rpc.h>
3614240Swpaul#include <rpcsvc/yp.h>
3714240Swpaul#include <rpcsvc/yppasswd.h>
3818586Swpaul#include <rpcsvc/ypxfrd.h>
3912891Swpaul#include <sys/types.h>
4015426Swpaul#include <limits.h>
4115426Swpaul#include <db.h>
4212891Swpaul#include <sys/socket.h>
4312891Swpaul#include <netinet/in.h>
4412891Swpaul#include <arpa/inet.h>
4512891Swpaul#include <sys/stat.h>
4615426Swpaul#include <sys/fcntl.h>
4712891Swpaul#include <paths.h>
4814240Swpaul#include <errno.h>
4912891Swpaul#include <sys/param.h>
5012891Swpaul#include "yp_extern.h"
5112891Swpaul#ifdef TCP_WRAPPER
5212891Swpaul#include "tcpd.h"
5312891Swpaul#endif
5412891Swpaul
5514240Swpaul#ifndef lint
5621366Swpaulstatic const char rcsid[] = "$Id: yp_access.c,v 1.11 1996/10/24 18:58:22 wpaul Exp $";
5714240Swpaul#endif
5814240Swpaul
5912891Swpaulextern int debug;
6012891Swpaul
6114262Swpaul			/* NIS v1 */
6214262Swpaulchar *yp_procs[] = {	"ypoldproc_null",
6314262Swpaul			"ypoldproc_domain",
6414262Swpaul			"ypoldproc_domain_nonack",
6514262Swpaul			"ypoldproc_match",
6614262Swpaul			"ypoldproc_first",
6714262Swpaul			"ypoldproc_next",
6814262Swpaul			"ypoldproc_poll",
6914262Swpaul			"ypoldproc_push",
7014262Swpaul			"ypoldproc_get",
7114262Swpaul			"badproc1", /* placeholder */
7214262Swpaul			"badproc2", /* placeholder */
7314262Swpaul			"badproc3", /* placeholder */
7414262Swpaul
7514262Swpaul			/* NIS v2 */
7614262Swpaul			"ypproc_null" ,
7712891Swpaul			"ypproc_domain",
7812891Swpaul			"ypproc_domain_nonack",
7912891Swpaul			"ypproc_match",
8012891Swpaul			"ypproc_first",
8112891Swpaul			"ypproc_next",
8212891Swpaul			"ypproc_xfr",
8312891Swpaul			"ypproc_clear",
8412891Swpaul			"ypproc_all",
8512891Swpaul			"ypproc_master",
8612891Swpaul			"ypproc_order",
8712891Swpaul			"ypproc_maplist"
8812891Swpaul		   };
8912891Swpaul
9014262Swpaul
9114240Swpaul#ifdef TCP_WRAPPER
9214240Swpaulvoid load_securenets()
9314240Swpaul{
9414240Swpaul}
9514240Swpaul#else
9614240Swpaulstruct securenet {
9714240Swpaul	struct in_addr net;
9814240Swpaul	struct in_addr mask;
9914240Swpaul	struct securenet *next;
10014240Swpaul};
10114240Swpaul
10214240Swpaulstruct securenet *securenets;
10314240Swpaul
10414240Swpaul#define LINEBUFSZ 1024
10514240Swpaul
10612891Swpaul/*
10714240Swpaul * Read /var/yp/securenets file and initialize the securenets
10814240Swpaul * list. If the file doesn't exist, we set up a dummy entry that
10914240Swpaul * allows all hosts to connect.
11014240Swpaul */
11114240Swpaulvoid load_securenets()
11214240Swpaul{
11314240Swpaul	FILE *fp;
11414240Swpaul	char path[MAXPATHLEN + 2];
11514240Swpaul	char linebuf[1024 + 2];
11614240Swpaul	struct securenet *tmp;
11714240Swpaul
11814240Swpaul	/*
11914240Swpaul	 * If securenets is not NULL, we are being called to reload
12014240Swpaul	 * the list; free the existing list before re-reading the
12114240Swpaul	 * securenets file.
12214240Swpaul	 */
12315426Swpaul	while(securenets) {
12415426Swpaul		tmp = securenets->next;
12515426Swpaul		free(securenets);
12615426Swpaul		securenets = tmp;
12714240Swpaul	}
12814240Swpaul
12914240Swpaul	snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
13014240Swpaul
13114240Swpaul	if ((fp = fopen(path, "r")) == NULL) {
13214240Swpaul		if (errno == ENOENT) {
13314240Swpaul			securenets = (struct securenet *)malloc(sizeof(struct securenet));
13414240Swpaul			securenets->net.s_addr = INADDR_ANY;
13514302Sadam			securenets->mask.s_addr = INADDR_ANY;
13614240Swpaul			securenets->next = NULL;
13714240Swpaul			return;
13814240Swpaul		} else {
13914240Swpaul			yp_error("fopen(%s) failed: %s", path, strerror(errno));
14014240Swpaul			exit(1);
14114240Swpaul		}
14214240Swpaul	}
14314240Swpaul
14414240Swpaul	securenets = NULL;
14514240Swpaul
14614240Swpaul	while(fgets(linebuf, LINEBUFSZ, fp)) {
14714240Swpaul		char addr1[20], addr2[20];
14814240Swpaul
14914240Swpaul		if (linebuf[0] == '#')
15014240Swpaul			continue;
15114240Swpaul		if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) {
15214240Swpaul			yp_error("badly formatted securenets entry: %s",
15314240Swpaul							linebuf);
15414240Swpaul			continue;
15514240Swpaul		}
15614240Swpaul
15714240Swpaul		tmp = (struct securenet *)malloc(sizeof(struct securenet));
15814240Swpaul
15914240Swpaul		if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) {
16014240Swpaul			yp_error("badly formatted securenets entry: %s", addr1);
16114240Swpaul			free(tmp);
16214240Swpaul			continue;
16314240Swpaul		}
16414240Swpaul
16514240Swpaul		if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) {
16614240Swpaul			yp_error("badly formatted securenets entry: %s", addr2);
16714240Swpaul			free(tmp);
16814240Swpaul			continue;
16914240Swpaul		}
17014240Swpaul
17114240Swpaul		tmp->next = securenets;
17214240Swpaul		securenets = tmp;
17314240Swpaul	}
17414240Swpaul
17514240Swpaul	fclose(fp);
17614240Swpaul
17714240Swpaul}
17814240Swpaul#endif
17914240Swpaul
18014240Swpaul/*
18112891Swpaul * Access control functions.
18212891Swpaul *
18312891Swpaul * yp_access() checks the mapname and client host address and watches for
18412891Swpaul * the following things:
18512891Swpaul *
18612891Swpaul * - If the client is referencing one of the master.passwd.* maps, it must
18712891Swpaul *   be using a privileged port to make its RPC to us. If it is, then we can
18812891Swpaul *   assume that the caller is root and allow the RPC to succeed. If it
18912891Swpaul *   isn't access is denied.
19012891Swpaul *
19114240Swpaul * - The client's IP address is checked against the securenets rules.
19214240Swpaul *   There are two kinds of securenets support: the built-in support,
19315426Swpaul *   which is very simple and depends on the presence of a
19414240Swpaul *   /var/yp/securenets file, and tcp-wrapper support, which requires
19514240Swpaul *   Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
19614240Swpaul *   package does not ship with FreeBSD, we use the built-in support
19715426Swpaul *   by default. Users can recompile the server with the tcp-wrapper library
19814240Swpaul *   if they already have it installed and want to use hosts.allow and
19915426Swpaul *   hosts.deny to control access instead of having a seperate securenets
20014240Swpaul *   file.)
20112891Swpaul *
20214240Swpaul *   If no /var/yp/securenets file is present, the host access checks
20314240Swpaul *   are bypassed and all hosts are allowed to connect.
20414240Swpaul *
20515426Swpaul * The yp_validdomain() function checks the domain specified by the caller
20612891Swpaul * to make sure it's actually served by this server. This is more a sanity
20712891Swpaul * check than an a security check, but this seems to be the best place for
20812891Swpaul * it.
20912891Swpaul */
21012891Swpaul
21119161Swpaul#ifdef DB_CACHE
21219161Swpaulint yp_access(map, domain, rqstp)
21319161Swpaul#else
21412891Swpaulint yp_access(map, rqstp)
21519161Swpaul#endif
21612891Swpaul	const char *map;
21719161Swpaul#ifdef DB_CACHE
21819161Swpaul	const char *domain;
21919161Swpaul#endif
22012891Swpaul	const struct svc_req *rqstp;
22112891Swpaul{
22212891Swpaul	struct sockaddr_in *rqhost;
22312891Swpaul	int status = 0;
22416044Swpaul	static unsigned long oldaddr = 0;
22514240Swpaul#ifndef TCP_WRAPPER
22614240Swpaul	struct securenet *tmp;
22712891Swpaul#endif
22814262Swpaul	char *yp_procedure = NULL;
22916118Swpaul	char procbuf[50];
23012891Swpaul
23116118Swpaul	if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
23216118Swpaul		snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu", rqstp->rq_prog,
23316118Swpaul								rqstp->rq_proc);
23416118Swpaul		yp_procedure = (char *)&procbuf;
23516118Swpaul	} else {
23616118Swpaul		yp_procedure = rqstp->rq_prog == YPPASSWDPROG ?
23716118Swpaul		"yppasswdprog_update" :
23816118Swpaul		yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))];
23916118Swpaul	}
24014262Swpaul
24112891Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
24212891Swpaul
24312891Swpaul	if (debug) {
24414262Swpaul		yp_error("Procedure %s called from %s:%d", yp_procedure,
24514262Swpaul			inet_ntoa(rqhost->sin_addr),
24612891Swpaul			ntohs(rqhost->sin_port));
24712891Swpaul		if (map != NULL)
24812891Swpaul			yp_error("Client is referencing map \"%s\".", map);
24912891Swpaul	}
25012891Swpaul
25112891Swpaul	/* Check the map name if one was supplied. */
25212891Swpaul	if (map != NULL) {
25318586Swpaul		if (strchr(map, '/')) {
25418586Swpaul			yp_error("embedded slash in map name \"%s\" -- \
25518586Swpaulpossible spoof attempt from %s:%d",
25618586Swpaul				map, inet_ntoa(rqhost->sin_addr),
25718586Swpaul				ntohs(rqhost->sin_port));
25821366Swpaul			return(1);
25918586Swpaul		}
26019161Swpaul#ifdef DB_CACHE
26119161Swpaul		if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) ||
26219161Swpaul#else
26314240Swpaul		if ((strstr(map, "master.passwd.") ||
26419161Swpaul#endif
26518586Swpaul		    (rqstp->rq_prog == YPPROG &&
26618586Swpaul		     rqstp->rq_proc == YPPROC_XFR) ||
26718586Swpaul		    (rqstp->rq_prog == YPXFRD_FREEBSD_PROG &&
26818586Swpaul		     rqstp->rq_proc == YPXFRD_GETMAP)) &&
26918586Swpaul		     ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
27018586Swpaul			yp_error("Access to %s denied -- client %s:%d \
27118586Swpaulnot privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port));
27212891Swpaul			return(1);
27312891Swpaul		}
27412891Swpaul	}
27512891Swpaul
27612891Swpaul#ifdef TCP_WRAPPER
27712891Swpaul	status = hosts_ctl(progname, STRING_UNKNOWN,
27814240Swpaul			   inet_ntoa(rqhost->sin_addr), "");
27914240Swpaul#else
28014240Swpaul	tmp = securenets;
28114240Swpaul	while(tmp) {
28214240Swpaul		if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr)
28314240Swpaul		    | tmp->net.s_addr) == rqhost->sin_addr.s_addr) {
28414240Swpaul			status = 1;
28514240Swpaul			break;
28614240Swpaul		}
28714240Swpaul		tmp = tmp->next;
28814240Swpaul	}
28914240Swpaul#endif
29012891Swpaul
29114240Swpaul	if (!status) {
29214240Swpaul		if (rqhost->sin_addr.s_addr != oldaddr) {
29314240Swpaul			yp_error("connect from %s:%d to procedure %s refused",
29414240Swpaul					inet_ntoa(rqhost->sin_addr),
29514240Swpaul					ntohs(rqhost->sin_port),
29614262Swpaul					yp_procedure);
29714240Swpaul			oldaddr = rqhost->sin_addr.s_addr;
29814240Swpaul		}
29912891Swpaul		return(1);
30012891Swpaul	}
30112891Swpaul	return(0);
30212891Swpaul
30312891Swpaul}
30412891Swpaul
30512891Swpaulint yp_validdomain(domain)
30612891Swpaul	const char *domain;
30712891Swpaul{
30812891Swpaul	struct stat statbuf;
30912891Swpaul	char dompath[MAXPATHLEN + 2];
31012891Swpaul
31112891Swpaul	if (domain == NULL || strstr(domain, "binding") ||
31212891Swpaul	    !strcmp(domain, ".") || !strcmp(domain, "..") ||
31314240Swpaul	    strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
31412891Swpaul		return(1);
31512891Swpaul
31612891Swpaul	snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
31712891Swpaul
31812891Swpaul	if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
31912891Swpaul		return(1);
32012891Swpaul
32115426Swpaul
32212891Swpaul	return(0);
32312891Swpaul}
324