GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
login.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <grass/gis.h>
8 #include <grass/dbmi.h>
9 #include <grass/glocale.h>
10 
11 typedef struct
12 {
13  char *driver;
14  char *database;
15  char *user;
16  char *password;
17 } DATA;
18 
19 typedef struct
20 {
21  int n, a;
23 } LOGIN;
24 
25 static const char *login_filename(void)
26 {
27  static char *file;
28 
29  if (!file) {
30  file = (char *)malloc(1000);
31  sprintf(file, "%s/.grasslogin65", G_home());
32  }
33  return file;
34 }
35 
36 void init_login(LOGIN * login)
37 {
38  login->n = 0;
39  login->a = 10;
40 
41  login->data = (DATA *) malloc(login->a * sizeof(DATA));
42 }
43 
44 void
45 add_login(LOGIN * login, const char *dr, const char *db, const char *usr,
46  const char *pwd)
47 {
48  if (login->n == login->a) {
49  login->a += 10;
50  login->data =
51  (DATA *) realloc((void *)login->data, login->a * sizeof(DATA));
52  }
53  login->data[login->n].driver = G_store(dr);
54  login->data[login->n].database = G_store(db);
55  login->data[login->n].user = G_store(usr ? usr : "");
56  login->data[login->n].password = G_store(pwd ? pwd : "");
57 
58  login->n++;
59 }
60 
61 /*
62  Read the DB login file if it exists
63  return: -1 error (cannot read file)
64  number of items (0 also if file does not exist)
65  */
66 int read_file(LOGIN * login)
67 {
68  int ret;
69  const char *file;
70  struct stat info;
71  FILE *fd;
72  char buf[2001], dr[500], db[500], usr[500], pwd[500];
73 
74  login->n = 0;
75  file = login_filename();
76 
77  G_debug(3, "DB login file = <%s>", file);
78 
79  if (stat(file, &info) != 0) {
80  G_debug(3, "login file does not exist");
81  return 0;
82  }
83 
84  fd = fopen(file, "r");
85  if (fd == NULL)
86  return -1;
87 
88  while (G_getl2(buf, 2000, fd)) {
89  G_chop(buf);
90 
91  usr[0] = pwd[0] = '\0';
92  ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd);
93 
94  G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s]",
95  ret, dr, db, usr, pwd);
96 
97  if (ret < 2) {
98  G_warning(_("Login file corrupted"));
99  continue;
100  }
101 
102  add_login(login, dr, db, usr, pwd);
103  }
104 
105  fclose(fd);
106 
107  return (login->n);
108 }
109 
110 /*
111  Write the DB login file
112  return: -1 error (cannot read file)
113  0 OK
114  */
115 int write_file(LOGIN * login)
116 {
117  int i;
118  const char *file;
119  FILE *fd;
120 
121  file = login_filename();
122 
123  G_debug(3, "DB login file = <%s>", file);
124 
125  fd = fopen(file, "w");
126  if (fd == NULL)
127  return -1;
128 
129  /* fchmod is not available on Windows */
130  /* fchmod ( fileno(fd), S_IRUSR | S_IWUSR ); */
131  chmod(file, S_IRUSR | S_IWUSR);
132 
133  for (i = 0; i < login->n; i++) {
134  fprintf(fd, "%s|%s", login->data[i].driver, login->data[i].database);
135  if (login->data[i].user) {
136  fprintf(fd, "|%s", login->data[i].user);
137 
138  if (login->data[i].password)
139  fprintf(fd, "|%s", login->data[i].password);
140  }
141  fprintf(fd, "\n");
142  }
143 
144  fclose(fd);
145 
146  return 0;
147 }
148 
154 int
155 db_set_login(const char *driver, const char *database, const char *user,
156  const char *password)
157 {
158  int i, found;
159  LOGIN login;
160 
161  G_debug(3, "db_set_login(): drv=[%s] db=[%s] usr=[%s] pwd=[%s]",
162  driver, database, user, password);
163 
164  init_login(&login);
165 
166  if (read_file(&login) == -1)
167  return DB_FAILED;
168 
169  found = 0;
170  for (i = 0; i < login.n; i++) {
171  if (strcmp(login.data[i].driver, driver) == 0 &&
172  strcmp(login.data[i].database, database) == 0) {
173  if (user)
174  login.data[i].user = G_store(user);
175  else
176  login.data[i].user = G_store("");
177 
178  if (password)
179  login.data[i].password = G_store(password);
180  else
181  login.data[i].password = G_store("");
182 
183  found = 1;
184  break;
185  }
186  }
187 
188  if (!found)
189  add_login(&login, driver, database, user, password);
190 
191  if (write_file(&login) == -1)
192  return DB_FAILED;
193 
194  return DB_OK;
195 }
196 
203 int
204 db_get_login(const char *driver, const char *database, const char **user,
205  const char **password)
206 {
207  int i;
208  LOGIN login;
209 
210  G_debug(3, "db_get_login(): drv=[%s] db=[%s]", driver, database);
211 
212  user[0] = '\0';
213  password[0] = '\0';
214 
215  init_login(&login);
216 
217  if (read_file(&login) == -1)
218  return DB_FAILED;
219 
220  for (i = 0; i < login.n; i++) {
221  if (strcmp(login.data[i].driver, driver) == 0 &&
222  strcmp(login.data[i].database, database) == 0) {
223  if (login.data[i].user && strlen(login.data[i].user) > 0)
224  *user = G_store(login.data[i].user);
225  else
226  *user = NULL;
227 
228  if (login.data[i].password && strlen(login.data[i].password) > 0)
229  *password = G_store(login.data[i].password);
230  else
231  *password = NULL;
232 
233  break;
234  }
235  }
236 
237  return DB_OK;
238 }
Definition: login.c:11
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
char * driver
Definition: login.c:13
int write_file(LOGIN *login)
Definition: login.c:115
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
void init_login(LOGIN *login)
Definition: login.c:36
FILE * fd
Definition: g3dcolor.c:368
char * G_chop(char *line)
Chop leading and trailing white spaces:
Definition: strings.c:418
Definition: login.c:19
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
char * password
Definition: login.c:16
int stat
Definition: g3dcolor.c:369
void * malloc(YYSIZE_T)
char * G_home(void)
user&#39;s home directory
Definition: home.c:32
int db_set_login(const char *driver, const char *database, const char *user, const char *password)
Set user/password for driver/database.
Definition: login.c:155
int a
Definition: login.c:21
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
char * user
Definition: login.c:15
return NULL
Definition: dbfopen.c:1394
Definition: driver.h:25
char * database
Definition: login.c:14
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
fclose(fd)
int read_file(LOGIN *login)
Definition: login.c:66
#define file
int db_get_login(const char *driver, const char *database, const char **user, const char **password)
Get user/password for driver/database if driver/database is not found, user/password are set to NULL...
Definition: login.c:204
void add_login(LOGIN *login, const char *dr, const char *db, const char *usr, const char *pwd)
Definition: login.c:45
DATA * data
Definition: login.c:22
int n
Definition: login.c:21