void pgConn::ExamineLibpqVersion() { libpqVersion = 7.3; PQconninfoOption *cio = PQconndefaults(); if (cio) { PQconninfoOption *co = cio; while (co->keyword) { if (!strcmp(co->keyword, "sslmode")) { if (libpqVersion < 7.4) libpqVersion = 7.4; } if (!strcmp(co->keyword, "sslrootcert")) { if (libpqVersion < 8.4) libpqVersion = 8.4; } if (!strcmp(co->keyword, "sslcompression")) { if (libpqVersion < 9.2) libpqVersion = 9.2; } co++; } PQconninfoFree(cio); } }
/* * check_pghost_envvar() * * Tests that PGHOST does not point to a non-local server */ void check_pghost_envvar(void) { PQconninfoOption *option; PQconninfoOption *start; /* Get valid libpq env vars from the PQconndefaults function */ start = PQconndefaults(); if (!start) pg_fatal("out of memory\n"); for (option = start; option->keyword != NULL; option++) { if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 || strcmp(option->envvar, "PGHOSTADDR") == 0)) { const char *value = getenv(option->envvar); if (value && strlen(value) > 0 && /* check for 'local' host values */ (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 && strcmp(value, "::1") != 0 && value[0] != '/')) pg_fatal("libpq environment variable %s has a non-local server value: %s\n", option->envvar, value); } } /* Free the memory that libpq allocated on our behalf */ PQconninfoFree(start); }
int Pg_conndefaults(ClientData cData, Tcl_Interp *interp, int argc, CONST84 char *argv[]) { PQconninfoOption *options = PQconndefaults(); PQconninfoOption *option; Tcl_DString result; char ibuf[32]; if (options) { Tcl_DStringInit(&result); for (option = options; option->keyword != NULL; option++) { char *val = option->val ? option->val : ""; sprintf(ibuf, "%d", option->dispsize); Tcl_DStringStartSublist(&result); Tcl_DStringAppendElement(&result, option->keyword); Tcl_DStringAppendElement(&result, option->label); Tcl_DStringAppendElement(&result, option->dispchar); Tcl_DStringAppendElement(&result, ibuf); Tcl_DStringAppendElement(&result, val); Tcl_DStringEndSublist(&result); } Tcl_DStringResult(interp, &result); PQconninfoFree(options); } return TCL_OK; }
/* * check_for_libpq_envvars() * * tests whether any libpq environment variables are set. * Since pg_upgrade connects to both the old and the new server, * it is potentially dangerous to have any of these set. * * If any are found, will log them and cancel. */ void check_for_libpq_envvars(void) { PQconninfoOption *option; PQconninfoOption *start; bool found = false; /* Get valid libpq env vars from the PQconndefaults function */ start = option = PQconndefaults(); while (option->keyword != NULL) { const char *value; if (option->envvar && (value = getenv(option->envvar)) && strlen(value) > 0) { found = true; pg_log(PG_WARNING, "libpq env var %-20s is currently set to: %s\n", option->envvar, value); } option++; } /* Free the memory that libpq allocated on our behalf */ PQconninfoFree(start); if (found) pg_log(PG_FATAL, "libpq env vars have been found and listed above, please unset them for pg_upgrade\n"); }
CAMLprim value PQconndefaults_stub(value __unused v_unit) { CAMLparam0(); CAMLlocal2(v_res, v_el); PQconninfoOption *cios = PQconndefaults(), *p = cios; int i, j, n; while (p->keyword != NULL) p++; n = p - cios; p = cios; v_res = caml_alloc_tuple(n); for (i = 0; i < n; i++, p++) { value v_field; v_el = caml_alloc_small(7, 0); for (j = 0; j < 7; j++) Field(v_el, j) = v_None; Store_field(v_res, i, v_el); v_field = caml_copy_string(p->keyword); Field(v_el, 0) = v_field; if (p->envvar) { v_field = make_some(caml_copy_string(p->envvar)); caml_modify(&Field(v_el, 1), v_field); } if (p->compiled) { v_field = make_some(caml_copy_string(p->compiled)); caml_modify(&Field(v_el, 2), v_field); }; if (p->val) { v_field = make_some(caml_copy_string(p->val)); caml_modify(&Field(v_el, 3), v_field); }; v_field = caml_copy_string(p->label); caml_modify(&Field(v_el, 4), v_field); v_field = caml_copy_string(p->dispchar); caml_modify(&Field(v_el, 5), v_field); caml_modify(&Field(v_el, 6), Val_int(p->dispsize)); }; PQconninfoFree(cios); CAMLreturn(v_res); }
int main(int argc, char **argv) { int c; const char *progname; const char *pghost = NULL; const char *pgport = NULL; const char *pguser = NULL; const char *pgdbname = NULL; const char *connect_timeout = DEFAULT_CONNECT_TIMEOUT; const char *pghost_str = NULL; const char *pgport_str = NULL; #define PARAMS_ARRAY_SIZE 7 const char *keywords[PARAMS_ARRAY_SIZE]; const char *values[PARAMS_ARRAY_SIZE]; bool quiet = false; PGPing rv; PQconninfoOption *opts = NULL; PQconninfoOption *defs = NULL; PQconninfoOption *opt; PQconninfoOption *def; char *errmsg = NULL; /* * We accept user and database as options to avoid useless errors from * connecting with invalid params */ static struct option long_options[] = { {"dbname", required_argument, NULL, 'd'}, {"host", required_argument, NULL, 'h'}, {"port", required_argument, NULL, 'p'}, {"quiet", no_argument, NULL, 'q'}, {"timeout", required_argument, NULL, 't'}, {"username", required_argument, NULL, 'U'}, {NULL, 0, NULL, 0} }; progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); handle_help_version_opts(argc, argv, progname, help); while ((c = getopt_long(argc, argv, "d:h:p:qt:U:", long_options, NULL)) != -1) { switch (c) { case 'd': pgdbname = pg_strdup(optarg); break; case 'h': pghost = pg_strdup(optarg); break; case 'p': pgport = pg_strdup(optarg); break; case 'q': quiet = true; break; case 't': connect_timeout = pg_strdup(optarg); break; case 'U': pguser = pg_strdup(optarg); break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); /* * We need to make sure we don't return 1 here because someone * checking the return code might infer unintended meaning */ exit(PQPING_NO_ATTEMPT); } } if (optind < argc) { fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); /* * We need to make sure we don't return 1 here because someone * checking the return code might infer unintended meaning */ exit(PQPING_NO_ATTEMPT); } keywords[0] = "host"; values[0] = pghost; keywords[1] = "port"; values[1] = pgport; keywords[2] = "user"; values[2] = pguser; keywords[3] = "dbname"; values[3] = pgdbname; keywords[4] = "connect_timeout"; values[4] = connect_timeout; keywords[5] = "fallback_application_name"; values[5] = progname; keywords[6] = NULL; values[6] = NULL; /* * Get the host and port so we can display them in our output */ if (pgdbname) { opts = PQconninfoParse(pgdbname, &errmsg); if (opts == NULL) { fprintf(stderr, _("%s: %s"), progname, errmsg); exit(PQPING_NO_ATTEMPT); } } defs = PQconndefaults(); if (defs == NULL) { fprintf(stderr, _("%s: could not fetch default options\n"), progname); exit(PQPING_NO_ATTEMPT); } for (opt = opts, def = defs; def->keyword; def++) { if (strcmp(def->keyword, "hostaddr") == 0 || strcmp(def->keyword, "host") == 0) { if (opt && opt->val) pghost_str = opt->val; else if (pghost) pghost_str = pghost; else if (def->val) pghost_str = def->val; else pghost_str = DEFAULT_PGSOCKET_DIR; } else if (strcmp(def->keyword, "port") == 0) { if (opt && opt->val) pgport_str = opt->val; else if (pgport) pgport_str = pgport; else if (def->val) pgport_str = def->val; } if (opt) opt++; } rv = PQpingParams(keywords, values, 1); if (!quiet) { printf("%s:%s - ", pghost_str, pgport_str); switch (rv) { case PQPING_OK: printf("accepting connections\n"); break; case PQPING_REJECT: printf("rejecting connections\n"); break; case PQPING_NO_RESPONSE: printf("no response\n"); break; case PQPING_NO_ATTEMPT: printf("no attempt\n"); break; default: printf("unknown\n"); } } exit(rv); }