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;
|
332
|
721 char *bmhdr, *pwhdr, *tempstr, *password;
|
122
|
722 FILE * bmfile;
|
199
|
723 int i;
|
122
|
724
|
|
725 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
|
726 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
|
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));
|
332
|
744 write_comment (bmfile, _(pwhdr));
|
122
|
745 fwrite ("\n", 1, 1, bmfile);
|
|
746
|
|
747 tempentry = gftp_bookmarks->children;
|
|
748 while (tempentry != NULL)
|
|
749 {
|
|
750 if (tempentry->children != NULL)
|
|
751 {
|
|
752 tempentry = tempentry->children;
|
|
753 continue;
|
|
754 }
|
330
|
755
|
122
|
756 tempstr = tempentry->path;
|
|
757 while (*tempstr == '/')
|
|
758 tempstr++;
|
330
|
759
|
|
760 if (tempentry->save_password && tempentry->pass != NULL)
|
332
|
761 password = gftp_scramble_password (tempentry->pass);
|
330
|
762 else
|
|
763 password = NULL;
|
|
764
|
122
|
765 fprintf (bmfile,
|
|
766 "[%s]\nhostname=%s\nport=%d\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nusername=%s\npassword=%s\naccount=%s\n",
|
|
767 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname,
|
|
768 tempentry->port, tempentry->protocol == NULL
|
|
769 || *tempentry->protocol ==
|
|
770 '\0' ? gftp_protocols[0].name : tempentry->protocol,
|
|
771 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir,
|
|
772 tempentry->local_dir == NULL ? "" : tempentry->local_dir,
|
|
773 tempentry->user == NULL ? "" : tempentry->user,
|
330
|
774 password == NULL ? "" : password,
|
122
|
775 tempentry->acct == NULL ? "" : tempentry->acct);
|
|
776
|
330
|
777 if (password != NULL)
|
|
778 g_free(password);
|
|
779
|
199
|
780 if (tempentry->local_options_vars != NULL)
|
|
781 {
|
|
782 for (i=0; i<tempentry->num_local_options_vars; i++)
|
|
783 {
|
|
784 fprintf (bmfile, "%s=", tempentry->local_options_vars[i].key);
|
|
785 gftp_option_types[tempentry->local_options_vars[i].otype].write_function (&tempentry->local_options_vars[i], bmfile, 1);
|
|
786 fprintf (bmfile, "\n");
|
|
787 }
|
|
788 }
|
122
|
789
|
|
790 fprintf (bmfile, "\n");
|
|
791
|
|
792 if (tempentry->next == NULL)
|
|
793 {
|
|
794 tempentry = tempentry->prev;
|
|
795 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
796 tempentry = tempentry->prev;
|
|
797 tempentry = tempentry->next;
|
|
798 }
|
|
799 else
|
|
800 tempentry = tempentry->next;
|
|
801 }
|
|
802
|
|
803 fclose (bmfile);
|
|
804 }
|
|
805
|
|
806
|
|
807 void
|
|
808 gftp_write_config_file (void)
|
|
809 {
|
|
810 gftp_config_vars * cv;
|
|
811 GList *templist;
|
|
812 FILE *conffile;
|
|
813 char *tempstr;
|
|
814 int i;
|
|
815
|
|
816 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
817 {
|
|
818 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
819 exit (1);
|
|
820 }
|
|
821
|
|
822 if ((conffile = fopen (tempstr, "w+")) == NULL)
|
|
823 {
|
|
824 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
825 g_strerror (errno));
|
|
826 exit (1);
|
|
827 }
|
|
828
|
|
829 g_free (tempstr);
|
|
830
|
|
831 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"));
|
|
832
|
|
833 for (templist = gftp_options_list;
|
|
834 templist != NULL;
|
|
835 templist = templist->next)
|
|
836 {
|
|
837 cv = templist->data;
|
|
838
|
|
839 for (i=0; cv[i].key != NULL; i++)
|
|
840 {
|
|
841 if (gftp_option_types[cv[i].otype].write_function == NULL ||
|
|
842 *cv[i].key == '\0')
|
|
843 continue;
|
|
844
|
|
845 fprintf (conffile, "\n");
|
|
846 if (cv[i].comment != NULL)
|
|
847 write_comment (conffile, _(cv[i].comment));
|
|
848
|
|
849 fprintf (conffile, "%s=", cv[i].key);
|
|
850 gftp_option_types[cv[i].otype].write_function (&cv[i], conffile, 1);
|
|
851 fprintf (conffile, "\n");
|
|
852 }
|
|
853 }
|
|
854
|
218
|
855 for (i=0; gftp_config_list[i].key != NULL; i++)
|
122
|
856 {
|
218
|
857 if (gftp_config_list[i].header == NULL &&
|
|
858 gftp_config_list[i].list == NULL)
|
|
859 continue;
|
|
860
|
122
|
861 fprintf (conffile, "\n");
|
|
862 if (gftp_config_list[i].header != NULL)
|
|
863 write_comment (conffile, _(gftp_config_list[i].header));
|
|
864
|
218
|
865 for (templist = gftp_config_list[i].list;
|
122
|
866 templist != NULL;
|
|
867 templist = templist->next)
|
|
868 {
|
|
869 fprintf (conffile, "%s=", gftp_config_list[i].key);
|
|
870 gftp_config_list[i].write_func (conffile, templist->data);
|
|
871 fprintf (conffile, "\n");
|
|
872 }
|
|
873 }
|
|
874
|
|
875 fclose (conffile);
|
|
876 }
|
|
877
|
|
878
|
1
|
879 GHashTable *
|
122
|
880 build_bookmarks_hash_table (gftp_bookmarks_var * entry)
|
1
|
881 {
|
122
|
882 gftp_bookmarks_var * tempentry;
|
1
|
883 GHashTable * htable;
|
|
884
|
|
885 htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
886 tempentry = entry;
|
|
887 while (tempentry != NULL)
|
|
888 {
|
|
889 g_hash_table_insert (htable, tempentry->path, tempentry);
|
|
890 if (tempentry->children != NULL)
|
|
891 {
|
|
892 tempentry = tempentry->children;
|
|
893 continue;
|
|
894 }
|
|
895 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
896 tempentry = tempentry->prev;
|
|
897 tempentry = tempentry->next;
|
|
898 }
|
|
899 return (htable);
|
|
900 }
|
|
901
|
|
902
|
|
903 void
|
122
|
904 print_bookmarks (gftp_bookmarks_var * bookmarks)
|
1
|
905 {
|
122
|
906 gftp_bookmarks_var * tempentry;
|
1
|
907
|
|
908 tempentry = bookmarks->children;
|
|
909 while (tempentry != NULL)
|
|
910 {
|
|
911 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL);
|
|
912 if (tempentry->children != NULL)
|
|
913 {
|
|
914 tempentry = tempentry->children;
|
|
915 continue;
|
|
916 }
|
|
917
|
|
918 if (tempentry->next == NULL)
|
|
919 {
|
|
920 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
921 tempentry = tempentry->prev;
|
|
922 tempentry = tempentry->next;
|
|
923 }
|
|
924 else
|
|
925 tempentry = tempentry->next;
|
|
926 }
|
|
927 }
|
|
928
|
122
|
929
|
|
930 static int
|
|
931 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line)
|
|
932 {
|
136
|
933 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
934 g_free (cv->value);
|
|
935
|
122
|
936 if (str != NULL)
|
|
937 {
|
|
938 cv->value = g_strdup (str);
|
136
|
939 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
940 return (0);
|
|
941 }
|
|
942 else
|
136
|
943 {
|
|
944 cv->value = NULL;
|
|
945 cv->flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
946 return (-1);
|
|
947 }
|
122
|
948 }
|
|
949
|
|
950
|
|
951 static int
|
|
952 gftp_config_file_write_text (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
953 {
|
|
954 char *outstr;
|
|
955
|
|
956 if (cv->value != NULL)
|
|
957 {
|
|
958 outstr = cv->value;
|
|
959 if (*outstr != '\0')
|
|
960 fprintf (fd, "%s", outstr);
|
|
961 return (0);
|
|
962 }
|
|
963 else
|
|
964 return (-1);
|
|
965 }
|
|
966
|
|
967
|
|
968 static int
|
|
969 gftp_config_file_write_hidetext (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
970 {
|
|
971 char *outstr;
|
|
972
|
|
973 if (cv->value != NULL)
|
|
974 {
|
|
975 outstr = cv->value;
|
|
976 if (*outstr != '\0')
|
|
977 {
|
|
978 if (to_config_file)
|
|
979 fprintf (fd, "%s", outstr);
|
|
980 else
|
|
981 fprintf (fd, "*****");
|
|
982 }
|
|
983 return (0);
|
|
984 }
|
|
985 else
|
|
986 return (-1);
|
|
987 }
|
|
988
|
|
989
|
207
|
990 static void
|
|
991 gftp_config_file_copy_text (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
992 {
|
|
993 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
994 g_free (dest_cv->value);
|
|
995
|
|
996 if (cv->value != NULL)
|
|
997 {
|
|
998 dest_cv->value = g_strdup ((char *) cv->value);
|
|
999 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1000 }
|
|
1001 else
|
|
1002 dest_cv->value = NULL;
|
|
1003 }
|
|
1004
|
|
1005
|
227
|
1006 static int
|
|
1007 gftp_config_file_compare_text (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1008 {
|
|
1009 char *str1, *str2;
|
|
1010
|
|
1011 str1 = cv1->value;
|
|
1012 str2 = cv2->value;
|
|
1013
|
|
1014 if (cv1->value == NULL && cv2->value == NULL)
|
|
1015 return (0);
|
|
1016
|
|
1017 if ((cv1->value == NULL && cv2->value != NULL) ||
|
|
1018 (cv1->value != NULL && cv2->value == NULL))
|
|
1019 return (-1);
|
|
1020
|
|
1021 return (strcmp (str1, str2));
|
|
1022 }
|
|
1023
|
|
1024
|
207
|
1025 static void
|
|
1026 gftp_config_file_copy_ptr_contents (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1027 {
|
|
1028 memcpy (&dest_cv->value, &cv->value, sizeof (dest_cv->value));
|
|
1029 }
|
|
1030
|
122
|
1031 static int
|
|
1032 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line)
|
|
1033 {
|
|
1034 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10));
|
|
1035 return (0);
|
|
1036 }
|
|
1037
|
|
1038
|
|
1039 static int
|
|
1040 gftp_config_file_write_int (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1041 {
|
|
1042 fprintf (fd, "%d", GPOINTER_TO_INT(cv->value));
|
|
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
|
|
1074 gftp_config_file_write_float (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1075 {
|
131
|
1076 float f;
|
|
1077
|
|
1078 memcpy (&f, &cv->value, sizeof (f));
|
|
1079 fprintf (fd, "%.2f", f);
|
122
|
1080 return (0);
|
|
1081 }
|
|
1082
|
|
1083
|
|
1084 static int
|
227
|
1085 gftp_config_file_compare_float (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1086 {
|
|
1087 float f1, f2;
|
|
1088
|
|
1089 memcpy (&f1, &cv1->value, sizeof (f1));
|
|
1090 memcpy (&f2, &cv2->value, sizeof (f2));
|
|
1091 return (f1 == f2 ? 0 : -1);
|
|
1092 }
|
|
1093
|
|
1094
|
|
1095 static int
|
122
|
1096 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line)
|
|
1097 {
|
|
1098 char *red, *green, *blue;
|
|
1099 gftp_color * color;
|
|
1100
|
136
|
1101 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
1102 g_free (cv->value);
|
|
1103
|
198
|
1104 gftp_config_parse_args (str, 3, line, &red, &green, &blue);
|
122
|
1105
|
|
1106 color = g_malloc (sizeof (*color));
|
|
1107 color->red = strtol (red, NULL, 16);
|
|
1108 color->green = strtol (green, NULL, 16);
|
|
1109 color->blue = strtol (blue, NULL, 16);
|
|
1110 g_free (red);
|
|
1111 g_free (green);
|
|
1112 g_free (blue);
|
|
1113
|
|
1114 cv->value = color;
|
136
|
1115 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
1116
|
|
1117 return (0);
|
|
1118 }
|
|
1119
|
|
1120
|
|
1121 static int
|
|
1122 gftp_config_file_write_color (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1123 {
|
|
1124 gftp_color * color;
|
|
1125
|
|
1126 color = cv->value;
|
|
1127 fprintf (fd, "%x:%x:%x", color->red, color->green, color->blue);
|
|
1128 return (0);
|
|
1129 }
|
|
1130
|
|
1131
|
207
|
1132 static void
|
|
1133 gftp_config_file_copy_color (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1134 {
|
|
1135 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1136 g_free (dest_cv->value);
|
|
1137
|
|
1138 dest_cv->value = g_malloc (sizeof (gftp_color));
|
|
1139 memcpy (dest_cv->value, cv->value, sizeof (gftp_color));
|
|
1140 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1141 }
|
|
1142
|
|
1143
|
122
|
1144 static int
|
227
|
1145 gftp_config_file_compare_color (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1146 {
|
|
1147 gftp_color * color1, * color2;
|
|
1148
|
|
1149 color1 = cv1->value;
|
|
1150 color2 = cv2->value;
|
|
1151
|
|
1152 return (color1->red == color2->red && color1->green == color2->green &&
|
|
1153 color1->blue == color2->blue ? 0 : -1);
|
|
1154 }
|
|
1155
|
|
1156
|
|
1157 static int
|
122
|
1158 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line)
|
|
1159 {
|
|
1160 char **clist;
|
|
1161 int i;
|
|
1162
|
|
1163 cv->value = 0;
|
|
1164 if (cv->listdata != NULL)
|
|
1165 {
|
|
1166 clist = cv->listdata;
|
|
1167 for (i=0; clist[i] != NULL; i++)
|
|
1168 {
|
125
|
1169 if (strcasecmp (clist[i], str) == 0)
|
122
|
1170 {
|
|
1171 cv->value = GINT_TO_POINTER(i);
|
|
1172 break;
|
|
1173 }
|
|
1174 }
|
|
1175 }
|
|
1176
|
|
1177 return (0);
|
|
1178 }
|
|
1179
|
|
1180
|
|
1181 static int
|
|
1182 gftp_config_file_write_intcombo (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1183 {
|
|
1184 char **clist;
|
|
1185
|
|
1186 clist = cv->listdata;
|
|
1187 if (clist != NULL)
|
125
|
1188 fprintf (fd, "%s", clist[GPOINTER_TO_INT(cv->value)]);
|
122
|
1189 else
|
|
1190 fprintf (fd, _("<unknown>"));
|
|
1191
|
|
1192 return (0);
|
|
1193 }
|
|
1194
|
|
1195
|
125
|
1196 static int
|
|
1197 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line)
|
|
1198 {
|
|
1199 char **clist;
|
|
1200 int i;
|
|
1201
|
|
1202 cv->value = NULL;
|
|
1203 if (cv->listdata != NULL)
|
|
1204 {
|
|
1205 clist = cv->listdata;
|
|
1206 for (i=0; clist[i] != NULL; i++)
|
|
1207 {
|
|
1208 if (strcasecmp (clist[i], str) == 0)
|
|
1209 {
|
|
1210 cv->value = clist[i];
|
|
1211 break;
|
|
1212 }
|
|
1213 }
|
|
1214 }
|
|
1215
|
|
1216 return (0);
|
|
1217 }
|
|
1218
|
|
1219
|
|
1220 /* Note, the index numbers of this array must match up to the numbers in
|
122
|
1221 gftp_option_type_enum in gftp.h */
|
|
1222 gftp_option_type_var gftp_option_types[] = {
|
227
|
1223 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1224 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1225 {gftp_config_file_read_textcombo, gftp_config_file_write_text,
|
|
1226 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1227 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1228 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1229 {gftp_config_file_read_text, gftp_config_file_write_hidetext,
|
|
1230 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1231 {gftp_config_file_read_int, gftp_config_file_write_int,
|
|
1232 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1233 NULL, NULL, NULL},
|
|
1234 {gftp_config_file_read_checkbox, 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_intcombo, gftp_config_file_write_intcombo,
|
|
1238 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1239 NULL, NULL, NULL},
|
|
1240 {gftp_config_file_read_float, gftp_config_file_write_float,
|
|
1241 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_float,
|
|
1242 NULL, NULL, NULL},
|
|
1243 {gftp_config_file_read_color, gftp_config_file_write_color,
|
|
1244 gftp_config_file_copy_color, gftp_config_file_compare_color,
|
|
1245 NULL, NULL, NULL},
|
|
1246 {NULL, NULL, NULL, NULL, NULL, NULL},
|
|
1247 {NULL, NULL, NULL, NULL, NULL, NULL}
|
122
|
1248 };
|
|
1249
|
|
1250
|
|
1251 void
|
374
|
1252 gftp_lookup_global_option (const char * key, void *value)
|
122
|
1253 {
|
|
1254 gftp_config_list_vars * tmplistvar;
|
|
1255 gftp_config_vars * tmpconfigvar;
|
|
1256
|
|
1257 if (gftp_global_options_htable != NULL &&
|
|
1258 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1259 key)) != NULL)
|
124
|
1260 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
122
|
1261 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
1262 key)) != NULL)
|
124
|
1263 *(gftp_config_list_vars **) value = tmplistvar;
|
122
|
1264 else
|
|
1265 {
|
|
1266 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1267 exit (1);
|
|
1268 }
|
|
1269 }
|
|
1270
|
|
1271
|
|
1272 void
|
374
|
1273 gftp_lookup_request_option (gftp_request * request, const char * key,
|
|
1274 void *value)
|
122
|
1275 {
|
143
|
1276 gftp_config_vars * tmpconfigvar;
|
|
1277
|
|
1278 if (request != NULL && request->local_options_hash != NULL &&
|
|
1279 (tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1280 key)) != NULL)
|
|
1281 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1282 else
|
|
1283 gftp_lookup_global_option (key, value);
|
122
|
1284 }
|
|
1285
|
|
1286
|
|
1287 void
|
374
|
1288 gftp_lookup_bookmark_option (gftp_bookmarks_var * bm, const char * key,
|
|
1289 void *value)
|
229
|
1290 {
|
|
1291 gftp_config_vars * tmpconfigvar;
|
|
1292
|
|
1293 if (bm != NULL && bm->local_options_hash != NULL &&
|
|
1294 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1295 key)) != NULL)
|
|
1296 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1297 else
|
|
1298 gftp_lookup_global_option (key, value);
|
|
1299 }
|
|
1300
|
|
1301
|
|
1302 void
|
374
|
1303 gftp_set_global_option (const char * key, const void *value)
|
122
|
1304 {
|
227
|
1305 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1306 void *nc_ptr;
|
|
1307 int ret;
|
129
|
1308
|
|
1309 if (gftp_global_options_htable != NULL &&
|
|
1310 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1311 key)) != NULL)
|
201
|
1312 {
|
227
|
1313 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1314
|
|
1315 /* Cheap warning fix for const pointer... */
|
|
1316 memcpy (&nc_ptr, &value, sizeof (nc_ptr));
|
|
1317 newconfigvar.value = nc_ptr;
|
|
1318 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
201
|
1319
|
227
|
1320 ret = gftp_option_types[newconfigvar.otype].compare_function (&newconfigvar, tmpconfigvar);
|
|
1321 if (ret != 0)
|
|
1322 {
|
|
1323 gftp_option_types[newconfigvar.otype].copy_function (&newconfigvar, tmpconfigvar);
|
|
1324 gftp_configuration_changed = 1;
|
|
1325 }
|
201
|
1326 }
|
129
|
1327 else
|
|
1328 {
|
|
1329 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1330 exit (1);
|
|
1331 }
|
122
|
1332 }
|
|
1333
|
|
1334
|
229
|
1335 static void
|
|
1336 _gftp_set_option_value (gftp_config_vars * cv, const void * newval)
|
|
1337 {
|
|
1338 gftp_config_vars newconfigvar;
|
|
1339 void *nc_ptr;
|
|
1340
|
|
1341 memcpy (&newconfigvar, cv, sizeof (newconfigvar));
|
|
1342
|
|
1343 /* Cheap warning fix for const pointer... */
|
|
1344 memcpy (&nc_ptr, &newval, sizeof (nc_ptr));
|
|
1345 newconfigvar.value = nc_ptr;
|
|
1346 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1347
|
|
1348 gftp_option_types[newconfigvar.otype].copy_function (&newconfigvar, cv);
|
|
1349 }
|
|
1350
|
|
1351
|
122
|
1352 void
|
374
|
1353 gftp_set_request_option (gftp_request * request, const char * key,
|
|
1354 const void *value)
|
122
|
1355 {
|
136
|
1356 gftp_config_vars * tmpconfigvar;
|
|
1357
|
|
1358 if (request->local_options_hash == NULL)
|
|
1359 request->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1360 string_hash_compare);
|
|
1361
|
|
1362 if ((tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1363 key)) != NULL)
|
229
|
1364 _gftp_set_option_value (tmpconfigvar, value);
|
136
|
1365 else
|
|
1366 {
|
143
|
1367 if (gftp_global_options_htable == NULL ||
|
136
|
1368 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1369 key)) == NULL)
|
|
1370 {
|
|
1371 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1372 exit (1);
|
|
1373 }
|
|
1374
|
|
1375 request->num_local_options_vars++;
|
|
1376 request->local_options_vars = g_realloc (request->local_options_vars,
|
199
|
1377 sizeof (gftp_config_vars) * request->num_local_options_vars);
|
136
|
1378
|
229
|
1379 memcpy (&request->local_options_vars[request->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1380 _gftp_set_option_value (&request->local_options_vars[request->num_local_options_vars - 1], value);
|
143
|
1381
|
|
1382 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
|
1383 }
|
122
|
1384 }
|
|
1385
|
|
1386
|
|
1387 void
|
374
|
1388 gftp_set_bookmark_option (gftp_bookmarks_var * bm, const char * key,
|
|
1389 const void *value)
|
229
|
1390 {
|
|
1391 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1392 int ret;
|
|
1393
|
|
1394 if (bm->local_options_hash != NULL &&
|
|
1395 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1396 key)) != NULL)
|
|
1397 _gftp_set_option_value (tmpconfigvar, value);
|
|
1398 else
|
|
1399 {
|
|
1400 if (gftp_global_options_htable == NULL ||
|
|
1401 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1402 key)) == NULL)
|
|
1403 {
|
|
1404 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1405 exit (1);
|
|
1406 }
|
|
1407
|
|
1408 /* Check to see if this is set to the same value as the global option.
|
|
1409 If so, don't add it to the bookmark preferences */
|
|
1410 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1411 memcpy (&newconfigvar.value, &value, sizeof (newconfigvar.value));
|
|
1412 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1413
|
|
1414 ret = gftp_option_types[newconfigvar.otype].compare_function (&newconfigvar, tmpconfigvar);
|
|
1415 if (ret == 0)
|
|
1416 return;
|
|
1417
|
|
1418 if (bm->local_options_hash == NULL)
|
|
1419 bm->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1420 string_hash_compare);
|
|
1421
|
|
1422 bm->num_local_options_vars++;
|
|
1423 bm->local_options_vars = g_realloc (bm->local_options_vars,
|
|
1424 sizeof (gftp_config_vars) * bm->num_local_options_vars);
|
|
1425
|
|
1426 memcpy (&bm->local_options_vars[bm->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1427 _gftp_set_option_value (&bm->local_options_vars[bm->num_local_options_vars - 1], value);
|
|
1428
|
|
1429 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]);
|
|
1430 }
|
|
1431 }
|
|
1432
|
|
1433
|
|
1434 void
|
122
|
1435 gftp_register_config_vars (gftp_config_vars * config_vars)
|
|
1436 {
|
|
1437 gftp_options_list = g_list_append (gftp_options_list, config_vars);
|
|
1438 gftp_setup_global_options (config_vars);
|
|
1439 }
|
|
1440
|
199
|
1441
|
|
1442 void
|
|
1443 gftp_copy_local_options (gftp_config_vars ** new_options_vars,
|
|
1444 GHashTable ** new_options_hash,
|
|
1445 gftp_config_vars * orig_options,
|
|
1446 int num_local_options_vars)
|
|
1447 {
|
|
1448 int i;
|
|
1449
|
|
1450 if (orig_options == NULL || num_local_options_vars == 0)
|
|
1451 {
|
|
1452 *new_options_vars = NULL;
|
|
1453 *new_options_hash = NULL;
|
|
1454 return;
|
|
1455 }
|
|
1456
|
|
1457 *new_options_hash = g_hash_table_new (string_hash_function,
|
|
1458 string_hash_compare);
|
|
1459
|
|
1460 *new_options_vars = g_malloc (sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1461 memcpy (*new_options_vars, orig_options,
|
|
1462 sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1463
|
|
1464 for (i=0; i<num_local_options_vars; i++)
|
|
1465 {
|
|
1466 g_hash_table_insert (*new_options_hash, (*new_options_vars)[i].key,
|
|
1467 &(*new_options_vars)[i]);
|
207
|
1468
|
|
1469 (*new_options_vars)[i].value = NULL;
|
|
1470 (*new_options_vars)[i].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
199
|
1471
|
207
|
1472 gftp_option_types[(*new_options_vars)[i].otype].copy_function (&(orig_options)[i], &(*new_options_vars)[i]);
|
199
|
1473 }
|
|
1474 }
|
|
1475
|
201
|
1476
|
|
1477 void
|
|
1478 gftp_config_free_options (gftp_config_vars * options_vars,
|
|
1479 GHashTable * options_hash,
|
|
1480 int num_options_vars)
|
|
1481 {
|
|
1482 int i;
|
|
1483
|
|
1484 if (num_options_vars == 0)
|
|
1485 return;
|
|
1486
|
|
1487 if (num_options_vars > 0)
|
|
1488 {
|
|
1489 /* If num_options_vars is 0, then the options was allocated with malloc */
|
|
1490
|
|
1491 for (i=0; i<num_options_vars; i++)
|
|
1492 {
|
|
1493 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1494 options_vars[i].value != NULL)
|
|
1495 g_free (options_vars[i].value);
|
|
1496 }
|
|
1497
|
|
1498 g_free (options_vars);
|
|
1499 }
|
|
1500 else if (num_options_vars < 0)
|
|
1501 {
|
|
1502 /* Otherwise we are freeing the global options */
|
|
1503
|
|
1504 for (i=0; options_vars[i].key != NULL; i++)
|
|
1505 {
|
|
1506 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1507 options_vars[i].value != NULL)
|
|
1508 g_free (options_vars[i].value);
|
|
1509
|
|
1510 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNLISTMEM &&
|
|
1511 options_vars[i].listdata != NULL)
|
|
1512 g_free (options_vars[i].listdata);
|
|
1513 }
|
|
1514 }
|
|
1515
|
|
1516 if (options_hash != NULL)
|
|
1517 g_hash_table_destroy (options_hash);
|
|
1518 }
|
|
1519
|
|
1520
|
|
1521 void
|
|
1522 gftp_bookmarks_destroy (gftp_bookmarks_var * bookmarks)
|
|
1523 {
|
|
1524 gftp_bookmarks_var * tempentry, * delentry;
|
|
1525
|
|
1526 if (bookmarks == NULL)
|
|
1527 return;
|
|
1528
|
|
1529 tempentry = bookmarks;
|
|
1530 while (tempentry != NULL)
|
|
1531 {
|
|
1532 gftp_free_bookmark (tempentry);
|
227
|
1533
|
|
1534 if (tempentry->path != NULL)
|
|
1535 g_free (tempentry->path);
|
201
|
1536
|
|
1537 if (tempentry->children != NULL)
|
|
1538 {
|
|
1539 tempentry = tempentry->children;
|
|
1540 continue;
|
|
1541 }
|
|
1542
|
|
1543 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
1544 {
|
|
1545 delentry = tempentry;
|
|
1546 tempentry = tempentry->prev;
|
|
1547 g_free (delentry);
|
|
1548 }
|
|
1549
|
|
1550 delentry = tempentry;
|
|
1551 tempentry = tempentry->next;
|
|
1552 g_free (delentry);
|
|
1553 }
|
|
1554 }
|
|
1555
|
218
|
1556
|
|
1557 void
|
|
1558 gftp_free_proxy_hosts (GList * proxy_hosts)
|
|
1559 {
|
|
1560 gftp_proxy_hosts * hosts;
|
|
1561 GList * templist;
|
|
1562
|
|
1563 for (templist = proxy_hosts;
|
|
1564 templist != NULL;
|
|
1565 templist = templist->next)
|
|
1566 {
|
|
1567 hosts = templist->data;
|
|
1568
|
|
1569 if (hosts->domain)
|
|
1570 g_free (hosts->domain);
|
|
1571 g_free (hosts);
|
|
1572 }
|
|
1573
|
|
1574 g_list_free (proxy_hosts);
|
|
1575 }
|
|
1576
|
|
1577
|
|
1578 GList *
|
|
1579 gftp_copy_proxy_hosts (GList * proxy_hosts)
|
|
1580 {
|
|
1581 gftp_proxy_hosts * oldhosts, * newhosts;
|
|
1582 GList * templist, * new_proxy_hosts;
|
|
1583
|
|
1584 new_proxy_hosts = NULL;
|
|
1585
|
|
1586 if (proxy_hosts != NULL)
|
|
1587 {
|
|
1588 for (templist = proxy_hosts;
|
|
1589 templist != NULL;
|
|
1590 templist = templist->next)
|
|
1591 {
|
|
1592 oldhosts = templist->data;
|
|
1593
|
|
1594 newhosts = g_malloc0 (sizeof (*newhosts));
|
|
1595 memcpy (newhosts, oldhosts, sizeof (*newhosts));
|
|
1596
|
|
1597 if (oldhosts->domain)
|
|
1598 newhosts->domain = g_strdup (oldhosts->domain);
|
|
1599
|
|
1600 new_proxy_hosts = g_list_append (new_proxy_hosts, newhosts);
|
|
1601 }
|
|
1602 }
|
|
1603
|
|
1604 return (new_proxy_hosts);
|
|
1605 }
|
|
1606
|