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