exec.c 11.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * exec.c
4
 *
Bruce Momjian's avatar
Bruce Momjian committed
5
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
6
 * Portions Copyright (c) 1994, Regents of the University of California
7 8 9
 *
 *
 * IDENTIFICATION
10
 *	  $PostgreSQL: pgsql/src/port/exec.c,v 1.27 2004/09/27 22:11:23 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14 15

#ifndef FRONTEND
16
#include "postgres.h"
17 18 19
#else
#include "postgres_fe.h"
#endif
20

21 22
#include <grp.h>
#include <pwd.h>
Bruce Momjian's avatar
Bruce Momjian committed
23
#include <sys/stat.h>
24
#include <sys/wait.h>
25
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
26
#include <unistd.h>
27
#endif
28

29
#include "miscadmin.h"
30

31
#define _(x) gettext(x)
32

33 34 35 36 37 38 39 40 41 42
#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)
43 44
#endif

45 46 47 48 49 50 51
#ifndef FRONTEND
/* We use only 3-parameter elog calls in this file, for simplicity */
#define log_error(str, param)	elog(LOG, (str), (param))
#else
#define log_error(str, param)	fprintf(stderr, (str), (param))
#endif

52

53
static void win32_make_absolute(char *path);
54

55

56
/*
57
 * validate_exec -- validate "path" as an executable file
58 59
 *
 * returns 0 if the file is found and no error is encountered.
60 61
 *		  -1 if the regular file "path" does not exist or cannot be executed.
 *		  -2 if the file is otherwise valid but cannot be read.
62
 */
63
static int
64
validate_exec(const char *path)
65
{
66
	struct stat buf;
Bruce Momjian's avatar
Bruce Momjian committed
67

68
#ifndef WIN32
69 70 71
	uid_t		euid;
	struct group *gp;
	struct passwd *pwp;
72 73
	int			i;
	int			in_grp = 0;
Bruce Momjian's avatar
Bruce Momjian committed
74

75
#else
76
	char		path_exe[MAXPGPATH + sizeof(".exe") - 1];
77
#endif
78 79
	int			is_r = 0;
	int			is_x = 0;
80

81 82
#ifdef WIN32
	/* Win32 requires a .exe suffix for stat() */
83 84
	if (strlen(path) >= strlen(".exe") &&
		pg_strcasecmp(path + strlen(path) - strlen(".exe"), ".exe") != 0)
85 86 87 88 89 90 91
	{
		strcpy(path_exe, path);
		strcat(path_exe, ".exe");
		path = path_exe;
	}
#endif

92 93 94 95 96 97 98
	/*
	 * 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 (stat(path, &buf) < 0)
99
		return -1;
100 101

	if ((buf.st_mode & S_IFMT) != S_IFREG)
102
		return -1;
103 104

	/*
105
	 * Ensure that we are using an authorized executable.
106 107 108 109 110 111
	 */

	/*
	 * Ensure that the file is both executable and readable (required for
	 * dynamic loading).
	 */
112
#ifdef WIN32
Bruce Momjian's avatar
Bruce Momjian committed
113 114 115
	is_r = buf.st_mode & S_IRUSR;
	is_x = buf.st_mode & S_IXUSR;
	return is_x ? (is_r ? 0 : -2) : -1;
116
#else
117
	euid = geteuid();
118 119

	/* If owned by us, just check owner bits */
120 121 122 123
	if (euid == buf.st_uid)
	{
		is_r = buf.st_mode & S_IRUSR;
		is_x = buf.st_mode & S_IXUSR;
124
		return is_x ? (is_r ? 0 : -2) : -1;
125
	}
126 127

	/* OK, check group bits */
Bruce Momjian's avatar
Bruce Momjian committed
128 129

	pwp = getpwuid(euid);		/* not thread-safe */
130 131
	if (pwp)
	{
132
		if (pwp->pw_gid == buf.st_gid)	/* my primary group? */
133 134
			++in_grp;
		else if (pwp->pw_name &&
135
				 (gp = getgrgid(buf.st_gid)) != NULL && /* not thread-safe */
136
				 gp->gr_mem != NULL)
Bruce Momjian's avatar
Bruce Momjian committed
137
		{						/* try list of member groups */
138 139 140 141 142 143 144 145 146 147 148 149 150
			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;
151
			return is_x ? (is_r ? 0 : -2) : -1;
152 153
		}
	}
154 155

	/* Check "other" bits */
156 157
	is_r = buf.st_mode & S_IROTH;
	is_x = buf.st_mode & S_IXOTH;
158
	return is_x ? (is_r ? 0 : -2) : -1;
159
#endif
160 161 162
}

