]> mj.ucw.cz Git - moe.git/blob - submit/commands.c
93f5b83509f6edf08c86046612afd1d8c4e61ed4
[moe.git] / submit / commands.c
1 /*
2  *  The Submit Daemon: High-Level Part of the Protocol
3  *
4  *  (c) 2007 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/mempool.h"
9 #include "lib/simple-lists.h"
10 #include "lib/stkstring.h"
11 #include "sherlock/object.h"
12 #include "sherlock/objread.h"
13
14 #include <time.h>
15
16 #include "submitd.h"
17
18 /*** REQUESTS AND REPLIES ***/
19
20 static void NONRET
21 read_error_cb(struct obj_read_state *st UNUSED, byte *msg)
22 {
23   client_error("Request parse error: %s", msg);
24 }
25
26 static int
27 read_request(struct conn *c)
28 {
29   if (c->pool)
30     mp_flush(c->pool);
31   else
32     c->pool = mp_new(1024);
33   c->request = obj_new(c->pool);
34   c->reply = obj_new(c->pool);
35
36   struct obj_read_state st;
37   obj_read_start(&st, c->request);
38   st.error_callback = read_error_cb;
39   byte line[1024];
40   uns size = 0;
41   for (;;)
42     {
43       int l = bgets_nodie(&c->rx_fb, line, sizeof(line));
44       if (l < 0)
45         client_error("Request line too long");
46       if (!l)
47         {
48           if (!size)
49             return 0;
50           else
51             client_error("Truncated request");
52         }
53       if (l == 1)
54         break;
55       size += l;
56       if (size >= max_request_size)
57         client_error("Request too long");
58       obj_read_attr(&st, line[0], line+1);
59     }
60   obj_read_end(&st);
61   return 1;
62 }
63
64 static void
65 write_reply(struct conn *c)
66 {
67   if (!obj_find_attr(c->reply, '-') && !obj_find_attr(c->reply, '+'))
68     obj_set_attr(c->reply, '+', "OK");
69   if (trace_commands)
70     {
71       byte *msg;
72       if (msg = obj_find_aval(c->reply, '-'))
73         log(L_DEBUG, ">> -%s", msg);
74       else if (msg = obj_find_aval(c->reply, '+'))
75         log(L_DEBUG, ">> +%s", msg);
76       else
77         log(L_DEBUG, ">> ???");
78     }
79   obj_write(&c->tx_fb, c->reply, BUCKET_TYPE_PLAIN);
80   bputc(&c->tx_fb, '\n');
81   bflush(&c->tx_fb);
82 }
83
84 static void
85 err(struct conn *c, byte *msg)
86 {
87   obj_set_attr(c->reply, '-', msg);
88 }
89
90 /*** STATUS ***/
91
92 static void
93 copy_attrs(struct odes *dest, struct odes *src)
94 {
95   for (struct oattr *a = src->attrs ; a; a=a->next)
96     for (struct oattr *aa = a; aa; aa=aa->same)
97       obj_add_attr(dest, aa->attr, aa->val);
98 }
99
100 static void
101 cmd_status(struct conn *c)
102 {
103   uns verbose = obj_find_anum(c->request, 'V', 0);
104   task_load_status(c);
105
106   CLIST_FOR_EACH(struct task *, t, task_list)
107     {
108       struct odes *to = task_status_find_task(c, t, 1);
109       struct odes *tr = obj_add_son(c->reply, 'T' + OBJ_ATTR_SON);
110       copy_attrs(tr, to);
111       CLIST_FOR_EACH(simp_node *, p, t->parts)
112         {
113           struct odes *po = task_status_find_part(to, p->s, 1);
114           struct odes *pr = obj_add_son(tr, 'P' + OBJ_ATTR_SON);
115           copy_attrs(pr, po);
116           uns current_ver = obj_find_anum(po, 'V', 0);
117           for (struct oattr *v = obj_find_attr(po, 'V' + OBJ_ATTR_SON); v; v=v->same)
118             {
119               struct odes *vo = v->son;
120               uns ver = obj_find_anum(vo, 'V', 0);
121               if (ver == current_ver || verbose)
122                 obj_add_son_ref(pr, 'V' + OBJ_ATTR_SON, vo);
123             }
124         }
125     }
126 }
127
128 /*** SUBMIT ***/
129
130 static struct fastbuf *
131 read_attachment(struct conn *c)
132 {
133   uns size = obj_find_anum(c->request, 'S', 0);
134   if (size > max_attachment_size)
135     {
136       err(c, "Submission too large");
137       return NULL;
138     }
139   obj_set_attr(c->reply, '+', "Go on");
140   write_reply(c);
141   obj_set_attr(c->reply, '+', NULL);
142
143   // This is less efficient than bbcopy(), but we want our own error handling.
144   struct fastbuf *fb = bopen_tmp(4096);
145   byte buf[4096];
146   uns remains = size;
147   while (remains)
148     {
149       uns cnt = bread(&c->rx_fb, buf, MIN(remains, (uns)sizeof(buf)));
150       if (!cnt)
151         {
152           bclose(fb);
153           client_error("Truncated attachment");
154         }
155       bwrite(fb, buf, cnt);
156       remains -= cnt;
157     }
158   brewind(fb);
159   return fb;
160 }
161
162 static void
163 cmd_submit(struct conn *c)
164 {
165   byte *tname = obj_find_aval(c->request, 'T');
166   if (!tname)
167     {
168       err(c, "No task specified");
169       return;
170     }
171   struct task *task = task_find(tname);
172   if (!task)
173     {
174       err(c, "No such task");
175       return;
176     }
177
178   byte *pname = obj_find_aval(c->request, 'P');
179   if (!pname)
180     {
181       simp_node *s = clist_head(&task->parts);
182       ASSERT(s);
183       pname = s->s;
184     }
185   else if (!part_exists_p(task, pname))
186     {
187       err(c, "No such task part");
188       return;
189     }
190
191   byte *ext = obj_find_aval(c->request, 'X');
192   if (!ext || !ext_exists_p(task, ext))
193     {
194       err(c, "Missing or invalid extension");
195       return;
196     }
197
198   struct fastbuf *fb = read_attachment(c);
199   if (!fb)
200     return;
201
202   // FIXME: Check contest time
203   // FIXME: Keep history of submitted tasks
204
205   task_lock_status(c);
206   struct odes *tasko = task_status_find_task(c, task, 1);
207   struct odes *parto = task_status_find_part(tasko, pname, 1);
208   uns current_ver = obj_find_anum(parto, 'V', 0);
209   uns last_ver = 0;
210   uns replaced_ver = 0;
211   for (struct oattr *a = obj_find_attr(parto, 'V' + OBJ_ATTR_SON); a; a=a->same)
212     {
213       uns ver = obj_find_anum(a->son, 'V', 0);
214       byte *ext = obj_find_aval(a->son, 'X');
215       ASSERT(ver && ext);
216       last_ver = MAX(last_ver, ver);
217       if (ver == current_ver)
218         {
219           task_delete_part(c->user, tname, pname, ext, ver);
220           obj_set_attr(a->son, 'S', "replaced");
221           replaced_ver = current_ver;
222         }
223     }
224   struct odes *vero = obj_add_son(parto, 'V' + OBJ_ATTR_SON);
225   obj_set_attr_num(vero, 'V', ++last_ver);
226   obj_set_attr_num(vero, 'T', time(NULL));
227   obj_set_attr(vero, 'S', "submitted");
228   obj_set_attr(vero, 'X', ext);
229   // FIXME: hash
230   // FIXME: remove old versions from the status file?
231   task_submit_part(c->user, tname, pname, ext, last_ver, fb);
232   obj_set_attr_num(parto, 'V', last_ver);
233   task_unlock_status(c, 1);
234
235   log(L_INFO, "User %s submitted task %s%s (version %d%s)",
236         c->user, tname,
237         (strcmp(tname, pname) ? stk_printf("/%s", pname) : ""),
238         last_ver,
239         (replaced_ver ? stk_printf(", replaced %d", replaced_ver) : ""));
240 }
241
242 /*** COMMAND MUX ***/
243
244 static void
245 execute_command(struct conn *c)
246 {
247   byte *cmd = obj_find_aval(c->request, '!');
248   if (!cmd)
249     {
250       err(c, "Missing command");
251       return;
252     }
253   if (trace_commands)
254     log(L_DEBUG, "<< %s", cmd);
255   if (!strcasecmp(cmd, "SUBMIT"))
256     cmd_submit(c);
257   else if (!strcasecmp(cmd, "STATUS"))
258     cmd_status(c);
259   else
260     err(c, "Unknown command");
261 }
262
263 int
264 process_command(struct conn *c)
265 {
266   if (!read_request(c))
267     return 0;
268   execute_command(c);
269   write_reply(c);
270   return 1;
271 }
272
273 /*** INITIAL HANDSHAKE ***/
274
275 static void
276 execute_init(struct conn *c)
277 {
278   byte *user = obj_find_aval(c->request, 'U');
279   if (!user)
280     {
281       err(c, "Missing user");
282       return;
283     }
284   if (!c->cert_name ||
285       !strcmp(user, c->cert_name) ||
286       c->rule->allow_admin && !strcmp(c->cert_name, "admin"))
287     {
288       if (!user_exists_p(user))
289         {
290           err(c, "Unknown user");
291           return;
292         }
293       log(L_INFO, "Logged in %s", user);
294     }
295   else
296     {
297       err(c, "Permission denied");
298       log(L_ERROR, "Unauthorized attempt to log in as %s", user);
299       return;
300     }
301   c->user = xstrdup(user);
302 }
303
304 int
305 process_init(struct conn *c)
306 {
307   if (!read_request(c))
308     return 0;
309   execute_init(c);
310   write_reply(c);
311   return !obj_find_attr(c->reply, '-');
312 }