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