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";
30 static struct main_file listen_socket;
31 static uint num_connections;
33 static byte packet_buffer[MAX_PACKET_SIZE];
34 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
36 static int socket_read_handler(struct main_file *fi);
37 static int socket_write_handler(struct main_file *fi);
39 static void client_close(struct client *c)
41 DBG("Closing connection");
50 static void socket_timeout_handler(struct main_timer *tm)
52 struct client *c = tm->data;
53 msg(L_INFO, "Client timeout");
57 static void try_send_reply(struct client *c)
60 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
64 json_write(c->json, &fb, c->reply);
68 msg(L_ERROR, "Unable to construct reply, it is probably too long");
69 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
70 bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
74 int len = fbbuf_count_written(&fb);
75 DBG("Sending reply of %d bytes", len);
76 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
78 if (errno == EAGAIN || errno == EINTR)
80 DBG("Postponed send");
81 c->socket.write_handler = socket_write_handler;
84 msg(L_ERROR, "Client write error: %m");
90 c->socket.read_handler = socket_read_handler;
91 c->socket.write_handler = NULL;
93 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
97 static void send_reply(struct client *c)
99 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
103 static void received_packet(struct client *c, byte *pkt, int len)
108 fbbuf_init_read(&fb, pkt, len, 0);
110 c->reply = json_new_object(c->json);
114 c->request = json_parse(c->json, &fb);
118 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
128 static int socket_write_handler(struct main_file *fi)
130 struct client *c = fi->data;
135 static int socket_read_handler(struct main_file *fi)
137 struct client *c = fi->data;
140 .iov_base = packet_buffer,
141 .iov_len = MAX_PACKET_SIZE,
147 .msg_control = oob_data_buffer,
148 .msg_controllen = MAX_OOB_DATA_SIZE,
151 ssize_t len = recvmsg(fi->fd, &mh, 0);
154 if (errno != EAGAIN && errno != EINTR)
155 msg(L_ERROR, "Socket read: %m");
165 struct ucred *cred = NULL;
166 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
168 if (cm->cmsg_level == SOL_SOCKET)
170 if (cm->cmsg_type == SCM_RIGHTS)
172 // We are not interested in receiving file descriptor, but despite
173 // that they could be attached to the message. If it happens, simply
175 int *fdptr = (int *) CMSG_DATA(cm);
176 int nfd = cm->cmsg_len / sizeof(int);
177 for (int i=0; i<nfd; i++)
180 else if (cm->cmsg_type == SCM_CREDENTIALS)
182 ASSERT(cm->cmsg_len >= sizeof(cred));
183 cred = (struct ucred *) CMSG_DATA(cm);
190 msg(L_ERROR, "Dropping message with no credentials");
194 DBG("Got message from UID %d", (int) cred->uid);
197 fi->read_handler = NULL;
200 received_packet(c, packet_buffer, len);
204 static int listen_read_handler(struct main_file *fi)
206 struct sockaddr_un client;
207 socklen_t addr_len = sizeof(client);
209 int new_sk = accept(fi->fd, &client, &addr_len);
212 if (errno != EAGAIN && errno != EINTR)
213 msg(L_ERROR, "Socket accept: %m");
217 if (num_connections >= max_connections)
219 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
225 DBG("Accepted connection");
227 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
228 die("Cannot set O_NONBLOCK: %m");
230 struct client *c = xmalloc_zero(sizeof(*c));
231 c->json = json_new();
233 c->socket.fd = new_sk;
234 c->socket.read_handler = socket_read_handler;
236 file_add(&c->socket);
238 c->timer.handler = socket_timeout_handler;
240 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
245 static void init_socket(void)
247 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
249 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
251 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
252 die("Cannot set O_NONBLOCK: %m");
254 struct sockaddr_un sun;
255 sun.sun_family = AF_UNIX;
256 if (strlen(socket_path) >= sizeof(sun.sun_path))
257 die("SocketPath too long");
258 strcpy(sun.sun_path, socket_path);
260 if (unlink(socket_path) < 0 && errno != ENOENT)
261 die("Cannot unlink old socket %s: %m", socket_path);
263 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
264 die("Cannot bind to %s: %m", socket_path);
266 if (listen(sk, 64) < 0)
270 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
271 die("setsockopt(SO_PASSCRED): %m");
273 listen_socket.fd = sk;
274 listen_socket.read_handler = listen_read_handler;
275 file_add(&listen_socket);
277 msg(L_INFO, "Listening on %s", socket_path);
280 static char *zone_commit(void *z_)
282 struct auth_zone *z = z_;
284 return "A zone must have a name";
288 static struct cf_section zone_config = {
289 CF_TYPE(struct auth_zone),
290 CF_COMMIT(zone_commit),
292 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
293 CF_STRING("Description", PTR_TO(struct auth_zone, desc)),
294 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
295 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
296 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
301 static struct cf_section daemon_config = {
303 CF_STRING("SocketPath", &socket_path),
304 CF_UINT("MaxConnections", &max_connections),
305 CF_LIST("Zone", &zone_list, &zone_config),
306 CF_STRING("Database", &database_name),
311 static const struct opt_section options = {
313 OPT_HELP("A sub-authentication daemon."),
314 OPT_HELP("Usage: subauthd [options]"),
316 OPT_HELP("Options:"),
323 int main(int argc UNUSED, char **argv)
327 cf_def_file = CONFIG_DIR "/subauthd";
328 cf_declare_section("SubauthD", &daemon_config, 0);
329 opt_parse(&options, argv+1);