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