xref: /onnv-gate/usr/src/cmd/gss/gssd/gssd_generic.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1988-1997, by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <rpc/rpc.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate #include <syslog.h>
34*0Sstevel@tonic-gate #include <rpc/nettype.h>
35*0Sstevel@tonic-gate #include <netconfig.h>
36*0Sstevel@tonic-gate #include <netdir.h>
37*0Sstevel@tonic-gate #include <tiuser.h>
38*0Sstevel@tonic-gate #include <fcntl.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <rpc/svc.h>
41*0Sstevel@tonic-gate #include <locale.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate extern int __rpc_negotiate_uid(int);
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * The highest level interface for server creation.
47*0Sstevel@tonic-gate  * Copied from svc_generic.c and cmd/keyserv/key_generic.c, but adapted
48*0Sstevel@tonic-gate  * to work only for TPI_CLTS semantics, and to be called only once
49*0Sstevel@tonic-gate  * from gssd.c. Returns 1 (interface created) on success and 0
50*0Sstevel@tonic-gate  * (no interfaces created) on failure.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate int
53*0Sstevel@tonic-gate svc_create_local_service(dispatch, prognum, versnum, nettype, servname)
54*0Sstevel@tonic-gate void (*dispatch) ();		/* Dispatch function */
55*0Sstevel@tonic-gate u_long prognum;			/* Program number */
56*0Sstevel@tonic-gate u_long versnum;			/* Version number */
57*0Sstevel@tonic-gate char *nettype;			/* Networktype token */
58*0Sstevel@tonic-gate char *servname;			/* name of the service */
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	int num = 0;
61*0Sstevel@tonic-gate 	SVCXPRT *xprt;
62*0Sstevel@tonic-gate 	struct netconfig *nconf;
63*0Sstevel@tonic-gate 	struct t_bind *bind_addr;
64*0Sstevel@tonic-gate 	void *net;
65*0Sstevel@tonic-gate 	int fd;
66*0Sstevel@tonic-gate 	struct nd_hostserv ns;
67*0Sstevel@tonic-gate 	struct nd_addrlist *nas;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if ((net = __rpc_setconf(nettype)) == 0) {
70*0Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
71*0Sstevel@tonic-gate 		gettext("svc_create: could not read netconfig database"));
72*0Sstevel@tonic-gate 		return (0);
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 	while (nconf = __rpc_getconf(net)) {
75*0Sstevel@tonic-gate 		if ((strcmp(nconf->nc_protofmly, NC_LOOPBACK)) ||
76*0Sstevel@tonic-gate 				(nconf->nc_semantics != NC_TPI_COTS_ORD))
77*0Sstevel@tonic-gate 			continue;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
80*0Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
81*0Sstevel@tonic-gate 			gettext("svc_create: %s: cannot open connection: %s"),
82*0Sstevel@tonic-gate 				nconf->nc_netid, t_errlist[t_errno]);
83*0Sstevel@tonic-gate 			break;
84*0Sstevel@tonic-gate 		}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 		/*
87*0Sstevel@tonic-gate 		 * Negotiate for returning the uid of the caller.
88*0Sstevel@tonic-gate 		 * This should be done before enabling the endpoint for
89*0Sstevel@tonic-gate 		 * service via t_bind() (called in svc_tli_create())
90*0Sstevel@tonic-gate 		 * so that requests to gssd contain the uid.
91*0Sstevel@tonic-gate 		 */
92*0Sstevel@tonic-gate 		if (__rpc_negotiate_uid(fd) != 0) {
93*0Sstevel@tonic-gate 			syslog(LOG_ERR,
94*0Sstevel@tonic-gate 			gettext("Could not negotiate for"
95*0Sstevel@tonic-gate 				" uid with loopback transport %s"),
96*0Sstevel@tonic-gate 				nconf->nc_netid);
97*0Sstevel@tonic-gate 			t_close(fd);
98*0Sstevel@tonic-gate 			break;
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		/* LINTED pointer alignment */
102*0Sstevel@tonic-gate 		bind_addr = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
103*0Sstevel@tonic-gate 		if ((bind_addr == NULL)) {
104*0Sstevel@tonic-gate 			(void) t_close(fd);
105*0Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
106*0Sstevel@tonic-gate 				gettext("svc_create: t_alloc failed\n"));
107*0Sstevel@tonic-gate 			break;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 		ns.h_host = HOST_SELF;
110*0Sstevel@tonic-gate 		ns.h_serv = servname;
111*0Sstevel@tonic-gate 		if (!netdir_getbyname(nconf, &ns, &nas)) {
112*0Sstevel@tonic-gate 			/* Copy the address */
113*0Sstevel@tonic-gate 			bind_addr->addr.len = nas->n_addrs->len;
114*0Sstevel@tonic-gate 			(void) memcpy(bind_addr->addr.buf, nas->n_addrs->buf,
115*0Sstevel@tonic-gate 				(int) nas->n_addrs->len);
116*0Sstevel@tonic-gate 			bind_addr->qlen = 8;
117*0Sstevel@tonic-gate 			netdir_free((char *) nas, ND_ADDRLIST);
118*0Sstevel@tonic-gate 		} else {
119*0Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
120*0Sstevel@tonic-gate 			gettext("svc_create: no well known "
121*0Sstevel@tonic-gate 				"address for %s on %s\n"),
122*0Sstevel@tonic-gate 				servname, nconf->nc_netid);
123*0Sstevel@tonic-gate 			(void) t_free((char *) bind_addr, T_BIND);
124*0Sstevel@tonic-gate 			bind_addr = NULL;
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		xprt = svc_tli_create(fd, nconf, bind_addr, 0, 0);
128*0Sstevel@tonic-gate 		if (bind_addr)
129*0Sstevel@tonic-gate 			(void) t_free((char *) bind_addr, T_BIND);
130*0Sstevel@tonic-gate 		if (xprt == NULL) {
131*0Sstevel@tonic-gate 			(void) t_close(fd);
132*0Sstevel@tonic-gate 			(void) syslog(LOG_ERR,
133*0Sstevel@tonic-gate 			    gettext("svc_create: svc_tli_create failed\n"));
134*0Sstevel@tonic-gate 			break;
135*0Sstevel@tonic-gate 		} else {
136*0Sstevel@tonic-gate 			(void) rpcb_unset(prognum, versnum, nconf);
137*0Sstevel@tonic-gate 			if (svc_reg(xprt, prognum, versnum, dispatch, nconf)
138*0Sstevel@tonic-gate 					== FALSE) {
139*0Sstevel@tonic-gate 				(void) syslog(LOG_ERR,
140*0Sstevel@tonic-gate 				gettext("svc_create: cannot"
141*0Sstevel@tonic-gate 					" register %d vers %d on %s"),
142*0Sstevel@tonic-gate 					prognum, versnum, nconf->nc_netid);
143*0Sstevel@tonic-gate 				SVC_DESTROY(xprt);	/* also t_closes fd */
144*0Sstevel@tonic-gate 				break;
145*0Sstevel@tonic-gate 			}
146*0Sstevel@tonic-gate 			num = 1;
147*0Sstevel@tonic-gate 			break;
148*0Sstevel@tonic-gate 		}
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 	__rpc_endconf(net);
151*0Sstevel@tonic-gate 	return (num);
152*0Sstevel@tonic-gate }
153