1 /*
2 * SysDB - src/client/sock.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "client/sock.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
35 #include "utils/proto.h"
37 #include <arpa/inet.h>
39 #include <errno.h>
40 #include <limits.h>
42 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
47 #include <unistd.h>
49 #include <sys/socket.h>
50 #include <sys/un.h>
52 /*
53 * private data types
54 */
56 struct sdb_client {
57 char *address;
58 int fd;
59 bool eof;
60 };
62 /*
63 * private helper functions
64 */
66 static int
67 connect_unixsock(sdb_client_t *client, const char *address)
68 {
69 struct sockaddr_un sa;
71 client->fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
72 if (client->fd < 0) {
73 char errbuf[1024];
74 sdb_log(SDB_LOG_ERR, "Failed to open socket: %s",
75 sdb_strerror(errno, errbuf, sizeof(errbuf)));
76 return -1;
77 }
79 sa.sun_family = AF_UNIX;
80 strncpy(sa.sun_path, address, sizeof(sa.sun_path));
81 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
83 if (connect(client->fd, (struct sockaddr *)&sa, sizeof(sa))) {
84 char errbuf[1024];
85 sdb_client_close(client);
86 sdb_log(SDB_LOG_ERR, "Failed to connect to '%s': %s",
87 sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
88 return -1;
89 }
90 return client->fd;
91 } /* connect_unixsock */
93 /*
94 * public API
95 */
97 sdb_client_t *
98 sdb_client_create(const char *address)
99 {
100 sdb_client_t *client;
102 if (! address)
103 return NULL;
105 client = malloc(sizeof(*client));
106 if (! client) {
107 sdb_log(SDB_LOG_ERR, "Out of memory");
108 return NULL;
109 }
110 memset(client, 0, sizeof(*client));
111 client->fd = -1;
112 client->eof = 1;
114 client->address = strdup(address);
115 if (! client->address) {
116 sdb_client_destroy(client);
117 sdb_log(SDB_LOG_ERR, "Out of memory");
118 return NULL;
119 }
121 return client;
122 } /* sdb_client_create */
124 void
125 sdb_client_destroy(sdb_client_t *client)
126 {
127 if (! client)
128 return;
130 sdb_client_close(client);
132 if (client->address)
133 free(client->address);
134 client->address = NULL;
136 free(client);
137 } /* sdb_client_destroy */
139 int
140 sdb_client_connect(sdb_client_t *client, const char *username)
141 {
142 sdb_strbuf_t *buf;
143 ssize_t status;
144 uint32_t rstatus;
146 if ((! client) || (! client->address))
147 return -1;
149 if (client->fd >= 0)
150 return -1;
152 if (!strncasecmp(client->address, "unix:", strlen("unix:")))
153 connect_unixsock(client, client->address + strlen("unix:"));
154 else if (*client->address == '/')
155 connect_unixsock(client, client->address);
156 else {
157 sdb_log(SDB_LOG_ERR, "Unknown address type: %s", client->address);
158 return -1;
159 }
161 if (client->fd < 0)
162 return -1;
163 client->eof = 0;
165 /* XXX */
166 if (! username)
167 username = "";
169 status = sdb_client_send(client, SDB_CONNECTION_STARTUP,
170 (uint32_t)strlen(username), username);
171 if (status < 0) {
172 char errbuf[1024];
173 sdb_client_close(client);
174 sdb_log(SDB_LOG_ERR, "Failed to send STARTUP message to server: %s",
175 sdb_strerror(errno, errbuf, sizeof(errbuf)));
176 return (int)status;
177 }
179 buf = sdb_strbuf_create(64);
180 rstatus = 0;
181 status = sdb_client_recv(client, &rstatus, buf);
182 if ((status > 0) && (rstatus == SDB_CONNECTION_OK)) {
183 sdb_strbuf_destroy(buf);
184 return 0;
185 }
187 if (status < 0) {
188 char errbuf[1024];
189 sdb_log(SDB_LOG_ERR, "Failed to receive server response: %s",
190 sdb_strerror(errno, errbuf, sizeof(errbuf)));
191 }
192 else if (client->eof)
193 sdb_log(SDB_LOG_ERR, "Encountered end-of-file while waiting "
194 "for server response");
196 if (rstatus == SDB_CONNECTION_ERROR) {
197 sdb_log(SDB_LOG_ERR, "Access denied for user '%s': %s",
198 username, sdb_strbuf_string(buf));
199 status = -((int)rstatus);
200 }
201 else if (rstatus != SDB_CONNECTION_OK) {
202 sdb_log(SDB_LOG_ERR, "Received unsupported authentication request "
203 "(status %d) during startup", (int)rstatus);
204 status = -((int)rstatus);
205 }
207 sdb_client_close(client);
208 sdb_strbuf_destroy(buf);
209 return (int)status;
210 } /* sdb_client_connect */
212 int
213 sdb_client_sockfd(sdb_client_t *client)
214 {
215 if (! client)
216 return -1;
217 return client->fd;
218 } /* sdb_client_sockfd */
220 int
221 sdb_client_shutdown(sdb_client_t *client, int how)
222 {
223 if (! client) {
224 errno = ENOTSOCK;
225 return -1;
226 }
228 if (client->fd < 0) {
229 errno = EBADF;
230 return -1;
231 }
233 return shutdown(client->fd, how);
234 } /* sdb_client_shutdown */
236 void
237 sdb_client_close(sdb_client_t *client)
238 {
239 if (! client)
240 return;
242 close(client->fd);
243 client->fd = -1;
244 client->eof = 1;
245 } /* sdb_client_close */
247 ssize_t
248 sdb_client_send(sdb_client_t *client,
249 uint32_t cmd, uint32_t msg_len, const char *msg)
250 {
251 char buf[2 * sizeof(uint32_t) + msg_len];
253 if ((! client) || (! client->fd))
254 return -1;
255 if (sdb_proto_marshal(buf, sizeof(buf), cmd, msg_len, msg) < 0)
256 return -1;
258 return sdb_proto_send(client->fd, sizeof(buf), buf);
259 } /* sdb_client_send */
261 ssize_t
262 sdb_client_recv(sdb_client_t *client,
263 uint32_t *code, sdb_strbuf_t *buf)
264 {
265 uint32_t rstatus = UINT32_MAX;
266 uint32_t rlen = UINT32_MAX;
268 size_t total = 0;
269 size_t req = 2 * sizeof(uint32_t);
271 size_t data_offset = sdb_strbuf_len(buf);
273 if (code)
274 *code = UINT32_MAX;
276 if ((! client) || (! client->fd) || (! buf)) {
277 errno = EBADF;
278 return -1;
279 }
281 while (42) {
282 ssize_t status;
284 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
285 return -1;
287 errno = 0;
288 status = sdb_strbuf_read(buf, client->fd, req);
289 if (status < 0) {
290 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
291 continue;
292 return status;
293 }
294 else if (! status) {
295 client->eof = 1;
296 break;
297 }
299 total += (size_t)status;
301 if (total != req)
302 continue;
304 if (rstatus == UINT32_MAX) {
305 /* retrieve status and data len */
306 rstatus = sdb_proto_get_int(buf, data_offset);
307 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
309 if (! rlen)
310 break;
312 req = (size_t)rlen;
313 total = 0;
314 }
315 else /* finished reading data */
316 break;
317 }
319 if (total != req) {
320 /* unexpected EOF; clear partially read data */
321 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
322 return 0;
323 }
325 if (rstatus != UINT32_MAX)
326 /* remove status,len */
327 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
329 if (code)
330 *code = rstatus;
332 return (ssize_t)total;
333 } /* sdb_client_recv */
335 bool
336 sdb_client_eof(sdb_client_t *client)
337 {
338 if ((! client) || (client->fd < 0))
339 return 1;
340 return client->eof;
341 } /* sdb_client_eof */
343 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */