1
|
1 /*****************************************************************************/
|
|
2 /* config_file.c - config file routines */
|
885
|
3 /* Copyright (C) 1998-2007 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
|
942
|
366 *dest = g_malloc0 ((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 **);
|
942
|
398 *dest = g_malloc0 (1);
|
1
|
399 numargs--;
|
|
400 }
|
|
401 va_end (argp);
|
516
|
402 return (ret);
|
1
|
403 }
|
|
404
|
|
405
|
122
|
406 static void *
|
|
407 gftp_config_read_str (char *buf, int line)
|
|
408 {
|
516
|
409 return (g_strdup (buf));
|
122
|
410 }
|
|
411
|
|
412
|
|
413 static void
|
|
414 gftp_config_write_str (FILE *fd, void *data)
|
|
415 {
|
|
416 fprintf (fd, "%s", (char *) data);
|
|
417 }
|
|
418
|
|
419
|
|
420 static void *
|
|
421 gftp_config_read_proxy (char *buf, int line)
|
|
422 {
|
|
423 gftp_proxy_hosts * host;
|
|
424 unsigned int nums[4];
|
|
425 char *pos;
|
|
426
|
|
427 host = g_malloc0 (sizeof (*host));
|
|
428 if ((pos = strchr (buf, '/')) == NULL)
|
|
429 host->domain = g_strdup (buf);
|
|
430 else
|
|
431 {
|
|
432 *pos = '\0';
|
|
433 sscanf (buf, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], &nums[3]);
|
|
434 host->ipv4_network_address =
|
|
435 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
436
|
|
437 if (strchr (pos + 1, '.') == NULL)
|
|
438 host->ipv4_netmask = 0xffffffff << (32 - strtol (pos + 1, NULL, 10));
|
|
439 else
|
|
440 {
|
|
441 sscanf (pos + 1, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2],
|
|
442 &nums[3]);
|
|
443 host->ipv4_netmask =
|
|
444 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
445 }
|
|
446 }
|
|
447
|
|
448 return (host);
|
|
449 }
|
|
450
|
|
451
|
|
452 static void
|
|
453 gftp_config_write_proxy (FILE *fd, void *data)
|
|
454 {
|
|
455 gftp_proxy_hosts * host;
|
|
456
|
|
457 host = data;
|
|
458
|
|
459 if (host->domain)
|
|
460 fprintf (fd, "%s", host->domain);
|
|
461 else
|
|
462 fprintf (fd, "%d.%d.%d.%d/%d.%d.%d.%d",
|
|
463 host->ipv4_network_address >> 24 & 0xff,
|
|
464 host->ipv4_network_address >> 16 & 0xff,
|
|
465 host->ipv4_network_address >> 8 & 0xff,
|
|
466 host->ipv4_network_address & 0xff,
|
|
467 host->ipv4_netmask >> 24 & 0xff,
|
|
468 host->ipv4_netmask >> 16 & 0xff,
|
|
469 host->ipv4_netmask >> 8 & 0xff,
|
|
470 host->ipv4_netmask & 0xff);
|
|
471 }
|
|
472
|
|
473
|
|
474 static void *
|
|
475 gftp_config_read_ext (char *buf, int line)
|
|
476 {
|
|
477 gftp_file_extensions * tempext;
|
|
478
|
942
|
479 tempext = g_malloc0 (sizeof (*tempext));
|
198
|
480 gftp_config_parse_args (buf, 4, line, &tempext->ext, &tempext->filename,
|
|
481 &tempext->ascii_binary, &tempext->view_program);
|
122
|
482
|
|
483 tempext->stlen = strlen (tempext->ext);
|
|
484
|
|
485 return (tempext);
|
|
486 }
|
|
487
|
|
488
|
|
489 static void
|
|
490 gftp_config_write_ext (FILE *fd, void *data)
|
|
491 {
|
|
492 gftp_file_extensions * tempext;
|
|
493
|
|
494 tempext = data;
|
|
495 fprintf (fd, "%s:%s:%c:%s", tempext->ext, tempext->filename,
|
|
496 *tempext->ascii_binary == '\0' ? ' ' : *tempext->ascii_binary,
|
|
497 tempext->view_program);
|
|
498 }
|
|
499
|
|
500
|
516
|
501 static gftp_config_list_vars gftp_config_list[] = {
|
122
|
502 {"dont_use_proxy", gftp_config_read_proxy, gftp_config_write_proxy,
|
|
503 NULL, 0,
|
|
504 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")},
|
|
505 {"ext", gftp_config_read_ext, gftp_config_write_ext,
|
|
506 NULL, 0,
|
|
507 N_("ext=file extenstion:XPM file:Ascii or Binary (A or B):viewer program. Note: All arguments except the file extension are optional")},
|
|
508 {"localhistory", gftp_config_read_str, gftp_config_write_str,
|
|
509 NULL, 0, NULL},
|
|
510 {"remotehistory", gftp_config_read_str, gftp_config_write_str,
|
|
511 NULL, 0, NULL},
|
|
512 {"hosthistory", gftp_config_read_str, gftp_config_write_str,
|
|
513 NULL, 0, NULL},
|
|
514 {"porthistory", gftp_config_read_str, gftp_config_write_str,
|
|
515 NULL, 0, NULL},
|
|
516 {"userhistory", gftp_config_read_str, gftp_config_write_str,
|
|
517 NULL, 0, NULL},
|
|
518 {NULL, NULL, NULL,
|
|
519 NULL, 0, NULL}
|
|
520 };
|
|
521
|
|
522
|
|
523 static void
|
|
524 gftp_setup_global_options (gftp_config_vars * cvars)
|
|
525 {
|
|
526 int i;
|
|
527
|
|
528 for (i=0; cvars[i].key != NULL; i++)
|
|
529 {
|
136
|
530 if (cvars[i].key != NULL && *cvars[i].key != '\0')
|
122
|
531 g_hash_table_insert (gftp_global_options_htable,
|
|
532 cvars[i].key, &cvars[i]);
|
|
533 }
|
|
534 }
|
|
535
|
|
536
|
|
537 void
|
124
|
538 gftp_read_config_file (char *global_data_path)
|
122
|
539 {
|
|
540 char *tempstr, *temp1str, *curpos, buf[255];
|
|
541 gftp_config_list_vars * tmplistvar;
|
|
542 gftp_config_vars * tmpconfigvar;
|
125
|
543 char **protocol_list;
|
122
|
544 FILE *conffile;
|
600
|
545 int line, i, j;
|
122
|
546 size_t len;
|
|
547
|
|
548 gftp_global_options_htable = g_hash_table_new (string_hash_function,
|
|
549 string_hash_compare);
|
|
550
|
|
551 gftp_register_config_vars (gftp_global_config_vars);
|
|
552
|
125
|
553 protocol_list = NULL;
|
600
|
554 for (i=0, j=0; gftp_protocols[i].register_options != NULL; i++)
|
122
|
555 {
|
125
|
556 if (gftp_protocols[i].shown)
|
|
557 {
|
765
|
558 protocol_list = g_realloc (protocol_list,
|
|
559 (gulong) sizeof (char *) * (j + 2));
|
600
|
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);
|
765
|
588 exit (EXIT_FAILURE);
|
122
|
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));
|
765
|
600 exit (EXIT_FAILURE);
|
122
|
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"));
|
765
|
611 exit (EXIT_FAILURE);
|
122
|
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));
|
765
|
621 exit (EXIT_FAILURE);
|
122
|
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);
|
765
|
659 exit (EXIT_FAILURE);
|
122
|
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);
|
765
|
672 exit (EXIT_FAILURE);
|
122
|
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);
|
854
|
707 fwrite ("\n", 1, 1, fd);
|
122
|
708 if (*endpos == '\0')
|
|
709 {
|
|
710 pos = endpos;
|
|
711 break;
|
|
712 }
|
854
|
713 pos = endpos + 1;
|
|
714 fwrite ("# ", 1, 2, fd);
|
122
|
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
|
885
|
732 bmhdr = N_("Bookmarks file for gFTP. Copyright (C) 1998-2007 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);
|
765
|
738 exit (EXIT_FAILURE);
|
122
|
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));
|
765
|
745 exit (EXIT_FAILURE);
|
122
|
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
|
607
|
767 if (tempentry->isfolder)
|
|
768 {
|
|
769 fprintf (bmfile, "[%s/]\n", tempstr);
|
|
770 }
|
330
|
771 else
|
607
|
772 {
|
|
773 if (tempentry->save_password && tempentry->pass != NULL)
|
|
774 password = gftp_scramble_password (tempentry->pass);
|
|
775 else
|
|
776 password = NULL;
|
330
|
777
|
607
|
778 fprintf (bmfile,
|
765
|
779 "[%s]\nhostname=%s\nport=%u\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nusername=%s\npassword=%s\naccount=%s\n",
|
607
|
780 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname,
|
|
781 tempentry->port, tempentry->protocol == NULL
|
|
782 || *tempentry->protocol ==
|
|
783 '\0' ? gftp_protocols[0].name : tempentry->protocol,
|
|
784 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir,
|
|
785 tempentry->local_dir == NULL ? "" : tempentry->local_dir,
|
|
786 tempentry->user == NULL ? "" : tempentry->user,
|
|
787 password == NULL ? "" : password,
|
|
788 tempentry->acct == NULL ? "" : tempentry->acct);
|
|
789 if (password != NULL)
|
|
790 g_free(password);
|
122
|
791
|
607
|
792 if (tempentry->local_options_vars != NULL)
|
199
|
793 {
|
607
|
794 for (i=0; i<tempentry->num_local_options_vars; i++)
|
|
795 {
|
|
796 gftp_option_types[tempentry->local_options_vars[i].otype].write_function (&tempentry->local_options_vars[i], buf, sizeof (buf), 1);
|
|
797
|
|
798 fprintf (bmfile, "%s=%s\n", tempentry->local_options_vars[i].key,
|
|
799 buf);
|
|
800 }
|
199
|
801 }
|
|
802 }
|
122
|
803
|
|
804 fprintf (bmfile, "\n");
|
|
805
|
|
806 if (tempentry->next == NULL)
|
|
807 {
|
|
808 tempentry = tempentry->prev;
|
|
809 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
810 tempentry = tempentry->prev;
|
|
811 tempentry = tempentry->next;
|
|
812 }
|
|
813 else
|
|
814 tempentry = tempentry->next;
|
|
815 }
|
|
816
|
|
817 fclose (bmfile);
|
|
818 }
|
|
819
|
|
820
|
|
821 void
|
|
822 gftp_write_config_file (void)
|
|
823 {
|
387
|
824 char *tempstr, buf[256];
|
122
|
825 gftp_config_vars * cv;
|
|
826 GList *templist;
|
|
827 FILE *conffile;
|
|
828 int i;
|
|
829
|
555
|
830 if ((tempstr = gftp_expand_path (NULL, CONFIG_FILE)) == NULL)
|
122
|
831 {
|
|
832 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
765
|
833 exit (EXIT_FAILURE);
|
122
|
834 }
|
|
835
|
|
836 if ((conffile = fopen (tempstr, "w+")) == NULL)
|
|
837 {
|
|
838 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
839 g_strerror (errno));
|
765
|
840 exit (EXIT_FAILURE);
|
122
|
841 }
|
|
842
|
|
843 g_free (tempstr);
|
|
844
|
885
|
845 write_comment (conffile, _("Config file for gFTP. Copyright (C) 1998-2007 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"));
|
122
|
846
|
|
847 for (templist = gftp_options_list;
|
|
848 templist != NULL;
|
|
849 templist = templist->next)
|
|
850 {
|
|
851 cv = templist->data;
|
|
852
|
|
853 for (i=0; cv[i].key != NULL; i++)
|
|
854 {
|
|
855 if (gftp_option_types[cv[i].otype].write_function == NULL ||
|
|
856 *cv[i].key == '\0')
|
|
857 continue;
|
|
858
|
|
859 fprintf (conffile, "\n");
|
|
860 if (cv[i].comment != NULL)
|
|
861 write_comment (conffile, _(cv[i].comment));
|
|
862
|
387
|
863 gftp_option_types[cv[i].otype].write_function (&cv[i], buf,
|
|
864 sizeof (buf), 1);
|
|
865 fprintf (conffile, "%s=%s\n", cv[i].key, buf);
|
122
|
866 }
|
|
867 }
|
|
868
|
218
|
869 for (i=0; gftp_config_list[i].key != NULL; i++)
|
122
|
870 {
|
218
|
871 if (gftp_config_list[i].header == NULL &&
|
|
872 gftp_config_list[i].list == NULL)
|
|
873 continue;
|
|
874
|
122
|
875 fprintf (conffile, "\n");
|
|
876 if (gftp_config_list[i].header != NULL)
|
|
877 write_comment (conffile, _(gftp_config_list[i].header));
|
|
878
|
218
|
879 for (templist = gftp_config_list[i].list;
|
122
|
880 templist != NULL;
|
|
881 templist = templist->next)
|
|
882 {
|
|
883 fprintf (conffile, "%s=", gftp_config_list[i].key);
|
|
884 gftp_config_list[i].write_func (conffile, templist->data);
|
|
885 fprintf (conffile, "\n");
|
|
886 }
|
|
887 }
|
|
888
|
|
889 fclose (conffile);
|
|
890 }
|
|
891
|
|
892
|
1
|
893 GHashTable *
|
122
|
894 build_bookmarks_hash_table (gftp_bookmarks_var * entry)
|
1
|
895 {
|
122
|
896 gftp_bookmarks_var * tempentry;
|
1
|
897 GHashTable * htable;
|
|
898
|
|
899 htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
900 tempentry = entry;
|
|
901 while (tempentry != NULL)
|
|
902 {
|
|
903 g_hash_table_insert (htable, tempentry->path, tempentry);
|
|
904 if (tempentry->children != NULL)
|
|
905 {
|
|
906 tempentry = tempentry->children;
|
|
907 continue;
|
|
908 }
|
|
909 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
910 tempentry = tempentry->prev;
|
|
911 tempentry = tempentry->next;
|
|
912 }
|
|
913 return (htable);
|
|
914 }
|
|
915
|
|
916
|
|
917 void
|
122
|
918 print_bookmarks (gftp_bookmarks_var * bookmarks)
|
1
|
919 {
|
122
|
920 gftp_bookmarks_var * tempentry;
|
1
|
921
|
|
922 tempentry = bookmarks->children;
|
|
923 while (tempentry != NULL)
|
|
924 {
|
|
925 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL);
|
|
926 if (tempentry->children != NULL)
|
|
927 {
|
|
928 tempentry = tempentry->children;
|
|
929 continue;
|
|
930 }
|
|
931
|
|
932 if (tempentry->next == NULL)
|
|
933 {
|
|
934 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
935 tempentry = tempentry->prev;
|
|
936 tempentry = tempentry->next;
|
|
937 }
|
|
938 else
|
|
939 tempentry = tempentry->next;
|
|
940 }
|
|
941 }
|
|
942
|
122
|
943
|
|
944 static int
|
|
945 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line)
|
|
946 {
|
136
|
947 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
948 g_free (cv->value);
|
|
949
|
122
|
950 if (str != NULL)
|
|
951 {
|
|
952 cv->value = g_strdup (str);
|
136
|
953 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
954 return (0);
|
|
955 }
|
|
956 else
|
136
|
957 {
|
|
958 cv->value = NULL;
|
|
959 cv->flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
960 return (-1);
|
|
961 }
|
122
|
962 }
|
|
963
|
|
964
|
|
965 static int
|
387
|
966 gftp_config_file_write_text (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
967 int to_config_file)
|
122
|
968 {
|
|
969 char *outstr;
|
|
970
|
|
971 if (cv->value != NULL)
|
|
972 {
|
|
973 outstr = cv->value;
|
387
|
974 g_snprintf (buf, buflen, "%s", outstr);
|
122
|
975 return (0);
|
|
976 }
|
|
977 else
|
|
978 return (-1);
|
|
979 }
|
|
980
|
|
981
|
|
982 static int
|
387
|
983 gftp_config_file_write_hidetext (gftp_config_vars * cv, char *buf,
|
|
984 size_t buflen, int to_config_file)
|
122
|
985 {
|
|
986 char *outstr;
|
|
987
|
|
988 if (cv->value != NULL)
|
|
989 {
|
|
990 outstr = cv->value;
|
|
991 if (*outstr != '\0')
|
|
992 {
|
|
993 if (to_config_file)
|
387
|
994 g_snprintf (buf, buflen, "%s", outstr);
|
122
|
995 else
|
387
|
996 g_snprintf (buf, buflen, "*****");
|
122
|
997 }
|
|
998 return (0);
|
|
999 }
|
|
1000 else
|
|
1001 return (-1);
|
|
1002 }
|
|
1003
|
|
1004
|
207
|
1005 static void
|
|
1006 gftp_config_file_copy_text (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1007 {
|
|
1008 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1009 g_free (dest_cv->value);
|
|
1010
|
|
1011 if (cv->value != NULL)
|
|
1012 {
|
|
1013 dest_cv->value = g_strdup ((char *) cv->value);
|
|
1014 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1015 }
|
|
1016 else
|
|
1017 dest_cv->value = NULL;
|
|
1018 }
|
|
1019
|
|
1020
|
227
|
1021 static int
|
|
1022 gftp_config_file_compare_text (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1023 {
|
|
1024 char *str1, *str2;
|
|
1025
|
|
1026 str1 = cv1->value;
|
|
1027 str2 = cv2->value;
|
|
1028
|
|
1029 if (cv1->value == NULL && cv2->value == NULL)
|
|
1030 return (0);
|
|
1031
|
|
1032 if ((cv1->value == NULL && cv2->value != NULL) ||
|
|
1033 (cv1->value != NULL && cv2->value == NULL))
|
|
1034 return (-1);
|
|
1035
|
|
1036 return (strcmp (str1, str2));
|
|
1037 }
|
|
1038
|
|
1039
|
207
|
1040 static void
|
|
1041 gftp_config_file_copy_ptr_contents (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1042 {
|
|
1043 memcpy (&dest_cv->value, &cv->value, sizeof (dest_cv->value));
|
|
1044 }
|
|
1045
|
122
|
1046 static int
|
|
1047 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line)
|
|
1048 {
|
|
1049 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10));
|
|
1050 return (0);
|
|
1051 }
|
|
1052
|
|
1053
|
|
1054 static int
|
387
|
1055 gftp_config_file_write_int (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
1056 int to_config_file)
|
122
|
1057 {
|
387
|
1058 g_snprintf (buf, buflen, "%d", GPOINTER_TO_INT (cv->value));
|
122
|
1059 return (0);
|
|
1060 }
|
|
1061
|
|
1062
|
|
1063 static int
|
227
|
1064 gftp_config_file_compare_int (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1065 {
|
|
1066 return (GPOINTER_TO_INT(cv1->value) == GPOINTER_TO_INT(cv2->value) ? 0 : -1);
|
|
1067 }
|
|
1068
|
|
1069
|
|
1070 static int
|
122
|
1071 gftp_config_file_read_checkbox (char *str, gftp_config_vars * cv, int line)
|
|
1072 {
|
|
1073 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10) ? 1 : 0);
|
|
1074 return (0);
|
|
1075 }
|
|
1076
|
|
1077
|
|
1078 static int
|
|
1079 gftp_config_file_read_float (char *str, gftp_config_vars * cv, int line)
|
|
1080 {
|
325
|
1081 union { intptr_t i; float f; } r;
|
131
|
1082
|
325
|
1083 r.f = strtod (str, NULL);
|
|
1084 memcpy (&cv->value, &r.i, sizeof (cv->value));
|
122
|
1085 return (0);
|
|
1086 }
|
|
1087
|
|
1088
|
|
1089 static int
|
387
|
1090 gftp_config_file_write_float (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
1091 int to_config_file)
|
122
|
1092 {
|
131
|
1093 float f;
|
|
1094
|
|
1095 memcpy (&f, &cv->value, sizeof (f));
|
387
|
1096 g_snprintf (buf, buflen, "%.2f", f);
|
122
|
1097 return (0);
|
|
1098 }
|
|
1099
|
|
1100
|
|
1101 static int
|
227
|
1102 gftp_config_file_compare_float (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1103 {
|
|
1104 float f1, f2;
|
|
1105
|
|
1106 memcpy (&f1, &cv1->value, sizeof (f1));
|
|
1107 memcpy (&f2, &cv2->value, sizeof (f2));
|
|
1108 return (f1 == f2 ? 0 : -1);
|
|
1109 }
|
|
1110
|
|
1111
|
|
1112 static int
|
122
|
1113 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line)
|
|
1114 {
|
|
1115 char *red, *green, *blue;
|
|
1116 gftp_color * color;
|
|
1117
|
136
|
1118 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
1119 g_free (cv->value);
|
|
1120
|
198
|
1121 gftp_config_parse_args (str, 3, line, &red, &green, &blue);
|
122
|
1122
|
942
|
1123 color = g_malloc0 (sizeof (*color));
|
122
|
1124 color->red = strtol (red, NULL, 16);
|
|
1125 color->green = strtol (green, NULL, 16);
|
|
1126 color->blue = strtol (blue, NULL, 16);
|
|
1127 g_free (red);
|
|
1128 g_free (green);
|
|
1129 g_free (blue);
|
|
1130
|
|
1131 cv->value = color;
|
136
|
1132 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
1133
|
|
1134 return (0);
|
|
1135 }
|
|
1136
|
|
1137
|
|
1138 static int
|
387
|
1139 gftp_config_file_write_color (gftp_config_vars * cv, char *buf, size_t buflen,
|
|
1140 int to_config_file)
|
122
|
1141 {
|
|
1142 gftp_color * color;
|
|
1143
|
|
1144 color = cv->value;
|
387
|
1145 g_snprintf (buf, buflen, "%x:%x:%x", color->red, color->green, color->blue);
|
122
|
1146 return (0);
|
|
1147 }
|
|
1148
|
|
1149
|
207
|
1150 static void
|
|
1151 gftp_config_file_copy_color (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1152 {
|
|
1153 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1154 g_free (dest_cv->value);
|
|
1155
|
942
|
1156 dest_cv->value = g_malloc0 (sizeof (gftp_color));
|
207
|
1157 memcpy (dest_cv->value, cv->value, sizeof (gftp_color));
|
|
1158 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1159 }
|
|
1160
|
|
1161
|
122
|
1162 static int
|
227
|
1163 gftp_config_file_compare_color (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1164 {
|
|
1165 gftp_color * color1, * color2;
|
|
1166
|
|
1167 color1 = cv1->value;
|
|
1168 color2 = cv2->value;
|
|
1169
|
|
1170 return (color1->red == color2->red && color1->green == color2->green &&
|
|
1171 color1->blue == color2->blue ? 0 : -1);
|
|
1172 }
|
|
1173
|
|
1174
|
|
1175 static int
|
122
|
1176 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line)
|
|
1177 {
|
|
1178 char **clist;
|
|
1179 int i;
|
|
1180
|
|
1181 cv->value = 0;
|
|
1182 if (cv->listdata != NULL)
|
|
1183 {
|
|
1184 clist = cv->listdata;
|
|
1185 for (i=0; clist[i] != NULL; i++)
|
|
1186 {
|
125
|
1187 if (strcasecmp (clist[i], str) == 0)
|
122
|
1188 {
|
|
1189 cv->value = GINT_TO_POINTER(i);
|
|
1190 break;
|
|
1191 }
|
|
1192 }
|
|
1193 }
|
|
1194
|
|
1195 return (0);
|
|
1196 }
|
|
1197
|
|
1198
|
|
1199 static int
|
387
|
1200 gftp_config_file_write_intcombo (gftp_config_vars * cv, char *buf,
|
|
1201 size_t buflen, int to_config_file)
|
122
|
1202 {
|
|
1203 char **clist;
|
|
1204
|
|
1205 clist = cv->listdata;
|
|
1206 if (clist != NULL)
|
387
|
1207 g_snprintf (buf, buflen, "%s", clist[GPOINTER_TO_INT(cv->value)]);
|
122
|
1208 else
|
387
|
1209 g_snprintf (buf, buflen, _("<unknown>"));
|
122
|
1210
|
|
1211 return (0);
|
|
1212 }
|
|
1213
|
|
1214
|
125
|
1215 static int
|
|
1216 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line)
|
|
1217 {
|
|
1218 char **clist;
|
|
1219 int i;
|
|
1220
|
|
1221 cv->value = NULL;
|
|
1222 if (cv->listdata != NULL)
|
|
1223 {
|
|
1224 clist = cv->listdata;
|
|
1225 for (i=0; clist[i] != NULL; i++)
|
|
1226 {
|
|
1227 if (strcasecmp (clist[i], str) == 0)
|
|
1228 {
|
|
1229 cv->value = clist[i];
|
|
1230 break;
|
|
1231 }
|
|
1232 }
|
|
1233 }
|
|
1234
|
|
1235 return (0);
|
|
1236 }
|
|
1237
|
|
1238
|
|
1239 /* Note, the index numbers of this array must match up to the numbers in
|
122
|
1240 gftp_option_type_enum in gftp.h */
|
|
1241 gftp_option_type_var gftp_option_types[] = {
|
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_textcombo, gftp_config_file_write_text,
|
526
|
1246 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL,
|
|
1247 NULL},
|
227
|
1248 {gftp_config_file_read_text, gftp_config_file_write_text,
|
526
|
1249 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL,
|
|
1250 NULL},
|
227
|
1251 {gftp_config_file_read_text, gftp_config_file_write_hidetext,
|
526
|
1252 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL,
|
|
1253 NULL},
|
227
|
1254 {gftp_config_file_read_int, gftp_config_file_write_int,
|
|
1255 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
526
|
1256 NULL, NULL, NULL, NULL},
|
227
|
1257 {gftp_config_file_read_checkbox, gftp_config_file_write_int,
|
|
1258 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
526
|
1259 NULL, NULL, NULL, NULL},
|
227
|
1260 {gftp_config_file_read_intcombo, gftp_config_file_write_intcombo,
|
|
1261 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
526
|
1262 NULL, NULL, NULL, NULL},
|
227
|
1263 {gftp_config_file_read_float, gftp_config_file_write_float,
|
|
1264 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_float,
|
526
|
1265 NULL, NULL, NULL, NULL},
|
227
|
1266 {gftp_config_file_read_color, gftp_config_file_write_color,
|
|
1267 gftp_config_file_copy_color, gftp_config_file_compare_color,
|
526
|
1268 NULL, NULL, NULL, NULL},
|
|
1269 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
|
|
1270 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
|
122
|
1271 };
|
|
1272
|
|
1273
|
|
1274 void
|
374
|
1275 gftp_lookup_global_option (const char * key, void *value)
|
122
|
1276 {
|
|
1277 gftp_config_list_vars * tmplistvar;
|
|
1278 gftp_config_vars * tmpconfigvar;
|
|
1279
|
|
1280 if (gftp_global_options_htable != NULL &&
|
|
1281 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1282 key)) != NULL)
|
124
|
1283 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
122
|
1284 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
1285 key)) != NULL)
|
124
|
1286 *(gftp_config_list_vars **) value = tmplistvar;
|
122
|
1287 else
|
|
1288 {
|
|
1289 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
765
|
1290 exit (EXIT_FAILURE);
|
122
|
1291 }
|
|
1292 }
|
|
1293
|
|
1294
|
|
1295 void
|
374
|
1296 gftp_lookup_request_option (gftp_request * request, const char * key,
|
|
1297 void *value)
|
122
|
1298 {
|
143
|
1299 gftp_config_vars * tmpconfigvar;
|
|
1300
|
|
1301 if (request != NULL && request->local_options_hash != NULL &&
|
|
1302 (tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1303 key)) != NULL)
|
|
1304 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1305 else
|
|
1306 gftp_lookup_global_option (key, value);
|
122
|
1307 }
|
|
1308
|
|
1309
|
|
1310 void
|
374
|
1311 gftp_lookup_bookmark_option (gftp_bookmarks_var * bm, const char * key,
|
|
1312 void *value)
|
229
|
1313 {
|
|
1314 gftp_config_vars * tmpconfigvar;
|
|
1315
|
|
1316 if (bm != NULL && bm->local_options_hash != NULL &&
|
|
1317 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1318 key)) != NULL)
|
|
1319 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1320 else
|
|
1321 gftp_lookup_global_option (key, value);
|
|
1322 }
|
|
1323
|
|
1324
|
|
1325 void
|
374
|
1326 gftp_set_global_option (const char * key, const void *value)
|
122
|
1327 {
|
227
|
1328 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1329 void *nc_ptr;
|
|
1330 int ret;
|
129
|
1331
|
|
1332 if (gftp_global_options_htable != NULL &&
|
|
1333 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1334 key)) != NULL)
|
201
|
1335 {
|
227
|
1336 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1337
|
|
1338 /* Cheap warning fix for const pointer... */
|
|
1339 memcpy (&nc_ptr, &value, sizeof (nc_ptr));
|
|
1340 newconfigvar.value = nc_ptr;
|
|
1341 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
201
|
1342
|
516
|
1343 ret = gftp_option_types[tmpconfigvar->otype].compare_function (&newconfigvar, tmpconfigvar);
|
227
|
1344 if (ret != 0)
|
|
1345 {
|
516
|
1346 gftp_option_types[tmpconfigvar->otype].copy_function (&newconfigvar, tmpconfigvar);
|
227
|
1347 gftp_configuration_changed = 1;
|
|
1348 }
|
201
|
1349 }
|
129
|
1350 else
|
|
1351 {
|
|
1352 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
765
|
1353 exit (EXIT_FAILURE);
|
129
|
1354 }
|
122
|
1355 }
|
|
1356
|
|
1357
|
229
|
1358 static void
|
|
1359 _gftp_set_option_value (gftp_config_vars * cv, const void * newval)
|
|
1360 {
|
|
1361 gftp_config_vars newconfigvar;
|
|
1362 void *nc_ptr;
|
|
1363
|
|
1364 memcpy (&newconfigvar, cv, sizeof (newconfigvar));
|
|
1365
|
|
1366 /* Cheap warning fix for const pointer... */
|
|
1367 memcpy (&nc_ptr, &newval, sizeof (nc_ptr));
|
|
1368 newconfigvar.value = nc_ptr;
|
|
1369 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1370
|
516
|
1371 gftp_option_types[cv->otype].copy_function (&newconfigvar, cv);
|
229
|
1372 }
|
|
1373
|
|
1374
|
122
|
1375 void
|
374
|
1376 gftp_set_request_option (gftp_request * request, const char * key,
|
|
1377 const void *value)
|
122
|
1378 {
|
136
|
1379 gftp_config_vars * tmpconfigvar;
|
|
1380
|
|
1381 if (request->local_options_hash == NULL)
|
|
1382 request->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1383 string_hash_compare);
|
|
1384
|
|
1385 if ((tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1386 key)) != NULL)
|
229
|
1387 _gftp_set_option_value (tmpconfigvar, value);
|
136
|
1388 else
|
|
1389 {
|
143
|
1390 if (gftp_global_options_htable == NULL ||
|
136
|
1391 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1392 key)) == NULL)
|
|
1393 {
|
|
1394 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
765
|
1395 exit (EXIT_FAILURE);
|
136
|
1396 }
|
|
1397
|
|
1398 request->num_local_options_vars++;
|
|
1399 request->local_options_vars = g_realloc (request->local_options_vars,
|
765
|
1400 (gulong) sizeof (gftp_config_vars) * request->num_local_options_vars);
|
136
|
1401
|
229
|
1402 memcpy (&request->local_options_vars[request->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1403 _gftp_set_option_value (&request->local_options_vars[request->num_local_options_vars - 1], value);
|
143
|
1404
|
|
1405 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
|
1406 }
|
122
|
1407 }
|
|
1408
|
|
1409
|
|
1410 void
|
374
|
1411 gftp_set_bookmark_option (gftp_bookmarks_var * bm, const char * key,
|
|
1412 const void *value)
|
229
|
1413 {
|
|
1414 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1415 int ret;
|
|
1416
|
|
1417 if (bm->local_options_hash != NULL &&
|
|
1418 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1419 key)) != NULL)
|
|
1420 _gftp_set_option_value (tmpconfigvar, value);
|
|
1421 else
|
|
1422 {
|
|
1423 if (gftp_global_options_htable == NULL ||
|
|
1424 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1425 key)) == NULL)
|
|
1426 {
|
|
1427 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
765
|
1428 exit (EXIT_FAILURE);
|
229
|
1429 }
|
|
1430
|
|
1431 /* Check to see if this is set to the same value as the global option.
|
|
1432 If so, don't add it to the bookmark preferences */
|
|
1433 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1434 memcpy (&newconfigvar.value, &value, sizeof (newconfigvar.value));
|
|
1435 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1436
|
516
|
1437 ret = gftp_option_types[tmpconfigvar->otype].compare_function (&newconfigvar, tmpconfigvar);
|
229
|
1438 if (ret == 0)
|
|
1439 return;
|
|
1440
|
|
1441 if (bm->local_options_hash == NULL)
|
|
1442 bm->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1443 string_hash_compare);
|
|
1444
|
|
1445 bm->num_local_options_vars++;
|
|
1446 bm->local_options_vars = g_realloc (bm->local_options_vars,
|
765
|
1447 (gulong) sizeof (gftp_config_vars) * bm->num_local_options_vars);
|
229
|
1448
|
|
1449 memcpy (&bm->local_options_vars[bm->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1450 _gftp_set_option_value (&bm->local_options_vars[bm->num_local_options_vars - 1], value);
|
|
1451
|
|
1452 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]);
|
|
1453 }
|
|
1454 }
|
|
1455
|
|
1456
|
|
1457 void
|
122
|
1458 gftp_register_config_vars (gftp_config_vars * config_vars)
|
|
1459 {
|
|
1460 gftp_options_list = g_list_append (gftp_options_list, config_vars);
|
|
1461 gftp_setup_global_options (config_vars);
|
|
1462 }
|
|
1463
|
199
|
1464
|
|
1465 void
|
|
1466 gftp_copy_local_options (gftp_config_vars ** new_options_vars,
|
|
1467 GHashTable ** new_options_hash,
|
429
|
1468 int *new_num_local_options_vars,
|
199
|
1469 gftp_config_vars * orig_options,
|
|
1470 int num_local_options_vars)
|
|
1471 {
|
|
1472 int i;
|
|
1473
|
429
|
1474 *new_num_local_options_vars = num_local_options_vars;
|
199
|
1475 if (orig_options == NULL || num_local_options_vars == 0)
|
|
1476 {
|
|
1477 *new_options_vars = NULL;
|
|
1478 *new_options_hash = NULL;
|
|
1479 return;
|
|
1480 }
|
|
1481
|
|
1482 *new_options_hash = g_hash_table_new (string_hash_function,
|
|
1483 string_hash_compare);
|
|
1484
|
942
|
1485 *new_options_vars = g_malloc0 ((gulong) sizeof (gftp_config_vars) * num_local_options_vars);
|
199
|
1486 memcpy (*new_options_vars, orig_options,
|
|
1487 sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1488
|
|
1489 for (i=0; i<num_local_options_vars; i++)
|
|
1490 {
|
|
1491 g_hash_table_insert (*new_options_hash, (*new_options_vars)[i].key,
|
|
1492 &(*new_options_vars)[i]);
|
207
|
1493
|
|
1494 (*new_options_vars)[i].value = NULL;
|
|
1495 (*new_options_vars)[i].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
199
|
1496
|
207
|
1497 gftp_option_types[(*new_options_vars)[i].otype].copy_function (&(orig_options)[i], &(*new_options_vars)[i]);
|
199
|
1498 }
|
|
1499 }
|
|
1500
|
201
|
1501
|
|
1502 void
|
|
1503 gftp_config_free_options (gftp_config_vars * options_vars,
|
|
1504 GHashTable * options_hash,
|
|
1505 int num_options_vars)
|
|
1506 {
|
|
1507 int i;
|
|
1508
|
|
1509 if (num_options_vars == 0)
|
|
1510 return;
|
|
1511
|
|
1512 if (num_options_vars > 0)
|
|
1513 {
|
|
1514 /* If num_options_vars is 0, then the options was allocated with malloc */
|
|
1515
|
|
1516 for (i=0; i<num_options_vars; i++)
|
|
1517 {
|
|
1518 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1519 options_vars[i].value != NULL)
|
|
1520 g_free (options_vars[i].value);
|
|
1521 }
|
|
1522
|
|
1523 g_free (options_vars);
|
|
1524 }
|
|
1525 else if (num_options_vars < 0)
|
|
1526 {
|
|
1527 /* Otherwise we are freeing the global options */
|
|
1528
|
|
1529 for (i=0; options_vars[i].key != NULL; i++)
|
|
1530 {
|
|
1531 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1532 options_vars[i].value != NULL)
|
|
1533 g_free (options_vars[i].value);
|
|
1534
|
|
1535 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNLISTMEM &&
|
|
1536 options_vars[i].listdata != NULL)
|
|
1537 g_free (options_vars[i].listdata);
|
|
1538 }
|
|
1539 }
|
|
1540
|
|
1541 if (options_hash != NULL)
|
|
1542 g_hash_table_destroy (options_hash);
|
|
1543 }
|
|
1544
|
|
1545
|
|
1546 void
|
|
1547 gftp_bookmarks_destroy (gftp_bookmarks_var * bookmarks)
|
|
1548 {
|
|
1549 gftp_bookmarks_var * tempentry, * delentry;
|
|
1550
|
|
1551 if (bookmarks == NULL)
|
|
1552 return;
|
|
1553
|
|
1554 tempentry = bookmarks;
|
|
1555 while (tempentry != NULL)
|
|
1556 {
|
|
1557 if (tempentry->children != NULL)
|
609
|
1558 tempentry = tempentry->children;
|
|
1559 else
|
201
|
1560 {
|
609
|
1561 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
1562 {
|
|
1563 delentry = tempentry;
|
|
1564 tempentry = tempentry->prev;
|
|
1565 gftp_free_bookmark (delentry, 1);
|
|
1566 }
|
|
1567
|
|
1568 delentry = tempentry;
|
|
1569 tempentry = tempentry->next;
|
|
1570 gftp_free_bookmark (delentry, 1);
|
201
|
1571 }
|
|
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
|