You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.3 KiB

  1. Index: postgresql-9.5.4/src/bin/pg_ctl/pg_ctl.c
  2. ===================================================================
  3. --- postgresql-9.5.4.orig/src/bin/pg_ctl/pg_ctl.c
  4. +++ postgresql-9.5.4/src/bin/pg_ctl/pg_ctl.c
  5. @@ -95,6 +95,7 @@ static char *event_source = NULL;
  6. static char *register_servicename = "PostgreSQL"; /* FIXME: + version ID? */
  7. static char *register_username = NULL;
  8. static char *register_password = NULL;
  9. +static char *username = "";
  10. static char *argv0 = NULL;
  11. static bool allow_core_files = false;
  12. static time_t start_time;
  13. @@ -2114,6 +2115,9 @@ do_help(void)
  14. #endif
  15. printf(_(" -s, --silent only print errors, no informational messages\n"));
  16. printf(_(" -t, --timeout=SECS seconds to wait when using -w option\n"));
  17. +#if !defined(WIN32) && !defined(__CYGWIN__)
  18. + printf(_(" -U, --username=NAME user name of account PostgreSQL server is running as\n"));
  19. +#endif
  20. printf(_(" -V, --version output version information, then exit\n"));
  21. printf(_(" -w wait until operation completes\n"));
  22. printf(_(" -W do not wait until operation completes\n"));
  23. @@ -2310,6 +2314,7 @@ main(int argc, char **argv)
  24. {"pgdata", required_argument, NULL, 'D'},
  25. {"silent", no_argument, NULL, 's'},
  26. {"timeout", required_argument, NULL, 't'},
  27. + {"username", required_argument, NULL, 'U'},
  28. {"core-files", no_argument, NULL, 'c'},
  29. {NULL, 0, NULL, 0}
  30. };
  31. @@ -2350,20 +2355,6 @@ main(int argc, char **argv)
  32. }
  33. }
  34. - /*
  35. - * Disallow running as root, to forestall any possible security holes.
  36. - */
  37. -#ifndef WIN32
  38. - if (geteuid() == 0)
  39. - {
  40. - write_stderr(_("%s: cannot be run as root\n"
  41. - "Please log in (using, e.g., \"su\") as the "
  42. - "(unprivileged) user that will\n"
  43. - "own the server process.\n"),
  44. - progname);
  45. - exit(1);
  46. - }
  47. -#endif
  48. env_wait = getenv("PGCTLTIMEOUT");
  49. if (env_wait != NULL)
  50. @@ -2449,11 +2440,15 @@ main(int argc, char **argv)
  51. wait_seconds_arg = true;
  52. break;
  53. case 'U':
  54. +#if defined(WIN32) || defined(__CYGWIN__)
  55. if (strchr(optarg, '\\'))
  56. register_username = pg_strdup(optarg);
  57. else
  58. /* Prepend .\ for local accounts */
  59. register_username = psprintf(".\\%s", optarg);
  60. +#else
  61. + username = pg_strdup(optarg);
  62. +#endif
  63. break;
  64. case 'w':
  65. do_wait = true;
  66. @@ -2535,6 +2530,41 @@ main(int argc, char **argv)
  67. exit(1);
  68. }
  69. + /*
  70. + * Disallow running as root, to forestall any possible security holes.
  71. + */
  72. +#if !defined(WIN32) && !defined(__CYGWIN__)
  73. + if (geteuid() == 0)
  74. + {
  75. + struct passwd *p;
  76. + if (!username || !strlen(username)) {
  77. + fprintf(stderr,
  78. + _("%s: when run as root, username needs to be provided\n"),
  79. + progname);
  80. + exit(1);
  81. + }
  82. + p = getpwnam(username);
  83. + if (!p) {
  84. + fprintf(stderr,
  85. + _("%s: invalid username: %s\n"),
  86. + progname, username);
  87. + exit(1);
  88. + }
  89. + if (!p->pw_uid) {
  90. + fprintf(stderr,
  91. + _("%s: user needs to be non-root\n"),
  92. + progname);
  93. + exit(1);
  94. + }
  95. + if (setgid(p->pw_gid) || setuid(p->pw_uid)) {
  96. + fprintf(stderr,
  97. + _("%s: failed to set user id %d: %d (%s)\n"),
  98. + progname, p->pw_uid, errno, strerror(errno));
  99. + exit(1);
  100. + }
  101. + }
  102. +#endif
  103. +
  104. /* Note we put any -D switch into the env var above */
  105. pg_config = getenv("PGDATA");
  106. if (pg_config)