1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* findbe.c --
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-06-09 19:13:06 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/findbe.c,v 1.9 1998/06/09 17:13:05 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
1997-08-27 05:48:50 +02:00
|
|
|
#include <sys/stat.h>
|
1996-11-06 11:32:10 +01:00
|
|
|
#include <unistd.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-11-06 11:32:10 +01:00
|
|
|
#include "postgres.h"
|
1997-09-07 07:04:48 +02:00
|
|
|
#include "miscadmin.h" /* for DebugLvl */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef S_IRUSR /* XXX [TRH] should be in a header */
|
|
|
|
#define S_IRUSR S_IREAD
|
|
|
|
#define S_IWUSR S_IWRITE
|
|
|
|
#define S_IXUSR S_IEXEC
|
|
|
|
#define S_IRGRP ((S_IRUSR)>>3)
|
|
|
|
#define S_IWGRP ((S_IWUSR)>>3)
|
|
|
|
#define S_IXGRP ((S_IXUSR)>>3)
|
|
|
|
#define S_IROTH ((S_IRUSR)>>6)
|
|
|
|
#define S_IWOTH ((S_IWUSR)>>6)
|
|
|
|
#define S_IXOTH ((S_IXUSR)>>6)
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1998-06-09 19:13:06 +02:00
|
|
|
* ValidateBinary -- validate "path" as a POSTMASTER/POSTGRES executable file
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* returns 0 if the file is found and no error is encountered.
|
1997-09-07 07:04:48 +02:00
|
|
|
* -1 if the regular file "path" does not exist or cannot be executed.
|
|
|
|
* -2 if the file is otherwise valid but cannot be read.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
1998-06-09 19:13:06 +02:00
|
|
|
ValidateBinary(char *path)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
struct stat buf;
|
|
|
|
uid_t euid;
|
|
|
|
struct group *gp;
|
|
|
|
struct passwd *pwp;
|
|
|
|
int i;
|
|
|
|
int is_r = 0;
|
|
|
|
int is_x = 0;
|
|
|
|
int in_grp = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* Ensure that the file exists and is a regular file.
|
|
|
|
*
|
|
|
|
* XXX if you have a broken system where stat() looks at the symlink
|
|
|
|
* instead of the underlying file, you lose.
|
|
|
|
*/
|
|
|
|
if (strlen(path) >= MAXPGPATH)
|
|
|
|
{
|
|
|
|
if (DebugLvl > 1)
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "ValidateBinary: pathname \"%s\" is too long\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
path);
|
|
|
|
return (-1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (stat(path, &buf) < 0)
|
|
|
|
{
|
|
|
|
if (DebugLvl > 1)
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "ValidateBinary: can't stat \"%s\"\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
path);
|
|
|
|
return (-1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!(buf.st_mode & S_IFREG))
|
|
|
|
{
|
|
|
|
if (DebugLvl > 1)
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "ValidateBinary: \"%s\" is not a regular file\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
path);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that we are using an authorized backend.
|
|
|
|
*
|
|
|
|
* XXX I'm open to suggestions here. I would like to enforce ownership
|
1998-06-09 19:13:06 +02:00
|
|
|
* of binaries by user "postgres" but people seem to like to run as
|
1997-09-07 07:04:48 +02:00
|
|
|
* users other than "postgres"...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the file is both executable and readable (required for
|
|
|
|
* dynamic loading).
|
|
|
|
*
|
|
|
|
* We use the effective uid here because the backend will not have
|
|
|
|
* executed setuid() by the time it calls this routine.
|
|
|
|
*/
|
|
|
|
euid = geteuid();
|
|
|
|
if (euid == buf.st_uid)
|
|
|
|
{
|
|
|
|
is_r = buf.st_mode & S_IRUSR;
|
|
|
|
is_x = buf.st_mode & S_IXUSR;
|
|
|
|
if (DebugLvl > 1 && !(is_r && is_x))
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "ValidateBinary: \"%s\" is not user read/execute\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
path);
|
|
|
|
return (is_x ? (is_r ? 0 : -2) : -1);
|
|
|
|
}
|
|
|
|
pwp = getpwuid(euid);
|
|
|
|
if (pwp)
|
|
|
|
{
|
|
|
|
if (pwp->pw_gid == buf.st_gid)
|
|
|
|
{
|
|
|
|
++in_grp;
|
|
|
|
}
|
|
|
|
else if (pwp->pw_name &&
|
|
|
|
(gp = getgrgid(buf.st_gid)))
|
|
|
|
{
|
|
|
|
for (i = 0; gp->gr_mem[i]; ++i)
|
|
|
|
{
|
|
|
|
if (!strcmp(gp->gr_mem[i], pwp->pw_name))
|
|
|
|
{
|
|
|
|
++in_grp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in_grp)
|
|
|
|
{
|
|
|
|
is_r = buf.st_mode & S_IRGRP;
|
|
|
|
is_x = buf.st_mode & S_IXGRP;
|
|
|
|
if (DebugLvl > 1 && !(is_r && is_x))
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "ValidateBinary: \"%s\" is not group read/execute\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
path);
|
|
|
|
return (is_x ? (is_r ? 0 : -2) : -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is_r = buf.st_mode & S_IROTH;
|
|
|
|
is_x = buf.st_mode & S_IXOTH;
|
|
|
|
if (DebugLvl > 1 && !(is_r && is_x))
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "ValidateBinary: \"%s\" is not other read/execute\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
path);
|
|
|
|
return (is_x ? (is_r ? 0 : -2) : -1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-06-09 00:28:30 +02:00
|
|
|
* FindExec -- find an absolute path to a valid backend executable
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* The reason we have to work so hard to find an absolute path is that
|
1998-06-09 19:13:06 +02:00
|
|
|
* we need to feed the binary the location of its actual executable file,
|
|
|
|
* otherwise, we can't do dynamic loading. It needs a full pathname because
|
|
|
|
* we change directories to the /data directory.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
1998-06-09 19:13:06 +02:00
|
|
|
FindExec(char *full_path, char *argv0, char *binary_name)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char buf[MAXPGPATH + 2];
|
|
|
|
char *p;
|
|
|
|
char *path,
|
|
|
|
*startp,
|
|
|
|
*endp;
|
|
|
|
int pathlen;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
1998-06-09 19:13:06 +02:00
|
|
|
* for the postmaster: First try: use the binary that's located in
|
1997-09-07 07:04:48 +02:00
|
|
|
* the same directory as the postmaster, if it was invoked with an
|
|
|
|
* explicit path. Presumably the user used an explicit path because it
|
|
|
|
* wasn't in PATH, and we don't want to use incompatible executables.
|
|
|
|
*
|
|
|
|
* This has the neat property that it works for installed binaries, old
|
|
|
|
* source trees (obj/support/post{master,gres}) and new marc source
|
|
|
|
* trees (obj/post{master,gres}) because they all put the two binaries
|
|
|
|
* in the same place.
|
|
|
|
*
|
1998-06-09 19:13:06 +02:00
|
|
|
* for the binary: First try: if we're given some kind of path,
|
1997-09-07 07:04:48 +02:00
|
|
|
* use it (making sure that a relative path is made absolute before
|
|
|
|
* returning it).
|
|
|
|
*/
|
|
|
|
if (argv0 && (p = strrchr(argv0, '/')) && *++p)
|
|
|
|
{
|
|
|
|
if (*argv0 == '/' || !getcwd(buf, MAXPGPATH))
|
|
|
|
buf[0] = '\0';
|
|
|
|
else
|
|
|
|
strcat(buf, "/");
|
|
|
|
strcat(buf, argv0);
|
|
|
|
p = strrchr(buf, '/');
|
1998-06-09 19:13:06 +02:00
|
|
|
strcpy(++p, binary_name);
|
|
|
|
if (!ValidateBinary(buf))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-06-09 19:13:06 +02:00
|
|
|
strncpy(full_path, buf, MAXPGPATH);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (DebugLvl)
|
1998-06-09 00:28:30 +02:00
|
|
|
fprintf(stderr, "FindExec: found \"%s\" using argv[0]\n",
|
1998-06-09 19:13:06 +02:00
|
|
|
full_path);
|
1997-09-07 07:04:48 +02:00
|
|
|
return (0);
|
|
|
|
}
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "FindExec: invalid binary \"%s\"\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
buf);
|
|
|
|
return (-1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Second try: since no explicit path was supplied, the user must have
|
|
|
|
* been relying on PATH. We'll use the same PATH.
|
|
|
|
*/
|
|
|
|
if ((p = getenv("PATH")) && *p)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
if (DebugLvl)
|
1998-06-09 00:28:30 +02:00
|
|
|
fprintf(stderr, "FindExec: searching PATH ...\n");
|
1997-09-07 07:04:48 +02:00
|
|
|
pathlen = strlen(p);
|
|
|
|
path = malloc(pathlen + 1);
|
|
|
|
strcpy(path, p);
|
|
|
|
for (startp = path, endp = strchr(path, ':');
|
|
|
|
startp && *startp;
|
|
|
|
startp = endp + 1, endp = strchr(startp, ':'))
|
|
|
|
{
|
|
|
|
if (startp == endp) /* it's a "::" */
|
|
|
|
continue;
|
|
|
|
if (endp)
|
|
|
|
*endp = '\0';
|
|
|
|
if (*startp == '/' || !getcwd(buf, MAXPGPATH))
|
|
|
|
buf[0] = '\0';
|
|
|
|
strcat(buf, startp);
|
1998-06-09 19:13:06 +02:00
|
|
|
strcat(buf, "/");
|
|
|
|
strcat(buf, binary_name);
|
|
|
|
switch (ValidateBinary(buf))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 0: /* found ok */
|
1998-06-09 19:13:06 +02:00
|
|
|
strncpy(full_path, buf, MAXPGPATH);
|
1997-09-08 04:41:22 +02:00
|
|
|
if (DebugLvl)
|
1998-06-09 00:28:30 +02:00
|
|
|
fprintf(stderr, "FindExec: found \"%s\" using PATH\n",
|
1998-06-09 19:13:06 +02:00
|
|
|
full_path);
|
1997-09-08 04:41:22 +02:00
|
|
|
free(path);
|
|
|
|
return (0);
|
|
|
|
case -1: /* wasn't even a candidate, keep looking */
|
|
|
|
break;
|
|
|
|
case -2: /* found but disqualified */
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "FindExec: could not read binary \"%s\"\n",
|
1997-09-08 04:41:22 +02:00
|
|
|
buf);
|
|
|
|
free(path);
|
|
|
|
return (-1);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (!endp) /* last one */
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
1998-06-09 19:13:06 +02:00
|
|
|
fprintf(stderr, "FindExec: could not find a %s to execute...\n", binary_name);
|
1997-09-07 07:04:48 +02:00
|
|
|
return (-1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|