dsynth - Make dummy /usr/packages directory for pkg compatibility
[dragonfly.git] / usr.bin / newkey / update.c
blob116a53e8464cb05a4dfa61f892c44c967f272ccf
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 * @(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro
31 * $FreeBSD: src/usr.bin/newkey/update.c,v 1.5 1999/08/28 01:04:35 peter Exp $
35 * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
39 * Administrative tool to add a new user to the publickey database
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <sys/resource.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include <rpc/rpc.h>
51 #include <rpc/key_prot.h>
52 #ifdef YP
53 #include <rpcsvc/yp_prot.h>
54 #include <rpcsvc/ypclnt.h>
55 #include <sys/wait.h>
56 #include <netdb.h>
57 #endif /* YP */
59 #include "externs.h"
61 #ifdef YP
62 #define MAXMAPNAMELEN 256
63 #else
64 #define YPOP_CHANGE 1 /* change, do not add */
65 #define YPOP_INSERT 2 /* add, do not change */
66 #define YPOP_DELETE 3 /* delete this entry */
67 #define YPOP_STORE 4 /* add, or change */
68 #endif
70 #ifdef YP
71 static char *basename(char *);
72 static char SHELL[] = "/bin/sh";
73 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */
74 static char UPDATEFILE[] = "updaters";
75 #endif /* YP */
77 #ifdef YP
78 static int _openchild(char *, FILE **, FILE ** );
81 * Determine if requester is allowed to update the given map,
82 * and update it if so. Returns the yp status, which is zero
83 * if there is no access violation.
85 int
86 mapupdate(char *requester, char *mapname, u_int op, u_int keylen, char *key,
87 u_int datalen, char *data)
89 char updater[MAXMAPNAMELEN + 40];
90 FILE *childargs;
91 FILE *childrslt;
92 #ifdef WEXITSTATUS
93 int status;
94 #else
95 union wait status;
96 #endif
97 pid_t pid;
98 u_int yperrno;
101 #ifdef DEBUG
102 printf("%s %s\n", key, data);
103 #endif
104 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
105 UPDATEFILE, mapname);
106 pid = _openchild(updater, &childargs, &childrslt);
107 if (pid < 0) {
108 return (YPERR_YPERR);
112 * Write to child
114 (void)fprintf(childargs, "%s\n", requester);
115 (void)fprintf(childargs, "%u\n", op);
116 (void)fprintf(childargs, "%u\n", keylen);
117 (void)fwrite(key, (int)keylen, 1, childargs);
118 (void)fprintf(childargs, "\n");
119 (void)fprintf(childargs, "%u\n", datalen);
120 (void)fwrite(data, (int)datalen, 1, childargs);
121 (void)fprintf(childargs, "\n");
122 (void)fclose(childargs);
125 * Read from child
127 (void)fscanf(childrslt, "%d", &yperrno);
128 (void)fclose(childrslt);
130 (void)wait(&status);
131 #ifdef WEXITSTATUS
132 if (WEXITSTATUS(status) != 0) {
133 #else
134 if (status.w_retcode != 0) {
135 #endif
136 return (YPERR_YPERR);
138 return (yperrno);
142 * returns pid, or -1 for failure
144 static int
145 _openchild(char *command, FILE **fto, FILE **ffrom)
147 int i;
148 pid_t pid;
149 int pdto[2];
150 int pdfrom[2];
151 char *com;
152 struct rlimit rl;
154 if (pipe(pdto) < 0) {
155 goto error1;
157 if (pipe(pdfrom) < 0) {
158 goto error2;
160 switch (pid = fork()) {
161 case -1:
162 goto error3;
164 case 0:
166 * child: read from pdto[0], write into pdfrom[1]
168 (void)close(0);
169 (void)dup(pdto[0]);
170 (void)close(1);
171 (void)dup(pdfrom[1]);
172 getrlimit(RLIMIT_NOFILE, &rl);
173 for (i = rl.rlim_max - 1; i >= 3; i--) {
174 (void) close(i);
176 com = malloc((unsigned) strlen(command) + 6);
177 if (com == NULL) {
178 _exit(~0);
180 (void)sprintf(com, "exec %s", command);
181 execl(SHELL, basename(SHELL), "-c", com, NULL);
182 _exit(~0);
184 default:
186 * parent: write into pdto[1], read from pdfrom[0]
188 *fto = fdopen(pdto[1], "w");
189 (void)close(pdto[0]);
190 *ffrom = fdopen(pdfrom[0], "r");
191 (void)close(pdfrom[1]);
192 break;
194 return (pid);
197 * error cleanup and return
199 error3:
200 (void)close(pdfrom[0]);
201 (void)close(pdfrom[1]);
202 error2:
203 (void)close(pdto[0]);
204 (void)close(pdto[1]);
205 error1:
206 return (-1);
209 static char *
210 basename(char *path)
212 char *p;
214 p = strrchr(path, '/');
215 if (p == NULL) {
216 return (path);
217 } else {
218 return (p + 1);
222 #else /* YP */
224 #define ERR_ACCESS 1
225 #define ERR_MALLOC 2
226 #define ERR_READ 3
227 #define ERR_WRITE 4
228 #define ERR_DBASE 5
229 #define ERR_KEY 6
231 static int match( char * , char * );
234 * Determine if requester is allowed to update the given map,
235 * and update it if so. Returns the status, which is zero
236 * if there is no access violation. This function updates
237 * the local file and then shuts up.
240 localupdate(char *name, char *filename, u_int op, char *key, char *data)
242 char line[256];
243 FILE *rf;
244 FILE *wf;
245 char *tmpname;
246 int err;
249 * Check permission
251 if (strcmp(name, key) != 0) {
252 return (ERR_ACCESS);
254 if (strcmp(name, "nobody") == 0) {
256 * Can't change "nobody"s key.
258 return (ERR_ACCESS);
262 * Open files
264 tmpname = malloc(strlen(filename) + 4);
265 if (tmpname == NULL) {
266 return (ERR_MALLOC);
268 sprintf(tmpname, "%s.tmp", filename);
269 rf = fopen(filename, "r");
270 if (rf == NULL) {
271 return (ERR_READ);
273 wf = fopen(tmpname, "w");
274 if (wf == NULL) {
275 return (ERR_WRITE);
277 err = -1;
278 while (fgets(line, sizeof (line), rf)) {
279 if (err < 0 && match(line, name)) {
280 switch (op) {
281 case YPOP_INSERT:
282 err = ERR_KEY;
283 break;
284 case YPOP_STORE:
285 case YPOP_CHANGE:
286 fprintf(wf, "%s %s\n", key, data);
287 err = 0;
288 break;
289 case YPOP_DELETE:
290 /* do nothing */
291 err = 0;
292 break;
294 } else {
295 fputs(line, wf);
298 if (err < 0) {
299 switch (op) {
300 case YPOP_CHANGE:
301 case YPOP_DELETE:
302 err = ERR_KEY;
303 break;
304 case YPOP_INSERT:
305 case YPOP_STORE:
306 err = 0;
307 fprintf(wf, "%s %s\n", key, data);
308 break;
311 fclose(wf);
312 fclose(rf);
313 if (err == 0) {
314 if (rename(tmpname, filename) < 0) {
315 return (ERR_DBASE);
317 } else {
318 if (unlink(tmpname) < 0) {
319 return (ERR_DBASE);
322 return (err);
325 static int
326 match(char *line, char *name)
328 int len;
330 len = strlen(name);
331 return (strncmp(line, name, len) == 0 &&
332 (line[len] == ' ' || line[len] == '\t'));
334 #endif /* !YP */