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