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;
31 static uint max_packet_size = 16384;
33 static struct main_file listen_socket;
34 static uint num_connections;
36 static byte *packet_buffer;
37 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
39 static int socket_read_handler(struct main_file *fi);
40 static int socket_write_handler(struct main_file *fi);
42 static void client_close(struct client *c)
44 DBG("Closing connection");
49 mp_delete(c->pool); // This includes the connection structure
53 static void socket_timeout_handler(struct main_timer *tm)
55 struct client *c = tm->data;
56 msg(L_INFO, "Client timeout");
60 static void try_send_reply(struct client *c)
67 fbbuf_init_write(&fb, packet_buffer, max_packet_size);
69 json_write(c->json, &fb, c->reply);
70 len = fbbuf_count_written(&fb);
74 msg(L_ERROR, "Unable to construct reply, it is probably too long");
76 fbbuf_init_write(&fb2, packet_buffer, max_packet_size);
77 bputs(&fb2, "{ \"error\": \"Reply too long\" }\n");
78 len = fbbuf_count_written(&fb2);
82 DBG("Sending reply of %d bytes", len);
83 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
85 if (errno == EAGAIN || errno == EINTR)
87 DBG("Postponed send");
88 c->socket.write_handler = socket_write_handler;
91 msg(L_ERROR, "Client write error: %m");
97 c->socket.read_handler = socket_read_handler;
98 c->socket.write_handler = NULL;
100 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
104 static void send_reply(struct client *c)
106 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
110 static void received_packet(struct client *c, byte *pkt, int len)
115 fbbuf_init_read(&fb, pkt, len, 0);
117 c->reply = json_new_object(c->json);
121 c->request = json_parse(c->json, &fb);
125 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
135 static int socket_write_handler(struct main_file *fi)
137 struct client *c = fi->data;
142 static int socket_read_handler(struct main_file *fi)
144 struct client *c = fi->data;
147 .iov_base = packet_buffer,
148 .iov_len = max_packet_size,
154 .msg_control = oob_data_buffer,
155 .msg_controllen = MAX_OOB_DATA_SIZE,
158 ssize_t len = recvmsg(fi->fd, &mh, 0);
161 if (errno != EAGAIN && errno != EINTR)
162 msg(L_ERROR, "Socket read: %m");
172 struct ucred *cred = NULL;
173 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
175 if (cm->cmsg_level == SOL_SOCKET)
177 if (cm->cmsg_type == SCM_RIGHTS)
179 // We are not interested in receiving file descriptor, but despite
180 // that they could be attached to the message. If it happens, simply
182 int *fdptr = (int *) CMSG_DATA(cm);
183 int nfd = cm->cmsg_len / sizeof(int);
184 for (int i=0; i<nfd; i++)
187 else if (cm->cmsg_type == SCM_CREDENTIALS)
189 ASSERT(cm->cmsg_len >= sizeof(cred));
190 cred = (struct ucred *) CMSG_DATA(cm);
197 msg(L_ERROR, "Dropping message with no credentials");
201 DBG("Got message from UID %d", (int) cred->uid);
204 fi->read_handler = NULL;
207 received_packet(c, packet_buffer, len);
211 static int listen_read_handler(struct main_file *fi)
213 struct sockaddr_un client;
214 socklen_t addr_len = sizeof(client);
216 int new_sk = accept(fi->fd, &client, &addr_len);
219 if (errno != EAGAIN && errno != EINTR)
220 msg(L_ERROR, "Socket accept: %m");
224 if (num_connections >= max_connections)
226 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
232 DBG("Accepted connection");
234 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
235 die("Cannot set O_NONBLOCK: %m");
237 struct mempool *mp = mp_new(4096);
238 struct client *c = mp_alloc_zero(mp, sizeof(*c));
240 c->json = json_new();
242 c->socket.fd = new_sk;
243 c->socket.read_handler = socket_read_handler;
245 file_add(&c->socket);
247 c->timer.handler = socket_timeout_handler;
249 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
254 static void init_socket(void)
256 packet_buffer = xmalloc(max_packet_size);
258 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
260 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
262 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
263 die("Cannot set O_NONBLOCK: %m");
265 struct sockaddr_un sun;
266 sun.sun_family = AF_UNIX;
267 if (strlen(socket_path) >= sizeof(sun.sun_path))
268 die("SocketPath too long");
269 strcpy(sun.sun_path, socket_path);
271 if (unlink(socket_path) < 0 && errno != ENOENT)
272 die("Cannot unlink old socket %s: %m", socket_path);
274 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
275 die("Cannot bind to %s: %m", socket_path);
277 if (listen(sk, 64) < 0)
281 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
282 die("setsockopt(SO_PASSCRED): %m");
284 listen_socket.fd = sk;
285 listen_socket.read_handler = listen_read_handler;
286 file_add(&listen_socket);
288 if (chmod(socket_path, 0666) < 0)
289 die("Cannot chmod socket: %m");
291 msg(L_INFO, "Listening on %s", socket_path);
294 static char *zone_commit(void *z_)
296 struct auth_zone *z = z_;
298 return "A zone must have a name";
302 static struct cf_section zone_config = {
303 CF_TYPE(struct auth_zone),
304 CF_COMMIT(zone_commit),
306 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
307 CF_STRING("Description", PTR_TO(struct auth_zone, desc)),
308 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
309 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
310 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
311 CF_UINT("MaxTempValidity", PTR_TO(struct auth_zone, max_temp_validity)),
316 static struct cf_section daemon_config = {
318 CF_STRING("SocketPath", &socket_path),
319 CF_UINT("MaxConnections", &max_connections),
320 CF_UINT("MaxPacketSize", &max_packet_size),
321 CF_LIST("Zone", &zone_list, &zone_config),
322 CF_STRING("Database", &database_name),
323 CF_STRING("TempKeyFile", &temp_key_file),
324 CF_STRING("LogStream", &log_stream_name),
329 static const struct opt_section options = {
331 OPT_HELP("A sub-authentication daemon."),
332 OPT_HELP("Usage: subauthd [options]"),
334 OPT_HELP("Options:"),
341 int main(int argc UNUSED, char **argv)
345 cf_def_file = CONFIG_DIR "/subauthd";
346 cf_declare_section("SubauthD", &daemon_config, 0);
347 opt_parse(&options, argv+1);
350 log_configured(log_stream_name);