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 char *log_stream_name;
32 static struct main_file listen_socket;
33 static uint num_connections;
35 static byte packet_buffer[MAX_PACKET_SIZE];
36 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
38 static int socket_read_handler(struct main_file *fi);
39 static int socket_write_handler(struct main_file *fi);
41 static void client_close(struct client *c)
43 DBG("Closing connection");
48 mp_delete(c->pool); // This includes the connection structure
52 static void socket_timeout_handler(struct main_timer *tm)
54 struct client *c = tm->data;
55 msg(L_INFO, "Client timeout");
59 static void try_send_reply(struct client *c)
62 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
66 json_write(c->json, &fb, c->reply);
70 msg(L_ERROR, "Unable to construct reply, it is probably too long");
71 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
72 bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
76 int len = fbbuf_count_written(&fb);
77 DBG("Sending reply of %d bytes", len);
78 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
80 if (errno == EAGAIN || errno == EINTR)
82 DBG("Postponed send");
83 c->socket.write_handler = socket_write_handler;
86 msg(L_ERROR, "Client write error: %m");
92 c->socket.read_handler = socket_read_handler;
93 c->socket.write_handler = NULL;
95 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
99 static void send_reply(struct client *c)
101 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
105 static void received_packet(struct client *c, byte *pkt, int len)
110 fbbuf_init_read(&fb, pkt, len, 0);
112 c->reply = json_new_object(c->json);
116 c->request = json_parse(c->json, &fb);
120 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
130 static int socket_write_handler(struct main_file *fi)
132 struct client *c = fi->data;
137 static int socket_read_handler(struct main_file *fi)
139 struct client *c = fi->data;
142 .iov_base = packet_buffer,
143 .iov_len = MAX_PACKET_SIZE,
149 .msg_control = oob_data_buffer,
150 .msg_controllen = MAX_OOB_DATA_SIZE,
153 ssize_t len = recvmsg(fi->fd, &mh, 0);
156 if (errno != EAGAIN && errno != EINTR)
157 msg(L_ERROR, "Socket read: %m");
167 struct ucred *cred = NULL;
168 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
170 if (cm->cmsg_level == SOL_SOCKET)
172 if (cm->cmsg_type == SCM_RIGHTS)
174 // We are not interested in receiving file descriptor, but despite
175 // that they could be attached to the message. If it happens, simply
177 int *fdptr = (int *) CMSG_DATA(cm);
178 int nfd = cm->cmsg_len / sizeof(int);
179 for (int i=0; i<nfd; i++)
182 else if (cm->cmsg_type == SCM_CREDENTIALS)
184 ASSERT(cm->cmsg_len >= sizeof(cred));
185 cred = (struct ucred *) CMSG_DATA(cm);
192 msg(L_ERROR, "Dropping message with no credentials");
196 DBG("Got message from UID %d", (int) cred->uid);
199 fi->read_handler = NULL;
202 received_packet(c, packet_buffer, len);
206 static int listen_read_handler(struct main_file *fi)
208 struct sockaddr_un client;
209 socklen_t addr_len = sizeof(client);
211 int new_sk = accept(fi->fd, &client, &addr_len);
214 if (errno != EAGAIN && errno != EINTR)
215 msg(L_ERROR, "Socket accept: %m");
219 if (num_connections >= max_connections)
221 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
227 DBG("Accepted connection");
229 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
230 die("Cannot set O_NONBLOCK: %m");
232 struct mempool *mp = mp_new(4096);
233 struct client *c = mp_alloc_zero(mp, sizeof(*c));
235 c->json = json_new();
237 c->socket.fd = new_sk;
238 c->socket.read_handler = socket_read_handler;
240 file_add(&c->socket);
242 c->timer.handler = socket_timeout_handler;
244 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
249 static void init_socket(void)
251 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
253 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
255 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
256 die("Cannot set O_NONBLOCK: %m");
258 struct sockaddr_un sun;
259 sun.sun_family = AF_UNIX;
260 if (strlen(socket_path) >= sizeof(sun.sun_path))
261 die("SocketPath too long");
262 strcpy(sun.sun_path, socket_path);
264 if (unlink(socket_path) < 0 && errno != ENOENT)
265 die("Cannot unlink old socket %s: %m", socket_path);
267 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
268 die("Cannot bind to %s: %m", socket_path);
270 if (listen(sk, 64) < 0)
274 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
275 die("setsockopt(SO_PASSCRED): %m");
277 listen_socket.fd = sk;
278 listen_socket.read_handler = listen_read_handler;
279 file_add(&listen_socket);
281 if (chmod(socket_path, 0666) < 0)
282 die("Cannot chmod socket: %m");
284 msg(L_INFO, "Listening on %s", socket_path);
287 static char *zone_commit(void *z_)
289 struct auth_zone *z = z_;
291 return "A zone must have a name";
295 static struct cf_section zone_config = {
296 CF_TYPE(struct auth_zone),
297 CF_COMMIT(zone_commit),
299 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
300 CF_STRING("Description", PTR_TO(struct auth_zone, desc)),
301 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
302 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
303 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
304 CF_UINT("MaxTempValidity", PTR_TO(struct auth_zone, max_temp_validity)),
309 static struct cf_section daemon_config = {
311 CF_STRING("SocketPath", &socket_path),
312 CF_UINT("MaxConnections", &max_connections),
313 CF_LIST("Zone", &zone_list, &zone_config),
314 CF_STRING("Database", &database_name),
315 CF_STRING("TempKeyFile", &temp_key_file),
316 CF_STRING("LogStream", &log_stream_name),
321 static const struct opt_section options = {
323 OPT_HELP("A sub-authentication daemon."),
324 OPT_HELP("Usage: subauthd [options]"),
326 OPT_HELP("Options:"),
333 int main(int argc UNUSED, char **argv)
337 cf_def_file = CONFIG_DIR "/subauthd";
338 cf_declare_section("SubauthD", &daemon_config, 0);
339 opt_parse(&options, argv+1);
342 log_configured(log_stream_name);