1 /* $NetBSD: getservent_r.c,v 1.14 2025/12/16 12:39:02 nia Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93"; 36 #else 37 __RCSID("$NetBSD: getservent_r.c,v 1.14 2025/12/16 12:39:02 nia Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include "namespace.h" 42 #include <sys/endian.h> 43 #include <cdbr.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <netdb.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "servent.h" 52 53 #ifdef __weak_alias 54 __weak_alias(endservent_r,_endservent_r) 55 __weak_alias(getservent_r,_getservent_r) 56 __weak_alias(setservent_r,_setservent_r) 57 #endif 58 59 int 60 _servent_open(struct servent_data *sd) 61 { 62 if (sd->flags & (_SV_CDB | _SV_PLAINFILE)) { 63 sd->flags |= _SV_FIRST; 64 return 0; 65 } 66 67 free(sd->line); 68 sd->line = NULL; 69 free(sd->cdb_buf); 70 sd->cdb_buf = NULL; 71 sd->cdb_buf_len = 0; 72 free(sd->aliases); 73 sd->aliases = NULL; 74 sd->maxaliases = 0; 75 sd->flags |= _SV_FIRST; 76 77 sd->cdb = cdbr_open(_PATH_SERVICES_CDB, CDBR_DEFAULT); 78 if (sd->cdb != NULL) { 79 sd->flags |= _SV_CDB; 80 return 0; 81 } 82 83 sd->plainfile = fopen(_PATH_SERVICES, "re"); 84 if (sd->plainfile != NULL) { 85 sd->flags |= _SV_PLAINFILE; 86 return 0; 87 } 88 return -1; 89 } 90 91 void 92 _servent_close(struct servent_data *sd) 93 { 94 if (sd->flags & _SV_CDB) { 95 cdbr_close(sd->cdb); 96 sd->cdb = NULL; 97 sd->flags &= ~_SV_CDB; 98 } 99 100 if (sd->flags & _SV_PLAINFILE) { 101 (void)fclose(sd->plainfile); 102 sd->plainfile = NULL; 103 sd->flags &= ~_SV_PLAINFILE; 104 } 105 sd->flags &= ~_SV_STAYOPEN; 106 } 107 108 109 int 110 _servent_getline(struct servent_data *sd) 111 { 112 113 if (sd->flags & _SV_CDB) 114 return -1; 115 116 if ((sd->flags & _SV_PLAINFILE) == 0) 117 return -1; 118 119 free(sd->line); 120 sd->line = NULL; 121 122 if (sd->flags & _SV_FIRST) { 123 (void)rewind((FILE *)sd->plainfile); 124 sd->flags &= ~_SV_FIRST; 125 } 126 sd->line = fparseln(sd->plainfile, NULL, NULL, NULL, 127 FPARSELN_UNESCALL); 128 return sd->line == NULL ? -1 : 0; 129 } 130 131 struct servent * 132 _servent_parseline(struct servent_data *sd, struct servent *sp) 133 { 134 size_t i = 0; 135 int oerrno; 136 char *p, *cp, **q; 137 138 if (sd->line == NULL) 139 return NULL; 140 141 sp->s_name = p = sd->line; 142 p = strpbrk(p, " \t"); 143 if (p == NULL) 144 return NULL; 145 *p++ = '\0'; 146 while (*p == ' ' || *p == '\t') 147 p++; 148 cp = strpbrk(p, ",/"); 149 if (cp == NULL) 150 return NULL; 151 *cp++ = '\0'; 152 sp->s_port = htons((u_short)atoi(p)); 153 sp->s_proto = cp; 154 if (sd->aliases == NULL) { 155 sd->maxaliases = 10; 156 sd->aliases = calloc(sd->maxaliases, sizeof(*sd->aliases)); 157 if (sd->aliases == NULL) { 158 oerrno = errno; 159 endservent_r(sd); 160 errno = oerrno; 161 return NULL; 162 } 163 } 164 sp->s_aliases = sd->aliases; 165 cp = strpbrk(cp, " \t"); 166 if (cp != NULL) 167 *cp++ = '\0'; 168 while (cp && *cp) { 169 if (*cp == ' ' || *cp == '\t') { 170 cp++; 171 continue; 172 } 173 if (i == sd->maxaliases - 2) { 174 sd->maxaliases *= 2; 175 q = realloc(sd->aliases, sd->maxaliases * sizeof(*q)); 176 if (q == NULL) { 177 oerrno = errno; 178 endservent_r(sd); 179 errno = oerrno; 180 return NULL; 181 } 182 sp->s_aliases = sd->aliases = q; 183 } 184 sp->s_aliases[i++] = cp; 185 cp = strpbrk(cp, " \t"); 186 if (cp != NULL) 187 *cp++ = '\0'; 188 } 189 sp->s_aliases[i] = NULL; 190 return sp; 191 } 192 193 void 194 setservent_r(int f, struct servent_data *sd) 195 { 196 (void)_servent_open(sd); 197 sd->flags |= f ? _SV_STAYOPEN : 0; 198 } 199 200 void 201 endservent_r(struct servent_data *sd) 202 { 203 _servent_close(sd); 204 free(sd->aliases); 205 sd->aliases = NULL; 206 sd->maxaliases = 0; 207 free(sd->line); 208 sd->line = NULL; 209 free(sd->cdb_buf); 210 sd->cdb_buf = NULL; 211 sd->cdb_buf_len = 0; 212 } 213 214 struct servent * 215 getservent_r(struct servent *sp, struct servent_data *sd) 216 { 217 218 if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0 && 219 _servent_open(sd) == -1) 220 return NULL; 221 222 if (sd->flags & _SV_CDB) { 223 const void *data; 224 size_t len; 225 226 if (sd->flags & _SV_FIRST) { 227 sd->cdb_index = 0; 228 sd->flags &= ~_SV_FIRST; 229 } 230 231 if (cdbr_get(sd->cdb, sd->cdb_index, &data, &len)) 232 return NULL; 233 ++sd->cdb_index; 234 return _servent_parsedb(sd, sp, data, len); 235 } 236 if (sd->flags & _SV_PLAINFILE) { 237 for (;;) { 238 if (_servent_getline(sd) == -1) 239 return NULL; 240 if (_servent_parseline(sd, sp) == NULL) 241 continue; 242 return sp; 243 } 244 } 245 return NULL; 246 } 247 248 struct servent * 249 _servent_parsedb(struct servent_data *sd, struct servent *sp, 250 const uint8_t *data, size_t len) 251 { 252 char **q; 253 size_t i; 254 int oerrno; 255 256 if ((sd->flags & _SV_STAYOPEN) == 0) { 257 if (len > sd->cdb_buf_len) { 258 void *tmp = realloc(sd->cdb_buf, len); 259 if (tmp == NULL) 260 goto fail; 261 sd->cdb_buf = tmp; 262 sd->cdb_buf_len = len; 263 } 264 memcpy(sd->cdb_buf, data, len); 265 data = sd->cdb_buf; 266 } 267 268 if (len < 2) 269 goto fail; 270 sp->s_port = htobe16(be16dec(data)); 271 data += 2; 272 len -= 2; 273 274 if (len == 0 || len < (size_t)data[0] + 2) 275 goto fail; 276 sp->s_proto = __UNCONST(data + 1); 277 278 if (sp->s_proto[data[0]] != '\0') 279 goto fail; 280 281 len -= 2 + data[0]; 282 data += 2 + data[0]; 283 284 if (len == 0) 285 goto fail; 286 if (len < (size_t)data[0] + 2) 287 goto fail; 288 289 sp->s_name = __UNCONST(data + 1); 290 len -= 2 + data[0]; 291 data += 2 + data[0]; 292 293 if (sd->aliases == NULL) { 294 sd->maxaliases = 10; 295 sd->aliases = NULL; 296 errno = reallocarr(&sd->aliases, 297 sd->maxaliases, sizeof(*sd->aliases)); 298 if (errno) 299 goto fail; 300 } 301 sp->s_aliases = sd->aliases; 302 i = 0; 303 while (len) { 304 if (len < (size_t)data[0] + 2) 305 goto fail; 306 if (i == sd->maxaliases - 2) { 307 sd->maxaliases *= 2; 308 q = sd->aliases; 309 errno = reallocarr(&q, sd->maxaliases, sizeof(*q)); 310 if (errno) 311 goto fail; 312 sp->s_aliases = sd->aliases = q; 313 } 314 sp->s_aliases[i++] = __UNCONST(data + 1); 315 len -= 2 + data[0]; 316 data += 2 + data[0]; 317 } 318 sp->s_aliases[i] = NULL; 319 return sp; 320 321 fail: 322 oerrno = errno; 323 endservent_r(sd); 324 errno = oerrno; 325 return NULL; 326 } 327 328