2 * Sub-authentication Daemon
4 * (c) 2017 Martin Mares <mj@ucw.cz>
13 #include <ucw/trans.h>
17 #include <sys/socket.h>
24 static char *socket_path = "subauthd.socket";
25 static uint max_connections = ~0U;
27 char *database_name = "subauthd.db";
29 static struct main_file listen_socket;
30 static uint num_connections;
32 static byte packet_buffer[MAX_PACKET_SIZE];
33 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
35 static int socket_read_handler(struct main_file *fi);
36 static int socket_write_handler(struct main_file *fi);
38 static void client_close(struct client *c)
40 DBG("Closing connection");
49 static void socket_timeout_handler(struct main_timer *tm)
51 struct client *c = tm->data;
52 msg(L_INFO, "Client timeout");
56 static void try_send_reply(struct client *c)
59 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
63 json_write(c->json, &fb, c->reply);
67 msg(L_ERROR, "Unable to construct reply, it is probably too long");
68 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
69 bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
73 int len = fbbuf_count_written(&fb);
74 DBG("Sending reply of %d bytes", len);
75 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
77 if (errno == EAGAIN || errno == EINTR)
79 DBG("Postponed send");
80 c->socket.write_handler = socket_write_handler;
83 msg(L_ERROR, "Client write error: %m");
89 c->socket.read_handler = socket_read_handler;
90 c->socket.write_handler = NULL;
92 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
96 static void send_reply(struct client *c)
98 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
102 static void received_packet(struct client *c, byte *pkt, int len)
107 fbbuf_init_read(&fb, pkt, len, 0);
109 c->reply = json_new_object(c->json);
113 c->request = json_parse(c->json, &fb);
117 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
127 static int socket_write_handler(struct main_file *fi)
129 struct client *c = fi->data;
134 static int socket_read_handler(struct main_file *fi)
136 struct client *c = fi->data;
139 .iov_base = packet_buffer,
140 .iov_len = MAX_PACKET_SIZE,
146 .msg_control = oob_data_buffer,
147 .msg_controllen = MAX_OOB_DATA_SIZE,
150 ssize_t len = recvmsg(fi->fd, &mh, 0);
153 if (errno != EAGAIN && errno != EINTR)
154 msg(L_ERROR, "Socket read: %m");
164 struct ucred *cred = NULL;
165 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
167 if (cm->cmsg_level == SOL_SOCKET)
169 if (cm->cmsg_type == SCM_RIGHTS)
171 // We are not interested in receiving file descriptor, but despite
172 // that they could be attached to the message. If it happens, simply
174 int *fdptr = (int *) CMSG_DATA(cm);
175 int nfd = cm->cmsg_len / sizeof(int);
176 for (int i=0; i<nfd; i++)
179 else if (cm->cmsg_type == SCM_CREDENTIALS)
181 ASSERT(cm->cmsg_len >= sizeof(cred));
182 cred = (struct ucred *) CMSG_DATA(cm);
189 msg(L_ERROR, "Dropping message with no credentials");
193 DBG("Got message from UID %d", (int) cred->uid);
196 fi->read_handler = NULL;
199 received_packet(c, packet_buffer, len);
203 static int listen_read_handler(struct main_file *fi)
205 struct sockaddr_un client;
206 socklen_t addr_len = sizeof(client);
208 int new_sk = accept(fi->fd, &client, &addr_len);
211 if (errno != EAGAIN && errno != EINTR)
212 msg(L_ERROR, "Socket accept: %m");
216 if (num_connections >= max_connections)
218 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
224 DBG("Accepted connection");
226 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
227 die("Cannot set O_NONBLOCK: %m");
229 struct client *c = xmalloc_zero(sizeof(*c));
230 c->json = json_new();
232 c->socket.fd = new_sk;
233 c->socket.read_handler = socket_read_handler;
235 file_add(&c->socket);
237 c->timer.handler = socket_timeout_handler;
239 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
244 static void init_socket(void)
246 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
248 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
250 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
251 die("Cannot set O_NONBLOCK: %m");
253 struct sockaddr_un sun;
254 sun.sun_family = AF_UNIX;
255 if (strlen(socket_path) >= sizeof(sun.sun_path))
256 die("SocketPath too long");
257 strcpy(sun.sun_path, socket_path);
259 if (unlink(socket_path) < 0 && errno != ENOENT)
260 die("Cannot unlink old socket %s: %m", socket_path);
262 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
263 die("Cannot bind to %s: %m", socket_path);
265 if (listen(sk, 64) < 0)
269 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
270 die("setsockopt(SO_PASSCRED): %m");
272 listen_socket.fd = sk;
273 listen_socket.read_handler = listen_read_handler;
274 file_add(&listen_socket);
276 msg(L_INFO, "Listening on %s", socket_path);
279 static char *zone_commit(void *z_)
281 struct auth_zone *z = z_;
283 return "A zone must have a name";
287 static struct cf_section zone_config = {
288 CF_TYPE(struct auth_zone),
289 CF_COMMIT(zone_commit),
291 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
292 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
293 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
294 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
299 static struct cf_section daemon_config = {
301 CF_STRING("SocketPath", &socket_path),
302 CF_UINT("MaxConnections", &max_connections),
303 CF_LIST("Zone", &zone_list, &zone_config),
304 CF_STRING("Database", &database_name),
309 static const struct opt_section options = {
311 OPT_HELP("A sub-authentication daemon."),
312 OPT_HELP("Usage: subauthd [options]"),
314 OPT_HELP("Options:"),
321 int main(int argc UNUSED, char **argv)
323 cf_def_file = CONFIG_DIR "/subauthd";
324 cf_declare_section("SubauthD", &daemon_config, 0);
325 opt_parse(&options, argv+1);