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