/*
163
 * find_my_exec -- find an absolute path to a valid executable
164 165
 *
 * The reason we have to work so hard to find an absolute path is that
166 167 168
 * on some platforms we can't do dynamic loading unless we know the
 * executable's location.  Also, we need a full path not a relative
 * path because we will later change working directory.
169 170
 *
 * This function is not thread-safe because of it calls validate_exec(),
Bruce Momjian's avatar
Bruce Momjian committed
171
 * which calls getgrgid().	This function should be used only in
172
 * non-threaded binaries, not in library routines.
173 174
 */
int
175
find_my_exec(const char *argv0, char *retpath)
176
{
Bruce Momjian's avatar
Bruce Momjian committed
177 178 179
	char		cwd[MAXPGPATH],
				test_path[MAXPGPATH];
	char	   *path;
180

181
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
182
	if (!getcwd(cwd, MAXPGPATH))
183 184 185
#else
	if (!GetCurrentDirectory(MAXPGPATH, cwd))
#endif
186 187
		cwd[0] = '\0';

188
	/*
Bruce Momjian's avatar
Bruce Momjian committed
189 190 191 192
	 * First try: use the binary that's located in the same directory 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.
193
	 *
194
	 * For the binary: First try: if we're given some kind of path, use it
195 196
	 * (making sure that a relative path is made absolute before returning
	 * it).
197
	 */
198
	/* Does argv0 have a separator? */
199
	if ((path = last_dir_separator(argv0)))
200
	{
201
		if (*++path == '\0')
202
		{
203
			log_error("argv[0] ends with a path separator \"%s\"", argv0);
204 205
			return -1;
		}
206 207

		if (is_absolute_path(argv0))
208
			StrNCpy(retpath, argv0, MAXPGPATH);
209
		else
210
			snprintf(retpath, MAXPGPATH, "%s/%s", cwd, argv0);
Bruce Momjian's avatar
Bruce Momjian committed
211

212 213
		canonicalize_path(retpath);
		if (validate_exec(retpath) == 0)
214
		{
215
			win32_make_absolute(retpath);
216
			return 0;
217
		}
218 219
		else
		{
220
			log_error("invalid binary \"%s\"", retpath);
221 222
			return -1;
		}
223
	}
224

225 226 227 228
#ifdef WIN32
	/* Win32 checks the current directory first for names without slashes */
	if (validate_exec(argv0) == 0)
	{
229 230
		snprintf(retpath, MAXPGPATH, "%s/%s", cwd, argv0);
		win32_make_absolute(retpath);
231 232 233 234
		return 0;
	}
#endif

235 236 237 238
	/*
	 * Second try: since no explicit path was supplied, the user must have
	 * been relying on PATH.  We'll use the same PATH.
	 */
239
	if ((path = getenv("PATH")) && *path)
240
	{
Bruce Momjian's avatar
Bruce Momjian committed
241 242
		char	   *startp = NULL,
				   *endp = NULL;
243 244

		do
245
		{
246 247 248 249
			if (!startp)
				startp = path;
			else
				startp = endp + 1;
250

251
			endp = first_path_separator(startp);
252
			if (!endp)
Bruce Momjian's avatar
Bruce Momjian committed
253
				endp = startp + strlen(startp); /* point to end */
254 255 256 257 258

			StrNCpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));

			if (is_absolute_path(test_path))
				snprintf(retpath, MAXPGPATH, "%s/%s", test_path, argv0);
259
			else
260
				snprintf(retpath, MAXPGPATH, "%s/%s/%s", cwd, test_path, argv0);
261

262 263
			canonicalize_path(retpath);
			switch (validate_exec(retpath))
264
			{
265
				case 0: /* found ok */
266
					win32_make_absolute(retpath);
267
					return 0;
268
				case -1:		/* wasn't even a candidate, keep looking */
269
					continue;
270
				case -2:		/* found but disqualified */
271
					log_error("could not read binary \"%s\"", retpath);
272
					continue;
273
			}
274
		} while (*endp);
275 276
	}

277
	log_error("could not find a \"%s\" to execute", argv0);
278
	return -1;
279 280

#if 0
Bruce Momjian's avatar
Bruce Momjian committed
281

282
	/*
Bruce Momjian's avatar
Bruce Momjian committed
283 284
	 * Win32 has a native way to find the executable name, but the above
	 * method works too.
285
	 */
286
	if (GetModuleFileName(NULL, retpath, MAXPGPATH) == 0)
Bruce Momjian's avatar
Bruce Momjian committed
287
		log_error("GetModuleFileName failed (%i)", (int) GetLastError());
288
#endif
289
}
290

