xref: /illumos-gate/usr/src/cmd/gss/gssd/gssd_generic.c (revision 2a8bcb4e)
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 (c) 1988-1997, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate #include <syslog.h>
32*7c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
33*7c478bd9Sstevel@tonic-gate #include <netconfig.h>
34*7c478bd9Sstevel@tonic-gate #include <netdir.h>
35*7c478bd9Sstevel@tonic-gate #include <tiuser.h>
36*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
39*7c478bd9Sstevel@tonic-gate #include <locale.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate extern int __rpc_negotiate_uid(int);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * The highest level interface for server creation.
45*7c478bd9Sstevel@tonic-gate  * Copied from svc_generic.c and cmd/keyserv/key_generic.c, but adapted
46*7c478bd9Sstevel@tonic-gate  * to work only for TPI_CLTS semantics, and to be called only once
47*7c478bd9Sstevel@tonic-gate  * from gssd.c. Returns 1 (interface created) on success and 0
48*7c478bd9Sstevel@tonic-gate  * (no interfaces created) on failure.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate int
51*7c478bd9Sstevel@tonic-gate svc_create_local_service(dispatch, prognum, versnum, nettype, servname)
52*7c478bd9Sstevel@tonic-gate void (*dispatch) ();		/* Dispatch function */
53*7c478bd9Sstevel@tonic-gate u_long prognum;			/* Program number */
54*7c478bd9Sstevel@tonic-gate u_long versnum;			/* Version number */
55*7c478bd9Sstevel@tonic-gate char *nettype;			/* Networktype token */
56*7c478bd9Sstevel@tonic-gate char *servname;			/* name of the service */
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	int num = 0;
59*7c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
60*7c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
61*7c478bd9Sstevel@tonic-gate 	struct t_bind *bind_addr;
62*7c478bd9Sstevel@tonic-gate 	void *net;
63*7c478bd9Sstevel@tonic-gate 	int fd;
64*7c478bd9Sstevel@tonic-gate 	struct nd_hostserv ns;
65*7c478bd9Sstevel@tonic-gate 	struct nd_addrlist *nas;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if ((net = __rpc_setconf(nettype)) == 0) {
68*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
69*7c478bd9Sstevel@tonic-gate 		gettext("svc_create: could not read netconfig database"));
70*7c478bd9Sstevel@tonic-gate 		return (0);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	while (nconf = __rpc_getconf(net)) {
73*7c478bd9Sstevel@tonic-gate 		if ((strcmp(nconf->nc_protofmly, NC_LOOPBACK)) ||
74*7c478bd9Sstevel@tonic-gate 				(nconf->nc_semantics != NC_TPI_COTS_ORD))
75*7c478bd9Sstevel@tonic-gate 			continue;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
78*7c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
79*7c478bd9Sstevel@tonic-gate 			gettext("svc_create: %s: cannot open connection: %s"),
80*7c478bd9Sstevel@tonic-gate 				nconf->nc_netid, t_errlist[t_errno]);
81*7c478bd9Sstevel@tonic-gate 			break;
82*7c478bd9Sstevel@tonic-gate 		}
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 		/*
85*7c478bd9Sstevel@tonic-gate 		 * Negotiate for returning the uid of the caller.
86*7c478bd9Sstevel@tonic-gate 		 * This should be done before enabling the endpoint for
87*7c478bd9Sstevel@tonic-gate 		 * service via t_bind() (called in svc_tli_create())
88*7c478bd9Sstevel@tonic-gate 		 * so that requests to gssd contain the uid.
89*7c478bd9Sstevel@tonic-gate 		 */
90*7c478bd9Sstevel@tonic-gate 		if (__rpc_negotiate_uid(fd) != 0) {
91*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
92*7c478bd9Sstevel@tonic-gate 			gettext("Could not negotiate for"
93*7c478bd9Sstevel@tonic-gate 				" uid with loopback transport %s"),
94*7c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
95*7c478bd9Sstevel@tonic-gate 			t_close(fd);
96*7c478bd9Sstevel@tonic-gate 			break;
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
100*7c478bd9Sstevel@tonic-gate 		bind_addr = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
101*7c478bd9Sstevel@tonic-gate 		if ((bind_addr == NULL)) {
102*7c478bd9Sstevel@tonic-gate 			(void) t_close(fd);
103*7c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
104*7c478bd9Sstevel@tonic-gate 				gettext("svc_create: t_alloc failed\n"));
105*7c478bd9Sstevel@tonic-gate 			break;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 		ns.h_host = HOST_SELF;
108*7c478bd9Sstevel@tonic-gate 		ns.h_serv = servname;
109*7c478bd9Sstevel@tonic-gate 		if (!netdir_getbyname(nconf, &ns, &nas)) {
110*7c478bd9Sstevel@tonic-gate 			/* Copy the address */
111*7c478bd9Sstevel@tonic-gate 			bind_addr->addr.len = nas->n_addrs->len;
112*7c478bd9Sstevel@tonic-gate 			(void) memcpy(bind_addr->addr.buf, nas->n_addrs->buf,
113*7c478bd9Sstevel@tonic-gate 				(int) nas->n_addrs->len);
114*7c478bd9Sstevel@tonic-gate 			bind_addr->qlen = 8;
115*7c478bd9Sstevel@tonic-gate 			netdir_free((char *) nas, ND_ADDRLIST);
116*7c478bd9Sstevel@tonic-gate 		} else {
117*7c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
118*7c478bd9Sstevel@tonic-gate 			gettext("svc_create: no well known "
119*7c478bd9Sstevel@tonic-gate 				"address for %s on %s\n"),
120*7c478bd9Sstevel@tonic-gate 				servname, nconf->nc_netid);
121*7c478bd9Sstevel@tonic-gate 			(void) t_free((char *) bind_addr, T_BIND);
122*7c478bd9Sstevel@tonic-gate 			bind_addr = NULL;
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 		xprt = svc_tli_create(fd, nconf, bind_addr, 0, 0);
126*7c478bd9Sstevel@tonic-gate 		if (bind_addr)
127*7c478bd9Sstevel@tonic-gate 			(void) t_free((char *) bind_addr, T_BIND);
128*7c478bd9Sstevel@tonic-gate 		if (xprt == NULL) {
129*7c478bd9Sstevel@tonic-gate 			(void) t_close(fd);
130*7c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
131*7c478bd9Sstevel@tonic-gate 			    gettext("svc_create: svc_tli_create failed\n"));
132*7c478bd9Sstevel@tonic-gate 			break;
133*7c478bd9Sstevel@tonic-gate 		} else {
134*7c478bd9Sstevel@tonic-gate 			(void) rpcb_unset(prognum, versnum, nconf);
135*7c478bd9Sstevel@tonic-gate 			if (svc_reg(xprt, prognum, versnum, dispatch, nconf)
136*7c478bd9Sstevel@tonic-gate 					== FALSE) {
137*7c478bd9Sstevel@tonic-gate 				(void) syslog(LOG_ERR,
138*7c478bd9Sstevel@tonic-gate 				gettext("svc_create: cannot"
139*7c478bd9Sstevel@tonic-gate 					" register %d vers %d on %s"),
140*7c478bd9Sstevel@tonic-gate 					prognum, versnum, nconf->nc_netid);
141*7c478bd9Sstevel@tonic-gate 				SVC_DESTROY(xprt);	/* also t_closes fd */
142*7c478bd9Sstevel@tonic-gate 				break;
143*7c478bd9Sstevel@tonic-gate 			}
144*7c478bd9Sstevel@tonic-gate 			num = 1;
145*7c478bd9Sstevel@tonic-gate 			break;
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	__rpc_endconf(net);
149*7c478bd9Sstevel@tonic-gate 	return (num);
150*7c478bd9Sstevel@tonic-gate }
151