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