1997-05-28 15:52:07 +00:00
|
|
|
/*
|
|
|
|
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
|
|
|
|
* unrestricted use provided that this legend is included on all tape
|
|
|
|
* media and as a part of the software program in whole or part. Users
|
|
|
|
* may copy or modify Sun RPC without charge, but are not authorized
|
|
|
|
* to license or distribute it to anyone else except as part of a product or
|
|
|
|
* program developed by the user or with the express written consent of
|
|
|
|
* Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
|
|
|
|
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
|
|
|
|
*
|
|
|
|
* Sun RPC is provided with no support and without any obligation on the
|
|
|
|
* part of Sun Microsystems, Inc. to assist in its use, correction,
|
|
|
|
* modification or enhancement.
|
|
|
|
*
|
|
|
|
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
|
|
|
|
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
|
|
|
|
* OR ANY PART THEREOF.
|
|
|
|
*
|
|
|
|
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
|
|
|
|
* or profits or other special, indirect and consequential damages, even if
|
|
|
|
* Sun has been advised of the possibility of such damages.
|
|
|
|
*
|
|
|
|
* Sun Microsystems, Inc.
|
|
|
|
* 2550 Garcia Avenue
|
|
|
|
* Mountain View, California 94043
|
|
|
|
*/
|
2002-04-28 12:09:33 +00:00
|
|
|
|
1997-05-28 15:52:07 +00:00
|
|
|
#ifndef lint
|
1997-07-29 06:54:16 +00:00
|
|
|
#if 0
|
1997-05-28 15:52:07 +00:00
|
|
|
static char sccsid[] = "@(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro";
|
|
|
|
#endif
|
1997-07-29 06:54:16 +00:00
|
|
|
#endif
|
1997-05-28 15:52:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1986, 1989, Sun Microsystems, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Administrative tool to add a new user to the publickey database
|
|
|
|
*/
|
2002-04-28 12:09:33 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-12-02 12:17:20 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2002-04-28 12:09:33 +00:00
|
|
|
|
1997-05-28 15:52:07 +00:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/key_prot.h>
|
2002-04-28 12:09:33 +00:00
|
|
|
|
1997-05-28 15:52:07 +00:00
|
|
|
#ifdef YP
|
2001-12-02 12:17:20 +00:00
|
|
|
#include <sys/wait.h>
|
1997-05-28 15:52:07 +00:00
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif /* YP */
|
2002-04-28 12:09:33 +00:00
|
|
|
|
|
|
|
#include <pwd.h>
|
2001-12-02 12:17:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-05-28 15:52:07 +00:00
|
|
|
#include <string.h>
|
1997-07-29 06:54:16 +00:00
|
|
|
#include <unistd.h>
|
1997-05-28 15:52:07 +00:00
|
|
|
|
2001-12-02 12:17:20 +00:00
|
|
|
#include "extern.h"
|
1997-05-28 15:52:07 +00:00
|
|
|
|
|
|
|
#ifdef YP
|
|
|
|
static char SHELL[] = "/bin/sh";
|
|
|
|
static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */
|
|
|
|
static char UPDATEFILE[] = "updaters";
|
|
|
|
#else
|
|
|
|
static char PKFILE[] = "/etc/publickey";
|
|
|
|
#endif /* YP */
|
|
|
|
|
|
|
|
#ifdef YP
|
2002-03-22 01:33:25 +00:00
|
|
|
static int _openchild(char *, FILE **, FILE **);
|
|
|
|
static char *basename(char *path);
|
1997-05-28 15:52:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if requester is allowed to update the given map,
|
|
|
|
* and update it if so. Returns the yp status, which is zero
|
|
|
|
* if there is no access violation.
|
|
|
|
*/
|
2001-12-02 12:17:20 +00:00
|
|
|
int
|
2002-04-28 12:09:33 +00:00
|
|
|
mapupdate(char *requester, char *mapname, u_int op, u_int keylen,
|
|
|
|
char *key, u_int datalen, char *data)
|
1997-05-28 15:52:07 +00:00
|
|
|
{
|
|
|
|
char updater[MAXMAPNAMELEN + 40];
|
|
|
|
FILE *childargs;
|
|
|
|
FILE *childrslt;
|
|
|
|
#ifdef WEXITSTATUS
|
|
|
|
int status;
|
|
|
|
#else
|
|
|
|
union wait status;
|
|
|
|
#endif
|
|
|
|
pid_t pid;
|
|
|
|
u_int yperrno;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("%s %s\n", key, data);
|
|
|
|
#endif
|
|
|
|
(void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
|
|
|
|
UPDATEFILE, mapname);
|
|
|
|
pid = _openchild(updater, &childargs, &childrslt);
|
|
|
|
if (pid < 0) {
|
|
|
|
return (YPERR_YPERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write to child
|
|
|
|
*/
|
|
|
|
(void)fprintf(childargs, "%s\n", requester);
|
|
|
|
(void)fprintf(childargs, "%u\n", op);
|
|
|
|
(void)fprintf(childargs, "%u\n", keylen);
|
|
|
|
(void)fwrite(key, (int)keylen, 1, childargs);
|
|
|
|
(void)fprintf(childargs, "\n");
|
|
|
|
(void)fprintf(childargs, "%u\n", datalen);
|
|
|
|
(void)fwrite(data, (int)datalen, 1, childargs);
|
|
|
|
(void)fprintf(childargs, "\n");
|
|
|
|
(void)fclose(childargs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read from child
|
|
|
|
*/
|
|
|
|
(void)fscanf(childrslt, "%d", &yperrno);
|
|
|
|
(void)fclose(childrslt);
|
|
|
|
|
|
|
|
(void)wait(&status);
|
|
|
|
#ifdef WEXITSTATUS
|
|
|
|
if (WEXITSTATUS(status) != 0) {
|
|
|
|
#else
|
|
|
|
if (status.w_retcode != 0) {
|
|
|
|
#endif
|
|
|
|
return (YPERR_YPERR);
|
|
|
|
}
|
|
|
|
return (yperrno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* returns pid, or -1 for failure
|
|
|
|
*/
|
2001-12-02 12:17:20 +00:00
|
|
|
static pid_t
|
2002-04-28 12:09:33 +00:00
|
|
|
_openchild(char *command, FILE **fto, FILE **ffrom)
|
1997-05-28 15:52:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
pid_t pid;
|
|
|
|
int pdto[2];
|
|
|
|
int pdfrom[2];
|
|
|
|
char *com;
|
|
|
|
struct rlimit rl;
|
|
|
|
|
|
|
|
if (pipe(pdto) < 0) {
|
|
|
|
goto error1;
|
|
|
|
}
|
|
|
|
if (pipe(pdfrom) < 0) {
|
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
goto error3;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* child: read from pdto[0], write into pdfrom[1]
|
|
|
|
*/
|
|
|
|
(void)close(0);
|
|
|
|
(void)dup(pdto[0]);
|
|
|
|
(void)close(1);
|
|
|
|
(void)dup(pdfrom[1]);
|
|
|
|
getrlimit(RLIMIT_NOFILE, &rl);
|
|
|
|
for (i = rl.rlim_max - 1; i >= 3; i--) {
|
|
|
|
(void) close(i);
|
|
|
|
}
|
|
|
|
com = malloc((unsigned) strlen(command) + 6);
|
|
|
|
if (com == NULL) {
|
|
|
|
_exit(~0);
|
|
|
|
}
|
|
|
|
(void)sprintf(com, "exec %s", command);
|
2001-07-09 09:24:06 +00:00
|
|
|
execl(SHELL, basename(SHELL), "-c", com, (char *)NULL);
|
1997-05-28 15:52:07 +00:00
|
|
|
_exit(~0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* parent: write into pdto[1], read from pdfrom[0]
|
|
|
|
*/
|
|
|
|
*fto = fdopen(pdto[1], "w");
|
|
|
|
(void)close(pdto[0]);
|
|
|
|
*ffrom = fdopen(pdfrom[0], "r");
|
|
|
|
(void)close(pdfrom[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (pid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* error cleanup and return
|
|
|
|
*/
|
|
|
|
error3:
|
|
|
|
(void)close(pdfrom[0]);
|
|
|
|
(void)close(pdfrom[1]);
|
|
|
|
error2:
|
|
|
|
(void)close(pdto[0]);
|
|
|
|
(void)close(pdto[1]);
|
|
|
|
error1:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2002-04-28 12:09:33 +00:00
|
|
|
basename(char *path)
|
1997-05-28 15:52:07 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = strrchr(path, '/');
|
|
|
|
if (p == NULL) {
|
|
|
|
return (path);
|
|
|
|
} else {
|
|
|
|
return (p + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* YP */
|
|
|
|
|
|
|
|
#define ERR_ACCESS 1
|
|
|
|
#define ERR_MALLOC 2
|
|
|
|
#define ERR_READ 3
|
|
|
|
#define ERR_WRITE 4
|
|
|
|
#define ERR_DBASE 5
|
|
|
|
#define ERR_KEY 6
|
|
|
|
|
2002-04-28 12:09:33 +00:00
|
|
|
static int match(char *, char *);
|
1997-05-28 15:52:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if requester is allowed to update the given map,
|
|
|
|
* and update it if so. Returns the status, which is zero
|
|
|
|
* if there is no access violation. This function updates
|
|
|
|
* the local file and then shuts up.
|
|
|
|
*/
|
2002-04-28 12:09:33 +00:00
|
|
|
localupdate(char *name, char *filename, u_int op, u_int keylen,
|
|
|
|
char *key, u_int datalen, char *data)
|
1997-05-28 15:52:07 +00:00
|
|
|
{
|
|
|
|
char line[256];
|
|
|
|
FILE *rf;
|
|
|
|
FILE *wf;
|
|
|
|
char *tmpname;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check permission
|
|
|
|
*/
|
|
|
|
if (strcmp(name, key) != 0) {
|
|
|
|
return (ERR_ACCESS);
|
|
|
|
}
|
|
|
|
if (strcmp(name, "nobody") == 0) {
|
|
|
|
/*
|
|
|
|
* Can't change "nobody"s key.
|
|
|
|
*/
|
|
|
|
return (ERR_ACCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open files
|
|
|
|
*/
|
|
|
|
tmpname = malloc(strlen(filename) + 4);
|
|
|
|
if (tmpname == NULL) {
|
|
|
|
return (ERR_MALLOC);
|
|
|
|
}
|
|
|
|
sprintf(tmpname, "%s.tmp", filename);
|
|
|
|
rf = fopen(filename, "r");
|
|
|
|
if (rf == NULL) {
|
|
|
|
return (ERR_READ);
|
|
|
|
}
|
|
|
|
wf = fopen(tmpname, "w");
|
|
|
|
if (wf == NULL) {
|
|
|
|
return (ERR_WRITE);
|
|
|
|
}
|
|
|
|
err = -1;
|
|
|
|
while (fgets(line, sizeof (line), rf)) {
|
|
|
|
if (err < 0 && match(line, name)) {
|
|
|
|
switch (op) {
|
|
|
|
case YPOP_INSERT:
|
|
|
|
err = ERR_KEY;
|
|
|
|
break;
|
|
|
|
case YPOP_STORE:
|
|
|
|
case YPOP_CHANGE:
|
|
|
|
fprintf(wf, "%s %s\n", key, data);
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
case YPOP_DELETE:
|
|
|
|
/* do nothing */
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fputs(line, wf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err < 0) {
|
|
|
|
switch (op) {
|
|
|
|
case YPOP_CHANGE:
|
|
|
|
case YPOP_DELETE:
|
|
|
|
err = ERR_KEY;
|
|
|
|
break;
|
|
|
|
case YPOP_INSERT:
|
|
|
|
case YPOP_STORE:
|
|
|
|
err = 0;
|
|
|
|
fprintf(wf, "%s %s\n", key, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(wf);
|
|
|
|
fclose(rf);
|
|
|
|
if (err == 0) {
|
|
|
|
if (rename(tmpname, filename) < 0) {
|
|
|
|
return (ERR_DBASE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink(tmpname) < 0) {
|
|
|
|
return (ERR_DBASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2002-04-28 12:09:33 +00:00
|
|
|
match(char *line, char *name)
|
1997-05-28 15:52:07 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
return (strncmp(line, name, len) == 0 &&
|
|
|
|
(line[len] == ' ' || line[len] == '\t'));
|
|
|
|
}
|
|
|
|
#endif /* !YP */
|