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)
66 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
68 json_write(c->json, &fb, c->reply);
69 len = fbbuf_count_written(&fb);
73 msg(L_ERROR, "Unable to construct reply, it is probably too long");
75 fbbuf_init_write(&fb2, packet_buffer, MAX_PACKET_SIZE);
76 bputs(&fb2, "{ \"error\": \"Reply too long\" }\n");
77 len = fbbuf_count_written(&fb2);
81 DBG("Sending reply of %d bytes", len);
82 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
84 if (errno == EAGAIN || errno == EINTR)
86 DBG("Postponed send");
87 c->socket.write_handler = socket_write_handler;
90 msg(L_ERROR, "Client write error: %m");
96 c->socket.read_handler = socket_read_handler;
97 c->socket.write_handler = NULL;
99 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
103 static void send_reply(struct client *c)
105 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
109 static void received_packet(struct client *c, byte *pkt, int len)
114 fbbuf_init_read(&fb, pkt, len, 0);
116 c->reply = json_new_object(c->json);
120 c->request = json_parse(c->json, &fb);
124 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
134 static int socket_write_handler(struct main_file *fi)
136 struct client *c = fi->data;
141 static int socket_read_handler(struct main_file *fi)
143 struct client *c = fi->data;
146 .iov_base = packet_buffer,
147 .iov_len = MAX_PACKET_SIZE,
153 .msg_control = oob_data_buffer,
154 .msg_controllen = MAX_OOB_DATA_SIZE,
157 ssize_t len = recvmsg(fi->fd, &mh, 0);
160 if (errno != EAGAIN && errno != EINTR)
161 msg(L_ERROR, "Socket read: %m");
171 struct ucred *cred = NULL;
172 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
174 if (cm->cmsg_level == SOL_SOCKET)
176 if (cm->cmsg_type == SCM_RIGHTS)
178 // We are not interested in receiving file descriptor, but despite
179 // that they could be attached to the message. If it happens, simply
181 int *fdptr = (int *) CMSG_DATA(cm);
182 int nfd = cm->cmsg_len / sizeof(int);
183 for (int i=0; i<nfd; i++)
186 else if (cm->cmsg_type == SCM_CREDENTIALS)
188 ASSERT(cm->cmsg_len >= sizeof(cred));
189 cred = (struct ucred *) CMSG_DATA(cm);
196 msg(L_ERROR, "Dropping message with no credentials");
200 DBG("Got message from UID %d", (int) cred->uid);
203 fi->read_handler = NULL;
206 received_packet(c, packet_buffer, len);
210 static int listen_read_handler(struct main_file *fi)
212 struct sockaddr_un client;
213 socklen_t addr_len = sizeof(client);
215 int new_sk = accept(fi->fd, &client, &addr_len);
218 if (errno != EAGAIN && errno != EINTR)
219 msg(L_ERROR, "Socket accept: %m");
223 if (num_connections >= max_connections)
225 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
231 DBG("Accepted connection");
233 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
234 die("Cannot set O_NONBLOCK: %m");
236 struct mempool *mp = mp_new(4096);
237 struct client *c = mp_alloc_zero(mp, sizeof(*c));
239 c->json = json_new();
241 c->socket.fd = new_sk;
242 c->socket.read_handler = socket_read_handler;
244 file_add(&c->socket);
246 c->timer.handler = socket_timeout_handler;
248 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
253 static void init_socket(void)
255 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
257 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
259 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
260 die("Cannot set O_NONBLOCK: %m");
262 struct sockaddr_un sun;
263 sun.sun_family = AF_UNIX;
264 if (strlen(socket_path) >= sizeof(sun.sun_path))
265 die("SocketPath too long");
266 strcpy(sun.sun_path, socket_path);
268 if (unlink(socket_path) < 0 && errno != ENOENT)
269 die("Cannot unlink old socket %s: %m", socket_path);
271 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
272 die("Cannot bind to %s: %m", socket_path);
274 if (listen(sk, 64) < 0)
278 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
279 die("setsockopt(SO_PASSCRED): %m");
281 listen_socket.fd = sk;
282 listen_socket.read_handler = listen_read_handler;
283 file_add(&listen_socket);
285 if (chmod(socket_path, 0666) < 0)
286 die("Cannot chmod socket: %m");
288 msg(L_INFO, "Listening on %s", socket_path);
291 static char *zone_commit(void *z_)
293 struct auth_zone *z = z_;
295 return "A zone must have a name";
299 static struct cf_section zone_config = {
300 CF_TYPE(struct auth_zone),
301 CF_COMMIT(zone_commit),
303 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
304 CF_STRING("Description", PTR_TO(struct auth_zone, desc)),
305 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
306 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
307 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
308 CF_UINT("MaxTempValidity", PTR_TO(struct auth_zone, max_temp_validity)),
313 static struct cf_section daemon_config = {
315 CF_STRING("SocketPath", &socket_path),
316 CF_UINT("MaxConnections", &max_connections),
317 CF_LIST("Zone", &zone_list, &zone_config),
318 CF_STRING("Database", &database_name),
319 CF_STRING("TempKeyFile", &temp_key_file),
320 CF_STRING("LogStream", &log_stream_name),
325 static const struct opt_section options = {
327 OPT_HELP("A sub-authentication daemon."),
328 OPT_HELP("Usage: subauthd [options]"),
330 OPT_HELP("Options:"),
337 int main(int argc UNUSED, char **argv)
341 cf_def_file = CONFIG_DIR "/subauthd";
342 cf_declare_section("SubauthD", &daemon_config, 0);
343 opt_parse(&options, argv+1);
346 log_configured(log_stream_name);