18 #include <sys/types.h>
40 static const char *login_filename(
void)
51 static void init_login(LOGIN *login)
56 login->data = (DATA *)
malloc(login->a *
sizeof(DATA));
59 static void add_login(LOGIN *login,
const char *dr,
const char *db,
60 const char *usr,
const char *pwd,
const char *host,
61 const char *port,
int idx)
67 "add_login(): drv='%s' db='%s' usr='%s' pwd='%s' host='%s', port='%s'",
68 dr, db, usr ? usr :
"null", pwd ? pwd :
"null", host ? host :
"null",
69 port ? port :
"null");
71 if (login->n == login->a) {
74 (DATA *)realloc((
void *)login->data, login->a *
sizeof(DATA));
76 if (idx > -1 && idx < login->n) {
83 login->data[login_idx].driver =
G_store(dr);
84 login->data[login_idx].database =
G_store(db);
85 login->data[login_idx].user =
G_store(usr ? usr :
"");
86 login->data[login_idx].password =
G_store(pwd ? pwd :
"");
87 login->data[login_idx].host =
G_store(host ? host :
"");
88 login->data[login_idx].port =
G_store(port ? port :
"");
96 static int read_file(LOGIN *login)
105 file = login_filename();
107 G_debug(3,
"read_file(): DB login file = <%s>",
file);
109 if (access(
file, F_OK) != 0) {
110 G_debug(3,
"login file does not exist");
114 fd = fopen(
file,
"r");
120 while (
G_getl2(buf, 2000, fd)) {
132 add_login(login, tokens[0],
134 ret > 2 ? tokens[2] :
NULL,
135 ret > 3 ? tokens[3] :
NULL,
136 ret > 4 ? tokens[4] :
NULL,
137 ret > 5 ? tokens[5] :
NULL,
152 static int write_file(LOGIN *login)
158 file = login_filename();
160 G_debug(3,
"write_file(): DB login file = <%s>",
file);
162 fd = fopen(
file,
"w");
170 chmod(
file, S_IRUSR | S_IWUSR);
172 for (i = 0; i < login->n; i++) {
173 fprintf(fd,
"%s|%s", login->data[i].driver, login->data[i].database);
174 if (login->data[i].user) {
175 fprintf(fd,
"|%s", login->data[i].user);
177 if (login->data[i].password)
178 fprintf(fd,
"|%s", login->data[i].password);
180 if (login->data[i].host)
181 fprintf(fd,
"|%s", login->data[i].host);
182 if (login->data[i].port)
183 fprintf(fd,
"|%s", login->data[i].port);
193 static int set_login(
const char *
driver,
const char *database,
const char *user,
194 const char *password,
const char *host,
const char *port,
201 "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s] "
203 driver, database, user, password, host, port);
207 if (read_file(&login) == -1)
211 for (i = 0; i < login.n; i++) {
212 if (strcmp(login.data[i].driver,
driver) == 0 &&
213 strcmp(login.data[i].database, database) == 0) {
215 login.data[i].user =
G_store(user);
217 login.data[i].user =
G_store(
"");
220 login.data[i].password =
G_store(password);
222 login.data[i].password =
G_store(
"");
231 G_warning(
_(
"DB connection <%s/%s> already exists and will be "
233 driver, database ? database :
"");
236 "Re-run '%s' with '--%s' flag to overwrite "
237 "existing settings."),
243 add_login(&login,
driver, database, user, password, host, port, -1);
245 add_login(&login,
driver, database, user, password, host, port, i);
247 if (write_file(&login) == -1)
269 const char *password)
289 const char *password,
const char *host,
const char *port,
292 return set_login(
driver, database, user, password, host, port, overwrite);
295 static int get_login(
const char *
driver,
const char *database,
296 const char **user,
const char **password,
297 const char **host,
const char **port)
302 G_debug(3,
"db_get_login(): drv=[%s] db=[%s]",
driver, database);
311 if (read_file(&login) == -1)
314 for (i = 0; i < login.n; i++) {
315 if (strcmp(login.data[i].driver,
driver) == 0 &&
316 (!database || strcmp(login.data[i].database, database) == 0)) {
317 if (login.data[i].user && strlen(login.data[i].user) > 0)
318 *user =
G_store(login.data[i].user);
322 if (login.data[i].password && strlen(login.data[i].password) > 0)
323 *password =
G_store(login.data[i].password);
327 if (login.data[i].host && strlen(login.data[i].host) > 0 && host)
328 *host =
G_store(login.data[i].host);
332 if (login.data[i].port && strlen(login.data[i].port) > 0 && port)
333 *port =
G_store(login.data[i].port);
362 const char **password)
383 const char **password,
const char **host,
const char **port)
385 return get_login(
driver, database, user, password, host, port);
401 G_debug(3,
"db_get_login_dump()");
404 if (read_file(&login) == -1)
407 for (i = 0; i < login.n; i++) {
408 fprintf(fd,
"%s|%s|%s|%s|%s|%s\n", login.data[i].driver,
409 login.data[i].database, login.data[i].user,
410 login.data[i].password, login.data[i].host, login.data[i].port);
void * db_malloc(int)
Allocate memory.
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
const char * G_config_path(void)
Get user's config path directory.
void G_free_tokens(char **)
Free memory allocated to tokens.
int G_number_of_tokens(char **)
Return number of tokens.
const char * G_program_name(void)
Return module name.
char * G_chop(char *)
Chop leading and trailing white spaces.
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
char ** G_tokenize(const char *, const char *)
Tokenize string.
const struct driver * driver
int db_get_login(const char *driver, const char *database, const char **user, const char **password)
Get login parameters for driver/database.
int db_set_login(const char *driver, const char *database, const char *user, const char *password)
Set login parameters for driver/database.
int db_get_login2(const char *driver, const char *database, const char **user, const char **password, const char **host, const char **port)
Get login parameters for driver/database.
int db_get_login_dump(FILE *fd)
Print all connection settings to file.
int db_set_login2(const char *driver, const char *database, const char *user, const char *password, const char *host, const char *port, int overwrite)
Set login parameters for driver/database.