login.c

Go to the documentation of this file.
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 {
00012     char *driver;
00013     char *database;
00014     char *user;
00015     char *password;
00016 } DATA;
00017 
00018 typedef struct
00019 {
00020     int n, a;
00021     DATA *data;
00022 } LOGIN;
00023 
00024 static const char *login_filename(void)
00025 {
00026     static char *file;
00027 
00028     if (!file) {
00029         file = (char *)malloc(1000);
00030         sprintf(file, "%s/.grasslogin6", G_home());
00031     }
00032     return file;
00033 }
00034 
00035 void 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, const char *dr, const char *db, const char *usr,
00045           const char *pwd)
00046 {
00047     if (login->n == login->a) {
00048         login->a += 10;
00049         login->data =
00050             (DATA *) realloc((void *)login->data, login->a * sizeof(DATA));
00051     }
00052     login->data[login->n].driver = G_store(dr);
00053     login->data[login->n].database = G_store(db);
00054     login->data[login->n].user = G_store(usr ? usr : "");
00055     login->data[login->n].password = G_store(pwd ? pwd : "");
00056 
00057     login->n++;
00058 }
00059 
00060 /*
00061    Read file if exists
00062    return: -1 error (cannot read file)
00063    number of items (0 also if file does not exist)
00064  */
00065 int read_file(LOGIN * login)
00066 {
00067     int ret;
00068     const char *file;
00069     struct stat info;
00070     FILE *fd;
00071     char buf[2001], dr[500], db[500], usr[500], pwd[500];
00072 
00073     login->n = 0;
00074     file = login_filename();
00075 
00076     G_debug(3, "file = %s", file);
00077 
00078     if (stat(file, &info) != 0) {
00079         G_debug(3, "login file does not exist");
00080         return 0;
00081     }
00082 
00083     fd = fopen(file, "r");
00084     if (fd == NULL)
00085         return -1;
00086 
00087     while (fgets(buf, 2000, fd)) {
00088         G_chop(buf);
00089 
00090         usr[0] = pwd[0] = '\0';
00091         ret = sscanf(buf, "%[^ ] %[^ ] %[^ ] %[^ ]", dr, db, usr, pwd);
00092 
00093         G_debug(3, "ret = %d : %s %s %s %s", ret, dr, db, usr, pwd);
00094 
00095         if (ret < 2) {
00096             G_warning("Login file corrupted");
00097             continue;
00098         }
00099 
00100         add_login(login, dr, db, usr, pwd);
00101     }
00102 
00103     fclose(fd);
00104 
00105     return (login->n);
00106 }
00107 
00108 /*
00109    Write file
00110    return: -1 error (cannot read file)
00111    0 OK
00112  */
00113 int write_file(LOGIN * login)
00114 {
00115     int i;
00116     const 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     /* fchmod is not available on Windows */
00128     /* fchmod ( fileno(fd), S_IRUSR | S_IWUSR ); */
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(const char *driver, const char *database, const char *user,
00154              const char *password)
00155 {
00156     int i, found;
00157     LOGIN login;
00158 
00159     G_debug(3, "db_set_login(): %s %s %s %s", driver, database, user,
00160             password);
00161 
00162     init_login(&login);
00163 
00164     if (read_file(&login) == -1)
00165         return DB_FAILED;
00166 
00167     found = 0;
00168     for (i = 0; i < login.n; i++) {
00169         if (strcmp(login.data[i].driver, driver) == 0 &&
00170             strcmp(login.data[i].database, database) == 0) {
00171             if (user)
00172                 login.data[i].user = G_store(user);
00173             else
00174                 login.data[i].user = G_store("");
00175 
00176             if (password)
00177                 login.data[i].password = G_store(password);
00178             else
00179                 login.data[i].password = G_store("");
00180 
00181             found = 1;
00182             break;
00183         }
00184     }
00185 
00186     if (!found)
00187         add_login(&login, driver, database, user, password);
00188 
00189     if (write_file(&login) == -1)
00190         return DB_FAILED;
00191 
00192     return DB_OK;
00193 }
00194 
00201 int
00202 db_get_login(const char *driver, const char *database, const char **user,
00203              const char **password)
00204 {
00205     int i;
00206     LOGIN login;
00207 
00208     G_debug(3, "db_get_login(): %s %s", driver, database);
00209 
00210     user[0] = '\0';
00211     password[0] = '\0';
00212 
00213     init_login(&login);
00214 
00215     if (read_file(&login) == -1)
00216         return DB_FAILED;
00217 
00218     for (i = 0; i < login.n; i++) {
00219         if (strcmp(login.data[i].driver, driver) == 0 &&
00220             strcmp(login.data[i].database, database) == 0) {
00221             if (login.data[i].user && strlen(login.data[i].user) > 0)
00222                 *user = G_store(login.data[i].user);
00223             else
00224                 *user = NULL;
00225 
00226             if (login.data[i].password && strlen(login.data[i].password) > 0)
00227                 *password = G_store(login.data[i].password);
00228             else
00229                 *password = NULL;
00230 
00231             break;
00232         }
00233     }
00234 
00235     return DB_OK;
00236 }

Generated on Thu Jul 16 13:20:15 2009 for GRASS Programmer's Manual by  doxygen 1.5.6