1 1.3 joerg /* $NetBSD: get_net.c,v 1.3 2020/04/23 00:22:01 joerg Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 1989, 1993 5 1.1 christos * The Regents of the University of California. All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to Berkeley by 8 1.1 christos * Herb Hasler and Rick Macklem at The University of Guelph. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 3. Neither the name of the University nor the names of its contributors 19 1.1 christos * may be used to endorse or promote products derived from this software 20 1.1 christos * without specific prior written permission. 21 1.1 christos * 22 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 christos * SUCH DAMAGE. 33 1.1 christos */ 34 1.1 christos 35 1.1 christos #include <sys/cdefs.h> 36 1.3 joerg __RCSID("$NetBSD: get_net.c,v 1.3 2020/04/23 00:22:01 joerg Exp $"); 37 1.1 christos 38 1.1 christos #include <sys/types.h> 39 1.1 christos #include <netdb.h> 40 1.1 christos #include <stdio.h> 41 1.1 christos #include <limits.h> 42 1.1 christos #include <errno.h> 43 1.1 christos #include <string.h> 44 1.1 christos #include <stdlib.h> 45 1.1 christos #include <unistd.h> 46 1.1 christos #include <util.h> 47 1.1 christos #include <arpa/inet.h> 48 1.1 christos #include <netinet/in.h> 49 1.1 christos 50 1.1 christos #include "mountd.h" 51 1.1 christos 52 1.1 christos #ifdef TEST 53 1.1 christos int opt_flags; 54 1.1 christos const int ninumeric = NI_NUMERICHOST; 55 1.3 joerg int mountd_debug = 1; 56 1.1 christos #endif 57 1.1 christos 58 1.1 christos static int 59 1.1 christos isdottedquad(const char *cp) 60 1.1 christos { 61 1.1 christos for (;;) 62 1.1 christos switch (*cp++) { 63 1.1 christos case '\0': 64 1.1 christos return 1; 65 1.1 christos case '.': 66 1.1 christos case '0': case '1': case '2': case '3': case '4': 67 1.1 christos case '5': case '6': case '7': case '8': case '9': 68 1.1 christos continue; 69 1.1 christos default: 70 1.1 christos return 0; 71 1.1 christos } 72 1.1 christos } 73 1.1 christos 74 1.1 christos static int 75 1.1 christos countones(struct sockaddr *sa) 76 1.1 christos { 77 1.1 christos void *mask; 78 1.1 christos int i, bits = 0, bytelen; 79 1.1 christos u_int8_t *p; 80 1.1 christos 81 1.1 christos switch (sa->sa_family) { 82 1.1 christos case AF_INET: 83 1.1 christos mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr; 84 1.1 christos bytelen = 4; 85 1.1 christos break; 86 1.1 christos case AF_INET6: 87 1.1 christos mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr; 88 1.1 christos bytelen = 16; 89 1.1 christos break; 90 1.1 christos default: 91 1.1 christos return 0; 92 1.1 christos } 93 1.1 christos 94 1.1 christos p = mask; 95 1.1 christos 96 1.1 christos for (i = 0; i < bytelen; i++, p++) { 97 1.1 christos if (*p != 0xff) { 98 1.1 christos for (bits = 0; bits < 8; bits++) { 99 1.1 christos if (!(*p & (1 << (7 - bits)))) 100 1.1 christos break; 101 1.1 christos } 102 1.1 christos break; 103 1.1 christos } 104 1.1 christos } 105 1.1 christos 106 1.1 christos return (i * 8 + bits); 107 1.1 christos } 108 1.1 christos 109 1.1 christos /* 110 1.1 christos * Translate a net address. 111 1.1 christos */ 112 1.1 christos int 113 1.1 christos get_net(char *cp, struct netmsk *net, int maskflg) 114 1.1 christos { 115 1.1 christos struct netent *np; 116 1.1 christos char *nname, *p, *prefp; 117 1.1 christos struct sockaddr_in sin, *sinp; 118 1.1 christos struct sockaddr *sa; 119 1.1 christos struct addrinfo hints, *ai = NULL; 120 1.1 christos char netname[NI_MAXHOST]; 121 1.1 christos long preflen; 122 1.1 christos int ecode; 123 1.1 christos 124 1.1 christos (void)memset(&sin, 0, sizeof(sin)); 125 1.1 christos if ((opt_flags & OP_MASKLEN) && !maskflg) { 126 1.1 christos p = strchr(cp, '/'); 127 1.1 christos *p = '\0'; 128 1.1 christos prefp = p + 1; 129 1.1 christos } else { 130 1.1 christos p = NULL; /* XXXGCC -Wuninitialized */ 131 1.1 christos prefp = NULL; /* XXXGCC -Wuninitialized */ 132 1.1 christos } 133 1.1 christos 134 1.1 christos if ((np = getnetbyname(cp)) != NULL) { 135 1.1 christos sin.sin_family = AF_INET; 136 1.1 christos sin.sin_len = sizeof sin; 137 1.1 christos sin.sin_addr = inet_makeaddr(np->n_net, 0); 138 1.1 christos sa = (struct sockaddr *)&sin; 139 1.1 christos } else if (isdottedquad(cp)) { 140 1.1 christos /* 141 1.1 christos * Handle dotted quad [or less than quad] notation specially 142 1.1 christos * because getaddrinfo() will parse them in the old style: 143 1.1 christos * i.e. 1.2.3 -> 1.2.0.3 not 1.2.3.0 144 1.1 christos */ 145 1.1 christos sin.sin_family = AF_INET; 146 1.1 christos sin.sin_len = sizeof sin; 147 1.1 christos sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 148 1.3 joerg if (mountd_debug) 149 1.1 christos fprintf(stderr, "get_net: '%s' v4 addr %x\n", 150 1.1 christos cp, sin.sin_addr.s_addr); 151 1.1 christos sa = (struct sockaddr *)&sin; 152 1.1 christos } else { 153 1.1 christos memset(&hints, 0, sizeof hints); 154 1.1 christos hints.ai_family = AF_UNSPEC; 155 1.1 christos hints.ai_flags = AI_NUMERICHOST; 156 1.1 christos if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 157 1.1 christos sa = ai->ai_addr; 158 1.1 christos else 159 1.1 christos goto fail; 160 1.1 christos } 161 1.1 christos 162 1.1 christos /* 163 1.2 christos * Disallow v6 addresses without a specific mask or masklen 164 1.1 christos */ 165 1.1 christos if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg)) 166 1.1 christos return 1; 167 1.1 christos 168 1.1 christos ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname, 169 1.1 christos NULL, 0, ninumeric); 170 1.1 christos if (ecode != 0) 171 1.1 christos goto fail; 172 1.1 christos 173 1.1 christos if (maskflg) 174 1.1 christos net->nt_len = countones(sa); 175 1.1 christos else { 176 1.1 christos if (opt_flags & OP_MASKLEN) { 177 1.1 christos errno = 0; 178 1.1 christos preflen = strtol(prefp, NULL, 10); 179 1.1 christos if (preflen == LONG_MIN && errno == ERANGE) 180 1.1 christos goto fail; 181 1.1 christos net->nt_len = (int)preflen; 182 1.1 christos *p = '/'; 183 1.1 christos } 184 1.1 christos 185 1.1 christos if (np) 186 1.1 christos nname = np->n_name; 187 1.1 christos else { 188 1.1 christos if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 189 1.1 christos NULL, 0, ninumeric) != 0) 190 1.1 christos strlcpy(netname, "?", sizeof(netname)); 191 1.1 christos nname = netname; 192 1.1 christos } 193 1.1 christos net->nt_name = estrdup(nname); 194 1.1 christos memcpy(&net->nt_net, sa, sa->sa_len); 195 1.1 christos } 196 1.1 christos 197 1.1 christos if (!maskflg && sa->sa_family == AF_INET && 198 1.1 christos !(opt_flags & (OP_MASK|OP_MASKLEN))) { 199 1.1 christos sinp = (struct sockaddr_in *)sa; 200 1.1 christos if (IN_CLASSA(sinp->sin_addr.s_addr)) 201 1.1 christos net->nt_len = 8; 202 1.1 christos else if (IN_CLASSB(sinp->sin_addr.s_addr)) 203 1.1 christos net->nt_len = 16; 204 1.1 christos else if (IN_CLASSC(sinp->sin_addr.s_addr)) 205 1.1 christos net->nt_len = 24; 206 1.1 christos else if (IN_CLASSD(sinp->sin_addr.s_addr)) 207 1.1 christos net->nt_len = 28; 208 1.1 christos else 209 1.1 christos net->nt_len = 32; /* XXX */ 210 1.1 christos } 211 1.1 christos 212 1.1 christos if (ai) 213 1.1 christos freeaddrinfo(ai); 214 1.1 christos return 0; 215 1.1 christos 216 1.1 christos fail: 217 1.1 christos if (ai) 218 1.1 christos freeaddrinfo(ai); 219 1.1 christos return 1; 220 1.1 christos } 221 1.1 christos 222 1.1 christos #ifdef TEST 223 1.1 christos int 224 1.1 christos main(int argc, char *argv[]) 225 1.1 christos { 226 1.1 christos char buf[1024]; 227 1.1 christos struct netmsk nm; 228 1.1 christos 229 1.1 christos if (get_net(argv[1], &nm, 0)) 230 1.1 christos errx(EXIT_FAILURE, "get_net failed"); 231 1.1 christos 232 1.1 christos sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (void *)&nm.nt_net); 233 1.1 christos printf("%s %d %s\n", buf, nm.nt_len, nm.nt_name); 234 1.1 christos return 0; 235 1.1 christos } 236 1.1 christos #endif 237