]> mj.ucw.cz Git - moe.git/blob - submit/commands.c
Minor enhancements of STATUS command.
[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 *, x, *t->extensions)
112         obj_add_attr(tr, 'A', x->s);
113       CLIST_FOR_EACH(simp_node *, p, t->parts)
114         {
115           struct odes *po = task_status_find_part(to, p->s, 1);
116           struct odes *pr = obj_add_son(tr, 'P' + OBJ_ATTR_SON);
117           copy_attrs(pr, po);
118           uns current_ver = obj_find_anum(po, 'V', 0);
119           for (struct oattr *v = obj_find_attr(po, 'V' + OBJ_ATTR_SON); v; v=v->same)
120             {
121               struct odes *vo = v->son;
122               uns ver = obj_find_anum(vo, 'V', 0);
123               if (ver == current_ver || verbose)
124                 obj_add_son_ref(pr, 'V' + OBJ_ATTR_SON, vo);
125             }
126         }
127     }
128 }
129
130 /*** SUBMIT ***/
131
132 static struct fastbuf *
133 read_attachment(struct conn *c)
134 {
135   uns size = obj_find_anum(c->request, 'S', 0);
136   if (size > max_attachment_size)
137     {
138       err(c, "Submission too large");
139       return NULL;
140     }
141   obj_set_attr(c->reply, '+', "Go on");
142   write_reply(c);
143   obj_set_attr(c->reply, '+', NULL);
144
145   // This is less efficient than bbcopy(), but we want our own error handling.
146   struct fastbuf *fb = bopen_tmp(4096);
147   byte buf[4096];
148   uns remains = size;
149   while (remains)
150     {
151       uns cnt = bread(&c->rx_fb, buf, MIN(remains, (uns)sizeof(buf)));
152       if (!cnt)
153         {
154           bclose(fb);
155           client_error("Truncated attachment");
156         }
157       bwrite(fb, buf, cnt);
158       remains -= cnt;
159     }
160   brewind(fb);
161   return fb;
162 }
163
164 static void
165 cmd_submit(struct conn *c)
166 {
167   byte *tname = obj_find_aval(c->request, 'T');
168   if (!tname)
169     {
170       err(c, "No task specified");
171       return;
172     }
173   struct task *task = task_find(tname);
174   if (!task)
175     {
176       err(c, "No such task");
177       return;
178     }
179
180   byte *pname = obj_find_aval(c->request, 'P');
181   if (!pname)
182     {
183       simp_node *s = clist_head(&task->parts);
184       ASSERT(s);
185       pname = s->s;
186     }
187   else if (!part_exists_p(task, pname))
188     {
189       err(c, "No such task part");
190       return;
191     }
192
193   byte *ext = obj_find_aval(c->request, 'X');
194   if (!ext || !ext_exists_p(task, ext))
195     {
196       err(c, "Missing or invalid extension");
197       return;
198     }
199
200   struct fastbuf *fb = read_attachment(c);
201   if (!fb)
202     return;
203
204   // FIXME: Check contest time
205   // FIXME: Keep history of submitted tasks
206
207   task_lock_status(c);
208   struct odes *tasko = task_status_find_task(c, task, 1);
209   struct odes *parto = task_status_find_part(tasko, pname, 1);
210   uns current_ver = obj_find_anum(parto, 'V', 0);
211   uns last_ver = 0;
212   uns replaced_ver = 0;
213   for (struct oattr *a = obj_find_attr(parto, 'V' + OBJ_ATTR_SON); a; a=a->same)
214     {
215       uns ver = obj_find_anum(a->son, 'V', 0);
216       byte *ext = obj_find_aval(a->son, 'X');
217       ASSERT(ver && ext);
218       last_ver = MAX(last_ver, ver);
219       if (ver == current_ver)
220         {
221           task_delete_part(c->user, tname, pname, ext, ver);
222           obj_set_attr(a->son, 'S', "replaced");
223           replaced_ver = current_ver;
224         }
225     }
226   struct odes *vero = obj_add_son(parto, 'V' + OBJ_ATTR_SON);
227   obj_set_attr_num(vero, 'V', ++last_ver);
228   obj_set_attr_num(vero, 'T', time(NULL));
229   obj_set_attr(vero, 'S', "submitted");
230   obj_set_attr(vero, 'X', ext);
231   // FIXME: hash
232   // FIXME: remove old versions from the status file?
233   task_submit_part(c->user, tname, pname, ext, last_ver, fb);
234   obj_set_attr_num(parto, 'V', last_ver);
235   task_unlock_status(c, 1);
236
237   log(L_INFO, "User %s submitted task %s%s (version %d%s)",
238         c->user, tname,
239         (strcmp(tname, pname) ? stk_printf("/%s", pname) : ""),
240         last_ver,
241         (replaced_ver ? stk_printf(", replaced %d", replaced_ver) : ""));
242 }
243
244 /*** COMMAND MUX ***/
245
246 static void
247 execute_command(struct conn *c)
248 {
249   byte *cmd = obj_find_aval(c->request, '!');
250   if (!cmd)
251     {
252       err(c, "Missing command");
253       return;
254     }
255   if (trace_commands)
256     log(L_DEBUG, "<< %s", cmd);
257   if (!strcasecmp(cmd, "SUBMIT"))
258     cmd_submit(c);
259   else if (!strcasecmp(cmd, "STATUS"))
260     cmd_status(c);
261   else
262     err(c, "Unknown command");
263 }
264
265 int
266 process_command(struct conn *c)
267 {
268   if (!read_request(c))
269     return 0;
270   execute_command(c);
271   write_reply(c);
272   return 1;
273 }
274
275 /*** INITIAL HANDSHAKE ***/
276
277 static void
278 execute_init(struct conn *c)
279 {
280   byte *user = obj_find_aval(c->request, 'U');
281   if (!user)
282     {
283       err(c, "Missing user");
284       return;
285     }
286   if (!c->cert_name ||
287       !strcmp(user, c->cert_name) ||
288       c->rule->allow_admin && !strcmp(c->cert_name, "admin"))
289     {
290       if (!user_exists_p(user))
291         {
292           err(c, "Unknown user");
293           return;
294         }
295       log(L_INFO, "Logged in %s", user);
296     }
297   else
298     {
299       err(c, "Permission denied");
300       log(L_ERROR, "Unauthorized attempt to log in as %s", user);
301       return;
302     }
303   c->user = xstrdup(user);
304 }
305
306 int
307 process_init(struct conn *c)
308 {
309   if (!read_request(c))
310     return 0;
311   execute_init(c);
312   write_reply(c);
313   return !obj_find_attr(c->reply, '-');
314 }