Line data Source code
1 : #include "../burp.h"
2 : #include "../alloc.h"
3 : #include "../asfd.h"
4 : #include "../async.h"
5 : #include "../cmd.h"
6 : #include "../cntr.h"
7 : #include "../conf.h"
8 : #include "../cstat.h"
9 : #include "../handy.h"
10 : #include "../iobuf.h"
11 : #include "../log.h"
12 : #include "../prepend.h"
13 : #include "../run_script.h"
14 : #include "extra_comms.h"
15 : #include "monitor/status_server.h"
16 : #include "run_action.h"
17 : #include "child.h"
18 :
19 : static struct asfd *wasfd=NULL;
20 :
21 218120 : int write_status(enum cntr_status cntr_status,
22 : const char *path, struct cntr *cntr)
23 : {
24 218120 : time_t now=0;
25 218120 : time_t diff=0;
26 : static time_t lasttime=0;
27 : static size_t l=0;
28 : static struct iobuf *wbuf=NULL;
29 :
30 218120 : if(!wasfd) return 0;
31 0 : if(!cntr || !cntr->bno)
32 : return 0;
33 :
34 : // Only update every 2 seconds.
35 0 : now=time(NULL);
36 0 : diff=now-lasttime;
37 0 : if(diff<2)
38 : {
39 : // Might as well do this in case they fiddled their
40 : // clock back in time.
41 0 : if(diff<0) lasttime=now;
42 : return 0;
43 : }
44 0 : lasttime=now;
45 :
46 : // Only get a new string if we did not manage to write the previous
47 : // one.
48 0 : if(!l)
49 : {
50 0 : cntr->cntr_status=cntr_status;
51 0 : if(!(l=cntr_to_str(cntr, path))) goto error;
52 0 : if(!wbuf && !(wbuf=iobuf_alloc())) goto error;
53 0 : iobuf_set(wbuf, CMD_APPEND, cntr->str, l);
54 : }
55 :
56 0 : switch(wasfd->append_all_to_write_buffer(wasfd, wbuf))
57 : {
58 : case APPEND_OK:
59 0 : l=0; // Fall through.
60 : case APPEND_BLOCKED:
61 : return 0;
62 : default:
63 : break;
64 : }
65 : error:
66 0 : iobuf_free(&wbuf);
67 0 : return -1;
68 : }
69 :
70 0 : static int run_server_script(struct asfd *asfd,
71 : const char *pre_or_post,
72 : const char *action_from_client,
73 : const char *script, struct strlist *script_arg,
74 : uint8_t notify, struct conf **cconfs, int backup_ret, int timer_ret)
75 : {
76 0 : int a=0;
77 0 : int ret=0;
78 0 : char *logbuf=NULL;
79 : const char *args[12];
80 0 : const char *cname=get_string(cconfs[OPT_CNAME]);
81 :
82 0 : args[a++]=script;
83 0 : args[a++]=pre_or_post;
84 0 : args[a++]=action_from_client;
85 0 : args[a++]=cname;
86 0 : args[a++]=backup_ret?"1":"0", // Indicate success or failure.
87 : // Indicate whether the timer script said OK or not.
88 0 : args[a++]=timer_ret?"1":"0",
89 0 : args[a++]=NULL;
90 :
91 : // Do not have a client storage directory, so capture the
92 : // output in a buffer to pass to the notification script.
93 0 : if(run_script_to_buf(asfd, args, script_arg, cconfs, 1, 1, 0, &logbuf))
94 : {
95 : char msg[256];
96 0 : snprintf(msg, sizeof(msg),
97 : "server %s script %s returned an error",
98 : pre_or_post, script);
99 0 : log_and_send(asfd, msg);
100 0 : ret=-1;
101 0 : if(!notify) goto end;
102 :
103 0 : a=0;
104 0 : args[a++]=get_string(cconfs[OPT_N_FAILURE_SCRIPT]);
105 0 : args[a++]=cname;
106 : // magic - set basedir blank and the
107 : // notify script will know to get the content
108 : // from the next argument (usually storagedir)
109 0 : args[a++]=""; // usually basedir
110 0 : args[a++]=logbuf?logbuf:""; //usually storagedir
111 0 : args[a++]=""; // usually file
112 0 : args[a++]=""; // usually brv
113 0 : args[a++]=""; // usually warnings
114 0 : args[a++]=NULL;
115 0 : run_script(asfd, args, get_strlist(cconfs[OPT_N_FAILURE_ARG]),
116 : cconfs, 1, 1, 0);
117 : }
118 : end:
119 0 : free_w(&logbuf);
120 0 : return ret;
121 : }
122 :
123 0 : static char *get_action_from_client(const char *buf)
124 : {
125 0 : char *cp=NULL;
126 0 : char *ret=NULL;
127 :
128 0 : if(buf)
129 : {
130 0 : if(!strcmp(buf, "backupphase1"))
131 0 : return strdup_w("backup", __func__);
132 0 : if(!strcmp(buf, "backupphase1timed"))
133 0 : return strdup_w("backup_timed", __func__);
134 : }
135 :
136 0 : if(!(ret=strdup_w(buf?buf:"", __func__)))
137 : return NULL;
138 0 : if((cp=strchr(ret, ' ')))
139 0 : *cp='\0';
140 : return ret;
141 : }
142 :
143 0 : int child(struct async *as, int is_status_server,
144 : int status_wfd, struct conf **confs, struct conf **cconfs)
145 : {
146 0 : int ret=-1;
147 0 : int srestore=0;
148 0 : int timer_ret=0;
149 0 : char *incexc=NULL;
150 0 : char *action_from_client=NULL;
151 : const char *confs_user;
152 : const char *cconfs_user;
153 : const char *confs_group;
154 : const char *cconfs_group;
155 : const char *s_script_pre;
156 : const char *s_script_post;
157 :
158 : // If we are not a status server, we are a normal child - set up the
159 : // parent socket to write status to.
160 0 : if(status_wfd>0)
161 : {
162 0 : if(!(wasfd=setup_asfd(as, "child status pipe", &status_wfd,
163 : /*listen*/"")))
164 : goto end;
165 0 : wasfd->attempt_reads=0;
166 : }
167 :
168 : /* Has to be before the chuser/chgrp stuff to allow clients to switch
169 : to different clients when both clients have different user/group
170 : settings. */
171 0 : if(extra_comms(as, &incexc, &srestore, confs, cconfs))
172 : {
173 0 : log_and_send(as->asfd, "running extra comms failed on server");
174 0 : goto end;
175 : }
176 :
177 : // Needs to happen after extra_comms, in case extra_comms resets them.
178 0 : confs_user=get_string(confs[OPT_USER]);
179 0 : cconfs_user=get_string(cconfs[OPT_USER]);
180 0 : confs_group=get_string(confs[OPT_GROUP]);
181 0 : cconfs_group=get_string(cconfs[OPT_GROUP]);
182 :
183 : /* Now that the client conf is loaded, we might want to chuser or
184 : chgrp.
185 : The main process could have already done this, so we don't want
186 : to try doing it again if cconfs has the same values, because it
187 : will fail. */
188 0 : if( (!confs_user || (cconfs_user && strcmp(confs_user, cconfs_user)))
189 0 : ||(!confs_group ||(cconfs_group && strcmp(confs_group,cconfs_group))))
190 : {
191 0 : if(chuser_and_or_chgrp(cconfs_user, cconfs_group))
192 : {
193 0 : log_and_send(as->asfd,
194 : "chuser_and_or_chgrp failed on server");
195 0 : goto end;
196 : }
197 : }
198 :
199 0 : if(as->asfd->read(as->asfd)) goto end;
200 :
201 : // If this is a status server, run the status server.
202 0 : if(is_status_server)
203 : {
204 0 : ret=status_server(as, cconfs);
205 0 : goto end;
206 : }
207 :
208 0 : if(!(action_from_client=get_action_from_client(as->asfd->rbuf->buf)))
209 : goto end;
210 0 : ret=0;
211 :
212 0 : s_script_pre=get_string(cconfs[OPT_S_SCRIPT_PRE]);
213 0 : s_script_post=get_string(cconfs[OPT_S_SCRIPT_POST]);
214 :
215 : // FIX THIS: Make the script components part of a struct, and just
216 : // pass in the correct struct. Same below.
217 :
218 0 : if(s_script_pre)
219 0 : ret=run_server_script(as->asfd, "pre", action_from_client,
220 : s_script_pre,
221 : get_strlist(cconfs[OPT_S_SCRIPT_PRE_ARG]),
222 0 : get_int(cconfs[OPT_S_SCRIPT_PRE_NOTIFY]),
223 : cconfs, ret, timer_ret);
224 :
225 0 : if(!ret)
226 0 : ret=run_action_server(as, incexc, srestore, &timer_ret, cconfs);
227 :
228 0 : if((!ret || get_int(cconfs[OPT_S_SCRIPT_POST_RUN_ON_FAIL]))
229 0 : && s_script_post)
230 0 : ret=run_server_script(as->asfd, "post", action_from_client,
231 : s_script_post,
232 : get_strlist(cconfs[OPT_S_SCRIPT_POST_ARG]),
233 0 : get_int(cconfs[OPT_S_SCRIPT_POST_NOTIFY]),
234 : cconfs, ret, timer_ret);
235 :
236 : end:
237 0 : free_w(&action_from_client);
238 0 : return ret;
239 : }
|