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 #include "client/sock.h"
29 #include "utils/error.h"
30 #include "utils/strbuf.h"
31 #include "utils/proto.h"
33 #include <arpa/inet.h>
35 #include <errno.h>
36 #include <limits.h>
38 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
43 #include <unistd.h>
45 #include <sys/socket.h>
46 #include <sys/un.h>
48 /*
49 * private data types
50 */
52 struct sdb_client {
53 char *address;
54 int fd;
55 _Bool eof;
56 };
58 /*
59 * private helper functions
60 */
62 static int
63 connect_unixsock(sdb_client_t *client, const char *address)
64 {
65 struct sockaddr_un sa;
67 client->fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
68 if (client->fd < 0) {
69 char errbuf[1024];
70 sdb_log(SDB_LOG_ERR, "Failed to open socket: %s",
71 sdb_strerror(errno, errbuf, sizeof(errbuf)));
72 return -1;
73 }
75 sa.sun_family = AF_UNIX;
76 strncpy(sa.sun_path, address, sizeof(sa.sun_path));
77 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
79 if (connect(client->fd, (struct sockaddr *)&sa, sizeof(sa))) {
80 char errbuf[1024];
81 sdb_client_close(client);
82 sdb_log(SDB_LOG_ERR, "Failed to connect to '%s': %s",
83 sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
84 return -1;
85 }
86 return client->fd;
87 } /* connect_unixsock */
89 /*
90 * public API
91 */
93 sdb_client_t *
94 sdb_client_create(const char *address)
95 {
96 sdb_client_t *client;
98 if (! address)
99 return NULL;
101 client = malloc(sizeof(*client));
102 if (! client) {
103 sdb_log(SDB_LOG_ERR, "Out of memory");
104 return NULL;
105 }
106 memset(client, 0, sizeof(*client));
107 client->fd = -1;
108 client->eof = 1;
110 client->address = strdup(address);
111 if (! client->address) {
112 sdb_client_destroy(client);
113 sdb_log(SDB_LOG_ERR, "Out of memory");
114 return NULL;
115 }
117 return client;
118 } /* sdb_client_create */
120 void
121 sdb_client_destroy(sdb_client_t *client)
122 {
123 if (! client)
124 return;
126 sdb_client_close(client);
128 if (client->address)
129 free(client->address);
130 client->address = NULL;
132 free(client);
133 } /* sdb_client_destroy */
135 int
136 sdb_client_connect(sdb_client_t *client, const char *username)
137 {
138 sdb_strbuf_t *buf;
139 ssize_t status;
140 uint32_t rstatus;
142 if ((! client) || (! client->address))
143 return -1;
145 if (client->fd >= 0)
146 return -1;
148 if (!strncasecmp(client->address, "unix:", strlen("unix:")))
149 connect_unixsock(client, client->address + strlen("unix:"));
150 else if (*client->address == '/')
151 connect_unixsock(client, client->address);
152 else {
153 sdb_log(SDB_LOG_ERR, "Unknown address type: %s", client->address);
154 return -1;
155 }
157 if (client->fd < 0)
158 return -1;
159 client->eof = 0;
161 /* XXX */
162 if (! username)
163 username = "";
165 status = sdb_client_send(client, CONNECTION_STARTUP,
166 (uint32_t)strlen(username), username);
167 if (status < 0) {
168 char errbuf[1024];
169 sdb_client_close(client);
170 sdb_log(SDB_LOG_ERR, "Failed to send STARTUP message to server: %s",
171 sdb_strerror(errno, errbuf, sizeof(errbuf)));
172 return (int)status;
173 }
175 buf = sdb_strbuf_create(64);
176 rstatus = 0;
177 status = sdb_client_recv(client, &rstatus, buf);
178 if ((status > 0) && (rstatus == CONNECTION_OK))
179 return 0;
181 if (status < 0) {
182 char errbuf[1024];
183 sdb_log(SDB_LOG_ERR, "Failed to receive server response: %s",
184 sdb_strerror(errno, errbuf, sizeof(errbuf)));
185 }
186 else if (client->eof)
187 sdb_log(SDB_LOG_ERR, "Encountered end-of-file while waiting "
188 "for server response");
190 if (rstatus != CONNECTION_OK) {
191 sdb_log(SDB_LOG_ERR, "Access denied for user '%s'", username);
192 status = -((int)rstatus);
193 }
195 sdb_client_close(client);
196 sdb_strbuf_destroy(buf);
197 return (int)status;
198 } /* sdb_client_connect */
200 int
201 sdb_client_sockfd(sdb_client_t *client)
202 {
203 if (! client)
204 return -1;
205 return client->fd;
206 } /* sdb_client_sockfd */
208 void
209 sdb_client_close(sdb_client_t *client)
210 {
211 if (! client)
212 return;
214 close(client->fd);
215 client->fd = -1;
216 client->eof = 1;
217 } /* sdb_client_close */
219 ssize_t
220 sdb_client_send(sdb_client_t *client,
221 uint32_t cmd, uint32_t msg_len, const char *msg)
222 {
223 if ((! client) || (! client->fd))
224 return -1;
226 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
227 } /* sdb_client_send */
229 ssize_t
230 sdb_client_recv(sdb_client_t *client,
231 uint32_t *code, sdb_strbuf_t *buf)
232 {
233 uint32_t rstatus = UINT32_MAX;
234 uint32_t rlen = UINT32_MAX;
236 size_t total = 0;
237 size_t req = 2 * sizeof(uint32_t);
239 size_t data_offset = sdb_strbuf_len(buf);
241 if (code)
242 *code = UINT32_MAX;
244 if ((! client) || (! client->fd) || (! buf)) {
245 errno = EBADF;
246 return -1;
247 }
249 while (42) {
250 ssize_t status;
252 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
253 return -1;
255 errno = 0;
256 status = sdb_strbuf_read(buf, client->fd, req);
257 if (status < 0) {
258 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
259 continue;
260 return status;
261 }
262 else if (! status) {
263 client->eof = 1;
264 break;
265 }
267 total += (size_t)status;
269 if (total != req)
270 continue;
272 if (rstatus == UINT32_MAX) {
273 /* retrieve status and data len */
274 rstatus = sdb_proto_get_int(buf, data_offset);
275 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
277 if (! rlen)
278 break;
280 req = (size_t)rlen;
281 total = 0;
282 }
283 else /* finished reading data */
284 break;
285 }
287 if (total != req) {
288 /* unexpected EOF; clear partially read data */
289 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
290 return 0;
291 }
293 if (rstatus != UINT32_MAX)
294 /* remove status,len */
295 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
297 if (code)
298 *code = rstatus;
300 return (ssize_t)total;
301 } /* sdb_client_recv */
303 _Bool
304 sdb_client_eof(sdb_client_t *client)
305 {
306 if ((! client) || (client->fd < 0))
307 return 1;
308 return client->eof;
309 } /* sdb_client_eof */
311 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */