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