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"
18 read_error_cb(struct obj_read_state *st UNUSED, byte *msg)
20 client_error("Request parse error: %s", msg);
24 read_request(struct conn *c)
29 c->pool = mp_new(1024);
30 c->request = obj_new(c->pool);
31 c->reply = obj_new(c->pool);
33 struct obj_read_state st;
34 obj_read_start(&st, c->request);
35 st.error_callback = read_error_cb;
40 int l = bgets_nodie(&c->rx_fb, line, sizeof(line));
42 client_error("Request line too long");
48 client_error("Truncated request");
53 if (size >= max_request_size)
54 client_error("Request too long");
55 obj_read_attr(&st, line[0], line+1);
62 write_reply(struct conn *c)
67 if (msg = obj_find_aval(c->reply, '-'))
68 log(L_DEBUG, ">> -%s", msg);
69 else if (msg = obj_find_aval(c->reply, '+'))
70 log(L_DEBUG, ">> +%s", msg);
72 log(L_DEBUG, ">> ???");
74 put_attr_set_type(BUCKET_TYPE_PLAIN);
75 bput_object(&c->tx_fb, c->reply);
76 bputc(&c->tx_fb, '\n');
81 execute_command(struct conn *c)
83 byte *cmd = obj_find_aval(c->request, '!');
86 obj_set_attr(c->reply, '-', "Missing command");
90 log(L_DEBUG, "<< %s", cmd);
91 obj_set_attr(c->reply, '-', "Unknown command");
95 process_command(struct conn *c)
105 user_exists_p(byte *user)
107 byte *fn = stk_printf("solutions/%s/status", user);
109 return !stat(fn, &st) && S_ISREG(st.st_mode);
113 execute_init(struct conn *c)
115 byte *user = obj_find_aval(c->request, 'U');
118 obj_set_attr(c->reply, '-', "Missing user");
122 !strcmp(user, c->cert_name) ||
123 c->rule->allow_admin && !strcmp(c->cert_name, "admin"))
125 if (!user_exists_p(user))
127 obj_set_attr(c->reply, '-', "Unknown user");
130 log(L_INFO, "Logged in %s", user);
134 obj_set_attr(c->reply, '-', "Permission denied");
135 log(L_ERROR, "Unauthorized attempt to log in as %s", user);
138 obj_set_attr(c->reply, '+', "OK");
139 c->user = xstrdup(user);
143 process_init(struct conn *c)
145 if (!read_request(c))
149 return !!obj_find_attr(c->reply, '+');