291 292 293 294 295 296 297 298
/*
 * The runtime librarys popen() on win32 does not work when being
 * called from a service when running on windows <= 2000, because
 * there is no stdin/stdout/stderr.
 *
 * Executing a command in a pipe and reading the first line from it
 * is all we need.
 */
Bruce Momjian's avatar
Bruce Momjian committed
299 300 301

static char *
pipe_read_line(char *cmd, char *line, int maxsize)
302 303
{
#ifndef WIN32
Bruce Momjian's avatar
Bruce Momjian committed
304
	FILE	   *pgver;
305 306 307 308 309 310 311

	/* flush output buffers in case popen does not... */
	fflush(stdout);
	fflush(stderr);

	if ((pgver = popen(cmd, "r")) == NULL)
		return NULL;
Bruce Momjian's avatar
Bruce Momjian committed
312

313 314 315 316 317 318 319 320
	if (fgets(line, maxsize, pgver) == NULL)
	{
		perror("fgets failure");
		return NULL;
	}

	if (pclose_check(pgver))
		return NULL;
Bruce Momjian's avatar
Bruce Momjian committed
321

322 323 324 325
	return line;
#else
	/* Win32 */
	SECURITY_ATTRIBUTES sattr;
Bruce Momjian's avatar
Bruce Momjian committed
326 327 328
	HANDLE		childstdoutrd,
				childstdoutwr,
				childstdoutrddup;
329 330
	PROCESS_INFORMATION pi;
	STARTUPINFO si;
Bruce Momjian's avatar
Bruce Momjian committed
331
	char	   *retval = NULL;
332 333 334 335 336 337 338

	sattr.nLength = sizeof(SECURITY_ATTRIBUTES);
	sattr.bInheritHandle = TRUE;
	sattr.lpSecurityDescriptor = NULL;

	if (!CreatePipe(&childstdoutrd, &childstdoutwr, &sattr, 0))
		return NULL;
Bruce Momjian's avatar
Bruce Momjian committed
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353
	if (!DuplicateHandle(GetCurrentProcess(),
						 childstdoutrd,
						 GetCurrentProcess(),
						 &childstdoutrddup,
						 0,
						 FALSE,
						 DUPLICATE_SAME_ACCESS))
	{
		CloseHandle(childstdoutrd);
		CloseHandle(childstdoutwr);
		return NULL;
	}

	CloseHandle(childstdoutrd);
Bruce Momjian's avatar
Bruce Momjian committed
354 355 356

	ZeroMemory(&pi, sizeof(pi));
	ZeroMemory(&si, sizeof(si));
357 358 359 360 361
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESTDHANDLES;
	si.hStdError = childstdoutwr;
	si.hStdOutput = childstdoutwr;
	si.hStdInput = INVALID_HANDLE_VALUE;
Bruce Momjian's avatar
Bruce Momjian committed
362

363 364 365 366 367 368 369 370 371 372 373
	if (CreateProcess(NULL,
					  cmd,
					  NULL,
					  NULL,
					  TRUE,
					  0,
					  NULL,
					  NULL,
					  &si,
					  &pi))
	{
Bruce Momjian's avatar
Bruce Momjian committed
374 375
		DWORD		bytesread = 0;

376 377
		/* Successfully started the process */

Bruce Momjian's avatar
Bruce Momjian committed
378 379
		ZeroMemory(line, maxsize);

380
		/* Let's see if we can read */
Bruce Momjian's avatar
Bruce Momjian committed
381
		if (WaitForSingleObject(childstdoutrddup, 10000) != WAIT_OBJECT_0)
382 383 384 385 386 387 388 389
		{
			/* Got timeout */
			CloseHandle(pi.hProcess);
			CloseHandle(pi.hThread);
			CloseHandle(childstdoutwr);
			CloseHandle(childstdoutrddup);
			return NULL;
		}
390

391 392 393 394 395
		/* We try just once */
		if (ReadFile(childstdoutrddup, line, maxsize, &bytesread, NULL) &&
			bytesread > 0)
		{
			/* So we read some data */
Bruce Momjian's avatar
Bruce Momjian committed
396
			int			len = strlen(line);
397
			retval = line;
398

399
			/*
Bruce Momjian's avatar
Bruce Momjian committed
400 401 402 403 404
			 * If EOL is \r\n, convert to just \n. Because stdout is a
			 * text-mode stream, the \n output by the child process is
			 * received as \r\n, so we convert it to \n.  The server
			 * main.c sets setvbuf(stdout, NULL, _IONBF, 0) which has the
			 * effect of disabling \n to \r\n expansion for stdout.
405
			 */
Bruce Momjian's avatar
Bruce Momjian committed
406
			if (len >= 2 && line[len - 2] == '\r' && line[len - 1] == '\n')
407
			{
Bruce Momjian's avatar
Bruce Momjian committed
408 409
				line[len - 2] = '\n';
				line[len - 1] = '\0';
410
				len--;
411 412
			}

Bruce Momjian's avatar
Bruce Momjian committed
413
			/*
Bruce Momjian's avatar
Bruce Momjian committed
414 415
			 * We emulate fgets() behaviour. So if there is no newline at
			 * the end, we add one...
Bruce Momjian's avatar
Bruce Momjian committed
416
			 */
Bruce Momjian's avatar
Bruce Momjian committed
417 418
			if (len == 0 || line[len - 1] != '\n')
				strcat(line, "\n");
419 420 421 422 423
		}

		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	}
Bruce Momjian's avatar
Bruce Momjian committed
424

425 426 427 428 429 430
	CloseHandle(childstdoutwr);
	CloseHandle(childstdoutrddup);

	return retval;
#endif
}
Bruce Momjian's avatar
Bruce Momjian committed
431

