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)
268 const char *password,
const char *host,
const char *port,
290 const char *password,
const char *host,
const char *port,
293 return set_login(
driver, database, user, password, host, port, overwrite);
296 static int get_login(
const char *
driver,
const char *database,
297 const char **user,
const char **password,
298 const char **host,
const char **port)
303 G_debug(3,
"db_get_login(): drv=[%s] db=[%s]",
driver, database);
312 if (read_file(&login) == -1)
315 for (i = 0; i < login.n; i++) {
316 if (strcmp(login.data[i].driver,
driver) == 0 &&
317 (!database || strcmp(login.data[i].database, database) == 0)) {
318 if (login.data[i].user && strlen(login.data[i].user) > 0)
319 *user =
G_store(login.data[i].user);
323 if (login.data[i].password && strlen(login.data[i].password) > 0)
324 *password =
G_store(login.data[i].password);
328 if (login.data[i].host && strlen(login.data[i].host) > 0 && host)
329 *host =
G_store(login.data[i].host);
333 if (login.data[i].port && strlen(login.data[i].port) > 0 && port)
334 *port =
G_store(login.data[i].port);
361 const char **password,
const char **host,
const char **port)
382 const char **password,
const char **host,
const char **port)
384 return get_login(
driver, database, user, password, host, port);
400 G_debug(3,
"db_get_login_dump()");
403 if (read_file(&login) == -1)
406 for (i = 0; i < login.n; i++) {
407 fprintf(fd,
"%s|%s|%s|%s|%s|%s\n", login.data[i].driver,
408 login.data[i].database, login.data[i].user,
409 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_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_set_login(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.
int db_get_login_dump(FILE *fd)
Print all connection settings to file.
int db_get_login(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_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.