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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2000 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#ifndef lint
static char sccsid[] = "@(#)rpc.ypupdated.c 1.9 87/10/30 Copyr 1986 Sun Micro";
#endif
/*
* YP update service
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <rpc/rpc.h>
#include <rpc/nettype.h>
#include <rpcsvc/ypupd.h>
#include <rpcsvc/ypclnt.h>
#include <netdir.h>
#include <stropts.h>
#ifdef SYSLOG
#include <syslog.h>
#else
#define LOG_ERR 1
#define openlog(a, b, c)
#endif
#ifdef DEBUG
#define RPC_SVC_FG
#define debug(msg) fprintf(stderr, "%s\n", msg);
#else
#define debug(msg) /* turn off debugging */
#endif
static char YPDIR[] = "/var/yp";
static char UPDATEFILE[] = "/var/yp/updaters";
#define _RPCSVC_CLOSEDOWN 120
static int addr2netname();
static void closedown();
static void ypupdate_prog();
static void msgout();
static int update();
static int insecure;
static int _rpcpmstart; /* Started by a port monitor ? */
static int _rpcsvcdirty; /* Still serving ? */
extern unsigned int alarm();
extern void exit();
extern int close();
extern long fork();
extern int free();
extern struct netconfig *getnetconfigent();
extern int strcmp();
extern int strcpy();
extern int syslog();
extern void *signal();
extern int setsid();
extern int t_getinfo();
extern int user2netname();
extern int _openchild();
main(argc, argv)
int argc;
char *argv[];
{
pid_t pid;
char *cmd;
char mname[FMNAMESZ + 1];
if (geteuid() != 0) {
(void) fprintf(stderr, "must be root to run %s\n", argv[0]);
exit(1);
}
cmd = argv[0];
switch (argc) {
case 0:
cmd = "ypupdated";
break;
case 1:
break;
case 2:
if (strcmp(argv[1], "-i") == 0) {
insecure++;
break;
}
default:
fprintf(stderr, "%s: warning -- options ignored\n", cmd);
break;
}
if (chdir(YPDIR) < 0) {
fprintf(stderr, "%s: can't chdir to ", cmd);
perror(YPDIR);
exit(1);
}
if (!ioctl(0, I_LOOK, mname) &&
(strcmp(mname, "sockmod") == 0 ||
strcmp(mname, "timod") == 0)) {
/*
* Started from port monitor: use 0 as fd
*/
char *netid;
struct netconfig *nconf = NULL;
SVCXPRT *transp;
int pmclose;
extern char *getenv();
_rpcpmstart = 1;
if ((netid = getenv("NLSPROVIDER")) == NULL) {
msgout("cannot get transport name");
}
if ((nconf = getnetconfigent(netid)) == NULL) {
msgout("cannot get transport info");
}
if (strcmp(mname, "sockmod") == 0) {
if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
msgout("could not get the right module");
exit(1);
}
}
pmclose = (t_getstate(0) != T_DATAXFER);
if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
msgout("cannot create update server handle");
exit(1);
}
if (!svc_reg(transp, YPU_PROG, YPU_VERS, ypupdate_prog, 0)) {
msgout("unable to register (YPBINDPROG, YPBINDVERS).");
exit(1);
}
if (nconf)
freenetconfigent(nconf);
if (pmclose) {
(void) signal(SIGALRM, closedown);
(void) alarm(_RPCSVC_CLOSEDOWN);
}
svc_run();
exit(1);
}
#ifndef RPC_SVC_FG
/*
* Started from shell; background thyself and run
*/
pid = fork();
if (pid < 0) {
perror("cannot fork");
exit(1);
}
if (pid)
exit(0);
closefrom(0);
(void) setsid();
openlog("ypupdated", LOG_PID, LOG_DAEMON);
#endif
if (!svc_create(ypupdate_prog, YPU_PROG, YPU_VERS, "netpath")) {
msgout("unable to create (YPU_PROG, YPU_VERS) for netpath.");
exit(1);
}
svc_run();
msgout("svc_run returned");
exit(1);
/* NOTREACHED */
}
static void
ypupdate_prog(rqstp, transp)
struct svc_req *rqstp;
SVCXPRT *transp;
{
struct ypupdate_args args;
uint_t rslt;
uint_t op;
char *netname;
char namebuf[MAXNETNAMELEN+1];
struct authunix_parms *aup;
switch (rqstp->rq_proc) {
case NULLPROC:
svc_sendreply(transp, xdr_void, NULL);
return;
case YPU_CHANGE:
op = YPOP_CHANGE;
break;
case YPU_DELETE:
op = YPOP_DELETE;
break;
case YPU_INSERT:
op = YPOP_INSERT;
break;
case YPU_STORE:
op = YPOP_STORE;
break;
default:
svcerr_noproc(transp);
return;
}
#ifdef DEBUG
fprintf(stderr, "ypupdated: request received\n");
#endif
switch (rqstp->rq_cred.oa_flavor) {
case AUTH_DES:
netname = ((struct authdes_cred *)
rqstp->rq_clntcred)->adc_fullname.name;
break;
case AUTH_UNIX:
if (insecure) {
aup = (struct authunix_parms *)rqstp->rq_clntcred;
if (aup->aup_uid == 0) {
if (addr2netname(namebuf, transp) != 0) {
fprintf(stderr,
"addr2netname failing for %d\n",
aup->aup_uid);
svcerr_systemerr(transp);
return;
}
} else {
if (user2netname(namebuf, aup->aup_uid, NULL)
!= 0) {
fprintf(stderr,
"user2netname failing for %d\n",
aup->aup_uid);
svcerr_systemerr(transp);
return;
}
}
netname = namebuf;
break;
}
default:
svcerr_weakauth(transp);
return;
}
memset(&args, 0, sizeof (args));
if (!svc_getargs(transp, xdr_ypupdate_args, (char *)&args)) {
svcerr_decode(transp);
return;
}
#ifdef DEBUG
fprintf(stderr, "netname = %s\n, map=%s\n key=%s\n",
netname, args.mapname, args.key.yp_buf_val);
#endif
rslt = update(netname, args.mapname, op,
args.key.yp_buf_len, args.key.yp_buf_val,
args.datum.yp_buf_len, args.datum.yp_buf_val);
if (!svc_sendreply(transp, xdr_u_int, (char *)&rslt)) {
debug("svc_sendreply failed");
}
if (!svc_freeargs(transp, xdr_ypupdate_args, (char *)&args)) {
debug("svc_freeargs failed");
}
}
/*
* Determine if requester is allowed to update the given map,
* and update it if so. Returns the yp status, which is zero
* if there is no access violation.
*/
static
update(requester, mapname, op, keylen, key, datalen, data)
char *requester;
char *mapname;
uint_t op;
uint_t keylen;
char *key;
uint_t datalen;
char *data;
{
char updater[MAXMAPNAMELEN + 40];
FILE *childargs;
FILE *childrslt;
int status;
int yperrno = 0;
int pid;
sprintf(updater, "/usr/ccs/bin/make -s -f %s %s", UPDATEFILE, mapname);
#ifdef DEBUG
fprintf(stderr, "updater: %s\n", updater);
fprintf(stderr, "requestor = %s, op = %d, key = %s\n",
requester, op, key);
fprintf(stderr, "data = %s\n", data);
#endif
pid = _openchild(updater, &childargs, &childrslt);
if (pid < 0) {
debug("openpipes failed");
return (YPERR_YPERR);
}
/*
* Write to child
*/
fprintf(childargs, "%s\n", requester);
fprintf(childargs, "%u\n", op);
fprintf(childargs, "%u\n", keylen);
fwrite(key, keylen, 1, childargs);
fprintf(childargs, "\n");
fprintf(childargs, "%u\n", datalen);
fwrite(data, datalen, 1, childargs);
fprintf(childargs, "\n");
fclose(childargs);
/*
* Read from child
*/
fscanf(childrslt, "%d", &yperrno);
fclose(childrslt);
wait(&status);
if (!WIFEXITED(status)) {
return (YPERR_YPERR);
}
return (yperrno);
}
static void
msgout(msg)
char *msg;
{
if (_rpcpmstart)
syslog(LOG_ERR, msg);
else
(void) fprintf(stderr, "%s\n", msg);
}
void
closedown()
{
if (_rpcsvcdirty == 0) {
int i, openfd;
struct t_info tinfo;
if (t_getinfo(0, tinfo) || (tinfo.servtype == T_CLTS))
exit(0);
for (i = 0, openfd = 0; i < svc_max_pollfd && openfd < 2; i++)
if (svc_pollfd[i].fd >= 0)
openfd++;
if (openfd <= 1)
exit(0);
}
(void) alarm(_RPCSVC_CLOSEDOWN);
}
static int
addr2netname(namebuf, transp)
char *namebuf;
SVCXPRT *transp;
{
struct nd_hostservlist *hostservs = NULL;
struct netconfig *nconf;
struct netbuf *who;
who = svc_getrpccaller(transp);
if ((who == NULL) || (who->len == 0))
return (-1);
if ((nconf = getnetconfigent(transp->xp_netid))
== (struct netconfig *)NULL)
return (-1);
if (netdir_getbyaddr(nconf, &hostservs, who) != 0) {
(void) freenetconfigent(nconf);
return (-1);
}
if (hostservs == NULL) {
msgout("ypupdated: netdir_getbyaddr failed\n");
} else {
strcpy(namebuf, hostservs->h_hostservs->h_host);
}
(void) freenetconfigent(nconf);
netdir_free((char *)hostservs, ND_HOSTSERVLIST);
return (0);
}
|