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 {
|
|
204 len = strlen (buf);
|
107
|
205 if (len > 0 && buf[len - 1] == '\n')
|
1
|
206 buf[--len] = '\0';
|
107
|
207 if (len > 0 && buf[len - 1] == '\r')
|
1
|
208 buf[--len] = '\0';
|
|
209 line++;
|
|
210
|
|
211 if (*buf == '[')
|
|
212 {
|
|
213 newentry = g_malloc0 (sizeof (*newentry));
|
|
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);
|
|
394 return (1);
|
|
395 }
|
|
396
|
|
397
|
122
|
398 static void *
|
|
399 gftp_config_read_str (char *buf, int line)
|
|
400 {
|
|
401 char *ret;
|
|
402
|
|
403 ret = g_strdup (buf);
|
|
404 return (ret);
|
|
405 }
|
|
406
|
|
407
|
|
408 static void
|
|
409 gftp_config_write_str (FILE *fd, void *data)
|
|
410 {
|
|
411 fprintf (fd, "%s", (char *) data);
|
|
412 }
|
|
413
|
|
414
|
|
415 static void *
|
|
416 gftp_config_read_proxy (char *buf, int line)
|
|
417 {
|
|
418 gftp_proxy_hosts * host;
|
|
419 unsigned int nums[4];
|
|
420 char *pos;
|
|
421
|
|
422 host = g_malloc0 (sizeof (*host));
|
|
423 if ((pos = strchr (buf, '/')) == NULL)
|
|
424 host->domain = g_strdup (buf);
|
|
425 else
|
|
426 {
|
|
427 *pos = '\0';
|
|
428 sscanf (buf, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], &nums[3]);
|
|
429 host->ipv4_network_address =
|
|
430 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
431
|
|
432 if (strchr (pos + 1, '.') == NULL)
|
|
433 host->ipv4_netmask = 0xffffffff << (32 - strtol (pos + 1, NULL, 10));
|
|
434 else
|
|
435 {
|
|
436 sscanf (pos + 1, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2],
|
|
437 &nums[3]);
|
|
438 host->ipv4_netmask =
|
|
439 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
440 }
|
|
441 }
|
|
442
|
|
443 return (host);
|
|
444 }
|
|
445
|
|
446
|
|
447 static void
|
|
448 gftp_config_write_proxy (FILE *fd, void *data)
|
|
449 {
|
|
450 gftp_proxy_hosts * host;
|
|
451
|
|
452 host = data;
|
|
453
|
|
454 if (host->domain)
|
|
455 fprintf (fd, "%s", host->domain);
|
|
456 else
|
|
457 fprintf (fd, "%d.%d.%d.%d/%d.%d.%d.%d",
|
|
458 host->ipv4_network_address >> 24 & 0xff,
|
|
459 host->ipv4_network_address >> 16 & 0xff,
|
|
460 host->ipv4_network_address >> 8 & 0xff,
|
|
461 host->ipv4_network_address & 0xff,
|
|
462 host->ipv4_netmask >> 24 & 0xff,
|
|
463 host->ipv4_netmask >> 16 & 0xff,
|
|
464 host->ipv4_netmask >> 8 & 0xff,
|
|
465 host->ipv4_netmask & 0xff);
|
|
466 }
|
|
467
|
|
468
|
|
469 static void *
|
|
470 gftp_config_read_ext (char *buf, int line)
|
|
471 {
|
|
472 gftp_file_extensions * tempext;
|
|
473
|
|
474 tempext = g_malloc (sizeof (*tempext));
|
198
|
475 gftp_config_parse_args (buf, 4, line, &tempext->ext, &tempext->filename,
|
|
476 &tempext->ascii_binary, &tempext->view_program);
|
122
|
477
|
|
478 tempext->stlen = strlen (tempext->ext);
|
|
479
|
|
480 return (tempext);
|
|
481 }
|
|
482
|
|
483
|
|
484 static void
|
|
485 gftp_config_write_ext (FILE *fd, void *data)
|
|
486 {
|
|
487 gftp_file_extensions * tempext;
|
|
488
|
|
489 tempext = data;
|
|
490 fprintf (fd, "%s:%s:%c:%s", tempext->ext, tempext->filename,
|
|
491 *tempext->ascii_binary == '\0' ? ' ' : *tempext->ascii_binary,
|
|
492 tempext->view_program);
|
|
493 }
|
|
494
|
|
495
|
|
496 gftp_config_list_vars gftp_config_list[] = {
|
|
497 {"dont_use_proxy", gftp_config_read_proxy, gftp_config_write_proxy,
|
|
498 NULL, 0,
|
|
499 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")},
|
|
500 {"ext", gftp_config_read_ext, gftp_config_write_ext,
|
|
501 NULL, 0,
|
|
502 N_("ext=file extenstion:XPM file:Ascii or Binary (A or B):viewer program. Note: All arguments except the file extension are optional")},
|
|
503 {"localhistory", gftp_config_read_str, gftp_config_write_str,
|
|
504 NULL, 0, NULL},
|
|
505 {"remotehistory", gftp_config_read_str, gftp_config_write_str,
|
|
506 NULL, 0, NULL},
|
|
507 {"hosthistory", gftp_config_read_str, gftp_config_write_str,
|
|
508 NULL, 0, NULL},
|
|
509 {"porthistory", gftp_config_read_str, gftp_config_write_str,
|
|
510 NULL, 0, NULL},
|
|
511 {"userhistory", gftp_config_read_str, gftp_config_write_str,
|
|
512 NULL, 0, NULL},
|
|
513 {NULL, NULL, NULL,
|
|
514 NULL, 0, NULL}
|
|
515 };
|
|
516
|
|
517
|
|
518 static void
|
|
519 gftp_setup_global_options (gftp_config_vars * cvars)
|
|
520 {
|
|
521 int i;
|
|
522
|
|
523 for (i=0; cvars[i].key != NULL; i++)
|
|
524 {
|
136
|
525 if (cvars[i].key != NULL && *cvars[i].key != '\0')
|
122
|
526 g_hash_table_insert (gftp_global_options_htable,
|
|
527 cvars[i].key, &cvars[i]);
|
|
528 }
|
|
529 }
|
|
530
|
|
531
|
|
532 void
|
124
|
533 gftp_read_config_file (char *global_data_path)
|
122
|
534 {
|
|
535 char *tempstr, *temp1str, *curpos, buf[255];
|
|
536 gftp_config_list_vars * tmplistvar;
|
|
537 gftp_config_vars * tmpconfigvar;
|
125
|
538 char **protocol_list;
|
122
|
539 FILE *conffile;
|
|
540 int line, i;
|
|
541 size_t len;
|
|
542
|
|
543 gftp_global_options_htable = g_hash_table_new (string_hash_function,
|
|
544 string_hash_compare);
|
|
545
|
|
546 gftp_register_config_vars (gftp_global_config_vars);
|
|
547
|
125
|
548 protocol_list = NULL;
|
122
|
549 for (i=0; gftp_protocols[i].register_options != NULL; i++)
|
|
550 {
|
125
|
551 if (gftp_protocols[i].shown)
|
|
552 {
|
|
553 protocol_list = g_realloc (protocol_list, sizeof (char *) * (i + 2));
|
|
554 protocol_list[i] = gftp_protocols[i].name;
|
|
555 protocol_list[i + 1] = NULL;
|
|
556 }
|
|
557
|
122
|
558 if (gftp_protocols[i].register_options != NULL)
|
|
559 gftp_protocols[i].register_options ();
|
|
560 }
|
|
561
|
125
|
562 if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
563 "default_protocol")) != NULL)
|
201
|
564 {
|
|
565 tmpconfigvar->listdata = protocol_list;
|
|
566 tmpconfigvar->flags |= GFTP_CVARS_FLAGS_DYNLISTMEM;
|
|
567 }
|
125
|
568
|
122
|
569 gftp_config_list_htable = g_hash_table_new (string_hash_function,
|
|
570 string_hash_compare);
|
|
571
|
|
572 for (i=0; gftp_config_list[i].key != NULL; i++)
|
|
573 {
|
|
574 g_hash_table_insert (gftp_config_list_htable, gftp_config_list[i].key,
|
|
575 &gftp_config_list[i]);
|
|
576 }
|
|
577
|
|
578 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
579 {
|
|
580 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
581 exit (1);
|
|
582 }
|
|
583
|
|
584 if (access (tempstr, F_OK) == -1)
|
|
585 {
|
|
586 temp1str = expand_path (BASE_CONF_DIR);
|
|
587 if (access (temp1str, F_OK) == -1)
|
|
588 {
|
|
589 if (mkdir (temp1str, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
|
|
590 {
|
|
591 printf (_("gFTP Error: Could not make directory %s: %s\n"),
|
|
592 temp1str, g_strerror (errno));
|
|
593 exit (1);
|
|
594 }
|
|
595 }
|
|
596 g_free (temp1str);
|
|
597
|
124
|
598 temp1str = g_strdup_printf ("%s/gftprc", global_data_path);
|
122
|
599 if (access (temp1str, F_OK) == -1)
|
|
600 {
|
|
601 printf (_("gFTP Error: Cannot find master config file %s\n"),
|
|
602 temp1str);
|
|
603 printf (_("Did you do a make install?\n"));
|
|
604 exit (1);
|
|
605 }
|
|
606 copyfile (temp1str, tempstr);
|
|
607 g_free (temp1str);
|
|
608 }
|
|
609
|
|
610 if ((conffile = fopen (tempstr, "r")) == NULL)
|
|
611 {
|
|
612 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
613 g_strerror (errno));
|
|
614 exit (1);
|
|
615 }
|
|
616 g_free (tempstr);
|
|
617
|
|
618 line = 0;
|
|
619 while (fgets (buf, sizeof (buf), conffile))
|
|
620 {
|
|
621 len = strlen (buf);
|
|
622 if (len > 0 && buf[len - 1] == '\n')
|
|
623 buf[--len] = '\0';
|
|
624 if (len > 0 && buf[len - 1] == '\r')
|
|
625 buf[--len] = '\0';
|
|
626 line++;
|
|
627
|
|
628 if (*buf == '#' || *buf == '\0')
|
|
629 continue;
|
|
630
|
|
631 if ((curpos = strchr (buf, '=')) == NULL)
|
|
632 continue;
|
|
633
|
|
634 *curpos = '\0';
|
|
635
|
|
636 if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
637 buf)) != NULL)
|
|
638 {
|
|
639 tmplistvar->list = g_list_append (tmplistvar->list,
|
|
640 tmplistvar->read_func (curpos + 1,
|
|
641 line));
|
|
642 tmplistvar->num_items++;
|
|
643 }
|
|
644 else if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
645 buf)) != NULL &&
|
|
646 gftp_option_types[tmpconfigvar->otype].read_function != NULL)
|
|
647 {
|
|
648 if (gftp_option_types[tmpconfigvar->otype].read_function (curpos + 1,
|
|
649 tmpconfigvar, line) != 0)
|
|
650 {
|
|
651 printf (_("Terminating due to parse errors at line %d in the config file\n"), line);
|
|
652 exit (1);
|
|
653 }
|
|
654 }
|
|
655 else
|
|
656 {
|
|
657 printf (_("gFTP Warning: Skipping line %d in config file: %s\n"),
|
|
658 line, buf);
|
|
659 }
|
|
660 }
|
|
661
|
|
662 if ((tempstr = expand_path (LOG_FILE)) == NULL)
|
|
663 {
|
|
664 printf (_("gFTP Error: Bad log file name %s\n"), LOG_FILE);
|
|
665 exit (1);
|
|
666 }
|
|
667
|
|
668 if ((gftp_logfd = fopen (tempstr, "w")) == NULL)
|
|
669 {
|
|
670 printf (_("gFTP Warning: Cannot open %s for writing: %s\n"),
|
|
671 tempstr, g_strerror (errno));
|
|
672 }
|
|
673 g_free (tempstr);
|
|
674
|
|
675 gftp_bookmarks = g_malloc0 (sizeof (*gftp_bookmarks));
|
|
676 gftp_bookmarks->isfolder = 1;
|
|
677 gftp_bookmarks->path = g_malloc0 (1);
|
|
678 gftp_bookmarks_htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
679
|
124
|
680 gftp_read_bookmarks (global_data_path);
|
122
|
681 }
|
|
682
|
|
683
|
|
684 static void
|
|
685 write_comment (FILE * fd, const char *comment)
|
|
686 {
|
|
687 const char *pos, *endpos;
|
|
688
|
|
689 fwrite ("# ", 1, 2, fd);
|
|
690 pos = comment;
|
|
691 while (strlen (pos) > 76)
|
|
692 {
|
|
693 for (endpos = pos + 76; *endpos != ' ' && endpos > pos; endpos--);
|
|
694 if (endpos == pos)
|
|
695 {
|
|
696 for (endpos = pos + 76; *endpos != ' ' && *endpos != '\0';
|
|
697 endpos++);
|
|
698 }
|
|
699 fwrite (pos, 1, endpos - pos, fd);
|
|
700 fwrite ("\n# ", 1, 3, fd);
|
|
701 if (*endpos == '\0')
|
|
702 {
|
|
703 pos = endpos;
|
|
704 break;
|
|
705 }
|
|
706 else
|
|
707 pos = endpos + 1;
|
|
708 }
|
|
709 if (strlen (pos) > 1)
|
|
710 {
|
|
711 fwrite (pos, 1, strlen (pos), fd);
|
|
712 fwrite ("\n", 1, 1, fd);
|
|
713 }
|
|
714 }
|
|
715
|
|
716
|
|
717 void
|
|
718 gftp_write_bookmarks_file (void)
|
|
719 {
|
|
720 gftp_bookmarks_var * tempentry;
|
330
|
721 char *bmhdr, *tempstr, *password;
|
|
722 intptr_t scramble_passwords;
|
122
|
723 FILE * bmfile;
|
199
|
724 int i;
|
122
|
725
|
|
726 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");
|
|
727
|
|
728 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
729 {
|
|
730 printf (_("gFTP Error: Bad bookmarks file name %s\n"), CONFIG_FILE);
|
|
731 exit (1);
|
|
732 }
|
|
733
|
|
734 if ((bmfile = fopen (tempstr, "w+")) == NULL)
|
|
735 {
|
|
736 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"),
|
|
737 CONFIG_FILE, g_strerror (errno));
|
|
738 exit (1);
|
|
739 }
|
|
740
|
|
741 g_free (tempstr);
|
|
742
|
|
743 write_comment (bmfile, _(bmhdr));
|
|
744 fwrite ("\n", 1, 1, bmfile);
|
|
745
|
330
|
746 gftp_lookup_global_option ("scramble_passwords", &scramble_passwords);
|
|
747
|
122
|
748 tempentry = gftp_bookmarks->children;
|
|
749 while (tempentry != NULL)
|
|
750 {
|
|
751 if (tempentry->children != NULL)
|
|
752 {
|
|
753 tempentry = tempentry->children;
|
|
754 continue;
|
|
755 }
|
330
|
756
|
122
|
757 tempstr = tempentry->path;
|
|
758 while (*tempstr == '/')
|
|
759 tempstr++;
|
330
|
760
|
|
761 if (tempentry->save_password && tempentry->pass != NULL)
|
|
762 {
|
|
763 if (scramble_passwords)
|
|
764 password = gftp_scramble_password (tempentry->pass);
|
|
765 else
|
|
766 password = g_strdup (tempentry->pass);
|
|
767 }
|
|
768 else
|
|
769 password = NULL;
|
|
770
|
122
|
771 fprintf (bmfile,
|
|
772 "[%s]\nhostname=%s\nport=%d\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nusername=%s\npassword=%s\naccount=%s\n",
|
|
773 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname,
|
|
774 tempentry->port, tempentry->protocol == NULL
|
|
775 || *tempentry->protocol ==
|
|
776 '\0' ? gftp_protocols[0].name : tempentry->protocol,
|
|
777 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir,
|
|
778 tempentry->local_dir == NULL ? "" : tempentry->local_dir,
|
|
779 tempentry->user == NULL ? "" : tempentry->user,
|
330
|
780 password == NULL ? "" : password,
|
122
|
781 tempentry->acct == NULL ? "" : tempentry->acct);
|
|
782
|
330
|
783 if (password != NULL)
|
|
784 g_free(password);
|
|
785
|
199
|
786 if (tempentry->local_options_vars != NULL)
|
|
787 {
|
|
788 for (i=0; i<tempentry->num_local_options_vars; i++)
|
|
789 {
|
|
790 fprintf (bmfile, "%s=", tempentry->local_options_vars[i].key);
|
|
791 gftp_option_types[tempentry->local_options_vars[i].otype].write_function (&tempentry->local_options_vars[i], bmfile, 1);
|
|
792 fprintf (bmfile, "\n");
|
|
793 }
|
|
794 }
|
122
|
795
|
|
796 fprintf (bmfile, "\n");
|
|
797
|
|
798 if (tempentry->next == NULL)
|
|
799 {
|
|
800 tempentry = tempentry->prev;
|
|
801 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
802 tempentry = tempentry->prev;
|
|
803 tempentry = tempentry->next;
|
|
804 }
|
|
805 else
|
|
806 tempentry = tempentry->next;
|
|
807 }
|
|
808
|
|
809 fclose (bmfile);
|
|
810 }
|
|
811
|
|
812
|
|
813 void
|
|
814 gftp_write_config_file (void)
|
|
815 {
|
|
816 gftp_config_vars * cv;
|
|
817 GList *templist;
|
|
818 FILE *conffile;
|
|
819 char *tempstr;
|
|
820 int i;
|
|
821
|
|
822 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
823 {
|
|
824 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
825 exit (1);
|
|
826 }
|
|
827
|
|
828 if ((conffile = fopen (tempstr, "w+")) == NULL)
|
|
829 {
|
|
830 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
831 g_strerror (errno));
|
|
832 exit (1);
|
|
833 }
|
|
834
|
|
835 g_free (tempstr);
|
|
836
|
|
837 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"));
|
|
838
|
|
839 for (templist = gftp_options_list;
|
|
840 templist != NULL;
|
|
841 templist = templist->next)
|
|
842 {
|
|
843 cv = templist->data;
|
|
844
|
|
845 for (i=0; cv[i].key != NULL; i++)
|
|
846 {
|
|
847 if (gftp_option_types[cv[i].otype].write_function == NULL ||
|
|
848 *cv[i].key == '\0')
|
|
849 continue;
|
|
850
|
|
851 fprintf (conffile, "\n");
|
|
852 if (cv[i].comment != NULL)
|
|
853 write_comment (conffile, _(cv[i].comment));
|
|
854
|
|
855 fprintf (conffile, "%s=", cv[i].key);
|
|
856 gftp_option_types[cv[i].otype].write_function (&cv[i], conffile, 1);
|
|
857 fprintf (conffile, "\n");
|
|
858 }
|
|
859 }
|
|
860
|
218
|
861 for (i=0; gftp_config_list[i].key != NULL; i++)
|
122
|
862 {
|
218
|
863 if (gftp_config_list[i].header == NULL &&
|
|
864 gftp_config_list[i].list == NULL)
|
|
865 continue;
|
|
866
|
122
|
867 fprintf (conffile, "\n");
|
|
868 if (gftp_config_list[i].header != NULL)
|
|
869 write_comment (conffile, _(gftp_config_list[i].header));
|
|
870
|
218
|
871 for (templist = gftp_config_list[i].list;
|
122
|
872 templist != NULL;
|
|
873 templist = templist->next)
|
|
874 {
|
|
875 fprintf (conffile, "%s=", gftp_config_list[i].key);
|
|
876 gftp_config_list[i].write_func (conffile, templist->data);
|
|
877 fprintf (conffile, "\n");
|
|
878 }
|
|
879 }
|
|
880
|
|
881 fclose (conffile);
|
|
882 }
|
|
883
|
|
884
|
1
|
885 GHashTable *
|
122
|
886 build_bookmarks_hash_table (gftp_bookmarks_var * entry)
|
1
|
887 {
|
122
|
888 gftp_bookmarks_var * tempentry;
|
1
|
889 GHashTable * htable;
|
|
890
|
|
891 htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
892 tempentry = entry;
|
|
893 while (tempentry != NULL)
|
|
894 {
|
|
895 g_hash_table_insert (htable, tempentry->path, tempentry);
|
|
896 if (tempentry->children != NULL)
|
|
897 {
|
|
898 tempentry = tempentry->children;
|
|
899 continue;
|
|
900 }
|
|
901 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
902 tempentry = tempentry->prev;
|
|
903 tempentry = tempentry->next;
|
|
904 }
|
|
905 return (htable);
|
|
906 }
|
|
907
|
|
908
|
|
909 void
|
122
|
910 print_bookmarks (gftp_bookmarks_var * bookmarks)
|
1
|
911 {
|
122
|
912 gftp_bookmarks_var * tempentry;
|
1
|
913
|
|
914 tempentry = bookmarks->children;
|
|
915 while (tempentry != NULL)
|
|
916 {
|
|
917 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL);
|
|
918 if (tempentry->children != NULL)
|
|
919 {
|
|
920 tempentry = tempentry->children;
|
|
921 continue;
|
|
922 }
|
|
923
|
|
924 if (tempentry->next == NULL)
|
|
925 {
|
|
926 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
927 tempentry = tempentry->prev;
|
|
928 tempentry = tempentry->next;
|
|
929 }
|
|
930 else
|
|
931 tempentry = tempentry->next;
|
|
932 }
|
|
933 }
|
|
934
|
122
|
935
|
|
936 static int
|
|
937 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line)
|
|
938 {
|
136
|
939 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
940 g_free (cv->value);
|
|
941
|
122
|
942 if (str != NULL)
|
|
943 {
|
|
944 cv->value = g_strdup (str);
|
136
|
945 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
946 return (0);
|
|
947 }
|
|
948 else
|
136
|
949 {
|
|
950 cv->value = NULL;
|
|
951 cv->flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
952 return (-1);
|
|
953 }
|
122
|
954 }
|
|
955
|
|
956
|
|
957 static int
|
|
958 gftp_config_file_write_text (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
959 {
|
|
960 char *outstr;
|
|
961
|
|
962 if (cv->value != NULL)
|
|
963 {
|
|
964 outstr = cv->value;
|
|
965 if (*outstr != '\0')
|
|
966 fprintf (fd, "%s", outstr);
|
|
967 return (0);
|
|
968 }
|
|
969 else
|
|
970 return (-1);
|
|
971 }
|
|
972
|
|
973
|
|
974 static int
|
|
975 gftp_config_file_write_hidetext (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
976 {
|
|
977 char *outstr;
|
|
978
|
|
979 if (cv->value != NULL)
|
|
980 {
|
|
981 outstr = cv->value;
|
|
982 if (*outstr != '\0')
|
|
983 {
|
|
984 if (to_config_file)
|
|
985 fprintf (fd, "%s", outstr);
|
|
986 else
|
|
987 fprintf (fd, "*****");
|
|
988 }
|
|
989 return (0);
|
|
990 }
|
|
991 else
|
|
992 return (-1);
|
|
993 }
|
|
994
|
|
995
|
207
|
996 static void
|
|
997 gftp_config_file_copy_text (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
998 {
|
|
999 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1000 g_free (dest_cv->value);
|
|
1001
|
|
1002 if (cv->value != NULL)
|
|
1003 {
|
|
1004 dest_cv->value = g_strdup ((char *) cv->value);
|
|
1005 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1006 }
|
|
1007 else
|
|
1008 dest_cv->value = NULL;
|
|
1009 }
|
|
1010
|
|
1011
|
227
|
1012 static int
|
|
1013 gftp_config_file_compare_text (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1014 {
|
|
1015 char *str1, *str2;
|
|
1016
|
|
1017 str1 = cv1->value;
|
|
1018 str2 = cv2->value;
|
|
1019
|
|
1020 if (cv1->value == NULL && cv2->value == NULL)
|
|
1021 return (0);
|
|
1022
|
|
1023 if ((cv1->value == NULL && cv2->value != NULL) ||
|
|
1024 (cv1->value != NULL && cv2->value == NULL))
|
|
1025 return (-1);
|
|
1026
|
|
1027 return (strcmp (str1, str2));
|
|
1028 }
|
|
1029
|
|
1030
|
207
|
1031 static void
|
|
1032 gftp_config_file_copy_ptr_contents (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1033 {
|
|
1034 memcpy (&dest_cv->value, &cv->value, sizeof (dest_cv->value));
|
|
1035 }
|
|
1036
|
122
|
1037 static int
|
|
1038 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line)
|
|
1039 {
|
|
1040 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10));
|
|
1041 return (0);
|
|
1042 }
|
|
1043
|
|
1044
|
|
1045 static int
|
|
1046 gftp_config_file_write_int (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1047 {
|
|
1048 fprintf (fd, "%d", GPOINTER_TO_INT(cv->value));
|
|
1049 return (0);
|
|
1050 }
|
|
1051
|
|
1052
|
|
1053 static int
|
227
|
1054 gftp_config_file_compare_int (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1055 {
|
|
1056 return (GPOINTER_TO_INT(cv1->value) == GPOINTER_TO_INT(cv2->value) ? 0 : -1);
|
|
1057 }
|
|
1058
|
|
1059
|
|
1060 static int
|
122
|
1061 gftp_config_file_read_checkbox (char *str, gftp_config_vars * cv, int line)
|
|
1062 {
|
|
1063 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10) ? 1 : 0);
|
|
1064 return (0);
|
|
1065 }
|
|
1066
|
|
1067
|
|
1068 static int
|
|
1069 gftp_config_file_read_float (char *str, gftp_config_vars * cv, int line)
|
|
1070 {
|
325
|
1071 union { intptr_t i; float f; } r;
|
131
|
1072
|
325
|
1073 r.f = strtod (str, NULL);
|
|
1074 memcpy (&cv->value, &r.i, sizeof (cv->value));
|
122
|
1075 return (0);
|
|
1076 }
|
|
1077
|
|
1078
|
|
1079 static int
|
|
1080 gftp_config_file_write_float (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1081 {
|
131
|
1082 float f;
|
|
1083
|
|
1084 memcpy (&f, &cv->value, sizeof (f));
|
|
1085 fprintf (fd, "%.2f", f);
|
122
|
1086 return (0);
|
|
1087 }
|
|
1088
|
|
1089
|
|
1090 static int
|
227
|
1091 gftp_config_file_compare_float (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1092 {
|
|
1093 float f1, f2;
|
|
1094
|
|
1095 memcpy (&f1, &cv1->value, sizeof (f1));
|
|
1096 memcpy (&f2, &cv2->value, sizeof (f2));
|
|
1097 return (f1 == f2 ? 0 : -1);
|
|
1098 }
|
|
1099
|
|
1100
|
|
1101 static int
|
122
|
1102 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line)
|
|
1103 {
|
|
1104 char *red, *green, *blue;
|
|
1105 gftp_color * color;
|
|
1106
|
136
|
1107 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
1108 g_free (cv->value);
|
|
1109
|
198
|
1110 gftp_config_parse_args (str, 3, line, &red, &green, &blue);
|
122
|
1111
|
|
1112 color = g_malloc (sizeof (*color));
|
|
1113 color->red = strtol (red, NULL, 16);
|
|
1114 color->green = strtol (green, NULL, 16);
|
|
1115 color->blue = strtol (blue, NULL, 16);
|
|
1116 g_free (red);
|
|
1117 g_free (green);
|
|
1118 g_free (blue);
|
|
1119
|
|
1120 cv->value = color;
|
136
|
1121 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
1122
|
|
1123 return (0);
|
|
1124 }
|
|
1125
|
|
1126
|
|
1127 static int
|
|
1128 gftp_config_file_write_color (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1129 {
|
|
1130 gftp_color * color;
|
|
1131
|
|
1132 color = cv->value;
|
|
1133 fprintf (fd, "%x:%x:%x", color->red, color->green, color->blue);
|
|
1134 return (0);
|
|
1135 }
|
|
1136
|
|
1137
|
207
|
1138 static void
|
|
1139 gftp_config_file_copy_color (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1140 {
|
|
1141 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1142 g_free (dest_cv->value);
|
|
1143
|
|
1144 dest_cv->value = g_malloc (sizeof (gftp_color));
|
|
1145 memcpy (dest_cv->value, cv->value, sizeof (gftp_color));
|
|
1146 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1147 }
|
|
1148
|
|
1149
|
122
|
1150 static int
|
227
|
1151 gftp_config_file_compare_color (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1152 {
|
|
1153 gftp_color * color1, * color2;
|
|
1154
|
|
1155 color1 = cv1->value;
|
|
1156 color2 = cv2->value;
|
|
1157
|
|
1158 return (color1->red == color2->red && color1->green == color2->green &&
|
|
1159 color1->blue == color2->blue ? 0 : -1);
|
|
1160 }
|
|
1161
|
|
1162
|
|
1163 static int
|
122
|
1164 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line)
|
|
1165 {
|
|
1166 char **clist;
|
|
1167 int i;
|
|
1168
|
|
1169 cv->value = 0;
|
|
1170 if (cv->listdata != NULL)
|
|
1171 {
|
|
1172 clist = cv->listdata;
|
|
1173 for (i=0; clist[i] != NULL; i++)
|
|
1174 {
|
125
|
1175 if (strcasecmp (clist[i], str) == 0)
|
122
|
1176 {
|
|
1177 cv->value = GINT_TO_POINTER(i);
|
|
1178 break;
|
|
1179 }
|
|
1180 }
|
|
1181 }
|
|
1182
|
|
1183 return (0);
|
|
1184 }
|
|
1185
|
|
1186
|
|
1187 static int
|
|
1188 gftp_config_file_write_intcombo (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1189 {
|
|
1190 char **clist;
|
|
1191
|
|
1192 clist = cv->listdata;
|
|
1193 if (clist != NULL)
|
125
|
1194 fprintf (fd, "%s", clist[GPOINTER_TO_INT(cv->value)]);
|
122
|
1195 else
|
|
1196 fprintf (fd, _("<unknown>"));
|
|
1197
|
|
1198 return (0);
|
|
1199 }
|
|
1200
|
|
1201
|
125
|
1202 static int
|
|
1203 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line)
|
|
1204 {
|
|
1205 char **clist;
|
|
1206 int i;
|
|
1207
|
|
1208 cv->value = NULL;
|
|
1209 if (cv->listdata != NULL)
|
|
1210 {
|
|
1211 clist = cv->listdata;
|
|
1212 for (i=0; clist[i] != NULL; i++)
|
|
1213 {
|
|
1214 if (strcasecmp (clist[i], str) == 0)
|
|
1215 {
|
|
1216 cv->value = clist[i];
|
|
1217 break;
|
|
1218 }
|
|
1219 }
|
|
1220 }
|
|
1221
|
|
1222 return (0);
|
|
1223 }
|
|
1224
|
|
1225
|
|
1226 /* Note, the index numbers of this array must match up to the numbers in
|
122
|
1227 gftp_option_type_enum in gftp.h */
|
|
1228 gftp_option_type_var gftp_option_types[] = {
|
227
|
1229 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1230 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1231 {gftp_config_file_read_textcombo, gftp_config_file_write_text,
|
|
1232 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1233 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1234 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1235 {gftp_config_file_read_text, gftp_config_file_write_hidetext,
|
|
1236 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1237 {gftp_config_file_read_int, 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_checkbox, gftp_config_file_write_int,
|
|
1241 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1242 NULL, NULL, NULL},
|
|
1243 {gftp_config_file_read_intcombo, gftp_config_file_write_intcombo,
|
|
1244 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1245 NULL, NULL, NULL},
|
|
1246 {gftp_config_file_read_float, gftp_config_file_write_float,
|
|
1247 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_float,
|
|
1248 NULL, NULL, NULL},
|
|
1249 {gftp_config_file_read_color, gftp_config_file_write_color,
|
|
1250 gftp_config_file_copy_color, gftp_config_file_compare_color,
|
|
1251 NULL, NULL, NULL},
|
|
1252 {NULL, NULL, NULL, NULL, NULL, NULL},
|
|
1253 {NULL, NULL, NULL, NULL, NULL, NULL}
|
122
|
1254 };
|
|
1255
|
|
1256
|
|
1257 void
|
|
1258 gftp_lookup_global_option (char * key, void *value)
|
|
1259 {
|
|
1260 gftp_config_list_vars * tmplistvar;
|
|
1261 gftp_config_vars * tmpconfigvar;
|
|
1262
|
|
1263 if (gftp_global_options_htable != NULL &&
|
|
1264 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1265 key)) != NULL)
|
124
|
1266 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
122
|
1267 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
1268 key)) != NULL)
|
124
|
1269 *(gftp_config_list_vars **) value = tmplistvar;
|
122
|
1270 else
|
|
1271 {
|
|
1272 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1273 exit (1);
|
|
1274 }
|
|
1275 }
|
|
1276
|
|
1277
|
|
1278 void
|
|
1279 gftp_lookup_request_option (gftp_request * request, char * key, void *value)
|
|
1280 {
|
143
|
1281 gftp_config_vars * tmpconfigvar;
|
|
1282
|
|
1283 if (request != NULL && request->local_options_hash != NULL &&
|
|
1284 (tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1285 key)) != NULL)
|
|
1286 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1287 else
|
|
1288 gftp_lookup_global_option (key, value);
|
122
|
1289 }
|
|
1290
|
|
1291
|
|
1292 void
|
229
|
1293 gftp_lookup_bookmark_option (gftp_bookmarks_var * bm, char * key, void *value)
|
|
1294 {
|
|
1295 gftp_config_vars * tmpconfigvar;
|
|
1296
|
|
1297 if (bm != NULL && bm->local_options_hash != NULL &&
|
|
1298 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1299 key)) != NULL)
|
|
1300 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1301 else
|
|
1302 gftp_lookup_global_option (key, value);
|
|
1303 }
|
|
1304
|
|
1305
|
|
1306 void
|
227
|
1307 gftp_set_global_option (char * key, const void *value)
|
122
|
1308 {
|
227
|
1309 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1310 void *nc_ptr;
|
|
1311 int ret;
|
129
|
1312
|
|
1313 if (gftp_global_options_htable != NULL &&
|
|
1314 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1315 key)) != NULL)
|
201
|
1316 {
|
227
|
1317 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1318
|
|
1319 /* Cheap warning fix for const pointer... */
|
|
1320 memcpy (&nc_ptr, &value, sizeof (nc_ptr));
|
|
1321 newconfigvar.value = nc_ptr;
|
|
1322 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
201
|
1323
|
227
|
1324 ret = gftp_option_types[newconfigvar.otype].compare_function (&newconfigvar, tmpconfigvar);
|
|
1325 if (ret != 0)
|
|
1326 {
|
|
1327 gftp_option_types[newconfigvar.otype].copy_function (&newconfigvar, tmpconfigvar);
|
|
1328 gftp_configuration_changed = 1;
|
|
1329 }
|
201
|
1330 }
|
129
|
1331 else
|
|
1332 {
|
|
1333 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1334 exit (1);
|
|
1335 }
|
122
|
1336 }
|
|
1337
|
|
1338
|
229
|
1339 static void
|
|
1340 _gftp_set_option_value (gftp_config_vars * cv, const void * newval)
|
|
1341 {
|
|
1342 gftp_config_vars newconfigvar;
|
|
1343 void *nc_ptr;
|
|
1344
|
|
1345 memcpy (&newconfigvar, cv, sizeof (newconfigvar));
|
|
1346
|
|
1347 /* Cheap warning fix for const pointer... */
|
|
1348 memcpy (&nc_ptr, &newval, sizeof (nc_ptr));
|
|
1349 newconfigvar.value = nc_ptr;
|
|
1350 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1351
|
|
1352 gftp_option_types[newconfigvar.otype].copy_function (&newconfigvar, cv);
|
|
1353 }
|
|
1354
|
|
1355
|
122
|
1356 void
|
229
|
1357 gftp_set_request_option (gftp_request * request, char * key, 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
|
229
|
1391 gftp_set_bookmark_option (gftp_bookmarks_var * bm, char * key, const void *value)
|
|
1392 {
|
|
1393 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1394 int ret;
|
|
1395
|
|
1396 if (bm->local_options_hash != NULL &&
|
|
1397 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1398 key)) != NULL)
|
|
1399 _gftp_set_option_value (tmpconfigvar, value);
|
|
1400 else
|
|
1401 {
|
|
1402 if (gftp_global_options_htable == NULL ||
|
|
1403 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1404 key)) == NULL)
|
|
1405 {
|
|
1406 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1407 exit (1);
|
|
1408 }
|
|
1409
|
|
1410 /* Check to see if this is set to the same value as the global option.
|
|
1411 If so, don't add it to the bookmark preferences */
|
|
1412 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1413 memcpy (&newconfigvar.value, &value, sizeof (newconfigvar.value));
|
|
1414 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1415
|
|
1416 ret = gftp_option_types[newconfigvar.otype].compare_function (&newconfigvar, tmpconfigvar);
|
|
1417 if (ret == 0)
|
|
1418 return;
|
|
1419
|
|
1420 if (bm->local_options_hash == NULL)
|
|
1421 bm->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1422 string_hash_compare);
|
|
1423
|
|
1424 bm->num_local_options_vars++;
|
|
1425 bm->local_options_vars = g_realloc (bm->local_options_vars,
|
|
1426 sizeof (gftp_config_vars) * bm->num_local_options_vars);
|
|
1427
|
|
1428 memcpy (&bm->local_options_vars[bm->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1429 _gftp_set_option_value (&bm->local_options_vars[bm->num_local_options_vars - 1], value);
|
|
1430
|
|
1431 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]);
|
|
1432 }
|
|
1433 }
|
|
1434
|
|
1435
|
|
1436 void
|
122
|
1437 gftp_register_config_vars (gftp_config_vars * config_vars)
|
|
1438 {
|
|
1439 gftp_options_list = g_list_append (gftp_options_list, config_vars);
|
|
1440 gftp_setup_global_options (config_vars);
|
|
1441 }
|
|
1442
|
199
|
1443
|
|
1444 void
|
|
1445 gftp_copy_local_options (gftp_config_vars ** new_options_vars,
|
|
1446 GHashTable ** new_options_hash,
|
|
1447 gftp_config_vars * orig_options,
|
|
1448 int num_local_options_vars)
|
|
1449 {
|
|
1450 int i;
|
|
1451
|
|
1452 if (orig_options == NULL || num_local_options_vars == 0)
|
|
1453 {
|
|
1454 *new_options_vars = NULL;
|
|
1455 *new_options_hash = NULL;
|
|
1456 return;
|
|
1457 }
|
|
1458
|
|
1459 *new_options_hash = g_hash_table_new (string_hash_function,
|
|
1460 string_hash_compare);
|
|
1461
|
|
1462 *new_options_vars = g_malloc (sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1463 memcpy (*new_options_vars, orig_options,
|
|
1464 sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1465
|
|
1466 for (i=0; i<num_local_options_vars; i++)
|
|
1467 {
|
|
1468 g_hash_table_insert (*new_options_hash, (*new_options_vars)[i].key,
|
|
1469 &(*new_options_vars)[i]);
|
207
|
1470
|
|
1471 (*new_options_vars)[i].value = NULL;
|
|
1472 (*new_options_vars)[i].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
199
|
1473
|
207
|
1474 gftp_option_types[(*new_options_vars)[i].otype].copy_function (&(orig_options)[i], &(*new_options_vars)[i]);
|
199
|
1475 }
|
|
1476 }
|
|
1477
|
201
|
1478
|
|
1479 void
|
|
1480 gftp_config_free_options (gftp_config_vars * options_vars,
|
|
1481 GHashTable * options_hash,
|
|
1482 int num_options_vars)
|
|
1483 {
|
|
1484 int i;
|
|
1485
|
|
1486 if (num_options_vars == 0)
|
|
1487 return;
|
|
1488
|
|
1489 if (num_options_vars > 0)
|
|
1490 {
|
|
1491 /* If num_options_vars is 0, then the options was allocated with malloc */
|
|
1492
|
|
1493 for (i=0; i<num_options_vars; i++)
|
|
1494 {
|
|
1495 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1496 options_vars[i].value != NULL)
|
|
1497 g_free (options_vars[i].value);
|
|
1498 }
|
|
1499
|
|
1500 g_free (options_vars);
|
|
1501 }
|
|
1502 else if (num_options_vars < 0)
|
|
1503 {
|
|
1504 /* Otherwise we are freeing the global options */
|
|
1505
|
|
1506 for (i=0; options_vars[i].key != NULL; i++)
|
|
1507 {
|
|
1508 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1509 options_vars[i].value != NULL)
|
|
1510 g_free (options_vars[i].value);
|
|
1511
|
|
1512 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNLISTMEM &&
|
|
1513 options_vars[i].listdata != NULL)
|
|
1514 g_free (options_vars[i].listdata);
|
|
1515 }
|
|
1516 }
|
|
1517
|
|
1518 if (options_hash != NULL)
|
|
1519 g_hash_table_destroy (options_hash);
|
|
1520 }
|
|
1521
|
|
1522
|
|
1523 void
|
|
1524 gftp_bookmarks_destroy (gftp_bookmarks_var * bookmarks)
|
|
1525 {
|
|
1526 gftp_bookmarks_var * tempentry, * delentry;
|
|
1527
|
|
1528 if (bookmarks == NULL)
|
|
1529 return;
|
|
1530
|
|
1531 tempentry = bookmarks;
|
|
1532 while (tempentry != NULL)
|
|
1533 {
|
|
1534 gftp_free_bookmark (tempentry);
|
227
|
1535
|
|
1536 if (tempentry->path != NULL)
|
|
1537 g_free (tempentry->path);
|
201
|
1538
|
|
1539 if (tempentry->children != NULL)
|
|
1540 {
|
|
1541 tempentry = tempentry->children;
|
|
1542 continue;
|
|
1543 }
|
|
1544
|
|
1545 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
1546 {
|
|
1547 delentry = tempentry;
|
|
1548 tempentry = tempentry->prev;
|
|
1549 g_free (delentry);
|
|
1550 }
|
|
1551
|
|
1552 delentry = tempentry;
|
|
1553 tempentry = tempentry->next;
|
|
1554 g_free (delentry);
|
|
1555 }
|
|
1556 }
|
|
1557
|
218
|
1558
|
|
1559 void
|
|
1560 gftp_free_proxy_hosts (GList * proxy_hosts)
|
|
1561 {
|
|
1562 gftp_proxy_hosts * hosts;
|
|
1563 GList * templist;
|
|
1564
|
|
1565 for (templist = proxy_hosts;
|
|
1566 templist != NULL;
|
|
1567 templist = templist->next)
|
|
1568 {
|
|
1569 hosts = templist->data;
|
|
1570
|
|
1571 if (hosts->domain)
|
|
1572 g_free (hosts->domain);
|
|
1573 g_free (hosts);
|
|
1574 }
|
|
1575
|
|
1576 g_list_free (proxy_hosts);
|
|
1577 }
|
|
1578
|
|
1579
|
|
1580 GList *
|
|
1581 gftp_copy_proxy_hosts (GList * proxy_hosts)
|
|
1582 {
|
|
1583 gftp_proxy_hosts * oldhosts, * newhosts;
|
|
1584 GList * templist, * new_proxy_hosts;
|
|
1585
|
|
1586 new_proxy_hosts = NULL;
|
|
1587
|
|
1588 if (proxy_hosts != NULL)
|
|
1589 {
|
|
1590 for (templist = proxy_hosts;
|
|
1591 templist != NULL;
|
|
1592 templist = templist->next)
|
|
1593 {
|
|
1594 oldhosts = templist->data;
|
|
1595
|
|
1596 newhosts = g_malloc0 (sizeof (*newhosts));
|
|
1597 memcpy (newhosts, oldhosts, sizeof (*newhosts));
|
|
1598
|
|
1599 if (oldhosts->domain)
|
|
1600 newhosts->domain = g_strdup (oldhosts->domain);
|
|
1601
|
|
1602 new_proxy_hosts = g_list_append (new_proxy_hosts, newhosts);
|
|
1603 }
|
|
1604 }
|
|
1605
|
|
1606 return (new_proxy_hosts);
|
|
1607 }
|
|
1608
|