xref: /illumos-gate/usr/src/cmd/keyserv/key_generic.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <stdio.h>
43 #include <rpc/rpc.h>
44 #include <errno.h>
45 #ifdef SYSLOG
46 #include <sys/syslog.h>
47 #else
48 #define	LOG_ERR 3
49 #endif				/* SYSLOG */
50 #include <rpc/nettype.h>
51 #include <netconfig.h>
52 #include <netdir.h>
53 
54 extern char *strdup();
55 
56 /*
57  * The highest level interface for server creation.
58  * It tries for all the nettokens in that particular class of token
59  * and returns the number of handles it can create and/or find.
60  *
61  * It creates a link list of all the handles it could create.
62  * If svc_create() is called multiple times, it uses the handle
63  * created earlier instead of creating a new handle every time.
64  *
65  * Copied from svc_generic.c
66  */
67 int
68 svc_create_local_service(dispatch, prognum, versnum, nettype, servname)
69 void (*dispatch) ();		/* Dispatch function */
70 ulong_t prognum;			/* Program number */
71 ulong_t versnum;			/* Version number */
72 char *nettype;			/* Networktype token */
73 char *servname;			/* name of the service */
74 {
75 	struct xlist {
76 		SVCXPRT *xprt;		/* Server handle */
77 		struct xlist *next;	/* Next item */
78 	} *l;
79 	static struct xlist *xprtlist;
80 	int num = 0;
81 	SVCXPRT *xprt;
82 	struct netconfig *nconf;
83 	struct t_bind *bind_addr;
84 	void *handle;
85 	int fd;
86 	struct nd_hostserv ns;
87 	struct nd_addrlist *nas;
88 
89 	if ((handle = __rpc_setconf(nettype)) == NULL) {
90 		(void) syslog(LOG_ERR,
91 		"svc_create: could not read netconfig database");
92 		return (0);
93 	}
94 	while (nconf = __rpc_getconf(handle)) {
95 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK))
96 			continue;
97 		for (l = xprtlist; l; l = l->next) {
98 			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
99 				/* Found an  old  one,  use  it */
100 				(void) rpcb_unset(prognum, versnum, nconf);
101 				if (svc_reg(l->xprt, prognum, versnum,
102 					dispatch, nconf) == FALSE)
103 					(void) syslog(LOG_ERR,
104 	    "svc_create: could not register prog %d vers %d on %s",
105 					    prognum, versnum, nconf->nc_netid);
106 				else
107 					num++;
108 				break;
109 			}
110 		}
111 		if (l)
112 			continue;
113 		/* It was not found. Now create a new one */
114 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
115 			syslog(LOG_ERR,
116 		"svc_create: %s: cannot open connection: %s",
117 			    nconf->nc_netid, t_errlist[t_errno]);
118 			continue;
119 		}
120 
121 		/*
122 		 * Negotiate for returning the uid of the caller.
123 		 * This should be done before enabling the endpoint for
124 		 * service via t_bind() (called in svc_tli_create())
125 		 * so that requests to keyserv contain the uid.
126 		 */
127 		if (__rpc_negotiate_uid(fd) != 0) {
128 			syslog(LOG_ERR,
129 			"Couldn't negotiate for uid with loopback transport %s",
130 				nconf->nc_netid);
131 			t_close(fd);
132 			continue;
133 		}
134 
135 		bind_addr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
136 		if ((bind_addr == NULL)) {
137 			t_close(fd);
138 			(void) syslog(LOG_ERR, "svc_create: t_alloc failed\n");
139 			continue;
140 		}
141 		ns.h_host = HOST_SELF;
142 		ns.h_serv = servname;
143 		if (!netdir_getbyname(nconf, &ns, &nas)) {
144 			/* Copy the address */
145 			bind_addr->addr.len = nas->n_addrs->len;
146 			memcpy(bind_addr->addr.buf, nas->n_addrs->buf,
147 				(int)nas->n_addrs->len);
148 			bind_addr->qlen = 8;
149 			netdir_free((char *)nas, ND_ADDRLIST);
150 		} else {
151 			syslog(LOG_ERR,
152 	"svc_create: no well known address for %s on transport %s",
153 			    servname, nconf->nc_netid);
154 			(void) t_free((char *)bind_addr, T_BIND);
155 			bind_addr = NULL;
156 		}
157 
158 		xprt = svc_tli_create(fd, nconf, bind_addr, 0, 0);
159 		if (bind_addr)
160 			(void) t_free((char *)bind_addr, T_BIND);
161 		if (xprt) {
162 			(void) rpcb_unset(prognum, versnum, nconf);
163 			if (svc_reg(xprt, prognum, versnum,
164 				dispatch, nconf) == FALSE) {
165 				(void) syslog(LOG_ERR,
166 	    "svc_create: could not register prog %d vers %d on %s",
167 				    prognum, versnum, nconf->nc_netid);
168 				SVC_DESTROY(xprt);
169 				continue;
170 			}
171 			l = (struct xlist *)malloc(sizeof (struct xlist));
172 			if (l == (struct xlist *)NULL) {
173 				(void) syslog(LOG_ERR,
174 					    "svc_create: no memory");
175 				SVC_DESTROY(xprt);
176 				return (num);
177 			}
178 			l->xprt = xprt;
179 			l->next = xprtlist;
180 			xprtlist = l;
181 			num++;
182 		}
183 	}
184 	__rpc_endconf(handle);
185 	return (num);
186 }
187