1 #include "burp.h"
2 #include "alloc.h"
3 #include "asfd.h"
4 #include "cmd.h"
5 #include "log.h"
6 #include "prepend.h"
7 #include "strlist.h"
8 #include "incexc_send.h"
9 
send_incexc_string(struct asfd * asfd,const char * field,const char * str)10 static int send_incexc_string(struct asfd *asfd,
11 	const char *field, const char *str)
12 {
13 	char *tosend=NULL;
14 	int ret=-1;
15 	if(!str) return 0;
16 	if(!(tosend=prepend_n(field, str, strlen(str), " = ")))
17 		goto end;
18 	if(asfd->write_str(asfd, CMD_GEN, tosend))
19 	{
20 		logp("Error in async_write_str when sending incexc\n");
21 		goto end;
22 	}
23 	ret=0;
24 end:
25 	free_w(&tosend);
26 	return ret;
27 }
28 
send_incexc_str(struct asfd * asfd,struct conf * conf)29 static int send_incexc_str(struct asfd *asfd, struct conf *conf)
30 {
31 	return send_incexc_string(asfd, conf->field, get_string(conf));
32 }
33 
send_incexc_uint(struct asfd * asfd,struct conf * conf)34 static int send_incexc_uint(struct asfd *asfd, struct conf *conf)
35 {
36 	char tmp[64]="";
37 	snprintf(tmp, sizeof(tmp), "%d", get_int(conf));
38 	return send_incexc_string(asfd, conf->field, tmp);
39 }
40 
send_incexc_uint64(struct asfd * asfd,struct conf * conf)41 static int send_incexc_uint64(struct asfd *asfd, struct conf *conf)
42 {
43 	char tmp[32]="";
44 	snprintf(tmp, sizeof(tmp), "%" PRIu64, get_uint64_t(conf));
45 	return send_incexc_string(asfd, conf->field, tmp);
46 }
47 
send_incexc_strlist(struct asfd * asfd,struct conf * conf)48 static int send_incexc_strlist(struct asfd *asfd, struct conf *conf)
49 {
50 	struct strlist *l;
51 	for(l=get_strlist(conf); l; l=l->next)
52 		if(send_incexc_string(asfd, conf->field, l->path)) return -1;
53 	return 0;
54 }
55 
do_sends(struct asfd * asfd,struct conf ** confs,int flag)56 static int do_sends(struct asfd *asfd, struct conf **confs, int flag)
57 {
58 	int i=0;
59 	int r=-1;
60 	for(i=0; i<OPT_MAX; i++)
61 	{
62 		if(!(confs[i]->flags & flag)) continue;
63 		switch(confs[i]->conf_type)
64 		{
65 			case CT_STRING:
66 				if(send_incexc_str(asfd, confs[i]))
67 					goto end;
68 				break;
69 			case CT_STRLIST:
70 				if(send_incexc_strlist(asfd, confs[i]))
71 					goto end;
72 				break;
73 			case CT_UINT:
74 				if(send_incexc_uint(asfd, confs[i]))
75 					goto end;
76 				break;
77 			case CT_SSIZE_T:
78 				if(send_incexc_uint64(asfd, confs[i]))
79 					goto end;
80 				break;
81 			case CT_FLOAT:
82 			case CT_MODE_T:
83 			case CT_E_BURP_MODE:
84 			case CT_E_PROTOCOL:
85 			case CT_E_RECOVERY_METHOD:
86 			case CT_E_RSHASH:
87 			case CT_CNTR:
88 				break;
89 		}
90 	}
91 	r=0;
92 end:
93 	return r;
94 }
95 
do_request_response(struct asfd * asfd,const char * reqstr,const char * repstr)96 static int do_request_response(struct asfd *asfd,
97 	const char *reqstr, const char *repstr)
98 {
99 	return (asfd->write_str(asfd, CMD_GEN, reqstr)
100 	  || asfd_read_expect(asfd, CMD_GEN, repstr));
101 }
102 
incexc_send_client(struct asfd * asfd,struct conf ** confs)103 int incexc_send_client(struct asfd *asfd, struct conf **confs)
104 {
105 	if(do_request_response(asfd, "incexc", "incexc ok")
106 	  || do_sends(asfd, confs, CONF_FLAG_INCEXC)
107 	  || do_request_response(asfd, "incexc end", "incexc end ok"))
108 		return -1;
109 	return 0;
110 }
111 
incexc_send_server(struct asfd * asfd,struct conf ** confs)112 int incexc_send_server(struct asfd *asfd, struct conf **confs)
113 {
114 	/* 'sincexc' and 'sincexc ok' have already been exchanged,
115 	   so go straight into doing the sends. */
116 	if(do_sends(asfd, confs, CONF_FLAG_INCEXC)
117 	  || do_request_response(asfd, "sincexc end", "sincexc end ok"))
118 		return -1;
119 	return 0;
120 }
121 
incexc_send_server_restore(struct asfd * asfd,struct conf ** confs)122 int incexc_send_server_restore(struct asfd *asfd, struct conf **confs)
123 {
124 	/* 'srestore' and 'srestore ok' have already been exchanged,
125 	   so go straight into doing the sends. */
126 	if(do_sends(asfd, confs, CONF_FLAG_INCEXC_RESTORE)
127 	  || do_request_response(asfd, "srestore end", "srestore end ok"))
128 		return -1;
129 	return 0;
130 }
131