10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate *
22*702Sth160488 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate * under license from the Regents of the University of
320Sstevel@tonic-gate * California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * YP updater for public key map
390Sstevel@tonic-gate */
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <rpc/rpc.h>
420Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate extern char *malloc();
460Sstevel@tonic-gate
47*702Sth160488 int
main(argc,argv)480Sstevel@tonic-gate main(argc, argv)
490Sstevel@tonic-gate int argc;
500Sstevel@tonic-gate char *argv[];
510Sstevel@tonic-gate {
520Sstevel@tonic-gate unsigned op;
530Sstevel@tonic-gate char name[MAXNETNAMELEN + 1];
540Sstevel@tonic-gate char key[256];
550Sstevel@tonic-gate char data[256];
560Sstevel@tonic-gate char line[256];
570Sstevel@tonic-gate unsigned keylen;
580Sstevel@tonic-gate unsigned datalen;
590Sstevel@tonic-gate FILE *rf;
600Sstevel@tonic-gate FILE *wf;
610Sstevel@tonic-gate char *fname;
620Sstevel@tonic-gate char *tmpname;
630Sstevel@tonic-gate int err;
640Sstevel@tonic-gate
650Sstevel@tonic-gate
660Sstevel@tonic-gate if (argc != 3) {
670Sstevel@tonic-gate exit(YPERR_YPERR);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate fname = argv[1];
700Sstevel@tonic-gate tmpname = malloc(strlen(fname) + 4);
710Sstevel@tonic-gate if (tmpname == NULL) {
720Sstevel@tonic-gate exit(YPERR_YPERR);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate sprintf(tmpname, "%s.tmp", fname);
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * Get input
780Sstevel@tonic-gate */
790Sstevel@tonic-gate if (! scanf("%s\n", name)) {
800Sstevel@tonic-gate exit(YPERR_YPERR);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate if (! scanf("%u\n", &op)) {
830Sstevel@tonic-gate exit(YPERR_YPERR);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate if (! scanf("%u\n", &keylen)) {
860Sstevel@tonic-gate exit(YPERR_YPERR);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate if (! fread(key, keylen, 1, stdin)) {
890Sstevel@tonic-gate exit(YPERR_YPERR);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate key[keylen] = 0;
920Sstevel@tonic-gate if (! scanf("%u\n", &datalen)) {
930Sstevel@tonic-gate exit(YPERR_YPERR);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate if (! fread(data, datalen, 1, stdin)) {
960Sstevel@tonic-gate exit(YPERR_YPERR);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate data[datalen] = 0;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * Check permission
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate if (strcmp(name, key) != 0) {
1040Sstevel@tonic-gate exit(YPERR_ACCESS);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate if (strcmp(name, "nobody") == 0) {
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * Can't change "nobody"s key.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate exit(YPERR_ACCESS);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * Open files
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate rf = fopen(fname, "r");
1170Sstevel@tonic-gate if (rf == NULL) {
1180Sstevel@tonic-gate exit(YPERR_YPERR);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate wf = fopen(tmpname, "w");
1210Sstevel@tonic-gate if (wf == NULL) {
1220Sstevel@tonic-gate exit(YPERR_YPERR);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate err = -1;
1250Sstevel@tonic-gate while (fgets(line, sizeof (line), rf)) {
1260Sstevel@tonic-gate if (err < 0 && match(line, name)) {
1270Sstevel@tonic-gate switch (op) {
1280Sstevel@tonic-gate case YPOP_INSERT:
1290Sstevel@tonic-gate err = YPERR_KEY;
1300Sstevel@tonic-gate break;
1310Sstevel@tonic-gate case YPOP_STORE:
1320Sstevel@tonic-gate case YPOP_CHANGE:
1330Sstevel@tonic-gate fprintf(wf, "%s %s\n", key, data);
1340Sstevel@tonic-gate err = 0;
1350Sstevel@tonic-gate break;
1360Sstevel@tonic-gate case YPOP_DELETE:
1370Sstevel@tonic-gate /* do nothing */
1380Sstevel@tonic-gate err = 0;
1390Sstevel@tonic-gate break;
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate } else {
1420Sstevel@tonic-gate fputs(line, wf);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate if (err < 0) {
1460Sstevel@tonic-gate switch (op) {
1470Sstevel@tonic-gate case YPOP_CHANGE:
1480Sstevel@tonic-gate case YPOP_DELETE:
1490Sstevel@tonic-gate err = YPERR_KEY;
1500Sstevel@tonic-gate break;
1510Sstevel@tonic-gate case YPOP_INSERT:
1520Sstevel@tonic-gate case YPOP_STORE:
1530Sstevel@tonic-gate err = 0;
1540Sstevel@tonic-gate fprintf(wf, "%s %s\n", key, data);
1550Sstevel@tonic-gate break;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate fclose(wf);
1590Sstevel@tonic-gate fclose(rf);
1600Sstevel@tonic-gate if (err == 0) {
1610Sstevel@tonic-gate if (rename(tmpname, fname) < 0) {
1620Sstevel@tonic-gate exit(YPERR_YPERR);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate } else {
1650Sstevel@tonic-gate if (unlink(tmpname) < 0) {
1660Sstevel@tonic-gate exit(YPERR_YPERR);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate if (fork() == 0) {
1700Sstevel@tonic-gate close(0); close(1); close(2);
1710Sstevel@tonic-gate open("/dev/null", O_RDWR, 0);
1720Sstevel@tonic-gate dup(0); dup(0);
1730Sstevel@tonic-gate execl("/bin/sh", "sh", "-c", argv[2], NULL);
1740Sstevel@tonic-gate }
175*702Sth160488 return (err);
1760Sstevel@tonic-gate /* NOTREACHED */
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate
180*702Sth160488 int
match(line,name)1810Sstevel@tonic-gate match(line, name)
1820Sstevel@tonic-gate char *line;
1830Sstevel@tonic-gate char *name;
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate int len;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate len = strlen(name);
1880Sstevel@tonic-gate return (strncmp(line, name, len) == 0 &&
1890Sstevel@tonic-gate (line[len] == ' ' || line[len] == '\t'));
1900Sstevel@tonic-gate }
191