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