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 "sherlock/object.h"
10 #include "sherlock/objread.h"
14 /*** REQUESTS AND REPLIES ***/
17 read_error_cb(struct obj_read_state *st UNUSED, byte *msg)
19 client_error("Request parse error: %s", msg);
23 read_request(struct conn *c)
28 c->pool = mp_new(1024);
29 c->request = obj_new(c->pool);
30 c->reply = obj_new(c->pool);
32 struct obj_read_state st;
33 obj_read_start(&st, c->request);
34 st.error_callback = read_error_cb;
39 int l = bgets_nodie(&c->rx_fb, line, sizeof(line));
41 client_error("Request line too long");
47 client_error("Truncated request");
52 if (size >= max_request_size)
53 client_error("Request too long");
54 obj_read_attr(&st, line[0], line+1);
61 write_reply(struct conn *c)
63 if (!obj_find_attr(c->reply, '-') && !obj_find_attr(c->reply, '+'))
64 obj_set_attr(c->reply, '+', "OK");
68 if (msg = obj_find_aval(c->reply, '-'))
69 log(L_DEBUG, ">> -%s", msg);
70 else if (msg = obj_find_aval(c->reply, '+'))
71 log(L_DEBUG, ">> +%s", msg);
73 log(L_DEBUG, ">> ???");
75 put_attr_set_type(BUCKET_TYPE_PLAIN);
76 bput_object(&c->tx_fb, c->reply);
77 bputc(&c->tx_fb, '\n');
82 err(struct conn *c, byte *msg)
84 obj_set_attr(c->reply, '-', msg);
90 cmd_submit(struct conn *c)
92 byte *tname = obj_find_aval(c->request, 'T');
95 err(c, "No task specified");
98 struct task *task = task_find(tname);
101 err(c, "No such task");
106 /*** COMMAND MUX ***/
109 execute_command(struct conn *c)
111 byte *cmd = obj_find_aval(c->request, '!');
114 err(c, "Missing command");
118 log(L_DEBUG, "<< %s", cmd);
119 if (!strcasecmp(cmd, "SUBMIT"))
122 err(c, "Unknown command");
126 process_command(struct conn *c)
128 if (!read_request(c))
135 /*** INITIAL HANDSHAKE ***/
138 execute_init(struct conn *c)
140 byte *user = obj_find_aval(c->request, 'U');
143 err(c, "Missing user");
147 !strcmp(user, c->cert_name) ||
148 c->rule->allow_admin && !strcmp(c->cert_name, "admin"))
150 if (!user_exists_p(user))
152 err(c, "Unknown user");
155 log(L_INFO, "Logged in %s", user);
159 err(c, "Permission denied");
160 log(L_ERROR, "Unauthorized attempt to log in as %s", user);
163 c->user = xstrdup(user);
167 process_init(struct conn *c)
169 if (!read_request(c))
173 return !obj_find_attr(c->reply, '-');