| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#include <pthread.h> |
| 45 |
#include <reentrant.h> |
| 46 |
#include <sys/types.h> |
| 47 |
#include <rpc/rpc.h> |
| 48 |
#include <rpc/nettype.h> |
| 49 |
#include <stdio.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
#include <err.h> |
| 53 |
|
| 54 |
#include "rpc_com.h" |
| 55 |
|
| 56 |
static void universal(struct svc_req *, SVCXPRT *); |
| 57 |
|
| 58 |
static struct proglst { |
| 59 |
char *(*p_progname)(char *); |
| 60 |
rpcprog_t p_prognum; |
| 61 |
rpcvers_t p_versnum; |
| 62 |
rpcproc_t p_procnum; |
| 63 |
SVCXPRT *p_transp; |
| 64 |
char *p_netid; |
| 65 |
char *p_xdrbuf; |
| 66 |
int p_recvsz; |
| 67 |
xdrproc_t p_inproc, p_outproc; |
| 68 |
struct proglst *p_nxt; |
| 69 |
} *proglst; |
| 70 |
|
| 71 |
static const char rpc_reg_err[] = "%s: %s"; |
| 72 |
static const char rpc_reg_msg[] = "rpc_reg: "; |
| 73 |
static const char __reg_err1[] = "can't find appropriate transport"; |
| 74 |
static const char __reg_err2[] = "can't get protocol info"; |
| 75 |
static const char __reg_err3[] = "unsupported transport size"; |
| 76 |
static const char __no_mem_str[] = "out of memory"; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
int |
| 90 |
rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype) |
| 91 |
rpcprog_t prognum; |
| 92 |
rpcvers_t versnum; |
| 93 |
rpcproc_t procnum; |
| 94 |
char *(*progname)(char *); |
| 95 |
xdrproc_t inproc, outproc; |
| 96 |
char *nettype; |
| 97 |
{ |
| 98 |
struct netconfig *nconf; |
| 99 |
int done = FALSE; |
| 100 |
void *handle; |
| 101 |
extern mutex_t proglst_lock; |
| 102 |
|
| 103 |
if (procnum == NULLPROC) { |
| 104 |
warnx("%s can't reassign procedure number %u", rpc_reg_msg, |
| 105 |
NULLPROC); |
| 106 |
return (-1); |
| 107 |
} |
| 108 |
|
| 109 |
if (nettype == NULL) |
| 110 |
nettype = "netpath"; |
| 111 |
if ((handle = __rpc_setconf(nettype)) == NULL) { |
| 112 |
warnx(rpc_reg_err, rpc_reg_msg, __reg_err1); |
| 113 |
return (-1); |
| 114 |
} |
| 115 |
|
| 116 |
mutex_lock(&proglst_lock); |
| 117 |
while ((nconf = __rpc_getconf(handle)) != NULL) { |
| 118 |
struct proglst *pl; |
| 119 |
SVCXPRT *svcxprt; |
| 120 |
int madenow; |
| 121 |
u_int recvsz; |
| 122 |
char *xdrbuf; |
| 123 |
char *netid; |
| 124 |
|
| 125 |
madenow = FALSE; |
| 126 |
svcxprt = NULL; |
| 127 |
recvsz = 0; |
| 128 |
xdrbuf = netid = NULL; |
| 129 |
for (pl = proglst; pl; pl = pl->p_nxt) { |
| 130 |
if (strcmp(pl->p_netid, nconf->nc_netid) == 0) { |
| 131 |
svcxprt = pl->p_transp; |
| 132 |
xdrbuf = pl->p_xdrbuf; |
| 133 |
recvsz = pl->p_recvsz; |
| 134 |
netid = pl->p_netid; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (svcxprt == NULL) { |
| 140 |
struct __rpc_sockinfo si; |
| 141 |
|
| 142 |
svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); |
| 143 |
if (svcxprt == NULL) |
| 144 |
continue; |
| 145 |
if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) { |
| 146 |
warnx(rpc_reg_err, rpc_reg_msg, __reg_err2); |
| 147 |
SVC_DESTROY(svcxprt); |
| 148 |
continue; |
| 149 |
} |
| 150 |
recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0); |
| 151 |
if (recvsz == 0) { |
| 152 |
warnx(rpc_reg_err, rpc_reg_msg, __reg_err3); |
| 153 |
SVC_DESTROY(svcxprt); |
| 154 |
continue; |
| 155 |
} |
| 156 |
if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) || |
| 157 |
((netid = strdup(nconf->nc_netid)) == NULL)) { |
| 158 |
warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); |
| 159 |
SVC_DESTROY(svcxprt); |
| 160 |
free(xdrbuf); |
| 161 |
break; |
| 162 |
} |
| 163 |
madenow = TRUE; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
for (pl = proglst; pl; pl = pl->p_nxt) |
| 170 |
if ((pl->p_prognum == prognum) && |
| 171 |
(pl->p_versnum == versnum) && |
| 172 |
(strcmp(pl->p_netid, netid) == 0)) |
| 173 |
break; |
| 174 |
if (pl == NULL) { |
| 175 |
(void) rpcb_unset(prognum, versnum, nconf); |
| 176 |
} else { |
| 177 |
|
| 178 |
nconf = NULL; |
| 179 |
} |
| 180 |
|
| 181 |
if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) { |
| 182 |
warnx("%s couldn't register prog %u vers %u for %s", |
| 183 |
rpc_reg_msg, (unsigned)prognum, |
| 184 |
(unsigned)versnum, netid); |
| 185 |
if (madenow) { |
| 186 |
SVC_DESTROY(svcxprt); |
| 187 |
free(xdrbuf); |
| 188 |
free(netid); |
| 189 |
} |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
pl = malloc(sizeof (struct proglst)); |
| 194 |
if (pl == NULL) { |
| 195 |
warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); |
| 196 |
if (madenow) { |
| 197 |
SVC_DESTROY(svcxprt); |
| 198 |
free(xdrbuf); |
| 199 |
free(netid); |
| 200 |
} |
| 201 |
break; |
| 202 |
} |
| 203 |
pl->p_progname = progname; |
| 204 |
pl->p_prognum = prognum; |
| 205 |
pl->p_versnum = versnum; |
| 206 |
pl->p_procnum = procnum; |
| 207 |
pl->p_inproc = inproc; |
| 208 |
pl->p_outproc = outproc; |
| 209 |
pl->p_transp = svcxprt; |
| 210 |
pl->p_xdrbuf = xdrbuf; |
| 211 |
pl->p_recvsz = recvsz; |
| 212 |
pl->p_netid = netid; |
| 213 |
pl->p_nxt = proglst; |
| 214 |
proglst = pl; |
| 215 |
done = TRUE; |
| 216 |
} |
| 217 |
__rpc_endconf(handle); |
| 218 |
mutex_unlock(&proglst_lock); |
| 219 |
|
| 220 |
if (done == FALSE) { |
| 221 |
warnx("%s cant find suitable transport for %s", |
| 222 |
rpc_reg_msg, nettype); |
| 223 |
return (-1); |
| 224 |
} |
| 225 |
return (0); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
static void |
| 234 |
universal(rqstp, transp) |
| 235 |
struct svc_req *rqstp; |
| 236 |
SVCXPRT *transp; |
| 237 |
{ |
| 238 |
rpcprog_t prog; |
| 239 |
rpcvers_t vers; |
| 240 |
rpcproc_t proc; |
| 241 |
char *outdata; |
| 242 |
char *xdrbuf; |
| 243 |
struct proglst *pl; |
| 244 |
extern mutex_t proglst_lock; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
if (rqstp->rq_proc == NULLPROC) { |
| 250 |
if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) == |
| 251 |
FALSE) { |
| 252 |
warnx("svc_sendreply failed"); |
| 253 |
} |
| 254 |
return; |
| 255 |
} |
| 256 |
prog = rqstp->rq_prog; |
| 257 |
vers = rqstp->rq_vers; |
| 258 |
proc = rqstp->rq_proc; |
| 259 |
mutex_lock(&proglst_lock); |
| 260 |
for (pl = proglst; pl; pl = pl->p_nxt) |
| 261 |
if (pl->p_prognum == prog && pl->p_procnum == proc && |
| 262 |
pl->p_versnum == vers && |
| 263 |
(strcmp(pl->p_netid, transp->xp_netid) == 0)) { |
| 264 |
|
| 265 |
xdrbuf = pl->p_xdrbuf; |
| 266 |
|
| 267 |
(void) memset(xdrbuf, 0, sizeof (pl->p_recvsz)); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) { |
| 274 |
svcerr_decode(transp); |
| 275 |
mutex_unlock(&proglst_lock); |
| 276 |
return; |
| 277 |
} |
| 278 |
outdata = (*(pl->p_progname))(xdrbuf); |
| 279 |
if (outdata == NULL && |
| 280 |
pl->p_outproc != (xdrproc_t) xdr_void){ |
| 281 |
|
| 282 |
mutex_unlock(&proglst_lock); |
| 283 |
return; |
| 284 |
} |
| 285 |
if (!svc_sendreply(transp, pl->p_outproc, outdata)) { |
| 286 |
warnx( |
| 287 |
"rpc: rpc_reg trouble replying to prog %u vers %u", |
| 288 |
(unsigned)prog, (unsigned)vers); |
| 289 |
mutex_unlock(&proglst_lock); |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
(void)svc_freeargs(transp, pl->p_inproc, xdrbuf); |
| 294 |
mutex_unlock(&proglst_lock); |
| 295 |
return; |
| 296 |
} |
| 297 |
mutex_unlock(&proglst_lock); |
| 298 |
|
| 299 |
warnx("rpc: rpc_reg: never registered prog %u vers %u", |
| 300 |
(unsigned)prog, (unsigned)vers); |
| 301 |
return; |
| 302 |
} |