Path: blob/main/lib/libcasper/services/cap_netdb/cap_netdb.c
48254 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020 Ryan Moeller <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/cdefs.h>28#include <sys/dnv.h>29#include <sys/nv.h>30#include <netinet/in.h>3132#include <assert.h>33#include <errno.h>34#include <netdb.h>35#include <stdlib.h>36#include <string.h>37#include <unistd.h>3839#include <libcasper.h>40#include <libcasper_service.h>4142#include "cap_netdb.h"4344static struct protoent *45protoent_unpack(nvlist_t *nvl)46{47struct protoent *pp;48char **aliases;49size_t n;5051pp = malloc(sizeof(*pp));52if (pp == NULL) {53nvlist_destroy(nvl);54return (NULL);55}5657pp->p_name = nvlist_take_string(nvl, "name");5859aliases = nvlist_take_string_array(nvl, "aliases", &n);60pp->p_aliases = realloc(aliases, sizeof(char *) * (n + 1));61if (pp->p_aliases == NULL) {62while (n-- > 0)63free(aliases[n]);64free(aliases);65free(pp->p_name);66free(pp);67nvlist_destroy(nvl);68return (NULL);69}70pp->p_aliases[n] = NULL;7172pp->p_proto = (int)nvlist_take_number(nvl, "proto");7374nvlist_destroy(nvl);75return (pp);76}7778struct protoent *79cap_getprotobyname(cap_channel_t *chan, const char *name)80{81nvlist_t *nvl;8283nvl = nvlist_create(0);84nvlist_add_string(nvl, "cmd", "getprotobyname");85nvlist_add_string(nvl, "name", name);86nvl = cap_xfer_nvlist(chan, nvl);87if (nvl == NULL)88return (NULL);89if (dnvlist_get_number(nvl, "error", 0) != 0) {90nvlist_destroy(nvl);91return (NULL);92}93return (protoent_unpack(nvl));94}9596static void97protoent_pack(const struct protoent *pp, nvlist_t *nvl)98{99int n = 0;100101nvlist_add_string(nvl, "name", pp->p_name);102103while (pp->p_aliases[n] != NULL)104++n;105nvlist_add_string_array(nvl, "aliases",106(const char * const *)pp->p_aliases, n);107108nvlist_add_number(nvl, "proto", (uint64_t)pp->p_proto);109}110111static int112netdb_getprotobyname(const nvlist_t *limits __unused, const nvlist_t *nvlin,113nvlist_t *nvlout)114{115const char *name;116struct protoent *pp;117118name = dnvlist_get_string(nvlin, "name", NULL);119if (name == NULL)120return (EDOOFUS);121122pp = getprotobyname(name);123if (pp == NULL)124return (EINVAL);125126protoent_pack(pp, nvlout);127return (0);128}129130static int131netdb_limit(const nvlist_t *oldlimits __unused,132const nvlist_t *newlimits __unused)133{134135return (0);136}137138static int139netdb_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,140nvlist_t *nvlout)141{142int error;143144if (strcmp(cmd, "getprotobyname") == 0)145error = netdb_getprotobyname(limits, nvlin, nvlout);146else147error = NO_RECOVERY;148149return (error);150}151152CREATE_SERVICE("system.netdb", netdb_limit, netdb_command, 0);153154155