2 * The Submit Daemon: High-Level Part of the Protocol
4 * (c) 2007 Martin Mares <mj@ucw.cz>
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"
18 /*** REQUESTS AND REPLIES ***/
21 read_error_cb(struct obj_read_state *st UNUSED, byte *msg)
23 client_error("Request parse error: %s", msg);
27 read_request(struct conn *c)
32 c->pool = mp_new(1024);
33 c->request = obj_new(c->pool);
34 c->reply = obj_new(c->pool);
36 struct obj_read_state st;
37 obj_read_start(&st, c->request);
38 st.error_callback = read_error_cb;
43 int l = bgets_nodie(&c->rx_fb, line, sizeof(line));
45 client_error("Request line too long");
51 client_error("Truncated request");
56 if (size >= max_request_size)
57 client_error("Request too long");
58 obj_read_attr(&st, line[0], line+1);
65 write_reply(struct conn *c)
67 if (!obj_find_attr(c->reply, '-') && !obj_find_attr(c->reply, '+'))
68 obj_set_attr(c->reply, '+', "OK");
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);
77 log(L_DEBUG, ">> ???");
79 obj_write(&c->tx_fb, c->reply, BUCKET_TYPE_PLAIN);
80 bputc(&c->tx_fb, '\n');
85 err(struct conn *c, byte *msg)
87 obj_set_attr(c->reply, '-', msg);
93 copy_attrs(struct odes *dest, struct odes *src)
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);
101 cmd_status(struct conn *c)
103 uns verbose = obj_find_anum(c->request, 'V', 0);
106 CLIST_FOR_EACH(struct task *, t, task_list)
108 struct odes *to = task_status_find_task(c, t, 1);
109 struct odes *tr = obj_add_son(c->reply, 'T' + OBJ_ATTR_SON);
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)
115 struct odes *po = task_status_find_part(to, p->s, 1);
116 struct odes *pr = obj_add_son(tr, 'P' + OBJ_ATTR_SON);
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)
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);
132 static struct fastbuf *
133 read_attachment(struct conn *c)
135 uns size = obj_find_anum(c->request, 'S', 0);
136 if (size > max_attachment_size)
138 err(c, "Submission too large");
141 obj_set_attr(c->reply, '+', "Go on");
143 obj_set_attr(c->reply, '+', NULL);
145 // This is less efficient than bbcopy(), but we want our own error handling.
146 struct fastbuf *fb = bopen_tmp(4096);
151 uns cnt = bread(&c->rx_fb, buf, MIN(remains, (uns)sizeof(buf)));
155 client_error("Truncated attachment");
157 bwrite(fb, buf, cnt);
165 cmd_submit(struct conn *c)
167 byte *tname = obj_find_aval(c->request, 'T');
170 err(c, "No task specified");
173 struct task *task = task_find(tname);
176 err(c, "No such task");
180 byte *pname = obj_find_aval(c->request, 'P');
183 simp_node *s = clist_head(&task->parts);
187 else if (!part_exists_p(task, pname))
189 err(c, "No such task part");
193 byte *ext = obj_find_aval(c->request, 'X');
194 if (!ext || !ext_exists_p(task, ext))
196 err(c, "Missing or invalid extension");
200 struct fastbuf *fb = read_attachment(c);
204 // FIXME: Check contest time
205 // FIXME: Keep history of submitted tasks
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);
212 uns replaced_ver = 0;
213 for (struct oattr *a = obj_find_attr(parto, 'V' + OBJ_ATTR_SON); a; a=a->same)
215 uns ver = obj_find_anum(a->son, 'V', 0);
216 byte *ext = obj_find_aval(a->son, 'X');
218 last_ver = MAX(last_ver, ver);
219 if (ver == current_ver)
221 task_delete_part(c->user, tname, pname, ext, ver);
222 obj_set_attr(a->son, 'S', "replaced");
223 replaced_ver = current_ver;
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);
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);
237 log(L_INFO, "User %s submitted task %s%s (version %d%s)",
239 (strcmp(tname, pname) ? stk_printf("/%s", pname) : ""),
241 (replaced_ver ? stk_printf(", replaced %d", replaced_ver) : ""));
244 /*** COMMAND MUX ***/
247 execute_command(struct conn *c)
249 byte *cmd = obj_find_aval(c->request, '!');
252 err(c, "Missing command");
256 log(L_DEBUG, "<< %s", cmd);
257 if (!strcasecmp(cmd, "SUBMIT"))
259 else if (!strcasecmp(cmd, "STATUS"))
262 err(c, "Unknown command");
266 process_command(struct conn *c)
268 if (!read_request(c))
275 /*** INITIAL HANDSHAKE ***/
278 execute_init(struct conn *c)
280 byte *user = obj_find_aval(c->request, 'U');
283 err(c, "Missing user");
287 !strcmp(user, c->cert_name) ||
288 c->rule->allow_admin && !strcmp(c->cert_name, "admin"))
290 if (!user_exists_p(user))
292 err(c, "Unknown user");
295 log(L_INFO, "Logged in %s", user);
299 err(c, "Permission denied");
300 log(L_ERROR, "Unauthorized attempt to log in as %s", user);
303 c->user = xstrdup(user);
307 process_init(struct conn *c)
309 if (!read_request(c))
313 return !obj_find_attr(c->reply, '-');