1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/*!
\file lib/db/dbmi_base/login.c
\brief DBMI Library (base) - login settings
(C) 1999-2015 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
\author Joel Jones (CERL/UIUC), Radim Blazek
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
typedef struct {
char *driver;
char *database;
char *user;
char *password;
char *host;
char *port;
} DATA;
typedef struct {
int n, a;
DATA *data;
} LOGIN;
static const char *login_filename(void)
{
static char *file;
if (!file) {
file = (char *)db_malloc(GPATH_MAX);
sprintf(file, "%s%cdblogin", G_config_path(), HOST_DIRSEP);
}
return file;
}
static void init_login(LOGIN *login)
{
login->n = 0;
login->a = 10;
login->data = (DATA *)malloc(login->a * sizeof(DATA));
}
static void add_login(LOGIN *login, const char *dr, const char *db,
const char *usr, const char *pwd, const char *host,
const char *port, int idx)
{
int login_idx;
G_debug(
3,
"add_login(): drv='%s' db='%s' usr='%s' pwd='%s' host='%s', port='%s'",
dr, db, usr ? usr : "null", pwd ? pwd : "null", host ? host : "null",
port ? port : "null");
if (login->n == login->a) {
login->a += 10;
login->data =
(DATA *)realloc((void *)login->data, login->a * sizeof(DATA));
}
if (idx > -1 && idx < login->n) {
login_idx = idx;
}
else {
login_idx = login->n;
login->n++;
}
login->data[login_idx].driver = G_store(dr);
login->data[login_idx].database = G_store(db);
login->data[login_idx].user = G_store(usr ? usr : "");
login->data[login_idx].password = G_store(pwd ? pwd : "");
login->data[login_idx].host = G_store(host ? host : "");
login->data[login_idx].port = G_store(port ? port : "");
}
/*
Read the DB login file if it exists
return: -1 error (cannot read file)
number of items (0 also if file does not exist)
*/
static int read_file(LOGIN *login)
{
int ret;
const char *file;
FILE *fd;
char buf[DB_SQL_MAX];
char **tokens;
login->n = 0;
file = login_filename();
G_debug(3, "read_file(): DB login file = <%s>", file);
if (access(file, F_OK) != 0) {
G_debug(3, "login file does not exist");
return 0;
}
fd = fopen(file, "r");
if (fd == NULL) {
G_warning(_("Unable to read file '%s'"), file);
return -1;
}
while (G_getl2(buf, 2000, fd)) {
G_chop(buf);
tokens = G_tokenize(buf, "|");
ret = G_number_of_tokens(tokens);
if (ret < 2) {
G_warning(_("Login file (%s) corrupted (line: %s)"), file, buf);
G_free_tokens(tokens);
continue;
}
add_login(login, tokens[0], /* driver */
tokens[1], /* database */
ret > 2 ? tokens[2] : NULL, /* user */
ret > 3 ? tokens[3] : NULL, /* password */
ret > 4 ? tokens[4] : NULL, /* host */
ret > 5 ? tokens[5] : NULL, /* port */
-1);
G_free_tokens(tokens);
}
fclose(fd);
return (login->n);
}
/*
Write the DB login file
return: -1 error (cannot read file)
0 OK
*/
static int write_file(LOGIN *login)
{
int i;
const char *file;
FILE *fd;
file = login_filename();
G_debug(3, "write_file(): DB login file = <%s>", file);
fd = fopen(file, "w");
if (fd == NULL) {
G_warning(_("Unable to write file '%s'"), file);
return -1;
}
/* fchmod is not available on Windows */
/* fchmod ( fileno(fd), S_IRUSR | S_IWUSR ); */
chmod(file, S_IRUSR | S_IWUSR);
for (i = 0; i < login->n; i++) {
fprintf(fd, "%s|%s", login->data[i].driver, login->data[i].database);
if (login->data[i].user) {
fprintf(fd, "|%s", login->data[i].user);
if (login->data[i].password)
fprintf(fd, "|%s", login->data[i].password);
}
if (login->data[i].host)
fprintf(fd, "|%s", login->data[i].host);
if (login->data[i].port)
fprintf(fd, "|%s", login->data[i].port);
fprintf(fd, "\n");
}
fclose(fd);
return 0;
}
static int set_login(const char *driver, const char *database, const char *user,
const char *password, const char *host, const char *port,
int overwrite)
{
int i, found;
LOGIN login;
G_debug(3,
"db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s] "
"port=[%s]",
driver, database, user, password, host, port);
init_login(&login);
if (read_file(&login) == -1)
return DB_FAILED;
found = FALSE;
for (i = 0; i < login.n; i++) {
if (strcmp(login.data[i].driver, driver) == 0 &&
strcmp(login.data[i].database, database) == 0) {
if (user)
login.data[i].user = G_store(user);
else
login.data[i].user = G_store("");
if (password)
login.data[i].password = G_store(password);
else
login.data[i].password = G_store("");
found = TRUE;
break;
}
}
if (found) {
if (overwrite)
G_warning(_("DB connection <%s/%s> already exists and will be "
"overwritten"),
driver, database ? database : "");
else
G_fatal_error(_("DB connection <%s/%s> already exists. "
"Re-run '%s' with '--%s' flag to overwrite "
"existing settings."),
driver, database ? database : "", G_program_name(),
"overwrite");
}
if (!found)
add_login(&login, driver, database, user, password, host, port, -1);
else
add_login(&login, driver, database, user, password, host, port, i);
if (write_file(&login) == -1)
return DB_FAILED;
return DB_OK;
}
/*!
\brief Set login parameters for driver/database
\deprecated Use db_set_login2() instead.
\todo: GRASS 8: to be replaced by db_set_login2().
\param driver driver name
\param database database name
\param user user name
\param password password string
\return DB_OK on success
\return DB_FAILED on failure
*/
int db_set_login(const char *driver, const char *database, const char *user,
const char *password)
{
return set_login(driver, database, user, password, NULL, NULL, FALSE);
}
/*!
\brief Set login parameters for driver/database
\param driver driver name
\param database database name
\param user user name
\param password password string
\param host host name
\param port
\param overwrite TRUE to overwrite existing connections
\return DB_OK on success
\return DB_FAILED on failure
*/
int db_set_login2(const char *driver, const char *database, const char *user,
const char *password, const char *host, const char *port,
int overwrite)
{
return set_login(driver, database, user, password, host, port, overwrite);
}
static int get_login(const char *driver, const char *database,
const char **user, const char **password,
const char **host, const char **port)
{
int i;
LOGIN login;
G_debug(3, "db_get_login(): drv=[%s] db=[%s]", driver, database);
*user = NULL;
*password = NULL;
*host = NULL;
*port = NULL;
init_login(&login);
if (read_file(&login) == -1)
return DB_FAILED;
for (i = 0; i < login.n; i++) {
if (strcmp(login.data[i].driver, driver) == 0 &&
(!database || strcmp(login.data[i].database, database) == 0)) {
if (login.data[i].user && strlen(login.data[i].user) > 0)
*user = G_store(login.data[i].user);
else
*user = NULL;
if (login.data[i].password && strlen(login.data[i].password) > 0)
*password = G_store(login.data[i].password);
else
*password = NULL;
if (login.data[i].host && strlen(login.data[i].host) > 0 && host)
*host = G_store(login.data[i].host);
else
*host = NULL;
if (login.data[i].port && strlen(login.data[i].port) > 0 && port)
*port = G_store(login.data[i].port);
else
*port = NULL;
break;
}
}
return DB_OK;
}
/*!
\brief Get login parameters for driver/database
If driver/database is not found, output arguments are set to NULL.
\deprecated Use db_set_login2() instead.
\todo: GRASS 8: to be replaced by db_set_login2().
\param driver driver name
\param database database name (can be NULL)
\param[out] user name
\param[out] password string
\return DB_OK on success
\return DB_FAILED on failure
*/
int db_get_login(const char *driver, const char *database, const char **user,
const char **password)
{
return get_login(driver, database, user, password, NULL, NULL);
}
/*!
\brief Get login parameters for driver/database
If driver/database is not found, output arguments are set to NULL.
\param driver driver name
\param database database name (can be NULL)
\param[out] user name
\param[out] password string
\param[out] host name
\param[out] port
\return DB_OK on success
\return DB_FAILED on failure
*/
int db_get_login2(const char *driver, const char *database, const char **user,
const char **password, const char **host, const char **port)
{
return get_login(driver, database, user, password, host, port);
}
/*!
\brief Print all connection settings to file
\param fd file where to print settings
\return DB_OK on success
\return DB_FAILED on failure
*/
int db_get_login_dump(FILE *fd)
{
int i;
LOGIN login;
G_debug(3, "db_get_login_dump()");
init_login(&login);
if (read_file(&login) == -1)
return DB_FAILED;
for (i = 0; i < login.n; i++) {
fprintf(fd, "%s|%s|%s|%s|%s|%s\n", login.data[i].driver,
login.data[i].database, login.data[i].user,
login.data[i].password, login.data[i].host, login.data[i].port);
}
return DB_OK;
}
|