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