Line data Source code
1 : #include "burp.h"
2 : #include "alloc.h"
3 : #include "conf.h"
4 : #include "fsops.h"
5 : #include "handy.h"
6 : #include "lock.h"
7 : #include "log.h"
8 : #include "msg.h"
9 : #include "pathcmp.h"
10 : #include "prepend.h"
11 : #include "strlist.h"
12 : #include "times.h"
13 : #include "client/glob_windows.h"
14 : #include "conffile.h"
15 :
16 : // This will strip off everything after the last quote. So, configs like this
17 : // should work:
18 : // exclude_regex = "[A-Z]:/pagefile.sys" # swap file (Windows XP, 7, 8)
19 : // Return 1 for quotes removed, -1 for error, 0 for OK.
20 2552 : static int remove_quotes(const char *f, char **v, char quote)
21 : {
22 2552 : char *dp=NULL;
23 2552 : char *sp=NULL;
24 2552 : char *copy=NULL;
25 :
26 : // If it does not start with a quote, leave it alone.
27 2552 : if(**v!=quote) return 0;
28 :
29 4 : if(!(copy=strdup_w(*v, __func__)))
30 : return -1;
31 :
32 8 : for(dp=*v, sp=copy+1; *sp; sp++)
33 : {
34 6 : if(*sp==quote)
35 : {
36 : // Found a matching quote. Stop here.
37 2 : *dp='\0';
38 2 : for(sp++; *sp && isspace(*sp); sp++) { }
39 : // Do not complain about trailing comments.
40 2 : if(*sp && *sp!='#')
41 0 : logp("ignoring trailing characters after quote in config '%s = %s'\n", f, copy);
42 : return 1;
43 : }
44 4 : else if(*sp=='\\')
45 : {
46 0 : sp++;
47 0 : *dp=*sp;
48 0 : dp++;
49 0 : if(*sp!=quote
50 0 : && *sp!='\\')
51 0 : logp("unknown escape sequence '\\%c' in config '%s = %s' - treating it as '%c'\n", *sp, f, copy, *sp);
52 : }
53 : else
54 : {
55 4 : *dp=*sp;
56 4 : dp++;
57 : }
58 : }
59 2 : logp("Did not find closing quote in config '%s = %s'\n", f, copy);
60 2 : *dp='\0';
61 2 : return 1;
62 : }
63 :
64 : // Get field and value pair.
65 1292 : int conf_get_pair(char buf[], char **f, char **v)
66 : {
67 1292 : char *cp=NULL;
68 1292 : char *eq=NULL;
69 :
70 : // strip leading space
71 1292 : for(cp=buf; *cp && isspace(*cp); cp++) { }
72 1292 : if(!*cp || *cp=='#')
73 : {
74 14 : *f=NULL;
75 14 : *v=NULL;
76 14 : return 0;
77 : }
78 1278 : *f=cp;
79 1278 : if(!(eq=strchr(*f, '='))) return -1;
80 1277 : *eq='\0';
81 :
82 : // Strip white space from before the equals sign.
83 1277 : for(cp=eq-1; *cp && isspace(*cp); cp--) *cp='\0';
84 : // Skip white space after the equals sign.
85 1277 : for(cp=eq+1; *cp && isspace(*cp); cp++) { }
86 1277 : *v=cp;
87 : // Strip white space at the end of the line.
88 1277 : for(cp+=strlen(cp)-1; *cp && isspace(*cp); cp--) { *cp='\0'; }
89 :
90 : // FIX THIS: Make this more sophisticated - it should understand
91 : // escapes, for example.
92 :
93 1277 : switch(remove_quotes(*f, v, '\''))
94 : {
95 : case -1: return -1;
96 : case 1: break;
97 : default:
98 : // If single quotes were not removed, try to remove
99 : // double quotes.
100 1275 : if(remove_quotes(*f, v, '\"')<0) return -1;
101 : break;
102 : }
103 :
104 1277 : if(!*f || !**f || !*v || !**v) return -1;
105 :
106 1277 : return 0;
107 : }
108 :
109 : static int conf_error(const char *conf_path, int line)
110 : {
111 1 : logp("%s: parse error on line %d\n", conf_path, line);
112 : return -1;
113 : }
114 :
115 2 : static int get_file_size(const char *v, uint64_t *dest, const char *conf_path, int line)
116 : {
117 : // Store in bytes, allow k/m/g.
118 2 : const char *cp=NULL;
119 2 : *dest=strtoul(v, NULL, 10);
120 2 : for(cp=v; *cp && (isspace(*cp) || isdigit(*cp)); cp++) { }
121 2 : if(tolower(*cp)=='k') *dest*=1024;
122 2 : else if(tolower(*cp)=='m') *dest*=1024*1024;
123 2 : else if(tolower(*cp)=='g') *dest*=1024*1024*1024;
124 2 : else if(!*cp || *cp=='b')
125 : { }
126 : else
127 : {
128 0 : logp("Unknown file size type '%s' - please use b/kb/mb/gb\n",
129 : cp);
130 0 : return conf_error(conf_path, line);
131 : }
132 : return 0;
133 : }
134 :
135 450 : static int pre_post_override(struct conf *c,
136 : struct conf *pre, struct conf *post)
137 : {
138 450 : const char *override=get_string(c);
139 450 : if(!override) return 0;
140 5 : if(set_string(pre, override)
141 5 : || set_string(post, override))
142 : return -1;
143 : return 0;
144 : }
145 :
146 : #ifdef HAVE_LINUX_OS
147 : struct fstype
148 : {
149 : const char *str;
150 : uint64_t flag;
151 : };
152 :
153 : static struct fstype fstypes[]={
154 : { "debugfs", 0x64626720 },
155 : { "devfs", 0x00001373 },
156 : { "devpts", 0x00001CD1 },
157 : { "devtmpfs", 0x00009FA0 },
158 : { "ecryptfs", 0x0000f15f },
159 : { "ext2", 0x0000EF53 },
160 : { "ext3", 0x0000EF53 },
161 : { "ext4", 0x0000EF53 },
162 : { "iso9660", 0x00009660 },
163 : { "jfs", 0x3153464A },
164 : { "nfs", 0x00006969 },
165 : { "ntfs", 0x5346544E },
166 : { "proc", 0x00009fa0 },
167 : { "reiserfs", 0x52654973 },
168 : { "securityfs", 0x73636673 },
169 : { "sysfs", 0x62656572 },
170 : { "smbfs", 0x0000517B },
171 : { "usbdevfs", 0x00009fa2 },
172 : { "xfs", 0x58465342 },
173 : { "ramfs", 0x858458f6 },
174 : { "romfs", 0x00007275 },
175 : { "tmpfs", 0x01021994 },
176 : { "zfs", 0x2fc12fc1 },
177 : { NULL, 0 },
178 : };
179 : /* Use this C code to figure out what f_type gets set to.
180 : #include <stdio.h>
181 : #include <sys/vfs.h>
182 :
183 : int main(int argc, char *argv[])
184 : {
185 : int i=0;
186 : struct statfs buf;
187 : if(argc<1)
188 : {
189 : printf("not enough args\n");
190 : return -1;
191 : }
192 : if(statfs(argv[1], &buf))
193 : {
194 : printf("error\n");
195 : return -1;
196 : }
197 : printf("0x%08X\n", buf.f_type);
198 : return 0;
199 : }
200 : */
201 :
202 : #endif
203 :
204 0 : static int fstype_to_flag(const char *fstype, long *flag)
205 : {
206 : #ifdef HAVE_LINUX_OS
207 0 : int i=0;
208 0 : for(i=0; fstypes[i].str; i++)
209 : {
210 0 : if(!strcmp(fstypes[i].str, fstype))
211 : {
212 0 : *flag=fstypes[i].flag;
213 0 : return 0;
214 : }
215 : }
216 : #else
217 : return 0;
218 : #endif
219 : return -1;
220 : }
221 :
222 5 : static int get_compression(const char *v)
223 : {
224 5 : const char *cp=v;
225 5 : if(!strncmp(v, "gzip", strlen("gzip"))
226 5 : || !(strncmp(v, "zlib", strlen("zlib"))))
227 0 : cp=v+strlen("gzip"); // Or "zlib".
228 5 : if(strlen(cp)==1 && isdigit(*cp))
229 5 : return atoi(cp);
230 : return -1;
231 : }
232 :
233 1264 : static int load_conf_field_and_value(struct conf **c,
234 : const char *f, // field
235 : const char *v, // value
236 : const char *conf_path,
237 : int line)
238 : {
239 1264 : if(!strcmp(f, "compression"))
240 : {
241 5 : int compression=get_compression(v);
242 5 : if(compression<0) return -1;
243 5 : set_int(c[OPT_COMPRESSION], compression);
244 : }
245 1259 : else if(!strcmp(f, "ssl_compression"))
246 : {
247 0 : int compression=get_compression(v);
248 0 : if(compression<0) return -1;
249 0 : set_int(c[OPT_SSL_COMPRESSION], compression);
250 : }
251 1259 : else if(!strcmp(f, "ratelimit"))
252 : {
253 0 : float f=0;
254 0 : f=atof(v);
255 : // User is specifying Mega bits per second.
256 : // Need to convert to bytes per second.
257 0 : f=(f*1024*1024)/8;
258 0 : if(!f)
259 : {
260 0 : logp("ratelimit should be greater than zero\n");
261 0 : return -1;
262 : }
263 0 : set_float(c[OPT_RATELIMIT], f);
264 : }
265 : else
266 : {
267 : int i=0;
268 57086 : for(i=0; i<OPT_MAX; i++)
269 : {
270 58340 : if(strcmp(c[i]->field, f)) continue;
271 1254 : switch(c[i]->conf_type)
272 : {
273 : case CT_STRING:
274 714 : return set_string(c[i], v);
275 : case CT_UINT:
276 52 : return set_int(c[i], atoi(v));
277 : case CT_FLOAT:
278 0 : return set_float(c[i], atof(v));
279 : break;
280 : case CT_MODE_T:
281 0 : return set_mode_t(c[i],
282 0 : strtol(v, NULL, 8));
283 : case CT_SSIZE_T:
284 : {
285 2 : uint64_t s=0;
286 : return
287 2 : get_file_size(v, &s, conf_path, line)
288 2 : || set_uint64_t(c[i], s);
289 : }
290 : case CT_E_BURP_MODE:
291 95 : return set_e_burp_mode(c[i],
292 : str_to_burp_mode(v));
293 : case CT_E_PROTOCOL:
294 14 : return set_e_protocol(c[i],
295 : str_to_protocol(v));
296 : case CT_E_RECOVERY_METHOD:
297 1 : return set_e_recovery_method(c[i],
298 : str_to_recovery_method(v));
299 : case CT_STRLIST:
300 376 : return add_to_strlist(c[i], v,
301 376 : !strcmp(c[i]->field, "include"));
302 : case CT_E_RSHASH:
303 : break;
304 : case CT_CNTR:
305 : break;
306 : // No default so we get a warning if something
307 : // was missed;
308 : }
309 : }
310 : }
311 : return 0;
312 : }
313 :
314 : // Recursing, so need to define this ahead of conf_parse_line.
315 : static int conf_load_lines_from_file(const char *conf_path,
316 : struct conf **confs);
317 :
318 0 : static int deal_with_dot_inclusion(const char *conf_path,
319 : char **extrafile, struct conf **confs)
320 : {
321 0 : int ret=-1;
322 0 : char *copy=NULL;
323 : #ifndef HAVE_WIN32
324 0 : int i=0;
325 : glob_t globbuf;
326 0 : if(**extrafile!='/')
327 : #else
328 : if(strlen(*extrafile)>2
329 : && (*extrafile)[1]!=':')
330 : #endif
331 : {
332 : // It is relative to the directory that the
333 : // current conf file is in.
334 0 : char *cp=NULL;
335 0 : char *tmp=NULL;
336 0 : if(!(copy=strdup_w(conf_path, __func__)))
337 : goto end;
338 0 : if((cp=strrchr(copy, '/'))) *cp='\0';
339 0 : if(!(tmp=prepend_s(copy, *extrafile)))
340 : {
341 0 : log_out_of_memory(__func__);
342 0 : goto end;
343 : }
344 0 : free_w(extrafile);
345 0 : *extrafile=tmp;
346 : }
347 : #ifndef HAVE_WIN32
348 : // Treat it is a glob expression.
349 : memset(&globbuf, 0, sizeof(globbuf));
350 0 : glob(*extrafile, 0, NULL, &globbuf);
351 0 : for(i=0; (unsigned int)i<globbuf.gl_pathc; i++)
352 0 : if((ret=conf_load_lines_from_file(globbuf.gl_pathv[i], confs)))
353 : goto end;
354 : #else
355 : ret=conf_load_lines_from_file(*extrafile, confs);
356 : #endif
357 :
358 : end:
359 0 : free_w(©);
360 0 : return ret;
361 : }
362 :
363 1277 : static int conf_parse_line(struct conf **confs, const char *conf_path,
364 : char buf[], int line)
365 : {
366 1277 : int ret=-1;
367 1277 : char *f=NULL; // field
368 1277 : char *v=NULL; // value
369 1277 : char *extrafile=NULL;
370 :
371 1277 : if(!strncmp(buf, ". ", 2))
372 : {
373 : // The conf file specifies another file to include.
374 0 : char *np=NULL;
375 :
376 0 : if(!(extrafile=strdup_w(buf+2, __func__))) goto end;
377 :
378 0 : if((np=strrchr(extrafile, '\n'))) *np='\0';
379 0 : if(!*extrafile) goto end;
380 :
381 0 : ret=deal_with_dot_inclusion(conf_path, &extrafile, confs);
382 0 : goto end;
383 : }
384 :
385 1277 : if(conf_get_pair(buf, &f, &v)) goto end;
386 1276 : if(f && v
387 1264 : && load_conf_field_and_value(confs, f, v, conf_path, line))
388 : goto end;
389 : ret=0;
390 : end:
391 1277 : free_w(&extrafile);
392 1277 : return ret;
393 : }
394 :
395 : static void conf_problem(const char *conf_path, const char *msg, int *r)
396 : {
397 11 : logp("%s: %s\n", conf_path, msg);
398 11 : (*r)--;
399 : }
400 :
401 : #ifdef HAVE_IPV6
402 : // These should work for IPv4 connections too.
403 : #define DEFAULT_ADDRESS_MAIN "::"
404 : #define DEFAULT_ADDRESS_STATUS "::1"
405 : #else
406 : // Fall back to IPv4 address if IPv6 is not compiled in.
407 : #define DEFAULT_ADDRESS_MAIN "0.0.0.0"
408 : #define DEFAULT_ADDRESS_STATUS "127.0.0.1"
409 : #endif
410 :
411 42 : static int server_conf_checks(struct conf **c, const char *path, int *r)
412 : {
413 42 : if(!get_strlist(c[OPT_PORT]))
414 : conf_problem(path, "port unset", r);
415 :
416 : // FIX THIS: Most of this could be done by flags.
417 42 : if(!get_string(c[OPT_ADDRESS])
418 42 : && set_string(c[OPT_ADDRESS], DEFAULT_ADDRESS_MAIN))
419 : return -1;
420 42 : if(!get_string(c[OPT_DIRECTORY]))
421 : conf_problem(path, "directory unset", r);
422 42 : if(!get_string(c[OPT_DEDUP_GROUP]))
423 : conf_problem(path, "dedup_group unset", r);
424 42 : if(!get_string(c[OPT_CLIENTCONFDIR]))
425 : conf_problem(path, "clientconfdir unset", r);
426 42 : if(get_e_recovery_method(c[OPT_WORKING_DIR_RECOVERY_METHOD])==RECOVERY_METHOD_UNSET)
427 : conf_problem(path, "working_dir_recovery_method unset", r);
428 42 : if(!get_string(c[OPT_SSL_DHFILE]))
429 : conf_problem(path, "ssl_dhfile unset", r);
430 42 : if(get_string(c[OPT_ENCRYPTION_PASSWORD]))
431 : conf_problem(path,
432 : "encryption_password should not be set on the server!", r);
433 42 : if(!get_string(c[OPT_STATUS_ADDRESS])
434 42 : && set_string(c[OPT_STATUS_ADDRESS], DEFAULT_ADDRESS_STATUS))
435 : return -1;
436 42 : if(!get_strlist(c[OPT_STATUS_PORT])) // carry on if not set.
437 1 : logp("%s: status_port unset", path);
438 42 : if(!get_strlist(c[OPT_KEEP]))
439 : conf_problem(path, "keep unset", r);
440 42 : if(get_int(c[OPT_MAX_HARDLINKS])<2)
441 : conf_problem(path, "max_hardlinks too low", r);
442 :
443 42 : if(get_int(c[OPT_MAX_STORAGE_SUBDIRS])<=1000)
444 : conf_problem(path, "max_storage_subdirs too low", r);
445 42 : if(!get_string(c[OPT_TIMESTAMP_FORMAT])
446 42 : && set_string(c[OPT_TIMESTAMP_FORMAT], DEFAULT_TIMESTAMP_FORMAT))
447 : return -1;
448 42 : if(get_string(c[OPT_CA_CONF]))
449 : {
450 1 : int ca_err=0;
451 1 : if(!get_string(c[OPT_CA_NAME]))
452 : {
453 0 : logp("ca_conf set, but ca_name not set\n");
454 0 : ca_err++;
455 : }
456 1 : if(!get_string(c[OPT_CA_SERVER_NAME]))
457 : {
458 0 : logp("ca_conf set, but ca_server_name not set\n");
459 0 : ca_err++;
460 : }
461 1 : if(!get_string(c[OPT_CA_BURP_CA]))
462 : {
463 0 : logp("ca_conf set, but ca_burp_ca not set\n");
464 0 : ca_err++;
465 : }
466 1 : if(!get_string(c[OPT_SSL_DHFILE]))
467 : {
468 0 : logp("ca_conf set, but ssl_dhfile not set\n");
469 0 : ca_err++;
470 : }
471 1 : if(!get_string(c[OPT_SSL_CERT_CA]))
472 : {
473 0 : logp("ca_conf set, but ssl_cert_ca not set\n");
474 0 : ca_err++;
475 : }
476 1 : if(!get_string(c[OPT_SSL_CERT]))
477 : {
478 0 : logp("ca_conf set, but ssl_cert not set\n");
479 0 : ca_err++;
480 : }
481 1 : if(!get_string(c[OPT_SSL_KEY]))
482 : {
483 0 : logp("ca_conf set, but ssl_key not set\n");
484 0 : ca_err++;
485 : }
486 1 : if(ca_err) return -1;
487 : }
488 42 : if(get_string(c[OPT_MANUAL_DELETE]))
489 : {
490 0 : if(!is_absolute(get_string(c[OPT_MANUAL_DELETE])))
491 : {
492 0 : logp("ERROR: Please use an absolute manual_delete path.\n");
493 0 : return -1;
494 : }
495 : }
496 :
497 : return 0;
498 : }
499 :
500 : #ifdef HAVE_WIN32
501 : #undef X509_NAME
502 : #include <openssl/x509.h>
503 : #endif
504 :
505 0 : static char *extract_cn(X509_NAME *subj)
506 : {
507 : int nid;
508 : int index;
509 : ASN1_STRING *d;
510 : X509_NAME_ENTRY *e;
511 :
512 0 : nid=OBJ_txt2nid("CN");
513 0 : if((index=X509_NAME_get_index_by_NID(subj, nid, -1))<0
514 0 : || !(e=X509_NAME_get_entry(subj, index))
515 0 : || !(d=X509_NAME_ENTRY_get_data(e)))
516 : return NULL;
517 : #if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(LIBRESSL_VERSION_NUMBER)
518 0 : return (char *)ASN1_STRING_data(d);
519 : #else
520 : return (char *)ASN1_STRING_get0_data(d);
521 : #endif
522 : }
523 :
524 50 : static void mangle_cname(char **cname, struct conf **c)
525 : {
526 50 : if(!get_int(c[OPT_CNAME_FQDN]))
527 0 : strip_fqdn(cname);
528 50 : if(get_int(c[OPT_CNAME_LOWERCASE]))
529 0 : strlwr(*cname);
530 50 : }
531 :
532 50 : static int get_cname_from_ssl_cert(struct conf **c)
533 : {
534 50 : int ret=-1;
535 50 : struct fzp *fzp=NULL;
536 50 : X509 *cert=NULL;
537 50 : X509_NAME *subj=NULL;
538 50 : char *path=get_string(c[OPT_SSL_CERT]);
539 50 : const char *cn=NULL;
540 50 : char *copy=NULL;
541 :
542 50 : if(!path || !(fzp=fzp_open(path, "rb"))) return 0;
543 :
544 0 : if(!(cert=fzp_PEM_read_X509(fzp)))
545 : {
546 0 : logp("unable to parse %s in: %s\n", path, __func__);
547 0 : goto end;
548 : }
549 0 : if(!(subj=X509_get_subject_name(cert)))
550 : {
551 0 : logp("unable to get subject from %s in: %s\n", path, __func__);
552 0 : goto end;
553 : }
554 :
555 0 : if(!(cn=extract_cn(subj)))
556 : {
557 0 : logp("could not get CN from %s\n", path);
558 0 : goto end;
559 : }
560 0 : if(!(copy=strdup_w(cn, __func__)))
561 : goto end;
562 0 : mangle_cname(©, c);
563 0 : if(set_string(c[OPT_CNAME], copy))
564 : goto end;
565 0 : logp("cname from cert: %s\n", cn);
566 0 : if(strcmp(copy, cn))
567 0 : logp("cname mangled to: %s\n", copy);
568 :
569 : ret=0;
570 : end:
571 0 : if(cert) X509_free(cert);
572 0 : fzp_close(&fzp);
573 0 : free_w(©);
574 0 : return ret;
575 : }
576 :
577 : #ifdef HAVE_WIN32
578 : #include <winsock2.h>
579 : #include <ws2tcpip.h>
580 : #endif
581 :
582 50 : static int get_fqdn(struct conf **c)
583 : {
584 50 : int ret=-1;
585 : int gai_result;
586 : struct addrinfo hints;
587 50 : struct addrinfo *info=NULL;
588 50 : char hostname[1024]="";
589 50 : char *fqdn=NULL;
590 50 : hostname[1023] = '\0';
591 50 : if(gethostname(hostname, 1023))
592 : {
593 0 : logp("gethostname() failed: %s\n", strerror(errno));
594 0 : goto end;
595 : }
596 :
597 : memset(&hints, 0, sizeof hints);
598 : hints.ai_family=AF_UNSPEC;
599 50 : hints.ai_socktype=SOCK_STREAM;
600 50 : hints.ai_flags=AI_CANONNAME;
601 :
602 50 : if((gai_result=getaddrinfo(hostname, NULL, &hints, &info)))
603 : {
604 0 : logp("getaddrinfo in %s: %s\n", __func__,
605 : gai_strerror(gai_result));
606 0 : goto end;
607 : }
608 :
609 : //for(p=info; p; p=p->ai_next)
610 : // Just use the first one.
611 50 : if(!info)
612 : {
613 0 : logp("Got no hostname in %s\n", __func__);
614 0 : goto end;
615 : }
616 :
617 50 : if(!(fqdn=strdup_w(info->ai_canonname, __func__)))
618 : goto end;
619 50 : mangle_cname(&fqdn, c);
620 :
621 50 : if(set_string(c[OPT_CNAME], fqdn))
622 : goto end;
623 50 : logp("cname from hostname: %s\n", get_string(c[OPT_CNAME]));
624 :
625 50 : ret=0;
626 : end:
627 50 : if(info) freeaddrinfo(info);
628 50 : free_w(&fqdn);
629 50 : return ret;
630 : }
631 :
632 92 : const char *confs_get_lockfile(struct conf **confs)
633 : {
634 92 : const char *lockfile=get_string(confs[OPT_LOCKFILE]);
635 92 : if(!lockfile) lockfile=get_string(confs[OPT_PIDFILE]);
636 92 : return lockfile;
637 : }
638 :
639 92 : static int general_conf_checks(struct conf **c, const char *path, int *r)
640 : {
641 92 : if(!confs_get_lockfile(c))
642 : conf_problem(path, "lockfile unset", r);
643 92 : if(!get_string(c[OPT_SSL_CERT]))
644 : conf_problem(path, "ssl_cert unset", r);
645 92 : if(!get_string(c[OPT_SSL_CERT_CA]))
646 : conf_problem(path, "ssl_cert_ca unset", r);
647 92 : return 0;
648 : }
649 :
650 50 : static int client_conf_checks(struct conf **c, const char *path, int *r)
651 : {
652 50 : const char *autoupgrade_os=get_string(c[OPT_AUTOUPGRADE_OS]);
653 :
654 50 : if(!get_int(c[OPT_PORT_BACKUP]))
655 : conf_problem(path, "port_backup unset", r);
656 50 : if(!get_int(c[OPT_PORT_RESTORE]))
657 : conf_problem(path, "port_restore unset", r);
658 50 : if(!get_int(c[OPT_PORT_VERIFY]))
659 : conf_problem(path, "port_verify unset", r);
660 50 : if(!get_int(c[OPT_PORT_LIST]))
661 : conf_problem(path, "port_list unset", r);
662 50 : if(!get_int(c[OPT_PORT_DELETE]))
663 : conf_problem(path, "port_delete unset", r);
664 :
665 50 : if(!get_string(c[OPT_CNAME]))
666 : {
667 50 : if(get_cname_from_ssl_cert(c)) return -1;
668 : // There was no error. This is probably a new install.
669 : // Try getting the fqdn and using that.
670 50 : if(!get_string(c[OPT_CNAME]))
671 : {
672 50 : if(get_fqdn(c)) return -1;
673 50 : if(!get_string(c[OPT_CNAME]))
674 : conf_problem(path, "client name unset", r);
675 : }
676 : }
677 50 : if(!get_string(c[OPT_PASSWORD]))
678 : {
679 50 : logp("password not set, falling back to \"password\"\n");
680 50 : if(set_string(c[OPT_PASSWORD], "password"))
681 : return -1;
682 : }
683 50 : if(!get_string(c[OPT_SERVER]))
684 : conf_problem(path, "server unset", r);
685 50 : if(!get_strlist(c[OPT_STATUS_PORT])) // carry on if not set.
686 4 : logp("%s: status_port unset\n", path);
687 50 : if(!get_string(c[OPT_SSL_PEER_CN]))
688 : {
689 1 : const char *server=get_string(c[OPT_SERVER]);
690 1 : logp("ssl_peer_cn unset\n");
691 1 : if(!server)
692 : {
693 0 : logp("falling back to '%s'\n", server);
694 0 : if(set_string(c[OPT_SSL_PEER_CN], server))
695 : return -1;
696 : }
697 : }
698 50 : if(autoupgrade_os
699 0 : && strstr(autoupgrade_os, ".."))
700 : conf_problem(path,
701 : "autoupgrade_os must not contain a '..' component", r);
702 50 : if(get_string(c[OPT_CA_BURP_CA]))
703 : {
704 1 : if(!get_string(c[OPT_CA_CSR_DIR]))
705 : conf_problem(path,
706 : "ca_burp_ca set, but ca_csr_dir not set\n", r);
707 1 : if(!get_string(c[OPT_SSL_CERT_CA]))
708 : conf_problem(path,
709 : "ca_burp_ca set, but ssl_cert_ca not set\n", r);
710 1 : if(!get_string(c[OPT_SSL_CERT]))
711 : conf_problem(path,
712 : "ca_burp_ca set, but ssl_cert not set\n", r);
713 1 : if(!get_string(c[OPT_SSL_KEY]))
714 : conf_problem(path,
715 : "ca_burp_ca set, but ssl_key not set\n", r);
716 : }
717 :
718 50 : if(!r)
719 : {
720 : struct strlist *l;
721 0 : logp("Listing configured paths:\n");
722 0 : for(l=get_strlist(c[OPT_INCEXCDIR]); l; l=l->next)
723 0 : logp("%s: %s\n", l->flag?"include":"exclude", l->path);
724 0 : logp("Listing starting paths:\n");
725 0 : for(l=get_strlist(c[OPT_STARTDIR]); l; l=l->next)
726 0 : if(l->flag) logp("%s\n", l->path);
727 : }
728 : return 0;
729 : }
730 :
731 151 : static int finalise_keep_args(struct conf **c)
732 : {
733 : struct strlist *k;
734 151 : struct strlist *last=NULL;
735 151 : uint64_t mult=1;
736 240 : for(k=get_strlist(c[OPT_KEEP]); k; k=k->next)
737 : {
738 178 : if(!(k->flag=atoi(k->path)))
739 : {
740 0 : logp("'keep' value cannot be set to '%s'\n", k->path);
741 0 : return -1;
742 : }
743 89 : mult*=k->flag;
744 :
745 : // An error if you try to keep backups every second
746 : // for 100 years.
747 89 : if(mult>52560000)
748 : {
749 0 : logp("Your 'keep' values are far too high. High enough to keep a backup every second for 10 years. Please lower them to something sensible.\n");
750 0 : return -1;
751 : }
752 89 : last=k;
753 : }
754 : // If more than one keep value is set, add one to the last one.
755 : // This is so that, for example, having set 7, 4, 6, then
756 : // a backup of age 7*4*6=168 or more is guaranteed to be kept.
757 : // Otherwise, only 7*4*5=140 would be guaranteed to be kept.
758 151 : k=get_strlist(c[OPT_KEEP]);
759 151 : if(k && k->next) last->flag++;
760 : return 0;
761 : }
762 :
763 44 : static int incexc_munge(struct conf **c, struct strlist *s)
764 : {
765 : #ifdef HAVE_WIN32
766 : convert_backslashes(&s->path);
767 : #endif
768 44 : if(!is_absolute(s->path))
769 : {
770 1 : logp("ERROR: Please use absolute include/exclude paths.\n");
771 : return -1;
772 : }
773 43 : if(add_to_strlist(c[OPT_INCEXCDIR], s->path, s->flag))
774 : return -1;
775 : return 0;
776 : }
777 :
778 155 : static int finalise_incexc_dirs(struct conf **c)
779 : {
780 155 : struct strlist *s=NULL;
781 :
782 193 : for(s=get_strlist(c[OPT_INCLUDE]); s; s=s->next)
783 39 : if(incexc_munge(c, s)) return -1;
784 159 : for(s=get_strlist(c[OPT_EXCLUDE]); s; s=s->next)
785 5 : if(incexc_munge(c, s)) return -1;
786 : return 0;
787 : }
788 :
789 6 : static int add_to_cross_filesystem(struct conf **c, const char *path)
790 : {
791 6 : if(strlist_find(get_strlist(c[OPT_FSCHGDIR]), path, 0))
792 : return 0;
793 6 : return add_to_strlist(c[OPT_FSCHGDIR], path, 0);
794 : }
795 :
796 : // This decides which directories to start backing up, and which
797 : // are subdirectories which don't need to be started separately.
798 154 : static int finalise_start_dirs(struct conf **c)
799 : {
800 154 : struct strlist *s=NULL;
801 154 : struct strlist *last_ie=NULL;
802 154 : struct strlist *last_sd=NULL;
803 :
804 : // Make sure that the startdir list starts empty, or chaos will ensue.
805 154 : conf_free_content(c[OPT_STARTDIR]);
806 :
807 191 : for(s=get_strlist(c[OPT_INCLUDE]); s; s=s->next)
808 : {
809 : #ifdef HAVE_WIN32
810 : convert_backslashes(&s->path);
811 : #endif
812 38 : if(!is_absolute(s->path))
813 : {
814 0 : logp("ERROR: Please use absolute include/exclude paths.\n");
815 0 : return -1;
816 : }
817 :
818 : // Ensure that we do not backup the same directory twice.
819 38 : if(last_ie && !strcmp(s->path, last_ie->path))
820 : {
821 1 : logp("Directory appears twice in conf: %s\n",
822 : s->path);
823 1 : return -1;
824 : }
825 : // If it is not a subdirectory of the most recent start point,
826 : // we have found another start point.
827 37 : if(!get_strlist(c[OPT_STARTDIR])
828 14 : || !last_sd || !is_subdir(last_sd->path, s->path))
829 : {
830 : // Do not use strlist_add_sorted, because last_sd is
831 : // relying on incexcdir already being sorted.
832 31 : if(add_to_strlist(c[OPT_STARTDIR], s->path, s->flag))
833 : return -1;
834 : last_sd=s;
835 : }
836 : else
837 : {
838 : // If it is not a starting directory, it should at
839 : // least be included as a cross_filesystem entry.
840 6 : if(add_to_cross_filesystem(c, s->path))
841 : return -1;
842 : }
843 37 : last_ie=s;
844 : }
845 : return 0;
846 : }
847 :
848 : // The glob stuff should only run on the client side.
849 54 : static int finalise_glob(struct conf **c)
850 : {
851 54 : int ret=-1;
852 : #ifdef HAVE_WIN32
853 : if(glob_windows(c)) goto end;
854 : #else
855 : int i;
856 : glob_t globbuf;
857 : struct strlist *l;
858 54 : struct strlist *last=NULL;
859 : memset(&globbuf, 0, sizeof(globbuf));
860 57 : for(l=get_strlist(c[OPT_INCGLOB]); l; l=l->next)
861 : {
862 3 : glob(l->path, last?GLOB_APPEND:0, NULL, &globbuf);
863 3 : last=l;
864 : }
865 :
866 3 : for(i=0; (unsigned int)i<globbuf.gl_pathc; i++)
867 3 : if(add_to_strlist_include_uniq(c[OPT_INCLUDE], globbuf.gl_pathv[i]))
868 : goto end;
869 :
870 54 : globfree(&globbuf);
871 : #endif
872 54 : ret=0;
873 : end:
874 54 : return ret;
875 : }
876 :
877 : // Reeval the glob after script pre
878 2 : int reeval_glob(struct conf **c)
879 : {
880 2 : if(finalise_glob(c))
881 : return -1;
882 :
883 2 : if(finalise_incexc_dirs(c)
884 2 : || finalise_start_dirs(c)) return -1;
885 :
886 : return 0;
887 : }
888 :
889 : // Set the flag of the first item in a list that looks at extensions to the
890 : // maximum number of characters that need to be checked, plus one. This is for
891 : // a bit of added efficiency.
892 459 : static void set_max_ext(struct strlist *list)
893 : {
894 459 : int max=0;
895 459 : struct strlist *l=NULL;
896 463 : for(l=list; l; l=l->next)
897 : {
898 4 : int s=strlen(l->path);
899 4 : if(s>max) max=s;
900 : }
901 459 : if(list) list->flag=max+1;
902 459 : }
903 :
904 306 : static int finalise_fstypes(struct conf **c, int opt)
905 : {
906 : struct strlist *l;
907 : // Set the strlist flag for the excluded fstypes
908 306 : for(l=get_strlist(c[opt]); l; l=l->next)
909 : {
910 0 : l->flag=0;
911 0 : if(!strncasecmp(l->path, "0x", 2))
912 : {
913 0 : l->flag=strtol((l->path)+2, NULL, 16);
914 0 : logp("Excluding file system type 0x%08lX\n", l->flag);
915 : }
916 : else
917 : {
918 0 : if(fstype_to_flag(l->path, &(l->flag)))
919 : {
920 0 : logp("Unknown exclude fs type: %s\n", l->path);
921 0 : l->flag=0;
922 : }
923 : }
924 : }
925 306 : return 0;
926 : }
927 :
928 10 : static int setup_script_arg_override(struct conf *c, struct conf *args)
929 : {
930 : struct strlist *s;
931 10 : set_strlist(args, NULL);
932 30 : for(s=get_strlist(c); s; s=s->next)
933 20 : if(add_to_strlist(args, s->path, s->flag))
934 : return -1;
935 : return 0;
936 : }
937 :
938 450 : static int setup_script_arg_overrides(struct conf *c,
939 : struct conf *pre_args, struct conf *post_args)
940 : {
941 450 : if(!get_strlist(c)) return 0;
942 10 : return setup_script_arg_override(c, pre_args)
943 5 : || setup_script_arg_override(c, post_args);
944 : }
945 :
946 85 : static int finalise_server_ports(struct conf **c,
947 : enum conf_opt port_opt, enum conf_opt max_children_opt)
948 : {
949 : struct strlist *p;
950 : struct strlist *mc;
951 85 : long max_children=5;
952 :
953 345 : for(p=get_strlist(c[port_opt]),
954 175 : mc=get_strlist(c[max_children_opt]); p; p=p->next)
955 : {
956 90 : if(mc)
957 : {
958 22 : if((max_children=atol(mc->path))<=0)
959 : {
960 0 : logp("%s too low for %s %s\n",
961 0 : c[max_children_opt]->field,
962 0 : c[port_opt]->field,
963 : p->path);
964 0 : return -1;
965 : }
966 11 : p->flag=max_children;
967 :
968 11 : mc=mc->next;
969 : }
970 : else
971 : {
972 158 : logp("%s %s defaulting to %s %lu\n",
973 79 : c[port_opt]->field,
974 : p->path,
975 79 : c[max_children_opt]->field,
976 : max_children);
977 79 : p->flag=max_children;
978 : }
979 : }
980 :
981 85 : if(mc)
982 : {
983 1 : logp("too many %s options\n", c[max_children_opt]->field);
984 1 : return -1;
985 : }
986 :
987 : return 0;
988 : }
989 :
990 50 : static int finalise_client_ports(struct conf **c)
991 : {
992 50 : int port=0;
993 : struct strlist *p;
994 :
995 98 : for(p=get_strlist(c[OPT_PORT]); p; p=p->next)
996 96 : port=atoi(p->path);
997 50 : if(!port)
998 : return 0;
999 :
1000 48 : if(!get_int(c[OPT_PORT_BACKUP]))
1001 47 : set_int(c[OPT_PORT_BACKUP], port);
1002 48 : if(!get_int(c[OPT_PORT_RESTORE]))
1003 46 : set_int(c[OPT_PORT_RESTORE], port);
1004 48 : if(!get_int(c[OPT_PORT_VERIFY]))
1005 47 : set_int(c[OPT_PORT_VERIFY], get_int(c[OPT_PORT_RESTORE]));
1006 48 : if(!get_int(c[OPT_PORT_LIST]))
1007 47 : set_int(c[OPT_PORT_LIST], port);
1008 48 : if(!get_int(c[OPT_PORT_DELETE]))
1009 47 : set_int(c[OPT_PORT_DELETE], port);
1010 :
1011 : return 0;
1012 : }
1013 :
1014 153 : static int conf_finalise(struct conf **c)
1015 : {
1016 153 : enum burp_mode burp_mode=get_e_burp_mode(c[OPT_BURP_MODE]);
1017 153 : int s_script_notify=0;
1018 153 : if(finalise_fstypes(c, OPT_EXCFS)) return -1;
1019 153 : if(finalise_fstypes(c, OPT_INCFS)) return -1;
1020 :
1021 153 : strlist_compile_regexes(get_strlist(c[OPT_INCREG]));
1022 153 : strlist_compile_regexes(get_strlist(c[OPT_EXCREG]));
1023 :
1024 153 : set_max_ext(get_strlist(c[OPT_INCEXT]));
1025 153 : set_max_ext(get_strlist(c[OPT_EXCEXT]));
1026 153 : set_max_ext(get_strlist(c[OPT_EXCOM]));
1027 :
1028 153 : if(burp_mode==BURP_MODE_CLIENT
1029 52 : && finalise_glob(c)) return -1;
1030 :
1031 153 : if(finalise_incexc_dirs(c)
1032 152 : || finalise_start_dirs(c)) return -1;
1033 :
1034 151 : if(finalise_keep_args(c)) return -1;
1035 :
1036 151 : if(burp_mode==BURP_MODE_SERVER)
1037 : {
1038 43 : if(finalise_server_ports(c,
1039 : OPT_PORT, OPT_MAX_CHILDREN)
1040 42 : || finalise_server_ports(c,
1041 : OPT_STATUS_PORT, OPT_MAX_STATUS_CHILDREN))
1042 : return -1;
1043 : }
1044 150 : if(burp_mode==BURP_MODE_CLIENT)
1045 : {
1046 50 : if(finalise_client_ports(c))
1047 : return -1;
1048 : }
1049 :
1050 150 : if((s_script_notify=get_int(c[OPT_S_SCRIPT_NOTIFY])))
1051 : {
1052 4 : set_int(c[OPT_S_SCRIPT_PRE_NOTIFY], s_script_notify);
1053 4 : set_int(c[OPT_S_SCRIPT_POST_NOTIFY], s_script_notify);
1054 : }
1055 :
1056 : // These override the specific pre/post script paths with the general
1057 : // one. For example, if 'server_script' is set, its value is used for
1058 : // 'server_script_pre' and 'server_script_post'.
1059 150 : if(pre_post_override(c[OPT_B_SCRIPT],
1060 : c[OPT_B_SCRIPT_PRE], c[OPT_B_SCRIPT_POST])
1061 150 : || pre_post_override(c[OPT_R_SCRIPT],
1062 : c[OPT_R_SCRIPT_PRE], c[OPT_R_SCRIPT_POST])
1063 150 : || pre_post_override(c[OPT_S_SCRIPT],
1064 : c[OPT_S_SCRIPT_PRE], c[OPT_S_SCRIPT_POST])
1065 : // And these do the same for the script arguments.
1066 150 : || setup_script_arg_overrides(c[OPT_B_SCRIPT_ARG],
1067 : c[OPT_B_SCRIPT_PRE_ARG], c[OPT_B_SCRIPT_POST_ARG])
1068 150 : || setup_script_arg_overrides(c[OPT_R_SCRIPT_ARG],
1069 : c[OPT_R_SCRIPT_PRE_ARG], c[OPT_R_SCRIPT_POST_ARG])
1070 150 : || setup_script_arg_overrides(c[OPT_S_SCRIPT_ARG],
1071 : c[OPT_S_SCRIPT_PRE_ARG], c[OPT_S_SCRIPT_POST_ARG]))
1072 : return -1;
1073 :
1074 : // We are now done with these. Clear them, otherwise they interfere.
1075 150 : set_string(c[OPT_S_SCRIPT], NULL);
1076 150 : set_strlist(c[OPT_S_SCRIPT_ARG], NULL);
1077 150 : return 0;
1078 : }
1079 :
1080 92 : static int conf_finalise_global_only(const char *conf_path, struct conf **confs)
1081 : {
1082 92 : int r=0;
1083 :
1084 : // Let the caller check the 'keep' value.
1085 :
1086 92 : if(!get_string(confs[OPT_SSL_KEY_PASSWORD])
1087 92 : && set_string(confs[OPT_SSL_KEY_PASSWORD], ""))
1088 0 : r--;
1089 :
1090 92 : if(general_conf_checks(confs, conf_path, &r)) r--;
1091 :
1092 92 : switch(get_e_burp_mode(confs[OPT_BURP_MODE]))
1093 : {
1094 : case BURP_MODE_SERVER:
1095 42 : if(server_conf_checks(confs, conf_path, &r)) r--;
1096 : break;
1097 : case BURP_MODE_CLIENT:
1098 50 : if(client_conf_checks(confs, conf_path, &r)) r--;
1099 : break;
1100 : case BURP_MODE_UNSET:
1101 : default:
1102 0 : logp("%s: mode unset - need 'server' or 'client'\n",
1103 : conf_path);
1104 0 : r--;
1105 0 : break;
1106 : }
1107 :
1108 92 : return r;
1109 : }
1110 :
1111 158 : static int conf_load_lines_from_file(const char *conf_path, struct conf **confs)
1112 : {
1113 158 : int ret=0;
1114 158 : int line=0;
1115 158 : FILE *fp=NULL;
1116 158 : char buf[4096]="";
1117 :
1118 158 : if(!(fp=fopen(conf_path, "r")))
1119 : {
1120 7 : logp("could not open '%s' for reading.\n", conf_path);
1121 7 : return -1;
1122 : }
1123 1420 : while(fgets(buf, sizeof(buf), fp))
1124 : {
1125 1269 : line++;
1126 1269 : if(conf_parse_line(confs, conf_path, buf, line))
1127 : {
1128 : conf_error(conf_path, line);
1129 1 : ret=-1;
1130 : }
1131 : }
1132 151 : if(fp) fclose(fp);
1133 151 : return ret;
1134 : }
1135 :
1136 : #ifndef UTEST
1137 : static
1138 : #endif
1139 3 : int conf_load_lines_from_buf(const char *buf, struct conf **c)
1140 : {
1141 3 : int ret=0;
1142 3 : int line=0;
1143 3 : char *tok=NULL;
1144 3 : char *copy=NULL;
1145 :
1146 3 : if(!buf) return 0;
1147 :
1148 3 : if(!(copy=strdup_w(buf, __func__))) return -1;
1149 3 : if(!(tok=strtok(copy, "\n")))
1150 : {
1151 0 : logp("unable to parse conf buffer\n");
1152 0 : free_w(©);
1153 0 : return -1;
1154 : }
1155 : do
1156 : {
1157 8 : line++;
1158 8 : if(conf_parse_line(c, "", tok, line))
1159 : {
1160 : ret=-1;
1161 : break;
1162 : }
1163 8 : } while((tok=strtok(NULL, "\n")));
1164 3 : free_w(©);
1165 :
1166 3 : return ret;
1167 : }
1168 :
1169 : /* The server runs this when parsing a restore file on the server. */
1170 10 : int conf_parse_incexcs_path(struct conf **c, const char *path)
1171 : {
1172 10 : free_incexcs(c);
1173 10 : if(conf_load_lines_from_file(path, c)
1174 6 : || conf_finalise(c))
1175 : return -1;
1176 : return 0;
1177 : }
1178 :
1179 : /* The client runs this when the server overrides the incexcs. */
1180 3 : int conf_parse_incexcs_buf(struct conf **c, const char *incexc)
1181 : {
1182 3 : free_incexcs(c);
1183 3 : if(conf_load_lines_from_buf(incexc, c)
1184 3 : || conf_finalise(c))
1185 : return -1;
1186 : return 0;
1187 : }
1188 :
1189 53 : static int conf_set_from_global(struct conf **globalc, struct conf **cc)
1190 : {
1191 53 : int i=0;
1192 8798 : for(i=0; i<OPT_MAX; i++)
1193 : {
1194 8745 : if(!(cc[i]->flags & CONF_FLAG_CC_OVERRIDE))
1195 5830 : continue;
1196 2915 : switch(cc[i]->conf_type)
1197 : {
1198 : case CT_STRING:
1199 742 : set_string(cc[i], get_string(globalc[i]));
1200 742 : break;
1201 : case CT_UINT:
1202 1431 : set_int(cc[i], get_int(globalc[i]));
1203 1431 : break;
1204 : case CT_FLOAT:
1205 0 : set_float(cc[i], get_float(globalc[i]));
1206 0 : break;
1207 : case CT_MODE_T:
1208 0 : set_mode_t(cc[i], get_mode_t(globalc[i]));
1209 0 : break;
1210 : case CT_SSIZE_T:
1211 106 : set_uint64_t(cc[i], get_uint64_t(globalc[i]));
1212 106 : break;
1213 : case CT_E_BURP_MODE:
1214 0 : set_e_burp_mode(cc[i], get_e_burp_mode(globalc[i]));
1215 0 : break;
1216 : case CT_E_PROTOCOL:
1217 53 : set_e_protocol(cc[i], get_e_protocol(globalc[i]));
1218 53 : break;
1219 : case CT_E_RECOVERY_METHOD:
1220 53 : set_e_recovery_method(cc[i], get_e_recovery_method(globalc[i]));
1221 53 : break;
1222 : case CT_E_RSHASH:
1223 53 : set_e_rshash(cc[i], get_e_rshash(globalc[i]));
1224 53 : break;
1225 : case CT_STRLIST:
1226 : // Done later.
1227 : break;
1228 : case CT_CNTR:
1229 : break;
1230 : // No default so that there are warnings if anything
1231 : // was missed.
1232 : }
1233 : }
1234 :
1235 : // If ssl_peer_cn is not set, default it to the client name.
1236 53 : if(!get_string(globalc[OPT_SSL_PEER_CN])
1237 42 : && set_string(cc[OPT_SSL_PEER_CN], get_string(cc[OPT_CNAME])))
1238 : return -1;
1239 :
1240 : return 0;
1241 : }
1242 :
1243 472 : static int append_strlist(struct conf *dst, struct conf *src)
1244 : {
1245 : struct strlist *s;
1246 539 : for(s=get_strlist(src); s; s=s->next)
1247 67 : if(add_to_strlist(dst, s->path, s->flag))
1248 : return -1;
1249 : return 0;
1250 : }
1251 :
1252 : // Instead of adding onto the end of the list, this replaces the list.
1253 49 : static int conf_set_from_global_arg_list_overrides(struct conf **globalc,
1254 : struct conf **cc)
1255 : {
1256 49 : int i=0;
1257 8134 : for(i=0; i<OPT_MAX; i++)
1258 : {
1259 8085 : if(cc[i]->conf_type!=CT_STRLIST) continue;
1260 1715 : if(!(cc[i]->flags & CONF_FLAG_CC_OVERRIDE)) continue;
1261 441 : if(cc[i]->flags & CONF_FLAG_STRLIST_REPLACE)
1262 : {
1263 : // If there was no cc[i] strlist set, use the global.
1264 392 : if(!get_strlist(cc[i])
1265 374 : && append_strlist(cc[i], globalc[i]))
1266 : return -1;
1267 : }
1268 : else
1269 : {
1270 : struct conf tmpconf;
1271 : // A bit painful.
1272 49 : tmpconf.conf_type=cc[i]->conf_type;
1273 49 : tmpconf.flags=cc[i]->flags;
1274 : memset(&tmpconf.data, 0, sizeof(tmpconf.data));
1275 49 : if(append_strlist(&tmpconf, globalc[i])
1276 49 : || append_strlist(&tmpconf, cc[i]))
1277 0 : return -1;
1278 49 : set_strlist(cc[i], get_strlist(&tmpconf));
1279 : }
1280 : }
1281 : return 0;
1282 : }
1283 :
1284 34 : static int conf_init_save_cname_and_version(struct conf **cconfs)
1285 : {
1286 34 : int ret=-1;
1287 34 : char *cname=NULL;
1288 34 : char *cversion=NULL;
1289 34 : char *orig_cname=get_string(cconfs[OPT_CNAME]);
1290 34 : char *orig_cversion=get_string(cconfs[OPT_PEER_VERSION]);
1291 :
1292 34 : if((orig_cname && !(cname=strdup_w(orig_cname, __func__)))
1293 34 : || (orig_cversion
1294 10 : && !(cversion=strdup_w(orig_cversion, __func__))))
1295 : goto end;
1296 :
1297 34 : set_string(cconfs[OPT_CNAME], NULL);
1298 34 : set_string(cconfs[OPT_PEER_VERSION], NULL);
1299 34 : if(confs_init(cconfs)) goto end;
1300 34 : set_string(cconfs[OPT_CNAME], cname);
1301 34 : set_string(cconfs[OPT_PEER_VERSION], cversion);
1302 34 : ret=0;
1303 : end:
1304 34 : free_w(&cname);
1305 34 : free_w(&cversion);
1306 34 : return ret;
1307 : }
1308 :
1309 53 : static int do_conf_load_overrides(struct conf **globalcs, struct conf **cconfs,
1310 : const char *path, const char *buf)
1311 : {
1312 : // Some client settings can be globally set in the server conf and
1313 : // overridden in the client specific conf.
1314 53 : if(conf_set_from_global(globalcs, cconfs)) return -1;
1315 53 : if(buf) { if(conf_load_lines_from_buf(buf, cconfs)) return -1; }
1316 53 : else { if(conf_load_lines_from_file(path, cconfs)) return -1; }
1317 49 : if(conf_set_from_global_arg_list_overrides(globalcs, cconfs)
1318 49 : || conf_finalise(cconfs))
1319 : return -1;
1320 : return 0;
1321 : }
1322 :
1323 : #ifndef UTEST
1324 : static
1325 : #endif
1326 19 : int conf_load_overrides(struct conf **globalcs, struct conf **cconfs,
1327 : const char *path)
1328 : {
1329 53 : return do_conf_load_overrides(globalcs, cconfs, path, NULL);
1330 : }
1331 :
1332 34 : int conf_load_clientconfdir(struct conf **globalcs, struct conf **cconfs)
1333 : {
1334 34 : int ret=-1;
1335 34 : char *path=NULL;
1336 34 : const char *cname=NULL;
1337 :
1338 34 : if(conf_init_save_cname_and_version(cconfs)) goto end;
1339 34 : cname=get_string(cconfs[OPT_CNAME]);
1340 34 : if(looks_like_tmp_or_hidden_file(cname))
1341 : {
1342 0 : logp("client name '%s' is invalid\n", cname);
1343 0 : goto end;
1344 : }
1345 :
1346 34 : if(!(path=prepend_s(get_string(globalcs[OPT_CLIENTCONFDIR]), cname)))
1347 : goto end;
1348 68 : ret=conf_load_overrides(globalcs, cconfs, path);
1349 : end:
1350 34 : free_w(&path);
1351 34 : return ret;
1352 : }
1353 :
1354 95 : static int do_load_global_only(struct conf **globalcs,
1355 : const char *path, const char *buf)
1356 : {
1357 95 : if(set_string(globalcs[OPT_CONFFILE], path)) return -1;
1358 95 : if(buf) { if(conf_load_lines_from_buf(buf, globalcs)) return -1; }
1359 95 : else { if(conf_load_lines_from_file(path, globalcs)) return -1; }
1360 95 : if(conf_finalise(globalcs)
1361 92 : || conf_finalise_global_only(path, globalcs))
1362 : return -1;
1363 : return 0;
1364 :
1365 : }
1366 :
1367 95 : int conf_load_global_only(const char *path, struct conf **globalcs)
1368 : {
1369 95 : return do_load_global_only(globalcs, path, NULL);
1370 : }
1371 :
1372 4 : static int restore_client_allowed(struct conf **cconfs, struct conf **sconfs)
1373 : {
1374 : struct strlist *r;
1375 7 : for(r=get_strlist(sconfs[OPT_RESTORE_CLIENTS]); r; r=r->next)
1376 6 : if(!strcmp(r->path, get_string(cconfs[OPT_CNAME])))
1377 : return 1;
1378 1 : logp("Access to client is not allowed: %s\n",
1379 : get_string(sconfs[OPT_CNAME]));
1380 1 : return 0;
1381 : }
1382 :
1383 : // FIX THIS: need to unit test this.
1384 5 : int conf_switch_to_orig_client(struct conf **globalcs,
1385 : struct conf **cconfs, const char *orig_client)
1386 : {
1387 5 : int ret=-1;
1388 5 : struct conf **sconfs=NULL;
1389 5 : if(!(sconfs=confs_alloc())
1390 5 : || confs_init(sconfs)) goto end;
1391 5 : if(set_string(sconfs[OPT_CNAME], orig_client))
1392 : goto end;
1393 5 : logp("Client wants to switch to client: %s\n",
1394 5 : get_string(sconfs[OPT_CNAME]));
1395 :
1396 5 : if(conf_load_clientconfdir(globalcs, sconfs))
1397 : {
1398 1 : logp("Could not load alternate config: %s",
1399 1 : get_string(sconfs[OPT_CNAME]));
1400 1 : goto end;
1401 : }
1402 4 : set_int(sconfs[OPT_SEND_CLIENT_CNTR],
1403 4 : get_int(cconfs[OPT_SEND_CLIENT_CNTR]));
1404 :
1405 4 : if(!restore_client_allowed(cconfs, sconfs))
1406 : goto end;
1407 :
1408 3 : if(set_string(sconfs[OPT_RESTORE_PATH],
1409 3 : get_string(cconfs[OPT_RESTORE_PATH])))
1410 : goto end;
1411 3 : if(set_string(cconfs[OPT_RESTORE_PATH], NULL))
1412 : goto end;
1413 3 : set_cntr(sconfs[OPT_CNTR], get_cntr(cconfs));
1414 3 : set_cntr(cconfs[OPT_CNTR], NULL);
1415 3 : confs_free_content(cconfs);
1416 3 : confs_init(cconfs);
1417 3 : confs_memcpy(cconfs, sconfs);
1418 3 : confs_null(sconfs);
1419 3 : if(set_string(cconfs[OPT_RESTORE_CLIENT],
1420 3 : get_string(cconfs[OPT_CNAME]))) goto end;
1421 3 : if(set_string(cconfs[OPT_ORIG_CLIENT],
1422 3 : get_string(cconfs[OPT_CNAME]))) goto end;
1423 :
1424 3 : logp("Switched to client %s\n", get_string(cconfs[OPT_CNAME]));
1425 3 : ret=0;
1426 : end:
1427 5 : confs_free(&sconfs);
1428 5 : return ret;
1429 : }
1430 :
1431 25 : char *config_default_path(void)
1432 : {
1433 : static char path[256]="";
1434 : #ifdef HAVE_WIN32
1435 : char *pfenv=NULL;
1436 :
1437 : // Burp used to always install to 'C:/Program Files/Burp/', but as
1438 : // of 1.3.11, it changed to %PROGRAMFILES%. Still want the old way
1439 : // to work though. So check %PROGRAMFILES% first, then fall back.
1440 : if((pfenv=getenv("PROGRAMFILES")))
1441 : {
1442 : struct stat statp;
1443 : snprintf(path, sizeof(path), "%s/Burp/burp.conf", pfenv);
1444 : if(!lstat(path, &statp)
1445 : && !S_ISDIR(statp.st_mode))
1446 : return path;
1447 : }
1448 : snprintf(path, sizeof(path), "C:/Program Files/Burp/burp.conf");
1449 : #else
1450 : snprintf(path, sizeof(path), "%s", SYSCONFDIR "/burp.conf");
1451 : #endif
1452 25 : return path;
1453 : }
|