432

433 434 435 436 437

/*
 * Find our binary directory, then make sure the "target" executable
 * is the proper version.
 */
438 439 440
int
find_other_exec(const char *argv0, const char *target,
				const char *versionstr, char *retpath)
441 442 443
{
	char		cmd[MAXPGPATH];
	char		line[100];
Bruce Momjian's avatar
Bruce Momjian committed
444

445
	if (find_my_exec(argv0, retpath) < 0)
446 447
		return -1;

Bruce Momjian's avatar
Bruce Momjian committed
448
	/* Trim off program name and keep just directory */
449
	*last_dir_separator(retpath) = '\0';
450 451 452
	canonicalize_path(retpath);

	/* Now append the other program's name */
453 454 455 456 457
	snprintf(retpath + strlen(retpath), MAXPGPATH - strlen(retpath),
			 "/%s%s", target, EXE);

	if (validate_exec(retpath))
		return -1;
Bruce Momjian's avatar
Bruce Momjian committed
458

459 460
	snprintf(cmd, sizeof(cmd), "\"%s\" -V 2>%s", retpath, DEVNULL);

461
	if (!pipe_read_line(cmd, line, sizeof(line)))
462
		return -1;
Bruce Momjian's avatar
Bruce Momjian committed
463

464 465 466 467 468 469 470
	if (strcmp(line, versionstr) != 0)
		return -2;

	return 0;
}


471 472 473 474 475 476 477 478
/*
 * pclose() plus useful error reporting
 * Is this necessary?  bjm 2004-05-11
 * It is better here because pipe.c has win32 backend linkage.
 */
int
pclose_check(FILE *stream)
{
Bruce Momjian's avatar
Bruce Momjian committed
479
	int			exitstatus;
480 481 482 483

	exitstatus = pclose(stream);

	if (exitstatus == 0)
Bruce Momjian's avatar
Bruce Momjian committed
484
		return 0;				/* all is well */
485 486 487 488 489 490 491 492

	if (exitstatus == -1)
	{
		/* pclose() itself failed, and hopefully set errno */
		perror("pclose failed");
	}
	else if (WIFEXITED(exitstatus))
	{
493
		log_error(_("child process exited with exit code %d\n"),
Bruce Momjian's avatar
Bruce Momjian committed
494
				  WEXITSTATUS(exitstatus));
495 496 497
	}
	else if (WIFSIGNALED(exitstatus))
	{
498
		log_error(_("child process was terminated by signal %d\n"),
Bruce Momjian's avatar
Bruce Momjian committed
499
				  WTERMSIG(exitstatus));
500 501 502
	}
	else
	{
503
		log_error(_("child process exited with unrecognized status %d\n"),
Bruce Momjian's avatar
Bruce Momjian committed
504
				  exitstatus);
505 506 507 508 509 510
	}

	return -1;
}


511 512 513 514 515 516 517 518 519 520 521 522
/*
 * Windows doesn't like relative paths to executables (other things work fine)
 * so we call its builtin function to expand them. Elsewhere this is a NOOP
 */
static void
win32_make_absolute(char *path)
{
#ifdef WIN32
	char		abspath[MAXPGPATH];

	if (_fullpath(abspath, path, MAXPGPATH) == NULL)
	{
523
		log_error("Win32 path expansion failed: %s", strerror(errno));
524
		StrNCpy(abspath, path, MAXPGPATH);
525 526 527 528 529 530
	}
	canonicalize_path(abspath);

	StrNCpy(path, abspath, MAXPGPATH);
#endif
}