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
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 usr[0] = pwd[0] = '\0';
00090 ret = sscanf (buf, "%[^ ] %[^ ] %[^ ] %[^ ]", dr, db, usr, pwd);
00091
00092 G_debug ( 3, "ret = %d : %s %s %s %s", ret, dr, db, usr, pwd);
00093
00094 if ( ret < 2 ) {
00095 G_warning ( "Login file corrupted" );
00096 continue;
00097 }
00098
00099 add_login ( login, dr, db, usr, pwd );
00100 }
00101
00102 fclose (fd);
00103
00104 return (login->n);
00105 }
00106
00107
00108
00109
00110
00111
00112 int
00113 write_file ( LOGIN *login )
00114 {
00115 int i;
00116 char *file;
00117 FILE *fd;
00118
00119 file = login_filename();
00120
00121 G_debug ( 3, "file = %s", file );
00122
00123 fd = fopen (file, "w");
00124 if (fd == NULL)
00125 return -1;
00126
00127
00128
00129 chmod ( file, S_IRUSR | S_IWUSR );
00130
00131 for ( i = 0; i < login->n; i++ ) {
00132 fprintf ( fd, "%s %s", login->data[i].driver, login->data[i].database );
00133 if ( login->data[i].user ) {
00134 fprintf ( fd, " %s", login->data[i].user );
00135
00136 if ( login->data[i].password )
00137 fprintf ( fd, " %s", login->data[i].password );
00138 }
00139 fprintf ( fd, "\n" );
00140 }
00141
00142 fclose (fd);
00143
00144 return 0;
00145 }
00146
00152 int
00153 db_set_login ( char *driver, char *database, char *user, char *password )
00154 {
00155 int i, found;
00156 LOGIN login;
00157
00158 G_debug ( 3, "db_set_login(): %s %s %s %s", driver, database, user, password );
00159
00160 init_login ( &login );
00161
00162 if ( read_file ( &login ) == -1 )
00163 return DB_FAILED;
00164
00165 found = 0;
00166 for ( i = 0; i < login.n; i++ ) {
00167 if ( strcmp(login.data[i].driver,driver) == 0 && strcmp(login.data[i].database,database) == 0 ) {
00168 if ( user )
00169 login.data[i].user = G_store ( user );
00170 else
00171 login.data[i].user = G_store ( "" );
00172
00173 if ( password )
00174 login.data[i].password = G_store ( password );
00175 else
00176 login.data[i].password = G_store ( "" );
00177
00178 found = 1;
00179 break;
00180 }
00181 }
00182
00183 if ( !found )
00184 add_login ( &login, driver, database, user, password );
00185
00186 if ( write_file ( &login ) == -1 )
00187 return DB_FAILED;
00188
00189 return DB_OK;
00190 }
00191
00198 int
00199 db_get_login ( char *driver, char *database, char **user, char **password )
00200 {
00201 int i;
00202 LOGIN login;
00203
00204 G_debug ( 3, "db_get_login(): %s %s", driver, database );
00205
00206 user[0] = '\0';
00207 password[0] = '\0';
00208
00209 init_login ( &login );
00210
00211 if ( read_file ( &login ) == -1 )
00212 return DB_FAILED;
00213
00214 for ( i = 0; i < login.n; i++ ) {
00215 if ( strcmp(login.data[i].driver,driver) == 0 && strcmp(login.data[i].database,database) == 0 ) {
00216 if ( login.data[i].user && strlen(login.data[i].user) > 0 )
00217 *user = G_store ( login.data[i].user );
00218 else
00219 *user = NULL;
00220
00221 if ( login.data[i].password && strlen(login.data[i].password) > 0 )
00222 *password = G_store ( login.data[i].password );
00223 else
00224 *password = NULL;
00225
00226 break;
00227 }
00228 }
00229
00230 return DB_OK;
00231 }
00232