yp_access.c revision 194968
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: head/usr.sbin/ypserv/yp_access.c 194968 2009-06-25 16:15:39Z brian $"); 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 10012891Swpaul/* 10114240Swpaul * Read /var/yp/securenets file and initialize the securenets 10214240Swpaul * list. If the file doesn't exist, we set up a dummy entry that 10314240Swpaul * allows all hosts to connect. 10414240Swpaul */ 10590298Sdesvoid 10690298Sdesload_securenets(void) 10714240Swpaul{ 10814240Swpaul FILE *fp; 10914240Swpaul char path[MAXPATHLEN + 2]; 11014240Swpaul char linebuf[1024 + 2]; 11114240Swpaul struct securenet *tmp; 11214240Swpaul 11314240Swpaul /* 11414240Swpaul * If securenets is not NULL, we are being called to reload 11514240Swpaul * the list; free the existing list before re-reading the 11614240Swpaul * securenets file. 11714240Swpaul */ 11890297Sdes while (securenets) { 11915426Swpaul tmp = securenets->next; 12015426Swpaul free(securenets); 12115426Swpaul securenets = tmp; 12214240Swpaul } 12314240Swpaul 12414240Swpaul snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir); 12514240Swpaul 12614240Swpaul if ((fp = fopen(path, "r")) == NULL) { 12714240Swpaul if (errno == ENOENT) { 12814240Swpaul securenets = (struct securenet *)malloc(sizeof(struct securenet)); 12914240Swpaul securenets->net.s_addr = INADDR_ANY; 13014302Sadam securenets->mask.s_addr = INADDR_ANY; 13114240Swpaul securenets->next = NULL; 13214240Swpaul return; 13314240Swpaul } else { 13414240Swpaul yp_error("fopen(%s) failed: %s", path, strerror(errno)); 13514240Swpaul exit(1); 13614240Swpaul } 13714240Swpaul } 13814240Swpaul 13914240Swpaul securenets = NULL; 14014240Swpaul 14190297Sdes while (fgets(linebuf, LINEBUFSZ, fp)) { 14214240Swpaul char addr1[20], addr2[20]; 14314240Swpaul 14437681Sdes if ((linebuf[0] == '#') 14537681Sdes || (strspn(linebuf, " \t\r\n") == strlen(linebuf))) 14614240Swpaul continue; 14714240Swpaul if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) { 14814240Swpaul yp_error("badly formatted securenets entry: %s", 14914240Swpaul linebuf); 15014240Swpaul continue; 15114240Swpaul } 15214240Swpaul 15314240Swpaul tmp = (struct securenet *)malloc(sizeof(struct securenet)); 15414240Swpaul 15514240Swpaul if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) { 15614240Swpaul yp_error("badly formatted securenets entry: %s", addr1); 15714240Swpaul free(tmp); 15814240Swpaul continue; 15914240Swpaul } 16014240Swpaul 16114240Swpaul if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) { 16214240Swpaul yp_error("badly formatted securenets entry: %s", addr2); 16314240Swpaul free(tmp); 16414240Swpaul continue; 16514240Swpaul } 16614240Swpaul 16714240Swpaul tmp->next = securenets; 16814240Swpaul securenets = tmp; 16914240Swpaul } 17014240Swpaul 17114240Swpaul fclose(fp); 17290297Sdes 17314240Swpaul} 17414240Swpaul 17514240Swpaul/* 17612891Swpaul * Access control functions. 17712891Swpaul * 17812891Swpaul * yp_access() checks the mapname and client host address and watches for 17912891Swpaul * the following things: 18012891Swpaul * 181194968Sbrian * - If the client is referencing one of the master.passwd.* or shadow.* maps, 182194968Sbrian * it must be using a privileged port to make its RPC to us. If it is, then 183194968Sbrian * we can assume that the caller is root and allow the RPC to succeed. If it 18412891Swpaul * isn't access is denied. 18512891Swpaul * 18614240Swpaul * - The client's IP address is checked against the securenets rules. 18714240Swpaul * There are two kinds of securenets support: the built-in support, 18815426Swpaul * which is very simple and depends on the presence of a 18914240Swpaul * /var/yp/securenets file, and tcp-wrapper support, which requires 19014240Swpaul * Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper 19114240Swpaul * package does not ship with FreeBSD, we use the built-in support 19215426Swpaul * by default. Users can recompile the server with the tcp-wrapper library 19314240Swpaul * if they already have it installed and want to use hosts.allow and 19472091Sasmodai * hosts.deny to control access instead of having a separate securenets 19514240Swpaul * file.) 19612891Swpaul * 19714240Swpaul * If no /var/yp/securenets file is present, the host access checks 19814240Swpaul * are bypassed and all hosts are allowed to connect. 19914240Swpaul * 20015426Swpaul * The yp_validdomain() function checks the domain specified by the caller 20112891Swpaul * to make sure it's actually served by this server. This is more a sanity 20212891Swpaul * check than an a security check, but this seems to be the best place for 20312891Swpaul * it. 20412891Swpaul */ 20512891Swpaul 20619161Swpaul#ifdef DB_CACHE 20790298Sdesint 20890298Sdesyp_access(const char *map, const char *domain, const struct svc_req *rqstp) 20919161Swpaul#else 21090298Sdesint 21190298Sdesyp_access(const char *map, const struct svc_req *rqstp) 21219161Swpaul#endif 21312891Swpaul{ 21412891Swpaul struct sockaddr_in *rqhost; 215159117Scperciva int status_securenets = 0; 216159117Scperciva#ifdef TCP_WRAPPER 217159117Scperciva int status_tcpwrap; 218159117Scperciva#endif 21916044Swpaul static unsigned long oldaddr = 0; 22014240Swpaul struct securenet *tmp; 22196221Sdes const char *yp_procedure = NULL; 22216118Swpaul char procbuf[50]; 22312891Swpaul 22416118Swpaul if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) { 22596221Sdes snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu", 22696221Sdes (unsigned long)rqstp->rq_prog, 22796221Sdes (unsigned long)rqstp->rq_proc); 22816118Swpaul yp_procedure = (char *)&procbuf; 22916118Swpaul } else { 23016118Swpaul yp_procedure = rqstp->rq_prog == YPPASSWDPROG ? 23116118Swpaul "yppasswdprog_update" : 23216118Swpaul yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))]; 23316118Swpaul } 23414262Swpaul 23512891Swpaul rqhost = svc_getcaller(rqstp->rq_xprt); 23612891Swpaul 23712891Swpaul if (debug) { 23830827Scharnier yp_error("procedure %s called from %s:%d", yp_procedure, 23914262Swpaul inet_ntoa(rqhost->sin_addr), 24012891Swpaul ntohs(rqhost->sin_port)); 24112891Swpaul if (map != NULL) 24230827Scharnier yp_error("client is referencing map \"%s\".", map); 24312891Swpaul } 24412891Swpaul 24512891Swpaul /* Check the map name if one was supplied. */ 24612891Swpaul if (map != NULL) { 24718586Swpaul if (strchr(map, '/')) { 24818586Swpaul yp_error("embedded slash in map name \"%s\" -- \ 24918586Swpaulpossible spoof attempt from %s:%d", 25018586Swpaul map, inet_ntoa(rqhost->sin_addr), 25118586Swpaul ntohs(rqhost->sin_port)); 25221366Swpaul return(1); 25318586Swpaul } 25419161Swpaul#ifdef DB_CACHE 25519161Swpaul if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) || 25619161Swpaul#else 257194968Sbrian if ((strstr(map, "master.passwd.") || strstr(map, "shadow.") || 25819161Swpaul#endif 25918586Swpaul (rqstp->rq_prog == YPPROG && 26018586Swpaul rqstp->rq_proc == YPPROC_XFR) || 26118586Swpaul (rqstp->rq_prog == YPXFRD_FREEBSD_PROG && 26218586Swpaul rqstp->rq_proc == YPXFRD_GETMAP)) && 26318586Swpaul ntohs(rqhost->sin_port) >= IPPORT_RESERVED) { 26430827Scharnier yp_error("access to %s denied -- client %s:%d \ 26518586Swpaulnot privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port)); 26612891Swpaul return(1); 26712891Swpaul } 26812891Swpaul } 26912891Swpaul 27012891Swpaul#ifdef TCP_WRAPPER 271159117Scperciva status_tcpwrap = hosts_ctl("ypserv", STRING_UNKNOWN, 27214240Swpaul inet_ntoa(rqhost->sin_addr), ""); 273159117Scperciva#endif 27414240Swpaul tmp = securenets; 27590297Sdes while (tmp) { 27614240Swpaul if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr) 27714240Swpaul | tmp->net.s_addr) == rqhost->sin_addr.s_addr) { 278159117Scperciva status_securenets = 1; 27914240Swpaul break; 28014240Swpaul } 28114240Swpaul tmp = tmp->next; 28214240Swpaul } 283159117Scperciva 284159117Scperciva#ifdef TCP_WRAPPER 285159117Scperciva if (status_securenets == 0 || status_tcpwrap == 0) { 286159117Scperciva#else 287159117Scperciva if (status_securenets == 0) { 28814240Swpaul#endif 289159117Scperciva /* 290159117Scperciva * One of the following two events occured: 291159117Scperciva * 292159117Scperciva * (1) The /var/yp/securenets exists and the remote host does not 293159117Scperciva * match any of the networks specified in it. 294159117Scperciva * (2) The hosts.allow file has denied access and TCP_WRAPPER is 295159117Scperciva * defined. 296159117Scperciva * 297159117Scperciva * In either case deny access. 298159117Scperciva */ 29914240Swpaul if (rqhost->sin_addr.s_addr != oldaddr) { 30014240Swpaul yp_error("connect from %s:%d to procedure %s refused", 30114240Swpaul inet_ntoa(rqhost->sin_addr), 30214240Swpaul ntohs(rqhost->sin_port), 30314262Swpaul yp_procedure); 30414240Swpaul oldaddr = rqhost->sin_addr.s_addr; 30514240Swpaul } 30612891Swpaul return(1); 30712891Swpaul } 30812891Swpaul return(0); 30912891Swpaul 31012891Swpaul} 31112891Swpaul 31290298Sdesint 31390298Sdesyp_validdomain(const char *domain) 31412891Swpaul{ 31512891Swpaul struct stat statbuf; 31612891Swpaul char dompath[MAXPATHLEN + 2]; 31712891Swpaul 31812891Swpaul if (domain == NULL || strstr(domain, "binding") || 31912891Swpaul !strcmp(domain, ".") || !strcmp(domain, "..") || 32014240Swpaul strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN) 32112891Swpaul return(1); 32212891Swpaul 32312891Swpaul snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain); 32412891Swpaul 32512891Swpaul if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode)) 32612891Swpaul return(1); 32712891Swpaul 32815426Swpaul 32912891Swpaul return(0); 33012891Swpaul} 331