15325 bhyve upstream sync 2023 January
[illumos-gate.git] / usr / src / cmd / keyserv / key_generic.c
blobb643b9b8d377441fa7827d64cd4597ccb303fdd7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #include <stdio.h>
41 #include <rpc/rpc.h>
42 #include <errno.h>
43 #ifdef SYSLOG
44 #include <sys/syslog.h>
45 #else
46 #define LOG_ERR 3
47 #endif /* SYSLOG */
48 #include <rpc/nettype.h>
49 #include <netconfig.h>
50 #include <netdir.h>
52 extern char *strdup();
55 * The highest level interface for server creation.
56 * It tries for all the nettokens in that particular class of token
57 * and returns the number of handles it can create and/or find.
59 * It creates a link list of all the handles it could create.
60 * If svc_create() is called multiple times, it uses the handle
61 * created earlier instead of creating a new handle every time.
63 * Copied from svc_generic.c
65 int
66 svc_create_local_service(dispatch, prognum, versnum, nettype, servname)
67 void (*dispatch) (); /* Dispatch function */
68 ulong_t prognum; /* Program number */
69 ulong_t versnum; /* Version number */
70 char *nettype; /* Networktype token */
71 char *servname; /* name of the service */
73 struct xlist {
74 SVCXPRT *xprt; /* Server handle */
75 struct xlist *next; /* Next item */
76 } *l;
77 static struct xlist *xprtlist;
78 int num = 0;
79 SVCXPRT *xprt;
80 struct netconfig *nconf;
81 struct t_bind *bind_addr;
82 void *handle;
83 int fd;
84 struct nd_hostserv ns;
85 struct nd_addrlist *nas;
87 if ((handle = __rpc_setconf(nettype)) == NULL) {
88 (void) syslog(LOG_ERR,
89 "svc_create: could not read netconfig database");
90 return (0);
92 while (nconf = __rpc_getconf(handle)) {
93 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK))
94 continue;
95 for (l = xprtlist; l; l = l->next) {
96 if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
97 /* Found an old one, use it */
98 (void) rpcb_unset(prognum, versnum, nconf);
99 if (svc_reg(l->xprt, prognum, versnum,
100 dispatch, nconf) == FALSE)
101 (void) syslog(LOG_ERR,
102 "svc_create: could not register prog %d vers %d on %s",
103 prognum, versnum, nconf->nc_netid);
104 else
105 num++;
106 break;
109 if (l)
110 continue;
111 /* It was not found. Now create a new one */
112 if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
113 syslog(LOG_ERR,
114 "svc_create: %s: cannot open connection: %s",
115 nconf->nc_netid, t_errlist[t_errno]);
116 continue;
120 * Negotiate for returning the uid of the caller.
121 * This should be done before enabling the endpoint for
122 * service via t_bind() (called in svc_tli_create())
123 * so that requests to keyserv contain the uid.
125 if (__rpc_negotiate_uid(fd) != 0) {
126 syslog(LOG_ERR,
127 "Couldn't negotiate for uid with loopback transport %s",
128 nconf->nc_netid);
129 t_close(fd);
130 continue;
133 bind_addr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
134 if ((bind_addr == NULL)) {
135 t_close(fd);
136 (void) syslog(LOG_ERR, "svc_create: t_alloc failed\n");
137 continue;
139 ns.h_host = HOST_SELF;
140 ns.h_serv = servname;
141 if (!netdir_getbyname(nconf, &ns, &nas)) {
142 /* Copy the address */
143 bind_addr->addr.len = nas->n_addrs->len;
144 memcpy(bind_addr->addr.buf, nas->n_addrs->buf,
145 (int)nas->n_addrs->len);
146 bind_addr->qlen = 8;
147 netdir_free((char *)nas, ND_ADDRLIST);
148 } else {
149 syslog(LOG_ERR,
150 "svc_create: no well known address for %s on transport %s",
151 servname, nconf->nc_netid);
152 (void) t_free((char *)bind_addr, T_BIND);
153 bind_addr = NULL;
156 xprt = svc_tli_create(fd, nconf, bind_addr, 0, 0);
157 if (bind_addr)
158 (void) t_free((char *)bind_addr, T_BIND);
159 if (xprt) {
160 (void) rpcb_unset(prognum, versnum, nconf);
161 if (svc_reg(xprt, prognum, versnum,
162 dispatch, nconf) == FALSE) {
163 (void) syslog(LOG_ERR,
164 "svc_create: could not register prog %d vers %d on %s",
165 prognum, versnum, nconf->nc_netid);
166 SVC_DESTROY(xprt);
167 continue;
169 l = (struct xlist *)malloc(sizeof (struct xlist));
170 if (l == (struct xlist *)NULL) {
171 (void) syslog(LOG_ERR,
172 "svc_create: no memory");
173 SVC_DESTROY(xprt);
174 return (num);
176 l->xprt = xprt;
177 l->next = xprtlist;
178 xprtlist = l;
179 num++;
182 __rpc_endconf(handle);
183 return (num);