ypserv.c revision 1.22 1 1.22 chuck /* $NetBSD: ypserv.c,v 1.22 2008/05/16 16:41:42 chuck Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Mats O Jansson
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.7 lukem #include <sys/cdefs.h>
35 1.7 lukem #ifndef lint
36 1.22 chuck __RCSID("$NetBSD: ypserv.c,v 1.22 2008/05/16 16:41:42 chuck Exp $");
37 1.7 lukem #endif
38 1.7 lukem
39 1.1 thorpej #include <sys/types.h>
40 1.1 thorpej #include <sys/socket.h>
41 1.1 thorpej #include <sys/wait.h>
42 1.1 thorpej
43 1.5 thorpej #include <err.h>
44 1.1 thorpej #include <netdb.h>
45 1.1 thorpej #include <signal.h>
46 1.1 thorpej #include <stdio.h>
47 1.1 thorpej #include <stdlib.h>
48 1.1 thorpej #include <string.h>
49 1.5 thorpej #include <syslog.h>
50 1.5 thorpej #include <unistd.h>
51 1.10 thorpej #include <util.h>
52 1.20 christos #include <stdarg.h>
53 1.20 christos #include <errno.h>
54 1.1 thorpej
55 1.1 thorpej #include <rpc/rpc.h>
56 1.1 thorpej #include <rpc/xdr.h>
57 1.1 thorpej #include <rpc/pmap_clnt.h>
58 1.1 thorpej
59 1.1 thorpej #include <rpcsvc/yp_prot.h>
60 1.1 thorpej
61 1.1 thorpej #include "ypdef.h"
62 1.1 thorpej #include "ypserv.h"
63 1.1 thorpej
64 1.8 thorpej #ifdef LIBWRAP
65 1.8 thorpej #include <tcpd.h>
66 1.8 thorpej
67 1.8 thorpej int allow_severity = LOG_DAEMON | LOG_INFO;
68 1.8 thorpej int deny_severity = LOG_DAEMON | LOG_WARNING;
69 1.8 thorpej
70 1.8 thorpej /* XXX For ypserv_proc.c -- NOT THREAD SAFE! (like any of this code is) */
71 1.8 thorpej const char *clientstr;
72 1.8 thorpej const char *svcname;
73 1.8 thorpej #endif /* LIBWRAP */
74 1.1 thorpej
75 1.1 thorpej int usedns;
76 1.9 abs #ifdef DEBUG
77 1.20 christos static int foreground = 1;
78 1.9 abs #else
79 1.20 christos static int foreground;
80 1.9 abs #endif
81 1.9 abs
82 1.8 thorpej #ifdef LIBWRAP
83 1.8 thorpej int lflag;
84 1.8 thorpej #endif
85 1.1 thorpej
86 1.20 christos static struct bindsock {
87 1.20 christos sa_family_t family;
88 1.20 christos int type;
89 1.20 christos int proto;
90 1.20 christos const char *name;
91 1.20 christos } socklist[] = {
92 1.20 christos { AF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp" },
93 1.20 christos { AF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp" },
94 1.20 christos { AF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp6" },
95 1.20 christos { AF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp6" },
96 1.20 christos };
97 1.1 thorpej
98 1.21 perry static void usage(void) __dead;
99 1.20 christos static int bind_resv_port(int, sa_family_t, in_port_t);
100 1.22 chuck void ypserv_sock_hostname(struct host_info *host);
101 1.20 christos
102 1.20 christos static void
103 1.20 christos _msgout(int level, const char *msg, ...)
104 1.1 thorpej {
105 1.20 christos va_list ap;
106 1.20 christos va_start(ap, msg);
107 1.12 thorpej if (foreground)
108 1.20 christos vwarnx(msg, ap);
109 1.9 abs else
110 1.20 christos vsyslog(level, msg, ap);
111 1.20 christos va_end(ap);
112 1.1 thorpej }
113 1.1 thorpej
114 1.22 chuck void ypserv_sock_hostname(struct host_info *host)
115 1.22 chuck {
116 1.22 chuck host->name[0] = 0;
117 1.22 chuck }
118 1.22 chuck
119 1.1 thorpej static void
120 1.6 lukem ypprog_2(struct svc_req *rqstp, SVCXPRT *transp)
121 1.1 thorpej {
122 1.1 thorpej union {
123 1.1 thorpej char * ypproc_domain_2_arg;
124 1.1 thorpej char * ypproc_domain_nonack_2_arg;
125 1.1 thorpej struct ypreq_key ypproc_match_2_arg;
126 1.1 thorpej struct ypreq_nokey ypproc_first_2_arg;
127 1.1 thorpej struct ypreq_key ypproc_next_2_arg;
128 1.1 thorpej struct ypreq_xfr ypproc_xfr_2_arg;
129 1.1 thorpej struct ypreq_nokey ypproc_all_2_arg;
130 1.1 thorpej struct ypreq_nokey ypproc_master_2_arg;
131 1.1 thorpej struct ypreq_nokey ypproc_order_2_arg;
132 1.1 thorpej char * ypproc_maplist_2_arg;
133 1.1 thorpej } argument;
134 1.20 christos void *argp = &argument;
135 1.1 thorpej char *result;
136 1.1 thorpej xdrproc_t xdr_argument, xdr_result;
137 1.16 wiz void *(*local)(void *, struct svc_req *);
138 1.8 thorpej #ifdef LIBWRAP
139 1.8 thorpej struct request_info req;
140 1.12 thorpej struct sockaddr *caller;
141 1.8 thorpej #define SVCNAME(x) svcname = x
142 1.8 thorpej #else
143 1.8 thorpej #define SVCNAME(x) /* nothing */
144 1.8 thorpej #endif
145 1.1 thorpej
146 1.8 thorpej #ifdef LIBWRAP
147 1.12 thorpej caller = svc_getrpccaller(transp)->buf;
148 1.20 christos (void)request_init(&req, RQ_DAEMON, getprogname(), RQ_CLIENT_SIN,
149 1.20 christos caller, NULL);
150 1.8 thorpej sock_methods(&req);
151 1.22 chuck
152 1.22 chuck /*
153 1.22 chuck * Do not do hostname lookups! This avoids possible delays due
154 1.22 chuck * to DNS, preventing a possible DoS attack, as well as possible
155 1.22 chuck * circular lookups (e.g. a hostname lookup requiring a request
156 1.22 chuck * to ourselves).
157 1.22 chuck */
158 1.22 chuck req.hostname = ypserv_sock_hostname;
159 1.8 thorpej #endif
160 1.8 thorpej
161 1.1 thorpej switch (rqstp->rq_proc) {
162 1.1 thorpej case YPPROC_NULL:
163 1.1 thorpej xdr_argument = xdr_void;
164 1.1 thorpej xdr_result = xdr_void;
165 1.1 thorpej local = ypproc_null_2_svc;
166 1.8 thorpej SVCNAME("null_2");
167 1.1 thorpej break;
168 1.1 thorpej
169 1.1 thorpej case YPPROC_DOMAIN:
170 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
171 1.1 thorpej xdr_result = xdr_bool;
172 1.1 thorpej local = ypproc_domain_2_svc;
173 1.8 thorpej SVCNAME("domain_2");
174 1.1 thorpej break;
175 1.1 thorpej
176 1.1 thorpej case YPPROC_DOMAIN_NONACK:
177 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
178 1.1 thorpej xdr_result = xdr_bool;
179 1.1 thorpej local = ypproc_domain_nonack_2_svc;
180 1.8 thorpej SVCNAME("domain_nonack_2");
181 1.1 thorpej break;
182 1.1 thorpej
183 1.1 thorpej case YPPROC_MATCH:
184 1.1 thorpej xdr_argument = xdr_ypreq_key;
185 1.1 thorpej xdr_result = xdr_ypresp_val;
186 1.1 thorpej local = ypproc_match_2_svc;
187 1.8 thorpej SVCNAME("match_2");
188 1.1 thorpej break;
189 1.1 thorpej
190 1.1 thorpej case YPPROC_FIRST:
191 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
192 1.1 thorpej xdr_result = xdr_ypresp_key_val;
193 1.1 thorpej local = ypproc_first_2_svc;
194 1.8 thorpej SVCNAME("first_2");
195 1.1 thorpej break;
196 1.1 thorpej
197 1.1 thorpej case YPPROC_NEXT:
198 1.1 thorpej xdr_argument = xdr_ypreq_key;
199 1.1 thorpej xdr_result = xdr_ypresp_key_val;
200 1.1 thorpej local = ypproc_next_2_svc;
201 1.8 thorpej SVCNAME("next_2");
202 1.1 thorpej break;
203 1.1 thorpej
204 1.1 thorpej case YPPROC_XFR:
205 1.1 thorpej xdr_argument = xdr_ypreq_xfr;
206 1.1 thorpej xdr_result = xdr_ypresp_xfr;
207 1.1 thorpej local = ypproc_xfr_2_svc;
208 1.8 thorpej SVCNAME("xfer_2");
209 1.1 thorpej break;
210 1.1 thorpej
211 1.1 thorpej case YPPROC_CLEAR:
212 1.1 thorpej xdr_argument = xdr_void;
213 1.1 thorpej xdr_result = xdr_void;
214 1.1 thorpej local = ypproc_clear_2_svc;
215 1.8 thorpej SVCNAME("clear_2");
216 1.1 thorpej break;
217 1.1 thorpej
218 1.1 thorpej case YPPROC_ALL:
219 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
220 1.1 thorpej xdr_result = xdr_ypresp_all;
221 1.1 thorpej local = ypproc_all_2_svc;
222 1.8 thorpej SVCNAME("all_2");
223 1.1 thorpej break;
224 1.1 thorpej
225 1.1 thorpej case YPPROC_MASTER:
226 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
227 1.1 thorpej xdr_result = xdr_ypresp_master;
228 1.1 thorpej local = ypproc_master_2_svc;
229 1.8 thorpej SVCNAME("master_2");
230 1.1 thorpej break;
231 1.1 thorpej
232 1.1 thorpej case YPPROC_ORDER:
233 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
234 1.1 thorpej xdr_result = xdr_ypresp_order;
235 1.1 thorpej local = ypproc_order_2_svc;
236 1.8 thorpej SVCNAME("order_2");
237 1.1 thorpej break;
238 1.1 thorpej
239 1.1 thorpej case YPPROC_MAPLIST:
240 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
241 1.1 thorpej xdr_result = xdr_ypresp_maplist;
242 1.1 thorpej local = ypproc_maplist_2_svc;
243 1.8 thorpej SVCNAME("maplist_2");
244 1.1 thorpej break;
245 1.1 thorpej
246 1.1 thorpej default:
247 1.1 thorpej svcerr_noproc(transp);
248 1.1 thorpej return;
249 1.1 thorpej }
250 1.8 thorpej
251 1.8 thorpej #ifdef LIBWRAP
252 1.8 thorpej clientstr = eval_client(&req);
253 1.8 thorpej
254 1.8 thorpej if (hosts_access(&req) == 0) {
255 1.8 thorpej syslog(deny_severity,
256 1.8 thorpej "%s: refused request from %.500s", svcname, clientstr);
257 1.8 thorpej svcerr_auth(transp, AUTH_FAILED);
258 1.8 thorpej return;
259 1.8 thorpej }
260 1.8 thorpej #endif
261 1.8 thorpej
262 1.20 christos (void)memset(&argument, 0, sizeof (argument));
263 1.20 christos if (!svc_getargs(transp, xdr_argument, argp)) {
264 1.1 thorpej svcerr_decode(transp);
265 1.1 thorpej return;
266 1.1 thorpej }
267 1.1 thorpej result = (*local)(&argument, rqstp);
268 1.1 thorpej if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
269 1.1 thorpej svcerr_systemerr(transp);
270 1.1 thorpej }
271 1.20 christos if (!svc_freeargs(transp, xdr_argument, argp)) {
272 1.12 thorpej _msgout(LOG_ERR, "unable to free arguments");
273 1.1 thorpej exit(1);
274 1.1 thorpej }
275 1.1 thorpej return;
276 1.1 thorpej }
277 1.1 thorpej
278 1.4 christos /*
279 1.4 christos * limited NIS version 1 support: the null, domain, and domain_nonack
280 1.4 christos * request/reply format is identical between v1 and v2. SunOS4's ypbind
281 1.4 christos * makes v1 domain_nonack calls.
282 1.4 christos */
283 1.4 christos static void
284 1.6 lukem ypprog_1(struct svc_req *rqstp, SVCXPRT *transp)
285 1.4 christos {
286 1.4 christos switch (rqstp->rq_proc) {
287 1.4 christos case YPPROC_NULL:
288 1.4 christos case YPPROC_DOMAIN:
289 1.4 christos case YPPROC_DOMAIN_NONACK:
290 1.4 christos ypprog_2(rqstp, transp);
291 1.4 christos return;
292 1.4 christos
293 1.4 christos default:
294 1.4 christos svcerr_noproc(transp);
295 1.4 christos return;
296 1.4 christos }
297 1.4 christos }
298 1.4 christos
299 1.1 thorpej int
300 1.18 wiz main(int argc, char *argv[])
301 1.1 thorpej {
302 1.20 christos SVCXPRT *xprt;
303 1.20 christos struct netconfig *cfg = NULL;
304 1.20 christos int s;
305 1.5 thorpej struct sigaction sa;
306 1.20 christos struct bindsock *bs;
307 1.20 christos in_port_t port = 0;
308 1.12 thorpej int ch, xcreated = 0, one = 1;
309 1.5 thorpej
310 1.19 christos setprogname(argv[0]);
311 1.19 christos
312 1.8 thorpej #ifdef LIBWRAP
313 1.20 christos #define GETOPTSTR "dflp:"
314 1.8 thorpej #else
315 1.20 christos #define GETOPTSTR "dfp:"
316 1.8 thorpej #endif
317 1.8 thorpej while ((ch = getopt(argc, argv, GETOPTSTR)) != -1) {
318 1.1 thorpej switch (ch) {
319 1.1 thorpej case 'd':
320 1.1 thorpej usedns = 1;
321 1.1 thorpej break;
322 1.9 abs case 'f':
323 1.9 abs foreground = 1;
324 1.9 abs break;
325 1.20 christos case 'p':
326 1.20 christos port = atoi(optarg);
327 1.20 christos break;
328 1.8 thorpej #ifdef LIBWRAP
329 1.8 thorpej case 'l':
330 1.8 thorpej lflag = 1;
331 1.1 thorpej break;
332 1.8 thorpej #endif
333 1.1 thorpej default:
334 1.1 thorpej usage();
335 1.1 thorpej }
336 1.1 thorpej }
337 1.1 thorpej
338 1.8 thorpej #undef GETOPTSTR
339 1.8 thorpej
340 1.1 thorpej /* This program must be run by root. */
341 1.1 thorpej if (geteuid() != 0)
342 1.1 thorpej errx(1, "must run as root");
343 1.1 thorpej
344 1.12 thorpej if (foreground == 0 && daemon(0, 0))
345 1.3 mikel err(1, "can't detach");
346 1.3 mikel
347 1.13 lukem openlog("ypserv", LOG_PID, LOG_DAEMON);
348 1.8 thorpej syslog(LOG_INFO, "starting");
349 1.20 christos (void)pidfile(NULL);
350 1.1 thorpej
351 1.20 christos (void) rpcb_unset((u_int)YPPROG, (u_int)YPVERS, NULL);
352 1.20 christos (void) rpcb_unset((u_int)YPPROG, (u_int)YPVERS_ORIG, NULL);
353 1.12 thorpej
354 1.1 thorpej
355 1.1 thorpej ypdb_init(); /* init db stuff */
356 1.1 thorpej
357 1.19 christos sa.sa_handler = SIG_IGN;
358 1.19 christos sa.sa_flags = SA_NOCLDWAIT;
359 1.8 thorpej if (sigemptyset(&sa.sa_mask)) {
360 1.20 christos _msgout(LOG_ERR, "sigemptyset: %s", strerror(errno));
361 1.8 thorpej exit(1);
362 1.8 thorpej }
363 1.8 thorpej if (sigaction(SIGCHLD, &sa, NULL)) {
364 1.20 christos _msgout(LOG_ERR, "sigaction: %s", strerror(errno));
365 1.8 thorpej exit(1);
366 1.8 thorpej }
367 1.1 thorpej
368 1.20 christos for (bs = socklist;
369 1.20 christos bs < &socklist[sizeof(socklist) / sizeof(socklist[0])]; bs++) {
370 1.20 christos
371 1.20 christos if ((s = socket(bs->family, bs->type, bs->proto)) == -1)
372 1.20 christos continue;
373 1.20 christos
374 1.20 christos if (bs->family == AF_INET6) {
375 1.20 christos /*
376 1.20 christos * We're doing host-based access checks here, so don't
377 1.20 christos * allow v4-in-v6 to confuse things.
378 1.20 christos */
379 1.20 christos if (setsockopt(s, IPPROTO_IPV6,
380 1.20 christos IPV6_V6ONLY, &one, sizeof(one)) == -1) {
381 1.20 christos _msgout(LOG_ERR,
382 1.20 christos "can't disable v4-in-v6 on %s socket",
383 1.20 christos bs->name);
384 1.20 christos exit(1);
385 1.20 christos }
386 1.20 christos }
387 1.20 christos
388 1.20 christos if ((cfg = getnetconfigent(bs->name)) == NULL) {
389 1.20 christos _msgout(LOG_ERR,
390 1.20 christos "unable to get network configuration for %s port",
391 1.20 christos bs->name);
392 1.20 christos goto out;
393 1.20 christos }
394 1.20 christos
395 1.20 christos if (bind_resv_port(s, bs->family, port) != 0)
396 1.20 christos goto out;
397 1.20 christos
398 1.20 christos if (bs->type == SOCK_STREAM) {
399 1.20 christos (void)listen(s, SOMAXCONN);
400 1.20 christos xprt = svc_vc_create(s, 0, 0);
401 1.20 christos } else {
402 1.20 christos xprt = svc_dg_create(s, 0, 0);
403 1.20 christos }
404 1.20 christos
405 1.20 christos if (xprt == NULL) {
406 1.20 christos _msgout(LOG_WARNING, "unable to create %s service",
407 1.20 christos bs->name);
408 1.20 christos goto out;
409 1.20 christos }
410 1.20 christos if (svc_reg(xprt, (u_int)YPPROG, (u_int)YPVERS_ORIG, ypprog_1,
411 1.20 christos cfg) == 0 ||
412 1.20 christos svc_reg(xprt, (u_int)YPPROG, (u_int)YPVERS, ypprog_2,
413 1.20 christos cfg) == 0) {
414 1.20 christos _msgout(LOG_WARNING, "unable to register %s service",
415 1.20 christos bs->name);
416 1.20 christos goto out;
417 1.20 christos }
418 1.20 christos xcreated++;
419 1.20 christos freenetconfigent(cfg);
420 1.20 christos continue;
421 1.20 christos out:
422 1.20 christos if (s != -1)
423 1.20 christos (void)close(s);
424 1.20 christos if (cfg) {
425 1.20 christos freenetconfigent(cfg);
426 1.20 christos cfg = NULL;
427 1.20 christos }
428 1.1 thorpej }
429 1.1 thorpej
430 1.12 thorpej if (xcreated == 0) {
431 1.12 thorpej _msgout(LOG_ERR, "unable to create any services");
432 1.12 thorpej exit(1);
433 1.1 thorpej }
434 1.1 thorpej
435 1.1 thorpej svc_run();
436 1.12 thorpej _msgout(LOG_ERR, "svc_run returned");
437 1.1 thorpej exit(1);
438 1.1 thorpej /* NOTREACHED */
439 1.1 thorpej }
440 1.1 thorpej
441 1.20 christos static void
442 1.18 wiz usage(void)
443 1.1 thorpej {
444 1.1 thorpej
445 1.8 thorpej #ifdef LIBWRAP
446 1.20 christos #define USAGESTR "Usage: %s [-d] [-l] [-p <port>]\n"
447 1.8 thorpej #else
448 1.20 christos #define USAGESTR "Usage: %s [-d] [-p <port>]\n"
449 1.8 thorpej #endif
450 1.8 thorpej
451 1.20 christos (void)fprintf(stderr, USAGESTR, getprogname());
452 1.1 thorpej exit(1);
453 1.8 thorpej
454 1.8 thorpej #undef USAGESTR
455 1.7 lukem }
456 1.7 lukem
457 1.7 lukem /*
458 1.7 lukem * _yp_invalid_map: check if given map name isn't legal.
459 1.7 lukem * returns non-zero if invalid
460 1.7 lukem *
461 1.7 lukem * XXX: this probably should be in libc/yp/yplib.c
462 1.7 lukem */
463 1.7 lukem int
464 1.18 wiz _yp_invalid_map(const char *map)
465 1.7 lukem {
466 1.7 lukem if (map == NULL || *map == '\0')
467 1.7 lukem return 1;
468 1.7 lukem
469 1.7 lukem if (strlen(map) > YPMAXMAP)
470 1.7 lukem return 1;
471 1.7 lukem
472 1.7 lukem if (strchr(map, '/') != NULL)
473 1.7 lukem return 1;
474 1.7 lukem
475 1.7 lukem return 0;
476 1.1 thorpej }
477 1.20 christos
478 1.20 christos static int
479 1.20 christos bind_resv_port(int sock, sa_family_t family, in_port_t port)
480 1.20 christos {
481 1.20 christos struct sockaddr *sa;
482 1.20 christos struct sockaddr_in sasin;
483 1.20 christos struct sockaddr_in6 sasin6;
484 1.20 christos
485 1.20 christos switch (family) {
486 1.20 christos case AF_INET:
487 1.20 christos (void)memset(&sasin, 0, sizeof(sasin));
488 1.20 christos sasin.sin_len = sizeof(sasin);
489 1.20 christos sasin.sin_family = family;
490 1.20 christos sasin.sin_port = htons(port);
491 1.20 christos sa = (struct sockaddr *)(void *)&sasin;
492 1.20 christos break;
493 1.20 christos case AF_INET6:
494 1.20 christos (void)memset(&sasin6, 0, sizeof(sasin6));
495 1.20 christos sasin6.sin6_len = sizeof(sasin6);
496 1.20 christos sasin6.sin6_family = family;
497 1.20 christos sasin6.sin6_port = htons(port);
498 1.20 christos sa = (struct sockaddr *)(void *)&sasin6;
499 1.20 christos break;
500 1.20 christos default:
501 1.20 christos _msgout(LOG_ERR, "Unsupported address family %d", family);
502 1.20 christos return -1;
503 1.20 christos }
504 1.20 christos if (bindresvport_sa(sock, sa) == -1) {
505 1.20 christos _msgout(LOG_ERR, "Cannot bind to reserved port %d (%s)", port,
506 1.20 christos strerror(errno));
507 1.20 christos return -1;
508 1.20 christos }
509 1.20 christos return 0;
510 1.20 christos }
511