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