1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate * 22*7c478bd9Sstevel@tonic-gate * Copyright 1992 Sun Microsystems, Inc. All rights reserved. 23*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*7c478bd9Sstevel@tonic-gate */ 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 31*7c478bd9Sstevel@tonic-gate * under license from the Regents of the University of 32*7c478bd9Sstevel@tonic-gate * California. 33*7c478bd9Sstevel@tonic-gate */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate /* 38*7c478bd9Sstevel@tonic-gate * YP updater for public key map 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate #include <stdio.h> 41*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 42*7c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate extern char *malloc(); 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate main(argc, argv) 48*7c478bd9Sstevel@tonic-gate int argc; 49*7c478bd9Sstevel@tonic-gate char *argv[]; 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate unsigned op; 52*7c478bd9Sstevel@tonic-gate char name[MAXNETNAMELEN + 1]; 53*7c478bd9Sstevel@tonic-gate char key[256]; 54*7c478bd9Sstevel@tonic-gate char data[256]; 55*7c478bd9Sstevel@tonic-gate char line[256]; 56*7c478bd9Sstevel@tonic-gate unsigned keylen; 57*7c478bd9Sstevel@tonic-gate unsigned datalen; 58*7c478bd9Sstevel@tonic-gate FILE *rf; 59*7c478bd9Sstevel@tonic-gate FILE *wf; 60*7c478bd9Sstevel@tonic-gate char *fname; 61*7c478bd9Sstevel@tonic-gate char *tmpname; 62*7c478bd9Sstevel@tonic-gate int err; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate if (argc != 3) { 66*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 67*7c478bd9Sstevel@tonic-gate } 68*7c478bd9Sstevel@tonic-gate fname = argv[1]; 69*7c478bd9Sstevel@tonic-gate tmpname = malloc(strlen(fname) + 4); 70*7c478bd9Sstevel@tonic-gate if (tmpname == NULL) { 71*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 72*7c478bd9Sstevel@tonic-gate } 73*7c478bd9Sstevel@tonic-gate sprintf(tmpname, "%s.tmp", fname); 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* 76*7c478bd9Sstevel@tonic-gate * Get input 77*7c478bd9Sstevel@tonic-gate */ 78*7c478bd9Sstevel@tonic-gate if (! scanf("%s\n", name)) { 79*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate if (! scanf("%u\n", &op)) { 82*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate if (! scanf("%u\n", &keylen)) { 85*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 86*7c478bd9Sstevel@tonic-gate } 87*7c478bd9Sstevel@tonic-gate if (! fread(key, keylen, 1, stdin)) { 88*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate key[keylen] = 0; 91*7c478bd9Sstevel@tonic-gate if (! scanf("%u\n", &datalen)) { 92*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate if (! fread(data, datalen, 1, stdin)) { 95*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate data[datalen] = 0; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * Check permission 101*7c478bd9Sstevel@tonic-gate */ 102*7c478bd9Sstevel@tonic-gate if (strcmp(name, key) != 0) { 103*7c478bd9Sstevel@tonic-gate exit(YPERR_ACCESS); 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate if (strcmp(name, "nobody") == 0) { 106*7c478bd9Sstevel@tonic-gate /* 107*7c478bd9Sstevel@tonic-gate * Can't change "nobody"s key. 108*7c478bd9Sstevel@tonic-gate */ 109*7c478bd9Sstevel@tonic-gate exit(YPERR_ACCESS); 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate /* 113*7c478bd9Sstevel@tonic-gate * Open files 114*7c478bd9Sstevel@tonic-gate */ 115*7c478bd9Sstevel@tonic-gate rf = fopen(fname, "r"); 116*7c478bd9Sstevel@tonic-gate if (rf == NULL) { 117*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate wf = fopen(tmpname, "w"); 120*7c478bd9Sstevel@tonic-gate if (wf == NULL) { 121*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate err = -1; 124*7c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), rf)) { 125*7c478bd9Sstevel@tonic-gate if (err < 0 && match(line, name)) { 126*7c478bd9Sstevel@tonic-gate switch (op) { 127*7c478bd9Sstevel@tonic-gate case YPOP_INSERT: 128*7c478bd9Sstevel@tonic-gate err = YPERR_KEY; 129*7c478bd9Sstevel@tonic-gate break; 130*7c478bd9Sstevel@tonic-gate case YPOP_STORE: 131*7c478bd9Sstevel@tonic-gate case YPOP_CHANGE: 132*7c478bd9Sstevel@tonic-gate fprintf(wf, "%s %s\n", key, data); 133*7c478bd9Sstevel@tonic-gate err = 0; 134*7c478bd9Sstevel@tonic-gate break; 135*7c478bd9Sstevel@tonic-gate case YPOP_DELETE: 136*7c478bd9Sstevel@tonic-gate /* do nothing */ 137*7c478bd9Sstevel@tonic-gate err = 0; 138*7c478bd9Sstevel@tonic-gate break; 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate } else { 141*7c478bd9Sstevel@tonic-gate fputs(line, wf); 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate if (err < 0) { 145*7c478bd9Sstevel@tonic-gate switch (op) { 146*7c478bd9Sstevel@tonic-gate case YPOP_CHANGE: 147*7c478bd9Sstevel@tonic-gate case YPOP_DELETE: 148*7c478bd9Sstevel@tonic-gate err = YPERR_KEY; 149*7c478bd9Sstevel@tonic-gate break; 150*7c478bd9Sstevel@tonic-gate case YPOP_INSERT: 151*7c478bd9Sstevel@tonic-gate case YPOP_STORE: 152*7c478bd9Sstevel@tonic-gate err = 0; 153*7c478bd9Sstevel@tonic-gate fprintf(wf, "%s %s\n", key, data); 154*7c478bd9Sstevel@tonic-gate break; 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate fclose(wf); 158*7c478bd9Sstevel@tonic-gate fclose(rf); 159*7c478bd9Sstevel@tonic-gate if (err == 0) { 160*7c478bd9Sstevel@tonic-gate if (rename(tmpname, fname) < 0) { 161*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate } else { 164*7c478bd9Sstevel@tonic-gate if (unlink(tmpname) < 0) { 165*7c478bd9Sstevel@tonic-gate exit(YPERR_YPERR); 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate if (fork() == 0) { 169*7c478bd9Sstevel@tonic-gate close(0); close(1); close(2); 170*7c478bd9Sstevel@tonic-gate open("/dev/null", O_RDWR, 0); 171*7c478bd9Sstevel@tonic-gate dup(0); dup(0); 172*7c478bd9Sstevel@tonic-gate execl("/bin/sh", "sh", "-c", argv[2], NULL); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate exit(err); 175*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate match(line, name) 180*7c478bd9Sstevel@tonic-gate char *line; 181*7c478bd9Sstevel@tonic-gate char *name; 182*7c478bd9Sstevel@tonic-gate { 183*7c478bd9Sstevel@tonic-gate int len; 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate len = strlen(name); 186*7c478bd9Sstevel@tonic-gate return (strncmp(line, name, len) == 0 && 187*7c478bd9Sstevel@tonic-gate (line[len] == ' ' || line[len] == '\t')); 188*7c478bd9Sstevel@tonic-gate } 189