Skip to content

Commit f2ef4df

Browse files
committed
TNS:
I prepared and tested a patch vs. 7.0.2, and it works fine. I've added another option which allows users to have their own service file in ~/.pg_service.conf, which might come handy sometimes. Mario Weilguni
1 parent 1333f07 commit f2ef4df

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.138 2000/10/14 23:56:59 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.139 2000/10/17 01:00:58 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -111,6 +111,9 @@ static const PQconninfoOption PQconninfoOptions[] = {
111111
{"authtype", "PGAUTHTYPE", DefaultAuthtype, NULL,
112112
"Database-Authtype", "D", 20},
113113

114+
{"service", "PGSERVICE", NULL, NULL,
115+
"Database-Service", "", 20},
116+
114117
{"user", "PGUSER", NULL, NULL,
115118
"Database-User", "", 20},
116119

@@ -187,6 +190,8 @@ static PQconninfoOption *conninfo_parse(const char *conninfo,
187190
static char *conninfo_getval(PQconninfoOption *connOptions,
188191
const char *keyword);
189192
static void defaultNoticeProcessor(void *arg, const char *message);
193+
static int parseServiceInfo(PQconninfoOption *options,
194+
PQExpBuffer errorMessage);
190195

191196

192197
/* ----------------
@@ -2090,6 +2095,114 @@ pqPacketSend(PGconn *conn, const char *buf, size_t len)
20902095
return STATUS_OK;
20912096
}
20922097

2098+
int parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage) {
2099+
char *service = conninfo_getval(options, "service");
2100+
char *serviceFile = "/etc/pg_service.conf";
2101+
int MAXBUFSIZE = 256;
2102+
int group_found = 0;
2103+
int linenr=0, i;
2104+
2105+
if(service != NULL) {
2106+
FILE *f;
2107+
char buf[MAXBUFSIZE], *line;
2108+
2109+
f = fopen(serviceFile, "r");
2110+
if(f == NULL) {
2111+
printfPQExpBuffer(errorMessage, "ERROR: Service file '%s' not found\n",
2112+
serviceFile);
2113+
return 1;
2114+
}
2115+
2116+
/* As default, set the database name to the name of the service */
2117+
for(i = 0; options[i].keyword; i++)
2118+
if(strcmp(options[i].keyword, "dbname") == 0) {
2119+
if(options[i].val != NULL)
2120+
free(options[i].val);
2121+
options[i].val = strdup(service);
2122+
}
2123+
2124+
while((line = fgets(buf, MAXBUFSIZE-1, f)) != NULL) {
2125+
linenr++;
2126+
2127+
if(strlen(line) >= MAXBUFSIZE - 2) {
2128+
fclose(f);
2129+
printfPQExpBuffer(errorMessage,
2130+
"ERROR: line %d too long in service file '%s'\n",
2131+
linenr,
2132+
serviceFile);
2133+
return 2;
2134+
}
2135+
2136+
/* ignore EOL at end of line */
2137+
if(strlen(line) && line[strlen(line)-1] == '\n')
2138+
line[strlen(line)-1] = 0;
2139+
2140+
/* ignore leading blanks */
2141+
while(*line && isspace(line[0]))
2142+
line++;
2143+
2144+
/* ignore comments and empty lines */
2145+
if(strlen(line) == 0 || line[0] == '#')
2146+
continue;
2147+
2148+
/* Check for right groupname */
2149+
if(line[0] == '[') {
2150+
if(group_found) {
2151+
/* group info already read */
2152+
fclose(f);
2153+
return 0;
2154+
}
2155+
2156+
if(strncmp(line+1, service, strlen(service)) == 0 &&
2157+
line[strlen(service)+1] == ']')
2158+
group_found = 1;
2159+
else
2160+
group_found = 0;
2161+
} else {
2162+
if(group_found) {
2163+
/* Finally, we are in the right group and can parse the line */
2164+
char *key, *val;
2165+
int found_keyword;
2166+
2167+
key = strtok(line, "=");
2168+
if(key == NULL) {
2169+
printfPQExpBuffer(errorMessage,
2170+
"ERROR: syntax error in service file '%s', line %d\n",
2171+
serviceFile,
2172+
linenr);
2173+
fclose(f);
2174+
return 3;
2175+
}
2176+
val = line + strlen(line) + 1;
2177+
2178+
found_keyword = 0;
2179+
for(i = 0; options[i].keyword; i++) {
2180+
if(strcmp(options[i].keyword, key) == 0) {
2181+
if(options[i].val != NULL)
2182+
free(options[i].val);
2183+
options[i].val = strdup(val);
2184+
found_keyword = 1;
2185+
}
2186+
}
2187+
2188+
if(!found_keyword) {
2189+
printfPQExpBuffer(errorMessage,
2190+
"ERROR: syntax error in service file '%s', line %d\n",
2191+
serviceFile,
2192+
linenr);
2193+
fclose(f);
2194+
return 3;
2195+
}
2196+
}
2197+
}
2198+
}
2199+
2200+
fclose(f);
2201+
}
2202+
2203+
return 0;
2204+
}
2205+
20932206

20942207
/* ----------------
20952208
* Conninfo parser routine
@@ -2263,6 +2376,14 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage)
22632376
if (option->val)
22642377
free(option->val);
22652378
option->val = strdup(pval);
2379+
2380+
}
2381+
2382+
/* Now check for service info */
2383+
if(parseServiceInfo(options, errorMessage)) {
2384+
PQconninfoFree(options);
2385+
free(buf);
2386+
return NULL;
22662387
}
22672388

22682389
/* Done with the modifiable input string */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy