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 #include <sys/dnv.h>
30 #include <sys/nv.h>
31 #include <netinet/in.h>
32 
33 #include <assert.h>
34 #include <errno.h>
35 #include <netdb.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <libcasper.h>
41 #include <libcasper_service.h>
42 
43 #include "cap_netdb.h"
44 
45 static struct protoent *
46 protoent_unpack(nvlist_t *nvl)
47 {
48 	struct protoent *pp;
49 	char **aliases;
50 	size_t n;
51 
52 	pp = malloc(sizeof(*pp));
53 	if (pp == NULL) {
54 		nvlist_destroy(nvl);
55 		return (NULL);
56 	}
57 
58 	pp->p_name = nvlist_take_string(nvl, "name");
59 
60 	aliases = nvlist_take_string_array(nvl, "aliases", &n);
61 	pp->p_aliases = realloc(aliases, sizeof(char *) * (n + 1));
62 	if (pp->p_aliases == NULL) {
63 		while (n-- > 0)
64 			free(aliases[n]);
65 		free(aliases);
66 		free(pp->p_name);
67 		free(pp);
68 		nvlist_destroy(nvl);
69 		return (NULL);
70 	}
71 	pp->p_aliases[n] = NULL;
72 
73 	pp->p_proto = (int)nvlist_take_number(nvl, "proto");
74 
75 	nvlist_destroy(nvl);
76 	return (pp);
77 }
78 
79 struct protoent *
80 cap_getprotobyname(cap_channel_t *chan, const char *name)
81 {
82 	nvlist_t *nvl;
83 
84 	nvl = nvlist_create(0);
85 	nvlist_add_string(nvl, "cmd", "getprotobyname");
86 	nvlist_add_string(nvl, "name", name);
87 	nvl = cap_xfer_nvlist(chan, nvl);
88 	if (nvl == NULL)
89 		return (NULL);
90 	if (dnvlist_get_number(nvl, "error", 0) != 0) {
91 		nvlist_destroy(nvl);
92 		return (NULL);
93 	}
94 	return (protoent_unpack(nvl));
95 }
96 
97 static void
98 protoent_pack(const struct protoent *pp, nvlist_t *nvl)
99 {
100 	int n = 0;
101 
102 	nvlist_add_string(nvl, "name", pp->p_name);
103 
104 	while (pp->p_aliases[n] != NULL)
105 		++n;
106 	nvlist_add_string_array(nvl, "aliases",
107 	    (const char * const *)pp->p_aliases, n);
108 
109 	nvlist_add_number(nvl, "proto", (uint64_t)pp->p_proto);
110 }
111 
112 static int
113 netdb_getprotobyname(const nvlist_t *limits __unused, const nvlist_t *nvlin,
114     nvlist_t *nvlout)
115 {
116 	const char *name;
117 	struct protoent *pp;
118 
119 	name = dnvlist_get_string(nvlin, "name", NULL);
120 	if (name == NULL)
121 		return (EDOOFUS);
122 
123 	pp = getprotobyname(name);
124 	if (pp == NULL)
125 		return (EINVAL);
126 
127 	protoent_pack(pp, nvlout);
128 	return (0);
129 }
130 
131 static int
132 netdb_limit(const nvlist_t *oldlimits __unused,
133     const nvlist_t *newlimits __unused)
134 {
135 
136 	return (0);
137 }
138 
139 static int
140 netdb_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
141     nvlist_t *nvlout)
142 {
143 	int error;
144 
145 	if (strcmp(cmd, "getprotobyname") == 0)
146 		error = netdb_getprotobyname(limits, nvlin, nvlout);
147 	else
148 		error = NO_RECOVERY;
149 
150 	return (error);
151 }
152 
153 CREATE_SERVICE("system.netdb", netdb_limit, netdb_command, 0);
154