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
|
122
|
108 static void
|
124
|
109 gftp_read_bookmarks (char *global_data_path)
|
1
|
110 {
|
|
111 char *tempstr, *temp1str, buf[255], *curpos;
|
122
|
112 gftp_bookmarks_var * newentry;
|
1
|
113 FILE *bmfile;
|
|
114 size_t len;
|
|
115 int line;
|
|
116
|
|
117 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
118 {
|
|
119 printf (_("gFTP Error: Bad bookmarks file name %s\n"), BOOKMARKS_FILE);
|
87
|
120 exit (1);
|
1
|
121 }
|
|
122
|
|
123 if (access (tempstr, F_OK) == -1)
|
|
124 {
|
124
|
125 temp1str = g_strdup_printf ("%s/bookmarks", global_data_path);
|
1
|
126 if (access (temp1str, F_OK) == -1)
|
|
127 {
|
|
128 printf (_("Warning: Cannot find master bookmark file %s\n"),
|
|
129 temp1str);
|
|
130 g_free (temp1str);
|
|
131 return;
|
|
132 }
|
|
133 copyfile (temp1str, tempstr);
|
|
134 g_free (temp1str);
|
|
135 }
|
58
|
136
|
1
|
137 if ((bmfile = fopen (tempstr, "r")) == NULL)
|
|
138 {
|
|
139 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"), tempstr,
|
|
140 g_strerror (errno));
|
87
|
141 exit (1);
|
1
|
142 }
|
|
143 g_free (tempstr);
|
|
144
|
|
145 line = 0;
|
|
146 newentry = NULL;
|
|
147 while (fgets (buf, sizeof (buf), bmfile))
|
|
148 {
|
|
149 len = strlen (buf);
|
107
|
150 if (len > 0 && buf[len - 1] == '\n')
|
1
|
151 buf[--len] = '\0';
|
107
|
152 if (len > 0 && buf[len - 1] == '\r')
|
1
|
153 buf[--len] = '\0';
|
|
154 line++;
|
|
155
|
|
156 if (*buf == '[')
|
|
157 {
|
|
158 newentry = g_malloc0 (sizeof (*newentry));
|
|
159 for (; buf[len - 1] == ' ' || buf[len - 1] == ']'; buf[--len] = '\0');
|
124
|
160 newentry->path = g_strdup (buf + 1);
|
1
|
161 newentry->isfolder = 0;
|
122
|
162 gftp_add_bookmark (newentry);
|
1
|
163 }
|
|
164 else if (strncmp (buf, "hostname", 8) == 0 && newentry)
|
|
165 {
|
|
166 curpos = buf + 9;
|
|
167 if (newentry->hostname)
|
|
168 g_free (newentry->hostname);
|
124
|
169 newentry->hostname = g_strdup (curpos);
|
1
|
170 }
|
|
171 else if (strncmp (buf, "port", 4) == 0 && newentry)
|
|
172 newentry->port = strtol (buf + 5, NULL, 10);
|
|
173 else if (strncmp (buf, "protocol", 8) == 0 && newentry)
|
|
174 {
|
|
175 curpos = buf + 9;
|
|
176 if (newentry->protocol)
|
|
177 g_free (newentry->protocol);
|
124
|
178 newentry->protocol = g_strdup (curpos);
|
1
|
179 }
|
|
180 else if (strncmp (buf, "remote directory", 16) == 0 && newentry)
|
|
181 {
|
|
182 curpos = buf + 17;
|
|
183 if (newentry->remote_dir)
|
|
184 g_free (newentry->remote_dir);
|
124
|
185 newentry->remote_dir = g_strdup (curpos);
|
1
|
186 }
|
|
187 else if (strncmp (buf, "local directory", 15) == 0 && newentry)
|
|
188 {
|
|
189 curpos = buf + 16;
|
|
190 if (newentry->local_dir)
|
|
191 g_free (newentry->local_dir);
|
124
|
192 newentry->local_dir = g_strdup (curpos);
|
1
|
193 }
|
|
194 else if (strncmp (buf, "username", 8) == 0 && newentry)
|
|
195 {
|
|
196 curpos = buf + 9;
|
|
197 if (newentry->user)
|
|
198 g_free (newentry->user);
|
124
|
199 newentry->user = g_strdup (curpos);
|
1
|
200 }
|
|
201 else if (strncmp (buf, "password", 8) == 0 && newentry)
|
|
202 {
|
|
203 curpos = buf + 9;
|
|
204 if (newentry->pass)
|
|
205 g_free (newentry->pass);
|
124
|
206 newentry->pass = g_strdup (curpos);
|
1
|
207 newentry->save_password = *newentry->pass != '\0';
|
|
208 }
|
|
209 else if (strncmp (buf, "account", 7) == 0 && newentry)
|
|
210 {
|
|
211 curpos = buf + 8;
|
|
212 if (newentry->acct)
|
|
213 g_free (newentry->acct);
|
124
|
214 newentry->acct = g_strdup (curpos);
|
1
|
215 }
|
|
216 else if (strncmp (buf, "sftpserv_path", 13) == 0 && newentry)
|
|
217 {
|
|
218 curpos = buf + 14;
|
|
219 if (newentry->sftpserv_path)
|
|
220 g_free (newentry->sftpserv_path);
|
124
|
221 newentry->sftpserv_path = g_strdup (curpos);
|
1
|
222 }
|
|
223 else if (*buf != '#' && *buf != '\0')
|
|
224 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"),
|
|
225 line, buf);
|
|
226 }
|
|
227 }
|
|
228
|
|
229
|
|
230 static int
|
|
231 parse_args (char *str, int numargs, int lineno, char **first, ...)
|
|
232 {
|
|
233 char *curpos, *endpos, *pos, **dest, tempchar;
|
|
234 int ret, has_colon;
|
|
235 va_list argp;
|
|
236
|
|
237 ret = 1;
|
|
238 va_start (argp, first);
|
|
239 curpos = str;
|
|
240 dest = first;
|
|
241 *dest = NULL;
|
|
242 while (numargs > 0)
|
|
243 {
|
|
244 has_colon = 0;
|
|
245 if (numargs > 1)
|
|
246 {
|
|
247 if ((endpos = strchr (curpos, ':')) == NULL)
|
|
248 {
|
|
249 printf (_("gFTP Warning: Line %d doesn't have enough arguments\n"),
|
|
250 lineno);
|
|
251 ret = 0;
|
|
252 endpos = curpos + strlen (curpos);
|
|
253 }
|
|
254 else
|
|
255 {
|
|
256 /* Allow colons inside the fields. If you want a colon inside a
|
|
257 field, just put 2 colons in there */
|
|
258 while (endpos != NULL && *(endpos - 1) == '\\')
|
|
259 {
|
|
260 endpos = strchr (endpos + 1, ':');
|
|
261 has_colon = 1;
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265 else
|
|
266 endpos = curpos + strlen (curpos);
|
|
267
|
|
268 *dest = g_malloc (endpos - curpos + 1);
|
|
269 tempchar = *endpos;
|
|
270 *endpos = '\0';
|
|
271 strcpy (*dest, curpos);
|
|
272 *endpos = tempchar;
|
|
273 if (has_colon)
|
|
274 {
|
|
275 pos = *dest;
|
|
276 curpos = *dest;
|
|
277 while (*pos != '\0')
|
|
278 {
|
|
279 if (*pos != '\\' && *(pos + 1) != ':')
|
|
280 *curpos++ = *pos++;
|
|
281 else
|
|
282 pos++;
|
|
283 }
|
|
284 *curpos = '\0';
|
|
285 }
|
|
286 if (*endpos == '\0')
|
|
287 break;
|
|
288 curpos = endpos + 1;
|
|
289 if (numargs > 1)
|
|
290 {
|
|
291 dest = va_arg (argp, char **);
|
|
292 *dest = NULL;
|
|
293 }
|
|
294 numargs--;
|
|
295 }
|
|
296
|
|
297 while (numargs > 1)
|
|
298 {
|
|
299 dest = va_arg (argp, char **);
|
|
300 *dest = g_malloc (1);
|
|
301 **dest = '\0';
|
|
302 numargs--;
|
|
303 }
|
|
304 va_end (argp);
|
|
305 return (1);
|
|
306 }
|
|
307
|
|
308
|
122
|
309 static void *
|
|
310 gftp_config_read_str (char *buf, int line)
|
|
311 {
|
|
312 char *ret;
|
|
313
|
|
314 ret = g_strdup (buf);
|
|
315 return (ret);
|
|
316 }
|
|
317
|
|
318
|
|
319 static void
|
|
320 gftp_config_write_str (FILE *fd, void *data)
|
|
321 {
|
|
322 fprintf (fd, "%s", (char *) data);
|
|
323 }
|
|
324
|
|
325
|
|
326 static void *
|
|
327 gftp_config_read_proxy (char *buf, int line)
|
|
328 {
|
|
329 gftp_proxy_hosts * host;
|
|
330 unsigned int nums[4];
|
|
331 char *pos;
|
|
332
|
|
333 host = g_malloc0 (sizeof (*host));
|
|
334 if ((pos = strchr (buf, '/')) == NULL)
|
|
335 host->domain = g_strdup (buf);
|
|
336 else
|
|
337 {
|
|
338 *pos = '\0';
|
|
339 sscanf (buf, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], &nums[3]);
|
|
340 host->ipv4_network_address =
|
|
341 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
342
|
|
343 if (strchr (pos + 1, '.') == NULL)
|
|
344 host->ipv4_netmask = 0xffffffff << (32 - strtol (pos + 1, NULL, 10));
|
|
345 else
|
|
346 {
|
|
347 sscanf (pos + 1, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2],
|
|
348 &nums[3]);
|
|
349 host->ipv4_netmask =
|
|
350 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
351 }
|
|
352 }
|
|
353
|
|
354 return (host);
|
|
355 }
|
|
356
|
|
357
|
|
358 static void
|
|
359 gftp_config_write_proxy (FILE *fd, void *data)
|
|
360 {
|
|
361 gftp_proxy_hosts * host;
|
|
362
|
|
363 host = data;
|
|
364
|
|
365 if (host->domain)
|
|
366 fprintf (fd, "%s", host->domain);
|
|
367 else
|
|
368 fprintf (fd, "%d.%d.%d.%d/%d.%d.%d.%d",
|
|
369 host->ipv4_network_address >> 24 & 0xff,
|
|
370 host->ipv4_network_address >> 16 & 0xff,
|
|
371 host->ipv4_network_address >> 8 & 0xff,
|
|
372 host->ipv4_network_address & 0xff,
|
|
373 host->ipv4_netmask >> 24 & 0xff,
|
|
374 host->ipv4_netmask >> 16 & 0xff,
|
|
375 host->ipv4_netmask >> 8 & 0xff,
|
|
376 host->ipv4_netmask & 0xff);
|
|
377 }
|
|
378
|
|
379
|
|
380 static void *
|
|
381 gftp_config_read_ext (char *buf, int line)
|
|
382 {
|
|
383 gftp_file_extensions * tempext;
|
|
384 char *tempstr;
|
|
385
|
|
386 tempext = g_malloc (sizeof (*tempext));
|
|
387 parse_args (buf, 4, line, &tempext->ext, &tempext->filename,
|
|
388 &tempext->ascii_binary, &tempext->view_program);
|
|
389
|
|
390 if ((tempstr = get_xpm_path (tempext->filename, 1)) != NULL)
|
|
391 g_free (tempstr);
|
|
392
|
|
393 tempext->stlen = strlen (tempext->ext);
|
|
394
|
|
395 return (tempext);
|
|
396 }
|
|
397
|
|
398
|
|
399 static void
|
|
400 gftp_config_write_ext (FILE *fd, void *data)
|
|
401 {
|
|
402 gftp_file_extensions * tempext;
|
|
403
|
|
404 tempext = data;
|
|
405 fprintf (fd, "%s:%s:%c:%s", tempext->ext, tempext->filename,
|
|
406 *tempext->ascii_binary == '\0' ? ' ' : *tempext->ascii_binary,
|
|
407 tempext->view_program);
|
|
408 }
|
|
409
|
|
410
|
|
411 gftp_config_list_vars gftp_config_list[] = {
|
|
412 {"dont_use_proxy", gftp_config_read_proxy, gftp_config_write_proxy,
|
|
413 NULL, 0,
|
|
414 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")},
|
|
415 {"ext", gftp_config_read_ext, gftp_config_write_ext,
|
|
416 NULL, 0,
|
|
417 N_("ext=file extenstion:XPM file:Ascii or Binary (A or B):viewer program. Note: All arguments except the file extension are optional")},
|
|
418 {"localhistory", gftp_config_read_str, gftp_config_write_str,
|
|
419 NULL, 0, NULL},
|
|
420 {"remotehistory", gftp_config_read_str, gftp_config_write_str,
|
|
421 NULL, 0, NULL},
|
|
422 {"hosthistory", gftp_config_read_str, gftp_config_write_str,
|
|
423 NULL, 0, NULL},
|
|
424 {"porthistory", gftp_config_read_str, gftp_config_write_str,
|
|
425 NULL, 0, NULL},
|
|
426 {"userhistory", gftp_config_read_str, gftp_config_write_str,
|
|
427 NULL, 0, NULL},
|
|
428 {NULL, NULL, NULL,
|
|
429 NULL, 0, NULL}
|
|
430 };
|
|
431
|
|
432
|
|
433 static void
|
|
434 gftp_setup_global_options (gftp_config_vars * cvars)
|
|
435 {
|
|
436 int i;
|
|
437
|
|
438 for (i=0; cvars[i].key != NULL; i++)
|
|
439 {
|
|
440 if (cvars[i].otype == gftp_option_type_subtree)
|
|
441 gftp_setup_global_options (cvars[i].value);
|
|
442 else if (cvars[i].key != NULL && *cvars[i].key != '\0')
|
|
443 g_hash_table_insert (gftp_global_options_htable,
|
|
444 cvars[i].key, &cvars[i]);
|
|
445 }
|
|
446 }
|
|
447
|
|
448
|
|
449 void
|
124
|
450 gftp_read_config_file (char *global_data_path)
|
122
|
451 {
|
|
452 char *tempstr, *temp1str, *curpos, buf[255];
|
|
453 gftp_config_list_vars * tmplistvar;
|
|
454 gftp_config_vars * tmpconfigvar;
|
125
|
455 char **protocol_list;
|
122
|
456 FILE *conffile;
|
|
457 int line, i;
|
|
458 size_t len;
|
|
459
|
|
460 gftp_global_options_htable = g_hash_table_new (string_hash_function,
|
|
461 string_hash_compare);
|
|
462
|
|
463 gftp_register_config_vars (gftp_global_config_vars);
|
|
464
|
125
|
465 protocol_list = NULL;
|
122
|
466 for (i=0; gftp_protocols[i].register_options != NULL; i++)
|
|
467 {
|
125
|
468 if (gftp_protocols[i].shown)
|
|
469 {
|
|
470 protocol_list = g_realloc (protocol_list, sizeof (char *) * (i + 2));
|
|
471 protocol_list[i] = gftp_protocols[i].name;
|
|
472 protocol_list[i + 1] = NULL;
|
|
473 }
|
|
474
|
122
|
475 if (gftp_protocols[i].register_options != NULL)
|
|
476 gftp_protocols[i].register_options ();
|
|
477 }
|
|
478
|
125
|
479 if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
480 "default_protocol")) != NULL)
|
|
481 tmpconfigvar->listdata = protocol_list;
|
|
482
|
122
|
483 gftp_config_list_htable = g_hash_table_new (string_hash_function,
|
|
484 string_hash_compare);
|
|
485
|
|
486 for (i=0; gftp_config_list[i].key != NULL; i++)
|
|
487 {
|
|
488 g_hash_table_insert (gftp_config_list_htable, gftp_config_list[i].key,
|
|
489 &gftp_config_list[i]);
|
|
490 }
|
|
491
|
|
492 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
493 {
|
|
494 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
495 exit (1);
|
|
496 }
|
|
497
|
|
498 if (access (tempstr, F_OK) == -1)
|
|
499 {
|
|
500 temp1str = expand_path (BASE_CONF_DIR);
|
|
501 if (access (temp1str, F_OK) == -1)
|
|
502 {
|
|
503 if (mkdir (temp1str, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
|
|
504 {
|
|
505 printf (_("gFTP Error: Could not make directory %s: %s\n"),
|
|
506 temp1str, g_strerror (errno));
|
|
507 exit (1);
|
|
508 }
|
|
509 }
|
|
510 g_free (temp1str);
|
|
511
|
124
|
512 temp1str = g_strdup_printf ("%s/gftprc", global_data_path);
|
122
|
513 if (access (temp1str, F_OK) == -1)
|
|
514 {
|
|
515 printf (_("gFTP Error: Cannot find master config file %s\n"),
|
|
516 temp1str);
|
|
517 printf (_("Did you do a make install?\n"));
|
|
518 exit (1);
|
|
519 }
|
|
520 copyfile (temp1str, tempstr);
|
|
521 g_free (temp1str);
|
|
522 }
|
|
523
|
|
524 if ((conffile = fopen (tempstr, "r")) == NULL)
|
|
525 {
|
|
526 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
527 g_strerror (errno));
|
|
528 exit (1);
|
|
529 }
|
|
530 g_free (tempstr);
|
|
531
|
|
532 line = 0;
|
|
533 while (fgets (buf, sizeof (buf), conffile))
|
|
534 {
|
|
535 len = strlen (buf);
|
|
536 if (len > 0 && buf[len - 1] == '\n')
|
|
537 buf[--len] = '\0';
|
|
538 if (len > 0 && buf[len - 1] == '\r')
|
|
539 buf[--len] = '\0';
|
|
540 line++;
|
|
541
|
|
542 if (*buf == '#' || *buf == '\0')
|
|
543 continue;
|
|
544
|
|
545 if ((curpos = strchr (buf, '=')) == NULL)
|
|
546 continue;
|
|
547
|
|
548 *curpos = '\0';
|
|
549
|
|
550 if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
551 buf)) != NULL)
|
|
552 {
|
|
553 tmplistvar->list = g_list_append (tmplistvar->list,
|
|
554 tmplistvar->read_func (curpos + 1,
|
|
555 line));
|
|
556 tmplistvar->num_items++;
|
|
557 }
|
|
558 else if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
559 buf)) != NULL &&
|
|
560 gftp_option_types[tmpconfigvar->otype].read_function != NULL)
|
|
561 {
|
|
562 if (gftp_option_types[tmpconfigvar->otype].read_function (curpos + 1,
|
|
563 tmpconfigvar, line) != 0)
|
|
564 {
|
|
565 printf (_("Terminating due to parse errors at line %d in the config file\n"), line);
|
|
566 exit (1);
|
|
567 }
|
|
568 }
|
|
569 else
|
|
570 {
|
|
571 printf (_("gFTP Warning: Skipping line %d in config file: %s\n"),
|
|
572 line, buf);
|
|
573 }
|
|
574 }
|
|
575
|
|
576 if ((tempstr = expand_path (LOG_FILE)) == NULL)
|
|
577 {
|
|
578 printf (_("gFTP Error: Bad log file name %s\n"), LOG_FILE);
|
|
579 exit (1);
|
|
580 }
|
|
581
|
|
582 if ((gftp_logfd = fopen (tempstr, "w")) == NULL)
|
|
583 {
|
|
584 printf (_("gFTP Warning: Cannot open %s for writing: %s\n"),
|
|
585 tempstr, g_strerror (errno));
|
|
586 }
|
|
587 g_free (tempstr);
|
|
588
|
|
589 gftp_bookmarks = g_malloc0 (sizeof (*gftp_bookmarks));
|
|
590 gftp_bookmarks->isfolder = 1;
|
|
591 gftp_bookmarks->path = g_malloc0 (1);
|
|
592 gftp_bookmarks_htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
593
|
124
|
594 gftp_read_bookmarks (global_data_path);
|
122
|
595 }
|
|
596
|
|
597
|
|
598 static void
|
|
599 write_comment (FILE * fd, const char *comment)
|
|
600 {
|
|
601 const char *pos, *endpos;
|
|
602
|
|
603 fwrite ("# ", 1, 2, fd);
|
|
604 pos = comment;
|
|
605 while (strlen (pos) > 76)
|
|
606 {
|
|
607 for (endpos = pos + 76; *endpos != ' ' && endpos > pos; endpos--);
|
|
608 if (endpos == pos)
|
|
609 {
|
|
610 for (endpos = pos + 76; *endpos != ' ' && *endpos != '\0';
|
|
611 endpos++);
|
|
612 }
|
|
613 fwrite (pos, 1, endpos - pos, fd);
|
|
614 fwrite ("\n# ", 1, 3, fd);
|
|
615 if (*endpos == '\0')
|
|
616 {
|
|
617 pos = endpos;
|
|
618 break;
|
|
619 }
|
|
620 else
|
|
621 pos = endpos + 1;
|
|
622 }
|
|
623 if (strlen (pos) > 1)
|
|
624 {
|
|
625 fwrite (pos, 1, strlen (pos), fd);
|
|
626 fwrite ("\n", 1, 1, fd);
|
|
627 }
|
|
628 }
|
|
629
|
|
630
|
|
631 void
|
|
632 gftp_write_bookmarks_file (void)
|
|
633 {
|
|
634 gftp_bookmarks_var * tempentry;
|
|
635 char *bmhdr, *tempstr;
|
|
636 FILE * bmfile;
|
|
637
|
|
638 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");
|
|
639
|
|
640 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
641 {
|
|
642 printf (_("gFTP Error: Bad bookmarks file name %s\n"), CONFIG_FILE);
|
|
643 exit (1);
|
|
644 }
|
|
645
|
|
646 if ((bmfile = fopen (tempstr, "w+")) == NULL)
|
|
647 {
|
|
648 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"),
|
|
649 CONFIG_FILE, g_strerror (errno));
|
|
650 exit (1);
|
|
651 }
|
|
652
|
|
653 g_free (tempstr);
|
|
654
|
|
655 write_comment (bmfile, _(bmhdr));
|
|
656 fwrite ("\n", 1, 1, bmfile);
|
|
657
|
|
658 tempentry = gftp_bookmarks->children;
|
|
659 while (tempentry != NULL)
|
|
660 {
|
|
661 if (tempentry->children != NULL)
|
|
662 {
|
|
663 tempentry = tempentry->children;
|
|
664 continue;
|
|
665 }
|
|
666 tempstr = tempentry->path;
|
|
667 while (*tempstr == '/')
|
|
668 tempstr++;
|
|
669 fprintf (bmfile,
|
|
670 "[%s]\nhostname=%s\nport=%d\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nusername=%s\npassword=%s\naccount=%s\n",
|
|
671 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname,
|
|
672 tempentry->port, tempentry->protocol == NULL
|
|
673 || *tempentry->protocol ==
|
|
674 '\0' ? gftp_protocols[0].name : tempentry->protocol,
|
|
675 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir,
|
|
676 tempentry->local_dir == NULL ? "" : tempentry->local_dir,
|
|
677 tempentry->user == NULL ? "" : tempentry->user,
|
|
678 !tempentry->save_password
|
|
679 || tempentry->pass == NULL ? "" : tempentry->pass,
|
|
680 tempentry->acct == NULL ? "" : tempentry->acct);
|
|
681
|
|
682 if (tempentry->sftpserv_path)
|
|
683 fprintf (bmfile, "sftpserv_path=%s\n", tempentry->sftpserv_path);
|
|
684
|
|
685 fprintf (bmfile, "\n");
|
|
686
|
|
687 if (tempentry->next == NULL)
|
|
688 {
|
|
689 tempentry = tempentry->prev;
|
|
690 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
691 tempentry = tempentry->prev;
|
|
692 tempentry = tempentry->next;
|
|
693 }
|
|
694 else
|
|
695 tempentry = tempentry->next;
|
|
696 }
|
|
697
|
|
698 fclose (bmfile);
|
|
699 }
|
|
700
|
|
701
|
|
702 void
|
|
703 gftp_write_config_file (void)
|
|
704 {
|
|
705 gftp_config_vars * cv;
|
|
706 GList *templist;
|
|
707 FILE *conffile;
|
|
708 char *tempstr;
|
|
709 int i;
|
|
710
|
|
711 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
712 {
|
|
713 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
714 exit (1);
|
|
715 }
|
|
716
|
|
717 if ((conffile = fopen (tempstr, "w+")) == NULL)
|
|
718 {
|
|
719 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
720 g_strerror (errno));
|
|
721 exit (1);
|
|
722 }
|
|
723
|
|
724 g_free (tempstr);
|
|
725
|
|
726 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"));
|
|
727
|
|
728 for (templist = gftp_options_list;
|
|
729 templist != NULL;
|
|
730 templist = templist->next)
|
|
731 {
|
|
732 cv = templist->data;
|
|
733
|
|
734 for (i=0; cv[i].key != NULL; i++)
|
|
735 {
|
|
736 if (gftp_option_types[cv[i].otype].write_function == NULL ||
|
|
737 *cv[i].key == '\0')
|
|
738 continue;
|
|
739
|
|
740 fprintf (conffile, "\n");
|
|
741 if (cv[i].comment != NULL)
|
|
742 write_comment (conffile, _(cv[i].comment));
|
|
743
|
|
744 fprintf (conffile, "%s=", cv[i].key);
|
|
745 gftp_option_types[cv[i].otype].write_function (&cv[i], conffile, 1);
|
|
746 fprintf (conffile, "\n");
|
|
747 }
|
|
748 }
|
|
749
|
|
750 for (i=0; gftp_config_list[i].list != NULL; i++)
|
|
751 {
|
|
752 fprintf (conffile, "\n");
|
|
753 if (gftp_config_list[i].header != NULL)
|
|
754 write_comment (conffile, _(gftp_config_list[i].header));
|
|
755
|
|
756 for (templist = gftp_options_list;
|
|
757 templist != NULL;
|
|
758 templist = templist->next)
|
|
759 {
|
|
760 fprintf (conffile, "%s=", gftp_config_list[i].key);
|
|
761 gftp_config_list[i].write_func (conffile, templist->data);
|
|
762 fprintf (conffile, "\n");
|
|
763 }
|
|
764 }
|
|
765
|
|
766 fclose (conffile);
|
|
767 }
|
|
768
|
|
769
|
1
|
770 GHashTable *
|
122
|
771 build_bookmarks_hash_table (gftp_bookmarks_var * entry)
|
1
|
772 {
|
122
|
773 gftp_bookmarks_var * tempentry;
|
1
|
774 GHashTable * htable;
|
|
775
|
|
776 htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
777 tempentry = entry;
|
|
778 while (tempentry != NULL)
|
|
779 {
|
|
780 g_hash_table_insert (htable, tempentry->path, tempentry);
|
|
781 if (tempentry->children != NULL)
|
|
782 {
|
|
783 tempentry = tempentry->children;
|
|
784 continue;
|
|
785 }
|
|
786 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
787 tempentry = tempentry->prev;
|
|
788 tempentry = tempentry->next;
|
|
789 }
|
|
790 return (htable);
|
|
791 }
|
|
792
|
|
793
|
|
794 void
|
122
|
795 print_bookmarks (gftp_bookmarks_var * bookmarks)
|
1
|
796 {
|
122
|
797 gftp_bookmarks_var * tempentry;
|
1
|
798
|
|
799 tempentry = bookmarks->children;
|
|
800 while (tempentry != NULL)
|
|
801 {
|
|
802 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL);
|
|
803 if (tempentry->children != NULL)
|
|
804 {
|
|
805 tempentry = tempentry->children;
|
|
806 continue;
|
|
807 }
|
|
808
|
|
809 if (tempentry->next == NULL)
|
|
810 {
|
|
811 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
812 tempentry = tempentry->prev;
|
|
813 tempentry = tempentry->next;
|
|
814 }
|
|
815 else
|
|
816 tempentry = tempentry->next;
|
|
817 }
|
|
818 }
|
|
819
|
122
|
820
|
|
821 static int
|
|
822 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line)
|
|
823 {
|
|
824 if (str != NULL)
|
|
825 {
|
|
826 cv->value = g_strdup (str);
|
|
827 return (0);
|
|
828 }
|
|
829 else
|
|
830 return (-1);
|
|
831 }
|
|
832
|
|
833
|
|
834 static int
|
|
835 gftp_config_file_write_text (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
836 {
|
|
837 char *outstr;
|
|
838
|
|
839 if (cv->value != NULL)
|
|
840 {
|
|
841 outstr = cv->value;
|
|
842 if (*outstr != '\0')
|
|
843 fprintf (fd, "%s", outstr);
|
|
844 return (0);
|
|
845 }
|
|
846 else
|
|
847 return (-1);
|
|
848 }
|
|
849
|
|
850
|
|
851 static int
|
|
852 gftp_config_file_write_hidetext (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
853 {
|
|
854 char *outstr;
|
|
855
|
|
856 if (cv->value != NULL)
|
|
857 {
|
|
858 outstr = cv->value;
|
|
859 if (*outstr != '\0')
|
|
860 {
|
|
861 if (to_config_file)
|
|
862 fprintf (fd, "%s", outstr);
|
|
863 else
|
|
864 fprintf (fd, "*****");
|
|
865 }
|
|
866 return (0);
|
|
867 }
|
|
868 else
|
|
869 return (-1);
|
|
870 }
|
|
871
|
|
872
|
|
873 static int
|
|
874 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line)
|
|
875 {
|
|
876 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10));
|
|
877 return (0);
|
|
878 }
|
|
879
|
|
880
|
|
881 static int
|
|
882 gftp_config_file_write_int (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
883 {
|
|
884 fprintf (fd, "%d", GPOINTER_TO_INT(cv->value));
|
|
885 return (0);
|
|
886 }
|
|
887
|
|
888
|
|
889 static int
|
|
890 gftp_config_file_read_checkbox (char *str, gftp_config_vars * cv, int line)
|
|
891 {
|
|
892 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10) ? 1 : 0);
|
|
893 return (0);
|
|
894 }
|
|
895
|
|
896
|
|
897 static int
|
|
898 gftp_config_file_read_float (char *str, gftp_config_vars * cv, int line)
|
|
899 {
|
|
900 *(float *) cv->value = strtof (str, NULL);
|
|
901 return (0);
|
|
902 }
|
|
903
|
|
904
|
|
905 static int
|
|
906 gftp_config_file_write_float (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
907 {
|
|
908 fprintf (fd, "%.2f", 0.0); /* FIXME */
|
|
909 return (0);
|
|
910 }
|
|
911
|
|
912
|
|
913 static int
|
|
914 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line)
|
|
915 {
|
|
916 char *red, *green, *blue;
|
|
917 gftp_color * color;
|
|
918
|
|
919 parse_args (str, 3, line, &red, &green, &blue);
|
|
920
|
|
921 color = g_malloc (sizeof (*color));
|
|
922 color->red = strtol (red, NULL, 16);
|
|
923 color->green = strtol (green, NULL, 16);
|
|
924 color->blue = strtol (blue, NULL, 16);
|
|
925 g_free (red);
|
|
926 g_free (green);
|
|
927 g_free (blue);
|
|
928
|
|
929 cv->value = color;
|
|
930
|
|
931 return (0);
|
|
932 }
|
|
933
|
|
934
|
|
935 static int
|
|
936 gftp_config_file_write_color (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
937 {
|
|
938 gftp_color * color;
|
|
939
|
|
940 color = cv->value;
|
|
941 fprintf (fd, "%x:%x:%x", color->red, color->green, color->blue);
|
|
942 return (0);
|
|
943 }
|
|
944
|
|
945
|
|
946 static int
|
|
947 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line)
|
|
948 {
|
|
949 char **clist;
|
|
950 int i;
|
|
951
|
|
952 cv->value = 0;
|
|
953 if (cv->listdata != NULL)
|
|
954 {
|
|
955 clist = cv->listdata;
|
|
956 for (i=0; clist[i] != NULL; i++)
|
|
957 {
|
125
|
958 if (strcasecmp (clist[i], str) == 0)
|
122
|
959 {
|
|
960 cv->value = GINT_TO_POINTER(i);
|
|
961 break;
|
|
962 }
|
|
963 }
|
|
964 }
|
|
965
|
|
966 return (0);
|
|
967 }
|
|
968
|
|
969
|
|
970 static int
|
|
971 gftp_config_file_write_intcombo (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
972 {
|
|
973 char **clist;
|
|
974
|
|
975 clist = cv->listdata;
|
|
976 if (clist != NULL)
|
125
|
977 fprintf (fd, "%s", clist[GPOINTER_TO_INT(cv->value)]);
|
122
|
978 else
|
|
979 fprintf (fd, _("<unknown>"));
|
|
980
|
|
981 return (0);
|
|
982 }
|
|
983
|
|
984
|
125
|
985 static int
|
|
986 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line)
|
|
987 {
|
|
988 char **clist;
|
|
989 int i;
|
|
990
|
|
991 cv->value = NULL;
|
|
992 if (cv->listdata != NULL)
|
|
993 {
|
|
994 clist = cv->listdata;
|
|
995 for (i=0; clist[i] != NULL; i++)
|
|
996 {
|
|
997 if (strcasecmp (clist[i], str) == 0)
|
|
998 {
|
|
999 cv->value = clist[i];
|
|
1000 break;
|
|
1001 }
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005 return (0);
|
|
1006 }
|
|
1007
|
|
1008
|
|
1009 /* Note, the index numbers of this array must match up to the numbers in
|
122
|
1010 gftp_option_type_enum in gftp.h */
|
|
1011 gftp_option_type_var gftp_option_types[] = {
|
124
|
1012 {gftp_config_file_read_text, gftp_config_file_write_text, NULL, NULL},
|
126
|
1013 {gftp_config_file_read_textcombo, gftp_config_file_write_text, NULL, NULL},
|
|
1014 {gftp_config_file_read_text, gftp_config_file_write_text, NULL, NULL},
|
|
1015 {gftp_config_file_read_text, gftp_config_file_write_hidetext, NULL, NULL},
|
124
|
1016 {gftp_config_file_read_int, gftp_config_file_write_int, NULL, NULL},
|
126
|
1017 {gftp_config_file_read_checkbox, gftp_config_file_write_int, NULL, NULL},
|
|
1018 {gftp_config_file_read_intcombo, gftp_config_file_write_intcombo, NULL, NULL},
|
124
|
1019 {gftp_config_file_read_float, gftp_config_file_write_float, NULL, NULL},
|
|
1020 {gftp_config_file_read_color, gftp_config_file_write_color, NULL, NULL},
|
|
1021 {NULL, NULL, NULL, NULL},
|
|
1022 {NULL, NULL, NULL, NULL},
|
|
1023 {NULL, NULL, NULL, NULL},
|
|
1024 {NULL, NULL, NULL, NULL},
|
|
1025 {NULL, NULL, NULL, NULL}
|
122
|
1026 };
|
|
1027
|
|
1028
|
|
1029 void
|
|
1030 gftp_lookup_global_option (char * key, void *value)
|
|
1031 {
|
|
1032 gftp_config_list_vars * tmplistvar;
|
|
1033 gftp_config_vars * tmpconfigvar;
|
|
1034
|
|
1035 if (gftp_global_options_htable != NULL &&
|
|
1036 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1037 key)) != NULL)
|
124
|
1038 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
122
|
1039 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
1040 key)) != NULL)
|
124
|
1041 *(gftp_config_list_vars **) value = tmplistvar;
|
122
|
1042 else
|
|
1043 {
|
|
1044 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1045 exit (1);
|
|
1046 }
|
|
1047 }
|
|
1048
|
|
1049
|
|
1050 void
|
|
1051 gftp_lookup_request_option (gftp_request * request, char * key, void *value)
|
|
1052 {
|
|
1053 gftp_lookup_global_option (key, value);
|
|
1054 }
|
|
1055
|
|
1056
|
|
1057 void
|
|
1058 gftp_set_global_option (char * key, void *value)
|
|
1059 {
|
|
1060 }
|
|
1061
|
|
1062
|
|
1063 void
|
|
1064 gftp_set_request_option (gftp_request * request, char * key, void *value)
|
|
1065 {
|
|
1066 }
|
|
1067
|
|
1068
|
|
1069 void
|
|
1070 gftp_register_config_vars (gftp_config_vars * config_vars)
|
|
1071 {
|
|
1072 gftp_options_list = g_list_append (gftp_options_list, config_vars);
|
|
1073 gftp_setup_global_options (config_vars);
|
|
1074 }
|
|
1075
|