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
34114601Sobrien#include <sys/cdefs.h>
35114601Sobrien__FBSDID("$FreeBSD$");
3630827Scharnier
3714248Swpaul#include <stdlib.h>
3812891Swpaul#include <rpc/rpc.h>
3914240Swpaul#include <rpcsvc/yp.h>
4014240Swpaul#include <rpcsvc/yppasswd.h>
4118586Swpaul#include <rpcsvc/ypxfrd.h>
4212891Swpaul#include <sys/types.h>
4315426Swpaul#include <limits.h>
4415426Swpaul#include <db.h>
4512891Swpaul#include <sys/socket.h>
4612891Swpaul#include <netinet/in.h>
4712891Swpaul#include <arpa/inet.h>
4812891Swpaul#include <sys/stat.h>
4915426Swpaul#include <sys/fcntl.h>
5012891Swpaul#include <paths.h>
5114240Swpaul#include <errno.h>
5212891Swpaul#include <sys/param.h>
5312891Swpaul#include "yp_extern.h"
5412891Swpaul#ifdef TCP_WRAPPER
5512891Swpaul#include "tcpd.h"
5612891Swpaul#endif
5712891Swpaul
5812891Swpaulextern int debug;
5912891Swpaul
6014262Swpaul			/* NIS v1 */
6196221Sdesconst char *yp_procs[] = {
6296221Sdes	"ypoldproc_null",
6396221Sdes	"ypoldproc_domain",
6496221Sdes	"ypoldproc_domain_nonack",
6596221Sdes	"ypoldproc_match",
6696221Sdes	"ypoldproc_first",
6796221Sdes	"ypoldproc_next",
6896221Sdes	"ypoldproc_poll",
6996221Sdes	"ypoldproc_push",
7096221Sdes	"ypoldproc_get",
7196221Sdes	"badproc1", /* placeholder */
7296221Sdes	"badproc2", /* placeholder */
7396221Sdes	"badproc3", /* placeholder */
7496221Sdes
7596221Sdes	/* NIS v2 */
7696221Sdes	"ypproc_null",
7796221Sdes	"ypproc_domain",
7896221Sdes	"ypproc_domain_nonack",
7996221Sdes	"ypproc_match",
8096221Sdes	"ypproc_first",
8196221Sdes	"ypproc_next",
8296221Sdes	"ypproc_xfr",
8396221Sdes	"ypproc_clear",
8496221Sdes	"ypproc_all",
8596221Sdes	"ypproc_master",
8696221Sdes	"ypproc_order",
8796221Sdes	"ypproc_maplist"
8896221Sdes};
8914262Swpaul
9014240Swpaulstruct securenet {
9114240Swpaul	struct in_addr net;
9214240Swpaul	struct in_addr mask;
9314240Swpaul	struct securenet *next;
9414240Swpaul};
9514240Swpaul
9614240Swpaulstruct securenet *securenets;
9714240Swpaul
9814240Swpaul#define LINEBUFSZ 1024
9914240Swpaul
100253350Shrs#ifdef TCP_WRAPPER
101253350Shrsint hosts_ctl(char *, char *, char *, char *);
102253350Shrs#endif
103253350Shrs
10412891Swpaul/*
10514240Swpaul * Read /var/yp/securenets file and initialize the securenets
10614240Swpaul * list. If the file doesn't exist, we set up a dummy entry that
10714240Swpaul * allows all hosts to connect.
10814240Swpaul */
10990298Sdesvoid
11090298Sdesload_securenets(void)
11114240Swpaul{
11214240Swpaul	FILE *fp;
11314240Swpaul	char path[MAXPATHLEN + 2];
11414240Swpaul	char linebuf[1024 + 2];
11514240Swpaul	struct securenet *tmp;
11614240Swpaul
11714240Swpaul	/*
11814240Swpaul	 * If securenets is not NULL, we are being called to reload
11914240Swpaul	 * the list; free the existing list before re-reading the
12014240Swpaul	 * securenets file.
12114240Swpaul	 */
12290297Sdes	while (securenets) {
12315426Swpaul		tmp = securenets->next;
12415426Swpaul		free(securenets);
12515426Swpaul		securenets = tmp;
12614240Swpaul	}
12714240Swpaul
12814240Swpaul	snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
12914240Swpaul
13014240Swpaul	if ((fp = fopen(path, "r")) == NULL) {
13114240Swpaul		if (errno == ENOENT) {
13214240Swpaul			securenets = (struct securenet *)malloc(sizeof(struct securenet));
13314240Swpaul			securenets->net.s_addr = INADDR_ANY;
13414302Sadam			securenets->mask.s_addr = INADDR_ANY;
13514240Swpaul			securenets->next = NULL;
13614240Swpaul			return;
13714240Swpaul		} else {
13814240Swpaul			yp_error("fopen(%s) failed: %s", path, strerror(errno));
13914240Swpaul			exit(1);
14014240Swpaul		}
14114240Swpaul	}
14214240Swpaul
14314240Swpaul	securenets = NULL;
14414240Swpaul
14590297Sdes	while (fgets(linebuf, LINEBUFSZ, fp)) {
14614240Swpaul		char addr1[20], addr2[20];
14714240Swpaul
14837681Sdes		if ((linebuf[0] == '#')
14937681Sdes		    || (strspn(linebuf, " \t\r\n") == strlen(linebuf)))
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);
17690297Sdes
17714240Swpaul}
17814240Swpaul
17914240Swpaul/*
18012891Swpaul * Access control functions.
18112891Swpaul *
18212891Swpaul * yp_access() checks the mapname and client host address and watches for
18312891Swpaul * the following things:
18412891Swpaul *
185194968Sbrian * - If the client is referencing one of the master.passwd.* or shadow.* maps,
186194968Sbrian *   it must be using a privileged port to make its RPC to us. If it is, then
187194968Sbrian *   we can assume that the caller is root and allow the RPC to succeed. If it
18812891Swpaul *   isn't access is denied.
18912891Swpaul *
19014240Swpaul * - The client's IP address is checked against the securenets rules.
19114240Swpaul *   There are two kinds of securenets support: the built-in support,
19215426Swpaul *   which is very simple and depends on the presence of a
19314240Swpaul *   /var/yp/securenets file, and tcp-wrapper support, which requires
19414240Swpaul *   Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
19514240Swpaul *   package does not ship with FreeBSD, we use the built-in support
19615426Swpaul *   by default. Users can recompile the server with the tcp-wrapper library
19714240Swpaul *   if they already have it installed and want to use hosts.allow and
19872091Sasmodai *   hosts.deny to control access instead of having a separate securenets
19914240Swpaul *   file.)
20012891Swpaul *
20114240Swpaul *   If no /var/yp/securenets file is present, the host access checks
20214240Swpaul *   are bypassed and all hosts are allowed to connect.
20314240Swpaul *
20415426Swpaul * The yp_validdomain() function checks the domain specified by the caller
20512891Swpaul * to make sure it's actually served by this server. This is more a sanity
20612891Swpaul * check than an a security check, but this seems to be the best place for
20712891Swpaul * it.
20812891Swpaul */
20912891Swpaul
21019161Swpaul#ifdef DB_CACHE
21190298Sdesint
21290298Sdesyp_access(const char *map, const char *domain, const struct svc_req *rqstp)
21319161Swpaul#else
21490298Sdesint
21590298Sdesyp_access(const char *map, const struct svc_req *rqstp)
21619161Swpaul#endif
21712891Swpaul{
21812891Swpaul	struct sockaddr_in *rqhost;
219159117Scperciva	int status_securenets = 0;
220159117Scperciva#ifdef TCP_WRAPPER
221159117Scperciva	int status_tcpwrap;
222159117Scperciva#endif
22316044Swpaul	static unsigned long oldaddr = 0;
22414240Swpaul	struct securenet *tmp;
22596221Sdes	const char *yp_procedure = NULL;
22616118Swpaul	char procbuf[50];
22712891Swpaul
22816118Swpaul	if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
22996221Sdes		snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu",
23096221Sdes		    (unsigned long)rqstp->rq_prog,
23196221Sdes		    (unsigned long)rqstp->rq_proc);
23216118Swpaul		yp_procedure = (char *)&procbuf;
23316118Swpaul	} else {
23416118Swpaul		yp_procedure = rqstp->rq_prog == YPPASSWDPROG ?
23516118Swpaul		"yppasswdprog_update" :
23616118Swpaul		yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))];
23716118Swpaul	}
23814262Swpaul
23912891Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
24012891Swpaul
24112891Swpaul	if (debug) {
24230827Scharnier		yp_error("procedure %s called from %s:%d", yp_procedure,
24314262Swpaul			inet_ntoa(rqhost->sin_addr),
24412891Swpaul			ntohs(rqhost->sin_port));
24512891Swpaul		if (map != NULL)
24630827Scharnier			yp_error("client is referencing map \"%s\".", map);
24712891Swpaul	}
24812891Swpaul
24912891Swpaul	/* Check the map name if one was supplied. */
25012891Swpaul	if (map != NULL) {
25118586Swpaul		if (strchr(map, '/')) {
25218586Swpaul			yp_error("embedded slash in map name \"%s\" -- \
25318586Swpaulpossible spoof attempt from %s:%d",
25418586Swpaul				map, inet_ntoa(rqhost->sin_addr),
25518586Swpaul				ntohs(rqhost->sin_port));
25621366Swpaul			return(1);
25718586Swpaul		}
25819161Swpaul#ifdef DB_CACHE
25919161Swpaul		if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) ||
26019161Swpaul#else
261194968Sbrian		if ((strstr(map, "master.passwd.") || strstr(map, "shadow.") ||
26219161Swpaul#endif
26318586Swpaul		    (rqstp->rq_prog == YPPROG &&
26418586Swpaul		     rqstp->rq_proc == YPPROC_XFR) ||
26518586Swpaul		    (rqstp->rq_prog == YPXFRD_FREEBSD_PROG &&
26618586Swpaul		     rqstp->rq_proc == YPXFRD_GETMAP)) &&
26718586Swpaul		     ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
26830827Scharnier			yp_error("access to %s denied -- client %s:%d \
26918586Swpaulnot privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port));
27012891Swpaul			return(1);
27112891Swpaul		}
27212891Swpaul	}
27312891Swpaul
27412891Swpaul#ifdef TCP_WRAPPER
275159117Scperciva	status_tcpwrap = hosts_ctl("ypserv", STRING_UNKNOWN,
27614240Swpaul			   inet_ntoa(rqhost->sin_addr), "");
277159117Scperciva#endif
27814240Swpaul	tmp = securenets;
27990297Sdes	while (tmp) {
28014240Swpaul		if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr)
28114240Swpaul		    | tmp->net.s_addr) == rqhost->sin_addr.s_addr) {
282159117Scperciva			status_securenets = 1;
28314240Swpaul			break;
28414240Swpaul		}
28514240Swpaul		tmp = tmp->next;
28614240Swpaul	}
287159117Scperciva
288159117Scperciva#ifdef TCP_WRAPPER
289159117Scperciva	if (status_securenets == 0 || status_tcpwrap == 0) {
290159117Scperciva#else
291159117Scperciva	if (status_securenets == 0) {
29214240Swpaul#endif
293159117Scperciva	/*
294228990Suqs	 * One of the following two events occurred:
295159117Scperciva	 *
296159117Scperciva	 * (1) The /var/yp/securenets exists and the remote host does not
297159117Scperciva	 *     match any of the networks specified in it.
298159117Scperciva	 * (2) The hosts.allow file has denied access and TCP_WRAPPER is
299159117Scperciva	 *     defined.
300159117Scperciva	 *
301159117Scperciva	 * In either case deny access.
302159117Scperciva	 */
30314240Swpaul		if (rqhost->sin_addr.s_addr != oldaddr) {
30414240Swpaul			yp_error("connect from %s:%d to procedure %s refused",
30514240Swpaul					inet_ntoa(rqhost->sin_addr),
30614240Swpaul					ntohs(rqhost->sin_port),
30714262Swpaul					yp_procedure);
30814240Swpaul			oldaddr = rqhost->sin_addr.s_addr;
30914240Swpaul		}
31012891Swpaul		return(1);
31112891Swpaul	}
31212891Swpaul	return(0);
31312891Swpaul
31412891Swpaul}
31512891Swpaul
31690298Sdesint
31790298Sdesyp_validdomain(const char *domain)
31812891Swpaul{
31912891Swpaul	struct stat statbuf;
32012891Swpaul	char dompath[MAXPATHLEN + 2];
32112891Swpaul
32212891Swpaul	if (domain == NULL || strstr(domain, "binding") ||
32312891Swpaul	    !strcmp(domain, ".") || !strcmp(domain, "..") ||
32414240Swpaul	    strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
32512891Swpaul		return(1);
32612891Swpaul
32712891Swpaul	snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
32812891Swpaul
32912891Swpaul	if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
33012891Swpaul		return(1);
33112891Swpaul
33215426Swpaul
33312891Swpaul	return(0);
33412891Swpaul}
335