2 * Sub-authentication Daemon
4 * (c) 2017 Martin Mares <mj@ucw.cz>
11 #include <ucw/trans.h>
15 #include <sys/socket.h>
22 static char *socket_path = "subauthd.socket";
23 static uint max_connections = ~0U;
25 char *database_name = "subauthd.db";
27 static struct main_file listen_socket;
28 static uint num_connections;
30 static byte packet_buffer[MAX_PACKET_SIZE];
31 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
33 static int socket_read_handler(struct main_file *fi);
34 static int socket_write_handler(struct main_file *fi);
36 static void client_close(struct client *c)
38 msg(L_INFO, "Closing connection");
47 static void socket_timeout_handler(struct main_timer *tm)
49 struct client *c = tm->data;
50 msg(L_INFO, "Client timeout");
54 static void try_send_reply(struct client *c)
57 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
61 json_write(c->json, &fb, c->reply);
65 msg(L_ERROR, "Unable to construct reply, it is probably too long");
66 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
67 bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
71 int len = fbbuf_count_written(&fb);
72 msg(L_INFO, "Sending reply of %d bytes", len);
73 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
75 if (errno == EAGAIN || errno == EINTR)
77 msg(L_INFO, "Postponed send");
78 c->socket.write_handler = socket_write_handler;
81 msg(L_ERROR, "Client write error: %m");
86 msg(L_INFO, "Reply sent");
87 c->socket.read_handler = socket_read_handler;
88 c->socket.write_handler = NULL;
90 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
94 static void send_reply(struct client *c)
96 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
100 static void received_packet(struct client *c, byte *pkt, int len)
105 fbbuf_init_read(&fb, pkt, len, 0);
107 c->reply = json_new_object(c->json);
111 c->request = json_parse(c->json, &fb);
115 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
125 static int socket_write_handler(struct main_file *fi)
127 struct client *c = fi->data;
132 static int socket_read_handler(struct main_file *fi)
134 struct client *c = fi->data;
137 .iov_base = packet_buffer,
138 .iov_len = MAX_PACKET_SIZE,
144 .msg_control = oob_data_buffer,
145 .msg_controllen = MAX_OOB_DATA_SIZE,
148 ssize_t len = recvmsg(fi->fd, &mh, 0);
151 if (errno != EAGAIN && errno != EINTR)
152 msg(L_ERROR, "Socket read: %m");
162 struct ucred *cred = NULL;
163 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
165 if (cm->cmsg_level == SOL_SOCKET)
167 if (cm->cmsg_type == SCM_RIGHTS)
169 // We are not interested in receiving file descriptor, but despite
170 // that they could be attached to the message. If it happens, simply
172 int *fdptr = (int *) CMSG_DATA(cm);
173 int nfd = cm->cmsg_len / sizeof(int);
174 for (int i=0; i<nfd; i++)
177 else if (cm->cmsg_type == SCM_CREDENTIALS)
179 ASSERT(cm->cmsg_len >= sizeof(cred));
180 cred = (struct ucred *) CMSG_DATA(cm);
187 msg(L_ERROR, "Dropping message with no credentials");
191 msg(L_INFO, "Got message from UID %d", (int) cred->uid);
194 fi->read_handler = NULL;
197 received_packet(c, packet_buffer, len);
201 static int listen_read_handler(struct main_file *fi)
203 struct sockaddr_un client;
204 socklen_t addr_len = sizeof(client);
206 int new_sk = accept(fi->fd, &client, &addr_len);
209 if (errno != EAGAIN && errno != EINTR)
210 msg(L_ERROR, "Socket accept: %m");
214 if (num_connections >= max_connections)
216 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
222 msg(L_INFO, "Accepted connection");
224 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
225 die("Cannot set O_NONBLOCK: %m");
227 struct client *c = xmalloc_zero(sizeof(*c));
228 c->json = json_new();
230 c->socket.fd = new_sk;
231 c->socket.read_handler = socket_read_handler;
233 file_add(&c->socket);
235 c->timer.handler = socket_timeout_handler;
237 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
242 static void init_socket(void)
244 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
246 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
248 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
249 die("Cannot set O_NONBLOCK: %m");
251 struct sockaddr_un sun;
252 sun.sun_family = AF_UNIX;
253 if (strlen(socket_path) >= sizeof(sun.sun_path))
254 die("SocketPath too long");
255 strcpy(sun.sun_path, socket_path);
257 if (unlink(socket_path) < 0 && errno != ENOENT)
258 die("Cannot unlink old socket %s: %m", socket_path);
260 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
261 die("Cannot bind to %s: %m", socket_path);
263 if (listen(sk, 64) < 0)
267 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
268 die("setsockopt(SO_PASSCRED): %m");
270 listen_socket.fd = sk;
271 listen_socket.read_handler = listen_read_handler;
272 file_add(&listen_socket);
274 msg(L_INFO, "Listening on %s", socket_path);
277 static char *zone_commit(void *z_)
279 struct auth_zone *z = z_;
281 return "A zone must have a name";
285 static struct cf_section zone_config = {
286 CF_TYPE(struct auth_zone),
287 CF_COMMIT(zone_commit),
289 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
290 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
291 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
292 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
297 static struct cf_section daemon_config = {
299 CF_STRING("SocketPath", &socket_path),
300 CF_UINT("MaxConnections", &max_connections),
301 CF_LIST("Zone", &zone_list, &zone_config),
302 CF_STRING("Database", &database_name),
307 static const struct opt_section options = {
309 OPT_HELP("A sub-authentication daemon."),
310 OPT_HELP("Usage: subauthd [options]"),
312 OPT_HELP("Options:"),
319 int main(int argc UNUSED, char **argv)
321 cf_def_file = CONFIG_DIR "/subauthd";
322 cf_declare_section("SubauthD", &daemon_config, 0);
323 opt_parse(&options, argv+1);