-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcgiedit.c
4427 lines (3880 loc) · 132 KB
/
cgiedit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
*
* Purpose : CGI-based actionsfile editor.
*
* NOTE: The CGIs in this file use parameter names
* such as "f" and "s" which are really *BAD* choices.
* However, I'm trying to save bytes in the
* edit-actions-list HTML page - the standard actions
* file generated a 550kbyte page, which is ridiculous.
*
* Stick to the short names in this file for consistency.
*
* Copyright : Written by and Copyright (C) 2001-2014 the
* Privoxy team. http://www.privoxy.org/
*
* Based on the Internet Junkbuster originally written
* by and Copyright (C) 1997 Anonymous Coders and
* Junkbusters Corporation. http://www.junkbusters.com
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
**********************************************************************/
#include "config.h"
/*
* FIXME: Following includes copied from cgi.c - which are actually needed?
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include "project.h"
#include "cgi.h"
#include "cgiedit.h"
#include "cgisimple.h"
#include "list.h"
#include "encode.h"
#include "actions.h"
#include "miscutil.h"
#include "errlog.h"
#include "loaders.h"
#ifdef FEATURE_TOGGLE
/* loadcfg.h is for global_toggle_state only */
#include "loadcfg.h"
#endif /* def FEATURE_TOGGLE */
#include "urlmatch.h"
#ifdef FEATURE_CGI_EDIT_ACTIONS
/**
* A line in an editable_file.
*/
struct file_line
{
/** Next entry in the linked list */
struct file_line * next;
/** The raw data, to write out if this line is unmodified. */
char * raw;
/** Comments and/or whitespace to put before this line if it's modified
and then written out. */
char * prefix;
/** The actual data, as a string. Line continuation and comment removal
are performed on the data read from file before it's stored here, so
it will be a single line of data. */
char * unprocessed;
/** The type of data on this line. One of the FILE_LINE_xxx constants. */
int type;
/** The actual data, processed into some sensible data type. */
union
{
/** An action specification. */
struct action_spec action[1];
/** A name=value pair. */
struct
{
/** The name in the name=value pair. */
char * name;
/** The value in the name=value pair, as a string. */
char * svalue;
/** The value in the name=value pair, as an integer. */
int ivalue;
} setting;
} data;
};
/** This file_line has not been processed yet. */
#define FILE_LINE_UNPROCESSED 1
/** This file_line is blank. Can only appear at the end of a file, due to
the way the parser works. */
#define FILE_LINE_BLANK 2
/** This file_line says {{alias}}. */
#define FILE_LINE_ALIAS_HEADER 3
/** This file_line defines an alias. */
#define FILE_LINE_ALIAS_ENTRY 4
/** This file_line defines an {action}. */
#define FILE_LINE_ACTION 5
/** This file_line specifies a URL pattern. */
#define FILE_LINE_URL 6
/** This file_line says {{settings}}. */
#define FILE_LINE_SETTINGS_HEADER 7
/** This file_line is in a {{settings}} block. */
#define FILE_LINE_SETTINGS_ENTRY 8
/** This file_line says {{description}}. */
#define FILE_LINE_DESCRIPTION_HEADER 9
/** This file_line is in a {{description}} block. */
#define FILE_LINE_DESCRIPTION_ENTRY 10
/*
* Number of file modification time mismatches
* before the CGI editor gets turned off.
*/
#define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
/**
* A configuration file, in a format that can be edited and written back to
* disk.
*/
struct editable_file
{
struct file_line * lines; /**< The contents of the file. A linked list of lines. */
const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
const char * version_str; /**< Last modification time, as a string. For CGI param. */
/**< Can be used in URL without using url_param(). */
unsigned version; /**< Last modification time - prevents chaos with
the browser's "back" button. Note that this is a
time_t cast to an unsigned. When comparing, always
cast the time_t to an unsigned, and *NOT* vice-versa.
This may lose the top few bits, but they're not
significant anyway. */
int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
Note that changing this after the file has been
read in will cause a mess. */
struct file_line * parse_error; /**< On parse error, this is the offending line. */
const char * parse_error_text; /**< On parse error, this is the problem.
(Statically allocated) */
};
/**
* Information about the filter types.
* Used for macro replacement in cgi_edit_actions_for_url.
*/
struct filter_type_info
{
const int multi_action_index; /**< The multi action index as defined in project.h */
const char *macro_name; /**< Name of the macro that has to be replaced
with the prepared templates.
For example "content-filter-params" */
const char *type; /**< Name of the filter type,
for example "server-header-filter". */
/* XXX: check if these two can be combined. */
const char *disable_all_option; /**< Name of the catch-all radio option that has
to be checked or unchecked for this filter type. */
const char *disable_all_param; /**< Name of the parameter that causes all filters of
this type to be disabled. */
const char *abbr_type; /**< Abbreviation of the filter type, usually the
first or second character capitalized */
const char *anchor; /**< Anchor for the User Manual link,
for example "SERVER-HEADER-FILTER" */
};
/* Accessed by index, keep the order in the way the FT_ macros are defined. */
static const struct filter_type_info filter_type_info[] =
{
{
ACTION_MULTI_FILTER,
"content-filter-params", "filter",
"filter-all", "filter_all",
"F", "FILTER"
},
{
ACTION_MULTI_CLIENT_HEADER_FILTER,
"client-header-filter-params", "client-header-filter",
"client-header-filter-all", "client_header_filter_all",
"C", "CLIENT-HEADER-FILTER"
},
{
ACTION_MULTI_SERVER_HEADER_FILTER,
"server-header-filter-params", "server-header-filter",
"server-header-filter-all", "server_header_filter_all",
"S", "SERVER-HEADER-FILTER"
},
{
ACTION_MULTI_CLIENT_HEADER_TAGGER,
"client-header-tagger-params", "client-header-tagger",
"client-header-tagger-all", "client_header_tagger_all",
"L", "CLIENT-HEADER-TAGGER"
},
{
ACTION_MULTI_SERVER_HEADER_TAGGER,
"server-header-tagger-params", "server-header-tagger",
"server-header-tagger-all", "server_header_tagger_all",
"E", "SERVER-HEADER-TAGGER"
},
#ifdef FEATURE_EXTERNAL_FILTERS
{
ACTION_MULTI_EXTERNAL_FILTER,
"external-content-filter-params", "external-filter",
"external-content-filter-all", "external_content_filter_all",
"E", "EXTERNAL-CONTENT-FILTER"
},
#endif
};
/* FIXME: Following non-static functions should be prototyped in .h or made static */
/* Functions to read and write arbitrary config files */
jb_err edit_read_file(struct client_state *csp,
const struct map *parameters,
int require_version,
struct editable_file **pfile);
jb_err edit_write_file(struct editable_file * file);
void edit_free_file(struct editable_file * file);
/* Functions to read and write actions files */
jb_err edit_parse_actions_file(struct editable_file * file);
jb_err edit_read_actions_file(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters,
int require_version,
struct editable_file **pfile);
/* Error handlers */
jb_err cgi_error_modified(struct client_state *csp,
struct http_response *rsp,
const char *filename);
jb_err cgi_error_parse(struct client_state *csp,
struct http_response *rsp,
struct editable_file *file);
jb_err cgi_error_file(struct client_state *csp,
struct http_response *rsp,
const char *filename);
jb_err cgi_error_file_read_only(struct client_state *csp,
struct http_response *rsp,
const char *filename);
/* Internal arbitrary config file support functions */
static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
static void edit_free_file_lines(struct file_line * first_line);
/* Internal actions file support functions */
static int match_actions_file_header_line(const char * line, const char * name);
static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
/* Internal parameter parsing functions */
static jb_err get_url_spec_param(struct client_state *csp,
const struct map *parameters,
const char *name,
char **pvalue);
/* Internal actionsfile <==> HTML conversion functions */
static jb_err map_radio(struct map * exports,
const char * optionname,
const char * values,
int value);
static jb_err actions_to_radio(struct map * exports,
const struct action_spec *action);
static jb_err actions_from_radio(const struct map * parameters,
struct action_spec *action);
static jb_err map_copy_parameter_html(struct map *out,
const struct map *in,
const char *name);
static jb_err get_file_name_param(struct client_state *csp,
const struct map *parameters,
const char *param_name,
const char **pfilename);
/* Internal convenience functions */
static char *section_target(const unsigned sectionid);
/*********************************************************************
*
* Function : section_target
*
* Description : Given an unsigned (section id) n, produce a dynamically
* allocated string of the form #l<n>, for use in link
* targets.
*
* XXX: The hash should be moved into the templates
* to make this function more generic and render
* stringify() obsolete.
*
* Parameters :
* 1 : sectionid = start line number of section
*
* Returns : String with link target, or NULL if out of
* memory
*
*********************************************************************/
static char *section_target(const unsigned sectionid)
{
char buf[30];
snprintf(buf, sizeof(buf), "#l%u", sectionid);
return(strdup(buf));
}
/*********************************************************************
*
* Function : stringify
*
* Description : Convert a number into a dynamically allocated string.
*
* Parameters :
* 1 : number = The number to convert.
*
* Returns : String with link target, or NULL if out of memory
*
*********************************************************************/
static char *stringify(const unsigned number)
{
char buf[6];
snprintf(buf, sizeof(buf), "%u", number);
return strdup(buf);
}
/*********************************************************************
*
* Function : map_copy_parameter_html
*
* Description : Copy a CGI parameter from one map to another, HTML
* encoding it.
*
* Parameters :
* 1 : out = target map
* 2 : in = source map
* 3 : name = name of cgi parameter to copy
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory
* JB_ERR_CGI_PARAMS if the parameter doesn't exist
* in the source map
*
*********************************************************************/
static jb_err map_copy_parameter_html(struct map *out,
const struct map *in,
const char *name)
{
const char * value;
jb_err err;
assert(out);
assert(in);
assert(name);
value = lookup(in, name);
err = map(out, name, 1, html_encode(value), 0);
if (err)
{
/* Out of memory */
return err;
}
else if (*value == '\0')
{
return JB_ERR_CGI_PARAMS;
}
else
{
return JB_ERR_OK;
}
}
/*********************************************************************
*
* Function : cgi_edit_actions_url_form
*
* Description : CGI function that displays a form for
* edit-actions-url
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters
* i : (action index) Identifies the file to edit
* v : (version) File's last-modified time
* p : (pattern) Line number of pattern to edit
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory
* JB_ERR_CGI_PARAMS if the CGI parameters are not
* specified or not valid.
*
*********************************************************************/
jb_err cgi_edit_actions_url_form(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map * exports;
unsigned patternid;
struct editable_file * file;
struct file_line * cur_line;
unsigned line_number;
unsigned section_start_line_number = 0;
jb_err err;
assert(csp);
assert(rsp);
assert(parameters);
if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
{
return cgi_error_disabled(csp, rsp);
}
err = get_number_param(csp, parameters, "p", &patternid);
if (err)
{
return err;
}
err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
if (err)
{
/* No filename specified, can't read file, modified, or out of memory. */
return (err == JB_ERR_FILE ? JB_ERR_OK : err);
}
cur_line = file->lines;
for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
{
if (cur_line->type == FILE_LINE_ACTION)
{
section_start_line_number = line_number;
}
cur_line = cur_line->next;
}
if ( (cur_line == NULL)
|| (line_number != patternid)
|| (patternid < 1U)
|| (cur_line->type != FILE_LINE_URL))
{
/* Invalid "patternid" parameter */
edit_free_file(file);
return JB_ERR_CGI_PARAMS;
}
if (NULL == (exports = default_exports(csp, NULL)))
{
edit_free_file(file);
return JB_ERR_MEMORY;
}
err = map(exports, "f", 1, stringify(file->identifier), 0);
if (!err) err = map(exports, "v", 1, file->version_str, 1);
if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
edit_free_file(file);
if (err)
{
free_map(exports);
return err;
}
return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
}
/*********************************************************************
*
* Function : cgi_edit_actions_add_url_form
*
* Description : CGI function that displays a form for
* edit-actions-url
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters :
* f : (filename) Identifies the file to edit
* v : (version) File's last-modified time
* s : (section) Line number of section to edit
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory
* JB_ERR_CGI_PARAMS if the CGI parameters are not
* specified or not valid.
*
*********************************************************************/
jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map *exports;
jb_err err;
assert(csp);
assert(rsp);
assert(parameters);
if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
{
return cgi_error_disabled(csp, rsp);
}
if (NULL == (exports = default_exports(csp, NULL)))
{
return JB_ERR_MEMORY;
}
err = map_copy_parameter_html(exports, parameters, "f");
if (!err) err = map_copy_parameter_html(exports, parameters, "v");
if (!err) err = map_copy_parameter_html(exports, parameters, "s");
if (err)
{
free_map(exports);
return err;
}
return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
}
/*********************************************************************
*
* Function : cgi_edit_actions_remove_url_form
*
* Description : CGI function that displays a form for
* edit-actions-url
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters :
* f : (number) The action file identifier.
* v : (version) File's last-modified time
* p : (pattern) Line number of pattern to edit
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory
* JB_ERR_CGI_PARAMS if the CGI parameters are not
* specified or not valid.
*
*********************************************************************/
jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map * exports;
unsigned patternid;
struct editable_file * file;
struct file_line * cur_line;
unsigned line_number;
unsigned section_start_line_number = 0;
jb_err err;
assert(csp);
assert(rsp);
assert(parameters);
if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
{
return cgi_error_disabled(csp, rsp);
}
err = get_number_param(csp, parameters, "p", &patternid);
if (err)
{
return err;
}
err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
if (err)
{
/* No filename specified, can't read file, modified, or out of memory. */
return (err == JB_ERR_FILE ? JB_ERR_OK : err);
}
cur_line = file->lines;
for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
{
if (cur_line->type == FILE_LINE_ACTION)
{
section_start_line_number = line_number;
}
cur_line = cur_line->next;
}
if ( (cur_line == NULL)
|| (line_number != patternid)
|| (patternid < 1U)
|| (cur_line->type != FILE_LINE_URL))
{
/* Invalid "patternid" parameter */
edit_free_file(file);
return JB_ERR_CGI_PARAMS;
}
if (NULL == (exports = default_exports(csp, NULL)))
{
edit_free_file(file);
return JB_ERR_MEMORY;
}
err = map(exports, "f", 1, stringify(file->identifier), 0);
if (!err) err = map(exports, "v", 1, file->version_str, 1);
if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
edit_free_file(file);
if (err)
{
free_map(exports);
return err;
}
return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
}
/*********************************************************************
*
* Function : edit_write_file
*
* Description : Write a complete file to disk.
*
* Parameters :
* 1 : file = File to write.
*
* Returns : JB_ERR_OK on success
* JB_ERR_FILE on error writing to file.
* JB_ERR_MEMORY on out of memory
*
*********************************************************************/
jb_err edit_write_file(struct editable_file * file)
{
FILE * fp;
struct file_line * cur_line;
struct stat statbuf[1];
char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
digits in time_t, assuming this is a 64-bit
machine, plus null terminator, plus one
for paranoia */
assert(file);
assert(file->filename);
if (NULL == (fp = fopen(file->filename, "wb")))
{
return JB_ERR_FILE;
}
cur_line = file->lines;
while (cur_line != NULL)
{
if (cur_line->raw)
{
if (fputs(cur_line->raw, fp) < 0)
{
fclose(fp);
return JB_ERR_FILE;
}
}
else
{
if (cur_line->prefix)
{
if (fputs(cur_line->prefix, fp) < 0)
{
fclose(fp);
return JB_ERR_FILE;
}
}
if (cur_line->unprocessed)
{
if (NULL != strchr(cur_line->unprocessed, '#'))
{
/* Must quote '#' characters */
int numhash = 0;
size_t len;
char * src;
char * dest;
char * str;
/* Count number of # characters, so we know length of output string */
src = cur_line->unprocessed;
while (NULL != (src = strchr(src, '#')))
{
numhash++;
src++;
}
assert(numhash > 0);
/* Allocate new memory for string */
len = strlen(cur_line->unprocessed) + (size_t)numhash;
str = malloc_or_die(len + 1);
/* Copy string but quote hashes */
src = cur_line->unprocessed;
dest = str;
while (*src)
{
if (*src == '#')
{
*dest++ = '\\';
numhash--;
assert(numhash >= 0);
}
*dest++ = *src++;
}
*dest = '\0';
assert(numhash == 0);
assert(strlen(str) == len);
assert(str == dest - len);
assert(src - len <= cur_line->unprocessed);
if ((strlen(str) != len) || (numhash != 0))
{
/*
* Escaping didn't work as expected, go spread the news.
* Only reached in non-debugging builds.
*/
log_error(LOG_LEVEL_ERROR,
"Looks like hash escaping failed. %s might be corrupted now.",
file->filename);
}
if (fputs(str, fp) < 0)
{
free(str);
fclose(fp);
return JB_ERR_FILE;
}
free(str);
}
else
{
/* Can write without quoting '#' characters. */
if (fputs(cur_line->unprocessed, fp) < 0)
{
fclose(fp);
return JB_ERR_FILE;
}
}
if (fputs(NEWLINE(file->newline), fp) < 0)
{
fclose(fp);
return JB_ERR_FILE;
}
}
else
{
/* FIXME: Write data from file->data->whatever */
assert(0);
}
}
cur_line = cur_line->next;
}
fclose(fp);
/* Update the version stamp in the file structure, since we just
* wrote to the file & changed it's date.
*/
if (stat(file->filename, statbuf) < 0)
{
/* Error, probably file not found. */
return JB_ERR_FILE;
}
file->version = (unsigned)statbuf->st_mtime;
/* Correct file->version_str */
freez(file->version_str);
snprintf(version_buf, sizeof(version_buf), "%u", file->version);
version_buf[sizeof(version_buf)-1] = '\0';
file->version_str = strdup_or_die(version_buf);
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : edit_free_file
*
* Description : Free a complete file in memory.
*
* Parameters :
* 1 : file = Data structure to free.
*
* Returns : N/A
*
*********************************************************************/
void edit_free_file(struct editable_file * file)
{
if (!file)
{
/* Silently ignore NULL pointer */
return;
}
edit_free_file_lines(file->lines);
freez(file->version_str);
file->version = 0;
file->parse_error_text = NULL; /* Statically allocated */
file->parse_error = NULL;
free(file);
}
/*********************************************************************
*
* Function : edit_free_file_lines
*
* Description : Free an entire linked list of file lines.
*
* Parameters :
* 1 : first_line = Data structure to free.
*
* Returns : N/A
*
*********************************************************************/
static void edit_free_file_lines(struct file_line * first_line)
{
struct file_line * next_line;
while (first_line != NULL)
{
next_line = first_line->next;
first_line->next = NULL;
freez(first_line->raw);
freez(first_line->prefix);
freez(first_line->unprocessed);
switch(first_line->type)
{
case 0: /* special case if memory zeroed */
case FILE_LINE_UNPROCESSED:
case FILE_LINE_BLANK:
case FILE_LINE_ALIAS_HEADER:
case FILE_LINE_SETTINGS_HEADER:
case FILE_LINE_DESCRIPTION_HEADER:
case FILE_LINE_DESCRIPTION_ENTRY:
case FILE_LINE_ALIAS_ENTRY:
case FILE_LINE_URL:
/* No data is stored for these */
break;
case FILE_LINE_ACTION:
free_action(first_line->data.action);
break;
case FILE_LINE_SETTINGS_ENTRY:
freez(first_line->data.setting.name);
freez(first_line->data.setting.svalue);
break;
default:
/* Should never happen */
assert(0);
break;
}
first_line->type = 0; /* paranoia */
free(first_line);
first_line = next_line;
}
}
/*********************************************************************
*
* Function : match_actions_file_header_line
*
* Description : Match an actions file {{header}} line
*
* Parameters :
* 1 : line = String from file
* 2 : name = Header to match against
*
* Returns : 0 iff they match.
*
*********************************************************************/
static int match_actions_file_header_line(const char * line, const char * name)
{
size_t len;
assert(line);
assert(name);
/* Look for "{{" */
if ((line[0] != '{') || (line[1] != '{'))
{
return 1;
}
line += 2;
/* Look for optional whitespace */
while ((*line == ' ') || (*line == '\t'))
{
line++;
}
/* Look for the specified name (case-insensitive) */
len = strlen(name);
if (0 != strncmpic(line, name, len))
{
return 1;
}
line += len;
/* Look for optional whitespace */
while ((*line == ' ') || (*line == '\t'))
{
line++;
}
/* Look for "}}" and end of string*/
if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
{
return 1;
}
/* It matched!! */
return 0;
}
/*********************************************************************
*
* Function : match_actions_file_header_line
*
* Description : Match an actions file {{header}} line
*
* Parameters :