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