1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1995 5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 #include <stdlib.h> 38 #include <rpc/rpc.h> 39 #include <rpcsvc/yp.h> 40 #include <rpcsvc/yppasswd.h> 41 #include <rpcsvc/ypxfrd.h> 42 #include <sys/types.h> 43 #include <limits.h> 44 #include <db.h> 45 #include <sys/socket.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 #include <sys/stat.h> 49 #include <sys/fcntl.h> 50 #include <paths.h> 51 #include <errno.h> 52 #include <sys/param.h> 53 #include "yp_extern.h" 54 #ifdef TCP_WRAPPER 55 #include "tcpd.h" 56 #endif 57 58 static const char *yp_procs[] = { 59 /* NIS v1 */ 60 "ypoldproc_null", 61 "ypoldproc_domain", 62 "ypoldproc_domain_nonack", 63 "ypoldproc_match", 64 "ypoldproc_first", 65 "ypoldproc_next", 66 "ypoldproc_poll", 67 "ypoldproc_push", 68 "ypoldproc_get", 69 "badproc1", /* placeholder */ 70 "badproc2", /* placeholder */ 71 "badproc3", /* placeholder */ 72 73 /* NIS v2 */ 74 "ypproc_null", 75 "ypproc_domain", 76 "ypproc_domain_nonack", 77 "ypproc_match", 78 "ypproc_first", 79 "ypproc_next", 80 "ypproc_xfr", 81 "ypproc_clear", 82 "ypproc_all", 83 "ypproc_master", 84 "ypproc_order", 85 "ypproc_maplist" 86 }; 87 88 struct securenet { 89 struct in_addr net; 90 struct in_addr mask; 91 struct securenet *next; 92 }; 93 94 static struct securenet *securenets; 95 96 #define LINEBUFSZ 1024 97 98 #ifdef TCP_WRAPPER 99 int hosts_ctl(char *, char *, char *, char *); 100 #endif 101 102 /* 103 * Read /var/yp/securenets file and initialize the securenets 104 * list. If the file doesn't exist, we set up a dummy entry that 105 * allows all hosts to connect. 106 */ 107 void 108 load_securenets(void) 109 { 110 FILE *fp; 111 char path[MAXPATHLEN + 2]; 112 char linebuf[1024 + 2]; 113 struct securenet *tmp; 114 115 /* 116 * If securenets is not NULL, we are being called to reload 117 * the list; free the existing list before re-reading the 118 * securenets file. 119 */ 120 while (securenets) { 121 tmp = securenets->next; 122 free(securenets); 123 securenets = tmp; 124 } 125 126 snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir); 127 128 if ((fp = fopen(path, "r")) == NULL) { 129 if (errno == ENOENT) { 130 securenets = malloc(sizeof(struct securenet)); 131 securenets->net.s_addr = INADDR_ANY; 132 securenets->mask.s_addr = INADDR_ANY; 133 securenets->next = NULL; 134 return; 135 } else { 136 yp_error("fopen(%s) failed: %s", path, strerror(errno)); 137 exit(1); 138 } 139 } 140 141 securenets = NULL; 142 143 while (fgets(linebuf, LINEBUFSZ, fp)) { 144 char addr1[20], addr2[20]; 145 146 if ((linebuf[0] == '#') 147 || (strspn(linebuf, " \t\r\n") == strlen(linebuf))) 148 continue; 149 if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) { 150 yp_error("badly formatted securenets entry: %s", 151 linebuf); 152 continue; 153 } 154 155 tmp = malloc(sizeof(struct securenet)); 156 157 if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) { 158 yp_error("badly formatted securenets entry: %s", addr1); 159 free(tmp); 160 continue; 161 } 162 163 if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) { 164 yp_error("badly formatted securenets entry: %s", addr2); 165 free(tmp); 166 continue; 167 } 168 169 tmp->next = securenets; 170 securenets = tmp; 171 } 172 173 fclose(fp); 174 175 } 176 177 /* 178 * Access control functions. 179 * 180 * yp_access() checks the mapname and client host address and watches for 181 * the following things: 182 * 183 * - If the client is referencing one of the master.passwd.* or shadow.* maps, 184 * it must be using a privileged port to make its RPC to us. If it is, then 185 * we can assume that the caller is root and allow the RPC to succeed. If it 186 * isn't access is denied. 187 * 188 * - The client's IP address is checked against the securenets rules. 189 * There are two kinds of securenets support: the built-in support, 190 * which is very simple and depends on the presence of a 191 * /var/yp/securenets file, and tcp-wrapper support, which requires 192 * Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper 193 * package does not ship with FreeBSD, we use the built-in support 194 * by default. Users can recompile the server with the tcp-wrapper library 195 * if they already have it installed and want to use hosts.allow and 196 * hosts.deny to control access instead of having a separate securenets 197 * file.) 198 * 199 * If no /var/yp/securenets file is present, the host access checks 200 * are bypassed and all hosts are allowed to connect. 201 * 202 * The yp_validdomain() function checks the domain specified by the caller 203 * to make sure it's actually served by this server. This is more a sanity 204 * check than an a security check, but this seems to be the best place for 205 * it. 206 */ 207 208 #ifdef DB_CACHE 209 int 210 yp_access(const char *map, const char *domain, const struct svc_req *rqstp) 211 #else 212 int 213 yp_access(const char *map, const struct svc_req *rqstp) 214 #endif 215 { 216 struct sockaddr_in *rqhost; 217 int status_securenets = 0; 218 #ifdef TCP_WRAPPER 219 int status_tcpwrap; 220 #endif 221 static unsigned long oldaddr = 0; 222 struct securenet *tmp; 223 const char *yp_procedure = NULL; 224 char procbuf[50]; 225 226 if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) { 227 snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu", 228 (unsigned long)rqstp->rq_prog, 229 (unsigned long)rqstp->rq_proc); 230 yp_procedure = (char *)&procbuf; 231 } else { 232 yp_procedure = rqstp->rq_prog == YPPASSWDPROG ? 233 "yppasswdprog_update" : 234 yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))]; 235 } 236 237 rqhost = svc_getcaller(rqstp->rq_xprt); 238 239 if (debug) { 240 yp_error("procedure %s called from %s:%d", yp_procedure, 241 inet_ntoa(rqhost->sin_addr), 242 ntohs(rqhost->sin_port)); 243 if (map != NULL) 244 yp_error("client is referencing map \"%s\".", map); 245 } 246 247 /* Check the map name if one was supplied. */ 248 if (map != NULL) { 249 if (strchr(map, '/')) { 250 yp_error("embedded slash in map name \"%s\" -- \ 251 possible spoof attempt from %s:%d", 252 map, inet_ntoa(rqhost->sin_addr), 253 ntohs(rqhost->sin_port)); 254 return(1); 255 } 256 #ifdef DB_CACHE 257 if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) || 258 #else 259 if ((strstr(map, "master.passwd.") || strstr(map, "shadow.") || 260 #endif 261 (rqstp->rq_prog == YPPROG && 262 rqstp->rq_proc == YPPROC_XFR) || 263 (rqstp->rq_prog == YPXFRD_FREEBSD_PROG && 264 rqstp->rq_proc == YPXFRD_GETMAP)) && 265 ntohs(rqhost->sin_port) >= IPPORT_RESERVED) { 266 yp_error("access to %s denied -- client %s:%d \ 267 not privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port)); 268 return(1); 269 } 270 } 271 272 #ifdef TCP_WRAPPER 273 status_tcpwrap = hosts_ctl("ypserv", STRING_UNKNOWN, 274 inet_ntoa(rqhost->sin_addr), ""); 275 #endif 276 tmp = securenets; 277 while (tmp) { 278 if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr) 279 | tmp->net.s_addr) == rqhost->sin_addr.s_addr) { 280 status_securenets = 1; 281 break; 282 } 283 tmp = tmp->next; 284 } 285 286 #ifdef TCP_WRAPPER 287 if (status_securenets == 0 || status_tcpwrap == 0) { 288 #else 289 if (status_securenets == 0) { 290 #endif 291 /* 292 * One of the following two events occurred: 293 * 294 * (1) The /var/yp/securenets exists and the remote host does not 295 * match any of the networks specified in it. 296 * (2) The hosts.allow file has denied access and TCP_WRAPPER is 297 * defined. 298 * 299 * In either case deny access. 300 */ 301 if (rqhost->sin_addr.s_addr != oldaddr) { 302 yp_error("connect from %s:%d to procedure %s refused", 303 inet_ntoa(rqhost->sin_addr), 304 ntohs(rqhost->sin_port), 305 yp_procedure); 306 oldaddr = rqhost->sin_addr.s_addr; 307 } 308 return(1); 309 } 310 return(0); 311 312 } 313 314 int 315 yp_validdomain(const char *domain) 316 { 317 struct stat statbuf; 318 char dompath[MAXPATHLEN + 2]; 319 320 if (domain == NULL || strstr(domain, "binding") || 321 !strcmp(domain, ".") || !strcmp(domain, "..") || 322 strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN) 323 return(1); 324 325 snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain); 326 327 if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode)) 328 return(1); 329 330 331 return(0); 332 } 333