00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <sys/types.h>
00005 #include <sys/stat.h>
00006 #include <unistd.h>
00007 #include <grass/gis.h>
00008 #include <grass/dbmi.h>
00009 #include <grass/glocale.h>
00010
00011 typedef struct
00012 {
00013 char *driver;
00014 char *database;
00015 char *user;
00016 char *password;
00017 } DATA;
00018
00019 typedef struct
00020 {
00021 int n, a;
00022 DATA *data;
00023 } LOGIN;
00024
00025 static const char *login_filename(void)
00026 {
00027 static char *file;
00028
00029 if (!file) {
00030 file = (char *)malloc(1000);
00031 sprintf(file, "%s/.grasslogin64", G_home());
00032 }
00033 return file;
00034 }
00035
00036 void init_login(LOGIN * login)
00037 {
00038 login->n = 0;
00039 login->a = 10;
00040
00041 login->data = (DATA *) malloc(login->a * sizeof(DATA));
00042 }
00043
00044 void
00045 add_login(LOGIN * login, const char *dr, const char *db, const char *usr,
00046 const char *pwd)
00047 {
00048 if (login->n == login->a) {
00049 login->a += 10;
00050 login->data =
00051 (DATA *) realloc((void *)login->data, login->a * sizeof(DATA));
00052 }
00053 login->data[login->n].driver = G_store(dr);
00054 login->data[login->n].database = G_store(db);
00055 login->data[login->n].user = G_store(usr ? usr : "");
00056 login->data[login->n].password = G_store(pwd ? pwd : "");
00057
00058 login->n++;
00059 }
00060
00061
00062
00063
00064
00065
00066 int read_file(LOGIN * login)
00067 {
00068 int ret;
00069 const char *file;
00070 struct stat info;
00071 FILE *fd;
00072 char buf[2001], dr[500], db[500], usr[500], pwd[500];
00073
00074 login->n = 0;
00075 file = login_filename();
00076
00077 G_debug(3, "DB login file = <%s>", file);
00078
00079 if (stat(file, &info) != 0) {
00080 G_debug(3, "login file does not exist");
00081 return 0;
00082 }
00083
00084 fd = fopen(file, "r");
00085 if (fd == NULL)
00086 return -1;
00087
00088 while (G_getl2(buf, 2000, fd)) {
00089 G_chop(buf);
00090
00091 usr[0] = pwd[0] = '\0';
00092 ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd);
00093
00094 G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s]",
00095 ret, dr, db, usr, pwd);
00096
00097 if (ret < 2) {
00098 G_warning(_("Login file corrupted"));
00099 continue;
00100 }
00101
00102 add_login(login, dr, db, usr, pwd);
00103 }
00104
00105 fclose(fd);
00106
00107 return (login->n);
00108 }
00109
00110
00111
00112
00113
00114
00115 int write_file(LOGIN * login)
00116 {
00117 int i;
00118 const char *file;
00119 FILE *fd;
00120
00121 file = login_filename();
00122
00123 G_debug(3, "DB login file = <%s>", file);
00124
00125 fd = fopen(file, "w");
00126 if (fd == NULL)
00127 return -1;
00128
00129
00130
00131 chmod(file, S_IRUSR | S_IWUSR);
00132
00133 for (i = 0; i < login->n; i++) {
00134 fprintf(fd, "%s|%s", login->data[i].driver, login->data[i].database);
00135 if (login->data[i].user) {
00136 fprintf(fd, "|%s", login->data[i].user);
00137
00138 if (login->data[i].password)
00139 fprintf(fd, "|%s", login->data[i].password);
00140 }
00141 fprintf(fd, "\n");
00142 }
00143
00144 fclose(fd);
00145
00146 return 0;
00147 }
00148
00154 int
00155 db_set_login(const char *driver, const char *database, const char *user,
00156 const char *password)
00157 {
00158 int i, found;
00159 LOGIN login;
00160
00161 G_debug(3, "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s]",
00162 driver, database, user, password);
00163
00164 init_login(&login);
00165
00166 if (read_file(&login) == -1)
00167 return DB_FAILED;
00168
00169 found = 0;
00170 for (i = 0; i < login.n; i++) {
00171 if (strcmp(login.data[i].driver, driver) == 0 &&
00172 strcmp(login.data[i].database, database) == 0) {
00173 if (user)
00174 login.data[i].user = G_store(user);
00175 else
00176 login.data[i].user = G_store("");
00177
00178 if (password)
00179 login.data[i].password = G_store(password);
00180 else
00181 login.data[i].password = G_store("");
00182
00183 found = 1;
00184 break;
00185 }
00186 }
00187
00188 if (!found)
00189 add_login(&login, driver, database, user, password);
00190
00191 if (write_file(&login) == -1)
00192 return DB_FAILED;
00193
00194 return DB_OK;
00195 }
00196
00203 int
00204 db_get_login(const char *driver, const char *database, const char **user,
00205 const char **password)
00206 {
00207 int i;
00208 LOGIN login;
00209
00210 G_debug(3, "db_get_login(): drv=[%s] db=[%s]", driver, database);
00211
00212 user[0] = '\0';
00213 password[0] = '\0';
00214
00215 init_login(&login);
00216
00217 if (read_file(&login) == -1)
00218 return DB_FAILED;
00219
00220 for (i = 0; i < login.n; i++) {
00221 if (strcmp(login.data[i].driver, driver) == 0 &&
00222 strcmp(login.data[i].database, database) == 0) {
00223 if (login.data[i].user && strlen(login.data[i].user) > 0)
00224 *user = G_store(login.data[i].user);
00225 else
00226 *user = NULL;
00227
00228 if (login.data[i].password && strlen(login.data[i].password) > 0)
00229 *password = G_store(login.data[i].password);
00230 else
00231 *password = NULL;
00232
00233 break;
00234 }
00235 }
00236
00237 return DB_OK;
00238 }