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