8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / ypcmd / udpublickey.c
blobc83ac46c76447943614e9012ef8be7504006365f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
32 * California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * YP updater for public key map
40 #include <stdio.h>
41 #include <rpc/rpc.h>
42 #include <rpcsvc/ypclnt.h>
43 #include <sys/file.h>
45 extern char *malloc();
47 int
48 main(argc, argv)
49 int argc;
50 char *argv[];
52 unsigned op;
53 char name[MAXNETNAMELEN + 1];
54 char key[256];
55 char data[256];
56 char line[256];
57 unsigned keylen;
58 unsigned datalen;
59 FILE *rf;
60 FILE *wf;
61 char *fname;
62 char *tmpname;
63 int err;
66 if (argc != 3) {
67 exit(YPERR_YPERR);
69 fname = argv[1];
70 tmpname = malloc(strlen(fname) + 4);
71 if (tmpname == NULL) {
72 exit(YPERR_YPERR);
74 sprintf(tmpname, "%s.tmp", fname);
77 * Get input
79 if (! scanf("%s\n", name)) {
80 exit(YPERR_YPERR);
82 if (! scanf("%u\n", &op)) {
83 exit(YPERR_YPERR);
85 if (! scanf("%u\n", &keylen)) {
86 exit(YPERR_YPERR);
88 if (! fread(key, keylen, 1, stdin)) {
89 exit(YPERR_YPERR);
91 key[keylen] = 0;
92 if (! scanf("%u\n", &datalen)) {
93 exit(YPERR_YPERR);
95 if (! fread(data, datalen, 1, stdin)) {
96 exit(YPERR_YPERR);
98 data[datalen] = 0;
101 * Check permission
103 if (strcmp(name, key) != 0) {
104 exit(YPERR_ACCESS);
106 if (strcmp(name, "nobody") == 0) {
108 * Can't change "nobody"s key.
110 exit(YPERR_ACCESS);
114 * Open files
116 rf = fopen(fname, "r");
117 if (rf == NULL) {
118 exit(YPERR_YPERR);
120 wf = fopen(tmpname, "w");
121 if (wf == NULL) {
122 exit(YPERR_YPERR);
124 err = -1;
125 while (fgets(line, sizeof (line), rf)) {
126 if (err < 0 && match(line, name)) {
127 switch (op) {
128 case YPOP_INSERT:
129 err = YPERR_KEY;
130 break;
131 case YPOP_STORE:
132 case YPOP_CHANGE:
133 fprintf(wf, "%s %s\n", key, data);
134 err = 0;
135 break;
136 case YPOP_DELETE:
137 /* do nothing */
138 err = 0;
139 break;
141 } else {
142 fputs(line, wf);
145 if (err < 0) {
146 switch (op) {
147 case YPOP_CHANGE:
148 case YPOP_DELETE:
149 err = YPERR_KEY;
150 break;
151 case YPOP_INSERT:
152 case YPOP_STORE:
153 err = 0;
154 fprintf(wf, "%s %s\n", key, data);
155 break;
158 fclose(wf);
159 fclose(rf);
160 if (err == 0) {
161 if (rename(tmpname, fname) < 0) {
162 exit(YPERR_YPERR);
164 } else {
165 if (unlink(tmpname) < 0) {
166 exit(YPERR_YPERR);
169 if (fork() == 0) {
170 close(0); close(1); close(2);
171 open("/dev/null", O_RDWR, 0);
172 dup(0); dup(0);
173 execl("/bin/sh", "sh", "-c", argv[2], NULL);
175 return (err);
176 /* NOTREACHED */
181 match(line, name)
182 char *line;
183 char *name;
185 int len;
187 len = strlen(name);
188 return (strncmp(line, name, len) == 0 &&
189 (line[len] == ' ' || line[len] == '\t'));