2 * Sub-authentication Daemon
4 * (c) 2017 Martin Mares <mj@ucw.cz>
13 #include <ucw/trans.h>
17 #include <sys/socket.h>
25 static char *socket_path = "subauthd.socket";
26 static uint max_connections = ~0U;
28 char *database_name = "subauthd.db";
31 static struct main_file listen_socket;
32 static uint num_connections;
34 static byte packet_buffer[MAX_PACKET_SIZE];
35 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
37 static int socket_read_handler(struct main_file *fi);
38 static int socket_write_handler(struct main_file *fi);
40 static void client_close(struct client *c)
42 DBG("Closing connection");
47 mp_delete(c->pool); // This includes the connection structure
51 static void socket_timeout_handler(struct main_timer *tm)
53 struct client *c = tm->data;
54 msg(L_INFO, "Client timeout");
58 static void try_send_reply(struct client *c)
61 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
65 json_write(c->json, &fb, c->reply);
69 msg(L_ERROR, "Unable to construct reply, it is probably too long");
70 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
71 bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
75 int len = fbbuf_count_written(&fb);
76 DBG("Sending reply of %d bytes", len);
77 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
79 if (errno == EAGAIN || errno == EINTR)
81 DBG("Postponed send");
82 c->socket.write_handler = socket_write_handler;
85 msg(L_ERROR, "Client write error: %m");
91 c->socket.read_handler = socket_read_handler;
92 c->socket.write_handler = NULL;
94 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
98 static void send_reply(struct client *c)
100 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
104 static void received_packet(struct client *c, byte *pkt, int len)
109 fbbuf_init_read(&fb, pkt, len, 0);
111 c->reply = json_new_object(c->json);
115 c->request = json_parse(c->json, &fb);
119 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
129 static int socket_write_handler(struct main_file *fi)
131 struct client *c = fi->data;
136 static int socket_read_handler(struct main_file *fi)
138 struct client *c = fi->data;
141 .iov_base = packet_buffer,
142 .iov_len = MAX_PACKET_SIZE,
148 .msg_control = oob_data_buffer,
149 .msg_controllen = MAX_OOB_DATA_SIZE,
152 ssize_t len = recvmsg(fi->fd, &mh, 0);
155 if (errno != EAGAIN && errno != EINTR)
156 msg(L_ERROR, "Socket read: %m");
166 struct ucred *cred = NULL;
167 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
169 if (cm->cmsg_level == SOL_SOCKET)
171 if (cm->cmsg_type == SCM_RIGHTS)
173 // We are not interested in receiving file descriptor, but despite
174 // that they could be attached to the message. If it happens, simply
176 int *fdptr = (int *) CMSG_DATA(cm);
177 int nfd = cm->cmsg_len / sizeof(int);
178 for (int i=0; i<nfd; i++)
181 else if (cm->cmsg_type == SCM_CREDENTIALS)
183 ASSERT(cm->cmsg_len >= sizeof(cred));
184 cred = (struct ucred *) CMSG_DATA(cm);
191 msg(L_ERROR, "Dropping message with no credentials");
195 DBG("Got message from UID %d", (int) cred->uid);
198 fi->read_handler = NULL;
201 received_packet(c, packet_buffer, len);
205 static int listen_read_handler(struct main_file *fi)
207 struct sockaddr_un client;
208 socklen_t addr_len = sizeof(client);
210 int new_sk = accept(fi->fd, &client, &addr_len);
213 if (errno != EAGAIN && errno != EINTR)
214 msg(L_ERROR, "Socket accept: %m");
218 if (num_connections >= max_connections)
220 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
226 DBG("Accepted connection");
228 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
229 die("Cannot set O_NONBLOCK: %m");
231 struct mempool *mp = mp_new(4096);
232 struct client *c = mp_alloc_zero(mp, sizeof(*c));
234 c->json = json_new();
236 c->socket.fd = new_sk;
237 c->socket.read_handler = socket_read_handler;
239 file_add(&c->socket);
241 c->timer.handler = socket_timeout_handler;
243 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
248 static void init_socket(void)
250 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
252 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
254 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
255 die("Cannot set O_NONBLOCK: %m");
257 struct sockaddr_un sun;
258 sun.sun_family = AF_UNIX;
259 if (strlen(socket_path) >= sizeof(sun.sun_path))
260 die("SocketPath too long");
261 strcpy(sun.sun_path, socket_path);
263 if (unlink(socket_path) < 0 && errno != ENOENT)
264 die("Cannot unlink old socket %s: %m", socket_path);
266 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
267 die("Cannot bind to %s: %m", socket_path);
269 if (listen(sk, 64) < 0)
273 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
274 die("setsockopt(SO_PASSCRED): %m");
276 listen_socket.fd = sk;
277 listen_socket.read_handler = listen_read_handler;
278 file_add(&listen_socket);
280 msg(L_INFO, "Listening on %s", socket_path);
283 static char *zone_commit(void *z_)
285 struct auth_zone *z = z_;
287 return "A zone must have a name";
291 static struct cf_section zone_config = {
292 CF_TYPE(struct auth_zone),
293 CF_COMMIT(zone_commit),
295 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
296 CF_STRING("Description", PTR_TO(struct auth_zone, desc)),
297 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
298 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
299 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
300 CF_UINT("MaxTempValidity", PTR_TO(struct auth_zone, max_temp_validity)),
305 static struct cf_section daemon_config = {
307 CF_STRING("SocketPath", &socket_path),
308 CF_UINT("MaxConnections", &max_connections),
309 CF_LIST("Zone", &zone_list, &zone_config),
310 CF_STRING("Database", &database_name),
311 CF_STRING("TempKeyFile", &temp_key_file),
316 static const struct opt_section options = {
318 OPT_HELP("A sub-authentication daemon."),
319 OPT_HELP("Usage: subauthd [options]"),
321 OPT_HELP("Options:"),
328 int main(int argc UNUSED, char **argv)
332 cf_def_file = CONFIG_DIR "/subauthd";
333 cf_declare_section("SubauthD", &daemon_config, 0);
334 opt_parse(&options, argv+1);