1
|
1 /*****************************************************************************/
|
|
2 /* config_file.c - config file routines */
|
122
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
1
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp.h"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
122
|
23 void
|
|
24 gftp_add_bookmark (gftp_bookmarks_var * newentry)
|
1
|
25 {
|
122
|
26 gftp_bookmarks_var * preventry, * folderentry, * endentry;
|
|
27 char *curpos;
|
1
|
28
|
122
|
29 if (!newentry->protocol)
|
124
|
30 newentry->protocol = g_strdup ("FTP");
|
1
|
31
|
122
|
32 /* We have to create the folders. For example, if we have
|
|
33 Debian Sites/Debian, we have to create a Debian Sites entry */
|
|
34 preventry = gftp_bookmarks;
|
|
35 if (preventry->children != NULL)
|
1
|
36 {
|
122
|
37 endentry = preventry->children;
|
|
38 while (endentry->next != NULL)
|
|
39 endentry = endentry->next;
|
|
40 }
|
|
41 else
|
|
42 endentry = NULL;
|
|
43 curpos = newentry->path;
|
|
44 while ((curpos = strchr (curpos, '/')) != NULL)
|
|
45 {
|
|
46 *curpos = '\0';
|
|
47 /* See if we already made this folder */
|
|
48 if ((folderentry = (gftp_bookmarks_var *)
|
|
49 g_hash_table_lookup (gftp_bookmarks_htable, newentry->path)) == NULL)
|
1
|
50 {
|
122
|
51 /* Allocate the individual folder. We have to do this for the edit
|
|
52 bookmarks feature */
|
|
53 folderentry = g_malloc0 (sizeof (*folderentry));
|
124
|
54 folderentry->path = g_strdup (newentry->path);
|
122
|
55 folderentry->prev = preventry;
|
|
56 folderentry->isfolder = 1;
|
|
57 g_hash_table_insert (gftp_bookmarks_htable, folderentry->path,
|
|
58 folderentry);
|
|
59 if (preventry->children == NULL)
|
|
60 preventry->children = folderentry;
|
1
|
61 else
|
122
|
62 endentry->next = folderentry;
|
|
63 preventry = folderentry;
|
|
64 endentry = NULL;
|
1
|
65 }
|
|
66 else
|
|
67 {
|
122
|
68 preventry = folderentry;
|
|
69 if (preventry->children != NULL)
|
|
70 {
|
|
71 endentry = preventry->children;
|
|
72 while (endentry->next != NULL)
|
|
73 endentry = endentry->next;
|
|
74 }
|
|
75 else
|
|
76 endentry = NULL;
|
1
|
77 }
|
122
|
78 *curpos = '/';
|
|
79 curpos++;
|
1
|
80 }
|
|
81
|
122
|
82 /* Get the parent node */
|
|
83 if ((curpos = strrchr (newentry->path, '/')) == NULL)
|
|
84 preventry = gftp_bookmarks;
|
|
85 else
|
1
|
86 {
|
122
|
87 *curpos = '\0';
|
|
88 preventry = (gftp_bookmarks_var *)
|
|
89 g_hash_table_lookup (gftp_bookmarks_htable, newentry->path);
|
|
90 *curpos = '/';
|
1
|
91 }
|
|
92
|
122
|
93 if (preventry->children != NULL)
|
1
|
94 {
|
122
|
95 endentry = preventry->children;
|
|
96 while (endentry->next != NULL)
|
|
97 endentry = endentry->next;
|
|
98 endentry->next = newentry;
|
1
|
99 }
|
122
|
100 else
|
|
101 preventry->children = newentry;
|
|
102 newentry->prev = preventry;
|
|
103 newentry->next = NULL;
|
|
104 g_hash_table_insert (gftp_bookmarks_htable, newentry->path, newentry);
|
1
|
105 }
|
|
106
|
|
107
|
320
|
108 static int
|
|
109 copyfile (char *source, char *dest)
|
|
110 {
|
|
111 int srcfd, destfd;
|
|
112 char buf[8192];
|
|
113 ssize_t n;
|
|
114
|
|
115 if ((srcfd = gftp_fd_open (NULL, source, O_RDONLY, 0)) == -1)
|
|
116 {
|
|
117 printf (_("Error: Cannot open local file %s: %s\n"),
|
|
118 source, g_strerror (errno));
|
|
119 exit (1);
|
|
120 }
|
|
121
|
|
122 if ((destfd = gftp_fd_open (NULL, dest, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
|
|
123 {
|
|
124 printf (_("Error: Cannot open local file %s: %s\n"),
|
|
125 dest, g_strerror (errno));
|
|
126 close (srcfd);
|
|
127 exit (1);
|
|
128 }
|
|
129
|
|
130 while ((n = read (srcfd, buf, sizeof (buf))) > 0)
|
|
131 {
|
|
132 if (write (destfd, buf, n) == -1)
|
|
133 {
|
|
134 printf (_("Error: Could not write to socket: %s\n"),
|
|
135 g_strerror (errno));
|
|
136 exit (1);
|
|
137 }
|
|
138 }
|
|
139
|
|
140 if (n == -1)
|
|
141 {
|
|
142 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno));
|
|
143 exit (1);
|
|
144 }
|
|
145
|
|
146 if (close (srcfd) == -1)
|
|
147 {
|
|
148 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno));
|
|
149 exit (1);
|
|
150 }
|
|
151
|
|
152 if (close (destfd) == -1)
|
|
153 {
|
|
154 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno));
|
|
155 exit (1);
|
|
156 }
|
|
157
|
|
158 return (1);
|
|
159 }
|
|
160
|
|
161
|
122
|
162 static void
|
124
|
163 gftp_read_bookmarks (char *global_data_path)
|
1
|
164 {
|
|
165 char *tempstr, *temp1str, buf[255], *curpos;
|
199
|
166 gftp_config_vars * global_entry;
|
122
|
167 gftp_bookmarks_var * newentry;
|
1
|
168 FILE *bmfile;
|
|
169 size_t len;
|
|
170 int line;
|
|
171
|
|
172 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
173 {
|
|
174 printf (_("gFTP Error: Bad bookmarks file name %s\n"), BOOKMARKS_FILE);
|
87
|
175 exit (1);
|
1
|
176 }
|
|
177
|
|
178 if (access (tempstr, F_OK) == -1)
|
|
179 {
|
124
|
180 temp1str = g_strdup_printf ("%s/bookmarks", global_data_path);
|
1
|
181 if (access (temp1str, F_OK) == -1)
|
|
182 {
|
|
183 printf (_("Warning: Cannot find master bookmark file %s\n"),
|
|
184 temp1str);
|
|
185 g_free (temp1str);
|
|
186 return;
|
|
187 }
|
|
188 copyfile (temp1str, tempstr);
|
|
189 g_free (temp1str);
|
|
190 }
|
58
|
191
|
1
|
192 if ((bmfile = fopen (tempstr, "r")) == NULL)
|
|
193 {
|
|
194 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"), tempstr,
|
|
195 g_strerror (errno));
|
87
|
196 exit (1);
|
1
|
197 }
|
|
198 g_free (tempstr);
|
|
199
|
|
200 line = 0;
|
|
201 newentry = NULL;
|
|
202 while (fgets (buf, sizeof (buf), bmfile))
|
|
203 {
|
460
|
204 len = strlen (buf);
|
107
|
205 if (len > 0 && buf[len - 1] == '\n')
|
516
|
206 buf[--len] = '\0';
|
107
|
207 if (len > 0 && buf[len - 1] == '\r')
|
516
|
208 buf[--len] = '\0';
|
1
|
209 line++;
|
|
210
|
|
211 if (*buf == '[')
|
|
212 {
|
|
213 newentry = g_malloc0 (sizeof (*newentry));
|
516
|
214 for (; buf[len - 1] == ' ' || buf[len - 1] == ']'; buf[--len] = '\0');
|
124
|
215 newentry->path = g_strdup (buf + 1);
|
1
|
216 newentry->isfolder = 0;
|
122
|
217 gftp_add_bookmark (newentry);
|
1
|
218 }
|
|
219 else if (strncmp (buf, "hostname", 8) == 0 && newentry)
|
|
220 {
|
|
221 curpos = buf + 9;
|
|
222 if (newentry->hostname)
|
|
223 g_free (newentry->hostname);
|
124
|
224 newentry->hostname = g_strdup (curpos);
|
1
|
225 }
|
|
226 else if (strncmp (buf, "port", 4) == 0 && newentry)
|
|
227 newentry->port = strtol (buf + 5, NULL, 10);
|
|
228 else if (strncmp (buf, "protocol", 8) == 0 && newentry)
|
|
229 {
|
|
230 curpos = buf + 9;
|
|
231 if (newentry->protocol)
|
|
232 g_free (newentry->protocol);
|
124
|
233 newentry->protocol = g_strdup (curpos);
|
1
|
234 }
|
|
235 else if (strncmp (buf, "remote directory", 16) == 0 && newentry)
|
|
236 {
|
|
237 curpos = buf + 17;
|
|
238 if (newentry->remote_dir)
|
|
239 g_free (newentry->remote_dir);
|
124
|
240 newentry->remote_dir = g_strdup (curpos);
|
1
|
241 }
|
|
242 else if (strncmp (buf, "local directory", 15) == 0 && newentry)
|
|
243 {
|
|
244 curpos = buf + 16;
|
|
245 if (newentry->local_dir)
|
|
246 g_free (newentry->local_dir);
|
124
|
247 newentry->local_dir = g_strdup (curpos);
|
1
|
248 }
|
|
249 else if (strncmp (buf, "username", 8) == 0 && newentry)
|
|
250 {
|
|
251 curpos = buf + 9;
|
|
252 if (newentry->user)
|
|
253 g_free (newentry->user);
|
124
|
254 newentry->user = g_strdup (curpos);
|
1
|
255 }
|
|
256 else if (strncmp (buf, "password", 8) == 0 && newentry)
|
|
257 {
|
|
258 curpos = buf + 9;
|
|
259 if (newentry->pass)
|
|
260 g_free (newentry->pass);
|
330
|
261
|
|
262 /* Always try to descramble passords. If the password is not
|
|
263 scrambled, descramble_password returns the string unchanged */
|
|
264 newentry->pass = gftp_descramble_password (curpos);
|
1
|
265 newentry->save_password = *newentry->pass != '\0';
|
|
266 }
|
|
267 else if (strncmp (buf, "account", 7) == 0 && newentry)
|
|
268 {
|
|
269 curpos = buf + 8;
|
|
270 if (newentry->acct)
|
|
271 g_free (newentry->acct);
|
124
|
272 newentry->acct = g_strdup (curpos);
|
1
|
273 }
|
199
|
274 else if (*buf == '#' || *buf == '\0')
|
|
275 continue;
|
|
276 else
|
1
|
277 {
|
199
|
278 if ((curpos = strchr (buf, '=')) == NULL)
|
|
279 continue;
|
|
280 *curpos = '\0';
|
|
281
|
|
282 if ((global_entry = g_hash_table_lookup (gftp_global_options_htable,
|
|
283 buf)) == NULL ||
|
|
284 gftp_option_types[global_entry->otype].read_function == NULL)
|
|
285 {
|
|
286 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"),
|
|
287 line, buf);
|
|
288 continue;
|
|
289 }
|
|
290
|
|
291 if (newentry->local_options_hash == NULL)
|
|
292 newentry->local_options_hash = g_hash_table_new (string_hash_function,
|
|
293 string_hash_compare);
|
|
294
|
|
295 newentry->num_local_options_vars++;
|
|
296 newentry->local_options_vars = g_realloc (newentry->local_options_vars,
|
|
297 sizeof (gftp_config_vars) * newentry->num_local_options_vars);
|
|
298
|
|
299 memcpy (&newentry->local_options_vars[newentry->num_local_options_vars - 1], global_entry,
|
|
300 sizeof (newentry->local_options_vars[newentry->num_local_options_vars - 1]));
|
|
301
|
|
302 newentry->local_options_vars[newentry->num_local_options_vars - 1].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
303 newentry->local_options_vars[newentry->num_local_options_vars - 1].value = NULL;
|
|
304
|
|
305 if (gftp_option_types[global_entry->otype].read_function (curpos + 1,
|
|
306 &newentry->local_options_vars[newentry->num_local_options_vars - 1], line) != 0)
|
|
307 {
|
|
308 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"),
|
|
309 line, buf);
|
|
310 continue;
|
|
311 }
|
|
312
|
|
313 g_hash_table_insert (newentry->local_options_hash, newentry->local_options_vars[newentry->num_local_options_vars - 1].key, &newentry->local_options_vars[newentry->num_local_options_vars - 1]);
|
1
|
314 }
|
|
315 }
|
|
316 }
|
|
317
|
|
318
|
198
|
319 int
|
|
320 gftp_config_parse_args (char *str, int numargs, int lineno, char **first, ...)
|
1
|
321 {
|
|
322 char *curpos, *endpos, *pos, **dest, tempchar;
|
|
323 int ret, has_colon;
|
|
324 va_list argp;
|
|
325
|
|
326 ret = 1;
|
|
327 va_start (argp, first);
|
|
328 curpos = str;
|
|
329 dest = first;
|
|
330 *dest = NULL;
|
|
331 while (numargs > 0)
|
|
332 {
|
|
333 has_colon = 0;
|
|
334 if (numargs > 1)
|
|
335 {
|
|
336 if ((endpos = strchr (curpos, ':')) == NULL)
|
|
337 {
|
|
338 printf (_("gFTP Warning: Line %d doesn't have enough arguments\n"),
|
|
339 lineno);
|
|
340 ret = 0;
|
|
341 endpos = curpos + strlen (curpos);
|
|
342 }
|
|
343 else
|
|
344 {
|
|
345 /* Allow colons inside the fields. If you want a colon inside a
|
|
346 field, just put 2 colons in there */
|
|
347 while (endpos != NULL && *(endpos - 1) == '\\')
|
|
348 {
|
|
349 endpos = strchr (endpos + 1, ':');
|
|
350 has_colon = 1;
|
|
351 }
|
|
352 }
|
|
353 }
|
|
354 else
|
|
355 endpos = curpos + strlen (curpos);
|
|
356
|
|
357 *dest = g_malloc (endpos - curpos + 1);
|
|
358 tempchar = *endpos;
|
|
359 *endpos = '\0';
|
|
360 strcpy (*dest, curpos);
|
|
361 *endpos = tempchar;
|
|
362 if (has_colon)
|
|
363 {
|
|
364 pos = *dest;
|
|
365 curpos = *dest;
|
|
366 while (*pos != '\0')
|
|
367 {
|
|
368 if (*pos != '\\' && *(pos + 1) != ':')
|
|
369 *curpos++ = *pos++;
|
|
370 else
|
|
371 pos++;
|
|
372 }
|
|
373 *curpos = '\0';
|
|
374 }
|
|
375 if (*endpos == '\0')
|
|
376 break;
|
|
377 curpos = endpos + 1;
|
|
378 if (numargs > 1)
|
|
379 {
|
|
380 dest = va_arg (argp, char **);
|
|
381 *dest = NULL;
|
|
382 }
|
|
383 numargs--;
|
|
384 }
|
|
385
|
|
386 while (numargs > 1)
|
|
387 {
|
|
388 dest = va_arg (argp, char **);
|
|
389 *dest = g_malloc (1);
|
|
390 **dest = '\0';
|
|
391 numargs--;
|
|
392 }
|
|
393 va_end (argp);
|
516
|
394 return (ret);
|
1
|
395 }
|
|
396
|
|
397
|
122
|
398 static void *
|
|
399 gftp_config_read_str (char *buf, int line)
|
|
400 {
|
516
|
401 return (g_strdup (buf));
|
122
|
402 }
|
|
403
|
|
404
|
|
405 static void
|
|
406 gftp_config_write_str (FILE *fd, void *data)
|
|
407 {
|
|
408 fprintf (fd, "%s", (char *) data);
|
|
409 }
|
|
410
|
|
411
|
|
412 static void *
|
|
413 gftp_config_read_proxy (char *buf, int line)
|
|
414 {
|
|
415 gftp_proxy_hosts * host;
|
|
416 unsigned int nums[4];
|
|
417 char *pos;
|
|
418
|
|
419 host = g_malloc0 (sizeof (*host));
|
|
420 if ((pos = strchr (buf, '/')) == NULL)
|
|
421 host->domain = g_strdup (buf);
|
|
422 else
|
|
423 {
|
|
424 *pos = '\0';
|
|
425 sscanf (buf, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], &nums[3]);
|
|
426 host->ipv4_network_address =
|
|
427 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
428
|
|
429 if (strchr (pos + 1, '.') == NULL)
|
|
430 host->ipv4_netmask = 0xffffffff << (32 - strtol (pos + 1, NULL, 10));
|
|
431 else
|
|
432 {
|
|
433 sscanf (pos + 1, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2],
|
|
434 &nums[3]);
|
|
435 host->ipv4_netmask =
|
|
436 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
437 }
|
|
438 }
|
|
439
|
|
440 return (host);
|
|
441 }
|
|
442
|
|
443
|
|
444 static void
|
|
445 gftp_config_write_proxy (FILE *fd, void *data)
|
|
446 {
|
|
447 gftp_proxy_hosts * host;
|
|
448
|
|
449 host = data;
|
|
450
|
|
451 if (host->domain)
|
|
452 fprintf (fd, "%s", host->domain);
|
|
453 else
|
|
454 fprintf (fd, "%d.%d.%d.%d/%d.%d.%d.%d",
|
|
455 host->ipv4_network_address >> 24 & 0xff,
|
|
456 host->ipv4_network_address >> 16 & 0xff,
|
|
457 host->ipv4_network_address >> 8 & 0xff,
|
|
458 host->ipv4_network_address & 0xff,
|
|
459 host->ipv4_netmask >> 24 & 0xff,
|
|
460 host->ipv4_netmask >> 16 & 0xff,
|
|
461 host->ipv4_netmask >> 8 & 0xff,
|
|
462 host->ipv4_netmask & 0xff);
|
|
463 }
|
|
464
|
|
465
|
|
466 static void *
|
|
467 gftp_config_read_ext (char *buf, int line)
|
|
468 {
|
|
469 gftp_file_extensions * tempext;
|
|
470
|
|
471 tempext = g_malloc (sizeof (*tempext));
|
198
|
472 gftp_config_parse_args (buf, 4, line, &tempext->ext, &tempext->filename,
|
|
473 &tempext->ascii_binary, &tempext->view_program);
|
122
|
474
|
|
475 tempext->stlen = strlen (tempext->ext);
|
|
476
|
|
477 return (tempext);
|
|
478 }
|
|
479
|
|
480
|
|
481 static void
|
|
482 gftp_config_write_ext (FILE *fd, void *data)
|
|
483 {
|
|
484 gftp_file_extensions * tempext;
|
|
485
|
|
486 tempext = data;
|
|
487 fprintf (fd, "%s:%s:%c:%s", tempext->ext, tempext->filename,
|
|
488 *tempext->ascii_binary == '\0' ? ' ' : *tempext->ascii_binary,
|
|
489 tempext->view_program);
|
|
490 }
|
|
491
|
|
492
|
516
|
493 static gftp_config_list_vars gftp_config_list[] = {
|
122
|
494 {"dont_use_proxy", gftp_config_read_proxy, gftp_config_write_proxy,
|
|
495 NULL, 0,
|
|
496 N_("This section specifies which hosts are on the local subnet and won't need to go out the proxy server (if available). Syntax: dont_use_proxy=.domain or dont_use_proxy=network number/netmask")},
|
|
497 {"ext", gftp_config_read_ext, gftp_config_write_ext,
|
|
498 NULL, 0,
|
|
499 N_("ext=file extenstion:XPM file:Ascii or Binary (A or B):viewer program. Note: All arguments except the file extension are optional")},
|
|
500 {"localhistory", gftp_config_read_str, gftp_config_write_str,
|
|
501 NULL, 0, NULL},
|
|
502 {"remotehistory", gftp_config_read_str, gftp_config_write_str,
|
|
503 NULL, 0, NULL},
|
|
504 {"hosthistory", gftp_config_read_str, gftp_config_write_str,
|
|
505 NULL, 0, NULL},
|
|
506 {"porthistory", gftp_config_read_str, gftp_config_write_str,
|
|
507 NULL, 0, NULL},
|
|
508 {"userhistory", gftp_config_read_str, gftp_config_write_str,
|
|
509 NULL, 0, NULL},
|
|
510 {NULL, NULL, NULL,
|
|
511 NULL, 0, NULL}
|
|
512 };
|
|
513
|
|
514
|
|
515 static void
|
|
516 gftp_setup_global_options (gftp_config_vars * cvars)
|
|
517 {
|
|
518 int i;
|
|
519
|
|
520 for (i=0; cvars[i].key != NULL; i++)
|
|
521 {
|
136
|
522 if (cvars[i].key != NULL && *cvars[i].key != '\0')
|
122
|
523 g_hash_table_insert (gftp_global_options_htable,
|
|
524 cvars[i].key, &cvars[i]);
|
|
525 }
|
|
526 }
|
|
527
|
|
528
|
|
529 void
|
124
|
530 gftp_read_config_file (char *global_data_path)
|
122
|
531 {
|
|
532 char *tempstr, *temp1str, *curpos, buf[255];
|
|
533 gftp_config_list_vars * tmplistvar;
|
|
534 gftp_config_vars * tmpconfigvar;
|
125
|
535 char **protocol_list;
|
122
|
536 FILE *conffile;
|
|
537 int line, i;
|
|
538 size_t len;
|
|
539
|
|
540 gftp_global_options_htable = g_hash_table_new (string_hash_function,
|
|
541 string_hash_compare);
|
|
542
|
|
543 gftp_register_config_vars (gftp_global_config_vars);
|
|
544
|
125
|
545 protocol_list = NULL;
|
122
|
546 for (i=0; gftp_protocols[i].register_options != NULL; i++)
|
|
547 {
|
125
|
548 if (gftp_protocols[i].shown)
|
|
549 {
|
|
550 protocol_list = g_realloc (protocol_list, sizeof (char *) * (i + 2));
|
|
551 protocol_list[i] = gftp_protocols[i].name;
|
|
552 protocol_list[i + 1] = NULL;
|
|
553 }
|
|
554
|
122
|
555 if (gftp_protocols[i].register_options != NULL)
|
|
556 gftp_protocols[i].register_options ();
|
|
557 }
|
|
558
|
125
|
559 if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
560 "default_protocol")) != NULL)
|
201
|
561 {
|
|
562 tmpconfigvar->listdata = protocol_list;
|
|
563 tmpconfigvar->flags |= GFTP_CVARS_FLAGS_DYNLISTMEM;
|
|
564 }
|
125
|
565
|
122
|
566 gftp_config_list_htable = g_hash_table_new (string_hash_function,
|
|
567 string_hash_compare);
|
|
568
|
|
569 for (i=0; gftp_config_list[i].key != NULL; i++)
|
|
570 {
|
|
571 g_hash_table_insert (gftp_config_list_htable, gftp_config_list[i].key,
|
|
572 &gftp_config_list[i]);
|
|
573 }
|
|
574
|
|
575 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
576 {
|
|
577 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
578 exit (1);
|
|
579 }
|
|
580
|
|
581 if (access (tempstr, F_OK) == -1)
|
|
582 {
|
|
583 temp1str = expand_path (BASE_CONF_DIR);
|
|
584 if (access (temp1str, F_OK) == -1)
|
|
585 {
|
|
586 if (mkdir (temp1str, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
|
|
587 {
|
|
588 printf (_("gFTP Error: Could not make directory %s: %s\n"),
|
|
589 temp1str, g_strerror (errno));
|
|
590 exit (1);
|
|
591 }
|
|
592 }
|
|
593 g_free (temp1str);
|
|
594
|
124
|
595 temp1str = g_strdup_printf ("%s/gftprc", global_data_path);
|
122
|
596 if (access (temp1str, F_OK) == -1)
|
|
597 {
|
|
598 printf (_("gFTP Error: Cannot find master config file %s\n"),
|
|
599 temp1str);
|
|
600 printf (_("Did you do a make install?\n"));
|
|
601 exit (1);
|
|
602 }
|
|
603 copyfile (temp1str, tempstr);
|
|
604 g_free (temp1str);
|
|
605 }
|
|
606
|
|
607 if ((conffile = fopen (tempstr, "r")) == NULL)
|
|
608 {
|
|
609 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
610 g_strerror (errno));
|
|
611 exit (1);
|
|
612 }
|
|
613 g_free (tempstr);
|
|
614
|
|
615 line = 0;
|
|
616 while (fgets (buf, sizeof (buf), conffile))
|
|
617 {
|
|
618 len = strlen (buf);
|
|
619 if (len > 0 && buf[len - 1] == '\n')
|
|
620 buf[--len] = '\0';
|
|
621 if (len > 0 && buf[len - 1] == '\r')
|
|
622 buf[--len] = '\0';
|
|
623 line++;
|
|
624
|
|
625 if (*buf == '#' || *buf == '\0')
|
|
626 continue;
|
|
627
|
|
628 if ((curpos = strchr (buf, '=')) == NULL)
|
|
629 continue;
|
|
630
|
|
631 *curpos = '\0';
|
|
632
|
|
633 if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
634 buf)) != NULL)
|
|
635 {
|
|
636 tmplistvar->list = g_list_append (tmplistvar->list,
|
|
637 tmplistvar->read_func (curpos + 1,
|
|
638 line));
|
|
639 tmplistvar->num_items++;
|
|
640 }
|
|
641 else if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
642 buf)) != NULL &&
|
|
643 gftp_option_types[tmpconfigvar->otype].read_function != NULL)
|
|
644 {
|
|
645 if (gftp_option_types[tmpconfigvar->otype].read_function (curpos + 1,
|
|
646 tmpconfigvar, line) != 0)
|
|
647 {
|
|
648 printf (_("Terminating due to parse errors at line %d in the config file\n"), line);
|
|
649 exit (1);
|
|
650 }
|
|
651 }
|
|
652 else
|
|
653 {
|
|
654 printf (_("gFTP Warning: Skipping line %d in config file: %s\n"),
|
|
655 line, buf);
|
|
656 }
|
|
657 }
|
|
658
|
|
659 if ((tempstr = expand_path (LOG_FILE)) == NULL)
|
|
660 {
|
|
661 printf (_("gFTP Error: Bad log file name %s\n"), LOG_FILE);
|
|
662 exit (1);
|
|
663 }
|
|
664
|
|
665 if ((gftp_logfd = fopen (tempstr, "w")) == NULL)
|
|
666 {
|
|
667 printf (_("gFTP Warning: Cannot open %s for writing: %s\n"),
|
|
668 tempstr, g_strerror (errno));
|
|
669 }
|
|
670 g_free (tempstr);
|
|
671
|
|
672 gftp_bookmarks = g_malloc0 (sizeof (*gftp_bookmarks));
|
|
673 gftp_bookmarks->isfolder = 1;
|
|
674 gftp_bookmarks->path = g_malloc0 (1);
|
|
675 gftp_bookmarks_htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
676
|
124
|
677 gftp_read_bookmarks (global_data_path);
|
122
|
678 }
|
|
679
|
|
680
|
|
681 static void
|
|
682 write_comment (FILE * fd, const char *comment)
|
|
683 {
|
|
684 const char *pos, *endpos;
|
|
685
|
|
686 fwrite ("# ", 1, 2, fd);
|
|
687 pos = comment;
|
|
688 while (strlen (pos) > 76)
|
|
689 {
|
|
690 for (endpos = pos + 76; *endpos != ' ' && endpos > pos; endpos--);
|
|
691 if (endpos == pos)
|
|
692 {
|
|
693 for (endpos = pos + 76; *endpos != ' ' && *endpos != '\0';
|
|
694 endpos++);
|
|
695 }
|
|
696 fwrite (pos, 1, endpos - pos, fd);
|
|
697 fwrite ("\n# ", 1, 3, fd);
|
|
698 if (*endpos == '\0')
|
|
699 {
|
|
700 pos = endpos;
|
|
701 break;
|
|
702 }
|
|
703 else
|
|
704 pos = endpos + 1;
|
|
705 }
|
|
706 if (strlen (pos) > 1)
|
|
707 {
|
|
708 fwrite (pos, 1, strlen (pos), fd);
|
|
709 fwrite ("\n", 1, 1, fd);
|
|
710 }
|
|
711 }
|
|
712
|
|
713
|
|
714 void
|
|
715 gftp_write_bookmarks_file (void)
|
|
716 {
|
387
|
717 char *bmhdr, *pwhdr, *tempstr, *password, buf[256];
|
122
|
718 gftp_bookmarks_var * tempentry;
|
|
719 FILE * bmfile;
|
199
|
720 int i;
|
122
|
721
|
|
722 bmhdr = N_("Bookmarks file for gFTP. Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org>. Warning: Any comments that you add to this file WILL be overwritten");
|
332
|
723 pwhdr = N_("Note: The passwords contained inside this file are scrambled. This algorithm is not secure. This is to avoid your password being easily remembered by someone standing over your shoulder while you're editing this file. Prior to this, all passwords were stored in plaintext.");
|
122
|
724
|
|
725 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
726 {
|
|
727 printf (_("gFTP Error: Bad bookmarks file name %s\n"), CONFIG_FILE);
|
|
728 exit (1);
|
|
729 }
|
|
730
|
|
731 if ((bmfile = fopen (tempstr, "w+")) == NULL)
|
|
732 {
|
|
733 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"),
|
|
734 CONFIG_FILE, g_strerror (errno));
|
|
735 exit (1);
|
|
736 }
|
|
737
|
|
738 g_free (tempstr);
|
|
739
|
|
740 write_comment (bmfile, _(bmhdr));
|
332
|
741 write_comment (bmfile, _(pwhdr));
|
122
|
742 fwrite ("\n", 1, 1, bmfile);
|
|
743
|
|
744 tempentry = gftp_bookmarks->children;
|
|
745 while (tempentry != NULL)
|
|
746 {
|
|
747 if (tempentry->children != NULL)
|
|
748 {
|
|
749 tempentry = tempentry->children;
|
|
750 continue;
|
|
751 }
|
330
|
752
|
122
|
753 tempstr = tempentry->path;
|
|
754 while (*tempstr == '/')
|
|
755 tempstr++;
|
330
|
756
|
|
757 if (tempentry->save_password && tempentry->pass != NULL)
|
332
|
758 password = gftp_scramble_password (tempentry->pass);
|
330
|
759 else
|
|
760 password = NULL;
|
|
761
|
122
|
762 fprintf (bmfile,
|
|
763 "[%s]\nhostname=%s\nport=%d\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nusername=%s\npassword=%s\naccount=%s\n",
|
|
764 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname,
|
|
765 tempentry->port, tempentry->protocol == NULL
|
|
766 || *tempentry->protocol ==
|
|
767 '\0' ? gftp_protocols[0].name : tempentry->protocol,
|
|
768 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir,
|
|
769 tempentry->local_dir == NULL ? "" : tempentry->local_dir,
|
|
770 tempentry->user == NULL ? "" : tempentry->user,
|
330
|
771 password == NULL ? "" : password,
|
122
|
772 tempentry->acct == NULL ? "" : tempentry->acct);
|
|
773
|
330
|
774 if (password != NULL)
|
|
775 g_free(password);
|
|
776
|
199
|
777 if (tempentry->local_options_vars != NULL)
|
|
778 {
|
|
779 for (i=0; i<tempentry->num_local_options_vars; i++)
|
|
780 {
|
387
|
781 gftp_option_types[tempentry->local_options_vars[i].otype].write_function (&tempentry->local_options_vars[i], buf, sizeof (buf), 1);
|
|
782
|
|
783 fprintf (bmfile, "%s=%s\n", tempentry->local_options_vars[i].key,
|
|
784 buf);
|
199
|
785 }
|
|
786 }
|
122
|
787
|
|
788 fprintf (bmfile, "\n");
|
|
789
|
|
790 if (tempentry->next == NULL)
|
|
791 {
|
|
792 tempentry = tempentry->prev;
|
|
793 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
794 tempentry = tempentry->prev;
|
|
795 tempentry = tempentry->next;
|
|
796 }
|
|
797 else
|
|
798 tempentry = tempentry->next;
|
|
799 }
|
|
800
|
|
801 fclose (bmfile);
|
|
802 }
|
|
803
|
|
804
|
|
805 void
|
|
806 gftp_write_config_file (void)
|
|
807 {
|
387
|
808 char *tempstr, buf[256];
|
122
|
809 gftp_config_vars * cv;
|
|
810 GList *templist;
|
|
811 FILE *conffile;
|
|
812 int i;
|
|
813
|
|
814 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
815 {
|
|
816 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
817 exit (1);
|
|
818 }
|
|
819
|
|
820 if ((conffile = fopen (tempstr, "w+")) == NULL)
|
|
821 {
|
|
822 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
823 g_strerror (errno));
|
|
824 exit (1);
|
|
825 }
|
|
826
|
|
827 g_free (tempstr);
|
|
828
|
|
829 write_comment (conffile, _("Config file for gFTP. Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org>. Warning: Any comments that you add to this file WILL be overwritten. If a entry has a (*) in it's comment, you can't change it inside gFTP"));
|
|
830
|
|
831 for (templist = gftp_options_list;
|
|
832 templist != NULL;
|
|
833 templist = templist->next)
|
|
834 {
|
|
835 cv = templist->data;
|
|
836
|
|
837 for (i=0; cv[i].key != NULL; i++)
|
|
838 {
|
|
839 if (gftp_option_types[cv[i].otype].write_function == NULL ||
|
|
840 *cv[i].key == '\0')
|
|
841 continue;
|
|
842
|
|
843 fprintf (conffile, "\n");
|
|
844 if (cv[i].comment != NULL)
|
|
845 write_comment (conffile, _(cv[i].comment));
|
|
846
|
387
|
847 gftp_option_types[cv[i].otype].write_function (&cv[i], buf,
|
|
848 sizeof (buf), 1);
|
|
849 fprintf (conffile, "%s=%s\n", cv[i].key, buf);
|
122
|
850 }
|
|
851 }
|
|
852
|
218
|
853 for (i=0; gftp_config_list[i].key != NULL; i++)
|
122
|
854 {
|
218
|
855 if (gftp_config_list[i].header == NULL &&
|
|
856 gftp_config_list[i].list == NULL)
|
|
857 continue;
|
|
858
|
122
|
859 fprintf (conffile, "\n");
|
|
860 if (gftp_config_list[i].header != NULL)
|
|
861 write_comment (conffile, _(gftp_config_list[i].header));
|
|
862
|
218
|
863 for (templist = gftp_config_list[i].list;
|
122
|
864 templist != NULL;
|
|
865 templist = templist->next)
|
|
866 {
|
|
867 fprintf (conffile, "%s=", gftp_config_list[i].key);
|
|
868 gftp_config_list[i].write_func (conffile, templist->data);
|
|
869 fprintf (conffile, "\n");
|
|
870 }
|
|
871 }
|
|
872
|
|
873 fclose (conffile);
|
|
874 }
|
|
875
|
|
876
|
1
|
877 GHashTable *
|
122
|
878 build_bookmarks_hash_table (gftp_bookmarks_var * entry)
|
1
|
879 {
|
122
|
880 gftp_bookmarks_var * tempentry;
|
1
|
881 GHashTable * htable;
|
|
882
|
|
883 htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
884 tempentry = entry;
|
|
885 while (tempentry != NULL)
|
|
886 {
|
|
887 g_hash_table_insert (htable, tempentry->path, tempentry);
|
|
888 if (tempentry->children != NULL)
|
|
889 {
|
|
890 tempentry = tempentry->children;
|
|
891 continue;
|
|
892 }
|
|
893 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
894 tempentry = tempentry->prev;
|
|
895 tempentry = tempentry->next;
|
|
896 }
|
|
897 return (htable);
|
|
898 }
|
|
899
|
|
900
|
|
901 void
|
122
|
902 print_bookmarks (gftp_bookmarks_var * bookmarks)
|
1
|
903 {
|
122
|
904 gftp_bookmarks_var * tempentry;
|
1
|
905
|
|
906 tempentry = bookmarks->children;
|
|
907 while (tempentry != NULL)
|
|
908 {
|
|
909 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL);
|
|
910 if (tempentry->children != NULL)
|
|
911 {
|
|
912 tempentry = tempentry->children;
|
|
913 continue;
|
|
914 }
|
|
915
|
|
916 if (tempentry->next == NULL)
|
|
917 {
|
|
918 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
919 tempentry = tempentry->prev;
|
|
920 tempentry = tempentry->next;
|
|
921 }
|
|
922 else
|
|
923 tempentry = tempentry->next;
|
|
924 }
|
|
925 }
|
|
926
|
122
|
927
|
|
928 static int
|
|
929 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line)
|
|
930 {
|
136
|
931 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
932 g_free (cv->value);
|
|
933
|
122
|
934 if (str != NULL)
|
|
935 {
|
|
936 cv->value = g_strdup (str);
|
136
|
937 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
938 return (0);
|
|
939 }
|
|
940 else
|
136
|
941 {
|
|
942 cv->value = NULL;
|
|
943 cv->flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
944 return (-1);
|
|
945 }
|
122
|
946 }
|
|
947
|
|
948
|
|
949 static int
|
387
|
950 gftp_config_file_write_text (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
951 int to_config_file)
|
122
|
952 {
|
|
953 char *outstr;
|
|
954
|
|
955 if (cv->value != NULL)
|
|
956 {
|
|
957 outstr = cv->value;
|
387
|
958 g_snprintf (buf, buflen, "%s", outstr);
|
122
|
959 return (0);
|
|
960 }
|
|
961 else
|
|
962 return (-1);
|
|
963 }
|
|
964
|
|
965
|
|
966 static int
|
387
|
967 gftp_config_file_write_hidetext (gftp_config_vars * cv, char *buf,
|
|
968 size_t buflen, int to_config_file)
|
122
|
969 {
|
|
970 char *outstr;
|
|
971
|
|
972 if (cv->value != NULL)
|
|
973 {
|
|
974 outstr = cv->value;
|
|
975 if (*outstr != '\0')
|
|
976 {
|
|
977 if (to_config_file)
|
387
|
978 g_snprintf (buf, buflen, "%s", outstr);
|
122
|
979 else
|
387
|
980 g_snprintf (buf, buflen, "*****");
|
122
|
981 }
|
|
982 return (0);
|
|
983 }
|
|
984 else
|
|
985 return (-1);
|
|
986 }
|
|
987
|
|
988
|
207
|
989 static void
|
|
990 gftp_config_file_copy_text (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
991 {
|
|
992 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
993 g_free (dest_cv->value);
|
|
994
|
|
995 if (cv->value != NULL)
|
|
996 {
|
|
997 dest_cv->value = g_strdup ((char *) cv->value);
|
|
998 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
999 }
|
|
1000 else
|
|
1001 dest_cv->value = NULL;
|
|
1002 }
|
|
1003
|
|
1004
|
227
|
1005 static int
|
|
1006 gftp_config_file_compare_text (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1007 {
|
|
1008 char *str1, *str2;
|
|
1009
|
|
1010 str1 = cv1->value;
|
|
1011 str2 = cv2->value;
|
|
1012
|
|
1013 if (cv1->value == NULL && cv2->value == NULL)
|
|
1014 return (0);
|
|
1015
|
|
1016 if ((cv1->value == NULL && cv2->value != NULL) ||
|
|
1017 (cv1->value != NULL && cv2->value == NULL))
|
|
1018 return (-1);
|
|
1019
|
|
1020 return (strcmp (str1, str2));
|
|
1021 }
|
|
1022
|
|
1023
|
207
|
1024 static void
|
|
1025 gftp_config_file_copy_ptr_contents (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1026 {
|
|
1027 memcpy (&dest_cv->value, &cv->value, sizeof (dest_cv->value));
|
|
1028 }
|
|
1029
|
122
|
1030 static int
|
|
1031 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line)
|
|
1032 {
|
|
1033 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10));
|
|
1034 return (0);
|
|
1035 }
|
|
1036
|
|
1037
|
|
1038 static int
|
387
|
1039 gftp_config_file_write_int (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
1040 int to_config_file)
|
122
|
1041 {
|
387
|
1042 g_snprintf (buf, buflen, "%d", GPOINTER_TO_INT (cv->value));
|
122
|
1043 return (0);
|
|
1044 }
|
|
1045
|
|
1046
|
|
1047 static int
|
227
|
1048 gftp_config_file_compare_int (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1049 {
|
|
1050 return (GPOINTER_TO_INT(cv1->value) == GPOINTER_TO_INT(cv2->value) ? 0 : -1);
|
|
1051 }
|
|
1052
|
|
1053
|
|
1054 static int
|
122
|
1055 gftp_config_file_read_checkbox (char *str, gftp_config_vars * cv, int line)
|
|
1056 {
|
|
1057 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10) ? 1 : 0);
|
|
1058 return (0);
|
|
1059 }
|
|
1060
|
|
1061
|
|
1062 static int
|
|
1063 gftp_config_file_read_float (char *str, gftp_config_vars * cv, int line)
|
|
1064 {
|
325
|
1065 union { intptr_t i; float f; } r;
|
131
|
1066
|
325
|
1067 r.f = strtod (str, NULL);
|
|
1068 memcpy (&cv->value, &r.i, sizeof (cv->value));
|
122
|
1069 return (0);
|
|
1070 }
|
|
1071
|
|
1072
|
|
1073 static int
|
387
|
1074 gftp_config_file_write_float (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
1075 int to_config_file)
|
122
|
1076 {
|
131
|
1077 float f;
|
|
1078
|
|
1079 memcpy (&f, &cv->value, sizeof (f));
|
387
|
1080 g_snprintf (buf, buflen, "%.2f", f);
|
122
|
1081 return (0);
|
|
1082 }
|
|
1083
|
|
1084
|
|
1085 static int
|
227
|
1086 gftp_config_file_compare_float (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1087 {
|
|
1088 float f1, f2;
|
|
1089
|
|
1090 memcpy (&f1, &cv1->value, sizeof (f1));
|
|
1091 memcpy (&f2, &cv2->value, sizeof (f2));
|
|
1092 return (f1 == f2 ? 0 : -1);
|
|
1093 }
|
|
1094
|
|
1095
|
|
1096 static int
|
122
|
1097 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line)
|
|
1098 {
|
|
1099 char *red, *green, *blue;
|
|
1100 gftp_color * color;
|
|
1101
|
136
|
1102 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
1103 g_free (cv->value);
|
|
1104
|
198
|
1105 gftp_config_parse_args (str, 3, line, &red, &green, &blue);
|
122
|
1106
|
|
1107 color = g_malloc (sizeof (*color));
|
|
1108 color->red = strtol (red, NULL, 16);
|
|
1109 color->green = strtol (green, NULL, 16);
|
|
1110 color->blue = strtol (blue, NULL, 16);
|
|
1111 g_free (red);
|
|
1112 g_free (green);
|
|
1113 g_free (blue);
|
|
1114
|
|
1115 cv->value = color;
|
136
|
1116 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
1117
|
|
1118 return (0);
|
|
1119 }
|
|
1120
|
|
1121
|
|
1122 static int
|
387
|
1123 gftp_config_file_write_color (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
1124 int to_config_file)
|
122
|
1125 {
|
|
1126 gftp_color * color;
|
|
1127
|
|
1128 color = cv->value;
|
387
|
1129 g_snprintf (buf, buflen, "%x:%x:%x", color->red, color->green, color->blue);
|
122
|
1130 return (0);
|
|
1131 }
|
|
1132
|
|
1133
|
207
|
1134 static void
|
|
1135 gftp_config_file_copy_color (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1136 {
|
|
1137 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1138 g_free (dest_cv->value);
|
|
1139
|
|
1140 dest_cv->value = g_malloc (sizeof (gftp_color));
|
|
1141 memcpy (dest_cv->value, cv->value, sizeof (gftp_color));
|
|
1142 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1143 }
|
|
1144
|
|
1145
|
122
|
1146 static int
|
227
|
1147 gftp_config_file_compare_color (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1148 {
|
|
1149 gftp_color * color1, * color2;
|
|
1150
|
|
1151 color1 = cv1->value;
|
|
1152 color2 = cv2->value;
|
|
1153
|
|
1154 return (color1->red == color2->red && color1->green == color2->green &&
|
|
1155 color1->blue == color2->blue ? 0 : -1);
|
|
1156 }
|
|
1157
|
|
1158
|
|
1159 static int
|
122
|
1160 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line)
|
|
1161 {
|
|
1162 char **clist;
|
|
1163 int i;
|
|
1164
|
|
1165 cv->value = 0;
|
|
1166 if (cv->listdata != NULL)
|
|
1167 {
|
|
1168 clist = cv->listdata;
|
|
1169 for (i=0; clist[i] != NULL; i++)
|
|
1170 {
|
125
|
1171 if (strcasecmp (clist[i], str) == 0)
|
122
|
1172 {
|
|
1173 cv->value = GINT_TO_POINTER(i);
|
|
1174 break;
|
|
1175 }
|
|
1176 }
|
|
1177 }
|
|
1178
|
|
1179 return (0);
|
|
1180 }
|
|
1181
|
|
1182
|
|
1183 static int
|
387
|
1184 gftp_config_file_write_intcombo (gftp_config_vars * cv, char *buf,
|
|
1185 size_t buflen, int to_config_file)
|
122
|
1186 {
|
|
1187 char **clist;
|
|
1188
|
|
1189 clist = cv->listdata;
|
|
1190 if (clist != NULL)
|
387
|
1191 g_snprintf (buf, buflen, "%s", clist[GPOINTER_TO_INT(cv->value)]);
|
122
|
1192 else
|
387
|
1193 g_snprintf (buf, buflen, _("<unknown>"));
|
122
|
1194
|
|
1195 return (0);
|
|
1196 }
|
|
1197
|
|
1198
|
125
|
1199 static int
|
|
1200 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line)
|
|
1201 {
|
|
1202 char **clist;
|
|
1203 int i;
|
|
1204
|
|
1205 cv->value = NULL;
|
|
1206 if (cv->listdata != NULL)
|
|
1207 {
|
|
1208 clist = cv->listdata;
|
|
1209 for (i=0; clist[i] != NULL; i++)
|
|
1210 {
|
|
1211 if (strcasecmp (clist[i], str) == 0)
|
|
1212 {
|
|
1213 cv->value = clist[i];
|
|
1214 break;
|
|
1215 }
|
|
1216 }
|
|
1217 }
|
|
1218
|
|
1219 return (0);
|
|
1220 }
|
|
1221
|
|
1222
|
|
1223 /* Note, the index numbers of this array must match up to the numbers in
|
122
|
1224 gftp_option_type_enum in gftp.h */
|
|
1225 gftp_option_type_var gftp_option_types[] = {
|
227
|
1226 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1227 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1228 {gftp_config_file_read_textcombo, gftp_config_file_write_text,
|
|
1229 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1230 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1231 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1232 {gftp_config_file_read_text, gftp_config_file_write_hidetext,
|
|
1233 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1234 {gftp_config_file_read_int, gftp_config_file_write_int,
|
|
1235 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1236 NULL, NULL, NULL},
|
|
1237 {gftp_config_file_read_checkbox, gftp_config_file_write_int,
|
|
1238 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1239 NULL, NULL, NULL},
|
|
1240 {gftp_config_file_read_intcombo, gftp_config_file_write_intcombo,
|
|
1241 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1242 NULL, NULL, NULL},
|
|
1243 {gftp_config_file_read_float, gftp_config_file_write_float,
|
|
1244 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_float,
|
|
1245 NULL, NULL, NULL},
|
|
1246 {gftp_config_file_read_color, gftp_config_file_write_color,
|
|
1247 gftp_config_file_copy_color, gftp_config_file_compare_color,
|
|
1248 NULL, NULL, NULL},
|
|
1249 {NULL, NULL, NULL, NULL, NULL, NULL},
|
|
1250 {NULL, NULL, NULL, NULL, NULL, NULL}
|
122
|
1251 };
|
|
1252
|
|
1253
|
|
1254 void
|
374
|
1255 gftp_lookup_global_option (const char * key, void *value)
|
122
|
1256 {
|
|
1257 gftp_config_list_vars * tmplistvar;
|
|
1258 gftp_config_vars * tmpconfigvar;
|
|
1259
|
|
1260 if (gftp_global_options_htable != NULL &&
|
|
1261 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1262 key)) != NULL)
|
124
|
1263 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
122
|
1264 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
1265 key)) != NULL)
|
124
|
1266 *(gftp_config_list_vars **) value = tmplistvar;
|
122
|
1267 else
|
|
1268 {
|
|
1269 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1270 exit (1);
|
|
1271 }
|
|
1272 }
|
|
1273
|
|
1274
|
|
1275 void
|
374
|
1276 gftp_lookup_request_option (gftp_request * request, const char * key,
|
|
1277 void *value)
|
122
|
1278 {
|
143
|
1279 gftp_config_vars * tmpconfigvar;
|
|
1280
|
|
1281 if (request != NULL && request->local_options_hash != NULL &&
|
|
1282 (tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1283 key)) != NULL)
|
|
1284 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1285 else
|
|
1286 gftp_lookup_global_option (key, value);
|
122
|
1287 }
|
|
1288
|
|
1289
|
|
1290 void
|
374
|
1291 gftp_lookup_bookmark_option (gftp_bookmarks_var * bm, const char * key,
|
|
1292 void *value)
|
229
|
1293 {
|
|
1294 gftp_config_vars * tmpconfigvar;
|
|
1295
|
|
1296 if (bm != NULL && bm->local_options_hash != NULL &&
|
|
1297 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1298 key)) != NULL)
|
|
1299 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1300 else
|
|
1301 gftp_lookup_global_option (key, value);
|
|
1302 }
|
|
1303
|
|
1304
|
|
1305 void
|
374
|
1306 gftp_set_global_option (const char * key, const void *value)
|
122
|
1307 {
|
227
|
1308 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1309 void *nc_ptr;
|
|
1310 int ret;
|
129
|
1311
|
|
1312 if (gftp_global_options_htable != NULL &&
|
|
1313 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1314 key)) != NULL)
|
201
|
1315 {
|
227
|
1316 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1317
|
|
1318 /* Cheap warning fix for const pointer... */
|
|
1319 memcpy (&nc_ptr, &value, sizeof (nc_ptr));
|
|
1320 newconfigvar.value = nc_ptr;
|
|
1321 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
201
|
1322
|
516
|
1323 ret = gftp_option_types[tmpconfigvar->otype].compare_function (&newconfigvar, tmpconfigvar);
|
227
|
1324 if (ret != 0)
|
|
1325 {
|
516
|
1326 gftp_option_types[tmpconfigvar->otype].copy_function (&newconfigvar, tmpconfigvar);
|
227
|
1327 gftp_configuration_changed = 1;
|
|
1328 }
|
201
|
1329 }
|
129
|
1330 else
|
|
1331 {
|
|
1332 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1333 exit (1);
|
|
1334 }
|
122
|
1335 }
|
|
1336
|
|
1337
|
229
|
1338 static void
|
|
1339 _gftp_set_option_value (gftp_config_vars * cv, const void * newval)
|
|
1340 {
|
|
1341 gftp_config_vars newconfigvar;
|
|
1342 void *nc_ptr;
|
|
1343
|
|
1344 memcpy (&newconfigvar, cv, sizeof (newconfigvar));
|
|
1345
|
|
1346 /* Cheap warning fix for const pointer... */
|
|
1347 memcpy (&nc_ptr, &newval, sizeof (nc_ptr));
|
|
1348 newconfigvar.value = nc_ptr;
|
|
1349 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1350
|
516
|
1351 gftp_option_types[cv->otype].copy_function (&newconfigvar, cv);
|
229
|
1352 }
|
|
1353
|
|
1354
|
122
|
1355 void
|
374
|
1356 gftp_set_request_option (gftp_request * request, const char * key,
|
|
1357 const void *value)
|
122
|
1358 {
|
136
|
1359 gftp_config_vars * tmpconfigvar;
|
|
1360
|
|
1361 if (request->local_options_hash == NULL)
|
|
1362 request->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1363 string_hash_compare);
|
|
1364
|
|
1365 if ((tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1366 key)) != NULL)
|
229
|
1367 _gftp_set_option_value (tmpconfigvar, value);
|
136
|
1368 else
|
|
1369 {
|
143
|
1370 if (gftp_global_options_htable == NULL ||
|
136
|
1371 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1372 key)) == NULL)
|
|
1373 {
|
|
1374 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1375 exit (1);
|
|
1376 }
|
|
1377
|
|
1378 request->num_local_options_vars++;
|
|
1379 request->local_options_vars = g_realloc (request->local_options_vars,
|
199
|
1380 sizeof (gftp_config_vars) * request->num_local_options_vars);
|
136
|
1381
|
229
|
1382 memcpy (&request->local_options_vars[request->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1383 _gftp_set_option_value (&request->local_options_vars[request->num_local_options_vars - 1], value);
|
143
|
1384
|
|
1385 g_hash_table_insert (request->local_options_hash, request->local_options_vars[request->num_local_options_vars - 1].key, &request->local_options_vars[request->num_local_options_vars - 1]);
|
136
|
1386 }
|
122
|
1387 }
|
|
1388
|
|
1389
|
|
1390 void
|
374
|
1391 gftp_set_bookmark_option (gftp_bookmarks_var * bm, const char * key,
|
|
1392 const void *value)
|
229
|
1393 {
|
|
1394 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1395 int ret;
|
|
1396
|
|
1397 if (bm->local_options_hash != NULL &&
|
|
1398 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1399 key)) != NULL)
|
|
1400 _gftp_set_option_value (tmpconfigvar, value);
|
|
1401 else
|
|
1402 {
|
|
1403 if (gftp_global_options_htable == NULL ||
|
|
1404 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1405 key)) == NULL)
|
|
1406 {
|
|
1407 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1408 exit (1);
|
|
1409 }
|
|
1410
|
|
1411 /* Check to see if this is set to the same value as the global option.
|
|
1412 If so, don't add it to the bookmark preferences */
|
|
1413 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1414 memcpy (&newconfigvar.value, &value, sizeof (newconfigvar.value));
|
|
1415 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1416
|
516
|
1417 ret = gftp_option_types[tmpconfigvar->otype].compare_function (&newconfigvar, tmpconfigvar);
|
229
|
1418 if (ret == 0)
|
|
1419 return;
|
|
1420
|
|
1421 if (bm->local_options_hash == NULL)
|
|
1422 bm->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1423 string_hash_compare);
|
|
1424
|
|
1425 bm->num_local_options_vars++;
|
|
1426 bm->local_options_vars = g_realloc (bm->local_options_vars,
|
|
1427 sizeof (gftp_config_vars) * bm->num_local_options_vars);
|
|
1428
|
|
1429 memcpy (&bm->local_options_vars[bm->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1430 _gftp_set_option_value (&bm->local_options_vars[bm->num_local_options_vars - 1], value);
|
|
1431
|
|
1432 g_hash_table_insert (bm->local_options_hash, bm->local_options_vars[bm->num_local_options_vars - 1].key, &bm->local_options_vars[bm->num_local_options_vars - 1]);
|
|
1433 }
|
|
1434 }
|
|
1435
|
|
1436
|
|
1437 void
|
122
|
1438 gftp_register_config_vars (gftp_config_vars * config_vars)
|
|
1439 {
|
|
1440 gftp_options_list = g_list_append (gftp_options_list, config_vars);
|
|
1441 gftp_setup_global_options (config_vars);
|
|
1442 }
|
|
1443
|
199
|
1444
|
|
1445 void
|
|
1446 gftp_copy_local_options (gftp_config_vars ** new_options_vars,
|
|
1447 GHashTable ** new_options_hash,
|
429
|
1448 int *new_num_local_options_vars,
|
199
|
1449 gftp_config_vars * orig_options,
|
|
1450 int num_local_options_vars)
|
|
1451 {
|
|
1452 int i;
|
|
1453
|
429
|
1454 *new_num_local_options_vars = num_local_options_vars;
|
199
|
1455 if (orig_options == NULL || num_local_options_vars == 0)
|
|
1456 {
|
|
1457 *new_options_vars = NULL;
|
|
1458 *new_options_hash = NULL;
|
|
1459 return;
|
|
1460 }
|
|
1461
|
|
1462 *new_options_hash = g_hash_table_new (string_hash_function,
|
|
1463 string_hash_compare);
|
|
1464
|
|
1465 *new_options_vars = g_malloc (sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1466 memcpy (*new_options_vars, orig_options,
|
|
1467 sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1468
|
|
1469 for (i=0; i<num_local_options_vars; i++)
|
|
1470 {
|
|
1471 g_hash_table_insert (*new_options_hash, (*new_options_vars)[i].key,
|
|
1472 &(*new_options_vars)[i]);
|
207
|
1473
|
|
1474 (*new_options_vars)[i].value = NULL;
|
|
1475 (*new_options_vars)[i].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
199
|
1476
|
207
|
1477 gftp_option_types[(*new_options_vars)[i].otype].copy_function (&(orig_options)[i], &(*new_options_vars)[i]);
|
199
|
1478 }
|
|
1479 }
|
|
1480
|
201
|
1481
|
|
1482 void
|
|
1483 gftp_config_free_options (gftp_config_vars * options_vars,
|
|
1484 GHashTable * options_hash,
|
|
1485 int num_options_vars)
|
|
1486 {
|
|
1487 int i;
|
|
1488
|
|
1489 if (num_options_vars == 0)
|
|
1490 return;
|
|
1491
|
|
1492 if (num_options_vars > 0)
|
|
1493 {
|
|
1494 /* If num_options_vars is 0, then the options was allocated with malloc */
|
|
1495
|
|
1496 for (i=0; i<num_options_vars; i++)
|
|
1497 {
|
|
1498 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1499 options_vars[i].value != NULL)
|
|
1500 g_free (options_vars[i].value);
|
|
1501 }
|
|
1502
|
|
1503 g_free (options_vars);
|
|
1504 }
|
|
1505 else if (num_options_vars < 0)
|
|
1506 {
|
|
1507 /* Otherwise we are freeing the global options */
|
|
1508
|
|
1509 for (i=0; options_vars[i].key != NULL; i++)
|
|
1510 {
|
|
1511 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1512 options_vars[i].value != NULL)
|
|
1513 g_free (options_vars[i].value);
|
|
1514
|
|
1515 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNLISTMEM &&
|
|
1516 options_vars[i].listdata != NULL)
|
|
1517 g_free (options_vars[i].listdata);
|
|
1518 }
|
|
1519 }
|
|
1520
|
|
1521 if (options_hash != NULL)
|
|
1522 g_hash_table_destroy (options_hash);
|
|
1523 }
|
|
1524
|
|
1525
|
|
1526 void
|
|
1527 gftp_bookmarks_destroy (gftp_bookmarks_var * bookmarks)
|
|
1528 {
|
|
1529 gftp_bookmarks_var * tempentry, * delentry;
|
|
1530
|
|
1531 if (bookmarks == NULL)
|
|
1532 return;
|
|
1533
|
|
1534 tempentry = bookmarks;
|
|
1535 while (tempentry != NULL)
|
|
1536 {
|
|
1537 gftp_free_bookmark (tempentry);
|
227
|
1538
|
|
1539 if (tempentry->path != NULL)
|
|
1540 g_free (tempentry->path);
|
201
|
1541
|
|
1542 if (tempentry->children != NULL)
|
|
1543 {
|
|
1544 tempentry = tempentry->children;
|
|
1545 continue;
|
|
1546 }
|
|
1547
|
|
1548 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
1549 {
|
|
1550 delentry = tempentry;
|
|
1551 tempentry = tempentry->prev;
|
|
1552 g_free (delentry);
|
|
1553 }
|
|
1554
|
|
1555 delentry = tempentry;
|
|
1556 tempentry = tempentry->next;
|
|
1557 g_free (delentry);
|
|
1558 }
|
|
1559 }
|
|
1560
|
218
|
1561
|
|
1562 void
|
|
1563 gftp_free_proxy_hosts (GList * proxy_hosts)
|
|
1564 {
|
|
1565 gftp_proxy_hosts * hosts;
|
|
1566 GList * templist;
|
|
1567
|
|
1568 for (templist = proxy_hosts;
|
|
1569 templist != NULL;
|
|
1570 templist = templist->next)
|
|
1571 {
|
|
1572 hosts = templist->data;
|
|
1573
|
|
1574 if (hosts->domain)
|
|
1575 g_free (hosts->domain);
|
|
1576 g_free (hosts);
|
|
1577 }
|
|
1578
|
|
1579 g_list_free (proxy_hosts);
|
|
1580 }
|
|
1581
|
|
1582
|
|
1583 GList *
|
|
1584 gftp_copy_proxy_hosts (GList * proxy_hosts)
|
|
1585 {
|
|
1586 gftp_proxy_hosts * oldhosts, * newhosts;
|
|
1587 GList * templist, * new_proxy_hosts;
|
|
1588
|
|
1589 new_proxy_hosts = NULL;
|
|
1590
|
|
1591 if (proxy_hosts != NULL)
|
|
1592 {
|
|
1593 for (templist = proxy_hosts;
|
|
1594 templist != NULL;
|
|
1595 templist = templist->next)
|
|
1596 {
|
|
1597 oldhosts = templist->data;
|
|
1598
|
|
1599 newhosts = g_malloc0 (sizeof (*newhosts));
|
|
1600 memcpy (newhosts, oldhosts, sizeof (*newhosts));
|
|
1601
|
|
1602 if (oldhosts->domain)
|
|
1603 newhosts->domain = g_strdup (oldhosts->domain);
|
|
1604
|
|
1605 new_proxy_hosts = g_list_append (new_proxy_hosts, newhosts);
|
|
1606 }
|
|
1607 }
|
|
1608
|
|
1609 return (new_proxy_hosts);
|
|
1610 }
|
|
1611
|