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";
31 static struct main_file listen_socket;
32 static uint num_connections;
34 static byte packet_buffer[MAX_PACKET_SIZE];
35 static byte oob_data_buffer[MAX_OOB_DATA_SIZE];
37 static int socket_read_handler(struct main_file *fi);
38 static int socket_write_handler(struct main_file *fi);
40 static void client_close(struct client *c)
42 DBG("Closing connection");
51 static void socket_timeout_handler(struct main_timer *tm)
53 struct client *c = tm->data;
54 msg(L_INFO, "Client timeout");
58 static void try_send_reply(struct client *c)
61 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
65 json_write(c->json, &fb, c->reply);
69 msg(L_ERROR, "Unable to construct reply, it is probably too long");
70 fbbuf_init_write(&fb, packet_buffer, MAX_PACKET_SIZE);
71 bputs(&fb, "{ \"error\": \"Reply too long\" }\n");
75 int len = fbbuf_count_written(&fb);
76 DBG("Sending reply of %d bytes", len);
77 if (send(c->socket.fd, packet_buffer, len, 0) < 0)
79 if (errno == EAGAIN || errno == EINTR)
81 DBG("Postponed send");
82 c->socket.write_handler = socket_write_handler;
85 msg(L_ERROR, "Client write error: %m");
91 c->socket.read_handler = socket_read_handler;
92 c->socket.write_handler = NULL;
94 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
98 static void send_reply(struct client *c)
100 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
104 static void received_packet(struct client *c, byte *pkt, int len)
109 fbbuf_init_read(&fb, pkt, len, 0);
111 c->reply = json_new_object(c->json);
115 c->request = json_parse(c->json, &fb);
119 json_object_set(c->reply, "error", json_new_string_ref(c->json, "Parse error"));
129 static int socket_write_handler(struct main_file *fi)
131 struct client *c = fi->data;
136 static int socket_read_handler(struct main_file *fi)
138 struct client *c = fi->data;
141 .iov_base = packet_buffer,
142 .iov_len = MAX_PACKET_SIZE,
148 .msg_control = oob_data_buffer,
149 .msg_controllen = MAX_OOB_DATA_SIZE,
152 ssize_t len = recvmsg(fi->fd, &mh, 0);
155 if (errno != EAGAIN && errno != EINTR)
156 msg(L_ERROR, "Socket read: %m");
166 struct ucred *cred = NULL;
167 for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm))
169 if (cm->cmsg_level == SOL_SOCKET)
171 if (cm->cmsg_type == SCM_RIGHTS)
173 // We are not interested in receiving file descriptor, but despite
174 // that they could be attached to the message. If it happens, simply
176 int *fdptr = (int *) CMSG_DATA(cm);
177 int nfd = cm->cmsg_len / sizeof(int);
178 for (int i=0; i<nfd; i++)
181 else if (cm->cmsg_type == SCM_CREDENTIALS)
183 ASSERT(cm->cmsg_len >= sizeof(cred));
184 cred = (struct ucred *) CMSG_DATA(cm);
191 msg(L_ERROR, "Dropping message with no credentials");
195 DBG("Got message from UID %d", (int) cred->uid);
198 fi->read_handler = NULL;
201 received_packet(c, packet_buffer, len);
205 static int listen_read_handler(struct main_file *fi)
207 struct sockaddr_un client;
208 socklen_t addr_len = sizeof(client);
210 int new_sk = accept(fi->fd, &client, &addr_len);
213 if (errno != EAGAIN && errno != EINTR)
214 msg(L_ERROR, "Socket accept: %m");
218 if (num_connections >= max_connections)
220 msg(L_WARN, "Too many connections (you might need to increase MaxConnections)");
226 DBG("Accepted connection");
228 if (fcntl(new_sk, F_SETFL, fcntl(new_sk, F_GETFL) | O_NONBLOCK) < 0)
229 die("Cannot set O_NONBLOCK: %m");
231 struct client *c = xmalloc_zero(sizeof(*c));
232 c->json = json_new();
234 c->socket.fd = new_sk;
235 c->socket.read_handler = socket_read_handler;
237 file_add(&c->socket);
239 c->timer.handler = socket_timeout_handler;
241 timer_add_rel(&c->timer, SOCKET_TIMEOUT);
246 static void init_socket(void)
248 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
250 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
252 if (fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) < 0)
253 die("Cannot set O_NONBLOCK: %m");
255 struct sockaddr_un sun;
256 sun.sun_family = AF_UNIX;
257 if (strlen(socket_path) >= sizeof(sun.sun_path))
258 die("SocketPath too long");
259 strcpy(sun.sun_path, socket_path);
261 if (unlink(socket_path) < 0 && errno != ENOENT)
262 die("Cannot unlink old socket %s: %m", socket_path);
264 if (bind(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
265 die("Cannot bind to %s: %m", socket_path);
267 if (listen(sk, 64) < 0)
271 if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
272 die("setsockopt(SO_PASSCRED): %m");
274 listen_socket.fd = sk;
275 listen_socket.read_handler = listen_read_handler;
276 file_add(&listen_socket);
278 msg(L_INFO, "Listening on %s", socket_path);
281 static char *zone_commit(void *z_)
283 struct auth_zone *z = z_;
285 return "A zone must have a name";
289 static struct cf_section zone_config = {
290 CF_TYPE(struct auth_zone),
291 CF_COMMIT(zone_commit),
293 CF_STRING("Name", PTR_TO(struct auth_zone, name)),
294 CF_STRING("Description", PTR_TO(struct auth_zone, desc)),
295 CF_UINT("AutoCreateAcct", PTR_TO(struct auth_zone, auto_create_acct)),
296 CF_UINT("AllowPasswd", PTR_TO(struct auth_zone, allow_passwd)),
297 CF_UINT("AllowTokens", PTR_TO(struct auth_zone, allow_tokens)),
298 CF_UINT("MaxTempValidity", PTR_TO(struct auth_zone, max_temp_validity)),
303 static struct cf_section daemon_config = {
305 CF_STRING("SocketPath", &socket_path),
306 CF_UINT("MaxConnections", &max_connections),
307 CF_LIST("Zone", &zone_list, &zone_config),
308 CF_STRING("Database", &database_name),
309 CF_STRING("TempKeyFile", &temp_key_file),
314 static const struct opt_section options = {
316 OPT_HELP("A sub-authentication daemon."),
317 OPT_HELP("Usage: subauthd [options]"),
319 OPT_HELP("Options:"),
326 int main(int argc UNUSED, char **argv)
330 cf_def_file = CONFIG_DIR "/subauthd";
331 cf_declare_section("SubauthD", &daemon_config, 0);
332 opt_parse(&options, argv+1);