1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Ryan Moeller <freqlabs@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/dnv.h> 32 #include <sys/nv.h> 33 #include <netinet/in.h> 34 35 #include <assert.h> 36 #include <errno.h> 37 #include <netdb.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 #include <libcasper.h> 43 #include <libcasper_service.h> 44 45 #include "cap_netdb.h" 46 47 static struct protoent * 48 protoent_unpack(nvlist_t *nvl) 49 { 50 struct protoent *pp; 51 char **aliases; 52 size_t n; 53 54 pp = malloc(sizeof(*pp)); 55 if (pp == NULL) { 56 nvlist_destroy(nvl); 57 return (NULL); 58 } 59 60 pp->p_name = nvlist_take_string(nvl, "name"); 61 62 aliases = nvlist_take_string_array(nvl, "aliases", &n); 63 pp->p_aliases = realloc(aliases, sizeof(char *) * (n + 1)); 64 if (pp->p_aliases == NULL) { 65 while (n-- > 0) 66 free(aliases[n]); 67 free(aliases); 68 free(pp->p_name); 69 free(pp); 70 nvlist_destroy(nvl); 71 return (NULL); 72 } 73 pp->p_aliases[n] = NULL; 74 75 pp->p_proto = (int)nvlist_take_number(nvl, "proto"); 76 77 nvlist_destroy(nvl); 78 return (pp); 79 } 80 81 struct protoent * 82 cap_getprotobyname(cap_channel_t *chan, const char *name) 83 { 84 nvlist_t *nvl; 85 86 nvl = nvlist_create(0); 87 nvlist_add_string(nvl, "cmd", "getprotobyname"); 88 nvlist_add_string(nvl, "name", name); 89 nvl = cap_xfer_nvlist(chan, nvl); 90 if (nvl == NULL) 91 return (NULL); 92 if (dnvlist_get_number(nvl, "error", 0) != 0) { 93 nvlist_destroy(nvl); 94 return (NULL); 95 } 96 return (protoent_unpack(nvl)); 97 } 98 99 static void 100 protoent_pack(const struct protoent *pp, nvlist_t *nvl) 101 { 102 int n = 0; 103 104 nvlist_add_string(nvl, "name", pp->p_name); 105 106 while (pp->p_aliases[n] != NULL) 107 ++n; 108 nvlist_add_string_array(nvl, "aliases", 109 (const char * const *)pp->p_aliases, n); 110 111 nvlist_add_number(nvl, "proto", (uint64_t)pp->p_proto); 112 } 113 114 static int 115 netdb_getprotobyname(const nvlist_t *limits __unused, const nvlist_t *nvlin, 116 nvlist_t *nvlout) 117 { 118 const char *name; 119 struct protoent *pp; 120 121 name = dnvlist_get_string(nvlin, "name", NULL); 122 if (name == NULL) 123 return (EDOOFUS); 124 125 pp = getprotobyname(name); 126 if (pp == NULL) 127 return (EINVAL); 128 129 protoent_pack(pp, nvlout); 130 return (0); 131 } 132 133 static int 134 netdb_limit(const nvlist_t *oldlimits __unused, 135 const nvlist_t *newlimits __unused) 136 { 137 138 return (0); 139 } 140 141 static int 142 netdb_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin, 143 nvlist_t *nvlout) 144 { 145 int error; 146 147 if (strcmp(cmd, "getprotobyname") == 0) 148 error = netdb_getprotobyname(limits, nvlin, nvlout); 149 else 150 error = NO_RECOVERY; 151 152 return (error); 153 } 154 155 CREATE_SERVICE("system.netdb", netdb_limit, netdb_command, 0); 156