18 #include <sys/types.h> 42 static const char *login_filename(
void)
53 static void init_login(LOGIN * login)
58 login->data = (DATA *)
malloc(login->a *
sizeof(DATA));
61 static void add_login(LOGIN * login,
const char *dr,
const char *db,
const char *usr,
62 const char *pwd,
const char *host,
const char *port,
int idx)
66 G_debug(3,
"add_login(): drv='%s' db='%s' usr='%s' pwd='%s' host='%s', port='%s'",
67 dr, db, usr ? usr :
"null", pwd ? pwd :
"null", host ? host :
"null",
68 port ? port :
"null");
70 if (login->n == login->a) {
73 (DATA *) realloc((
void *)login->data, login->a *
sizeof(DATA));
75 if (idx > -1 && idx < login->n) {
82 login->data[login_idx].driver =
G_store(dr);
83 login->data[login_idx].database =
G_store(db);
84 login->data[login_idx].user =
G_store(usr ? usr :
"");
85 login->data[login_idx].password =
G_store(pwd ? pwd :
"");
86 login->data[login_idx].host =
G_store(host ? host :
"");
87 login->data[login_idx].port =
G_store(port ? port :
"");
95 static int read_file(LOGIN * login)
104 file = login_filename();
106 G_debug(3,
"read_file(): DB login file = <%s>", file);
108 if (access(file, F_OK) != 0) {
109 G_debug(3,
"login file does not exist");
113 fd = fopen(file,
"r");
115 G_warning(
_(
"Unable to read file '%s'"), file);
119 while (
G_getl2(buf, 2000, fd)) {
126 G_warning(
_(
"Login file (%s) corrupted (line: %s)"), file, buf);
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");
164 G_warning(
_(
"Unable to write file '%s'"), file);
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,
200 G_debug(3,
"db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s] port=[%s]",
201 driver, database, user, password, host, port);
205 if (read_file(&login) == -1)
209 for (i = 0; i < login.n; i++) {
210 if (strcmp(login.data[i].driver, driver) == 0 &&
211 strcmp(login.data[i].database, database) == 0) {
213 login.data[i].user =
G_store(user);
215 login.data[i].user =
G_store(
"");
218 login.data[i].password =
G_store(password);
220 login.data[i].password =
G_store(
"");
229 G_warning(
_(
"DB connection <%s/%s> already exists and will be overwritten"),
230 driver, database ? database :
"");
233 "Re-run '%s' with '--%s' flag to overwrite existing settings."),
238 add_login(&login, driver, database, user, password, host, port, -1);
240 add_login(&login, driver, database, user, password, host, port, i);
242 if (write_file(&login) == -1)
263 int db_set_login(
const char *driver,
const char *database,
const char *user,
264 const char *password)
266 return set_login(driver, database, user,
284 int db_set_login2(
const char *driver,
const char *database,
const char *user,
285 const char *password,
const char *host,
const char *port,
288 return set_login(driver, database, user,
289 password, host, port, overwrite);
292 static int get_login(
const char *driver,
const char *database,
const char **user,
293 const char **password,
const char **host,
const char **port)
298 G_debug(3,
"db_get_login(): drv=[%s] db=[%s]", driver, database);
307 if (read_file(&login) == -1)
310 for (i = 0; i < login.n; i++) {
311 if (strcmp(login.data[i].driver, driver) == 0 &&
312 (!database || strcmp(login.data[i].database, database) == 0)) {
313 if (login.data[i].user && strlen(login.data[i].user) > 0)
314 *user =
G_store(login.data[i].user);
318 if (login.data[i].password && strlen(login.data[i].password) > 0)
319 *password =
G_store(login.data[i].password);
323 if (login.data[i].host && strlen(login.data[i].host) > 0 && host)
324 *host =
G_store(login.data[i].host);
328 if (login.data[i].port && strlen(login.data[i].port) > 0 && port)
329 *port =
G_store(login.data[i].port);
357 int db_get_login(
const char *driver,
const char *database,
const char **user,
358 const char **password)
360 return get_login(driver, database, user,
379 int db_get_login2(
const char *driver,
const char *database,
const char **user,
380 const char **password,
const char **host,
const char **port)
382 return get_login(driver, database, user,
383 password, host, port);
399 G_debug(3,
"db_get_login_dump()");
402 if (read_file(&login) == -1)
405 for (i = 0; i < login.n; i++) {
406 fprintf(fd,
"%s|%s|%s|%s|%s|%s\n",
407 login.data[i].driver,
408 login.data[i].database,
410 login.data[i].password,
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
const char * G_program_name(void)
Return module name.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char ** G_tokenize(const char *, const char *)
Tokenize string.
int db_get_login_dump(FILE *fd)
Print all connection settings to file.
int G_number_of_tokens(char **)
Return number of tokens.
const struct driver * driver
int db_set_login(const char *driver, const char *database, const char *user, const char *password)
Set 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.
char * G_chop(char *)
Chop leading and trailing white spaces.
const char * G_config_path(void)
Get user's config path directory.
void G_free_tokens(char **)
Free memory allocated to tokens.
void G_warning(const char *,...) __attribute__((format(printf
int db_get_login(const char *driver, const char *database, const char **user, const char **password)
Get login parameters for driver/database.
char * G_store(const char *)
Copy string to allocated memory.
void * db_malloc(int)
Allocate memory.
int G_debug(int, const char *,...) __attribute__((format(printf
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.