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/stkstring.h"
10 #include "sherlock/object.h"
11 #include "sherlock/objread.h"
17 /*** REQUESTS AND REPLIES ***/
20 read_error_cb(struct obj_read_state *st UNUSED, byte *msg)
22 client_error("Request parse error: %s", msg);
26 read_request(struct conn *c)
31 c->pool = mp_new(1024);
32 c->request = obj_new(c->pool);
33 c->reply = obj_new(c->pool);
35 struct obj_read_state st;
36 obj_read_start(&st, c->request);
37 st.error_callback = read_error_cb;
42 int l = bgets_nodie(&c->rx_fb, line, sizeof(line));
44 client_error("Request line too long");
50 client_error("Truncated request");
55 if (size >= max_request_size)
56 client_error("Request too long");
57 obj_read_attr(&st, line[0], line+1);
64 write_reply(struct conn *c)
69 if (msg = obj_find_aval(c->reply, '-'))
70 log(L_DEBUG, ">> -%s", msg);
71 else if (msg = obj_find_aval(c->reply, '+'))
72 log(L_DEBUG, ">> +%s", msg);
74 log(L_DEBUG, ">> ???");
76 put_attr_set_type(BUCKET_TYPE_PLAIN);
77 bput_object(&c->tx_fb, c->reply);
78 bputc(&c->tx_fb, '\n');
85 execute_command(struct conn *c)
87 byte *cmd = obj_find_aval(c->request, '!');
90 obj_set_attr(c->reply, '-', "Missing command");
94 log(L_DEBUG, "<< %s", cmd);
95 obj_set_attr(c->reply, '-', "Unknown command");
99 process_command(struct conn *c)
101 if (!read_request(c))
108 /*** INITIAL HANDSHAKE ***/
111 user_exists_p(byte *user)
113 byte *fn = stk_printf("solutions/%s/status", user);
115 return !stat(fn, &st) && S_ISREG(st.st_mode);
119 execute_init(struct conn *c)
121 byte *user = obj_find_aval(c->request, 'U');
124 obj_set_attr(c->reply, '-', "Missing user");
128 !strcmp(user, c->cert_name) ||
129 c->rule->allow_admin && !strcmp(c->cert_name, "admin"))
131 if (!user_exists_p(user))
133 obj_set_attr(c->reply, '-', "Unknown user");
136 log(L_INFO, "Logged in %s", user);
140 obj_set_attr(c->reply, '-', "Permission denied");
141 log(L_ERROR, "Unauthorized attempt to log in as %s", user);
144 obj_set_attr(c->reply, '+', "OK");
145 c->user = xstrdup(user);
149 process_init(struct conn *c)
151 if (!read_request(c))
155 return !!obj_find_attr(c->reply, '+');